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