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