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