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