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