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