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