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