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