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