2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
3 * Copyright (c) 2010 Jean Tourrilhes - HP-Labs.
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 #include "byte-order.h"
27 #include "classifier.h"
30 #include "dynamic-string.h"
33 #include "meta-flow.h"
36 #include "ofp-actions.h"
37 #include "ofp-errors.h"
39 #include "ofp-print.h"
42 #include "ofproto-provider.h"
43 #include "openflow/nicira-ext.h"
44 #include "openflow/openflow.h"
48 #include "poll-loop.h"
54 #include "unaligned.h"
58 VLOG_DEFINE_THIS_MODULE(ofproto
);
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
);
71 S_OPENFLOW
, /* Processing OpenFlow commands. */
72 S_EVICT
, /* Evicting flows from over-limit tables. */
73 S_FLUSH
, /* Deleting all flow table rules. */
76 enum ofoperation_type
{
82 /* A single OpenFlow request can execute any number of operations. The
83 * ofopgroup maintain OpenFlow state common to all of the operations, e.g. the
84 * ofconn to which an error reply should be sent if necessary.
86 * ofproto initiates some operations internally. These operations are still
87 * assigned to groups but will not have an associated ofconn. */
89 struct ofproto
*ofproto
; /* Owning ofproto. */
90 struct list ofproto_node
; /* In ofproto's "pending" list. */
91 struct list ops
; /* List of "struct ofoperation"s. */
92 int n_running
; /* Number of ops still pending. */
94 /* Data needed to send OpenFlow reply on failure or to send a buffered
97 * If list_is_empty(ofconn_node) then this ofopgroup never had an
98 * associated ofconn or its ofconn's connection dropped after it initiated
99 * the operation. In the latter case 'ofconn' is a wild pointer that
100 * refers to freed memory, so the 'ofconn' member must be used only if
101 * !list_is_empty(ofconn_node).
103 struct list ofconn_node
; /* In ofconn's list of pending opgroups. */
104 struct ofconn
*ofconn
; /* ofconn for reply (but see note above). */
105 struct ofp_header
*request
; /* Original request (truncated at 64 bytes). */
106 uint32_t buffer_id
; /* Buffer id from original request. */
109 static struct ofopgroup
*ofopgroup_create_unattached(struct ofproto
*);
110 static struct ofopgroup
*ofopgroup_create(struct ofproto
*, struct ofconn
*,
111 const struct ofp_header
*,
113 static void ofopgroup_submit(struct ofopgroup
*);
114 static void ofopgroup_complete(struct ofopgroup
*);
116 /* A single flow table operation. */
118 struct ofopgroup
*group
; /* Owning group. */
119 struct list group_node
; /* In ofopgroup's "ops" list. */
120 struct hmap_node hmap_node
; /* In ofproto's "deletions" hmap. */
121 struct rule
*rule
; /* Rule being operated upon. */
122 enum ofoperation_type type
; /* Type of operation. */
124 /* OFOPERATION_ADD. */
125 struct rule
*victim
; /* Rule being replaced, if any.. */
127 /* OFOPERATION_MODIFY: The old actions, if the actions are changing. */
128 struct ofpact
*ofpacts
;
132 /* OFOPERATION_DELETE. */
133 enum ofp_flow_removed_reason reason
; /* Reason flow was removed. */
135 ovs_be64 flow_cookie
; /* Rule's old flow cookie. */
136 enum ofperr error
; /* 0 if no error. */
139 static struct ofoperation
*ofoperation_create(struct ofopgroup
*,
141 enum ofoperation_type
,
142 enum ofp_flow_removed_reason
);
143 static void ofoperation_destroy(struct ofoperation
*);
146 static void oftable_init(struct oftable
*);
147 static void oftable_destroy(struct oftable
*);
149 static void oftable_set_name(struct oftable
*, const char *name
);
151 static void oftable_disable_eviction(struct oftable
*);
152 static void oftable_enable_eviction(struct oftable
*,
153 const struct mf_subfield
*fields
,
156 static void oftable_remove_rule(struct rule
*rule
) OVS_RELEASES(rule
->evict
);
157 static void oftable_remove_rule__(struct ofproto
*ofproto
,
158 struct classifier
*cls
, struct rule
*rule
)
159 OVS_REQ_WRLOCK(cls
->rwlock
) OVS_RELEASES(rule
->evict
);
160 static struct rule
*oftable_replace_rule(struct rule
*);
161 static void oftable_substitute_rule(struct rule
*old
, struct rule
*new);
163 /* A set of rules within a single OpenFlow table (oftable) that have the same
164 * values for the oftable's eviction_fields. A rule to be evicted, when one is
165 * needed, is taken from the eviction group that contains the greatest number
168 * An oftable owns any number of eviction groups, each of which contains any
171 * Membership in an eviction group is imprecise, based on the hash of the
172 * oftable's eviction_fields (in the eviction_group's id_node.hash member).
173 * That is, if two rules have different eviction_fields, but those
174 * eviction_fields hash to the same value, then they will belong to the same
175 * eviction_group anyway.
177 * (When eviction is not enabled on an oftable, we don't track any eviction
178 * groups, to save time and space.) */
179 struct eviction_group
{
180 struct hmap_node id_node
; /* In oftable's "eviction_groups_by_id". */
181 struct heap_node size_node
; /* In oftable's "eviction_groups_by_size". */
182 struct heap rules
; /* Contains "struct rule"s. */
185 static bool choose_rule_to_evict(struct oftable
*table
, struct rule
**rulep
)
186 OVS_TRY_WRLOCK(true, (*rulep
)->evict
);
187 static void ofproto_evict(struct ofproto
*);
188 static uint32_t rule_eviction_priority(struct rule
*);
189 static void eviction_group_add_rule(struct rule
*rule
);
192 static void ofport_destroy__(struct ofport
*);
193 static void ofport_destroy(struct ofport
*);
195 static void update_port(struct ofproto
*, const char *devname
);
196 static int init_ports(struct ofproto
*);
197 static void reinit_ports(struct ofproto
*);
200 static void ofproto_rule_destroy__(struct rule
*);
201 static void ofproto_rule_send_removed(struct rule
*, uint8_t reason
);
202 static bool rule_is_modifiable(const struct rule
*);
205 static enum ofperr
add_flow(struct ofproto
*, struct ofconn
*,
206 struct ofputil_flow_mod
*,
207 const struct ofp_header
*);
208 static void delete_flow__(struct rule
*rule
, struct ofopgroup
*,
209 enum ofp_flow_removed_reason
)
210 OVS_RELEASES(rule
->evict
);
211 static bool handle_openflow(struct ofconn
*, const struct ofpbuf
*);
212 static enum ofperr
handle_flow_mod__(struct ofproto
*, struct ofconn
*,
213 struct ofputil_flow_mod
*,
214 const struct ofp_header
*);
215 static void calc_duration(long long int start
, long long int now
,
216 uint32_t *sec
, uint32_t *nsec
);
219 static uint64_t pick_datapath_id(const struct ofproto
*);
220 static uint64_t pick_fallback_dpid(void);
221 static void ofproto_destroy__(struct ofproto
*);
222 static void update_mtu(struct ofproto
*, struct ofport
*);
223 static void meter_delete(struct ofproto
*, uint32_t first
, uint32_t last
);
226 static void ofproto_unixctl_init(void);
228 /* All registered ofproto classes, in probe order. */
229 static const struct ofproto_class
**ofproto_classes
;
230 static size_t n_ofproto_classes
;
231 static size_t allocated_ofproto_classes
;
233 unsigned flow_eviction_threshold
= OFPROTO_FLOW_EVICTION_THRESHOLD_DEFAULT
;
234 unsigned n_handler_threads
;
235 enum ofproto_flow_miss_model flow_miss_model
= OFPROTO_HANDLE_MISS_AUTO
;
237 /* Map from datapath name to struct ofproto, for use by unixctl commands. */
238 static struct hmap all_ofprotos
= HMAP_INITIALIZER(&all_ofprotos
);
240 /* Initial mappings of port to OpenFlow number mappings. */
241 static struct shash init_ofp_ports
= SHASH_INITIALIZER(&init_ofp_ports
);
243 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
245 /* The default value of true waits for flow restore. */
246 static bool flow_restore_wait
= true;
248 /* Must be called to initialize the ofproto library.
250 * The caller may pass in 'iface_hints', which contains an shash of
251 * "iface_hint" elements indexed by the interface's name. The provider
252 * may use these hints to describe the startup configuration in order to
253 * reinitialize its state. The caller owns the provided data, so a
254 * provider will make copies of anything required. An ofproto provider
255 * will remove any existing state that is not described by the hint, and
256 * may choose to remove it all. */
258 ofproto_init(const struct shash
*iface_hints
)
260 struct shash_node
*node
;
263 ofproto_class_register(&ofproto_dpif_class
);
265 /* Make a local copy, since we don't own 'iface_hints' elements. */
266 SHASH_FOR_EACH(node
, iface_hints
) {
267 const struct iface_hint
*orig_hint
= node
->data
;
268 struct iface_hint
*new_hint
= xmalloc(sizeof *new_hint
);
269 const char *br_type
= ofproto_normalize_type(orig_hint
->br_type
);
271 new_hint
->br_name
= xstrdup(orig_hint
->br_name
);
272 new_hint
->br_type
= xstrdup(br_type
);
273 new_hint
->ofp_port
= orig_hint
->ofp_port
;
275 shash_add(&init_ofp_ports
, node
->name
, new_hint
);
278 for (i
= 0; i
< n_ofproto_classes
; i
++) {
279 ofproto_classes
[i
]->init(&init_ofp_ports
);
283 /* 'type' should be a normalized datapath type, as returned by
284 * ofproto_normalize_type(). Returns the corresponding ofproto_class
285 * structure, or a null pointer if there is none registered for 'type'. */
286 static const struct ofproto_class
*
287 ofproto_class_find__(const char *type
)
291 for (i
= 0; i
< n_ofproto_classes
; i
++) {
292 const struct ofproto_class
*class = ofproto_classes
[i
];
297 class->enumerate_types(&types
);
298 found
= sset_contains(&types
, type
);
299 sset_destroy(&types
);
305 VLOG_WARN("unknown datapath type %s", type
);
309 /* Registers a new ofproto class. After successful registration, new ofprotos
310 * of that type can be created using ofproto_create(). */
312 ofproto_class_register(const struct ofproto_class
*new_class
)
316 for (i
= 0; i
< n_ofproto_classes
; i
++) {
317 if (ofproto_classes
[i
] == new_class
) {
322 if (n_ofproto_classes
>= allocated_ofproto_classes
) {
323 ofproto_classes
= x2nrealloc(ofproto_classes
,
324 &allocated_ofproto_classes
,
325 sizeof *ofproto_classes
);
327 ofproto_classes
[n_ofproto_classes
++] = new_class
;
331 /* Unregisters a datapath provider. 'type' must have been previously
332 * registered and not currently be in use by any ofprotos. After
333 * unregistration new datapaths of that type cannot be opened using
334 * ofproto_create(). */
336 ofproto_class_unregister(const struct ofproto_class
*class)
340 for (i
= 0; i
< n_ofproto_classes
; i
++) {
341 if (ofproto_classes
[i
] == class) {
342 for (i
++; i
< n_ofproto_classes
; i
++) {
343 ofproto_classes
[i
- 1] = ofproto_classes
[i
];
349 VLOG_WARN("attempted to unregister an ofproto class that is not "
354 /* Clears 'types' and enumerates all registered ofproto types into it. The
355 * caller must first initialize the sset. */
357 ofproto_enumerate_types(struct sset
*types
)
361 for (i
= 0; i
< n_ofproto_classes
; i
++) {
362 ofproto_classes
[i
]->enumerate_types(types
);
366 /* Returns the fully spelled out name for the given ofproto 'type'.
368 * Normalized type string can be compared with strcmp(). Unnormalized type
369 * string might be the same even if they have different spellings. */
371 ofproto_normalize_type(const char *type
)
373 return type
&& type
[0] ? type
: "system";
376 /* Clears 'names' and enumerates the names of all known created ofprotos with
377 * the given 'type'. The caller must first initialize the sset. Returns 0 if
378 * successful, otherwise a positive errno value.
380 * Some kinds of datapaths might not be practically enumerable. This is not
381 * considered an error. */
383 ofproto_enumerate_names(const char *type
, struct sset
*names
)
385 const struct ofproto_class
*class = ofproto_class_find__(type
);
386 return class ? class->enumerate_names(type
, names
) : EAFNOSUPPORT
;
390 ofproto_create(const char *datapath_name
, const char *datapath_type
,
391 struct ofproto
**ofprotop
)
393 const struct ofproto_class
*class;
394 struct ofproto
*ofproto
;
400 ofproto_unixctl_init();
402 datapath_type
= ofproto_normalize_type(datapath_type
);
403 class = ofproto_class_find__(datapath_type
);
405 VLOG_WARN("could not create datapath %s of unknown type %s",
406 datapath_name
, datapath_type
);
410 ofproto
= class->alloc();
412 VLOG_ERR("failed to allocate datapath %s of type %s",
413 datapath_name
, datapath_type
);
418 memset(ofproto
, 0, sizeof *ofproto
);
419 ofproto
->ofproto_class
= class;
420 ofproto
->name
= xstrdup(datapath_name
);
421 ofproto
->type
= xstrdup(datapath_type
);
422 hmap_insert(&all_ofprotos
, &ofproto
->hmap_node
,
423 hash_string(ofproto
->name
, 0));
424 ofproto
->datapath_id
= 0;
425 ofproto
->forward_bpdu
= false;
426 ofproto
->fallback_dpid
= pick_fallback_dpid();
427 ofproto
->mfr_desc
= NULL
;
428 ofproto
->hw_desc
= NULL
;
429 ofproto
->sw_desc
= NULL
;
430 ofproto
->serial_desc
= NULL
;
431 ofproto
->dp_desc
= NULL
;
432 ofproto
->frag_handling
= OFPC_FRAG_NORMAL
;
433 hmap_init(&ofproto
->ports
);
434 shash_init(&ofproto
->port_by_name
);
435 simap_init(&ofproto
->ofp_requests
);
436 ofproto
->max_ports
= ofp_to_u16(OFPP_MAX
);
437 ofproto
->tables
= NULL
;
438 ofproto
->n_tables
= 0;
439 hindex_init(&ofproto
->cookies
);
440 list_init(&ofproto
->expirable
);
441 ovs_mutex_init_recursive(&ofproto
->expirable_mutex
);
442 ofproto
->connmgr
= connmgr_create(ofproto
, datapath_name
, datapath_name
);
443 ofproto
->state
= S_OPENFLOW
;
444 list_init(&ofproto
->pending
);
445 ofproto
->n_pending
= 0;
446 hmap_init(&ofproto
->deletions
);
447 ofproto
->n_add
= ofproto
->n_delete
= ofproto
->n_modify
= 0;
448 ofproto
->first_op
= ofproto
->last_op
= LLONG_MIN
;
449 ofproto
->next_op_report
= LLONG_MAX
;
450 ofproto
->op_backoff
= LLONG_MIN
;
451 ofproto
->vlan_bitmap
= NULL
;
452 ofproto
->vlans_changed
= false;
453 ofproto
->min_mtu
= INT_MAX
;
455 error
= ofproto
->ofproto_class
->construct(ofproto
);
457 VLOG_ERR("failed to open datapath %s: %s",
458 datapath_name
, ovs_strerror(error
));
459 ofproto_destroy__(ofproto
);
463 /* The "max_ports" member should have been set by ->construct(ofproto).
464 * Port 0 is not a valid OpenFlow port, so mark that as unavailable. */
465 ofproto
->ofp_port_ids
= bitmap_allocate(ofproto
->max_ports
);
466 bitmap_set1(ofproto
->ofp_port_ids
, 0);
468 /* Check that hidden tables, if any, are at the end. */
469 ovs_assert(ofproto
->n_tables
);
470 for (i
= 0; i
+ 1 < ofproto
->n_tables
; i
++) {
471 enum oftable_flags flags
= ofproto
->tables
[i
].flags
;
472 enum oftable_flags next_flags
= ofproto
->tables
[i
+ 1].flags
;
474 ovs_assert(!(flags
& OFTABLE_HIDDEN
) || next_flags
& OFTABLE_HIDDEN
);
477 ofproto
->datapath_id
= pick_datapath_id(ofproto
);
480 /* Initialize meters table. */
481 if (ofproto
->ofproto_class
->meter_get_features
) {
482 ofproto
->ofproto_class
->meter_get_features(ofproto
,
483 &ofproto
->meter_features
);
485 memset(&ofproto
->meter_features
, 0, sizeof ofproto
->meter_features
);
487 ofproto
->meters
= xzalloc((ofproto
->meter_features
.max_meters
+ 1)
488 * sizeof(struct meter
*));
494 /* Must be called (only) by an ofproto implementation in its constructor
495 * function. See the large comment on 'construct' in struct ofproto_class for
498 ofproto_init_tables(struct ofproto
*ofproto
, int n_tables
)
500 struct oftable
*table
;
502 ovs_assert(!ofproto
->n_tables
);
503 ovs_assert(n_tables
>= 1 && n_tables
<= 255);
505 ofproto
->n_tables
= n_tables
;
506 ofproto
->tables
= xmalloc(n_tables
* sizeof *ofproto
->tables
);
507 OFPROTO_FOR_EACH_TABLE (table
, ofproto
) {
512 /* To be optionally called (only) by an ofproto implementation in its
513 * constructor function. See the large comment on 'construct' in struct
514 * ofproto_class for details.
516 * Sets the maximum number of ports to 'max_ports'. The ofproto generic layer
517 * will then ensure that actions passed into the ofproto implementation will
518 * not refer to OpenFlow ports numbered 'max_ports' or higher. If this
519 * function is not called, there will be no such restriction.
521 * Reserved ports numbered OFPP_MAX and higher are special and not subject to
522 * the 'max_ports' restriction. */
524 ofproto_init_max_ports(struct ofproto
*ofproto
, uint16_t max_ports
)
526 ovs_assert(max_ports
<= ofp_to_u16(OFPP_MAX
));
527 ofproto
->max_ports
= max_ports
;
531 ofproto_get_datapath_id(const struct ofproto
*ofproto
)
533 return ofproto
->datapath_id
;
537 ofproto_set_datapath_id(struct ofproto
*p
, uint64_t datapath_id
)
539 uint64_t old_dpid
= p
->datapath_id
;
540 p
->datapath_id
= datapath_id
? datapath_id
: pick_datapath_id(p
);
541 if (p
->datapath_id
!= old_dpid
) {
542 /* Force all active connections to reconnect, since there is no way to
543 * notify a controller that the datapath ID has changed. */
544 ofproto_reconnect_controllers(p
);
549 ofproto_set_controllers(struct ofproto
*p
,
550 const struct ofproto_controller
*controllers
,
551 size_t n_controllers
, uint32_t allowed_versions
)
553 connmgr_set_controllers(p
->connmgr
, controllers
, n_controllers
,
558 ofproto_set_fail_mode(struct ofproto
*p
, enum ofproto_fail_mode fail_mode
)
560 connmgr_set_fail_mode(p
->connmgr
, fail_mode
);
563 /* Drops the connections between 'ofproto' and all of its controllers, forcing
564 * them to reconnect. */
566 ofproto_reconnect_controllers(struct ofproto
*ofproto
)
568 connmgr_reconnect(ofproto
->connmgr
);
571 /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'ofproto''s
572 * in-band control should guarantee access, in the same way that in-band
573 * control guarantees access to OpenFlow controllers. */
575 ofproto_set_extra_in_band_remotes(struct ofproto
*ofproto
,
576 const struct sockaddr_in
*extras
, size_t n
)
578 connmgr_set_extra_in_band_remotes(ofproto
->connmgr
, extras
, n
);
581 /* Sets the OpenFlow queue used by flows set up by in-band control on
582 * 'ofproto' to 'queue_id'. If 'queue_id' is negative, then in-band control
583 * flows will use the default queue. */
585 ofproto_set_in_band_queue(struct ofproto
*ofproto
, int queue_id
)
587 connmgr_set_in_band_queue(ofproto
->connmgr
, queue_id
);
590 /* Sets the number of flows at which eviction from the kernel flow table
593 ofproto_set_flow_eviction_threshold(unsigned threshold
)
595 flow_eviction_threshold
= MAX(OFPROTO_FLOW_EVICTION_THRESHOLD_MIN
,
599 /* Sets the path for handling flow misses. */
601 ofproto_set_flow_miss_model(unsigned model
)
603 flow_miss_model
= model
;
606 /* If forward_bpdu is true, the NORMAL action will forward frames with
607 * reserved (e.g. STP) destination Ethernet addresses. if forward_bpdu is false,
608 * the NORMAL action will drop these frames. */
610 ofproto_set_forward_bpdu(struct ofproto
*ofproto
, bool forward_bpdu
)
612 bool old_val
= ofproto
->forward_bpdu
;
613 ofproto
->forward_bpdu
= forward_bpdu
;
614 if (old_val
!= ofproto
->forward_bpdu
) {
615 if (ofproto
->ofproto_class
->forward_bpdu_changed
) {
616 ofproto
->ofproto_class
->forward_bpdu_changed(ofproto
);
621 /* Sets the MAC aging timeout for the OFPP_NORMAL action on 'ofproto' to
622 * 'idle_time', in seconds, and the maximum number of MAC table entries to
625 ofproto_set_mac_table_config(struct ofproto
*ofproto
, unsigned idle_time
,
628 if (ofproto
->ofproto_class
->set_mac_table_config
) {
629 ofproto
->ofproto_class
->set_mac_table_config(ofproto
, idle_time
,
634 /* Sets number of upcall handler threads. The default is
635 * (number of online cores - 1). */
637 ofproto_set_n_handler_threads(unsigned limit
)
640 n_handler_threads
= limit
;
642 n_handler_threads
= MAX(1, sysconf(_SC_NPROCESSORS_ONLN
) - 1);
647 ofproto_set_dp_desc(struct ofproto
*p
, const char *dp_desc
)
650 p
->dp_desc
= dp_desc
? xstrdup(dp_desc
) : NULL
;
654 ofproto_set_snoops(struct ofproto
*ofproto
, const struct sset
*snoops
)
656 return connmgr_set_snoops(ofproto
->connmgr
, snoops
);
660 ofproto_set_netflow(struct ofproto
*ofproto
,
661 const struct netflow_options
*nf_options
)
663 if (nf_options
&& sset_is_empty(&nf_options
->collectors
)) {
667 if (ofproto
->ofproto_class
->set_netflow
) {
668 return ofproto
->ofproto_class
->set_netflow(ofproto
, nf_options
);
670 return nf_options
? EOPNOTSUPP
: 0;
675 ofproto_set_sflow(struct ofproto
*ofproto
,
676 const struct ofproto_sflow_options
*oso
)
678 if (oso
&& sset_is_empty(&oso
->targets
)) {
682 if (ofproto
->ofproto_class
->set_sflow
) {
683 return ofproto
->ofproto_class
->set_sflow(ofproto
, oso
);
685 return oso
? EOPNOTSUPP
: 0;
690 ofproto_set_ipfix(struct ofproto
*ofproto
,
691 const struct ofproto_ipfix_bridge_exporter_options
*bo
,
692 const struct ofproto_ipfix_flow_exporter_options
*fo
,
695 if (ofproto
->ofproto_class
->set_ipfix
) {
696 return ofproto
->ofproto_class
->set_ipfix(ofproto
, bo
, fo
, n_fo
);
698 return (bo
|| fo
) ? EOPNOTSUPP
: 0;
703 ofproto_set_flow_restore_wait(bool flow_restore_wait_db
)
705 flow_restore_wait
= flow_restore_wait_db
;
709 ofproto_get_flow_restore_wait(void)
711 return flow_restore_wait
;
715 /* Spanning Tree Protocol (STP) configuration. */
717 /* Configures STP on 'ofproto' using the settings defined in 's'. If
718 * 's' is NULL, disables STP.
720 * Returns 0 if successful, otherwise a positive errno value. */
722 ofproto_set_stp(struct ofproto
*ofproto
,
723 const struct ofproto_stp_settings
*s
)
725 return (ofproto
->ofproto_class
->set_stp
726 ? ofproto
->ofproto_class
->set_stp(ofproto
, s
)
730 /* Retrieves STP status of 'ofproto' and stores it in 's'. If the
731 * 'enabled' member of 's' is false, then the other members are not
734 * Returns 0 if successful, otherwise a positive errno value. */
736 ofproto_get_stp_status(struct ofproto
*ofproto
,
737 struct ofproto_stp_status
*s
)
739 return (ofproto
->ofproto_class
->get_stp_status
740 ? ofproto
->ofproto_class
->get_stp_status(ofproto
, s
)
744 /* Configures STP on 'ofp_port' of 'ofproto' using the settings defined
745 * in 's'. The caller is responsible for assigning STP port numbers
746 * (using the 'port_num' member in the range of 1 through 255, inclusive)
747 * and ensuring there are no duplicates. If the 's' is NULL, then STP
748 * is disabled on the port.
750 * Returns 0 if successful, otherwise a positive errno value.*/
752 ofproto_port_set_stp(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
753 const struct ofproto_port_stp_settings
*s
)
755 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
757 VLOG_WARN("%s: cannot configure STP on nonexistent port %"PRIu16
,
758 ofproto
->name
, ofp_port
);
762 return (ofproto
->ofproto_class
->set_stp_port
763 ? ofproto
->ofproto_class
->set_stp_port(ofport
, s
)
767 /* Retrieves STP port status of 'ofp_port' on 'ofproto' and stores it in
768 * 's'. If the 'enabled' member in 's' is false, then the other members
769 * are not meaningful.
771 * Returns 0 if successful, otherwise a positive errno value.*/
773 ofproto_port_get_stp_status(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
774 struct ofproto_port_stp_status
*s
)
776 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
778 VLOG_WARN_RL(&rl
, "%s: cannot get STP status on nonexistent "
779 "port %"PRIu16
, ofproto
->name
, ofp_port
);
783 return (ofproto
->ofproto_class
->get_stp_port_status
784 ? ofproto
->ofproto_class
->get_stp_port_status(ofport
, s
)
788 /* Queue DSCP configuration. */
790 /* Registers meta-data associated with the 'n_qdscp' Qualities of Service
791 * 'queues' attached to 'ofport'. This data is not intended to be sufficient
792 * to implement QoS. Instead, it is used to implement features which require
793 * knowledge of what queues exist on a port, and some basic information about
796 * Returns 0 if successful, otherwise a positive errno value. */
798 ofproto_port_set_queues(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
799 const struct ofproto_port_queue
*queues
,
802 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
805 VLOG_WARN("%s: cannot set queues on nonexistent port %"PRIu16
,
806 ofproto
->name
, ofp_port
);
810 return (ofproto
->ofproto_class
->set_queues
811 ? ofproto
->ofproto_class
->set_queues(ofport
, queues
, n_queues
)
815 /* Connectivity Fault Management configuration. */
817 /* Clears the CFM configuration from 'ofp_port' on 'ofproto'. */
819 ofproto_port_clear_cfm(struct ofproto
*ofproto
, ofp_port_t ofp_port
)
821 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
822 if (ofport
&& ofproto
->ofproto_class
->set_cfm
) {
823 ofproto
->ofproto_class
->set_cfm(ofport
, NULL
);
827 /* Configures connectivity fault management on 'ofp_port' in 'ofproto'. Takes
828 * basic configuration from the configuration members in 'cfm', and the remote
829 * maintenance point ID from remote_mpid. Ignores the statistics members of
832 * This function has no effect if 'ofproto' does not have a port 'ofp_port'. */
834 ofproto_port_set_cfm(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
835 const struct cfm_settings
*s
)
837 struct ofport
*ofport
;
840 ofport
= ofproto_get_port(ofproto
, ofp_port
);
842 VLOG_WARN("%s: cannot configure CFM on nonexistent port %"PRIu16
,
843 ofproto
->name
, ofp_port
);
847 /* XXX: For configuration simplicity, we only support one remote_mpid
848 * outside of the CFM module. It's not clear if this is the correct long
849 * term solution or not. */
850 error
= (ofproto
->ofproto_class
->set_cfm
851 ? ofproto
->ofproto_class
->set_cfm(ofport
, s
)
854 VLOG_WARN("%s: CFM configuration on port %"PRIu16
" (%s) failed (%s)",
855 ofproto
->name
, ofp_port
, netdev_get_name(ofport
->netdev
),
856 ovs_strerror(error
));
860 /* Configures BFD on 'ofp_port' in 'ofproto'. This function has no effect if
861 * 'ofproto' does not have a port 'ofp_port'. */
863 ofproto_port_set_bfd(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
864 const struct smap
*cfg
)
866 struct ofport
*ofport
;
869 ofport
= ofproto_get_port(ofproto
, ofp_port
);
871 VLOG_WARN("%s: cannot configure bfd on nonexistent port %"PRIu16
,
872 ofproto
->name
, ofp_port
);
876 error
= (ofproto
->ofproto_class
->set_bfd
877 ? ofproto
->ofproto_class
->set_bfd(ofport
, cfg
)
880 VLOG_WARN("%s: bfd configuration on port %"PRIu16
" (%s) failed (%s)",
881 ofproto
->name
, ofp_port
, netdev_get_name(ofport
->netdev
),
882 ovs_strerror(error
));
886 /* Populates 'status' with key value pairs indicating the status of the BFD
887 * session on 'ofp_port'. This information is intended to be populated in the
888 * OVS database. Has no effect if 'ofp_port' is not na OpenFlow port in
891 ofproto_port_get_bfd_status(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
894 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
895 return (ofport
&& ofproto
->ofproto_class
->get_bfd_status
896 ? ofproto
->ofproto_class
->get_bfd_status(ofport
, status
)
900 /* Checks the status of LACP negotiation for 'ofp_port' within ofproto.
901 * Returns 1 if LACP partner information for 'ofp_port' is up-to-date,
902 * 0 if LACP partner information is not current (generally indicating a
903 * connectivity problem), or -1 if LACP is not enabled on 'ofp_port'. */
905 ofproto_port_is_lacp_current(struct ofproto
*ofproto
, ofp_port_t ofp_port
)
907 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
908 return (ofport
&& ofproto
->ofproto_class
->port_is_lacp_current
909 ? ofproto
->ofproto_class
->port_is_lacp_current(ofport
)
915 /* Registers a "bundle" associated with client data pointer 'aux' in 'ofproto'.
916 * A bundle is the same concept as a Port in OVSDB, that is, it consists of one
917 * or more "slave" devices (Interfaces, in OVSDB) along with a VLAN
918 * configuration plus, if there is more than one slave, a bonding
921 * If 'aux' is already registered then this function updates its configuration
922 * to 's'. Otherwise, this function registers a new bundle.
924 * Bundles only affect the NXAST_AUTOPATH action and output to the OFPP_NORMAL
927 ofproto_bundle_register(struct ofproto
*ofproto
, void *aux
,
928 const struct ofproto_bundle_settings
*s
)
930 return (ofproto
->ofproto_class
->bundle_set
931 ? ofproto
->ofproto_class
->bundle_set(ofproto
, aux
, s
)
935 /* Unregisters the bundle registered on 'ofproto' with auxiliary data 'aux'.
936 * If no such bundle has been registered, this has no effect. */
938 ofproto_bundle_unregister(struct ofproto
*ofproto
, void *aux
)
940 return ofproto_bundle_register(ofproto
, aux
, NULL
);
944 /* Registers a mirror associated with client data pointer 'aux' in 'ofproto'.
945 * If 'aux' is already registered then this function updates its configuration
946 * to 's'. Otherwise, this function registers a new mirror. */
948 ofproto_mirror_register(struct ofproto
*ofproto
, void *aux
,
949 const struct ofproto_mirror_settings
*s
)
951 return (ofproto
->ofproto_class
->mirror_set
952 ? ofproto
->ofproto_class
->mirror_set(ofproto
, aux
, s
)
956 /* Unregisters the mirror registered on 'ofproto' with auxiliary data 'aux'.
957 * If no mirror has been registered, this has no effect. */
959 ofproto_mirror_unregister(struct ofproto
*ofproto
, void *aux
)
961 return ofproto_mirror_register(ofproto
, aux
, NULL
);
964 /* Retrieves statistics from mirror associated with client data pointer
965 * 'aux' in 'ofproto'. Stores packet and byte counts in 'packets' and
966 * 'bytes', respectively. If a particular counters is not supported,
967 * the appropriate argument is set to UINT64_MAX. */
969 ofproto_mirror_get_stats(struct ofproto
*ofproto
, void *aux
,
970 uint64_t *packets
, uint64_t *bytes
)
972 if (!ofproto
->ofproto_class
->mirror_get_stats
) {
973 *packets
= *bytes
= UINT64_MAX
;
977 return ofproto
->ofproto_class
->mirror_get_stats(ofproto
, aux
,
981 /* Configures the VLANs whose bits are set to 1 in 'flood_vlans' as VLANs on
982 * which all packets are flooded, instead of using MAC learning. If
983 * 'flood_vlans' is NULL, then MAC learning applies to all VLANs.
985 * Flood VLANs affect only the treatment of packets output to the OFPP_NORMAL
988 ofproto_set_flood_vlans(struct ofproto
*ofproto
, unsigned long *flood_vlans
)
990 return (ofproto
->ofproto_class
->set_flood_vlans
991 ? ofproto
->ofproto_class
->set_flood_vlans(ofproto
, flood_vlans
)
995 /* Returns true if 'aux' is a registered bundle that is currently in use as the
996 * output for a mirror. */
998 ofproto_is_mirror_output_bundle(const struct ofproto
*ofproto
, void *aux
)
1000 return (ofproto
->ofproto_class
->is_mirror_output_bundle
1001 ? ofproto
->ofproto_class
->is_mirror_output_bundle(ofproto
, aux
)
1005 /* Configuration of OpenFlow tables. */
1007 /* Returns the number of OpenFlow tables in 'ofproto'. */
1009 ofproto_get_n_tables(const struct ofproto
*ofproto
)
1011 return ofproto
->n_tables
;
1014 /* Configures the OpenFlow table in 'ofproto' with id 'table_id' with the
1015 * settings from 's'. 'table_id' must be in the range 0 through the number of
1016 * OpenFlow tables in 'ofproto' minus 1, inclusive.
1018 * For read-only tables, only the name may be configured. */
1020 ofproto_configure_table(struct ofproto
*ofproto
, int table_id
,
1021 const struct ofproto_table_settings
*s
)
1023 struct oftable
*table
;
1025 ovs_assert(table_id
>= 0 && table_id
< ofproto
->n_tables
);
1026 table
= &ofproto
->tables
[table_id
];
1028 oftable_set_name(table
, s
->name
);
1030 if (table
->flags
& OFTABLE_READONLY
) {
1035 oftable_enable_eviction(table
, s
->groups
, s
->n_groups
);
1037 oftable_disable_eviction(table
);
1040 table
->max_flows
= s
->max_flows
;
1041 ovs_rwlock_rdlock(&table
->cls
.rwlock
);
1042 if (classifier_count(&table
->cls
) > table
->max_flows
1043 && table
->eviction_fields
) {
1044 /* 'table' contains more flows than allowed. We might not be able to
1045 * evict them right away because of the asynchronous nature of flow
1046 * table changes. Schedule eviction for later. */
1047 switch (ofproto
->state
) {
1049 ofproto
->state
= S_EVICT
;
1053 /* We're already deleting flows, nothing more to do. */
1057 ovs_rwlock_unlock(&table
->cls
.rwlock
);
1061 ofproto_has_snoops(const struct ofproto
*ofproto
)
1063 return connmgr_has_snoops(ofproto
->connmgr
);
1067 ofproto_get_snoops(const struct ofproto
*ofproto
, struct sset
*snoops
)
1069 connmgr_get_snoops(ofproto
->connmgr
, snoops
);
1073 ofproto_flush__(struct ofproto
*ofproto
)
1075 struct ofopgroup
*group
;
1076 struct oftable
*table
;
1078 if (ofproto
->ofproto_class
->flush
) {
1079 ofproto
->ofproto_class
->flush(ofproto
);
1082 group
= ofopgroup_create_unattached(ofproto
);
1083 OFPROTO_FOR_EACH_TABLE (table
, ofproto
) {
1084 struct rule
*rule
, *next_rule
;
1085 struct cls_cursor cursor
;
1087 if (table
->flags
& OFTABLE_HIDDEN
) {
1091 ovs_rwlock_wrlock(&table
->cls
.rwlock
);
1092 cls_cursor_init(&cursor
, &table
->cls
, NULL
);
1093 CLS_CURSOR_FOR_EACH_SAFE (rule
, next_rule
, cr
, &cursor
) {
1094 if (!rule
->pending
) {
1095 ofoperation_create(group
, rule
, OFOPERATION_DELETE
,
1097 ovs_rwlock_wrlock(&rule
->evict
);
1098 oftable_remove_rule__(ofproto
, &table
->cls
, rule
);
1099 ofproto
->ofproto_class
->rule_destruct(rule
);
1102 ovs_rwlock_unlock(&table
->cls
.rwlock
);
1104 ofopgroup_submit(group
);
1108 ofproto_destroy__(struct ofproto
*ofproto
)
1110 struct oftable
*table
;
1112 ovs_assert(list_is_empty(&ofproto
->pending
));
1113 ovs_assert(!ofproto
->n_pending
);
1115 if (ofproto
->meters
) {
1116 meter_delete(ofproto
, 1, ofproto
->meter_features
.max_meters
);
1117 free(ofproto
->meters
);
1120 connmgr_destroy(ofproto
->connmgr
);
1122 hmap_remove(&all_ofprotos
, &ofproto
->hmap_node
);
1123 free(ofproto
->name
);
1124 free(ofproto
->type
);
1125 free(ofproto
->mfr_desc
);
1126 free(ofproto
->hw_desc
);
1127 free(ofproto
->sw_desc
);
1128 free(ofproto
->serial_desc
);
1129 free(ofproto
->dp_desc
);
1130 hmap_destroy(&ofproto
->ports
);
1131 shash_destroy(&ofproto
->port_by_name
);
1132 bitmap_free(ofproto
->ofp_port_ids
);
1133 simap_destroy(&ofproto
->ofp_requests
);
1135 OFPROTO_FOR_EACH_TABLE (table
, ofproto
) {
1136 oftable_destroy(table
);
1138 free(ofproto
->tables
);
1140 hmap_destroy(&ofproto
->deletions
);
1142 free(ofproto
->vlan_bitmap
);
1144 ovs_mutex_destroy(&ofproto
->expirable_mutex
);
1145 ofproto
->ofproto_class
->dealloc(ofproto
);
1149 ofproto_destroy(struct ofproto
*p
)
1151 struct ofport
*ofport
, *next_ofport
;
1158 HMAP_FOR_EACH_SAFE (ofport
, next_ofport
, hmap_node
, &p
->ports
) {
1159 ofport_destroy(ofport
);
1162 p
->ofproto_class
->destruct(p
);
1163 ofproto_destroy__(p
);
1166 /* Destroys the datapath with the respective 'name' and 'type'. With the Linux
1167 * kernel datapath, for example, this destroys the datapath in the kernel, and
1168 * with the netdev-based datapath, it tears down the data structures that
1169 * represent the datapath.
1171 * The datapath should not be currently open as an ofproto. */
1173 ofproto_delete(const char *name
, const char *type
)
1175 const struct ofproto_class
*class = ofproto_class_find__(type
);
1176 return (!class ? EAFNOSUPPORT
1177 : !class->del
? EACCES
1178 : class->del(type
, name
));
1182 process_port_change(struct ofproto
*ofproto
, int error
, char *devname
)
1184 if (error
== ENOBUFS
) {
1185 reinit_ports(ofproto
);
1186 } else if (!error
) {
1187 update_port(ofproto
, devname
);
1193 ofproto_type_run(const char *datapath_type
)
1195 const struct ofproto_class
*class;
1198 datapath_type
= ofproto_normalize_type(datapath_type
);
1199 class = ofproto_class_find__(datapath_type
);
1201 error
= class->type_run
? class->type_run(datapath_type
) : 0;
1202 if (error
&& error
!= EAGAIN
) {
1203 VLOG_ERR_RL(&rl
, "%s: type_run failed (%s)",
1204 datapath_type
, ovs_strerror(error
));
1210 ofproto_type_run_fast(const char *datapath_type
)
1212 const struct ofproto_class
*class;
1215 datapath_type
= ofproto_normalize_type(datapath_type
);
1216 class = ofproto_class_find__(datapath_type
);
1218 error
= class->type_run_fast
? class->type_run_fast(datapath_type
) : 0;
1219 if (error
&& error
!= EAGAIN
) {
1220 VLOG_ERR_RL(&rl
, "%s: type_run_fast failed (%s)",
1221 datapath_type
, ovs_strerror(error
));
1227 ofproto_type_wait(const char *datapath_type
)
1229 const struct ofproto_class
*class;
1231 datapath_type
= ofproto_normalize_type(datapath_type
);
1232 class = ofproto_class_find__(datapath_type
);
1234 if (class->type_wait
) {
1235 class->type_wait(datapath_type
);
1240 ofproto_run(struct ofproto
*p
)
1242 struct sset changed_netdevs
;
1243 const char *changed_netdev
;
1244 struct ofport
*ofport
;
1247 error
= p
->ofproto_class
->run(p
);
1248 if (error
&& error
!= EAGAIN
) {
1249 VLOG_ERR_RL(&rl
, "%s: run failed (%s)", p
->name
, ovs_strerror(error
));
1252 if (p
->ofproto_class
->port_poll
) {
1255 while ((error
= p
->ofproto_class
->port_poll(p
, &devname
)) != EAGAIN
) {
1256 process_port_change(p
, error
, devname
);
1260 /* Update OpenFlow port status for any port whose netdev has changed.
1262 * Refreshing a given 'ofport' can cause an arbitrary ofport to be
1263 * destroyed, so it's not safe to update ports directly from the
1264 * HMAP_FOR_EACH loop, or even to use HMAP_FOR_EACH_SAFE. Instead, we
1265 * need this two-phase approach. */
1266 sset_init(&changed_netdevs
);
1267 HMAP_FOR_EACH (ofport
, hmap_node
, &p
->ports
) {
1268 unsigned int change_seq
= netdev_change_seq(ofport
->netdev
);
1269 if (ofport
->change_seq
!= change_seq
) {
1270 ofport
->change_seq
= change_seq
;
1271 sset_add(&changed_netdevs
, netdev_get_name(ofport
->netdev
));
1274 SSET_FOR_EACH (changed_netdev
, &changed_netdevs
) {
1275 update_port(p
, changed_netdev
);
1277 sset_destroy(&changed_netdevs
);
1281 connmgr_run(p
->connmgr
, handle_openflow
);
1285 connmgr_run(p
->connmgr
, NULL
);
1287 if (list_is_empty(&p
->pending
) && hmap_is_empty(&p
->deletions
)) {
1288 p
->state
= S_OPENFLOW
;
1293 connmgr_run(p
->connmgr
, NULL
);
1295 if (list_is_empty(&p
->pending
) && hmap_is_empty(&p
->deletions
)) {
1296 connmgr_flushed(p
->connmgr
);
1297 p
->state
= S_OPENFLOW
;
1305 if (time_msec() >= p
->next_op_report
) {
1306 long long int ago
= (time_msec() - p
->first_op
) / 1000;
1307 long long int interval
= (p
->last_op
- p
->first_op
) / 1000;
1311 ds_put_format(&s
, "%d flow_mods ",
1312 p
->n_add
+ p
->n_delete
+ p
->n_modify
);
1313 if (interval
== ago
) {
1314 ds_put_format(&s
, "in the last %lld s", ago
);
1315 } else if (interval
) {
1316 ds_put_format(&s
, "in the %lld s starting %lld s ago",
1319 ds_put_format(&s
, "%lld s ago", ago
);
1322 ds_put_cstr(&s
, " (");
1324 ds_put_format(&s
, "%d adds, ", p
->n_add
);
1327 ds_put_format(&s
, "%d deletes, ", p
->n_delete
);
1330 ds_put_format(&s
, "%d modifications, ", p
->n_modify
);
1333 ds_put_char(&s
, ')');
1335 VLOG_INFO("%s: %s", p
->name
, ds_cstr(&s
));
1338 p
->n_add
= p
->n_delete
= p
->n_modify
= 0;
1339 p
->next_op_report
= LLONG_MAX
;
1345 /* Performs periodic activity required by 'ofproto' that needs to be done
1346 * with the least possible latency.
1348 * It makes sense to call this function a couple of times per poll loop, to
1349 * provide a significant performance boost on some benchmarks with the
1350 * ofproto-dpif implementation. */
1352 ofproto_run_fast(struct ofproto
*p
)
1356 error
= p
->ofproto_class
->run_fast
? p
->ofproto_class
->run_fast(p
) : 0;
1357 if (error
&& error
!= EAGAIN
) {
1358 VLOG_ERR_RL(&rl
, "%s: fastpath run failed (%s)",
1359 p
->name
, ovs_strerror(error
));
1365 ofproto_wait(struct ofproto
*p
)
1367 struct ofport
*ofport
;
1369 p
->ofproto_class
->wait(p
);
1370 if (p
->ofproto_class
->port_poll_wait
) {
1371 p
->ofproto_class
->port_poll_wait(p
);
1374 HMAP_FOR_EACH (ofport
, hmap_node
, &p
->ports
) {
1375 if (ofport
->change_seq
!= netdev_change_seq(ofport
->netdev
)) {
1376 poll_immediate_wake();
1382 connmgr_wait(p
->connmgr
, true);
1387 connmgr_wait(p
->connmgr
, false);
1388 if (list_is_empty(&p
->pending
) && hmap_is_empty(&p
->deletions
)) {
1389 poll_immediate_wake();
1396 ofproto_is_alive(const struct ofproto
*p
)
1398 return connmgr_has_controllers(p
->connmgr
);
1401 /* Adds some memory usage statistics for 'ofproto' into 'usage', for use with
1402 * memory_report(). */
1404 ofproto_get_memory_usage(const struct ofproto
*ofproto
, struct simap
*usage
)
1406 const struct oftable
*table
;
1407 unsigned int n_rules
;
1409 simap_increase(usage
, "ports", hmap_count(&ofproto
->ports
));
1410 simap_increase(usage
, "ops",
1411 ofproto
->n_pending
+ hmap_count(&ofproto
->deletions
));
1414 OFPROTO_FOR_EACH_TABLE (table
, ofproto
) {
1415 ovs_rwlock_rdlock(&table
->cls
.rwlock
);
1416 n_rules
+= classifier_count(&table
->cls
);
1417 ovs_rwlock_unlock(&table
->cls
.rwlock
);
1419 simap_increase(usage
, "rules", n_rules
);
1421 if (ofproto
->ofproto_class
->get_memory_usage
) {
1422 ofproto
->ofproto_class
->get_memory_usage(ofproto
, usage
);
1425 connmgr_get_memory_usage(ofproto
->connmgr
, usage
);
1429 ofproto_get_ofproto_controller_info(const struct ofproto
*ofproto
,
1432 connmgr_get_controller_info(ofproto
->connmgr
, info
);
1436 ofproto_free_ofproto_controller_info(struct shash
*info
)
1438 connmgr_free_controller_info(info
);
1441 /* Makes a deep copy of 'old' into 'port'. */
1443 ofproto_port_clone(struct ofproto_port
*port
, const struct ofproto_port
*old
)
1445 port
->name
= xstrdup(old
->name
);
1446 port
->type
= xstrdup(old
->type
);
1447 port
->ofp_port
= old
->ofp_port
;
1450 /* Frees memory allocated to members of 'ofproto_port'.
1452 * Do not call this function on an ofproto_port obtained from
1453 * ofproto_port_dump_next(): that function retains ownership of the data in the
1456 ofproto_port_destroy(struct ofproto_port
*ofproto_port
)
1458 free(ofproto_port
->name
);
1459 free(ofproto_port
->type
);
1462 /* Initializes 'dump' to begin dumping the ports in an ofproto.
1464 * This function provides no status indication. An error status for the entire
1465 * dump operation is provided when it is completed by calling
1466 * ofproto_port_dump_done().
1469 ofproto_port_dump_start(struct ofproto_port_dump
*dump
,
1470 const struct ofproto
*ofproto
)
1472 dump
->ofproto
= ofproto
;
1473 dump
->error
= ofproto
->ofproto_class
->port_dump_start(ofproto
,
1477 /* Attempts to retrieve another port from 'dump', which must have been created
1478 * with ofproto_port_dump_start(). On success, stores a new ofproto_port into
1479 * 'port' and returns true. On failure, returns false.
1481 * Failure might indicate an actual error or merely that the last port has been
1482 * dumped. An error status for the entire dump operation is provided when it
1483 * is completed by calling ofproto_port_dump_done().
1485 * The ofproto owns the data stored in 'port'. It will remain valid until at
1486 * least the next time 'dump' is passed to ofproto_port_dump_next() or
1487 * ofproto_port_dump_done(). */
1489 ofproto_port_dump_next(struct ofproto_port_dump
*dump
,
1490 struct ofproto_port
*port
)
1492 const struct ofproto
*ofproto
= dump
->ofproto
;
1498 dump
->error
= ofproto
->ofproto_class
->port_dump_next(ofproto
, dump
->state
,
1501 ofproto
->ofproto_class
->port_dump_done(ofproto
, dump
->state
);
1507 /* Completes port table dump operation 'dump', which must have been created
1508 * with ofproto_port_dump_start(). Returns 0 if the dump operation was
1509 * error-free, otherwise a positive errno value describing the problem. */
1511 ofproto_port_dump_done(struct ofproto_port_dump
*dump
)
1513 const struct ofproto
*ofproto
= dump
->ofproto
;
1515 dump
->error
= ofproto
->ofproto_class
->port_dump_done(ofproto
,
1518 return dump
->error
== EOF
? 0 : dump
->error
;
1521 /* Returns the type to pass to netdev_open() when a datapath of type
1522 * 'datapath_type' has a port of type 'port_type', for a few special
1523 * cases when a netdev type differs from a port type. For example, when
1524 * using the userspace datapath, a port of type "internal" needs to be
1527 * Returns either 'type' itself or a string literal, which must not be
1530 ofproto_port_open_type(const char *datapath_type
, const char *port_type
)
1532 const struct ofproto_class
*class;
1534 datapath_type
= ofproto_normalize_type(datapath_type
);
1535 class = ofproto_class_find__(datapath_type
);
1540 return (class->port_open_type
1541 ? class->port_open_type(datapath_type
, port_type
)
1545 /* Attempts to add 'netdev' as a port on 'ofproto'. If 'ofp_portp' is
1546 * non-null and '*ofp_portp' is not OFPP_NONE, attempts to use that as
1547 * the port's OpenFlow port number.
1549 * If successful, returns 0 and sets '*ofp_portp' to the new port's
1550 * OpenFlow port number (if 'ofp_portp' is non-null). On failure,
1551 * returns a positive errno value and sets '*ofp_portp' to OFPP_NONE (if
1552 * 'ofp_portp' is non-null). */
1554 ofproto_port_add(struct ofproto
*ofproto
, struct netdev
*netdev
,
1555 ofp_port_t
*ofp_portp
)
1557 ofp_port_t ofp_port
= ofp_portp
? *ofp_portp
: OFPP_NONE
;
1560 error
= ofproto
->ofproto_class
->port_add(ofproto
, netdev
);
1562 const char *netdev_name
= netdev_get_name(netdev
);
1564 simap_put(&ofproto
->ofp_requests
, netdev_name
,
1565 ofp_to_u16(ofp_port
));
1566 update_port(ofproto
, netdev_name
);
1569 struct ofproto_port ofproto_port
;
1571 ofproto_port_query_by_name(ofproto
, netdev_get_name(netdev
),
1573 *ofp_portp
= error
? OFPP_NONE
: ofproto_port
.ofp_port
;
1574 ofproto_port_destroy(&ofproto_port
);
1579 /* Looks up a port named 'devname' in 'ofproto'. On success, returns 0 and
1580 * initializes '*port' appropriately; on failure, returns a positive errno
1583 * The caller owns the data in 'ofproto_port' and must free it with
1584 * ofproto_port_destroy() when it is no longer needed. */
1586 ofproto_port_query_by_name(const struct ofproto
*ofproto
, const char *devname
,
1587 struct ofproto_port
*port
)
1591 error
= ofproto
->ofproto_class
->port_query_by_name(ofproto
, devname
, port
);
1593 memset(port
, 0, sizeof *port
);
1598 /* Deletes port number 'ofp_port' from the datapath for 'ofproto'.
1599 * Returns 0 if successful, otherwise a positive errno. */
1601 ofproto_port_del(struct ofproto
*ofproto
, ofp_port_t ofp_port
)
1603 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
1604 const char *name
= ofport
? netdev_get_name(ofport
->netdev
) : "<unknown>";
1605 struct simap_node
*ofp_request_node
;
1608 ofp_request_node
= simap_find(&ofproto
->ofp_requests
, name
);
1609 if (ofp_request_node
) {
1610 simap_delete(&ofproto
->ofp_requests
, ofp_request_node
);
1613 error
= ofproto
->ofproto_class
->port_del(ofproto
, ofp_port
);
1614 if (!error
&& ofport
) {
1615 /* 'name' is the netdev's name and update_port() is going to close the
1616 * netdev. Just in case update_port() refers to 'name' after it
1617 * destroys 'ofport', make a copy of it around the update_port()
1619 char *devname
= xstrdup(name
);
1620 update_port(ofproto
, devname
);
1626 /* Adds a flow to OpenFlow flow table 0 in 'p' that matches 'cls_rule' and
1627 * performs the 'n_actions' actions in 'actions'. The new flow will not
1630 * If cls_rule->priority is in the range of priorities supported by OpenFlow
1631 * (0...65535, inclusive) then the flow will be visible to OpenFlow
1632 * controllers; otherwise, it will be hidden.
1634 * The caller retains ownership of 'cls_rule' and 'ofpacts'.
1636 * This is a helper function for in-band control and fail-open. */
1638 ofproto_add_flow(struct ofproto
*ofproto
, const struct match
*match
,
1639 unsigned int priority
,
1640 const struct ofpact
*ofpacts
, size_t ofpacts_len
)
1642 const struct rule
*rule
;
1644 ovs_rwlock_rdlock(&ofproto
->tables
[0].cls
.rwlock
);
1645 rule
= rule_from_cls_rule(classifier_find_match_exactly(
1646 &ofproto
->tables
[0].cls
, match
, priority
));
1647 ovs_rwlock_unlock(&ofproto
->tables
[0].cls
.rwlock
);
1648 if (!rule
|| !ofpacts_equal(rule
->ofpacts
, rule
->ofpacts_len
,
1649 ofpacts
, ofpacts_len
)) {
1650 struct ofputil_flow_mod fm
;
1652 memset(&fm
, 0, sizeof fm
);
1654 fm
.priority
= priority
;
1655 fm
.buffer_id
= UINT32_MAX
;
1656 fm
.ofpacts
= xmemdup(ofpacts
, ofpacts_len
);
1657 fm
.ofpacts_len
= ofpacts_len
;
1658 add_flow(ofproto
, NULL
, &fm
, NULL
);
1663 /* Executes the flow modification specified in 'fm'. Returns 0 on success, an
1664 * OFPERR_* OpenFlow error code on failure, or OFPROTO_POSTPONE if the
1665 * operation cannot be initiated now but may be retried later.
1667 * This is a helper function for in-band control and fail-open. */
1669 ofproto_flow_mod(struct ofproto
*ofproto
, struct ofputil_flow_mod
*fm
)
1671 return handle_flow_mod__(ofproto
, NULL
, fm
, NULL
);
1674 /* Searches for a rule with matching criteria exactly equal to 'target' in
1675 * ofproto's table 0 and, if it finds one, deletes it.
1677 * This is a helper function for in-band control and fail-open. */
1679 ofproto_delete_flow(struct ofproto
*ofproto
,
1680 const struct match
*target
, unsigned int priority
)
1684 ovs_rwlock_rdlock(&ofproto
->tables
[0].cls
.rwlock
);
1685 rule
= rule_from_cls_rule(classifier_find_match_exactly(
1686 &ofproto
->tables
[0].cls
, target
, priority
));
1687 ovs_rwlock_unlock(&ofproto
->tables
[0].cls
.rwlock
);
1689 /* No such rule -> success. */
1691 } else if (rule
->pending
) {
1692 /* An operation on the rule is already pending -> failure.
1693 * Caller must retry later if it's important. */
1696 /* Initiate deletion -> success. */
1697 struct ofopgroup
*group
= ofopgroup_create_unattached(ofproto
);
1698 ofoperation_create(group
, rule
, OFOPERATION_DELETE
, OFPRR_DELETE
);
1699 ovs_rwlock_wrlock(&rule
->evict
);
1700 oftable_remove_rule(rule
);
1701 ofproto
->ofproto_class
->rule_destruct(rule
);
1702 ofopgroup_submit(group
);
1708 /* Starts the process of deleting all of the flows from all of ofproto's flow
1709 * tables and then reintroducing the flows required by in-band control and
1710 * fail-open. The process will complete in a later call to ofproto_run(). */
1712 ofproto_flush_flows(struct ofproto
*ofproto
)
1714 COVERAGE_INC(ofproto_flush
);
1715 ofproto
->state
= S_FLUSH
;
1719 reinit_ports(struct ofproto
*p
)
1721 struct ofproto_port_dump dump
;
1722 struct sset devnames
;
1723 struct ofport
*ofport
;
1724 struct ofproto_port ofproto_port
;
1725 const char *devname
;
1727 COVERAGE_INC(ofproto_reinit_ports
);
1729 sset_init(&devnames
);
1730 HMAP_FOR_EACH (ofport
, hmap_node
, &p
->ports
) {
1731 sset_add(&devnames
, netdev_get_name(ofport
->netdev
));
1733 OFPROTO_PORT_FOR_EACH (&ofproto_port
, &dump
, p
) {
1734 sset_add(&devnames
, ofproto_port
.name
);
1737 SSET_FOR_EACH (devname
, &devnames
) {
1738 update_port(p
, devname
);
1740 sset_destroy(&devnames
);
1744 alloc_ofp_port(struct ofproto
*ofproto
, const char *netdev_name
)
1748 port_idx
= simap_get(&ofproto
->ofp_requests
, netdev_name
);
1749 port_idx
= port_idx
? port_idx
: UINT16_MAX
;
1751 if (port_idx
>= ofproto
->max_ports
1752 || bitmap_is_set(ofproto
->ofp_port_ids
, port_idx
)) {
1753 uint16_t end_port_no
= ofproto
->alloc_port_no
;
1755 /* Search for a free OpenFlow port number. We try not to
1756 * immediately reuse them to prevent problems due to old
1759 if (++ofproto
->alloc_port_no
>= ofproto
->max_ports
) {
1760 ofproto
->alloc_port_no
= 0;
1762 if (!bitmap_is_set(ofproto
->ofp_port_ids
,
1763 ofproto
->alloc_port_no
)) {
1764 port_idx
= ofproto
->alloc_port_no
;
1767 if (ofproto
->alloc_port_no
== end_port_no
) {
1772 bitmap_set1(ofproto
->ofp_port_ids
, port_idx
);
1773 return u16_to_ofp(port_idx
);
1777 dealloc_ofp_port(const struct ofproto
*ofproto
, ofp_port_t ofp_port
)
1779 if (ofp_to_u16(ofp_port
) < ofproto
->max_ports
) {
1780 bitmap_set0(ofproto
->ofp_port_ids
, ofp_to_u16(ofp_port
));
1784 /* Opens and returns a netdev for 'ofproto_port' in 'ofproto', or a null
1785 * pointer if the netdev cannot be opened. On success, also fills in
1787 static struct netdev
*
1788 ofport_open(struct ofproto
*ofproto
,
1789 struct ofproto_port
*ofproto_port
,
1790 struct ofputil_phy_port
*pp
)
1792 enum netdev_flags flags
;
1793 struct netdev
*netdev
;
1796 error
= netdev_open(ofproto_port
->name
, ofproto_port
->type
, &netdev
);
1798 VLOG_WARN_RL(&rl
, "%s: ignoring port %s (%"PRIu16
") because netdev %s "
1799 "cannot be opened (%s)",
1801 ofproto_port
->name
, ofproto_port
->ofp_port
,
1802 ofproto_port
->name
, ovs_strerror(error
));
1806 if (ofproto_port
->ofp_port
== OFPP_NONE
) {
1807 if (!strcmp(ofproto
->name
, ofproto_port
->name
)) {
1808 ofproto_port
->ofp_port
= OFPP_LOCAL
;
1810 ofproto_port
->ofp_port
= alloc_ofp_port(ofproto
,
1811 ofproto_port
->name
);
1814 pp
->port_no
= ofproto_port
->ofp_port
;
1815 netdev_get_etheraddr(netdev
, pp
->hw_addr
);
1816 ovs_strlcpy(pp
->name
, ofproto_port
->name
, sizeof pp
->name
);
1817 netdev_get_flags(netdev
, &flags
);
1818 pp
->config
= flags
& NETDEV_UP
? 0 : OFPUTIL_PC_PORT_DOWN
;
1819 pp
->state
= netdev_get_carrier(netdev
) ? 0 : OFPUTIL_PS_LINK_DOWN
;
1820 netdev_get_features(netdev
, &pp
->curr
, &pp
->advertised
,
1821 &pp
->supported
, &pp
->peer
);
1822 pp
->curr_speed
= netdev_features_to_bps(pp
->curr
, 0);
1823 pp
->max_speed
= netdev_features_to_bps(pp
->supported
, 0);
1828 /* Returns true if most fields of 'a' and 'b' are equal. Differences in name,
1829 * port number, and 'config' bits other than OFPUTIL_PS_LINK_DOWN are
1832 ofport_equal(const struct ofputil_phy_port
*a
,
1833 const struct ofputil_phy_port
*b
)
1835 return (eth_addr_equals(a
->hw_addr
, b
->hw_addr
)
1836 && a
->state
== b
->state
1837 && !((a
->config
^ b
->config
) & OFPUTIL_PC_PORT_DOWN
)
1838 && a
->curr
== b
->curr
1839 && a
->advertised
== b
->advertised
1840 && a
->supported
== b
->supported
1841 && a
->peer
== b
->peer
1842 && a
->curr_speed
== b
->curr_speed
1843 && a
->max_speed
== b
->max_speed
);
1846 /* Adds an ofport to 'p' initialized based on the given 'netdev' and 'opp'.
1847 * The caller must ensure that 'p' does not have a conflicting ofport (that is,
1848 * one with the same name or port number). */
1850 ofport_install(struct ofproto
*p
,
1851 struct netdev
*netdev
, const struct ofputil_phy_port
*pp
)
1853 const char *netdev_name
= netdev_get_name(netdev
);
1854 struct ofport
*ofport
;
1857 /* Create ofport. */
1858 ofport
= p
->ofproto_class
->port_alloc();
1863 ofport
->ofproto
= p
;
1864 ofport
->netdev
= netdev
;
1865 ofport
->change_seq
= netdev_change_seq(netdev
);
1867 ofport
->ofp_port
= pp
->port_no
;
1868 ofport
->created
= time_msec();
1870 /* Add port to 'p'. */
1871 hmap_insert(&p
->ports
, &ofport
->hmap_node
,
1872 hash_ofp_port(ofport
->ofp_port
));
1873 shash_add(&p
->port_by_name
, netdev_name
, ofport
);
1875 update_mtu(p
, ofport
);
1877 /* Let the ofproto_class initialize its private data. */
1878 error
= p
->ofproto_class
->port_construct(ofport
);
1882 connmgr_send_port_status(p
->connmgr
, pp
, OFPPR_ADD
);
1886 VLOG_WARN_RL(&rl
, "%s: could not add port %s (%s)",
1887 p
->name
, netdev_name
, ovs_strerror(error
));
1889 ofport_destroy__(ofport
);
1891 netdev_close(netdev
);
1895 /* Removes 'ofport' from 'p' and destroys it. */
1897 ofport_remove(struct ofport
*ofport
)
1899 connmgr_send_port_status(ofport
->ofproto
->connmgr
, &ofport
->pp
,
1901 ofport_destroy(ofport
);
1904 /* If 'ofproto' contains an ofport named 'name', removes it from 'ofproto' and
1907 ofport_remove_with_name(struct ofproto
*ofproto
, const char *name
)
1909 struct ofport
*port
= shash_find_data(&ofproto
->port_by_name
, name
);
1911 ofport_remove(port
);
1915 /* Updates 'port' with new 'pp' description.
1917 * Does not handle a name or port number change. The caller must implement
1918 * such a change as a delete followed by an add. */
1920 ofport_modified(struct ofport
*port
, struct ofputil_phy_port
*pp
)
1922 memcpy(port
->pp
.hw_addr
, pp
->hw_addr
, ETH_ADDR_LEN
);
1923 port
->pp
.config
= ((port
->pp
.config
& ~OFPUTIL_PC_PORT_DOWN
)
1924 | (pp
->config
& OFPUTIL_PC_PORT_DOWN
));
1925 port
->pp
.state
= pp
->state
;
1926 port
->pp
.curr
= pp
->curr
;
1927 port
->pp
.advertised
= pp
->advertised
;
1928 port
->pp
.supported
= pp
->supported
;
1929 port
->pp
.peer
= pp
->peer
;
1930 port
->pp
.curr_speed
= pp
->curr_speed
;
1931 port
->pp
.max_speed
= pp
->max_speed
;
1933 connmgr_send_port_status(port
->ofproto
->connmgr
, &port
->pp
, OFPPR_MODIFY
);
1936 /* Update OpenFlow 'state' in 'port' and notify controller. */
1938 ofproto_port_set_state(struct ofport
*port
, enum ofputil_port_state state
)
1940 if (port
->pp
.state
!= state
) {
1941 port
->pp
.state
= state
;
1942 connmgr_send_port_status(port
->ofproto
->connmgr
, &port
->pp
,
1948 ofproto_port_unregister(struct ofproto
*ofproto
, ofp_port_t ofp_port
)
1950 struct ofport
*port
= ofproto_get_port(ofproto
, ofp_port
);
1952 if (port
->ofproto
->ofproto_class
->set_realdev
) {
1953 port
->ofproto
->ofproto_class
->set_realdev(port
, 0, 0);
1955 if (port
->ofproto
->ofproto_class
->set_stp_port
) {
1956 port
->ofproto
->ofproto_class
->set_stp_port(port
, NULL
);
1958 if (port
->ofproto
->ofproto_class
->set_cfm
) {
1959 port
->ofproto
->ofproto_class
->set_cfm(port
, NULL
);
1961 if (port
->ofproto
->ofproto_class
->bundle_remove
) {
1962 port
->ofproto
->ofproto_class
->bundle_remove(port
);
1968 ofport_destroy__(struct ofport
*port
)
1970 struct ofproto
*ofproto
= port
->ofproto
;
1971 const char *name
= netdev_get_name(port
->netdev
);
1973 hmap_remove(&ofproto
->ports
, &port
->hmap_node
);
1974 shash_delete(&ofproto
->port_by_name
,
1975 shash_find(&ofproto
->port_by_name
, name
));
1977 netdev_close(port
->netdev
);
1978 ofproto
->ofproto_class
->port_dealloc(port
);
1982 ofport_destroy(struct ofport
*port
)
1985 dealloc_ofp_port(port
->ofproto
, port
->ofp_port
);
1986 port
->ofproto
->ofproto_class
->port_destruct(port
);
1987 ofport_destroy__(port
);
1992 ofproto_get_port(const struct ofproto
*ofproto
, ofp_port_t ofp_port
)
1994 struct ofport
*port
;
1996 HMAP_FOR_EACH_IN_BUCKET (port
, hmap_node
, hash_ofp_port(ofp_port
),
1998 if (port
->ofp_port
== ofp_port
) {
2006 ofproto_port_get_stats(const struct ofport
*port
, struct netdev_stats
*stats
)
2008 struct ofproto
*ofproto
= port
->ofproto
;
2011 if (ofproto
->ofproto_class
->port_get_stats
) {
2012 error
= ofproto
->ofproto_class
->port_get_stats(port
, stats
);
2021 update_port(struct ofproto
*ofproto
, const char *name
)
2023 struct ofproto_port ofproto_port
;
2024 struct ofputil_phy_port pp
;
2025 struct netdev
*netdev
;
2026 struct ofport
*port
;
2028 COVERAGE_INC(ofproto_update_port
);
2030 /* Fetch 'name''s location and properties from the datapath. */
2031 netdev
= (!ofproto_port_query_by_name(ofproto
, name
, &ofproto_port
)
2032 ? ofport_open(ofproto
, &ofproto_port
, &pp
)
2036 port
= ofproto_get_port(ofproto
, ofproto_port
.ofp_port
);
2037 if (port
&& !strcmp(netdev_get_name(port
->netdev
), name
)) {
2038 struct netdev
*old_netdev
= port
->netdev
;
2040 /* 'name' hasn't changed location. Any properties changed? */
2041 if (!ofport_equal(&port
->pp
, &pp
)) {
2042 ofport_modified(port
, &pp
);
2045 update_mtu(ofproto
, port
);
2047 /* Install the newly opened netdev in case it has changed.
2048 * Don't close the old netdev yet in case port_modified has to
2049 * remove a retained reference to it.*/
2050 port
->netdev
= netdev
;
2051 port
->change_seq
= netdev_change_seq(netdev
);
2053 if (port
->ofproto
->ofproto_class
->port_modified
) {
2054 port
->ofproto
->ofproto_class
->port_modified(port
);
2057 netdev_close(old_netdev
);
2059 /* If 'port' is nonnull then its name differs from 'name' and thus
2060 * we should delete it. If we think there's a port named 'name'
2061 * then its port number must be wrong now so delete it too. */
2063 ofport_remove(port
);
2065 ofport_remove_with_name(ofproto
, name
);
2066 ofport_install(ofproto
, netdev
, &pp
);
2069 /* Any port named 'name' is gone now. */
2070 ofport_remove_with_name(ofproto
, name
);
2072 ofproto_port_destroy(&ofproto_port
);
2076 init_ports(struct ofproto
*p
)
2078 struct ofproto_port_dump dump
;
2079 struct ofproto_port ofproto_port
;
2080 struct shash_node
*node
, *next
;
2082 OFPROTO_PORT_FOR_EACH (&ofproto_port
, &dump
, p
) {
2083 const char *name
= ofproto_port
.name
;
2085 if (shash_find(&p
->port_by_name
, name
)) {
2086 VLOG_WARN_RL(&rl
, "%s: ignoring duplicate device %s in datapath",
2089 struct ofputil_phy_port pp
;
2090 struct netdev
*netdev
;
2092 /* Check if an OpenFlow port number had been requested. */
2093 node
= shash_find(&init_ofp_ports
, name
);
2095 const struct iface_hint
*iface_hint
= node
->data
;
2096 simap_put(&p
->ofp_requests
, name
,
2097 ofp_to_u16(iface_hint
->ofp_port
));
2100 netdev
= ofport_open(p
, &ofproto_port
, &pp
);
2102 ofport_install(p
, netdev
, &pp
);
2103 if (ofp_to_u16(ofproto_port
.ofp_port
) < p
->max_ports
) {
2104 p
->alloc_port_no
= MAX(p
->alloc_port_no
,
2105 ofp_to_u16(ofproto_port
.ofp_port
));
2111 SHASH_FOR_EACH_SAFE(node
, next
, &init_ofp_ports
) {
2112 struct iface_hint
*iface_hint
= node
->data
;
2114 if (!strcmp(iface_hint
->br_name
, p
->name
)) {
2115 free(iface_hint
->br_name
);
2116 free(iface_hint
->br_type
);
2118 shash_delete(&init_ofp_ports
, node
);
2125 /* Find the minimum MTU of all non-datapath devices attached to 'p'.
2126 * Returns ETH_PAYLOAD_MAX or the minimum of the ports. */
2128 find_min_mtu(struct ofproto
*p
)
2130 struct ofport
*ofport
;
2133 HMAP_FOR_EACH (ofport
, hmap_node
, &p
->ports
) {
2134 struct netdev
*netdev
= ofport
->netdev
;
2137 /* Skip any internal ports, since that's what we're trying to
2139 if (!strcmp(netdev_get_type(netdev
), "internal")) {
2143 if (netdev_get_mtu(netdev
, &dev_mtu
)) {
2146 if (!mtu
|| dev_mtu
< mtu
) {
2151 return mtu
? mtu
: ETH_PAYLOAD_MAX
;
2154 /* Update MTU of all datapath devices on 'p' to the minimum of the
2155 * non-datapath ports in event of 'port' added or changed. */
2157 update_mtu(struct ofproto
*p
, struct ofport
*port
)
2159 struct ofport
*ofport
;
2160 struct netdev
*netdev
= port
->netdev
;
2161 int dev_mtu
, old_min
;
2163 if (netdev_get_mtu(netdev
, &dev_mtu
)) {
2167 if (!strcmp(netdev_get_type(port
->netdev
), "internal")) {
2168 if (dev_mtu
> p
->min_mtu
) {
2169 if (!netdev_set_mtu(port
->netdev
, p
->min_mtu
)) {
2170 dev_mtu
= p
->min_mtu
;
2173 port
->mtu
= dev_mtu
;
2177 /* For non-internal port find new min mtu. */
2178 old_min
= p
->min_mtu
;
2179 port
->mtu
= dev_mtu
;
2180 p
->min_mtu
= find_min_mtu(p
);
2181 if (p
->min_mtu
== old_min
) {
2185 HMAP_FOR_EACH (ofport
, hmap_node
, &p
->ports
) {
2186 struct netdev
*netdev
= ofport
->netdev
;
2188 if (!strcmp(netdev_get_type(netdev
), "internal")) {
2189 if (!netdev_set_mtu(netdev
, p
->min_mtu
)) {
2190 ofport
->mtu
= p
->min_mtu
;
2197 ofproto_rule_destroy__(struct rule
*rule
)
2200 cls_rule_destroy(&rule
->cr
);
2201 free(rule
->ofpacts
);
2202 ovs_mutex_destroy(&rule
->timeout_mutex
);
2203 ovs_rwlock_destroy(&rule
->evict
);
2204 rule
->ofproto
->ofproto_class
->rule_dealloc(rule
);
2208 /* This function allows an ofproto implementation to destroy any rules that
2209 * remain when its ->destruct() function is called. The caller must have
2210 * already uninitialized any derived members of 'rule' (step 5 described in the
2211 * large comment in ofproto/ofproto-provider.h titled "Life Cycle").
2212 * This function implements steps 6 and 7.
2214 * This function should only be called from an ofproto implementation's
2215 * ->destruct() function. It is not suitable elsewhere. */
2217 ofproto_rule_destroy(struct ofproto
*ofproto
, struct classifier
*cls
,
2218 struct rule
*rule
) OVS_REQ_WRLOCK(cls
->rwlock
)
2220 ovs_assert(!rule
->pending
);
2221 if (!ovs_rwlock_trywrlock(&rule
->evict
)) {
2222 oftable_remove_rule__(ofproto
, cls
, rule
);
2226 ofproto_rule_destroy__(rule
);
2229 /* Returns true if 'rule' has an OpenFlow OFPAT_OUTPUT or OFPAT_ENQUEUE action
2230 * that outputs to 'port' (output to OFPP_FLOOD and OFPP_ALL doesn't count). */
2232 ofproto_rule_has_out_port(const struct rule
*rule
, ofp_port_t port
)
2234 return (port
== OFPP_ANY
2235 || ofpacts_output_to_port(rule
->ofpacts
, rule
->ofpacts_len
, port
));
2238 /* Returns true if a rule related to 'op' has an OpenFlow OFPAT_OUTPUT or
2239 * OFPAT_ENQUEUE action that outputs to 'out_port'. */
2241 ofoperation_has_out_port(const struct ofoperation
*op
, ofp_port_t out_port
)
2243 if (ofproto_rule_has_out_port(op
->rule
, out_port
)) {
2248 case OFOPERATION_ADD
:
2249 return op
->victim
&& ofproto_rule_has_out_port(op
->victim
, out_port
);
2251 case OFOPERATION_DELETE
:
2254 case OFOPERATION_MODIFY
:
2255 return ofpacts_output_to_port(op
->ofpacts
, op
->ofpacts_len
, out_port
);
2261 /* Executes the actions indicated by 'rule' on 'packet' and credits 'rule''s
2262 * statistics appropriately. 'packet' must have at least sizeof(struct
2263 * ofp10_packet_in) bytes of headroom.
2265 * 'packet' doesn't necessarily have to match 'rule'. 'rule' will be credited
2266 * with statistics for 'packet' either way.
2268 * Takes ownership of 'packet'. */
2270 rule_execute(struct rule
*rule
, ofp_port_t in_port
, struct ofpbuf
*packet
)
2273 union flow_in_port in_port_
;
2275 ovs_assert(ofpbuf_headroom(packet
) >= sizeof(struct ofp10_packet_in
));
2277 in_port_
.ofp_port
= in_port
;
2278 flow_extract(packet
, 0, 0, NULL
, &in_port_
, &flow
);
2279 return rule
->ofproto
->ofproto_class
->rule_execute(rule
, &flow
, packet
);
2282 /* Returns true if 'rule' should be hidden from the controller.
2284 * Rules with priority higher than UINT16_MAX are set up by ofproto itself
2285 * (e.g. by in-band control) and are intentionally hidden from the
2288 ofproto_rule_is_hidden(const struct rule
*rule
)
2290 return rule
->cr
.priority
> UINT16_MAX
;
2293 static enum oftable_flags
2294 rule_get_flags(const struct rule
*rule
)
2296 return rule
->ofproto
->tables
[rule
->table_id
].flags
;
2300 rule_is_modifiable(const struct rule
*rule
)
2302 return !(rule_get_flags(rule
) & OFTABLE_READONLY
);
2306 handle_echo_request(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
2308 ofconn_send_reply(ofconn
, make_echo_reply(oh
));
2313 handle_features_request(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
2315 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
2316 struct ofputil_switch_features features
;
2317 struct ofport
*port
;
2323 ofproto
->ofproto_class
->get_features(ofproto
, &arp_match_ip
,
2325 ovs_assert(features
.actions
& OFPUTIL_A_OUTPUT
); /* sanity check */
2327 /* Count only non-hidden tables in the number of tables. (Hidden tables,
2328 * if present, are always at the end.) */
2329 n_tables
= ofproto
->n_tables
;
2330 for (i
= 0; i
< ofproto
->n_tables
; i
++) {
2331 if (ofproto
->tables
[i
].flags
& OFTABLE_HIDDEN
) {
2337 features
.datapath_id
= ofproto
->datapath_id
;
2338 features
.n_buffers
= pktbuf_capacity();
2339 features
.n_tables
= n_tables
;
2340 features
.capabilities
= (OFPUTIL_C_FLOW_STATS
| OFPUTIL_C_TABLE_STATS
|
2341 OFPUTIL_C_PORT_STATS
| OFPUTIL_C_QUEUE_STATS
);
2343 features
.capabilities
|= OFPUTIL_C_ARP_MATCH_IP
;
2345 /* FIXME: Fill in proper features.auxiliary_id for auxiliary connections */
2346 features
.auxiliary_id
= 0;
2347 b
= ofputil_encode_switch_features(&features
, ofconn_get_protocol(ofconn
),
2349 HMAP_FOR_EACH (port
, hmap_node
, &ofproto
->ports
) {
2350 ofputil_put_switch_features_port(&port
->pp
, b
);
2353 ofconn_send_reply(ofconn
, b
);
2358 handle_get_config_request(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
2360 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
2361 struct ofp_switch_config
*osc
;
2362 enum ofp_config_flags flags
;
2366 buf
= ofpraw_alloc_reply(OFPRAW_OFPT_GET_CONFIG_REPLY
, oh
, 0);
2367 osc
= ofpbuf_put_uninit(buf
, sizeof *osc
);
2368 flags
= ofproto
->frag_handling
;
2369 /* OFPC_INVALID_TTL_TO_CONTROLLER is deprecated in OF 1.3 */
2370 if (oh
->version
< OFP13_VERSION
2371 && ofconn_get_invalid_ttl_to_controller(ofconn
)) {
2372 flags
|= OFPC_INVALID_TTL_TO_CONTROLLER
;
2374 osc
->flags
= htons(flags
);
2375 osc
->miss_send_len
= htons(ofconn_get_miss_send_len(ofconn
));
2376 ofconn_send_reply(ofconn
, buf
);
2382 handle_set_config(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
2384 const struct ofp_switch_config
*osc
= ofpmsg_body(oh
);
2385 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
2386 uint16_t flags
= ntohs(osc
->flags
);
2388 if (ofconn_get_type(ofconn
) != OFCONN_PRIMARY
2389 || ofconn_get_role(ofconn
) != OFPCR12_ROLE_SLAVE
) {
2390 enum ofp_config_flags cur
= ofproto
->frag_handling
;
2391 enum ofp_config_flags next
= flags
& OFPC_FRAG_MASK
;
2393 ovs_assert((cur
& OFPC_FRAG_MASK
) == cur
);
2395 if (ofproto
->ofproto_class
->set_frag_handling(ofproto
, next
)) {
2396 ofproto
->frag_handling
= next
;
2398 VLOG_WARN_RL(&rl
, "%s: unsupported fragment handling mode %s",
2400 ofputil_frag_handling_to_string(next
));
2404 /* OFPC_INVALID_TTL_TO_CONTROLLER is deprecated in OF 1.3 */
2405 ofconn_set_invalid_ttl_to_controller(ofconn
,
2406 (oh
->version
< OFP13_VERSION
2407 && flags
& OFPC_INVALID_TTL_TO_CONTROLLER
));
2409 ofconn_set_miss_send_len(ofconn
, ntohs(osc
->miss_send_len
));
2414 /* Checks whether 'ofconn' is a slave controller. If so, returns an OpenFlow
2415 * error message code for the caller to propagate upward. Otherwise, returns
2418 * The log message mentions 'msg_type'. */
2420 reject_slave_controller(struct ofconn
*ofconn
)
2422 if (ofconn_get_type(ofconn
) == OFCONN_PRIMARY
2423 && ofconn_get_role(ofconn
) == OFPCR12_ROLE_SLAVE
) {
2424 return OFPERR_OFPBRC_EPERM
;
2430 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
2431 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
2433 * This function relies on the order of 'ofpacts' being correct (as checked by
2434 * ofpacts_verify()). */
2436 find_meter(const struct ofpact ofpacts
[], size_t ofpacts_len
)
2438 const struct ofpact
*a
;
2440 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
2441 enum ovs_instruction_type inst
;
2443 inst
= ovs_instruction_type_from_ofpact_type(a
->type
);
2444 if (a
->type
== OFPACT_METER
) {
2445 return ofpact_get_METER(a
)->meter_id
;
2446 } else if (inst
> OVSINST_OFPIT13_METER
) {
2454 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are appropriate
2455 * for a packet with the prerequisites satisfied by 'flow' in table 'table_id'.
2456 * 'flow' may be temporarily modified, but is restored at return.
2459 ofproto_check_ofpacts(struct ofproto
*ofproto
,
2460 const struct ofpact ofpacts
[], size_t ofpacts_len
,
2461 struct flow
*flow
, uint8_t table_id
)
2466 error
= ofpacts_check(ofpacts
, ofpacts_len
, flow
,
2467 u16_to_ofp(ofproto
->max_ports
), table_id
);
2472 mid
= find_meter(ofpacts
, ofpacts_len
);
2473 if (mid
&& ofproto_get_provider_meter_id(ofproto
, mid
) == UINT32_MAX
) {
2474 return OFPERR_OFPMMFC_INVALID_METER
;
2480 handle_packet_out(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
2482 struct ofproto
*p
= ofconn_get_ofproto(ofconn
);
2483 struct ofputil_packet_out po
;
2484 struct ofpbuf
*payload
;
2485 uint64_t ofpacts_stub
[1024 / 8];
2486 struct ofpbuf ofpacts
;
2488 union flow_in_port in_port_
;
2491 COVERAGE_INC(ofproto_packet_out
);
2493 error
= reject_slave_controller(ofconn
);
2498 /* Decode message. */
2499 ofpbuf_use_stub(&ofpacts
, ofpacts_stub
, sizeof ofpacts_stub
);
2500 error
= ofputil_decode_packet_out(&po
, oh
, &ofpacts
);
2502 goto exit_free_ofpacts
;
2504 if (ofp_to_u16(po
.in_port
) >= p
->max_ports
2505 && ofp_to_u16(po
.in_port
) < ofp_to_u16(OFPP_MAX
)) {
2506 error
= OFPERR_OFPBRC_BAD_PORT
;
2507 goto exit_free_ofpacts
;
2512 if (po
.buffer_id
!= UINT32_MAX
) {
2513 error
= ofconn_pktbuf_retrieve(ofconn
, po
.buffer_id
, &payload
, NULL
);
2514 if (error
|| !payload
) {
2515 goto exit_free_ofpacts
;
2518 payload
= xmalloc(sizeof *payload
);
2519 ofpbuf_use_const(payload
, po
.packet
, po
.packet_len
);
2522 /* Verify actions against packet, then send packet if successful. */
2523 in_port_
.ofp_port
= po
.in_port
;
2524 flow_extract(payload
, 0, 0, NULL
, &in_port_
, &flow
);
2525 error
= ofproto_check_ofpacts(p
, po
.ofpacts
, po
.ofpacts_len
, &flow
, 0);
2527 error
= p
->ofproto_class
->packet_out(p
, payload
, &flow
,
2528 po
.ofpacts
, po
.ofpacts_len
);
2530 ofpbuf_delete(payload
);
2533 ofpbuf_uninit(&ofpacts
);
2539 update_port_config(struct ofport
*port
,
2540 enum ofputil_port_config config
,
2541 enum ofputil_port_config mask
)
2543 enum ofputil_port_config old_config
= port
->pp
.config
;
2544 enum ofputil_port_config toggle
;
2546 toggle
= (config
^ port
->pp
.config
) & mask
;
2547 if (toggle
& OFPUTIL_PC_PORT_DOWN
) {
2548 if (config
& OFPUTIL_PC_PORT_DOWN
) {
2549 netdev_turn_flags_off(port
->netdev
, NETDEV_UP
, NULL
);
2551 netdev_turn_flags_on(port
->netdev
, NETDEV_UP
, NULL
);
2553 toggle
&= ~OFPUTIL_PC_PORT_DOWN
;
2556 port
->pp
.config
^= toggle
;
2557 if (port
->pp
.config
!= old_config
) {
2558 port
->ofproto
->ofproto_class
->port_reconfigured(port
, old_config
);
2563 handle_port_mod(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
2565 struct ofproto
*p
= ofconn_get_ofproto(ofconn
);
2566 struct ofputil_port_mod pm
;
2567 struct ofport
*port
;
2570 error
= reject_slave_controller(ofconn
);
2575 error
= ofputil_decode_port_mod(oh
, &pm
);
2580 port
= ofproto_get_port(p
, pm
.port_no
);
2582 return OFPERR_OFPPMFC_BAD_PORT
;
2583 } else if (!eth_addr_equals(port
->pp
.hw_addr
, pm
.hw_addr
)) {
2584 return OFPERR_OFPPMFC_BAD_HW_ADDR
;
2586 update_port_config(port
, pm
.config
, pm
.mask
);
2588 netdev_set_advertisements(port
->netdev
, pm
.advertise
);
2595 handle_desc_stats_request(struct ofconn
*ofconn
,
2596 const struct ofp_header
*request
)
2598 static const char *default_mfr_desc
= "Nicira, Inc.";
2599 static const char *default_hw_desc
= "Open vSwitch";
2600 static const char *default_sw_desc
= VERSION
;
2601 static const char *default_serial_desc
= "None";
2602 static const char *default_dp_desc
= "None";
2604 struct ofproto
*p
= ofconn_get_ofproto(ofconn
);
2605 struct ofp_desc_stats
*ods
;
2608 msg
= ofpraw_alloc_stats_reply(request
, 0);
2609 ods
= ofpbuf_put_zeros(msg
, sizeof *ods
);
2610 ovs_strlcpy(ods
->mfr_desc
, p
->mfr_desc
? p
->mfr_desc
: default_mfr_desc
,
2611 sizeof ods
->mfr_desc
);
2612 ovs_strlcpy(ods
->hw_desc
, p
->hw_desc
? p
->hw_desc
: default_hw_desc
,
2613 sizeof ods
->hw_desc
);
2614 ovs_strlcpy(ods
->sw_desc
, p
->sw_desc
? p
->sw_desc
: default_sw_desc
,
2615 sizeof ods
->sw_desc
);
2616 ovs_strlcpy(ods
->serial_num
,
2617 p
->serial_desc
? p
->serial_desc
: default_serial_desc
,
2618 sizeof ods
->serial_num
);
2619 ovs_strlcpy(ods
->dp_desc
, p
->dp_desc
? p
->dp_desc
: default_dp_desc
,
2620 sizeof ods
->dp_desc
);
2621 ofconn_send_reply(ofconn
, msg
);
2627 handle_table_stats_request(struct ofconn
*ofconn
,
2628 const struct ofp_header
*request
)
2630 struct ofproto
*p
= ofconn_get_ofproto(ofconn
);
2631 struct ofp12_table_stats
*ots
;
2636 /* Set up default values.
2638 * ofp12_table_stats is used as a generic structure as
2639 * it is able to hold all the fields for ofp10_table_stats
2640 * and ofp11_table_stats (and of course itself).
2642 ots
= xcalloc(p
->n_tables
, sizeof *ots
);
2643 for (i
= 0; i
< p
->n_tables
; i
++) {
2644 ots
[i
].table_id
= i
;
2645 sprintf(ots
[i
].name
, "table%zu", i
);
2646 ots
[i
].match
= htonll(OFPXMT12_MASK
);
2647 ots
[i
].wildcards
= htonll(OFPXMT12_MASK
);
2648 ots
[i
].write_actions
= htonl(OFPAT11_OUTPUT
);
2649 ots
[i
].apply_actions
= htonl(OFPAT11_OUTPUT
);
2650 ots
[i
].write_setfields
= htonll(OFPXMT12_MASK
);
2651 ots
[i
].apply_setfields
= htonll(OFPXMT12_MASK
);
2652 ots
[i
].metadata_match
= htonll(UINT64_MAX
);
2653 ots
[i
].metadata_write
= htonll(UINT64_MAX
);
2654 ots
[i
].instructions
= htonl(OFPIT11_ALL
);
2655 ots
[i
].config
= htonl(OFPTC11_TABLE_MISS_MASK
);
2656 ots
[i
].max_entries
= htonl(1000000); /* An arbitrary big number. */
2657 ovs_rwlock_rdlock(&p
->tables
[i
].cls
.rwlock
);
2658 ots
[i
].active_count
= htonl(classifier_count(&p
->tables
[i
].cls
));
2659 ovs_rwlock_unlock(&p
->tables
[i
].cls
.rwlock
);
2662 p
->ofproto_class
->get_tables(p
, ots
);
2664 /* Post-process the tables, dropping hidden tables. */
2665 n_tables
= p
->n_tables
;
2666 for (i
= 0; i
< p
->n_tables
; i
++) {
2667 const struct oftable
*table
= &p
->tables
[i
];
2669 if (table
->flags
& OFTABLE_HIDDEN
) {
2675 ovs_strzcpy(ots
[i
].name
, table
->name
, sizeof ots
[i
].name
);
2678 if (table
->max_flows
< ntohl(ots
[i
].max_entries
)) {
2679 ots
[i
].max_entries
= htonl(table
->max_flows
);
2683 msg
= ofputil_encode_table_stats_reply(ots
, n_tables
, request
);
2684 ofconn_send_reply(ofconn
, msg
);
2692 append_port_stat(struct ofport
*port
, struct list
*replies
)
2694 struct ofputil_port_stats ops
= { .port_no
= port
->pp
.port_no
};
2696 calc_duration(port
->created
, time_msec(),
2697 &ops
.duration_sec
, &ops
.duration_nsec
);
2699 /* Intentionally ignore return value, since errors will set
2700 * 'stats' to all-1s, which is correct for OpenFlow, and
2701 * netdev_get_stats() will log errors. */
2702 ofproto_port_get_stats(port
, &ops
.stats
);
2704 ofputil_append_port_stat(replies
, &ops
);
2708 handle_port_stats_request(struct ofconn
*ofconn
,
2709 const struct ofp_header
*request
)
2711 struct ofproto
*p
= ofconn_get_ofproto(ofconn
);
2712 struct ofport
*port
;
2713 struct list replies
;
2717 error
= ofputil_decode_port_stats_request(request
, &port_no
);
2722 ofpmp_init(&replies
, request
);
2723 if (port_no
!= OFPP_ANY
) {
2724 port
= ofproto_get_port(p
, port_no
);
2726 append_port_stat(port
, &replies
);
2729 HMAP_FOR_EACH (port
, hmap_node
, &p
->ports
) {
2730 append_port_stat(port
, &replies
);
2734 ofconn_send_replies(ofconn
, &replies
);
2739 handle_port_desc_stats_request(struct ofconn
*ofconn
,
2740 const struct ofp_header
*request
)
2742 struct ofproto
*p
= ofconn_get_ofproto(ofconn
);
2743 enum ofp_version version
;
2744 struct ofport
*port
;
2745 struct list replies
;
2747 ofpmp_init(&replies
, request
);
2749 version
= ofputil_protocol_to_ofp_version(ofconn_get_protocol(ofconn
));
2750 HMAP_FOR_EACH (port
, hmap_node
, &p
->ports
) {
2751 ofputil_append_port_desc_stats_reply(version
, &port
->pp
, &replies
);
2754 ofconn_send_replies(ofconn
, &replies
);
2759 hash_cookie(ovs_be64 cookie
)
2761 return hash_2words((OVS_FORCE
uint64_t)cookie
>> 32,
2762 (OVS_FORCE
uint64_t)cookie
);
2766 cookies_insert(struct ofproto
*ofproto
, struct rule
*rule
)
2768 hindex_insert(&ofproto
->cookies
, &rule
->cookie_node
,
2769 hash_cookie(rule
->flow_cookie
));
2773 cookies_remove(struct ofproto
*ofproto
, struct rule
*rule
)
2775 hindex_remove(&ofproto
->cookies
, &rule
->cookie_node
);
2779 ofproto_rule_change_cookie(struct ofproto
*ofproto
, struct rule
*rule
,
2780 ovs_be64 new_cookie
)
2782 if (new_cookie
!= rule
->flow_cookie
) {
2783 cookies_remove(ofproto
, rule
);
2785 rule
->flow_cookie
= new_cookie
;
2787 cookies_insert(ofproto
, rule
);
2792 calc_duration(long long int start
, long long int now
,
2793 uint32_t *sec
, uint32_t *nsec
)
2795 long long int msecs
= now
- start
;
2796 *sec
= msecs
/ 1000;
2797 *nsec
= (msecs
% 1000) * (1000 * 1000);
2800 /* Checks whether 'table_id' is 0xff or a valid table ID in 'ofproto'. Returns
2801 * 0 if 'table_id' is OK, otherwise an OpenFlow error code. */
2803 check_table_id(const struct ofproto
*ofproto
, uint8_t table_id
)
2805 return (table_id
== 0xff || table_id
< ofproto
->n_tables
2807 : OFPERR_OFPBRC_BAD_TABLE_ID
);
2811 static struct oftable
*
2812 next_visible_table(const struct ofproto
*ofproto
, uint8_t table_id
)
2814 struct oftable
*table
;
2816 for (table
= &ofproto
->tables
[table_id
];
2817 table
< &ofproto
->tables
[ofproto
->n_tables
];
2819 if (!(table
->flags
& OFTABLE_HIDDEN
)) {
2827 static struct oftable
*
2828 first_matching_table(const struct ofproto
*ofproto
, uint8_t table_id
)
2830 if (table_id
== 0xff) {
2831 return next_visible_table(ofproto
, 0);
2832 } else if (table_id
< ofproto
->n_tables
) {
2833 return &ofproto
->tables
[table_id
];
2839 static struct oftable
*
2840 next_matching_table(const struct ofproto
*ofproto
,
2841 const struct oftable
*table
, uint8_t table_id
)
2843 return (table_id
== 0xff
2844 ? next_visible_table(ofproto
, (table
- ofproto
->tables
) + 1)
2848 /* Assigns TABLE to each oftable, in turn, that matches TABLE_ID in OFPROTO:
2850 * - If TABLE_ID is 0xff, this iterates over every classifier table in
2851 * OFPROTO, skipping tables marked OFTABLE_HIDDEN.
2853 * - If TABLE_ID is the number of a table in OFPROTO, then the loop iterates
2854 * only once, for that table. (This can be used to access tables marked
2857 * - Otherwise, TABLE_ID isn't valid for OFPROTO, so the loop won't be
2858 * entered at all. (Perhaps you should have validated TABLE_ID with
2859 * check_table_id().)
2861 * All parameters are evaluated multiple times.
2863 #define FOR_EACH_MATCHING_TABLE(TABLE, TABLE_ID, OFPROTO) \
2864 for ((TABLE) = first_matching_table(OFPROTO, TABLE_ID); \
2866 (TABLE) = next_matching_table(OFPROTO, TABLE, TABLE_ID))
2868 /* Searches 'ofproto' for rules in table 'table_id' (or in all tables, if
2869 * 'table_id' is 0xff) that match 'match' in the "loose" way required for
2870 * OpenFlow OFPFC_MODIFY and OFPFC_DELETE requests and puts them on list
2873 * If 'out_port' is anything other than OFPP_ANY, then only rules that output
2874 * to 'out_port' are included.
2876 * Hidden rules are always omitted.
2878 * Returns 0 on success, otherwise an OpenFlow error code. */
2880 collect_rules_loose(struct ofproto
*ofproto
, uint8_t table_id
,
2881 const struct match
*match
,
2882 ovs_be64 cookie
, ovs_be64 cookie_mask
,
2883 ofp_port_t out_port
, struct list
*rules
)
2885 struct oftable
*table
;
2889 error
= check_table_id(ofproto
, table_id
);
2895 cls_rule_init(&cr
, match
, 0);
2897 if (cookie_mask
== htonll(UINT64_MAX
)) {
2900 HINDEX_FOR_EACH_WITH_HASH (rule
, cookie_node
, hash_cookie(cookie
),
2901 &ofproto
->cookies
) {
2902 if (table_id
!= rule
->table_id
&& table_id
!= 0xff) {
2905 if (ofproto_rule_is_hidden(rule
)) {
2908 if (cls_rule_is_loose_match(&rule
->cr
, &cr
.match
)) {
2909 if (rule
->pending
) {
2910 error
= OFPROTO_POSTPONE
;
2913 if (rule
->flow_cookie
== cookie
/* Hash collisions possible. */
2914 && ofproto_rule_has_out_port(rule
, out_port
)) {
2915 list_push_back(rules
, &rule
->ofproto_node
);
2922 FOR_EACH_MATCHING_TABLE (table
, table_id
, ofproto
) {
2923 struct cls_cursor cursor
;
2926 ovs_rwlock_rdlock(&table
->cls
.rwlock
);
2927 cls_cursor_init(&cursor
, &table
->cls
, &cr
);
2928 CLS_CURSOR_FOR_EACH (rule
, cr
, &cursor
) {
2929 if (rule
->pending
) {
2930 ovs_rwlock_unlock(&table
->cls
.rwlock
);
2931 error
= OFPROTO_POSTPONE
;
2934 if (!ofproto_rule_is_hidden(rule
)
2935 && ofproto_rule_has_out_port(rule
, out_port
)
2936 && !((rule
->flow_cookie
^ cookie
) & cookie_mask
)) {
2937 list_push_back(rules
, &rule
->ofproto_node
);
2940 ovs_rwlock_unlock(&table
->cls
.rwlock
);
2944 cls_rule_destroy(&cr
);
2948 /* Searches 'ofproto' for rules in table 'table_id' (or in all tables, if
2949 * 'table_id' is 0xff) that match 'match' in the "strict" way required for
2950 * OpenFlow OFPFC_MODIFY_STRICT and OFPFC_DELETE_STRICT requests and puts them
2953 * If 'out_port' is anything other than OFPP_ANY, then only rules that output
2954 * to 'out_port' are included.
2956 * Hidden rules are always omitted.
2958 * Returns 0 on success, otherwise an OpenFlow error code. */
2960 collect_rules_strict(struct ofproto
*ofproto
, uint8_t table_id
,
2961 const struct match
*match
, unsigned int priority
,
2962 ovs_be64 cookie
, ovs_be64 cookie_mask
,
2963 ofp_port_t out_port
, struct list
*rules
)
2965 struct oftable
*table
;
2969 error
= check_table_id(ofproto
, table_id
);
2975 cls_rule_init(&cr
, match
, priority
);
2977 if (cookie_mask
== htonll(UINT64_MAX
)) {
2980 HINDEX_FOR_EACH_WITH_HASH (rule
, cookie_node
, hash_cookie(cookie
),
2981 &ofproto
->cookies
) {
2982 if (table_id
!= rule
->table_id
&& table_id
!= 0xff) {
2985 if (ofproto_rule_is_hidden(rule
)) {
2988 if (cls_rule_equal(&rule
->cr
, &cr
)) {
2989 if (rule
->pending
) {
2990 error
= OFPROTO_POSTPONE
;
2993 if (rule
->flow_cookie
== cookie
/* Hash collisions possible. */
2994 && ofproto_rule_has_out_port(rule
, out_port
)) {
2995 list_push_back(rules
, &rule
->ofproto_node
);
3002 FOR_EACH_MATCHING_TABLE (table
, table_id
, ofproto
) {
3005 ovs_rwlock_rdlock(&table
->cls
.rwlock
);
3006 rule
= rule_from_cls_rule(classifier_find_rule_exactly(&table
->cls
,
3008 ovs_rwlock_unlock(&table
->cls
.rwlock
);
3010 if (rule
->pending
) {
3011 error
= OFPROTO_POSTPONE
;
3014 if (!ofproto_rule_is_hidden(rule
)
3015 && ofproto_rule_has_out_port(rule
, out_port
)
3016 && !((rule
->flow_cookie
^ cookie
) & cookie_mask
)) {
3017 list_push_back(rules
, &rule
->ofproto_node
);
3023 cls_rule_destroy(&cr
);
3027 /* Returns 'age_ms' (a duration in milliseconds), converted to seconds and
3028 * forced into the range of a uint16_t. */
3030 age_secs(long long int age_ms
)
3032 return (age_ms
< 0 ? 0
3033 : age_ms
>= UINT16_MAX
* 1000 ? UINT16_MAX
3034 : (unsigned int) age_ms
/ 1000);
3038 handle_flow_stats_request(struct ofconn
*ofconn
,
3039 const struct ofp_header
*request
)
3041 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
3042 struct ofputil_flow_stats_request fsr
;
3043 struct list replies
;
3048 error
= ofputil_decode_flow_stats_request(&fsr
, request
);
3053 error
= collect_rules_loose(ofproto
, fsr
.table_id
, &fsr
.match
,
3054 fsr
.cookie
, fsr
.cookie_mask
,
3055 fsr
.out_port
, &rules
);
3060 ofpmp_init(&replies
, request
);
3061 LIST_FOR_EACH (rule
, ofproto_node
, &rules
) {
3062 long long int now
= time_msec();
3063 struct ofputil_flow_stats fs
;
3065 minimatch_expand(&rule
->cr
.match
, &fs
.match
);
3066 fs
.priority
= rule
->cr
.priority
;
3067 fs
.cookie
= rule
->flow_cookie
;
3068 fs
.table_id
= rule
->table_id
;
3069 calc_duration(rule
->created
, now
, &fs
.duration_sec
, &fs
.duration_nsec
);
3070 fs
.idle_age
= age_secs(now
- rule
->used
);
3071 fs
.hard_age
= age_secs(now
- rule
->modified
);
3072 ofproto
->ofproto_class
->rule_get_stats(rule
, &fs
.packet_count
,
3074 fs
.ofpacts
= rule
->ofpacts
;
3075 fs
.ofpacts_len
= rule
->ofpacts_len
;
3077 ovs_mutex_lock(&rule
->timeout_mutex
);
3078 fs
.idle_timeout
= rule
->idle_timeout
;
3079 fs
.hard_timeout
= rule
->hard_timeout
;
3080 ovs_mutex_unlock(&rule
->timeout_mutex
);
3083 if (rule
->send_flow_removed
) {
3084 fs
.flags
|= OFPUTIL_FF_SEND_FLOW_REM
;
3085 /* FIXME: Implement OFPUTIL_FF_NO_PKT_COUNTS and
3086 OFPUTIL_FF_NO_BYT_COUNTS. */
3088 ofputil_append_flow_stats_reply(&fs
, &replies
);
3090 ofconn_send_replies(ofconn
, &replies
);
3096 flow_stats_ds(struct rule
*rule
, struct ds
*results
)
3098 uint64_t packet_count
, byte_count
;
3100 rule
->ofproto
->ofproto_class
->rule_get_stats(rule
,
3101 &packet_count
, &byte_count
);
3103 if (rule
->table_id
!= 0) {
3104 ds_put_format(results
, "table_id=%"PRIu8
", ", rule
->table_id
);
3106 ds_put_format(results
, "duration=%llds, ",
3107 (time_msec() - rule
->created
) / 1000);
3108 ds_put_format(results
, "priority=%u, ", rule
->cr
.priority
);
3109 ds_put_format(results
, "n_packets=%"PRIu64
", ", packet_count
);
3110 ds_put_format(results
, "n_bytes=%"PRIu64
", ", byte_count
);
3111 cls_rule_format(&rule
->cr
, results
);
3112 ds_put_char(results
, ',');
3113 ofpacts_format(rule
->ofpacts
, rule
->ofpacts_len
, results
);
3114 ds_put_cstr(results
, "\n");
3117 /* Adds a pretty-printed description of all flows to 'results', including
3118 * hidden flows (e.g., set up by in-band control). */
3120 ofproto_get_all_flows(struct ofproto
*p
, struct ds
*results
)
3122 struct oftable
*table
;
3124 OFPROTO_FOR_EACH_TABLE (table
, p
) {
3125 struct cls_cursor cursor
;
3128 ovs_rwlock_rdlock(&table
->cls
.rwlock
);
3129 cls_cursor_init(&cursor
, &table
->cls
, NULL
);
3130 CLS_CURSOR_FOR_EACH (rule
, cr
, &cursor
) {
3131 flow_stats_ds(rule
, results
);
3133 ovs_rwlock_unlock(&table
->cls
.rwlock
);
3137 /* Obtains the NetFlow engine type and engine ID for 'ofproto' into
3138 * '*engine_type' and '*engine_id', respectively. */
3140 ofproto_get_netflow_ids(const struct ofproto
*ofproto
,
3141 uint8_t *engine_type
, uint8_t *engine_id
)
3143 ofproto
->ofproto_class
->get_netflow_ids(ofproto
, engine_type
, engine_id
);
3146 /* Checks the status of CFM configured on 'ofp_port' within 'ofproto'. Returns
3147 * true if the port's CFM status was successfully stored into '*status'.
3148 * Returns false if the port did not have CFM configured, in which case
3149 * '*status' is indeterminate.
3151 * The caller must provide and owns '*status', and must free 'status->rmps'. */
3153 ofproto_port_get_cfm_status(const struct ofproto
*ofproto
, ofp_port_t ofp_port
,
3154 struct ofproto_cfm_status
*status
)
3156 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
3158 && ofproto
->ofproto_class
->get_cfm_status
3159 && ofproto
->ofproto_class
->get_cfm_status(ofport
, status
));
3163 handle_aggregate_stats_request(struct ofconn
*ofconn
,
3164 const struct ofp_header
*oh
)
3166 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
3167 struct ofputil_flow_stats_request request
;
3168 struct ofputil_aggregate_stats stats
;
3169 bool unknown_packets
, unknown_bytes
;
3170 struct ofpbuf
*reply
;
3175 error
= ofputil_decode_flow_stats_request(&request
, oh
);
3180 error
= collect_rules_loose(ofproto
, request
.table_id
, &request
.match
,
3181 request
.cookie
, request
.cookie_mask
,
3182 request
.out_port
, &rules
);
3187 memset(&stats
, 0, sizeof stats
);
3188 unknown_packets
= unknown_bytes
= false;
3189 LIST_FOR_EACH (rule
, ofproto_node
, &rules
) {
3190 uint64_t packet_count
;
3191 uint64_t byte_count
;
3193 ofproto
->ofproto_class
->rule_get_stats(rule
, &packet_count
,
3196 if (packet_count
== UINT64_MAX
) {
3197 unknown_packets
= true;
3199 stats
.packet_count
+= packet_count
;
3202 if (byte_count
== UINT64_MAX
) {
3203 unknown_bytes
= true;
3205 stats
.byte_count
+= byte_count
;
3210 if (unknown_packets
) {
3211 stats
.packet_count
= UINT64_MAX
;
3213 if (unknown_bytes
) {
3214 stats
.byte_count
= UINT64_MAX
;
3217 reply
= ofputil_encode_aggregate_stats_reply(&stats
, oh
);
3218 ofconn_send_reply(ofconn
, reply
);
3223 struct queue_stats_cbdata
{
3224 struct ofport
*ofport
;
3225 struct list replies
;
3230 put_queue_stats(struct queue_stats_cbdata
*cbdata
, uint32_t queue_id
,
3231 const struct netdev_queue_stats
*stats
)
3233 struct ofputil_queue_stats oqs
;
3235 oqs
.port_no
= cbdata
->ofport
->pp
.port_no
;
3236 oqs
.queue_id
= queue_id
;
3237 oqs
.tx_bytes
= stats
->tx_bytes
;
3238 oqs
.tx_packets
= stats
->tx_packets
;
3239 oqs
.tx_errors
= stats
->tx_errors
;
3240 if (stats
->created
!= LLONG_MIN
) {
3241 calc_duration(stats
->created
, cbdata
->now
,
3242 &oqs
.duration_sec
, &oqs
.duration_nsec
);
3244 oqs
.duration_sec
= oqs
.duration_nsec
= UINT32_MAX
;
3246 ofputil_append_queue_stat(&cbdata
->replies
, &oqs
);
3250 handle_queue_stats_dump_cb(uint32_t queue_id
,
3251 struct netdev_queue_stats
*stats
,
3254 struct queue_stats_cbdata
*cbdata
= cbdata_
;
3256 put_queue_stats(cbdata
, queue_id
, stats
);
3260 handle_queue_stats_for_port(struct ofport
*port
, uint32_t queue_id
,
3261 struct queue_stats_cbdata
*cbdata
)
3263 cbdata
->ofport
= port
;
3264 if (queue_id
== OFPQ_ALL
) {
3265 netdev_dump_queue_stats(port
->netdev
,
3266 handle_queue_stats_dump_cb
, cbdata
);
3268 struct netdev_queue_stats stats
;
3270 if (!netdev_get_queue_stats(port
->netdev
, queue_id
, &stats
)) {
3271 put_queue_stats(cbdata
, queue_id
, &stats
);
3273 return OFPERR_OFPQOFC_BAD_QUEUE
;
3280 handle_queue_stats_request(struct ofconn
*ofconn
,
3281 const struct ofp_header
*rq
)
3283 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
3284 struct queue_stats_cbdata cbdata
;
3285 struct ofport
*port
;
3287 struct ofputil_queue_stats_request oqsr
;
3289 COVERAGE_INC(ofproto_queue_req
);
3291 ofpmp_init(&cbdata
.replies
, rq
);
3292 cbdata
.now
= time_msec();
3294 error
= ofputil_decode_queue_stats_request(rq
, &oqsr
);
3299 if (oqsr
.port_no
== OFPP_ANY
) {
3300 error
= OFPERR_OFPQOFC_BAD_QUEUE
;
3301 HMAP_FOR_EACH (port
, hmap_node
, &ofproto
->ports
) {
3302 if (!handle_queue_stats_for_port(port
, oqsr
.queue_id
, &cbdata
)) {
3307 port
= ofproto_get_port(ofproto
, oqsr
.port_no
);
3309 ? handle_queue_stats_for_port(port
, oqsr
.queue_id
, &cbdata
)
3310 : OFPERR_OFPQOFC_BAD_PORT
);
3313 ofconn_send_replies(ofconn
, &cbdata
.replies
);
3315 ofpbuf_list_delete(&cbdata
.replies
);
3322 is_flow_deletion_pending(const struct ofproto
*ofproto
,
3323 const struct cls_rule
*cls_rule
,
3326 if (!hmap_is_empty(&ofproto
->deletions
)) {
3327 struct ofoperation
*op
;
3329 HMAP_FOR_EACH_WITH_HASH (op
, hmap_node
,
3330 cls_rule_hash(cls_rule
, table_id
),
3331 &ofproto
->deletions
) {
3332 if (cls_rule_equal(cls_rule
, &op
->rule
->cr
)) {
3341 /* Implements OFPFC_ADD and the cases for OFPFC_MODIFY and OFPFC_MODIFY_STRICT
3342 * in which no matching flow already exists in the flow table.
3344 * Adds the flow specified by 'ofm', which is followed by 'n_actions'
3345 * ofp_actions, to the ofproto's flow table. Returns 0 on success, an OpenFlow
3346 * error code on failure, or OFPROTO_POSTPONE if the operation cannot be
3347 * initiated now but may be retried later.
3349 * Upon successful return, takes ownership of 'fm->ofpacts'. On failure,
3350 * ownership remains with the caller.
3352 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
3355 add_flow(struct ofproto
*ofproto
, struct ofconn
*ofconn
,
3356 struct ofputil_flow_mod
*fm
, const struct ofp_header
*request
)
3358 struct oftable
*table
;
3359 struct ofopgroup
*group
;
3360 struct rule
*victim
;
3365 error
= check_table_id(ofproto
, fm
->table_id
);
3371 if (fm
->table_id
== 0xff) {
3372 if (ofproto
->ofproto_class
->rule_choose_table
) {
3373 error
= ofproto
->ofproto_class
->rule_choose_table(ofproto
,
3379 ovs_assert(table_id
< ofproto
->n_tables
);
3383 } else if (fm
->table_id
< ofproto
->n_tables
) {
3384 table_id
= fm
->table_id
;
3386 return OFPERR_OFPBRC_BAD_TABLE_ID
;
3389 table
= &ofproto
->tables
[table_id
];
3391 if (table
->flags
& OFTABLE_READONLY
) {
3392 return OFPERR_OFPBRC_EPERM
;
3395 /* Verify actions. */
3396 error
= ofproto_check_ofpacts(ofproto
, fm
->ofpacts
, fm
->ofpacts_len
,
3397 &fm
->match
.flow
, table_id
);
3402 /* Allocate new rule and initialize classifier rule. */
3403 rule
= ofproto
->ofproto_class
->rule_alloc();
3405 VLOG_WARN_RL(&rl
, "%s: failed to create rule (%s)",
3406 ofproto
->name
, ovs_strerror(error
));
3409 cls_rule_init(&rule
->cr
, &fm
->match
, fm
->priority
);
3411 /* Serialize against pending deletion. */
3412 if (is_flow_deletion_pending(ofproto
, &rule
->cr
, table_id
)) {
3413 cls_rule_destroy(&rule
->cr
);
3414 ofproto
->ofproto_class
->rule_dealloc(rule
);
3415 return OFPROTO_POSTPONE
;
3418 /* Check for overlap, if requested. */
3419 if (fm
->flags
& OFPUTIL_FF_CHECK_OVERLAP
) {
3422 ovs_rwlock_rdlock(&table
->cls
.rwlock
);
3423 overlaps
= classifier_rule_overlaps(&table
->cls
, &rule
->cr
);
3424 ovs_rwlock_unlock(&table
->cls
.rwlock
);
3427 cls_rule_destroy(&rule
->cr
);
3428 ofproto
->ofproto_class
->rule_dealloc(rule
);
3429 return OFPERR_OFPFMFC_OVERLAP
;
3433 /* FIXME: Implement OFPFF12_RESET_COUNTS */
3435 rule
->ofproto
= ofproto
;
3436 rule
->pending
= NULL
;
3437 rule
->flow_cookie
= fm
->new_cookie
;
3438 rule
->created
= rule
->modified
= rule
->used
= time_msec();
3440 ovs_mutex_init(&rule
->timeout_mutex
);
3441 ovs_mutex_lock(&rule
->timeout_mutex
);
3442 rule
->idle_timeout
= fm
->idle_timeout
;
3443 rule
->hard_timeout
= fm
->hard_timeout
;
3444 ovs_mutex_unlock(&rule
->timeout_mutex
);
3446 rule
->table_id
= table
- ofproto
->tables
;
3447 rule
->send_flow_removed
= (fm
->flags
& OFPUTIL_FF_SEND_FLOW_REM
) != 0;
3448 rule
->ofpacts
= xmemdup(fm
->ofpacts
, fm
->ofpacts_len
);
3449 rule
->ofpacts_len
= fm
->ofpacts_len
;
3450 rule
->meter_id
= find_meter(rule
->ofpacts
, rule
->ofpacts_len
);
3451 list_init(&rule
->meter_list_node
);
3452 rule
->eviction_group
= NULL
;
3453 list_init(&rule
->expirable
);
3454 rule
->monitor_flags
= 0;
3455 rule
->add_seqno
= 0;
3456 rule
->modify_seqno
= 0;
3457 ovs_rwlock_init(&rule
->evict
);
3459 /* Insert new rule. */
3460 victim
= oftable_replace_rule(rule
);
3461 if (victim
&& !rule_is_modifiable(victim
)) {
3462 error
= OFPERR_OFPBRC_EPERM
;
3463 } else if (victim
&& victim
->pending
) {
3464 error
= OFPROTO_POSTPONE
;
3466 struct ofoperation
*op
;
3470 ovs_rwlock_rdlock(&table
->cls
.rwlock
);
3471 n_rules
= classifier_count(&table
->cls
);
3472 ovs_rwlock_unlock(&table
->cls
.rwlock
);
3473 if (n_rules
> table
->max_flows
) {
3474 ovs_rwlock_rdlock(&rule
->evict
);
3475 if (choose_rule_to_evict(table
, &evict
)) {
3476 ovs_rwlock_unlock(&rule
->evict
);
3477 ovs_rwlock_unlock(&evict
->evict
);
3478 if (evict
->pending
) {
3479 error
= OFPROTO_POSTPONE
;
3483 ovs_rwlock_unlock(&rule
->evict
);
3484 error
= OFPERR_OFPFMFC_TABLE_FULL
;
3491 group
= ofopgroup_create(ofproto
, ofconn
, request
, fm
->buffer_id
);
3492 op
= ofoperation_create(group
, rule
, OFOPERATION_ADD
, 0);
3493 op
->victim
= victim
;
3495 error
= ofproto
->ofproto_class
->rule_construct(rule
);
3497 op
->group
->n_running
--;
3498 ofoperation_destroy(rule
->pending
);
3500 /* It would be better if we maintained the lock we took in
3501 * choose_rule_to_evict() earlier, but that confuses the thread
3502 * safety analysis, and this code is fragile enough that we really
3503 * need it. In the worst case, we'll have to block a little while
3504 * before we perform the eviction, which doesn't seem like a big
3506 ovs_rwlock_wrlock(&evict
->evict
);
3507 delete_flow__(evict
, group
, OFPRR_EVICTION
);
3509 ofopgroup_submit(group
);
3513 /* Back out if an error occurred. */
3515 oftable_substitute_rule(rule
, victim
);
3516 ofproto_rule_destroy__(rule
);
3521 /* OFPFC_MODIFY and OFPFC_MODIFY_STRICT. */
3523 /* Modifies the rules listed in 'rules', changing their actions to match those
3526 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
3529 * Returns 0 on success, otherwise an OpenFlow error code. */
3531 modify_flows__(struct ofproto
*ofproto
, struct ofconn
*ofconn
,
3532 struct ofputil_flow_mod
*fm
, const struct ofp_header
*request
,
3535 struct ofopgroup
*group
;
3539 group
= ofopgroup_create(ofproto
, ofconn
, request
, fm
->buffer_id
);
3540 error
= OFPERR_OFPBRC_EPERM
;
3541 LIST_FOR_EACH (rule
, ofproto_node
, rules
) {
3542 struct ofoperation
*op
;
3543 bool actions_changed
;
3545 /* FIXME: Implement OFPFUTIL_FF_RESET_COUNTS */
3547 if (rule_is_modifiable(rule
)) {
3548 /* At least one rule is modifiable, don't report EPERM error. */
3554 /* Verify actions. */
3555 error
= ofpacts_check(fm
->ofpacts
, fm
->ofpacts_len
, &fm
->match
.flow
,
3556 u16_to_ofp(ofproto
->max_ports
), rule
->table_id
);
3561 actions_changed
= !ofpacts_equal(fm
->ofpacts
, fm
->ofpacts_len
,
3562 rule
->ofpacts
, rule
->ofpacts_len
);
3564 op
= ofoperation_create(group
, rule
, OFOPERATION_MODIFY
, 0);
3566 if (fm
->modify_cookie
&& fm
->new_cookie
!= htonll(UINT64_MAX
)) {
3567 ofproto_rule_change_cookie(ofproto
, rule
, fm
->new_cookie
);
3569 if (actions_changed
) {
3570 op
->ofpacts
= rule
->ofpacts
;
3571 op
->ofpacts_len
= rule
->ofpacts_len
;
3572 op
->meter_id
= rule
->meter_id
;
3573 rule
->ofpacts
= xmemdup(fm
->ofpacts
, fm
->ofpacts_len
);
3574 rule
->ofpacts_len
= fm
->ofpacts_len
;
3575 rule
->meter_id
= find_meter(rule
->ofpacts
, rule
->ofpacts_len
);
3576 rule
->ofproto
->ofproto_class
->rule_modify_actions(rule
);
3578 ofoperation_complete(op
, 0);
3581 ofopgroup_submit(group
);
3587 modify_flows_add(struct ofproto
*ofproto
, struct ofconn
*ofconn
,
3588 struct ofputil_flow_mod
*fm
, const struct ofp_header
*request
)
3590 if (fm
->cookie_mask
!= htonll(0) || fm
->new_cookie
== htonll(UINT64_MAX
)) {
3593 return add_flow(ofproto
, ofconn
, fm
, request
);
3596 /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code on
3599 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
3602 modify_flows_loose(struct ofproto
*ofproto
, struct ofconn
*ofconn
,
3603 struct ofputil_flow_mod
*fm
,
3604 const struct ofp_header
*request
)
3609 error
= collect_rules_loose(ofproto
, fm
->table_id
, &fm
->match
,
3610 fm
->cookie
, fm
->cookie_mask
,
3614 } else if (list_is_empty(&rules
)) {
3615 return modify_flows_add(ofproto
, ofconn
, fm
, request
);
3617 return modify_flows__(ofproto
, ofconn
, fm
, request
, &rules
);
3621 /* Implements OFPFC_MODIFY_STRICT. Returns 0 on success or an OpenFlow error
3624 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
3627 modify_flow_strict(struct ofproto
*ofproto
, struct ofconn
*ofconn
,
3628 struct ofputil_flow_mod
*fm
,
3629 const struct ofp_header
*request
)
3634 error
= collect_rules_strict(ofproto
, fm
->table_id
, &fm
->match
,
3635 fm
->priority
, fm
->cookie
, fm
->cookie_mask
,
3640 } else if (list_is_empty(&rules
)) {
3641 return modify_flows_add(ofproto
, ofconn
, fm
, request
);
3643 return list_is_singleton(&rules
) ? modify_flows__(ofproto
, ofconn
,
3644 fm
, request
, &rules
)
3649 /* OFPFC_DELETE implementation. */
3652 delete_flow__(struct rule
*rule
, struct ofopgroup
*group
,
3653 enum ofp_flow_removed_reason reason
)
3655 struct ofproto
*ofproto
= rule
->ofproto
;
3657 ofproto_rule_send_removed(rule
, reason
);
3659 ofoperation_create(group
, rule
, OFOPERATION_DELETE
, reason
);
3660 oftable_remove_rule(rule
);
3661 ofproto
->ofproto_class
->rule_destruct(rule
);
3664 /* Deletes the rules listed in 'rules'.
3666 * Returns 0 on success, otherwise an OpenFlow error code. */
3668 delete_flows__(struct ofproto
*ofproto
, struct ofconn
*ofconn
,
3669 const struct ofp_header
*request
, struct list
*rules
,
3670 enum ofp_flow_removed_reason reason
)
3672 struct rule
*rule
, *next
;
3673 struct ofopgroup
*group
;
3675 group
= ofopgroup_create(ofproto
, ofconn
, request
, UINT32_MAX
);
3676 LIST_FOR_EACH_SAFE (rule
, next
, ofproto_node
, rules
) {
3677 ovs_rwlock_wrlock(&rule
->evict
);
3678 delete_flow__(rule
, group
, reason
);
3680 ofopgroup_submit(group
);
3685 /* Implements OFPFC_DELETE. */
3687 delete_flows_loose(struct ofproto
*ofproto
, struct ofconn
*ofconn
,
3688 const struct ofputil_flow_mod
*fm
,
3689 const struct ofp_header
*request
)
3694 error
= collect_rules_loose(ofproto
, fm
->table_id
, &fm
->match
,
3695 fm
->cookie
, fm
->cookie_mask
,
3696 fm
->out_port
, &rules
);
3697 return (error
? error
3698 : !list_is_empty(&rules
) ? delete_flows__(ofproto
, ofconn
, request
,
3699 &rules
, OFPRR_DELETE
)
3703 /* Implements OFPFC_DELETE_STRICT. */
3705 delete_flow_strict(struct ofproto
*ofproto
, struct ofconn
*ofconn
,
3706 const struct ofputil_flow_mod
*fm
,
3707 const struct ofp_header
*request
)
3712 error
= collect_rules_strict(ofproto
, fm
->table_id
, &fm
->match
,
3713 fm
->priority
, fm
->cookie
, fm
->cookie_mask
,
3714 fm
->out_port
, &rules
);
3715 return (error
? error
3716 : list_is_singleton(&rules
) ? delete_flows__(ofproto
, ofconn
,
3723 ofproto_rule_send_removed(struct rule
*rule
, uint8_t reason
)
3725 struct ofputil_flow_removed fr
;
3727 if (ofproto_rule_is_hidden(rule
) || !rule
->send_flow_removed
) {
3731 minimatch_expand(&rule
->cr
.match
, &fr
.match
);
3732 fr
.priority
= rule
->cr
.priority
;
3733 fr
.cookie
= rule
->flow_cookie
;
3735 fr
.table_id
= rule
->table_id
;
3736 calc_duration(rule
->created
, time_msec(),
3737 &fr
.duration_sec
, &fr
.duration_nsec
);
3738 ovs_mutex_lock(&rule
->timeout_mutex
);
3739 fr
.idle_timeout
= rule
->idle_timeout
;
3740 fr
.hard_timeout
= rule
->hard_timeout
;
3741 ovs_mutex_unlock(&rule
->timeout_mutex
);
3742 rule
->ofproto
->ofproto_class
->rule_get_stats(rule
, &fr
.packet_count
,
3745 connmgr_send_flow_removed(rule
->ofproto
->connmgr
, &fr
);
3749 ofproto_rule_update_used(struct rule
*rule
, long long int used
)
3751 if (used
> rule
->used
) {
3752 struct eviction_group
*evg
= rule
->eviction_group
;
3756 heap_change(&evg
->rules
, &rule
->evg_node
,
3757 rule_eviction_priority(rule
));
3762 /* Sends an OpenFlow "flow removed" message with the given 'reason' (either
3763 * OFPRR_HARD_TIMEOUT or OFPRR_IDLE_TIMEOUT), and then removes 'rule' from its
3766 * 'rule' must not have a pending operation (that is, 'rule->pending' must be
3769 * ofproto implementation ->run() functions should use this function to expire
3770 * OpenFlow flows. */
3772 ofproto_rule_expire(struct rule
*rule
, uint8_t reason
)
3774 struct ofproto
*ofproto
= rule
->ofproto
;
3775 struct ofopgroup
*group
;
3777 ovs_assert(reason
== OFPRR_HARD_TIMEOUT
|| reason
== OFPRR_IDLE_TIMEOUT
);
3779 ofproto_rule_send_removed(rule
, reason
);
3781 group
= ofopgroup_create_unattached(ofproto
);
3782 ofoperation_create(group
, rule
, OFOPERATION_DELETE
, reason
);
3783 oftable_remove_rule(rule
);
3784 ofproto
->ofproto_class
->rule_destruct(rule
);
3785 ofopgroup_submit(group
);
3788 /* Reduces '*timeout' to no more than 'max'. A value of zero in either case
3789 * means "infinite". */
3791 reduce_timeout(uint16_t max
, uint16_t *timeout
)
3793 if (max
&& (!*timeout
|| *timeout
> max
)) {
3798 /* If 'idle_timeout' is nonzero, and 'rule' has no idle timeout or an idle
3799 * timeout greater than 'idle_timeout', lowers 'rule''s idle timeout to
3800 * 'idle_timeout' seconds. Similarly for 'hard_timeout'.
3802 * Suitable for implementing OFPACT_FIN_TIMEOUT. */
3804 ofproto_rule_reduce_timeouts(struct rule
*rule
,
3805 uint16_t idle_timeout
, uint16_t hard_timeout
)
3806 OVS_EXCLUDED(rule
->ofproto
->expirable_mutex
, rule
->timeout_mutex
)
3808 if (!idle_timeout
&& !hard_timeout
) {
3812 ovs_mutex_lock(&rule
->ofproto
->expirable_mutex
);
3813 if (list_is_empty(&rule
->expirable
)) {
3814 list_insert(&rule
->ofproto
->expirable
, &rule
->expirable
);
3816 ovs_mutex_unlock(&rule
->ofproto
->expirable_mutex
);
3818 ovs_mutex_lock(&rule
->timeout_mutex
);
3819 reduce_timeout(idle_timeout
, &rule
->idle_timeout
);
3820 reduce_timeout(hard_timeout
, &rule
->hard_timeout
);
3821 ovs_mutex_unlock(&rule
->timeout_mutex
);
3823 if (!rule
->eviction_group
) {
3824 eviction_group_add_rule(rule
);
3829 handle_flow_mod(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
3831 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
3832 struct ofputil_flow_mod fm
;
3833 uint64_t ofpacts_stub
[1024 / 8];
3834 struct ofpbuf ofpacts
;
3838 error
= reject_slave_controller(ofconn
);
3843 ofpbuf_use_stub(&ofpacts
, ofpacts_stub
, sizeof ofpacts_stub
);
3844 error
= ofputil_decode_flow_mod(&fm
, oh
, ofconn_get_protocol(ofconn
),
3847 error
= handle_flow_mod__(ofproto
, ofconn
, &fm
, oh
);
3850 goto exit_free_ofpacts
;
3853 /* Record the operation for logging a summary report. */
3854 switch (fm
.command
) {
3860 case OFPFC_MODIFY_STRICT
:
3861 ofproto
->n_modify
++;
3865 case OFPFC_DELETE_STRICT
:
3866 ofproto
->n_delete
++;
3871 if (ofproto
->next_op_report
== LLONG_MAX
) {
3872 ofproto
->first_op
= now
;
3873 ofproto
->next_op_report
= MAX(now
+ 10 * 1000,
3874 ofproto
->op_backoff
);
3875 ofproto
->op_backoff
= ofproto
->next_op_report
+ 60 * 1000;
3877 ofproto
->last_op
= now
;
3880 ofpbuf_uninit(&ofpacts
);
3886 handle_flow_mod__(struct ofproto
*ofproto
, struct ofconn
*ofconn
,
3887 struct ofputil_flow_mod
*fm
, const struct ofp_header
*oh
)
3889 if (ofproto
->n_pending
>= 50) {
3890 ovs_assert(!list_is_empty(&ofproto
->pending
));
3891 return OFPROTO_POSTPONE
;
3894 switch (fm
->command
) {
3896 return add_flow(ofproto
, ofconn
, fm
, oh
);
3899 return modify_flows_loose(ofproto
, ofconn
, fm
, oh
);
3901 case OFPFC_MODIFY_STRICT
:
3902 return modify_flow_strict(ofproto
, ofconn
, fm
, oh
);
3905 return delete_flows_loose(ofproto
, ofconn
, fm
, oh
);
3907 case OFPFC_DELETE_STRICT
:
3908 return delete_flow_strict(ofproto
, ofconn
, fm
, oh
);
3911 if (fm
->command
> 0xff) {
3912 VLOG_WARN_RL(&rl
, "%s: flow_mod has explicit table_id but "
3913 "flow_mod_table_id extension is not enabled",
3916 return OFPERR_OFPFMFC_BAD_COMMAND
;
3921 handle_role_request(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
3923 struct ofputil_role_request request
;
3924 struct ofputil_role_request reply
;
3928 error
= ofputil_decode_role_message(oh
, &request
);
3933 if (request
.role
!= OFPCR12_ROLE_NOCHANGE
) {
3934 if (ofconn_get_role(ofconn
) != request
.role
3935 && ofconn_has_pending_opgroups(ofconn
)) {
3936 return OFPROTO_POSTPONE
;
3939 if (request
.have_generation_id
3940 && !ofconn_set_master_election_id(ofconn
, request
.generation_id
)) {
3941 return OFPERR_OFPRRFC_STALE
;
3944 ofconn_set_role(ofconn
, request
.role
);
3947 reply
.role
= ofconn_get_role(ofconn
);
3948 reply
.have_generation_id
= ofconn_get_master_election_id(
3949 ofconn
, &reply
.generation_id
);
3950 buf
= ofputil_encode_role_reply(oh
, &reply
);
3951 ofconn_send_reply(ofconn
, buf
);
3957 handle_nxt_flow_mod_table_id(struct ofconn
*ofconn
,
3958 const struct ofp_header
*oh
)
3960 const struct nx_flow_mod_table_id
*msg
= ofpmsg_body(oh
);
3961 enum ofputil_protocol cur
, next
;
3963 cur
= ofconn_get_protocol(ofconn
);
3964 next
= ofputil_protocol_set_tid(cur
, msg
->set
!= 0);
3965 ofconn_set_protocol(ofconn
, next
);
3971 handle_nxt_set_flow_format(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
3973 const struct nx_set_flow_format
*msg
= ofpmsg_body(oh
);
3974 enum ofputil_protocol cur
, next
;
3975 enum ofputil_protocol next_base
;
3977 next_base
= ofputil_nx_flow_format_to_protocol(ntohl(msg
->format
));
3979 return OFPERR_OFPBRC_EPERM
;
3982 cur
= ofconn_get_protocol(ofconn
);
3983 next
= ofputil_protocol_set_base(cur
, next_base
);
3984 if (cur
!= next
&& ofconn_has_pending_opgroups(ofconn
)) {
3985 /* Avoid sending async messages in surprising protocol. */
3986 return OFPROTO_POSTPONE
;
3989 ofconn_set_protocol(ofconn
, next
);
3994 handle_nxt_set_packet_in_format(struct ofconn
*ofconn
,
3995 const struct ofp_header
*oh
)
3997 const struct nx_set_packet_in_format
*msg
= ofpmsg_body(oh
);
4000 format
= ntohl(msg
->format
);
4001 if (format
!= NXPIF_OPENFLOW10
&& format
!= NXPIF_NXM
) {
4002 return OFPERR_OFPBRC_EPERM
;
4005 if (format
!= ofconn_get_packet_in_format(ofconn
)
4006 && ofconn_has_pending_opgroups(ofconn
)) {
4007 /* Avoid sending async message in surprsing packet in format. */
4008 return OFPROTO_POSTPONE
;
4011 ofconn_set_packet_in_format(ofconn
, format
);
4016 handle_nxt_set_async_config(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
4018 const struct nx_async_config
*msg
= ofpmsg_body(oh
);
4019 uint32_t master
[OAM_N_TYPES
];
4020 uint32_t slave
[OAM_N_TYPES
];
4022 master
[OAM_PACKET_IN
] = ntohl(msg
->packet_in_mask
[0]);
4023 master
[OAM_PORT_STATUS
] = ntohl(msg
->port_status_mask
[0]);
4024 master
[OAM_FLOW_REMOVED
] = ntohl(msg
->flow_removed_mask
[0]);
4026 slave
[OAM_PACKET_IN
] = ntohl(msg
->packet_in_mask
[1]);
4027 slave
[OAM_PORT_STATUS
] = ntohl(msg
->port_status_mask
[1]);
4028 slave
[OAM_FLOW_REMOVED
] = ntohl(msg
->flow_removed_mask
[1]);
4030 ofconn_set_async_config(ofconn
, master
, slave
);
4031 if (ofconn_get_type(ofconn
) == OFCONN_SERVICE
&&
4032 !ofconn_get_miss_send_len(ofconn
)) {
4033 ofconn_set_miss_send_len(ofconn
, OFP_DEFAULT_MISS_SEND_LEN
);
4040 handle_nxt_set_controller_id(struct ofconn
*ofconn
,
4041 const struct ofp_header
*oh
)
4043 const struct nx_controller_id
*nci
= ofpmsg_body(oh
);
4045 if (!is_all_zeros(nci
->zero
, sizeof nci
->zero
)) {
4046 return OFPERR_NXBRC_MUST_BE_ZERO
;
4049 ofconn_set_controller_id(ofconn
, ntohs(nci
->controller_id
));
4054 handle_barrier_request(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
4058 if (ofconn_has_pending_opgroups(ofconn
)) {
4059 return OFPROTO_POSTPONE
;
4062 buf
= ofpraw_alloc_reply((oh
->version
== OFP10_VERSION
4063 ? OFPRAW_OFPT10_BARRIER_REPLY
4064 : OFPRAW_OFPT11_BARRIER_REPLY
), oh
, 0);
4065 ofconn_send_reply(ofconn
, buf
);
4070 ofproto_compose_flow_refresh_update(const struct rule
*rule
,
4071 enum nx_flow_monitor_flags flags
,
4074 struct ofoperation
*op
= rule
->pending
;
4075 struct ofputil_flow_update fu
;
4078 if (op
&& op
->type
== OFOPERATION_ADD
&& !op
->victim
) {
4079 /* We'll report the final flow when the operation completes. Reporting
4080 * it now would cause a duplicate report later. */
4084 fu
.event
= (flags
& (NXFMF_INITIAL
| NXFMF_ADD
)
4085 ? NXFME_ADDED
: NXFME_MODIFIED
);
4087 ovs_mutex_lock(&rule
->timeout_mutex
);
4088 fu
.idle_timeout
= rule
->idle_timeout
;
4089 fu
.hard_timeout
= rule
->hard_timeout
;
4090 ovs_mutex_unlock(&rule
->timeout_mutex
);
4091 fu
.table_id
= rule
->table_id
;
4092 fu
.cookie
= rule
->flow_cookie
;
4093 minimatch_expand(&rule
->cr
.match
, &match
);
4095 fu
.priority
= rule
->cr
.priority
;
4096 if (!(flags
& NXFMF_ACTIONS
)) {
4100 fu
.ofpacts
= rule
->ofpacts
;
4101 fu
.ofpacts_len
= rule
->ofpacts_len
;
4103 /* An operation is in progress. Use the previous version of the flow's
4104 * actions, so that when the operation commits we report the change. */
4106 case OFOPERATION_ADD
:
4107 /* We already verified that there was a victim. */
4108 fu
.ofpacts
= op
->victim
->ofpacts
;
4109 fu
.ofpacts_len
= op
->victim
->ofpacts_len
;
4112 case OFOPERATION_MODIFY
:
4114 fu
.ofpacts
= op
->ofpacts
;
4115 fu
.ofpacts_len
= op
->ofpacts_len
;
4117 fu
.ofpacts
= rule
->ofpacts
;
4118 fu
.ofpacts_len
= rule
->ofpacts_len
;
4122 case OFOPERATION_DELETE
:
4123 fu
.ofpacts
= rule
->ofpacts
;
4124 fu
.ofpacts_len
= rule
->ofpacts_len
;
4132 if (list_is_empty(msgs
)) {
4133 ofputil_start_flow_update(msgs
);
4135 ofputil_append_flow_update(&fu
, msgs
);
4139 ofmonitor_compose_refresh_updates(struct list
*rules
, struct list
*msgs
)
4143 LIST_FOR_EACH (rule
, ofproto_node
, rules
) {
4144 enum nx_flow_monitor_flags flags
= rule
->monitor_flags
;
4145 rule
->monitor_flags
= 0;
4147 ofproto_compose_flow_refresh_update(rule
, flags
, msgs
);
4152 ofproto_collect_ofmonitor_refresh_rule(const struct ofmonitor
*m
,
4153 struct rule
*rule
, uint64_t seqno
,
4156 enum nx_flow_monitor_flags update
;
4158 if (ofproto_rule_is_hidden(rule
)) {
4163 ? ofoperation_has_out_port(rule
->pending
, m
->out_port
)
4164 : ofproto_rule_has_out_port(rule
, m
->out_port
))) {
4169 if (rule
->add_seqno
> seqno
) {
4170 update
= NXFMF_ADD
| NXFMF_MODIFY
;
4171 } else if (rule
->modify_seqno
> seqno
) {
4172 update
= NXFMF_MODIFY
;
4177 if (!(m
->flags
& update
)) {
4181 update
= NXFMF_INITIAL
;
4184 if (!rule
->monitor_flags
) {
4185 list_push_back(rules
, &rule
->ofproto_node
);
4187 rule
->monitor_flags
|= update
| (m
->flags
& NXFMF_ACTIONS
);
4191 ofproto_collect_ofmonitor_refresh_rules(const struct ofmonitor
*m
,
4195 const struct ofproto
*ofproto
= ofconn_get_ofproto(m
->ofconn
);
4196 const struct ofoperation
*op
;
4197 const struct oftable
*table
;
4198 struct cls_rule target
;
4200 cls_rule_init_from_minimatch(&target
, &m
->match
, 0);
4201 FOR_EACH_MATCHING_TABLE (table
, m
->table_id
, ofproto
) {
4202 struct cls_cursor cursor
;
4205 ovs_rwlock_rdlock(&table
->cls
.rwlock
);
4206 cls_cursor_init(&cursor
, &table
->cls
, &target
);
4207 CLS_CURSOR_FOR_EACH (rule
, cr
, &cursor
) {
4208 ovs_assert(!rule
->pending
); /* XXX */
4209 ofproto_collect_ofmonitor_refresh_rule(m
, rule
, seqno
, rules
);
4211 ovs_rwlock_unlock(&table
->cls
.rwlock
);
4214 HMAP_FOR_EACH (op
, hmap_node
, &ofproto
->deletions
) {
4215 struct rule
*rule
= op
->rule
;
4217 if (((m
->table_id
== 0xff
4218 ? !(ofproto
->tables
[rule
->table_id
].flags
& OFTABLE_HIDDEN
)
4219 : m
->table_id
== rule
->table_id
))
4220 && cls_rule_is_loose_match(&rule
->cr
, &target
.match
)) {
4221 ofproto_collect_ofmonitor_refresh_rule(m
, rule
, seqno
, rules
);
4224 cls_rule_destroy(&target
);
4228 ofproto_collect_ofmonitor_initial_rules(struct ofmonitor
*m
,
4231 if (m
->flags
& NXFMF_INITIAL
) {
4232 ofproto_collect_ofmonitor_refresh_rules(m
, 0, rules
);
4237 ofmonitor_collect_resume_rules(struct ofmonitor
*m
,
4238 uint64_t seqno
, struct list
*rules
)
4240 ofproto_collect_ofmonitor_refresh_rules(m
, seqno
, rules
);
4244 handle_flow_monitor_request(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
4246 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
4247 struct ofmonitor
**monitors
;
4248 size_t n_monitors
, allocated_monitors
;
4249 struct list replies
;
4256 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
4258 n_monitors
= allocated_monitors
= 0;
4260 struct ofputil_flow_monitor_request request
;
4261 struct ofmonitor
*m
;
4264 retval
= ofputil_decode_flow_monitor_request(&request
, &b
);
4265 if (retval
== EOF
) {
4267 } else if (retval
) {
4272 if (request
.table_id
!= 0xff
4273 && request
.table_id
>= ofproto
->n_tables
) {
4274 error
= OFPERR_OFPBRC_BAD_TABLE_ID
;
4278 error
= ofmonitor_create(&request
, ofconn
, &m
);
4283 if (n_monitors
>= allocated_monitors
) {
4284 monitors
= x2nrealloc(monitors
, &allocated_monitors
,
4287 monitors
[n_monitors
++] = m
;
4291 for (i
= 0; i
< n_monitors
; i
++) {
4292 ofproto_collect_ofmonitor_initial_rules(monitors
[i
], &rules
);
4295 ofpmp_init(&replies
, oh
);
4296 ofmonitor_compose_refresh_updates(&rules
, &replies
);
4297 ofconn_send_replies(ofconn
, &replies
);
4304 for (i
= 0; i
< n_monitors
; i
++) {
4305 ofmonitor_destroy(monitors
[i
]);
4312 handle_flow_monitor_cancel(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
4314 struct ofmonitor
*m
;
4317 id
= ofputil_decode_flow_monitor_cancel(oh
);
4318 m
= ofmonitor_lookup(ofconn
, id
);
4320 return OFPERR_NXBRC_FM_BAD_ID
;
4323 ofmonitor_destroy(m
);
4327 /* Meters implementation.
4329 * Meter table entry, indexed by the OpenFlow meter_id.
4330 * These are always dynamically allocated to allocate enough space for
4332 * 'created' is used to compute the duration for meter stats.
4333 * 'list rules' is needed so that we can delete the dependent rules when the
4334 * meter table entry is deleted.
4335 * 'provider_meter_id' is for the provider's private use.
4338 long long int created
; /* Time created. */
4339 struct list rules
; /* List of "struct rule_dpif"s. */
4340 ofproto_meter_id provider_meter_id
;
4341 uint16_t flags
; /* Meter flags. */
4342 uint16_t n_bands
; /* Number of meter bands. */
4343 struct ofputil_meter_band
*bands
;
4347 * This is used in instruction validation at flow set-up time,
4348 * as flows may not use non-existing meters.
4349 * This is also used by ofproto-providers to translate OpenFlow meter_ids
4350 * in METER instructions to the corresponding provider meter IDs.
4351 * Return value of UINT32_MAX signifies an invalid meter.
4354 ofproto_get_provider_meter_id(const struct ofproto
* ofproto
,
4355 uint32_t of_meter_id
)
4357 if (of_meter_id
&& of_meter_id
<= ofproto
->meter_features
.max_meters
) {
4358 const struct meter
*meter
= ofproto
->meters
[of_meter_id
];
4360 return meter
->provider_meter_id
.uint32
;
4367 meter_update(struct meter
*meter
, const struct ofputil_meter_config
*config
)
4371 meter
->flags
= config
->flags
;
4372 meter
->n_bands
= config
->n_bands
;
4373 meter
->bands
= xmemdup(config
->bands
,
4374 config
->n_bands
* sizeof *meter
->bands
);
4377 static struct meter
*
4378 meter_create(const struct ofputil_meter_config
*config
,
4379 ofproto_meter_id provider_meter_id
)
4381 struct meter
*meter
;
4383 meter
= xzalloc(sizeof *meter
);
4384 meter
->provider_meter_id
= provider_meter_id
;
4385 meter
->created
= time_msec();
4386 list_init(&meter
->rules
);
4388 meter_update(meter
, config
);
4394 meter_delete(struct ofproto
*ofproto
, uint32_t first
, uint32_t last
)
4397 for (mid
= first
; mid
<= last
; ++mid
) {
4398 struct meter
*meter
= ofproto
->meters
[mid
];
4400 ofproto
->meters
[mid
] = NULL
;
4401 ofproto
->ofproto_class
->meter_del(ofproto
,
4402 meter
->provider_meter_id
);
4410 handle_add_meter(struct ofproto
*ofproto
, struct ofputil_meter_mod
*mm
)
4412 ofproto_meter_id provider_meter_id
= { UINT32_MAX
};
4413 struct meter
**meterp
= &ofproto
->meters
[mm
->meter
.meter_id
];
4417 return OFPERR_OFPMMFC_METER_EXISTS
;
4420 error
= ofproto
->ofproto_class
->meter_set(ofproto
, &provider_meter_id
,
4423 ovs_assert(provider_meter_id
.uint32
!= UINT32_MAX
);
4424 *meterp
= meter_create(&mm
->meter
, provider_meter_id
);
4430 handle_modify_meter(struct ofproto
*ofproto
, struct ofputil_meter_mod
*mm
)
4432 struct meter
*meter
= ofproto
->meters
[mm
->meter
.meter_id
];
4436 return OFPERR_OFPMMFC_UNKNOWN_METER
;
4439 error
= ofproto
->ofproto_class
->meter_set(ofproto
,
4440 &meter
->provider_meter_id
,
4442 ovs_assert(meter
->provider_meter_id
.uint32
!= UINT32_MAX
);
4444 meter_update(meter
, &mm
->meter
);
4450 handle_delete_meter(struct ofconn
*ofconn
, const struct ofp_header
*oh
,
4451 struct ofputil_meter_mod
*mm
)
4453 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
4454 uint32_t meter_id
= mm
->meter
.meter_id
;
4455 uint32_t first
, last
;
4458 if (meter_id
== OFPM13_ALL
) {
4460 last
= ofproto
->meter_features
.max_meters
;
4462 if (!meter_id
|| meter_id
> ofproto
->meter_features
.max_meters
) {
4465 first
= last
= meter_id
;
4468 /* First delete the rules that use this meter. If any of those rules are
4469 * currently being modified, postpone the whole operation until later. */
4471 for (meter_id
= first
; meter_id
<= last
; ++meter_id
) {
4472 struct meter
*meter
= ofproto
->meters
[meter_id
];
4473 if (meter
&& !list_is_empty(&meter
->rules
)) {
4476 LIST_FOR_EACH (rule
, meter_list_node
, &meter
->rules
) {
4477 if (rule
->pending
) {
4478 return OFPROTO_POSTPONE
;
4480 list_push_back(&rules
, &rule
->ofproto_node
);
4484 if (!list_is_empty(&rules
)) {
4485 delete_flows__(ofproto
, ofconn
, oh
, &rules
, OFPRR_METER_DELETE
);
4488 /* Delete the meters. */
4489 meter_delete(ofproto
, first
, last
);
4495 handle_meter_mod(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
4497 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
4498 struct ofputil_meter_mod mm
;
4499 uint64_t bands_stub
[256 / 8];
4500 struct ofpbuf bands
;
4504 error
= reject_slave_controller(ofconn
);
4509 ofpbuf_use_stub(&bands
, bands_stub
, sizeof bands_stub
);
4511 error
= ofputil_decode_meter_mod(oh
, &mm
, &bands
);
4513 goto exit_free_bands
;
4516 meter_id
= mm
.meter
.meter_id
;
4518 if (mm
.command
!= OFPMC13_DELETE
) {
4519 /* Fails also when meters are not implemented by the provider. */
4520 if (!meter_id
|| meter_id
> ofproto
->meter_features
.max_meters
) {
4521 error
= OFPERR_OFPMMFC_INVALID_METER
;
4522 goto exit_free_bands
;
4524 if (mm
.meter
.n_bands
> ofproto
->meter_features
.max_bands
) {
4525 error
= OFPERR_OFPMMFC_OUT_OF_BANDS
;
4526 goto exit_free_bands
;
4530 switch (mm
.command
) {
4532 error
= handle_add_meter(ofproto
, &mm
);
4535 case OFPMC13_MODIFY
:
4536 error
= handle_modify_meter(ofproto
, &mm
);
4539 case OFPMC13_DELETE
:
4540 error
= handle_delete_meter(ofconn
, oh
, &mm
);
4544 error
= OFPERR_OFPMMFC_BAD_COMMAND
;
4549 ofpbuf_uninit(&bands
);
4554 handle_meter_features_request(struct ofconn
*ofconn
,
4555 const struct ofp_header
*request
)
4557 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
4558 struct ofputil_meter_features features
;
4561 if (ofproto
->ofproto_class
->meter_get_features
) {
4562 ofproto
->ofproto_class
->meter_get_features(ofproto
, &features
);
4564 memset(&features
, 0, sizeof features
);
4566 b
= ofputil_encode_meter_features_reply(&features
, request
);
4568 ofconn_send_reply(ofconn
, b
);
4573 handle_meter_request(struct ofconn
*ofconn
, const struct ofp_header
*request
,
4576 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
4577 struct list replies
;
4578 uint64_t bands_stub
[256 / 8];
4579 struct ofpbuf bands
;
4580 uint32_t meter_id
, first
, last
;
4582 ofputil_decode_meter_request(request
, &meter_id
);
4584 if (meter_id
== OFPM13_ALL
) {
4586 last
= ofproto
->meter_features
.max_meters
;
4588 if (!meter_id
|| meter_id
> ofproto
->meter_features
.max_meters
||
4589 !ofproto
->meters
[meter_id
]) {
4590 return OFPERR_OFPMMFC_UNKNOWN_METER
;
4592 first
= last
= meter_id
;
4595 ofpbuf_use_stub(&bands
, bands_stub
, sizeof bands_stub
);
4596 ofpmp_init(&replies
, request
);
4598 for (meter_id
= first
; meter_id
<= last
; ++meter_id
) {
4599 struct meter
*meter
= ofproto
->meters
[meter_id
];
4601 continue; /* Skip non-existing meters. */
4603 if (type
== OFPTYPE_METER_STATS_REQUEST
) {
4604 struct ofputil_meter_stats stats
;
4606 stats
.meter_id
= meter_id
;
4608 /* Provider sets the packet and byte counts, we do the rest. */
4609 stats
.flow_count
= list_size(&meter
->rules
);
4610 calc_duration(meter
->created
, time_msec(),
4611 &stats
.duration_sec
, &stats
.duration_nsec
);
4612 stats
.n_bands
= meter
->n_bands
;
4613 ofpbuf_clear(&bands
);
4615 = ofpbuf_put_uninit(&bands
,
4616 meter
->n_bands
* sizeof *stats
.bands
);
4618 if (!ofproto
->ofproto_class
->meter_get(ofproto
,
4619 meter
->provider_meter_id
,
4621 ofputil_append_meter_stats(&replies
, &stats
);
4623 } else { /* type == OFPTYPE_METER_CONFIG_REQUEST */
4624 struct ofputil_meter_config config
;
4626 config
.meter_id
= meter_id
;
4627 config
.flags
= meter
->flags
;
4628 config
.n_bands
= meter
->n_bands
;
4629 config
.bands
= meter
->bands
;
4630 ofputil_append_meter_config(&replies
, &config
);
4634 ofconn_send_replies(ofconn
, &replies
);
4635 ofpbuf_uninit(&bands
);
4640 handle_openflow__(struct ofconn
*ofconn
, const struct ofpbuf
*msg
)
4642 const struct ofp_header
*oh
= msg
->data
;
4646 error
= ofptype_decode(&type
, oh
);
4652 /* OpenFlow requests. */
4653 case OFPTYPE_ECHO_REQUEST
:
4654 return handle_echo_request(ofconn
, oh
);
4656 case OFPTYPE_FEATURES_REQUEST
:
4657 return handle_features_request(ofconn
, oh
);
4659 case OFPTYPE_GET_CONFIG_REQUEST
:
4660 return handle_get_config_request(ofconn
, oh
);
4662 case OFPTYPE_SET_CONFIG
:
4663 return handle_set_config(ofconn
, oh
);
4665 case OFPTYPE_PACKET_OUT
:
4666 return handle_packet_out(ofconn
, oh
);
4668 case OFPTYPE_PORT_MOD
:
4669 return handle_port_mod(ofconn
, oh
);
4671 case OFPTYPE_FLOW_MOD
:
4672 return handle_flow_mod(ofconn
, oh
);
4674 case OFPTYPE_METER_MOD
:
4675 return handle_meter_mod(ofconn
, oh
);
4677 case OFPTYPE_BARRIER_REQUEST
:
4678 return handle_barrier_request(ofconn
, oh
);
4680 case OFPTYPE_ROLE_REQUEST
:
4681 return handle_role_request(ofconn
, oh
);
4683 /* OpenFlow replies. */
4684 case OFPTYPE_ECHO_REPLY
:
4687 /* Nicira extension requests. */
4688 case OFPTYPE_FLOW_MOD_TABLE_ID
:
4689 return handle_nxt_flow_mod_table_id(ofconn
, oh
);
4691 case OFPTYPE_SET_FLOW_FORMAT
:
4692 return handle_nxt_set_flow_format(ofconn
, oh
);
4694 case OFPTYPE_SET_PACKET_IN_FORMAT
:
4695 return handle_nxt_set_packet_in_format(ofconn
, oh
);
4697 case OFPTYPE_SET_CONTROLLER_ID
:
4698 return handle_nxt_set_controller_id(ofconn
, oh
);
4700 case OFPTYPE_FLOW_AGE
:
4701 /* Nothing to do. */
4704 case OFPTYPE_FLOW_MONITOR_CANCEL
:
4705 return handle_flow_monitor_cancel(ofconn
, oh
);
4707 case OFPTYPE_SET_ASYNC_CONFIG
:
4708 return handle_nxt_set_async_config(ofconn
, oh
);
4710 /* Statistics requests. */
4711 case OFPTYPE_DESC_STATS_REQUEST
:
4712 return handle_desc_stats_request(ofconn
, oh
);
4714 case OFPTYPE_FLOW_STATS_REQUEST
:
4715 return handle_flow_stats_request(ofconn
, oh
);
4717 case OFPTYPE_AGGREGATE_STATS_REQUEST
:
4718 return handle_aggregate_stats_request(ofconn
, oh
);
4720 case OFPTYPE_TABLE_STATS_REQUEST
:
4721 return handle_table_stats_request(ofconn
, oh
);
4723 case OFPTYPE_PORT_STATS_REQUEST
:
4724 return handle_port_stats_request(ofconn
, oh
);
4726 case OFPTYPE_QUEUE_STATS_REQUEST
:
4727 return handle_queue_stats_request(ofconn
, oh
);
4729 case OFPTYPE_PORT_DESC_STATS_REQUEST
:
4730 return handle_port_desc_stats_request(ofconn
, oh
);
4732 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST
:
4733 return handle_flow_monitor_request(ofconn
, oh
);
4735 case OFPTYPE_METER_STATS_REQUEST
:
4736 case OFPTYPE_METER_CONFIG_STATS_REQUEST
:
4737 return handle_meter_request(ofconn
, oh
, type
);
4739 case OFPTYPE_METER_FEATURES_STATS_REQUEST
:
4740 return handle_meter_features_request(ofconn
, oh
);
4742 /* FIXME: Change the following once they are implemented: */
4743 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST
:
4744 case OFPTYPE_GET_ASYNC_REQUEST
:
4745 case OFPTYPE_GROUP_STATS_REQUEST
:
4746 case OFPTYPE_GROUP_DESC_STATS_REQUEST
:
4747 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST
:
4748 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST
:
4749 return OFPERR_OFPBRC_BAD_TYPE
;
4753 case OFPTYPE_FEATURES_REPLY
:
4754 case OFPTYPE_GET_CONFIG_REPLY
:
4755 case OFPTYPE_PACKET_IN
:
4756 case OFPTYPE_FLOW_REMOVED
:
4757 case OFPTYPE_PORT_STATUS
:
4758 case OFPTYPE_BARRIER_REPLY
:
4759 case OFPTYPE_QUEUE_GET_CONFIG_REPLY
:
4760 case OFPTYPE_DESC_STATS_REPLY
:
4761 case OFPTYPE_FLOW_STATS_REPLY
:
4762 case OFPTYPE_QUEUE_STATS_REPLY
:
4763 case OFPTYPE_PORT_STATS_REPLY
:
4764 case OFPTYPE_TABLE_STATS_REPLY
:
4765 case OFPTYPE_AGGREGATE_STATS_REPLY
:
4766 case OFPTYPE_PORT_DESC_STATS_REPLY
:
4767 case OFPTYPE_ROLE_REPLY
:
4768 case OFPTYPE_FLOW_MONITOR_PAUSED
:
4769 case OFPTYPE_FLOW_MONITOR_RESUMED
:
4770 case OFPTYPE_FLOW_MONITOR_STATS_REPLY
:
4771 case OFPTYPE_GET_ASYNC_REPLY
:
4772 case OFPTYPE_GROUP_STATS_REPLY
:
4773 case OFPTYPE_GROUP_DESC_STATS_REPLY
:
4774 case OFPTYPE_GROUP_FEATURES_STATS_REPLY
:
4775 case OFPTYPE_METER_STATS_REPLY
:
4776 case OFPTYPE_METER_CONFIG_STATS_REPLY
:
4777 case OFPTYPE_METER_FEATURES_STATS_REPLY
:
4778 case OFPTYPE_TABLE_FEATURES_STATS_REPLY
:
4780 return OFPERR_OFPBRC_BAD_TYPE
;
4785 handle_openflow(struct ofconn
*ofconn
, const struct ofpbuf
*ofp_msg
)
4787 int error
= handle_openflow__(ofconn
, ofp_msg
);
4788 if (error
&& error
!= OFPROTO_POSTPONE
) {
4789 ofconn_send_error(ofconn
, ofp_msg
->data
, error
);
4791 COVERAGE_INC(ofproto_recv_openflow
);
4792 return error
!= OFPROTO_POSTPONE
;
4795 /* Asynchronous operations. */
4797 /* Creates and returns a new ofopgroup that is not associated with any
4798 * OpenFlow connection.
4800 * The caller should add operations to the returned group with
4801 * ofoperation_create() and then submit it with ofopgroup_submit(). */
4802 static struct ofopgroup
*
4803 ofopgroup_create_unattached(struct ofproto
*ofproto
)
4805 struct ofopgroup
*group
= xzalloc(sizeof *group
);
4806 group
->ofproto
= ofproto
;
4807 list_init(&group
->ofproto_node
);
4808 list_init(&group
->ops
);
4809 list_init(&group
->ofconn_node
);
4813 /* Creates and returns a new ofopgroup for 'ofproto'.
4815 * If 'ofconn' is NULL, the new ofopgroup is not associated with any OpenFlow
4816 * connection. The 'request' and 'buffer_id' arguments are ignored.
4818 * If 'ofconn' is nonnull, then the new ofopgroup is associated with 'ofconn'.
4819 * If the ofopgroup eventually fails, then the error reply will include
4820 * 'request'. If the ofopgroup eventually succeeds, then the packet with
4821 * buffer id 'buffer_id' on 'ofconn' will be sent by 'ofconn''s ofproto.
4823 * The caller should add operations to the returned group with
4824 * ofoperation_create() and then submit it with ofopgroup_submit(). */
4825 static struct ofopgroup
*
4826 ofopgroup_create(struct ofproto
*ofproto
, struct ofconn
*ofconn
,
4827 const struct ofp_header
*request
, uint32_t buffer_id
)
4829 struct ofopgroup
*group
= ofopgroup_create_unattached(ofproto
);
4831 size_t request_len
= ntohs(request
->length
);
4833 ovs_assert(ofconn_get_ofproto(ofconn
) == ofproto
);
4835 ofconn_add_opgroup(ofconn
, &group
->ofconn_node
);
4836 group
->ofconn
= ofconn
;
4837 group
->request
= xmemdup(request
, MIN(request_len
, 64));
4838 group
->buffer_id
= buffer_id
;
4843 /* Submits 'group' for processing.
4845 * If 'group' contains no operations (e.g. none were ever added, or all of the
4846 * ones that were added completed synchronously), then it is destroyed
4847 * immediately. Otherwise it is added to the ofproto's list of pending
4850 ofopgroup_submit(struct ofopgroup
*group
)
4852 if (!group
->n_running
) {
4853 ofopgroup_complete(group
);
4855 list_push_back(&group
->ofproto
->pending
, &group
->ofproto_node
);
4856 group
->ofproto
->n_pending
++;
4861 ofopgroup_complete(struct ofopgroup
*group
)
4863 struct ofproto
*ofproto
= group
->ofproto
;
4865 struct ofconn
*abbrev_ofconn
;
4866 ovs_be32 abbrev_xid
;
4868 struct ofoperation
*op
, *next_op
;
4871 ovs_assert(!group
->n_running
);
4874 LIST_FOR_EACH (op
, group_node
, &group
->ops
) {
4881 if (!error
&& group
->ofconn
&& group
->buffer_id
!= UINT32_MAX
) {
4882 LIST_FOR_EACH (op
, group_node
, &group
->ops
) {
4883 if (op
->type
!= OFOPERATION_DELETE
) {
4884 struct ofpbuf
*packet
;
4887 error
= ofconn_pktbuf_retrieve(group
->ofconn
, group
->buffer_id
,
4891 error
= rule_execute(op
->rule
, in_port
, packet
);
4898 if (!error
&& !list_is_empty(&group
->ofconn_node
)) {
4899 abbrev_ofconn
= group
->ofconn
;
4900 abbrev_xid
= group
->request
->xid
;
4902 abbrev_ofconn
= NULL
;
4903 abbrev_xid
= htonl(0);
4905 LIST_FOR_EACH_SAFE (op
, next_op
, group_node
, &group
->ops
) {
4906 struct rule
*rule
= op
->rule
;
4908 /* We generally want to report the change to active OpenFlow flow
4909 monitors (e.g. NXST_FLOW_MONITOR). There are three exceptions:
4911 - The operation failed.
4913 - The affected rule is not visible to controllers.
4915 - The operation's only effect was to update rule->modified. */
4917 || ofproto_rule_is_hidden(rule
)
4918 || (op
->type
== OFOPERATION_MODIFY
4920 && rule
->flow_cookie
== op
->flow_cookie
))) {
4921 /* Check that we can just cast from ofoperation_type to
4922 * nx_flow_update_event. */
4923 BUILD_ASSERT_DECL((enum nx_flow_update_event
) OFOPERATION_ADD
4925 BUILD_ASSERT_DECL((enum nx_flow_update_event
) OFOPERATION_DELETE
4927 BUILD_ASSERT_DECL((enum nx_flow_update_event
) OFOPERATION_MODIFY
4930 ofmonitor_report(ofproto
->connmgr
, rule
,
4931 (enum nx_flow_update_event
) op
->type
,
4932 op
->reason
, abbrev_ofconn
, abbrev_xid
);
4935 rule
->pending
= NULL
;
4938 case OFOPERATION_ADD
:
4942 ofproto_rule_destroy__(op
->victim
);
4943 vid_mask
= minimask_get_vid_mask(&rule
->cr
.match
.mask
);
4944 if (vid_mask
== VLAN_VID_MASK
) {
4945 if (ofproto
->vlan_bitmap
) {
4946 uint16_t vid
= miniflow_get_vid(&rule
->cr
.match
.flow
);
4947 if (!bitmap_is_set(ofproto
->vlan_bitmap
, vid
)) {
4948 bitmap_set1(ofproto
->vlan_bitmap
, vid
);
4949 ofproto
->vlans_changed
= true;
4952 ofproto
->vlans_changed
= true;
4956 oftable_substitute_rule(rule
, op
->victim
);
4957 ofproto_rule_destroy__(rule
);
4961 case OFOPERATION_DELETE
:
4962 ovs_assert(!op
->error
);
4963 ofproto_rule_destroy__(rule
);
4967 case OFOPERATION_MODIFY
:
4969 rule
->modified
= time_msec();
4971 ofproto_rule_change_cookie(ofproto
, rule
, op
->flow_cookie
);
4973 free(rule
->ofpacts
);
4974 rule
->ofpacts
= op
->ofpacts
;
4975 rule
->ofpacts_len
= op
->ofpacts_len
;
4977 op
->ofpacts_len
= 0;
4986 ofoperation_destroy(op
);
4989 ofmonitor_flush(ofproto
->connmgr
);
4991 if (!list_is_empty(&group
->ofproto_node
)) {
4992 ovs_assert(ofproto
->n_pending
> 0);
4993 ofproto
->n_pending
--;
4994 list_remove(&group
->ofproto_node
);
4996 if (!list_is_empty(&group
->ofconn_node
)) {
4997 list_remove(&group
->ofconn_node
);
4999 ofconn_send_error(group
->ofconn
, group
->request
, error
);
5001 connmgr_retry(ofproto
->connmgr
);
5003 free(group
->request
);
5007 /* Initiates a new operation on 'rule', of the specified 'type', within
5008 * 'group'. Prior to calling, 'rule' must not have any pending operation.
5010 * For a 'type' of OFOPERATION_DELETE, 'reason' should specify the reason that
5011 * the flow is being deleted. For other 'type's, 'reason' is ignored (use 0).
5013 * Returns the newly created ofoperation (which is also available as
5014 * rule->pending). */
5015 static struct ofoperation
*
5016 ofoperation_create(struct ofopgroup
*group
, struct rule
*rule
,
5017 enum ofoperation_type type
,
5018 enum ofp_flow_removed_reason reason
)
5020 struct ofproto
*ofproto
= group
->ofproto
;
5021 struct ofoperation
*op
;
5023 ovs_assert(!rule
->pending
);
5025 op
= rule
->pending
= xzalloc(sizeof *op
);
5027 list_push_back(&group
->ops
, &op
->group_node
);
5030 op
->reason
= reason
;
5031 op
->flow_cookie
= rule
->flow_cookie
;
5035 if (type
== OFOPERATION_DELETE
) {
5036 hmap_insert(&ofproto
->deletions
, &op
->hmap_node
,
5037 cls_rule_hash(&rule
->cr
, rule
->table_id
));
5044 ofoperation_destroy(struct ofoperation
*op
)
5046 struct ofopgroup
*group
= op
->group
;
5049 op
->rule
->pending
= NULL
;
5051 if (op
->type
== OFOPERATION_DELETE
) {
5052 hmap_remove(&group
->ofproto
->deletions
, &op
->hmap_node
);
5054 list_remove(&op
->group_node
);
5059 /* Indicates that 'op' completed with status 'error', which is either 0 to
5060 * indicate success or an OpenFlow error code on failure.
5062 * If 'error' is 0, indicating success, the operation will be committed
5063 * permanently to the flow table. There is one interesting subcase:
5065 * - If 'op' is an "add flow" operation that is replacing an existing rule in
5066 * the flow table (the "victim" rule) by a new one, then the caller must
5067 * have uninitialized any derived state in the victim rule, as in step 5 in
5068 * the "Life Cycle" in ofproto/ofproto-provider.h. ofoperation_complete()
5069 * performs steps 6 and 7 for the victim rule, most notably by calling its
5070 * ->rule_dealloc() function.
5072 * If 'error' is nonzero, then generally the operation will be rolled back:
5074 * - If 'op' is an "add flow" operation, ofproto removes the new rule or
5075 * restores the original rule. The caller must have uninitialized any
5076 * derived state in the new rule, as in step 5 of in the "Life Cycle" in
5077 * ofproto/ofproto-provider.h. ofoperation_complete() performs steps 6 and
5078 * and 7 for the new rule, calling its ->rule_dealloc() function.
5080 * - If 'op' is a "modify flow" operation, ofproto restores the original
5083 * - 'op' must not be a "delete flow" operation. Removing a rule is not
5084 * allowed to fail. It must always succeed.
5086 * Please see the large comment in ofproto/ofproto-provider.h titled
5087 * "Asynchronous Operation Support" for more information. */
5089 ofoperation_complete(struct ofoperation
*op
, enum ofperr error
)
5091 struct ofopgroup
*group
= op
->group
;
5093 ovs_assert(op
->rule
->pending
== op
);
5094 ovs_assert(group
->n_running
> 0);
5095 ovs_assert(!error
|| op
->type
!= OFOPERATION_DELETE
);
5098 if (!--group
->n_running
&& !list_is_empty(&group
->ofproto_node
)) {
5099 ofopgroup_complete(group
);
5104 ofoperation_get_victim(struct ofoperation
*op
)
5106 ovs_assert(op
->type
== OFOPERATION_ADD
);
5111 pick_datapath_id(const struct ofproto
*ofproto
)
5113 const struct ofport
*port
;
5115 port
= ofproto_get_port(ofproto
, OFPP_LOCAL
);
5117 uint8_t ea
[ETH_ADDR_LEN
];
5120 error
= netdev_get_etheraddr(port
->netdev
, ea
);
5122 return eth_addr_to_uint64(ea
);
5124 VLOG_WARN("%s: could not get MAC address for %s (%s)",
5125 ofproto
->name
, netdev_get_name(port
->netdev
),
5126 ovs_strerror(error
));
5128 return ofproto
->fallback_dpid
;
5132 pick_fallback_dpid(void)
5134 uint8_t ea
[ETH_ADDR_LEN
];
5135 eth_addr_nicira_random(ea
);
5136 return eth_addr_to_uint64(ea
);
5139 /* Table overflow policy. */
5141 /* Chooses and updates 'rulep' with a rule to evict from 'table'. Sets 'rulep'
5142 * to NULL if the table is not configured to evict rules or if the table
5143 * contains no evictable rules. (Rules with a readlock on their evict rwlock,
5144 * or with no timeouts are not evictable.) */
5146 choose_rule_to_evict(struct oftable
*table
, struct rule
**rulep
)
5148 struct eviction_group
*evg
;
5151 if (!table
->eviction_fields
) {
5155 /* In the common case, the outer and inner loops here will each be entered
5158 * - The inner loop normally "return"s in its first iteration. If the
5159 * eviction group has any evictable rules, then it always returns in
5162 * - The outer loop only iterates more than once if the largest eviction
5163 * group has no evictable rules.
5165 * - The outer loop can exit only if table's 'max_flows' is all filled up
5166 * by unevictable rules. */
5167 HEAP_FOR_EACH (evg
, size_node
, &table
->eviction_groups_by_size
) {
5170 HEAP_FOR_EACH (rule
, evg_node
, &evg
->rules
) {
5171 if (!ovs_rwlock_trywrlock(&rule
->evict
)) {
5181 /* Searches 'ofproto' for tables that have more flows than their configured
5182 * maximum and that have flow eviction enabled, and evicts as many flows as
5183 * necessary and currently feasible from them.
5185 * This triggers only when an OpenFlow table has N flows in it and then the
5186 * client configures a maximum number of flows less than N. */
5188 ofproto_evict(struct ofproto
*ofproto
)
5190 struct ofopgroup
*group
;
5191 struct oftable
*table
;
5193 group
= ofopgroup_create_unattached(ofproto
);
5194 OFPROTO_FOR_EACH_TABLE (table
, ofproto
) {
5195 while (table
->eviction_fields
) {
5199 ovs_rwlock_rdlock(&table
->cls
.rwlock
);
5200 n_rules
= classifier_count(&table
->cls
);
5201 ovs_rwlock_unlock(&table
->cls
.rwlock
);
5203 if (n_rules
<= table
->max_flows
) {
5207 if (!choose_rule_to_evict(table
, &rule
)) {
5211 if (rule
->pending
) {
5212 ovs_rwlock_unlock(&rule
->evict
);
5216 ofoperation_create(group
, rule
,
5217 OFOPERATION_DELETE
, OFPRR_EVICTION
);
5218 oftable_remove_rule(rule
);
5219 ofproto
->ofproto_class
->rule_destruct(rule
);
5222 ofopgroup_submit(group
);
5225 /* Eviction groups. */
5227 /* Returns the priority to use for an eviction_group that contains 'n_rules'
5228 * rules. The priority contains low-order random bits to ensure that eviction
5229 * groups with the same number of rules are prioritized randomly. */
5231 eviction_group_priority(size_t n_rules
)
5233 uint16_t size
= MIN(UINT16_MAX
, n_rules
);
5234 return (size
<< 16) | random_uint16();
5237 /* Updates 'evg', an eviction_group within 'table', following a change that
5238 * adds or removes rules in 'evg'. */
5240 eviction_group_resized(struct oftable
*table
, struct eviction_group
*evg
)
5242 heap_change(&table
->eviction_groups_by_size
, &evg
->size_node
,
5243 eviction_group_priority(heap_count(&evg
->rules
)));
5246 /* Destroys 'evg', an eviction_group within 'table':
5248 * - Removes all the rules, if any, from 'evg'. (It doesn't destroy the
5249 * rules themselves, just removes them from the eviction group.)
5251 * - Removes 'evg' from 'table'.
5255 eviction_group_destroy(struct oftable
*table
, struct eviction_group
*evg
)
5257 while (!heap_is_empty(&evg
->rules
)) {
5260 rule
= CONTAINER_OF(heap_pop(&evg
->rules
), struct rule
, evg_node
);
5261 rule
->eviction_group
= NULL
;
5263 hmap_remove(&table
->eviction_groups_by_id
, &evg
->id_node
);
5264 heap_remove(&table
->eviction_groups_by_size
, &evg
->size_node
);
5265 heap_destroy(&evg
->rules
);
5269 /* Removes 'rule' from its eviction group, if any. */
5271 eviction_group_remove_rule(struct rule
*rule
)
5273 if (rule
->eviction_group
) {
5274 struct oftable
*table
= &rule
->ofproto
->tables
[rule
->table_id
];
5275 struct eviction_group
*evg
= rule
->eviction_group
;
5277 rule
->eviction_group
= NULL
;
5278 heap_remove(&evg
->rules
, &rule
->evg_node
);
5279 if (heap_is_empty(&evg
->rules
)) {
5280 eviction_group_destroy(table
, evg
);
5282 eviction_group_resized(table
, evg
);
5287 /* Hashes the 'rule''s values for the eviction_fields of 'rule''s table, and
5288 * returns the hash value. */
5290 eviction_group_hash_rule(struct rule
*rule
)
5292 struct oftable
*table
= &rule
->ofproto
->tables
[rule
->table_id
];
5293 const struct mf_subfield
*sf
;
5297 hash
= table
->eviction_group_id_basis
;
5298 miniflow_expand(&rule
->cr
.match
.flow
, &flow
);
5299 for (sf
= table
->eviction_fields
;
5300 sf
< &table
->eviction_fields
[table
->n_eviction_fields
];
5303 if (mf_are_prereqs_ok(sf
->field
, &flow
)) {
5304 union mf_value value
;
5306 mf_get_value(sf
->field
, &flow
, &value
);
5308 bitwise_zero(&value
, sf
->field
->n_bytes
, 0, sf
->ofs
);
5310 if (sf
->ofs
+ sf
->n_bits
< sf
->field
->n_bytes
* 8) {
5311 unsigned int start
= sf
->ofs
+ sf
->n_bits
;
5312 bitwise_zero(&value
, sf
->field
->n_bytes
, start
,
5313 sf
->field
->n_bytes
* 8 - start
);
5315 hash
= hash_bytes(&value
, sf
->field
->n_bytes
, hash
);
5317 hash
= hash_int(hash
, 0);
5324 /* Returns an eviction group within 'table' with the given 'id', creating one
5326 static struct eviction_group
*
5327 eviction_group_find(struct oftable
*table
, uint32_t id
)
5329 struct eviction_group
*evg
;
5331 HMAP_FOR_EACH_WITH_HASH (evg
, id_node
, id
, &table
->eviction_groups_by_id
) {
5335 evg
= xmalloc(sizeof *evg
);
5336 hmap_insert(&table
->eviction_groups_by_id
, &evg
->id_node
, id
);
5337 heap_insert(&table
->eviction_groups_by_size
, &evg
->size_node
,
5338 eviction_group_priority(0));
5339 heap_init(&evg
->rules
);
5344 /* Returns an eviction priority for 'rule'. The return value should be
5345 * interpreted so that higher priorities make a rule more attractive candidates
5348 rule_eviction_priority(struct rule
*rule
)
5350 long long int hard_expiration
;
5351 long long int idle_expiration
;
5352 long long int expiration
;
5353 uint32_t expiration_offset
;
5355 /* Calculate time of expiration. */
5356 ovs_mutex_lock(&rule
->timeout_mutex
);
5357 hard_expiration
= (rule
->hard_timeout
5358 ? rule
->modified
+ rule
->hard_timeout
* 1000
5360 idle_expiration
= (rule
->idle_timeout
5361 ? rule
->used
+ rule
->idle_timeout
* 1000
5363 expiration
= MIN(hard_expiration
, idle_expiration
);
5364 ovs_mutex_unlock(&rule
->timeout_mutex
);
5365 if (expiration
== LLONG_MAX
) {
5369 /* Calculate the time of expiration as a number of (approximate) seconds
5370 * after program startup.
5372 * This should work OK for program runs that last UINT32_MAX seconds or
5373 * less. Therefore, please restart OVS at least once every 136 years. */
5374 expiration_offset
= (expiration
>> 10) - (time_boot_msec() >> 10);
5376 /* Invert the expiration offset because we're using a max-heap. */
5377 return UINT32_MAX
- expiration_offset
;
5380 /* Adds 'rule' to an appropriate eviction group for its oftable's
5381 * configuration. Does nothing if 'rule''s oftable doesn't have eviction
5382 * enabled, or if 'rule' is a permanent rule (one that will never expire on its
5385 * The caller must ensure that 'rule' is not already in an eviction group. */
5387 eviction_group_add_rule(struct rule
*rule
)
5389 struct ofproto
*ofproto
= rule
->ofproto
;
5390 struct oftable
*table
= &ofproto
->tables
[rule
->table_id
];
5393 ovs_mutex_lock(&rule
->timeout_mutex
);
5394 has_timeout
= rule
->hard_timeout
|| rule
->idle_timeout
;
5395 ovs_mutex_unlock(&rule
->timeout_mutex
);
5397 if (table
->eviction_fields
&& has_timeout
) {
5398 struct eviction_group
*evg
;
5400 evg
= eviction_group_find(table
, eviction_group_hash_rule(rule
));
5402 rule
->eviction_group
= evg
;
5403 heap_insert(&evg
->rules
, &rule
->evg_node
,
5404 rule_eviction_priority(rule
));
5405 eviction_group_resized(table
, evg
);
5411 /* Initializes 'table'. */
5413 oftable_init(struct oftable
*table
)
5415 memset(table
, 0, sizeof *table
);
5416 classifier_init(&table
->cls
);
5417 table
->max_flows
= UINT_MAX
;
5420 /* Destroys 'table', including its classifier and eviction groups.
5422 * The caller is responsible for freeing 'table' itself. */
5424 oftable_destroy(struct oftable
*table
)
5426 ovs_rwlock_rdlock(&table
->cls
.rwlock
);
5427 ovs_assert(classifier_is_empty(&table
->cls
));
5428 ovs_rwlock_unlock(&table
->cls
.rwlock
);
5429 oftable_disable_eviction(table
);
5430 classifier_destroy(&table
->cls
);
5434 /* Changes the name of 'table' to 'name'. If 'name' is NULL or the empty
5435 * string, then 'table' will use its default name.
5437 * This only affects the name exposed for a table exposed through the OpenFlow
5438 * OFPST_TABLE (as printed by "ovs-ofctl dump-tables"). */
5440 oftable_set_name(struct oftable
*table
, const char *name
)
5442 if (name
&& name
[0]) {
5443 int len
= strnlen(name
, OFP_MAX_TABLE_NAME_LEN
);
5444 if (!table
->name
|| strncmp(name
, table
->name
, len
)) {
5446 table
->name
= xmemdup0(name
, len
);
5454 /* oftables support a choice of two policies when adding a rule would cause the
5455 * number of flows in the table to exceed the configured maximum number: either
5456 * they can refuse to add the new flow or they can evict some existing flow.
5457 * This function configures the former policy on 'table'. */
5459 oftable_disable_eviction(struct oftable
*table
)
5461 if (table
->eviction_fields
) {
5462 struct eviction_group
*evg
, *next
;
5464 HMAP_FOR_EACH_SAFE (evg
, next
, id_node
,
5465 &table
->eviction_groups_by_id
) {
5466 eviction_group_destroy(table
, evg
);
5468 hmap_destroy(&table
->eviction_groups_by_id
);
5469 heap_destroy(&table
->eviction_groups_by_size
);
5471 free(table
->eviction_fields
);
5472 table
->eviction_fields
= NULL
;
5473 table
->n_eviction_fields
= 0;
5477 /* oftables support a choice of two policies when adding a rule would cause the
5478 * number of flows in the table to exceed the configured maximum number: either
5479 * they can refuse to add the new flow or they can evict some existing flow.
5480 * This function configures the latter policy on 'table', with fairness based
5481 * on the values of the 'n_fields' fields specified in 'fields'. (Specifying
5482 * 'n_fields' as 0 disables fairness.) */
5484 oftable_enable_eviction(struct oftable
*table
,
5485 const struct mf_subfield
*fields
, size_t n_fields
)
5487 struct cls_cursor cursor
;
5490 if (table
->eviction_fields
5491 && n_fields
== table
->n_eviction_fields
5493 || !memcmp(fields
, table
->eviction_fields
,
5494 n_fields
* sizeof *fields
))) {
5499 oftable_disable_eviction(table
);
5501 table
->n_eviction_fields
= n_fields
;
5502 table
->eviction_fields
= xmemdup(fields
, n_fields
* sizeof *fields
);
5504 table
->eviction_group_id_basis
= random_uint32();
5505 hmap_init(&table
->eviction_groups_by_id
);
5506 heap_init(&table
->eviction_groups_by_size
);
5508 ovs_rwlock_rdlock(&table
->cls
.rwlock
);
5509 cls_cursor_init(&cursor
, &table
->cls
, NULL
);
5510 CLS_CURSOR_FOR_EACH (rule
, cr
, &cursor
) {
5511 eviction_group_add_rule(rule
);
5513 ovs_rwlock_unlock(&table
->cls
.rwlock
);
5516 /* Removes 'rule' from the oftable that contains it. */
5518 oftable_remove_rule__(struct ofproto
*ofproto
, struct classifier
*cls
,
5520 OVS_REQ_WRLOCK(cls
->rwlock
) OVS_RELEASES(rule
->evict
)
5522 classifier_remove(cls
, &rule
->cr
);
5523 if (rule
->meter_id
) {
5524 list_remove(&rule
->meter_list_node
);
5526 cookies_remove(ofproto
, rule
);
5527 eviction_group_remove_rule(rule
);
5528 ovs_mutex_lock(&ofproto
->expirable_mutex
);
5529 if (!list_is_empty(&rule
->expirable
)) {
5530 list_remove(&rule
->expirable
);
5532 ovs_mutex_unlock(&ofproto
->expirable_mutex
);
5533 if (!list_is_empty(&rule
->meter_list_node
)) {
5534 list_remove(&rule
->meter_list_node
);
5536 ovs_rwlock_unlock(&rule
->evict
);
5540 oftable_remove_rule(struct rule
*rule
)
5542 struct ofproto
*ofproto
= rule
->ofproto
;
5543 struct oftable
*table
= &ofproto
->tables
[rule
->table_id
];
5545 ovs_rwlock_wrlock(&table
->cls
.rwlock
);
5546 oftable_remove_rule__(ofproto
, &table
->cls
, rule
);
5547 ovs_rwlock_unlock(&table
->cls
.rwlock
);
5550 /* Inserts 'rule' into its oftable. Removes any existing rule from 'rule''s
5551 * oftable that has an identical cls_rule. Returns the rule that was removed,
5552 * if any, and otherwise NULL. */
5553 static struct rule
*
5554 oftable_replace_rule(struct rule
*rule
)
5556 struct ofproto
*ofproto
= rule
->ofproto
;
5557 struct oftable
*table
= &ofproto
->tables
[rule
->table_id
];
5558 struct rule
*victim
;
5561 ovs_mutex_lock(&rule
->timeout_mutex
);
5562 may_expire
= rule
->hard_timeout
|| rule
->idle_timeout
;
5563 ovs_mutex_unlock(&rule
->timeout_mutex
);
5566 ovs_mutex_lock(&ofproto
->expirable_mutex
);
5567 list_insert(&ofproto
->expirable
, &rule
->expirable
);
5568 ovs_mutex_unlock(&ofproto
->expirable_mutex
);
5570 cookies_insert(ofproto
, rule
);
5571 if (rule
->meter_id
) {
5572 struct meter
*meter
= ofproto
->meters
[rule
->meter_id
];
5573 list_insert(&meter
->rules
, &rule
->meter_list_node
);
5575 ovs_rwlock_wrlock(&table
->cls
.rwlock
);
5576 victim
= rule_from_cls_rule(classifier_replace(&table
->cls
, &rule
->cr
));
5577 ovs_rwlock_unlock(&table
->cls
.rwlock
);
5579 if (victim
->meter_id
) {
5580 list_remove(&victim
->meter_list_node
);
5582 cookies_remove(ofproto
, victim
);
5584 ovs_mutex_lock(&ofproto
->expirable_mutex
);
5585 if (!list_is_empty(&victim
->expirable
)) {
5586 list_remove(&victim
->expirable
);
5588 ovs_mutex_unlock(&ofproto
->expirable_mutex
);
5589 eviction_group_remove_rule(victim
);
5591 eviction_group_add_rule(rule
);
5595 /* Removes 'old' from its oftable then, if 'new' is nonnull, inserts 'new'. */
5597 oftable_substitute_rule(struct rule
*old
, struct rule
*new)
5600 oftable_replace_rule(new);
5602 ovs_rwlock_wrlock(&old
->evict
);
5603 oftable_remove_rule(old
);
5607 /* unixctl commands. */
5610 ofproto_lookup(const char *name
)
5612 struct ofproto
*ofproto
;
5614 HMAP_FOR_EACH_WITH_HASH (ofproto
, hmap_node
, hash_string(name
, 0),
5616 if (!strcmp(ofproto
->name
, name
)) {
5624 ofproto_unixctl_list(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
5625 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
5627 struct ofproto
*ofproto
;
5631 HMAP_FOR_EACH (ofproto
, hmap_node
, &all_ofprotos
) {
5632 ds_put_format(&results
, "%s\n", ofproto
->name
);
5634 unixctl_command_reply(conn
, ds_cstr(&results
));
5635 ds_destroy(&results
);
5639 ofproto_unixctl_init(void)
5641 static bool registered
;
5647 unixctl_command_register("ofproto/list", "", 0, 0,
5648 ofproto_unixctl_list
, NULL
);
5651 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
5653 * This is deprecated. It is only for compatibility with broken device drivers
5654 * in old versions of Linux that do not properly support VLANs when VLAN
5655 * devices are not used. When broken device drivers are no longer in
5656 * widespread use, we will delete these interfaces. */
5658 /* Sets a 1-bit in the 4096-bit 'vlan_bitmap' for each VLAN ID that is matched
5659 * (exactly) by an OpenFlow rule in 'ofproto'. */
5661 ofproto_get_vlan_usage(struct ofproto
*ofproto
, unsigned long int *vlan_bitmap
)
5663 const struct oftable
*oftable
;
5665 free(ofproto
->vlan_bitmap
);
5666 ofproto
->vlan_bitmap
= bitmap_allocate(4096);
5667 ofproto
->vlans_changed
= false;
5669 OFPROTO_FOR_EACH_TABLE (oftable
, ofproto
) {
5670 const struct cls_table
*table
;
5672 HMAP_FOR_EACH (table
, hmap_node
, &oftable
->cls
.tables
) {
5673 if (minimask_get_vid_mask(&table
->mask
) == VLAN_VID_MASK
) {
5674 const struct cls_rule
*rule
;
5676 HMAP_FOR_EACH (rule
, hmap_node
, &table
->rules
) {
5677 uint16_t vid
= miniflow_get_vid(&rule
->match
.flow
);
5678 bitmap_set1(vlan_bitmap
, vid
);
5679 bitmap_set1(ofproto
->vlan_bitmap
, vid
);
5686 /* Returns true if new VLANs have come into use by the flow table since the
5687 * last call to ofproto_get_vlan_usage().
5689 * We don't track when old VLANs stop being used. */
5691 ofproto_has_vlan_usage_changed(const struct ofproto
*ofproto
)
5693 return ofproto
->vlans_changed
;
5696 /* Configures a VLAN splinter binding between the ports identified by OpenFlow
5697 * port numbers 'vlandev_ofp_port' and 'realdev_ofp_port'. If
5698 * 'realdev_ofp_port' is nonzero, then the VLAN device is enslaved to the real
5699 * device as a VLAN splinter for VLAN ID 'vid'. If 'realdev_ofp_port' is zero,
5700 * then the VLAN device is un-enslaved. */
5702 ofproto_port_set_realdev(struct ofproto
*ofproto
, ofp_port_t vlandev_ofp_port
,
5703 ofp_port_t realdev_ofp_port
, int vid
)
5705 struct ofport
*ofport
;
5708 ovs_assert(vlandev_ofp_port
!= realdev_ofp_port
);
5710 ofport
= ofproto_get_port(ofproto
, vlandev_ofp_port
);
5712 VLOG_WARN("%s: cannot set realdev on nonexistent port %"PRIu16
,
5713 ofproto
->name
, vlandev_ofp_port
);
5717 if (!ofproto
->ofproto_class
->set_realdev
) {
5718 if (!vlandev_ofp_port
) {
5721 VLOG_WARN("%s: vlan splinters not supported", ofproto
->name
);
5725 error
= ofproto
->ofproto_class
->set_realdev(ofport
, realdev_ofp_port
, vid
);
5727 VLOG_WARN("%s: setting realdev on port %"PRIu16
" (%s) failed (%s)",
5728 ofproto
->name
, vlandev_ofp_port
,
5729 netdev_get_name(ofport
->netdev
), ovs_strerror(error
));