]> git.proxmox.com Git - mirror_ovs.git/blob - ofproto/ofproto.c
ofproto: Convert units correctly in ofport_open().
[mirror_ovs.git] / ofproto / ofproto.c
1 /*
2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 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 "ofproto.h"
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <stdbool.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include "bitmap.h"
26 #include "byte-order.h"
27 #include "classifier.h"
28 #include "connmgr.h"
29 #include "coverage.h"
30 #include "dynamic-string.h"
31 #include "hash.h"
32 #include "hmap.h"
33 #include "meta-flow.h"
34 #include "netdev.h"
35 #include "nx-match.h"
36 #include "ofp-actions.h"
37 #include "ofp-errors.h"
38 #include "ofp-msgs.h"
39 #include "ofp-print.h"
40 #include "ofp-util.h"
41 #include "ofpbuf.h"
42 #include "ofproto-provider.h"
43 #include "openflow/nicira-ext.h"
44 #include "openflow/openflow.h"
45 #include "packets.h"
46 #include "pinsched.h"
47 #include "pktbuf.h"
48 #include "poll-loop.h"
49 #include "random.h"
50 #include "shash.h"
51 #include "simap.h"
52 #include "sset.h"
53 #include "timeval.h"
54 #include "unaligned.h"
55 #include "unixctl.h"
56 #include "vlog.h"
57
58 VLOG_DEFINE_THIS_MODULE(ofproto);
59
60 COVERAGE_DEFINE(ofproto_error);
61 COVERAGE_DEFINE(ofproto_flush);
62 COVERAGE_DEFINE(ofproto_no_packet_in);
63 COVERAGE_DEFINE(ofproto_packet_out);
64 COVERAGE_DEFINE(ofproto_queue_req);
65 COVERAGE_DEFINE(ofproto_recv_openflow);
66 COVERAGE_DEFINE(ofproto_reinit_ports);
67 COVERAGE_DEFINE(ofproto_uninstallable);
68 COVERAGE_DEFINE(ofproto_update_port);
69
70 enum ofproto_state {
71 S_OPENFLOW, /* Processing OpenFlow commands. */
72 S_EVICT, /* Evicting flows from over-limit tables. */
73 S_FLUSH, /* Deleting all flow table rules. */
74 };
75
76 enum ofoperation_type {
77 OFOPERATION_ADD,
78 OFOPERATION_DELETE,
79 OFOPERATION_MODIFY,
80 OFOPERATION_REPLACE
81 };
82
83 /* A single OpenFlow request can execute any number of operations. The
84 * ofopgroup maintain OpenFlow state common to all of the operations, e.g. the
85 * ofconn to which an error reply should be sent if necessary.
86 *
87 * ofproto initiates some operations internally. These operations are still
88 * assigned to groups but will not have an associated ofconn. */
89 struct ofopgroup {
90 struct ofproto *ofproto; /* Owning ofproto. */
91 struct list ofproto_node; /* In ofproto's "pending" list. */
92 struct list ops; /* List of "struct ofoperation"s. */
93 int n_running; /* Number of ops still pending. */
94
95 /* Data needed to send OpenFlow reply on failure or to send a buffered
96 * packet on success.
97 *
98 * If list_is_empty(ofconn_node) then this ofopgroup never had an
99 * associated ofconn or its ofconn's connection dropped after it initiated
100 * the operation. In the latter case 'ofconn' is a wild pointer that
101 * refers to freed memory, so the 'ofconn' member must be used only if
102 * !list_is_empty(ofconn_node).
103 */
104 struct list ofconn_node; /* In ofconn's list of pending opgroups. */
105 struct ofconn *ofconn; /* ofconn for reply (but see note above). */
106 struct ofp_header *request; /* Original request (truncated at 64 bytes). */
107 uint32_t buffer_id; /* Buffer id from original request. */
108 };
109
110 static struct ofopgroup *ofopgroup_create_unattached(struct ofproto *);
111 static struct ofopgroup *ofopgroup_create(struct ofproto *, struct ofconn *,
112 const struct ofp_header *,
113 uint32_t buffer_id);
114 static void ofopgroup_submit(struct ofopgroup *);
115 static void ofopgroup_complete(struct ofopgroup *);
116
117 /* A single flow table operation. */
118 struct ofoperation {
119 struct ofopgroup *group; /* Owning group. */
120 struct list group_node; /* In ofopgroup's "ops" list. */
121 struct hmap_node hmap_node; /* In ofproto's "deletions" hmap. */
122 struct rule *rule; /* Rule being operated upon. */
123 enum ofoperation_type type; /* Type of operation. */
124
125 /* OFOPERATION_MODIFY, OFOPERATION_REPLACE: The old actions, if the actions
126 * are changing. */
127 struct ofpact *ofpacts;
128 size_t ofpacts_len;
129 uint32_t meter_id;
130
131 /* OFOPERATION_DELETE. */
132 enum ofp_flow_removed_reason reason; /* Reason flow was removed. */
133
134 ovs_be64 flow_cookie; /* Rule's old flow cookie. */
135 uint16_t idle_timeout; /* Rule's old idle timeout. */
136 uint16_t hard_timeout; /* Rule's old hard timeout. */
137 bool send_flow_removed; /* Rule's old 'send_flow_removed'. */
138 enum ofperr error; /* 0 if no error. */
139 };
140
141 static struct ofoperation *ofoperation_create(struct ofopgroup *,
142 struct rule *,
143 enum ofoperation_type,
144 enum ofp_flow_removed_reason);
145 static void ofoperation_destroy(struct ofoperation *);
146
147 /* oftable. */
148 static void oftable_init(struct oftable *);
149 static void oftable_destroy(struct oftable *);
150
151 static void oftable_set_name(struct oftable *, const char *name);
152
153 static void oftable_disable_eviction(struct oftable *);
154 static void oftable_enable_eviction(struct oftable *,
155 const struct mf_subfield *fields,
156 size_t n_fields);
157
158 static void oftable_remove_rule(struct rule *rule) OVS_RELEASES(rule->evict);
159 static void oftable_remove_rule__(struct ofproto *ofproto,
160 struct classifier *cls, struct rule *rule)
161 OVS_REQ_WRLOCK(cls->rwlock) OVS_RELEASES(rule->evict);
162 static void oftable_insert_rule(struct rule *);
163
164 /* A set of rules within a single OpenFlow table (oftable) that have the same
165 * values for the oftable's eviction_fields. A rule to be evicted, when one is
166 * needed, is taken from the eviction group that contains the greatest number
167 * of rules.
168 *
169 * An oftable owns any number of eviction groups, each of which contains any
170 * number of rules.
171 *
172 * Membership in an eviction group is imprecise, based on the hash of the
173 * oftable's eviction_fields (in the eviction_group's id_node.hash member).
174 * That is, if two rules have different eviction_fields, but those
175 * eviction_fields hash to the same value, then they will belong to the same
176 * eviction_group anyway.
177 *
178 * (When eviction is not enabled on an oftable, we don't track any eviction
179 * groups, to save time and space.) */
180 struct eviction_group {
181 struct hmap_node id_node; /* In oftable's "eviction_groups_by_id". */
182 struct heap_node size_node; /* In oftable's "eviction_groups_by_size". */
183 struct heap rules; /* Contains "struct rule"s. */
184 };
185
186 static bool choose_rule_to_evict(struct oftable *table, struct rule **rulep)
187 OVS_TRY_WRLOCK(true, (*rulep)->evict);
188 static void ofproto_evict(struct ofproto *);
189 static uint32_t rule_eviction_priority(struct rule *);
190 static void eviction_group_add_rule(struct rule *);
191 static void eviction_group_remove_rule(struct rule *);
192
193 /* ofport. */
194 static void ofport_destroy__(struct ofport *);
195 static void ofport_destroy(struct ofport *);
196
197 static void update_port(struct ofproto *, const char *devname);
198 static int init_ports(struct ofproto *);
199 static void reinit_ports(struct ofproto *);
200
201 /* rule. */
202 static void ofproto_rule_destroy__(struct rule *);
203 static void ofproto_rule_send_removed(struct rule *, uint8_t reason);
204 static bool rule_is_modifiable(const struct rule *);
205
206 /* OpenFlow. */
207 static enum ofperr add_flow(struct ofproto *, struct ofconn *,
208 struct ofputil_flow_mod *,
209 const struct ofp_header *);
210 static enum ofperr modify_flows__(struct ofproto *, struct ofconn *,
211 struct ofputil_flow_mod *,
212 const struct ofp_header *, struct list *);
213 static void delete_flow__(struct rule *rule, struct ofopgroup *,
214 enum ofp_flow_removed_reason)
215 OVS_RELEASES(rule->evict);
216 static enum ofperr add_group(struct ofproto *, struct ofputil_group_mod *);
217 static bool handle_openflow(struct ofconn *, const struct ofpbuf *);
218 static enum ofperr handle_flow_mod__(struct ofproto *, struct ofconn *,
219 struct ofputil_flow_mod *,
220 const struct ofp_header *);
221 static void calc_duration(long long int start, long long int now,
222 uint32_t *sec, uint32_t *nsec);
223
224 /* ofproto. */
225 static uint64_t pick_datapath_id(const struct ofproto *);
226 static uint64_t pick_fallback_dpid(void);
227 static void ofproto_destroy__(struct ofproto *);
228 static void update_mtu(struct ofproto *, struct ofport *);
229 static void meter_delete(struct ofproto *, uint32_t first, uint32_t last);
230
231 /* unixctl. */
232 static void ofproto_unixctl_init(void);
233
234 /* All registered ofproto classes, in probe order. */
235 static const struct ofproto_class **ofproto_classes;
236 static size_t n_ofproto_classes;
237 static size_t allocated_ofproto_classes;
238
239 unsigned flow_eviction_threshold = OFPROTO_FLOW_EVICTION_THRESHOLD_DEFAULT;
240 unsigned n_handler_threads;
241 enum ofproto_flow_miss_model flow_miss_model = OFPROTO_HANDLE_MISS_AUTO;
242
243 /* Map from datapath name to struct ofproto, for use by unixctl commands. */
244 static struct hmap all_ofprotos = HMAP_INITIALIZER(&all_ofprotos);
245
246 /* Initial mappings of port to OpenFlow number mappings. */
247 static struct shash init_ofp_ports = SHASH_INITIALIZER(&init_ofp_ports);
248
249 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
250
251 /* The default value of true waits for flow restore. */
252 static bool flow_restore_wait = true;
253
254 /* Must be called to initialize the ofproto library.
255 *
256 * The caller may pass in 'iface_hints', which contains an shash of
257 * "iface_hint" elements indexed by the interface's name. The provider
258 * may use these hints to describe the startup configuration in order to
259 * reinitialize its state. The caller owns the provided data, so a
260 * provider will make copies of anything required. An ofproto provider
261 * will remove any existing state that is not described by the hint, and
262 * may choose to remove it all. */
263 void
264 ofproto_init(const struct shash *iface_hints)
265 {
266 struct shash_node *node;
267 size_t i;
268
269 ofproto_class_register(&ofproto_dpif_class);
270
271 /* Make a local copy, since we don't own 'iface_hints' elements. */
272 SHASH_FOR_EACH(node, iface_hints) {
273 const struct iface_hint *orig_hint = node->data;
274 struct iface_hint *new_hint = xmalloc(sizeof *new_hint);
275 const char *br_type = ofproto_normalize_type(orig_hint->br_type);
276
277 new_hint->br_name = xstrdup(orig_hint->br_name);
278 new_hint->br_type = xstrdup(br_type);
279 new_hint->ofp_port = orig_hint->ofp_port;
280
281 shash_add(&init_ofp_ports, node->name, new_hint);
282 }
283
284 for (i = 0; i < n_ofproto_classes; i++) {
285 ofproto_classes[i]->init(&init_ofp_ports);
286 }
287 }
288
289 /* 'type' should be a normalized datapath type, as returned by
290 * ofproto_normalize_type(). Returns the corresponding ofproto_class
291 * structure, or a null pointer if there is none registered for 'type'. */
292 static const struct ofproto_class *
293 ofproto_class_find__(const char *type)
294 {
295 size_t i;
296
297 for (i = 0; i < n_ofproto_classes; i++) {
298 const struct ofproto_class *class = ofproto_classes[i];
299 struct sset types;
300 bool found;
301
302 sset_init(&types);
303 class->enumerate_types(&types);
304 found = sset_contains(&types, type);
305 sset_destroy(&types);
306
307 if (found) {
308 return class;
309 }
310 }
311 VLOG_WARN("unknown datapath type %s", type);
312 return NULL;
313 }
314
315 /* Registers a new ofproto class. After successful registration, new ofprotos
316 * of that type can be created using ofproto_create(). */
317 int
318 ofproto_class_register(const struct ofproto_class *new_class)
319 {
320 size_t i;
321
322 for (i = 0; i < n_ofproto_classes; i++) {
323 if (ofproto_classes[i] == new_class) {
324 return EEXIST;
325 }
326 }
327
328 if (n_ofproto_classes >= allocated_ofproto_classes) {
329 ofproto_classes = x2nrealloc(ofproto_classes,
330 &allocated_ofproto_classes,
331 sizeof *ofproto_classes);
332 }
333 ofproto_classes[n_ofproto_classes++] = new_class;
334 return 0;
335 }
336
337 /* Unregisters a datapath provider. 'type' must have been previously
338 * registered and not currently be in use by any ofprotos. After
339 * unregistration new datapaths of that type cannot be opened using
340 * ofproto_create(). */
341 int
342 ofproto_class_unregister(const struct ofproto_class *class)
343 {
344 size_t i;
345
346 for (i = 0; i < n_ofproto_classes; i++) {
347 if (ofproto_classes[i] == class) {
348 for (i++; i < n_ofproto_classes; i++) {
349 ofproto_classes[i - 1] = ofproto_classes[i];
350 }
351 n_ofproto_classes--;
352 return 0;
353 }
354 }
355 VLOG_WARN("attempted to unregister an ofproto class that is not "
356 "registered");
357 return EAFNOSUPPORT;
358 }
359
360 /* Clears 'types' and enumerates all registered ofproto types into it. The
361 * caller must first initialize the sset. */
362 void
363 ofproto_enumerate_types(struct sset *types)
364 {
365 size_t i;
366
367 for (i = 0; i < n_ofproto_classes; i++) {
368 ofproto_classes[i]->enumerate_types(types);
369 }
370 }
371
372 /* Returns the fully spelled out name for the given ofproto 'type'.
373 *
374 * Normalized type string can be compared with strcmp(). Unnormalized type
375 * string might be the same even if they have different spellings. */
376 const char *
377 ofproto_normalize_type(const char *type)
378 {
379 return type && type[0] ? type : "system";
380 }
381
382 /* Clears 'names' and enumerates the names of all known created ofprotos with
383 * the given 'type'. The caller must first initialize the sset. Returns 0 if
384 * successful, otherwise a positive errno value.
385 *
386 * Some kinds of datapaths might not be practically enumerable. This is not
387 * considered an error. */
388 int
389 ofproto_enumerate_names(const char *type, struct sset *names)
390 {
391 const struct ofproto_class *class = ofproto_class_find__(type);
392 return class ? class->enumerate_names(type, names) : EAFNOSUPPORT;
393 }
394
395 int
396 ofproto_create(const char *datapath_name, const char *datapath_type,
397 struct ofproto **ofprotop)
398 {
399 const struct ofproto_class *class;
400 struct ofproto *ofproto;
401 int error;
402 int i;
403
404 *ofprotop = NULL;
405
406 ofproto_unixctl_init();
407
408 datapath_type = ofproto_normalize_type(datapath_type);
409 class = ofproto_class_find__(datapath_type);
410 if (!class) {
411 VLOG_WARN("could not create datapath %s of unknown type %s",
412 datapath_name, datapath_type);
413 return EAFNOSUPPORT;
414 }
415
416 ofproto = class->alloc();
417 if (!ofproto) {
418 VLOG_ERR("failed to allocate datapath %s of type %s",
419 datapath_name, datapath_type);
420 return ENOMEM;
421 }
422
423 /* Initialize. */
424 memset(ofproto, 0, sizeof *ofproto);
425 ofproto->ofproto_class = class;
426 ofproto->name = xstrdup(datapath_name);
427 ofproto->type = xstrdup(datapath_type);
428 hmap_insert(&all_ofprotos, &ofproto->hmap_node,
429 hash_string(ofproto->name, 0));
430 ofproto->datapath_id = 0;
431 ofproto->forward_bpdu = false;
432 ofproto->fallback_dpid = pick_fallback_dpid();
433 ofproto->mfr_desc = NULL;
434 ofproto->hw_desc = NULL;
435 ofproto->sw_desc = NULL;
436 ofproto->serial_desc = NULL;
437 ofproto->dp_desc = NULL;
438 ofproto->frag_handling = OFPC_FRAG_NORMAL;
439 hmap_init(&ofproto->ports);
440 shash_init(&ofproto->port_by_name);
441 simap_init(&ofproto->ofp_requests);
442 ofproto->max_ports = ofp_to_u16(OFPP_MAX);
443 ofproto->tables = NULL;
444 ofproto->n_tables = 0;
445 hindex_init(&ofproto->cookies);
446 list_init(&ofproto->expirable);
447 ovs_mutex_init_recursive(&ofproto->expirable_mutex);
448 ofproto->connmgr = connmgr_create(ofproto, datapath_name, datapath_name);
449 ofproto->state = S_OPENFLOW;
450 list_init(&ofproto->pending);
451 ofproto->n_pending = 0;
452 hmap_init(&ofproto->deletions);
453 ofproto->n_add = ofproto->n_delete = ofproto->n_modify = 0;
454 ofproto->first_op = ofproto->last_op = LLONG_MIN;
455 ofproto->next_op_report = LLONG_MAX;
456 ofproto->op_backoff = LLONG_MIN;
457 ofproto->vlan_bitmap = NULL;
458 ofproto->vlans_changed = false;
459 ofproto->min_mtu = INT_MAX;
460 ovs_rwlock_init(&ofproto->groups_rwlock);
461 hmap_init(&ofproto->groups);
462
463 error = ofproto->ofproto_class->construct(ofproto);
464 if (error) {
465 VLOG_ERR("failed to open datapath %s: %s",
466 datapath_name, ovs_strerror(error));
467 ofproto_destroy__(ofproto);
468 return error;
469 }
470
471 /* The "max_ports" member should have been set by ->construct(ofproto).
472 * Port 0 is not a valid OpenFlow port, so mark that as unavailable. */
473 ofproto->ofp_port_ids = bitmap_allocate(ofproto->max_ports);
474 bitmap_set1(ofproto->ofp_port_ids, 0);
475
476 /* Check that hidden tables, if any, are at the end. */
477 ovs_assert(ofproto->n_tables);
478 for (i = 0; i + 1 < ofproto->n_tables; i++) {
479 enum oftable_flags flags = ofproto->tables[i].flags;
480 enum oftable_flags next_flags = ofproto->tables[i + 1].flags;
481
482 ovs_assert(!(flags & OFTABLE_HIDDEN) || next_flags & OFTABLE_HIDDEN);
483 }
484
485 ofproto->datapath_id = pick_datapath_id(ofproto);
486 init_ports(ofproto);
487
488 /* Initialize meters table. */
489 if (ofproto->ofproto_class->meter_get_features) {
490 ofproto->ofproto_class->meter_get_features(ofproto,
491 &ofproto->meter_features);
492 } else {
493 memset(&ofproto->meter_features, 0, sizeof ofproto->meter_features);
494 }
495 ofproto->meters = xzalloc((ofproto->meter_features.max_meters + 1)
496 * sizeof(struct meter *));
497
498 *ofprotop = ofproto;
499 return 0;
500 }
501
502 /* Must be called (only) by an ofproto implementation in its constructor
503 * function. See the large comment on 'construct' in struct ofproto_class for
504 * details. */
505 void
506 ofproto_init_tables(struct ofproto *ofproto, int n_tables)
507 {
508 struct oftable *table;
509
510 ovs_assert(!ofproto->n_tables);
511 ovs_assert(n_tables >= 1 && n_tables <= 255);
512
513 ofproto->n_tables = n_tables;
514 ofproto->tables = xmalloc(n_tables * sizeof *ofproto->tables);
515 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
516 oftable_init(table);
517 }
518 }
519
520 /* To be optionally called (only) by an ofproto implementation in its
521 * constructor function. See the large comment on 'construct' in struct
522 * ofproto_class for details.
523 *
524 * Sets the maximum number of ports to 'max_ports'. The ofproto generic layer
525 * will then ensure that actions passed into the ofproto implementation will
526 * not refer to OpenFlow ports numbered 'max_ports' or higher. If this
527 * function is not called, there will be no such restriction.
528 *
529 * Reserved ports numbered OFPP_MAX and higher are special and not subject to
530 * the 'max_ports' restriction. */
531 void
532 ofproto_init_max_ports(struct ofproto *ofproto, uint16_t max_ports)
533 {
534 ovs_assert(max_ports <= ofp_to_u16(OFPP_MAX));
535 ofproto->max_ports = max_ports;
536 }
537
538 uint64_t
539 ofproto_get_datapath_id(const struct ofproto *ofproto)
540 {
541 return ofproto->datapath_id;
542 }
543
544 void
545 ofproto_set_datapath_id(struct ofproto *p, uint64_t datapath_id)
546 {
547 uint64_t old_dpid = p->datapath_id;
548 p->datapath_id = datapath_id ? datapath_id : pick_datapath_id(p);
549 if (p->datapath_id != old_dpid) {
550 /* Force all active connections to reconnect, since there is no way to
551 * notify a controller that the datapath ID has changed. */
552 ofproto_reconnect_controllers(p);
553 }
554 }
555
556 void
557 ofproto_set_controllers(struct ofproto *p,
558 const struct ofproto_controller *controllers,
559 size_t n_controllers, uint32_t allowed_versions)
560 {
561 connmgr_set_controllers(p->connmgr, controllers, n_controllers,
562 allowed_versions);
563 }
564
565 void
566 ofproto_set_fail_mode(struct ofproto *p, enum ofproto_fail_mode fail_mode)
567 {
568 connmgr_set_fail_mode(p->connmgr, fail_mode);
569 }
570
571 /* Drops the connections between 'ofproto' and all of its controllers, forcing
572 * them to reconnect. */
573 void
574 ofproto_reconnect_controllers(struct ofproto *ofproto)
575 {
576 connmgr_reconnect(ofproto->connmgr);
577 }
578
579 /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'ofproto''s
580 * in-band control should guarantee access, in the same way that in-band
581 * control guarantees access to OpenFlow controllers. */
582 void
583 ofproto_set_extra_in_band_remotes(struct ofproto *ofproto,
584 const struct sockaddr_in *extras, size_t n)
585 {
586 connmgr_set_extra_in_band_remotes(ofproto->connmgr, extras, n);
587 }
588
589 /* Sets the OpenFlow queue used by flows set up by in-band control on
590 * 'ofproto' to 'queue_id'. If 'queue_id' is negative, then in-band control
591 * flows will use the default queue. */
592 void
593 ofproto_set_in_band_queue(struct ofproto *ofproto, int queue_id)
594 {
595 connmgr_set_in_band_queue(ofproto->connmgr, queue_id);
596 }
597
598 /* Sets the number of flows at which eviction from the kernel flow table
599 * will occur. */
600 void
601 ofproto_set_flow_eviction_threshold(unsigned threshold)
602 {
603 flow_eviction_threshold = MAX(OFPROTO_FLOW_EVICTION_THRESHOLD_MIN,
604 threshold);
605 }
606
607 /* Sets the path for handling flow misses. */
608 void
609 ofproto_set_flow_miss_model(unsigned model)
610 {
611 flow_miss_model = model;
612 }
613
614 /* If forward_bpdu is true, the NORMAL action will forward frames with
615 * reserved (e.g. STP) destination Ethernet addresses. if forward_bpdu is false,
616 * the NORMAL action will drop these frames. */
617 void
618 ofproto_set_forward_bpdu(struct ofproto *ofproto, bool forward_bpdu)
619 {
620 bool old_val = ofproto->forward_bpdu;
621 ofproto->forward_bpdu = forward_bpdu;
622 if (old_val != ofproto->forward_bpdu) {
623 if (ofproto->ofproto_class->forward_bpdu_changed) {
624 ofproto->ofproto_class->forward_bpdu_changed(ofproto);
625 }
626 }
627 }
628
629 /* Sets the MAC aging timeout for the OFPP_NORMAL action on 'ofproto' to
630 * 'idle_time', in seconds, and the maximum number of MAC table entries to
631 * 'max_entries'. */
632 void
633 ofproto_set_mac_table_config(struct ofproto *ofproto, unsigned idle_time,
634 size_t max_entries)
635 {
636 if (ofproto->ofproto_class->set_mac_table_config) {
637 ofproto->ofproto_class->set_mac_table_config(ofproto, idle_time,
638 max_entries);
639 }
640 }
641
642 /* Sets number of upcall handler threads. The default is
643 * (number of online cores - 1). */
644 void
645 ofproto_set_n_handler_threads(unsigned limit)
646 {
647 if (limit) {
648 n_handler_threads = limit;
649 } else {
650 n_handler_threads = MAX(1, sysconf(_SC_NPROCESSORS_ONLN) - 1);
651 }
652 }
653
654 void
655 ofproto_set_dp_desc(struct ofproto *p, const char *dp_desc)
656 {
657 free(p->dp_desc);
658 p->dp_desc = dp_desc ? xstrdup(dp_desc) : NULL;
659 }
660
661 int
662 ofproto_set_snoops(struct ofproto *ofproto, const struct sset *snoops)
663 {
664 return connmgr_set_snoops(ofproto->connmgr, snoops);
665 }
666
667 int
668 ofproto_set_netflow(struct ofproto *ofproto,
669 const struct netflow_options *nf_options)
670 {
671 if (nf_options && sset_is_empty(&nf_options->collectors)) {
672 nf_options = NULL;
673 }
674
675 if (ofproto->ofproto_class->set_netflow) {
676 return ofproto->ofproto_class->set_netflow(ofproto, nf_options);
677 } else {
678 return nf_options ? EOPNOTSUPP : 0;
679 }
680 }
681
682 int
683 ofproto_set_sflow(struct ofproto *ofproto,
684 const struct ofproto_sflow_options *oso)
685 {
686 if (oso && sset_is_empty(&oso->targets)) {
687 oso = NULL;
688 }
689
690 if (ofproto->ofproto_class->set_sflow) {
691 return ofproto->ofproto_class->set_sflow(ofproto, oso);
692 } else {
693 return oso ? EOPNOTSUPP : 0;
694 }
695 }
696
697 int
698 ofproto_set_ipfix(struct ofproto *ofproto,
699 const struct ofproto_ipfix_bridge_exporter_options *bo,
700 const struct ofproto_ipfix_flow_exporter_options *fo,
701 size_t n_fo)
702 {
703 if (ofproto->ofproto_class->set_ipfix) {
704 return ofproto->ofproto_class->set_ipfix(ofproto, bo, fo, n_fo);
705 } else {
706 return (bo || fo) ? EOPNOTSUPP : 0;
707 }
708 }
709
710 void
711 ofproto_set_flow_restore_wait(bool flow_restore_wait_db)
712 {
713 flow_restore_wait = flow_restore_wait_db;
714 }
715
716 bool
717 ofproto_get_flow_restore_wait(void)
718 {
719 return flow_restore_wait;
720 }
721
722 \f
723 /* Spanning Tree Protocol (STP) configuration. */
724
725 /* Configures STP on 'ofproto' using the settings defined in 's'. If
726 * 's' is NULL, disables STP.
727 *
728 * Returns 0 if successful, otherwise a positive errno value. */
729 int
730 ofproto_set_stp(struct ofproto *ofproto,
731 const struct ofproto_stp_settings *s)
732 {
733 return (ofproto->ofproto_class->set_stp
734 ? ofproto->ofproto_class->set_stp(ofproto, s)
735 : EOPNOTSUPP);
736 }
737
738 /* Retrieves STP status of 'ofproto' and stores it in 's'. If the
739 * 'enabled' member of 's' is false, then the other members are not
740 * meaningful.
741 *
742 * Returns 0 if successful, otherwise a positive errno value. */
743 int
744 ofproto_get_stp_status(struct ofproto *ofproto,
745 struct ofproto_stp_status *s)
746 {
747 return (ofproto->ofproto_class->get_stp_status
748 ? ofproto->ofproto_class->get_stp_status(ofproto, s)
749 : EOPNOTSUPP);
750 }
751
752 /* Configures STP on 'ofp_port' of 'ofproto' using the settings defined
753 * in 's'. The caller is responsible for assigning STP port numbers
754 * (using the 'port_num' member in the range of 1 through 255, inclusive)
755 * and ensuring there are no duplicates. If the 's' is NULL, then STP
756 * is disabled on the port.
757 *
758 * Returns 0 if successful, otherwise a positive errno value.*/
759 int
760 ofproto_port_set_stp(struct ofproto *ofproto, ofp_port_t ofp_port,
761 const struct ofproto_port_stp_settings *s)
762 {
763 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
764 if (!ofport) {
765 VLOG_WARN("%s: cannot configure STP on nonexistent port %"PRIu16,
766 ofproto->name, ofp_port);
767 return ENODEV;
768 }
769
770 return (ofproto->ofproto_class->set_stp_port
771 ? ofproto->ofproto_class->set_stp_port(ofport, s)
772 : EOPNOTSUPP);
773 }
774
775 /* Retrieves STP port status of 'ofp_port' on 'ofproto' and stores it in
776 * 's'. If the 'enabled' member in 's' is false, then the other members
777 * are not meaningful.
778 *
779 * Returns 0 if successful, otherwise a positive errno value.*/
780 int
781 ofproto_port_get_stp_status(struct ofproto *ofproto, ofp_port_t ofp_port,
782 struct ofproto_port_stp_status *s)
783 {
784 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
785 if (!ofport) {
786 VLOG_WARN_RL(&rl, "%s: cannot get STP status on nonexistent "
787 "port %"PRIu16, ofproto->name, ofp_port);
788 return ENODEV;
789 }
790
791 return (ofproto->ofproto_class->get_stp_port_status
792 ? ofproto->ofproto_class->get_stp_port_status(ofport, s)
793 : EOPNOTSUPP);
794 }
795 \f
796 /* Queue DSCP configuration. */
797
798 /* Registers meta-data associated with the 'n_qdscp' Qualities of Service
799 * 'queues' attached to 'ofport'. This data is not intended to be sufficient
800 * to implement QoS. Instead, it is used to implement features which require
801 * knowledge of what queues exist on a port, and some basic information about
802 * them.
803 *
804 * Returns 0 if successful, otherwise a positive errno value. */
805 int
806 ofproto_port_set_queues(struct ofproto *ofproto, ofp_port_t ofp_port,
807 const struct ofproto_port_queue *queues,
808 size_t n_queues)
809 {
810 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
811
812 if (!ofport) {
813 VLOG_WARN("%s: cannot set queues on nonexistent port %"PRIu16,
814 ofproto->name, ofp_port);
815 return ENODEV;
816 }
817
818 return (ofproto->ofproto_class->set_queues
819 ? ofproto->ofproto_class->set_queues(ofport, queues, n_queues)
820 : EOPNOTSUPP);
821 }
822 \f
823 /* Connectivity Fault Management configuration. */
824
825 /* Clears the CFM configuration from 'ofp_port' on 'ofproto'. */
826 void
827 ofproto_port_clear_cfm(struct ofproto *ofproto, ofp_port_t ofp_port)
828 {
829 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
830 if (ofport && ofproto->ofproto_class->set_cfm) {
831 ofproto->ofproto_class->set_cfm(ofport, NULL);
832 }
833 }
834
835 /* Configures connectivity fault management on 'ofp_port' in 'ofproto'. Takes
836 * basic configuration from the configuration members in 'cfm', and the remote
837 * maintenance point ID from remote_mpid. Ignores the statistics members of
838 * 'cfm'.
839 *
840 * This function has no effect if 'ofproto' does not have a port 'ofp_port'. */
841 void
842 ofproto_port_set_cfm(struct ofproto *ofproto, ofp_port_t ofp_port,
843 const struct cfm_settings *s)
844 {
845 struct ofport *ofport;
846 int error;
847
848 ofport = ofproto_get_port(ofproto, ofp_port);
849 if (!ofport) {
850 VLOG_WARN("%s: cannot configure CFM on nonexistent port %"PRIu16,
851 ofproto->name, ofp_port);
852 return;
853 }
854
855 /* XXX: For configuration simplicity, we only support one remote_mpid
856 * outside of the CFM module. It's not clear if this is the correct long
857 * term solution or not. */
858 error = (ofproto->ofproto_class->set_cfm
859 ? ofproto->ofproto_class->set_cfm(ofport, s)
860 : EOPNOTSUPP);
861 if (error) {
862 VLOG_WARN("%s: CFM configuration on port %"PRIu16" (%s) failed (%s)",
863 ofproto->name, ofp_port, netdev_get_name(ofport->netdev),
864 ovs_strerror(error));
865 }
866 }
867
868 /* Configures BFD on 'ofp_port' in 'ofproto'. This function has no effect if
869 * 'ofproto' does not have a port 'ofp_port'. */
870 void
871 ofproto_port_set_bfd(struct ofproto *ofproto, ofp_port_t ofp_port,
872 const struct smap *cfg)
873 {
874 struct ofport *ofport;
875 int error;
876
877 ofport = ofproto_get_port(ofproto, ofp_port);
878 if (!ofport) {
879 VLOG_WARN("%s: cannot configure bfd on nonexistent port %"PRIu16,
880 ofproto->name, ofp_port);
881 return;
882 }
883
884 error = (ofproto->ofproto_class->set_bfd
885 ? ofproto->ofproto_class->set_bfd(ofport, cfg)
886 : EOPNOTSUPP);
887 if (error) {
888 VLOG_WARN("%s: bfd configuration on port %"PRIu16" (%s) failed (%s)",
889 ofproto->name, ofp_port, netdev_get_name(ofport->netdev),
890 ovs_strerror(error));
891 }
892 }
893
894 /* Populates 'status' with key value pairs indicating the status of the BFD
895 * session on 'ofp_port'. This information is intended to be populated in the
896 * OVS database. Has no effect if 'ofp_port' is not na OpenFlow port in
897 * 'ofproto'. */
898 int
899 ofproto_port_get_bfd_status(struct ofproto *ofproto, ofp_port_t ofp_port,
900 struct smap *status)
901 {
902 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
903 return (ofport && ofproto->ofproto_class->get_bfd_status
904 ? ofproto->ofproto_class->get_bfd_status(ofport, status)
905 : EOPNOTSUPP);
906 }
907
908 /* Checks the status of LACP negotiation for 'ofp_port' within ofproto.
909 * Returns 1 if LACP partner information for 'ofp_port' is up-to-date,
910 * 0 if LACP partner information is not current (generally indicating a
911 * connectivity problem), or -1 if LACP is not enabled on 'ofp_port'. */
912 int
913 ofproto_port_is_lacp_current(struct ofproto *ofproto, ofp_port_t ofp_port)
914 {
915 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
916 return (ofport && ofproto->ofproto_class->port_is_lacp_current
917 ? ofproto->ofproto_class->port_is_lacp_current(ofport)
918 : -1);
919 }
920 \f
921 /* Bundles. */
922
923 /* Registers a "bundle" associated with client data pointer 'aux' in 'ofproto'.
924 * A bundle is the same concept as a Port in OVSDB, that is, it consists of one
925 * or more "slave" devices (Interfaces, in OVSDB) along with a VLAN
926 * configuration plus, if there is more than one slave, a bonding
927 * configuration.
928 *
929 * If 'aux' is already registered then this function updates its configuration
930 * to 's'. Otherwise, this function registers a new bundle.
931 *
932 * Bundles only affect the NXAST_AUTOPATH action and output to the OFPP_NORMAL
933 * port. */
934 int
935 ofproto_bundle_register(struct ofproto *ofproto, void *aux,
936 const struct ofproto_bundle_settings *s)
937 {
938 return (ofproto->ofproto_class->bundle_set
939 ? ofproto->ofproto_class->bundle_set(ofproto, aux, s)
940 : EOPNOTSUPP);
941 }
942
943 /* Unregisters the bundle registered on 'ofproto' with auxiliary data 'aux'.
944 * If no such bundle has been registered, this has no effect. */
945 int
946 ofproto_bundle_unregister(struct ofproto *ofproto, void *aux)
947 {
948 return ofproto_bundle_register(ofproto, aux, NULL);
949 }
950
951 \f
952 /* Registers a mirror associated with client data pointer 'aux' in 'ofproto'.
953 * If 'aux' is already registered then this function updates its configuration
954 * to 's'. Otherwise, this function registers a new mirror. */
955 int
956 ofproto_mirror_register(struct ofproto *ofproto, void *aux,
957 const struct ofproto_mirror_settings *s)
958 {
959 return (ofproto->ofproto_class->mirror_set
960 ? ofproto->ofproto_class->mirror_set(ofproto, aux, s)
961 : EOPNOTSUPP);
962 }
963
964 /* Unregisters the mirror registered on 'ofproto' with auxiliary data 'aux'.
965 * If no mirror has been registered, this has no effect. */
966 int
967 ofproto_mirror_unregister(struct ofproto *ofproto, void *aux)
968 {
969 return ofproto_mirror_register(ofproto, aux, NULL);
970 }
971
972 /* Retrieves statistics from mirror associated with client data pointer
973 * 'aux' in 'ofproto'. Stores packet and byte counts in 'packets' and
974 * 'bytes', respectively. If a particular counters is not supported,
975 * the appropriate argument is set to UINT64_MAX. */
976 int
977 ofproto_mirror_get_stats(struct ofproto *ofproto, void *aux,
978 uint64_t *packets, uint64_t *bytes)
979 {
980 if (!ofproto->ofproto_class->mirror_get_stats) {
981 *packets = *bytes = UINT64_MAX;
982 return EOPNOTSUPP;
983 }
984
985 return ofproto->ofproto_class->mirror_get_stats(ofproto, aux,
986 packets, bytes);
987 }
988
989 /* Configures the VLANs whose bits are set to 1 in 'flood_vlans' as VLANs on
990 * which all packets are flooded, instead of using MAC learning. If
991 * 'flood_vlans' is NULL, then MAC learning applies to all VLANs.
992 *
993 * Flood VLANs affect only the treatment of packets output to the OFPP_NORMAL
994 * port. */
995 int
996 ofproto_set_flood_vlans(struct ofproto *ofproto, unsigned long *flood_vlans)
997 {
998 return (ofproto->ofproto_class->set_flood_vlans
999 ? ofproto->ofproto_class->set_flood_vlans(ofproto, flood_vlans)
1000 : EOPNOTSUPP);
1001 }
1002
1003 /* Returns true if 'aux' is a registered bundle that is currently in use as the
1004 * output for a mirror. */
1005 bool
1006 ofproto_is_mirror_output_bundle(const struct ofproto *ofproto, void *aux)
1007 {
1008 return (ofproto->ofproto_class->is_mirror_output_bundle
1009 ? ofproto->ofproto_class->is_mirror_output_bundle(ofproto, aux)
1010 : false);
1011 }
1012 \f
1013 /* Configuration of OpenFlow tables. */
1014
1015 /* Returns the number of OpenFlow tables in 'ofproto'. */
1016 int
1017 ofproto_get_n_tables(const struct ofproto *ofproto)
1018 {
1019 return ofproto->n_tables;
1020 }
1021
1022 /* Configures the OpenFlow table in 'ofproto' with id 'table_id' with the
1023 * settings from 's'. 'table_id' must be in the range 0 through the number of
1024 * OpenFlow tables in 'ofproto' minus 1, inclusive.
1025 *
1026 * For read-only tables, only the name may be configured. */
1027 void
1028 ofproto_configure_table(struct ofproto *ofproto, int table_id,
1029 const struct ofproto_table_settings *s)
1030 {
1031 struct oftable *table;
1032
1033 ovs_assert(table_id >= 0 && table_id < ofproto->n_tables);
1034 table = &ofproto->tables[table_id];
1035
1036 oftable_set_name(table, s->name);
1037
1038 if (table->flags & OFTABLE_READONLY) {
1039 return;
1040 }
1041
1042 if (s->groups) {
1043 oftable_enable_eviction(table, s->groups, s->n_groups);
1044 } else {
1045 oftable_disable_eviction(table);
1046 }
1047
1048 table->max_flows = s->max_flows;
1049 ovs_rwlock_rdlock(&table->cls.rwlock);
1050 if (classifier_count(&table->cls) > table->max_flows
1051 && table->eviction_fields) {
1052 /* 'table' contains more flows than allowed. We might not be able to
1053 * evict them right away because of the asynchronous nature of flow
1054 * table changes. Schedule eviction for later. */
1055 switch (ofproto->state) {
1056 case S_OPENFLOW:
1057 ofproto->state = S_EVICT;
1058 break;
1059 case S_EVICT:
1060 case S_FLUSH:
1061 /* We're already deleting flows, nothing more to do. */
1062 break;
1063 }
1064 }
1065 ovs_rwlock_unlock(&table->cls.rwlock);
1066 }
1067 \f
1068 bool
1069 ofproto_has_snoops(const struct ofproto *ofproto)
1070 {
1071 return connmgr_has_snoops(ofproto->connmgr);
1072 }
1073
1074 void
1075 ofproto_get_snoops(const struct ofproto *ofproto, struct sset *snoops)
1076 {
1077 connmgr_get_snoops(ofproto->connmgr, snoops);
1078 }
1079
1080 /* Deletes 'rule' from 'cls' within 'ofproto'.
1081 *
1082 * The 'cls' argument is redundant (it is &ofproto->tables[rule->table_id].cls)
1083 * but it allows Clang to do better checking. */
1084 static void
1085 ofproto_delete_rule(struct ofproto *ofproto, struct classifier *cls,
1086 struct rule *rule)
1087 OVS_REQ_WRLOCK(cls->rwlock)
1088 {
1089 struct ofopgroup *group;
1090
1091 ovs_assert(!rule->pending);
1092 ovs_assert(cls == &ofproto->tables[rule->table_id].cls);
1093
1094 group = ofopgroup_create_unattached(ofproto);
1095 ofoperation_create(group, rule, OFOPERATION_DELETE, OFPRR_DELETE);
1096 ovs_rwlock_wrlock(&rule->evict);
1097 oftable_remove_rule__(ofproto, cls, rule);
1098 ofproto->ofproto_class->rule_delete(rule);
1099 ofopgroup_submit(group);
1100 }
1101
1102 static void
1103 ofproto_flush__(struct ofproto *ofproto)
1104 {
1105 struct oftable *table;
1106
1107 if (ofproto->ofproto_class->flush) {
1108 ofproto->ofproto_class->flush(ofproto);
1109 }
1110
1111 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
1112 struct rule *rule, *next_rule;
1113 struct cls_cursor cursor;
1114
1115 if (table->flags & OFTABLE_HIDDEN) {
1116 continue;
1117 }
1118
1119 ovs_rwlock_wrlock(&table->cls.rwlock);
1120 cls_cursor_init(&cursor, &table->cls, NULL);
1121 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) {
1122 if (!rule->pending) {
1123 ofproto_delete_rule(ofproto, &table->cls, rule);
1124 }
1125 }
1126 ovs_rwlock_unlock(&table->cls.rwlock);
1127 }
1128 }
1129
1130 static void delete_group(struct ofproto *ofproto, uint32_t group_id);
1131
1132 static void
1133 ofproto_destroy__(struct ofproto *ofproto)
1134 {
1135 struct oftable *table;
1136
1137 ovs_assert(list_is_empty(&ofproto->pending));
1138 ovs_assert(!ofproto->n_pending);
1139
1140 if (ofproto->meters) {
1141 meter_delete(ofproto, 1, ofproto->meter_features.max_meters);
1142 free(ofproto->meters);
1143 }
1144
1145 delete_group(ofproto, OFPG_ALL);
1146 ovs_rwlock_destroy(&ofproto->groups_rwlock);
1147 hmap_destroy(&ofproto->groups);
1148
1149 connmgr_destroy(ofproto->connmgr);
1150
1151 hmap_remove(&all_ofprotos, &ofproto->hmap_node);
1152 free(ofproto->name);
1153 free(ofproto->type);
1154 free(ofproto->mfr_desc);
1155 free(ofproto->hw_desc);
1156 free(ofproto->sw_desc);
1157 free(ofproto->serial_desc);
1158 free(ofproto->dp_desc);
1159 hmap_destroy(&ofproto->ports);
1160 shash_destroy(&ofproto->port_by_name);
1161 bitmap_free(ofproto->ofp_port_ids);
1162 simap_destroy(&ofproto->ofp_requests);
1163
1164 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
1165 oftable_destroy(table);
1166 }
1167 free(ofproto->tables);
1168
1169 hmap_destroy(&ofproto->deletions);
1170
1171 free(ofproto->vlan_bitmap);
1172
1173 ovs_mutex_destroy(&ofproto->expirable_mutex);
1174 ofproto->ofproto_class->dealloc(ofproto);
1175 }
1176
1177 void
1178 ofproto_destroy(struct ofproto *p)
1179 {
1180 struct ofport *ofport, *next_ofport;
1181
1182 if (!p) {
1183 return;
1184 }
1185
1186 ofproto_flush__(p);
1187 HMAP_FOR_EACH_SAFE (ofport, next_ofport, hmap_node, &p->ports) {
1188 ofport_destroy(ofport);
1189 }
1190
1191 p->ofproto_class->destruct(p);
1192 ofproto_destroy__(p);
1193 }
1194
1195 /* Destroys the datapath with the respective 'name' and 'type'. With the Linux
1196 * kernel datapath, for example, this destroys the datapath in the kernel, and
1197 * with the netdev-based datapath, it tears down the data structures that
1198 * represent the datapath.
1199 *
1200 * The datapath should not be currently open as an ofproto. */
1201 int
1202 ofproto_delete(const char *name, const char *type)
1203 {
1204 const struct ofproto_class *class = ofproto_class_find__(type);
1205 return (!class ? EAFNOSUPPORT
1206 : !class->del ? EACCES
1207 : class->del(type, name));
1208 }
1209
1210 static void
1211 process_port_change(struct ofproto *ofproto, int error, char *devname)
1212 {
1213 if (error == ENOBUFS) {
1214 reinit_ports(ofproto);
1215 } else if (!error) {
1216 update_port(ofproto, devname);
1217 free(devname);
1218 }
1219 }
1220
1221 int
1222 ofproto_type_run(const char *datapath_type)
1223 {
1224 const struct ofproto_class *class;
1225 int error;
1226
1227 datapath_type = ofproto_normalize_type(datapath_type);
1228 class = ofproto_class_find__(datapath_type);
1229
1230 error = class->type_run ? class->type_run(datapath_type) : 0;
1231 if (error && error != EAGAIN) {
1232 VLOG_ERR_RL(&rl, "%s: type_run failed (%s)",
1233 datapath_type, ovs_strerror(error));
1234 }
1235 return error;
1236 }
1237
1238 int
1239 ofproto_type_run_fast(const char *datapath_type)
1240 {
1241 const struct ofproto_class *class;
1242 int error;
1243
1244 datapath_type = ofproto_normalize_type(datapath_type);
1245 class = ofproto_class_find__(datapath_type);
1246
1247 error = class->type_run_fast ? class->type_run_fast(datapath_type) : 0;
1248 if (error && error != EAGAIN) {
1249 VLOG_ERR_RL(&rl, "%s: type_run_fast failed (%s)",
1250 datapath_type, ovs_strerror(error));
1251 }
1252 return error;
1253 }
1254
1255 void
1256 ofproto_type_wait(const char *datapath_type)
1257 {
1258 const struct ofproto_class *class;
1259
1260 datapath_type = ofproto_normalize_type(datapath_type);
1261 class = ofproto_class_find__(datapath_type);
1262
1263 if (class->type_wait) {
1264 class->type_wait(datapath_type);
1265 }
1266 }
1267
1268 int
1269 ofproto_run(struct ofproto *p)
1270 {
1271 struct sset changed_netdevs;
1272 const char *changed_netdev;
1273 struct ofport *ofport;
1274 int error;
1275
1276 error = p->ofproto_class->run(p);
1277 if (error && error != EAGAIN) {
1278 VLOG_ERR_RL(&rl, "%s: run failed (%s)", p->name, ovs_strerror(error));
1279 }
1280
1281 if (p->ofproto_class->port_poll) {
1282 char *devname;
1283
1284 while ((error = p->ofproto_class->port_poll(p, &devname)) != EAGAIN) {
1285 process_port_change(p, error, devname);
1286 }
1287 }
1288
1289 /* Update OpenFlow port status for any port whose netdev has changed.
1290 *
1291 * Refreshing a given 'ofport' can cause an arbitrary ofport to be
1292 * destroyed, so it's not safe to update ports directly from the
1293 * HMAP_FOR_EACH loop, or even to use HMAP_FOR_EACH_SAFE. Instead, we
1294 * need this two-phase approach. */
1295 sset_init(&changed_netdevs);
1296 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
1297 unsigned int change_seq = netdev_change_seq(ofport->netdev);
1298 if (ofport->change_seq != change_seq) {
1299 ofport->change_seq = change_seq;
1300 sset_add(&changed_netdevs, netdev_get_name(ofport->netdev));
1301 }
1302 }
1303 SSET_FOR_EACH (changed_netdev, &changed_netdevs) {
1304 update_port(p, changed_netdev);
1305 }
1306 sset_destroy(&changed_netdevs);
1307
1308 switch (p->state) {
1309 case S_OPENFLOW:
1310 connmgr_run(p->connmgr, handle_openflow);
1311 break;
1312
1313 case S_EVICT:
1314 connmgr_run(p->connmgr, NULL);
1315 ofproto_evict(p);
1316 if (list_is_empty(&p->pending) && hmap_is_empty(&p->deletions)) {
1317 p->state = S_OPENFLOW;
1318 }
1319 break;
1320
1321 case S_FLUSH:
1322 connmgr_run(p->connmgr, NULL);
1323 ofproto_flush__(p);
1324 if (list_is_empty(&p->pending) && hmap_is_empty(&p->deletions)) {
1325 connmgr_flushed(p->connmgr);
1326 p->state = S_OPENFLOW;
1327 }
1328 break;
1329
1330 default:
1331 NOT_REACHED();
1332 }
1333
1334 if (time_msec() >= p->next_op_report) {
1335 long long int ago = (time_msec() - p->first_op) / 1000;
1336 long long int interval = (p->last_op - p->first_op) / 1000;
1337 struct ds s;
1338
1339 ds_init(&s);
1340 ds_put_format(&s, "%d flow_mods ",
1341 p->n_add + p->n_delete + p->n_modify);
1342 if (interval == ago) {
1343 ds_put_format(&s, "in the last %lld s", ago);
1344 } else if (interval) {
1345 ds_put_format(&s, "in the %lld s starting %lld s ago",
1346 interval, ago);
1347 } else {
1348 ds_put_format(&s, "%lld s ago", ago);
1349 }
1350
1351 ds_put_cstr(&s, " (");
1352 if (p->n_add) {
1353 ds_put_format(&s, "%d adds, ", p->n_add);
1354 }
1355 if (p->n_delete) {
1356 ds_put_format(&s, "%d deletes, ", p->n_delete);
1357 }
1358 if (p->n_modify) {
1359 ds_put_format(&s, "%d modifications, ", p->n_modify);
1360 }
1361 s.length -= 2;
1362 ds_put_char(&s, ')');
1363
1364 VLOG_INFO("%s: %s", p->name, ds_cstr(&s));
1365 ds_destroy(&s);
1366
1367 p->n_add = p->n_delete = p->n_modify = 0;
1368 p->next_op_report = LLONG_MAX;
1369 }
1370
1371 return error;
1372 }
1373
1374 /* Performs periodic activity required by 'ofproto' that needs to be done
1375 * with the least possible latency.
1376 *
1377 * It makes sense to call this function a couple of times per poll loop, to
1378 * provide a significant performance boost on some benchmarks with the
1379 * ofproto-dpif implementation. */
1380 int
1381 ofproto_run_fast(struct ofproto *p)
1382 {
1383 int error;
1384
1385 error = p->ofproto_class->run_fast ? p->ofproto_class->run_fast(p) : 0;
1386 if (error && error != EAGAIN) {
1387 VLOG_ERR_RL(&rl, "%s: fastpath run failed (%s)",
1388 p->name, ovs_strerror(error));
1389 }
1390 return error;
1391 }
1392
1393 void
1394 ofproto_wait(struct ofproto *p)
1395 {
1396 struct ofport *ofport;
1397
1398 p->ofproto_class->wait(p);
1399 if (p->ofproto_class->port_poll_wait) {
1400 p->ofproto_class->port_poll_wait(p);
1401 }
1402
1403 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
1404 if (ofport->change_seq != netdev_change_seq(ofport->netdev)) {
1405 poll_immediate_wake();
1406 }
1407 }
1408
1409 switch (p->state) {
1410 case S_OPENFLOW:
1411 connmgr_wait(p->connmgr, true);
1412 break;
1413
1414 case S_EVICT:
1415 case S_FLUSH:
1416 connmgr_wait(p->connmgr, false);
1417 if (list_is_empty(&p->pending) && hmap_is_empty(&p->deletions)) {
1418 poll_immediate_wake();
1419 }
1420 break;
1421 }
1422 }
1423
1424 bool
1425 ofproto_is_alive(const struct ofproto *p)
1426 {
1427 return connmgr_has_controllers(p->connmgr);
1428 }
1429
1430 /* Adds some memory usage statistics for 'ofproto' into 'usage', for use with
1431 * memory_report(). */
1432 void
1433 ofproto_get_memory_usage(const struct ofproto *ofproto, struct simap *usage)
1434 {
1435 const struct oftable *table;
1436 unsigned int n_rules;
1437
1438 simap_increase(usage, "ports", hmap_count(&ofproto->ports));
1439 simap_increase(usage, "ops",
1440 ofproto->n_pending + hmap_count(&ofproto->deletions));
1441
1442 n_rules = 0;
1443 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
1444 ovs_rwlock_rdlock(&table->cls.rwlock);
1445 n_rules += classifier_count(&table->cls);
1446 ovs_rwlock_unlock(&table->cls.rwlock);
1447 }
1448 simap_increase(usage, "rules", n_rules);
1449
1450 if (ofproto->ofproto_class->get_memory_usage) {
1451 ofproto->ofproto_class->get_memory_usage(ofproto, usage);
1452 }
1453
1454 connmgr_get_memory_usage(ofproto->connmgr, usage);
1455 }
1456
1457 void
1458 ofproto_get_ofproto_controller_info(const struct ofproto *ofproto,
1459 struct shash *info)
1460 {
1461 connmgr_get_controller_info(ofproto->connmgr, info);
1462 }
1463
1464 void
1465 ofproto_free_ofproto_controller_info(struct shash *info)
1466 {
1467 connmgr_free_controller_info(info);
1468 }
1469
1470 /* Makes a deep copy of 'old' into 'port'. */
1471 void
1472 ofproto_port_clone(struct ofproto_port *port, const struct ofproto_port *old)
1473 {
1474 port->name = xstrdup(old->name);
1475 port->type = xstrdup(old->type);
1476 port->ofp_port = old->ofp_port;
1477 }
1478
1479 /* Frees memory allocated to members of 'ofproto_port'.
1480 *
1481 * Do not call this function on an ofproto_port obtained from
1482 * ofproto_port_dump_next(): that function retains ownership of the data in the
1483 * ofproto_port. */
1484 void
1485 ofproto_port_destroy(struct ofproto_port *ofproto_port)
1486 {
1487 free(ofproto_port->name);
1488 free(ofproto_port->type);
1489 }
1490
1491 /* Initializes 'dump' to begin dumping the ports in an ofproto.
1492 *
1493 * This function provides no status indication. An error status for the entire
1494 * dump operation is provided when it is completed by calling
1495 * ofproto_port_dump_done().
1496 */
1497 void
1498 ofproto_port_dump_start(struct ofproto_port_dump *dump,
1499 const struct ofproto *ofproto)
1500 {
1501 dump->ofproto = ofproto;
1502 dump->error = ofproto->ofproto_class->port_dump_start(ofproto,
1503 &dump->state);
1504 }
1505
1506 /* Attempts to retrieve another port from 'dump', which must have been created
1507 * with ofproto_port_dump_start(). On success, stores a new ofproto_port into
1508 * 'port' and returns true. On failure, returns false.
1509 *
1510 * Failure might indicate an actual error or merely that the last port has been
1511 * dumped. An error status for the entire dump operation is provided when it
1512 * is completed by calling ofproto_port_dump_done().
1513 *
1514 * The ofproto owns the data stored in 'port'. It will remain valid until at
1515 * least the next time 'dump' is passed to ofproto_port_dump_next() or
1516 * ofproto_port_dump_done(). */
1517 bool
1518 ofproto_port_dump_next(struct ofproto_port_dump *dump,
1519 struct ofproto_port *port)
1520 {
1521 const struct ofproto *ofproto = dump->ofproto;
1522
1523 if (dump->error) {
1524 return false;
1525 }
1526
1527 dump->error = ofproto->ofproto_class->port_dump_next(ofproto, dump->state,
1528 port);
1529 if (dump->error) {
1530 ofproto->ofproto_class->port_dump_done(ofproto, dump->state);
1531 return false;
1532 }
1533 return true;
1534 }
1535
1536 /* Completes port table dump operation 'dump', which must have been created
1537 * with ofproto_port_dump_start(). Returns 0 if the dump operation was
1538 * error-free, otherwise a positive errno value describing the problem. */
1539 int
1540 ofproto_port_dump_done(struct ofproto_port_dump *dump)
1541 {
1542 const struct ofproto *ofproto = dump->ofproto;
1543 if (!dump->error) {
1544 dump->error = ofproto->ofproto_class->port_dump_done(ofproto,
1545 dump->state);
1546 }
1547 return dump->error == EOF ? 0 : dump->error;
1548 }
1549
1550 /* Returns the type to pass to netdev_open() when a datapath of type
1551 * 'datapath_type' has a port of type 'port_type', for a few special
1552 * cases when a netdev type differs from a port type. For example, when
1553 * using the userspace datapath, a port of type "internal" needs to be
1554 * opened as "tap".
1555 *
1556 * Returns either 'type' itself or a string literal, which must not be
1557 * freed. */
1558 const char *
1559 ofproto_port_open_type(const char *datapath_type, const char *port_type)
1560 {
1561 const struct ofproto_class *class;
1562
1563 datapath_type = ofproto_normalize_type(datapath_type);
1564 class = ofproto_class_find__(datapath_type);
1565 if (!class) {
1566 return port_type;
1567 }
1568
1569 return (class->port_open_type
1570 ? class->port_open_type(datapath_type, port_type)
1571 : port_type);
1572 }
1573
1574 /* Attempts to add 'netdev' as a port on 'ofproto'. If 'ofp_portp' is
1575 * non-null and '*ofp_portp' is not OFPP_NONE, attempts to use that as
1576 * the port's OpenFlow port number.
1577 *
1578 * If successful, returns 0 and sets '*ofp_portp' to the new port's
1579 * OpenFlow port number (if 'ofp_portp' is non-null). On failure,
1580 * returns a positive errno value and sets '*ofp_portp' to OFPP_NONE (if
1581 * 'ofp_portp' is non-null). */
1582 int
1583 ofproto_port_add(struct ofproto *ofproto, struct netdev *netdev,
1584 ofp_port_t *ofp_portp)
1585 {
1586 ofp_port_t ofp_port = ofp_portp ? *ofp_portp : OFPP_NONE;
1587 int error;
1588
1589 error = ofproto->ofproto_class->port_add(ofproto, netdev);
1590 if (!error) {
1591 const char *netdev_name = netdev_get_name(netdev);
1592
1593 simap_put(&ofproto->ofp_requests, netdev_name,
1594 ofp_to_u16(ofp_port));
1595 update_port(ofproto, netdev_name);
1596 }
1597 if (ofp_portp) {
1598 struct ofproto_port ofproto_port;
1599
1600 ofproto_port_query_by_name(ofproto, netdev_get_name(netdev),
1601 &ofproto_port);
1602 *ofp_portp = error ? OFPP_NONE : ofproto_port.ofp_port;
1603 ofproto_port_destroy(&ofproto_port);
1604 }
1605 return error;
1606 }
1607
1608 /* Looks up a port named 'devname' in 'ofproto'. On success, returns 0 and
1609 * initializes '*port' appropriately; on failure, returns a positive errno
1610 * value.
1611 *
1612 * The caller owns the data in 'ofproto_port' and must free it with
1613 * ofproto_port_destroy() when it is no longer needed. */
1614 int
1615 ofproto_port_query_by_name(const struct ofproto *ofproto, const char *devname,
1616 struct ofproto_port *port)
1617 {
1618 int error;
1619
1620 error = ofproto->ofproto_class->port_query_by_name(ofproto, devname, port);
1621 if (error) {
1622 memset(port, 0, sizeof *port);
1623 }
1624 return error;
1625 }
1626
1627 /* Deletes port number 'ofp_port' from the datapath for 'ofproto'.
1628 * Returns 0 if successful, otherwise a positive errno. */
1629 int
1630 ofproto_port_del(struct ofproto *ofproto, ofp_port_t ofp_port)
1631 {
1632 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
1633 const char *name = ofport ? netdev_get_name(ofport->netdev) : "<unknown>";
1634 struct simap_node *ofp_request_node;
1635 int error;
1636
1637 ofp_request_node = simap_find(&ofproto->ofp_requests, name);
1638 if (ofp_request_node) {
1639 simap_delete(&ofproto->ofp_requests, ofp_request_node);
1640 }
1641
1642 error = ofproto->ofproto_class->port_del(ofproto, ofp_port);
1643 if (!error && ofport) {
1644 /* 'name' is the netdev's name and update_port() is going to close the
1645 * netdev. Just in case update_port() refers to 'name' after it
1646 * destroys 'ofport', make a copy of it around the update_port()
1647 * call. */
1648 char *devname = xstrdup(name);
1649 update_port(ofproto, devname);
1650 free(devname);
1651 }
1652 return error;
1653 }
1654
1655 /* Adds a flow to OpenFlow flow table 0 in 'p' that matches 'cls_rule' and
1656 * performs the 'n_actions' actions in 'actions'. The new flow will not
1657 * timeout.
1658 *
1659 * If cls_rule->priority is in the range of priorities supported by OpenFlow
1660 * (0...65535, inclusive) then the flow will be visible to OpenFlow
1661 * controllers; otherwise, it will be hidden.
1662 *
1663 * The caller retains ownership of 'cls_rule' and 'ofpacts'.
1664 *
1665 * This is a helper function for in-band control and fail-open. */
1666 void
1667 ofproto_add_flow(struct ofproto *ofproto, const struct match *match,
1668 unsigned int priority,
1669 const struct ofpact *ofpacts, size_t ofpacts_len)
1670 {
1671 const struct rule *rule;
1672
1673 ovs_rwlock_rdlock(&ofproto->tables[0].cls.rwlock);
1674 rule = rule_from_cls_rule(classifier_find_match_exactly(
1675 &ofproto->tables[0].cls, match, priority));
1676 ovs_rwlock_unlock(&ofproto->tables[0].cls.rwlock);
1677 if (!rule || !ofpacts_equal(rule->ofpacts, rule->ofpacts_len,
1678 ofpacts, ofpacts_len)) {
1679 struct ofputil_flow_mod fm;
1680
1681 memset(&fm, 0, sizeof fm);
1682 fm.match = *match;
1683 fm.priority = priority;
1684 fm.buffer_id = UINT32_MAX;
1685 fm.ofpacts = xmemdup(ofpacts, ofpacts_len);
1686 fm.ofpacts_len = ofpacts_len;
1687 add_flow(ofproto, NULL, &fm, NULL);
1688 free(fm.ofpacts);
1689 }
1690 }
1691
1692 /* Executes the flow modification specified in 'fm'. Returns 0 on success, an
1693 * OFPERR_* OpenFlow error code on failure, or OFPROTO_POSTPONE if the
1694 * operation cannot be initiated now but may be retried later.
1695 *
1696 * This is a helper function for in-band control and fail-open. */
1697 int
1698 ofproto_flow_mod(struct ofproto *ofproto, struct ofputil_flow_mod *fm)
1699 {
1700 return handle_flow_mod__(ofproto, NULL, fm, NULL);
1701 }
1702
1703 /* Searches for a rule with matching criteria exactly equal to 'target' in
1704 * ofproto's table 0 and, if it finds one, deletes it.
1705 *
1706 * This is a helper function for in-band control and fail-open. */
1707 bool
1708 ofproto_delete_flow(struct ofproto *ofproto,
1709 const struct match *target, unsigned int priority)
1710 {
1711 struct classifier *cls = &ofproto->tables[0].cls;
1712 struct rule *rule;
1713
1714 ovs_rwlock_rdlock(&cls->rwlock);
1715 rule = rule_from_cls_rule(classifier_find_match_exactly(cls, target,
1716 priority));
1717 ovs_rwlock_unlock(&cls->rwlock);
1718 if (!rule) {
1719 /* No such rule -> success. */
1720 return true;
1721 } else if (rule->pending) {
1722 /* An operation on the rule is already pending -> failure.
1723 * Caller must retry later if it's important. */
1724 return false;
1725 } else {
1726 /* Initiate deletion -> success. */
1727 ovs_rwlock_wrlock(&cls->rwlock);
1728 ofproto_delete_rule(ofproto, cls, rule);
1729 ovs_rwlock_unlock(&cls->rwlock);
1730
1731 return true;
1732 }
1733
1734 }
1735
1736 /* Starts the process of deleting all of the flows from all of ofproto's flow
1737 * tables and then reintroducing the flows required by in-band control and
1738 * fail-open. The process will complete in a later call to ofproto_run(). */
1739 void
1740 ofproto_flush_flows(struct ofproto *ofproto)
1741 {
1742 COVERAGE_INC(ofproto_flush);
1743 ofproto->state = S_FLUSH;
1744 }
1745 \f
1746 static void
1747 reinit_ports(struct ofproto *p)
1748 {
1749 struct ofproto_port_dump dump;
1750 struct sset devnames;
1751 struct ofport *ofport;
1752 struct ofproto_port ofproto_port;
1753 const char *devname;
1754
1755 COVERAGE_INC(ofproto_reinit_ports);
1756
1757 sset_init(&devnames);
1758 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
1759 sset_add(&devnames, netdev_get_name(ofport->netdev));
1760 }
1761 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, p) {
1762 sset_add(&devnames, ofproto_port.name);
1763 }
1764
1765 SSET_FOR_EACH (devname, &devnames) {
1766 update_port(p, devname);
1767 }
1768 sset_destroy(&devnames);
1769 }
1770
1771 static ofp_port_t
1772 alloc_ofp_port(struct ofproto *ofproto, const char *netdev_name)
1773 {
1774 uint16_t port_idx;
1775
1776 port_idx = simap_get(&ofproto->ofp_requests, netdev_name);
1777 port_idx = port_idx ? port_idx : UINT16_MAX;
1778
1779 if (port_idx >= ofproto->max_ports
1780 || bitmap_is_set(ofproto->ofp_port_ids, port_idx)) {
1781 uint16_t end_port_no = ofproto->alloc_port_no;
1782
1783 /* Search for a free OpenFlow port number. We try not to
1784 * immediately reuse them to prevent problems due to old
1785 * flows. */
1786 for (;;) {
1787 if (++ofproto->alloc_port_no >= ofproto->max_ports) {
1788 ofproto->alloc_port_no = 0;
1789 }
1790 if (!bitmap_is_set(ofproto->ofp_port_ids,
1791 ofproto->alloc_port_no)) {
1792 port_idx = ofproto->alloc_port_no;
1793 break;
1794 }
1795 if (ofproto->alloc_port_no == end_port_no) {
1796 return OFPP_NONE;
1797 }
1798 }
1799 }
1800 bitmap_set1(ofproto->ofp_port_ids, port_idx);
1801 return u16_to_ofp(port_idx);
1802 }
1803
1804 static void
1805 dealloc_ofp_port(const struct ofproto *ofproto, ofp_port_t ofp_port)
1806 {
1807 if (ofp_to_u16(ofp_port) < ofproto->max_ports) {
1808 bitmap_set0(ofproto->ofp_port_ids, ofp_to_u16(ofp_port));
1809 }
1810 }
1811
1812 /* Opens and returns a netdev for 'ofproto_port' in 'ofproto', or a null
1813 * pointer if the netdev cannot be opened. On success, also fills in
1814 * 'opp'. */
1815 static struct netdev *
1816 ofport_open(struct ofproto *ofproto,
1817 struct ofproto_port *ofproto_port,
1818 struct ofputil_phy_port *pp)
1819 {
1820 enum netdev_flags flags;
1821 struct netdev *netdev;
1822 int error;
1823
1824 error = netdev_open(ofproto_port->name, ofproto_port->type, &netdev);
1825 if (error) {
1826 VLOG_WARN_RL(&rl, "%s: ignoring port %s (%"PRIu16") because netdev %s "
1827 "cannot be opened (%s)",
1828 ofproto->name,
1829 ofproto_port->name, ofproto_port->ofp_port,
1830 ofproto_port->name, ovs_strerror(error));
1831 return NULL;
1832 }
1833
1834 if (ofproto_port->ofp_port == OFPP_NONE) {
1835 if (!strcmp(ofproto->name, ofproto_port->name)) {
1836 ofproto_port->ofp_port = OFPP_LOCAL;
1837 } else {
1838 ofproto_port->ofp_port = alloc_ofp_port(ofproto,
1839 ofproto_port->name);
1840 }
1841 }
1842 pp->port_no = ofproto_port->ofp_port;
1843 netdev_get_etheraddr(netdev, pp->hw_addr);
1844 ovs_strlcpy(pp->name, ofproto_port->name, sizeof pp->name);
1845 netdev_get_flags(netdev, &flags);
1846 pp->config = flags & NETDEV_UP ? 0 : OFPUTIL_PC_PORT_DOWN;
1847 pp->state = netdev_get_carrier(netdev) ? 0 : OFPUTIL_PS_LINK_DOWN;
1848 netdev_get_features(netdev, &pp->curr, &pp->advertised,
1849 &pp->supported, &pp->peer);
1850 pp->curr_speed = netdev_features_to_bps(pp->curr, 0) / 1000;
1851 pp->max_speed = netdev_features_to_bps(pp->supported, 0) / 1000;
1852
1853 return netdev;
1854 }
1855
1856 /* Returns true if most fields of 'a' and 'b' are equal. Differences in name,
1857 * port number, and 'config' bits other than OFPUTIL_PS_LINK_DOWN are
1858 * disregarded. */
1859 static bool
1860 ofport_equal(const struct ofputil_phy_port *a,
1861 const struct ofputil_phy_port *b)
1862 {
1863 return (eth_addr_equals(a->hw_addr, b->hw_addr)
1864 && a->state == b->state
1865 && !((a->config ^ b->config) & OFPUTIL_PC_PORT_DOWN)
1866 && a->curr == b->curr
1867 && a->advertised == b->advertised
1868 && a->supported == b->supported
1869 && a->peer == b->peer
1870 && a->curr_speed == b->curr_speed
1871 && a->max_speed == b->max_speed);
1872 }
1873
1874 /* Adds an ofport to 'p' initialized based on the given 'netdev' and 'opp'.
1875 * The caller must ensure that 'p' does not have a conflicting ofport (that is,
1876 * one with the same name or port number). */
1877 static void
1878 ofport_install(struct ofproto *p,
1879 struct netdev *netdev, const struct ofputil_phy_port *pp)
1880 {
1881 const char *netdev_name = netdev_get_name(netdev);
1882 struct ofport *ofport;
1883 int error;
1884
1885 /* Create ofport. */
1886 ofport = p->ofproto_class->port_alloc();
1887 if (!ofport) {
1888 error = ENOMEM;
1889 goto error;
1890 }
1891 ofport->ofproto = p;
1892 ofport->netdev = netdev;
1893 ofport->change_seq = netdev_change_seq(netdev);
1894 ofport->pp = *pp;
1895 ofport->ofp_port = pp->port_no;
1896 ofport->created = time_msec();
1897
1898 /* Add port to 'p'. */
1899 hmap_insert(&p->ports, &ofport->hmap_node,
1900 hash_ofp_port(ofport->ofp_port));
1901 shash_add(&p->port_by_name, netdev_name, ofport);
1902
1903 update_mtu(p, ofport);
1904
1905 /* Let the ofproto_class initialize its private data. */
1906 error = p->ofproto_class->port_construct(ofport);
1907 if (error) {
1908 goto error;
1909 }
1910 connmgr_send_port_status(p->connmgr, pp, OFPPR_ADD);
1911 return;
1912
1913 error:
1914 VLOG_WARN_RL(&rl, "%s: could not add port %s (%s)",
1915 p->name, netdev_name, ovs_strerror(error));
1916 if (ofport) {
1917 ofport_destroy__(ofport);
1918 } else {
1919 netdev_close(netdev);
1920 }
1921 }
1922
1923 /* Removes 'ofport' from 'p' and destroys it. */
1924 static void
1925 ofport_remove(struct ofport *ofport)
1926 {
1927 connmgr_send_port_status(ofport->ofproto->connmgr, &ofport->pp,
1928 OFPPR_DELETE);
1929 ofport_destroy(ofport);
1930 }
1931
1932 /* If 'ofproto' contains an ofport named 'name', removes it from 'ofproto' and
1933 * destroys it. */
1934 static void
1935 ofport_remove_with_name(struct ofproto *ofproto, const char *name)
1936 {
1937 struct ofport *port = shash_find_data(&ofproto->port_by_name, name);
1938 if (port) {
1939 ofport_remove(port);
1940 }
1941 }
1942
1943 /* Updates 'port' with new 'pp' description.
1944 *
1945 * Does not handle a name or port number change. The caller must implement
1946 * such a change as a delete followed by an add. */
1947 static void
1948 ofport_modified(struct ofport *port, struct ofputil_phy_port *pp)
1949 {
1950 memcpy(port->pp.hw_addr, pp->hw_addr, ETH_ADDR_LEN);
1951 port->pp.config = ((port->pp.config & ~OFPUTIL_PC_PORT_DOWN)
1952 | (pp->config & OFPUTIL_PC_PORT_DOWN));
1953 port->pp.state = pp->state;
1954 port->pp.curr = pp->curr;
1955 port->pp.advertised = pp->advertised;
1956 port->pp.supported = pp->supported;
1957 port->pp.peer = pp->peer;
1958 port->pp.curr_speed = pp->curr_speed;
1959 port->pp.max_speed = pp->max_speed;
1960
1961 connmgr_send_port_status(port->ofproto->connmgr, &port->pp, OFPPR_MODIFY);
1962 }
1963
1964 /* Update OpenFlow 'state' in 'port' and notify controller. */
1965 void
1966 ofproto_port_set_state(struct ofport *port, enum ofputil_port_state state)
1967 {
1968 if (port->pp.state != state) {
1969 port->pp.state = state;
1970 connmgr_send_port_status(port->ofproto->connmgr, &port->pp,
1971 OFPPR_MODIFY);
1972 }
1973 }
1974
1975 void
1976 ofproto_port_unregister(struct ofproto *ofproto, ofp_port_t ofp_port)
1977 {
1978 struct ofport *port = ofproto_get_port(ofproto, ofp_port);
1979 if (port) {
1980 if (port->ofproto->ofproto_class->set_realdev) {
1981 port->ofproto->ofproto_class->set_realdev(port, 0, 0);
1982 }
1983 if (port->ofproto->ofproto_class->set_stp_port) {
1984 port->ofproto->ofproto_class->set_stp_port(port, NULL);
1985 }
1986 if (port->ofproto->ofproto_class->set_cfm) {
1987 port->ofproto->ofproto_class->set_cfm(port, NULL);
1988 }
1989 if (port->ofproto->ofproto_class->bundle_remove) {
1990 port->ofproto->ofproto_class->bundle_remove(port);
1991 }
1992 }
1993 }
1994
1995 static void
1996 ofport_destroy__(struct ofport *port)
1997 {
1998 struct ofproto *ofproto = port->ofproto;
1999 const char *name = netdev_get_name(port->netdev);
2000
2001 hmap_remove(&ofproto->ports, &port->hmap_node);
2002 shash_delete(&ofproto->port_by_name,
2003 shash_find(&ofproto->port_by_name, name));
2004
2005 netdev_close(port->netdev);
2006 ofproto->ofproto_class->port_dealloc(port);
2007 }
2008
2009 static void
2010 ofport_destroy(struct ofport *port)
2011 {
2012 if (port) {
2013 dealloc_ofp_port(port->ofproto, port->ofp_port);
2014 port->ofproto->ofproto_class->port_destruct(port);
2015 ofport_destroy__(port);
2016 }
2017 }
2018
2019 struct ofport *
2020 ofproto_get_port(const struct ofproto *ofproto, ofp_port_t ofp_port)
2021 {
2022 struct ofport *port;
2023
2024 HMAP_FOR_EACH_IN_BUCKET (port, hmap_node, hash_ofp_port(ofp_port),
2025 &ofproto->ports) {
2026 if (port->ofp_port == ofp_port) {
2027 return port;
2028 }
2029 }
2030 return NULL;
2031 }
2032
2033 int
2034 ofproto_port_get_stats(const struct ofport *port, struct netdev_stats *stats)
2035 {
2036 struct ofproto *ofproto = port->ofproto;
2037 int error;
2038
2039 if (ofproto->ofproto_class->port_get_stats) {
2040 error = ofproto->ofproto_class->port_get_stats(port, stats);
2041 } else {
2042 error = EOPNOTSUPP;
2043 }
2044
2045 return error;
2046 }
2047
2048 static void
2049 update_port(struct ofproto *ofproto, const char *name)
2050 {
2051 struct ofproto_port ofproto_port;
2052 struct ofputil_phy_port pp;
2053 struct netdev *netdev;
2054 struct ofport *port;
2055
2056 COVERAGE_INC(ofproto_update_port);
2057
2058 /* Fetch 'name''s location and properties from the datapath. */
2059 netdev = (!ofproto_port_query_by_name(ofproto, name, &ofproto_port)
2060 ? ofport_open(ofproto, &ofproto_port, &pp)
2061 : NULL);
2062
2063 if (netdev) {
2064 port = ofproto_get_port(ofproto, ofproto_port.ofp_port);
2065 if (port && !strcmp(netdev_get_name(port->netdev), name)) {
2066 struct netdev *old_netdev = port->netdev;
2067
2068 /* 'name' hasn't changed location. Any properties changed? */
2069 if (!ofport_equal(&port->pp, &pp)) {
2070 ofport_modified(port, &pp);
2071 }
2072
2073 update_mtu(ofproto, port);
2074
2075 /* Install the newly opened netdev in case it has changed.
2076 * Don't close the old netdev yet in case port_modified has to
2077 * remove a retained reference to it.*/
2078 port->netdev = netdev;
2079 port->change_seq = netdev_change_seq(netdev);
2080
2081 if (port->ofproto->ofproto_class->port_modified) {
2082 port->ofproto->ofproto_class->port_modified(port);
2083 }
2084
2085 netdev_close(old_netdev);
2086 } else {
2087 /* If 'port' is nonnull then its name differs from 'name' and thus
2088 * we should delete it. If we think there's a port named 'name'
2089 * then its port number must be wrong now so delete it too. */
2090 if (port) {
2091 ofport_remove(port);
2092 }
2093 ofport_remove_with_name(ofproto, name);
2094 ofport_install(ofproto, netdev, &pp);
2095 }
2096 } else {
2097 /* Any port named 'name' is gone now. */
2098 ofport_remove_with_name(ofproto, name);
2099 }
2100 ofproto_port_destroy(&ofproto_port);
2101 }
2102
2103 static int
2104 init_ports(struct ofproto *p)
2105 {
2106 struct ofproto_port_dump dump;
2107 struct ofproto_port ofproto_port;
2108 struct shash_node *node, *next;
2109
2110 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, p) {
2111 const char *name = ofproto_port.name;
2112
2113 if (shash_find(&p->port_by_name, name)) {
2114 VLOG_WARN_RL(&rl, "%s: ignoring duplicate device %s in datapath",
2115 p->name, name);
2116 } else {
2117 struct ofputil_phy_port pp;
2118 struct netdev *netdev;
2119
2120 /* Check if an OpenFlow port number had been requested. */
2121 node = shash_find(&init_ofp_ports, name);
2122 if (node) {
2123 const struct iface_hint *iface_hint = node->data;
2124 simap_put(&p->ofp_requests, name,
2125 ofp_to_u16(iface_hint->ofp_port));
2126 }
2127
2128 netdev = ofport_open(p, &ofproto_port, &pp);
2129 if (netdev) {
2130 ofport_install(p, netdev, &pp);
2131 if (ofp_to_u16(ofproto_port.ofp_port) < p->max_ports) {
2132 p->alloc_port_no = MAX(p->alloc_port_no,
2133 ofp_to_u16(ofproto_port.ofp_port));
2134 }
2135 }
2136 }
2137 }
2138
2139 SHASH_FOR_EACH_SAFE(node, next, &init_ofp_ports) {
2140 struct iface_hint *iface_hint = node->data;
2141
2142 if (!strcmp(iface_hint->br_name, p->name)) {
2143 free(iface_hint->br_name);
2144 free(iface_hint->br_type);
2145 free(iface_hint);
2146 shash_delete(&init_ofp_ports, node);
2147 }
2148 }
2149
2150 return 0;
2151 }
2152
2153 /* Find the minimum MTU of all non-datapath devices attached to 'p'.
2154 * Returns ETH_PAYLOAD_MAX or the minimum of the ports. */
2155 static int
2156 find_min_mtu(struct ofproto *p)
2157 {
2158 struct ofport *ofport;
2159 int mtu = 0;
2160
2161 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
2162 struct netdev *netdev = ofport->netdev;
2163 int dev_mtu;
2164
2165 /* Skip any internal ports, since that's what we're trying to
2166 * set. */
2167 if (!strcmp(netdev_get_type(netdev), "internal")) {
2168 continue;
2169 }
2170
2171 if (netdev_get_mtu(netdev, &dev_mtu)) {
2172 continue;
2173 }
2174 if (!mtu || dev_mtu < mtu) {
2175 mtu = dev_mtu;
2176 }
2177 }
2178
2179 return mtu ? mtu: ETH_PAYLOAD_MAX;
2180 }
2181
2182 /* Update MTU of all datapath devices on 'p' to the minimum of the
2183 * non-datapath ports in event of 'port' added or changed. */
2184 static void
2185 update_mtu(struct ofproto *p, struct ofport *port)
2186 {
2187 struct ofport *ofport;
2188 struct netdev *netdev = port->netdev;
2189 int dev_mtu, old_min;
2190
2191 if (netdev_get_mtu(netdev, &dev_mtu)) {
2192 port->mtu = 0;
2193 return;
2194 }
2195 if (!strcmp(netdev_get_type(port->netdev), "internal")) {
2196 if (dev_mtu > p->min_mtu) {
2197 if (!netdev_set_mtu(port->netdev, p->min_mtu)) {
2198 dev_mtu = p->min_mtu;
2199 }
2200 }
2201 port->mtu = dev_mtu;
2202 return;
2203 }
2204
2205 /* For non-internal port find new min mtu. */
2206 old_min = p->min_mtu;
2207 port->mtu = dev_mtu;
2208 p->min_mtu = find_min_mtu(p);
2209 if (p->min_mtu == old_min) {
2210 return;
2211 }
2212
2213 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
2214 struct netdev *netdev = ofport->netdev;
2215
2216 if (!strcmp(netdev_get_type(netdev), "internal")) {
2217 if (!netdev_set_mtu(netdev, p->min_mtu)) {
2218 ofport->mtu = p->min_mtu;
2219 }
2220 }
2221 }
2222 }
2223 \f
2224 static void
2225 ofproto_rule_destroy__(struct rule *rule)
2226 {
2227 if (rule) {
2228 rule->ofproto->ofproto_class->rule_destruct(rule);
2229 cls_rule_destroy(&rule->cr);
2230 free(rule->ofpacts);
2231 ovs_mutex_destroy(&rule->timeout_mutex);
2232 ovs_rwlock_destroy(&rule->evict);
2233 rule->ofproto->ofproto_class->rule_dealloc(rule);
2234 }
2235 }
2236
2237 /* This function allows an ofproto implementation to destroy any rules that
2238 * remain when its ->destruct() function is called.. This function implements
2239 * steps 4.4 and 4.5 in the section titled "Rule Life Cycle" in
2240 * ofproto-provider.h.
2241 *
2242 * This function should only be called from an ofproto implementation's
2243 * ->destruct() function. It is not suitable elsewhere. */
2244 void
2245 ofproto_rule_delete(struct ofproto *ofproto, struct classifier *cls,
2246 struct rule *rule)
2247 OVS_REQ_WRLOCK(cls->rwlock)
2248 {
2249 ofproto_delete_rule(ofproto, cls, rule);
2250 }
2251
2252 /* Returns true if 'rule' has an OpenFlow OFPAT_OUTPUT or OFPAT_ENQUEUE action
2253 * that outputs to 'port' (output to OFPP_FLOOD and OFPP_ALL doesn't count). */
2254 bool
2255 ofproto_rule_has_out_port(const struct rule *rule, ofp_port_t port)
2256 {
2257 return (port == OFPP_ANY
2258 || ofpacts_output_to_port(rule->ofpacts, rule->ofpacts_len, port));
2259 }
2260
2261 /* Returns true if 'rule' has group and equals group_id. */
2262 bool
2263 ofproto_rule_has_out_group(const struct rule *rule, uint32_t group_id)
2264 {
2265 return (group_id == OFPG11_ANY
2266 || ofpacts_output_to_group(rule->ofpacts, rule->ofpacts_len, group_id));
2267 }
2268
2269 /* Returns true if a rule related to 'op' has an OpenFlow OFPAT_OUTPUT or
2270 * OFPAT_ENQUEUE action that outputs to 'out_port'. */
2271 bool
2272 ofoperation_has_out_port(const struct ofoperation *op, ofp_port_t out_port)
2273 {
2274 if (ofproto_rule_has_out_port(op->rule, out_port)) {
2275 return true;
2276 }
2277
2278 switch (op->type) {
2279 case OFOPERATION_ADD:
2280 case OFOPERATION_DELETE:
2281 return false;
2282
2283 case OFOPERATION_MODIFY:
2284 case OFOPERATION_REPLACE:
2285 return ofpacts_output_to_port(op->ofpacts, op->ofpacts_len, out_port);
2286 }
2287
2288 NOT_REACHED();
2289 }
2290
2291 /* Executes the actions indicated by 'rule' on 'packet' and credits 'rule''s
2292 * statistics appropriately.
2293 *
2294 * 'packet' doesn't necessarily have to match 'rule'. 'rule' will be credited
2295 * with statistics for 'packet' either way.
2296 *
2297 * Takes ownership of 'packet'. */
2298 static int
2299 rule_execute(struct rule *rule, ofp_port_t in_port, struct ofpbuf *packet)
2300 {
2301 struct flow flow;
2302 union flow_in_port in_port_;
2303
2304 in_port_.ofp_port = in_port;
2305 flow_extract(packet, 0, 0, NULL, &in_port_, &flow);
2306 return rule->ofproto->ofproto_class->rule_execute(rule, &flow, packet);
2307 }
2308
2309 /* Returns true if 'rule' should be hidden from the controller.
2310 *
2311 * Rules with priority higher than UINT16_MAX are set up by ofproto itself
2312 * (e.g. by in-band control) and are intentionally hidden from the
2313 * controller. */
2314 bool
2315 ofproto_rule_is_hidden(const struct rule *rule)
2316 {
2317 return rule->cr.priority > UINT16_MAX;
2318 }
2319
2320 static enum oftable_flags
2321 rule_get_flags(const struct rule *rule)
2322 {
2323 return rule->ofproto->tables[rule->table_id].flags;
2324 }
2325
2326 static bool
2327 rule_is_modifiable(const struct rule *rule)
2328 {
2329 return !(rule_get_flags(rule) & OFTABLE_READONLY);
2330 }
2331 \f
2332 static enum ofperr
2333 handle_echo_request(struct ofconn *ofconn, const struct ofp_header *oh)
2334 {
2335 ofconn_send_reply(ofconn, make_echo_reply(oh));
2336 return 0;
2337 }
2338
2339 static enum ofperr
2340 handle_features_request(struct ofconn *ofconn, const struct ofp_header *oh)
2341 {
2342 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2343 struct ofputil_switch_features features;
2344 struct ofport *port;
2345 bool arp_match_ip;
2346 struct ofpbuf *b;
2347 int n_tables;
2348 int i;
2349
2350 ofproto->ofproto_class->get_features(ofproto, &arp_match_ip,
2351 &features.actions);
2352 ovs_assert(features.actions & OFPUTIL_A_OUTPUT); /* sanity check */
2353
2354 /* Count only non-hidden tables in the number of tables. (Hidden tables,
2355 * if present, are always at the end.) */
2356 n_tables = ofproto->n_tables;
2357 for (i = 0; i < ofproto->n_tables; i++) {
2358 if (ofproto->tables[i].flags & OFTABLE_HIDDEN) {
2359 n_tables = i;
2360 break;
2361 }
2362 }
2363
2364 features.datapath_id = ofproto->datapath_id;
2365 features.n_buffers = pktbuf_capacity();
2366 features.n_tables = n_tables;
2367 features.capabilities = (OFPUTIL_C_FLOW_STATS | OFPUTIL_C_TABLE_STATS |
2368 OFPUTIL_C_PORT_STATS | OFPUTIL_C_QUEUE_STATS);
2369 if (arp_match_ip) {
2370 features.capabilities |= OFPUTIL_C_ARP_MATCH_IP;
2371 }
2372 /* FIXME: Fill in proper features.auxiliary_id for auxiliary connections */
2373 features.auxiliary_id = 0;
2374 b = ofputil_encode_switch_features(&features, ofconn_get_protocol(ofconn),
2375 oh->xid);
2376 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
2377 ofputil_put_switch_features_port(&port->pp, b);
2378 }
2379
2380 ofconn_send_reply(ofconn, b);
2381 return 0;
2382 }
2383
2384 static enum ofperr
2385 handle_get_config_request(struct ofconn *ofconn, const struct ofp_header *oh)
2386 {
2387 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2388 struct ofp_switch_config *osc;
2389 enum ofp_config_flags flags;
2390 struct ofpbuf *buf;
2391
2392 /* Send reply. */
2393 buf = ofpraw_alloc_reply(OFPRAW_OFPT_GET_CONFIG_REPLY, oh, 0);
2394 osc = ofpbuf_put_uninit(buf, sizeof *osc);
2395 flags = ofproto->frag_handling;
2396 /* OFPC_INVALID_TTL_TO_CONTROLLER is deprecated in OF 1.3 */
2397 if (oh->version < OFP13_VERSION
2398 && ofconn_get_invalid_ttl_to_controller(ofconn)) {
2399 flags |= OFPC_INVALID_TTL_TO_CONTROLLER;
2400 }
2401 osc->flags = htons(flags);
2402 osc->miss_send_len = htons(ofconn_get_miss_send_len(ofconn));
2403 ofconn_send_reply(ofconn, buf);
2404
2405 return 0;
2406 }
2407
2408 static enum ofperr
2409 handle_set_config(struct ofconn *ofconn, const struct ofp_header *oh)
2410 {
2411 const struct ofp_switch_config *osc = ofpmsg_body(oh);
2412 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
2413 uint16_t flags = ntohs(osc->flags);
2414
2415 if (ofconn_get_type(ofconn) != OFCONN_PRIMARY
2416 || ofconn_get_role(ofconn) != OFPCR12_ROLE_SLAVE) {
2417 enum ofp_config_flags cur = ofproto->frag_handling;
2418 enum ofp_config_flags next = flags & OFPC_FRAG_MASK;
2419
2420 ovs_assert((cur & OFPC_FRAG_MASK) == cur);
2421 if (cur != next) {
2422 if (ofproto->ofproto_class->set_frag_handling(ofproto, next)) {
2423 ofproto->frag_handling = next;
2424 } else {
2425 VLOG_WARN_RL(&rl, "%s: unsupported fragment handling mode %s",
2426 ofproto->name,
2427 ofputil_frag_handling_to_string(next));
2428 }
2429 }
2430 }
2431 /* OFPC_INVALID_TTL_TO_CONTROLLER is deprecated in OF 1.3 */
2432 ofconn_set_invalid_ttl_to_controller(ofconn,
2433 (oh->version < OFP13_VERSION
2434 && flags & OFPC_INVALID_TTL_TO_CONTROLLER));
2435
2436 ofconn_set_miss_send_len(ofconn, ntohs(osc->miss_send_len));
2437
2438 return 0;
2439 }
2440
2441 /* Checks whether 'ofconn' is a slave controller. If so, returns an OpenFlow
2442 * error message code for the caller to propagate upward. Otherwise, returns
2443 * 0.
2444 *
2445 * The log message mentions 'msg_type'. */
2446 static enum ofperr
2447 reject_slave_controller(struct ofconn *ofconn)
2448 {
2449 if (ofconn_get_type(ofconn) == OFCONN_PRIMARY
2450 && ofconn_get_role(ofconn) == OFPCR12_ROLE_SLAVE) {
2451 return OFPERR_OFPBRC_EPERM;
2452 } else {
2453 return 0;
2454 }
2455 }
2456
2457 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
2458 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
2459 *
2460 * This function relies on the order of 'ofpacts' being correct (as checked by
2461 * ofpacts_verify()). */
2462 static uint32_t
2463 find_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
2464 {
2465 const struct ofpact *a;
2466
2467 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2468 enum ovs_instruction_type inst;
2469
2470 inst = ovs_instruction_type_from_ofpact_type(a->type);
2471 if (a->type == OFPACT_METER) {
2472 return ofpact_get_METER(a)->meter_id;
2473 } else if (inst > OVSINST_OFPIT13_METER) {
2474 break;
2475 }
2476 }
2477
2478 return 0;
2479 }
2480
2481 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are appropriate
2482 * for a packet with the prerequisites satisfied by 'flow' in table 'table_id'.
2483 * 'flow' may be temporarily modified, but is restored at return.
2484 */
2485 static enum ofperr
2486 ofproto_check_ofpacts(struct ofproto *ofproto,
2487 const struct ofpact ofpacts[], size_t ofpacts_len,
2488 struct flow *flow, uint8_t table_id)
2489 {
2490 enum ofperr error;
2491 uint32_t mid;
2492
2493 error = ofpacts_check(ofpacts, ofpacts_len, flow,
2494 u16_to_ofp(ofproto->max_ports), table_id);
2495 if (error) {
2496 return error;
2497 }
2498
2499 mid = find_meter(ofpacts, ofpacts_len);
2500 if (mid && ofproto_get_provider_meter_id(ofproto, mid) == UINT32_MAX) {
2501 return OFPERR_OFPMMFC_INVALID_METER;
2502 }
2503 return 0;
2504 }
2505
2506 static enum ofperr
2507 handle_packet_out(struct ofconn *ofconn, const struct ofp_header *oh)
2508 {
2509 struct ofproto *p = ofconn_get_ofproto(ofconn);
2510 struct ofputil_packet_out po;
2511 struct ofpbuf *payload;
2512 uint64_t ofpacts_stub[1024 / 8];
2513 struct ofpbuf ofpacts;
2514 struct flow flow;
2515 union flow_in_port in_port_;
2516 enum ofperr error;
2517
2518 COVERAGE_INC(ofproto_packet_out);
2519
2520 error = reject_slave_controller(ofconn);
2521 if (error) {
2522 goto exit;
2523 }
2524
2525 /* Decode message. */
2526 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
2527 error = ofputil_decode_packet_out(&po, oh, &ofpacts);
2528 if (error) {
2529 goto exit_free_ofpacts;
2530 }
2531 if (ofp_to_u16(po.in_port) >= p->max_ports
2532 && ofp_to_u16(po.in_port) < ofp_to_u16(OFPP_MAX)) {
2533 error = OFPERR_OFPBRC_BAD_PORT;
2534 goto exit_free_ofpacts;
2535 }
2536
2537
2538 /* Get payload. */
2539 if (po.buffer_id != UINT32_MAX) {
2540 error = ofconn_pktbuf_retrieve(ofconn, po.buffer_id, &payload, NULL);
2541 if (error || !payload) {
2542 goto exit_free_ofpacts;
2543 }
2544 } else {
2545 /* Ensure that the L3 header is 32-bit aligned. */
2546 payload = ofpbuf_clone_data_with_headroom(po.packet, po.packet_len, 2);
2547 }
2548
2549 /* Verify actions against packet, then send packet if successful. */
2550 in_port_.ofp_port = po.in_port;
2551 flow_extract(payload, 0, 0, NULL, &in_port_, &flow);
2552 error = ofproto_check_ofpacts(p, po.ofpacts, po.ofpacts_len, &flow, 0);
2553 if (!error) {
2554 error = p->ofproto_class->packet_out(p, payload, &flow,
2555 po.ofpacts, po.ofpacts_len);
2556 }
2557 ofpbuf_delete(payload);
2558
2559 exit_free_ofpacts:
2560 ofpbuf_uninit(&ofpacts);
2561 exit:
2562 return error;
2563 }
2564
2565 static void
2566 update_port_config(struct ofport *port,
2567 enum ofputil_port_config config,
2568 enum ofputil_port_config mask)
2569 {
2570 enum ofputil_port_config old_config = port->pp.config;
2571 enum ofputil_port_config toggle;
2572
2573 toggle = (config ^ port->pp.config) & mask;
2574 if (toggle & OFPUTIL_PC_PORT_DOWN) {
2575 if (config & OFPUTIL_PC_PORT_DOWN) {
2576 netdev_turn_flags_off(port->netdev, NETDEV_UP, NULL);
2577 } else {
2578 netdev_turn_flags_on(port->netdev, NETDEV_UP, NULL);
2579 }
2580 toggle &= ~OFPUTIL_PC_PORT_DOWN;
2581 }
2582
2583 port->pp.config ^= toggle;
2584 if (port->pp.config != old_config) {
2585 port->ofproto->ofproto_class->port_reconfigured(port, old_config);
2586 }
2587 }
2588
2589 static enum ofperr
2590 handle_port_mod(struct ofconn *ofconn, const struct ofp_header *oh)
2591 {
2592 struct ofproto *p = ofconn_get_ofproto(ofconn);
2593 struct ofputil_port_mod pm;
2594 struct ofport *port;
2595 enum ofperr error;
2596
2597 error = reject_slave_controller(ofconn);
2598 if (error) {
2599 return error;
2600 }
2601
2602 error = ofputil_decode_port_mod(oh, &pm);
2603 if (error) {
2604 return error;
2605 }
2606
2607 port = ofproto_get_port(p, pm.port_no);
2608 if (!port) {
2609 return OFPERR_OFPPMFC_BAD_PORT;
2610 } else if (!eth_addr_equals(port->pp.hw_addr, pm.hw_addr)) {
2611 return OFPERR_OFPPMFC_BAD_HW_ADDR;
2612 } else {
2613 update_port_config(port, pm.config, pm.mask);
2614 if (pm.advertise) {
2615 netdev_set_advertisements(port->netdev, pm.advertise);
2616 }
2617 }
2618 return 0;
2619 }
2620
2621 static enum ofperr
2622 handle_desc_stats_request(struct ofconn *ofconn,
2623 const struct ofp_header *request)
2624 {
2625 static const char *default_mfr_desc = "Nicira, Inc.";
2626 static const char *default_hw_desc = "Open vSwitch";
2627 static const char *default_sw_desc = VERSION;
2628 static const char *default_serial_desc = "None";
2629 static const char *default_dp_desc = "None";
2630
2631 struct ofproto *p = ofconn_get_ofproto(ofconn);
2632 struct ofp_desc_stats *ods;
2633 struct ofpbuf *msg;
2634
2635 msg = ofpraw_alloc_stats_reply(request, 0);
2636 ods = ofpbuf_put_zeros(msg, sizeof *ods);
2637 ovs_strlcpy(ods->mfr_desc, p->mfr_desc ? p->mfr_desc : default_mfr_desc,
2638 sizeof ods->mfr_desc);
2639 ovs_strlcpy(ods->hw_desc, p->hw_desc ? p->hw_desc : default_hw_desc,
2640 sizeof ods->hw_desc);
2641 ovs_strlcpy(ods->sw_desc, p->sw_desc ? p->sw_desc : default_sw_desc,
2642 sizeof ods->sw_desc);
2643 ovs_strlcpy(ods->serial_num,
2644 p->serial_desc ? p->serial_desc : default_serial_desc,
2645 sizeof ods->serial_num);
2646 ovs_strlcpy(ods->dp_desc, p->dp_desc ? p->dp_desc : default_dp_desc,
2647 sizeof ods->dp_desc);
2648 ofconn_send_reply(ofconn, msg);
2649
2650 return 0;
2651 }
2652
2653 static enum ofperr
2654 handle_table_stats_request(struct ofconn *ofconn,
2655 const struct ofp_header *request)
2656 {
2657 struct ofproto *p = ofconn_get_ofproto(ofconn);
2658 struct ofp12_table_stats *ots;
2659 struct ofpbuf *msg;
2660 int n_tables;
2661 size_t i;
2662
2663 /* Set up default values.
2664 *
2665 * ofp12_table_stats is used as a generic structure as
2666 * it is able to hold all the fields for ofp10_table_stats
2667 * and ofp11_table_stats (and of course itself).
2668 */
2669 ots = xcalloc(p->n_tables, sizeof *ots);
2670 for (i = 0; i < p->n_tables; i++) {
2671 ots[i].table_id = i;
2672 sprintf(ots[i].name, "table%zu", i);
2673 ots[i].match = htonll(OFPXMT12_MASK);
2674 ots[i].wildcards = htonll(OFPXMT12_MASK);
2675 ots[i].write_actions = htonl(OFPAT11_OUTPUT);
2676 ots[i].apply_actions = htonl(OFPAT11_OUTPUT);
2677 ots[i].write_setfields = htonll(OFPXMT12_MASK);
2678 ots[i].apply_setfields = htonll(OFPXMT12_MASK);
2679 ots[i].metadata_match = htonll(UINT64_MAX);
2680 ots[i].metadata_write = htonll(UINT64_MAX);
2681 ots[i].instructions = htonl(OFPIT11_ALL);
2682 ots[i].config = htonl(OFPTC11_TABLE_MISS_MASK);
2683 ots[i].max_entries = htonl(1000000); /* An arbitrary big number. */
2684 ovs_rwlock_rdlock(&p->tables[i].cls.rwlock);
2685 ots[i].active_count = htonl(classifier_count(&p->tables[i].cls));
2686 ovs_rwlock_unlock(&p->tables[i].cls.rwlock);
2687 }
2688
2689 p->ofproto_class->get_tables(p, ots);
2690
2691 /* Post-process the tables, dropping hidden tables. */
2692 n_tables = p->n_tables;
2693 for (i = 0; i < p->n_tables; i++) {
2694 const struct oftable *table = &p->tables[i];
2695
2696 if (table->flags & OFTABLE_HIDDEN) {
2697 n_tables = i;
2698 break;
2699 }
2700
2701 if (table->name) {
2702 ovs_strzcpy(ots[i].name, table->name, sizeof ots[i].name);
2703 }
2704
2705 if (table->max_flows < ntohl(ots[i].max_entries)) {
2706 ots[i].max_entries = htonl(table->max_flows);
2707 }
2708 }
2709
2710 msg = ofputil_encode_table_stats_reply(ots, n_tables, request);
2711 ofconn_send_reply(ofconn, msg);
2712
2713 free(ots);
2714
2715 return 0;
2716 }
2717
2718 static void
2719 append_port_stat(struct ofport *port, struct list *replies)
2720 {
2721 struct ofputil_port_stats ops = { .port_no = port->pp.port_no };
2722
2723 calc_duration(port->created, time_msec(),
2724 &ops.duration_sec, &ops.duration_nsec);
2725
2726 /* Intentionally ignore return value, since errors will set
2727 * 'stats' to all-1s, which is correct for OpenFlow, and
2728 * netdev_get_stats() will log errors. */
2729 ofproto_port_get_stats(port, &ops.stats);
2730
2731 ofputil_append_port_stat(replies, &ops);
2732 }
2733
2734 static enum ofperr
2735 handle_port_stats_request(struct ofconn *ofconn,
2736 const struct ofp_header *request)
2737 {
2738 struct ofproto *p = ofconn_get_ofproto(ofconn);
2739 struct ofport *port;
2740 struct list replies;
2741 ofp_port_t port_no;
2742 enum ofperr error;
2743
2744 error = ofputil_decode_port_stats_request(request, &port_no);
2745 if (error) {
2746 return error;
2747 }
2748
2749 ofpmp_init(&replies, request);
2750 if (port_no != OFPP_ANY) {
2751 port = ofproto_get_port(p, port_no);
2752 if (port) {
2753 append_port_stat(port, &replies);
2754 }
2755 } else {
2756 HMAP_FOR_EACH (port, hmap_node, &p->ports) {
2757 append_port_stat(port, &replies);
2758 }
2759 }
2760
2761 ofconn_send_replies(ofconn, &replies);
2762 return 0;
2763 }
2764
2765 static enum ofperr
2766 handle_port_desc_stats_request(struct ofconn *ofconn,
2767 const struct ofp_header *request)
2768 {
2769 struct ofproto *p = ofconn_get_ofproto(ofconn);
2770 enum ofp_version version;
2771 struct ofport *port;
2772 struct list replies;
2773
2774 ofpmp_init(&replies, request);
2775
2776 version = ofputil_protocol_to_ofp_version(ofconn_get_protocol(ofconn));
2777 HMAP_FOR_EACH (port, hmap_node, &p->ports) {
2778 ofputil_append_port_desc_stats_reply(version, &port->pp, &replies);
2779 }
2780
2781 ofconn_send_replies(ofconn, &replies);
2782 return 0;
2783 }
2784
2785 static uint32_t
2786 hash_cookie(ovs_be64 cookie)
2787 {
2788 return hash_2words((OVS_FORCE uint64_t)cookie >> 32,
2789 (OVS_FORCE uint64_t)cookie);
2790 }
2791
2792 static void
2793 cookies_insert(struct ofproto *ofproto, struct rule *rule)
2794 {
2795 hindex_insert(&ofproto->cookies, &rule->cookie_node,
2796 hash_cookie(rule->flow_cookie));
2797 }
2798
2799 static void
2800 cookies_remove(struct ofproto *ofproto, struct rule *rule)
2801 {
2802 hindex_remove(&ofproto->cookies, &rule->cookie_node);
2803 }
2804
2805 static void
2806 ofproto_rule_change_cookie(struct ofproto *ofproto, struct rule *rule,
2807 ovs_be64 new_cookie)
2808 {
2809 if (new_cookie != rule->flow_cookie) {
2810 cookies_remove(ofproto, rule);
2811
2812 rule->flow_cookie = new_cookie;
2813
2814 cookies_insert(ofproto, rule);
2815 }
2816 }
2817
2818 static void
2819 calc_duration(long long int start, long long int now,
2820 uint32_t *sec, uint32_t *nsec)
2821 {
2822 long long int msecs = now - start;
2823 *sec = msecs / 1000;
2824 *nsec = (msecs % 1000) * (1000 * 1000);
2825 }
2826
2827 /* Checks whether 'table_id' is 0xff or a valid table ID in 'ofproto'. Returns
2828 * 0 if 'table_id' is OK, otherwise an OpenFlow error code. */
2829 static enum ofperr
2830 check_table_id(const struct ofproto *ofproto, uint8_t table_id)
2831 {
2832 return (table_id == 0xff || table_id < ofproto->n_tables
2833 ? 0
2834 : OFPERR_OFPBRC_BAD_TABLE_ID);
2835
2836 }
2837
2838 static struct oftable *
2839 next_visible_table(const struct ofproto *ofproto, uint8_t table_id)
2840 {
2841 struct oftable *table;
2842
2843 for (table = &ofproto->tables[table_id];
2844 table < &ofproto->tables[ofproto->n_tables];
2845 table++) {
2846 if (!(table->flags & OFTABLE_HIDDEN)) {
2847 return table;
2848 }
2849 }
2850
2851 return NULL;
2852 }
2853
2854 static struct oftable *
2855 first_matching_table(const struct ofproto *ofproto, uint8_t table_id)
2856 {
2857 if (table_id == 0xff) {
2858 return next_visible_table(ofproto, 0);
2859 } else if (table_id < ofproto->n_tables) {
2860 return &ofproto->tables[table_id];
2861 } else {
2862 return NULL;
2863 }
2864 }
2865
2866 static struct oftable *
2867 next_matching_table(const struct ofproto *ofproto,
2868 const struct oftable *table, uint8_t table_id)
2869 {
2870 return (table_id == 0xff
2871 ? next_visible_table(ofproto, (table - ofproto->tables) + 1)
2872 : NULL);
2873 }
2874
2875 /* Assigns TABLE to each oftable, in turn, that matches TABLE_ID in OFPROTO:
2876 *
2877 * - If TABLE_ID is 0xff, this iterates over every classifier table in
2878 * OFPROTO, skipping tables marked OFTABLE_HIDDEN.
2879 *
2880 * - If TABLE_ID is the number of a table in OFPROTO, then the loop iterates
2881 * only once, for that table. (This can be used to access tables marked
2882 * OFTABLE_HIDDEN.)
2883 *
2884 * - Otherwise, TABLE_ID isn't valid for OFPROTO, so the loop won't be
2885 * entered at all. (Perhaps you should have validated TABLE_ID with
2886 * check_table_id().)
2887 *
2888 * All parameters are evaluated multiple times.
2889 */
2890 #define FOR_EACH_MATCHING_TABLE(TABLE, TABLE_ID, OFPROTO) \
2891 for ((TABLE) = first_matching_table(OFPROTO, TABLE_ID); \
2892 (TABLE) != NULL; \
2893 (TABLE) = next_matching_table(OFPROTO, TABLE, TABLE_ID))
2894
2895 /* Searches 'ofproto' for rules in table 'table_id' (or in all tables, if
2896 * 'table_id' is 0xff) that match 'match' in the "loose" way required for
2897 * OpenFlow OFPFC_MODIFY and OFPFC_DELETE requests and puts them on list
2898 * 'rules'.
2899 *
2900 * If 'out_port' is anything other than OFPP_ANY, then only rules that output
2901 * to 'out_port' are included.
2902 *
2903 * Hidden rules are always omitted.
2904 *
2905 * Returns 0 on success, otherwise an OpenFlow error code. */
2906 static enum ofperr
2907 collect_rules_loose(struct ofproto *ofproto, uint8_t table_id,
2908 const struct match *match,
2909 ovs_be64 cookie, ovs_be64 cookie_mask,
2910 ofp_port_t out_port, uint32_t out_group,
2911 struct list *rules)
2912 {
2913 struct oftable *table;
2914 struct cls_rule cr;
2915 enum ofperr error;
2916
2917 error = check_table_id(ofproto, table_id);
2918 if (error) {
2919 return error;
2920 }
2921
2922 list_init(rules);
2923 cls_rule_init(&cr, match, 0);
2924
2925 if (cookie_mask == htonll(UINT64_MAX)) {
2926 struct rule *rule;
2927
2928 HINDEX_FOR_EACH_WITH_HASH (rule, cookie_node, hash_cookie(cookie),
2929 &ofproto->cookies) {
2930 if (table_id != rule->table_id && table_id != 0xff) {
2931 continue;
2932 }
2933 if (ofproto_rule_is_hidden(rule)) {
2934 continue;
2935 }
2936 if (cls_rule_is_loose_match(&rule->cr, &cr.match)) {
2937 if (rule->pending) {
2938 error = OFPROTO_POSTPONE;
2939 goto exit;
2940 }
2941 if (rule->flow_cookie == cookie /* Hash collisions possible. */
2942 && ofproto_rule_has_out_port(rule, out_port)) {
2943 list_push_back(rules, &rule->ofproto_node);
2944 }
2945 }
2946 }
2947 goto exit;
2948 }
2949
2950 FOR_EACH_MATCHING_TABLE (table, table_id, ofproto) {
2951 struct cls_cursor cursor;
2952 struct rule *rule;
2953
2954 ovs_rwlock_rdlock(&table->cls.rwlock);
2955 cls_cursor_init(&cursor, &table->cls, &cr);
2956 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
2957 if (rule->pending) {
2958 ovs_rwlock_unlock(&table->cls.rwlock);
2959 error = OFPROTO_POSTPONE;
2960 goto exit;
2961 }
2962 if (!ofproto_rule_is_hidden(rule)
2963 && ofproto_rule_has_out_port(rule, out_port)
2964 && ofproto_rule_has_out_group(rule, out_group)
2965 && !((rule->flow_cookie ^ cookie) & cookie_mask)) {
2966 list_push_back(rules, &rule->ofproto_node);
2967 }
2968 }
2969 ovs_rwlock_unlock(&table->cls.rwlock);
2970 }
2971
2972 exit:
2973 cls_rule_destroy(&cr);
2974 return error;
2975 }
2976
2977 /* Searches 'ofproto' for rules in table 'table_id' (or in all tables, if
2978 * 'table_id' is 0xff) that match 'match' in the "strict" way required for
2979 * OpenFlow OFPFC_MODIFY_STRICT and OFPFC_DELETE_STRICT requests and puts them
2980 * on list 'rules'.
2981 *
2982 * If 'out_port' is anything other than OFPP_ANY, then only rules that output
2983 * to 'out_port' are included.
2984 *
2985 * Hidden rules are always omitted.
2986 *
2987 * Returns 0 on success, otherwise an OpenFlow error code. */
2988 static enum ofperr
2989 collect_rules_strict(struct ofproto *ofproto, uint8_t table_id,
2990 const struct match *match, unsigned int priority,
2991 ovs_be64 cookie, ovs_be64 cookie_mask,
2992 ofp_port_t out_port, uint32_t out_group,
2993 struct list *rules)
2994 {
2995 struct oftable *table;
2996 struct cls_rule cr;
2997 int error;
2998
2999 error = check_table_id(ofproto, table_id);
3000 if (error) {
3001 return error;
3002 }
3003
3004 list_init(rules);
3005 cls_rule_init(&cr, match, priority);
3006
3007 if (cookie_mask == htonll(UINT64_MAX)) {
3008 struct rule *rule;
3009
3010 HINDEX_FOR_EACH_WITH_HASH (rule, cookie_node, hash_cookie(cookie),
3011 &ofproto->cookies) {
3012 if (table_id != rule->table_id && table_id != 0xff) {
3013 continue;
3014 }
3015 if (ofproto_rule_is_hidden(rule)) {
3016 continue;
3017 }
3018 if (cls_rule_equal(&rule->cr, &cr)) {
3019 if (rule->pending) {
3020 error = OFPROTO_POSTPONE;
3021 goto exit;
3022 }
3023 if (rule->flow_cookie == cookie /* Hash collisions possible. */
3024 && ofproto_rule_has_out_port(rule, out_port)) {
3025 list_push_back(rules, &rule->ofproto_node);
3026 }
3027 }
3028 }
3029 goto exit;
3030 }
3031
3032 FOR_EACH_MATCHING_TABLE (table, table_id, ofproto) {
3033 struct rule *rule;
3034
3035 ovs_rwlock_rdlock(&table->cls.rwlock);
3036 rule = rule_from_cls_rule(classifier_find_rule_exactly(&table->cls,
3037 &cr));
3038 ovs_rwlock_unlock(&table->cls.rwlock);
3039 if (rule) {
3040 if (rule->pending) {
3041 error = OFPROTO_POSTPONE;
3042 goto exit;
3043 }
3044 if (!ofproto_rule_is_hidden(rule)
3045 && ofproto_rule_has_out_port(rule, out_port)
3046 && ofproto_rule_has_out_group(rule, out_group)
3047 && !((rule->flow_cookie ^ cookie) & cookie_mask)) {
3048 list_push_back(rules, &rule->ofproto_node);
3049 }
3050 }
3051 }
3052
3053 exit:
3054 cls_rule_destroy(&cr);
3055 return 0;
3056 }
3057
3058 /* Returns 'age_ms' (a duration in milliseconds), converted to seconds and
3059 * forced into the range of a uint16_t. */
3060 static int
3061 age_secs(long long int age_ms)
3062 {
3063 return (age_ms < 0 ? 0
3064 : age_ms >= UINT16_MAX * 1000 ? UINT16_MAX
3065 : (unsigned int) age_ms / 1000);
3066 }
3067
3068 static enum ofperr
3069 handle_flow_stats_request(struct ofconn *ofconn,
3070 const struct ofp_header *request)
3071 {
3072 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3073 struct ofputil_flow_stats_request fsr;
3074 struct list replies;
3075 struct list rules;
3076 struct rule *rule;
3077 enum ofperr error;
3078
3079 error = ofputil_decode_flow_stats_request(&fsr, request);
3080 if (error) {
3081 return error;
3082 }
3083
3084 error = collect_rules_loose(ofproto, fsr.table_id, &fsr.match,
3085 fsr.cookie, fsr.cookie_mask,
3086 fsr.out_port, fsr.out_group, &rules);
3087 if (error) {
3088 return error;
3089 }
3090
3091 ofpmp_init(&replies, request);
3092 LIST_FOR_EACH (rule, ofproto_node, &rules) {
3093 long long int now = time_msec();
3094 struct ofputil_flow_stats fs;
3095
3096 minimatch_expand(&rule->cr.match, &fs.match);
3097 fs.priority = rule->cr.priority;
3098 fs.cookie = rule->flow_cookie;
3099 fs.table_id = rule->table_id;
3100 calc_duration(rule->created, now, &fs.duration_sec, &fs.duration_nsec);
3101 fs.idle_age = age_secs(now - rule->used);
3102 fs.hard_age = age_secs(now - rule->modified);
3103 ofproto->ofproto_class->rule_get_stats(rule, &fs.packet_count,
3104 &fs.byte_count);
3105 fs.ofpacts = rule->ofpacts;
3106 fs.ofpacts_len = rule->ofpacts_len;
3107
3108 ovs_mutex_lock(&rule->timeout_mutex);
3109 fs.idle_timeout = rule->idle_timeout;
3110 fs.hard_timeout = rule->hard_timeout;
3111 ovs_mutex_unlock(&rule->timeout_mutex);
3112
3113 fs.flags = 0;
3114 if (rule->send_flow_removed) {
3115 fs.flags |= OFPUTIL_FF_SEND_FLOW_REM;
3116 /* FIXME: Implement OFPUTIL_FF_NO_PKT_COUNTS and
3117 OFPUTIL_FF_NO_BYT_COUNTS. */
3118 }
3119 ofputil_append_flow_stats_reply(&fs, &replies);
3120 }
3121 ofconn_send_replies(ofconn, &replies);
3122
3123 return 0;
3124 }
3125
3126 static void
3127 flow_stats_ds(struct rule *rule, struct ds *results)
3128 {
3129 uint64_t packet_count, byte_count;
3130
3131 rule->ofproto->ofproto_class->rule_get_stats(rule,
3132 &packet_count, &byte_count);
3133
3134 if (rule->table_id != 0) {
3135 ds_put_format(results, "table_id=%"PRIu8", ", rule->table_id);
3136 }
3137 ds_put_format(results, "duration=%llds, ",
3138 (time_msec() - rule->created) / 1000);
3139 ds_put_format(results, "priority=%u, ", rule->cr.priority);
3140 ds_put_format(results, "n_packets=%"PRIu64", ", packet_count);
3141 ds_put_format(results, "n_bytes=%"PRIu64", ", byte_count);
3142 cls_rule_format(&rule->cr, results);
3143 ds_put_char(results, ',');
3144 ofpacts_format(rule->ofpacts, rule->ofpacts_len, results);
3145 ds_put_cstr(results, "\n");
3146 }
3147
3148 /* Adds a pretty-printed description of all flows to 'results', including
3149 * hidden flows (e.g., set up by in-band control). */
3150 void
3151 ofproto_get_all_flows(struct ofproto *p, struct ds *results)
3152 {
3153 struct oftable *table;
3154
3155 OFPROTO_FOR_EACH_TABLE (table, p) {
3156 struct cls_cursor cursor;
3157 struct rule *rule;
3158
3159 ovs_rwlock_rdlock(&table->cls.rwlock);
3160 cls_cursor_init(&cursor, &table->cls, NULL);
3161 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
3162 flow_stats_ds(rule, results);
3163 }
3164 ovs_rwlock_unlock(&table->cls.rwlock);
3165 }
3166 }
3167
3168 /* Obtains the NetFlow engine type and engine ID for 'ofproto' into
3169 * '*engine_type' and '*engine_id', respectively. */
3170 void
3171 ofproto_get_netflow_ids(const struct ofproto *ofproto,
3172 uint8_t *engine_type, uint8_t *engine_id)
3173 {
3174 ofproto->ofproto_class->get_netflow_ids(ofproto, engine_type, engine_id);
3175 }
3176
3177 /* Checks the status of CFM configured on 'ofp_port' within 'ofproto'. Returns
3178 * true if the port's CFM status was successfully stored into '*status'.
3179 * Returns false if the port did not have CFM configured, in which case
3180 * '*status' is indeterminate.
3181 *
3182 * The caller must provide and owns '*status', and must free 'status->rmps'. */
3183 bool
3184 ofproto_port_get_cfm_status(const struct ofproto *ofproto, ofp_port_t ofp_port,
3185 struct ofproto_cfm_status *status)
3186 {
3187 struct ofport *ofport = ofproto_get_port(ofproto, ofp_port);
3188 return (ofport
3189 && ofproto->ofproto_class->get_cfm_status
3190 && ofproto->ofproto_class->get_cfm_status(ofport, status));
3191 }
3192
3193 static enum ofperr
3194 handle_aggregate_stats_request(struct ofconn *ofconn,
3195 const struct ofp_header *oh)
3196 {
3197 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3198 struct ofputil_flow_stats_request request;
3199 struct ofputil_aggregate_stats stats;
3200 bool unknown_packets, unknown_bytes;
3201 struct ofpbuf *reply;
3202 struct list rules;
3203 struct rule *rule;
3204 enum ofperr error;
3205
3206 error = ofputil_decode_flow_stats_request(&request, oh);
3207 if (error) {
3208 return error;
3209 }
3210
3211 error = collect_rules_loose(ofproto, request.table_id, &request.match,
3212 request.cookie, request.cookie_mask,
3213 request.out_port, request.out_group, &rules);
3214 if (error) {
3215 return error;
3216 }
3217
3218 memset(&stats, 0, sizeof stats);
3219 unknown_packets = unknown_bytes = false;
3220 LIST_FOR_EACH (rule, ofproto_node, &rules) {
3221 uint64_t packet_count;
3222 uint64_t byte_count;
3223
3224 ofproto->ofproto_class->rule_get_stats(rule, &packet_count,
3225 &byte_count);
3226
3227 if (packet_count == UINT64_MAX) {
3228 unknown_packets = true;
3229 } else {
3230 stats.packet_count += packet_count;
3231 }
3232
3233 if (byte_count == UINT64_MAX) {
3234 unknown_bytes = true;
3235 } else {
3236 stats.byte_count += byte_count;
3237 }
3238
3239 stats.flow_count++;
3240 }
3241 if (unknown_packets) {
3242 stats.packet_count = UINT64_MAX;
3243 }
3244 if (unknown_bytes) {
3245 stats.byte_count = UINT64_MAX;
3246 }
3247
3248 reply = ofputil_encode_aggregate_stats_reply(&stats, oh);
3249 ofconn_send_reply(ofconn, reply);
3250
3251 return 0;
3252 }
3253
3254 struct queue_stats_cbdata {
3255 struct ofport *ofport;
3256 struct list replies;
3257 long long int now;
3258 };
3259
3260 static void
3261 put_queue_stats(struct queue_stats_cbdata *cbdata, uint32_t queue_id,
3262 const struct netdev_queue_stats *stats)
3263 {
3264 struct ofputil_queue_stats oqs;
3265
3266 oqs.port_no = cbdata->ofport->pp.port_no;
3267 oqs.queue_id = queue_id;
3268 oqs.tx_bytes = stats->tx_bytes;
3269 oqs.tx_packets = stats->tx_packets;
3270 oqs.tx_errors = stats->tx_errors;
3271 if (stats->created != LLONG_MIN) {
3272 calc_duration(stats->created, cbdata->now,
3273 &oqs.duration_sec, &oqs.duration_nsec);
3274 } else {
3275 oqs.duration_sec = oqs.duration_nsec = UINT32_MAX;
3276 }
3277 ofputil_append_queue_stat(&cbdata->replies, &oqs);
3278 }
3279
3280 static void
3281 handle_queue_stats_dump_cb(uint32_t queue_id,
3282 struct netdev_queue_stats *stats,
3283 void *cbdata_)
3284 {
3285 struct queue_stats_cbdata *cbdata = cbdata_;
3286
3287 put_queue_stats(cbdata, queue_id, stats);
3288 }
3289
3290 static enum ofperr
3291 handle_queue_stats_for_port(struct ofport *port, uint32_t queue_id,
3292 struct queue_stats_cbdata *cbdata)
3293 {
3294 cbdata->ofport = port;
3295 if (queue_id == OFPQ_ALL) {
3296 netdev_dump_queue_stats(port->netdev,
3297 handle_queue_stats_dump_cb, cbdata);
3298 } else {
3299 struct netdev_queue_stats stats;
3300
3301 if (!netdev_get_queue_stats(port->netdev, queue_id, &stats)) {
3302 put_queue_stats(cbdata, queue_id, &stats);
3303 } else {
3304 return OFPERR_OFPQOFC_BAD_QUEUE;
3305 }
3306 }
3307 return 0;
3308 }
3309
3310 static enum ofperr
3311 handle_queue_stats_request(struct ofconn *ofconn,
3312 const struct ofp_header *rq)
3313 {
3314 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3315 struct queue_stats_cbdata cbdata;
3316 struct ofport *port;
3317 enum ofperr error;
3318 struct ofputil_queue_stats_request oqsr;
3319
3320 COVERAGE_INC(ofproto_queue_req);
3321
3322 ofpmp_init(&cbdata.replies, rq);
3323 cbdata.now = time_msec();
3324
3325 error = ofputil_decode_queue_stats_request(rq, &oqsr);
3326 if (error) {
3327 return error;
3328 }
3329
3330 if (oqsr.port_no == OFPP_ANY) {
3331 error = OFPERR_OFPQOFC_BAD_QUEUE;
3332 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
3333 if (!handle_queue_stats_for_port(port, oqsr.queue_id, &cbdata)) {
3334 error = 0;
3335 }
3336 }
3337 } else {
3338 port = ofproto_get_port(ofproto, oqsr.port_no);
3339 error = (port
3340 ? handle_queue_stats_for_port(port, oqsr.queue_id, &cbdata)
3341 : OFPERR_OFPQOFC_BAD_PORT);
3342 }
3343 if (!error) {
3344 ofconn_send_replies(ofconn, &cbdata.replies);
3345 } else {
3346 ofpbuf_list_delete(&cbdata.replies);
3347 }
3348
3349 return error;
3350 }
3351
3352 static bool
3353 is_flow_deletion_pending(const struct ofproto *ofproto,
3354 const struct cls_rule *cls_rule,
3355 uint8_t table_id)
3356 {
3357 if (!hmap_is_empty(&ofproto->deletions)) {
3358 struct ofoperation *op;
3359
3360 HMAP_FOR_EACH_WITH_HASH (op, hmap_node,
3361 cls_rule_hash(cls_rule, table_id),
3362 &ofproto->deletions) {
3363 if (cls_rule_equal(cls_rule, &op->rule->cr)) {
3364 return true;
3365 }
3366 }
3367 }
3368
3369 return false;
3370 }
3371
3372 static enum ofperr
3373 evict_rule_from_table(struct ofproto *ofproto, struct oftable *table)
3374 {
3375 struct rule *rule;
3376 size_t n_rules;
3377
3378 ovs_rwlock_rdlock(&table->cls.rwlock);
3379 n_rules = classifier_count(&table->cls);
3380 ovs_rwlock_unlock(&table->cls.rwlock);
3381
3382 if (n_rules < table->max_flows) {
3383 return 0;
3384 } else if (!choose_rule_to_evict(table, &rule)) {
3385 return OFPERR_OFPFMFC_TABLE_FULL;
3386 } else if (rule->pending) {
3387 ovs_rwlock_unlock(&rule->evict);
3388 return OFPROTO_POSTPONE;
3389 } else {
3390 struct ofopgroup *group;
3391
3392 group = ofopgroup_create_unattached(ofproto);
3393 delete_flow__(rule, group, OFPRR_EVICTION);
3394 ofopgroup_submit(group);
3395
3396 return 0;
3397 }
3398 }
3399
3400 /* Implements OFPFC_ADD and the cases for OFPFC_MODIFY and OFPFC_MODIFY_STRICT
3401 * in which no matching flow already exists in the flow table.
3402 *
3403 * Adds the flow specified by 'ofm', which is followed by 'n_actions'
3404 * ofp_actions, to the ofproto's flow table. Returns 0 on success, an OpenFlow
3405 * error code on failure, or OFPROTO_POSTPONE if the operation cannot be
3406 * initiated now but may be retried later.
3407 *
3408 * Upon successful return, takes ownership of 'fm->ofpacts'. On failure,
3409 * ownership remains with the caller.
3410 *
3411 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
3412 * if any. */
3413 static enum ofperr
3414 add_flow(struct ofproto *ofproto, struct ofconn *ofconn,
3415 struct ofputil_flow_mod *fm, const struct ofp_header *request)
3416 {
3417 struct oftable *table;
3418 struct ofopgroup *group;
3419 struct cls_rule cr;
3420 struct rule *rule;
3421 uint8_t table_id;
3422 int error;
3423
3424 error = check_table_id(ofproto, fm->table_id);
3425 if (error) {
3426 return error;
3427 }
3428
3429 /* Pick table. */
3430 if (fm->table_id == 0xff) {
3431 if (ofproto->ofproto_class->rule_choose_table) {
3432 error = ofproto->ofproto_class->rule_choose_table(ofproto,
3433 &fm->match,
3434 &table_id);
3435 if (error) {
3436 return error;
3437 }
3438 ovs_assert(table_id < ofproto->n_tables);
3439 } else {
3440 table_id = 0;
3441 }
3442 } else if (fm->table_id < ofproto->n_tables) {
3443 table_id = fm->table_id;
3444 } else {
3445 return OFPERR_OFPBRC_BAD_TABLE_ID;
3446 }
3447
3448 table = &ofproto->tables[table_id];
3449
3450 if (table->flags & OFTABLE_READONLY) {
3451 return OFPERR_OFPBRC_EPERM;
3452 }
3453
3454 cls_rule_init(&cr, &fm->match, fm->priority);
3455
3456 /* Transform "add" into "modify" if there's an existing identical flow. */
3457 ovs_rwlock_rdlock(&table->cls.rwlock);
3458 rule = rule_from_cls_rule(classifier_find_rule_exactly(&table->cls, &cr));
3459 ovs_rwlock_unlock(&table->cls.rwlock);
3460 if (rule) {
3461 cls_rule_destroy(&cr);
3462 if (!rule_is_modifiable(rule)) {
3463 return OFPERR_OFPBRC_EPERM;
3464 } else if (rule->pending) {
3465 return OFPROTO_POSTPONE;
3466 } else {
3467 struct list rules;
3468
3469 list_init(&rules);
3470 list_push_back(&rules, &rule->ofproto_node);
3471 fm->modify_cookie = true;
3472 return modify_flows__(ofproto, ofconn, fm, request, &rules);
3473 }
3474 }
3475
3476 /* Verify actions. */
3477 error = ofproto_check_ofpacts(ofproto, fm->ofpacts, fm->ofpacts_len,
3478 &fm->match.flow, table_id);
3479 if (error) {
3480 cls_rule_destroy(&cr);
3481 return error;
3482 }
3483
3484 /* Serialize against pending deletion. */
3485 if (is_flow_deletion_pending(ofproto, &cr, table_id)) {
3486 cls_rule_destroy(&cr);
3487 return OFPROTO_POSTPONE;
3488 }
3489
3490 /* Check for overlap, if requested. */
3491 if (fm->flags & OFPUTIL_FF_CHECK_OVERLAP) {
3492 bool overlaps;
3493
3494 ovs_rwlock_rdlock(&table->cls.rwlock);
3495 overlaps = classifier_rule_overlaps(&table->cls, &cr);
3496 ovs_rwlock_unlock(&table->cls.rwlock);
3497
3498 if (overlaps) {
3499 cls_rule_destroy(&cr);
3500 return OFPERR_OFPFMFC_OVERLAP;
3501 }
3502 }
3503
3504 /* If necessary, evict an existing rule to clear out space. */
3505 error = evict_rule_from_table(ofproto, table);
3506 if (error) {
3507 cls_rule_destroy(&cr);
3508 return error;
3509 }
3510
3511 /* Allocate new rule. */
3512 rule = ofproto->ofproto_class->rule_alloc();
3513 if (!rule) {
3514 cls_rule_destroy(&cr);
3515 VLOG_WARN_RL(&rl, "%s: failed to create rule (%s)",
3516 ofproto->name, ovs_strerror(error));
3517 return ENOMEM;
3518 }
3519
3520 /* Initialize base state. */
3521 rule->ofproto = ofproto;
3522 cls_rule_move(&rule->cr, &cr);
3523 rule->pending = NULL;
3524 rule->flow_cookie = fm->new_cookie;
3525 rule->created = rule->modified = rule->used = time_msec();
3526
3527 ovs_mutex_init(&rule->timeout_mutex);
3528 ovs_mutex_lock(&rule->timeout_mutex);
3529 rule->idle_timeout = fm->idle_timeout;
3530 rule->hard_timeout = fm->hard_timeout;
3531 ovs_mutex_unlock(&rule->timeout_mutex);
3532
3533 rule->table_id = table - ofproto->tables;
3534 rule->send_flow_removed = (fm->flags & OFPUTIL_FF_SEND_FLOW_REM) != 0;
3535 rule->ofpacts = xmemdup(fm->ofpacts, fm->ofpacts_len);
3536 rule->ofpacts_len = fm->ofpacts_len;
3537 rule->meter_id = find_meter(rule->ofpacts, rule->ofpacts_len);
3538 list_init(&rule->meter_list_node);
3539 rule->eviction_group = NULL;
3540 list_init(&rule->expirable);
3541 rule->monitor_flags = 0;
3542 rule->add_seqno = 0;
3543 rule->modify_seqno = 0;
3544 ovs_rwlock_init(&rule->evict);
3545
3546 /* Construct rule, initializing derived state. */
3547 error = ofproto->ofproto_class->rule_construct(rule);
3548 if (error) {
3549 ofproto_rule_destroy__(rule);
3550 return error;
3551 }
3552
3553 /* Insert rule. */
3554 oftable_insert_rule(rule);
3555
3556 group = ofopgroup_create(ofproto, ofconn, request, fm->buffer_id);
3557 ofoperation_create(group, rule, OFOPERATION_ADD, 0);
3558 ofproto->ofproto_class->rule_insert(rule);
3559 ofopgroup_submit(group);
3560
3561 return error;
3562 }
3563 \f
3564 /* OFPFC_MODIFY and OFPFC_MODIFY_STRICT. */
3565
3566 /* Modifies the rules listed in 'rules', changing their actions to match those
3567 * in 'fm'.
3568 *
3569 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
3570 * if any.
3571 *
3572 * Returns 0 on success, otherwise an OpenFlow error code. */
3573 static enum ofperr
3574 modify_flows__(struct ofproto *ofproto, struct ofconn *ofconn,
3575 struct ofputil_flow_mod *fm, const struct ofp_header *request,
3576 struct list *rules)
3577 {
3578 enum ofoperation_type type;
3579 struct ofopgroup *group;
3580 struct rule *rule;
3581 enum ofperr error;
3582
3583 type = fm->command == OFPFC_ADD ? OFOPERATION_REPLACE : OFOPERATION_MODIFY;
3584 group = ofopgroup_create(ofproto, ofconn, request, fm->buffer_id);
3585 error = OFPERR_OFPBRC_EPERM;
3586 LIST_FOR_EACH (rule, ofproto_node, rules) {
3587 struct ofoperation *op;
3588 bool actions_changed;
3589 bool reset_counters;
3590
3591 /* FIXME: Implement OFPFUTIL_FF_RESET_COUNTS */
3592
3593 if (rule_is_modifiable(rule)) {
3594 /* At least one rule is modifiable, don't report EPERM error. */
3595 error = 0;
3596 } else {
3597 continue;
3598 }
3599
3600 /* Verify actions. */
3601 error = ofpacts_check(fm->ofpacts, fm->ofpacts_len, &fm->match.flow,
3602 u16_to_ofp(ofproto->max_ports), rule->table_id);
3603 if (error) {
3604 return error;
3605 }
3606
3607 actions_changed = !ofpacts_equal(fm->ofpacts, fm->ofpacts_len,
3608 rule->ofpacts, rule->ofpacts_len);
3609
3610 op = ofoperation_create(group, rule, type, 0);
3611
3612 if (fm->modify_cookie && fm->new_cookie != htonll(UINT64_MAX)) {
3613 ofproto_rule_change_cookie(ofproto, rule, fm->new_cookie);
3614 }
3615 if (type == OFOPERATION_REPLACE) {
3616 ovs_mutex_lock(&rule->timeout_mutex);
3617 rule->idle_timeout = fm->idle_timeout;
3618 rule->hard_timeout = fm->hard_timeout;
3619 ovs_mutex_unlock(&rule->timeout_mutex);
3620
3621 rule->send_flow_removed = (fm->flags
3622 & OFPUTIL_FF_SEND_FLOW_REM) != 0;
3623
3624 if (fm->idle_timeout || fm->hard_timeout) {
3625 if (!rule->eviction_group) {
3626 eviction_group_add_rule(rule);
3627 }
3628 } else {
3629 eviction_group_remove_rule(rule);
3630 }
3631 }
3632
3633 reset_counters = (fm->flags & OFPUTIL_FF_RESET_COUNTS) != 0;
3634 if (actions_changed || reset_counters) {
3635 op->ofpacts = rule->ofpacts;
3636 op->ofpacts_len = rule->ofpacts_len;
3637 op->meter_id = rule->meter_id;
3638 rule->ofpacts = xmemdup(fm->ofpacts, fm->ofpacts_len);
3639 rule->ofpacts_len = fm->ofpacts_len;
3640 rule->meter_id = find_meter(rule->ofpacts, rule->ofpacts_len);
3641 rule->ofproto->ofproto_class->rule_modify_actions(rule,
3642 reset_counters);
3643 } else {
3644 ofoperation_complete(op, 0);
3645 }
3646 }
3647 ofopgroup_submit(group);
3648
3649 return error;
3650 }
3651
3652 static enum ofperr
3653 modify_flows_add(struct ofproto *ofproto, struct ofconn *ofconn,
3654 struct ofputil_flow_mod *fm, const struct ofp_header *request)
3655 {
3656 if (fm->cookie_mask != htonll(0) || fm->new_cookie == htonll(UINT64_MAX)) {
3657 return 0;
3658 }
3659 return add_flow(ofproto, ofconn, fm, request);
3660 }
3661
3662 /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code on
3663 * failure.
3664 *
3665 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
3666 * if any. */
3667 static enum ofperr
3668 modify_flows_loose(struct ofproto *ofproto, struct ofconn *ofconn,
3669 struct ofputil_flow_mod *fm,
3670 const struct ofp_header *request)
3671 {
3672 struct list rules;
3673 int error;
3674
3675 error = collect_rules_loose(ofproto, fm->table_id, &fm->match,
3676 fm->cookie, fm->cookie_mask,
3677 OFPP_ANY, OFPG11_ANY, &rules);
3678 if (error) {
3679 return error;
3680 } else if (list_is_empty(&rules)) {
3681 return modify_flows_add(ofproto, ofconn, fm, request);
3682 } else {
3683 return modify_flows__(ofproto, ofconn, fm, request, &rules);
3684 }
3685 }
3686
3687 /* Implements OFPFC_MODIFY_STRICT. Returns 0 on success or an OpenFlow error
3688 * code on failure.
3689 *
3690 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
3691 * if any. */
3692 static enum ofperr
3693 modify_flow_strict(struct ofproto *ofproto, struct ofconn *ofconn,
3694 struct ofputil_flow_mod *fm,
3695 const struct ofp_header *request)
3696 {
3697 struct list rules;
3698 int error;
3699
3700 error = collect_rules_strict(ofproto, fm->table_id, &fm->match,
3701 fm->priority, fm->cookie, fm->cookie_mask,
3702 OFPP_ANY, OFPG11_ANY, &rules);
3703 if (error) {
3704 return error;
3705 } else if (list_is_empty(&rules)) {
3706 return modify_flows_add(ofproto, ofconn, fm, request);
3707 } else {
3708 return list_is_singleton(&rules) ? modify_flows__(ofproto, ofconn,
3709 fm, request, &rules)
3710 : 0;
3711 }
3712 }
3713 \f
3714 /* OFPFC_DELETE implementation. */
3715
3716 static void
3717 delete_flow__(struct rule *rule, struct ofopgroup *group,
3718 enum ofp_flow_removed_reason reason)
3719 {
3720 struct ofproto *ofproto = rule->ofproto;
3721
3722 ofproto_rule_send_removed(rule, reason);
3723
3724 ofoperation_create(group, rule, OFOPERATION_DELETE, reason);
3725 oftable_remove_rule(rule);
3726 ofproto->ofproto_class->rule_delete(rule);
3727 }
3728
3729 /* Deletes the rules listed in 'rules'.
3730 *
3731 * Returns 0 on success, otherwise an OpenFlow error code. */
3732 static enum ofperr
3733 delete_flows__(struct ofproto *ofproto, struct ofconn *ofconn,
3734 const struct ofp_header *request, struct list *rules,
3735 enum ofp_flow_removed_reason reason)
3736 {
3737 struct rule *rule, *next;
3738 struct ofopgroup *group;
3739
3740 group = ofopgroup_create(ofproto, ofconn, request, UINT32_MAX);
3741 LIST_FOR_EACH_SAFE (rule, next, ofproto_node, rules) {
3742 ovs_rwlock_wrlock(&rule->evict);
3743 delete_flow__(rule, group, reason);
3744 }
3745 ofopgroup_submit(group);
3746
3747 return 0;
3748 }
3749
3750 /* Implements OFPFC_DELETE. */
3751 static enum ofperr
3752 delete_flows_loose(struct ofproto *ofproto, struct ofconn *ofconn,
3753 const struct ofputil_flow_mod *fm,
3754 const struct ofp_header *request)
3755 {
3756 struct list rules;
3757 enum ofperr error;
3758
3759 error = collect_rules_loose(ofproto, fm->table_id, &fm->match,
3760 fm->cookie, fm->cookie_mask,
3761 fm->out_port, fm->out_group, &rules);
3762 return (error ? error
3763 : !list_is_empty(&rules) ? delete_flows__(ofproto, ofconn, request,
3764 &rules, OFPRR_DELETE)
3765 : 0);
3766 }
3767
3768 /* Implements OFPFC_DELETE_STRICT. */
3769 static enum ofperr
3770 delete_flow_strict(struct ofproto *ofproto, struct ofconn *ofconn,
3771 const struct ofputil_flow_mod *fm,
3772 const struct ofp_header *request)
3773 {
3774 struct list rules;
3775 enum ofperr error;
3776
3777 error = collect_rules_strict(ofproto, fm->table_id, &fm->match,
3778 fm->priority, fm->cookie, fm->cookie_mask,
3779 fm->out_port, fm->out_group, &rules);
3780 return (error ? error
3781 : list_is_singleton(&rules) ? delete_flows__(ofproto, ofconn,
3782 request, &rules,
3783 OFPRR_DELETE)
3784 : 0);
3785 }
3786
3787 static void
3788 ofproto_rule_send_removed(struct rule *rule, uint8_t reason)
3789 {
3790 struct ofputil_flow_removed fr;
3791
3792 if (ofproto_rule_is_hidden(rule) || !rule->send_flow_removed) {
3793 return;
3794 }
3795
3796 minimatch_expand(&rule->cr.match, &fr.match);
3797 fr.priority = rule->cr.priority;
3798 fr.cookie = rule->flow_cookie;
3799 fr.reason = reason;
3800 fr.table_id = rule->table_id;
3801 calc_duration(rule->created, time_msec(),
3802 &fr.duration_sec, &fr.duration_nsec);
3803 ovs_mutex_lock(&rule->timeout_mutex);
3804 fr.idle_timeout = rule->idle_timeout;
3805 fr.hard_timeout = rule->hard_timeout;
3806 ovs_mutex_unlock(&rule->timeout_mutex);
3807 rule->ofproto->ofproto_class->rule_get_stats(rule, &fr.packet_count,
3808 &fr.byte_count);
3809
3810 connmgr_send_flow_removed(rule->ofproto->connmgr, &fr);
3811 }
3812
3813 void
3814 ofproto_rule_update_used(struct rule *rule, long long int used)
3815 {
3816 if (used > rule->used) {
3817 struct eviction_group *evg = rule->eviction_group;
3818
3819 rule->used = used;
3820 if (evg) {
3821 heap_change(&evg->rules, &rule->evg_node,
3822 rule_eviction_priority(rule));
3823 }
3824 }
3825 }
3826
3827 /* Sends an OpenFlow "flow removed" message with the given 'reason' (either
3828 * OFPRR_HARD_TIMEOUT or OFPRR_IDLE_TIMEOUT), and then removes 'rule' from its
3829 * ofproto.
3830 *
3831 * 'rule' must not have a pending operation (that is, 'rule->pending' must be
3832 * NULL).
3833 *
3834 * ofproto implementation ->run() functions should use this function to expire
3835 * OpenFlow flows. */
3836 void
3837 ofproto_rule_expire(struct rule *rule, uint8_t reason)
3838 {
3839 struct ofproto *ofproto = rule->ofproto;
3840 struct classifier *cls = &ofproto->tables[rule->table_id].cls;
3841
3842 ovs_assert(reason == OFPRR_HARD_TIMEOUT || reason == OFPRR_IDLE_TIMEOUT
3843 || reason == OFPRR_DELETE || reason == OFPRR_GROUP_DELETE);
3844 ofproto_rule_send_removed(rule, reason);
3845
3846 ovs_rwlock_wrlock(&cls->rwlock);
3847 ofproto_delete_rule(ofproto, cls, rule);
3848 ovs_rwlock_unlock(&cls->rwlock);
3849 }
3850
3851 /* Reduces '*timeout' to no more than 'max'. A value of zero in either case
3852 * means "infinite". */
3853 static void
3854 reduce_timeout(uint16_t max, uint16_t *timeout)
3855 {
3856 if (max && (!*timeout || *timeout > max)) {
3857 *timeout = max;
3858 }
3859 }
3860
3861 /* If 'idle_timeout' is nonzero, and 'rule' has no idle timeout or an idle
3862 * timeout greater than 'idle_timeout', lowers 'rule''s idle timeout to
3863 * 'idle_timeout' seconds. Similarly for 'hard_timeout'.
3864 *
3865 * Suitable for implementing OFPACT_FIN_TIMEOUT. */
3866 void
3867 ofproto_rule_reduce_timeouts(struct rule *rule,
3868 uint16_t idle_timeout, uint16_t hard_timeout)
3869 OVS_EXCLUDED(rule->ofproto->expirable_mutex, rule->timeout_mutex)
3870 {
3871 if (!idle_timeout && !hard_timeout) {
3872 return;
3873 }
3874
3875 ovs_mutex_lock(&rule->ofproto->expirable_mutex);
3876 if (list_is_empty(&rule->expirable)) {
3877 list_insert(&rule->ofproto->expirable, &rule->expirable);
3878 }
3879 ovs_mutex_unlock(&rule->ofproto->expirable_mutex);
3880
3881 ovs_mutex_lock(&rule->timeout_mutex);
3882 reduce_timeout(idle_timeout, &rule->idle_timeout);
3883 reduce_timeout(hard_timeout, &rule->hard_timeout);
3884 ovs_mutex_unlock(&rule->timeout_mutex);
3885
3886 if (!rule->eviction_group) {
3887 eviction_group_add_rule(rule);
3888 }
3889 }
3890 \f
3891 static enum ofperr
3892 handle_flow_mod(struct ofconn *ofconn, const struct ofp_header *oh)
3893 {
3894 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3895 struct ofputil_flow_mod fm;
3896 uint64_t ofpacts_stub[1024 / 8];
3897 struct ofpbuf ofpacts;
3898 enum ofperr error;
3899 long long int now;
3900
3901 error = reject_slave_controller(ofconn);
3902 if (error) {
3903 goto exit;
3904 }
3905
3906 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
3907 error = ofputil_decode_flow_mod(&fm, oh, ofconn_get_protocol(ofconn),
3908 &ofpacts);
3909 if (!error) {
3910 error = handle_flow_mod__(ofproto, ofconn, &fm, oh);
3911 }
3912 if (error) {
3913 goto exit_free_ofpacts;
3914 }
3915
3916 /* Record the operation for logging a summary report. */
3917 switch (fm.command) {
3918 case OFPFC_ADD:
3919 ofproto->n_add++;
3920 break;
3921
3922 case OFPFC_MODIFY:
3923 case OFPFC_MODIFY_STRICT:
3924 ofproto->n_modify++;
3925 break;
3926
3927 case OFPFC_DELETE:
3928 case OFPFC_DELETE_STRICT:
3929 ofproto->n_delete++;
3930 break;
3931 }
3932
3933 now = time_msec();
3934 if (ofproto->next_op_report == LLONG_MAX) {
3935 ofproto->first_op = now;
3936 ofproto->next_op_report = MAX(now + 10 * 1000,
3937 ofproto->op_backoff);
3938 ofproto->op_backoff = ofproto->next_op_report + 60 * 1000;
3939 }
3940 ofproto->last_op = now;
3941
3942 exit_free_ofpacts:
3943 ofpbuf_uninit(&ofpacts);
3944 exit:
3945 return error;
3946 }
3947
3948 static enum ofperr
3949 handle_flow_mod__(struct ofproto *ofproto, struct ofconn *ofconn,
3950 struct ofputil_flow_mod *fm, const struct ofp_header *oh)
3951 {
3952 if (ofproto->n_pending >= 50) {
3953 ovs_assert(!list_is_empty(&ofproto->pending));
3954 return OFPROTO_POSTPONE;
3955 }
3956
3957 switch (fm->command) {
3958 case OFPFC_ADD:
3959 return add_flow(ofproto, ofconn, fm, oh);
3960
3961 case OFPFC_MODIFY:
3962 return modify_flows_loose(ofproto, ofconn, fm, oh);
3963
3964 case OFPFC_MODIFY_STRICT:
3965 return modify_flow_strict(ofproto, ofconn, fm, oh);
3966
3967 case OFPFC_DELETE:
3968 return delete_flows_loose(ofproto, ofconn, fm, oh);
3969
3970 case OFPFC_DELETE_STRICT:
3971 return delete_flow_strict(ofproto, ofconn, fm, oh);
3972
3973 default:
3974 if (fm->command > 0xff) {
3975 VLOG_WARN_RL(&rl, "%s: flow_mod has explicit table_id but "
3976 "flow_mod_table_id extension is not enabled",
3977 ofproto->name);
3978 }
3979 return OFPERR_OFPFMFC_BAD_COMMAND;
3980 }
3981 }
3982
3983 static enum ofperr
3984 handle_role_request(struct ofconn *ofconn, const struct ofp_header *oh)
3985 {
3986 struct ofputil_role_request request;
3987 struct ofputil_role_request reply;
3988 struct ofpbuf *buf;
3989 enum ofperr error;
3990
3991 error = ofputil_decode_role_message(oh, &request);
3992 if (error) {
3993 return error;
3994 }
3995
3996 if (request.role != OFPCR12_ROLE_NOCHANGE) {
3997 if (ofconn_get_role(ofconn) != request.role
3998 && ofconn_has_pending_opgroups(ofconn)) {
3999 return OFPROTO_POSTPONE;
4000 }
4001
4002 if (request.have_generation_id
4003 && !ofconn_set_master_election_id(ofconn, request.generation_id)) {
4004 return OFPERR_OFPRRFC_STALE;
4005 }
4006
4007 ofconn_set_role(ofconn, request.role);
4008 }
4009
4010 reply.role = ofconn_get_role(ofconn);
4011 reply.have_generation_id = ofconn_get_master_election_id(
4012 ofconn, &reply.generation_id);
4013 buf = ofputil_encode_role_reply(oh, &reply);
4014 ofconn_send_reply(ofconn, buf);
4015
4016 return 0;
4017 }
4018
4019 static enum ofperr
4020 handle_nxt_flow_mod_table_id(struct ofconn *ofconn,
4021 const struct ofp_header *oh)
4022 {
4023 const struct nx_flow_mod_table_id *msg = ofpmsg_body(oh);
4024 enum ofputil_protocol cur, next;
4025
4026 cur = ofconn_get_protocol(ofconn);
4027 next = ofputil_protocol_set_tid(cur, msg->set != 0);
4028 ofconn_set_protocol(ofconn, next);
4029
4030 return 0;
4031 }
4032
4033 static enum ofperr
4034 handle_nxt_set_flow_format(struct ofconn *ofconn, const struct ofp_header *oh)
4035 {
4036 const struct nx_set_flow_format *msg = ofpmsg_body(oh);
4037 enum ofputil_protocol cur, next;
4038 enum ofputil_protocol next_base;
4039
4040 next_base = ofputil_nx_flow_format_to_protocol(ntohl(msg->format));
4041 if (!next_base) {
4042 return OFPERR_OFPBRC_EPERM;
4043 }
4044
4045 cur = ofconn_get_protocol(ofconn);
4046 next = ofputil_protocol_set_base(cur, next_base);
4047 if (cur != next && ofconn_has_pending_opgroups(ofconn)) {
4048 /* Avoid sending async messages in surprising protocol. */
4049 return OFPROTO_POSTPONE;
4050 }
4051
4052 ofconn_set_protocol(ofconn, next);
4053 return 0;
4054 }
4055
4056 static enum ofperr
4057 handle_nxt_set_packet_in_format(struct ofconn *ofconn,
4058 const struct ofp_header *oh)
4059 {
4060 const struct nx_set_packet_in_format *msg = ofpmsg_body(oh);
4061 uint32_t format;
4062
4063 format = ntohl(msg->format);
4064 if (format != NXPIF_OPENFLOW10 && format != NXPIF_NXM) {
4065 return OFPERR_OFPBRC_EPERM;
4066 }
4067
4068 if (format != ofconn_get_packet_in_format(ofconn)
4069 && ofconn_has_pending_opgroups(ofconn)) {
4070 /* Avoid sending async message in surprsing packet in format. */
4071 return OFPROTO_POSTPONE;
4072 }
4073
4074 ofconn_set_packet_in_format(ofconn, format);
4075 return 0;
4076 }
4077
4078 static enum ofperr
4079 handle_nxt_set_async_config(struct ofconn *ofconn, const struct ofp_header *oh)
4080 {
4081 const struct nx_async_config *msg = ofpmsg_body(oh);
4082 uint32_t master[OAM_N_TYPES];
4083 uint32_t slave[OAM_N_TYPES];
4084
4085 master[OAM_PACKET_IN] = ntohl(msg->packet_in_mask[0]);
4086 master[OAM_PORT_STATUS] = ntohl(msg->port_status_mask[0]);
4087 master[OAM_FLOW_REMOVED] = ntohl(msg->flow_removed_mask[0]);
4088
4089 slave[OAM_PACKET_IN] = ntohl(msg->packet_in_mask[1]);
4090 slave[OAM_PORT_STATUS] = ntohl(msg->port_status_mask[1]);
4091 slave[OAM_FLOW_REMOVED] = ntohl(msg->flow_removed_mask[1]);
4092
4093 ofconn_set_async_config(ofconn, master, slave);
4094 if (ofconn_get_type(ofconn) == OFCONN_SERVICE &&
4095 !ofconn_get_miss_send_len(ofconn)) {
4096 ofconn_set_miss_send_len(ofconn, OFP_DEFAULT_MISS_SEND_LEN);
4097 }
4098
4099 return 0;
4100 }
4101
4102 static enum ofperr
4103 handle_nxt_set_controller_id(struct ofconn *ofconn,
4104 const struct ofp_header *oh)
4105 {
4106 const struct nx_controller_id *nci = ofpmsg_body(oh);
4107
4108 if (!is_all_zeros(nci->zero, sizeof nci->zero)) {
4109 return OFPERR_NXBRC_MUST_BE_ZERO;
4110 }
4111
4112 ofconn_set_controller_id(ofconn, ntohs(nci->controller_id));
4113 return 0;
4114 }
4115
4116 static enum ofperr
4117 handle_barrier_request(struct ofconn *ofconn, const struct ofp_header *oh)
4118 {
4119 struct ofpbuf *buf;
4120
4121 if (ofconn_has_pending_opgroups(ofconn)) {
4122 return OFPROTO_POSTPONE;
4123 }
4124
4125 buf = ofpraw_alloc_reply((oh->version == OFP10_VERSION
4126 ? OFPRAW_OFPT10_BARRIER_REPLY
4127 : OFPRAW_OFPT11_BARRIER_REPLY), oh, 0);
4128 ofconn_send_reply(ofconn, buf);
4129 return 0;
4130 }
4131
4132 static void
4133 ofproto_compose_flow_refresh_update(const struct rule *rule,
4134 enum nx_flow_monitor_flags flags,
4135 struct list *msgs)
4136 {
4137 struct ofoperation *op = rule->pending;
4138 struct ofputil_flow_update fu;
4139 struct match match;
4140
4141 if (op && op->type == OFOPERATION_ADD) {
4142 /* We'll report the final flow when the operation completes. Reporting
4143 * it now would cause a duplicate report later. */
4144 return;
4145 }
4146
4147 fu.event = (flags & (NXFMF_INITIAL | NXFMF_ADD)
4148 ? NXFME_ADDED : NXFME_MODIFIED);
4149 fu.reason = 0;
4150 ovs_mutex_lock(&rule->timeout_mutex);
4151 fu.idle_timeout = rule->idle_timeout;
4152 fu.hard_timeout = rule->hard_timeout;
4153 ovs_mutex_unlock(&rule->timeout_mutex);
4154 fu.table_id = rule->table_id;
4155 fu.cookie = rule->flow_cookie;
4156 minimatch_expand(&rule->cr.match, &match);
4157 fu.match = &match;
4158 fu.priority = rule->cr.priority;
4159 if (!(flags & NXFMF_ACTIONS)) {
4160 fu.ofpacts = NULL;
4161 fu.ofpacts_len = 0;
4162 } else if (!op) {
4163 fu.ofpacts = rule->ofpacts;
4164 fu.ofpacts_len = rule->ofpacts_len;
4165 } else {
4166 /* An operation is in progress. Use the previous version of the flow's
4167 * actions, so that when the operation commits we report the change. */
4168 switch (op->type) {
4169 case OFOPERATION_ADD:
4170 NOT_REACHED();
4171
4172 case OFOPERATION_MODIFY:
4173 case OFOPERATION_REPLACE:
4174 if (op->ofpacts) {
4175 fu.ofpacts = op->ofpacts;
4176 fu.ofpacts_len = op->ofpacts_len;
4177 } else {
4178 fu.ofpacts = rule->ofpacts;
4179 fu.ofpacts_len = rule->ofpacts_len;
4180 }
4181 break;
4182
4183 case OFOPERATION_DELETE:
4184 fu.ofpacts = rule->ofpacts;
4185 fu.ofpacts_len = rule->ofpacts_len;
4186 break;
4187
4188 default:
4189 NOT_REACHED();
4190 }
4191 }
4192
4193 if (list_is_empty(msgs)) {
4194 ofputil_start_flow_update(msgs);
4195 }
4196 ofputil_append_flow_update(&fu, msgs);
4197 }
4198
4199 void
4200 ofmonitor_compose_refresh_updates(struct list *rules, struct list *msgs)
4201 {
4202 struct rule *rule;
4203
4204 LIST_FOR_EACH (rule, ofproto_node, rules) {
4205 enum nx_flow_monitor_flags flags = rule->monitor_flags;
4206 rule->monitor_flags = 0;
4207
4208 ofproto_compose_flow_refresh_update(rule, flags, msgs);
4209 }
4210 }
4211
4212 static void
4213 ofproto_collect_ofmonitor_refresh_rule(const struct ofmonitor *m,
4214 struct rule *rule, uint64_t seqno,
4215 struct list *rules)
4216 {
4217 enum nx_flow_monitor_flags update;
4218
4219 if (ofproto_rule_is_hidden(rule)) {
4220 return;
4221 }
4222
4223 if (!(rule->pending
4224 ? ofoperation_has_out_port(rule->pending, m->out_port)
4225 : ofproto_rule_has_out_port(rule, m->out_port))) {
4226 return;
4227 }
4228
4229 if (seqno) {
4230 if (rule->add_seqno > seqno) {
4231 update = NXFMF_ADD | NXFMF_MODIFY;
4232 } else if (rule->modify_seqno > seqno) {
4233 update = NXFMF_MODIFY;
4234 } else {
4235 return;
4236 }
4237
4238 if (!(m->flags & update)) {
4239 return;
4240 }
4241 } else {
4242 update = NXFMF_INITIAL;
4243 }
4244
4245 if (!rule->monitor_flags) {
4246 list_push_back(rules, &rule->ofproto_node);
4247 }
4248 rule->monitor_flags |= update | (m->flags & NXFMF_ACTIONS);
4249 }
4250
4251 static void
4252 ofproto_collect_ofmonitor_refresh_rules(const struct ofmonitor *m,
4253 uint64_t seqno,
4254 struct list *rules)
4255 {
4256 const struct ofproto *ofproto = ofconn_get_ofproto(m->ofconn);
4257 const struct ofoperation *op;
4258 const struct oftable *table;
4259 struct cls_rule target;
4260
4261 cls_rule_init_from_minimatch(&target, &m->match, 0);
4262 FOR_EACH_MATCHING_TABLE (table, m->table_id, ofproto) {
4263 struct cls_cursor cursor;
4264 struct rule *rule;
4265
4266 ovs_rwlock_rdlock(&table->cls.rwlock);
4267 cls_cursor_init(&cursor, &table->cls, &target);
4268 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
4269 ovs_assert(!rule->pending); /* XXX */
4270 ofproto_collect_ofmonitor_refresh_rule(m, rule, seqno, rules);
4271 }
4272 ovs_rwlock_unlock(&table->cls.rwlock);
4273 }
4274
4275 HMAP_FOR_EACH (op, hmap_node, &ofproto->deletions) {
4276 struct rule *rule = op->rule;
4277
4278 if (((m->table_id == 0xff
4279 ? !(ofproto->tables[rule->table_id].flags & OFTABLE_HIDDEN)
4280 : m->table_id == rule->table_id))
4281 && cls_rule_is_loose_match(&rule->cr, &target.match)) {
4282 ofproto_collect_ofmonitor_refresh_rule(m, rule, seqno, rules);
4283 }
4284 }
4285 cls_rule_destroy(&target);
4286 }
4287
4288 static void
4289 ofproto_collect_ofmonitor_initial_rules(struct ofmonitor *m,
4290 struct list *rules)
4291 {
4292 if (m->flags & NXFMF_INITIAL) {
4293 ofproto_collect_ofmonitor_refresh_rules(m, 0, rules);
4294 }
4295 }
4296
4297 void
4298 ofmonitor_collect_resume_rules(struct ofmonitor *m,
4299 uint64_t seqno, struct list *rules)
4300 {
4301 ofproto_collect_ofmonitor_refresh_rules(m, seqno, rules);
4302 }
4303
4304 static enum ofperr
4305 handle_flow_monitor_request(struct ofconn *ofconn, const struct ofp_header *oh)
4306 {
4307 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4308 struct ofmonitor **monitors;
4309 size_t n_monitors, allocated_monitors;
4310 struct list replies;
4311 enum ofperr error;
4312 struct list rules;
4313 struct ofpbuf b;
4314 size_t i;
4315
4316 error = 0;
4317 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4318 monitors = NULL;
4319 n_monitors = allocated_monitors = 0;
4320 for (;;) {
4321 struct ofputil_flow_monitor_request request;
4322 struct ofmonitor *m;
4323 int retval;
4324
4325 retval = ofputil_decode_flow_monitor_request(&request, &b);
4326 if (retval == EOF) {
4327 break;
4328 } else if (retval) {
4329 error = retval;
4330 goto error;
4331 }
4332
4333 if (request.table_id != 0xff
4334 && request.table_id >= ofproto->n_tables) {
4335 error = OFPERR_OFPBRC_BAD_TABLE_ID;
4336 goto error;
4337 }
4338
4339 error = ofmonitor_create(&request, ofconn, &m);
4340 if (error) {
4341 goto error;
4342 }
4343
4344 if (n_monitors >= allocated_monitors) {
4345 monitors = x2nrealloc(monitors, &allocated_monitors,
4346 sizeof *monitors);
4347 }
4348 monitors[n_monitors++] = m;
4349 }
4350
4351 list_init(&rules);
4352 for (i = 0; i < n_monitors; i++) {
4353 ofproto_collect_ofmonitor_initial_rules(monitors[i], &rules);
4354 }
4355
4356 ofpmp_init(&replies, oh);
4357 ofmonitor_compose_refresh_updates(&rules, &replies);
4358 ofconn_send_replies(ofconn, &replies);
4359
4360 free(monitors);
4361
4362 return 0;
4363
4364 error:
4365 for (i = 0; i < n_monitors; i++) {
4366 ofmonitor_destroy(monitors[i]);
4367 }
4368 free(monitors);
4369 return error;
4370 }
4371
4372 static enum ofperr
4373 handle_flow_monitor_cancel(struct ofconn *ofconn, const struct ofp_header *oh)
4374 {
4375 struct ofmonitor *m;
4376 uint32_t id;
4377
4378 id = ofputil_decode_flow_monitor_cancel(oh);
4379 m = ofmonitor_lookup(ofconn, id);
4380 if (!m) {
4381 return OFPERR_NXBRC_FM_BAD_ID;
4382 }
4383
4384 ofmonitor_destroy(m);
4385 return 0;
4386 }
4387
4388 /* Meters implementation.
4389 *
4390 * Meter table entry, indexed by the OpenFlow meter_id.
4391 * These are always dynamically allocated to allocate enough space for
4392 * the bands.
4393 * 'created' is used to compute the duration for meter stats.
4394 * 'list rules' is needed so that we can delete the dependent rules when the
4395 * meter table entry is deleted.
4396 * 'provider_meter_id' is for the provider's private use.
4397 */
4398 struct meter {
4399 long long int created; /* Time created. */
4400 struct list rules; /* List of "struct rule_dpif"s. */
4401 ofproto_meter_id provider_meter_id;
4402 uint16_t flags; /* Meter flags. */
4403 uint16_t n_bands; /* Number of meter bands. */
4404 struct ofputil_meter_band *bands;
4405 };
4406
4407 /*
4408 * This is used in instruction validation at flow set-up time,
4409 * as flows may not use non-existing meters.
4410 * This is also used by ofproto-providers to translate OpenFlow meter_ids
4411 * in METER instructions to the corresponding provider meter IDs.
4412 * Return value of UINT32_MAX signifies an invalid meter.
4413 */
4414 uint32_t
4415 ofproto_get_provider_meter_id(const struct ofproto * ofproto,
4416 uint32_t of_meter_id)
4417 {
4418 if (of_meter_id && of_meter_id <= ofproto->meter_features.max_meters) {
4419 const struct meter *meter = ofproto->meters[of_meter_id];
4420 if (meter) {
4421 return meter->provider_meter_id.uint32;
4422 }
4423 }
4424 return UINT32_MAX;
4425 }
4426
4427 static void
4428 meter_update(struct meter *meter, const struct ofputil_meter_config *config)
4429 {
4430 free(meter->bands);
4431
4432 meter->flags = config->flags;
4433 meter->n_bands = config->n_bands;
4434 meter->bands = xmemdup(config->bands,
4435 config->n_bands * sizeof *meter->bands);
4436 }
4437
4438 static struct meter *
4439 meter_create(const struct ofputil_meter_config *config,
4440 ofproto_meter_id provider_meter_id)
4441 {
4442 struct meter *meter;
4443
4444 meter = xzalloc(sizeof *meter);
4445 meter->provider_meter_id = provider_meter_id;
4446 meter->created = time_msec();
4447 list_init(&meter->rules);
4448
4449 meter_update(meter, config);
4450
4451 return meter;
4452 }
4453
4454 static void
4455 meter_delete(struct ofproto *ofproto, uint32_t first, uint32_t last)
4456 {
4457 uint32_t mid;
4458 for (mid = first; mid <= last; ++mid) {
4459 struct meter *meter = ofproto->meters[mid];
4460 if (meter) {
4461 ofproto->meters[mid] = NULL;
4462 ofproto->ofproto_class->meter_del(ofproto,
4463 meter->provider_meter_id);
4464 free(meter->bands);
4465 free(meter);
4466 }
4467 }
4468 }
4469
4470 static enum ofperr
4471 handle_add_meter(struct ofproto *ofproto, struct ofputil_meter_mod *mm)
4472 {
4473 ofproto_meter_id provider_meter_id = { UINT32_MAX };
4474 struct meter **meterp = &ofproto->meters[mm->meter.meter_id];
4475 enum ofperr error;
4476
4477 if (*meterp) {
4478 return OFPERR_OFPMMFC_METER_EXISTS;
4479 }
4480
4481 error = ofproto->ofproto_class->meter_set(ofproto, &provider_meter_id,
4482 &mm->meter);
4483 if (!error) {
4484 ovs_assert(provider_meter_id.uint32 != UINT32_MAX);
4485 *meterp = meter_create(&mm->meter, provider_meter_id);
4486 }
4487 return 0;
4488 }
4489
4490 static enum ofperr
4491 handle_modify_meter(struct ofproto *ofproto, struct ofputil_meter_mod *mm)
4492 {
4493 struct meter *meter = ofproto->meters[mm->meter.meter_id];
4494 enum ofperr error;
4495
4496 if (!meter) {
4497 return OFPERR_OFPMMFC_UNKNOWN_METER;
4498 }
4499
4500 error = ofproto->ofproto_class->meter_set(ofproto,
4501 &meter->provider_meter_id,
4502 &mm->meter);
4503 ovs_assert(meter->provider_meter_id.uint32 != UINT32_MAX);
4504 if (!error) {
4505 meter_update(meter, &mm->meter);
4506 }
4507 return error;
4508 }
4509
4510 static enum ofperr
4511 handle_delete_meter(struct ofconn *ofconn, const struct ofp_header *oh,
4512 struct ofputil_meter_mod *mm)
4513 {
4514 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4515 uint32_t meter_id = mm->meter.meter_id;
4516 uint32_t first, last;
4517 struct list rules;
4518
4519 if (meter_id == OFPM13_ALL) {
4520 first = 1;
4521 last = ofproto->meter_features.max_meters;
4522 } else {
4523 if (!meter_id || meter_id > ofproto->meter_features.max_meters) {
4524 return 0;
4525 }
4526 first = last = meter_id;
4527 }
4528
4529 /* First delete the rules that use this meter. If any of those rules are
4530 * currently being modified, postpone the whole operation until later. */
4531 list_init(&rules);
4532 for (meter_id = first; meter_id <= last; ++meter_id) {
4533 struct meter *meter = ofproto->meters[meter_id];
4534 if (meter && !list_is_empty(&meter->rules)) {
4535 struct rule *rule;
4536
4537 LIST_FOR_EACH (rule, meter_list_node, &meter->rules) {
4538 if (rule->pending) {
4539 return OFPROTO_POSTPONE;
4540 }
4541 list_push_back(&rules, &rule->ofproto_node);
4542 }
4543 }
4544 }
4545 if (!list_is_empty(&rules)) {
4546 delete_flows__(ofproto, ofconn, oh, &rules, OFPRR_METER_DELETE);
4547 }
4548
4549 /* Delete the meters. */
4550 meter_delete(ofproto, first, last);
4551
4552 return 0;
4553 }
4554
4555 static enum ofperr
4556 handle_meter_mod(struct ofconn *ofconn, const struct ofp_header *oh)
4557 {
4558 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4559 struct ofputil_meter_mod mm;
4560 uint64_t bands_stub[256 / 8];
4561 struct ofpbuf bands;
4562 uint32_t meter_id;
4563 enum ofperr error;
4564
4565 error = reject_slave_controller(ofconn);
4566 if (error) {
4567 return error;
4568 }
4569
4570 ofpbuf_use_stub(&bands, bands_stub, sizeof bands_stub);
4571
4572 error = ofputil_decode_meter_mod(oh, &mm, &bands);
4573 if (error) {
4574 goto exit_free_bands;
4575 }
4576
4577 meter_id = mm.meter.meter_id;
4578
4579 if (mm.command != OFPMC13_DELETE) {
4580 /* Fails also when meters are not implemented by the provider. */
4581 if (!meter_id || meter_id > ofproto->meter_features.max_meters) {
4582 error = OFPERR_OFPMMFC_INVALID_METER;
4583 goto exit_free_bands;
4584 }
4585 if (mm.meter.n_bands > ofproto->meter_features.max_bands) {
4586 error = OFPERR_OFPMMFC_OUT_OF_BANDS;
4587 goto exit_free_bands;
4588 }
4589 }
4590
4591 switch (mm.command) {
4592 case OFPMC13_ADD:
4593 error = handle_add_meter(ofproto, &mm);
4594 break;
4595
4596 case OFPMC13_MODIFY:
4597 error = handle_modify_meter(ofproto, &mm);
4598 break;
4599
4600 case OFPMC13_DELETE:
4601 error = handle_delete_meter(ofconn, oh, &mm);
4602 break;
4603
4604 default:
4605 error = OFPERR_OFPMMFC_BAD_COMMAND;
4606 break;
4607 }
4608
4609 exit_free_bands:
4610 ofpbuf_uninit(&bands);
4611 return error;
4612 }
4613
4614 static enum ofperr
4615 handle_meter_features_request(struct ofconn *ofconn,
4616 const struct ofp_header *request)
4617 {
4618 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4619 struct ofputil_meter_features features;
4620 struct ofpbuf *b;
4621
4622 if (ofproto->ofproto_class->meter_get_features) {
4623 ofproto->ofproto_class->meter_get_features(ofproto, &features);
4624 } else {
4625 memset(&features, 0, sizeof features);
4626 }
4627 b = ofputil_encode_meter_features_reply(&features, request);
4628
4629 ofconn_send_reply(ofconn, b);
4630 return 0;
4631 }
4632
4633 static enum ofperr
4634 handle_meter_request(struct ofconn *ofconn, const struct ofp_header *request,
4635 enum ofptype type)
4636 {
4637 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4638 struct list replies;
4639 uint64_t bands_stub[256 / 8];
4640 struct ofpbuf bands;
4641 uint32_t meter_id, first, last;
4642
4643 ofputil_decode_meter_request(request, &meter_id);
4644
4645 if (meter_id == OFPM13_ALL) {
4646 first = 1;
4647 last = ofproto->meter_features.max_meters;
4648 } else {
4649 if (!meter_id || meter_id > ofproto->meter_features.max_meters ||
4650 !ofproto->meters[meter_id]) {
4651 return OFPERR_OFPMMFC_UNKNOWN_METER;
4652 }
4653 first = last = meter_id;
4654 }
4655
4656 ofpbuf_use_stub(&bands, bands_stub, sizeof bands_stub);
4657 ofpmp_init(&replies, request);
4658
4659 for (meter_id = first; meter_id <= last; ++meter_id) {
4660 struct meter *meter = ofproto->meters[meter_id];
4661 if (!meter) {
4662 continue; /* Skip non-existing meters. */
4663 }
4664 if (type == OFPTYPE_METER_STATS_REQUEST) {
4665 struct ofputil_meter_stats stats;
4666
4667 stats.meter_id = meter_id;
4668
4669 /* Provider sets the packet and byte counts, we do the rest. */
4670 stats.flow_count = list_size(&meter->rules);
4671 calc_duration(meter->created, time_msec(),
4672 &stats.duration_sec, &stats.duration_nsec);
4673 stats.n_bands = meter->n_bands;
4674 ofpbuf_clear(&bands);
4675 stats.bands
4676 = ofpbuf_put_uninit(&bands,
4677 meter->n_bands * sizeof *stats.bands);
4678
4679 if (!ofproto->ofproto_class->meter_get(ofproto,
4680 meter->provider_meter_id,
4681 &stats)) {
4682 ofputil_append_meter_stats(&replies, &stats);
4683 }
4684 } else { /* type == OFPTYPE_METER_CONFIG_REQUEST */
4685 struct ofputil_meter_config config;
4686
4687 config.meter_id = meter_id;
4688 config.flags = meter->flags;
4689 config.n_bands = meter->n_bands;
4690 config.bands = meter->bands;
4691 ofputil_append_meter_config(&replies, &config);
4692 }
4693 }
4694
4695 ofconn_send_replies(ofconn, &replies);
4696 ofpbuf_uninit(&bands);
4697 return 0;
4698 }
4699
4700 bool
4701 ofproto_group_lookup(const struct ofproto *ofproto, uint32_t group_id,
4702 struct ofgroup **group)
4703 OVS_TRY_RDLOCK(true, (*group)->rwlock)
4704 {
4705 ovs_rwlock_rdlock(&ofproto->groups_rwlock);
4706 HMAP_FOR_EACH_IN_BUCKET (*group, hmap_node,
4707 hash_int(group_id, 0), &ofproto->groups) {
4708 if ((*group)->group_id == group_id) {
4709 ovs_rwlock_rdlock(&(*group)->rwlock);
4710 ovs_rwlock_unlock(&ofproto->groups_rwlock);
4711 return true;
4712 }
4713 }
4714 ovs_rwlock_unlock(&ofproto->groups_rwlock);
4715 return false;
4716 }
4717
4718 void
4719 ofproto_group_release(struct ofgroup *group)
4720 OVS_RELEASES(group->rwlock)
4721 {
4722 ovs_rwlock_unlock(&group->rwlock);
4723 }
4724
4725 static bool
4726 ofproto_group_write_lookup(const struct ofproto *ofproto, uint32_t group_id,
4727 struct ofgroup **group)
4728 OVS_TRY_WRLOCK(true, ofproto->groups_rwlock)
4729 OVS_TRY_WRLOCK(true, (*group)->rwlock)
4730 {
4731 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
4732 HMAP_FOR_EACH_IN_BUCKET (*group, hmap_node,
4733 hash_int(group_id, 0), &ofproto->groups) {
4734 if ((*group)->group_id == group_id) {
4735 ovs_rwlock_wrlock(&(*group)->rwlock);
4736 return true;
4737 }
4738 }
4739 ovs_rwlock_unlock(&ofproto->groups_rwlock);
4740 return false;
4741 }
4742
4743 static bool
4744 ofproto_group_exists(const struct ofproto *ofproto, uint32_t group_id)
4745 OVS_REQ_RDLOCK(ofproto->groups_rwlock)
4746 {
4747 struct ofgroup *grp;
4748
4749 HMAP_FOR_EACH_IN_BUCKET (grp, hmap_node,
4750 hash_int(group_id, 0), &ofproto->groups) {
4751 if (grp->group_id == group_id) {
4752 return true;
4753 }
4754 }
4755 return false;
4756 }
4757
4758 static void
4759 append_group_stats(struct ofgroup *group, struct list *replies)
4760 OVS_REQ_RDLOCK(group->rwlock)
4761 {
4762 struct ofputil_group_stats ogs;
4763 struct ofproto *ofproto = group->ofproto;
4764 long long int now = time_msec();
4765 int error;
4766
4767 error = (ofproto->ofproto_class->group_get_stats
4768 ? ofproto->ofproto_class->group_get_stats(group, &ogs)
4769 : EOPNOTSUPP);
4770 if (error) {
4771 ogs.ref_count = UINT32_MAX;
4772 ogs.packet_count = UINT64_MAX;
4773 ogs.byte_count = UINT64_MAX;
4774 ogs.n_buckets = group->n_buckets;
4775 memset(ogs.bucket_stats, 0xff,
4776 ogs.n_buckets * sizeof *ogs.bucket_stats);
4777 }
4778
4779 ogs.group_id = group->group_id;
4780 calc_duration(group->created, now, &ogs.duration_sec, &ogs.duration_nsec);
4781
4782 ofputil_append_group_stats(replies, &ogs);
4783 }
4784
4785 static enum ofperr
4786 handle_group_stats_request(struct ofconn *ofconn,
4787 const struct ofp_header *request)
4788 {
4789 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4790 struct list replies;
4791 enum ofperr error;
4792 struct ofgroup *group;
4793 uint32_t group_id;
4794
4795 error = ofputil_decode_group_stats_request(request, &group_id);
4796 if (error) {
4797 return error;
4798 }
4799
4800 ofpmp_init(&replies, request);
4801
4802 if (group_id == OFPG_ALL) {
4803 ovs_rwlock_rdlock(&ofproto->groups_rwlock);
4804 HMAP_FOR_EACH (group, hmap_node, &ofproto->groups) {
4805 ovs_rwlock_rdlock(&group->rwlock);
4806 append_group_stats(group, &replies);
4807 ovs_rwlock_unlock(&group->rwlock);
4808 }
4809 ovs_rwlock_unlock(&ofproto->groups_rwlock);
4810 } else {
4811 if (ofproto_group_lookup(ofproto, group_id, &group)) {
4812 append_group_stats(group, &replies);
4813 ofproto_group_release(group);
4814 }
4815 }
4816
4817 ofconn_send_replies(ofconn, &replies);
4818
4819 return 0;
4820 }
4821
4822 static enum ofperr
4823 handle_group_desc_stats_request(struct ofconn *ofconn,
4824 const struct ofp_header *request)
4825 {
4826 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4827 struct list replies;
4828 struct ofputil_group_desc gds;
4829 struct ofgroup *group;
4830
4831 ofpmp_init(&replies, request);
4832
4833 ovs_rwlock_rdlock(&ofproto->groups_rwlock);
4834 HMAP_FOR_EACH (group, hmap_node, &ofproto->groups) {
4835 gds.group_id = group->group_id;
4836 gds.type = group->type;
4837 ofputil_append_group_desc_reply(&gds, &group->buckets, &replies);
4838 }
4839 ovs_rwlock_unlock(&ofproto->groups_rwlock);
4840
4841 ofconn_send_replies(ofconn, &replies);
4842
4843 return 0;
4844 }
4845
4846 static enum ofperr
4847 handle_group_features_stats_request(struct ofconn *ofconn,
4848 const struct ofp_header *request)
4849 {
4850 struct ofproto *p = ofconn_get_ofproto(ofconn);
4851 struct ofpbuf *msg;
4852
4853 msg = ofputil_encode_group_features_reply(&p->ogf, request);
4854 if (msg) {
4855 ofconn_send_reply(ofconn, msg);
4856 }
4857
4858 return 0;
4859 }
4860
4861 /* Implements OFPGC11_ADD
4862 * in which no matching flow already exists in the flow table.
4863 *
4864 * Adds the flow specified by 'ofm', which is followed by 'n_actions'
4865 * ofp_actions, to the ofproto's flow table. Returns 0 on success, an OpenFlow
4866 * error code on failure, or OFPROTO_POSTPONE if the operation cannot be
4867 * initiated now but may be retried later.
4868 *
4869 * Upon successful return, takes ownership of 'fm->ofpacts'. On failure,
4870 * ownership remains with the caller.
4871 *
4872 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
4873 * if any. */
4874 static enum ofperr
4875 add_group(struct ofproto *ofproto, struct ofputil_group_mod *gm)
4876 {
4877 struct ofgroup *ofgroup;
4878 enum ofperr error;
4879
4880 if (gm->group_id > OFPG_MAX) {
4881 return OFPERR_OFPGMFC_INVALID_GROUP;
4882 }
4883 if (gm->type > OFPGT11_FF) {
4884 return OFPERR_OFPGMFC_BAD_TYPE;
4885 }
4886
4887 /* Allocate new group and initialize it. */
4888 ofgroup = ofproto->ofproto_class->group_alloc();
4889 if (!ofgroup) {
4890 VLOG_WARN_RL(&rl, "%s: failed to create group", ofproto->name);
4891 return OFPERR_OFPGMFC_OUT_OF_GROUPS;
4892 }
4893
4894 ovs_rwlock_init(&ofgroup->rwlock);
4895 ofgroup->ofproto = ofproto;
4896 ofgroup->group_id = gm->group_id;
4897 ofgroup->type = gm->type;
4898 ofgroup->created = ofgroup->modified = time_msec();
4899
4900 list_move(&ofgroup->buckets, &gm->buckets);
4901 ofgroup->n_buckets = list_size(&ofgroup->buckets);
4902
4903 /* Construct called BEFORE any locks are held. */
4904 error = ofproto->ofproto_class->group_construct(ofgroup);
4905 if (error) {
4906 goto free_out;
4907 }
4908
4909 /* We wrlock as late as possible to minimize the time we jam any other
4910 * threads: No visible state changes before acquiring the lock. */
4911 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
4912
4913 if (ofproto->n_groups[gm->type] >= ofproto->ogf.max_groups[gm->type]) {
4914 error = OFPERR_OFPGMFC_OUT_OF_GROUPS;
4915 goto unlock_out;
4916 }
4917
4918 if (ofproto_group_exists(ofproto, gm->group_id)) {
4919 error = OFPERR_OFPGMFC_GROUP_EXISTS;
4920 goto unlock_out;
4921 }
4922
4923 if (!error) {
4924 /* Insert new group. */
4925 hmap_insert(&ofproto->groups, &ofgroup->hmap_node,
4926 hash_int(ofgroup->group_id, 0));
4927 ofproto->n_groups[ofgroup->type]++;
4928
4929 ovs_rwlock_unlock(&ofproto->groups_rwlock);
4930 return error;
4931 }
4932
4933 unlock_out:
4934 ovs_rwlock_unlock(&ofproto->groups_rwlock);
4935 ofproto->ofproto_class->group_destruct(ofgroup);
4936 free_out:
4937 ofputil_bucket_list_destroy(&ofgroup->buckets);
4938 ofproto->ofproto_class->group_dealloc(ofgroup);
4939
4940 return error;
4941 }
4942
4943 /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code on
4944 * failure.
4945 *
4946 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
4947 * if any. */
4948 static enum ofperr
4949 modify_group(struct ofproto *ofproto, struct ofputil_group_mod *gm)
4950 {
4951 struct ofgroup *ofgroup;
4952 struct ofgroup *victim;
4953 enum ofperr error;
4954
4955 if (gm->group_id > OFPG_MAX) {
4956 return OFPERR_OFPGMFC_INVALID_GROUP;
4957 }
4958
4959 if (gm->type > OFPGT11_FF) {
4960 return OFPERR_OFPGMFC_BAD_TYPE;
4961 }
4962
4963 victim = ofproto->ofproto_class->group_alloc();
4964 if (!victim) {
4965 VLOG_WARN_RL(&rl, "%s: failed to allocate group", ofproto->name);
4966 return OFPERR_OFPGMFC_OUT_OF_GROUPS;
4967 }
4968
4969 if (!ofproto_group_write_lookup(ofproto, gm->group_id, &ofgroup)) {
4970 error = OFPERR_OFPGMFC_UNKNOWN_GROUP;
4971 goto free_out;
4972 }
4973 /* Both group's and its container's write locks held now.
4974 * Also, n_groups[] is protected by ofproto->groups_rwlock. */
4975 if (ofgroup->type != gm->type
4976 && ofproto->n_groups[gm->type] >= ofproto->ogf.max_groups[gm->type]) {
4977 error = OFPERR_OFPGMFC_OUT_OF_GROUPS;
4978 goto unlock_out;
4979 }
4980
4981 *victim = *ofgroup;
4982 list_move(&victim->buckets, &ofgroup->buckets);
4983
4984 ofgroup->type = gm->type;
4985 list_move(&ofgroup->buckets, &gm->buckets);
4986 ofgroup->n_buckets = list_size(&ofgroup->buckets);
4987
4988 error = ofproto->ofproto_class->group_modify(ofgroup, victim);
4989 if (!error) {
4990 ofputil_bucket_list_destroy(&victim->buckets);
4991 ofproto->n_groups[victim->type]--;
4992 ofproto->n_groups[ofgroup->type]++;
4993 ofgroup->modified = time_msec();
4994 } else {
4995 ofputil_bucket_list_destroy(&ofgroup->buckets);
4996
4997 *ofgroup = *victim;
4998 list_move(&ofgroup->buckets, &victim->buckets);
4999 }
5000
5001 unlock_out:
5002 ovs_rwlock_unlock(&ofgroup->rwlock);
5003 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5004 free_out:
5005 ofproto->ofproto_class->group_dealloc(victim);
5006 return error;
5007 }
5008
5009 static void
5010 delete_group__(struct ofproto *ofproto, struct ofgroup *ofgroup)
5011 OVS_RELEASES(ofproto->groups_rwlock)
5012 {
5013 /* Must wait until existing readers are done,
5014 * while holding the container's write lock at the same time. */
5015 ovs_rwlock_wrlock(&ofgroup->rwlock);
5016 hmap_remove(&ofproto->groups, &ofgroup->hmap_node);
5017 /* No-one can find this group any more. */
5018 ofproto->n_groups[ofgroup->type]--;
5019 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5020
5021 ofproto->ofproto_class->group_destruct(ofgroup);
5022 ofputil_bucket_list_destroy(&ofgroup->buckets);
5023 ovs_rwlock_unlock(&ofgroup->rwlock);
5024 ovs_rwlock_destroy(&ofgroup->rwlock);
5025 ofproto->ofproto_class->group_dealloc(ofgroup);
5026 }
5027
5028 /* Implements OFPGC_DELETE. */
5029 static void
5030 delete_group(struct ofproto *ofproto, uint32_t group_id)
5031 {
5032 struct ofgroup *ofgroup;
5033
5034 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
5035 if (group_id == OFPG_ALL) {
5036 for (;;) {
5037 struct hmap_node *node = hmap_first(&ofproto->groups);
5038 if (!node) {
5039 break;
5040 }
5041 ofgroup = CONTAINER_OF(node, struct ofgroup, hmap_node);
5042 delete_group__(ofproto, ofgroup);
5043 /* Lock for each node separately, so that we will not jam the
5044 * other threads for too long time. */
5045 ovs_rwlock_wrlock(&ofproto->groups_rwlock);
5046 }
5047 } else {
5048 HMAP_FOR_EACH_IN_BUCKET (ofgroup, hmap_node,
5049 hash_int(group_id, 0), &ofproto->groups) {
5050 if (ofgroup->group_id == group_id) {
5051 delete_group__(ofproto, ofgroup);
5052 return;
5053 }
5054 }
5055 }
5056 ovs_rwlock_unlock(&ofproto->groups_rwlock);
5057 }
5058
5059 static enum ofperr
5060 handle_group_mod(struct ofconn *ofconn, const struct ofp_header *oh)
5061 {
5062 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
5063 struct ofputil_group_mod gm;
5064 enum ofperr error;
5065
5066 error = reject_slave_controller(ofconn);
5067 if (error) {
5068 return error;
5069 }
5070
5071 error = ofputil_decode_group_mod(oh, &gm);
5072 if (error) {
5073 return error;
5074 }
5075
5076 switch (gm.command) {
5077 case OFPGC11_ADD:
5078 return add_group(ofproto, &gm);
5079
5080 case OFPGC11_MODIFY:
5081 return modify_group(ofproto, &gm);
5082
5083 case OFPGC11_DELETE:
5084 delete_group(ofproto, gm.group_id);
5085 return 0;
5086
5087 default:
5088 if (gm.command > OFPGC11_DELETE) {
5089 VLOG_WARN_RL(&rl, "%s: Invalid group_mod command type %d",
5090 ofproto->name, gm.command);
5091 }
5092 return OFPERR_OFPGMFC_BAD_COMMAND;
5093 }
5094 }
5095
5096 static enum ofperr
5097 handle_openflow__(struct ofconn *ofconn, const struct ofpbuf *msg)
5098 {
5099 const struct ofp_header *oh = msg->data;
5100 enum ofptype type;
5101 enum ofperr error;
5102
5103 error = ofptype_decode(&type, oh);
5104 if (error) {
5105 return error;
5106 }
5107
5108 switch (type) {
5109 /* OpenFlow requests. */
5110 case OFPTYPE_ECHO_REQUEST:
5111 return handle_echo_request(ofconn, oh);
5112
5113 case OFPTYPE_FEATURES_REQUEST:
5114 return handle_features_request(ofconn, oh);
5115
5116 case OFPTYPE_GET_CONFIG_REQUEST:
5117 return handle_get_config_request(ofconn, oh);
5118
5119 case OFPTYPE_SET_CONFIG:
5120 return handle_set_config(ofconn, oh);
5121
5122 case OFPTYPE_PACKET_OUT:
5123 return handle_packet_out(ofconn, oh);
5124
5125 case OFPTYPE_PORT_MOD:
5126 return handle_port_mod(ofconn, oh);
5127
5128 case OFPTYPE_FLOW_MOD:
5129 return handle_flow_mod(ofconn, oh);
5130
5131 case OFPTYPE_GROUP_MOD:
5132 return handle_group_mod(ofconn, oh);
5133
5134 case OFPTYPE_METER_MOD:
5135 return handle_meter_mod(ofconn, oh);
5136
5137 case OFPTYPE_BARRIER_REQUEST:
5138 return handle_barrier_request(ofconn, oh);
5139
5140 case OFPTYPE_ROLE_REQUEST:
5141 return handle_role_request(ofconn, oh);
5142
5143 /* OpenFlow replies. */
5144 case OFPTYPE_ECHO_REPLY:
5145 return 0;
5146
5147 /* Nicira extension requests. */
5148 case OFPTYPE_FLOW_MOD_TABLE_ID:
5149 return handle_nxt_flow_mod_table_id(ofconn, oh);
5150
5151 case OFPTYPE_SET_FLOW_FORMAT:
5152 return handle_nxt_set_flow_format(ofconn, oh);
5153
5154 case OFPTYPE_SET_PACKET_IN_FORMAT:
5155 return handle_nxt_set_packet_in_format(ofconn, oh);
5156
5157 case OFPTYPE_SET_CONTROLLER_ID:
5158 return handle_nxt_set_controller_id(ofconn, oh);
5159
5160 case OFPTYPE_FLOW_AGE:
5161 /* Nothing to do. */
5162 return 0;
5163
5164 case OFPTYPE_FLOW_MONITOR_CANCEL:
5165 return handle_flow_monitor_cancel(ofconn, oh);
5166
5167 case OFPTYPE_SET_ASYNC_CONFIG:
5168 return handle_nxt_set_async_config(ofconn, oh);
5169
5170 /* Statistics requests. */
5171 case OFPTYPE_DESC_STATS_REQUEST:
5172 return handle_desc_stats_request(ofconn, oh);
5173
5174 case OFPTYPE_FLOW_STATS_REQUEST:
5175 return handle_flow_stats_request(ofconn, oh);
5176
5177 case OFPTYPE_AGGREGATE_STATS_REQUEST:
5178 return handle_aggregate_stats_request(ofconn, oh);
5179
5180 case OFPTYPE_TABLE_STATS_REQUEST:
5181 return handle_table_stats_request(ofconn, oh);
5182
5183 case OFPTYPE_PORT_STATS_REQUEST:
5184 return handle_port_stats_request(ofconn, oh);
5185
5186 case OFPTYPE_QUEUE_STATS_REQUEST:
5187 return handle_queue_stats_request(ofconn, oh);
5188
5189 case OFPTYPE_PORT_DESC_STATS_REQUEST:
5190 return handle_port_desc_stats_request(ofconn, oh);
5191
5192 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
5193 return handle_flow_monitor_request(ofconn, oh);
5194
5195 case OFPTYPE_METER_STATS_REQUEST:
5196 case OFPTYPE_METER_CONFIG_STATS_REQUEST:
5197 return handle_meter_request(ofconn, oh, type);
5198
5199 case OFPTYPE_METER_FEATURES_STATS_REQUEST:
5200 return handle_meter_features_request(ofconn, oh);
5201
5202 case OFPTYPE_GROUP_STATS_REQUEST:
5203 return handle_group_stats_request(ofconn, oh);
5204
5205 case OFPTYPE_GROUP_DESC_STATS_REQUEST:
5206 return handle_group_desc_stats_request(ofconn, oh);
5207
5208 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
5209 return handle_group_features_stats_request(ofconn, oh);
5210
5211 /* FIXME: Change the following once they are implemented: */
5212 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
5213 case OFPTYPE_GET_ASYNC_REQUEST:
5214 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
5215 return OFPERR_OFPBRC_BAD_TYPE;
5216
5217 case OFPTYPE_HELLO:
5218 case OFPTYPE_ERROR:
5219 case OFPTYPE_FEATURES_REPLY:
5220 case OFPTYPE_GET_CONFIG_REPLY:
5221 case OFPTYPE_PACKET_IN:
5222 case OFPTYPE_FLOW_REMOVED:
5223 case OFPTYPE_PORT_STATUS:
5224 case OFPTYPE_BARRIER_REPLY:
5225 case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
5226 case OFPTYPE_DESC_STATS_REPLY:
5227 case OFPTYPE_FLOW_STATS_REPLY:
5228 case OFPTYPE_QUEUE_STATS_REPLY:
5229 case OFPTYPE_PORT_STATS_REPLY:
5230 case OFPTYPE_TABLE_STATS_REPLY:
5231 case OFPTYPE_AGGREGATE_STATS_REPLY:
5232 case OFPTYPE_PORT_DESC_STATS_REPLY:
5233 case OFPTYPE_ROLE_REPLY:
5234 case OFPTYPE_FLOW_MONITOR_PAUSED:
5235 case OFPTYPE_FLOW_MONITOR_RESUMED:
5236 case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
5237 case OFPTYPE_GET_ASYNC_REPLY:
5238 case OFPTYPE_GROUP_STATS_REPLY:
5239 case OFPTYPE_GROUP_DESC_STATS_REPLY:
5240 case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
5241 case OFPTYPE_METER_STATS_REPLY:
5242 case OFPTYPE_METER_CONFIG_STATS_REPLY:
5243 case OFPTYPE_METER_FEATURES_STATS_REPLY:
5244 case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
5245 default:
5246 return OFPERR_OFPBRC_BAD_TYPE;
5247 }
5248 }
5249
5250 static bool
5251 handle_openflow(struct ofconn *ofconn, const struct ofpbuf *ofp_msg)
5252 {
5253 int error = handle_openflow__(ofconn, ofp_msg);
5254 if (error && error != OFPROTO_POSTPONE) {
5255 ofconn_send_error(ofconn, ofp_msg->data, error);
5256 }
5257 COVERAGE_INC(ofproto_recv_openflow);
5258 return error != OFPROTO_POSTPONE;
5259 }
5260 \f
5261 /* Asynchronous operations. */
5262
5263 /* Creates and returns a new ofopgroup that is not associated with any
5264 * OpenFlow connection.
5265 *
5266 * The caller should add operations to the returned group with
5267 * ofoperation_create() and then submit it with ofopgroup_submit(). */
5268 static struct ofopgroup *
5269 ofopgroup_create_unattached(struct ofproto *ofproto)
5270 {
5271 struct ofopgroup *group = xzalloc(sizeof *group);
5272 group->ofproto = ofproto;
5273 list_init(&group->ofproto_node);
5274 list_init(&group->ops);
5275 list_init(&group->ofconn_node);
5276 return group;
5277 }
5278
5279 /* Creates and returns a new ofopgroup for 'ofproto'.
5280 *
5281 * If 'ofconn' is NULL, the new ofopgroup is not associated with any OpenFlow
5282 * connection. The 'request' and 'buffer_id' arguments are ignored.
5283 *
5284 * If 'ofconn' is nonnull, then the new ofopgroup is associated with 'ofconn'.
5285 * If the ofopgroup eventually fails, then the error reply will include
5286 * 'request'. If the ofopgroup eventually succeeds, then the packet with
5287 * buffer id 'buffer_id' on 'ofconn' will be sent by 'ofconn''s ofproto.
5288 *
5289 * The caller should add operations to the returned group with
5290 * ofoperation_create() and then submit it with ofopgroup_submit(). */
5291 static struct ofopgroup *
5292 ofopgroup_create(struct ofproto *ofproto, struct ofconn *ofconn,
5293 const struct ofp_header *request, uint32_t buffer_id)
5294 {
5295 struct ofopgroup *group = ofopgroup_create_unattached(ofproto);
5296 if (ofconn) {
5297 size_t request_len = ntohs(request->length);
5298
5299 ovs_assert(ofconn_get_ofproto(ofconn) == ofproto);
5300
5301 ofconn_add_opgroup(ofconn, &group->ofconn_node);
5302 group->ofconn = ofconn;
5303 group->request = xmemdup(request, MIN(request_len, 64));
5304 group->buffer_id = buffer_id;
5305 }
5306 return group;
5307 }
5308
5309 /* Submits 'group' for processing.
5310 *
5311 * If 'group' contains no operations (e.g. none were ever added, or all of the
5312 * ones that were added completed synchronously), then it is destroyed
5313 * immediately. Otherwise it is added to the ofproto's list of pending
5314 * groups. */
5315 static void
5316 ofopgroup_submit(struct ofopgroup *group)
5317 {
5318 if (!group->n_running) {
5319 ofopgroup_complete(group);
5320 } else {
5321 list_push_back(&group->ofproto->pending, &group->ofproto_node);
5322 group->ofproto->n_pending++;
5323 }
5324 }
5325
5326 static void
5327 ofopgroup_complete(struct ofopgroup *group)
5328 {
5329 struct ofproto *ofproto = group->ofproto;
5330
5331 struct ofconn *abbrev_ofconn;
5332 ovs_be32 abbrev_xid;
5333
5334 struct ofoperation *op, *next_op;
5335 int error;
5336
5337 ovs_assert(!group->n_running);
5338
5339 error = 0;
5340 LIST_FOR_EACH (op, group_node, &group->ops) {
5341 if (op->error) {
5342 error = op->error;
5343 break;
5344 }
5345 }
5346
5347 if (!error && group->ofconn && group->buffer_id != UINT32_MAX) {
5348 LIST_FOR_EACH (op, group_node, &group->ops) {
5349 if (op->type != OFOPERATION_DELETE) {
5350 struct ofpbuf *packet;
5351 ofp_port_t in_port;
5352
5353 error = ofconn_pktbuf_retrieve(group->ofconn, group->buffer_id,
5354 &packet, &in_port);
5355 if (packet) {
5356 ovs_assert(!error);
5357 error = rule_execute(op->rule, in_port, packet);
5358 }
5359 break;
5360 }
5361 }
5362 }
5363
5364 if (!error && !list_is_empty(&group->ofconn_node)) {
5365 abbrev_ofconn = group->ofconn;
5366 abbrev_xid = group->request->xid;
5367 } else {
5368 abbrev_ofconn = NULL;
5369 abbrev_xid = htonl(0);
5370 }
5371 LIST_FOR_EACH_SAFE (op, next_op, group_node, &group->ops) {
5372 struct rule *rule = op->rule;
5373
5374 /* We generally want to report the change to active OpenFlow flow
5375 monitors (e.g. NXST_FLOW_MONITOR). There are three exceptions:
5376
5377 - The operation failed.
5378
5379 - The affected rule is not visible to controllers.
5380
5381 - The operation's only effect was to update rule->modified. */
5382 if (!(op->error
5383 || ofproto_rule_is_hidden(rule)
5384 || (op->type == OFOPERATION_MODIFY
5385 && op->ofpacts
5386 && rule->flow_cookie == op->flow_cookie))) {
5387 /* Check that we can just cast from ofoperation_type to
5388 * nx_flow_update_event. */
5389 enum nx_flow_update_event event_type;
5390
5391 switch (op->type) {
5392 case OFOPERATION_ADD:
5393 case OFOPERATION_REPLACE:
5394 event_type = NXFME_ADDED;
5395 break;
5396
5397 case OFOPERATION_DELETE:
5398 event_type = NXFME_DELETED;
5399 break;
5400
5401 case OFOPERATION_MODIFY:
5402 event_type = NXFME_MODIFIED;
5403 break;
5404
5405 default:
5406 NOT_REACHED();
5407 }
5408
5409 ofmonitor_report(ofproto->connmgr, rule, event_type,
5410 op->reason, abbrev_ofconn, abbrev_xid);
5411 }
5412
5413 rule->pending = NULL;
5414
5415 switch (op->type) {
5416 case OFOPERATION_ADD:
5417 if (!op->error) {
5418 uint16_t vid_mask;
5419
5420 vid_mask = minimask_get_vid_mask(&rule->cr.match.mask);
5421 if (vid_mask == VLAN_VID_MASK) {
5422 if (ofproto->vlan_bitmap) {
5423 uint16_t vid = miniflow_get_vid(&rule->cr.match.flow);
5424 if (!bitmap_is_set(ofproto->vlan_bitmap, vid)) {
5425 bitmap_set1(ofproto->vlan_bitmap, vid);
5426 ofproto->vlans_changed = true;
5427 }
5428 } else {
5429 ofproto->vlans_changed = true;
5430 }
5431 }
5432 } else {
5433 ovs_rwlock_wrlock(&rule->evict);
5434 oftable_remove_rule(rule);
5435 ofproto_rule_destroy__(rule);
5436 }
5437 break;
5438
5439 case OFOPERATION_DELETE:
5440 ovs_assert(!op->error);
5441 ofproto_rule_destroy__(rule);
5442 op->rule = NULL;
5443 break;
5444
5445 case OFOPERATION_MODIFY:
5446 case OFOPERATION_REPLACE:
5447 if (!op->error) {
5448 long long int now = time_msec();
5449
5450 rule->modified = now;
5451 if (op->type == OFOPERATION_REPLACE) {
5452 rule->created = rule->used = now;
5453 }
5454 } else {
5455 ofproto_rule_change_cookie(ofproto, rule, op->flow_cookie);
5456 ovs_mutex_lock(&rule->timeout_mutex);
5457 rule->idle_timeout = op->idle_timeout;
5458 rule->hard_timeout = op->hard_timeout;
5459 ovs_mutex_unlock(&rule->timeout_mutex);
5460 if (op->ofpacts) {
5461 free(rule->ofpacts);
5462 rule->ofpacts = op->ofpacts;
5463 rule->ofpacts_len = op->ofpacts_len;
5464 op->ofpacts = NULL;
5465 op->ofpacts_len = 0;
5466 }
5467 rule->send_flow_removed = op->send_flow_removed;
5468 }
5469 break;
5470
5471 default:
5472 NOT_REACHED();
5473 }
5474
5475 ofoperation_destroy(op);
5476 }
5477
5478 ofmonitor_flush(ofproto->connmgr);
5479
5480 if (!list_is_empty(&group->ofproto_node)) {
5481 ovs_assert(ofproto->n_pending > 0);
5482 ofproto->n_pending--;
5483 list_remove(&group->ofproto_node);
5484 }
5485 if (!list_is_empty(&group->ofconn_node)) {
5486 list_remove(&group->ofconn_node);
5487 if (error) {
5488 ofconn_send_error(group->ofconn, group->request, error);
5489 }
5490 connmgr_retry(ofproto->connmgr);
5491 }
5492 free(group->request);
5493 free(group);
5494 }
5495
5496 /* Initiates a new operation on 'rule', of the specified 'type', within
5497 * 'group'. Prior to calling, 'rule' must not have any pending operation.
5498 *
5499 * For a 'type' of OFOPERATION_DELETE, 'reason' should specify the reason that
5500 * the flow is being deleted. For other 'type's, 'reason' is ignored (use 0).
5501 *
5502 * Returns the newly created ofoperation (which is also available as
5503 * rule->pending). */
5504 static struct ofoperation *
5505 ofoperation_create(struct ofopgroup *group, struct rule *rule,
5506 enum ofoperation_type type,
5507 enum ofp_flow_removed_reason reason)
5508 {
5509 struct ofproto *ofproto = group->ofproto;
5510 struct ofoperation *op;
5511
5512 ovs_assert(!rule->pending);
5513
5514 op = rule->pending = xzalloc(sizeof *op);
5515 op->group = group;
5516 list_push_back(&group->ops, &op->group_node);
5517 op->rule = rule;
5518 op->type = type;
5519 op->reason = reason;
5520 op->flow_cookie = rule->flow_cookie;
5521 ovs_mutex_lock(&rule->timeout_mutex);
5522 op->idle_timeout = rule->idle_timeout;
5523 op->hard_timeout = rule->hard_timeout;
5524 ovs_mutex_unlock(&rule->timeout_mutex);
5525 op->send_flow_removed = rule->send_flow_removed;
5526
5527 group->n_running++;
5528
5529 if (type == OFOPERATION_DELETE) {
5530 hmap_insert(&ofproto->deletions, &op->hmap_node,
5531 cls_rule_hash(&rule->cr, rule->table_id));
5532 }
5533
5534 return op;
5535 }
5536
5537 static void
5538 ofoperation_destroy(struct ofoperation *op)
5539 {
5540 struct ofopgroup *group = op->group;
5541
5542 if (op->rule) {
5543 op->rule->pending = NULL;
5544 }
5545 if (op->type == OFOPERATION_DELETE) {
5546 hmap_remove(&group->ofproto->deletions, &op->hmap_node);
5547 }
5548 list_remove(&op->group_node);
5549 free(op->ofpacts);
5550 free(op);
5551 }
5552
5553 /* Indicates that 'op' completed with status 'error', which is either 0 to
5554 * indicate success or an OpenFlow error code on failure.
5555 *
5556 * If 'error' is 0, indicating success, the operation will be committed
5557 * permanently to the flow table.
5558 *
5559 * If 'error' is nonzero, then generally the operation will be rolled back:
5560 *
5561 * - If 'op' is an "add flow" operation, ofproto removes the new rule or
5562 * restores the original rule. The caller must have uninitialized any
5563 * derived state in the new rule, as in step 5 of in the "Life Cycle" in
5564 * ofproto/ofproto-provider.h. ofoperation_complete() performs steps 6 and
5565 * and 7 for the new rule, calling its ->rule_dealloc() function.
5566 *
5567 * - If 'op' is a "modify flow" operation, ofproto restores the original
5568 * actions.
5569 *
5570 * - 'op' must not be a "delete flow" operation. Removing a rule is not
5571 * allowed to fail. It must always succeed.
5572 *
5573 * Please see the large comment in ofproto/ofproto-provider.h titled
5574 * "Asynchronous Operation Support" for more information. */
5575 void
5576 ofoperation_complete(struct ofoperation *op, enum ofperr error)
5577 {
5578 struct ofopgroup *group = op->group;
5579
5580 ovs_assert(op->rule->pending == op);
5581 ovs_assert(group->n_running > 0);
5582 ovs_assert(!error || op->type != OFOPERATION_DELETE);
5583
5584 op->error = error;
5585 if (!--group->n_running && !list_is_empty(&group->ofproto_node)) {
5586 ofopgroup_complete(group);
5587 }
5588 }
5589 \f
5590 static uint64_t
5591 pick_datapath_id(const struct ofproto *ofproto)
5592 {
5593 const struct ofport *port;
5594
5595 port = ofproto_get_port(ofproto, OFPP_LOCAL);
5596 if (port) {
5597 uint8_t ea[ETH_ADDR_LEN];
5598 int error;
5599
5600 error = netdev_get_etheraddr(port->netdev, ea);
5601 if (!error) {
5602 return eth_addr_to_uint64(ea);
5603 }
5604 VLOG_WARN("%s: could not get MAC address for %s (%s)",
5605 ofproto->name, netdev_get_name(port->netdev),
5606 ovs_strerror(error));
5607 }
5608 return ofproto->fallback_dpid;
5609 }
5610
5611 static uint64_t
5612 pick_fallback_dpid(void)
5613 {
5614 uint8_t ea[ETH_ADDR_LEN];
5615 eth_addr_nicira_random(ea);
5616 return eth_addr_to_uint64(ea);
5617 }
5618 \f
5619 /* Table overflow policy. */
5620
5621 /* Chooses and updates 'rulep' with a rule to evict from 'table'. Sets 'rulep'
5622 * to NULL if the table is not configured to evict rules or if the table
5623 * contains no evictable rules. (Rules with a readlock on their evict rwlock,
5624 * or with no timeouts are not evictable.) */
5625 static bool
5626 choose_rule_to_evict(struct oftable *table, struct rule **rulep)
5627 {
5628 struct eviction_group *evg;
5629
5630 *rulep = NULL;
5631 if (!table->eviction_fields) {
5632 return false;
5633 }
5634
5635 /* In the common case, the outer and inner loops here will each be entered
5636 * exactly once:
5637 *
5638 * - The inner loop normally "return"s in its first iteration. If the
5639 * eviction group has any evictable rules, then it always returns in
5640 * some iteration.
5641 *
5642 * - The outer loop only iterates more than once if the largest eviction
5643 * group has no evictable rules.
5644 *
5645 * - The outer loop can exit only if table's 'max_flows' is all filled up
5646 * by unevictable rules. */
5647 HEAP_FOR_EACH (evg, size_node, &table->eviction_groups_by_size) {
5648 struct rule *rule;
5649
5650 HEAP_FOR_EACH (rule, evg_node, &evg->rules) {
5651 if (!ovs_rwlock_trywrlock(&rule->evict)) {
5652 *rulep = rule;
5653 return true;
5654 }
5655 }
5656 }
5657
5658 return false;
5659 }
5660
5661 /* Searches 'ofproto' for tables that have more flows than their configured
5662 * maximum and that have flow eviction enabled, and evicts as many flows as
5663 * necessary and currently feasible from them.
5664 *
5665 * This triggers only when an OpenFlow table has N flows in it and then the
5666 * client configures a maximum number of flows less than N. */
5667 static void
5668 ofproto_evict(struct ofproto *ofproto)
5669 {
5670 struct ofopgroup *group;
5671 struct oftable *table;
5672
5673 group = ofopgroup_create_unattached(ofproto);
5674 OFPROTO_FOR_EACH_TABLE (table, ofproto) {
5675 while (table->eviction_fields) {
5676 struct rule *rule;
5677 size_t n_rules;
5678
5679 ovs_rwlock_rdlock(&table->cls.rwlock);
5680 n_rules = classifier_count(&table->cls);
5681 ovs_rwlock_unlock(&table->cls.rwlock);
5682
5683 if (n_rules <= table->max_flows) {
5684 break;
5685 }
5686
5687 if (!choose_rule_to_evict(table, &rule)) {
5688 break;
5689 }
5690
5691 if (rule->pending) {
5692 ovs_rwlock_unlock(&rule->evict);
5693 break;
5694 }
5695
5696 ofoperation_create(group, rule,
5697 OFOPERATION_DELETE, OFPRR_EVICTION);
5698 oftable_remove_rule(rule);
5699 ofproto->ofproto_class->rule_delete(rule);
5700 }
5701 }
5702 ofopgroup_submit(group);
5703 }
5704 \f
5705 /* Eviction groups. */
5706
5707 /* Returns the priority to use for an eviction_group that contains 'n_rules'
5708 * rules. The priority contains low-order random bits to ensure that eviction
5709 * groups with the same number of rules are prioritized randomly. */
5710 static uint32_t
5711 eviction_group_priority(size_t n_rules)
5712 {
5713 uint16_t size = MIN(UINT16_MAX, n_rules);
5714 return (size << 16) | random_uint16();
5715 }
5716
5717 /* Updates 'evg', an eviction_group within 'table', following a change that
5718 * adds or removes rules in 'evg'. */
5719 static void
5720 eviction_group_resized(struct oftable *table, struct eviction_group *evg)
5721 {
5722 heap_change(&table->eviction_groups_by_size, &evg->size_node,
5723 eviction_group_priority(heap_count(&evg->rules)));
5724 }
5725
5726 /* Destroys 'evg', an eviction_group within 'table':
5727 *
5728 * - Removes all the rules, if any, from 'evg'. (It doesn't destroy the
5729 * rules themselves, just removes them from the eviction group.)
5730 *
5731 * - Removes 'evg' from 'table'.
5732 *
5733 * - Frees 'evg'. */
5734 static void
5735 eviction_group_destroy(struct oftable *table, struct eviction_group *evg)
5736 {
5737 while (!heap_is_empty(&evg->rules)) {
5738 struct rule *rule;
5739
5740 rule = CONTAINER_OF(heap_pop(&evg->rules), struct rule, evg_node);
5741 rule->eviction_group = NULL;
5742 }
5743 hmap_remove(&table->eviction_groups_by_id, &evg->id_node);
5744 heap_remove(&table->eviction_groups_by_size, &evg->size_node);
5745 heap_destroy(&evg->rules);
5746 free(evg);
5747 }
5748
5749 /* Removes 'rule' from its eviction group, if any. */
5750 static void
5751 eviction_group_remove_rule(struct rule *rule)
5752 {
5753 if (rule->eviction_group) {
5754 struct oftable *table = &rule->ofproto->tables[rule->table_id];
5755 struct eviction_group *evg = rule->eviction_group;
5756
5757 rule->eviction_group = NULL;
5758 heap_remove(&evg->rules, &rule->evg_node);
5759 if (heap_is_empty(&evg->rules)) {
5760 eviction_group_destroy(table, evg);
5761 } else {
5762 eviction_group_resized(table, evg);
5763 }
5764 }
5765 }
5766
5767 /* Hashes the 'rule''s values for the eviction_fields of 'rule''s table, and
5768 * returns the hash value. */
5769 static uint32_t
5770 eviction_group_hash_rule(struct rule *rule)
5771 {
5772 struct oftable *table = &rule->ofproto->tables[rule->table_id];
5773 const struct mf_subfield *sf;
5774 struct flow flow;
5775 uint32_t hash;
5776
5777 hash = table->eviction_group_id_basis;
5778 miniflow_expand(&rule->cr.match.flow, &flow);
5779 for (sf = table->eviction_fields;
5780 sf < &table->eviction_fields[table->n_eviction_fields];
5781 sf++)
5782 {
5783 if (mf_are_prereqs_ok(sf->field, &flow)) {
5784 union mf_value value;
5785
5786 mf_get_value(sf->field, &flow, &value);
5787 if (sf->ofs) {
5788 bitwise_zero(&value, sf->field->n_bytes, 0, sf->ofs);
5789 }
5790 if (sf->ofs + sf->n_bits < sf->field->n_bytes * 8) {
5791 unsigned int start = sf->ofs + sf->n_bits;
5792 bitwise_zero(&value, sf->field->n_bytes, start,
5793 sf->field->n_bytes * 8 - start);
5794 }
5795 hash = hash_bytes(&value, sf->field->n_bytes, hash);
5796 } else {
5797 hash = hash_int(hash, 0);
5798 }
5799 }
5800
5801 return hash;
5802 }
5803
5804 /* Returns an eviction group within 'table' with the given 'id', creating one
5805 * if necessary. */
5806 static struct eviction_group *
5807 eviction_group_find(struct oftable *table, uint32_t id)
5808 {
5809 struct eviction_group *evg;
5810
5811 HMAP_FOR_EACH_WITH_HASH (evg, id_node, id, &table->eviction_groups_by_id) {
5812 return evg;
5813 }
5814
5815 evg = xmalloc(sizeof *evg);
5816 hmap_insert(&table->eviction_groups_by_id, &evg->id_node, id);
5817 heap_insert(&table->eviction_groups_by_size, &evg->size_node,
5818 eviction_group_priority(0));
5819 heap_init(&evg->rules);
5820
5821 return evg;
5822 }
5823
5824 /* Returns an eviction priority for 'rule'. The return value should be
5825 * interpreted so that higher priorities make a rule more attractive candidates
5826 * for eviction. */
5827 static uint32_t
5828 rule_eviction_priority(struct rule *rule)
5829 {
5830 long long int hard_expiration;
5831 long long int idle_expiration;
5832 long long int expiration;
5833 uint32_t expiration_offset;
5834
5835 /* Calculate time of expiration. */
5836 ovs_mutex_lock(&rule->timeout_mutex);
5837 hard_expiration = (rule->hard_timeout
5838 ? rule->modified + rule->hard_timeout * 1000
5839 : LLONG_MAX);
5840 idle_expiration = (rule->idle_timeout
5841 ? rule->used + rule->idle_timeout * 1000
5842 : LLONG_MAX);
5843 expiration = MIN(hard_expiration, idle_expiration);
5844 ovs_mutex_unlock(&rule->timeout_mutex);
5845 if (expiration == LLONG_MAX) {
5846 return 0;
5847 }
5848
5849 /* Calculate the time of expiration as a number of (approximate) seconds
5850 * after program startup.
5851 *
5852 * This should work OK for program runs that last UINT32_MAX seconds or
5853 * less. Therefore, please restart OVS at least once every 136 years. */
5854 expiration_offset = (expiration >> 10) - (time_boot_msec() >> 10);
5855
5856 /* Invert the expiration offset because we're using a max-heap. */
5857 return UINT32_MAX - expiration_offset;
5858 }
5859
5860 /* Adds 'rule' to an appropriate eviction group for its oftable's
5861 * configuration. Does nothing if 'rule''s oftable doesn't have eviction
5862 * enabled, or if 'rule' is a permanent rule (one that will never expire on its
5863 * own).
5864 *
5865 * The caller must ensure that 'rule' is not already in an eviction group. */
5866 static void
5867 eviction_group_add_rule(struct rule *rule)
5868 {
5869 struct ofproto *ofproto = rule->ofproto;
5870 struct oftable *table = &ofproto->tables[rule->table_id];
5871 bool has_timeout;
5872
5873 ovs_mutex_lock(&rule->timeout_mutex);
5874 has_timeout = rule->hard_timeout || rule->idle_timeout;
5875 ovs_mutex_unlock(&rule->timeout_mutex);
5876
5877 if (table->eviction_fields && has_timeout) {
5878 struct eviction_group *evg;
5879
5880 evg = eviction_group_find(table, eviction_group_hash_rule(rule));
5881
5882 rule->eviction_group = evg;
5883 heap_insert(&evg->rules, &rule->evg_node,
5884 rule_eviction_priority(rule));
5885 eviction_group_resized(table, evg);
5886 }
5887 }
5888 \f
5889 /* oftables. */
5890
5891 /* Initializes 'table'. */
5892 static void
5893 oftable_init(struct oftable *table)
5894 {
5895 memset(table, 0, sizeof *table);
5896 classifier_init(&table->cls);
5897 table->max_flows = UINT_MAX;
5898 }
5899
5900 /* Destroys 'table', including its classifier and eviction groups.
5901 *
5902 * The caller is responsible for freeing 'table' itself. */
5903 static void
5904 oftable_destroy(struct oftable *table)
5905 {
5906 ovs_rwlock_rdlock(&table->cls.rwlock);
5907 ovs_assert(classifier_is_empty(&table->cls));
5908 ovs_rwlock_unlock(&table->cls.rwlock);
5909 oftable_disable_eviction(table);
5910 classifier_destroy(&table->cls);
5911 free(table->name);
5912 }
5913
5914 /* Changes the name of 'table' to 'name'. If 'name' is NULL or the empty
5915 * string, then 'table' will use its default name.
5916 *
5917 * This only affects the name exposed for a table exposed through the OpenFlow
5918 * OFPST_TABLE (as printed by "ovs-ofctl dump-tables"). */
5919 static void
5920 oftable_set_name(struct oftable *table, const char *name)
5921 {
5922 if (name && name[0]) {
5923 int len = strnlen(name, OFP_MAX_TABLE_NAME_LEN);
5924 if (!table->name || strncmp(name, table->name, len)) {
5925 free(table->name);
5926 table->name = xmemdup0(name, len);
5927 }
5928 } else {
5929 free(table->name);
5930 table->name = NULL;
5931 }
5932 }
5933
5934 /* oftables support a choice of two policies when adding a rule would cause the
5935 * number of flows in the table to exceed the configured maximum number: either
5936 * they can refuse to add the new flow or they can evict some existing flow.
5937 * This function configures the former policy on 'table'. */
5938 static void
5939 oftable_disable_eviction(struct oftable *table)
5940 {
5941 if (table->eviction_fields) {
5942 struct eviction_group *evg, *next;
5943
5944 HMAP_FOR_EACH_SAFE (evg, next, id_node,
5945 &table->eviction_groups_by_id) {
5946 eviction_group_destroy(table, evg);
5947 }
5948 hmap_destroy(&table->eviction_groups_by_id);
5949 heap_destroy(&table->eviction_groups_by_size);
5950
5951 free(table->eviction_fields);
5952 table->eviction_fields = NULL;
5953 table->n_eviction_fields = 0;
5954 }
5955 }
5956
5957 /* oftables support a choice of two policies when adding a rule would cause the
5958 * number of flows in the table to exceed the configured maximum number: either
5959 * they can refuse to add the new flow or they can evict some existing flow.
5960 * This function configures the latter policy on 'table', with fairness based
5961 * on the values of the 'n_fields' fields specified in 'fields'. (Specifying
5962 * 'n_fields' as 0 disables fairness.) */
5963 static void
5964 oftable_enable_eviction(struct oftable *table,
5965 const struct mf_subfield *fields, size_t n_fields)
5966 {
5967 struct cls_cursor cursor;
5968 struct rule *rule;
5969
5970 if (table->eviction_fields
5971 && n_fields == table->n_eviction_fields
5972 && (!n_fields
5973 || !memcmp(fields, table->eviction_fields,
5974 n_fields * sizeof *fields))) {
5975 /* No change. */
5976 return;
5977 }
5978
5979 oftable_disable_eviction(table);
5980
5981 table->n_eviction_fields = n_fields;
5982 table->eviction_fields = xmemdup(fields, n_fields * sizeof *fields);
5983
5984 table->eviction_group_id_basis = random_uint32();
5985 hmap_init(&table->eviction_groups_by_id);
5986 heap_init(&table->eviction_groups_by_size);
5987
5988 ovs_rwlock_rdlock(&table->cls.rwlock);
5989 cls_cursor_init(&cursor, &table->cls, NULL);
5990 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
5991 eviction_group_add_rule(rule);
5992 }
5993 ovs_rwlock_unlock(&table->cls.rwlock);
5994 }
5995
5996 /* Removes 'rule' from the oftable that contains it. */
5997 static void
5998 oftable_remove_rule__(struct ofproto *ofproto, struct classifier *cls,
5999 struct rule *rule)
6000 OVS_REQ_WRLOCK(cls->rwlock) OVS_RELEASES(rule->evict)
6001 {
6002 classifier_remove(cls, &rule->cr);
6003 cookies_remove(ofproto, rule);
6004 eviction_group_remove_rule(rule);
6005 ovs_mutex_lock(&ofproto->expirable_mutex);
6006 if (!list_is_empty(&rule->expirable)) {
6007 list_remove(&rule->expirable);
6008 }
6009 ovs_mutex_unlock(&ofproto->expirable_mutex);
6010 if (!list_is_empty(&rule->meter_list_node)) {
6011 list_remove(&rule->meter_list_node);
6012 list_init(&rule->meter_list_node);
6013 }
6014 ovs_rwlock_unlock(&rule->evict);
6015 }
6016
6017 static void
6018 oftable_remove_rule(struct rule *rule)
6019 {
6020 struct ofproto *ofproto = rule->ofproto;
6021 struct oftable *table = &ofproto->tables[rule->table_id];
6022
6023 ovs_rwlock_wrlock(&table->cls.rwlock);
6024 oftable_remove_rule__(ofproto, &table->cls, rule);
6025 ovs_rwlock_unlock(&table->cls.rwlock);
6026 }
6027
6028 /* Inserts 'rule' into its oftable, which must not already contain any rule for
6029 * the same cls_rule. */
6030 static void
6031 oftable_insert_rule(struct rule *rule)
6032 {
6033 struct ofproto *ofproto = rule->ofproto;
6034 struct oftable *table = &ofproto->tables[rule->table_id];
6035 bool may_expire;
6036
6037 ovs_mutex_lock(&rule->timeout_mutex);
6038 may_expire = rule->hard_timeout || rule->idle_timeout;
6039 ovs_mutex_unlock(&rule->timeout_mutex);
6040
6041 if (may_expire) {
6042 ovs_mutex_lock(&ofproto->expirable_mutex);
6043 list_insert(&ofproto->expirable, &rule->expirable);
6044 ovs_mutex_unlock(&ofproto->expirable_mutex);
6045 }
6046 cookies_insert(ofproto, rule);
6047 if (rule->meter_id) {
6048 struct meter *meter = ofproto->meters[rule->meter_id];
6049 list_insert(&meter->rules, &rule->meter_list_node);
6050 }
6051 ovs_rwlock_wrlock(&table->cls.rwlock);
6052 classifier_insert(&table->cls, &rule->cr);
6053 ovs_rwlock_unlock(&table->cls.rwlock);
6054 eviction_group_add_rule(rule);
6055 }
6056 \f
6057 /* unixctl commands. */
6058
6059 struct ofproto *
6060 ofproto_lookup(const char *name)
6061 {
6062 struct ofproto *ofproto;
6063
6064 HMAP_FOR_EACH_WITH_HASH (ofproto, hmap_node, hash_string(name, 0),
6065 &all_ofprotos) {
6066 if (!strcmp(ofproto->name, name)) {
6067 return ofproto;
6068 }
6069 }
6070 return NULL;
6071 }
6072
6073 static void
6074 ofproto_unixctl_list(struct unixctl_conn *conn, int argc OVS_UNUSED,
6075 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
6076 {
6077 struct ofproto *ofproto;
6078 struct ds results;
6079
6080 ds_init(&results);
6081 HMAP_FOR_EACH (ofproto, hmap_node, &all_ofprotos) {
6082 ds_put_format(&results, "%s\n", ofproto->name);
6083 }
6084 unixctl_command_reply(conn, ds_cstr(&results));
6085 ds_destroy(&results);
6086 }
6087
6088 static void
6089 ofproto_unixctl_init(void)
6090 {
6091 static bool registered;
6092 if (registered) {
6093 return;
6094 }
6095 registered = true;
6096
6097 unixctl_command_register("ofproto/list", "", 0, 0,
6098 ofproto_unixctl_list, NULL);
6099 }
6100 \f
6101 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
6102 *
6103 * This is deprecated. It is only for compatibility with broken device drivers
6104 * in old versions of Linux that do not properly support VLANs when VLAN
6105 * devices are not used. When broken device drivers are no longer in
6106 * widespread use, we will delete these interfaces. */
6107
6108 /* Sets a 1-bit in the 4096-bit 'vlan_bitmap' for each VLAN ID that is matched
6109 * (exactly) by an OpenFlow rule in 'ofproto'. */
6110 void
6111 ofproto_get_vlan_usage(struct ofproto *ofproto, unsigned long int *vlan_bitmap)
6112 {
6113 const struct oftable *oftable;
6114
6115 free(ofproto->vlan_bitmap);
6116 ofproto->vlan_bitmap = bitmap_allocate(4096);
6117 ofproto->vlans_changed = false;
6118
6119 OFPROTO_FOR_EACH_TABLE (oftable, ofproto) {
6120 const struct cls_table *table;
6121
6122 HMAP_FOR_EACH (table, hmap_node, &oftable->cls.tables) {
6123 if (minimask_get_vid_mask(&table->mask) == VLAN_VID_MASK) {
6124 const struct cls_rule *rule;
6125
6126 HMAP_FOR_EACH (rule, hmap_node, &table->rules) {
6127 uint16_t vid = miniflow_get_vid(&rule->match.flow);
6128 bitmap_set1(vlan_bitmap, vid);
6129 bitmap_set1(ofproto->vlan_bitmap, vid);
6130 }
6131 }
6132 }
6133 }
6134 }
6135
6136 /* Returns true if new VLANs have come into use by the flow table since the
6137 * last call to ofproto_get_vlan_usage().
6138 *
6139 * We don't track when old VLANs stop being used. */
6140 bool
6141 ofproto_has_vlan_usage_changed(const struct ofproto *ofproto)
6142 {
6143 return ofproto->vlans_changed;
6144 }
6145
6146 /* Configures a VLAN splinter binding between the ports identified by OpenFlow
6147 * port numbers 'vlandev_ofp_port' and 'realdev_ofp_port'. If
6148 * 'realdev_ofp_port' is nonzero, then the VLAN device is enslaved to the real
6149 * device as a VLAN splinter for VLAN ID 'vid'. If 'realdev_ofp_port' is zero,
6150 * then the VLAN device is un-enslaved. */
6151 int
6152 ofproto_port_set_realdev(struct ofproto *ofproto, ofp_port_t vlandev_ofp_port,
6153 ofp_port_t realdev_ofp_port, int vid)
6154 {
6155 struct ofport *ofport;
6156 int error;
6157
6158 ovs_assert(vlandev_ofp_port != realdev_ofp_port);
6159
6160 ofport = ofproto_get_port(ofproto, vlandev_ofp_port);
6161 if (!ofport) {
6162 VLOG_WARN("%s: cannot set realdev on nonexistent port %"PRIu16,
6163 ofproto->name, vlandev_ofp_port);
6164 return EINVAL;
6165 }
6166
6167 if (!ofproto->ofproto_class->set_realdev) {
6168 if (!vlandev_ofp_port) {
6169 return 0;
6170 }
6171 VLOG_WARN("%s: vlan splinters not supported", ofproto->name);
6172 return EOPNOTSUPP;
6173 }
6174
6175 error = ofproto->ofproto_class->set_realdev(ofport, realdev_ofp_port, vid);
6176 if (error) {
6177 VLOG_WARN("%s: setting realdev on port %"PRIu16" (%s) failed (%s)",
6178 ofproto->name, vlandev_ofp_port,
6179 netdev_get_name(ofport->netdev), ovs_strerror(error));
6180 }
6181 return error;
6182 }