]> git.proxmox.com Git - mirror_ovs.git/blame - ofproto/ofproto.c
ofproto: Additional simplifications.
[mirror_ovs.git] / ofproto / ofproto.c
CommitLineData
064af421 1/*
c5f81b20 2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 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>
19#include "ofproto.h"
20#include <errno.h>
21#include <inttypes.h>
064af421
BP
22#include <stdbool.h>
23#include <stdlib.h>
448a4b2f 24#include <unistd.h>
52a90c29 25#include "bitmap.h"
10a24935 26#include "byte-order.h"
064af421 27#include "classifier.h"
da4a6191 28#include "connectivity.h"
19a87e36 29#include "connmgr.h"
064af421 30#include "coverage.h"
4f2cad2c 31#include "dynamic-string.h"
ca0f572c
BP
32#include "hash.h"
33#include "hmap.h"
254750ce 34#include "meta-flow.h"
064af421 35#include "netdev.h"
09246b99 36#include "nx-match.h"
f25d0cf3 37#include "ofp-actions.h"
90bf1e07 38#include "ofp-errors.h"
982697a4 39#include "ofp-msgs.h"
064af421 40#include "ofp-print.h"
fa37b408 41#include "ofp-util.h"
064af421 42#include "ofpbuf.h"
5bee6e26 43#include "ofproto-provider.h"
064af421
BP
44#include "openflow/nicira-ext.h"
45#include "openflow/openflow.h"
06a0f3e2 46#include "ovs-rcu.h"
064af421
BP
47#include "packets.h"
48#include "pinsched.h"
49#include "pktbuf.h"
50#include "poll-loop.h"
254750ce 51#include "random.h"
da4a6191 52#include "seq.h"
064af421 53#include "shash.h"
0d085684 54#include "simap.h"
e8f9a7bb 55#include "smap.h"
b3c01ed3 56#include "sset.h"
064af421 57#include "timeval.h"
c4617b3c 58#include "unaligned.h"
4f2cad2c 59#include "unixctl.h"
5136ce49 60#include "vlog.h"
777af88d 61#include "bundles.h"
064af421 62
d98e6007 63VLOG_DEFINE_THIS_MODULE(ofproto);
064af421 64
d76f09ea 65COVERAGE_DEFINE(ofproto_flush);
d76f09ea
BP
66COVERAGE_DEFINE(ofproto_packet_out);
67COVERAGE_DEFINE(ofproto_queue_req);
68COVERAGE_DEFINE(ofproto_recv_openflow);
69COVERAGE_DEFINE(ofproto_reinit_ports);
d76f09ea
BP
70COVERAGE_DEFINE(ofproto_update_port);
71
f017d986
JR
72/* Default fields to use for prefix tries in each flow table, unless something
73 * else is configured. */
74const enum mf_field_id default_prefix_fields[2] =
75 { MFF_IPV4_DST, MFF_IPV4_SRC };
76
d0918789
BP
77/* oftable. */
78static void oftable_init(struct oftable *);
79static void oftable_destroy(struct oftable *);
878ae780 80
254750ce
BP
81static void oftable_set_name(struct oftable *, const char *name);
82
834fe5cb
BP
83static enum ofperr evict_rules_from_table(struct oftable *,
84 unsigned int extra_space)
85 OVS_REQUIRES(ofproto_mutex);
254750ce
BP
86static void oftable_disable_eviction(struct oftable *);
87static void oftable_enable_eviction(struct oftable *,
88 const struct mf_subfield *fields,
89 size_t n_fields);
90
15aaf599 91static void oftable_remove_rule(struct rule *rule) OVS_REQUIRES(ofproto_mutex);
8b81d1ef 92static void oftable_remove_rule__(struct ofproto *, struct rule *)
15aaf599 93 OVS_REQUIRES(ofproto_mutex);
064af421 94
254750ce
BP
95/* A set of rules within a single OpenFlow table (oftable) that have the same
96 * values for the oftable's eviction_fields. A rule to be evicted, when one is
97 * needed, is taken from the eviction group that contains the greatest number
98 * of rules.
99 *
100 * An oftable owns any number of eviction groups, each of which contains any
101 * number of rules.
102 *
103 * Membership in an eviction group is imprecise, based on the hash of the
104 * oftable's eviction_fields (in the eviction_group's id_node.hash member).
105 * That is, if two rules have different eviction_fields, but those
106 * eviction_fields hash to the same value, then they will belong to the same
107 * eviction_group anyway.
108 *
109 * (When eviction is not enabled on an oftable, we don't track any eviction
110 * groups, to save time and space.) */
111struct eviction_group {
112 struct hmap_node id_node; /* In oftable's "eviction_groups_by_id". */
113 struct heap_node size_node; /* In oftable's "eviction_groups_by_size". */
114 struct heap rules; /* Contains "struct rule"s. */
115};
116
3d900aa7
BP
117static bool choose_rule_to_evict(struct oftable *table, struct rule **rulep)
118 OVS_REQUIRES(ofproto_mutex);
3d900aa7
BP
119static uint32_t rule_eviction_priority(struct ofproto *ofproto, struct rule *)
120 OVS_REQUIRES(ofproto_mutex);;
121static void eviction_group_add_rule(struct rule *)
122 OVS_REQUIRES(ofproto_mutex);
123static void eviction_group_remove_rule(struct rule *)
124 OVS_REQUIRES(ofproto_mutex);
f29152ca 125
a9b22b7f
BP
126/* Criteria that flow_mod and other operations use for selecting rules on
127 * which to operate. */
128struct rule_criteria {
129 /* An OpenFlow table or 255 for all tables. */
130 uint8_t table_id;
131
132 /* OpenFlow matching criteria. Interpreted different in "loose" way by
133 * collect_rules_loose() and "strict" way by collect_rules_strict(), as
134 * defined in the OpenFlow spec. */
135 struct cls_rule cr;
136
137 /* Matching criteria for the OpenFlow cookie. Consider a bit B in a rule's
138 * cookie and the corresponding bits C in 'cookie' and M in 'cookie_mask'.
139 * The rule will not be selected if M is 1 and B != C. */
140 ovs_be64 cookie;
141 ovs_be64 cookie_mask;
142
143 /* Selection based on actions within a rule:
144 *
145 * If out_port != OFPP_ANY, selects only rules that output to out_port.
146 * If out_group != OFPG_ALL, select only rules that output to out_group. */
147 ofp_port_t out_port;
148 uint32_t out_group;
dd51dae2
BP
149
150 /* If true, collects only rules that are modifiable. */
151 bool include_hidden;
152 bool include_readonly;
a9b22b7f
BP
153};
154
155static void rule_criteria_init(struct rule_criteria *, uint8_t table_id,
156 const struct match *match,
157 unsigned int priority,
158 ovs_be64 cookie, ovs_be64 cookie_mask,
159 ofp_port_t out_port, uint32_t out_group);
dd51dae2
BP
160static void rule_criteria_require_rw(struct rule_criteria *,
161 bool can_write_readonly);
a9b22b7f
BP
162static void rule_criteria_destroy(struct rule_criteria *);
163
15aaf599
BP
164/* A packet that needs to be passed to rule_execute().
165 *
166 * (We can't do this immediately from ofopgroup_complete() because that holds
167 * ofproto_mutex, which rule_execute() needs released.) */
35412852
BP
168struct rule_execute {
169 struct list list_node; /* In struct ofproto's "rule_executes" list. */
170 struct rule *rule; /* Owns a reference to the rule. */
171 ofp_port_t in_port;
172 struct ofpbuf *packet; /* Owns the packet. */
173};
174
15aaf599 175static void run_rule_executes(struct ofproto *) OVS_EXCLUDED(ofproto_mutex);
35412852
BP
176static void destroy_rule_executes(struct ofproto *);
177
d0918789 178/* ofport. */
15aaf599 179static void ofport_destroy__(struct ofport *) OVS_EXCLUDED(ofproto_mutex);
d0918789
BP
180static void ofport_destroy(struct ofport *);
181
182static void update_port(struct ofproto *, const char *devname);
183static int init_ports(struct ofproto *);
184static void reinit_ports(struct ofproto *);
7ee20df1 185
fdcea803
GS
186static long long int ofport_get_usage(const struct ofproto *,
187 ofp_port_t ofp_port);
188static void ofport_set_usage(struct ofproto *, ofp_port_t ofp_port,
189 long long int last_used);
865b26a4 190static void ofport_remove_usage(struct ofproto *, ofp_port_t ofp_port);
fdcea803
GS
191
192/* Ofport usage.
193 *
194 * Keeps track of the currently used and recently used ofport values and is
195 * used to prevent immediate recycling of ofport values. */
196struct ofport_usage {
197 struct hmap_node hmap_node; /* In struct ofproto's "ofport_usage" hmap. */
198 ofp_port_t ofp_port; /* OpenFlow port number. */
199 long long int last_used; /* Last time the 'ofp_port' was used. LLONG_MAX
200 represents in-use ofports. */
201};
202
254750ce 203/* rule. */
254750ce 204static void ofproto_rule_send_removed(struct rule *, uint8_t reason);
834fe5cb 205static bool rule_is_readonly(const struct rule *);
254750ce 206
baae3d02
BP
207/* The source of a flow_mod request, in the code that processes flow_mods.
208 *
209 * A flow table modification request can be generated externally, via OpenFlow,
210 * or internally through a function call. This structure indicates the source
211 * of an OpenFlow-generated flow_mod. For an internal flow_mod, it isn't
212 * meaningful and thus supplied as NULL. */
213struct flow_mod_requester {
214 struct ofconn *ofconn; /* Connection on which flow_mod arrived. */
834fe5cb 215 ovs_be32 xid; /* OpenFlow xid of flow_mod request. */
baae3d02
BP
216};
217
d0918789 218/* OpenFlow. */
baae3d02
BP
219static enum ofperr add_flow(struct ofproto *, struct ofputil_flow_mod *,
220 const struct flow_mod_requester *);
221
222static enum ofperr modify_flows__(struct ofproto *, struct ofputil_flow_mod *,
223 const struct rule_collection *,
224 const struct flow_mod_requester *);
834fe5cb
BP
225static void delete_flow__(struct rule *, enum ofp_flow_removed_reason,
226 const struct flow_mod_requester *)
15aaf599 227 OVS_REQUIRES(ofproto_mutex);
834fe5cb
BP
228
229static enum ofperr send_buffered_packet(struct ofconn *, uint32_t buffer_id,
230 struct rule *)
231 OVS_REQUIRES(ofproto_mutex);
232
7e9f8266
BP
233static bool ofproto_group_exists__(const struct ofproto *ofproto,
234 uint32_t group_id)
235 OVS_REQ_RDLOCK(ofproto->groups_rwlock);
84d68566
SH
236static bool ofproto_group_exists(const struct ofproto *ofproto,
237 uint32_t group_id)
7e9f8266 238 OVS_EXCLUDED(ofproto->groups_rwlock);
7395c052 239static enum ofperr add_group(struct ofproto *, struct ofputil_group_mod *);
b20f4073 240static void handle_openflow(struct ofconn *, const struct ofpbuf *);
baae3d02 241static enum ofperr handle_flow_mod__(struct ofproto *,
e3b56933 242 struct ofputil_flow_mod *,
baae3d02 243 const struct flow_mod_requester *)
15aaf599 244 OVS_EXCLUDED(ofproto_mutex);
65e0be10
BP
245static void calc_duration(long long int start, long long int now,
246 uint32_t *sec, uint32_t *nsec);
f29152ca 247
d0918789
BP
248/* ofproto. */
249static uint64_t pick_datapath_id(const struct ofproto *);
250static uint64_t pick_fallback_dpid(void);
251static void ofproto_destroy__(struct ofproto *);
ada3428f 252static void update_mtu(struct ofproto *, struct ofport *);
6b3f7f02 253static void meter_delete(struct ofproto *, uint32_t first, uint32_t last);
062fea06 254static void meter_insert_rule(struct rule *);
f29152ca 255
d0918789 256/* unixctl. */
abe529af 257static void ofproto_unixctl_init(void);
f29152ca 258
abe529af
BP
259/* All registered ofproto classes, in probe order. */
260static const struct ofproto_class **ofproto_classes;
261static size_t n_ofproto_classes;
262static size_t allocated_ofproto_classes;
7aa697dd 263
15aaf599
BP
264/* Global lock that protects all flow table operations. */
265struct ovs_mutex ofproto_mutex = OVS_MUTEX_INITIALIZER;
abe7b10f 266
e79a6c83 267unsigned ofproto_flow_limit = OFPROTO_FLOW_LIMIT_DEFAULT;
72310b04 268unsigned ofproto_max_idle = OFPROTO_MAX_IDLE_DEFAULT;
380f49c4 269
e79a6c83 270size_t n_handlers, n_revalidators;
6567010f 271
f797957a 272/* Map from datapath name to struct ofproto, for use by unixctl commands. */
abe529af 273static struct hmap all_ofprotos = HMAP_INITIALIZER(&all_ofprotos);
ebe482fd 274
e1b1d06a
JP
275/* Initial mappings of port to OpenFlow number mappings. */
276static struct shash init_ofp_ports = SHASH_INITIALIZER(&init_ofp_ports);
277
abe529af 278static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
f29152ca 279
40358701
GS
280/* The default value of true waits for flow restore. */
281static bool flow_restore_wait = true;
282
b0408fca
JP
283/* Must be called to initialize the ofproto library.
284 *
285 * The caller may pass in 'iface_hints', which contains an shash of
286 * "iface_hint" elements indexed by the interface's name. The provider
287 * may use these hints to describe the startup configuration in order to
288 * reinitialize its state. The caller owns the provided data, so a
289 * provider will make copies of anything required. An ofproto provider
290 * will remove any existing state that is not described by the hint, and
291 * may choose to remove it all. */
292void
293ofproto_init(const struct shash *iface_hints)
abe529af 294{
e1b1d06a 295 struct shash_node *node;
b0408fca 296 size_t i;
f29152ca 297
b0408fca
JP
298 ofproto_class_register(&ofproto_dpif_class);
299
e1b1d06a
JP
300 /* Make a local copy, since we don't own 'iface_hints' elements. */
301 SHASH_FOR_EACH(node, iface_hints) {
302 const struct iface_hint *orig_hint = node->data;
303 struct iface_hint *new_hint = xmalloc(sizeof *new_hint);
304 const char *br_type = ofproto_normalize_type(orig_hint->br_type);
305
306 new_hint->br_name = xstrdup(orig_hint->br_name);
307 new_hint->br_type = xstrdup(br_type);
308 new_hint->ofp_port = orig_hint->ofp_port;
309
310 shash_add(&init_ofp_ports, node->name, new_hint);
311 }
312
b0408fca 313 for (i = 0; i < n_ofproto_classes; i++) {
e1b1d06a 314 ofproto_classes[i]->init(&init_ofp_ports);
abe529af
BP
315 }
316}
f29152ca 317
abe529af
BP
318/* 'type' should be a normalized datapath type, as returned by
319 * ofproto_normalize_type(). Returns the corresponding ofproto_class
320 * structure, or a null pointer if there is none registered for 'type'. */
321static const struct ofproto_class *
322ofproto_class_find__(const char *type)
323{
324 size_t i;
f29152ca 325
abe529af
BP
326 for (i = 0; i < n_ofproto_classes; i++) {
327 const struct ofproto_class *class = ofproto_classes[i];
328 struct sset types;
329 bool found;
064af421 330
abe529af
BP
331 sset_init(&types);
332 class->enumerate_types(&types);
333 found = sset_contains(&types, type);
334 sset_destroy(&types);
bcf84111 335
abe529af
BP
336 if (found) {
337 return class;
338 }
339 }
340 VLOG_WARN("unknown datapath type %s", type);
341 return NULL;
342}
064af421 343
abe529af
BP
344/* Registers a new ofproto class. After successful registration, new ofprotos
345 * of that type can be created using ofproto_create(). */
346int
347ofproto_class_register(const struct ofproto_class *new_class)
348{
349 size_t i;
7aa697dd 350
abe529af
BP
351 for (i = 0; i < n_ofproto_classes; i++) {
352 if (ofproto_classes[i] == new_class) {
353 return EEXIST;
354 }
355 }
064af421 356
abe529af
BP
357 if (n_ofproto_classes >= allocated_ofproto_classes) {
358 ofproto_classes = x2nrealloc(ofproto_classes,
359 &allocated_ofproto_classes,
360 sizeof *ofproto_classes);
361 }
362 ofproto_classes[n_ofproto_classes++] = new_class;
363 return 0;
364}
064af421 365
abe529af
BP
366/* Unregisters a datapath provider. 'type' must have been previously
367 * registered and not currently be in use by any ofprotos. After
368 * unregistration new datapaths of that type cannot be opened using
369 * ofproto_create(). */
370int
371ofproto_class_unregister(const struct ofproto_class *class)
372{
373 size_t i;
76ce9432 374
abe529af
BP
375 for (i = 0; i < n_ofproto_classes; i++) {
376 if (ofproto_classes[i] == class) {
377 for (i++; i < n_ofproto_classes; i++) {
378 ofproto_classes[i - 1] = ofproto_classes[i];
379 }
380 n_ofproto_classes--;
381 return 0;
382 }
383 }
384 VLOG_WARN("attempted to unregister an ofproto class that is not "
385 "registered");
386 return EAFNOSUPPORT;
387}
4a4cdb3b 388
f79e673f
BP
389/* Clears 'types' and enumerates all registered ofproto types into it. The
390 * caller must first initialize the sset. */
391void
392ofproto_enumerate_types(struct sset *types)
393{
abe529af 394 size_t i;
064af421 395
c799c306 396 sset_clear(types);
abe529af
BP
397 for (i = 0; i < n_ofproto_classes; i++) {
398 ofproto_classes[i]->enumerate_types(types);
399 }
f79e673f 400}
064af421 401
f79e673f
BP
402/* Returns the fully spelled out name for the given ofproto 'type'.
403 *
404 * Normalized type string can be compared with strcmp(). Unnormalized type
405 * string might be the same even if they have different spellings. */
406const char *
407ofproto_normalize_type(const char *type)
408{
abe529af 409 return type && type[0] ? type : "system";
f79e673f 410}
064af421 411
f79e673f
BP
412/* Clears 'names' and enumerates the names of all known created ofprotos with
413 * the given 'type'. The caller must first initialize the sset. Returns 0 if
414 * successful, otherwise a positive errno value.
415 *
416 * Some kinds of datapaths might not be practically enumerable. This is not
417 * considered an error. */
418int
419ofproto_enumerate_names(const char *type, struct sset *names)
420{
abe529af
BP
421 const struct ofproto_class *class = ofproto_class_find__(type);
422 return class ? class->enumerate_names(type, names) : EAFNOSUPPORT;
239ad7d1 423}
7aa697dd 424
064af421 425int
abe529af 426ofproto_create(const char *datapath_name, const char *datapath_type,
064af421
BP
427 struct ofproto **ofprotop)
428{
abe529af
BP
429 const struct ofproto_class *class;
430 struct ofproto *ofproto;
064af421 431 int error;
c2f0373a 432 int i;
064af421
BP
433
434 *ofprotop = NULL;
435
7aa697dd
BP
436 ofproto_unixctl_init();
437
abe529af
BP
438 datapath_type = ofproto_normalize_type(datapath_type);
439 class = ofproto_class_find__(datapath_type);
440 if (!class) {
441 VLOG_WARN("could not create datapath %s of unknown type %s",
442 datapath_name, datapath_type);
443 return EAFNOSUPPORT;
064af421
BP
444 }
445
abe529af
BP
446 ofproto = class->alloc();
447 if (!ofproto) {
448 VLOG_ERR("failed to allocate datapath %s of type %s",
449 datapath_name, datapath_type);
450 return ENOMEM;
451 }
452
453 /* Initialize. */
abe7b10f 454 ovs_mutex_lock(&ofproto_mutex);
abe529af
BP
455 memset(ofproto, 0, sizeof *ofproto);
456 ofproto->ofproto_class = class;
457 ofproto->name = xstrdup(datapath_name);
458 ofproto->type = xstrdup(datapath_type);
459 hmap_insert(&all_ofprotos, &ofproto->hmap_node,
460 hash_string(ofproto->name, 0));
461 ofproto->datapath_id = 0;
8402c74b 462 ofproto->forward_bpdu = false;
abe529af 463 ofproto->fallback_dpid = pick_fallback_dpid();
061bfea4
BP
464 ofproto->mfr_desc = NULL;
465 ofproto->hw_desc = NULL;
466 ofproto->sw_desc = NULL;
467 ofproto->serial_desc = NULL;
468 ofproto->dp_desc = NULL;
7257b535 469 ofproto->frag_handling = OFPC_FRAG_NORMAL;
abe529af 470 hmap_init(&ofproto->ports);
fdcea803 471 hmap_init(&ofproto->ofport_usage);
abe529af 472 shash_init(&ofproto->port_by_name);
e1b1d06a 473 simap_init(&ofproto->ofp_requests);
430dbb14 474 ofproto->max_ports = ofp_to_u16(OFPP_MAX);
448c2fa8 475 ofproto->eviction_group_timer = LLONG_MIN;
6c1491fb
BP
476 ofproto->tables = NULL;
477 ofproto->n_tables = 0;
98eaac36 478 hindex_init(&ofproto->cookies);
e503cc19 479 list_init(&ofproto->expirable);
abe529af 480 ofproto->connmgr = connmgr_create(ofproto, datapath_name, datapath_name);
35412852 481 guarded_list_init(&ofproto->rule_executes);
52a90c29
BP
482 ofproto->vlan_bitmap = NULL;
483 ofproto->vlans_changed = false;
ada3428f 484 ofproto->min_mtu = INT_MAX;
7395c052
NZ
485 ovs_rwlock_init(&ofproto->groups_rwlock);
486 hmap_init(&ofproto->groups);
abe7b10f 487 ovs_mutex_unlock(&ofproto_mutex);
7cb279c2
SH
488 ofproto->ogf.capabilities = OFPGFC_CHAINING | OFPGFC_SELECT_LIVENESS |
489 OFPGFC_SELECT_WEIGHT;
f4fb341b 490 ofproto->ogf.max_groups[OFPGT11_ALL] = OFPG_MAX;
fe7e5749 491 ofproto->ogf.max_groups[OFPGT11_SELECT] = OFPG_MAX;
f4fb341b 492 ofproto->ogf.max_groups[OFPGT11_INDIRECT] = OFPG_MAX;
dd8cd4b4 493 ofproto->ogf.max_groups[OFPGT11_FF] = OFPG_MAX;
ddc627ad 494 ofproto->ogf.actions[0] =
f0e7025f
BP
495 (1 << OFPAT11_OUTPUT) |
496 (1 << OFPAT11_COPY_TTL_OUT) |
497 (1 << OFPAT11_COPY_TTL_IN) |
498 (1 << OFPAT11_SET_MPLS_TTL) |
499 (1 << OFPAT11_DEC_MPLS_TTL) |
500 (1 << OFPAT11_PUSH_VLAN) |
501 (1 << OFPAT11_POP_VLAN) |
502 (1 << OFPAT11_PUSH_MPLS) |
503 (1 << OFPAT11_POP_MPLS) |
504 (1 << OFPAT11_SET_QUEUE) |
505 (1 << OFPAT11_GROUP) |
506 (1 << OFPAT11_SET_NW_TTL) |
507 (1 << OFPAT11_DEC_NW_TTL) |
508 (1 << OFPAT12_SET_FIELD);
509/* not supported:
510 * (1 << OFPAT13_PUSH_PBB) |
511 * (1 << OFPAT13_POP_PBB) */
abe529af 512
0f5f95a9 513 error = ofproto->ofproto_class->construct(ofproto);
19a87e36 514 if (error) {
abe529af 515 VLOG_ERR("failed to open datapath %s: %s",
10a89ef0 516 datapath_name, ovs_strerror(error));
abe529af 517 ofproto_destroy__(ofproto);
19a87e36
BP
518 return error;
519 }
073e2a6f 520
c2f0373a 521 /* Check that hidden tables, if any, are at the end. */
cb22974d 522 ovs_assert(ofproto->n_tables);
c2f0373a
BP
523 for (i = 0; i + 1 < ofproto->n_tables; i++) {
524 enum oftable_flags flags = ofproto->tables[i].flags;
525 enum oftable_flags next_flags = ofproto->tables[i + 1].flags;
526
cb22974d 527 ovs_assert(!(flags & OFTABLE_HIDDEN) || next_flags & OFTABLE_HIDDEN);
c2f0373a 528 }
19a87e36 529
abe529af 530 ofproto->datapath_id = pick_datapath_id(ofproto);
abe529af 531 init_ports(ofproto);
7aa697dd 532
9cae45dc
JR
533 /* Initialize meters table. */
534 if (ofproto->ofproto_class->meter_get_features) {
535 ofproto->ofproto_class->meter_get_features(ofproto,
536 &ofproto->meter_features);
537 } else {
538 memset(&ofproto->meter_features, 0, sizeof ofproto->meter_features);
539 }
540 ofproto->meters = xzalloc((ofproto->meter_features.max_meters + 1)
541 * sizeof(struct meter *));
542
abe529af 543 *ofprotop = ofproto;
064af421
BP
544 return 0;
545}
546
91858960
BP
547/* Must be called (only) by an ofproto implementation in its constructor
548 * function. See the large comment on 'construct' in struct ofproto_class for
549 * details. */
0f5f95a9
BP
550void
551ofproto_init_tables(struct ofproto *ofproto, int n_tables)
552{
553 struct oftable *table;
554
cb22974d
BP
555 ovs_assert(!ofproto->n_tables);
556 ovs_assert(n_tables >= 1 && n_tables <= 255);
0f5f95a9
BP
557
558 ofproto->n_tables = n_tables;
559 ofproto->tables = xmalloc(n_tables * sizeof *ofproto->tables);
560 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
561 oftable_init(table);
562 }
563}
564
91858960
BP
565/* To be optionally called (only) by an ofproto implementation in its
566 * constructor function. See the large comment on 'construct' in struct
567 * ofproto_class for details.
568 *
569 * Sets the maximum number of ports to 'max_ports'. The ofproto generic layer
570 * will then ensure that actions passed into the ofproto implementation will
571 * not refer to OpenFlow ports numbered 'max_ports' or higher. If this
572 * function is not called, there will be no such restriction.
573 *
574 * Reserved ports numbered OFPP_MAX and higher are special and not subject to
575 * the 'max_ports' restriction. */
576void
430dbb14 577ofproto_init_max_ports(struct ofproto *ofproto, uint16_t max_ports)
91858960 578{
430dbb14 579 ovs_assert(max_ports <= ofp_to_u16(OFPP_MAX));
91858960
BP
580 ofproto->max_ports = max_ports;
581}
582
e825ace2
BP
583uint64_t
584ofproto_get_datapath_id(const struct ofproto *ofproto)
585{
586 return ofproto->datapath_id;
587}
588
064af421
BP
589void
590ofproto_set_datapath_id(struct ofproto *p, uint64_t datapath_id)
591{
592 uint64_t old_dpid = p->datapath_id;
fa60c019 593 p->datapath_id = datapath_id ? datapath_id : pick_datapath_id(p);
064af421 594 if (p->datapath_id != old_dpid) {
76ce9432
BP
595 /* Force all active connections to reconnect, since there is no way to
596 * notify a controller that the datapath ID has changed. */
fa05809b 597 ofproto_reconnect_controllers(p);
064af421
BP
598 }
599}
600
76ce9432
BP
601void
602ofproto_set_controllers(struct ofproto *p,
603 const struct ofproto_controller *controllers,
1d9ffc17 604 size_t n_controllers, uint32_t allowed_versions)
76ce9432 605{
1d9ffc17
SH
606 connmgr_set_controllers(p->connmgr, controllers, n_controllers,
607 allowed_versions);
064af421
BP
608}
609
31681a5d
JP
610void
611ofproto_set_fail_mode(struct ofproto *p, enum ofproto_fail_mode fail_mode)
612{
19a87e36 613 connmgr_set_fail_mode(p->connmgr, fail_mode);
31681a5d
JP
614}
615
fa05809b
BP
616/* Drops the connections between 'ofproto' and all of its controllers, forcing
617 * them to reconnect. */
618void
619ofproto_reconnect_controllers(struct ofproto *ofproto)
620{
19a87e36 621 connmgr_reconnect(ofproto->connmgr);
917e50e1
BP
622}
623
624/* Sets the 'n' TCP port addresses in 'extras' as ones to which 'ofproto''s
625 * in-band control should guarantee access, in the same way that in-band
626 * control guarantees access to OpenFlow controllers. */
627void
628ofproto_set_extra_in_band_remotes(struct ofproto *ofproto,
629 const struct sockaddr_in *extras, size_t n)
630{
19a87e36 631 connmgr_set_extra_in_band_remotes(ofproto->connmgr, extras, n);
917e50e1
BP
632}
633
b1da6250
BP
634/* Sets the OpenFlow queue used by flows set up by in-band control on
635 * 'ofproto' to 'queue_id'. If 'queue_id' is negative, then in-band control
636 * flows will use the default queue. */
637void
638ofproto_set_in_band_queue(struct ofproto *ofproto, int queue_id)
639{
19a87e36 640 connmgr_set_in_band_queue(ofproto->connmgr, queue_id);
b1da6250
BP
641}
642
084f5290
SH
643/* Sets the number of flows at which eviction from the kernel flow table
644 * will occur. */
645void
e79a6c83 646ofproto_set_flow_limit(unsigned limit)
084f5290 647{
e79a6c83 648 ofproto_flow_limit = limit;
084f5290
SH
649}
650
72310b04
JS
651/* Sets the maximum idle time for flows in the datapath before they are
652 * expired. */
653void
654ofproto_set_max_idle(unsigned max_idle)
655{
656 ofproto_max_idle = max_idle;
657}
658
b53055f4 659/* If forward_bpdu is true, the NORMAL action will forward frames with
8402c74b
SS
660 * reserved (e.g. STP) destination Ethernet addresses. if forward_bpdu is false,
661 * the NORMAL action will drop these frames. */
662void
663ofproto_set_forward_bpdu(struct ofproto *ofproto, bool forward_bpdu)
664{
665 bool old_val = ofproto->forward_bpdu;
666 ofproto->forward_bpdu = forward_bpdu;
667 if (old_val != ofproto->forward_bpdu) {
668 if (ofproto->ofproto_class->forward_bpdu_changed) {
669 ofproto->ofproto_class->forward_bpdu_changed(ofproto);
670 }
b53055f4 671 }
8402c74b
SS
672}
673
e764773c 674/* Sets the MAC aging timeout for the OFPP_NORMAL action on 'ofproto' to
c4069512
BP
675 * 'idle_time', in seconds, and the maximum number of MAC table entries to
676 * 'max_entries'. */
e764773c 677void
c4069512
BP
678ofproto_set_mac_table_config(struct ofproto *ofproto, unsigned idle_time,
679 size_t max_entries)
e764773c 680{
c4069512
BP
681 if (ofproto->ofproto_class->set_mac_table_config) {
682 ofproto->ofproto_class->set_mac_table_config(ofproto, idle_time,
683 max_entries);
e764773c
BP
684 }
685}
686
448a4b2f 687void
30ea0da7 688ofproto_set_threads(int n_handlers_, int n_revalidators_)
448a4b2f 689{
e79a6c83
EJ
690 int threads = MAX(count_cpu_cores(), 2);
691
30ea0da7
GS
692 n_revalidators = MAX(n_revalidators_, 0);
693 n_handlers = MAX(n_handlers_, 0);
e79a6c83
EJ
694
695 if (!n_revalidators) {
696 n_revalidators = n_handlers
697 ? MAX(threads - (int) n_handlers, 1)
698 : threads / 4 + 1;
699 }
700
701 if (!n_handlers) {
702 n_handlers = MAX(threads - (int) n_revalidators, 1);
703 }
448a4b2f
AW
704}
705
8b6ff729
BP
706void
707ofproto_set_dp_desc(struct ofproto *p, const char *dp_desc)
708{
709 free(p->dp_desc);
710 p->dp_desc = dp_desc ? xstrdup(dp_desc) : NULL;
711}
712
064af421 713int
81e2083f 714ofproto_set_snoops(struct ofproto *ofproto, const struct sset *snoops)
064af421 715{
19a87e36 716 return connmgr_set_snoops(ofproto->connmgr, snoops);
064af421
BP
717}
718
719int
0193b2af
JG
720ofproto_set_netflow(struct ofproto *ofproto,
721 const struct netflow_options *nf_options)
064af421 722{
abe529af
BP
723 if (nf_options && sset_is_empty(&nf_options->collectors)) {
724 nf_options = NULL;
725 }
726
727 if (ofproto->ofproto_class->set_netflow) {
728 return ofproto->ofproto_class->set_netflow(ofproto, nf_options);
064af421 729 } else {
abe529af 730 return nf_options ? EOPNOTSUPP : 0;
064af421
BP
731 }
732}
733
abe529af 734int
72b06300
BP
735ofproto_set_sflow(struct ofproto *ofproto,
736 const struct ofproto_sflow_options *oso)
737{
abe529af
BP
738 if (oso && sset_is_empty(&oso->targets)) {
739 oso = NULL;
740 }
72b06300 741
abe529af
BP
742 if (ofproto->ofproto_class->set_sflow) {
743 return ofproto->ofproto_class->set_sflow(ofproto, oso);
72b06300 744 } else {
abe529af 745 return oso ? EOPNOTSUPP : 0;
72b06300
BP
746 }
747}
29089a54
RL
748
749int
750ofproto_set_ipfix(struct ofproto *ofproto,
751 const struct ofproto_ipfix_bridge_exporter_options *bo,
752 const struct ofproto_ipfix_flow_exporter_options *fo,
753 size_t n_fo)
754{
755 if (ofproto->ofproto_class->set_ipfix) {
756 return ofproto->ofproto_class->set_ipfix(ofproto, bo, fo, n_fo);
757 } else {
758 return (bo || fo) ? EOPNOTSUPP : 0;
759 }
760}
40358701
GS
761
762void
763ofproto_set_flow_restore_wait(bool flow_restore_wait_db)
764{
765 flow_restore_wait = flow_restore_wait_db;
766}
767
768bool
769ofproto_get_flow_restore_wait(void)
770{
771 return flow_restore_wait;
772}
773
e7934396 774\f
21f7563c
JP
775/* Spanning Tree Protocol (STP) configuration. */
776
777/* Configures STP on 'ofproto' using the settings defined in 's'. If
778 * 's' is NULL, disables STP.
779 *
780 * Returns 0 if successful, otherwise a positive errno value. */
781int
782ofproto_set_stp(struct ofproto *ofproto,
783 const struct ofproto_stp_settings *s)
784{
785 return (ofproto->ofproto_class->set_stp
786 ? ofproto->ofproto_class->set_stp(ofproto, s)
787 : EOPNOTSUPP);
788}
789
790/* Retrieves STP status of 'ofproto' and stores it in 's'. If the
791 * 'enabled' member of 's' is false, then the other members are not
792 * meaningful.
793 *
794 * Returns 0 if successful, otherwise a positive errno value. */
795int
796ofproto_get_stp_status(struct ofproto *ofproto,
797 struct ofproto_stp_status *s)
798{
799 return (ofproto->ofproto_class->get_stp_status
800 ? ofproto->ofproto_class->get_stp_status(ofproto, s)
801 : EOPNOTSUPP);
802}
803
804/* Configures STP on 'ofp_port' of 'ofproto' using the settings defined
805 * in 's'. The caller is responsible for assigning STP port numbers
806 * (using the 'port_num' member in the range of 1 through 255, inclusive)
807 * and ensuring there are no duplicates. If the 's' is NULL, then STP
808 * is disabled on the port.
809 *
810 * Returns 0 if successful, otherwise a positive errno value.*/
811int
4e022ec0 812ofproto_port_set_stp(struct ofproto *ofproto, ofp_port_t ofp_port,
21f7563c
JP
813 const struct ofproto_port_stp_settings *s)
814{
815 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
816 if (!ofport) {
817 VLOG_WARN("%s: cannot configure STP on nonexistent port %"PRIu16,
818 ofproto->name, ofp_port);
819 return ENODEV;
820 }
821
822 return (ofproto->ofproto_class->set_stp_port
823 ? ofproto->ofproto_class->set_stp_port(ofport, s)
824 : EOPNOTSUPP);
825}
826
827/* Retrieves STP port status of 'ofp_port' on 'ofproto' and stores it in
828 * 's'. If the 'enabled' member in 's' is false, then the other members
829 * are not meaningful.
830 *
831 * Returns 0 if successful, otherwise a positive errno value.*/
832int
4e022ec0 833ofproto_port_get_stp_status(struct ofproto *ofproto, ofp_port_t ofp_port,
21f7563c
JP
834 struct ofproto_port_stp_status *s)
835{
836 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
837 if (!ofport) {
b0a5c43b
JP
838 VLOG_WARN_RL(&rl, "%s: cannot get STP status on nonexistent "
839 "port %"PRIu16, ofproto->name, ofp_port);
21f7563c
JP
840 return ENODEV;
841 }
842
843 return (ofproto->ofproto_class->get_stp_port_status
844 ? ofproto->ofproto_class->get_stp_port_status(ofport, s)
845 : EOPNOTSUPP);
846}
fd28ce3a
JS
847
848/* Retrieves STP port statistics of 'ofp_port' on 'ofproto' and stores it in
849 * 's'. If the 'enabled' member in 's' is false, then the other members
850 * are not meaningful.
851 *
852 * Returns 0 if successful, otherwise a positive errno value.*/
853int
854ofproto_port_get_stp_stats(struct ofproto *ofproto, ofp_port_t ofp_port,
855 struct ofproto_port_stp_stats *s)
856{
857 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
858 if (!ofport) {
859 VLOG_WARN_RL(&rl, "%s: cannot get STP stats on nonexistent "
860 "port %"PRIu16, ofproto->name, ofp_port);
861 return ENODEV;
862 }
863
864 return (ofproto->ofproto_class->get_stp_port_stats
865 ? ofproto->ofproto_class->get_stp_port_stats(ofport, s)
866 : EOPNOTSUPP);
867}
21f7563c 868\f
8b36f51e
EJ
869/* Queue DSCP configuration. */
870
871/* Registers meta-data associated with the 'n_qdscp' Qualities of Service
872 * 'queues' attached to 'ofport'. This data is not intended to be sufficient
873 * to implement QoS. Instead, it is used to implement features which require
874 * knowledge of what queues exist on a port, and some basic information about
875 * them.
876 *
877 * Returns 0 if successful, otherwise a positive errno value. */
878int
4e022ec0 879ofproto_port_set_queues(struct ofproto *ofproto, ofp_port_t ofp_port,
8b36f51e
EJ
880 const struct ofproto_port_queue *queues,
881 size_t n_queues)
882{
883 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
884
885 if (!ofport) {
886 VLOG_WARN("%s: cannot set queues on nonexistent port %"PRIu16,
887 ofproto->name, ofp_port);
888 return ENODEV;
889 }
890
891 return (ofproto->ofproto_class->set_queues
892 ? ofproto->ofproto_class->set_queues(ofport, queues, n_queues)
893 : EOPNOTSUPP);
894}
895\f
e7934396
BP
896/* Connectivity Fault Management configuration. */
897
892815f5 898/* Clears the CFM configuration from 'ofp_port' on 'ofproto'. */
e7934396 899void
4e022ec0 900ofproto_port_clear_cfm(struct ofproto *ofproto, ofp_port_t ofp_port)
e7934396 901{
abe529af
BP
902 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
903 if (ofport && ofproto->ofproto_class->set_cfm) {
a5610457 904 ofproto->ofproto_class->set_cfm(ofport, NULL);
e7934396
BP
905 }
906}
72b06300 907
892815f5 908/* Configures connectivity fault management on 'ofp_port' in 'ofproto'. Takes
93b8df38
EJ
909 * basic configuration from the configuration members in 'cfm', and the remote
910 * maintenance point ID from remote_mpid. Ignores the statistics members of
911 * 'cfm'.
e7934396 912 *
892815f5 913 * This function has no effect if 'ofproto' does not have a port 'ofp_port'. */
e7934396 914void
4e022ec0 915ofproto_port_set_cfm(struct ofproto *ofproto, ofp_port_t ofp_port,
a5610457 916 const struct cfm_settings *s)
e7934396
BP
917{
918 struct ofport *ofport;
abe529af 919 int error;
e7934396 920
abe529af 921 ofport = ofproto_get_port(ofproto, ofp_port);
e7934396 922 if (!ofport) {
892815f5
BP
923 VLOG_WARN("%s: cannot configure CFM on nonexistent port %"PRIu16,
924 ofproto->name, ofp_port);
e7934396
BP
925 return;
926 }
927
93b8df38
EJ
928 /* XXX: For configuration simplicity, we only support one remote_mpid
929 * outside of the CFM module. It's not clear if this is the correct long
930 * term solution or not. */
abe529af 931 error = (ofproto->ofproto_class->set_cfm
a5610457 932 ? ofproto->ofproto_class->set_cfm(ofport, s)
abe529af
BP
933 : EOPNOTSUPP);
934 if (error) {
935 VLOG_WARN("%s: CFM configuration on port %"PRIu16" (%s) failed (%s)",
936 ofproto->name, ofp_port, netdev_get_name(ofport->netdev),
10a89ef0 937 ovs_strerror(error));
e7934396 938 }
e7934396 939}
e7934396 940
ccc09689
EJ
941/* Configures BFD on 'ofp_port' in 'ofproto'. This function has no effect if
942 * 'ofproto' does not have a port 'ofp_port'. */
943void
4e022ec0 944ofproto_port_set_bfd(struct ofproto *ofproto, ofp_port_t ofp_port,
ccc09689
EJ
945 const struct smap *cfg)
946{
947 struct ofport *ofport;
948 int error;
949
950 ofport = ofproto_get_port(ofproto, ofp_port);
951 if (!ofport) {
952 VLOG_WARN("%s: cannot configure bfd on nonexistent port %"PRIu16,
953 ofproto->name, ofp_port);
e8999bdc 954 return;
ccc09689
EJ
955 }
956
957 error = (ofproto->ofproto_class->set_bfd
958 ? ofproto->ofproto_class->set_bfd(ofport, cfg)
959 : EOPNOTSUPP);
960 if (error) {
961 VLOG_WARN("%s: bfd configuration on port %"PRIu16" (%s) failed (%s)",
962 ofproto->name, ofp_port, netdev_get_name(ofport->netdev),
10a89ef0 963 ovs_strerror(error));
ccc09689
EJ
964 }
965}
966
88bf179a
AW
967/* Populates 'status' with the status of BFD on 'ofport'. Returns 0 on
968 * success. Returns a negative number if there is no status change since
969 * last update. Returns a positive errno otherwise. Has no effect if
970 * 'ofp_port' is not an OpenFlow port in 'ofproto'.
971 *
972 * The caller must provide and own '*status'. */
ccc09689 973int
4e022ec0 974ofproto_port_get_bfd_status(struct ofproto *ofproto, ofp_port_t ofp_port,
ccc09689
EJ
975 struct smap *status)
976{
977 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
978 return (ofport && ofproto->ofproto_class->get_bfd_status
979 ? ofproto->ofproto_class->get_bfd_status(ofport, status)
980 : EOPNOTSUPP);
981}
982
fa066f01
BP
983/* Checks the status of LACP negotiation for 'ofp_port' within ofproto.
984 * Returns 1 if LACP partner information for 'ofp_port' is up-to-date,
985 * 0 if LACP partner information is not current (generally indicating a
986 * connectivity problem), or -1 if LACP is not enabled on 'ofp_port'. */
987int
4e022ec0 988ofproto_port_is_lacp_current(struct ofproto *ofproto, ofp_port_t ofp_port)
fa066f01 989{
abe529af
BP
990 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
991 return (ofport && ofproto->ofproto_class->port_is_lacp_current
992 ? ofproto->ofproto_class->port_is_lacp_current(ofport)
fa066f01 993 : -1);
e7934396 994}
e7934396 995\f
fa066f01 996/* Bundles. */
e7934396 997
abe529af
BP
998/* Registers a "bundle" associated with client data pointer 'aux' in 'ofproto'.
999 * A bundle is the same concept as a Port in OVSDB, that is, it consists of one
1000 * or more "slave" devices (Interfaces, in OVSDB) along with a VLAN
1001 * configuration plus, if there is more than one slave, a bonding
1002 * configuration.
1003 *
1004 * If 'aux' is already registered then this function updates its configuration
1005 * to 's'. Otherwise, this function registers a new bundle.
1006 *
1007 * Bundles only affect the NXAST_AUTOPATH action and output to the OFPP_NORMAL
1008 * port. */
1009int
1010ofproto_bundle_register(struct ofproto *ofproto, void *aux,
1011 const struct ofproto_bundle_settings *s)
fa066f01 1012{
abe529af
BP
1013 return (ofproto->ofproto_class->bundle_set
1014 ? ofproto->ofproto_class->bundle_set(ofproto, aux, s)
1015 : EOPNOTSUPP);
fa066f01
BP
1016}
1017
abe529af
BP
1018/* Unregisters the bundle registered on 'ofproto' with auxiliary data 'aux'.
1019 * If no such bundle has been registered, this has no effect. */
1020int
1021ofproto_bundle_unregister(struct ofproto *ofproto, void *aux)
e7934396 1022{
abe529af 1023 return ofproto_bundle_register(ofproto, aux, NULL);
e7934396 1024}
fa066f01 1025
e7934396 1026\f
abe529af
BP
1027/* Registers a mirror associated with client data pointer 'aux' in 'ofproto'.
1028 * If 'aux' is already registered then this function updates its configuration
c06bba01 1029 * to 's'. Otherwise, this function registers a new mirror. */
abe529af
BP
1030int
1031ofproto_mirror_register(struct ofproto *ofproto, void *aux,
1032 const struct ofproto_mirror_settings *s)
064af421 1033{
abe529af
BP
1034 return (ofproto->ofproto_class->mirror_set
1035 ? ofproto->ofproto_class->mirror_set(ofproto, aux, s)
1036 : EOPNOTSUPP);
064af421
BP
1037}
1038
abe529af
BP
1039/* Unregisters the mirror registered on 'ofproto' with auxiliary data 'aux'.
1040 * If no mirror has been registered, this has no effect. */
1041int
1042ofproto_mirror_unregister(struct ofproto *ofproto, void *aux)
064af421 1043{
abe529af 1044 return ofproto_mirror_register(ofproto, aux, NULL);
064af421
BP
1045}
1046
9d24de3b
JP
1047/* Retrieves statistics from mirror associated with client data pointer
1048 * 'aux' in 'ofproto'. Stores packet and byte counts in 'packets' and
1049 * 'bytes', respectively. If a particular counters is not supported,
1050 * the appropriate argument is set to UINT64_MAX. */
1051int
1052ofproto_mirror_get_stats(struct ofproto *ofproto, void *aux,
1053 uint64_t *packets, uint64_t *bytes)
1054{
1055 if (!ofproto->ofproto_class->mirror_get_stats) {
1056 *packets = *bytes = UINT64_MAX;
1057 return EOPNOTSUPP;
1058 }
1059
1060 return ofproto->ofproto_class->mirror_get_stats(ofproto, aux,
1061 packets, bytes);
1062}
1063
abe529af
BP
1064/* Configures the VLANs whose bits are set to 1 in 'flood_vlans' as VLANs on
1065 * which all packets are flooded, instead of using MAC learning. If
1066 * 'flood_vlans' is NULL, then MAC learning applies to all VLANs.
1067 *
1068 * Flood VLANs affect only the treatment of packets output to the OFPP_NORMAL
1069 * port. */
1070int
1071ofproto_set_flood_vlans(struct ofproto *ofproto, unsigned long *flood_vlans)
abdfe474 1072{
abe529af
BP
1073 return (ofproto->ofproto_class->set_flood_vlans
1074 ? ofproto->ofproto_class->set_flood_vlans(ofproto, flood_vlans)
1075 : EOPNOTSUPP);
abdfe474
JP
1076}
1077
abe529af
BP
1078/* Returns true if 'aux' is a registered bundle that is currently in use as the
1079 * output for a mirror. */
1080bool
b4affc74 1081ofproto_is_mirror_output_bundle(const struct ofproto *ofproto, void *aux)
abe529af
BP
1082{
1083 return (ofproto->ofproto_class->is_mirror_output_bundle
1084 ? ofproto->ofproto_class->is_mirror_output_bundle(ofproto, aux)
1085 : false);
1086}
1087\f
254750ce
BP
1088/* Configuration of OpenFlow tables. */
1089
1090/* Returns the number of OpenFlow tables in 'ofproto'. */
1091int
1092ofproto_get_n_tables(const struct ofproto *ofproto)
1093{
1094 return ofproto->n_tables;
1095}
1096
adcf00ba
AZ
1097/* Returns the number of Controller visible OpenFlow tables
1098 * in 'ofproto'. This number will exclude Hidden tables.
1099 * This funtion's return value should be less or equal to that of
1100 * ofproto_get_n_tables() . */
1101uint8_t
1102ofproto_get_n_visible_tables(const struct ofproto *ofproto)
1103{
1104 uint8_t n = ofproto->n_tables;
1105
1106 /* Count only non-hidden tables in the number of tables. (Hidden tables,
1107 * if present, are always at the end.) */
1108 while(n && (ofproto->tables[n - 1].flags & OFTABLE_HIDDEN)) {
1109 n--;
1110 }
1111
1112 return n;
1113}
1114
254750ce
BP
1115/* Configures the OpenFlow table in 'ofproto' with id 'table_id' with the
1116 * settings from 's'. 'table_id' must be in the range 0 through the number of
1117 * OpenFlow tables in 'ofproto' minus 1, inclusive.
1118 *
1119 * For read-only tables, only the name may be configured. */
1120void
1121ofproto_configure_table(struct ofproto *ofproto, int table_id,
1122 const struct ofproto_table_settings *s)
1123{
1124 struct oftable *table;
1125
cb22974d 1126 ovs_assert(table_id >= 0 && table_id < ofproto->n_tables);
254750ce
BP
1127 table = &ofproto->tables[table_id];
1128
1129 oftable_set_name(table, s->name);
1130
1131 if (table->flags & OFTABLE_READONLY) {
1132 return;
1133 }
1134
1135 if (s->groups) {
1136 oftable_enable_eviction(table, s->groups, s->n_groups);
1137 } else {
1138 oftable_disable_eviction(table);
1139 }
1140
1141 table->max_flows = s->max_flows;
06f81620 1142 fat_rwlock_wrlock(&table->cls.rwlock);
13751fd8
JR
1143 classifier_set_prefix_fields(&table->cls,
1144 s->prefix_fields, s->n_prefix_fields);
06f81620 1145 fat_rwlock_unlock(&table->cls.rwlock);
834fe5cb
BP
1146
1147 ovs_mutex_lock(&ofproto_mutex);
1148 evict_rules_from_table(table, 0);
1149 ovs_mutex_unlock(&ofproto_mutex);
254750ce
BP
1150}
1151\f
81e2083f
BP
1152bool
1153ofproto_has_snoops(const struct ofproto *ofproto)
1154{
1155 return connmgr_has_snoops(ofproto->connmgr);
1156}
1157
064af421 1158void
81e2083f 1159ofproto_get_snoops(const struct ofproto *ofproto, struct sset *snoops)
064af421 1160{
19a87e36 1161 connmgr_get_snoops(ofproto->connmgr, snoops);
064af421
BP
1162}
1163
15aaf599 1164static void
b20f4073 1165ofproto_rule_delete__(struct rule *rule, uint8_t reason)
15aaf599
BP
1166 OVS_REQUIRES(ofproto_mutex)
1167{
834fe5cb 1168 struct ofproto *ofproto = rule->ofproto;
15aaf599 1169
834fe5cb
BP
1170 delete_flow__(rule, reason, NULL);
1171 ofmonitor_flush(ofproto->connmgr);
15aaf599
BP
1172}
1173
a73fcd71 1174/* Deletes 'rule' from 'ofproto'.
8037acb4 1175 *
b74cb5d8
BP
1176 * Within an ofproto implementation, this function allows an ofproto
1177 * implementation to destroy any rules that remain when its ->destruct()
1178 * function is called. This function is not suitable for use elsewhere in an
1179 * ofproto implementation.
1180 *
b74cb5d8 1181 * This function implements steps 4.4 and 4.5 in the section titled "Rule Life
8b81d1ef 1182 * Cycle" in ofproto-provider.h. */
b74cb5d8 1183void
8b81d1ef 1184ofproto_rule_delete(struct ofproto *ofproto, struct rule *rule)
15aaf599 1185 OVS_EXCLUDED(ofproto_mutex)
7ee20df1 1186{
834fe5cb
BP
1187 /* This skips the ofmonitor and flow-removed notifications because the
1188 * switch is being deleted and any OpenFlow channels have been or soon will
1189 * be killed. */
15aaf599 1190 ovs_mutex_lock(&ofproto_mutex);
8b81d1ef 1191 oftable_remove_rule__(ofproto, rule);
8037acb4 1192 ofproto->ofproto_class->rule_delete(rule);
15aaf599 1193 ovs_mutex_unlock(&ofproto_mutex);
8037acb4
BP
1194}
1195
1196static void
1197ofproto_flush__(struct ofproto *ofproto)
15aaf599 1198 OVS_EXCLUDED(ofproto_mutex)
8037acb4 1199{
d0918789 1200 struct oftable *table;
7ee20df1
BP
1201
1202 if (ofproto->ofproto_class->flush) {
1203 ofproto->ofproto_class->flush(ofproto);
1204 }
1205
15aaf599 1206 ovs_mutex_lock(&ofproto_mutex);
b772ded8 1207 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
7ee20df1
BP
1208 struct rule *rule, *next_rule;
1209 struct cls_cursor cursor;
1210
5c67e4af
BP
1211 if (table->flags & OFTABLE_HIDDEN) {
1212 continue;
1213 }
1214
06f81620 1215 fat_rwlock_rdlock(&table->cls.rwlock);
d0918789 1216 cls_cursor_init(&cursor, &table->cls, NULL);
06f81620 1217 fat_rwlock_unlock(&table->cls.rwlock);
7ee20df1 1218 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) {
b20f4073 1219 ofproto_rule_delete__(rule, OFPRR_DELETE);
7ee20df1
BP
1220 }
1221 }
15aaf599 1222 ovs_mutex_unlock(&ofproto_mutex);
7ee20df1
BP
1223}
1224
7395c052
NZ
1225static void delete_group(struct ofproto *ofproto, uint32_t group_id);
1226
abe529af
BP
1227static void
1228ofproto_destroy__(struct ofproto *ofproto)
15aaf599 1229 OVS_EXCLUDED(ofproto_mutex)
abe529af 1230{
d0918789 1231 struct oftable *table;
6c1491fb 1232
35412852 1233 destroy_rule_executes(ofproto);
7395c052 1234 delete_group(ofproto, OFPG_ALL);
5c09b672
GS
1235
1236 guarded_list_destroy(&ofproto->rule_executes);
7395c052
NZ
1237 ovs_rwlock_destroy(&ofproto->groups_rwlock);
1238 hmap_destroy(&ofproto->groups);
1239
abe529af 1240 connmgr_destroy(ofproto->connmgr);
fa066f01 1241
abe529af
BP
1242 hmap_remove(&all_ofprotos, &ofproto->hmap_node);
1243 free(ofproto->name);
955a7127 1244 free(ofproto->type);
abe529af
BP
1245 free(ofproto->mfr_desc);
1246 free(ofproto->hw_desc);
1247 free(ofproto->sw_desc);
1248 free(ofproto->serial_desc);
1249 free(ofproto->dp_desc);
abe529af 1250 hmap_destroy(&ofproto->ports);
fdcea803 1251 hmap_destroy(&ofproto->ofport_usage);
abe529af 1252 shash_destroy(&ofproto->port_by_name);
e1b1d06a 1253 simap_destroy(&ofproto->ofp_requests);
6c1491fb 1254
b772ded8 1255 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
d0918789 1256 oftable_destroy(table);
6c1491fb
BP
1257 }
1258 free(ofproto->tables);
fa066f01 1259
1406c79b
BP
1260 ovs_assert(hindex_is_empty(&ofproto->cookies));
1261 hindex_destroy(&ofproto->cookies);
1262
52a90c29
BP
1263 free(ofproto->vlan_bitmap);
1264
abe529af
BP
1265 ofproto->ofproto_class->dealloc(ofproto);
1266}
fa066f01 1267
064af421
BP
1268void
1269ofproto_destroy(struct ofproto *p)
15aaf599 1270 OVS_EXCLUDED(ofproto_mutex)
064af421 1271{
ca0f572c 1272 struct ofport *ofport, *next_ofport;
fdcea803 1273 struct ofport_usage *usage, *next_usage;
064af421
BP
1274
1275 if (!p) {
1276 return;
1277 }
1278
717de9ff
JR
1279 if (p->meters) {
1280 meter_delete(p, 1, p->meter_features.max_meters);
1281 p->meter_features.max_meters = 0;
1282 free(p->meters);
1283 p->meters = NULL;
1284 }
1285
7ee20df1 1286 ofproto_flush__(p);
4e8e4213 1287 HMAP_FOR_EACH_SAFE (ofport, next_ofport, hmap_node, &p->ports) {
abe529af 1288 ofport_destroy(ofport);
064af421 1289 }
064af421 1290
fdcea803
GS
1291 HMAP_FOR_EACH_SAFE (usage, next_usage, hmap_node, &p->ofport_usage) {
1292 hmap_remove(&p->ofport_usage, &usage->hmap_node);
1293 free(usage);
1294 }
1295
abe529af 1296 p->ofproto_class->destruct(p);
f416c8d6
JR
1297 /* Destroying rules is deferred, must have 'ofproto' around for them. */
1298 ovsrcu_postpone(ofproto_destroy__, p);
064af421
BP
1299}
1300
abe529af
BP
1301/* Destroys the datapath with the respective 'name' and 'type'. With the Linux
1302 * kernel datapath, for example, this destroys the datapath in the kernel, and
1303 * with the netdev-based datapath, it tears down the data structures that
1304 * represent the datapath.
1305 *
1306 * The datapath should not be currently open as an ofproto. */
064af421 1307int
abe529af 1308ofproto_delete(const char *name, const char *type)
064af421 1309{
abe529af
BP
1310 const struct ofproto_class *class = ofproto_class_find__(type);
1311 return (!class ? EAFNOSUPPORT
1312 : !class->del ? EACCES
1313 : class->del(type, name));
064af421
BP
1314}
1315
e9e28be3
BP
1316static void
1317process_port_change(struct ofproto *ofproto, int error, char *devname)
1318{
1319 if (error == ENOBUFS) {
1320 reinit_ports(ofproto);
1321 } else if (!error) {
1322 update_port(ofproto, devname);
1323 free(devname);
1324 }
1325}
1326
11a574a7
JP
1327int
1328ofproto_type_run(const char *datapath_type)
1329{
1330 const struct ofproto_class *class;
1331 int error;
1332
1333 datapath_type = ofproto_normalize_type(datapath_type);
1334 class = ofproto_class_find__(datapath_type);
1335
1336 error = class->type_run ? class->type_run(datapath_type) : 0;
1337 if (error && error != EAGAIN) {
1338 VLOG_ERR_RL(&rl, "%s: type_run failed (%s)",
10a89ef0 1339 datapath_type, ovs_strerror(error));
11a574a7
JP
1340 }
1341 return error;
1342}
1343
11a574a7
JP
1344void
1345ofproto_type_wait(const char *datapath_type)
1346{
1347 const struct ofproto_class *class;
1348
1349 datapath_type = ofproto_normalize_type(datapath_type);
1350 class = ofproto_class_find__(datapath_type);
1351
1352 if (class->type_wait) {
1353 class->type_wait(datapath_type);
1354 }
1355}
1356
064af421 1357int
abe529af 1358ofproto_run(struct ofproto *p)
064af421 1359{
064af421 1360 int error;
da4a6191 1361 uint64_t new_seq;
064af421 1362
abe529af 1363 error = p->ofproto_class->run(p);
5fcc0d00 1364 if (error && error != EAGAIN) {
10a89ef0 1365 VLOG_ERR_RL(&rl, "%s: run failed (%s)", p->name, ovs_strerror(error));
149f577a
JG
1366 }
1367
35412852
BP
1368 run_rule_executes(p);
1369
448c2fa8
EJ
1370 /* Restore the eviction group heap invariant occasionally. */
1371 if (p->eviction_group_timer < time_msec()) {
1372 size_t i;
1373
1374 p->eviction_group_timer = time_msec() + 1000;
1375
1376 for (i = 0; i < p->n_tables; i++) {
1377 struct oftable *table = &p->tables[i];
1378 struct eviction_group *evg;
1379 struct cls_cursor cursor;
448c2fa8
EJ
1380 struct rule *rule;
1381
1382 if (!table->eviction_fields) {
1383 continue;
1384 }
1385
15aaf599 1386 ovs_mutex_lock(&ofproto_mutex);
06f81620 1387 fat_rwlock_rdlock(&table->cls.rwlock);
c4ea31b1 1388 cls_cursor_init(&cursor, &table->cls, NULL);
448c2fa8 1389 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
6d56c1f1
K
1390 if (rule->idle_timeout || rule->hard_timeout) {
1391 if (!rule->eviction_group) {
1392 eviction_group_add_rule(rule);
1393 } else {
1394 heap_raw_change(&rule->evg_node,
1395 rule_eviction_priority(p, rule));
1396 }
448c2fa8
EJ
1397 }
1398 }
06f81620 1399 fat_rwlock_unlock(&table->cls.rwlock);
6d56c1f1
K
1400
1401 HEAP_FOR_EACH (evg, size_node, &table->eviction_groups_by_size) {
1402 heap_rebuild(&evg->rules);
1403 }
15aaf599 1404 ovs_mutex_unlock(&ofproto_mutex);
448c2fa8
EJ
1405 }
1406 }
1407
5bf0e941 1408 if (p->ofproto_class->port_poll) {
7436ed80
BP
1409 char *devname;
1410
5bf0e941
BP
1411 while ((error = p->ofproto_class->port_poll(p, &devname)) != EAGAIN) {
1412 process_port_change(p, error, devname);
064af421 1413 }
e9e28be3 1414 }
031d8bff 1415
da4a6191
JS
1416 new_seq = seq_read(connectivity_seq_get());
1417 if (new_seq != p->change_seq) {
1418 struct sset devnames;
1419 const char *devname;
1420 struct ofport *ofport;
1421
1422 /* Update OpenFlow port status for any port whose netdev has changed.
1423 *
1424 * Refreshing a given 'ofport' can cause an arbitrary ofport to be
1425 * destroyed, so it's not safe to update ports directly from the
1426 * HMAP_FOR_EACH loop, or even to use HMAP_FOR_EACH_SAFE. Instead, we
1427 * need this two-phase approach. */
1428 sset_init(&devnames);
1429 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
61501798
AW
1430 uint64_t port_change_seq;
1431
1432 port_change_seq = netdev_get_change_seq(ofport->netdev);
1433 if (ofport->change_seq != port_change_seq) {
1434 ofport->change_seq = port_change_seq;
1435 sset_add(&devnames, netdev_get_name(ofport->netdev));
1436 }
031d8bff 1437 }
da4a6191
JS
1438 SSET_FOR_EACH (devname, &devnames) {
1439 update_port(p, devname);
1440 }
1441 sset_destroy(&devnames);
1442
1443 p->change_seq = new_seq;
064af421
BP
1444 }
1445
834fe5cb 1446 connmgr_run(p->connmgr, handle_openflow);
064af421 1447
5fcc0d00
BP
1448 return error;
1449}
1450
064af421
BP
1451void
1452ofproto_wait(struct ofproto *p)
1453{
abe529af 1454 p->ofproto_class->wait(p);
5bf0e941
BP
1455 if (p->ofproto_class->port_poll_wait) {
1456 p->ofproto_class->port_poll_wait(p);
e7934396 1457 }
da4a6191 1458 seq_wait(connectivity_seq_get(), p->change_seq);
834fe5cb 1459 connmgr_wait(p->connmgr);
064af421
BP
1460}
1461
064af421
BP
1462bool
1463ofproto_is_alive(const struct ofproto *p)
1464{
19a87e36 1465 return connmgr_has_controllers(p->connmgr);
064af421
BP
1466}
1467
0d085684
BP
1468/* Adds some memory usage statistics for 'ofproto' into 'usage', for use with
1469 * memory_report(). */
1470void
1471ofproto_get_memory_usage(const struct ofproto *ofproto, struct simap *usage)
1472{
1473 const struct oftable *table;
1474 unsigned int n_rules;
1475
1476 simap_increase(usage, "ports", hmap_count(&ofproto->ports));
15aaf599 1477
0d085684
BP
1478 n_rules = 0;
1479 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
06f81620 1480 fat_rwlock_rdlock(&table->cls.rwlock);
0d085684 1481 n_rules += classifier_count(&table->cls);
06f81620 1482 fat_rwlock_unlock(&table->cls.rwlock);
0d085684
BP
1483 }
1484 simap_increase(usage, "rules", n_rules);
1485
1486 if (ofproto->ofproto_class->get_memory_usage) {
1487 ofproto->ofproto_class->get_memory_usage(ofproto, usage);
1488 }
1489
1490 connmgr_get_memory_usage(ofproto->connmgr, usage);
1491}
1492
1c030aa5
EJ
1493void
1494ofproto_type_get_memory_usage(const char *datapath_type, struct simap *usage)
1495{
1496 const struct ofproto_class *class;
1497
1498 datapath_type = ofproto_normalize_type(datapath_type);
1499 class = ofproto_class_find__(datapath_type);
1500
1501 if (class && class->type_get_memory_usage) {
1502 class->type_get_memory_usage(datapath_type, usage);
1503 }
1504}
1505
bffc0589 1506void
2cdcb898 1507ofproto_get_ofproto_controller_info(const struct ofproto *ofproto,
bffc0589
AE
1508 struct shash *info)
1509{
19a87e36 1510 connmgr_get_controller_info(ofproto->connmgr, info);
bffc0589
AE
1511}
1512
1513void
1514ofproto_free_ofproto_controller_info(struct shash *info)
1515{
72ba2ed3 1516 connmgr_free_controller_info(info);
bffc0589
AE
1517}
1518
b5827b24
BP
1519/* Makes a deep copy of 'old' into 'port'. */
1520void
1521ofproto_port_clone(struct ofproto_port *port, const struct ofproto_port *old)
1522{
1523 port->name = xstrdup(old->name);
1524 port->type = xstrdup(old->type);
1525 port->ofp_port = old->ofp_port;
1526}
1527
1528/* Frees memory allocated to members of 'ofproto_port'.
3a6ccc8c 1529 *
b5827b24
BP
1530 * Do not call this function on an ofproto_port obtained from
1531 * ofproto_port_dump_next(): that function retains ownership of the data in the
1532 * ofproto_port. */
1533void
1534ofproto_port_destroy(struct ofproto_port *ofproto_port)
1535{
1536 free(ofproto_port->name);
1537 free(ofproto_port->type);
1538}
1539
b5827b24 1540/* Initializes 'dump' to begin dumping the ports in an ofproto.
3a6ccc8c 1541 *
b5827b24
BP
1542 * This function provides no status indication. An error status for the entire
1543 * dump operation is provided when it is completed by calling
1544 * ofproto_port_dump_done().
1545 */
1546void
1547ofproto_port_dump_start(struct ofproto_port_dump *dump,
1548 const struct ofproto *ofproto)
1549{
abe529af
BP
1550 dump->ofproto = ofproto;
1551 dump->error = ofproto->ofproto_class->port_dump_start(ofproto,
1552 &dump->state);
b5827b24
BP
1553}
1554
1555/* Attempts to retrieve another port from 'dump', which must have been created
1556 * with ofproto_port_dump_start(). On success, stores a new ofproto_port into
1557 * 'port' and returns true. On failure, returns false.
1558 *
1559 * Failure might indicate an actual error or merely that the last port has been
1560 * dumped. An error status for the entire dump operation is provided when it
1561 * is completed by calling ofproto_port_dump_done().
1562 *
1563 * The ofproto owns the data stored in 'port'. It will remain valid until at
1564 * least the next time 'dump' is passed to ofproto_port_dump_next() or
1565 * ofproto_port_dump_done(). */
1566bool
1567ofproto_port_dump_next(struct ofproto_port_dump *dump,
1568 struct ofproto_port *port)
1569{
abe529af
BP
1570 const struct ofproto *ofproto = dump->ofproto;
1571
1572 if (dump->error) {
1573 return false;
1574 }
b5827b24 1575
abe529af
BP
1576 dump->error = ofproto->ofproto_class->port_dump_next(ofproto, dump->state,
1577 port);
1578 if (dump->error) {
1579 ofproto->ofproto_class->port_dump_done(ofproto, dump->state);
1580 return false;
b5827b24 1581 }
abe529af 1582 return true;
b5827b24
BP
1583}
1584
1585/* Completes port table dump operation 'dump', which must have been created
1586 * with ofproto_port_dump_start(). Returns 0 if the dump operation was
1587 * error-free, otherwise a positive errno value describing the problem. */
3a6ccc8c 1588int
b5827b24 1589ofproto_port_dump_done(struct ofproto_port_dump *dump)
3a6ccc8c 1590{
abe529af
BP
1591 const struct ofproto *ofproto = dump->ofproto;
1592 if (!dump->error) {
1593 dump->error = ofproto->ofproto_class->port_dump_done(ofproto,
1594 dump->state);
1595 }
1596 return dump->error == EOF ? 0 : dump->error;
b5827b24
BP
1597}
1598
0aeaabc8
JP
1599/* Returns the type to pass to netdev_open() when a datapath of type
1600 * 'datapath_type' has a port of type 'port_type', for a few special
1601 * cases when a netdev type differs from a port type. For example, when
1602 * using the userspace datapath, a port of type "internal" needs to be
1603 * opened as "tap".
1604 *
1605 * Returns either 'type' itself or a string literal, which must not be
1606 * freed. */
1607const char *
1608ofproto_port_open_type(const char *datapath_type, const char *port_type)
1609{
1610 const struct ofproto_class *class;
1611
1612 datapath_type = ofproto_normalize_type(datapath_type);
1613 class = ofproto_class_find__(datapath_type);
1614 if (!class) {
1615 return port_type;
1616 }
1617
1618 return (class->port_open_type
1619 ? class->port_open_type(datapath_type, port_type)
1620 : port_type);
1621}
1622
81816a5f
JP
1623/* Attempts to add 'netdev' as a port on 'ofproto'. If 'ofp_portp' is
1624 * non-null and '*ofp_portp' is not OFPP_NONE, attempts to use that as
1625 * the port's OpenFlow port number.
1626 *
1627 * If successful, returns 0 and sets '*ofp_portp' to the new port's
1628 * OpenFlow port number (if 'ofp_portp' is non-null). On failure,
1629 * returns a positive errno value and sets '*ofp_portp' to OFPP_NONE (if
1630 * 'ofp_portp' is non-null). */
b5827b24
BP
1631int
1632ofproto_port_add(struct ofproto *ofproto, struct netdev *netdev,
4e022ec0 1633 ofp_port_t *ofp_portp)
b5827b24 1634{
4e022ec0 1635 ofp_port_t ofp_port = ofp_portp ? *ofp_portp : OFPP_NONE;
3a6ccc8c
BP
1636 int error;
1637
e1b1d06a 1638 error = ofproto->ofproto_class->port_add(ofproto, netdev);
1fa24dea 1639 if (!error) {
e1b1d06a
JP
1640 const char *netdev_name = netdev_get_name(netdev);
1641
4e022ec0
AW
1642 simap_put(&ofproto->ofp_requests, netdev_name,
1643 ofp_to_u16(ofp_port));
e1b1d06a 1644 update_port(ofproto, netdev_name);
1fa24dea 1645 }
b5827b24 1646 if (ofp_portp) {
17f69db5
BP
1647 *ofp_portp = OFPP_NONE;
1648 if (!error) {
1649 struct ofproto_port ofproto_port;
1650
1651 error = ofproto_port_query_by_name(ofproto,
1652 netdev_get_name(netdev),
1653 &ofproto_port);
1654 if (!error) {
1655 *ofp_portp = ofproto_port.ofp_port;
1656 ofproto_port_destroy(&ofproto_port);
1657 }
1658 }
3a6ccc8c
BP
1659 }
1660 return error;
1661}
1662
b5827b24
BP
1663/* Looks up a port named 'devname' in 'ofproto'. On success, returns 0 and
1664 * initializes '*port' appropriately; on failure, returns a positive errno
1665 * value.
1666 *
abe529af 1667 * The caller owns the data in 'ofproto_port' and must free it with
b5827b24
BP
1668 * ofproto_port_destroy() when it is no longer needed. */
1669int
1670ofproto_port_query_by_name(const struct ofproto *ofproto, const char *devname,
1671 struct ofproto_port *port)
a4e2e1f2 1672{
b5827b24
BP
1673 int error;
1674
abe529af
BP
1675 error = ofproto->ofproto_class->port_query_by_name(ofproto, devname, port);
1676 if (error) {
1677 memset(port, 0, sizeof *port);
b5827b24
BP
1678 }
1679 return error;
a4e2e1f2
EJ
1680}
1681
b5827b24 1682/* Deletes port number 'ofp_port' from the datapath for 'ofproto'.
3a6ccc8c 1683 * Returns 0 if successful, otherwise a positive errno. */
064af421 1684int
4e022ec0 1685ofproto_port_del(struct ofproto *ofproto, ofp_port_t ofp_port)
064af421 1686{
abe529af 1687 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
1264ec95 1688 const char *name = ofport ? netdev_get_name(ofport->netdev) : "<unknown>";
e1b1d06a 1689 struct simap_node *ofp_request_node;
3cf10406 1690 int error;
cdee00fd 1691
e1b1d06a
JP
1692 ofp_request_node = simap_find(&ofproto->ofp_requests, name);
1693 if (ofp_request_node) {
1694 simap_delete(&ofproto->ofp_requests, ofp_request_node);
1695 }
1696
abe529af 1697 error = ofproto->ofproto_class->port_del(ofproto, ofp_port);
892815f5 1698 if (!error && ofport) {
1264ec95
BP
1699 /* 'name' is the netdev's name and update_port() is going to close the
1700 * netdev. Just in case update_port() refers to 'name' after it
3a6ccc8c
BP
1701 * destroys 'ofport', make a copy of it around the update_port()
1702 * call. */
1703 char *devname = xstrdup(name);
1704 update_port(ofproto, devname);
1705 free(devname);
3cf10406
BP
1706 }
1707 return error;
064af421
BP
1708}
1709
276f6518
SH
1710static void
1711flow_mod_init(struct ofputil_flow_mod *fm,
1712 const struct match *match, unsigned int priority,
1713 const struct ofpact *ofpacts, size_t ofpacts_len,
1714 enum ofp_flow_mod_command command)
1715{
1716 memset(fm, 0, sizeof *fm);
1717 fm->match = *match;
1718 fm->priority = priority;
1719 fm->cookie = 0;
1720 fm->new_cookie = 0;
1721 fm->modify_cookie = false;
1722 fm->table_id = 0;
1723 fm->command = command;
1724 fm->idle_timeout = 0;
1725 fm->hard_timeout = 0;
1726 fm->buffer_id = UINT32_MAX;
1727 fm->out_port = OFPP_ANY;
1728 fm->out_group = OFPG_ANY;
1729 fm->flags = 0;
1730 fm->ofpacts = CONST_CAST(struct ofpact *, ofpacts);
1731 fm->ofpacts_len = ofpacts_len;
cc40d06b 1732 fm->delete_reason = OFPRR_DELETE;
276f6518
SH
1733}
1734
97f63e57
BP
1735static int
1736simple_flow_mod(struct ofproto *ofproto,
1737 const struct match *match, unsigned int priority,
1738 const struct ofpact *ofpacts, size_t ofpacts_len,
1739 enum ofp_flow_mod_command command)
1740{
1741 struct ofputil_flow_mod fm;
1742
276f6518 1743 flow_mod_init(&fm, match, priority, ofpacts, ofpacts_len, command);
15aaf599 1744
baae3d02 1745 return handle_flow_mod__(ofproto, &fm, NULL);
97f63e57
BP
1746}
1747
6c1491fb 1748/* Adds a flow to OpenFlow flow table 0 in 'p' that matches 'cls_rule' and
fa8b054f
BP
1749 * performs the 'n_actions' actions in 'actions'. The new flow will not
1750 * timeout.
1751 *
1752 * If cls_rule->priority is in the range of priorities supported by OpenFlow
1753 * (0...65535, inclusive) then the flow will be visible to OpenFlow
1754 * controllers; otherwise, it will be hidden.
1755 *
f25d0cf3 1756 * The caller retains ownership of 'cls_rule' and 'ofpacts'.
6c1491fb
BP
1757 *
1758 * This is a helper function for in-band control and fail-open. */
064af421 1759void
81a76618
BP
1760ofproto_add_flow(struct ofproto *ofproto, const struct match *match,
1761 unsigned int priority,
f25d0cf3 1762 const struct ofpact *ofpacts, size_t ofpacts_len)
15aaf599 1763 OVS_EXCLUDED(ofproto_mutex)
064af421 1764{
7ee20df1 1765 const struct rule *rule;
6f00e29b 1766 bool must_add;
7ee20df1 1767
97f63e57
BP
1768 /* First do a cheap check whether the rule we're looking for already exists
1769 * with the actions that we want. If it does, then we're done. */
06f81620 1770 fat_rwlock_rdlock(&ofproto->tables[0].cls.rwlock);
81a76618
BP
1771 rule = rule_from_cls_rule(classifier_find_match_exactly(
1772 &ofproto->tables[0].cls, match, priority));
6f00e29b 1773 if (rule) {
dc723c44 1774 const struct rule_actions *actions = rule_get_actions(rule);
06a0f3e2 1775 must_add = !ofpacts_equal(actions->ofpacts, actions->ofpacts_len,
6f00e29b 1776 ofpacts, ofpacts_len);
6f00e29b
BP
1777 } else {
1778 must_add = true;
1779 }
06f81620 1780 fat_rwlock_unlock(&ofproto->tables[0].cls.rwlock);
97f63e57
BP
1781
1782 /* If there's no such rule or the rule doesn't have the actions we want,
1783 * fall back to a executing a full flow mod. We can't optimize this at
1784 * all because we didn't take enough locks above to ensure that the flow
1785 * table didn't already change beneath us. */
6f00e29b 1786 if (must_add) {
97f63e57
BP
1787 simple_flow_mod(ofproto, match, priority, ofpacts, ofpacts_len,
1788 OFPFC_MODIFY_STRICT);
7ee20df1 1789 }
064af421
BP
1790}
1791
75a75043 1792/* Executes the flow modification specified in 'fm'. Returns 0 on success, an
90bf1e07
BP
1793 * OFPERR_* OpenFlow error code on failure, or OFPROTO_POSTPONE if the
1794 * operation cannot be initiated now but may be retried later.
75a75043 1795 *
1b63b91e
BP
1796 * This is a helper function for in-band control and fail-open and the "learn"
1797 * action. */
75a75043 1798int
e3b56933 1799ofproto_flow_mod(struct ofproto *ofproto, struct ofputil_flow_mod *fm)
15aaf599 1800 OVS_EXCLUDED(ofproto_mutex)
75a75043 1801{
b90d6ee5
JR
1802 /* Optimize for the most common case of a repeated learn action.
1803 * If an identical flow already exists we only need to update its
1804 * 'modified' time. */
1805 if (fm->command == OFPFC_MODIFY_STRICT && fm->table_id != OFPTT_ALL
1806 && !(fm->flags & OFPUTIL_FF_RESET_COUNTS)) {
1807 struct oftable *table = &ofproto->tables[fm->table_id];
b90d6ee5
JR
1808 struct rule *rule;
1809 bool done = false;
1810
b90d6ee5 1811 fat_rwlock_rdlock(&table->cls.rwlock);
0ef165ec
EJ
1812 rule = rule_from_cls_rule(classifier_find_match_exactly(&table->cls,
1813 &fm->match,
1814 fm->priority));
b90d6ee5
JR
1815 if (rule) {
1816 /* Reading many of the rule fields and writing on 'modified'
1817 * requires the rule->mutex. Also, rule->actions may change
1818 * if rule->mutex is not held. */
06a0f3e2
BP
1819 const struct rule_actions *actions;
1820
b90d6ee5 1821 ovs_mutex_lock(&rule->mutex);
06a0f3e2 1822 actions = rule_get_actions(rule);
b90d6ee5
JR
1823 if (rule->idle_timeout == fm->idle_timeout
1824 && rule->hard_timeout == fm->hard_timeout
1825 && rule->flags == (fm->flags & OFPUTIL_FF_STATE)
1826 && (!fm->modify_cookie || (fm->new_cookie == rule->flow_cookie))
1827 && ofpacts_equal(fm->ofpacts, fm->ofpacts_len,
06a0f3e2 1828 actions->ofpacts, actions->ofpacts_len)) {
b90d6ee5
JR
1829 /* Rule already exists and need not change, only update the
1830 modified timestamp. */
1831 rule->modified = time_msec();
1832 done = true;
1833 }
1834 ovs_mutex_unlock(&rule->mutex);
1835 }
1836 fat_rwlock_unlock(&table->cls.rwlock);
1837
1838 if (done) {
1839 return 0;
1840 }
1841 }
1842
baae3d02 1843 return handle_flow_mod__(ofproto, fm, NULL);
75a75043
BP
1844}
1845
6c1491fb
BP
1846/* Searches for a rule with matching criteria exactly equal to 'target' in
1847 * ofproto's table 0 and, if it finds one, deletes it.
1848 *
1849 * This is a helper function for in-band control and fail-open. */
b20f4073 1850void
81a76618
BP
1851ofproto_delete_flow(struct ofproto *ofproto,
1852 const struct match *target, unsigned int priority)
15aaf599 1853 OVS_EXCLUDED(ofproto_mutex)
064af421 1854{
8037acb4 1855 struct classifier *cls = &ofproto->tables[0].cls;
064af421
BP
1856 struct rule *rule;
1857
97f63e57
BP
1858 /* First do a cheap check whether the rule we're looking for has already
1859 * been deleted. If so, then we're done. */
06f81620 1860 fat_rwlock_rdlock(&cls->rwlock);
8037acb4
BP
1861 rule = rule_from_cls_rule(classifier_find_match_exactly(cls, target,
1862 priority));
06f81620 1863 fat_rwlock_unlock(&cls->rwlock);
834fe5cb
BP
1864 if (!rule) {
1865 return;
bcf84111 1866 }
834fe5cb
BP
1867
1868 /* Execute a flow mod. We can't optimize this at all because we didn't
1869 * take enough locks above to ensure that the flow table didn't already
1870 * change beneath us. */
1871 simple_flow_mod(ofproto, target, priority, NULL, 0, OFPFC_DELETE_STRICT);
142e1f5c
BP
1872}
1873
834fe5cb
BP
1874/* Delete all of the flows from all of ofproto's flow tables, then reintroduce
1875 * the flows required by in-band control and fail-open. */
142e1f5c
BP
1876void
1877ofproto_flush_flows(struct ofproto *ofproto)
1878{
7ee20df1 1879 COVERAGE_INC(ofproto_flush);
834fe5cb
BP
1880 ofproto_flush__(ofproto);
1881 connmgr_flushed(ofproto->connmgr);
064af421
BP
1882}
1883\f
1884static void
1885reinit_ports(struct ofproto *p)
1886{
abe529af 1887 struct ofproto_port_dump dump;
b3c01ed3 1888 struct sset devnames;
064af421 1889 struct ofport *ofport;
abe529af 1890 struct ofproto_port ofproto_port;
b3c01ed3 1891 const char *devname;
064af421 1892
898bf89d
JP
1893 COVERAGE_INC(ofproto_reinit_ports);
1894
b3c01ed3 1895 sset_init(&devnames);
4e8e4213 1896 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
1264ec95 1897 sset_add(&devnames, netdev_get_name(ofport->netdev));
064af421 1898 }
abe529af
BP
1899 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, p) {
1900 sset_add(&devnames, ofproto_port.name);
064af421 1901 }
064af421 1902
b3c01ed3
BP
1903 SSET_FOR_EACH (devname, &devnames) {
1904 update_port(p, devname);
064af421 1905 }
b3c01ed3 1906 sset_destroy(&devnames);
064af421
BP
1907}
1908
4e022ec0 1909static ofp_port_t
e1b1d06a
JP
1910alloc_ofp_port(struct ofproto *ofproto, const char *netdev_name)
1911{
4e022ec0 1912 uint16_t port_idx;
e1b1d06a 1913
4e022ec0 1914 port_idx = simap_get(&ofproto->ofp_requests, netdev_name);
430dbb14 1915 port_idx = port_idx ? port_idx : UINT16_MAX;
4e022ec0 1916
430dbb14 1917 if (port_idx >= ofproto->max_ports
fdcea803
GS
1918 || ofport_get_usage(ofproto, u16_to_ofp(port_idx)) == LLONG_MAX) {
1919 uint16_t lru_ofport = 0, end_port_no = ofproto->alloc_port_no;
1920 long long int last_used_at, lru = LLONG_MAX;
e1b1d06a 1921
e1b1d06a
JP
1922 /* Search for a free OpenFlow port number. We try not to
1923 * immediately reuse them to prevent problems due to old
484c8355
BP
1924 * flows.
1925 *
1926 * We limit the automatically assigned port numbers to the lower half
1927 * of the port range, to reserve the upper half for assignment by
1928 * controllers. */
6033d9d9 1929 for (;;) {
484c8355 1930 if (++ofproto->alloc_port_no >= MIN(ofproto->max_ports, 32768)) {
fdcea803 1931 ofproto->alloc_port_no = 1;
6033d9d9 1932 }
fdcea803
GS
1933 last_used_at = ofport_get_usage(ofproto,
1934 u16_to_ofp(ofproto->alloc_port_no));
1935 if (!last_used_at) {
430dbb14 1936 port_idx = ofproto->alloc_port_no;
6033d9d9 1937 break;
865b26a4
GS
1938 } else if ( last_used_at < time_msec() - 60*60*1000) {
1939 /* If the port with ofport 'ofproto->alloc_port_no' was deleted
1940 * more than an hour ago, consider it usable. */
1941 ofport_remove_usage(ofproto,
1942 u16_to_ofp(ofproto->alloc_port_no));
1943 port_idx = ofproto->alloc_port_no;
1944 break;
fdcea803
GS
1945 } else if (last_used_at < lru) {
1946 lru = last_used_at;
1947 lru_ofport = ofproto->alloc_port_no;
e1b1d06a 1948 }
fdcea803 1949
430dbb14 1950 if (ofproto->alloc_port_no == end_port_no) {
fdcea803
GS
1951 if (lru_ofport) {
1952 port_idx = lru_ofport;
1953 break;
1954 }
6033d9d9 1955 return OFPP_NONE;
e1b1d06a
JP
1956 }
1957 }
1958 }
fdcea803 1959 ofport_set_usage(ofproto, u16_to_ofp(port_idx), LLONG_MAX);
4e022ec0 1960 return u16_to_ofp(port_idx);
e1b1d06a
JP
1961}
1962
1963static void
fdcea803 1964dealloc_ofp_port(struct ofproto *ofproto, ofp_port_t ofp_port)
e1b1d06a 1965{
430dbb14 1966 if (ofp_to_u16(ofp_port) < ofproto->max_ports) {
fdcea803 1967 ofport_set_usage(ofproto, ofp_port, time_msec());
40fa9417 1968 }
e1b1d06a
JP
1969}
1970
fbfa2911
BP
1971/* Opens and returns a netdev for 'ofproto_port' in 'ofproto', or a null
1972 * pointer if the netdev cannot be opened. On success, also fills in
d7d92203 1973 * '*pp'. */
b33951b8 1974static struct netdev *
e1b1d06a
JP
1975ofport_open(struct ofproto *ofproto,
1976 struct ofproto_port *ofproto_port,
9e1fd49b 1977 struct ofputil_phy_port *pp)
064af421
BP
1978{
1979 enum netdev_flags flags;
064af421 1980 struct netdev *netdev;
064af421
BP
1981 int error;
1982
18812dff 1983 error = netdev_open(ofproto_port->name, ofproto_port->type, &netdev);
064af421 1984 if (error) {
fbfa2911 1985 VLOG_WARN_RL(&rl, "%s: ignoring port %s (%"PRIu16") because netdev %s "
064af421 1986 "cannot be opened (%s)",
fbfa2911 1987 ofproto->name,
abe529af 1988 ofproto_port->name, ofproto_port->ofp_port,
10a89ef0 1989 ofproto_port->name, ovs_strerror(error));
064af421
BP
1990 return NULL;
1991 }
1992
e1b1d06a
JP
1993 if (ofproto_port->ofp_port == OFPP_NONE) {
1994 if (!strcmp(ofproto->name, ofproto_port->name)) {
1995 ofproto_port->ofp_port = OFPP_LOCAL;
1996 } else {
1997 ofproto_port->ofp_port = alloc_ofp_port(ofproto,
1998 ofproto_port->name);
1999 }
2000 }
9e1fd49b
BP
2001 pp->port_no = ofproto_port->ofp_port;
2002 netdev_get_etheraddr(netdev, pp->hw_addr);
2003 ovs_strlcpy(pp->name, ofproto_port->name, sizeof pp->name);
064af421 2004 netdev_get_flags(netdev, &flags);
9e1fd49b
BP
2005 pp->config = flags & NETDEV_UP ? 0 : OFPUTIL_PC_PORT_DOWN;
2006 pp->state = netdev_get_carrier(netdev) ? 0 : OFPUTIL_PS_LINK_DOWN;
2007 netdev_get_features(netdev, &pp->curr, &pp->advertised,
2008 &pp->supported, &pp->peer);
cd65125d
BP
2009 pp->curr_speed = netdev_features_to_bps(pp->curr, 0) / 1000;
2010 pp->max_speed = netdev_features_to_bps(pp->supported, 0) / 1000;
118c4676 2011
b33951b8 2012 return netdev;
064af421
BP
2013}
2014
b33951b8 2015/* Returns true if most fields of 'a' and 'b' are equal. Differences in name,
d7d92203 2016 * port number, and 'config' bits other than OFPUTIL_PC_PORT_DOWN are
b33951b8
BP
2017 * disregarded. */
2018static bool
9e1fd49b
BP
2019ofport_equal(const struct ofputil_phy_port *a,
2020 const struct ofputil_phy_port *b)
064af421 2021{
9e1fd49b 2022 return (eth_addr_equals(a->hw_addr, b->hw_addr)
064af421 2023 && a->state == b->state
9e1fd49b 2024 && !((a->config ^ b->config) & OFPUTIL_PC_PORT_DOWN)
064af421
BP
2025 && a->curr == b->curr
2026 && a->advertised == b->advertised
2027 && a->supported == b->supported
9e1fd49b
BP
2028 && a->peer == b->peer
2029 && a->curr_speed == b->curr_speed
2030 && a->max_speed == b->max_speed);
064af421
BP
2031}
2032
b33951b8
BP
2033/* Adds an ofport to 'p' initialized based on the given 'netdev' and 'opp'.
2034 * The caller must ensure that 'p' does not have a conflicting ofport (that is,
2035 * one with the same name or port number). */
064af421 2036static void
b33951b8 2037ofport_install(struct ofproto *p,
9e1fd49b 2038 struct netdev *netdev, const struct ofputil_phy_port *pp)
064af421 2039{
b33951b8
BP
2040 const char *netdev_name = netdev_get_name(netdev);
2041 struct ofport *ofport;
abe529af 2042 int error;
72b06300 2043
b33951b8 2044 /* Create ofport. */
abe529af
BP
2045 ofport = p->ofproto_class->port_alloc();
2046 if (!ofport) {
2047 error = ENOMEM;
2048 goto error;
2049 }
0f7d71a5 2050 ofport->ofproto = p;
b33951b8 2051 ofport->netdev = netdev;
61501798 2052 ofport->change_seq = netdev_get_change_seq(netdev);
9e1fd49b
BP
2053 ofport->pp = *pp;
2054 ofport->ofp_port = pp->port_no;
65e0be10 2055 ofport->created = time_msec();
b33951b8
BP
2056
2057 /* Add port to 'p'. */
4e022ec0 2058 hmap_insert(&p->ports, &ofport->hmap_node,
f9c0c3ec 2059 hash_ofp_port(ofport->ofp_port));
72b06300 2060 shash_add(&p->port_by_name, netdev_name, ofport);
abe529af 2061
ada3428f 2062 update_mtu(p, ofport);
9197df76 2063
abe529af
BP
2064 /* Let the ofproto_class initialize its private data. */
2065 error = p->ofproto_class->port_construct(ofport);
2066 if (error) {
2067 goto error;
2068 }
2a6f78e0 2069 connmgr_send_port_status(p->connmgr, NULL, pp, OFPPR_ADD);
abe529af
BP
2070 return;
2071
2072error:
2073 VLOG_WARN_RL(&rl, "%s: could not add port %s (%s)",
10a89ef0 2074 p->name, netdev_name, ovs_strerror(error));
abe529af
BP
2075 if (ofport) {
2076 ofport_destroy__(ofport);
2077 } else {
2078 netdev_close(netdev);
72b06300 2079 }
064af421
BP
2080}
2081
b33951b8 2082/* Removes 'ofport' from 'p' and destroys it. */
064af421 2083static void
0f7d71a5 2084ofport_remove(struct ofport *ofport)
064af421 2085{
2a6f78e0 2086 connmgr_send_port_status(ofport->ofproto->connmgr, NULL, &ofport->pp,
fa066f01 2087 OFPPR_DELETE);
abe529af 2088 ofport_destroy(ofport);
b33951b8
BP
2089}
2090
2091/* If 'ofproto' contains an ofport named 'name', removes it from 'ofproto' and
2092 * destroys it. */
2093static void
2094ofport_remove_with_name(struct ofproto *ofproto, const char *name)
2095{
2096 struct ofport *port = shash_find_data(&ofproto->port_by_name, name);
2097 if (port) {
0f7d71a5 2098 ofport_remove(port);
b33951b8
BP
2099 }
2100}
2101
9e1fd49b 2102/* Updates 'port' with new 'pp' description.
b33951b8
BP
2103 *
2104 * Does not handle a name or port number change. The caller must implement
2105 * such a change as a delete followed by an add. */
2106static void
9e1fd49b 2107ofport_modified(struct ofport *port, struct ofputil_phy_port *pp)
b33951b8 2108{
9e1fd49b
BP
2109 memcpy(port->pp.hw_addr, pp->hw_addr, ETH_ADDR_LEN);
2110 port->pp.config = ((port->pp.config & ~OFPUTIL_PC_PORT_DOWN)
2111 | (pp->config & OFPUTIL_PC_PORT_DOWN));
53fd5c7c
BP
2112 port->pp.state = ((port->pp.state & ~OFPUTIL_PS_LINK_DOWN)
2113 | (pp->state & OFPUTIL_PS_LINK_DOWN));
9e1fd49b
BP
2114 port->pp.curr = pp->curr;
2115 port->pp.advertised = pp->advertised;
2116 port->pp.supported = pp->supported;
2117 port->pp.peer = pp->peer;
2118 port->pp.curr_speed = pp->curr_speed;
2119 port->pp.max_speed = pp->max_speed;
b33951b8 2120
2a6f78e0
BP
2121 connmgr_send_port_status(port->ofproto->connmgr, NULL,
2122 &port->pp, OFPPR_MODIFY);
064af421
BP
2123}
2124
5a2dfd47
JP
2125/* Update OpenFlow 'state' in 'port' and notify controller. */
2126void
9e1fd49b 2127ofproto_port_set_state(struct ofport *port, enum ofputil_port_state state)
5a2dfd47 2128{
9e1fd49b
BP
2129 if (port->pp.state != state) {
2130 port->pp.state = state;
2a6f78e0
BP
2131 connmgr_send_port_status(port->ofproto->connmgr, NULL,
2132 &port->pp, OFPPR_MODIFY);
5a2dfd47
JP
2133 }
2134}
2135
abe529af 2136void
4e022ec0 2137ofproto_port_unregister(struct ofproto *ofproto, ofp_port_t ofp_port)
e7934396 2138{
abe529af
BP
2139 struct ofport *port = ofproto_get_port(ofproto, ofp_port);
2140 if (port) {
52a90c29
BP
2141 if (port->ofproto->ofproto_class->set_realdev) {
2142 port->ofproto->ofproto_class->set_realdev(port, 0, 0);
2143 }
b308140a
JP
2144 if (port->ofproto->ofproto_class->set_stp_port) {
2145 port->ofproto->ofproto_class->set_stp_port(port, NULL);
2146 }
abe529af 2147 if (port->ofproto->ofproto_class->set_cfm) {
a5610457 2148 port->ofproto->ofproto_class->set_cfm(port, NULL);
abe529af
BP
2149 }
2150 if (port->ofproto->ofproto_class->bundle_remove) {
2151 port->ofproto->ofproto_class->bundle_remove(port);
e7934396
BP
2152 }
2153 }
2154}
2155
2156static void
abe529af 2157ofport_destroy__(struct ofport *port)
e7934396 2158{
abe529af
BP
2159 struct ofproto *ofproto = port->ofproto;
2160 const char *name = netdev_get_name(port->netdev);
fa066f01 2161
abe529af
BP
2162 hmap_remove(&ofproto->ports, &port->hmap_node);
2163 shash_delete(&ofproto->port_by_name,
2164 shash_find(&ofproto->port_by_name, name));
fa066f01 2165
abe529af
BP
2166 netdev_close(port->netdev);
2167 ofproto->ofproto_class->port_dealloc(port);
e7934396
BP
2168}
2169
064af421 2170static void
abe529af 2171ofport_destroy(struct ofport *port)
064af421 2172{
fa066f01 2173 if (port) {
e1b1d06a 2174 dealloc_ofp_port(port->ofproto, port->ofp_port);
abe529af
BP
2175 port->ofproto->ofproto_class->port_destruct(port);
2176 ofport_destroy__(port);
2177 }
064af421
BP
2178}
2179
abe529af 2180struct ofport *
4e022ec0 2181ofproto_get_port(const struct ofproto *ofproto, ofp_port_t ofp_port)
ca0f572c
BP
2182{
2183 struct ofport *port;
2184
f9c0c3ec 2185 HMAP_FOR_EACH_IN_BUCKET (port, hmap_node, hash_ofp_port(ofp_port),
4e022ec0 2186 &ofproto->ports) {
abe529af 2187 if (port->ofp_port == ofp_port) {
ca0f572c
BP
2188 return port;
2189 }
2190 }
2191 return NULL;
2192}
2193
fdcea803
GS
2194static long long int
2195ofport_get_usage(const struct ofproto *ofproto, ofp_port_t ofp_port)
2196{
2197 struct ofport_usage *usage;
2198
2199 HMAP_FOR_EACH_IN_BUCKET (usage, hmap_node, hash_ofp_port(ofp_port),
2200 &ofproto->ofport_usage) {
2201 if (usage->ofp_port == ofp_port) {
2202 return usage->last_used;
2203 }
2204 }
2205 return 0;
2206}
2207
2208static void
2209ofport_set_usage(struct ofproto *ofproto, ofp_port_t ofp_port,
2210 long long int last_used)
2211{
2212 struct ofport_usage *usage;
2213 HMAP_FOR_EACH_IN_BUCKET (usage, hmap_node, hash_ofp_port(ofp_port),
2214 &ofproto->ofport_usage) {
2215 if (usage->ofp_port == ofp_port) {
2216 usage->last_used = last_used;
2217 return;
2218 }
2219 }
2220 ovs_assert(last_used == LLONG_MAX);
2221
2222 usage = xmalloc(sizeof *usage);
2223 usage->ofp_port = ofp_port;
2224 usage->last_used = last_used;
2225 hmap_insert(&ofproto->ofport_usage, &usage->hmap_node,
2226 hash_ofp_port(ofp_port));
2227}
2228
865b26a4
GS
2229static void
2230ofport_remove_usage(struct ofproto *ofproto, ofp_port_t ofp_port)
2231{
2232 struct ofport_usage *usage;
2233 HMAP_FOR_EACH_IN_BUCKET (usage, hmap_node, hash_ofp_port(ofp_port),
2234 &ofproto->ofport_usage) {
2235 if (usage->ofp_port == ofp_port) {
2236 hmap_remove(&ofproto->ofport_usage, &usage->hmap_node);
2237 free(usage);
2238 break;
2239 }
2240 }
2241}
2242
6527c598
PS
2243int
2244ofproto_port_get_stats(const struct ofport *port, struct netdev_stats *stats)
2245{
2246 struct ofproto *ofproto = port->ofproto;
2247 int error;
2248
2249 if (ofproto->ofproto_class->port_get_stats) {
2250 error = ofproto->ofproto_class->port_get_stats(port, stats);
2251 } else {
2252 error = EOPNOTSUPP;
2253 }
2254
2255 return error;
2256}
2257
064af421 2258static void
b33951b8 2259update_port(struct ofproto *ofproto, const char *name)
064af421 2260{
abe529af 2261 struct ofproto_port ofproto_port;
9e1fd49b 2262 struct ofputil_phy_port pp;
b33951b8
BP
2263 struct netdev *netdev;
2264 struct ofport *port;
064af421
BP
2265
2266 COVERAGE_INC(ofproto_update_port);
c874dc6d 2267
b33951b8 2268 /* Fetch 'name''s location and properties from the datapath. */
abe529af 2269 netdev = (!ofproto_port_query_by_name(ofproto, name, &ofproto_port)
fbfa2911 2270 ? ofport_open(ofproto, &ofproto_port, &pp)
b33951b8 2271 : NULL);
4e022ec0 2272
b33951b8 2273 if (netdev) {
abe529af 2274 port = ofproto_get_port(ofproto, ofproto_port.ofp_port);
b33951b8 2275 if (port && !strcmp(netdev_get_name(port->netdev), name)) {
e65942a0
BP
2276 struct netdev *old_netdev = port->netdev;
2277
b33951b8 2278 /* 'name' hasn't changed location. Any properties changed? */
9e1fd49b
BP
2279 if (!ofport_equal(&port->pp, &pp)) {
2280 ofport_modified(port, &pp);
abe529af
BP
2281 }
2282
ada3428f 2283 update_mtu(ofproto, port);
9197df76 2284
e65942a0
BP
2285 /* Install the newly opened netdev in case it has changed.
2286 * Don't close the old netdev yet in case port_modified has to
2287 * remove a retained reference to it.*/
abe529af 2288 port->netdev = netdev;
61501798 2289 port->change_seq = netdev_get_change_seq(netdev);
abe529af
BP
2290
2291 if (port->ofproto->ofproto_class->port_modified) {
2292 port->ofproto->ofproto_class->port_modified(port);
b33951b8 2293 }
e65942a0
BP
2294
2295 netdev_close(old_netdev);
b33951b8
BP
2296 } else {
2297 /* If 'port' is nonnull then its name differs from 'name' and thus
2298 * we should delete it. If we think there's a port named 'name'
2299 * then its port number must be wrong now so delete it too. */
2300 if (port) {
0f7d71a5 2301 ofport_remove(port);
b33951b8
BP
2302 }
2303 ofport_remove_with_name(ofproto, name);
9e1fd49b 2304 ofport_install(ofproto, netdev, &pp);
c874dc6d 2305 }
b33951b8
BP
2306 } else {
2307 /* Any port named 'name' is gone now. */
2308 ofport_remove_with_name(ofproto, name);
c874dc6d 2309 }
abe529af 2310 ofproto_port_destroy(&ofproto_port);
064af421
BP
2311}
2312
2313static int
2314init_ports(struct ofproto *p)
2315{
abe529af
BP
2316 struct ofproto_port_dump dump;
2317 struct ofproto_port ofproto_port;
e1b1d06a 2318 struct shash_node *node, *next;
abe529af
BP
2319
2320 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, p) {
e1b1d06a
JP
2321 const char *name = ofproto_port.name;
2322
2323 if (shash_find(&p->port_by_name, name)) {
fbfa2911 2324 VLOG_WARN_RL(&rl, "%s: ignoring duplicate device %s in datapath",
e1b1d06a 2325 p->name, name);
abe529af 2326 } else {
9e1fd49b 2327 struct ofputil_phy_port pp;
b33951b8
BP
2328 struct netdev *netdev;
2329
e1b1d06a
JP
2330 /* Check if an OpenFlow port number had been requested. */
2331 node = shash_find(&init_ofp_ports, name);
2332 if (node) {
2333 const struct iface_hint *iface_hint = node->data;
4e022ec0
AW
2334 simap_put(&p->ofp_requests, name,
2335 ofp_to_u16(iface_hint->ofp_port));
e1b1d06a
JP
2336 }
2337
fbfa2911 2338 netdev = ofport_open(p, &ofproto_port, &pp);
b33951b8 2339 if (netdev) {
9e1fd49b 2340 ofport_install(p, netdev, &pp);
430dbb14 2341 if (ofp_to_u16(ofproto_port.ofp_port) < p->max_ports) {
7c35397c 2342 p->alloc_port_no = MAX(p->alloc_port_no,
430dbb14 2343 ofp_to_u16(ofproto_port.ofp_port));
7c35397c 2344 }
064af421
BP
2345 }
2346 }
2347 }
b0ec0f27 2348
e1b1d06a 2349 SHASH_FOR_EACH_SAFE(node, next, &init_ofp_ports) {
4f9e08a5 2350 struct iface_hint *iface_hint = node->data;
e1b1d06a
JP
2351
2352 if (!strcmp(iface_hint->br_name, p->name)) {
2353 free(iface_hint->br_name);
2354 free(iface_hint->br_type);
4f9e08a5 2355 free(iface_hint);
e1b1d06a
JP
2356 shash_delete(&init_ofp_ports, node);
2357 }
2358 }
2359
064af421
BP
2360 return 0;
2361}
9197df76
JP
2362
2363/* Find the minimum MTU of all non-datapath devices attached to 'p'.
2364 * Returns ETH_PAYLOAD_MAX or the minimum of the ports. */
2365static int
2366find_min_mtu(struct ofproto *p)
2367{
2368 struct ofport *ofport;
2369 int mtu = 0;
2370
2371 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
2372 struct netdev *netdev = ofport->netdev;
2373 int dev_mtu;
2374
2375 /* Skip any internal ports, since that's what we're trying to
2376 * set. */
2377 if (!strcmp(netdev_get_type(netdev), "internal")) {
2378 continue;
2379 }
2380
2381 if (netdev_get_mtu(netdev, &dev_mtu)) {
2382 continue;
2383 }
2384 if (!mtu || dev_mtu < mtu) {
2385 mtu = dev_mtu;
2386 }
2387 }
2388
2389 return mtu ? mtu: ETH_PAYLOAD_MAX;
2390}
2391
ada3428f
PS
2392/* Update MTU of all datapath devices on 'p' to the minimum of the
2393 * non-datapath ports in event of 'port' added or changed. */
9197df76 2394static void
ada3428f 2395update_mtu(struct ofproto *p, struct ofport *port)
9197df76
JP
2396{
2397 struct ofport *ofport;
ada3428f
PS
2398 struct netdev *netdev = port->netdev;
2399 int dev_mtu, old_min;
2400
2401 if (netdev_get_mtu(netdev, &dev_mtu)) {
2402 port->mtu = 0;
2403 return;
2404 }
2405 if (!strcmp(netdev_get_type(port->netdev), "internal")) {
2406 if (dev_mtu > p->min_mtu) {
2407 if (!netdev_set_mtu(port->netdev, p->min_mtu)) {
2408 dev_mtu = p->min_mtu;
2409 }
2410 }
2411 port->mtu = dev_mtu;
2412 return;
2413 }
2414
2415 /* For non-internal port find new min mtu. */
2416 old_min = p->min_mtu;
2417 port->mtu = dev_mtu;
2418 p->min_mtu = find_min_mtu(p);
2419 if (p->min_mtu == old_min) {
2420 return;
2421 }
9197df76
JP
2422
2423 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
2424 struct netdev *netdev = ofport->netdev;
2425
2426 if (!strcmp(netdev_get_type(netdev), "internal")) {
ada3428f
PS
2427 if (!netdev_set_mtu(netdev, p->min_mtu)) {
2428 ofport->mtu = p->min_mtu;
2429 }
9197df76
JP
2430 }
2431 }
2432}
064af421 2433\f
f416c8d6
JR
2434static void
2435ofproto_rule_destroy__(struct rule *rule)
2436 OVS_NO_THREAD_SAFETY_ANALYSIS
2437{
2438 cls_rule_destroy(CONST_CAST(struct cls_rule *, &rule->cr));
2439 rule_actions_destroy(rule_get_actions(rule));
2440 ovs_mutex_destroy(&rule->mutex);
2441 rule->ofproto->ofproto_class->rule_dealloc(rule);
2442}
2443
2444static void
2445rule_destroy_cb(struct rule *rule)
2446{
2447 rule->ofproto->ofproto_class->rule_destruct(rule);
2448 ofproto_rule_destroy__(rule);
2449}
2450
a2143702
BP
2451void
2452ofproto_rule_ref(struct rule *rule)
064af421 2453{
1eae3d33 2454 if (rule) {
37bec3d3 2455 ovs_refcount_ref(&rule->ref_count);
a2143702
BP
2456 }
2457}
2458
f416c8d6
JR
2459/* Decrements 'rule''s ref_count and schedules 'rule' to be destroyed if the
2460 * ref_count reaches 0.
2461 *
2462 * Use of RCU allows short term use (between RCU quiescent periods) without
2463 * keeping a reference. A reference must be taken if the rule needs to
2464 * stay around accross the RCU quiescent periods. */
a2143702
BP
2465void
2466ofproto_rule_unref(struct rule *rule)
2467{
37bec3d3 2468 if (rule && ovs_refcount_unref(&rule->ref_count) == 1) {
f416c8d6 2469 ovsrcu_postpone(rule_destroy_cb, rule);
1eae3d33 2470 }
064af421
BP
2471}
2472
809c7548
RW
2473void
2474ofproto_group_ref(struct ofgroup *group)
2475{
2476 if (group) {
2477 ovs_refcount_ref(&group->ref_count);
2478 }
2479}
2480
2481void
2482ofproto_group_unref(struct ofgroup *group)
2483{
2484 if (group && ovs_refcount_unref(&group->ref_count) == 1) {
2485 group->ofproto->ofproto_class->group_destruct(group);
2486 ofputil_bucket_list_destroy(&group->buckets);
2487 group->ofproto->ofproto_class->group_dealloc(group);
2488 }
2489}
2490
65efd2ab
JR
2491static uint32_t get_provider_meter_id(const struct ofproto *,
2492 uint32_t of_meter_id);
2493
dc723c44
JR
2494/* Creates and returns a new 'struct rule_actions', whose actions are a copy
2495 * of from the 'ofpacts_len' bytes of 'ofpacts'. */
2496const struct rule_actions *
65efd2ab
JR
2497rule_actions_create(const struct ofproto *ofproto,
2498 const struct ofpact *ofpacts, size_t ofpacts_len)
6f00e29b
BP
2499{
2500 struct rule_actions *actions;
2501
dc723c44 2502 actions = xmalloc(sizeof *actions + ofpacts_len);
6f00e29b 2503 actions->ofpacts_len = ofpacts_len;
65efd2ab
JR
2504 actions->provider_meter_id
2505 = get_provider_meter_id(ofproto,
2506 ofpacts_get_meter(ofpacts, ofpacts_len));
dc723c44 2507 memcpy(actions->ofpacts, ofpacts, ofpacts_len);
65efd2ab 2508
6f00e29b
BP
2509 return actions;
2510}
2511
dc723c44 2512/* Free the actions after the RCU quiescent period is reached. */
6f00e29b 2513void
dc723c44 2514rule_actions_destroy(const struct rule_actions *actions)
6f00e29b 2515{
06a0f3e2 2516 if (actions) {
dc723c44 2517 ovsrcu_postpone(free, CONST_CAST(struct rule_actions *, actions));
6f00e29b
BP
2518 }
2519}
2520
bcf84111 2521/* Returns true if 'rule' has an OpenFlow OFPAT_OUTPUT or OFPAT_ENQUEUE action
f25d0cf3 2522 * that outputs to 'port' (output to OFPP_FLOOD and OFPP_ALL doesn't count). */
74e79b7c 2523static bool
4e022ec0 2524ofproto_rule_has_out_port(const struct rule *rule, ofp_port_t port)
15aaf599 2525 OVS_REQUIRES(ofproto_mutex)
064af421 2526{
06a0f3e2
BP
2527 if (port == OFPP_ANY) {
2528 return true;
2529 } else {
2530 const struct rule_actions *actions = rule_get_actions(rule);
2531 return ofpacts_output_to_port(actions->ofpacts,
2532 actions->ofpacts_len, port);
2533 }
064af421
BP
2534}
2535
7395c052 2536/* Returns true if 'rule' has group and equals group_id. */
74e79b7c 2537static bool
7395c052 2538ofproto_rule_has_out_group(const struct rule *rule, uint32_t group_id)
15aaf599 2539 OVS_REQUIRES(ofproto_mutex)
7395c052 2540{
06a0f3e2
BP
2541 if (group_id == OFPG_ANY) {
2542 return true;
2543 } else {
2544 const struct rule_actions *actions = rule_get_actions(rule);
2545 return ofpacts_output_to_group(actions->ofpacts,
2546 actions->ofpacts_len, group_id);
2547 }
7395c052
NZ
2548}
2549
35412852
BP
2550static void
2551rule_execute_destroy(struct rule_execute *e)
eedc0097 2552{
35412852
BP
2553 ofproto_rule_unref(e->rule);
2554 list_remove(&e->list_node);
2555 free(e);
2556}
eedc0097 2557
35412852
BP
2558/* Executes all "rule_execute" operations queued up in ofproto->rule_executes,
2559 * by passing them to the ofproto provider. */
2560static void
2561run_rule_executes(struct ofproto *ofproto)
15aaf599 2562 OVS_EXCLUDED(ofproto_mutex)
35412852
BP
2563{
2564 struct rule_execute *e, *next;
2565 struct list executes;
2566
2567 guarded_list_pop_all(&ofproto->rule_executes, &executes);
2568 LIST_FOR_EACH_SAFE (e, next, list_node, &executes) {
35412852
BP
2569 struct flow flow;
2570
b5e7e61a
AZ
2571 flow_extract(e->packet, NULL, &flow);
2572 flow.in_port.ofp_port = e->in_port;
35412852
BP
2573 ofproto->ofproto_class->rule_execute(e->rule, &flow, e->packet);
2574
2575 rule_execute_destroy(e);
2576 }
2577}
2578
2579/* Destroys and discards all "rule_execute" operations queued up in
2580 * ofproto->rule_executes. */
2581static void
2582destroy_rule_executes(struct ofproto *ofproto)
2583{
2584 struct rule_execute *e, *next;
2585 struct list executes;
2586
2587 guarded_list_pop_all(&ofproto->rule_executes, &executes);
2588 LIST_FOR_EACH_SAFE (e, next, list_node, &executes) {
2589 ofpbuf_delete(e->packet);
2590 rule_execute_destroy(e);
2591 }
350a665f
BP
2592}
2593
adcf00ba 2594static bool
834fe5cb 2595rule_is_readonly(const struct rule *rule)
5c67e4af 2596{
834fe5cb
BP
2597 const struct oftable *table = &rule->ofproto->tables[rule->table_id];
2598 return (table->flags & OFTABLE_READONLY) != 0;
5c67e4af 2599}
fa066f01 2600\f
90bf1e07 2601static enum ofperr
d1e2cf21 2602handle_echo_request(struct ofconn *ofconn, const struct ofp_header *oh)
064af421 2603{
b0421aa2 2604 ofconn_send_reply(ofconn, make_echo_reply(oh));
064af421 2605 return 0;
064af421
BP
2606}
2607
90bf1e07 2608static enum ofperr
d1e2cf21 2609handle_features_request(struct ofconn *ofconn, const struct ofp_header *oh)
064af421 2610{
64ff1d96 2611 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
9e1fd49b 2612 struct ofputil_switch_features features;
064af421 2613 struct ofport *port;
6c1491fb 2614 bool arp_match_ip;
9e1fd49b 2615 struct ofpbuf *b;
064af421 2616
9e1fd49b
BP
2617 ofproto->ofproto_class->get_features(ofproto, &arp_match_ip,
2618 &features.actions);
cb22974d 2619 ovs_assert(features.actions & OFPUTIL_A_OUTPUT); /* sanity check */
064af421 2620
9e1fd49b
BP
2621 features.datapath_id = ofproto->datapath_id;
2622 features.n_buffers = pktbuf_capacity();
adcf00ba 2623 features.n_tables = ofproto_get_n_visible_tables(ofproto);
9e1fd49b
BP
2624 features.capabilities = (OFPUTIL_C_FLOW_STATS | OFPUTIL_C_TABLE_STATS |
2625 OFPUTIL_C_PORT_STATS | OFPUTIL_C_QUEUE_STATS);
6c1491fb 2626 if (arp_match_ip) {
9e1fd49b 2627 features.capabilities |= OFPUTIL_C_ARP_MATCH_IP;
6c1491fb 2628 }
2e1ae200
JR
2629 /* FIXME: Fill in proper features.auxiliary_id for auxiliary connections */
2630 features.auxiliary_id = 0;
9e1fd49b
BP
2631 b = ofputil_encode_switch_features(&features, ofconn_get_protocol(ofconn),
2632 oh->xid);
64ff1d96 2633 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
9e1fd49b 2634 ofputil_put_switch_features_port(&port->pp, b);
064af421 2635 }
064af421 2636
9e1fd49b 2637 ofconn_send_reply(ofconn, b);
064af421
BP
2638 return 0;
2639}
064af421 2640
90bf1e07 2641static enum ofperr
d1e2cf21 2642handle_get_config_request(struct ofconn *ofconn, const struct ofp_header *oh)
064af421 2643{
64ff1d96 2644 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
064af421 2645 struct ofp_switch_config *osc;
f0fd1a17 2646 enum ofp_config_flags flags;
7257b535 2647 struct ofpbuf *buf;
959a2ecd 2648
064af421 2649 /* Send reply. */
982697a4
BP
2650 buf = ofpraw_alloc_reply(OFPRAW_OFPT_GET_CONFIG_REPLY, oh, 0);
2651 osc = ofpbuf_put_uninit(buf, sizeof *osc);
f0fd1a17 2652 flags = ofproto->frag_handling;
2e1ae200
JR
2653 /* OFPC_INVALID_TTL_TO_CONTROLLER is deprecated in OF 1.3 */
2654 if (oh->version < OFP13_VERSION
2655 && ofconn_get_invalid_ttl_to_controller(ofconn)) {
f0fd1a17
PS
2656 flags |= OFPC_INVALID_TTL_TO_CONTROLLER;
2657 }
2658 osc->flags = htons(flags);
810605a2 2659 osc->miss_send_len = htons(ofconn_get_miss_send_len(ofconn));
b0421aa2 2660 ofconn_send_reply(ofconn, buf);
2d70a31a 2661
064af421
BP
2662 return 0;
2663}
064af421 2664
90bf1e07 2665static enum ofperr
982697a4 2666handle_set_config(struct ofconn *ofconn, const struct ofp_header *oh)
064af421 2667{
982697a4 2668 const struct ofp_switch_config *osc = ofpmsg_body(oh);
64ff1d96 2669 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
d1e2cf21 2670 uint16_t flags = ntohs(osc->flags);
2d70a31a 2671
7257b535 2672 if (ofconn_get_type(ofconn) != OFCONN_PRIMARY
f4f1ea7e 2673 || ofconn_get_role(ofconn) != OFPCR12_ROLE_SLAVE) {
7257b535
BP
2674 enum ofp_config_flags cur = ofproto->frag_handling;
2675 enum ofp_config_flags next = flags & OFPC_FRAG_MASK;
2676
cb22974d 2677 ovs_assert((cur & OFPC_FRAG_MASK) == cur);
7257b535
BP
2678 if (cur != next) {
2679 if (ofproto->ofproto_class->set_frag_handling(ofproto, next)) {
2680 ofproto->frag_handling = next;
2681 } else {
2682 VLOG_WARN_RL(&rl, "%s: unsupported fragment handling mode %s",
2683 ofproto->name,
2684 ofputil_frag_handling_to_string(next));
2685 }
064af421
BP
2686 }
2687 }
2e1ae200 2688 /* OFPC_INVALID_TTL_TO_CONTROLLER is deprecated in OF 1.3 */
f0fd1a17 2689 ofconn_set_invalid_ttl_to_controller(ofconn,
2e1ae200
JR
2690 (oh->version < OFP13_VERSION
2691 && flags & OFPC_INVALID_TTL_TO_CONTROLLER));
ebe482fd 2692
810605a2 2693 ofconn_set_miss_send_len(ofconn, ntohs(osc->miss_send_len));
0ad9b732 2694
064af421 2695 return 0;
064af421
BP
2696}
2697
9deba63b 2698/* Checks whether 'ofconn' is a slave controller. If so, returns an OpenFlow
90bf1e07
BP
2699 * error message code for the caller to propagate upward. Otherwise, returns
2700 * 0.
2701 *
2702 * The log message mentions 'msg_type'. */
2703static enum ofperr
2704reject_slave_controller(struct ofconn *ofconn)
9deba63b 2705{
1ce0a5fa 2706 if (ofconn_get_type(ofconn) == OFCONN_PRIMARY
f4f1ea7e 2707 && ofconn_get_role(ofconn) == OFPCR12_ROLE_SLAVE) {
90bf1e07 2708 return OFPERR_OFPBRC_EPERM;
9deba63b
BP
2709 } else {
2710 return 0;
2711 }
2712}
2713
7e9f8266
BP
2714/* Checks that the 'ofpacts_len' bytes of action in 'ofpacts' are appropriate
2715 * for 'ofproto':
2716 *
2717 * - If they use a meter, then 'ofproto' has that meter configured.
2718 *
2719 * - If they use any groups, then 'ofproto' has that group configured.
2720 *
2721 * Returns 0 if successful, otherwise an OpenFlow error. */
9cae45dc
JR
2722static enum ofperr
2723ofproto_check_ofpacts(struct ofproto *ofproto,
7e9f8266 2724 const struct ofpact ofpacts[], size_t ofpacts_len)
9cae45dc 2725{
84d68566 2726 const struct ofpact *a;
9cae45dc
JR
2727 uint32_t mid;
2728
7e9f8266
BP
2729 mid = ofpacts_get_meter(ofpacts, ofpacts_len);
2730 if (mid && get_provider_meter_id(ofproto, mid) == UINT32_MAX) {
2731 return OFPERR_OFPMMFC_INVALID_METER;
9cae45dc
JR
2732 }
2733
84d68566 2734 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
7e9f8266
BP
2735 if (a->type == OFPACT_GROUP
2736 && !ofproto_group_exists(ofproto, ofpact_get_GROUP(a)->group_id)) {
2737 return OFPERR_OFPBAC_BAD_OUT_GROUP;
84d68566
SH
2738 }
2739 }
2740
9cae45dc
JR
2741 return 0;
2742}
2743
90bf1e07 2744static enum ofperr
982697a4 2745handle_packet_out(struct ofconn *ofconn, const struct ofp_header *oh)
064af421 2746{
64ff1d96 2747 struct ofproto *p = ofconn_get_ofproto(ofconn);
c6a93eb7
BP
2748 struct ofputil_packet_out po;
2749 struct ofpbuf *payload;
f25d0cf3
BP
2750 uint64_t ofpacts_stub[1024 / 8];
2751 struct ofpbuf ofpacts;
ae412e7d 2752 struct flow flow;
90bf1e07 2753 enum ofperr error;
064af421 2754
ac51afaf
BP
2755 COVERAGE_INC(ofproto_packet_out);
2756
76589937 2757 error = reject_slave_controller(ofconn);
9deba63b 2758 if (error) {
f25d0cf3 2759 goto exit;
9deba63b
BP
2760 }
2761
c6a93eb7 2762 /* Decode message. */
f25d0cf3 2763 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
982697a4 2764 error = ofputil_decode_packet_out(&po, oh, &ofpacts);
064af421 2765 if (error) {
f25d0cf3 2766 goto exit_free_ofpacts;
064af421 2767 }
430dbb14 2768 if (ofp_to_u16(po.in_port) >= p->max_ports
4e022ec0 2769 && ofp_to_u16(po.in_port) < ofp_to_u16(OFPP_MAX)) {
2e1bfcb6 2770 error = OFPERR_OFPBRC_BAD_PORT;
91858960
BP
2771 goto exit_free_ofpacts;
2772 }
064af421 2773
ac51afaf 2774 /* Get payload. */
c6a93eb7
BP
2775 if (po.buffer_id != UINT32_MAX) {
2776 error = ofconn_pktbuf_retrieve(ofconn, po.buffer_id, &payload, NULL);
2777 if (error || !payload) {
f25d0cf3 2778 goto exit_free_ofpacts;
064af421 2779 }
064af421 2780 } else {
bb622f82
BP
2781 /* Ensure that the L3 header is 32-bit aligned. */
2782 payload = ofpbuf_clone_data_with_headroom(po.packet, po.packet_len, 2);
e1154f71
BP
2783 }
2784
548de4dd 2785 /* Verify actions against packet, then send packet if successful. */
b5e7e61a
AZ
2786 flow_extract(payload, NULL, &flow);
2787 flow.in_port.ofp_port = po.in_port;
7e9f8266 2788 error = ofproto_check_ofpacts(p, po.ofpacts, po.ofpacts_len);
548de4dd
BP
2789 if (!error) {
2790 error = p->ofproto_class->packet_out(p, payload, &flow,
2791 po.ofpacts, po.ofpacts_len);
2792 }
c6a93eb7 2793 ofpbuf_delete(payload);
abe529af 2794
f25d0cf3
BP
2795exit_free_ofpacts:
2796 ofpbuf_uninit(&ofpacts);
2797exit:
abe529af 2798 return error;
064af421
BP
2799}
2800
2801static void
2a6f78e0 2802update_port_config(struct ofconn *ofconn, struct ofport *port,
9e1fd49b
BP
2803 enum ofputil_port_config config,
2804 enum ofputil_port_config mask)
064af421 2805{
2a6f78e0 2806 enum ofputil_port_config toggle = (config ^ port->pp.config) & mask;
abe529af 2807
2a6f78e0
BP
2808 if (toggle & OFPUTIL_PC_PORT_DOWN
2809 && (config & OFPUTIL_PC_PORT_DOWN
2810 ? netdev_turn_flags_off(port->netdev, NETDEV_UP, NULL)
2811 : netdev_turn_flags_on(port->netdev, NETDEV_UP, NULL))) {
2812 /* We tried to bring the port up or down, but it failed, so don't
2813 * update the "down" bit. */
9e1fd49b 2814 toggle &= ~OFPUTIL_PC_PORT_DOWN;
064af421 2815 }
abe529af 2816
2a6f78e0
BP
2817 if (toggle) {
2818 enum ofputil_port_config old_config = port->pp.config;
2819 port->pp.config ^= toggle;
abe529af 2820 port->ofproto->ofproto_class->port_reconfigured(port, old_config);
2a6f78e0
BP
2821 connmgr_send_port_status(port->ofproto->connmgr, ofconn, &port->pp,
2822 OFPPR_MODIFY);
064af421
BP
2823 }
2824}
2825
90bf1e07 2826static enum ofperr
d1e2cf21 2827handle_port_mod(struct ofconn *ofconn, const struct ofp_header *oh)
064af421 2828{
64ff1d96 2829 struct ofproto *p = ofconn_get_ofproto(ofconn);
9e1fd49b 2830 struct ofputil_port_mod pm;
064af421 2831 struct ofport *port;
9e1fd49b 2832 enum ofperr error;
064af421 2833
76589937 2834 error = reject_slave_controller(ofconn);
9deba63b
BP
2835 if (error) {
2836 return error;
2837 }
064af421 2838
18cc69d9 2839 error = ofputil_decode_port_mod(oh, &pm, false);
9e1fd49b
BP
2840 if (error) {
2841 return error;
2842 }
2843
2844 port = ofproto_get_port(p, pm.port_no);
064af421 2845 if (!port) {
90bf1e07 2846 return OFPERR_OFPPMFC_BAD_PORT;
9e1fd49b 2847 } else if (!eth_addr_equals(port->pp.hw_addr, pm.hw_addr)) {
90bf1e07 2848 return OFPERR_OFPPMFC_BAD_HW_ADDR;
064af421 2849 } else {
2a6f78e0 2850 update_port_config(ofconn, port, pm.config, pm.mask);
9e1fd49b
BP
2851 if (pm.advertise) {
2852 netdev_set_advertisements(port->netdev, pm.advertise);
064af421
BP
2853 }
2854 }
2855 return 0;
2856}
2857
90bf1e07 2858static enum ofperr
3269c562 2859handle_desc_stats_request(struct ofconn *ofconn,
982697a4 2860 const struct ofp_header *request)
064af421 2861{
061bfea4
BP
2862 static const char *default_mfr_desc = "Nicira, Inc.";
2863 static const char *default_hw_desc = "Open vSwitch";
2864 static const char *default_sw_desc = VERSION;
2865 static const char *default_serial_desc = "None";
2866 static const char *default_dp_desc = "None";
2867
64ff1d96 2868 struct ofproto *p = ofconn_get_ofproto(ofconn);
064af421
BP
2869 struct ofp_desc_stats *ods;
2870 struct ofpbuf *msg;
2871
982697a4
BP
2872 msg = ofpraw_alloc_stats_reply(request, 0);
2873 ods = ofpbuf_put_zeros(msg, sizeof *ods);
061bfea4
BP
2874 ovs_strlcpy(ods->mfr_desc, p->mfr_desc ? p->mfr_desc : default_mfr_desc,
2875 sizeof ods->mfr_desc);
2876 ovs_strlcpy(ods->hw_desc, p->hw_desc ? p->hw_desc : default_hw_desc,
2877 sizeof ods->hw_desc);
2878 ovs_strlcpy(ods->sw_desc, p->sw_desc ? p->sw_desc : default_sw_desc,
2879 sizeof ods->sw_desc);
2880 ovs_strlcpy(ods->serial_num,
2881 p->serial_desc ? p->serial_desc : default_serial_desc,
2882 sizeof ods->serial_num);
2883 ovs_strlcpy(ods->dp_desc, p->dp_desc ? p->dp_desc : default_dp_desc,
2884 sizeof ods->dp_desc);
b0421aa2 2885 ofconn_send_reply(ofconn, msg);
064af421
BP
2886
2887 return 0;
2888}
2889
90bf1e07 2890static enum ofperr
3269c562 2891handle_table_stats_request(struct ofconn *ofconn,
982697a4 2892 const struct ofp_header *request)
064af421 2893{
64ff1d96 2894 struct ofproto *p = ofconn_get_ofproto(ofconn);
307975da 2895 struct ofp12_table_stats *ots;
064af421 2896 struct ofpbuf *msg;
c2f0373a 2897 int n_tables;
6c1491fb 2898 size_t i;
064af421 2899
307975da
SH
2900 /* Set up default values.
2901 *
2902 * ofp12_table_stats is used as a generic structure as
2903 * it is able to hold all the fields for ofp10_table_stats
2904 * and ofp11_table_stats (and of course itself).
2905 */
2906 ots = xcalloc(p->n_tables, sizeof *ots);
6c1491fb
BP
2907 for (i = 0; i < p->n_tables; i++) {
2908 ots[i].table_id = i;
34582733 2909 sprintf(ots[i].name, "table%"PRIuSIZE, i);
6240624b
BP
2910 ots[i].match = htonll(OFPXMT13_MASK);
2911 ots[i].wildcards = htonll(OFPXMT13_MASK);
e78b61f6
BP
2912 ots[i].write_actions = htonl(OFPAT11_OUTPUT);
2913 ots[i].apply_actions = htonl(OFPAT11_OUTPUT);
6240624b
BP
2914 ots[i].write_setfields = htonll(OFPXMT13_MASK);
2915 ots[i].apply_setfields = htonll(OFPXMT13_MASK);
b8266395
BP
2916 ots[i].metadata_match = OVS_BE64_MAX;
2917 ots[i].metadata_write = OVS_BE64_MAX;
307975da
SH
2918 ots[i].instructions = htonl(OFPIT11_ALL);
2919 ots[i].config = htonl(OFPTC11_TABLE_MISS_MASK);
6c1491fb 2920 ots[i].max_entries = htonl(1000000); /* An arbitrary big number. */
06f81620 2921 fat_rwlock_rdlock(&p->tables[i].cls.rwlock);
d0918789 2922 ots[i].active_count = htonl(classifier_count(&p->tables[i].cls));
06f81620 2923 fat_rwlock_unlock(&p->tables[i].cls.rwlock);
6c1491fb 2924 }
064af421 2925
6c1491fb 2926 p->ofproto_class->get_tables(p, ots);
064af421 2927
c2f0373a
BP
2928 /* Post-process the tables, dropping hidden tables. */
2929 n_tables = p->n_tables;
254750ce
BP
2930 for (i = 0; i < p->n_tables; i++) {
2931 const struct oftable *table = &p->tables[i];
2932
c2f0373a
BP
2933 if (table->flags & OFTABLE_HIDDEN) {
2934 n_tables = i;
2935 break;
2936 }
2937
254750ce
BP
2938 if (table->name) {
2939 ovs_strzcpy(ots[i].name, table->name, sizeof ots[i].name);
2940 }
2941
2942 if (table->max_flows < ntohl(ots[i].max_entries)) {
2943 ots[i].max_entries = htonl(table->max_flows);
2944 }
2945 }
2946
c2f0373a 2947 msg = ofputil_encode_table_stats_reply(ots, n_tables, request);
b0421aa2 2948 ofconn_send_reply(ofconn, msg);
307975da
SH
2949
2950 free(ots);
2951
064af421
BP
2952 return 0;
2953}
2954
abaad8cf 2955static void
63f2140a 2956append_port_stat(struct ofport *port, struct list *replies)
abaad8cf 2957{
f8e4867e 2958 struct ofputil_port_stats ops = { .port_no = port->pp.port_no };
abaad8cf 2959
65e0be10
BP
2960 calc_duration(port->created, time_msec(),
2961 &ops.duration_sec, &ops.duration_nsec);
2962
d295e8e9
JP
2963 /* Intentionally ignore return value, since errors will set
2964 * 'stats' to all-1s, which is correct for OpenFlow, and
abaad8cf 2965 * netdev_get_stats() will log errors. */
f8e4867e
SH
2966 ofproto_port_get_stats(port, &ops.stats);
2967
2968 ofputil_append_port_stat(replies, &ops);
abaad8cf
JP
2969}
2970
70ae4f93
BP
2971static void
2972handle_port_request(struct ofconn *ofconn,
2973 const struct ofp_header *request, ofp_port_t port_no,
2974 void (*cb)(struct ofport *, struct list *replies))
064af421 2975{
70ae4f93 2976 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
064af421 2977 struct ofport *port;
63f2140a 2978 struct list replies;
064af421 2979
982697a4 2980 ofpmp_init(&replies, request);
7f05e7ab 2981 if (port_no != OFPP_ANY) {
70ae4f93 2982 port = ofproto_get_port(ofproto, port_no);
abaad8cf 2983 if (port) {
70ae4f93 2984 cb(port, &replies);
abaad8cf
JP
2985 }
2986 } else {
70ae4f93
BP
2987 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
2988 cb(port, &replies);
abaad8cf 2989 }
064af421
BP
2990 }
2991
63f2140a 2992 ofconn_send_replies(ofconn, &replies);
70ae4f93
BP
2993}
2994
2995static enum ofperr
2996handle_port_stats_request(struct ofconn *ofconn,
2997 const struct ofp_header *request)
2998{
2999 ofp_port_t port_no;
3000 enum ofperr error;
3001
3002 error = ofputil_decode_port_stats_request(request, &port_no);
3003 if (!error) {
3004 handle_port_request(ofconn, request, port_no, append_port_stat);
3005 }
3006 return error;
3007}
3008
3009static void
3010append_port_desc(struct ofport *port, struct list *replies)
3011{
3012 ofputil_append_port_desc_stats_reply(&port->pp, replies);
064af421
BP
3013}
3014
2be393ed
JP
3015static enum ofperr
3016handle_port_desc_stats_request(struct ofconn *ofconn,
982697a4 3017 const struct ofp_header *request)
2be393ed 3018{
70ae4f93
BP
3019 ofp_port_t port_no;
3020 enum ofperr error;
2be393ed 3021
70ae4f93
BP
3022 error = ofputil_decode_port_desc_stats_request(request, &port_no);
3023 if (!error) {
3024 handle_port_request(ofconn, request, port_no, append_port_desc);
2be393ed 3025 }
70ae4f93 3026 return error;
2be393ed
JP
3027}
3028
98eaac36
JR
3029static uint32_t
3030hash_cookie(ovs_be64 cookie)
3031{
965607c8 3032 return hash_uint64((OVS_FORCE uint64_t)cookie);
98eaac36
JR
3033}
3034
3035static void
3036cookies_insert(struct ofproto *ofproto, struct rule *rule)
2c916028 3037 OVS_REQUIRES(ofproto_mutex)
98eaac36
JR
3038{
3039 hindex_insert(&ofproto->cookies, &rule->cookie_node,
3040 hash_cookie(rule->flow_cookie));
3041}
3042
3043static void
3044cookies_remove(struct ofproto *ofproto, struct rule *rule)
2c916028 3045 OVS_REQUIRES(ofproto_mutex)
98eaac36
JR
3046{
3047 hindex_remove(&ofproto->cookies, &rule->cookie_node);
3048}
3049
c6ebb8fb 3050static void
65e0be10
BP
3051calc_duration(long long int start, long long int now,
3052 uint32_t *sec, uint32_t *nsec)
c6ebb8fb 3053{
f27f2134 3054 long long int msecs = now - start;
588cd7b5
BP
3055 *sec = msecs / 1000;
3056 *nsec = (msecs % 1000) * (1000 * 1000);
3057}
3058
48266274 3059/* Checks whether 'table_id' is 0xff or a valid table ID in 'ofproto'. Returns
554764d1
SH
3060 * true if 'table_id' is OK, false otherwise. */
3061static bool
48266274
BP
3062check_table_id(const struct ofproto *ofproto, uint8_t table_id)
3063{
554764d1 3064 return table_id == OFPTT_ALL || table_id < ofproto->n_tables;
48266274
BP
3065}
3066
5c67e4af 3067static struct oftable *
d4ce8a49 3068next_visible_table(const struct ofproto *ofproto, uint8_t table_id)
5c67e4af
BP
3069{
3070 struct oftable *table;
3071
3072 for (table = &ofproto->tables[table_id];
3073 table < &ofproto->tables[ofproto->n_tables];
3074 table++) {
3075 if (!(table->flags & OFTABLE_HIDDEN)) {
3076 return table;
3077 }
3078 }
3079
3080 return NULL;
3081}
3082
d0918789 3083static struct oftable *
d4ce8a49 3084first_matching_table(const struct ofproto *ofproto, uint8_t table_id)
064af421 3085{
6c1491fb 3086 if (table_id == 0xff) {
5c67e4af 3087 return next_visible_table(ofproto, 0);
6c1491fb
BP
3088 } else if (table_id < ofproto->n_tables) {
3089 return &ofproto->tables[table_id];
a02e5331 3090 } else {
6c1491fb 3091 return NULL;
a02e5331 3092 }
064af421
BP
3093}
3094
d0918789 3095static struct oftable *
d4ce8a49
BP
3096next_matching_table(const struct ofproto *ofproto,
3097 const struct oftable *table, uint8_t table_id)
6c1491fb 3098{
5c67e4af
BP
3099 return (table_id == 0xff
3100 ? next_visible_table(ofproto, (table - ofproto->tables) + 1)
6c1491fb
BP
3101 : NULL);
3102}
3103
d0918789 3104/* Assigns TABLE to each oftable, in turn, that matches TABLE_ID in OFPROTO:
6c1491fb
BP
3105 *
3106 * - If TABLE_ID is 0xff, this iterates over every classifier table in
5c67e4af 3107 * OFPROTO, skipping tables marked OFTABLE_HIDDEN.
6c1491fb
BP
3108 *
3109 * - If TABLE_ID is the number of a table in OFPROTO, then the loop iterates
5c67e4af
BP
3110 * only once, for that table. (This can be used to access tables marked
3111 * OFTABLE_HIDDEN.)
6c1491fb 3112 *
48266274
BP
3113 * - Otherwise, TABLE_ID isn't valid for OFPROTO, so the loop won't be
3114 * entered at all. (Perhaps you should have validated TABLE_ID with
3115 * check_table_id().)
6c1491fb
BP
3116 *
3117 * All parameters are evaluated multiple times.
3118 */
d0918789
BP
3119#define FOR_EACH_MATCHING_TABLE(TABLE, TABLE_ID, OFPROTO) \
3120 for ((TABLE) = first_matching_table(OFPROTO, TABLE_ID); \
3121 (TABLE) != NULL; \
3122 (TABLE) = next_matching_table(OFPROTO, TABLE, TABLE_ID))
6c1491fb 3123
a9b22b7f
BP
3124/* Initializes 'criteria' in a straightforward way based on the other
3125 * parameters.
3126 *
dd51dae2
BP
3127 * By default, the criteria include flows that are read-only, on the assumption
3128 * that the collected flows won't be modified. Call rule_criteria_require_rw()
3129 * if flows will be modified.
3130 *
a9b22b7f
BP
3131 * For "loose" matching, the 'priority' parameter is unimportant and may be
3132 * supplied as 0. */
3133static void
3134rule_criteria_init(struct rule_criteria *criteria, uint8_t table_id,
3135 const struct match *match, unsigned int priority,
3136 ovs_be64 cookie, ovs_be64 cookie_mask,
3137 ofp_port_t out_port, uint32_t out_group)
3138{
3139 criteria->table_id = table_id;
3140 cls_rule_init(&criteria->cr, match, priority);
3141 criteria->cookie = cookie;
3142 criteria->cookie_mask = cookie_mask;
3143 criteria->out_port = out_port;
3144 criteria->out_group = out_group;
dd51dae2
BP
3145
3146 /* We ordinarily want to skip hidden rules, but there has to be a way for
3147 * code internal to OVS to modify and delete them, so if the criteria
3148 * specify a priority that can only be for a hidden flow, then allow hidden
3149 * rules to be selected. (This doesn't allow OpenFlow clients to meddle
3150 * with hidden flows because OpenFlow uses only a 16-bit field to specify
3151 * priority.) */
3152 criteria->include_hidden = priority > UINT16_MAX;
3153
3154 /* We assume that the criteria are being used to collect flows for reading
3155 * but not modification. Thus, we should collect read-only flows. */
3156 criteria->include_readonly = true;
3157}
3158
3159/* By default, criteria initialized by rule_criteria_init() will match flows
3160 * that are read-only, on the assumption that the collected flows won't be
3161 * modified. Call this function to match only flows that are be modifiable.
3162 *
3163 * Specify 'can_write_readonly' as false in ordinary circumstances, true if the
3164 * caller has special privileges that allow it to modify even "read-only"
3165 * flows. */
3166static void
3167rule_criteria_require_rw(struct rule_criteria *criteria,
3168 bool can_write_readonly)
3169{
3170 criteria->include_readonly = can_write_readonly;
a9b22b7f
BP
3171}
3172
3173static void
3174rule_criteria_destroy(struct rule_criteria *criteria)
3175{
3176 cls_rule_destroy(&criteria->cr);
3177}
3178
a8e547c1
BP
3179void
3180rule_collection_init(struct rule_collection *rules)
3181{
3182 rules->rules = rules->stub;
3183 rules->n = 0;
3184 rules->capacity = ARRAY_SIZE(rules->stub);
3185}
3186
3187void
3188rule_collection_add(struct rule_collection *rules, struct rule *rule)
3189{
3190 if (rules->n >= rules->capacity) {
3191 size_t old_size, new_size;
3192
3193 old_size = rules->capacity * sizeof *rules->rules;
3194 rules->capacity *= 2;
3195 new_size = rules->capacity * sizeof *rules->rules;
3196
3197 if (rules->rules == rules->stub) {
3198 rules->rules = xmalloc(new_size);
3199 memcpy(rules->rules, rules->stub, old_size);
3200 } else {
3201 rules->rules = xrealloc(rules->rules, new_size);
3202 }
3203 }
3204
3205 rules->rules[rules->n++] = rule;
3206}
3207
15aaf599
BP
3208void
3209rule_collection_ref(struct rule_collection *rules)
3210 OVS_REQUIRES(ofproto_mutex)
3211{
3212 size_t i;
3213
3214 for (i = 0; i < rules->n; i++) {
3215 ofproto_rule_ref(rules->rules[i]);
3216 }
3217}
3218
3219void
3220rule_collection_unref(struct rule_collection *rules)
3221{
3222 size_t i;
3223
3224 for (i = 0; i < rules->n; i++) {
3225 ofproto_rule_unref(rules->rules[i]);
3226 }
3227}
3228
a8e547c1
BP
3229void
3230rule_collection_destroy(struct rule_collection *rules)
3231{
3232 if (rules->rules != rules->stub) {
3233 free(rules->rules);
3234 }
3235}
3236
dd51dae2
BP
3237/* Checks whether 'rule' matches 'c' and, if so, adds it to 'rules'. This
3238 * function verifies most of the criteria in 'c' itself, but the caller must
3239 * check 'c->cr' itself.
3240 *
3241 * Increments '*n_readonly' if 'rule' wasn't added because it's read-only (and
b20f4073
BP
3242 * 'c' only includes modifiable rules). */
3243static void
a9b22b7f 3244collect_rule(struct rule *rule, const struct rule_criteria *c,
dd51dae2 3245 struct rule_collection *rules, size_t *n_readonly)
15aaf599 3246 OVS_REQUIRES(ofproto_mutex)
6c1d7642 3247{
dd51dae2
BP
3248 if ((c->table_id == rule->table_id || c->table_id == 0xff)
3249 && ofproto_rule_has_out_port(rule, c->out_port)
3250 && ofproto_rule_has_out_group(rule, c->out_group)
3251 && !((rule->flow_cookie ^ c->cookie) & c->cookie_mask)
3252 && (!rule_is_hidden(rule) || c->include_hidden)) {
dd51dae2 3253 /* Rule matches all the criteria... */
834fe5cb 3254 if (!rule_is_readonly(rule) || c->include_readonly) {
dd51dae2 3255 /* ...add it. */
a8e547c1 3256 rule_collection_add(rules, rule);
dd51dae2
BP
3257 } else {
3258 /* ...except it's read-only. */
3259 ++*n_readonly;
6c1d7642 3260 }
6c1d7642
BP
3261 }
3262}
3263
a9b22b7f
BP
3264/* Searches 'ofproto' for rules that match the criteria in 'criteria'. Matches
3265 * on classifiers rules are done in the "loose" way required for OpenFlow
3266 * OFPFC_MODIFY and OFPFC_DELETE requests. Puts the selected rules on list
9ed18e46
BP
3267 * 'rules'.
3268 *
9ed18e46 3269 * Returns 0 on success, otherwise an OpenFlow error code. */
90bf1e07 3270static enum ofperr
a9b22b7f 3271collect_rules_loose(struct ofproto *ofproto,
a8e547c1
BP
3272 const struct rule_criteria *criteria,
3273 struct rule_collection *rules)
15aaf599 3274 OVS_REQUIRES(ofproto_mutex)
9ed18e46 3275{
d0918789 3276 struct oftable *table;
554764d1 3277 enum ofperr error = 0;
dd51dae2 3278 size_t n_readonly = 0;
48266274 3279
a8e547c1
BP
3280 rule_collection_init(rules);
3281
554764d1
SH
3282 if (!check_table_id(ofproto, criteria->table_id)) {
3283 error = OFPERR_OFPBRC_BAD_TABLE_ID;
a8e547c1 3284 goto exit;
48266274 3285 }
9ed18e46 3286
b8266395 3287 if (criteria->cookie_mask == OVS_BE64_MAX) {
98eaac36
JR
3288 struct rule *rule;
3289
a9b22b7f
BP
3290 HINDEX_FOR_EACH_WITH_HASH (rule, cookie_node,
3291 hash_cookie(criteria->cookie),
98eaac36 3292 &ofproto->cookies) {
a9b22b7f 3293 if (cls_rule_is_loose_match(&rule->cr, &criteria->cr.match)) {
b20f4073 3294 collect_rule(rule, criteria, rules, &n_readonly);
98eaac36
JR
3295 }
3296 }
6c1d7642 3297 } else {
a9b22b7f 3298 FOR_EACH_MATCHING_TABLE (table, criteria->table_id, ofproto) {
6c1d7642
BP
3299 struct cls_cursor cursor;
3300 struct rule *rule;
9ed18e46 3301
06f81620 3302 fat_rwlock_rdlock(&table->cls.rwlock);
a9b22b7f 3303 cls_cursor_init(&cursor, &table->cls, &criteria->cr);
6c1d7642 3304 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
b20f4073 3305 collect_rule(rule, criteria, rules, &n_readonly);
9ed18e46 3306 }
06f81620 3307 fat_rwlock_unlock(&table->cls.rwlock);
9ed18e46
BP
3308 }
3309 }
48d28ac1 3310
a8e547c1 3311exit:
dd51dae2
BP
3312 if (!error && !rules->n && n_readonly) {
3313 /* We didn't find any rules to modify. We did find some read-only
3314 * rules that we're not allowed to modify, so report that. */
3315 error = OFPERR_OFPBRC_EPERM;
3316 }
a8e547c1
BP
3317 if (error) {
3318 rule_collection_destroy(rules);
3319 }
48d28ac1 3320 return error;
9ed18e46
BP
3321}
3322
a9b22b7f
BP
3323/* Searches 'ofproto' for rules that match the criteria in 'criteria'. Matches
3324 * on classifiers rules are done in the "strict" way required for OpenFlow
3325 * OFPFC_MODIFY_STRICT and OFPFC_DELETE_STRICT requests. Puts the selected
3326 * rules on list 'rules'.
9ed18e46 3327 *
9ed18e46 3328 * Returns 0 on success, otherwise an OpenFlow error code. */
90bf1e07 3329static enum ofperr
a9b22b7f 3330collect_rules_strict(struct ofproto *ofproto,
a8e547c1
BP
3331 const struct rule_criteria *criteria,
3332 struct rule_collection *rules)
15aaf599 3333 OVS_REQUIRES(ofproto_mutex)
9ed18e46 3334{
d0918789 3335 struct oftable *table;
dd51dae2 3336 size_t n_readonly = 0;
554764d1 3337 int error = 0;
48266274 3338
a8e547c1
BP
3339 rule_collection_init(rules);
3340
554764d1
SH
3341 if (!check_table_id(ofproto, criteria->table_id)) {
3342 error = OFPERR_OFPBRC_BAD_TABLE_ID;
a8e547c1 3343 goto exit;
48266274 3344 }
9ed18e46 3345
b8266395 3346 if (criteria->cookie_mask == OVS_BE64_MAX) {
98eaac36
JR
3347 struct rule *rule;
3348
a9b22b7f
BP
3349 HINDEX_FOR_EACH_WITH_HASH (rule, cookie_node,
3350 hash_cookie(criteria->cookie),
98eaac36 3351 &ofproto->cookies) {
a9b22b7f 3352 if (cls_rule_equal(&rule->cr, &criteria->cr)) {
b20f4073 3353 collect_rule(rule, criteria, rules, &n_readonly);
98eaac36
JR
3354 }
3355 }
6c1d7642 3356 } else {
a9b22b7f 3357 FOR_EACH_MATCHING_TABLE (table, criteria->table_id, ofproto) {
6c1d7642 3358 struct rule *rule;
9ed18e46 3359
06f81620 3360 fat_rwlock_rdlock(&table->cls.rwlock);
a9b22b7f
BP
3361 rule = rule_from_cls_rule(classifier_find_rule_exactly(
3362 &table->cls, &criteria->cr));
06f81620 3363 fat_rwlock_unlock(&table->cls.rwlock);
6c1d7642 3364 if (rule) {
b20f4073 3365 collect_rule(rule, criteria, rules, &n_readonly);
9ed18e46
BP
3366 }
3367 }
3368 }
48d28ac1 3369
a8e547c1 3370exit:
b20f4073
BP
3371 if (!error && !rules->n && n_readonly) {
3372 /* We didn't find any rules to modify. We did find some read-only
3373 * rules that we're not allowed to modify, so report that. */
3374 error = OFPERR_OFPBRC_EPERM;
3375 }
a8e547c1
BP
3376 if (error) {
3377 rule_collection_destroy(rules);
3378 }
6c1d7642 3379 return error;
9ed18e46
BP
3380}
3381
f27f2134
BP
3382/* Returns 'age_ms' (a duration in milliseconds), converted to seconds and
3383 * forced into the range of a uint16_t. */
3384static int
3385age_secs(long long int age_ms)
3386{
3387 return (age_ms < 0 ? 0
3388 : age_ms >= UINT16_MAX * 1000 ? UINT16_MAX
3389 : (unsigned int) age_ms / 1000);
3390}
3391
90bf1e07 3392static enum ofperr
63f2140a 3393handle_flow_stats_request(struct ofconn *ofconn,
982697a4 3394 const struct ofp_header *request)
15aaf599 3395 OVS_EXCLUDED(ofproto_mutex)
064af421 3396{
64ff1d96 3397 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
81d1ea94 3398 struct ofputil_flow_stats_request fsr;
a9b22b7f 3399 struct rule_criteria criteria;
a8e547c1 3400 struct rule_collection rules;
63f2140a 3401 struct list replies;
90bf1e07 3402 enum ofperr error;
a8e547c1 3403 size_t i;
09246b99 3404
982697a4 3405 error = ofputil_decode_flow_stats_request(&fsr, request);
09246b99
BP
3406 if (error) {
3407 return error;
3408 }
3409
a9b22b7f
BP
3410 rule_criteria_init(&criteria, fsr.table_id, &fsr.match, 0, fsr.cookie,
3411 fsr.cookie_mask, fsr.out_port, fsr.out_group);
15aaf599
BP
3412
3413 ovs_mutex_lock(&ofproto_mutex);
a9b22b7f
BP
3414 error = collect_rules_loose(ofproto, &criteria, &rules);
3415 rule_criteria_destroy(&criteria);
15aaf599
BP
3416 if (!error) {
3417 rule_collection_ref(&rules);
3418 }
3419 ovs_mutex_unlock(&ofproto_mutex);
3420
9ed18e46
BP
3421 if (error) {
3422 return error;
3423 }
5ecc9d81 3424
982697a4 3425 ofpmp_init(&replies, request);
a8e547c1
BP
3426 for (i = 0; i < rules.n; i++) {
3427 struct rule *rule = rules.rules[i];
f27f2134 3428 long long int now = time_msec();
9ed18e46 3429 struct ofputil_flow_stats fs;
15aaf599 3430 long long int created, used, modified;
dc723c44 3431 const struct rule_actions *actions;
15aaf599 3432 enum ofputil_flow_mod_flags flags;
a3779dbc 3433
d10976b7 3434 ovs_mutex_lock(&rule->mutex);
15aaf599 3435 fs.cookie = rule->flow_cookie;
a3779dbc
EJ
3436 fs.idle_timeout = rule->idle_timeout;
3437 fs.hard_timeout = rule->hard_timeout;
15aaf599 3438 created = rule->created;
15aaf599 3439 modified = rule->modified;
06a0f3e2 3440 actions = rule_get_actions(rule);
15aaf599 3441 flags = rule->flags;
d10976b7 3442 ovs_mutex_unlock(&rule->mutex);
a3779dbc 3443
dc437090
JR
3444 ofproto->ofproto_class->rule_get_stats(rule, &fs.packet_count,
3445 &fs.byte_count, &used);
3446
15aaf599
BP
3447 minimatch_expand(&rule->cr.match, &fs.match);
3448 fs.table_id = rule->table_id;
3449 calc_duration(created, now, &fs.duration_sec, &fs.duration_nsec);
3450 fs.priority = rule->cr.priority;
3451 fs.idle_age = age_secs(now - used);
3452 fs.hard_age = age_secs(now - modified);
15aaf599
BP
3453 fs.ofpacts = actions->ofpacts;
3454 fs.ofpacts_len = actions->ofpacts_len;
3f517bcd 3455
15aaf599 3456 fs.flags = flags;
9ed18e46 3457 ofputil_append_flow_stats_reply(&fs, &replies);
3c4486a5 3458 }
15aaf599
BP
3459
3460 rule_collection_unref(&rules);
a8e547c1
BP
3461 rule_collection_destroy(&rules);
3462
63f2140a 3463 ofconn_send_replies(ofconn, &replies);
5ecc9d81 3464
09246b99
BP
3465 return 0;
3466}
3467
4f2cad2c 3468static void
3394b5b6 3469flow_stats_ds(struct rule *rule, struct ds *results)
4f2cad2c 3470{
4f2cad2c 3471 uint64_t packet_count, byte_count;
dc723c44 3472 const struct rule_actions *actions;
dc437090 3473 long long int created, used;
4f2cad2c 3474
dc437090
JR
3475 rule->ofproto->ofproto_class->rule_get_stats(rule, &packet_count,
3476 &byte_count, &used);
4f2cad2c 3477
15aaf599 3478 ovs_mutex_lock(&rule->mutex);
06a0f3e2 3479 actions = rule_get_actions(rule);
15aaf599
BP
3480 created = rule->created;
3481 ovs_mutex_unlock(&rule->mutex);
3482
6c1491fb
BP
3483 if (rule->table_id != 0) {
3484 ds_put_format(results, "table_id=%"PRIu8", ", rule->table_id);
3485 }
15aaf599 3486 ds_put_format(results, "duration=%llds, ", (time_msec() - created) / 1000);
4f2cad2c
JP
3487 ds_put_format(results, "n_packets=%"PRIu64", ", packet_count);
3488 ds_put_format(results, "n_bytes=%"PRIu64", ", byte_count);
cb833cf6 3489 cls_rule_format(&rule->cr, results);
a5df0e72 3490 ds_put_char(results, ',');
15aaf599 3491
455ecd77 3492 ds_put_cstr(results, "actions=");
15aaf599
BP
3493 ofpacts_format(actions->ofpacts, actions->ofpacts_len, results);
3494
4f2cad2c
JP
3495 ds_put_cstr(results, "\n");
3496}
3497
d295e8e9 3498/* Adds a pretty-printed description of all flows to 'results', including
ee8b231c 3499 * hidden flows (e.g., set up by in-band control). */
4f2cad2c
JP
3500void
3501ofproto_get_all_flows(struct ofproto *p, struct ds *results)
3502{
d0918789 3503 struct oftable *table;
6c1491fb 3504
d0918789 3505 OFPROTO_FOR_EACH_TABLE (table, p) {
6c1491fb
BP
3506 struct cls_cursor cursor;
3507 struct rule *rule;
064af421 3508
06f81620 3509 fat_rwlock_rdlock(&table->cls.rwlock);
d0918789 3510 cls_cursor_init(&cursor, &table->cls, NULL);
6c1491fb
BP
3511 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
3512 flow_stats_ds(rule, results);
3513 }
06f81620 3514 fat_rwlock_unlock(&table->cls.rwlock);
064af421 3515 }
064af421
BP
3516}
3517
b5827b24
BP
3518/* Obtains the NetFlow engine type and engine ID for 'ofproto' into
3519 * '*engine_type' and '*engine_id', respectively. */
3520void
3521ofproto_get_netflow_ids(const struct ofproto *ofproto,
3522 uint8_t *engine_type, uint8_t *engine_id)
3523{
abe529af 3524 ofproto->ofproto_class->get_netflow_ids(ofproto, engine_type, engine_id);
b5827b24
BP
3525}
3526
88bf179a
AW
3527/* Checks the status of CFM configured on 'ofp_port' within 'ofproto'.
3528 * Returns 0 if the port's CFM status was successfully stored into
3529 * '*status'. Returns positive errno if the port did not have CFM
3530 * configured. Returns negative number if there is no status change
3531 * since last update.
9a9e3786 3532 *
88bf179a
AW
3533 * The caller must provide and own '*status', and must free 'status->rmps'.
3534 * '*status' is indeterminate if the return value is non-zero. */
3535int
4e022ec0 3536ofproto_port_get_cfm_status(const struct ofproto *ofproto, ofp_port_t ofp_port,
9a9e3786 3537 struct ofproto_cfm_status *status)
3967a833
MM
3538{
3539 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
88bf179a
AW
3540 return (ofport && ofproto->ofproto_class->get_cfm_status
3541 ? ofproto->ofproto_class->get_cfm_status(ofport, status)
3542 : EOPNOTSUPP);
3967a833
MM
3543}
3544
90bf1e07 3545static enum ofperr
76c93b22 3546handle_aggregate_stats_request(struct ofconn *ofconn,
982697a4 3547 const struct ofp_header *oh)
15aaf599 3548 OVS_EXCLUDED(ofproto_mutex)
27d34fce 3549{
76c93b22 3550 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
81d1ea94 3551 struct ofputil_flow_stats_request request;
76c93b22 3552 struct ofputil_aggregate_stats stats;
5e9d0469 3553 bool unknown_packets, unknown_bytes;
a9b22b7f 3554 struct rule_criteria criteria;
a8e547c1 3555 struct rule_collection rules;
76c93b22 3556 struct ofpbuf *reply;
90bf1e07 3557 enum ofperr error;
a8e547c1 3558 size_t i;
27d34fce 3559
982697a4 3560 error = ofputil_decode_flow_stats_request(&request, oh);
76c93b22
BP
3561 if (error) {
3562 return error;
3563 }
5ecc9d81 3564
a9b22b7f
BP
3565 rule_criteria_init(&criteria, request.table_id, &request.match, 0,
3566 request.cookie, request.cookie_mask,
3567 request.out_port, request.out_group);
15aaf599
BP
3568
3569 ovs_mutex_lock(&ofproto_mutex);
a9b22b7f
BP
3570 error = collect_rules_loose(ofproto, &criteria, &rules);
3571 rule_criteria_destroy(&criteria);
15aaf599
BP
3572 if (!error) {
3573 rule_collection_ref(&rules);
3574 }
3575 ovs_mutex_unlock(&ofproto_mutex);
3576
9ed18e46
BP
3577 if (error) {
3578 return error;
3579 }
3c4486a5 3580
9ed18e46 3581 memset(&stats, 0, sizeof stats);
5e9d0469 3582 unknown_packets = unknown_bytes = false;
a8e547c1
BP
3583 for (i = 0; i < rules.n; i++) {
3584 struct rule *rule = rules.rules[i];
9ed18e46
BP
3585 uint64_t packet_count;
3586 uint64_t byte_count;
dc437090 3587 long long int used;
5ecc9d81 3588
9ed18e46 3589 ofproto->ofproto_class->rule_get_stats(rule, &packet_count,
dc437090 3590 &byte_count, &used);
5ecc9d81 3591
5e9d0469
BP
3592 if (packet_count == UINT64_MAX) {
3593 unknown_packets = true;
3594 } else {
3595 stats.packet_count += packet_count;
3596 }
3597
3598 if (byte_count == UINT64_MAX) {
3599 unknown_bytes = true;
3600 } else {
3601 stats.byte_count += byte_count;
3602 }
3603
9ed18e46 3604 stats.flow_count++;
3c4486a5 3605 }
5e9d0469
BP
3606 if (unknown_packets) {
3607 stats.packet_count = UINT64_MAX;
3608 }
3609 if (unknown_bytes) {
3610 stats.byte_count = UINT64_MAX;
3611 }
27d34fce 3612
15aaf599 3613 rule_collection_unref(&rules);
a8e547c1
BP
3614 rule_collection_destroy(&rules);
3615
982697a4 3616 reply = ofputil_encode_aggregate_stats_reply(&stats, oh);
76c93b22 3617 ofconn_send_reply(ofconn, reply);
09246b99
BP
3618
3619 return 0;
3620}
3621
c1c9c9c4 3622struct queue_stats_cbdata {
ca0f572c 3623 struct ofport *ofport;
63f2140a 3624 struct list replies;
6dc34a0d 3625 long long int now;
c1c9c9c4
BP
3626};
3627
3628static void
db9220c3 3629put_queue_stats(struct queue_stats_cbdata *cbdata, uint32_t queue_id,
c1c9c9c4
BP
3630 const struct netdev_queue_stats *stats)
3631{
6dc34a0d 3632 struct ofputil_queue_stats oqs;
c1c9c9c4 3633
6dc34a0d
BP
3634 oqs.port_no = cbdata->ofport->pp.port_no;
3635 oqs.queue_id = queue_id;
3636 oqs.tx_bytes = stats->tx_bytes;
3637 oqs.tx_packets = stats->tx_packets;
3638 oqs.tx_errors = stats->tx_errors;
3639 if (stats->created != LLONG_MIN) {
3640 calc_duration(stats->created, cbdata->now,
3641 &oqs.duration_sec, &oqs.duration_nsec);
3642 } else {
3643 oqs.duration_sec = oqs.duration_nsec = UINT32_MAX;
3644 }
64626975 3645 ofputil_append_queue_stat(&cbdata->replies, &oqs);
c1c9c9c4
BP
3646}
3647
3648static void
db9220c3 3649handle_queue_stats_dump_cb(uint32_t queue_id,
c1c9c9c4
BP
3650 struct netdev_queue_stats *stats,
3651 void *cbdata_)
3652{
3653 struct queue_stats_cbdata *cbdata = cbdata_;
3654
3655 put_queue_stats(cbdata, queue_id, stats);
3656}
3657
0414d158 3658static enum ofperr
ca0f572c 3659handle_queue_stats_for_port(struct ofport *port, uint32_t queue_id,
c1c9c9c4
BP
3660 struct queue_stats_cbdata *cbdata)
3661{
ca0f572c 3662 cbdata->ofport = port;
c1c9c9c4
BP
3663 if (queue_id == OFPQ_ALL) {
3664 netdev_dump_queue_stats(port->netdev,
3665 handle_queue_stats_dump_cb, cbdata);
3666 } else {
3667 struct netdev_queue_stats stats;
3668
1ac788f6
BP
3669 if (!netdev_get_queue_stats(port->netdev, queue_id, &stats)) {
3670 put_queue_stats(cbdata, queue_id, &stats);
0414d158
BP
3671 } else {
3672 return OFPERR_OFPQOFC_BAD_QUEUE;
1ac788f6 3673 }
c1c9c9c4 3674 }
0414d158 3675 return 0;
c1c9c9c4
BP
3676}
3677
90bf1e07 3678static enum ofperr
63f2140a 3679handle_queue_stats_request(struct ofconn *ofconn,
982697a4 3680 const struct ofp_header *rq)
c1c9c9c4 3681{
64ff1d96 3682 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
c1c9c9c4 3683 struct queue_stats_cbdata cbdata;
0414d158 3684 struct ofport *port;
0414d158 3685 enum ofperr error;
64626975 3686 struct ofputil_queue_stats_request oqsr;
c1c9c9c4 3687
c1c9c9c4
BP
3688 COVERAGE_INC(ofproto_queue_req);
3689
982697a4 3690 ofpmp_init(&cbdata.replies, rq);
6dc34a0d 3691 cbdata.now = time_msec();
c1c9c9c4 3692
64626975
SH
3693 error = ofputil_decode_queue_stats_request(rq, &oqsr);
3694 if (error) {
3695 return error;
3696 }
3697
7f05e7ab 3698 if (oqsr.port_no == OFPP_ANY) {
0414d158 3699 error = OFPERR_OFPQOFC_BAD_QUEUE;
4e8e4213 3700 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
64626975 3701 if (!handle_queue_stats_for_port(port, oqsr.queue_id, &cbdata)) {
0414d158
BP
3702 error = 0;
3703 }
c1c9c9c4 3704 }
0414d158 3705 } else {
64626975 3706 port = ofproto_get_port(ofproto, oqsr.port_no);
0414d158 3707 error = (port
64626975 3708 ? handle_queue_stats_for_port(port, oqsr.queue_id, &cbdata)
0414d158
BP
3709 : OFPERR_OFPQOFC_BAD_PORT);
3710 }
3711 if (!error) {
3712 ofconn_send_replies(ofconn, &cbdata.replies);
c1c9c9c4 3713 } else {
63f2140a 3714 ofpbuf_list_delete(&cbdata.replies);
c1c9c9c4 3715 }
c1c9c9c4 3716
0414d158 3717 return error;
c1c9c9c4 3718}
7ee20df1 3719
2e31a9b8
BP
3720static bool
3721should_evict_a_rule(struct oftable *table, unsigned int extra_space)
15aaf599
BP
3722 OVS_REQUIRES(ofproto_mutex)
3723 OVS_NO_THREAD_SAFETY_ANALYSIS
8037acb4 3724{
15aaf599 3725 return classifier_count(&table->cls) + extra_space > table->max_flows;
2e31a9b8 3726}
8037acb4 3727
2e31a9b8 3728static enum ofperr
834fe5cb 3729evict_rules_from_table(struct oftable *table, unsigned int extra_space)
15aaf599 3730 OVS_REQUIRES(ofproto_mutex)
2e31a9b8
BP
3731{
3732 while (should_evict_a_rule(table, extra_space)) {
3733 struct rule *rule;
8037acb4 3734
2e31a9b8
BP
3735 if (!choose_rule_to_evict(table, &rule)) {
3736 return OFPERR_OFPFMFC_TABLE_FULL;
2e31a9b8 3737 } else {
834fe5cb 3738 ofproto_rule_delete__(rule, OFPRR_EVICTION);
2e31a9b8 3739 }
8037acb4 3740 }
2e31a9b8
BP
3741
3742 return 0;
8037acb4
BP
3743}
3744
79eee1eb
BP
3745/* Implements OFPFC_ADD and the cases for OFPFC_MODIFY and OFPFC_MODIFY_STRICT
3746 * in which no matching flow already exists in the flow table.
3747 *
3748 * Adds the flow specified by 'ofm', which is followed by 'n_actions'
75a75043 3749 * ofp_actions, to the ofproto's flow table. Returns 0 on success, an OpenFlow
90bf1e07
BP
3750 * error code on failure, or OFPROTO_POSTPONE if the operation cannot be
3751 * initiated now but may be retried later.
79eee1eb 3752 *
2509616e 3753 * The caller retains ownership of 'fm->ofpacts'.
f25d0cf3 3754 *
79eee1eb
BP
3755 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
3756 * if any. */
90bf1e07 3757static enum ofperr
baae3d02
BP
3758add_flow(struct ofproto *ofproto, struct ofputil_flow_mod *fm,
3759 const struct flow_mod_requester *req)
15aaf599 3760 OVS_REQUIRES(ofproto_mutex)
064af421 3761{
062fea06 3762 const struct rule_actions *actions;
d0918789 3763 struct oftable *table;
8037acb4 3764 struct cls_rule cr;
064af421 3765 struct rule *rule;
5a361239 3766 uint8_t table_id;
554764d1 3767 int error = 0;
064af421 3768
554764d1
SH
3769 if (!check_table_id(ofproto, fm->table_id)) {
3770 error = OFPERR_OFPBRC_BAD_TABLE_ID;
48266274
BP
3771 return error;
3772 }
3773
7ee20df1
BP
3774 /* Pick table. */
3775 if (fm->table_id == 0xff) {
13521ff5 3776 if (ofproto->ofproto_class->rule_choose_table) {
81a76618
BP
3777 error = ofproto->ofproto_class->rule_choose_table(ofproto,
3778 &fm->match,
7ee20df1
BP
3779 &table_id);
3780 if (error) {
3781 return error;
3782 }
cb22974d 3783 ovs_assert(table_id < ofproto->n_tables);
7ee20df1 3784 } else {
5a361239 3785 table_id = 0;
7ee20df1
BP
3786 }
3787 } else if (fm->table_id < ofproto->n_tables) {
5a361239 3788 table_id = fm->table_id;
7ee20df1 3789 } else {
c22c56bd 3790 return OFPERR_OFPBRC_BAD_TABLE_ID;
064af421
BP
3791 }
3792
5a361239 3793 table = &ofproto->tables[table_id];
834fe5cb
BP
3794 if (table->flags & OFTABLE_READONLY
3795 && !(fm->flags & OFPUTIL_FF_NO_READONLY)) {
5c67e4af
BP
3796 return OFPERR_OFPBRC_EPERM;
3797 }
3798
adcf00ba
AZ
3799 if (!(fm->flags & OFPUTIL_FF_HIDDEN_FIELDS)) {
3800 if (!match_has_default_hidden_fields(&fm->match)) {
3801 VLOG_WARN_RL(&rl, "%s: (add_flow) only internal flows can set "
3802 "non-default values to hidden fields", ofproto->name);
3803 return OFPERR_OFPBRC_EPERM;
3804 }
3805 }
3806
8037acb4
BP
3807 cls_rule_init(&cr, &fm->match, fm->priority);
3808
b277c7cc 3809 /* Transform "add" into "modify" if there's an existing identical flow. */
06f81620 3810 fat_rwlock_rdlock(&table->cls.rwlock);
8037acb4 3811 rule = rule_from_cls_rule(classifier_find_rule_exactly(&table->cls, &cr));
06f81620 3812 fat_rwlock_unlock(&table->cls.rwlock);
b277c7cc 3813 if (rule) {
834fe5cb
BP
3814 struct rule_collection rules;
3815
8037acb4 3816 cls_rule_destroy(&cr);
b277c7cc 3817
834fe5cb
BP
3818 rule_collection_init(&rules);
3819 rule_collection_add(&rules, rule);
3820 fm->modify_cookie = true;
3821 error = modify_flows__(ofproto, fm, &rules, req);
3822 rule_collection_destroy(&rules);
a8e547c1 3823
834fe5cb 3824 return error;
b277c7cc
BP
3825 }
3826
81a76618 3827 /* Check for overlap, if requested. */
0fb88c18 3828 if (fm->flags & OFPUTIL_FF_CHECK_OVERLAP) {
c09f755d
BP
3829 bool overlaps;
3830
06f81620 3831 fat_rwlock_rdlock(&table->cls.rwlock);
8037acb4 3832 overlaps = classifier_rule_overlaps(&table->cls, &cr);
06f81620 3833 fat_rwlock_unlock(&table->cls.rwlock);
c09f755d
BP
3834
3835 if (overlaps) {
8037acb4 3836 cls_rule_destroy(&cr);
c09f755d
BP
3837 return OFPERR_OFPFMFC_OVERLAP;
3838 }
7ee20df1 3839 }
81a76618 3840
8037acb4 3841 /* If necessary, evict an existing rule to clear out space. */
834fe5cb 3842 error = evict_rules_from_table(table, 1);
8037acb4
BP
3843 if (error) {
3844 cls_rule_destroy(&cr);
3845 return error;
3846 }
2e1ae200 3847
8037acb4
BP
3848 /* Allocate new rule. */
3849 rule = ofproto->ofproto_class->rule_alloc();
3850 if (!rule) {
3851 cls_rule_destroy(&cr);
3852 VLOG_WARN_RL(&rl, "%s: failed to create rule (%s)",
3853 ofproto->name, ovs_strerror(error));
3854 return ENOMEM;
3855 }
3856
3857 /* Initialize base state. */
49a0e0eb
BP
3858 *CONST_CAST(struct ofproto **, &rule->ofproto) = ofproto;
3859 cls_rule_move(CONST_CAST(struct cls_rule *, &rule->cr), &cr);
37bec3d3 3860 ovs_refcount_init(&rule->ref_count);
623e1caf 3861 rule->flow_cookie = fm->new_cookie;
dc437090 3862 rule->created = rule->modified = time_msec();
a3779dbc 3863
d10976b7
BP
3864 ovs_mutex_init(&rule->mutex);
3865 ovs_mutex_lock(&rule->mutex);
7ee20df1
BP
3866 rule->idle_timeout = fm->idle_timeout;
3867 rule->hard_timeout = fm->hard_timeout;
d10976b7 3868 ovs_mutex_unlock(&rule->mutex);
a3779dbc 3869
49a0e0eb 3870 *CONST_CAST(uint8_t *, &rule->table_id) = table - ofproto->tables;
3f517bcd 3871 rule->flags = fm->flags & OFPUTIL_FF_STATE;
062fea06
BP
3872 actions = rule_actions_create(ofproto, fm->ofpacts, fm->ofpacts_len);
3873 ovsrcu_set(&rule->actions, actions);
9cae45dc 3874 list_init(&rule->meter_list_node);
254750ce 3875 rule->eviction_group = NULL;
e503cc19 3876 list_init(&rule->expirable);
2b07c8b1
BP
3877 rule->monitor_flags = 0;
3878 rule->add_seqno = 0;
3879 rule->modify_seqno = 0;
7ee20df1 3880
8037acb4 3881 /* Construct rule, initializing derived state. */
b277c7cc
BP
3882 error = ofproto->ofproto_class->rule_construct(rule);
3883 if (error) {
8037acb4
BP
3884 ofproto_rule_destroy__(rule);
3885 return error;
064af421 3886 }
8037acb4 3887
062fea06
BP
3888 if (fm->hard_timeout || fm->idle_timeout) {
3889 list_insert(&ofproto->expirable, &rule->expirable);
3890 }
3891 cookies_insert(ofproto, rule);
3892 eviction_group_add_rule(rule);
3893 if (actions->provider_meter_id != UINT32_MAX) {
3894 meter_insert_rule(rule);
3895 }
1ebeaaa7 3896
062fea06
BP
3897 fat_rwlock_wrlock(&table->cls.rwlock);
3898 classifier_insert(&table->cls, CONST_CAST(struct cls_rule *, &rule->cr));
3899 fat_rwlock_unlock(&table->cls.rwlock);
8037acb4 3900
b20f4073 3901 error = ofproto->ofproto_class->rule_insert(rule);
834fe5cb
BP
3902 if (error) {
3903 oftable_remove_rule(rule);
3904 ofproto_rule_unref(rule);
3905 return error;
3906 }
062fea06 3907
834fe5cb
BP
3908 if (minimask_get_vid_mask(&rule->cr.match.mask) == VLAN_VID_MASK) {
3909 if (ofproto->vlan_bitmap) {
3910 uint16_t vid = miniflow_get_vid(&rule->cr.match.flow);
3911 if (!bitmap_is_set(ofproto->vlan_bitmap, vid)) {
3912 bitmap_set1(ofproto->vlan_bitmap, vid);
3913 ofproto->vlans_changed = true;
3914 }
3915 } else {
3916 ofproto->vlans_changed = true;
3917 }
3918 }
3919
3920 ofmonitor_report(ofproto->connmgr, rule, NXFME_ADDED, 0,
3921 req ? req->ofconn : NULL, req ? req->xid : 0);
3922
3923 return req ? send_buffered_packet(req->ofconn, fm->buffer_id, rule) : 0;
064af421 3924}
79eee1eb
BP
3925\f
3926/* OFPFC_MODIFY and OFPFC_MODIFY_STRICT. */
064af421 3927
9ed18e46
BP
3928/* Modifies the rules listed in 'rules', changing their actions to match those
3929 * in 'fm'.
79eee1eb 3930 *
9ed18e46
BP
3931 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
3932 * if any.
3933 *
3934 * Returns 0 on success, otherwise an OpenFlow error code. */
90bf1e07 3935static enum ofperr
baae3d02
BP
3936modify_flows__(struct ofproto *ofproto, struct ofputil_flow_mod *fm,
3937 const struct rule_collection *rules,
3938 const struct flow_mod_requester *req)
15aaf599 3939 OVS_REQUIRES(ofproto_mutex)
79eee1eb 3940{
834fe5cb 3941 enum nx_flow_update_event event;
5c67e4af 3942 enum ofperr error;
a8e547c1 3943 size_t i;
79eee1eb 3944
af822017
BP
3945 if (ofproto->ofproto_class->rule_premodify_actions) {
3946 for (i = 0; i < rules->n; i++) {
3947 struct rule *rule = rules->rules[i];
3948
dd51dae2
BP
3949 error = ofproto->ofproto_class->rule_premodify_actions(
3950 rule, fm->ofpacts, fm->ofpacts_len);
3951 if (error) {
3952 return error;
af822017
BP
3953 }
3954 }
3955 }
3956
834fe5cb 3957 event = fm->command == OFPFC_ADD ? NXFME_ADDED : NXFME_MODIFIED;
a8e547c1
BP
3958 for (i = 0; i < rules->n; i++) {
3959 struct rule *rule = rules->rules[i];
08043761 3960
834fe5cb
BP
3961 /* 'fm' says that */
3962 bool change_cookie = (fm->modify_cookie
3963 && fm->new_cookie != OVS_BE64_MAX
3964 && fm->new_cookie != rule->flow_cookie);
2e1ae200 3965
834fe5cb
BP
3966 const struct rule_actions *actions = rule_get_actions(rule);
3967 bool change_actions = !ofpacts_equal(fm->ofpacts, fm->ofpacts_len,
3968 actions->ofpacts,
3969 actions->ofpacts_len);
5c67e4af 3970
834fe5cb 3971 bool reset_counters = (fm->flags & OFPUTIL_FF_RESET_COUNTS) != 0;
a7d94793 3972
834fe5cb
BP
3973 long long int now = time_msec();
3974
3975 /* FIXME: Implement OFPFUTIL_FF_RESET_COUNTS */
98eaac36 3976
834fe5cb
BP
3977 if (change_cookie) {
3978 cookies_remove(ofproto, rule);
98eaac36 3979 }
834fe5cb
BP
3980
3981 ovs_mutex_lock(&rule->mutex);
3982 if (fm->command == OFPFC_ADD) {
b277c7cc
BP
3983 rule->idle_timeout = fm->idle_timeout;
3984 rule->hard_timeout = fm->hard_timeout;
3f517bcd 3985 rule->flags = fm->flags & OFPUTIL_FF_STATE;
834fe5cb
BP
3986 rule->created = now;
3987 }
3988 if (change_cookie) {
3989 rule->flow_cookie = fm->new_cookie;
3990 }
3991 rule->modified = now;
3992 ovs_mutex_unlock(&rule->mutex);
3993
3994 if (change_cookie) {
3995 cookies_insert(ofproto, rule);
3996 }
3997 if (fm->command == OFPFC_ADD) {
b277c7cc
BP
3998 if (fm->idle_timeout || fm->hard_timeout) {
3999 if (!rule->eviction_group) {
4000 eviction_group_add_rule(rule);
4001 }
4002 } else {
4003 eviction_group_remove_rule(rule);
4004 }
4005 }
4006
834fe5cb
BP
4007 if (change_actions) {
4008 ovsrcu_set(&rule->actions, rule_actions_create(ofproto,
4009 fm->ofpacts,
4010 fm->ofpacts_len));
4011 rule_actions_destroy(actions);
4012 }
884e1dc4 4013
834fe5cb 4014 if (change_actions || reset_counters) {
af822017 4015 ofproto->ofproto_class->rule_modify_actions(rule, reset_counters);
623e1caf 4016 }
834fe5cb
BP
4017
4018 if (event != NXFME_MODIFIED || change_actions || change_cookie) {
4019 ofmonitor_report(ofproto->connmgr, rule, event, 0,
4020 req ? req->ofconn : NULL, req ? req->xid : 0);
4021 }
4022 }
4023
4024 if (fm->buffer_id != UINT32_MAX && req) {
4025 error = send_buffered_packet(req->ofconn, fm->buffer_id,
4026 rules->rules[0]);
5ecc9d81 4027 }
79eee1eb 4028
5c67e4af 4029 return error;
9ed18e46
BP
4030}
4031
9387b970 4032static enum ofperr
baae3d02
BP
4033modify_flows_add(struct ofproto *ofproto, struct ofputil_flow_mod *fm,
4034 const struct flow_mod_requester *req)
15aaf599 4035 OVS_REQUIRES(ofproto_mutex)
9387b970 4036{
b8266395 4037 if (fm->cookie_mask != htonll(0) || fm->new_cookie == OVS_BE64_MAX) {
9387b970
SH
4038 return 0;
4039 }
baae3d02 4040 return add_flow(ofproto, fm, req);
9387b970
SH
4041}
4042
90bf1e07
BP
4043/* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code on
4044 * failure.
9ed18e46
BP
4045 *
4046 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
4047 * if any. */
90bf1e07 4048static enum ofperr
baae3d02
BP
4049modify_flows_loose(struct ofproto *ofproto, struct ofputil_flow_mod *fm,
4050 const struct flow_mod_requester *req)
15aaf599 4051 OVS_REQUIRES(ofproto_mutex)
9ed18e46 4052{
a9b22b7f 4053 struct rule_criteria criteria;
a8e547c1 4054 struct rule_collection rules;
9ed18e46
BP
4055 int error;
4056
a9b22b7f
BP
4057 rule_criteria_init(&criteria, fm->table_id, &fm->match, 0,
4058 fm->cookie, fm->cookie_mask, OFPP_ANY, OFPG11_ANY);
dd51dae2
BP
4059 rule_criteria_require_rw(&criteria,
4060 (fm->flags & OFPUTIL_FF_NO_READONLY) != 0);
a9b22b7f
BP
4061 error = collect_rules_loose(ofproto, &criteria, &rules);
4062 rule_criteria_destroy(&criteria);
4063
a8e547c1
BP
4064 if (!error) {
4065 error = (rules.n > 0
baae3d02
BP
4066 ? modify_flows__(ofproto, fm, &rules, req)
4067 : modify_flows_add(ofproto, fm, req));
623e1caf 4068 }
a8e547c1
BP
4069
4070 rule_collection_destroy(&rules);
4071
4072 return error;
79eee1eb
BP
4073}
4074
4075/* Implements OFPFC_MODIFY_STRICT. Returns 0 on success or an OpenFlow error
baae3d02 4076 * code on failure. */
90bf1e07 4077static enum ofperr
baae3d02
BP
4078modify_flow_strict(struct ofproto *ofproto, struct ofputil_flow_mod *fm,
4079 const struct flow_mod_requester *req)
15aaf599 4080 OVS_REQUIRES(ofproto_mutex)
79eee1eb 4081{
a9b22b7f 4082 struct rule_criteria criteria;
a8e547c1 4083 struct rule_collection rules;
6c1491fb
BP
4084 int error;
4085
a9b22b7f
BP
4086 rule_criteria_init(&criteria, fm->table_id, &fm->match, fm->priority,
4087 fm->cookie, fm->cookie_mask, OFPP_ANY, OFPG11_ANY);
dd51dae2
BP
4088 rule_criteria_require_rw(&criteria,
4089 (fm->flags & OFPUTIL_FF_NO_READONLY) != 0);
a9b22b7f
BP
4090 error = collect_rules_strict(ofproto, &criteria, &rules);
4091 rule_criteria_destroy(&criteria);
4092
a8e547c1
BP
4093 if (!error) {
4094 if (rules.n == 0) {
baae3d02 4095 error = modify_flows_add(ofproto, fm, req);
a8e547c1 4096 } else if (rules.n == 1) {
baae3d02 4097 error = modify_flows__(ofproto, fm, &rules, req);
a8e547c1 4098 }
623e1caf 4099 }
a8e547c1
BP
4100
4101 rule_collection_destroy(&rules);
4102
4103 return error;
79eee1eb 4104}
9ed18e46
BP
4105\f
4106/* OFPFC_DELETE implementation. */
79eee1eb 4107
254750ce 4108static void
834fe5cb
BP
4109delete_flow__(struct rule *rule, enum ofp_flow_removed_reason reason,
4110 const struct flow_mod_requester *req)
15aaf599 4111 OVS_REQUIRES(ofproto_mutex)
254750ce
BP
4112{
4113 struct ofproto *ofproto = rule->ofproto;
4114
ca311b2f 4115 ofproto_rule_send_removed(rule, reason);
254750ce 4116
834fe5cb
BP
4117 ofmonitor_report(ofproto->connmgr, rule, NXFME_DELETED, reason,
4118 req ? req->ofconn : NULL, req ? req->xid : 0);
254750ce 4119 oftable_remove_rule(rule);
8037acb4 4120 ofproto->ofproto_class->rule_delete(rule);
254750ce
BP
4121}
4122
9ed18e46
BP
4123/* Deletes the rules listed in 'rules'.
4124 *
4125 * Returns 0 on success, otherwise an OpenFlow error code. */
90bf1e07 4126static enum ofperr
baae3d02 4127delete_flows__(struct ofproto *ofproto,
a8e547c1 4128 const struct rule_collection *rules,
baae3d02
BP
4129 enum ofp_flow_removed_reason reason,
4130 const struct flow_mod_requester *req)
15aaf599 4131 OVS_REQUIRES(ofproto_mutex)
064af421 4132{
a8e547c1 4133 size_t i;
79eee1eb 4134
a8e547c1 4135 for (i = 0; i < rules->n; i++) {
834fe5cb 4136 delete_flow__(rules->rules[i], reason, req);
79eee1eb 4137 }
834fe5cb 4138 ofmonitor_flush(ofproto->connmgr);
79eee1eb 4139
9ed18e46 4140 return 0;
79eee1eb 4141}
79eee1eb
BP
4142
4143/* Implements OFPFC_DELETE. */
90bf1e07 4144static enum ofperr
baae3d02 4145delete_flows_loose(struct ofproto *ofproto,
7024dffe 4146 const struct ofputil_flow_mod *fm,
baae3d02 4147 const struct flow_mod_requester *req)
15aaf599 4148 OVS_REQUIRES(ofproto_mutex)
79eee1eb 4149{
a9b22b7f 4150 struct rule_criteria criteria;
a8e547c1 4151 struct rule_collection rules;
90bf1e07 4152 enum ofperr error;
064af421 4153
a9b22b7f
BP
4154 rule_criteria_init(&criteria, fm->table_id, &fm->match, 0,
4155 fm->cookie, fm->cookie_mask,
4156 fm->out_port, fm->out_group);
dd51dae2
BP
4157 rule_criteria_require_rw(&criteria,
4158 (fm->flags & OFPUTIL_FF_NO_READONLY) != 0);
a9b22b7f
BP
4159 error = collect_rules_loose(ofproto, &criteria, &rules);
4160 rule_criteria_destroy(&criteria);
4161
a8e547c1 4162 if (!error && rules.n > 0) {
baae3d02 4163 error = delete_flows__(ofproto, &rules, fm->delete_reason, req);
a8e547c1
BP
4164 }
4165 rule_collection_destroy(&rules);
4166
4167 return error;
064af421
BP
4168}
4169
79eee1eb 4170/* Implements OFPFC_DELETE_STRICT. */
90bf1e07 4171static enum ofperr
baae3d02
BP
4172delete_flow_strict(struct ofproto *ofproto, const struct ofputil_flow_mod *fm,
4173 const struct flow_mod_requester *req)
15aaf599 4174 OVS_REQUIRES(ofproto_mutex)
79eee1eb 4175{
a9b22b7f 4176 struct rule_criteria criteria;
a8e547c1 4177 struct rule_collection rules;
90bf1e07 4178 enum ofperr error;
79eee1eb 4179
a9b22b7f
BP
4180 rule_criteria_init(&criteria, fm->table_id, &fm->match, fm->priority,
4181 fm->cookie, fm->cookie_mask,
4182 fm->out_port, fm->out_group);
dd51dae2
BP
4183 rule_criteria_require_rw(&criteria,
4184 (fm->flags & OFPUTIL_FF_NO_READONLY) != 0);
a9b22b7f
BP
4185 error = collect_rules_strict(ofproto, &criteria, &rules);
4186 rule_criteria_destroy(&criteria);
4187
a8e547c1 4188 if (!error && rules.n > 0) {
baae3d02 4189 error = delete_flows__(ofproto, &rules, fm->delete_reason, req);
a8e547c1
BP
4190 }
4191 rule_collection_destroy(&rules);
4192
4193 return error;
abe529af
BP
4194}
4195
4196static void
4197ofproto_rule_send_removed(struct rule *rule, uint8_t reason)
15aaf599 4198 OVS_REQUIRES(ofproto_mutex)
abe529af
BP
4199{
4200 struct ofputil_flow_removed fr;
dc437090 4201 long long int used;
abe529af 4202
834fe5cb
BP
4203 if (rule_is_hidden(rule) ||
4204 !(rule->flags & OFPUTIL_FF_SEND_FLOW_REM)) {
abe529af
BP
4205 return;
4206 }
4207
5cb7a798 4208 minimatch_expand(&rule->cr.match, &fr.match);
81a76618 4209 fr.priority = rule->cr.priority;
abe529af
BP
4210 fr.cookie = rule->flow_cookie;
4211 fr.reason = reason;
95216219 4212 fr.table_id = rule->table_id;
65e0be10
BP
4213 calc_duration(rule->created, time_msec(),
4214 &fr.duration_sec, &fr.duration_nsec);
d10976b7 4215 ovs_mutex_lock(&rule->mutex);
abe529af 4216 fr.idle_timeout = rule->idle_timeout;
fa2bad0f 4217 fr.hard_timeout = rule->hard_timeout;
d10976b7 4218 ovs_mutex_unlock(&rule->mutex);
abe529af 4219 rule->ofproto->ofproto_class->rule_get_stats(rule, &fr.packet_count,
dc437090 4220 &fr.byte_count, &used);
abe529af
BP
4221
4222 connmgr_send_flow_removed(rule->ofproto->connmgr, &fr);
4223}
4224
4225/* Sends an OpenFlow "flow removed" message with the given 'reason' (either
4226 * OFPRR_HARD_TIMEOUT or OFPRR_IDLE_TIMEOUT), and then removes 'rule' from its
4227 * ofproto.
4228 *
4229 * ofproto implementation ->run() functions should use this function to expire
4230 * OpenFlow flows. */
4231void
4232ofproto_rule_expire(struct rule *rule, uint8_t reason)
15aaf599 4233 OVS_REQUIRES(ofproto_mutex)
abe529af 4234{
b20f4073 4235 ofproto_rule_delete__(rule, reason);
79eee1eb 4236}
994c9973
BP
4237
4238/* Reduces '*timeout' to no more than 'max'. A value of zero in either case
4239 * means "infinite". */
4240static void
4241reduce_timeout(uint16_t max, uint16_t *timeout)
4242{
4243 if (max && (!*timeout || *timeout > max)) {
4244 *timeout = max;
4245 }
4246}
4247
4248/* If 'idle_timeout' is nonzero, and 'rule' has no idle timeout or an idle
4249 * timeout greater than 'idle_timeout', lowers 'rule''s idle timeout to
4250 * 'idle_timeout' seconds. Similarly for 'hard_timeout'.
4251 *
4252 * Suitable for implementing OFPACT_FIN_TIMEOUT. */
4253void
4254ofproto_rule_reduce_timeouts(struct rule *rule,
4255 uint16_t idle_timeout, uint16_t hard_timeout)
d10976b7 4256 OVS_EXCLUDED(ofproto_mutex, rule->mutex)
994c9973
BP
4257{
4258 if (!idle_timeout && !hard_timeout) {
4259 return;
4260 }
4261
abe7b10f 4262 ovs_mutex_lock(&ofproto_mutex);
994c9973
BP
4263 if (list_is_empty(&rule->expirable)) {
4264 list_insert(&rule->ofproto->expirable, &rule->expirable);
4265 }
abe7b10f 4266 ovs_mutex_unlock(&ofproto_mutex);
994c9973 4267
d10976b7 4268 ovs_mutex_lock(&rule->mutex);
994c9973
BP
4269 reduce_timeout(idle_timeout, &rule->idle_timeout);
4270 reduce_timeout(hard_timeout, &rule->hard_timeout);
d10976b7 4271 ovs_mutex_unlock(&rule->mutex);
994c9973 4272}
79eee1eb 4273\f
90bf1e07 4274static enum ofperr
2e4f5fcf 4275handle_flow_mod(struct ofconn *ofconn, const struct ofp_header *oh)
15aaf599 4276 OVS_EXCLUDED(ofproto_mutex)
064af421 4277{
a5b8d268 4278 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
a9a2da38 4279 struct ofputil_flow_mod fm;
f25d0cf3
BP
4280 uint64_t ofpacts_stub[1024 / 8];
4281 struct ofpbuf ofpacts;
90bf1e07 4282 enum ofperr error;
064af421 4283
76589937 4284 error = reject_slave_controller(ofconn);
9deba63b 4285 if (error) {
f25d0cf3 4286 goto exit;
9deba63b 4287 }
3052b0c5 4288
f25d0cf3
BP
4289 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
4290 error = ofputil_decode_flow_mod(&fm, oh, ofconn_get_protocol(ofconn),
7e9f8266
BP
4291 &ofpacts,
4292 u16_to_ofp(ofproto->max_ports),
4293 ofproto->n_tables);
4294 if (!error) {
4295 error = ofproto_check_ofpacts(ofproto, fm.ofpacts, fm.ofpacts_len);
4296 }
548de4dd 4297 if (!error) {
baae3d02
BP
4298 struct flow_mod_requester req;
4299
4300 req.ofconn = ofconn;
834fe5cb 4301 req.xid = oh->xid;
baae3d02 4302 error = handle_flow_mod__(ofproto, &fm, &req);
49bdc010 4303 }
a5b8d268 4304 if (error) {
f25d0cf3 4305 goto exit_free_ofpacts;
a5b8d268 4306 }
2e310801 4307
696d1bcf 4308 ofconn_report_flow_mod(ofconn, fm.command);
a5b8d268 4309
f25d0cf3
BP
4310exit_free_ofpacts:
4311 ofpbuf_uninit(&ofpacts);
4312exit:
4313 return error;
75a75043
BP
4314}
4315
90bf1e07 4316static enum ofperr
baae3d02
BP
4317handle_flow_mod__(struct ofproto *ofproto, struct ofputil_flow_mod *fm,
4318 const struct flow_mod_requester *req)
15aaf599 4319 OVS_EXCLUDED(ofproto_mutex)
75a75043 4320{
35412852 4321 enum ofperr error;
75a75043 4322
15aaf599 4323 ovs_mutex_lock(&ofproto_mutex);
baae3d02
BP
4324 switch (fm->command) {
4325 case OFPFC_ADD:
4326 error = add_flow(ofproto, fm, req);
4327 break;
3052b0c5 4328
baae3d02
BP
4329 case OFPFC_MODIFY:
4330 error = modify_flows_loose(ofproto, fm, req);
4331 break;
3052b0c5 4332
baae3d02
BP
4333 case OFPFC_MODIFY_STRICT:
4334 error = modify_flow_strict(ofproto, fm, req);
4335 break;
3052b0c5 4336
baae3d02
BP
4337 case OFPFC_DELETE:
4338 error = delete_flows_loose(ofproto, fm, req);
4339 break;
3052b0c5 4340
baae3d02
BP
4341 case OFPFC_DELETE_STRICT:
4342 error = delete_flow_strict(ofproto, fm, req);
4343 break;
3052b0c5 4344
baae3d02
BP
4345 default:
4346 if (fm->command > 0xff) {
4347 VLOG_WARN_RL(&rl, "%s: flow_mod has explicit table_id but "
4348 "flow_mod_table_id extension is not enabled",
4349 ofproto->name);
6c1491fb 4350 }
baae3d02
BP
4351 error = OFPERR_OFPFMFC_BAD_COMMAND;
4352 break;
3052b0c5 4353 }
baae3d02 4354 ofmonitor_flush(ofproto->connmgr);
15aaf599 4355 ovs_mutex_unlock(&ofproto_mutex);
35412852
BP
4356
4357 run_rule_executes(ofproto);
4358 return error;
3052b0c5
BP
4359}
4360
90bf1e07 4361static enum ofperr
d1e2cf21 4362handle_role_request(struct ofconn *ofconn, const struct ofp_header *oh)
9deba63b 4363{
f4f1ea7e
BP
4364 struct ofputil_role_request request;
4365 struct ofputil_role_request reply;
9deba63b 4366 struct ofpbuf *buf;
6ea4776b
JR
4367 enum ofperr error;
4368
f4f1ea7e 4369 error = ofputil_decode_role_message(oh, &request);
6ea4776b
JR
4370 if (error) {
4371 return error;
4372 }
9deba63b 4373
f4f1ea7e 4374 if (request.role != OFPCR12_ROLE_NOCHANGE) {
f4f1ea7e
BP
4375 if (request.have_generation_id
4376 && !ofconn_set_master_election_id(ofconn, request.generation_id)) {
4377 return OFPERR_OFPRRFC_STALE;
6ea4776b 4378 }
6ea4776b 4379
f4f1ea7e
BP
4380 ofconn_set_role(ofconn, request.role);
4381 }
9deba63b 4382
f4f1ea7e 4383 reply.role = ofconn_get_role(ofconn);
147cc9d3
BP
4384 reply.have_generation_id = ofconn_get_master_election_id(
4385 ofconn, &reply.generation_id);
f4f1ea7e 4386 buf = ofputil_encode_role_reply(oh, &reply);
b0421aa2 4387 ofconn_send_reply(ofconn, buf);
9deba63b
BP
4388
4389 return 0;
4390}
4391
90bf1e07 4392static enum ofperr
6c1491fb
BP
4393handle_nxt_flow_mod_table_id(struct ofconn *ofconn,
4394 const struct ofp_header *oh)
4395{
982697a4 4396 const struct nx_flow_mod_table_id *msg = ofpmsg_body(oh);
27527aa0
BP
4397 enum ofputil_protocol cur, next;
4398
4399 cur = ofconn_get_protocol(ofconn);
4400 next = ofputil_protocol_set_tid(cur, msg->set != 0);
4401 ofconn_set_protocol(ofconn, next);
6c1491fb 4402
6c1491fb
BP
4403 return 0;
4404}
4405
90bf1e07 4406static enum ofperr
d1e2cf21 4407handle_nxt_set_flow_format(struct ofconn *ofconn, const struct ofp_header *oh)
09246b99 4408{
982697a4 4409 const struct nx_set_flow_format *msg = ofpmsg_body(oh);
27527aa0
BP
4410 enum ofputil_protocol cur, next;
4411 enum ofputil_protocol next_base;
09246b99 4412
27527aa0
BP
4413 next_base = ofputil_nx_flow_format_to_protocol(ntohl(msg->format));
4414 if (!next_base) {
90bf1e07 4415 return OFPERR_OFPBRC_EPERM;
09246b99 4416 }
7ee20df1 4417
27527aa0
BP
4418 cur = ofconn_get_protocol(ofconn);
4419 next = ofputil_protocol_set_base(cur, next_base);
27527aa0 4420 ofconn_set_protocol(ofconn, next);
b20f4073 4421
7ee20df1 4422 return 0;
09246b99
BP
4423}
4424
90bf1e07 4425static enum ofperr
54834960
EJ
4426handle_nxt_set_packet_in_format(struct ofconn *ofconn,
4427 const struct ofp_header *oh)
4428{
982697a4 4429 const struct nx_set_packet_in_format *msg = ofpmsg_body(oh);
54834960
EJ
4430 uint32_t format;
4431
54834960 4432 format = ntohl(msg->format);
a15f0eeb 4433 if (format != NXPIF_OPENFLOW10 && format != NXPIF_NXM) {
90bf1e07 4434 return OFPERR_OFPBRC_EPERM;
54834960
EJ
4435 }
4436
54834960
EJ
4437 ofconn_set_packet_in_format(ofconn, format);
4438 return 0;
4439}
4440
80d5aefd
BP
4441static enum ofperr
4442handle_nxt_set_async_config(struct ofconn *ofconn, const struct ofp_header *oh)
4443{
982697a4 4444 const struct nx_async_config *msg = ofpmsg_body(oh);
80d5aefd
BP
4445 uint32_t master[OAM_N_TYPES];
4446 uint32_t slave[OAM_N_TYPES];
4447
4448 master[OAM_PACKET_IN] = ntohl(msg->packet_in_mask[0]);
4449 master[OAM_PORT_STATUS] = ntohl(msg->port_status_mask[0]);
4450 master[OAM_FLOW_REMOVED] = ntohl(msg->flow_removed_mask[0]);
4451
4452 slave[OAM_PACKET_IN] = ntohl(msg->packet_in_mask[1]);
4453 slave[OAM_PORT_STATUS] = ntohl(msg->port_status_mask[1]);
4454 slave[OAM_FLOW_REMOVED] = ntohl(msg->flow_removed_mask[1]);
4455
4456 ofconn_set_async_config(ofconn, master, slave);
4550b647
MM
4457 if (ofconn_get_type(ofconn) == OFCONN_SERVICE &&
4458 !ofconn_get_miss_send_len(ofconn)) {
4459 ofconn_set_miss_send_len(ofconn, OFP_DEFAULT_MISS_SEND_LEN);
4460 }
80d5aefd
BP
4461
4462 return 0;
4463}
4464
c423b3b3
AC
4465static enum ofperr
4466handle_nxt_get_async_request(struct ofconn *ofconn, const struct ofp_header *oh)
4467{
4468 struct ofpbuf *buf;
4469 uint32_t master[OAM_N_TYPES];
4470 uint32_t slave[OAM_N_TYPES];
4471 struct nx_async_config *msg;
4472
4473 ofconn_get_async_config(ofconn, master, slave);
4474 buf = ofpraw_alloc_reply(OFPRAW_OFPT13_GET_ASYNC_REPLY, oh, 0);
4475 msg = ofpbuf_put_zeros(buf, sizeof *msg);
4476
4477 msg->packet_in_mask[0] = htonl(master[OAM_PACKET_IN]);
4478 msg->port_status_mask[0] = htonl(master[OAM_PORT_STATUS]);
4479 msg->flow_removed_mask[0] = htonl(master[OAM_FLOW_REMOVED]);
4480
4481 msg->packet_in_mask[1] = htonl(slave[OAM_PACKET_IN]);
4482 msg->port_status_mask[1] = htonl(slave[OAM_PORT_STATUS]);
4483 msg->flow_removed_mask[1] = htonl(slave[OAM_FLOW_REMOVED]);
4484
4485 ofconn_send_reply(ofconn, buf);
4486
4487 return 0;
4488}
4489
a7349929
BP
4490static enum ofperr
4491handle_nxt_set_controller_id(struct ofconn *ofconn,
4492 const struct ofp_header *oh)
4493{
982697a4 4494 const struct nx_controller_id *nci = ofpmsg_body(oh);
a7349929 4495
a7349929
BP
4496 if (!is_all_zeros(nci->zero, sizeof nci->zero)) {
4497 return OFPERR_NXBRC_MUST_BE_ZERO;
4498 }
4499
4500 ofconn_set_controller_id(ofconn, ntohs(nci->controller_id));
4501 return 0;
4502}
4503
90bf1e07 4504static enum ofperr
d1e2cf21 4505handle_barrier_request(struct ofconn *ofconn, const struct ofp_header *oh)
246e61ea 4506{
246e61ea
JP
4507 struct ofpbuf *buf;
4508
982697a4
BP
4509 buf = ofpraw_alloc_reply((oh->version == OFP10_VERSION
4510 ? OFPRAW_OFPT10_BARRIER_REPLY
4511 : OFPRAW_OFPT11_BARRIER_REPLY), oh, 0);
b0421aa2 4512 ofconn_send_reply(ofconn, buf);
246e61ea
JP
4513 return 0;
4514}
4515
2b07c8b1
BP
4516static void
4517ofproto_compose_flow_refresh_update(const struct rule *rule,
4518 enum nx_flow_monitor_flags flags,
4519 struct list *msgs)
15aaf599 4520 OVS_REQUIRES(ofproto_mutex)
2b07c8b1 4521{
6f00e29b 4522 const struct rule_actions *actions;
2b07c8b1 4523 struct ofputil_flow_update fu;
5cb7a798 4524 struct match match;
2b07c8b1 4525
2b07c8b1
BP
4526 fu.event = (flags & (NXFMF_INITIAL | NXFMF_ADD)
4527 ? NXFME_ADDED : NXFME_MODIFIED);
4528 fu.reason = 0;
d10976b7 4529 ovs_mutex_lock(&rule->mutex);
2b07c8b1
BP
4530 fu.idle_timeout = rule->idle_timeout;
4531 fu.hard_timeout = rule->hard_timeout;
d10976b7 4532 ovs_mutex_unlock(&rule->mutex);
2b07c8b1
BP
4533 fu.table_id = rule->table_id;
4534 fu.cookie = rule->flow_cookie;
5cb7a798
BP
4535 minimatch_expand(&rule->cr.match, &match);
4536 fu.match = &match;
6b140a4e 4537 fu.priority = rule->cr.priority;
6f00e29b 4538
b20f4073 4539 actions = flags & NXFMF_ACTIONS ? rule_get_actions(rule) : NULL;
6f00e29b
BP
4540 fu.ofpacts = actions ? actions->ofpacts : NULL;
4541 fu.ofpacts_len = actions ? actions->ofpacts_len : 0;
2b07c8b1
BP
4542
4543 if (list_is_empty(msgs)) {
4544 ofputil_start_flow_update(msgs);
4545 }
4546 ofputil_append_flow_update(&fu, msgs);
4547}
4548
4549void
a8e547c1
BP
4550ofmonitor_compose_refresh_updates(struct rule_collection *rules,
4551 struct list *msgs)
15aaf599 4552 OVS_REQUIRES(ofproto_mutex)
2b07c8b1 4553{
a8e547c1 4554 size_t i;
2b07c8b1 4555
a8e547c1
BP
4556 for (i = 0; i < rules->n; i++) {
4557 struct rule *rule = rules->rules[i];
2b07c8b1
BP
4558 enum nx_flow_monitor_flags flags = rule->monitor_flags;
4559 rule->monitor_flags = 0;
4560
4561 ofproto_compose_flow_refresh_update(rule, flags, msgs);
4562 }
4563}
4564
4565static void
4566ofproto_collect_ofmonitor_refresh_rule(const struct ofmonitor *m,
4567 struct rule *rule, uint64_t seqno,
a8e547c1 4568 struct rule_collection *rules)
15aaf599 4569 OVS_REQUIRES(ofproto_mutex)
2b07c8b1
BP
4570{
4571 enum nx_flow_monitor_flags update;
4572
5e8b38b0 4573 if (rule_is_hidden(rule)) {
2b07c8b1
BP
4574 return;
4575 }
4576
b20f4073 4577 if (!ofproto_rule_has_out_port(rule, m->out_port)) {
2b07c8b1
BP
4578 return;
4579 }
4580
4581 if (seqno) {
4582 if (rule->add_seqno > seqno) {
4583 update = NXFMF_ADD | NXFMF_MODIFY;
4584 } else if (rule->modify_seqno > seqno) {
4585 update = NXFMF_MODIFY;
4586 } else {
4587 return;
4588 }
4589
4590 if (!(m->flags & update)) {
4591 return;
4592 }
4593 } else {
4594 update = NXFMF_INITIAL;
4595 }
4596
4597 if (!rule->monitor_flags) {
a8e547c1 4598 rule_collection_add(rules, rule);
2b07c8b1
BP
4599 }
4600 rule->monitor_flags |= update | (m->flags & NXFMF_ACTIONS);
4601}
4602
4603static void
4604ofproto_collect_ofmonitor_refresh_rules(const struct ofmonitor *m,
4605 uint64_t seqno,
a8e547c1 4606 struct rule_collection *rules)
15aaf599 4607 OVS_REQUIRES(ofproto_mutex)
2b07c8b1
BP
4608{
4609 const struct ofproto *ofproto = ofconn_get_ofproto(m->ofconn);
2b07c8b1 4610 const struct oftable *table;
81a76618 4611 struct cls_rule target;
2b07c8b1 4612
5cb7a798 4613 cls_rule_init_from_minimatch(&target, &m->match, 0);
2b07c8b1
BP
4614 FOR_EACH_MATCHING_TABLE (table, m->table_id, ofproto) {
4615 struct cls_cursor cursor;
4616 struct rule *rule;
4617
06f81620 4618 fat_rwlock_rdlock(&table->cls.rwlock);
81a76618 4619 cls_cursor_init(&cursor, &table->cls, &target);
2b07c8b1 4620 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
2b07c8b1
BP
4621 ofproto_collect_ofmonitor_refresh_rule(m, rule, seqno, rules);
4622 }
06f81620 4623 fat_rwlock_unlock(&table->cls.rwlock);
2b07c8b1 4624 }
48d28ac1 4625 cls_rule_destroy(&target);
2b07c8b1
BP
4626}
4627
4628static void
4629ofproto_collect_ofmonitor_initial_rules(struct ofmonitor *m,
a8e547c1 4630 struct rule_collection *rules)
15aaf599 4631 OVS_REQUIRES(ofproto_mutex)
2b07c8b1
BP
4632{
4633 if (m->flags & NXFMF_INITIAL) {
4634 ofproto_collect_ofmonitor_refresh_rules(m, 0, rules);
4635 }
4636}
4637
4638void
4639ofmonitor_collect_resume_rules(struct ofmonitor *m,
a8e547c1 4640 uint64_t seqno, struct rule_collection *rules)
15aaf599 4641 OVS_REQUIRES(ofproto_mutex)
2b07c8b1
BP
4642{
4643 ofproto_collect_ofmonitor_refresh_rules(m, seqno, rules);
4644}
4645
4646static enum ofperr
982697a4 4647handle_flow_monitor_request(struct ofconn *ofconn, const struct ofp_header *oh)
15aaf599 4648 OVS_EXCLUDED(ofproto_mutex)
2b07c8b1
BP
4649{
4650 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4651 struct ofmonitor **monitors;
4652 size_t n_monitors, allocated_monitors;
a8e547c1 4653 struct rule_collection rules;
2b07c8b1
BP
4654 struct list replies;
4655 enum ofperr error;
2b07c8b1
BP
4656 struct ofpbuf b;
4657 size_t i;
4658
4659 error = 0;
982697a4 4660 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2b07c8b1
BP
4661 monitors = NULL;
4662 n_monitors = allocated_monitors = 0;
15aaf599
BP
4663
4664 ovs_mutex_lock(&ofproto_mutex);
2b07c8b1
BP
4665 for (;;) {
4666 struct ofputil_flow_monitor_request request;
4667 struct ofmonitor *m;
4668 int retval;
4669
4670 retval = ofputil_decode_flow_monitor_request(&request, &b);
4671 if (retval == EOF) {
4672 break;
4673 } else if (retval) {
4674 error = retval;
4675 goto error;
4676 }
4677
4678 if (request.table_id != 0xff
4679 && request.table_id >= ofproto->n_tables) {
4680 error = OFPERR_OFPBRC_BAD_TABLE_ID;
4681 goto error;
4682 }
4683
4684 error = ofmonitor_create(&request, ofconn, &m);
4685 if (error) {
4686 goto error;
4687 }
4688
4689 if (n_monitors >= allocated_monitors) {
4690 monitors = x2nrealloc(monitors, &allocated_monitors,
4691 sizeof *monitors);
4692 }
4693 monitors[n_monitors++] = m;
4694 }
4695
a8e547c1 4696 rule_collection_init(&rules);
2b07c8b1
BP
4697 for (i = 0; i < n_monitors; i++) {
4698 ofproto_collect_ofmonitor_initial_rules(monitors[i], &rules);
4699 }
4700
982697a4 4701 ofpmp_init(&replies, oh);
2b07c8b1 4702 ofmonitor_compose_refresh_updates(&rules, &replies);
15aaf599
BP
4703 ovs_mutex_unlock(&ofproto_mutex);
4704
a8e547c1
BP
4705 rule_collection_destroy(&rules);
4706
2b07c8b1 4707 ofconn_send_replies(ofconn, &replies);
2b07c8b1
BP
4708 free(monitors);
4709
4710 return 0;
4711
4712error:
4713 for (i = 0; i < n_monitors; i++) {
4714 ofmonitor_destroy(monitors[i]);
4715 }
4716 free(monitors);
4cd1bc9d
BP
4717 ovs_mutex_unlock(&ofproto_mutex);
4718
2b07c8b1
BP
4719 return error;
4720}
4721
4722static enum ofperr
4723handle_flow_monitor_cancel(struct ofconn *ofconn, const struct ofp_header *oh)
15aaf599 4724 OVS_EXCLUDED(ofproto_mutex)
2b07c8b1
BP
4725{
4726 struct ofmonitor *m;
15aaf599 4727 enum ofperr error;
2b07c8b1
BP
4728 uint32_t id;
4729
4730 id = ofputil_decode_flow_monitor_cancel(oh);
15aaf599
BP
4731
4732 ovs_mutex_lock(&ofproto_mutex);
2b07c8b1 4733 m = ofmonitor_lookup(ofconn, id);
15aaf599
BP
4734 if (m) {
4735 ofmonitor_destroy(m);
4736 error = 0;
4737 } else {
4738 error = OFPERR_NXBRC_FM_BAD_ID;
2b07c8b1 4739 }
15aaf599 4740 ovs_mutex_unlock(&ofproto_mutex);
2b07c8b1 4741
15aaf599 4742 return error;
2b07c8b1
BP
4743}
4744
9cae45dc
JR
4745/* Meters implementation.
4746 *
4747 * Meter table entry, indexed by the OpenFlow meter_id.
9cae45dc
JR
4748 * 'created' is used to compute the duration for meter stats.
4749 * 'list rules' is needed so that we can delete the dependent rules when the
4750 * meter table entry is deleted.
4751 * 'provider_meter_id' is for the provider's private use.
4752 */
4753struct meter {
4754 long long int created; /* Time created. */
4755 struct list rules; /* List of "struct rule_dpif"s. */
4756 ofproto_meter_id provider_meter_id;
4757 uint16_t flags; /* Meter flags. */
4758 uint16_t n_bands; /* Number of meter bands. */
4759 struct ofputil_meter_band *bands;
4760};
4761
4762/*
4763 * This is used in instruction validation at flow set-up time,
4764 * as flows may not use non-existing meters.
9cae45dc
JR
4765 * Return value of UINT32_MAX signifies an invalid meter.
4766 */
65efd2ab
JR
4767static uint32_t
4768get_provider_meter_id(const struct ofproto *ofproto, uint32_t of_meter_id)
9cae45dc
JR
4769{
4770 if (of_meter_id && of_meter_id <= ofproto->meter_features.max_meters) {
4771 const struct meter *meter = ofproto->meters[of_meter_id];
4772 if (meter) {
4773 return meter->provider_meter_id.uint32;
4774 }
4775 }
4776 return UINT32_MAX;
4777}
4778
062fea06
BP
4779/* Finds the meter invoked by 'rule''s actions and adds 'rule' to the meter's
4780 * list of rules. */
4781static void
4782meter_insert_rule(struct rule *rule)
4783{
4784 const struct rule_actions *a = rule_get_actions(rule);
4785 uint32_t meter_id = ofpacts_get_meter(a->ofpacts, a->ofpacts_len);
4786 struct meter *meter = rule->ofproto->meters[meter_id];
4787
4788 list_insert(&meter->rules, &rule->meter_list_node);
4789}
4790
9cae45dc
JR
4791static void
4792meter_update(struct meter *meter, const struct ofputil_meter_config *config)
4793{
4794 free(meter->bands);
4795
4796 meter->flags = config->flags;
4797 meter->n_bands = config->n_bands;
4798 meter->bands = xmemdup(config->bands,
4799 config->n_bands * sizeof *meter->bands);
4800}
4801
4802static struct meter *
4803meter_create(const struct ofputil_meter_config *config,
4804 ofproto_meter_id provider_meter_id)
4805{
4806 struct meter *meter;
4807
4808 meter = xzalloc(sizeof *meter);
4809 meter->provider_meter_id = provider_meter_id;
4810 meter->created = time_msec();
4811 list_init(&meter->rules);
4812
4813 meter_update(meter, config);
4814
4815 return meter;
4816}
4817
6b3f7f02
JR
4818static void
4819meter_delete(struct ofproto *ofproto, uint32_t first, uint32_t last)
15aaf599 4820 OVS_REQUIRES(ofproto_mutex)
6b3f7f02
JR
4821{
4822 uint32_t mid;
4823 for (mid = first; mid <= last; ++mid) {
4824 struct meter *meter = ofproto->meters[mid];
4825 if (meter) {
4826 ofproto->meters[mid] = NULL;
4827 ofproto->ofproto_class->meter_del(ofproto,
4828 meter->provider_meter_id);
4829 free(meter->bands);
4830 free(meter);
4831 }
4832 }
4833}
4834
9cae45dc
JR
4835static enum ofperr
4836handle_add_meter(struct ofproto *ofproto, struct ofputil_meter_mod *mm)
4837{
4838 ofproto_meter_id provider_meter_id = { UINT32_MAX };
4839 struct meter **meterp = &ofproto->meters[mm->meter.meter_id];
4840 enum ofperr error;
4841
4842 if (*meterp) {
4843 return OFPERR_OFPMMFC_METER_EXISTS;
4844 }
4845
4846 error = ofproto->ofproto_class->meter_set(ofproto, &provider_meter_id,
4847 &mm->meter);
4848 if (!error) {
4849 ovs_assert(provider_meter_id.uint32 != UINT32_MAX);
4850 *meterp = meter_create(&mm->meter, provider_meter_id);
4851 }
f0f8c6c2 4852 return error;
9cae45dc
JR
4853}
4854
4855static enum ofperr
4856handle_modify_meter(struct ofproto *ofproto, struct ofputil_meter_mod *mm)
4857{
4858 struct meter *meter = ofproto->meters[mm->meter.meter_id];
4859 enum ofperr error;
e555eb7c 4860 uint32_t provider_meter_id;
9cae45dc
JR
4861
4862 if (!meter) {
4863 return OFPERR_OFPMMFC_UNKNOWN_METER;
4864 }
4865
e555eb7c 4866 provider_meter_id = meter->provider_meter_id.uint32;
9cae45dc
JR
4867 error = ofproto->ofproto_class->meter_set(ofproto,
4868 &meter->provider_meter_id,
4869 &mm->meter);
e555eb7c 4870 ovs_assert(meter->provider_meter_id.uint32 == provider_meter_id);
9cae45dc
JR
4871 if (!error) {
4872 meter_update(meter, &mm->meter);
4873 }
4874 return error;
4875}
4876
4877static enum ofperr
baae3d02 4878handle_delete_meter(struct ofconn *ofconn, struct ofputil_meter_mod *mm)
15aaf599 4879 OVS_EXCLUDED(ofproto_mutex)
9cae45dc
JR
4880{
4881 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4882 uint32_t meter_id = mm->meter.meter_id;
a8e547c1
BP
4883 struct rule_collection rules;
4884 enum ofperr error = 0;
9cae45dc 4885 uint32_t first, last;
9cae45dc
JR
4886
4887 if (meter_id == OFPM13_ALL) {
4888 first = 1;
4889 last = ofproto->meter_features.max_meters;
4890 } else {
4891 if (!meter_id || meter_id > ofproto->meter_features.max_meters) {
4892 return 0;
4893 }
4894 first = last = meter_id;
4895 }
4896
4897 /* First delete the rules that use this meter. If any of those rules are
4898 * currently being modified, postpone the whole operation until later. */
a8e547c1 4899 rule_collection_init(&rules);
15aaf599 4900 ovs_mutex_lock(&ofproto_mutex);
9cae45dc
JR
4901 for (meter_id = first; meter_id <= last; ++meter_id) {
4902 struct meter *meter = ofproto->meters[meter_id];
4903 if (meter && !list_is_empty(&meter->rules)) {
4904 struct rule *rule;
4905
4906 LIST_FOR_EACH (rule, meter_list_node, &meter->rules) {
a8e547c1 4907 rule_collection_add(&rules, rule);
9cae45dc
JR
4908 }
4909 }
4910 }
a8e547c1 4911 if (rules.n > 0) {
baae3d02 4912 delete_flows__(ofproto, &rules, OFPRR_METER_DELETE, NULL);
9cae45dc
JR
4913 }
4914
4915 /* Delete the meters. */
6b3f7f02 4916 meter_delete(ofproto, first, last);
9cae45dc 4917
15aaf599 4918 ovs_mutex_unlock(&ofproto_mutex);
a8e547c1
BP
4919 rule_collection_destroy(&rules);
4920
4921 return error;
9cae45dc
JR
4922}
4923
4924static enum ofperr
4925handle_meter_mod(struct ofconn *ofconn, const struct ofp_header *oh)
4926{
4927 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4928 struct ofputil_meter_mod mm;
4929 uint64_t bands_stub[256 / 8];
4930 struct ofpbuf bands;
4931 uint32_t meter_id;
4932 enum ofperr error;
4933
4934 error = reject_slave_controller(ofconn);
4935 if (error) {
4936 return error;
4937 }
4938
4939 ofpbuf_use_stub(&bands, bands_stub, sizeof bands_stub);
4940
4941 error = ofputil_decode_meter_mod(oh, &mm, &bands);
4942 if (error) {
4943 goto exit_free_bands;
4944 }
4945
4946 meter_id = mm.meter.meter_id;
4947
4948 if (mm.command != OFPMC13_DELETE) {
4949 /* Fails also when meters are not implemented by the provider. */
b31e8700 4950 if (meter_id == 0 || meter_id > OFPM13_MAX) {
9cae45dc
JR
4951 error = OFPERR_OFPMMFC_INVALID_METER;
4952 goto exit_free_bands;
b31e8700
JR
4953 } else if (meter_id > ofproto->meter_features.max_meters) {
4954 error = OFPERR_OFPMMFC_OUT_OF_METERS;
4955 goto exit_free_bands;
9cae45dc
JR
4956 }
4957 if (mm.meter.n_bands > ofproto->meter_features.max_bands) {
4958 error = OFPERR_OFPMMFC_OUT_OF_BANDS;
4959 goto exit_free_bands;
4960 }
4961 }
4962
4963 switch (mm.command) {
4964 case OFPMC13_ADD:
4965 error = handle_add_meter(ofproto, &mm);
4966 break;
4967
4968 case OFPMC13_MODIFY:
4969 error = handle_modify_meter(ofproto, &mm);
4970 break;
4971
4972 case OFPMC13_DELETE:
baae3d02 4973 error = handle_delete_meter(ofconn, &mm);
9cae45dc
JR
4974 break;
4975
4976 default:
4977 error = OFPERR_OFPMMFC_BAD_COMMAND;
4978 break;
4979 }
4980
4981exit_free_bands:
4982 ofpbuf_uninit(&bands);
4983 return error;
4984}
4985
4986static enum ofperr
4987handle_meter_features_request(struct ofconn *ofconn,
4988 const struct ofp_header *request)
4989{
4990 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4991 struct ofputil_meter_features features;
4992 struct ofpbuf *b;
4993
4994 if (ofproto->ofproto_class->meter_get_features) {
4995 ofproto->ofproto_class->meter_get_features(ofproto, &features);
4996 } else {
4997 memset(&features, 0, sizeof features);
4998 }
4999 b = ofputil_encode_meter_features_reply(&features, request);
5000
5001 ofconn_send_reply(ofconn, b);
5002 return 0;
5003}
5004
5005static enum ofperr
5006handle_meter_request(struct ofconn *ofconn, const struct ofp_header *request,
5007 enum ofptype type)
5008{
5009 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5010 struct list replies;
5011 uint64_t bands_stub[256 / 8];
5012 struct ofpbuf bands;
5013 uint32_t meter_id, first, last;
5014
5015 ofputil_decode_meter_request(request, &meter_id);
5016
5017 if (meter_id == OFPM13_ALL) {
5018 first = 1;
5019 last = ofproto->meter_features.max_meters;
5020 } else {
5021 if (!meter_id || meter_id > ofproto->meter_features.max_meters ||
5022 !ofproto->meters[meter_id]) {
5023 return OFPERR_OFPMMFC_UNKNOWN_METER;
5024 }
5025 first = last = meter_id;
5026 }
5027
5028 ofpbuf_use_stub(&bands, bands_stub, sizeof bands_stub);
5029 ofpmp_init(&replies, request);
5030
5031 for (meter_id = first; meter_id <= last; ++meter_id) {
5032 struct meter *meter = ofproto->meters[meter_id];
5033 if (!meter) {
5034 continue; /* Skip non-existing meters. */
5035 }
261bd854 5036 if (type == OFPTYPE_METER_STATS_REQUEST) {
9cae45dc
JR
5037 struct ofputil_meter_stats stats;
5038
5039 stats.meter_id = meter_id;
5040
5041 /* Provider sets the packet and byte counts, we do the rest. */
5042 stats.flow_count = list_size(&meter->rules);
5043 calc_duration(meter->created, time_msec(),
5044 &stats.duration_sec, &stats.duration_nsec);
5045 stats.n_bands = meter->n_bands;
5046 ofpbuf_clear(&bands);
5047 stats.bands
5048 = ofpbuf_put_uninit(&bands,
5049 meter->n_bands * sizeof *stats.bands);
5050
5051 if (!ofproto->ofproto_class->meter_get(ofproto,
5052 meter->provider_meter_id,
5053 &stats)) {
5054 ofputil_append_meter_stats(&replies, &stats);
5055 }
5056 } else { /* type == OFPTYPE_METER_CONFIG_REQUEST */
5057 struct ofputil_meter_config config;
5058
5059 config.meter_id = meter_id;
5060 config.flags = meter->flags;
5061 config.n_bands = meter->n_bands;
5062 config.bands = meter->bands;
5063 ofputil_append_meter_config(&replies, &config);
5064 }
5065 }
5066
5067 ofconn_send_replies(ofconn, &replies);
5068 ofpbuf_uninit(&bands);
5069 return 0;
5070}
5071
3fc3b679
AZ
5072static bool
5073ofproto_group_lookup__(const struct ofproto *ofproto, uint32_t group_id,
5074 struct ofgroup **group)
5075 OVS_REQ_RDLOCK(ofproto->groups_rwlock)
7395c052 5076{
7395c052
NZ
5077 HMAP_FOR_EACH_IN_BUCKET (*group, hmap_node,
5078 hash_int(group_id, 0), &ofproto->groups) {
5079 if ((*group)->group_id == group_id) {
7395c052
NZ
5080 return true;
5081 }
5082 }
3fc3b679 5083
7395c052
NZ
5084 return false;
5085}
5086
3fc3b679
AZ
5087/* If the group exists, this function increments the groups's reference count.
5088 *
5089 * Make sure to call ofproto_group_unref() after no longer needing to maintain
5090 * a reference to the group. */
5091bool
5092ofproto_group_lookup(const struct ofproto *ofproto, uint32_t group_id,
5093 struct ofgroup **group)
7395c052 5094{
3fc3b679
AZ
5095 bool found;
5096
5097 ovs_rwlock_rdlock(&ofproto->groups_rwlock);
5098 found = ofproto_group_lookup__(ofproto, group_id, group);
5099 if (found) {
5100 ofproto_group_ref(*group);
7395c052 5101 }
3fc3b679
AZ
5102 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5103 return found;
7395c052
NZ
5104}
5105
5106static bool
7e9f8266 5107ofproto_group_exists__(const struct ofproto *ofproto, uint32_t group_id)
7395c052
NZ
5108 OVS_REQ_RDLOCK(ofproto->groups_rwlock)
5109{
5110 struct ofgroup *grp;
5111
5112 HMAP_FOR_EACH_IN_BUCKET (grp, hmap_node,
5113 hash_int(group_id, 0), &ofproto->groups) {
5114 if (grp->group_id == group_id) {
5115 return true;
5116 }
5117 }
5118 return false;
5119}
5120
7e9f8266
BP
5121static bool
5122ofproto_group_exists(const struct ofproto *ofproto, uint32_t group_id)
5123 OVS_EXCLUDED(ofproto->groups_rwlock)
5124{
5125 bool exists;
5126
5127 ovs_rwlock_rdlock(&ofproto->groups_rwlock);
5128 exists = ofproto_group_exists__(ofproto, group_id);
5129 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5130
5131 return exists;
5132}
5133
732feb93
SH
5134static uint32_t
5135group_get_ref_count(struct ofgroup *group)
5136 OVS_EXCLUDED(ofproto_mutex)
5137{
eaf004bd 5138 struct ofproto *ofproto = CONST_CAST(struct ofproto *, group->ofproto);
732feb93
SH
5139 struct rule_criteria criteria;
5140 struct rule_collection rules;
5141 struct match match;
5142 enum ofperr error;
5143 uint32_t count;
5144
5145 match_init_catchall(&match);
5146 rule_criteria_init(&criteria, 0xff, &match, 0, htonll(0), htonll(0),
5147 OFPP_ANY, group->group_id);
5148 ovs_mutex_lock(&ofproto_mutex);
5149 error = collect_rules_loose(ofproto, &criteria, &rules);
5150 ovs_mutex_unlock(&ofproto_mutex);
5151 rule_criteria_destroy(&criteria);
5152
5153 count = !error && rules.n < UINT32_MAX ? rules.n : UINT32_MAX;
5154
5155 rule_collection_destroy(&rules);
5156 return count;
5157}
5158
7395c052
NZ
5159static void
5160append_group_stats(struct ofgroup *group, struct list *replies)
7395c052
NZ
5161{
5162 struct ofputil_group_stats ogs;
eaf004bd 5163 const struct ofproto *ofproto = group->ofproto;
7395c052
NZ
5164 long long int now = time_msec();
5165 int error;
5166
646b2a9c
SH
5167 ogs.bucket_stats = xmalloc(group->n_buckets * sizeof *ogs.bucket_stats);
5168
732feb93
SH
5169 /* Provider sets the packet and byte counts, we do the rest. */
5170 ogs.ref_count = group_get_ref_count(group);
5171 ogs.n_buckets = group->n_buckets;
5172
7395c052
NZ
5173 error = (ofproto->ofproto_class->group_get_stats
5174 ? ofproto->ofproto_class->group_get_stats(group, &ogs)
5175 : EOPNOTSUPP);
5176 if (error) {
7395c052
NZ
5177 ogs.packet_count = UINT64_MAX;
5178 ogs.byte_count = UINT64_MAX;
7395c052
NZ
5179 memset(ogs.bucket_stats, 0xff,
5180 ogs.n_buckets * sizeof *ogs.bucket_stats);
5181 }
5182
5183 ogs.group_id = group->group_id;
5184 calc_duration(group->created, now, &ogs.duration_sec, &ogs.duration_nsec);
5185
5186 ofputil_append_group_stats(replies, &ogs);
646b2a9c
SH
5187
5188 free(ogs.bucket_stats);
7395c052
NZ
5189}
5190
19187a71
BP
5191static void
5192handle_group_request(struct ofconn *ofconn,
5193 const struct ofp_header *request, uint32_t group_id,
5194 void (*cb)(struct ofgroup *, struct list *replies))
7395c052
NZ
5195{
5196 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
7395c052 5197 struct ofgroup *group;
19187a71 5198 struct list replies;
7395c052
NZ
5199
5200 ofpmp_init(&replies, request);
7395c052
NZ
5201 if (group_id == OFPG_ALL) {
5202 ovs_rwlock_rdlock(&ofproto->groups_rwlock);
5203 HMAP_FOR_EACH (group, hmap_node, &ofproto->groups) {
19187a71 5204 cb(group, &replies);
7395c052
NZ
5205 }
5206 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5207 } else {
5208 if (ofproto_group_lookup(ofproto, group_id, &group)) {
19187a71 5209 cb(group, &replies);
809c7548 5210 ofproto_group_unref(group);
7395c052
NZ
5211 }
5212 }
7395c052 5213 ofconn_send_replies(ofconn, &replies);
7395c052
NZ
5214}
5215
5216static enum ofperr
19187a71
BP
5217handle_group_stats_request(struct ofconn *ofconn,
5218 const struct ofp_header *request)
7395c052 5219{
19187a71
BP
5220 uint32_t group_id;
5221 enum ofperr error;
7395c052 5222
19187a71
BP
5223 error = ofputil_decode_group_stats_request(request, &group_id);
5224 if (error) {
5225 return error;
7395c052 5226 }
7395c052 5227
19187a71
BP
5228 handle_group_request(ofconn, request, group_id, append_group_stats);
5229 return 0;
5230}
5231
5232static void
5233append_group_desc(struct ofgroup *group, struct list *replies)
5234{
5235 struct ofputil_group_desc gds;
7395c052 5236
19187a71
BP
5237 gds.group_id = group->group_id;
5238 gds.type = group->type;
5239 ofputil_append_group_desc_reply(&gds, &group->buckets, replies);
5240}
5241
5242static enum ofperr
5243handle_group_desc_stats_request(struct ofconn *ofconn,
5244 const struct ofp_header *request)
5245{
5246 handle_group_request(ofconn, request,
5247 ofputil_decode_group_desc_request(request),
5248 append_group_desc);
7395c052
NZ
5249 return 0;
5250}
5251
5252static enum ofperr
5253handle_group_features_stats_request(struct ofconn *ofconn,
5254 const struct ofp_header *request)
5255{
5256 struct ofproto *p = ofconn_get_ofproto(ofconn);
5257 struct ofpbuf *msg;
5258
5259 msg = ofputil_encode_group_features_reply(&p->ogf, request);
5260 if (msg) {
5261 ofconn_send_reply(ofconn, msg);
5262 }
5263
5264 return 0;
5265}
5266
e8f9a7bb
VG
5267static enum ofperr
5268handle_queue_get_config_request(struct ofconn *ofconn,
5269 const struct ofp_header *oh)
5270{
5271 struct ofproto *p = ofconn_get_ofproto(ofconn);
5272 struct netdev_queue_dump queue_dump;
5273 struct ofport *ofport;
5274 unsigned int queue_id;
5275 struct ofpbuf *reply;
5276 struct smap details;
5277 ofp_port_t request;
5278 enum ofperr error;
5279
5280 error = ofputil_decode_queue_get_config_request(oh, &request);
5281 if (error) {
5282 return error;
5283 }
5284
5285 ofport = ofproto_get_port(p, request);
5286 if (!ofport) {
5287 return OFPERR_OFPQOFC_BAD_PORT;
5288 }
5289
5290 reply = ofputil_encode_queue_get_config_reply(oh);
5291
5292 smap_init(&details);
5293 NETDEV_QUEUE_FOR_EACH (&queue_id, &details, &queue_dump, ofport->netdev) {
5294 struct ofputil_queue_config queue;
5295
5296 /* None of the existing queues have compatible properties, so we
5297 * hard-code omitting min_rate and max_rate. */
5298 queue.queue_id = queue_id;
5299 queue.min_rate = UINT16_MAX;
5300 queue.max_rate = UINT16_MAX;
5301 ofputil_append_queue_get_config_reply(reply, &queue);
5302 }
5303 smap_destroy(&details);
5304
5305 ofconn_send_reply(ofconn, reply);
5306
5307 return 0;
5308}
5309
809c7548
RW
5310static enum ofperr
5311init_group(struct ofproto *ofproto, struct ofputil_group_mod *gm,
5312 struct ofgroup **ofgroup)
5313{
5314 enum ofperr error;
eaf004bd 5315 const long long int now = time_msec();
809c7548
RW
5316
5317 if (gm->group_id > OFPG_MAX) {
5318 return OFPERR_OFPGMFC_INVALID_GROUP;
5319 }
5320 if (gm->type > OFPGT11_FF) {
5321 return OFPERR_OFPGMFC_BAD_TYPE;
5322 }
5323
5324 *ofgroup = ofproto->ofproto_class->group_alloc();
5325 if (!*ofgroup) {
5326 VLOG_WARN_RL(&rl, "%s: failed to allocate group", ofproto->name);
5327 return OFPERR_OFPGMFC_OUT_OF_GROUPS;
5328 }
5329
eaf004bd
AZ
5330 (*ofgroup)->ofproto = ofproto;
5331 *CONST_CAST(uint32_t *, &((*ofgroup)->group_id)) = gm->group_id;
5332 *CONST_CAST(enum ofp11_group_type *, &(*ofgroup)->type) = gm->type;
5333 *CONST_CAST(long long int *, &((*ofgroup)->created)) = now;
5334 *CONST_CAST(long long int *, &((*ofgroup)->modified)) = now;
809c7548
RW
5335 ovs_refcount_init(&(*ofgroup)->ref_count);
5336
5337 list_move(&(*ofgroup)->buckets, &gm->buckets);
eaf004bd
AZ
5338 *CONST_CAST(uint32_t *, &(*ofgroup)->n_buckets) =
5339 list_size(&(*ofgroup)->buckets);
809c7548
RW
5340
5341 /* Construct called BEFORE any locks are held. */
5342 error = ofproto->ofproto_class->group_construct(*ofgroup);
5343 if (error) {
5344 ofputil_bucket_list_destroy(&(*ofgroup)->buckets);
5345 ofproto->ofproto_class->group_dealloc(*ofgroup);
5346 }
5347 return error;
5348}
5349
6c5b90ce
BP
5350/* Implements the OFPGC11_ADD operation specified by 'gm', adding a group to
5351 * 'ofproto''s group table. Returns 0 on success or an OpenFlow error code on
5352 * failure. */
7395c052
NZ
5353static enum ofperr
5354add_group(struct ofproto *ofproto, struct ofputil_group_mod *gm)
5355{
5356 struct ofgroup *ofgroup;
5357 enum ofperr error;
5358
7395c052 5359 /* Allocate new group and initialize it. */
809c7548 5360 error = init_group(ofproto, gm, &ofgroup);
7395c052 5361 if (error) {
809c7548 5362 return error;
7395c052
NZ
5363 }
5364
5365 /* We wrlock as late as possible to minimize the time we jam any other
5366 * threads: No visible state changes before acquiring the lock. */
5367 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
5368
5369 if (ofproto->n_groups[gm->type] >= ofproto->ogf.max_groups[gm->type]) {
5370 error = OFPERR_OFPGMFC_OUT_OF_GROUPS;
5371 goto unlock_out;
5372 }
5373
7e9f8266 5374 if (ofproto_group_exists__(ofproto, gm->group_id)) {
7395c052
NZ
5375 error = OFPERR_OFPGMFC_GROUP_EXISTS;
5376 goto unlock_out;
5377 }
5378
5379 if (!error) {
5380 /* Insert new group. */
5381 hmap_insert(&ofproto->groups, &ofgroup->hmap_node,
5382 hash_int(ofgroup->group_id, 0));
5383 ofproto->n_groups[ofgroup->type]++;
5384
5385 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5386 return error;
5387 }
5388
5389 unlock_out:
5390 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5391 ofproto->ofproto_class->group_destruct(ofgroup);
7395c052
NZ
5392 ofputil_bucket_list_destroy(&ofgroup->buckets);
5393 ofproto->ofproto_class->group_dealloc(ofgroup);
5394
5395 return error;
5396}
5397
6c5b90ce
BP
5398/* Implements OFPGC11_MODIFY. Returns 0 on success or an OpenFlow error code
5399 * on failure.
7395c052 5400 *
809c7548
RW
5401 * Note that the group is re-created and then replaces the old group in
5402 * ofproto's ofgroup hash map. Thus, the group is never altered while users of
6c5b90ce 5403 * the xlate module hold a pointer to the group. */
7395c052
NZ
5404static enum ofperr
5405modify_group(struct ofproto *ofproto, struct ofputil_group_mod *gm)
5406{
809c7548 5407 struct ofgroup *ofgroup, *new_ofgroup, *retiring;
7395c052
NZ
5408 enum ofperr error;
5409
809c7548
RW
5410 error = init_group(ofproto, gm, &new_ofgroup);
5411 if (error) {
5412 return error;
7395c052
NZ
5413 }
5414
809c7548 5415 retiring = new_ofgroup;
7395c052 5416
3fc3b679
AZ
5417 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
5418 if (!ofproto_group_lookup__(ofproto, gm->group_id, &ofgroup)) {
7395c052 5419 error = OFPERR_OFPGMFC_UNKNOWN_GROUP;
809c7548 5420 goto out;
7395c052 5421 }
809c7548
RW
5422
5423 /* Ofproto's group write lock is held now. */
7395c052
NZ
5424 if (ofgroup->type != gm->type
5425 && ofproto->n_groups[gm->type] >= ofproto->ogf.max_groups[gm->type]) {
5426 error = OFPERR_OFPGMFC_OUT_OF_GROUPS;
809c7548 5427 goto out;
7395c052
NZ
5428 }
5429
809c7548 5430 /* The group creation time does not change during modification. */
eaf004bd
AZ
5431 *CONST_CAST(long long int *, &(new_ofgroup->created)) = ofgroup->created;
5432 *CONST_CAST(long long int *, &(new_ofgroup->modified)) = time_msec();
7395c052 5433
809c7548
RW
5434 error = ofproto->ofproto_class->group_modify(new_ofgroup);
5435 if (error) {
5436 goto out;
5437 }
7395c052 5438
809c7548
RW
5439 retiring = ofgroup;
5440 /* Replace ofgroup in ofproto's groups hash map with new_ofgroup. */
5441 hmap_remove(&ofproto->groups, &ofgroup->hmap_node);
5442 hmap_insert(&ofproto->groups, &new_ofgroup->hmap_node,
5443 hash_int(new_ofgroup->group_id, 0));
5444 if (ofgroup->type != new_ofgroup->type) {
5445 ofproto->n_groups[ofgroup->type]--;
5446 ofproto->n_groups[new_ofgroup->type]++;
7395c052
NZ
5447 }
5448
809c7548
RW
5449out:
5450 ofproto_group_unref(retiring);
7395c052 5451 ovs_rwlock_unlock(&ofproto->groups_rwlock);
7395c052
NZ
5452 return error;
5453}
5454
5455static void
5456delete_group__(struct ofproto *ofproto, struct ofgroup *ofgroup)
5457 OVS_RELEASES(ofproto->groups_rwlock)
5458{
276f6518
SH
5459 struct match match;
5460 struct ofputil_flow_mod fm;
5461
5462 /* Delete all flow entries containing this group in a group action */
5463 match_init_catchall(&match);
5464 flow_mod_init(&fm, &match, 0, NULL, 0, OFPFC_DELETE);
cc40d06b 5465 fm.delete_reason = OFPRR_GROUP_DELETE;
276f6518 5466 fm.out_group = ofgroup->group_id;
baae3d02 5467 handle_flow_mod__(ofproto, &fm, NULL);
276f6518 5468
7395c052
NZ
5469 hmap_remove(&ofproto->groups, &ofgroup->hmap_node);
5470 /* No-one can find this group any more. */
5471 ofproto->n_groups[ofgroup->type]--;
5472 ovs_rwlock_unlock(&ofproto->groups_rwlock);
809c7548 5473 ofproto_group_unref(ofgroup);
7395c052
NZ
5474}
5475
6c5b90ce 5476/* Implements OFPGC11_DELETE. */
7395c052
NZ
5477static void
5478delete_group(struct ofproto *ofproto, uint32_t group_id)
5479{
5480 struct ofgroup *ofgroup;
5481
5482 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
5483 if (group_id == OFPG_ALL) {
5484 for (;;) {
5485 struct hmap_node *node = hmap_first(&ofproto->groups);
5486 if (!node) {
5487 break;
5488 }
5489 ofgroup = CONTAINER_OF(node, struct ofgroup, hmap_node);
5490 delete_group__(ofproto, ofgroup);
5491 /* Lock for each node separately, so that we will not jam the
5492 * other threads for too long time. */
5493 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
5494 }
5495 } else {
5496 HMAP_FOR_EACH_IN_BUCKET (ofgroup, hmap_node,
5497 hash_int(group_id, 0), &ofproto->groups) {
5498 if (ofgroup->group_id == group_id) {
5499 delete_group__(ofproto, ofgroup);
5500 return;
5501 }
5502 }
5503 }
5504 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5505}
5506
5507static enum ofperr
5508handle_group_mod(struct ofconn *ofconn, const struct ofp_header *oh)
5509{
5510 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5511 struct ofputil_group_mod gm;
5512 enum ofperr error;
5513
5514 error = reject_slave_controller(ofconn);
5515 if (error) {
5516 return error;
5517 }
5518
5519 error = ofputil_decode_group_mod(oh, &gm);
5520 if (error) {
5521 return error;
5522 }
5523
5524 switch (gm.command) {
5525 case OFPGC11_ADD:
5526 return add_group(ofproto, &gm);
5527
5528 case OFPGC11_MODIFY:
5529 return modify_group(ofproto, &gm);
5530
5531 case OFPGC11_DELETE:
5532 delete_group(ofproto, gm.group_id);
5533 return 0;
5534
5535 default:
5536 if (gm.command > OFPGC11_DELETE) {
5537 VLOG_WARN_RL(&rl, "%s: Invalid group_mod command type %d",
5538 ofproto->name, gm.command);
5539 }
5540 return OFPERR_OFPGMFC_BAD_COMMAND;
5541 }
5542}
5543
6b83a3c5
SH
5544enum ofproto_table_config
5545ofproto_table_get_config(const struct ofproto *ofproto, uint8_t table_id)
6d328fa2
SH
5546{
5547 unsigned int value;
5548 atomic_read(&ofproto->tables[table_id].config, &value);
6b83a3c5 5549 return (enum ofproto_table_config)value;
6d328fa2
SH
5550}
5551
67761761
SH
5552static enum ofperr
5553table_mod(struct ofproto *ofproto, const struct ofputil_table_mod *tm)
5554{
6d328fa2
SH
5555 /* Only accept currently supported configurations */
5556 if (tm->config & ~OFPTC11_TABLE_MISS_MASK) {
5557 return OFPERR_OFPTMFC_BAD_CONFIG;
5558 }
67761761
SH
5559
5560 if (tm->table_id == OFPTT_ALL) {
5561 int i;
5562 for (i = 0; i < ofproto->n_tables; i++) {
5563 atomic_store(&ofproto->tables[i].config,
5564 (unsigned int)tm->config);
5565 }
5566 } else if (!check_table_id(ofproto, tm->table_id)) {
5567 return OFPERR_OFPTMFC_BAD_TABLE;
5568 } else {
5569 atomic_store(&ofproto->tables[tm->table_id].config,
5570 (unsigned int)tm->config);
5571 }
5572
5573 return 0;
5574}
5575
918f2b82
AZ
5576static enum ofperr
5577handle_table_mod(struct ofconn *ofconn, const struct ofp_header *oh)
5578{
67761761 5579 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
918f2b82
AZ
5580 struct ofputil_table_mod tm;
5581 enum ofperr error;
5582
5583 error = reject_slave_controller(ofconn);
5584 if (error) {
5585 return error;
5586 }
5587
5588 error = ofputil_decode_table_mod(oh, &tm);
5589 if (error) {
5590 return error;
5591 }
5592
67761761 5593 return table_mod(ofproto, &tm);
918f2b82
AZ
5594}
5595
777af88d
AC
5596static enum ofperr
5597handle_bundle_control(struct ofconn *ofconn, const struct ofp_header *oh)
5598{
5599 enum ofperr error;
5600 struct ofputil_bundle_ctrl_msg bctrl;
5601 struct ofpbuf *buf;
5602 struct ofputil_bundle_ctrl_msg reply;
5603
5604 error = ofputil_decode_bundle_ctrl(oh, &bctrl);
5605 if (error) {
5606 return error;
5607 }
5608 reply.flags = 0;
5609 reply.bundle_id = bctrl.bundle_id;
5610
5611 switch (bctrl.type) {
5612 case OFPBCT_OPEN_REQUEST:
5613 error = ofp_bundle_open(ofconn, bctrl.bundle_id, bctrl.flags);
5614 reply.type = OFPBCT_OPEN_REPLY;
5615 break;
5616 case OFPBCT_CLOSE_REQUEST:
5617 error = ofp_bundle_close(ofconn, bctrl.bundle_id, bctrl.flags);
5618 reply.type = OFPBCT_CLOSE_REPLY;;
5619 break;
5620 case OFPBCT_COMMIT_REQUEST:
5621 error = ofp_bundle_commit(ofconn, bctrl.bundle_id, bctrl.flags);
5622 reply.type = OFPBCT_COMMIT_REPLY;
5623 break;
5624 case OFPBCT_DISCARD_REQUEST:
5625 error = ofp_bundle_discard(ofconn, bctrl.bundle_id);
5626 reply.type = OFPBCT_DISCARD_REPLY;
5627 break;
5628
5629 case OFPBCT_OPEN_REPLY:
5630 case OFPBCT_CLOSE_REPLY:
5631 case OFPBCT_COMMIT_REPLY:
5632 case OFPBCT_DISCARD_REPLY:
5633 return OFPERR_OFPBFC_BAD_TYPE;
5634 break;
5635 }
5636
5637 if (!error) {
5638 buf = ofputil_encode_bundle_ctrl_reply(oh, &reply);
5639 ofconn_send_reply(ofconn, buf);
5640 }
5641 return error;
5642}
5643
5644
5645static enum ofperr
5646handle_bundle_add(struct ofconn *ofconn, const struct ofp_header *oh)
5647{
5648 enum ofperr error;
5649 struct ofputil_bundle_add_msg badd;
5650
5651 error = ofputil_decode_bundle_add(oh, &badd);
5652 if (error) {
5653 return error;
5654 }
5655
5656 return ofp_bundle_add_message(ofconn, &badd);
5657}
5658
90bf1e07 5659static enum ofperr
d1e2cf21 5660handle_openflow__(struct ofconn *ofconn, const struct ofpbuf *msg)
15aaf599 5661 OVS_EXCLUDED(ofproto_mutex)
064af421 5662{
1f317cb5 5663 const struct ofp_header *oh = ofpbuf_data(msg);
982697a4 5664 enum ofptype type;
90bf1e07 5665 enum ofperr error;
064af421 5666
982697a4 5667 error = ofptype_decode(&type, oh);
d1e2cf21
BP
5668 if (error) {
5669 return error;
5670 }
ff3c2c63
YT
5671 if (oh->version >= OFP13_VERSION && ofpmsg_is_stat_request(oh)
5672 && ofpmp_more(oh)) {
5673 /* We have no buffer implementation for multipart requests.
5674 * Report overflow for requests which consists of multiple
5675 * messages. */
5676 return OFPERR_OFPBRC_MULTIPART_BUFFER_OVERFLOW;
5677 }
064af421 5678
982697a4 5679 switch (type) {
d1e2cf21 5680 /* OpenFlow requests. */
982697a4 5681 case OFPTYPE_ECHO_REQUEST:
d1e2cf21 5682 return handle_echo_request(ofconn, oh);
064af421 5683
982697a4 5684 case OFPTYPE_FEATURES_REQUEST:
d1e2cf21 5685 return handle_features_request(ofconn, oh);
064af421 5686
982697a4 5687 case OFPTYPE_GET_CONFIG_REQUEST:
d1e2cf21 5688 return handle_get_config_request(ofconn, oh);
064af421 5689
982697a4
BP
5690 case OFPTYPE_SET_CONFIG:
5691 return handle_set_config(ofconn, oh);
064af421 5692
982697a4
BP
5693 case OFPTYPE_PACKET_OUT:
5694 return handle_packet_out(ofconn, oh);
064af421 5695
982697a4 5696 case OFPTYPE_PORT_MOD:
d1e2cf21 5697 return handle_port_mod(ofconn, oh);
064af421 5698
982697a4 5699 case OFPTYPE_FLOW_MOD:
2e4f5fcf 5700 return handle_flow_mod(ofconn, oh);
064af421 5701
7395c052
NZ
5702 case OFPTYPE_GROUP_MOD:
5703 return handle_group_mod(ofconn, oh);
5704
918f2b82
AZ
5705 case OFPTYPE_TABLE_MOD:
5706 return handle_table_mod(ofconn, oh);
5707
9cae45dc
JR
5708 case OFPTYPE_METER_MOD:
5709 return handle_meter_mod(ofconn, oh);
5710
982697a4 5711 case OFPTYPE_BARRIER_REQUEST:
d1e2cf21 5712 return handle_barrier_request(ofconn, oh);
064af421 5713
6ea4776b
JR
5714 case OFPTYPE_ROLE_REQUEST:
5715 return handle_role_request(ofconn, oh);
5716
d1e2cf21 5717 /* OpenFlow replies. */
982697a4 5718 case OFPTYPE_ECHO_REPLY:
d1e2cf21 5719 return 0;
246e61ea 5720
d1e2cf21 5721 /* Nicira extension requests. */
982697a4 5722 case OFPTYPE_FLOW_MOD_TABLE_ID:
6c1491fb 5723 return handle_nxt_flow_mod_table_id(ofconn, oh);
d1e2cf21 5724
982697a4 5725 case OFPTYPE_SET_FLOW_FORMAT:
d1e2cf21
BP
5726 return handle_nxt_set_flow_format(ofconn, oh);
5727
982697a4 5728 case OFPTYPE_SET_PACKET_IN_FORMAT:
54834960
EJ
5729 return handle_nxt_set_packet_in_format(ofconn, oh);
5730
982697a4 5731 case OFPTYPE_SET_CONTROLLER_ID:
a7349929
BP
5732 return handle_nxt_set_controller_id(ofconn, oh);
5733
982697a4 5734 case OFPTYPE_FLOW_AGE:
f27f2134
BP
5735 /* Nothing to do. */
5736 return 0;
5737
982697a4 5738 case OFPTYPE_FLOW_MONITOR_CANCEL:
2b07c8b1
BP
5739 return handle_flow_monitor_cancel(ofconn, oh);
5740
982697a4 5741 case OFPTYPE_SET_ASYNC_CONFIG:
80d5aefd
BP
5742 return handle_nxt_set_async_config(ofconn, oh);
5743
c423b3b3
AC
5744 case OFPTYPE_GET_ASYNC_REQUEST:
5745 return handle_nxt_get_async_request(ofconn, oh);
5746
349adfb2 5747 /* Statistics requests. */
982697a4
BP
5748 case OFPTYPE_DESC_STATS_REQUEST:
5749 return handle_desc_stats_request(ofconn, oh);
5750
5751 case OFPTYPE_FLOW_STATS_REQUEST:
5752 return handle_flow_stats_request(ofconn, oh);
5753
5754 case OFPTYPE_AGGREGATE_STATS_REQUEST:
5755 return handle_aggregate_stats_request(ofconn, oh);
5756
5757 case OFPTYPE_TABLE_STATS_REQUEST:
5758 return handle_table_stats_request(ofconn, oh);
5759
5760 case OFPTYPE_PORT_STATS_REQUEST:
5761 return handle_port_stats_request(ofconn, oh);
5762
5763 case OFPTYPE_QUEUE_STATS_REQUEST:
5764 return handle_queue_stats_request(ofconn, oh);
5765
5766 case OFPTYPE_PORT_DESC_STATS_REQUEST:
5767 return handle_port_desc_stats_request(ofconn, oh);
5768
5769 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
5770 return handle_flow_monitor_request(ofconn, oh);
5771
261bd854
BP
5772 case OFPTYPE_METER_STATS_REQUEST:
5773 case OFPTYPE_METER_CONFIG_STATS_REQUEST:
9cae45dc
JR
5774 return handle_meter_request(ofconn, oh, type);
5775
261bd854 5776 case OFPTYPE_METER_FEATURES_STATS_REQUEST:
9cae45dc
JR
5777 return handle_meter_features_request(ofconn, oh);
5778
261bd854 5779 case OFPTYPE_GROUP_STATS_REQUEST:
7395c052
NZ
5780 return handle_group_stats_request(ofconn, oh);
5781
261bd854 5782 case OFPTYPE_GROUP_DESC_STATS_REQUEST:
7395c052
NZ
5783 return handle_group_desc_stats_request(ofconn, oh);
5784
261bd854 5785 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
7395c052
NZ
5786 return handle_group_features_stats_request(ofconn, oh);
5787
7395c052 5788 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
e8f9a7bb 5789 return handle_queue_get_config_request(ofconn, oh);
2e1ae200 5790
777af88d
AC
5791 case OFPTYPE_BUNDLE_CONTROL:
5792 return handle_bundle_control(ofconn, oh);
5793
5794 case OFPTYPE_BUNDLE_ADD_MESSAGE:
5795 return handle_bundle_add(ofconn, oh);
5796
982697a4
BP
5797 case OFPTYPE_HELLO:
5798 case OFPTYPE_ERROR:
5799 case OFPTYPE_FEATURES_REPLY:
5800 case OFPTYPE_GET_CONFIG_REPLY:
5801 case OFPTYPE_PACKET_IN:
5802 case OFPTYPE_FLOW_REMOVED:
5803 case OFPTYPE_PORT_STATUS:
5804 case OFPTYPE_BARRIER_REPLY:
c545d38d 5805 case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
982697a4
BP
5806 case OFPTYPE_DESC_STATS_REPLY:
5807 case OFPTYPE_FLOW_STATS_REPLY:
5808 case OFPTYPE_QUEUE_STATS_REPLY:
5809 case OFPTYPE_PORT_STATS_REPLY:
5810 case OFPTYPE_TABLE_STATS_REPLY:
5811 case OFPTYPE_AGGREGATE_STATS_REPLY:
5812 case OFPTYPE_PORT_DESC_STATS_REPLY:
5813 case OFPTYPE_ROLE_REPLY:
5814 case OFPTYPE_FLOW_MONITOR_PAUSED:
5815 case OFPTYPE_FLOW_MONITOR_RESUMED:
5816 case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
2e1ae200 5817 case OFPTYPE_GET_ASYNC_REPLY:
261bd854
BP
5818 case OFPTYPE_GROUP_STATS_REPLY:
5819 case OFPTYPE_GROUP_DESC_STATS_REPLY:
5820 case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
5821 case OFPTYPE_METER_STATS_REPLY:
5822 case OFPTYPE_METER_CONFIG_STATS_REPLY:
5823 case OFPTYPE_METER_FEATURES_STATS_REPLY:
e8f9a7bb 5824 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
261bd854 5825 case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
252f3411 5826 case OFPTYPE_ROLE_STATUS:
064af421 5827 default:
76ec08e0
YT
5828 if (ofpmsg_is_stat_request(oh)) {
5829 return OFPERR_OFPBRC_BAD_STAT;
5830 } else {
5831 return OFPERR_OFPBRC_BAD_TYPE;
5832 }
064af421 5833 }
d1e2cf21 5834}
064af421 5835
b20f4073 5836static void
e03248b7 5837handle_openflow(struct ofconn *ofconn, const struct ofpbuf *ofp_msg)
15aaf599 5838 OVS_EXCLUDED(ofproto_mutex)
d1e2cf21
BP
5839{
5840 int error = handle_openflow__(ofconn, ofp_msg);
b20f4073 5841 if (error) {
1f317cb5 5842 ofconn_send_error(ofconn, ofpbuf_data(ofp_msg), error);
064af421 5843 }
d1e2cf21 5844 COVERAGE_INC(ofproto_recv_openflow);
7ee20df1
BP
5845}
5846\f
5847/* Asynchronous operations. */
5848
834fe5cb
BP
5849static enum ofperr
5850send_buffered_packet(struct ofconn *ofconn, uint32_t buffer_id,
5851 struct rule *rule)
15aaf599 5852 OVS_REQUIRES(ofproto_mutex)
7ee20df1 5853{
834fe5cb
BP
5854 enum ofperr error = 0;
5855 if (ofconn && buffer_id != UINT32_MAX) {
5856 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5857 struct ofpbuf *packet;
5858 ofp_port_t in_port;
b277c7cc 5859
834fe5cb
BP
5860 error = ofconn_pktbuf_retrieve(ofconn, buffer_id, &packet, &in_port);
5861 if (packet) {
5862 struct rule_execute *re;
b277c7cc 5863
834fe5cb 5864 ofproto_rule_ref(rule);
b277c7cc 5865
834fe5cb
BP
5866 re = xmalloc(sizeof *re);
5867 re->rule = rule;
5868 re->in_port = in_port;
5869 re->packet = packet;
b277c7cc 5870
834fe5cb
BP
5871 if (!guarded_list_push_back(&ofproto->rule_executes,
5872 &re->list_node, 1024)) {
a2143702 5873 ofproto_rule_unref(rule);
834fe5cb
BP
5874 ofpbuf_delete(re->packet);
5875 free(re);
e615b0a3 5876 }
af822017 5877 }
7ee20df1 5878 }
834fe5cb 5879 return error;
7ee20df1 5880}
064af421 5881\f
064af421 5882static uint64_t
fa60c019 5883pick_datapath_id(const struct ofproto *ofproto)
064af421 5884{
fa60c019 5885 const struct ofport *port;
064af421 5886
abe529af 5887 port = ofproto_get_port(ofproto, OFPP_LOCAL);
fa60c019
BP
5888 if (port) {
5889 uint8_t ea[ETH_ADDR_LEN];
5890 int error;
5891
5892 error = netdev_get_etheraddr(port->netdev, ea);
064af421
BP
5893 if (!error) {
5894 return eth_addr_to_uint64(ea);
5895 }
fbfa2911
BP
5896 VLOG_WARN("%s: could not get MAC address for %s (%s)",
5897 ofproto->name, netdev_get_name(port->netdev),
10a89ef0 5898 ovs_strerror(error));
064af421 5899 }
fa60c019 5900 return ofproto->fallback_dpid;
064af421
BP
5901}
5902
5903static uint64_t
5904pick_fallback_dpid(void)
5905{
5906 uint8_t ea[ETH_ADDR_LEN];
70150daf 5907 eth_addr_nicira_random(ea);
064af421
BP
5908 return eth_addr_to_uint64(ea);
5909}
5910\f
254750ce
BP
5911/* Table overflow policy. */
5912
ad3efdcb
EJ
5913/* Chooses and updates 'rulep' with a rule to evict from 'table'. Sets 'rulep'
5914 * to NULL if the table is not configured to evict rules or if the table
5915 * contains no evictable rules. (Rules with a readlock on their evict rwlock,
5916 * or with no timeouts are not evictable.) */
5917static bool
5918choose_rule_to_evict(struct oftable *table, struct rule **rulep)
15aaf599 5919 OVS_REQUIRES(ofproto_mutex)
254750ce
BP
5920{
5921 struct eviction_group *evg;
5922
ad3efdcb 5923 *rulep = NULL;
254750ce 5924 if (!table->eviction_fields) {
ad3efdcb 5925 return false;
254750ce
BP
5926 }
5927
5928 /* In the common case, the outer and inner loops here will each be entered
5929 * exactly once:
5930 *
5931 * - The inner loop normally "return"s in its first iteration. If the
5932 * eviction group has any evictable rules, then it always returns in
5933 * some iteration.
5934 *
5935 * - The outer loop only iterates more than once if the largest eviction
5936 * group has no evictable rules.
5937 *
5938 * - The outer loop can exit only if table's 'max_flows' is all filled up
afe2143d 5939 * by unevictable rules. */
254750ce
BP
5940 HEAP_FOR_EACH (evg, size_node, &table->eviction_groups_by_size) {
5941 struct rule *rule;
5942
5943 HEAP_FOR_EACH (rule, evg_node, &evg->rules) {
15aaf599
BP
5944 *rulep = rule;
5945 return true;
254750ce
BP
5946 }
5947 }
5948
ad3efdcb 5949 return false;
254750ce 5950}
254750ce
BP
5951\f
5952/* Eviction groups. */
5953
5954/* Returns the priority to use for an eviction_group that contains 'n_rules'
5955 * rules. The priority contains low-order random bits to ensure that eviction
5956 * groups with the same number of rules are prioritized randomly. */
5957static uint32_t
5958eviction_group_priority(size_t n_rules)
5959{
5960 uint16_t size = MIN(UINT16_MAX, n_rules);
5961 return (size << 16) | random_uint16();
5962}
5963
5964/* Updates 'evg', an eviction_group within 'table', following a change that
5965 * adds or removes rules in 'evg'. */
5966static void
5967eviction_group_resized(struct oftable *table, struct eviction_group *evg)
15aaf599 5968 OVS_REQUIRES(ofproto_mutex)
254750ce
BP
5969{
5970 heap_change(&table->eviction_groups_by_size, &evg->size_node,
5971 eviction_group_priority(heap_count(&evg->rules)));
5972}
5973
5974/* Destroys 'evg', an eviction_group within 'table':
5975 *
5976 * - Removes all the rules, if any, from 'evg'. (It doesn't destroy the
5977 * rules themselves, just removes them from the eviction group.)
5978 *
5979 * - Removes 'evg' from 'table'.
5980 *
5981 * - Frees 'evg'. */
5982static void
5983eviction_group_destroy(struct oftable *table, struct eviction_group *evg)
15aaf599 5984 OVS_REQUIRES(ofproto_mutex)
254750ce
BP
5985{
5986 while (!heap_is_empty(&evg->rules)) {
5987 struct rule *rule;
5988
5989 rule = CONTAINER_OF(heap_pop(&evg->rules), struct rule, evg_node);
5990 rule->eviction_group = NULL;
5991 }
5992 hmap_remove(&table->eviction_groups_by_id, &evg->id_node);
5993 heap_remove(&table->eviction_groups_by_size, &evg->size_node);
5994 heap_destroy(&evg->rules);
5995 free(evg);
5996}
5997
5998/* Removes 'rule' from its eviction group, if any. */
5999static void
6000eviction_group_remove_rule(struct rule *rule)
15aaf599 6001 OVS_REQUIRES(ofproto_mutex)
254750ce
BP
6002{
6003 if (rule->eviction_group) {
6004 struct oftable *table = &rule->ofproto->tables[rule->table_id];
6005 struct eviction_group *evg = rule->eviction_group;
6006
6007 rule->eviction_group = NULL;
6008 heap_remove(&evg->rules, &rule->evg_node);
6009 if (heap_is_empty(&evg->rules)) {
6010 eviction_group_destroy(table, evg);
6011 } else {
6012 eviction_group_resized(table, evg);
6013 }
6014 }
6015}
6016
6017/* Hashes the 'rule''s values for the eviction_fields of 'rule''s table, and
6018 * returns the hash value. */
6019static uint32_t
6020eviction_group_hash_rule(struct rule *rule)
15aaf599 6021 OVS_REQUIRES(ofproto_mutex)
254750ce
BP
6022{
6023 struct oftable *table = &rule->ofproto->tables[rule->table_id];
6024 const struct mf_subfield *sf;
5cb7a798 6025 struct flow flow;
254750ce
BP
6026 uint32_t hash;
6027
6028 hash = table->eviction_group_id_basis;
5cb7a798 6029 miniflow_expand(&rule->cr.match.flow, &flow);
254750ce
BP
6030 for (sf = table->eviction_fields;
6031 sf < &table->eviction_fields[table->n_eviction_fields];
6032 sf++)
6033 {
5cb7a798 6034 if (mf_are_prereqs_ok(sf->field, &flow)) {
254750ce
BP
6035 union mf_value value;
6036
5cb7a798 6037 mf_get_value(sf->field, &flow, &value);
254750ce
BP
6038 if (sf->ofs) {
6039 bitwise_zero(&value, sf->field->n_bytes, 0, sf->ofs);
6040 }
6041 if (sf->ofs + sf->n_bits < sf->field->n_bytes * 8) {
6042 unsigned int start = sf->ofs + sf->n_bits;
6043 bitwise_zero(&value, sf->field->n_bytes, start,
6044 sf->field->n_bytes * 8 - start);
6045 }
6046 hash = hash_bytes(&value, sf->field->n_bytes, hash);
6047 } else {
6048 hash = hash_int(hash, 0);
6049 }
6050 }
6051
6052 return hash;
6053}
6054
6055/* Returns an eviction group within 'table' with the given 'id', creating one
6056 * if necessary. */
6057static struct eviction_group *
6058eviction_group_find(struct oftable *table, uint32_t id)
15aaf599 6059 OVS_REQUIRES(ofproto_mutex)
254750ce
BP
6060{
6061 struct eviction_group *evg;
6062
6063 HMAP_FOR_EACH_WITH_HASH (evg, id_node, id, &table->eviction_groups_by_id) {
6064 return evg;
6065 }
6066
6067 evg = xmalloc(sizeof *evg);
6068 hmap_insert(&table->eviction_groups_by_id, &evg->id_node, id);
6069 heap_insert(&table->eviction_groups_by_size, &evg->size_node,
6070 eviction_group_priority(0));
6071 heap_init(&evg->rules);
6072
6073 return evg;
6074}
6075
6076/* Returns an eviction priority for 'rule'. The return value should be
6077 * interpreted so that higher priorities make a rule more attractive candidates
dc437090
JR
6078 * for eviction.
6079 * Called only if have a timeout. */
254750ce 6080static uint32_t
dc437090 6081rule_eviction_priority(struct ofproto *ofproto, struct rule *rule)
15aaf599 6082 OVS_REQUIRES(ofproto_mutex)
254750ce 6083{
dc437090
JR
6084 long long int expiration = LLONG_MAX;
6085 long long int modified;
254750ce
BP
6086 uint32_t expiration_offset;
6087
dc437090 6088 /* 'modified' needs protection even when we hold 'ofproto_mutex'. */
d10976b7 6089 ovs_mutex_lock(&rule->mutex);
dc437090 6090 modified = rule->modified;
d10976b7 6091 ovs_mutex_unlock(&rule->mutex);
dc437090
JR
6092
6093 if (rule->hard_timeout) {
6094 expiration = modified + rule->hard_timeout * 1000;
6095 }
6096 if (rule->idle_timeout) {
6097 uint64_t packets, bytes;
6098 long long int used;
6099 long long int idle_expiration;
6100
6101 ofproto->ofproto_class->rule_get_stats(rule, &packets, &bytes, &used);
6102 idle_expiration = used + rule->idle_timeout * 1000;
6103 expiration = MIN(expiration, idle_expiration);
6104 }
6105
254750ce
BP
6106 if (expiration == LLONG_MAX) {
6107 return 0;
6108 }
6109
6110 /* Calculate the time of expiration as a number of (approximate) seconds
6111 * after program startup.
6112 *
6113 * This should work OK for program runs that last UINT32_MAX seconds or
6114 * less. Therefore, please restart OVS at least once every 136 years. */
6115 expiration_offset = (expiration >> 10) - (time_boot_msec() >> 10);
6116
6117 /* Invert the expiration offset because we're using a max-heap. */
6118 return UINT32_MAX - expiration_offset;
6119}
6120
6121/* Adds 'rule' to an appropriate eviction group for its oftable's
6122 * configuration. Does nothing if 'rule''s oftable doesn't have eviction
6123 * enabled, or if 'rule' is a permanent rule (one that will never expire on its
6124 * own).
6125 *
6126 * The caller must ensure that 'rule' is not already in an eviction group. */
6127static void
6128eviction_group_add_rule(struct rule *rule)
15aaf599 6129 OVS_REQUIRES(ofproto_mutex)
254750ce
BP
6130{
6131 struct ofproto *ofproto = rule->ofproto;
6132 struct oftable *table = &ofproto->tables[rule->table_id];
a3779dbc 6133 bool has_timeout;
254750ce 6134
dc437090
JR
6135 /* Timeouts may be modified only when holding 'ofproto_mutex'. We have it
6136 * so no additional protection is needed. */
a3779dbc 6137 has_timeout = rule->hard_timeout || rule->idle_timeout;
a3779dbc
EJ
6138
6139 if (table->eviction_fields && has_timeout) {
254750ce
BP
6140 struct eviction_group *evg;
6141
6142 evg = eviction_group_find(table, eviction_group_hash_rule(rule));
6143
6144 rule->eviction_group = evg;
6145 heap_insert(&evg->rules, &rule->evg_node,
dc437090 6146 rule_eviction_priority(ofproto, rule));
254750ce
BP
6147 eviction_group_resized(table, evg);
6148 }
6149}
6150\f
d0918789
BP
6151/* oftables. */
6152
6153/* Initializes 'table'. */
6154static void
6155oftable_init(struct oftable *table)
6156{
5c67e4af 6157 memset(table, 0, sizeof *table);
476f36e8 6158 classifier_init(&table->cls, flow_segment_u32s);
ec1c5c7e 6159 table->max_flows = UINT_MAX;
6b83a3c5 6160 atomic_init(&table->config, (unsigned int)OFPROTO_TABLE_MISS_DEFAULT);
f017d986
JR
6161
6162 fat_rwlock_wrlock(&table->cls.rwlock);
6163 classifier_set_prefix_fields(&table->cls, default_prefix_fields,
6164 ARRAY_SIZE(default_prefix_fields));
6165 fat_rwlock_unlock(&table->cls.rwlock);
d611866c
SH
6166
6167 atomic_init(&table->n_matched, 0);
6168 atomic_init(&table->n_missed, 0);
d0918789
BP
6169}
6170
254750ce 6171/* Destroys 'table', including its classifier and eviction groups.
d0918789
BP
6172 *
6173 * The caller is responsible for freeing 'table' itself. */
6174static void
6175oftable_destroy(struct oftable *table)
6176{
06f81620 6177 fat_rwlock_rdlock(&table->cls.rwlock);
cb22974d 6178 ovs_assert(classifier_is_empty(&table->cls));
06f81620 6179 fat_rwlock_unlock(&table->cls.rwlock);
254750ce 6180 oftable_disable_eviction(table);
d0918789 6181 classifier_destroy(&table->cls);
254750ce
BP
6182 free(table->name);
6183}
6184
6185/* Changes the name of 'table' to 'name'. If 'name' is NULL or the empty
6186 * string, then 'table' will use its default name.
6187 *
6188 * This only affects the name exposed for a table exposed through the OpenFlow
6189 * OFPST_TABLE (as printed by "ovs-ofctl dump-tables"). */
6190static void
6191oftable_set_name(struct oftable *table, const char *name)
6192{
6193 if (name && name[0]) {
6194 int len = strnlen(name, OFP_MAX_TABLE_NAME_LEN);
6195 if (!table->name || strncmp(name, table->name, len)) {
6196 free(table->name);
6197 table->name = xmemdup0(name, len);
6198 }
6199 } else {
6200 free(table->name);
6201 table->name = NULL;
6202 }
6203}
6204
6205/* oftables support a choice of two policies when adding a rule would cause the
6206 * number of flows in the table to exceed the configured maximum number: either
6207 * they can refuse to add the new flow or they can evict some existing flow.
6208 * This function configures the former policy on 'table'. */
6209static void
6210oftable_disable_eviction(struct oftable *table)
15aaf599 6211 OVS_REQUIRES(ofproto_mutex)
254750ce
BP
6212{
6213 if (table->eviction_fields) {
6214 struct eviction_group *evg, *next;
6215
6216 HMAP_FOR_EACH_SAFE (evg, next, id_node,
6217 &table->eviction_groups_by_id) {
6218 eviction_group_destroy(table, evg);
6219 }
6220 hmap_destroy(&table->eviction_groups_by_id);
6221 heap_destroy(&table->eviction_groups_by_size);
6222
6223 free(table->eviction_fields);
6224 table->eviction_fields = NULL;
6225 table->n_eviction_fields = 0;
6226 }
6227}
6228
6229/* oftables support a choice of two policies when adding a rule would cause the
6230 * number of flows in the table to exceed the configured maximum number: either
6231 * they can refuse to add the new flow or they can evict some existing flow.
6232 * This function configures the latter policy on 'table', with fairness based
6233 * on the values of the 'n_fields' fields specified in 'fields'. (Specifying
6234 * 'n_fields' as 0 disables fairness.) */
6235static void
6236oftable_enable_eviction(struct oftable *table,
6237 const struct mf_subfield *fields, size_t n_fields)
15aaf599 6238 OVS_REQUIRES(ofproto_mutex)
254750ce
BP
6239{
6240 struct cls_cursor cursor;
6241 struct rule *rule;
6242
6243 if (table->eviction_fields
6244 && n_fields == table->n_eviction_fields
6245 && (!n_fields
6246 || !memcmp(fields, table->eviction_fields,
6247 n_fields * sizeof *fields))) {
6248 /* No change. */
6249 return;
6250 }
6251
6252 oftable_disable_eviction(table);
6253
6254 table->n_eviction_fields = n_fields;
6255 table->eviction_fields = xmemdup(fields, n_fields * sizeof *fields);
6256
6257 table->eviction_group_id_basis = random_uint32();
6258 hmap_init(&table->eviction_groups_by_id);
6259 heap_init(&table->eviction_groups_by_size);
6260
06f81620 6261 fat_rwlock_rdlock(&table->cls.rwlock);
254750ce
BP
6262 cls_cursor_init(&cursor, &table->cls, NULL);
6263 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
6264 eviction_group_add_rule(rule);
6265 }
06f81620 6266 fat_rwlock_unlock(&table->cls.rwlock);
d0918789
BP
6267}
6268
6269/* Removes 'rule' from the oftable that contains it. */
6270static void
8b81d1ef 6271oftable_remove_rule__(struct ofproto *ofproto, struct rule *rule)
15aaf599 6272 OVS_REQUIRES(ofproto_mutex)
d0918789 6273{
8b81d1ef
BP
6274 struct classifier *cls = &ofproto->tables[rule->table_id].cls;
6275
06f81620 6276 fat_rwlock_wrlock(&cls->rwlock);
49a0e0eb 6277 classifier_remove(cls, CONST_CAST(struct cls_rule *, &rule->cr));
06f81620 6278 fat_rwlock_unlock(&cls->rwlock);
2c916028 6279
98eaac36 6280 cookies_remove(ofproto, rule);
2c916028 6281
254750ce 6282 eviction_group_remove_rule(rule);
e503cc19
SH
6283 if (!list_is_empty(&rule->expirable)) {
6284 list_remove(&rule->expirable);
6285 }
9cae45dc
JR
6286 if (!list_is_empty(&rule->meter_list_node)) {
6287 list_remove(&rule->meter_list_node);
51b50dfd 6288 list_init(&rule->meter_list_node);
9cae45dc 6289 }
d0918789
BP
6290}
6291
0b4f2078
EJ
6292static void
6293oftable_remove_rule(struct rule *rule)
15aaf599 6294 OVS_REQUIRES(ofproto_mutex)
0b4f2078 6295{
8b81d1ef 6296 oftable_remove_rule__(rule->ofproto, rule);
0b4f2078 6297}
d0918789 6298\f
abe529af 6299/* unixctl commands. */
7aa697dd 6300
abe529af 6301struct ofproto *
2ac6bedd 6302ofproto_lookup(const char *name)
7aa697dd 6303{
2ac6bedd 6304 struct ofproto *ofproto;
7aa697dd 6305
2ac6bedd
BP
6306 HMAP_FOR_EACH_WITH_HASH (ofproto, hmap_node, hash_string(name, 0),
6307 &all_ofprotos) {
6308 if (!strcmp(ofproto->name, name)) {
6309 return ofproto;
6310 }
7aa697dd 6311 }
2ac6bedd 6312 return NULL;
7aa697dd
BP
6313}
6314
6315static void
0e15264f
BP
6316ofproto_unixctl_list(struct unixctl_conn *conn, int argc OVS_UNUSED,
6317 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
7aa697dd 6318{
7aa697dd 6319 struct ofproto *ofproto;
7aa697dd 6320 struct ds results;
7aa697dd 6321
7aa697dd 6322 ds_init(&results);
2ac6bedd
BP
6323 HMAP_FOR_EACH (ofproto, hmap_node, &all_ofprotos) {
6324 ds_put_format(&results, "%s\n", ofproto->name);
7aa697dd 6325 }
bde9f75d 6326 unixctl_command_reply(conn, ds_cstr(&results));
7aa697dd 6327 ds_destroy(&results);
7aa697dd
BP
6328}
6329
6330static void
6331ofproto_unixctl_init(void)
6332{
6333 static bool registered;
6334 if (registered) {
6335 return;
6336 }
6337 registered = true;
6338
0e15264f
BP
6339 unixctl_command_register("ofproto/list", "", 0, 0,
6340 ofproto_unixctl_list, NULL);
064af421 6341}
52a90c29
BP
6342\f
6343/* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
6344 *
6345 * This is deprecated. It is only for compatibility with broken device drivers
6346 * in old versions of Linux that do not properly support VLANs when VLAN
6347 * devices are not used. When broken device drivers are no longer in
6348 * widespread use, we will delete these interfaces. */
6349
6350/* Sets a 1-bit in the 4096-bit 'vlan_bitmap' for each VLAN ID that is matched
6351 * (exactly) by an OpenFlow rule in 'ofproto'. */
6352void
6353ofproto_get_vlan_usage(struct ofproto *ofproto, unsigned long int *vlan_bitmap)
6354{
ac4aa4c8
JR
6355 struct match match;
6356 struct cls_rule target;
d0918789 6357 const struct oftable *oftable;
52a90c29 6358
ac4aa4c8
JR
6359 match_init_catchall(&match);
6360 match_set_vlan_vid_masked(&match, htons(VLAN_CFI), htons(VLAN_CFI));
6361 cls_rule_init(&target, &match, 0);
6362
52a90c29
BP
6363 free(ofproto->vlan_bitmap);
6364 ofproto->vlan_bitmap = bitmap_allocate(4096);
6365 ofproto->vlans_changed = false;
6366
d0918789 6367 OFPROTO_FOR_EACH_TABLE (oftable, ofproto) {
ac4aa4c8
JR
6368 struct cls_cursor cursor;
6369 struct rule *rule;
52a90c29 6370
06f81620 6371 fat_rwlock_rdlock(&oftable->cls.rwlock);
ac4aa4c8
JR
6372 cls_cursor_init(&cursor, &oftable->cls, &target);
6373 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
6374 if (minimask_get_vid_mask(&rule->cr.match.mask) == VLAN_VID_MASK) {
6375 uint16_t vid = miniflow_get_vid(&rule->cr.match.flow);
6376
6377 bitmap_set1(vlan_bitmap, vid);
6378 bitmap_set1(ofproto->vlan_bitmap, vid);
52a90c29
BP
6379 }
6380 }
06f81620 6381 fat_rwlock_unlock(&oftable->cls.rwlock);
52a90c29 6382 }
059ef3c6
EJ
6383
6384 cls_rule_destroy(&target);
52a90c29
BP
6385}
6386
6387/* Returns true if new VLANs have come into use by the flow table since the
6388 * last call to ofproto_get_vlan_usage().
6389 *
6390 * We don't track when old VLANs stop being used. */
6391bool
6392ofproto_has_vlan_usage_changed(const struct ofproto *ofproto)
6393{
6394 return ofproto->vlans_changed;
6395}
6396
6397/* Configures a VLAN splinter binding between the ports identified by OpenFlow
6398 * port numbers 'vlandev_ofp_port' and 'realdev_ofp_port'. If
6399 * 'realdev_ofp_port' is nonzero, then the VLAN device is enslaved to the real
6400 * device as a VLAN splinter for VLAN ID 'vid'. If 'realdev_ofp_port' is zero,
6401 * then the VLAN device is un-enslaved. */
6402int
4e022ec0
AW
6403ofproto_port_set_realdev(struct ofproto *ofproto, ofp_port_t vlandev_ofp_port,
6404 ofp_port_t realdev_ofp_port, int vid)
52a90c29
BP
6405{
6406 struct ofport *ofport;
6407 int error;
6408
cb22974d 6409 ovs_assert(vlandev_ofp_port != realdev_ofp_port);
52a90c29
BP
6410
6411 ofport = ofproto_get_port(ofproto, vlandev_ofp_port);
6412 if (!ofport) {
6413 VLOG_WARN("%s: cannot set realdev on nonexistent port %"PRIu16,
6414 ofproto->name, vlandev_ofp_port);
6415 return EINVAL;
6416 }
6417
6418 if (!ofproto->ofproto_class->set_realdev) {
6419 if (!vlandev_ofp_port) {
6420 return 0;
6421 }
6422 VLOG_WARN("%s: vlan splinters not supported", ofproto->name);
6423 return EOPNOTSUPP;
6424 }
6425
6426 error = ofproto->ofproto_class->set_realdev(ofport, realdev_ofp_port, vid);
6427 if (error) {
6428 VLOG_WARN("%s: setting realdev on port %"PRIu16" (%s) failed (%s)",
6429 ofproto->name, vlandev_ofp_port,
10a89ef0 6430 netdev_get_name(ofport->netdev), ovs_strerror(error));
52a90c29
BP
6431 }
6432 return error;
6433}