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