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