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