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