]> git.proxmox.com Git - mirror_ovs.git/blame - ofproto/ofproto.c
ofp-util: remove flow mod's delete_reason.
[mirror_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 *,
dd27be82 268 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
283static void replace_rule_finish(struct ofproto *, struct ofputil_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{
8be00367 4797 struct ofputil_flow_mod *fm = &ofm->fm;
fe59694b
JR
4798 struct rule *old_rule = rule_collection_stub(&ofm->old_rules)[0];
4799 struct rule *new_rule = rule_collection_stub(&ofm->new_rules)[0];
39c94593 4800 struct ovs_list dead_cookies = OVS_LIST_INITIALIZER(&dead_cookies);
8037acb4 4801
39c94593
JR
4802 replace_rule_finish(ofproto, fm, req, old_rule, new_rule, &dead_cookies);
4803 learned_cookies_flush(ofproto, &dead_cookies);
802f84ff 4804
39c94593
JR
4805 if (old_rule) {
4806 ovsrcu_postpone(remove_rule_rcu, old_rule);
4807 } else {
39c94593 4808 ofmonitor_report(ofproto->connmgr, new_rule, NXFME_ADDED, 0,
c84d8691
JR
4809 req ? req->ofconn : NULL,
4810 req ? req->request->xid : 0, NULL);
6c6eedc5
SJ
4811
4812 /* Send Vacancy Events for OF1.4+. */
4813 send_table_status(ofproto, new_rule->table_id);
c84d8691
JR
4814 }
4815
39c94593 4816 send_buffered_packet(req, fm->buffer_id, new_rule);
c84d8691 4817}
79eee1eb
BP
4818\f
4819/* OFPFC_MODIFY and OFPFC_MODIFY_STRICT. */
064af421 4820
39c94593
JR
4821/* Create a new rule based on attributes in 'fm', match in 'cr', 'table_id',
4822 * and 'old_rule'. Note that the rule is NOT inserted into a any data
4823 * structures yet. Takes ownership of 'cr'. */
90bf1e07 4824static enum ofperr
39c94593
JR
4825replace_rule_create(struct ofproto *ofproto, struct ofputil_flow_mod *fm,
4826 struct cls_rule *cr, uint8_t table_id,
4827 struct rule *old_rule, struct rule **new_rule)
79eee1eb 4828{
39c94593 4829 struct rule *rule;
dd27be82 4830 enum ofperr error;
79eee1eb 4831
39c94593
JR
4832 /* Allocate new rule. */
4833 rule = ofproto->ofproto_class->rule_alloc();
4834 if (!rule) {
4835 cls_rule_destroy(cr);
4836 VLOG_WARN_RL(&rl, "%s: failed to allocate a rule.", ofproto->name);
4837 return OFPERR_OFPFMFC_UNKNOWN;
af822017
BP
4838 }
4839
39c94593
JR
4840 /* Initialize base state. */
4841 *CONST_CAST(struct ofproto **, &rule->ofproto) = ofproto;
4842 cls_rule_move(CONST_CAST(struct cls_rule *, &rule->cr), cr);
4843 ovs_refcount_init(&rule->ref_count);
4844 rule->flow_cookie = fm->new_cookie;
4845 rule->created = rule->modified = time_msec();
834fe5cb 4846
39c94593
JR
4847 ovs_mutex_init(&rule->mutex);
4848 ovs_mutex_lock(&rule->mutex);
4849 rule->idle_timeout = fm->idle_timeout;
4850 rule->hard_timeout = fm->hard_timeout;
cf119e09 4851 *CONST_CAST(uint16_t *, &rule->importance) = fm->importance;
f695ebfa 4852 rule->removed_reason = OVS_OFPRR_NONE;
834fe5cb 4853
39c94593
JR
4854 *CONST_CAST(uint8_t *, &rule->table_id) = table_id;
4855 rule->flags = fm->flags & OFPUTIL_FF_STATE;
4856 *CONST_CAST(const struct rule_actions **, &rule->actions)
4857 = rule_actions_create(fm->ofpacts, fm->ofpacts_len);
417e7e66 4858 ovs_list_init(&rule->meter_list_node);
39c94593 4859 rule->eviction_group = NULL;
417e7e66 4860 ovs_list_init(&rule->expirable);
39c94593
JR
4861 rule->monitor_flags = 0;
4862 rule->add_seqno = 0;
4863 rule->modify_seqno = 0;
834fe5cb 4864
39c94593 4865 /* Copy values from old rule for modify semantics. */
5aacc3e2 4866 if (old_rule && old_rule->removed_reason != OFPRR_EVICTION) {
39c94593
JR
4867 bool change_cookie = (fm->modify_cookie
4868 && fm->new_cookie != OVS_BE64_MAX
4869 && fm->new_cookie != old_rule->flow_cookie);
4870
4871 ovs_mutex_lock(&old_rule->mutex);
4872 if (fm->command != OFPFC_ADD) {
4873 rule->idle_timeout = old_rule->idle_timeout;
4874 rule->hard_timeout = old_rule->hard_timeout;
cf119e09 4875 *CONST_CAST(uint16_t *, &rule->importance) = old_rule->importance;
39c94593
JR
4876 rule->flags = old_rule->flags;
4877 rule->created = old_rule->created;
4878 }
4879 if (!change_cookie) {
4880 rule->flow_cookie = old_rule->flow_cookie;
4881 }
4882 ovs_mutex_unlock(&old_rule->mutex);
dd27be82 4883 }
dd27be82
JR
4884 ovs_mutex_unlock(&rule->mutex);
4885
39c94593
JR
4886 /* Construct rule, initializing derived state. */
4887 error = ofproto->ofproto_class->rule_construct(rule);
4888 if (error) {
4889 ofproto_rule_destroy__(rule);
4890 return error;
dd27be82 4891 }
b277c7cc 4892
39c94593 4893 rule->removed = true; /* Not yet in ofproto data structures. */
dd27be82 4894
39c94593
JR
4895 *new_rule = rule;
4896 return 0;
4897}
884e1dc4 4898
39c94593 4899static void
44e0c35d 4900replace_rule_start(struct ofproto *ofproto, ovs_version_t version,
39c94593
JR
4901 struct rule *old_rule, struct rule *new_rule,
4902 struct cls_conjunction *conjs, size_t n_conjs)
4903{
4904 struct oftable *table = &ofproto->tables[new_rule->table_id];
834fe5cb 4905
6787a49f 4906 /* 'old_rule' may be either an evicted rule or replaced rule. */
39c94593
JR
4907 if (old_rule) {
4908 /* Mark the old rule for removal in the next version. */
8be00367 4909 cls_rule_make_invisible_in_version(&old_rule->cr, version);
9bab38ff
JR
4910
4911 /* Remove the old rule from data structures. */
4912 ofproto_rule_remove__(ofproto, old_rule);
d79e3d70
JR
4913 } else {
4914 table->n_flows++;
dd27be82 4915 }
cc099268
JR
4916 /* Insert flow to ofproto data structures, so that later flow_mods may
4917 * relate to it. This is reversible, in case later errors require this to
39c94593
JR
4918 * be reverted. */
4919 ofproto_rule_insert__(ofproto, new_rule);
4920 /* Make the new rule visible for classifier lookups only from the next
4921 * version. */
bd53aa17 4922 classifier_insert(&table->cls, &new_rule->cr, version, conjs, n_conjs);
39c94593
JR
4923}
4924
cc099268
JR
4925static void
4926replace_rule_revert(struct ofproto *ofproto,
4927 struct rule *old_rule, struct rule *new_rule)
39c94593
JR
4928{
4929 struct oftable *table = &ofproto->tables[new_rule->table_id];
35f48b8b 4930
39c94593 4931 if (old_rule) {
9bab38ff
JR
4932 /* Restore the old rule to data structures. */
4933 ofproto_rule_insert__(ofproto, old_rule);
4934
d79e3d70 4935 /* Restore the original visibility of the old rule. */
39c94593 4936 cls_rule_restore_visibility(&old_rule->cr);
d79e3d70
JR
4937 } else {
4938 /* Restore table's rule count. */
4939 table->n_flows--;
834fe5cb
BP
4940 }
4941
39c94593
JR
4942 /* Remove the new rule immediately. It was never visible to lookups. */
4943 if (!classifier_remove(&table->cls, &new_rule->cr)) {
4944 OVS_NOT_REACHED();
5ecc9d81 4945 }
39c94593
JR
4946 ofproto_rule_remove__(ofproto, new_rule);
4947 /* The rule was not inserted to the ofproto provider, so we can
4948 * release it without deleting it from the ofproto provider. */
4949 ofproto_rule_unref(new_rule);
dd27be82 4950}
79eee1eb 4951
39c94593 4952/* Adds the 'new_rule', replacing the 'old_rule'. */
dd27be82 4953static void
39c94593 4954replace_rule_finish(struct ofproto *ofproto, struct ofputil_flow_mod *fm,
0a0d9385 4955 const struct openflow_mod_requester *req,
39c94593
JR
4956 struct rule *old_rule, struct rule *new_rule,
4957 struct ovs_list *dead_cookies)
dd27be82
JR
4958 OVS_REQUIRES(ofproto_mutex)
4959{
748eb2f5 4960 bool forward_counts = !(new_rule->flags & OFPUTIL_FF_RESET_COUNTS);
6787a49f
JR
4961 struct rule *replaced_rule;
4962
5aacc3e2
JR
4963 replaced_rule = (old_rule && old_rule->removed_reason != OFPRR_EVICTION)
4964 ? old_rule : NULL;
dd27be82 4965
6787a49f
JR
4966 /* Insert the new flow to the ofproto provider. A non-NULL 'replaced_rule'
4967 * is a duplicate rule the 'new_rule' is replacing. The provider should
748eb2f5
JR
4968 * link the packet and byte counts from the old rule to the new one if
4969 * 'forward_counts' is 'true'. The 'replaced_rule' will be deleted right
4970 * after this call. */
6787a49f 4971 ofproto->ofproto_class->rule_insert(new_rule, replaced_rule,
748eb2f5 4972 forward_counts);
39c94593
JR
4973 learned_cookies_inc(ofproto, rule_get_actions(new_rule));
4974
4975 if (old_rule) {
4976 const struct rule_actions *old_actions = rule_get_actions(old_rule);
4977
39c94593
JR
4978 learned_cookies_dec(ofproto, old_actions, dead_cookies);
4979
6787a49f
JR
4980 if (replaced_rule) {
4981 enum nx_flow_update_event event = fm->command == OFPFC_ADD
4982 ? NXFME_ADDED : NXFME_MODIFIED;
4983
4984 bool change_cookie = (fm->modify_cookie
4985 && fm->new_cookie != OVS_BE64_MAX
4986 && fm->new_cookie != old_rule->flow_cookie);
4987
4988 bool change_actions = !ofpacts_equal(fm->ofpacts,
4989 fm->ofpacts_len,
4990 old_actions->ofpacts,
4991 old_actions->ofpacts_len);
4992
4993 if (event != NXFME_MODIFIED || change_actions || change_cookie) {
4994 ofmonitor_report(ofproto->connmgr, new_rule, event, 0,
4995 req ? req->ofconn : NULL,
4996 req ? req->request->xid : 0,
4997 change_actions ? old_actions : NULL);
4998 }
4999 } else {
5000 /* XXX: This is slight duplication with delete_flows_finish__() */
6787a49f
JR
5001 ofmonitor_report(ofproto->connmgr, old_rule, NXFME_DELETED,
5002 OFPRR_EVICTION,
39c94593 5003 req ? req->ofconn : NULL,
6787a49f 5004 req ? req->request->xid : 0, NULL);
39c94593 5005 }
dd27be82 5006 }
9ed18e46
BP
5007}
5008
9387b970 5009static enum ofperr
8be00367 5010modify_flows_start__(struct ofproto *ofproto, struct ofproto_flow_mod *ofm)
dd27be82
JR
5011 OVS_REQUIRES(ofproto_mutex)
5012{
8be00367
JR
5013 struct ofputil_flow_mod *fm = &ofm->fm;
5014 struct rule_collection *old_rules = &ofm->old_rules;
5015 struct rule_collection *new_rules = &ofm->new_rules;
dd27be82
JR
5016 enum ofperr error;
5017
39c94593
JR
5018 rule_collection_init(new_rules);
5019
fe59694b 5020 if (rule_collection_n(old_rules) > 0) {
39c94593
JR
5021 struct cls_conjunction *conjs;
5022 size_t n_conjs;
39c94593
JR
5023
5024 /* Create a new 'modified' rule for each old rule. */
fe59694b
JR
5025 struct rule *old_rule;
5026 RULE_COLLECTION_FOR_EACH (old_rule, old_rules) {
39c94593
JR
5027 struct rule *new_rule;
5028 struct cls_rule cr;
5029
bd53aa17 5030 cls_rule_clone(&cr, &old_rule->cr);
39c94593
JR
5031 error = replace_rule_create(ofproto, fm, &cr, old_rule->table_id,
5032 old_rule, &new_rule);
5033 if (!error) {
5034 rule_collection_add(new_rules, new_rule);
5035 } else {
5036 rule_collection_unref(new_rules);
5037 rule_collection_destroy(new_rules);
5038 return error;
5039 }
5040 }
fe59694b
JR
5041 ovs_assert(rule_collection_n(new_rules)
5042 == rule_collection_n(old_rules));
39c94593
JR
5043
5044 get_conjunctions(fm, &conjs, &n_conjs);
fe59694b
JR
5045 struct rule *new_rule;
5046 RULE_COLLECTIONS_FOR_EACH (old_rule, new_rule, old_rules, new_rules) {
5047 replace_rule_start(ofproto, ofm->version, old_rule, new_rule,
5048 conjs, n_conjs);
39c94593
JR
5049 }
5050 free(conjs);
d99f64d7
JR
5051 } else if (!(fm->cookie_mask != htonll(0)
5052 || fm->new_cookie == OVS_BE64_MAX)) {
39c94593 5053 /* No match, add a new flow. */
8be00367 5054 error = add_flow_start(ofproto, ofm);
fe59694b 5055 new_rules->collection.n = 1;
dd27be82 5056 } else {
d99f64d7 5057 error = 0;
dd27be82 5058 }
39c94593 5059
dd27be82
JR
5060 return error;
5061}
5062
90bf1e07
BP
5063/* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code on
5064 * failure.
9ed18e46
BP
5065 *
5066 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
5067 * if any. */
90bf1e07 5068static enum ofperr
8be00367 5069modify_flows_start_loose(struct ofproto *ofproto, struct ofproto_flow_mod *ofm)
15aaf599 5070 OVS_REQUIRES(ofproto_mutex)
9ed18e46 5071{
8be00367
JR
5072 struct ofputil_flow_mod *fm = &ofm->fm;
5073 struct rule_collection *old_rules = &ofm->old_rules;
a9b22b7f 5074 struct rule_criteria criteria;
d51c8b71 5075 enum ofperr error;
9ed18e46 5076
44e0c35d 5077 rule_criteria_init(&criteria, fm->table_id, &fm->match, 0, OVS_VERSION_MAX,
30ef36c6 5078 fm->cookie, fm->cookie_mask, OFPP_ANY, OFPG_ANY);
dd51dae2
BP
5079 rule_criteria_require_rw(&criteria,
5080 (fm->flags & OFPUTIL_FF_NO_READONLY) != 0);
39c94593 5081 error = collect_rules_loose(ofproto, &criteria, old_rules);
a9b22b7f
BP
5082 rule_criteria_destroy(&criteria);
5083
a8e547c1 5084 if (!error) {
8be00367 5085 error = modify_flows_start__(ofproto, ofm);
d99f64d7
JR
5086 }
5087
5088 if (error) {
39c94593 5089 rule_collection_destroy(old_rules);
d99f64d7
JR
5090 }
5091 return error;
5092}
5093
1f42be1c 5094static void
8be00367 5095modify_flows_revert(struct ofproto *ofproto, struct ofproto_flow_mod *ofm)
1f42be1c
JR
5096 OVS_REQUIRES(ofproto_mutex)
5097{
8be00367
JR
5098 struct rule_collection *old_rules = &ofm->old_rules;
5099 struct rule_collection *new_rules = &ofm->new_rules;
5100
39c94593 5101 /* Old rules were not changed yet, only need to revert new rules. */
fe59694b
JR
5102 if (rule_collection_n(old_rules) == 0
5103 && rule_collection_n(new_rules) == 1) {
8be00367 5104 add_flow_revert(ofproto, ofm);
fe59694b
JR
5105 } else if (rule_collection_n(old_rules) > 0) {
5106 struct rule *old_rule, *new_rule;
5107 RULE_COLLECTIONS_FOR_EACH (old_rule, new_rule, old_rules, new_rules) {
5108 replace_rule_revert(ofproto, old_rule, new_rule);
39c94593
JR
5109 }
5110 rule_collection_destroy(new_rules);
5111 rule_collection_destroy(old_rules);
1f42be1c 5112 }
1f42be1c
JR
5113}
5114
d99f64d7 5115static void
8be00367 5116modify_flows_finish(struct ofproto *ofproto, struct ofproto_flow_mod *ofm,
0a0d9385 5117 const struct openflow_mod_requester *req)
d99f64d7
JR
5118 OVS_REQUIRES(ofproto_mutex)
5119{
8be00367
JR
5120 struct ofputil_flow_mod *fm = &ofm->fm;
5121 struct rule_collection *old_rules = &ofm->old_rules;
5122 struct rule_collection *new_rules = &ofm->new_rules;
5123
fe59694b
JR
5124 if (rule_collection_n(old_rules) == 0
5125 && rule_collection_n(new_rules) == 1) {
8be00367 5126 add_flow_finish(ofproto, ofm, req);
fe59694b 5127 } else if (rule_collection_n(old_rules) > 0) {
39c94593
JR
5128 struct ovs_list dead_cookies = OVS_LIST_INITIALIZER(&dead_cookies);
5129
fe59694b
JR
5130 ovs_assert(rule_collection_n(new_rules)
5131 == rule_collection_n(old_rules));
39c94593 5132
fe59694b
JR
5133 struct rule *old_rule, *new_rule;
5134 RULE_COLLECTIONS_FOR_EACH (old_rule, new_rule, old_rules, new_rules) {
5135 replace_rule_finish(ofproto, fm, req, old_rule, new_rule,
5136 &dead_cookies);
39c94593
JR
5137 }
5138 learned_cookies_flush(ofproto, &dead_cookies);
cc099268 5139 remove_rules_postponed(old_rules);
39c94593 5140
fe59694b
JR
5141 send_buffered_packet(req, fm->buffer_id,
5142 rule_collection_rules(new_rules)[0]);
39c94593 5143 rule_collection_destroy(new_rules);
d99f64d7 5144 }
d99f64d7
JR
5145}
5146
79eee1eb 5147/* Implements OFPFC_MODIFY_STRICT. Returns 0 on success or an OpenFlow error
baae3d02 5148 * code on failure. */
90bf1e07 5149static enum ofperr
8be00367 5150modify_flow_start_strict(struct ofproto *ofproto, struct ofproto_flow_mod *ofm)
15aaf599 5151 OVS_REQUIRES(ofproto_mutex)
79eee1eb 5152{
8be00367
JR
5153 struct ofputil_flow_mod *fm = &ofm->fm;
5154 struct rule_collection *old_rules = &ofm->old_rules;
a9b22b7f 5155 struct rule_criteria criteria;
d51c8b71 5156 enum ofperr error;
6c1491fb 5157
a9b22b7f 5158 rule_criteria_init(&criteria, fm->table_id, &fm->match, fm->priority,
44e0c35d 5159 OVS_VERSION_MAX, fm->cookie, fm->cookie_mask, OFPP_ANY,
30ef36c6 5160 OFPG_ANY);
dd51dae2
BP
5161 rule_criteria_require_rw(&criteria,
5162 (fm->flags & OFPUTIL_FF_NO_READONLY) != 0);
39c94593 5163 error = collect_rules_strict(ofproto, &criteria, old_rules);
a9b22b7f
BP
5164 rule_criteria_destroy(&criteria);
5165
a8e547c1 5166 if (!error) {
dd27be82 5167 /* collect_rules_strict() can return max 1 rule. */
8be00367 5168 error = modify_flows_start__(ofproto, ofm);
d99f64d7
JR
5169 }
5170
5171 if (error) {
39c94593 5172 rule_collection_destroy(old_rules);
d99f64d7
JR
5173 }
5174 return error;
5175}
9ed18e46
BP
5176\f
5177/* OFPFC_DELETE implementation. */
79eee1eb 5178
254750ce 5179static void
44e0c35d 5180delete_flows_start__(struct ofproto *ofproto, ovs_version_t version,
39c94593
JR
5181 const struct rule_collection *rules)
5182 OVS_REQUIRES(ofproto_mutex)
5183{
fe59694b
JR
5184 struct rule *rule;
5185
5186 RULE_COLLECTION_FOR_EACH (rule, rules) {
d79e3d70
JR
5187 struct oftable *table = &ofproto->tables[rule->table_id];
5188
5189 table->n_flows--;
8be00367 5190 cls_rule_make_invisible_in_version(&rule->cr, version);
9bab38ff
JR
5191
5192 /* Remove rule from ofproto data structures. */
5193 ofproto_rule_remove__(ofproto, rule);
39c94593
JR
5194 }
5195}
5196
25070e04
JR
5197static void
5198delete_flows_revert__(struct ofproto *ofproto,
5199 const struct rule_collection *rules)
5200 OVS_REQUIRES(ofproto_mutex)
5201{
5202 struct rule *rule;
5203
5204 RULE_COLLECTION_FOR_EACH (rule, rules) {
5205 struct oftable *table = &ofproto->tables[rule->table_id];
5206
5207 /* Add rule back to ofproto data structures. */
5208 ofproto_rule_insert__(ofproto, rule);
5209
5210 /* Restore table's rule count. */
5211 table->n_flows++;
5212
5213 /* Restore the original visibility of the rule. */
5214 cls_rule_restore_visibility(&rule->cr);
5215 }
5216}
5217
39c94593
JR
5218static void
5219delete_flows_finish__(struct ofproto *ofproto,
5220 struct rule_collection *rules,
5221 enum ofp_flow_removed_reason reason,
0a0d9385 5222 const struct openflow_mod_requester *req)
15aaf599 5223 OVS_REQUIRES(ofproto_mutex)
064af421 5224{
fe59694b 5225 if (rule_collection_n(rules)) {
55951e15 5226 struct ovs_list dead_cookies = OVS_LIST_INITIALIZER(&dead_cookies);
fe59694b 5227 struct rule *rule;
79eee1eb 5228
fe59694b 5229 RULE_COLLECTION_FOR_EACH (rule, rules) {
f695ebfa
JR
5230 /* This value will be used to send the flow removed message right
5231 * before the rule is actually destroyed. */
5232 rule->removed_reason = reason;
5233
9ca4a86f 5234 ofmonitor_report(ofproto->connmgr, rule, NXFME_DELETED, reason,
c84d8691
JR
5235 req ? req->ofconn : NULL,
5236 req ? req->request->xid : 0, NULL);
6c6eedc5
SJ
5237
5238 /* Send Vacancy Event for OF1.4+. */
5239 send_table_status(ofproto, rule->table_id);
5240
802f84ff
JR
5241 learned_cookies_dec(ofproto, rule_get_actions(rule),
5242 &dead_cookies);
9ca4a86f 5243 }
cc099268 5244 remove_rules_postponed(rules);
39c94593 5245
35f48b8b 5246 learned_cookies_flush(ofproto, &dead_cookies);
39c94593
JR
5247 }
5248}
5249
5250/* Deletes the rules listed in 'rules'.
5251 * The deleted rules will become invisible to the lookups in the next version.
5252 * Destroys 'rules'. */
5253static void
5254delete_flows__(struct rule_collection *rules,
5255 enum ofp_flow_removed_reason reason,
0a0d9385 5256 const struct openflow_mod_requester *req)
39c94593
JR
5257 OVS_REQUIRES(ofproto_mutex)
5258{
fe59694b
JR
5259 if (rule_collection_n(rules)) {
5260 struct ofproto *ofproto = rule_collection_rules(rules)[0]->ofproto;
39c94593 5261
8be00367 5262 delete_flows_start__(ofproto, ofproto->tables_version + 1, rules);
39c94593
JR
5263 ofproto_bump_tables_version(ofproto);
5264 delete_flows_finish__(ofproto, rules, reason, req);
9ca4a86f
BP
5265 ofmonitor_flush(ofproto->connmgr);
5266 }
79eee1eb 5267}
79eee1eb
BP
5268
5269/* Implements OFPFC_DELETE. */
90bf1e07 5270static enum ofperr
8be00367 5271delete_flows_start_loose(struct ofproto *ofproto, struct ofproto_flow_mod *ofm)
15aaf599 5272 OVS_REQUIRES(ofproto_mutex)
79eee1eb 5273{
8be00367
JR
5274 const struct ofputil_flow_mod *fm = &ofm->fm;
5275 struct rule_collection *rules = &ofm->old_rules;
a9b22b7f 5276 struct rule_criteria criteria;
90bf1e07 5277 enum ofperr error;
064af421 5278
44e0c35d 5279 rule_criteria_init(&criteria, fm->table_id, &fm->match, 0, OVS_VERSION_MAX,
39c94593
JR
5280 fm->cookie, fm->cookie_mask, fm->out_port,
5281 fm->out_group);
dd51dae2
BP
5282 rule_criteria_require_rw(&criteria,
5283 (fm->flags & OFPUTIL_FF_NO_READONLY) != 0);
ce59413f 5284 error = collect_rules_loose(ofproto, &criteria, rules);
a9b22b7f
BP
5285 rule_criteria_destroy(&criteria);
5286
802f84ff 5287 if (!error) {
8be00367 5288 delete_flows_start__(ofproto, ofm->version, rules);
a8e547c1 5289 }
a8e547c1
BP
5290
5291 return error;
064af421
BP
5292}
5293
ce59413f 5294static void
8be00367 5295delete_flows_revert(struct ofproto *ofproto, struct ofproto_flow_mod *ofm)
ce59413f
JR
5296 OVS_REQUIRES(ofproto_mutex)
5297{
25070e04
JR
5298 delete_flows_revert__(ofproto, &ofm->old_rules);
5299 rule_collection_destroy(&ofm->old_rules);
ce59413f
JR
5300}
5301
1f42be1c 5302static void
39c94593 5303delete_flows_finish(struct ofproto *ofproto,
8be00367 5304 struct ofproto_flow_mod *ofm,
0a0d9385 5305 const struct openflow_mod_requester *req)
15aaf599 5306 OVS_REQUIRES(ofproto_mutex)
79eee1eb 5307{
5aacc3e2 5308 delete_flows_finish__(ofproto, &ofm->old_rules, OFPRR_DELETE, req);
ce59413f
JR
5309}
5310
5311/* Implements OFPFC_DELETE_STRICT. */
5312static enum ofperr
4fcb2083 5313delete_flow_start_strict(struct ofproto *ofproto,
8be00367 5314 struct ofproto_flow_mod *ofm)
ce59413f
JR
5315 OVS_REQUIRES(ofproto_mutex)
5316{
8be00367
JR
5317 const struct ofputil_flow_mod *fm = &ofm->fm;
5318 struct rule_collection *rules = &ofm->old_rules;
ce59413f
JR
5319 struct rule_criteria criteria;
5320 enum ofperr error;
5321
a9b22b7f 5322 rule_criteria_init(&criteria, fm->table_id, &fm->match, fm->priority,
44e0c35d 5323 OVS_VERSION_MAX, fm->cookie, fm->cookie_mask,
a9b22b7f 5324 fm->out_port, fm->out_group);
dd51dae2
BP
5325 rule_criteria_require_rw(&criteria,
5326 (fm->flags & OFPUTIL_FF_NO_READONLY) != 0);
ce59413f 5327 error = collect_rules_strict(ofproto, &criteria, rules);
a9b22b7f
BP
5328 rule_criteria_destroy(&criteria);
5329
802f84ff 5330 if (!error) {
8be00367 5331 delete_flows_start__(ofproto, ofm->version, rules);
ce59413f
JR
5332 }
5333
5334 return error;
5335}
5336
f695ebfa 5337/* This may only be called by rule_destroy_cb()! */
abe529af 5338static void
f695ebfa
JR
5339ofproto_rule_send_removed(struct rule *rule)
5340 OVS_EXCLUDED(ofproto_mutex)
abe529af
BP
5341{
5342 struct ofputil_flow_removed fr;
dc437090 5343 long long int used;
abe529af 5344
5cb7a798 5345 minimatch_expand(&rule->cr.match, &fr.match);
81a76618 5346 fr.priority = rule->cr.priority;
f695ebfa
JR
5347
5348 ovs_mutex_lock(&ofproto_mutex);
abe529af 5349 fr.cookie = rule->flow_cookie;
f695ebfa 5350 fr.reason = rule->removed_reason;
95216219 5351 fr.table_id = rule->table_id;
65e0be10
BP
5352 calc_duration(rule->created, time_msec(),
5353 &fr.duration_sec, &fr.duration_nsec);
d10976b7 5354 ovs_mutex_lock(&rule->mutex);
abe529af 5355 fr.idle_timeout = rule->idle_timeout;
fa2bad0f 5356 fr.hard_timeout = rule->hard_timeout;
d10976b7 5357 ovs_mutex_unlock(&rule->mutex);
abe529af 5358 rule->ofproto->ofproto_class->rule_get_stats(rule, &fr.packet_count,
dc437090 5359 &fr.byte_count, &used);
abe529af 5360 connmgr_send_flow_removed(rule->ofproto->connmgr, &fr);
f695ebfa 5361 ovs_mutex_unlock(&ofproto_mutex);
abe529af
BP
5362}
5363
5364/* Sends an OpenFlow "flow removed" message with the given 'reason' (either
5365 * OFPRR_HARD_TIMEOUT or OFPRR_IDLE_TIMEOUT), and then removes 'rule' from its
5366 * ofproto.
5367 *
5368 * ofproto implementation ->run() functions should use this function to expire
5369 * OpenFlow flows. */
5370void
5371ofproto_rule_expire(struct rule *rule, uint8_t reason)
15aaf599 5372 OVS_REQUIRES(ofproto_mutex)
abe529af 5373{
802f84ff
JR
5374 struct rule_collection rules;
5375
fe59694b
JR
5376 rule_collection_init(&rules);
5377 rule_collection_add(&rules, rule);
802f84ff 5378 delete_flows__(&rules, reason, NULL);
79eee1eb 5379}
994c9973
BP
5380
5381/* Reduces '*timeout' to no more than 'max'. A value of zero in either case
5382 * means "infinite". */
5383static void
5384reduce_timeout(uint16_t max, uint16_t *timeout)
5385{
5386 if (max && (!*timeout || *timeout > max)) {
5387 *timeout = max;
5388 }
5389}
5390
5391/* If 'idle_timeout' is nonzero, and 'rule' has no idle timeout or an idle
5392 * timeout greater than 'idle_timeout', lowers 'rule''s idle timeout to
5393 * 'idle_timeout' seconds. Similarly for 'hard_timeout'.
5394 *
5395 * Suitable for implementing OFPACT_FIN_TIMEOUT. */
5396void
5397ofproto_rule_reduce_timeouts(struct rule *rule,
5398 uint16_t idle_timeout, uint16_t hard_timeout)
d10976b7 5399 OVS_EXCLUDED(ofproto_mutex, rule->mutex)
994c9973
BP
5400{
5401 if (!idle_timeout && !hard_timeout) {
5402 return;
5403 }
5404
abe7b10f 5405 ovs_mutex_lock(&ofproto_mutex);
417e7e66
BW
5406 if (ovs_list_is_empty(&rule->expirable)) {
5407 ovs_list_insert(&rule->ofproto->expirable, &rule->expirable);
994c9973 5408 }
abe7b10f 5409 ovs_mutex_unlock(&ofproto_mutex);
994c9973 5410
d10976b7 5411 ovs_mutex_lock(&rule->mutex);
994c9973
BP
5412 reduce_timeout(idle_timeout, &rule->idle_timeout);
5413 reduce_timeout(hard_timeout, &rule->hard_timeout);
d10976b7 5414 ovs_mutex_unlock(&rule->mutex);
994c9973 5415}
79eee1eb 5416\f
90bf1e07 5417static enum ofperr
2e4f5fcf 5418handle_flow_mod(struct ofconn *ofconn, const struct ofp_header *oh)
15aaf599 5419 OVS_EXCLUDED(ofproto_mutex)
064af421 5420{
a5b8d268 5421 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
8be00367 5422 struct ofproto_flow_mod ofm;
f25d0cf3
BP
5423 uint64_t ofpacts_stub[1024 / 8];
5424 struct ofpbuf ofpacts;
90bf1e07 5425 enum ofperr error;
064af421 5426
76589937 5427 error = reject_slave_controller(ofconn);
9deba63b 5428 if (error) {
b0d38b2f 5429 return error;
9deba63b 5430 }
3052b0c5 5431
f25d0cf3 5432 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
8be00367 5433 error = ofputil_decode_flow_mod(&ofm.fm, oh, ofconn_get_protocol(ofconn),
7e9f8266
BP
5434 &ofpacts,
5435 u16_to_ofp(ofproto->max_ports),
5436 ofproto->n_tables);
548de4dd 5437 if (!error) {
0a0d9385 5438 struct openflow_mod_requester req = { ofconn, oh };
8be00367 5439 error = handle_flow_mod__(ofproto, &ofm, &req);
49bdc010 5440 }
2e310801 5441
f25d0cf3 5442 ofpbuf_uninit(&ofpacts);
f25d0cf3 5443 return error;
75a75043
BP
5444}
5445
90bf1e07 5446static enum ofperr
8be00367 5447handle_flow_mod__(struct ofproto *ofproto, struct ofproto_flow_mod *ofm,
0a0d9385 5448 const struct openflow_mod_requester *req)
15aaf599 5449 OVS_EXCLUDED(ofproto_mutex)
75a75043 5450{
35412852 5451 enum ofperr error;
75a75043 5452
15aaf599 5453 ovs_mutex_lock(&ofproto_mutex);
8be00367
JR
5454 ofm->version = ofproto->tables_version + 1;
5455 error = ofproto_flow_mod_start(ofproto, ofm);
1f42be1c 5456 if (!error) {
39c94593 5457 ofproto_bump_tables_version(ofproto);
8be00367 5458 ofproto_flow_mod_finish(ofproto, ofm, req);
3052b0c5 5459 }
baae3d02 5460 ofmonitor_flush(ofproto->connmgr);
15aaf599 5461 ovs_mutex_unlock(&ofproto_mutex);
35412852
BP
5462
5463 run_rule_executes(ofproto);
5464 return error;
3052b0c5
BP
5465}
5466
90bf1e07 5467static enum ofperr
d1e2cf21 5468handle_role_request(struct ofconn *ofconn, const struct ofp_header *oh)
9deba63b 5469{
f4f1ea7e
BP
5470 struct ofputil_role_request request;
5471 struct ofputil_role_request reply;
9deba63b 5472 struct ofpbuf *buf;
6ea4776b
JR
5473 enum ofperr error;
5474
f4f1ea7e 5475 error = ofputil_decode_role_message(oh, &request);
6ea4776b
JR
5476 if (error) {
5477 return error;
5478 }
9deba63b 5479
f4f1ea7e 5480 if (request.role != OFPCR12_ROLE_NOCHANGE) {
d26eda9f
BP
5481 if (request.role != OFPCR12_ROLE_EQUAL
5482 && request.have_generation_id
f4f1ea7e
BP
5483 && !ofconn_set_master_election_id(ofconn, request.generation_id)) {
5484 return OFPERR_OFPRRFC_STALE;
6ea4776b 5485 }
6ea4776b 5486
f4f1ea7e
BP
5487 ofconn_set_role(ofconn, request.role);
5488 }
9deba63b 5489
f4f1ea7e 5490 reply.role = ofconn_get_role(ofconn);
147cc9d3
BP
5491 reply.have_generation_id = ofconn_get_master_election_id(
5492 ofconn, &reply.generation_id);
f4f1ea7e 5493 buf = ofputil_encode_role_reply(oh, &reply);
b0421aa2 5494 ofconn_send_reply(ofconn, buf);
9deba63b
BP
5495
5496 return 0;
5497}
5498
90bf1e07 5499static enum ofperr
6c1491fb
BP
5500handle_nxt_flow_mod_table_id(struct ofconn *ofconn,
5501 const struct ofp_header *oh)
5502{
982697a4 5503 const struct nx_flow_mod_table_id *msg = ofpmsg_body(oh);
27527aa0
BP
5504 enum ofputil_protocol cur, next;
5505
5506 cur = ofconn_get_protocol(ofconn);
5507 next = ofputil_protocol_set_tid(cur, msg->set != 0);
5508 ofconn_set_protocol(ofconn, next);
6c1491fb 5509
6c1491fb
BP
5510 return 0;
5511}
5512
90bf1e07 5513static enum ofperr
d1e2cf21 5514handle_nxt_set_flow_format(struct ofconn *ofconn, const struct ofp_header *oh)
09246b99 5515{
982697a4 5516 const struct nx_set_flow_format *msg = ofpmsg_body(oh);
27527aa0
BP
5517 enum ofputil_protocol cur, next;
5518 enum ofputil_protocol next_base;
09246b99 5519
27527aa0
BP
5520 next_base = ofputil_nx_flow_format_to_protocol(ntohl(msg->format));
5521 if (!next_base) {
90bf1e07 5522 return OFPERR_OFPBRC_EPERM;
09246b99 5523 }
7ee20df1 5524
27527aa0
BP
5525 cur = ofconn_get_protocol(ofconn);
5526 next = ofputil_protocol_set_base(cur, next_base);
27527aa0 5527 ofconn_set_protocol(ofconn, next);
b20f4073 5528
7ee20df1 5529 return 0;
09246b99
BP
5530}
5531
90bf1e07 5532static enum ofperr
54834960
EJ
5533handle_nxt_set_packet_in_format(struct ofconn *ofconn,
5534 const struct ofp_header *oh)
5535{
982697a4 5536 const struct nx_set_packet_in_format *msg = ofpmsg_body(oh);
54834960
EJ
5537 uint32_t format;
5538
54834960 5539 format = ntohl(msg->format);
6409e008 5540 if (!ofputil_packet_in_format_is_valid(format)) {
90bf1e07 5541 return OFPERR_OFPBRC_EPERM;
54834960
EJ
5542 }
5543
54834960
EJ
5544 ofconn_set_packet_in_format(ofconn, format);
5545 return 0;
5546}
5547
80d5aefd
BP
5548static enum ofperr
5549handle_nxt_set_async_config(struct ofconn *ofconn, const struct ofp_header *oh)
5550{
5876b4db
BP
5551 struct ofputil_async_cfg basis = ofconn_get_async_config(ofconn);
5552 struct ofputil_async_cfg ac;
d18cc1ee 5553 enum ofperr error;
80d5aefd 5554
5876b4db 5555 error = ofputil_decode_set_async_config(oh, false, &basis, &ac);
d18cc1ee
AA
5556 if (error) {
5557 return error;
5558 }
80d5aefd 5559
a930d4c5 5560 ofconn_set_async_config(ofconn, &ac);
4550b647
MM
5561 if (ofconn_get_type(ofconn) == OFCONN_SERVICE &&
5562 !ofconn_get_miss_send_len(ofconn)) {
5563 ofconn_set_miss_send_len(ofconn, OFP_DEFAULT_MISS_SEND_LEN);
5564 }
80d5aefd
BP
5565
5566 return 0;
5567}
5568
c423b3b3
AC
5569static enum ofperr
5570handle_nxt_get_async_request(struct ofconn *ofconn, const struct ofp_header *oh)
5571{
a930d4c5 5572 struct ofputil_async_cfg ac = ofconn_get_async_config(ofconn);
af7bc161 5573 ofconn_send_reply(ofconn, ofputil_encode_get_async_reply(oh, &ac));
c423b3b3
AC
5574
5575 return 0;
5576}
5577
a7349929
BP
5578static enum ofperr
5579handle_nxt_set_controller_id(struct ofconn *ofconn,
5580 const struct ofp_header *oh)
5581{
982697a4 5582 const struct nx_controller_id *nci = ofpmsg_body(oh);
a7349929 5583
a7349929
BP
5584 if (!is_all_zeros(nci->zero, sizeof nci->zero)) {
5585 return OFPERR_NXBRC_MUST_BE_ZERO;
5586 }
5587
5588 ofconn_set_controller_id(ofconn, ntohs(nci->controller_id));
5589 return 0;
5590}
5591
90bf1e07 5592static enum ofperr
d1e2cf21 5593handle_barrier_request(struct ofconn *ofconn, const struct ofp_header *oh)
246e61ea 5594{
246e61ea
JP
5595 struct ofpbuf *buf;
5596
982697a4
BP
5597 buf = ofpraw_alloc_reply((oh->version == OFP10_VERSION
5598 ? OFPRAW_OFPT10_BARRIER_REPLY
5599 : OFPRAW_OFPT11_BARRIER_REPLY), oh, 0);
b0421aa2 5600 ofconn_send_reply(ofconn, buf);
246e61ea
JP
5601 return 0;
5602}
5603
2b07c8b1
BP
5604static void
5605ofproto_compose_flow_refresh_update(const struct rule *rule,
5606 enum nx_flow_monitor_flags flags,
ca6ba700 5607 struct ovs_list *msgs)
15aaf599 5608 OVS_REQUIRES(ofproto_mutex)
2b07c8b1 5609{
6f00e29b 5610 const struct rule_actions *actions;
2b07c8b1 5611 struct ofputil_flow_update fu;
5cb7a798 5612 struct match match;
2b07c8b1 5613
2b07c8b1
BP
5614 fu.event = (flags & (NXFMF_INITIAL | NXFMF_ADD)
5615 ? NXFME_ADDED : NXFME_MODIFIED);
5616 fu.reason = 0;
d10976b7 5617 ovs_mutex_lock(&rule->mutex);
2b07c8b1
BP
5618 fu.idle_timeout = rule->idle_timeout;
5619 fu.hard_timeout = rule->hard_timeout;
d10976b7 5620 ovs_mutex_unlock(&rule->mutex);
2b07c8b1
BP
5621 fu.table_id = rule->table_id;
5622 fu.cookie = rule->flow_cookie;
5cb7a798
BP
5623 minimatch_expand(&rule->cr.match, &match);
5624 fu.match = &match;
6b140a4e 5625 fu.priority = rule->cr.priority;
6f00e29b 5626
b20f4073 5627 actions = flags & NXFMF_ACTIONS ? rule_get_actions(rule) : NULL;
6f00e29b
BP
5628 fu.ofpacts = actions ? actions->ofpacts : NULL;
5629 fu.ofpacts_len = actions ? actions->ofpacts_len : 0;
2b07c8b1 5630
417e7e66 5631 if (ovs_list_is_empty(msgs)) {
2b07c8b1
BP
5632 ofputil_start_flow_update(msgs);
5633 }
5634 ofputil_append_flow_update(&fu, msgs);
5635}
5636
5637void
a8e547c1 5638ofmonitor_compose_refresh_updates(struct rule_collection *rules,
ca6ba700 5639 struct ovs_list *msgs)
15aaf599 5640 OVS_REQUIRES(ofproto_mutex)
2b07c8b1 5641{
fe59694b 5642 struct rule *rule;
2b07c8b1 5643
fe59694b 5644 RULE_COLLECTION_FOR_EACH (rule, rules) {
2b07c8b1
BP
5645 enum nx_flow_monitor_flags flags = rule->monitor_flags;
5646 rule->monitor_flags = 0;
5647
5648 ofproto_compose_flow_refresh_update(rule, flags, msgs);
5649 }
5650}
5651
5652static void
5653ofproto_collect_ofmonitor_refresh_rule(const struct ofmonitor *m,
5654 struct rule *rule, uint64_t seqno,
a8e547c1 5655 struct rule_collection *rules)
15aaf599 5656 OVS_REQUIRES(ofproto_mutex)
2b07c8b1
BP
5657{
5658 enum nx_flow_monitor_flags update;
5659
5e8b38b0 5660 if (rule_is_hidden(rule)) {
2b07c8b1
BP
5661 return;
5662 }
5663
b20f4073 5664 if (!ofproto_rule_has_out_port(rule, m->out_port)) {
2b07c8b1
BP
5665 return;
5666 }
5667
5668 if (seqno) {
5669 if (rule->add_seqno > seqno) {
5670 update = NXFMF_ADD | NXFMF_MODIFY;
5671 } else if (rule->modify_seqno > seqno) {
5672 update = NXFMF_MODIFY;
5673 } else {
5674 return;
5675 }
5676
5677 if (!(m->flags & update)) {
5678 return;
5679 }
5680 } else {
5681 update = NXFMF_INITIAL;
5682 }
5683
5684 if (!rule->monitor_flags) {
a8e547c1 5685 rule_collection_add(rules, rule);
2b07c8b1
BP
5686 }
5687 rule->monitor_flags |= update | (m->flags & NXFMF_ACTIONS);
5688}
5689
5690static void
5691ofproto_collect_ofmonitor_refresh_rules(const struct ofmonitor *m,
5692 uint64_t seqno,
a8e547c1 5693 struct rule_collection *rules)
15aaf599 5694 OVS_REQUIRES(ofproto_mutex)
2b07c8b1
BP
5695{
5696 const struct ofproto *ofproto = ofconn_get_ofproto(m->ofconn);
2b07c8b1 5697 const struct oftable *table;
81a76618 5698 struct cls_rule target;
2b07c8b1 5699
bd53aa17 5700 cls_rule_init_from_minimatch(&target, &m->match, 0);
2b07c8b1 5701 FOR_EACH_MATCHING_TABLE (table, m->table_id, ofproto) {
2b07c8b1
BP
5702 struct rule *rule;
5703
44e0c35d 5704 CLS_FOR_EACH_TARGET (rule, cr, &table->cls, &target, OVS_VERSION_MAX) {
2b07c8b1
BP
5705 ofproto_collect_ofmonitor_refresh_rule(m, rule, seqno, rules);
5706 }
5707 }
48d28ac1 5708 cls_rule_destroy(&target);
2b07c8b1
BP
5709}
5710
5711static void
5712ofproto_collect_ofmonitor_initial_rules(struct ofmonitor *m,
a8e547c1 5713 struct rule_collection *rules)
15aaf599 5714 OVS_REQUIRES(ofproto_mutex)
2b07c8b1
BP
5715{
5716 if (m->flags & NXFMF_INITIAL) {
5717 ofproto_collect_ofmonitor_refresh_rules(m, 0, rules);
5718 }
5719}
5720
5721void
5722ofmonitor_collect_resume_rules(struct ofmonitor *m,
a8e547c1 5723 uint64_t seqno, struct rule_collection *rules)
15aaf599 5724 OVS_REQUIRES(ofproto_mutex)
2b07c8b1
BP
5725{
5726 ofproto_collect_ofmonitor_refresh_rules(m, seqno, rules);
5727}
5728
7b900689
SH
5729static enum ofperr
5730flow_monitor_delete(struct ofconn *ofconn, uint32_t id)
5731 OVS_REQUIRES(ofproto_mutex)
5732{
5733 struct ofmonitor *m;
5734 enum ofperr error;
5735
5736 m = ofmonitor_lookup(ofconn, id);
5737 if (m) {
5738 ofmonitor_destroy(m);
5739 error = 0;
5740 } else {
5741 error = OFPERR_OFPMOFC_UNKNOWN_MONITOR;
5742 }
5743
5744 return error;
5745}
5746
2b07c8b1 5747static enum ofperr
982697a4 5748handle_flow_monitor_request(struct ofconn *ofconn, const struct ofp_header *oh)
15aaf599 5749 OVS_EXCLUDED(ofproto_mutex)
2b07c8b1
BP
5750{
5751 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2b07c8b1 5752
0a2869d5
BP
5753 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
5754
5755 struct ofmonitor **monitors = NULL;
5756 size_t allocated_monitors = 0;
5757 size_t n_monitors = 0;
5758
5759 enum ofperr error;
15aaf599
BP
5760
5761 ovs_mutex_lock(&ofproto_mutex);
2b07c8b1
BP
5762 for (;;) {
5763 struct ofputil_flow_monitor_request request;
5764 struct ofmonitor *m;
5765 int retval;
5766
5767 retval = ofputil_decode_flow_monitor_request(&request, &b);
5768 if (retval == EOF) {
5769 break;
5770 } else if (retval) {
5771 error = retval;
5772 goto error;
5773 }
5774
5775 if (request.table_id != 0xff
5776 && request.table_id >= ofproto->n_tables) {
5777 error = OFPERR_OFPBRC_BAD_TABLE_ID;
5778 goto error;
5779 }
5780
5781 error = ofmonitor_create(&request, ofconn, &m);
5782 if (error) {
5783 goto error;
5784 }
5785
5786 if (n_monitors >= allocated_monitors) {
5787 monitors = x2nrealloc(monitors, &allocated_monitors,
5788 sizeof *monitors);
5789 }
5790 monitors[n_monitors++] = m;
5791 }
5792
0a2869d5 5793 struct rule_collection rules;
a8e547c1 5794 rule_collection_init(&rules);
0a2869d5 5795 for (size_t i = 0; i < n_monitors; i++) {
2b07c8b1
BP
5796 ofproto_collect_ofmonitor_initial_rules(monitors[i], &rules);
5797 }
5798
0a2869d5 5799 struct ovs_list replies;
982697a4 5800 ofpmp_init(&replies, oh);
2b07c8b1 5801 ofmonitor_compose_refresh_updates(&rules, &replies);
15aaf599
BP
5802 ovs_mutex_unlock(&ofproto_mutex);
5803
a8e547c1
BP
5804 rule_collection_destroy(&rules);
5805
2b07c8b1 5806 ofconn_send_replies(ofconn, &replies);
2b07c8b1
BP
5807 free(monitors);
5808
5809 return 0;
5810
5811error:
0a2869d5 5812 for (size_t i = 0; i < n_monitors; i++) {
2b07c8b1
BP
5813 ofmonitor_destroy(monitors[i]);
5814 }
5815 free(monitors);
4cd1bc9d
BP
5816 ovs_mutex_unlock(&ofproto_mutex);
5817
2b07c8b1
BP
5818 return error;
5819}
5820
5821static enum ofperr
5822handle_flow_monitor_cancel(struct ofconn *ofconn, const struct ofp_header *oh)
15aaf599 5823 OVS_EXCLUDED(ofproto_mutex)
2b07c8b1 5824{
15aaf599 5825 enum ofperr error;
2b07c8b1
BP
5826 uint32_t id;
5827
5828 id = ofputil_decode_flow_monitor_cancel(oh);
15aaf599
BP
5829
5830 ovs_mutex_lock(&ofproto_mutex);
7b900689 5831 error = flow_monitor_delete(ofconn, id);
15aaf599 5832 ovs_mutex_unlock(&ofproto_mutex);
2b07c8b1 5833
15aaf599 5834 return error;
2b07c8b1
BP
5835}
5836
9cae45dc
JR
5837/* Meters implementation.
5838 *
5839 * Meter table entry, indexed by the OpenFlow meter_id.
9cae45dc
JR
5840 * 'created' is used to compute the duration for meter stats.
5841 * 'list rules' is needed so that we can delete the dependent rules when the
5842 * meter table entry is deleted.
5843 * 'provider_meter_id' is for the provider's private use.
5844 */
5845struct meter {
5846 long long int created; /* Time created. */
ca6ba700 5847 struct ovs_list rules; /* List of "struct rule_dpif"s. */
9cae45dc
JR
5848 ofproto_meter_id provider_meter_id;
5849 uint16_t flags; /* Meter flags. */
5850 uint16_t n_bands; /* Number of meter bands. */
5851 struct ofputil_meter_band *bands;
5852};
5853
5854/*
5855 * This is used in instruction validation at flow set-up time,
5856 * as flows may not use non-existing meters.
9cae45dc
JR
5857 * Return value of UINT32_MAX signifies an invalid meter.
5858 */
65efd2ab
JR
5859static uint32_t
5860get_provider_meter_id(const struct ofproto *ofproto, uint32_t of_meter_id)
9cae45dc
JR
5861{
5862 if (of_meter_id && of_meter_id <= ofproto->meter_features.max_meters) {
5863 const struct meter *meter = ofproto->meters[of_meter_id];
5864 if (meter) {
5865 return meter->provider_meter_id.uint32;
5866 }
5867 }
5868 return UINT32_MAX;
5869}
5870
062fea06
BP
5871/* Finds the meter invoked by 'rule''s actions and adds 'rule' to the meter's
5872 * list of rules. */
5873static void
5874meter_insert_rule(struct rule *rule)
5875{
5876 const struct rule_actions *a = rule_get_actions(rule);
5877 uint32_t meter_id = ofpacts_get_meter(a->ofpacts, a->ofpacts_len);
5878 struct meter *meter = rule->ofproto->meters[meter_id];
5879
417e7e66 5880 ovs_list_insert(&meter->rules, &rule->meter_list_node);
062fea06
BP
5881}
5882
9cae45dc
JR
5883static void
5884meter_update(struct meter *meter, const struct ofputil_meter_config *config)
5885{
5886 free(meter->bands);
5887
5888 meter->flags = config->flags;
5889 meter->n_bands = config->n_bands;
5890 meter->bands = xmemdup(config->bands,
5891 config->n_bands * sizeof *meter->bands);
5892}
5893
5894static struct meter *
5895meter_create(const struct ofputil_meter_config *config,
5896 ofproto_meter_id provider_meter_id)
5897{
5898 struct meter *meter;
5899
5900 meter = xzalloc(sizeof *meter);
5901 meter->provider_meter_id = provider_meter_id;
5902 meter->created = time_msec();
417e7e66 5903 ovs_list_init(&meter->rules);
9cae45dc
JR
5904
5905 meter_update(meter, config);
5906
5907 return meter;
5908}
5909
6b3f7f02
JR
5910static void
5911meter_delete(struct ofproto *ofproto, uint32_t first, uint32_t last)
15aaf599 5912 OVS_REQUIRES(ofproto_mutex)
6b3f7f02
JR
5913{
5914 uint32_t mid;
5915 for (mid = first; mid <= last; ++mid) {
5916 struct meter *meter = ofproto->meters[mid];
5917 if (meter) {
5918 ofproto->meters[mid] = NULL;
5919 ofproto->ofproto_class->meter_del(ofproto,
5920 meter->provider_meter_id);
5921 free(meter->bands);
5922 free(meter);
5923 }
5924 }
5925}
5926
9cae45dc
JR
5927static enum ofperr
5928handle_add_meter(struct ofproto *ofproto, struct ofputil_meter_mod *mm)
5929{
5930 ofproto_meter_id provider_meter_id = { UINT32_MAX };
5931 struct meter **meterp = &ofproto->meters[mm->meter.meter_id];
5932 enum ofperr error;
5933
5934 if (*meterp) {
5935 return OFPERR_OFPMMFC_METER_EXISTS;
5936 }
5937
5938 error = ofproto->ofproto_class->meter_set(ofproto, &provider_meter_id,
5939 &mm->meter);
5940 if (!error) {
5941 ovs_assert(provider_meter_id.uint32 != UINT32_MAX);
5942 *meterp = meter_create(&mm->meter, provider_meter_id);
5943 }
f0f8c6c2 5944 return error;
9cae45dc
JR
5945}
5946
5947static enum ofperr
5948handle_modify_meter(struct ofproto *ofproto, struct ofputil_meter_mod *mm)
5949{
5950 struct meter *meter = ofproto->meters[mm->meter.meter_id];
5951 enum ofperr error;
e555eb7c 5952 uint32_t provider_meter_id;
9cae45dc
JR
5953
5954 if (!meter) {
5955 return OFPERR_OFPMMFC_UNKNOWN_METER;
5956 }
5957
e555eb7c 5958 provider_meter_id = meter->provider_meter_id.uint32;
9cae45dc
JR
5959 error = ofproto->ofproto_class->meter_set(ofproto,
5960 &meter->provider_meter_id,
5961 &mm->meter);
e555eb7c 5962 ovs_assert(meter->provider_meter_id.uint32 == provider_meter_id);
9cae45dc
JR
5963 if (!error) {
5964 meter_update(meter, &mm->meter);
5965 }
5966 return error;
5967}
5968
5969static enum ofperr
baae3d02 5970handle_delete_meter(struct ofconn *ofconn, struct ofputil_meter_mod *mm)
15aaf599 5971 OVS_EXCLUDED(ofproto_mutex)
9cae45dc
JR
5972{
5973 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5974 uint32_t meter_id = mm->meter.meter_id;
a8e547c1
BP
5975 struct rule_collection rules;
5976 enum ofperr error = 0;
9cae45dc 5977 uint32_t first, last;
9cae45dc
JR
5978
5979 if (meter_id == OFPM13_ALL) {
5980 first = 1;
5981 last = ofproto->meter_features.max_meters;
5982 } else {
5983 if (!meter_id || meter_id > ofproto->meter_features.max_meters) {
5984 return 0;
5985 }
5986 first = last = meter_id;
5987 }
5988
5989 /* First delete the rules that use this meter. If any of those rules are
5990 * currently being modified, postpone the whole operation until later. */
a8e547c1 5991 rule_collection_init(&rules);
15aaf599 5992 ovs_mutex_lock(&ofproto_mutex);
9cae45dc
JR
5993 for (meter_id = first; meter_id <= last; ++meter_id) {
5994 struct meter *meter = ofproto->meters[meter_id];
417e7e66 5995 if (meter && !ovs_list_is_empty(&meter->rules)) {
9cae45dc
JR
5996 struct rule *rule;
5997
5998 LIST_FOR_EACH (rule, meter_list_node, &meter->rules) {
a8e547c1 5999 rule_collection_add(&rules, rule);
9cae45dc
JR
6000 }
6001 }
6002 }
802f84ff 6003 delete_flows__(&rules, OFPRR_METER_DELETE, NULL);
9cae45dc
JR
6004
6005 /* Delete the meters. */
6b3f7f02 6006 meter_delete(ofproto, first, last);
9cae45dc 6007
15aaf599 6008 ovs_mutex_unlock(&ofproto_mutex);
a8e547c1
BP
6009
6010 return error;
9cae45dc
JR
6011}
6012
6013static enum ofperr
6014handle_meter_mod(struct ofconn *ofconn, const struct ofp_header *oh)
6015{
6016 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
6017 struct ofputil_meter_mod mm;
6018 uint64_t bands_stub[256 / 8];
6019 struct ofpbuf bands;
6020 uint32_t meter_id;
6021 enum ofperr error;
6022
6023 error = reject_slave_controller(ofconn);
6024 if (error) {
6025 return error;
6026 }
6027
6028 ofpbuf_use_stub(&bands, bands_stub, sizeof bands_stub);
6029
6030 error = ofputil_decode_meter_mod(oh, &mm, &bands);
6031 if (error) {
6032 goto exit_free_bands;
6033 }
6034
6035 meter_id = mm.meter.meter_id;
6036
6037 if (mm.command != OFPMC13_DELETE) {
6038 /* Fails also when meters are not implemented by the provider. */
b31e8700 6039 if (meter_id == 0 || meter_id > OFPM13_MAX) {
9cae45dc
JR
6040 error = OFPERR_OFPMMFC_INVALID_METER;
6041 goto exit_free_bands;
b31e8700
JR
6042 } else if (meter_id > ofproto->meter_features.max_meters) {
6043 error = OFPERR_OFPMMFC_OUT_OF_METERS;
6044 goto exit_free_bands;
9cae45dc
JR
6045 }
6046 if (mm.meter.n_bands > ofproto->meter_features.max_bands) {
6047 error = OFPERR_OFPMMFC_OUT_OF_BANDS;
6048 goto exit_free_bands;
6049 }
6050 }
6051
6052 switch (mm.command) {
6053 case OFPMC13_ADD:
6054 error = handle_add_meter(ofproto, &mm);
6055 break;
6056
6057 case OFPMC13_MODIFY:
6058 error = handle_modify_meter(ofproto, &mm);
6059 break;
6060
6061 case OFPMC13_DELETE:
baae3d02 6062 error = handle_delete_meter(ofconn, &mm);
9cae45dc
JR
6063 break;
6064
6065 default:
6066 error = OFPERR_OFPMMFC_BAD_COMMAND;
6067 break;
6068 }
6069
3c35db62
NR
6070 if (!error) {
6071 struct ofputil_requestforward rf;
6072 rf.xid = oh->xid;
6073 rf.reason = OFPRFR_METER_MOD;
6074 rf.meter_mod = &mm;
6075 connmgr_send_requestforward(ofproto->connmgr, ofconn, &rf);
6076 }
6077
9cae45dc
JR
6078exit_free_bands:
6079 ofpbuf_uninit(&bands);
6080 return error;
6081}
6082
6083static enum ofperr
6084handle_meter_features_request(struct ofconn *ofconn,
6085 const struct ofp_header *request)
6086{
6087 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
6088 struct ofputil_meter_features features;
6089 struct ofpbuf *b;
6090
6091 if (ofproto->ofproto_class->meter_get_features) {
6092 ofproto->ofproto_class->meter_get_features(ofproto, &features);
6093 } else {
6094 memset(&features, 0, sizeof features);
6095 }
6096 b = ofputil_encode_meter_features_reply(&features, request);
6097
6098 ofconn_send_reply(ofconn, b);
6099 return 0;
6100}
6101
6102static enum ofperr
6103handle_meter_request(struct ofconn *ofconn, const struct ofp_header *request,
6104 enum ofptype type)
6105{
6106 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
ca6ba700 6107 struct ovs_list replies;
9cae45dc
JR
6108 uint64_t bands_stub[256 / 8];
6109 struct ofpbuf bands;
6110 uint32_t meter_id, first, last;
6111
6112 ofputil_decode_meter_request(request, &meter_id);
6113
6114 if (meter_id == OFPM13_ALL) {
6115 first = 1;
6116 last = ofproto->meter_features.max_meters;
6117 } else {
6118 if (!meter_id || meter_id > ofproto->meter_features.max_meters ||
6119 !ofproto->meters[meter_id]) {
6120 return OFPERR_OFPMMFC_UNKNOWN_METER;
6121 }
6122 first = last = meter_id;
6123 }
6124
6125 ofpbuf_use_stub(&bands, bands_stub, sizeof bands_stub);
6126 ofpmp_init(&replies, request);
6127
6128 for (meter_id = first; meter_id <= last; ++meter_id) {
6129 struct meter *meter = ofproto->meters[meter_id];
6130 if (!meter) {
6131 continue; /* Skip non-existing meters. */
6132 }
261bd854 6133 if (type == OFPTYPE_METER_STATS_REQUEST) {
9cae45dc
JR
6134 struct ofputil_meter_stats stats;
6135
6136 stats.meter_id = meter_id;
6137
6138 /* Provider sets the packet and byte counts, we do the rest. */
417e7e66 6139 stats.flow_count = ovs_list_size(&meter->rules);
9cae45dc
JR
6140 calc_duration(meter->created, time_msec(),
6141 &stats.duration_sec, &stats.duration_nsec);
6142 stats.n_bands = meter->n_bands;
6143 ofpbuf_clear(&bands);
6144 stats.bands
6145 = ofpbuf_put_uninit(&bands,
6146 meter->n_bands * sizeof *stats.bands);
6147
6148 if (!ofproto->ofproto_class->meter_get(ofproto,
6149 meter->provider_meter_id,
6150 &stats)) {
6151 ofputil_append_meter_stats(&replies, &stats);
6152 }
6153 } else { /* type == OFPTYPE_METER_CONFIG_REQUEST */
6154 struct ofputil_meter_config config;
6155
6156 config.meter_id = meter_id;
6157 config.flags = meter->flags;
6158 config.n_bands = meter->n_bands;
6159 config.bands = meter->bands;
6160 ofputil_append_meter_config(&replies, &config);
6161 }
6162 }
6163
6164 ofconn_send_replies(ofconn, &replies);
6165 ofpbuf_uninit(&bands);
6166 return 0;
6167}
6168
db88b35c
JR
6169/* Returned group is RCU protected. */
6170static struct ofgroup *
5d08a275
JR
6171ofproto_group_lookup__(const struct ofproto *ofproto, uint32_t group_id,
6172 ovs_version_t version)
db88b35c
JR
6173{
6174 struct ofgroup *group;
6175
6176 CMAP_FOR_EACH_WITH_HASH (group, cmap_node, hash_int(group_id, 0),
6177 &ofproto->groups) {
5d08a275
JR
6178 if (group->group_id == group_id
6179 && versions_visible_in_version(&group->versions, version)) {
db88b35c 6180 return group;
7395c052
NZ
6181 }
6182 }
3fc3b679 6183
db88b35c 6184 return NULL;
7395c052
NZ
6185}
6186
3fc3b679
AZ
6187/* If the group exists, this function increments the groups's reference count.
6188 *
6189 * Make sure to call ofproto_group_unref() after no longer needing to maintain
6190 * a reference to the group. */
db88b35c 6191struct ofgroup *
76973237 6192ofproto_group_lookup(const struct ofproto *ofproto, uint32_t group_id,
5d08a275 6193 ovs_version_t version, bool take_ref)
7395c052 6194{
db88b35c 6195 struct ofgroup *group;
7395c052 6196
5d08a275 6197 group = ofproto_group_lookup__(ofproto, group_id, version);
76973237 6198 if (group && take_ref) {
db88b35c
JR
6199 /* Not holding a lock, so it is possible that another thread releases
6200 * the last reference just before we manage to get one. */
6201 return ofproto_group_try_ref(group) ? group : NULL;
7395c052 6202 }
76973237 6203 return group;
7395c052
NZ
6204}
6205
cc099268 6206/* Caller should hold 'ofproto_mutex' if it is important that the
db88b35c 6207 * group is not removed by someone else. */
7e9f8266
BP
6208static bool
6209ofproto_group_exists(const struct ofproto *ofproto, uint32_t group_id)
7e9f8266 6210{
5d08a275 6211 return ofproto_group_lookup__(ofproto, group_id, OVS_VERSION_MAX) != NULL;
7e9f8266
BP
6212}
6213
cc099268
JR
6214static void
6215group_add_rule(struct ofgroup *group, struct rule *rule)
732feb93 6216{
cc099268
JR
6217 rule_collection_add(&group->rules, rule);
6218}
732feb93 6219
cc099268
JR
6220static void
6221group_remove_rule(struct ofgroup *group, struct rule *rule)
6222{
6223 rule_collection_remove(&group->rules, rule);
732feb93
SH
6224}
6225
7395c052 6226static void
ca6ba700 6227append_group_stats(struct ofgroup *group, struct ovs_list *replies)
cc099268 6228 OVS_REQUIRES(ofproto_mutex)
7395c052
NZ
6229{
6230 struct ofputil_group_stats ogs;
eaf004bd 6231 const struct ofproto *ofproto = group->ofproto;
7395c052
NZ
6232 long long int now = time_msec();
6233 int error;
6234
646b2a9c
SH
6235 ogs.bucket_stats = xmalloc(group->n_buckets * sizeof *ogs.bucket_stats);
6236
732feb93 6237 /* Provider sets the packet and byte counts, we do the rest. */
fe59694b 6238 ogs.ref_count = rule_collection_n(&group->rules);
732feb93
SH
6239 ogs.n_buckets = group->n_buckets;
6240
7395c052
NZ
6241 error = (ofproto->ofproto_class->group_get_stats
6242 ? ofproto->ofproto_class->group_get_stats(group, &ogs)
6243 : EOPNOTSUPP);
6244 if (error) {
7395c052
NZ
6245 ogs.packet_count = UINT64_MAX;
6246 ogs.byte_count = UINT64_MAX;
7395c052
NZ
6247 memset(ogs.bucket_stats, 0xff,
6248 ogs.n_buckets * sizeof *ogs.bucket_stats);
6249 }
6250
6251 ogs.group_id = group->group_id;
6252 calc_duration(group->created, now, &ogs.duration_sec, &ogs.duration_nsec);
6253
6254 ofputil_append_group_stats(replies, &ogs);
646b2a9c
SH
6255
6256 free(ogs.bucket_stats);
7395c052
NZ
6257}
6258
19187a71
BP
6259static void
6260handle_group_request(struct ofconn *ofconn,
6261 const struct ofp_header *request, uint32_t group_id,
ca6ba700 6262 void (*cb)(struct ofgroup *, struct ovs_list *replies))
cc099268 6263 OVS_EXCLUDED(ofproto_mutex)
7395c052
NZ
6264{
6265 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
7395c052 6266 struct ofgroup *group;
ca6ba700 6267 struct ovs_list replies;
7395c052
NZ
6268
6269 ofpmp_init(&replies, request);
cc099268
JR
6270 /* Must exclude modifications to guarantee iterating groups. */
6271 ovs_mutex_lock(&ofproto_mutex);
7395c052 6272 if (group_id == OFPG_ALL) {
db88b35c 6273 CMAP_FOR_EACH (group, cmap_node, &ofproto->groups) {
19187a71 6274 cb(group, &replies);
7395c052 6275 }
7395c052 6276 } else {
5d08a275 6277 group = ofproto_group_lookup__(ofproto, group_id, OVS_VERSION_MAX);
db88b35c 6278 if (group) {
19187a71 6279 cb(group, &replies);
7395c052
NZ
6280 }
6281 }
cc099268 6282 ovs_mutex_unlock(&ofproto_mutex);
7395c052 6283 ofconn_send_replies(ofconn, &replies);
7395c052
NZ
6284}
6285
6286static enum ofperr
19187a71
BP
6287handle_group_stats_request(struct ofconn *ofconn,
6288 const struct ofp_header *request)
7395c052 6289{
19187a71
BP
6290 uint32_t group_id;
6291 enum ofperr error;
7395c052 6292
19187a71
BP
6293 error = ofputil_decode_group_stats_request(request, &group_id);
6294 if (error) {
6295 return error;
7395c052 6296 }
7395c052 6297
19187a71
BP
6298 handle_group_request(ofconn, request, group_id, append_group_stats);
6299 return 0;
6300}
6301
6302static void
ca6ba700 6303append_group_desc(struct ofgroup *group, struct ovs_list *replies)
19187a71
BP
6304{
6305 struct ofputil_group_desc gds;
7395c052 6306
19187a71
BP
6307 gds.group_id = group->group_id;
6308 gds.type = group->type;
53eb84a5
SH
6309 gds.props = group->props;
6310
19187a71
BP
6311 ofputil_append_group_desc_reply(&gds, &group->buckets, replies);
6312}
6313
6314static enum ofperr
6315handle_group_desc_stats_request(struct ofconn *ofconn,
6316 const struct ofp_header *request)
6317{
6318 handle_group_request(ofconn, request,
6319 ofputil_decode_group_desc_request(request),
6320 append_group_desc);
7395c052
NZ
6321 return 0;
6322}
6323
6324static enum ofperr
6325handle_group_features_stats_request(struct ofconn *ofconn,
6326 const struct ofp_header *request)
6327{
6328 struct ofproto *p = ofconn_get_ofproto(ofconn);
6329 struct ofpbuf *msg;
6330
6331 msg = ofputil_encode_group_features_reply(&p->ogf, request);
6332 if (msg) {
6333 ofconn_send_reply(ofconn, msg);
6334 }
6335
6336 return 0;
6337}
6338
56085be5 6339static void
e016fb63
BP
6340put_queue_get_config_reply(struct ofport *port, uint32_t queue,
6341 struct ovs_list *replies)
e8f9a7bb 6342{
e016fb63 6343 struct ofputil_queue_config qc;
e8f9a7bb 6344
e016fb63
BP
6345 /* None of the existing queues have compatible properties, so we hard-code
6346 * omitting min_rate and max_rate. */
6347 qc.port = port->ofp_port;
6348 qc.queue = queue;
6349 qc.min_rate = UINT16_MAX;
6350 qc.max_rate = UINT16_MAX;
6351 ofputil_append_queue_get_config_reply(&qc, replies);
6352}
e8f9a7bb 6353
e016fb63
BP
6354static int
6355handle_queue_get_config_request_for_port(struct ofport *port, uint32_t queue,
6356 struct ovs_list *replies)
6357{
6358 struct smap details = SMAP_INITIALIZER(&details);
6359 if (queue != OFPQ_ALL) {
6360 int error = netdev_get_queue(port->netdev, queue, &details);
6361 switch (error) {
6362 case 0:
6363 put_queue_get_config_reply(port, queue, replies);
6364 break;
6365 case EOPNOTSUPP:
6366 case EINVAL:
6367 return OFPERR_OFPQOFC_BAD_QUEUE;
6368 default:
6369 return OFPERR_NXQOFC_QUEUE_ERROR;
6370 }
6371 } else {
6372 struct netdev_queue_dump queue_dump;
6373 uint32_t queue_id;
6374
6375 NETDEV_QUEUE_FOR_EACH (&queue_id, &details, &queue_dump,
6376 port->netdev) {
6377 put_queue_get_config_reply(port, queue_id, replies);
6378 }
6379 }
6380 smap_destroy(&details);
6381 return 0;
56085be5
BP
6382}
6383
6384static enum ofperr
6385handle_queue_get_config_request(struct ofconn *ofconn,
6386 const struct ofp_header *oh)
6387{
e016fb63
BP
6388 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
6389 struct ovs_list replies;
6390 struct ofport *port;
6391 ofp_port_t req_port;
6392 uint32_t req_queue;
6393 enum ofperr error;
6394
6395 error = ofputil_decode_queue_get_config_request(oh, &req_port, &req_queue);
6396 if (error) {
6397 return error;
6398 }
6399
6400 ofputil_start_queue_get_config_reply(oh, &replies);
6401 if (req_port == OFPP_ANY) {
6402 error = OFPERR_OFPQOFC_BAD_QUEUE;
6403 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
6404 if (!handle_queue_get_config_request_for_port(port, req_queue,
6405 &replies)) {
6406 error = 0;
6407 }
6408 }
6409 } else {
6410 port = ofproto_get_port(ofproto, req_port);
6411 error = (port
6412 ? handle_queue_get_config_request_for_port(port, req_queue,
6413 &replies)
6414 : OFPERR_OFPQOFC_BAD_PORT);
6415 }
6416 if (!error) {
6417 ofconn_send_replies(ofconn, &replies);
6418 } else {
6419 ofpbuf_list_delete(&replies);
6420 }
6421
6422 return error;
e8f9a7bb
VG
6423}
6424
809c7548 6425static enum ofperr
63eded98 6426init_group(struct ofproto *ofproto, const struct ofputil_group_mod *gm,
5d08a275 6427 ovs_version_t version, struct ofgroup **ofgroup)
809c7548
RW
6428{
6429 enum ofperr error;
eaf004bd 6430 const long long int now = time_msec();
809c7548
RW
6431
6432 if (gm->group_id > OFPG_MAX) {
6433 return OFPERR_OFPGMFC_INVALID_GROUP;
6434 }
6435 if (gm->type > OFPGT11_FF) {
6436 return OFPERR_OFPGMFC_BAD_TYPE;
6437 }
6438
6439 *ofgroup = ofproto->ofproto_class->group_alloc();
6440 if (!*ofgroup) {
6441 VLOG_WARN_RL(&rl, "%s: failed to allocate group", ofproto->name);
6442 return OFPERR_OFPGMFC_OUT_OF_GROUPS;
6443 }
6444
5d08a275 6445 *CONST_CAST(struct ofproto **, &(*ofgroup)->ofproto) = ofproto;
eaf004bd
AZ
6446 *CONST_CAST(uint32_t *, &((*ofgroup)->group_id)) = gm->group_id;
6447 *CONST_CAST(enum ofp11_group_type *, &(*ofgroup)->type) = gm->type;
6448 *CONST_CAST(long long int *, &((*ofgroup)->created)) = now;
6449 *CONST_CAST(long long int *, &((*ofgroup)->modified)) = now;
809c7548 6450 ovs_refcount_init(&(*ofgroup)->ref_count);
cc099268 6451 (*ofgroup)->being_deleted = false;
809c7548 6452
db88b35c
JR
6453 ovs_list_init(CONST_CAST(struct ovs_list *, &(*ofgroup)->buckets));
6454 ofputil_bucket_clone_list(CONST_CAST(struct ovs_list *,
6455 &(*ofgroup)->buckets),
6456 &gm->buckets, NULL);
63eded98 6457
eaf004bd 6458 *CONST_CAST(uint32_t *, &(*ofgroup)->n_buckets) =
417e7e66 6459 ovs_list_size(&(*ofgroup)->buckets);
809c7548 6460
e8dba719
JR
6461 ofputil_group_properties_copy(CONST_CAST(struct ofputil_group_props *,
6462 &(*ofgroup)->props),
6463 &gm->props);
cc099268 6464 rule_collection_init(&(*ofgroup)->rules);
bc65c25a 6465
5d08a275
JR
6466 /* Make group visible from 'version'. */
6467 (*ofgroup)->versions = VERSIONS_INITIALIZER(version,
6468 OVS_VERSION_NOT_REMOVED);
6469
809c7548
RW
6470 /* Construct called BEFORE any locks are held. */
6471 error = ofproto->ofproto_class->group_construct(*ofgroup);
6472 if (error) {
e8dba719
JR
6473 ofputil_group_properties_destroy(CONST_CAST(struct ofputil_group_props *,
6474 &(*ofgroup)->props));
db88b35c
JR
6475 ofputil_bucket_list_destroy(CONST_CAST(struct ovs_list *,
6476 &(*ofgroup)->buckets));
809c7548
RW
6477 ofproto->ofproto_class->group_dealloc(*ofgroup);
6478 }
6479 return error;
6480}
6481
6c5b90ce
BP
6482/* Implements the OFPGC11_ADD operation specified by 'gm', adding a group to
6483 * 'ofproto''s group table. Returns 0 on success or an OpenFlow error code on
6484 * failure. */
7395c052 6485static enum ofperr
2b0b0b80 6486add_group_start(struct ofproto *ofproto, struct ofproto_group_mod *ogm)
cc099268 6487 OVS_REQUIRES(ofproto_mutex)
7395c052 6488{
7395c052
NZ
6489 enum ofperr error;
6490
2b0b0b80
JR
6491 if (ofproto_group_exists(ofproto, ogm->gm.group_id)) {
6492 return OFPERR_OFPGMFC_GROUP_EXISTS;
7395c052
NZ
6493 }
6494
2b0b0b80
JR
6495 if (ofproto->n_groups[ogm->gm.type]
6496 >= ofproto->ogf.max_groups[ogm->gm.type]) {
6497 return OFPERR_OFPGMFC_OUT_OF_GROUPS;
7395c052
NZ
6498 }
6499
2b0b0b80 6500 /* Allocate new group and initialize it. */
5d08a275 6501 error = init_group(ofproto, &ogm->gm, ogm->version, &ogm->new_group);
2b0b0b80
JR
6502 if (!error) {
6503 /* Insert new group. */
6504 cmap_insert(&ofproto->groups, &ogm->new_group->cmap_node,
6505 hash_int(ogm->new_group->group_id, 0));
6506 ofproto->n_groups[ogm->new_group->type]++;
7395c052 6507 }
7395c052
NZ
6508 return error;
6509}
6510
fce4730c
SH
6511/* Adds all of the buckets from 'ofgroup' to 'new_ofgroup'. The buckets
6512 * already in 'new_ofgroup' will be placed just after the (copy of the) bucket
6513 * in 'ofgroup' with bucket ID 'command_bucket_id'. Special
6514 * 'command_bucket_id' values OFPG15_BUCKET_FIRST and OFPG15_BUCKET_LAST are
6515 * also honored. */
6516static enum ofperr
6517copy_buckets_for_insert_bucket(const struct ofgroup *ofgroup,
6518 struct ofgroup *new_ofgroup,
6519 uint32_t command_bucket_id)
6520{
6521 struct ofputil_bucket *last = NULL;
6522
6523 if (command_bucket_id <= OFPG15_BUCKET_MAX) {
6524 /* Check here to ensure that a bucket corresponding to
6525 * command_bucket_id exists in the old bucket list.
6526 *
6527 * The subsequent search of below of new_ofgroup covers
6528 * both buckets in the old bucket list and buckets added
6529 * by the insert buckets group mod message this function processes. */
6530 if (!ofputil_bucket_find(&ofgroup->buckets, command_bucket_id)) {
6531 return OFPERR_OFPGMFC_UNKNOWN_BUCKET;
6532 }
6533
417e7e66 6534 if (!ovs_list_is_empty(&new_ofgroup->buckets)) {
fce4730c
SH
6535 last = ofputil_bucket_list_back(&new_ofgroup->buckets);
6536 }
6537 }
6538
db88b35c
JR
6539 ofputil_bucket_clone_list(CONST_CAST(struct ovs_list *,
6540 &new_ofgroup->buckets),
6541 &ofgroup->buckets, NULL);
fce4730c 6542
23a31238 6543 if (ofputil_bucket_check_duplicate_id(&new_ofgroup->buckets)) {
d2873d53 6544 VLOG_INFO_RL(&rl, "Duplicate bucket id");
fce4730c
SH
6545 return OFPERR_OFPGMFC_BUCKET_EXISTS;
6546 }
6547
6548 /* Rearrange list according to command_bucket_id */
6549 if (command_bucket_id == OFPG15_BUCKET_LAST) {
417e7e66 6550 if (!ovs_list_is_empty(&ofgroup->buckets)) {
8e19e655
BP
6551 struct ofputil_bucket *new_first;
6552 const struct ofputil_bucket *first;
fce4730c 6553
8e19e655
BP
6554 first = ofputil_bucket_list_front(&ofgroup->buckets);
6555 new_first = ofputil_bucket_find(&new_ofgroup->buckets,
6556 first->bucket_id);
fce4730c 6557
417e7e66 6558 ovs_list_splice(new_ofgroup->buckets.next, &new_first->list_node,
db88b35c
JR
6559 CONST_CAST(struct ovs_list *,
6560 &new_ofgroup->buckets));
8e19e655 6561 }
fce4730c
SH
6562 } else if (command_bucket_id <= OFPG15_BUCKET_MAX && last) {
6563 struct ofputil_bucket *after;
6564
6565 /* Presence of bucket is checked above so after should never be NULL */
6566 after = ofputil_bucket_find(&new_ofgroup->buckets, command_bucket_id);
6567
417e7e66 6568 ovs_list_splice(after->list_node.next, new_ofgroup->buckets.next,
fce4730c
SH
6569 last->list_node.next);
6570 }
6571
6572 return 0;
6573}
6574
6575/* Appends all of the a copy of all the buckets from 'ofgroup' to 'new_ofgroup'
6576 * with the exception of the bucket whose bucket id is 'command_bucket_id'.
6577 * Special 'command_bucket_id' values OFPG15_BUCKET_FIRST, OFPG15_BUCKET_LAST
6578 * and OFPG15_BUCKET_ALL are also honored. */
6579static enum ofperr
6580copy_buckets_for_remove_bucket(const struct ofgroup *ofgroup,
6581 struct ofgroup *new_ofgroup,
6582 uint32_t command_bucket_id)
6583{
6584 const struct ofputil_bucket *skip = NULL;
6585
6586 if (command_bucket_id == OFPG15_BUCKET_ALL) {
6587 return 0;
6588 }
6589
6590 if (command_bucket_id == OFPG15_BUCKET_FIRST) {
417e7e66 6591 if (!ovs_list_is_empty(&ofgroup->buckets)) {
fce4730c
SH
6592 skip = ofputil_bucket_list_front(&ofgroup->buckets);
6593 }
6594 } else if (command_bucket_id == OFPG15_BUCKET_LAST) {
417e7e66 6595 if (!ovs_list_is_empty(&ofgroup->buckets)) {
fce4730c
SH
6596 skip = ofputil_bucket_list_back(&ofgroup->buckets);
6597 }
6598 } else {
6599 skip = ofputil_bucket_find(&ofgroup->buckets, command_bucket_id);
6600 if (!skip) {
6601 return OFPERR_OFPGMFC_UNKNOWN_BUCKET;
6602 }
6603 }
6604
db88b35c
JR
6605 ofputil_bucket_clone_list(CONST_CAST(struct ovs_list *,
6606 &new_ofgroup->buckets),
6607 &ofgroup->buckets, skip);
fce4730c
SH
6608
6609 return 0;
6610}
6611
6612/* Implements OFPGC11_MODIFY, OFPGC15_INSERT_BUCKET and
6613 * OFPGC15_REMOVE_BUCKET. Returns 0 on success or an OpenFlow error code
6c5b90ce 6614 * on failure.
7395c052 6615 *
809c7548
RW
6616 * Note that the group is re-created and then replaces the old group in
6617 * ofproto's ofgroup hash map. Thus, the group is never altered while users of
6c5b90ce 6618 * the xlate module hold a pointer to the group. */
7395c052 6619static enum ofperr
2b0b0b80 6620modify_group_start(struct ofproto *ofproto, struct ofproto_group_mod *ogm)
cc099268 6621 OVS_REQUIRES(ofproto_mutex)
7395c052 6622{
2b0b0b80
JR
6623 struct ofgroup *old_group; /* Modified group. */
6624 struct ofgroup *new_group;
7395c052
NZ
6625 enum ofperr error;
6626
5d08a275
JR
6627 old_group = ofproto_group_lookup__(ofproto, ogm->gm.group_id,
6628 OVS_VERSION_MAX);
2b0b0b80
JR
6629 if (!old_group) {
6630 return OFPERR_OFPGMFC_UNKNOWN_GROUP;
7395c052
NZ
6631 }
6632
2b0b0b80
JR
6633 if (old_group->type != ogm->gm.type
6634 && (ofproto->n_groups[ogm->gm.type]
6635 >= ofproto->ogf.max_groups[ogm->gm.type])) {
6636 return OFPERR_OFPGMFC_OUT_OF_GROUPS;
7395c052 6637 }
809c7548 6638
5d08a275 6639 error = init_group(ofproto, &ogm->gm, ogm->version, &ogm->new_group);
2b0b0b80
JR
6640 if (error) {
6641 return error;
7395c052 6642 }
2b0b0b80 6643 new_group = ogm->new_group;
7395c052 6644
fce4730c 6645 /* Manipulate bucket list for bucket commands */
2b0b0b80
JR
6646 if (ogm->gm.command == OFPGC15_INSERT_BUCKET) {
6647 error = copy_buckets_for_insert_bucket(old_group, new_group,
6648 ogm->gm.command_bucket_id);
6649 } else if (ogm->gm.command == OFPGC15_REMOVE_BUCKET) {
6650 error = copy_buckets_for_remove_bucket(old_group, new_group,
6651 ogm->gm.command_bucket_id);
fce4730c
SH
6652 }
6653 if (error) {
6654 goto out;
6655 }
6656
809c7548 6657 /* The group creation time does not change during modification. */
2b0b0b80
JR
6658 *CONST_CAST(long long int *, &(new_group->created)) = old_group->created;
6659 *CONST_CAST(long long int *, &(new_group->modified)) = time_msec();
7395c052 6660
2b0b0b80 6661 group_collection_add(&ogm->old_groups, old_group);
db88b35c 6662
5d08a275
JR
6663 /* Mark the old group for deletion. */
6664 versions_set_remove_version(&old_group->versions, ogm->version);
6665 /* Insert replacement group. */
6666 cmap_insert(&ofproto->groups, &new_group->cmap_node,
6667 hash_int(new_group->group_id, 0));
cc099268 6668 /* Transfer rules. */
2b0b0b80 6669 rule_collection_move(&new_group->rules, &old_group->rules);
db88b35c 6670
2b0b0b80
JR
6671 if (old_group->type != new_group->type) {
6672 ofproto->n_groups[old_group->type]--;
6673 ofproto->n_groups[new_group->type]++;
7395c052 6674 }
2b0b0b80 6675 return 0;
7395c052 6676
809c7548 6677out:
2b0b0b80 6678 ofproto_group_unref(new_group);
7395c052
NZ
6679 return error;
6680}
6681
88b87a36
JS
6682/* Implements the OFPGC11_ADD_OR_MOD command which creates the group when it does not
6683 * exist yet and modifies it otherwise */
6684static enum ofperr
2b0b0b80
JR
6685add_or_modify_group_start(struct ofproto *ofproto,
6686 struct ofproto_group_mod *ogm)
cc099268 6687 OVS_REQUIRES(ofproto_mutex)
88b87a36 6688{
88b87a36 6689 enum ofperr error;
88b87a36 6690
2b0b0b80
JR
6691 if (!ofproto_group_exists(ofproto, ogm->gm.group_id)) {
6692 error = add_group_start(ofproto, ogm);
88b87a36 6693 } else {
2b0b0b80 6694 error = modify_group_start(ofproto, ogm);
88b87a36 6695 }
cc099268 6696
88b87a36
JS
6697 return error;
6698}
6699
7395c052 6700static void
5d08a275
JR
6701delete_group_start(struct ofproto *ofproto, ovs_version_t version,
6702 struct group_collection *groups, struct ofgroup *group)
cc099268 6703 OVS_REQUIRES(ofproto_mutex)
7395c052 6704{
2b0b0b80 6705 /* Makes flow deletion code leave the rule pointers in 'group->rules'
cc099268
JR
6706 * intact, so that we can later refer to the rules deleted due to the group
6707 * deletion. Rule pointers will be removed from all other groups, if any,
6708 * so we will never try to delete the same rule twice. */
2b0b0b80
JR
6709 group->being_deleted = true;
6710
6711 /* Mark all the referring groups for deletion. */
5d08a275 6712 delete_flows_start__(ofproto, version, &group->rules);
2b0b0b80 6713 group_collection_add(groups, group);
5d08a275 6714 versions_set_remove_version(&group->versions, version);
2b0b0b80
JR
6715 ofproto->n_groups[group->type]--;
6716}
276f6518 6717
2b0b0b80
JR
6718static void
6719delete_group_finish(struct ofproto *ofproto, struct ofgroup *group)
6720 OVS_REQUIRES(ofproto_mutex)
6721{
6722 /* Finish deletion of all flow entries containing this group in a group
6723 * action. */
6724 delete_flows_finish__(ofproto, &group->rules, OFPRR_GROUP_DELETE, NULL);
276f6518 6725
5d08a275 6726 /* Group removal is postponed by the caller. */
7395c052
NZ
6727}
6728
6c5b90ce 6729/* Implements OFPGC11_DELETE. */
7395c052 6730static void
2b0b0b80 6731delete_groups_start(struct ofproto *ofproto, struct ofproto_group_mod *ogm)
cc099268 6732 OVS_REQUIRES(ofproto_mutex)
7395c052 6733{
2b0b0b80 6734 struct ofgroup *group;
7395c052 6735
2b0b0b80
JR
6736 if (ogm->gm.group_id == OFPG_ALL) {
6737 CMAP_FOR_EACH (group, cmap_node, &ofproto->groups) {
5d08a275
JR
6738 if (versions_visible_in_version(&group->versions, ogm->version)) {
6739 delete_group_start(ofproto, ogm->version, &ogm->old_groups,
6740 group);
6741 }
7395c052
NZ
6742 }
6743 } else {
5d08a275
JR
6744 group = ofproto_group_lookup__(ofproto, ogm->gm.group_id, ogm->version);
6745 if (group) {
6746 delete_group_start(ofproto, ogm->version, &ogm->old_groups, group);
7395c052
NZ
6747 }
6748 }
7395c052
NZ
6749}
6750
6751static enum ofperr
2b0b0b80
JR
6752ofproto_group_mod_start(struct ofproto *ofproto, struct ofproto_group_mod *ogm)
6753 OVS_REQUIRES(ofproto_mutex)
7395c052 6754{
7395c052
NZ
6755 enum ofperr error;
6756
2b0b0b80
JR
6757 ogm->new_group = NULL;
6758 group_collection_init(&ogm->old_groups);
7395c052 6759
2b0b0b80 6760 switch (ogm->gm.command) {
7395c052 6761 case OFPGC11_ADD:
2b0b0b80 6762 error = add_group_start(ofproto, ogm);
3c35db62 6763 break;
7395c052
NZ
6764
6765 case OFPGC11_MODIFY:
2b0b0b80 6766 error = modify_group_start(ofproto, ogm);
3c35db62 6767 break;
7395c052 6768
88b87a36 6769 case OFPGC11_ADD_OR_MOD:
2b0b0b80 6770 error = add_or_modify_group_start(ofproto, ogm);
88b87a36
JS
6771 break;
6772
7395c052 6773 case OFPGC11_DELETE:
2b0b0b80 6774 delete_groups_start(ofproto, ogm);
3c35db62
NR
6775 error = 0;
6776 break;
7395c052 6777
fce4730c 6778 case OFPGC15_INSERT_BUCKET:
2b0b0b80 6779 error = modify_group_start(ofproto, ogm);
3c35db62 6780 break;
fce4730c
SH
6781
6782 case OFPGC15_REMOVE_BUCKET:
2b0b0b80 6783 error = modify_group_start(ofproto, ogm);
3c35db62 6784 break;
fce4730c 6785
7395c052 6786 default:
2b0b0b80 6787 if (ogm->gm.command > OFPGC11_DELETE) {
d2873d53 6788 VLOG_INFO_RL(&rl, "%s: Invalid group_mod command type %d",
2b0b0b80 6789 ofproto->name, ogm->gm.command);
7395c052 6790 }
63eded98 6791 error = OFPERR_OFPGMFC_BAD_COMMAND;
2b0b0b80 6792 break;
7395c052 6793 }
2b0b0b80
JR
6794 return error;
6795}
3c35db62 6796
25070e04
JR
6797static void
6798ofproto_group_mod_revert(struct ofproto *ofproto,
6799 struct ofproto_group_mod *ogm)
6800 OVS_REQUIRES(ofproto_mutex)
6801{
6802 struct ofgroup *new_group = ogm->new_group;
6803 struct ofgroup *old_group;
6804
6805 /* Restore replaced or deleted groups. */
6806 GROUP_COLLECTION_FOR_EACH (old_group, &ogm->old_groups) {
6807 ofproto->n_groups[old_group->type]++;
6808 if (new_group) {
6809 ovs_assert(group_collection_n(&ogm->old_groups) == 1);
6810 /* Transfer rules back. */
6811 rule_collection_move(&old_group->rules, &new_group->rules);
6812 } else {
6813 old_group->being_deleted = false;
6814 /* Revert rule deletion. */
6815 delete_flows_revert__(ofproto, &old_group->rules);
6816 }
6817 /* Restore visibility. */
6818 versions_set_remove_version(&old_group->versions,
6819 OVS_VERSION_NOT_REMOVED);
6820 }
6821 if (new_group) {
6822 /* Remove the new group immediately. It was never visible to
6823 * lookups. */
6824 cmap_remove(&ofproto->groups, &new_group->cmap_node,
6825 hash_int(new_group->group_id, 0));
6826 ofproto->n_groups[new_group->type]--;
6827 ofproto_group_unref(new_group);
6828 }
6829}
6830
2b0b0b80
JR
6831static void
6832ofproto_group_mod_finish(struct ofproto *ofproto,
6833 struct ofproto_group_mod *ogm,
6834 const struct openflow_mod_requester *req)
6835 OVS_REQUIRES(ofproto_mutex)
6836{
6837 struct ofgroup *new_group = ogm->new_group;
6838 struct ofgroup *old_group;
6839
5d08a275 6840 if (new_group && group_collection_n(&ogm->old_groups)) {
2b0b0b80
JR
6841 /* Modify a group. */
6842 ovs_assert(group_collection_n(&ogm->old_groups) == 1);
2b0b0b80
JR
6843
6844 /* XXX: OK to lose old group's stats? */
6845 ofproto->ofproto_class->group_modify(new_group);
5d08a275 6846 }
2b0b0b80 6847
5d08a275
JR
6848 /* Delete old groups. */
6849 GROUP_COLLECTION_FOR_EACH(old_group, &ogm->old_groups) {
6850 delete_group_finish(ofproto, old_group);
2b0b0b80 6851 }
5d08a275 6852 remove_groups_postponed(&ogm->old_groups);
2b0b0b80
JR
6853
6854 if (req) {
3c35db62 6855 struct ofputil_requestforward rf;
2b0b0b80 6856 rf.xid = req->request->xid;
3c35db62 6857 rf.reason = OFPRFR_GROUP_MOD;
2b0b0b80
JR
6858 rf.group_mod = &ogm->gm;
6859 connmgr_send_requestforward(ofproto->connmgr, req->ofconn, &rf);
6860 }
6861}
6862
6863/* Delete all groups from 'ofproto'.
6864 *
6865 * This is intended for use within an ofproto provider's 'destruct'
6866 * function. */
6867void
6868ofproto_group_delete_all(struct ofproto *ofproto)
6869 OVS_EXCLUDED(ofproto_mutex)
6870{
6871 struct ofproto_group_mod ogm;
6872
6873 ogm.gm.command = OFPGC11_DELETE;
6874 ogm.gm.group_id = OFPG_ALL;
6875
6876 ovs_mutex_lock(&ofproto_mutex);
5d08a275 6877 ogm.version = ofproto->tables_version + 1;
2b0b0b80 6878 ofproto_group_mod_start(ofproto, &ogm);
5d08a275 6879 ofproto_bump_tables_version(ofproto);
2b0b0b80
JR
6880 ofproto_group_mod_finish(ofproto, &ogm, NULL);
6881 ovs_mutex_unlock(&ofproto_mutex);
6882}
6883
6884static enum ofperr
6885handle_group_mod(struct ofconn *ofconn, const struct ofp_header *oh)
6886 OVS_EXCLUDED(ofproto_mutex)
6887{
6888 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
6889 struct ofproto_group_mod ogm;
6890 enum ofperr error;
6891
6892 error = reject_slave_controller(ofconn);
6893 if (error) {
6894 return error;
6895 }
6896
6897 error = ofputil_decode_group_mod(oh, &ogm.gm);
6898 if (error) {
6899 return error;
3c35db62 6900 }
2b0b0b80
JR
6901
6902 ovs_mutex_lock(&ofproto_mutex);
5d08a275 6903 ogm.version = ofproto->tables_version + 1;
2b0b0b80
JR
6904 error = ofproto_group_mod_start(ofproto, &ogm);
6905 if (!error) {
6906 struct openflow_mod_requester req = { ofconn, oh };
5d08a275
JR
6907
6908 ofproto_bump_tables_version(ofproto);
2b0b0b80
JR
6909 ofproto_group_mod_finish(ofproto, &ogm, &req);
6910 }
6911 ofmonitor_flush(ofproto->connmgr);
6912 ovs_mutex_unlock(&ofproto_mutex);
6913
75868d0e 6914 ofputil_uninit_group_mod(&ogm.gm);
63eded98 6915
3c35db62 6916 return error;
7395c052
NZ
6917}
6918
3c1bb396
BP
6919enum ofputil_table_miss
6920ofproto_table_get_miss_config(const struct ofproto *ofproto, uint8_t table_id)
6d328fa2 6921{
82c22d34
BP
6922 enum ofputil_table_miss miss;
6923
6924 atomic_read_relaxed(&ofproto->tables[table_id].miss_config, &miss);
6925 return miss;
6926}
6927
6928static void
6929table_mod__(struct oftable *oftable,
de7d3c07 6930 const struct ofputil_table_mod *tm)
82c22d34 6931{
de7d3c07 6932 if (tm->miss == OFPUTIL_TABLE_MISS_DEFAULT) {
82c22d34
BP
6933 /* This is how an OFPT_TABLE_MOD decodes if it doesn't specify any
6934 * table-miss configuration (because the protocol used doesn't have
6935 * such a concept), so there's nothing to do. */
6936 } else {
de7d3c07 6937 atomic_store_relaxed(&oftable->miss_config, tm->miss);
82c22d34
BP
6938 }
6939
6940 unsigned int new_eviction = oftable->eviction;
de7d3c07 6941 if (tm->eviction == OFPUTIL_TABLE_EVICTION_ON) {
82c22d34 6942 new_eviction |= EVICTION_OPENFLOW;
de7d3c07 6943 } else if (tm->eviction == OFPUTIL_TABLE_EVICTION_OFF) {
82c22d34
BP
6944 new_eviction &= ~EVICTION_OPENFLOW;
6945 }
afcea9ea 6946
82c22d34
BP
6947 if (new_eviction != oftable->eviction) {
6948 ovs_mutex_lock(&ofproto_mutex);
6949 oftable_configure_eviction(oftable, new_eviction,
6950 oftable->eviction_fields,
6951 oftable->n_eviction_fields);
6952 ovs_mutex_unlock(&ofproto_mutex);
6953 }
de7d3c07
SJ
6954
6955 if (tm->vacancy != OFPUTIL_TABLE_VACANCY_DEFAULT) {
6956 ovs_mutex_lock(&ofproto_mutex);
de7d3c07
SJ
6957 oftable->vacancy_down = tm->table_vacancy.vacancy_down;
6958 oftable->vacancy_up = tm->table_vacancy.vacancy_up;
6c6eedc5
SJ
6959 if (tm->vacancy == OFPUTIL_TABLE_VACANCY_OFF) {
6960 oftable->vacancy_event = 0;
6961 } else if (!oftable->vacancy_event) {
6962 uint8_t vacancy = oftable_vacancy(oftable);
6963 oftable->vacancy_event = (vacancy < oftable->vacancy_up
6964 ? OFPTR_VACANCY_UP
6965 : OFPTR_VACANCY_DOWN);
6966 }
de7d3c07
SJ
6967 ovs_mutex_unlock(&ofproto_mutex);
6968 }
6d328fa2
SH
6969}
6970
67761761
SH
6971static enum ofperr
6972table_mod(struct ofproto *ofproto, const struct ofputil_table_mod *tm)
6973{
3c1bb396 6974 if (!check_table_id(ofproto, tm->table_id)) {
67761761 6975 return OFPERR_OFPTMFC_BAD_TABLE;
82c22d34
BP
6976 }
6977
6978 /* Don't allow the eviction flags to be changed (except to the only fixed
6979 * value that OVS supports). OF1.4 says this is normal: "The
6980 * OFPTMPT_EVICTION property usually cannot be modified using a
6981 * OFP_TABLE_MOD request, because the eviction mechanism is switch
6982 * defined". */
6983 if (tm->eviction_flags != UINT32_MAX
6984 && tm->eviction_flags != OFPROTO_EVICTION_FLAGS) {
6985 return OFPERR_OFPTMFC_BAD_CONFIG;
6986 }
6987
6988 if (tm->table_id == OFPTT_ALL) {
6989 struct oftable *oftable;
6990 OFPROTO_FOR_EACH_TABLE (oftable, ofproto) {
6991 if (!(oftable->flags & (OFTABLE_HIDDEN | OFTABLE_READONLY))) {
de7d3c07 6992 table_mod__(oftable, tm);
3c1bb396 6993 }
3c1bb396 6994 }
82c22d34
BP
6995 } else {
6996 struct oftable *oftable = &ofproto->tables[tm->table_id];
6997 if (oftable->flags & OFTABLE_READONLY) {
6998 return OFPERR_OFPTMFC_EPERM;
6999 }
de7d3c07 7000 table_mod__(oftable, tm);
67761761 7001 }
82c22d34 7002
67761761
SH
7003 return 0;
7004}
7005
918f2b82
AZ
7006static enum ofperr
7007handle_table_mod(struct ofconn *ofconn, const struct ofp_header *oh)
7008{
67761761 7009 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
918f2b82
AZ
7010 struct ofputil_table_mod tm;
7011 enum ofperr error;
7012
7013 error = reject_slave_controller(ofconn);
7014 if (error) {
7015 return error;
7016 }
7017
7018 error = ofputil_decode_table_mod(oh, &tm);
7019 if (error) {
7020 return error;
7021 }
7022
67761761 7023 return table_mod(ofproto, &tm);
918f2b82
AZ
7024}
7025
777af88d 7026static enum ofperr
8be00367 7027ofproto_flow_mod_start(struct ofproto *ofproto, struct ofproto_flow_mod *ofm)
1f42be1c 7028 OVS_REQUIRES(ofproto_mutex)
777af88d 7029{
cc099268
JR
7030 /* Must check actions while holding ofproto_mutex to avoid a race. */
7031 enum ofperr error = ofproto_check_ofpacts(ofproto, ofm->fm.ofpacts,
7032 ofm->fm.ofpacts_len);
7033 if (error) {
7034 return error;
7035 }
7036
8be00367 7037 switch (ofm->fm.command) {
1f42be1c 7038 case OFPFC_ADD:
8be00367
JR
7039 return add_flow_start(ofproto, ofm);
7040 /* , &be->old_rules.stub[0],
7041 &be->new_rules.stub[0]); */
1f42be1c 7042 case OFPFC_MODIFY:
8be00367 7043 return modify_flows_start_loose(ofproto, ofm);
1f42be1c 7044 case OFPFC_MODIFY_STRICT:
8be00367 7045 return modify_flow_start_strict(ofproto, ofm);
1f42be1c 7046 case OFPFC_DELETE:
8be00367 7047 return delete_flows_start_loose(ofproto, ofm);
1f42be1c
JR
7048
7049 case OFPFC_DELETE_STRICT:
8be00367 7050 return delete_flow_start_strict(ofproto, ofm);
1f42be1c
JR
7051 }
7052
7053 return OFPERR_OFPFMFC_BAD_COMMAND;
7054}
7055
7056static void
8be00367 7057ofproto_flow_mod_revert(struct ofproto *ofproto, struct ofproto_flow_mod *ofm)
1f42be1c
JR
7058 OVS_REQUIRES(ofproto_mutex)
7059{
8be00367 7060 switch (ofm->fm.command) {
1f42be1c 7061 case OFPFC_ADD:
8be00367 7062 add_flow_revert(ofproto, ofm);
1f42be1c
JR
7063 break;
7064
7065 case OFPFC_MODIFY:
7066 case OFPFC_MODIFY_STRICT:
8be00367 7067 modify_flows_revert(ofproto, ofm);
1f42be1c
JR
7068 break;
7069
7070 case OFPFC_DELETE:
7071 case OFPFC_DELETE_STRICT:
8be00367 7072 delete_flows_revert(ofproto, ofm);
1f42be1c
JR
7073 break;
7074
7075 default:
7076 break;
7077 }
7078}
7079
7080static void
8be00367
JR
7081ofproto_flow_mod_finish(struct ofproto *ofproto,
7082 struct ofproto_flow_mod *ofm,
0a0d9385 7083 const struct openflow_mod_requester *req)
1f42be1c
JR
7084 OVS_REQUIRES(ofproto_mutex)
7085{
8be00367 7086 switch (ofm->fm.command) {
1f42be1c 7087 case OFPFC_ADD:
8be00367 7088 add_flow_finish(ofproto, ofm, req);
1f42be1c
JR
7089 break;
7090
7091 case OFPFC_MODIFY:
7092 case OFPFC_MODIFY_STRICT:
8be00367 7093 modify_flows_finish(ofproto, ofm, req);
1f42be1c
JR
7094 break;
7095
7096 case OFPFC_DELETE:
7097 case OFPFC_DELETE_STRICT:
8be00367 7098 delete_flows_finish(ofproto, ofm, req);
1f42be1c
JR
7099 break;
7100
7101 default:
7102 break;
7103 }
b0d38b2f
JR
7104
7105 if (req) {
7106 ofconn_report_flow_mod(req->ofconn, ofm->fm.command);
7107 }
1f42be1c
JR
7108}
7109
39c94593
JR
7110/* Commit phases (all while locking ofproto_mutex):
7111 *
7112 * 1. Begin: Gather resources and make changes visible in the next version.
7113 * - Mark affected rules for removal in the next version.
7114 * - Create new replacement rules, make visible in the next
7115 * version.
7116 * - Do not send any events or notifications.
7117 *
7118 * 2. Revert: Fail if any errors are found. After this point no errors are
7119 * possible. No visible changes were made, so rollback is minimal (remove
7120 * added invisible rules, restore visibility of rules marked for removal).
7121 *
1c38055d
JR
7122 * 3. Finish: Make the changes visible for lookups. Insert replacement rules to
7123 * the ofproto provider. Remove replaced and deleted rules from ofproto data
7124 * structures, and Schedule postponed removal of deleted rules from the
7125 * classifier. Send notifications, buffered packets, etc.
39c94593 7126 */
1f42be1c
JR
7127static enum ofperr
7128do_bundle_commit(struct ofconn *ofconn, uint32_t id, uint16_t flags)
7129{
7130 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
44e0c35d 7131 ovs_version_t version = ofproto->tables_version + 1;
1f42be1c
JR
7132 struct ofp_bundle *bundle;
7133 struct ofp_bundle_entry *be;
777af88d 7134 enum ofperr error;
1f42be1c
JR
7135
7136 bundle = ofconn_get_bundle(ofconn, id);
7137
7138 if (!bundle) {
7139 return OFPERR_OFPBFC_BAD_ID;
7140 }
7141 if (bundle->flags != flags) {
7142 error = OFPERR_OFPBFC_BAD_FLAGS;
7143 } else {
1c38055d
JR
7144 bool prev_is_port_mod = false;
7145
1f42be1c
JR
7146 error = 0;
7147 ovs_mutex_lock(&ofproto_mutex);
39c94593
JR
7148
7149 /* 1. Begin. */
1f42be1c
JR
7150 LIST_FOR_EACH (be, node, &bundle->msg_list) {
7151 if (be->type == OFPTYPE_PORT_MOD) {
1c38055d
JR
7152 /* Our port mods are not atomic. */
7153 if (flags & OFPBF_ATOMIC) {
7154 error = OFPERR_OFPBFC_MSG_FAILED;
7155 } else {
7156 prev_is_port_mod = true;
8be00367 7157 error = port_mod_start(ofconn, &be->opm.pm, &be->opm.port);
1c38055d 7158 }
25070e04
JR
7159 } else {
7160 /* Flow & group mods between port mods are applied as a single
7161 * version, but the versions are published only after we know
7162 * the commit is successful. */
1c38055d 7163 if (prev_is_port_mod) {
25070e04 7164 prev_is_port_mod = false;
8be00367 7165 ++version;
1c38055d 7166 }
25070e04
JR
7167 if (be->type == OFPTYPE_FLOW_MOD) {
7168 /* Store the version in which the changes should take
7169 * effect. */
7170 be->ofm.version = version;
7171 error = ofproto_flow_mod_start(ofproto, &be->ofm);
7172 } else if (be->type == OFPTYPE_GROUP_MOD) {
7173 /* Store the version in which the changes should take
7174 * effect. */
7175 be->ogm.version = version;
7176 error = ofproto_group_mod_start(ofproto, &be->ogm);
7177 } else {
7178 OVS_NOT_REACHED();
7179 }
1f42be1c
JR
7180 }
7181 if (error) {
7182 break;
7183 }
7184 }
1c38055d 7185
1f42be1c
JR
7186 if (error) {
7187 /* Send error referring to the original message. */
7188 if (error) {
7189 ofconn_send_error(ofconn, be->ofp_msg, error);
7190 error = OFPERR_OFPBFC_MSG_FAILED;
7191 }
7192
39c94593 7193 /* 2. Revert. Undo all the changes made above. */
1f42be1c
JR
7194 LIST_FOR_EACH_REVERSE_CONTINUE(be, node, &bundle->msg_list) {
7195 if (be->type == OFPTYPE_FLOW_MOD) {
8be00367 7196 ofproto_flow_mod_revert(ofproto, &be->ofm);
25070e04
JR
7197 } else if (be->type == OFPTYPE_GROUP_MOD) {
7198 ofproto_group_mod_revert(ofproto, &be->ogm);
1f42be1c 7199 }
25070e04 7200
1c38055d 7201 /* Nothing needs to be reverted for a port mod. */
1f42be1c
JR
7202 }
7203 } else {
39c94593 7204 /* 4. Finish. */
1f42be1c 7205 LIST_FOR_EACH (be, node, &bundle->msg_list) {
25070e04 7206 if (be->type == OFPTYPE_PORT_MOD) {
1c38055d
JR
7207 /* Perform the actual port mod. This is not atomic, i.e.,
7208 * the effects will be immediately seen by upcall
7209 * processing regardless of the lookup version. It should
7210 * be noted that port configuration changes can originate
7211 * also from OVSDB changes asynchronously to all upcall
7212 * processing. */
8be00367 7213 port_mod_finish(ofconn, &be->opm.pm, be->opm.port);
25070e04
JR
7214 } else {
7215 struct openflow_mod_requester req = { ofconn,
7216 be->ofp_msg };
7217 if (be->type == OFPTYPE_FLOW_MOD) {
7218 /* Bump the lookup version to the one of the current
7219 * message. This makes all the changes in the bundle
7220 * at this version visible to lookups at once. */
7221 if (ofproto->tables_version < be->ofm.version) {
7222 ofproto->tables_version = be->ofm.version;
7223 ofproto->ofproto_class->set_tables_version(
7224 ofproto, ofproto->tables_version);
7225 }
7226
7227 ofproto_flow_mod_finish(ofproto, &be->ofm, &req);
7228 } else if (be->type == OFPTYPE_GROUP_MOD) {
7229 /* Bump the lookup version to the one of the current
7230 * message. This makes all the changes in the bundle
7231 * at this version visible to lookups at once. */
7232 if (ofproto->tables_version < be->ogm.version) {
7233 ofproto->tables_version = be->ogm.version;
7234 ofproto->ofproto_class->set_tables_version(
7235 ofproto, ofproto->tables_version);
7236 }
7237
7238 ofproto_group_mod_finish(ofproto, &be->ogm, &req);
7239 }
1f42be1c
JR
7240 }
7241 }
7242 }
1c38055d 7243
1f42be1c
JR
7244 ofmonitor_flush(ofproto->connmgr);
7245 ovs_mutex_unlock(&ofproto_mutex);
7246
7247 run_rule_executes(ofproto);
7248 }
7249
7250 /* The bundle is discarded regardless the outcome. */
7251 ofp_bundle_remove__(ofconn, bundle, !error);
7252 return error;
7253}
7254
7255static enum ofperr
7256handle_bundle_control(struct ofconn *ofconn, const struct ofp_header *oh)
7257{
777af88d 7258 struct ofputil_bundle_ctrl_msg bctrl;
777af88d 7259 struct ofputil_bundle_ctrl_msg reply;
1f42be1c
JR
7260 struct ofpbuf *buf;
7261 enum ofperr error;
777af88d 7262
ba9d374a
JR
7263 error = reject_slave_controller(ofconn);
7264 if (error) {
7265 return error;
7266 }
7267
777af88d
AC
7268 error = ofputil_decode_bundle_ctrl(oh, &bctrl);
7269 if (error) {
7270 return error;
7271 }
7272 reply.flags = 0;
7273 reply.bundle_id = bctrl.bundle_id;
7274
7275 switch (bctrl.type) {
7276 case OFPBCT_OPEN_REQUEST:
7277 error = ofp_bundle_open(ofconn, bctrl.bundle_id, bctrl.flags);
7278 reply.type = OFPBCT_OPEN_REPLY;
7279 break;
7280 case OFPBCT_CLOSE_REQUEST:
7281 error = ofp_bundle_close(ofconn, bctrl.bundle_id, bctrl.flags);
ea53e3a8 7282 reply.type = OFPBCT_CLOSE_REPLY;
777af88d
AC
7283 break;
7284 case OFPBCT_COMMIT_REQUEST:
1f42be1c 7285 error = do_bundle_commit(ofconn, bctrl.bundle_id, bctrl.flags);
777af88d
AC
7286 reply.type = OFPBCT_COMMIT_REPLY;
7287 break;
7288 case OFPBCT_DISCARD_REQUEST:
7289 error = ofp_bundle_discard(ofconn, bctrl.bundle_id);
7290 reply.type = OFPBCT_DISCARD_REPLY;
7291 break;
7292
7293 case OFPBCT_OPEN_REPLY:
7294 case OFPBCT_CLOSE_REPLY:
7295 case OFPBCT_COMMIT_REPLY:
7296 case OFPBCT_DISCARD_REPLY:
7297 return OFPERR_OFPBFC_BAD_TYPE;
7298 break;
7299 }
7300
7301 if (!error) {
7302 buf = ofputil_encode_bundle_ctrl_reply(oh, &reply);
7303 ofconn_send_reply(ofconn, buf);
7304 }
7305 return error;
7306}
7307
777af88d
AC
7308static enum ofperr
7309handle_bundle_add(struct ofconn *ofconn, const struct ofp_header *oh)
7310{
7ac27a04 7311 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
777af88d
AC
7312 enum ofperr error;
7313 struct ofputil_bundle_add_msg badd;
7ac27a04
JR
7314 struct ofp_bundle_entry *bmsg;
7315 enum ofptype type;
777af88d 7316
ba9d374a
JR
7317 error = reject_slave_controller(ofconn);
7318 if (error) {
7319 return error;
7320 }
7321
7ac27a04 7322 error = ofputil_decode_bundle_add(oh, &badd, &type);
777af88d
AC
7323 if (error) {
7324 return error;
7325 }
7326
1f42be1c 7327 bmsg = ofp_bundle_entry_alloc(type, badd.msg);
7ac27a04
JR
7328
7329 if (type == OFPTYPE_PORT_MOD) {
8be00367 7330 error = ofputil_decode_port_mod(badd.msg, &bmsg->opm.pm, false);
7ac27a04
JR
7331 } else if (type == OFPTYPE_FLOW_MOD) {
7332 struct ofpbuf ofpacts;
7333 uint64_t ofpacts_stub[1024 / 8];
7334
7335 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
8be00367 7336 error = ofputil_decode_flow_mod(&bmsg->ofm.fm, badd.msg,
7ac27a04
JR
7337 ofconn_get_protocol(ofconn),
7338 &ofpacts,
7339 u16_to_ofp(ofproto->max_ports),
7340 ofproto->n_tables);
7341 /* Move actions to heap. */
8be00367 7342 bmsg->ofm.fm.ofpacts = ofpbuf_steal_data(&ofpacts);
25070e04
JR
7343 } else if (type == OFPTYPE_GROUP_MOD) {
7344 error = ofputil_decode_group_mod(badd.msg, &bmsg->ogm.gm);
7ac27a04
JR
7345 } else {
7346 OVS_NOT_REACHED();
7347 }
7348
7349 if (!error) {
7350 error = ofp_bundle_add_message(ofconn, badd.bundle_id, badd.flags,
7351 bmsg);
7352 }
7353
7354 if (error) {
7355 ofp_bundle_entry_free(bmsg);
7356 }
7357
7358 return error;
777af88d
AC
7359}
7360
6159c531 7361static enum ofperr
4e548ad9 7362handle_tlv_table_mod(struct ofconn *ofconn, const struct ofp_header *oh)
6159c531 7363{
4e548ad9 7364 struct ofputil_tlv_table_mod ttm;
6159c531
JG
7365 enum ofperr error;
7366
7367 error = reject_slave_controller(ofconn);
7368 if (error) {
7369 return error;
7370 }
7371
4e548ad9 7372 error = ofputil_decode_tlv_table_mod(oh, &ttm);
6159c531
JG
7373 if (error) {
7374 return error;
7375 }
7376
4e548ad9 7377 error = tun_metadata_table_mod(&ttm);
9558d2a5 7378
4e548ad9 7379 ofputil_uninit_tlv_table(&ttm.mappings);
9558d2a5 7380 return error;
6159c531
JG
7381}
7382
7383static enum ofperr
4e548ad9 7384handle_tlv_table_request(struct ofconn *ofconn, const struct ofp_header *oh)
6159c531 7385{
4e548ad9 7386 struct ofputil_tlv_table_reply ttr;
9558d2a5
JG
7387 struct ofpbuf *b;
7388
4e548ad9
ML
7389 tun_metadata_table_request(&ttr);
7390 b = ofputil_encode_tlv_table_reply(oh, &ttr);
7391 ofputil_uninit_tlv_table(&ttr.mappings);
9558d2a5
JG
7392
7393 ofconn_send_reply(ofconn, b);
7394 return 0;
6159c531
JG
7395}
7396
90bf1e07 7397static enum ofperr
d1e2cf21 7398handle_openflow__(struct ofconn *ofconn, const struct ofpbuf *msg)
15aaf599 7399 OVS_EXCLUDED(ofproto_mutex)
064af421 7400{
6fd6ed71 7401 const struct ofp_header *oh = msg->data;
982697a4 7402 enum ofptype type;
90bf1e07 7403 enum ofperr error;
064af421 7404
982697a4 7405 error = ofptype_decode(&type, oh);
d1e2cf21
BP
7406 if (error) {
7407 return error;
7408 }
ff3c2c63
YT
7409 if (oh->version >= OFP13_VERSION && ofpmsg_is_stat_request(oh)
7410 && ofpmp_more(oh)) {
7411 /* We have no buffer implementation for multipart requests.
7412 * Report overflow for requests which consists of multiple
7413 * messages. */
7414 return OFPERR_OFPBRC_MULTIPART_BUFFER_OVERFLOW;
7415 }
064af421 7416
982697a4 7417 switch (type) {
d1e2cf21 7418 /* OpenFlow requests. */
982697a4 7419 case OFPTYPE_ECHO_REQUEST:
d1e2cf21 7420 return handle_echo_request(ofconn, oh);
064af421 7421
982697a4 7422 case OFPTYPE_FEATURES_REQUEST:
d1e2cf21 7423 return handle_features_request(ofconn, oh);
064af421 7424
982697a4 7425 case OFPTYPE_GET_CONFIG_REQUEST:
d1e2cf21 7426 return handle_get_config_request(ofconn, oh);
064af421 7427
982697a4
BP
7428 case OFPTYPE_SET_CONFIG:
7429 return handle_set_config(ofconn, oh);
064af421 7430
982697a4
BP
7431 case OFPTYPE_PACKET_OUT:
7432 return handle_packet_out(ofconn, oh);
064af421 7433
982697a4 7434 case OFPTYPE_PORT_MOD:
d1e2cf21 7435 return handle_port_mod(ofconn, oh);
064af421 7436
982697a4 7437 case OFPTYPE_FLOW_MOD:
2e4f5fcf 7438 return handle_flow_mod(ofconn, oh);
064af421 7439
7395c052
NZ
7440 case OFPTYPE_GROUP_MOD:
7441 return handle_group_mod(ofconn, oh);
7442
918f2b82
AZ
7443 case OFPTYPE_TABLE_MOD:
7444 return handle_table_mod(ofconn, oh);
7445
9cae45dc
JR
7446 case OFPTYPE_METER_MOD:
7447 return handle_meter_mod(ofconn, oh);
7448
982697a4 7449 case OFPTYPE_BARRIER_REQUEST:
d1e2cf21 7450 return handle_barrier_request(ofconn, oh);
064af421 7451
6ea4776b
JR
7452 case OFPTYPE_ROLE_REQUEST:
7453 return handle_role_request(ofconn, oh);
7454
d1e2cf21 7455 /* OpenFlow replies. */
982697a4 7456 case OFPTYPE_ECHO_REPLY:
d1e2cf21 7457 return 0;
246e61ea 7458
d1e2cf21 7459 /* Nicira extension requests. */
982697a4 7460 case OFPTYPE_FLOW_MOD_TABLE_ID:
6c1491fb 7461 return handle_nxt_flow_mod_table_id(ofconn, oh);
d1e2cf21 7462
982697a4 7463 case OFPTYPE_SET_FLOW_FORMAT:
d1e2cf21
BP
7464 return handle_nxt_set_flow_format(ofconn, oh);
7465
982697a4 7466 case OFPTYPE_SET_PACKET_IN_FORMAT:
54834960
EJ
7467 return handle_nxt_set_packet_in_format(ofconn, oh);
7468
982697a4 7469 case OFPTYPE_SET_CONTROLLER_ID:
a7349929
BP
7470 return handle_nxt_set_controller_id(ofconn, oh);
7471
982697a4 7472 case OFPTYPE_FLOW_AGE:
f27f2134
BP
7473 /* Nothing to do. */
7474 return 0;
7475
982697a4 7476 case OFPTYPE_FLOW_MONITOR_CANCEL:
2b07c8b1
BP
7477 return handle_flow_monitor_cancel(ofconn, oh);
7478
982697a4 7479 case OFPTYPE_SET_ASYNC_CONFIG:
80d5aefd
BP
7480 return handle_nxt_set_async_config(ofconn, oh);
7481
c423b3b3
AC
7482 case OFPTYPE_GET_ASYNC_REQUEST:
7483 return handle_nxt_get_async_request(ofconn, oh);
7484
77ab5fd2
BP
7485 case OFPTYPE_NXT_RESUME:
7486 return handle_nxt_resume(ofconn, oh);
7487
349adfb2 7488 /* Statistics requests. */
982697a4
BP
7489 case OFPTYPE_DESC_STATS_REQUEST:
7490 return handle_desc_stats_request(ofconn, oh);
7491
7492 case OFPTYPE_FLOW_STATS_REQUEST:
7493 return handle_flow_stats_request(ofconn, oh);
7494
7495 case OFPTYPE_AGGREGATE_STATS_REQUEST:
7496 return handle_aggregate_stats_request(ofconn, oh);
7497
7498 case OFPTYPE_TABLE_STATS_REQUEST:
7499 return handle_table_stats_request(ofconn, oh);
7500
3c4e10fb
BP
7501 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
7502 return handle_table_features_request(ofconn, oh);
7503
03c72922
BP
7504 case OFPTYPE_TABLE_DESC_REQUEST:
7505 return handle_table_desc_request(ofconn, oh);
7506
982697a4
BP
7507 case OFPTYPE_PORT_STATS_REQUEST:
7508 return handle_port_stats_request(ofconn, oh);
7509
7510 case OFPTYPE_QUEUE_STATS_REQUEST:
7511 return handle_queue_stats_request(ofconn, oh);
7512
7513 case OFPTYPE_PORT_DESC_STATS_REQUEST:
7514 return handle_port_desc_stats_request(ofconn, oh);
7515
7516 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
7517 return handle_flow_monitor_request(ofconn, oh);
7518
261bd854
BP
7519 case OFPTYPE_METER_STATS_REQUEST:
7520 case OFPTYPE_METER_CONFIG_STATS_REQUEST:
9cae45dc
JR
7521 return handle_meter_request(ofconn, oh, type);
7522
261bd854 7523 case OFPTYPE_METER_FEATURES_STATS_REQUEST:
9cae45dc
JR
7524 return handle_meter_features_request(ofconn, oh);
7525
261bd854 7526 case OFPTYPE_GROUP_STATS_REQUEST:
7395c052
NZ
7527 return handle_group_stats_request(ofconn, oh);
7528
261bd854 7529 case OFPTYPE_GROUP_DESC_STATS_REQUEST:
7395c052
NZ
7530 return handle_group_desc_stats_request(ofconn, oh);
7531
261bd854 7532 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
7395c052
NZ
7533 return handle_group_features_stats_request(ofconn, oh);
7534
7395c052 7535 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
e8f9a7bb 7536 return handle_queue_get_config_request(ofconn, oh);
2e1ae200 7537
777af88d
AC
7538 case OFPTYPE_BUNDLE_CONTROL:
7539 return handle_bundle_control(ofconn, oh);
7540
7541 case OFPTYPE_BUNDLE_ADD_MESSAGE:
7542 return handle_bundle_add(ofconn, oh);
7543
4e548ad9
ML
7544 case OFPTYPE_NXT_TLV_TABLE_MOD:
7545 return handle_tlv_table_mod(ofconn, oh);
6159c531 7546
4e548ad9
ML
7547 case OFPTYPE_NXT_TLV_TABLE_REQUEST:
7548 return handle_tlv_table_request(ofconn, oh);
6159c531 7549
fb8f22c1
BY
7550 case OFPTYPE_IPFIX_BRIDGE_STATS_REQUEST:
7551 return handle_ipfix_bridge_stats_request(ofconn, oh);
7552
7553 case OFPTYPE_IPFIX_FLOW_STATS_REQUEST:
7554 return handle_ipfix_flow_stats_request(ofconn, oh);
7555
982697a4
BP
7556 case OFPTYPE_HELLO:
7557 case OFPTYPE_ERROR:
7558 case OFPTYPE_FEATURES_REPLY:
7559 case OFPTYPE_GET_CONFIG_REPLY:
7560 case OFPTYPE_PACKET_IN:
7561 case OFPTYPE_FLOW_REMOVED:
7562 case OFPTYPE_PORT_STATUS:
7563 case OFPTYPE_BARRIER_REPLY:
c545d38d 7564 case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
982697a4
BP
7565 case OFPTYPE_DESC_STATS_REPLY:
7566 case OFPTYPE_FLOW_STATS_REPLY:
7567 case OFPTYPE_QUEUE_STATS_REPLY:
7568 case OFPTYPE_PORT_STATS_REPLY:
7569 case OFPTYPE_TABLE_STATS_REPLY:
7570 case OFPTYPE_AGGREGATE_STATS_REPLY:
7571 case OFPTYPE_PORT_DESC_STATS_REPLY:
7572 case OFPTYPE_ROLE_REPLY:
7573 case OFPTYPE_FLOW_MONITOR_PAUSED:
7574 case OFPTYPE_FLOW_MONITOR_RESUMED:
7575 case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
2e1ae200 7576 case OFPTYPE_GET_ASYNC_REPLY:
261bd854
BP
7577 case OFPTYPE_GROUP_STATS_REPLY:
7578 case OFPTYPE_GROUP_DESC_STATS_REPLY:
7579 case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
7580 case OFPTYPE_METER_STATS_REPLY:
7581 case OFPTYPE_METER_CONFIG_STATS_REPLY:
7582 case OFPTYPE_METER_FEATURES_STATS_REPLY:
7583 case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
03c72922 7584 case OFPTYPE_TABLE_DESC_REPLY:
252f3411 7585 case OFPTYPE_ROLE_STATUS:
3c35db62 7586 case OFPTYPE_REQUESTFORWARD:
6c6eedc5 7587 case OFPTYPE_TABLE_STATUS:
4e548ad9 7588 case OFPTYPE_NXT_TLV_TABLE_REPLY:
fb8f22c1
BY
7589 case OFPTYPE_IPFIX_BRIDGE_STATS_REPLY:
7590 case OFPTYPE_IPFIX_FLOW_STATS_REPLY:
064af421 7591 default:
76ec08e0
YT
7592 if (ofpmsg_is_stat_request(oh)) {
7593 return OFPERR_OFPBRC_BAD_STAT;
7594 } else {
7595 return OFPERR_OFPBRC_BAD_TYPE;
7596 }
064af421 7597 }
d1e2cf21 7598}
064af421 7599
b20f4073 7600static void
e03248b7 7601handle_openflow(struct ofconn *ofconn, const struct ofpbuf *ofp_msg)
15aaf599 7602 OVS_EXCLUDED(ofproto_mutex)
d1e2cf21 7603{
d51c8b71
JR
7604 enum ofperr error = handle_openflow__(ofconn, ofp_msg);
7605
b20f4073 7606 if (error) {
6fd6ed71 7607 ofconn_send_error(ofconn, ofp_msg->data, error);
064af421 7608 }
d1e2cf21 7609 COVERAGE_INC(ofproto_recv_openflow);
7ee20df1
BP
7610}
7611\f
7612/* Asynchronous operations. */
7613
c84d8691 7614static void
0a0d9385
JR
7615send_buffered_packet(const struct openflow_mod_requester *req,
7616 uint32_t buffer_id, struct rule *rule)
15aaf599 7617 OVS_REQUIRES(ofproto_mutex)
7ee20df1 7618{
c84d8691
JR
7619 if (req && req->ofconn && buffer_id != UINT32_MAX) {
7620 struct ofproto *ofproto = ofconn_get_ofproto(req->ofconn);
cf62fa4c 7621 struct dp_packet *packet;
834fe5cb 7622 ofp_port_t in_port;
c84d8691 7623 enum ofperr error;
b277c7cc 7624
c84d8691
JR
7625 error = ofconn_pktbuf_retrieve(req->ofconn, buffer_id, &packet,
7626 &in_port);
834fe5cb
BP
7627 if (packet) {
7628 struct rule_execute *re;
b277c7cc 7629
834fe5cb 7630 ofproto_rule_ref(rule);
b277c7cc 7631
834fe5cb
BP
7632 re = xmalloc(sizeof *re);
7633 re->rule = rule;
7634 re->in_port = in_port;
7635 re->packet = packet;
b277c7cc 7636
834fe5cb
BP
7637 if (!guarded_list_push_back(&ofproto->rule_executes,
7638 &re->list_node, 1024)) {
a2143702 7639 ofproto_rule_unref(rule);
cf62fa4c 7640 dp_packet_delete(re->packet);
834fe5cb 7641 free(re);
e615b0a3 7642 }
c84d8691
JR
7643 } else {
7644 ofconn_send_error(req->ofconn, req->request, error);
af822017 7645 }
7ee20df1 7646 }
7ee20df1 7647}
064af421 7648\f
064af421 7649static uint64_t
fa60c019 7650pick_datapath_id(const struct ofproto *ofproto)
064af421 7651{
fa60c019 7652 const struct ofport *port;
064af421 7653
abe529af 7654 port = ofproto_get_port(ofproto, OFPP_LOCAL);
fa60c019 7655 if (port) {
74ff3298 7656 struct eth_addr ea;
fa60c019
BP
7657 int error;
7658
74ff3298 7659 error = netdev_get_etheraddr(port->netdev, &ea);
064af421
BP
7660 if (!error) {
7661 return eth_addr_to_uint64(ea);
7662 }
fbfa2911
BP
7663 VLOG_WARN("%s: could not get MAC address for %s (%s)",
7664 ofproto->name, netdev_get_name(port->netdev),
10a89ef0 7665 ovs_strerror(error));
064af421 7666 }
fa60c019 7667 return ofproto->fallback_dpid;
064af421
BP
7668}
7669
7670static uint64_t
7671pick_fallback_dpid(void)
7672{
74ff3298
JR
7673 struct eth_addr ea;
7674 eth_addr_nicira_random(&ea);
064af421
BP
7675 return eth_addr_to_uint64(ea);
7676}
7677\f
254750ce
BP
7678/* Table overflow policy. */
7679
ad3efdcb
EJ
7680/* Chooses and updates 'rulep' with a rule to evict from 'table'. Sets 'rulep'
7681 * to NULL if the table is not configured to evict rules or if the table
7682 * contains no evictable rules. (Rules with a readlock on their evict rwlock,
7683 * or with no timeouts are not evictable.) */
7684static bool
7685choose_rule_to_evict(struct oftable *table, struct rule **rulep)
15aaf599 7686 OVS_REQUIRES(ofproto_mutex)
254750ce
BP
7687{
7688 struct eviction_group *evg;
7689
ad3efdcb 7690 *rulep = NULL;
82c22d34 7691 if (!table->eviction) {
ad3efdcb 7692 return false;
254750ce
BP
7693 }
7694
7695 /* In the common case, the outer and inner loops here will each be entered
7696 * exactly once:
7697 *
7698 * - The inner loop normally "return"s in its first iteration. If the
7699 * eviction group has any evictable rules, then it always returns in
7700 * some iteration.
7701 *
7702 * - The outer loop only iterates more than once if the largest eviction
7703 * group has no evictable rules.
7704 *
7705 * - The outer loop can exit only if table's 'max_flows' is all filled up
afe2143d 7706 * by unevictable rules. */
254750ce
BP
7707 HEAP_FOR_EACH (evg, size_node, &table->eviction_groups_by_size) {
7708 struct rule *rule;
7709
7710 HEAP_FOR_EACH (rule, evg_node, &evg->rules) {
15aaf599
BP
7711 *rulep = rule;
7712 return true;
254750ce
BP
7713 }
7714 }
7715
ad3efdcb 7716 return false;
254750ce 7717}
254750ce
BP
7718\f
7719/* Eviction groups. */
7720
7721/* Returns the priority to use for an eviction_group that contains 'n_rules'
7722 * rules. The priority contains low-order random bits to ensure that eviction
7723 * groups with the same number of rules are prioritized randomly. */
7724static uint32_t
7725eviction_group_priority(size_t n_rules)
7726{
7727 uint16_t size = MIN(UINT16_MAX, n_rules);
7728 return (size << 16) | random_uint16();
7729}
7730
7731/* Updates 'evg', an eviction_group within 'table', following a change that
7732 * adds or removes rules in 'evg'. */
7733static void
7734eviction_group_resized(struct oftable *table, struct eviction_group *evg)
15aaf599 7735 OVS_REQUIRES(ofproto_mutex)
254750ce
BP
7736{
7737 heap_change(&table->eviction_groups_by_size, &evg->size_node,
7738 eviction_group_priority(heap_count(&evg->rules)));
7739}
7740
7741/* Destroys 'evg', an eviction_group within 'table':
7742 *
7743 * - Removes all the rules, if any, from 'evg'. (It doesn't destroy the
7744 * rules themselves, just removes them from the eviction group.)
7745 *
7746 * - Removes 'evg' from 'table'.
7747 *
7748 * - Frees 'evg'. */
7749static void
7750eviction_group_destroy(struct oftable *table, struct eviction_group *evg)
15aaf599 7751 OVS_REQUIRES(ofproto_mutex)
254750ce
BP
7752{
7753 while (!heap_is_empty(&evg->rules)) {
7754 struct rule *rule;
7755
7756 rule = CONTAINER_OF(heap_pop(&evg->rules), struct rule, evg_node);
7757 rule->eviction_group = NULL;
7758 }
7759 hmap_remove(&table->eviction_groups_by_id, &evg->id_node);
7760 heap_remove(&table->eviction_groups_by_size, &evg->size_node);
7761 heap_destroy(&evg->rules);
7762 free(evg);
7763}
7764
7765/* Removes 'rule' from its eviction group, if any. */
7766static void
7767eviction_group_remove_rule(struct rule *rule)
15aaf599 7768 OVS_REQUIRES(ofproto_mutex)
254750ce
BP
7769{
7770 if (rule->eviction_group) {
7771 struct oftable *table = &rule->ofproto->tables[rule->table_id];
7772 struct eviction_group *evg = rule->eviction_group;
7773
7774 rule->eviction_group = NULL;
7775 heap_remove(&evg->rules, &rule->evg_node);
7776 if (heap_is_empty(&evg->rules)) {
7777 eviction_group_destroy(table, evg);
7778 } else {
7779 eviction_group_resized(table, evg);
7780 }
7781 }
7782}
7783
7784/* Hashes the 'rule''s values for the eviction_fields of 'rule''s table, and
7785 * returns the hash value. */
7786static uint32_t
7787eviction_group_hash_rule(struct rule *rule)
15aaf599 7788 OVS_REQUIRES(ofproto_mutex)
254750ce
BP
7789{
7790 struct oftable *table = &rule->ofproto->tables[rule->table_id];
7791 const struct mf_subfield *sf;
5cb7a798 7792 struct flow flow;
254750ce
BP
7793 uint32_t hash;
7794
7795 hash = table->eviction_group_id_basis;
8fd47924 7796 miniflow_expand(rule->cr.match.flow, &flow);
254750ce
BP
7797 for (sf = table->eviction_fields;
7798 sf < &table->eviction_fields[table->n_eviction_fields];
7799 sf++)
7800 {
aff49b8c 7801 if (mf_are_prereqs_ok(sf->field, &flow, NULL)) {
254750ce
BP
7802 union mf_value value;
7803
5cb7a798 7804 mf_get_value(sf->field, &flow, &value);
254750ce
BP
7805 if (sf->ofs) {
7806 bitwise_zero(&value, sf->field->n_bytes, 0, sf->ofs);
7807 }
7808 if (sf->ofs + sf->n_bits < sf->field->n_bytes * 8) {
7809 unsigned int start = sf->ofs + sf->n_bits;
7810 bitwise_zero(&value, sf->field->n_bytes, start,
7811 sf->field->n_bytes * 8 - start);
7812 }
7813 hash = hash_bytes(&value, sf->field->n_bytes, hash);
7814 } else {
7815 hash = hash_int(hash, 0);
7816 }
7817 }
7818
7819 return hash;
7820}
7821
7822/* Returns an eviction group within 'table' with the given 'id', creating one
7823 * if necessary. */
7824static struct eviction_group *
7825eviction_group_find(struct oftable *table, uint32_t id)
15aaf599 7826 OVS_REQUIRES(ofproto_mutex)
254750ce
BP
7827{
7828 struct eviction_group *evg;
7829
7830 HMAP_FOR_EACH_WITH_HASH (evg, id_node, id, &table->eviction_groups_by_id) {
7831 return evg;
7832 }
7833
7834 evg = xmalloc(sizeof *evg);
7835 hmap_insert(&table->eviction_groups_by_id, &evg->id_node, id);
7836 heap_insert(&table->eviction_groups_by_size, &evg->size_node,
7837 eviction_group_priority(0));
7838 heap_init(&evg->rules);
7839
7840 return evg;
7841}
7842
7843/* Returns an eviction priority for 'rule'. The return value should be
f70b94de
BP
7844 * interpreted so that higher priorities make a rule a more attractive
7845 * candidate for eviction. */
7846static uint64_t
dc437090 7847rule_eviction_priority(struct ofproto *ofproto, struct rule *rule)
15aaf599 7848 OVS_REQUIRES(ofproto_mutex)
254750ce 7849{
f70b94de
BP
7850 /* Calculate absolute time when this flow will expire. If it will never
7851 * expire, then return 0 to make it unevictable. */
dc437090 7852 long long int expiration = LLONG_MAX;
dc437090 7853 if (rule->hard_timeout) {
f70b94de
BP
7854 /* 'modified' needs protection even when we hold 'ofproto_mutex'. */
7855 ovs_mutex_lock(&rule->mutex);
7856 long long int modified = rule->modified;
7857 ovs_mutex_unlock(&rule->mutex);
7858
dc437090
JR
7859 expiration = modified + rule->hard_timeout * 1000;
7860 }
7861 if (rule->idle_timeout) {
7862 uint64_t packets, bytes;
7863 long long int used;
7864 long long int idle_expiration;
7865
7866 ofproto->ofproto_class->rule_get_stats(rule, &packets, &bytes, &used);
7867 idle_expiration = used + rule->idle_timeout * 1000;
7868 expiration = MIN(expiration, idle_expiration);
7869 }
254750ce
BP
7870 if (expiration == LLONG_MAX) {
7871 return 0;
7872 }
7873
7874 /* Calculate the time of expiration as a number of (approximate) seconds
7875 * after program startup.
7876 *
7877 * This should work OK for program runs that last UINT32_MAX seconds or
7878 * less. Therefore, please restart OVS at least once every 136 years. */
f70b94de 7879 uint32_t expiration_ofs = (expiration >> 10) - (time_boot_msec() >> 10);
254750ce 7880
f70b94de
BP
7881 /* Combine expiration time with OpenFlow "importance" to form a single
7882 * priority value. We want flows with relatively low "importance" to be
7883 * evicted before even considering expiration time, so put "importance" in
7884 * the most significant bits and expiration time in the least significant
7885 * bits.
7886 *
7887 * Small 'priority' should be evicted before those with large 'priority'.
7888 * The caller expects the opposite convention (a large return value being
7889 * more attractive for eviction) so we invert it before returning. */
7890 uint64_t priority = ((uint64_t) rule->importance << 32) + expiration_ofs;
7891 return UINT64_MAX - priority;
254750ce
BP
7892}
7893
7894/* Adds 'rule' to an appropriate eviction group for its oftable's
7895 * configuration. Does nothing if 'rule''s oftable doesn't have eviction
7896 * enabled, or if 'rule' is a permanent rule (one that will never expire on its
7897 * own).
7898 *
7899 * The caller must ensure that 'rule' is not already in an eviction group. */
7900static void
7901eviction_group_add_rule(struct rule *rule)
15aaf599 7902 OVS_REQUIRES(ofproto_mutex)
254750ce
BP
7903{
7904 struct ofproto *ofproto = rule->ofproto;
7905 struct oftable *table = &ofproto->tables[rule->table_id];
a3779dbc 7906 bool has_timeout;
254750ce 7907
dc437090
JR
7908 /* Timeouts may be modified only when holding 'ofproto_mutex'. We have it
7909 * so no additional protection is needed. */
a3779dbc 7910 has_timeout = rule->hard_timeout || rule->idle_timeout;
a3779dbc 7911
82c22d34 7912 if (table->eviction && has_timeout) {
254750ce
BP
7913 struct eviction_group *evg;
7914
7915 evg = eviction_group_find(table, eviction_group_hash_rule(rule));
7916
7917 rule->eviction_group = evg;
7918 heap_insert(&evg->rules, &rule->evg_node,
dc437090 7919 rule_eviction_priority(ofproto, rule));
254750ce
BP
7920 eviction_group_resized(table, evg);
7921 }
7922}
7923\f
d0918789
BP
7924/* oftables. */
7925
7926/* Initializes 'table'. */
7927static void
7928oftable_init(struct oftable *table)
7929{
5c67e4af 7930 memset(table, 0, sizeof *table);
d70e8c28 7931 classifier_init(&table->cls, flow_segment_u64s);
ec1c5c7e 7932 table->max_flows = UINT_MAX;
d79e3d70 7933 table->n_flows = 0;
82c22d34
BP
7934 hmap_init(&table->eviction_groups_by_id);
7935 heap_init(&table->eviction_groups_by_size);
3c1bb396 7936 atomic_init(&table->miss_config, OFPUTIL_TABLE_MISS_DEFAULT);
f017d986 7937
f017d986
JR
7938 classifier_set_prefix_fields(&table->cls, default_prefix_fields,
7939 ARRAY_SIZE(default_prefix_fields));
d611866c
SH
7940
7941 atomic_init(&table->n_matched, 0);
7942 atomic_init(&table->n_missed, 0);
d0918789
BP
7943}
7944
254750ce 7945/* Destroys 'table', including its classifier and eviction groups.
d0918789
BP
7946 *
7947 * The caller is responsible for freeing 'table' itself. */
7948static void
7949oftable_destroy(struct oftable *table)
7950{
cb22974d 7951 ovs_assert(classifier_is_empty(&table->cls));
82c22d34 7952
7394b8fb 7953 ovs_mutex_lock(&ofproto_mutex);
82c22d34 7954 oftable_configure_eviction(table, 0, NULL, 0);
7394b8fb 7955 ovs_mutex_unlock(&ofproto_mutex);
82c22d34
BP
7956
7957 hmap_destroy(&table->eviction_groups_by_id);
7958 heap_destroy(&table->eviction_groups_by_size);
d0918789 7959 classifier_destroy(&table->cls);
254750ce
BP
7960 free(table->name);
7961}
7962
7963/* Changes the name of 'table' to 'name'. If 'name' is NULL or the empty
7964 * string, then 'table' will use its default name.
7965 *
7966 * This only affects the name exposed for a table exposed through the OpenFlow
7967 * OFPST_TABLE (as printed by "ovs-ofctl dump-tables"). */
7968static void
7969oftable_set_name(struct oftable *table, const char *name)
7970{
7971 if (name && name[0]) {
7972 int len = strnlen(name, OFP_MAX_TABLE_NAME_LEN);
7973 if (!table->name || strncmp(name, table->name, len)) {
7974 free(table->name);
7975 table->name = xmemdup0(name, len);
7976 }
7977 } else {
7978 free(table->name);
7979 table->name = NULL;
7980 }
7981}
7982
254750ce
BP
7983/* oftables support a choice of two policies when adding a rule would cause the
7984 * number of flows in the table to exceed the configured maximum number: either
7985 * they can refuse to add the new flow or they can evict some existing flow.
7986 * This function configures the latter policy on 'table', with fairness based
7987 * on the values of the 'n_fields' fields specified in 'fields'. (Specifying
7988 * 'n_fields' as 0 disables fairness.) */
7989static void
82c22d34
BP
7990oftable_configure_eviction(struct oftable *table, unsigned int eviction,
7991 const struct mf_subfield *fields, size_t n_fields)
15aaf599 7992 OVS_REQUIRES(ofproto_mutex)
254750ce 7993{
254750ce
BP
7994 struct rule *rule;
7995
82c22d34 7996 if ((table->eviction != 0) == (eviction != 0)
254750ce
BP
7997 && n_fields == table->n_eviction_fields
7998 && (!n_fields
7999 || !memcmp(fields, table->eviction_fields,
8000 n_fields * sizeof *fields))) {
82c22d34
BP
8001 /* The set of eviction fields did not change. If 'eviction' changed,
8002 * it remains nonzero, so that we can just update table->eviction
8003 * without fussing with the eviction groups. */
8004 table->eviction = eviction;
254750ce
BP
8005 return;
8006 }
8007
82c22d34
BP
8008 /* Destroy existing eviction groups, then destroy and recreate data
8009 * structures to recover memory. */
8010 struct eviction_group *evg, *next;
8011 HMAP_FOR_EACH_SAFE (evg, next, id_node, &table->eviction_groups_by_id) {
8012 eviction_group_destroy(table, evg);
8013 }
8014 hmap_destroy(&table->eviction_groups_by_id);
254750ce 8015 hmap_init(&table->eviction_groups_by_id);
82c22d34 8016 heap_destroy(&table->eviction_groups_by_size);
254750ce
BP
8017 heap_init(&table->eviction_groups_by_size);
8018
82c22d34
BP
8019 /* Replace eviction groups by the new ones, if there is a change. Free the
8020 * old fields only after allocating the new ones, because 'fields ==
8021 * table->eviction_fields' is possible. */
8022 struct mf_subfield *old_fields = table->eviction_fields;
8023 table->n_eviction_fields = n_fields;
8024 table->eviction_fields = (fields
8025 ? xmemdup(fields, n_fields * sizeof *fields)
8026 : NULL);
8027 free(old_fields);
8028
8029 /* Add the new eviction groups, if enabled. */
8030 table->eviction = eviction;
8031 if (table->eviction) {
8032 table->eviction_group_id_basis = random_uint32();
8033 CLS_FOR_EACH (rule, cr, &table->cls) {
8034 eviction_group_add_rule(rule);
8035 }
254750ce 8036 }
d0918789
BP
8037}
8038
bc5e6a91
JR
8039/* Inserts 'rule' from the ofproto data structures BEFORE caller has inserted
8040 * it to the classifier. */
8041static void
8042ofproto_rule_insert__(struct ofproto *ofproto, struct rule *rule)
8043 OVS_REQUIRES(ofproto_mutex)
8044{
8045 const struct rule_actions *actions = rule_get_actions(rule);
8046
39c94593
JR
8047 ovs_assert(rule->removed);
8048
bc5e6a91 8049 if (rule->hard_timeout || rule->idle_timeout) {
417e7e66 8050 ovs_list_insert(&ofproto->expirable, &rule->expirable);
bc5e6a91
JR
8051 }
8052 cookies_insert(ofproto, rule);
8053 eviction_group_add_rule(rule);
8054 if (actions->has_meter) {
8055 meter_insert_rule(rule);
8056 }
cc099268
JR
8057 if (actions->has_groups) {
8058 const struct ofpact_group *a;
8059 OFPACT_FOR_EACH_TYPE_FLATTENED (a, GROUP, actions->ofpacts,
8060 actions->ofpacts_len) {
8061 struct ofgroup *group;
8062
5d08a275
JR
8063 group = ofproto_group_lookup(ofproto, a->group_id, OVS_VERSION_MAX,
8064 false);
cc099268
JR
8065 ovs_assert(group != NULL);
8066 group_add_rule(group, rule);
8067 }
8068 }
8069
39c94593 8070 rule->removed = false;
bc5e6a91
JR
8071}
8072
6787a49f
JR
8073/* Removes 'rule' from the ofproto data structures. Caller may have deferred
8074 * the removal from the classifier. */
d0918789 8075static void
802f84ff 8076ofproto_rule_remove__(struct ofproto *ofproto, struct rule *rule)
15aaf599 8077 OVS_REQUIRES(ofproto_mutex)
d0918789 8078{
39c94593
JR
8079 ovs_assert(!rule->removed);
8080
98eaac36 8081 cookies_remove(ofproto, rule);
2c916028 8082
254750ce 8083 eviction_group_remove_rule(rule);
417e7e66
BW
8084 if (!ovs_list_is_empty(&rule->expirable)) {
8085 ovs_list_remove(&rule->expirable);
e503cc19 8086 }
417e7e66
BW
8087 if (!ovs_list_is_empty(&rule->meter_list_node)) {
8088 ovs_list_remove(&rule->meter_list_node);
8089 ovs_list_init(&rule->meter_list_node);
9cae45dc 8090 }
802f84ff 8091
cc099268
JR
8092 /* Remove the rule from any groups, except from the group that is being
8093 * deleted, if any. */
8094 const struct rule_actions *actions = rule_get_actions(rule);
8095
8096 if (actions->has_groups) {
8097 const struct ofpact_group *a;
8098
8099 OFPACT_FOR_EACH_TYPE (a, GROUP, actions->ofpacts,
8100 actions->ofpacts_len) {
8101 struct ofgroup *group;
8102
5d08a275
JR
8103 group = ofproto_group_lookup(ofproto, a->group_id, OVS_VERSION_MAX,
8104 false);
cc099268
JR
8105 ovs_assert(group);
8106
8107 /* Leave the rule for the group that is being deleted, if any,
8108 * as we still need the list of rules for clean-up. */
8109 if (!group->being_deleted) {
8110 group_remove_rule(group, rule);
8111 }
8112 }
8113 }
8114
39c94593 8115 rule->removed = true;
0b4f2078 8116}
d0918789 8117\f
abe529af 8118/* unixctl commands. */
7aa697dd 8119
abe529af 8120struct ofproto *
2ac6bedd 8121ofproto_lookup(const char *name)
7aa697dd 8122{
2ac6bedd 8123 struct ofproto *ofproto;
7aa697dd 8124
2ac6bedd
BP
8125 HMAP_FOR_EACH_WITH_HASH (ofproto, hmap_node, hash_string(name, 0),
8126 &all_ofprotos) {
8127 if (!strcmp(ofproto->name, name)) {
8128 return ofproto;
8129 }
7aa697dd 8130 }
2ac6bedd 8131 return NULL;
7aa697dd
BP
8132}
8133
8134static void
0e15264f
BP
8135ofproto_unixctl_list(struct unixctl_conn *conn, int argc OVS_UNUSED,
8136 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
7aa697dd 8137{
7aa697dd 8138 struct ofproto *ofproto;
7aa697dd 8139 struct ds results;
7aa697dd 8140
7aa697dd 8141 ds_init(&results);
2ac6bedd
BP
8142 HMAP_FOR_EACH (ofproto, hmap_node, &all_ofprotos) {
8143 ds_put_format(&results, "%s\n", ofproto->name);
7aa697dd 8144 }
bde9f75d 8145 unixctl_command_reply(conn, ds_cstr(&results));
7aa697dd 8146 ds_destroy(&results);
7aa697dd
BP
8147}
8148
8149static void
8150ofproto_unixctl_init(void)
8151{
8152 static bool registered;
8153 if (registered) {
8154 return;
8155 }
8156 registered = true;
8157
0e15264f
BP
8158 unixctl_command_register("ofproto/list", "", 0, 0,
8159 ofproto_unixctl_list, NULL);
064af421 8160}