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
{
83 /* A single OpenFlow request can execute any number of operations. The
84 * ofopgroup maintain OpenFlow state common to all of the operations, e.g. the
85 * ofconn to which an error reply should be sent if necessary.
87 * ofproto initiates some operations internally. These operations are still
88 * assigned to groups but will not have an associated ofconn. */
90 struct ofproto
*ofproto
; /* Owning ofproto. */
91 struct list ofproto_node
; /* In ofproto's "pending" list. */
92 struct list ops
; /* List of "struct ofoperation"s. */
93 int n_running
; /* Number of ops still pending. */
95 /* Data needed to send OpenFlow reply on failure or to send a buffered
98 * If list_is_empty(ofconn_node) then this ofopgroup never had an
99 * associated ofconn or its ofconn's connection dropped after it initiated
100 * the operation. In the latter case 'ofconn' is a wild pointer that
101 * refers to freed memory, so the 'ofconn' member must be used only if
102 * !list_is_empty(ofconn_node).
104 struct list ofconn_node
; /* In ofconn's list of pending opgroups. */
105 struct ofconn
*ofconn
; /* ofconn for reply (but see note above). */
106 struct ofp_header
*request
; /* Original request (truncated at 64 bytes). */
107 uint32_t buffer_id
; /* Buffer id from original request. */
110 static struct ofopgroup
*ofopgroup_create_unattached(struct ofproto
*);
111 static struct ofopgroup
*ofopgroup_create(struct ofproto
*, struct ofconn
*,
112 const struct ofp_header
*,
114 static void ofopgroup_submit(struct ofopgroup
*);
115 static void ofopgroup_complete(struct ofopgroup
*);
117 /* A single flow table operation. */
119 struct ofopgroup
*group
; /* Owning group. */
120 struct list group_node
; /* In ofopgroup's "ops" list. */
121 struct hmap_node hmap_node
; /* In ofproto's "deletions" hmap. */
122 struct rule
*rule
; /* Rule being operated upon. */
123 enum ofoperation_type type
; /* Type of operation. */
125 /* OFOPERATION_MODIFY, OFOPERATION_REPLACE: The old actions, if the actions
127 struct rule_actions
*actions
;
129 /* OFOPERATION_DELETE. */
130 enum ofp_flow_removed_reason reason
; /* Reason flow was removed. */
132 ovs_be64 flow_cookie
; /* Rule's old flow cookie. */
133 uint16_t idle_timeout
; /* Rule's old idle timeout. */
134 uint16_t hard_timeout
; /* Rule's old hard timeout. */
135 enum ofputil_flow_mod_flags flags
; /* Rule's old flags. */
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_REQUIRES(ofproto_mutex
);
157 static void oftable_remove_rule__(struct ofproto
*, struct rule
*)
158 OVS_REQUIRES(ofproto_mutex
);
159 static void oftable_insert_rule(struct rule
*);
161 /* A set of rules within a single OpenFlow table (oftable) that have the same
162 * values for the oftable's eviction_fields. A rule to be evicted, when one is
163 * needed, is taken from the eviction group that contains the greatest number
166 * An oftable owns any number of eviction groups, each of which contains any
169 * Membership in an eviction group is imprecise, based on the hash of the
170 * oftable's eviction_fields (in the eviction_group's id_node.hash member).
171 * That is, if two rules have different eviction_fields, but those
172 * eviction_fields hash to the same value, then they will belong to the same
173 * eviction_group anyway.
175 * (When eviction is not enabled on an oftable, we don't track any eviction
176 * groups, to save time and space.) */
177 struct eviction_group
{
178 struct hmap_node id_node
; /* In oftable's "eviction_groups_by_id". */
179 struct heap_node size_node
; /* In oftable's "eviction_groups_by_size". */
180 struct heap rules
; /* Contains "struct rule"s. */
183 static bool choose_rule_to_evict(struct oftable
*table
, struct rule
**rulep
);
184 static void ofproto_evict(struct ofproto
*) OVS_EXCLUDED(ofproto_mutex
);
185 static uint32_t rule_eviction_priority(struct rule
*);
186 static void eviction_group_add_rule(struct rule
*);
187 static void eviction_group_remove_rule(struct rule
*);
189 /* Criteria that flow_mod and other operations use for selecting rules on
190 * which to operate. */
191 struct rule_criteria
{
192 /* An OpenFlow table or 255 for all tables. */
195 /* OpenFlow matching criteria. Interpreted different in "loose" way by
196 * collect_rules_loose() and "strict" way by collect_rules_strict(), as
197 * defined in the OpenFlow spec. */
200 /* Matching criteria for the OpenFlow cookie. Consider a bit B in a rule's
201 * cookie and the corresponding bits C in 'cookie' and M in 'cookie_mask'.
202 * The rule will not be selected if M is 1 and B != C. */
204 ovs_be64 cookie_mask
;
206 /* Selection based on actions within a rule:
208 * If out_port != OFPP_ANY, selects only rules that output to out_port.
209 * If out_group != OFPG_ALL, select only rules that output to out_group. */
214 static void rule_criteria_init(struct rule_criteria
*, uint8_t table_id
,
215 const struct match
*match
,
216 unsigned int priority
,
217 ovs_be64 cookie
, ovs_be64 cookie_mask
,
218 ofp_port_t out_port
, uint32_t out_group
);
219 static void rule_criteria_destroy(struct rule_criteria
*);
221 /* A packet that needs to be passed to rule_execute().
223 * (We can't do this immediately from ofopgroup_complete() because that holds
224 * ofproto_mutex, which rule_execute() needs released.) */
225 struct rule_execute
{
226 struct list list_node
; /* In struct ofproto's "rule_executes" list. */
227 struct rule
*rule
; /* Owns a reference to the rule. */
229 struct ofpbuf
*packet
; /* Owns the packet. */
232 static void run_rule_executes(struct ofproto
*) OVS_EXCLUDED(ofproto_mutex
);
233 static void destroy_rule_executes(struct ofproto
*);
236 static void ofport_destroy__(struct ofport
*) OVS_EXCLUDED(ofproto_mutex
);
237 static void ofport_destroy(struct ofport
*);
239 static void update_port(struct ofproto
*, const char *devname
);
240 static int init_ports(struct ofproto
*);
241 static void reinit_ports(struct ofproto
*);
244 static void ofproto_rule_destroy__(struct rule
*);
245 static void ofproto_rule_send_removed(struct rule
*, uint8_t reason
);
246 static bool rule_is_modifiable(const struct rule
*);
249 static enum ofperr
add_flow(struct ofproto
*, struct ofconn
*,
250 struct ofputil_flow_mod
*,
251 const struct ofp_header
*);
252 static enum ofperr
modify_flows__(struct ofproto
*, struct ofconn
*,
253 struct ofputil_flow_mod
*,
254 const struct ofp_header
*,
255 const struct rule_collection
*);
256 static void delete_flow__(struct rule
*rule
, struct ofopgroup
*,
257 enum ofp_flow_removed_reason
)
258 OVS_REQUIRES(ofproto_mutex
);
259 static enum ofperr
add_group(struct ofproto
*, struct ofputil_group_mod
*);
260 static bool handle_openflow(struct ofconn
*, const struct ofpbuf
*);
261 static enum ofperr
handle_flow_mod__(struct ofproto
*, struct ofconn
*,
262 struct ofputil_flow_mod
*,
263 const struct ofp_header
*)
264 OVS_EXCLUDED(ofproto_mutex
);
265 static void calc_duration(long long int start
, long long int now
,
266 uint32_t *sec
, uint32_t *nsec
);
269 static uint64_t pick_datapath_id(const struct ofproto
*);
270 static uint64_t pick_fallback_dpid(void);
271 static void ofproto_destroy__(struct ofproto
*);
272 static void update_mtu(struct ofproto
*, struct ofport
*);
273 static void meter_delete(struct ofproto
*, uint32_t first
, uint32_t last
);
276 static void ofproto_unixctl_init(void);
278 /* All registered ofproto classes, in probe order. */
279 static const struct ofproto_class
**ofproto_classes
;
280 static size_t n_ofproto_classes
;
281 static size_t allocated_ofproto_classes
;
283 /* Global lock that protects all flow table operations. */
284 struct ovs_mutex ofproto_mutex
= OVS_MUTEX_INITIALIZER
;
286 unsigned flow_eviction_threshold
= OFPROTO_FLOW_EVICTION_THRESHOLD_DEFAULT
;
287 unsigned n_handler_threads
;
288 enum ofproto_flow_miss_model flow_miss_model
= OFPROTO_HANDLE_MISS_AUTO
;
290 /* Map from datapath name to struct ofproto, for use by unixctl commands. */
291 static struct hmap all_ofprotos
= HMAP_INITIALIZER(&all_ofprotos
);
293 /* Initial mappings of port to OpenFlow number mappings. */
294 static struct shash init_ofp_ports
= SHASH_INITIALIZER(&init_ofp_ports
);
296 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
298 /* The default value of true waits for flow restore. */
299 static bool flow_restore_wait
= true;
301 /* Must be called to initialize the ofproto library.
303 * The caller may pass in 'iface_hints', which contains an shash of
304 * "iface_hint" elements indexed by the interface's name. The provider
305 * may use these hints to describe the startup configuration in order to
306 * reinitialize its state. The caller owns the provided data, so a
307 * provider will make copies of anything required. An ofproto provider
308 * will remove any existing state that is not described by the hint, and
309 * may choose to remove it all. */
311 ofproto_init(const struct shash
*iface_hints
)
313 struct shash_node
*node
;
316 ofproto_class_register(&ofproto_dpif_class
);
318 /* Make a local copy, since we don't own 'iface_hints' elements. */
319 SHASH_FOR_EACH(node
, iface_hints
) {
320 const struct iface_hint
*orig_hint
= node
->data
;
321 struct iface_hint
*new_hint
= xmalloc(sizeof *new_hint
);
322 const char *br_type
= ofproto_normalize_type(orig_hint
->br_type
);
324 new_hint
->br_name
= xstrdup(orig_hint
->br_name
);
325 new_hint
->br_type
= xstrdup(br_type
);
326 new_hint
->ofp_port
= orig_hint
->ofp_port
;
328 shash_add(&init_ofp_ports
, node
->name
, new_hint
);
331 for (i
= 0; i
< n_ofproto_classes
; i
++) {
332 ofproto_classes
[i
]->init(&init_ofp_ports
);
336 /* 'type' should be a normalized datapath type, as returned by
337 * ofproto_normalize_type(). Returns the corresponding ofproto_class
338 * structure, or a null pointer if there is none registered for 'type'. */
339 static const struct ofproto_class
*
340 ofproto_class_find__(const char *type
)
344 for (i
= 0; i
< n_ofproto_classes
; i
++) {
345 const struct ofproto_class
*class = ofproto_classes
[i
];
350 class->enumerate_types(&types
);
351 found
= sset_contains(&types
, type
);
352 sset_destroy(&types
);
358 VLOG_WARN("unknown datapath type %s", type
);
362 /* Registers a new ofproto class. After successful registration, new ofprotos
363 * of that type can be created using ofproto_create(). */
365 ofproto_class_register(const struct ofproto_class
*new_class
)
369 for (i
= 0; i
< n_ofproto_classes
; i
++) {
370 if (ofproto_classes
[i
] == new_class
) {
375 if (n_ofproto_classes
>= allocated_ofproto_classes
) {
376 ofproto_classes
= x2nrealloc(ofproto_classes
,
377 &allocated_ofproto_classes
,
378 sizeof *ofproto_classes
);
380 ofproto_classes
[n_ofproto_classes
++] = new_class
;
384 /* Unregisters a datapath provider. 'type' must have been previously
385 * registered and not currently be in use by any ofprotos. After
386 * unregistration new datapaths of that type cannot be opened using
387 * ofproto_create(). */
389 ofproto_class_unregister(const struct ofproto_class
*class)
393 for (i
= 0; i
< n_ofproto_classes
; i
++) {
394 if (ofproto_classes
[i
] == class) {
395 for (i
++; i
< n_ofproto_classes
; i
++) {
396 ofproto_classes
[i
- 1] = ofproto_classes
[i
];
402 VLOG_WARN("attempted to unregister an ofproto class that is not "
407 /* Clears 'types' and enumerates all registered ofproto types into it. The
408 * caller must first initialize the sset. */
410 ofproto_enumerate_types(struct sset
*types
)
414 for (i
= 0; i
< n_ofproto_classes
; i
++) {
415 ofproto_classes
[i
]->enumerate_types(types
);
419 /* Returns the fully spelled out name for the given ofproto 'type'.
421 * Normalized type string can be compared with strcmp(). Unnormalized type
422 * string might be the same even if they have different spellings. */
424 ofproto_normalize_type(const char *type
)
426 return type
&& type
[0] ? type
: "system";
429 /* Clears 'names' and enumerates the names of all known created ofprotos with
430 * the given 'type'. The caller must first initialize the sset. Returns 0 if
431 * successful, otherwise a positive errno value.
433 * Some kinds of datapaths might not be practically enumerable. This is not
434 * considered an error. */
436 ofproto_enumerate_names(const char *type
, struct sset
*names
)
438 const struct ofproto_class
*class = ofproto_class_find__(type
);
439 return class ? class->enumerate_names(type
, names
) : EAFNOSUPPORT
;
443 ofproto_create(const char *datapath_name
, const char *datapath_type
,
444 struct ofproto
**ofprotop
)
446 const struct ofproto_class
*class;
447 struct ofproto
*ofproto
;
453 ofproto_unixctl_init();
455 datapath_type
= ofproto_normalize_type(datapath_type
);
456 class = ofproto_class_find__(datapath_type
);
458 VLOG_WARN("could not create datapath %s of unknown type %s",
459 datapath_name
, datapath_type
);
463 ofproto
= class->alloc();
465 VLOG_ERR("failed to allocate datapath %s of type %s",
466 datapath_name
, datapath_type
);
471 ovs_mutex_lock(&ofproto_mutex
);
472 memset(ofproto
, 0, sizeof *ofproto
);
473 ofproto
->ofproto_class
= class;
474 ofproto
->name
= xstrdup(datapath_name
);
475 ofproto
->type
= xstrdup(datapath_type
);
476 hmap_insert(&all_ofprotos
, &ofproto
->hmap_node
,
477 hash_string(ofproto
->name
, 0));
478 ofproto
->datapath_id
= 0;
479 ofproto
->forward_bpdu
= false;
480 ofproto
->fallback_dpid
= pick_fallback_dpid();
481 ofproto
->mfr_desc
= NULL
;
482 ofproto
->hw_desc
= NULL
;
483 ofproto
->sw_desc
= NULL
;
484 ofproto
->serial_desc
= NULL
;
485 ofproto
->dp_desc
= NULL
;
486 ofproto
->frag_handling
= OFPC_FRAG_NORMAL
;
487 hmap_init(&ofproto
->ports
);
488 shash_init(&ofproto
->port_by_name
);
489 simap_init(&ofproto
->ofp_requests
);
490 ofproto
->max_ports
= ofp_to_u16(OFPP_MAX
);
491 ofproto
->eviction_group_timer
= LLONG_MIN
;
492 ofproto
->tables
= NULL
;
493 ofproto
->n_tables
= 0;
494 hindex_init(&ofproto
->cookies
);
495 list_init(&ofproto
->expirable
);
496 ofproto
->connmgr
= connmgr_create(ofproto
, datapath_name
, datapath_name
);
497 ofproto
->state
= S_OPENFLOW
;
498 list_init(&ofproto
->pending
);
499 ofproto
->n_pending
= 0;
500 hmap_init(&ofproto
->deletions
);
501 guarded_list_init(&ofproto
->rule_executes
);
502 ofproto
->n_add
= ofproto
->n_delete
= ofproto
->n_modify
= 0;
503 ofproto
->first_op
= ofproto
->last_op
= LLONG_MIN
;
504 ofproto
->next_op_report
= LLONG_MAX
;
505 ofproto
->op_backoff
= LLONG_MIN
;
506 ofproto
->vlan_bitmap
= NULL
;
507 ofproto
->vlans_changed
= false;
508 ofproto
->min_mtu
= INT_MAX
;
509 ovs_rwlock_init(&ofproto
->groups_rwlock
);
510 hmap_init(&ofproto
->groups
);
511 ovs_mutex_unlock(&ofproto_mutex
);
513 error
= ofproto
->ofproto_class
->construct(ofproto
);
515 VLOG_ERR("failed to open datapath %s: %s",
516 datapath_name
, ovs_strerror(error
));
517 ofproto_destroy__(ofproto
);
521 /* The "max_ports" member should have been set by ->construct(ofproto).
522 * Port 0 is not a valid OpenFlow port, so mark that as unavailable. */
523 ofproto
->ofp_port_ids
= bitmap_allocate(ofproto
->max_ports
);
524 bitmap_set1(ofproto
->ofp_port_ids
, 0);
526 /* Check that hidden tables, if any, are at the end. */
527 ovs_assert(ofproto
->n_tables
);
528 for (i
= 0; i
+ 1 < ofproto
->n_tables
; i
++) {
529 enum oftable_flags flags
= ofproto
->tables
[i
].flags
;
530 enum oftable_flags next_flags
= ofproto
->tables
[i
+ 1].flags
;
532 ovs_assert(!(flags
& OFTABLE_HIDDEN
) || next_flags
& OFTABLE_HIDDEN
);
535 ofproto
->datapath_id
= pick_datapath_id(ofproto
);
538 /* Initialize meters table. */
539 if (ofproto
->ofproto_class
->meter_get_features
) {
540 ofproto
->ofproto_class
->meter_get_features(ofproto
,
541 &ofproto
->meter_features
);
543 memset(&ofproto
->meter_features
, 0, sizeof ofproto
->meter_features
);
545 ofproto
->meters
= xzalloc((ofproto
->meter_features
.max_meters
+ 1)
546 * sizeof(struct meter
*));
552 /* Must be called (only) by an ofproto implementation in its constructor
553 * function. See the large comment on 'construct' in struct ofproto_class for
556 ofproto_init_tables(struct ofproto
*ofproto
, int n_tables
)
558 struct oftable
*table
;
560 ovs_assert(!ofproto
->n_tables
);
561 ovs_assert(n_tables
>= 1 && n_tables
<= 255);
563 ofproto
->n_tables
= n_tables
;
564 ofproto
->tables
= xmalloc(n_tables
* sizeof *ofproto
->tables
);
565 OFPROTO_FOR_EACH_TABLE (table
, ofproto
) {
570 /* To be optionally called (only) by an ofproto implementation in its
571 * constructor function. See the large comment on 'construct' in struct
572 * ofproto_class for details.
574 * Sets the maximum number of ports to 'max_ports'. The ofproto generic layer
575 * will then ensure that actions passed into the ofproto implementation will
576 * not refer to OpenFlow ports numbered 'max_ports' or higher. If this
577 * function is not called, there will be no such restriction.
579 * Reserved ports numbered OFPP_MAX and higher are special and not subject to
580 * the 'max_ports' restriction. */
582 ofproto_init_max_ports(struct ofproto
*ofproto
, uint16_t max_ports
)
584 ovs_assert(max_ports
<= ofp_to_u16(OFPP_MAX
));
585 ofproto
->max_ports
= max_ports
;
589 ofproto_get_datapath_id(const struct ofproto
*ofproto
)
591 return ofproto
->datapath_id
;
595 ofproto_set_datapath_id(struct ofproto
*p
, uint64_t datapath_id
)
597 uint64_t old_dpid
= p
->datapath_id
;
598 p
->datapath_id
= datapath_id
? datapath_id
: pick_datapath_id(p
);
599 if (p
->datapath_id
!= old_dpid
) {
600 /* Force all active connections to reconnect, since there is no way to
601 * notify a controller that the datapath ID has changed. */
602 ofproto_reconnect_controllers(p
);
607 ofproto_set_controllers(struct ofproto
*p
,
608 const struct ofproto_controller
*controllers
,
609 size_t n_controllers
, uint32_t allowed_versions
)
611 connmgr_set_controllers(p
->connmgr
, controllers
, n_controllers
,
616 ofproto_set_fail_mode(struct ofproto
*p
, enum ofproto_fail_mode fail_mode
)
618 connmgr_set_fail_mode(p
->connmgr
, fail_mode
);
621 /* Drops the connections between 'ofproto' and all of its controllers, forcing
622 * them to reconnect. */
624 ofproto_reconnect_controllers(struct ofproto
*ofproto
)
626 connmgr_reconnect(ofproto
->connmgr
);
629 /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'ofproto''s
630 * in-band control should guarantee access, in the same way that in-band
631 * control guarantees access to OpenFlow controllers. */
633 ofproto_set_extra_in_band_remotes(struct ofproto
*ofproto
,
634 const struct sockaddr_in
*extras
, size_t n
)
636 connmgr_set_extra_in_band_remotes(ofproto
->connmgr
, extras
, n
);
639 /* Sets the OpenFlow queue used by flows set up by in-band control on
640 * 'ofproto' to 'queue_id'. If 'queue_id' is negative, then in-band control
641 * flows will use the default queue. */
643 ofproto_set_in_band_queue(struct ofproto
*ofproto
, int queue_id
)
645 connmgr_set_in_band_queue(ofproto
->connmgr
, queue_id
);
648 /* Sets the number of flows at which eviction from the kernel flow table
651 ofproto_set_flow_eviction_threshold(unsigned threshold
)
653 flow_eviction_threshold
= MAX(OFPROTO_FLOW_EVICTION_THRESHOLD_MIN
,
657 /* Sets the path for handling flow misses. */
659 ofproto_set_flow_miss_model(unsigned model
)
661 flow_miss_model
= model
;
664 /* If forward_bpdu is true, the NORMAL action will forward frames with
665 * reserved (e.g. STP) destination Ethernet addresses. if forward_bpdu is false,
666 * the NORMAL action will drop these frames. */
668 ofproto_set_forward_bpdu(struct ofproto
*ofproto
, bool forward_bpdu
)
670 bool old_val
= ofproto
->forward_bpdu
;
671 ofproto
->forward_bpdu
= forward_bpdu
;
672 if (old_val
!= ofproto
->forward_bpdu
) {
673 if (ofproto
->ofproto_class
->forward_bpdu_changed
) {
674 ofproto
->ofproto_class
->forward_bpdu_changed(ofproto
);
679 /* Sets the MAC aging timeout for the OFPP_NORMAL action on 'ofproto' to
680 * 'idle_time', in seconds, and the maximum number of MAC table entries to
683 ofproto_set_mac_table_config(struct ofproto
*ofproto
, unsigned idle_time
,
686 if (ofproto
->ofproto_class
->set_mac_table_config
) {
687 ofproto
->ofproto_class
->set_mac_table_config(ofproto
, idle_time
,
692 /* Sets number of upcall handler threads. The default is
693 * (number of online cores - 2). */
695 ofproto_set_n_handler_threads(unsigned limit
)
698 n_handler_threads
= limit
;
700 int n_proc
= sysconf(_SC_NPROCESSORS_ONLN
);
701 n_handler_threads
= n_proc
> 2 ? n_proc
- 2 : 1;
706 ofproto_set_dp_desc(struct ofproto
*p
, const char *dp_desc
)
709 p
->dp_desc
= dp_desc
? xstrdup(dp_desc
) : NULL
;
713 ofproto_set_snoops(struct ofproto
*ofproto
, const struct sset
*snoops
)
715 return connmgr_set_snoops(ofproto
->connmgr
, snoops
);
719 ofproto_set_netflow(struct ofproto
*ofproto
,
720 const struct netflow_options
*nf_options
)
722 if (nf_options
&& sset_is_empty(&nf_options
->collectors
)) {
726 if (ofproto
->ofproto_class
->set_netflow
) {
727 return ofproto
->ofproto_class
->set_netflow(ofproto
, nf_options
);
729 return nf_options
? EOPNOTSUPP
: 0;
734 ofproto_set_sflow(struct ofproto
*ofproto
,
735 const struct ofproto_sflow_options
*oso
)
737 if (oso
&& sset_is_empty(&oso
->targets
)) {
741 if (ofproto
->ofproto_class
->set_sflow
) {
742 return ofproto
->ofproto_class
->set_sflow(ofproto
, oso
);
744 return oso
? EOPNOTSUPP
: 0;
749 ofproto_set_ipfix(struct ofproto
*ofproto
,
750 const struct ofproto_ipfix_bridge_exporter_options
*bo
,
751 const struct ofproto_ipfix_flow_exporter_options
*fo
,
754 if (ofproto
->ofproto_class
->set_ipfix
) {
755 return ofproto
->ofproto_class
->set_ipfix(ofproto
, bo
, fo
, n_fo
);
757 return (bo
|| fo
) ? EOPNOTSUPP
: 0;
762 ofproto_set_flow_restore_wait(bool flow_restore_wait_db
)
764 flow_restore_wait
= flow_restore_wait_db
;
768 ofproto_get_flow_restore_wait(void)
770 return flow_restore_wait
;
774 /* Spanning Tree Protocol (STP) configuration. */
776 /* Configures STP on 'ofproto' using the settings defined in 's'. If
777 * 's' is NULL, disables STP.
779 * Returns 0 if successful, otherwise a positive errno value. */
781 ofproto_set_stp(struct ofproto
*ofproto
,
782 const struct ofproto_stp_settings
*s
)
784 return (ofproto
->ofproto_class
->set_stp
785 ? ofproto
->ofproto_class
->set_stp(ofproto
, s
)
789 /* Retrieves STP status of 'ofproto' and stores it in 's'. If the
790 * 'enabled' member of 's' is false, then the other members are not
793 * Returns 0 if successful, otherwise a positive errno value. */
795 ofproto_get_stp_status(struct ofproto
*ofproto
,
796 struct ofproto_stp_status
*s
)
798 return (ofproto
->ofproto_class
->get_stp_status
799 ? ofproto
->ofproto_class
->get_stp_status(ofproto
, s
)
803 /* Configures STP on 'ofp_port' of 'ofproto' using the settings defined
804 * in 's'. The caller is responsible for assigning STP port numbers
805 * (using the 'port_num' member in the range of 1 through 255, inclusive)
806 * and ensuring there are no duplicates. If the 's' is NULL, then STP
807 * is disabled on the port.
809 * Returns 0 if successful, otherwise a positive errno value.*/
811 ofproto_port_set_stp(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
812 const struct ofproto_port_stp_settings
*s
)
814 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
816 VLOG_WARN("%s: cannot configure STP on nonexistent port %"PRIu16
,
817 ofproto
->name
, ofp_port
);
821 return (ofproto
->ofproto_class
->set_stp_port
822 ? ofproto
->ofproto_class
->set_stp_port(ofport
, s
)
826 /* Retrieves STP port status of 'ofp_port' on 'ofproto' and stores it in
827 * 's'. If the 'enabled' member in 's' is false, then the other members
828 * are not meaningful.
830 * Returns 0 if successful, otherwise a positive errno value.*/
832 ofproto_port_get_stp_status(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
833 struct ofproto_port_stp_status
*s
)
835 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
837 VLOG_WARN_RL(&rl
, "%s: cannot get STP status on nonexistent "
838 "port %"PRIu16
, ofproto
->name
, ofp_port
);
842 return (ofproto
->ofproto_class
->get_stp_port_status
843 ? ofproto
->ofproto_class
->get_stp_port_status(ofport
, s
)
847 /* Queue DSCP configuration. */
849 /* Registers meta-data associated with the 'n_qdscp' Qualities of Service
850 * 'queues' attached to 'ofport'. This data is not intended to be sufficient
851 * to implement QoS. Instead, it is used to implement features which require
852 * knowledge of what queues exist on a port, and some basic information about
855 * Returns 0 if successful, otherwise a positive errno value. */
857 ofproto_port_set_queues(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
858 const struct ofproto_port_queue
*queues
,
861 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
864 VLOG_WARN("%s: cannot set queues on nonexistent port %"PRIu16
,
865 ofproto
->name
, ofp_port
);
869 return (ofproto
->ofproto_class
->set_queues
870 ? ofproto
->ofproto_class
->set_queues(ofport
, queues
, n_queues
)
874 /* Connectivity Fault Management configuration. */
876 /* Clears the CFM configuration from 'ofp_port' on 'ofproto'. */
878 ofproto_port_clear_cfm(struct ofproto
*ofproto
, ofp_port_t ofp_port
)
880 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
881 if (ofport
&& ofproto
->ofproto_class
->set_cfm
) {
882 ofproto
->ofproto_class
->set_cfm(ofport
, NULL
);
886 /* Configures connectivity fault management on 'ofp_port' in 'ofproto'. Takes
887 * basic configuration from the configuration members in 'cfm', and the remote
888 * maintenance point ID from remote_mpid. Ignores the statistics members of
891 * This function has no effect if 'ofproto' does not have a port 'ofp_port'. */
893 ofproto_port_set_cfm(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
894 const struct cfm_settings
*s
)
896 struct ofport
*ofport
;
899 ofport
= ofproto_get_port(ofproto
, ofp_port
);
901 VLOG_WARN("%s: cannot configure CFM on nonexistent port %"PRIu16
,
902 ofproto
->name
, ofp_port
);
906 /* XXX: For configuration simplicity, we only support one remote_mpid
907 * outside of the CFM module. It's not clear if this is the correct long
908 * term solution or not. */
909 error
= (ofproto
->ofproto_class
->set_cfm
910 ? ofproto
->ofproto_class
->set_cfm(ofport
, s
)
913 VLOG_WARN("%s: CFM configuration on port %"PRIu16
" (%s) failed (%s)",
914 ofproto
->name
, ofp_port
, netdev_get_name(ofport
->netdev
),
915 ovs_strerror(error
));
919 /* Configures BFD on 'ofp_port' in 'ofproto'. This function has no effect if
920 * 'ofproto' does not have a port 'ofp_port'. */
922 ofproto_port_set_bfd(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
923 const struct smap
*cfg
)
925 struct ofport
*ofport
;
928 ofport
= ofproto_get_port(ofproto
, ofp_port
);
930 VLOG_WARN("%s: cannot configure bfd on nonexistent port %"PRIu16
,
931 ofproto
->name
, ofp_port
);
935 error
= (ofproto
->ofproto_class
->set_bfd
936 ? ofproto
->ofproto_class
->set_bfd(ofport
, cfg
)
939 VLOG_WARN("%s: bfd configuration on port %"PRIu16
" (%s) failed (%s)",
940 ofproto
->name
, ofp_port
, netdev_get_name(ofport
->netdev
),
941 ovs_strerror(error
));
945 /* Populates 'status' with key value pairs indicating the status of the BFD
946 * session on 'ofp_port'. This information is intended to be populated in the
947 * OVS database. Has no effect if 'ofp_port' is not na OpenFlow port in
950 ofproto_port_get_bfd_status(struct ofproto
*ofproto
, ofp_port_t ofp_port
,
953 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
954 return (ofport
&& ofproto
->ofproto_class
->get_bfd_status
955 ? ofproto
->ofproto_class
->get_bfd_status(ofport
, status
)
959 /* Checks the status of LACP negotiation for 'ofp_port' within ofproto.
960 * Returns 1 if LACP partner information for 'ofp_port' is up-to-date,
961 * 0 if LACP partner information is not current (generally indicating a
962 * connectivity problem), or -1 if LACP is not enabled on 'ofp_port'. */
964 ofproto_port_is_lacp_current(struct ofproto
*ofproto
, ofp_port_t ofp_port
)
966 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
967 return (ofport
&& ofproto
->ofproto_class
->port_is_lacp_current
968 ? ofproto
->ofproto_class
->port_is_lacp_current(ofport
)
974 /* Registers a "bundle" associated with client data pointer 'aux' in 'ofproto'.
975 * A bundle is the same concept as a Port in OVSDB, that is, it consists of one
976 * or more "slave" devices (Interfaces, in OVSDB) along with a VLAN
977 * configuration plus, if there is more than one slave, a bonding
980 * If 'aux' is already registered then this function updates its configuration
981 * to 's'. Otherwise, this function registers a new bundle.
983 * Bundles only affect the NXAST_AUTOPATH action and output to the OFPP_NORMAL
986 ofproto_bundle_register(struct ofproto
*ofproto
, void *aux
,
987 const struct ofproto_bundle_settings
*s
)
989 return (ofproto
->ofproto_class
->bundle_set
990 ? ofproto
->ofproto_class
->bundle_set(ofproto
, aux
, s
)
994 /* Unregisters the bundle registered on 'ofproto' with auxiliary data 'aux'.
995 * If no such bundle has been registered, this has no effect. */
997 ofproto_bundle_unregister(struct ofproto
*ofproto
, void *aux
)
999 return ofproto_bundle_register(ofproto
, aux
, NULL
);
1003 /* Registers a mirror associated with client data pointer 'aux' in 'ofproto'.
1004 * If 'aux' is already registered then this function updates its configuration
1005 * to 's'. Otherwise, this function registers a new mirror. */
1007 ofproto_mirror_register(struct ofproto
*ofproto
, void *aux
,
1008 const struct ofproto_mirror_settings
*s
)
1010 return (ofproto
->ofproto_class
->mirror_set
1011 ? ofproto
->ofproto_class
->mirror_set(ofproto
, aux
, s
)
1015 /* Unregisters the mirror registered on 'ofproto' with auxiliary data 'aux'.
1016 * If no mirror has been registered, this has no effect. */
1018 ofproto_mirror_unregister(struct ofproto
*ofproto
, void *aux
)
1020 return ofproto_mirror_register(ofproto
, aux
, NULL
);
1023 /* Retrieves statistics from mirror associated with client data pointer
1024 * 'aux' in 'ofproto'. Stores packet and byte counts in 'packets' and
1025 * 'bytes', respectively. If a particular counters is not supported,
1026 * the appropriate argument is set to UINT64_MAX. */
1028 ofproto_mirror_get_stats(struct ofproto
*ofproto
, void *aux
,
1029 uint64_t *packets
, uint64_t *bytes
)
1031 if (!ofproto
->ofproto_class
->mirror_get_stats
) {
1032 *packets
= *bytes
= UINT64_MAX
;
1036 return ofproto
->ofproto_class
->mirror_get_stats(ofproto
, aux
,
1040 /* Configures the VLANs whose bits are set to 1 in 'flood_vlans' as VLANs on
1041 * which all packets are flooded, instead of using MAC learning. If
1042 * 'flood_vlans' is NULL, then MAC learning applies to all VLANs.
1044 * Flood VLANs affect only the treatment of packets output to the OFPP_NORMAL
1047 ofproto_set_flood_vlans(struct ofproto
*ofproto
, unsigned long *flood_vlans
)
1049 return (ofproto
->ofproto_class
->set_flood_vlans
1050 ? ofproto
->ofproto_class
->set_flood_vlans(ofproto
, flood_vlans
)
1054 /* Returns true if 'aux' is a registered bundle that is currently in use as the
1055 * output for a mirror. */
1057 ofproto_is_mirror_output_bundle(const struct ofproto
*ofproto
, void *aux
)
1059 return (ofproto
->ofproto_class
->is_mirror_output_bundle
1060 ? ofproto
->ofproto_class
->is_mirror_output_bundle(ofproto
, aux
)
1064 /* Configuration of OpenFlow tables. */
1066 /* Returns the number of OpenFlow tables in 'ofproto'. */
1068 ofproto_get_n_tables(const struct ofproto
*ofproto
)
1070 return ofproto
->n_tables
;
1073 /* Configures the OpenFlow table in 'ofproto' with id 'table_id' with the
1074 * settings from 's'. 'table_id' must be in the range 0 through the number of
1075 * OpenFlow tables in 'ofproto' minus 1, inclusive.
1077 * For read-only tables, only the name may be configured. */
1079 ofproto_configure_table(struct ofproto
*ofproto
, int table_id
,
1080 const struct ofproto_table_settings
*s
)
1082 struct oftable
*table
;
1084 ovs_assert(table_id
>= 0 && table_id
< ofproto
->n_tables
);
1085 table
= &ofproto
->tables
[table_id
];
1087 oftable_set_name(table
, s
->name
);
1089 if (table
->flags
& OFTABLE_READONLY
) {
1094 oftable_enable_eviction(table
, s
->groups
, s
->n_groups
);
1096 oftable_disable_eviction(table
);
1099 table
->max_flows
= s
->max_flows
;
1100 ovs_rwlock_rdlock(&table
->cls
.rwlock
);
1101 if (classifier_count(&table
->cls
) > table
->max_flows
1102 && table
->eviction_fields
) {
1103 /* 'table' contains more flows than allowed. We might not be able to
1104 * evict them right away because of the asynchronous nature of flow
1105 * table changes. Schedule eviction for later. */
1106 switch (ofproto
->state
) {
1108 ofproto
->state
= S_EVICT
;
1112 /* We're already deleting flows, nothing more to do. */
1116 ovs_rwlock_unlock(&table
->cls
.rwlock
);
1120 ofproto_has_snoops(const struct ofproto
*ofproto
)
1122 return connmgr_has_snoops(ofproto
->connmgr
);
1126 ofproto_get_snoops(const struct ofproto
*ofproto
, struct sset
*snoops
)
1128 connmgr_get_snoops(ofproto
->connmgr
, snoops
);
1132 ofproto_rule_delete__(struct ofproto
*ofproto
, struct rule
*rule
)
1133 OVS_REQUIRES(ofproto_mutex
)
1135 struct ofopgroup
*group
;
1137 ovs_assert(!rule
->pending
);
1139 group
= ofopgroup_create_unattached(ofproto
);
1140 delete_flow__(rule
, group
, OFPRR_DELETE
);
1141 ofopgroup_submit(group
);
1144 /* Deletes 'rule' from 'cls' within 'ofproto'.
1146 * Within an ofproto implementation, this function allows an ofproto
1147 * implementation to destroy any rules that remain when its ->destruct()
1148 * function is called. This function is not suitable for use elsewhere in an
1149 * ofproto implementation.
1151 * This function implements steps 4.4 and 4.5 in the section titled "Rule Life
1152 * Cycle" in ofproto-provider.h. */
1154 ofproto_rule_delete(struct ofproto
*ofproto
, struct rule
*rule
)
1155 OVS_EXCLUDED(ofproto_mutex
)
1157 struct ofopgroup
*group
;
1159 ovs_mutex_lock(&ofproto_mutex
);
1160 ovs_assert(!rule
->pending
);
1162 group
= ofopgroup_create_unattached(ofproto
);
1163 ofoperation_create(group
, rule
, OFOPERATION_DELETE
, OFPRR_DELETE
);
1164 oftable_remove_rule__(ofproto
, rule
);
1165 ofproto
->ofproto_class
->rule_delete(rule
);
1166 ofopgroup_submit(group
);
1168 ovs_mutex_unlock(&ofproto_mutex
);
1172 ofproto_flush__(struct ofproto
*ofproto
)
1173 OVS_EXCLUDED(ofproto_mutex
)
1175 struct oftable
*table
;
1177 if (ofproto
->ofproto_class
->flush
) {
1178 ofproto
->ofproto_class
->flush(ofproto
);
1181 ovs_mutex_lock(&ofproto_mutex
);
1182 OFPROTO_FOR_EACH_TABLE (table
, ofproto
) {
1183 struct rule
*rule
, *next_rule
;
1184 struct cls_cursor cursor
;
1186 if (table
->flags
& OFTABLE_HIDDEN
) {
1190 ovs_rwlock_rdlock(&table
->cls
.rwlock
);
1191 cls_cursor_init(&cursor
, &table
->cls
, NULL
);
1192 ovs_rwlock_unlock(&table
->cls
.rwlock
);
1193 CLS_CURSOR_FOR_EACH_SAFE (rule
, next_rule
, cr
, &cursor
) {
1194 if (!rule
->pending
) {
1195 ofproto_rule_delete__(ofproto
, rule
);
1199 ovs_mutex_unlock(&ofproto_mutex
);
1202 static void delete_group(struct ofproto
*ofproto
, uint32_t group_id
);
1205 ofproto_destroy__(struct ofproto
*ofproto
)
1206 OVS_EXCLUDED(ofproto_mutex
)
1208 struct oftable
*table
;
1210 ovs_assert(list_is_empty(&ofproto
->pending
));
1212 destroy_rule_executes(ofproto
);
1213 guarded_list_destroy(&ofproto
->rule_executes
);
1215 delete_group(ofproto
, OFPG_ALL
);
1216 ovs_rwlock_destroy(&ofproto
->groups_rwlock
);
1217 hmap_destroy(&ofproto
->groups
);
1219 connmgr_destroy(ofproto
->connmgr
);
1221 hmap_remove(&all_ofprotos
, &ofproto
->hmap_node
);
1222 free(ofproto
->name
);
1223 free(ofproto
->type
);
1224 free(ofproto
->mfr_desc
);
1225 free(ofproto
->hw_desc
);
1226 free(ofproto
->sw_desc
);
1227 free(ofproto
->serial_desc
);
1228 free(ofproto
->dp_desc
);
1229 hmap_destroy(&ofproto
->ports
);
1230 shash_destroy(&ofproto
->port_by_name
);
1231 bitmap_free(ofproto
->ofp_port_ids
);
1232 simap_destroy(&ofproto
->ofp_requests
);
1234 OFPROTO_FOR_EACH_TABLE (table
, ofproto
) {
1235 oftable_destroy(table
);
1237 free(ofproto
->tables
);
1239 hmap_destroy(&ofproto
->deletions
);
1241 free(ofproto
->vlan_bitmap
);
1243 ofproto
->ofproto_class
->dealloc(ofproto
);
1247 ofproto_destroy(struct ofproto
*p
)
1248 OVS_EXCLUDED(ofproto_mutex
)
1250 struct ofport
*ofport
, *next_ofport
;
1257 meter_delete(p
, 1, p
->meter_features
.max_meters
);
1258 p
->meter_features
.max_meters
= 0;
1264 HMAP_FOR_EACH_SAFE (ofport
, next_ofport
, hmap_node
, &p
->ports
) {
1265 ofport_destroy(ofport
);
1268 p
->ofproto_class
->destruct(p
);
1269 ofproto_destroy__(p
);
1272 /* Destroys the datapath with the respective 'name' and 'type'. With the Linux
1273 * kernel datapath, for example, this destroys the datapath in the kernel, and
1274 * with the netdev-based datapath, it tears down the data structures that
1275 * represent the datapath.
1277 * The datapath should not be currently open as an ofproto. */
1279 ofproto_delete(const char *name
, const char *type
)
1281 const struct ofproto_class
*class = ofproto_class_find__(type
);
1282 return (!class ? EAFNOSUPPORT
1283 : !class->del
? EACCES
1284 : class->del(type
, name
));
1288 process_port_change(struct ofproto
*ofproto
, int error
, char *devname
)
1290 if (error
== ENOBUFS
) {
1291 reinit_ports(ofproto
);
1292 } else if (!error
) {
1293 update_port(ofproto
, devname
);
1299 ofproto_type_run(const char *datapath_type
)
1301 const struct ofproto_class
*class;
1304 datapath_type
= ofproto_normalize_type(datapath_type
);
1305 class = ofproto_class_find__(datapath_type
);
1307 error
= class->type_run
? class->type_run(datapath_type
) : 0;
1308 if (error
&& error
!= EAGAIN
) {
1309 VLOG_ERR_RL(&rl
, "%s: type_run failed (%s)",
1310 datapath_type
, ovs_strerror(error
));
1316 ofproto_type_run_fast(const char *datapath_type
)
1318 const struct ofproto_class
*class;
1321 datapath_type
= ofproto_normalize_type(datapath_type
);
1322 class = ofproto_class_find__(datapath_type
);
1324 error
= class->type_run_fast
? class->type_run_fast(datapath_type
) : 0;
1325 if (error
&& error
!= EAGAIN
) {
1326 VLOG_ERR_RL(&rl
, "%s: type_run_fast failed (%s)",
1327 datapath_type
, ovs_strerror(error
));
1333 ofproto_type_wait(const char *datapath_type
)
1335 const struct ofproto_class
*class;
1337 datapath_type
= ofproto_normalize_type(datapath_type
);
1338 class = ofproto_class_find__(datapath_type
);
1340 if (class->type_wait
) {
1341 class->type_wait(datapath_type
);
1346 any_pending_ops(const struct ofproto
*p
)
1347 OVS_EXCLUDED(ofproto_mutex
)
1351 ovs_mutex_lock(&ofproto_mutex
);
1352 b
= !list_is_empty(&p
->pending
);
1353 ovs_mutex_unlock(&ofproto_mutex
);
1359 ofproto_run(struct ofproto
*p
)
1361 struct sset changed_netdevs
;
1362 const char *changed_netdev
;
1363 struct ofport
*ofport
;
1366 error
= p
->ofproto_class
->run(p
);
1367 if (error
&& error
!= EAGAIN
) {
1368 VLOG_ERR_RL(&rl
, "%s: run failed (%s)", p
->name
, ovs_strerror(error
));
1371 run_rule_executes(p
);
1373 /* Restore the eviction group heap invariant occasionally. */
1374 if (p
->eviction_group_timer
< time_msec()) {
1377 p
->eviction_group_timer
= time_msec() + 1000;
1379 for (i
= 0; i
< p
->n_tables
; i
++) {
1380 struct oftable
*table
= &p
->tables
[i
];
1381 struct eviction_group
*evg
;
1382 struct cls_cursor cursor
;
1385 if (!table
->eviction_fields
) {
1389 ovs_mutex_lock(&ofproto_mutex
);
1390 HEAP_FOR_EACH (evg
, size_node
, &table
->eviction_groups_by_size
) {
1391 heap_rebuild(&evg
->rules
);
1394 ovs_rwlock_rdlock(&table
->cls
.rwlock
);
1395 cls_cursor_init(&cursor
, &table
->cls
, NULL
);
1396 CLS_CURSOR_FOR_EACH (rule
, cr
, &cursor
) {
1397 if (!rule
->eviction_group
1398 && (rule
->idle_timeout
|| rule
->hard_timeout
)) {
1399 eviction_group_add_rule(rule
);
1402 ovs_rwlock_unlock(&table
->cls
.rwlock
);
1403 ovs_mutex_unlock(&ofproto_mutex
);
1407 if (p
->ofproto_class
->port_poll
) {
1410 while ((error
= p
->ofproto_class
->port_poll(p
, &devname
)) != EAGAIN
) {
1411 process_port_change(p
, error
, devname
);
1415 /* Update OpenFlow port status for any port whose netdev has changed.
1417 * Refreshing a given 'ofport' can cause an arbitrary ofport to be
1418 * destroyed, so it's not safe to update ports directly from the
1419 * HMAP_FOR_EACH loop, or even to use HMAP_FOR_EACH_SAFE. Instead, we
1420 * need this two-phase approach. */
1421 sset_init(&changed_netdevs
);
1422 HMAP_FOR_EACH (ofport
, hmap_node
, &p
->ports
) {
1423 unsigned int change_seq
= netdev_change_seq(ofport
->netdev
);
1424 if (ofport
->change_seq
!= change_seq
) {
1425 ofport
->change_seq
= change_seq
;
1426 sset_add(&changed_netdevs
, netdev_get_name(ofport
->netdev
));
1429 SSET_FOR_EACH (changed_netdev
, &changed_netdevs
) {
1430 update_port(p
, changed_netdev
);
1432 sset_destroy(&changed_netdevs
);
1436 connmgr_run(p
->connmgr
, handle_openflow
);
1440 connmgr_run(p
->connmgr
, NULL
);
1442 if (!any_pending_ops(p
)) {
1443 p
->state
= S_OPENFLOW
;
1448 connmgr_run(p
->connmgr
, NULL
);
1450 if (!any_pending_ops(p
)) {
1451 connmgr_flushed(p
->connmgr
);
1452 p
->state
= S_OPENFLOW
;
1460 if (time_msec() >= p
->next_op_report
) {
1461 long long int ago
= (time_msec() - p
->first_op
) / 1000;
1462 long long int interval
= (p
->last_op
- p
->first_op
) / 1000;
1466 ds_put_format(&s
, "%d flow_mods ",
1467 p
->n_add
+ p
->n_delete
+ p
->n_modify
);
1468 if (interval
== ago
) {
1469 ds_put_format(&s
, "in the last %lld s", ago
);
1470 } else if (interval
) {
1471 ds_put_format(&s
, "in the %lld s starting %lld s ago",
1474 ds_put_format(&s
, "%lld s ago", ago
);
1477 ds_put_cstr(&s
, " (");
1479 ds_put_format(&s
, "%d adds, ", p
->n_add
);
1482 ds_put_format(&s
, "%d deletes, ", p
->n_delete
);
1485 ds_put_format(&s
, "%d modifications, ", p
->n_modify
);
1488 ds_put_char(&s
, ')');
1490 VLOG_INFO("%s: %s", p
->name
, ds_cstr(&s
));
1493 p
->n_add
= p
->n_delete
= p
->n_modify
= 0;
1494 p
->next_op_report
= LLONG_MAX
;
1500 /* Performs periodic activity required by 'ofproto' that needs to be done
1501 * with the least possible latency.
1503 * It makes sense to call this function a couple of times per poll loop, to
1504 * provide a significant performance boost on some benchmarks with the
1505 * ofproto-dpif implementation. */
1507 ofproto_run_fast(struct ofproto
*p
)
1511 error
= p
->ofproto_class
->run_fast
? p
->ofproto_class
->run_fast(p
) : 0;
1512 if (error
&& error
!= EAGAIN
) {
1513 VLOG_ERR_RL(&rl
, "%s: fastpath run failed (%s)",
1514 p
->name
, ovs_strerror(error
));
1520 ofproto_wait(struct ofproto
*p
)
1522 struct ofport
*ofport
;
1524 p
->ofproto_class
->wait(p
);
1525 if (p
->ofproto_class
->port_poll_wait
) {
1526 p
->ofproto_class
->port_poll_wait(p
);
1529 HMAP_FOR_EACH (ofport
, hmap_node
, &p
->ports
) {
1530 if (ofport
->change_seq
!= netdev_change_seq(ofport
->netdev
)) {
1531 poll_immediate_wake();
1537 connmgr_wait(p
->connmgr
, true);
1542 connmgr_wait(p
->connmgr
, false);
1543 if (!any_pending_ops(p
)) {
1544 poll_immediate_wake();
1551 ofproto_is_alive(const struct ofproto
*p
)
1553 return connmgr_has_controllers(p
->connmgr
);
1556 /* Adds some memory usage statistics for 'ofproto' into 'usage', for use with
1557 * memory_report(). */
1559 ofproto_get_memory_usage(const struct ofproto
*ofproto
, struct simap
*usage
)
1561 const struct oftable
*table
;
1562 unsigned int n_rules
;
1564 simap_increase(usage
, "ports", hmap_count(&ofproto
->ports
));
1566 ovs_mutex_lock(&ofproto_mutex
);
1567 simap_increase(usage
, "ops",
1568 ofproto
->n_pending
+ hmap_count(&ofproto
->deletions
));
1569 ovs_mutex_unlock(&ofproto_mutex
);
1572 OFPROTO_FOR_EACH_TABLE (table
, ofproto
) {
1573 ovs_rwlock_rdlock(&table
->cls
.rwlock
);
1574 n_rules
+= classifier_count(&table
->cls
);
1575 ovs_rwlock_unlock(&table
->cls
.rwlock
);
1577 simap_increase(usage
, "rules", n_rules
);
1579 if (ofproto
->ofproto_class
->get_memory_usage
) {
1580 ofproto
->ofproto_class
->get_memory_usage(ofproto
, usage
);
1583 connmgr_get_memory_usage(ofproto
->connmgr
, usage
);
1587 ofproto_get_ofproto_controller_info(const struct ofproto
*ofproto
,
1590 connmgr_get_controller_info(ofproto
->connmgr
, info
);
1594 ofproto_free_ofproto_controller_info(struct shash
*info
)
1596 connmgr_free_controller_info(info
);
1599 /* Makes a deep copy of 'old' into 'port'. */
1601 ofproto_port_clone(struct ofproto_port
*port
, const struct ofproto_port
*old
)
1603 port
->name
= xstrdup(old
->name
);
1604 port
->type
= xstrdup(old
->type
);
1605 port
->ofp_port
= old
->ofp_port
;
1608 /* Frees memory allocated to members of 'ofproto_port'.
1610 * Do not call this function on an ofproto_port obtained from
1611 * ofproto_port_dump_next(): that function retains ownership of the data in the
1614 ofproto_port_destroy(struct ofproto_port
*ofproto_port
)
1616 free(ofproto_port
->name
);
1617 free(ofproto_port
->type
);
1620 /* Initializes 'dump' to begin dumping the ports in an ofproto.
1622 * This function provides no status indication. An error status for the entire
1623 * dump operation is provided when it is completed by calling
1624 * ofproto_port_dump_done().
1627 ofproto_port_dump_start(struct ofproto_port_dump
*dump
,
1628 const struct ofproto
*ofproto
)
1630 dump
->ofproto
= ofproto
;
1631 dump
->error
= ofproto
->ofproto_class
->port_dump_start(ofproto
,
1635 /* Attempts to retrieve another port from 'dump', which must have been created
1636 * with ofproto_port_dump_start(). On success, stores a new ofproto_port into
1637 * 'port' and returns true. On failure, returns false.
1639 * Failure might indicate an actual error or merely that the last port has been
1640 * dumped. An error status for the entire dump operation is provided when it
1641 * is completed by calling ofproto_port_dump_done().
1643 * The ofproto owns the data stored in 'port'. It will remain valid until at
1644 * least the next time 'dump' is passed to ofproto_port_dump_next() or
1645 * ofproto_port_dump_done(). */
1647 ofproto_port_dump_next(struct ofproto_port_dump
*dump
,
1648 struct ofproto_port
*port
)
1650 const struct ofproto
*ofproto
= dump
->ofproto
;
1656 dump
->error
= ofproto
->ofproto_class
->port_dump_next(ofproto
, dump
->state
,
1659 ofproto
->ofproto_class
->port_dump_done(ofproto
, dump
->state
);
1665 /* Completes port table dump operation 'dump', which must have been created
1666 * with ofproto_port_dump_start(). Returns 0 if the dump operation was
1667 * error-free, otherwise a positive errno value describing the problem. */
1669 ofproto_port_dump_done(struct ofproto_port_dump
*dump
)
1671 const struct ofproto
*ofproto
= dump
->ofproto
;
1673 dump
->error
= ofproto
->ofproto_class
->port_dump_done(ofproto
,
1676 return dump
->error
== EOF
? 0 : dump
->error
;
1679 /* Returns the type to pass to netdev_open() when a datapath of type
1680 * 'datapath_type' has a port of type 'port_type', for a few special
1681 * cases when a netdev type differs from a port type. For example, when
1682 * using the userspace datapath, a port of type "internal" needs to be
1685 * Returns either 'type' itself or a string literal, which must not be
1688 ofproto_port_open_type(const char *datapath_type
, const char *port_type
)
1690 const struct ofproto_class
*class;
1692 datapath_type
= ofproto_normalize_type(datapath_type
);
1693 class = ofproto_class_find__(datapath_type
);
1698 return (class->port_open_type
1699 ? class->port_open_type(datapath_type
, port_type
)
1703 /* Attempts to add 'netdev' as a port on 'ofproto'. If 'ofp_portp' is
1704 * non-null and '*ofp_portp' is not OFPP_NONE, attempts to use that as
1705 * the port's OpenFlow port number.
1707 * If successful, returns 0 and sets '*ofp_portp' to the new port's
1708 * OpenFlow port number (if 'ofp_portp' is non-null). On failure,
1709 * returns a positive errno value and sets '*ofp_portp' to OFPP_NONE (if
1710 * 'ofp_portp' is non-null). */
1712 ofproto_port_add(struct ofproto
*ofproto
, struct netdev
*netdev
,
1713 ofp_port_t
*ofp_portp
)
1715 ofp_port_t ofp_port
= ofp_portp
? *ofp_portp
: OFPP_NONE
;
1718 error
= ofproto
->ofproto_class
->port_add(ofproto
, netdev
);
1720 const char *netdev_name
= netdev_get_name(netdev
);
1722 simap_put(&ofproto
->ofp_requests
, netdev_name
,
1723 ofp_to_u16(ofp_port
));
1724 update_port(ofproto
, netdev_name
);
1727 struct ofproto_port ofproto_port
;
1729 ofproto_port_query_by_name(ofproto
, netdev_get_name(netdev
),
1731 *ofp_portp
= error
? OFPP_NONE
: ofproto_port
.ofp_port
;
1732 ofproto_port_destroy(&ofproto_port
);
1737 /* Looks up a port named 'devname' in 'ofproto'. On success, returns 0 and
1738 * initializes '*port' appropriately; on failure, returns a positive errno
1741 * The caller owns the data in 'ofproto_port' and must free it with
1742 * ofproto_port_destroy() when it is no longer needed. */
1744 ofproto_port_query_by_name(const struct ofproto
*ofproto
, const char *devname
,
1745 struct ofproto_port
*port
)
1749 error
= ofproto
->ofproto_class
->port_query_by_name(ofproto
, devname
, port
);
1751 memset(port
, 0, sizeof *port
);
1756 /* Deletes port number 'ofp_port' from the datapath for 'ofproto'.
1757 * Returns 0 if successful, otherwise a positive errno. */
1759 ofproto_port_del(struct ofproto
*ofproto
, ofp_port_t ofp_port
)
1761 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
1762 const char *name
= ofport
? netdev_get_name(ofport
->netdev
) : "<unknown>";
1763 struct simap_node
*ofp_request_node
;
1766 ofp_request_node
= simap_find(&ofproto
->ofp_requests
, name
);
1767 if (ofp_request_node
) {
1768 simap_delete(&ofproto
->ofp_requests
, ofp_request_node
);
1771 error
= ofproto
->ofproto_class
->port_del(ofproto
, ofp_port
);
1772 if (!error
&& ofport
) {
1773 /* 'name' is the netdev's name and update_port() is going to close the
1774 * netdev. Just in case update_port() refers to 'name' after it
1775 * destroys 'ofport', make a copy of it around the update_port()
1777 char *devname
= xstrdup(name
);
1778 update_port(ofproto
, devname
);
1785 simple_flow_mod(struct ofproto
*ofproto
,
1786 const struct match
*match
, unsigned int priority
,
1787 const struct ofpact
*ofpacts
, size_t ofpacts_len
,
1788 enum ofp_flow_mod_command command
)
1790 struct ofputil_flow_mod fm
;
1792 memset(&fm
, 0, sizeof fm
);
1794 fm
.priority
= priority
;
1797 fm
.modify_cookie
= false;
1799 fm
.command
= command
;
1800 fm
.idle_timeout
= 0;
1801 fm
.hard_timeout
= 0;
1802 fm
.buffer_id
= UINT32_MAX
;
1803 fm
.out_port
= OFPP_ANY
;
1804 fm
.out_group
= OFPG_ANY
;
1806 fm
.ofpacts
= CONST_CAST(struct ofpact
*, ofpacts
);
1807 fm
.ofpacts_len
= ofpacts_len
;
1809 return handle_flow_mod__(ofproto
, NULL
, &fm
, NULL
);
1812 /* Adds a flow to OpenFlow flow table 0 in 'p' that matches 'cls_rule' and
1813 * performs the 'n_actions' actions in 'actions'. The new flow will not
1816 * If cls_rule->priority is in the range of priorities supported by OpenFlow
1817 * (0...65535, inclusive) then the flow will be visible to OpenFlow
1818 * controllers; otherwise, it will be hidden.
1820 * The caller retains ownership of 'cls_rule' and 'ofpacts'.
1822 * This is a helper function for in-band control and fail-open. */
1824 ofproto_add_flow(struct ofproto
*ofproto
, const struct match
*match
,
1825 unsigned int priority
,
1826 const struct ofpact
*ofpacts
, size_t ofpacts_len
)
1827 OVS_EXCLUDED(ofproto_mutex
)
1829 const struct rule
*rule
;
1832 /* First do a cheap check whether the rule we're looking for already exists
1833 * with the actions that we want. If it does, then we're done. */
1834 ovs_rwlock_rdlock(&ofproto
->tables
[0].cls
.rwlock
);
1835 rule
= rule_from_cls_rule(classifier_find_match_exactly(
1836 &ofproto
->tables
[0].cls
, match
, priority
));
1838 ovs_mutex_lock(&rule
->mutex
);
1839 must_add
= !ofpacts_equal(rule
->actions
->ofpacts
,
1840 rule
->actions
->ofpacts_len
,
1841 ofpacts
, ofpacts_len
);
1842 ovs_mutex_unlock(&rule
->mutex
);
1846 ovs_rwlock_unlock(&ofproto
->tables
[0].cls
.rwlock
);
1848 /* If there's no such rule or the rule doesn't have the actions we want,
1849 * fall back to a executing a full flow mod. We can't optimize this at
1850 * all because we didn't take enough locks above to ensure that the flow
1851 * table didn't already change beneath us. */
1853 simple_flow_mod(ofproto
, match
, priority
, ofpacts
, ofpacts_len
,
1854 OFPFC_MODIFY_STRICT
);
1858 /* Executes the flow modification specified in 'fm'. Returns 0 on success, an
1859 * OFPERR_* OpenFlow error code on failure, or OFPROTO_POSTPONE if the
1860 * operation cannot be initiated now but may be retried later.
1862 * This is a helper function for in-band control and fail-open and the "learn"
1865 ofproto_flow_mod(struct ofproto
*ofproto
, struct ofputil_flow_mod
*fm
)
1866 OVS_EXCLUDED(ofproto_mutex
)
1868 return handle_flow_mod__(ofproto
, NULL
, fm
, NULL
);
1871 /* Searches for a rule with matching criteria exactly equal to 'target' in
1872 * ofproto's table 0 and, if it finds one, deletes it.
1874 * This is a helper function for in-band control and fail-open. */
1876 ofproto_delete_flow(struct ofproto
*ofproto
,
1877 const struct match
*target
, unsigned int priority
)
1878 OVS_EXCLUDED(ofproto_mutex
)
1880 struct classifier
*cls
= &ofproto
->tables
[0].cls
;
1883 /* First do a cheap check whether the rule we're looking for has already
1884 * been deleted. If so, then we're done. */
1885 ovs_rwlock_rdlock(&cls
->rwlock
);
1886 rule
= rule_from_cls_rule(classifier_find_match_exactly(cls
, target
,
1888 ovs_rwlock_unlock(&cls
->rwlock
);
1893 /* Fall back to a executing a full flow mod. We can't optimize this at all
1894 * because we didn't take enough locks above to ensure that the flow table
1895 * didn't already change beneath us. */
1896 return simple_flow_mod(ofproto
, target
, priority
, NULL
, 0,
1897 OFPFC_DELETE_STRICT
) != OFPROTO_POSTPONE
;
1900 /* Starts the process of deleting all of the flows from all of ofproto's flow
1901 * tables and then reintroducing the flows required by in-band control and
1902 * fail-open. The process will complete in a later call to ofproto_run(). */
1904 ofproto_flush_flows(struct ofproto
*ofproto
)
1906 COVERAGE_INC(ofproto_flush
);
1907 ofproto
->state
= S_FLUSH
;
1911 reinit_ports(struct ofproto
*p
)
1913 struct ofproto_port_dump dump
;
1914 struct sset devnames
;
1915 struct ofport
*ofport
;
1916 struct ofproto_port ofproto_port
;
1917 const char *devname
;
1919 COVERAGE_INC(ofproto_reinit_ports
);
1921 sset_init(&devnames
);
1922 HMAP_FOR_EACH (ofport
, hmap_node
, &p
->ports
) {
1923 sset_add(&devnames
, netdev_get_name(ofport
->netdev
));
1925 OFPROTO_PORT_FOR_EACH (&ofproto_port
, &dump
, p
) {
1926 sset_add(&devnames
, ofproto_port
.name
);
1929 SSET_FOR_EACH (devname
, &devnames
) {
1930 update_port(p
, devname
);
1932 sset_destroy(&devnames
);
1936 alloc_ofp_port(struct ofproto
*ofproto
, const char *netdev_name
)
1940 port_idx
= simap_get(&ofproto
->ofp_requests
, netdev_name
);
1941 port_idx
= port_idx
? port_idx
: UINT16_MAX
;
1943 if (port_idx
>= ofproto
->max_ports
1944 || bitmap_is_set(ofproto
->ofp_port_ids
, port_idx
)) {
1945 uint16_t end_port_no
= ofproto
->alloc_port_no
;
1947 /* Search for a free OpenFlow port number. We try not to
1948 * immediately reuse them to prevent problems due to old
1951 if (++ofproto
->alloc_port_no
>= ofproto
->max_ports
) {
1952 ofproto
->alloc_port_no
= 0;
1954 if (!bitmap_is_set(ofproto
->ofp_port_ids
,
1955 ofproto
->alloc_port_no
)) {
1956 port_idx
= ofproto
->alloc_port_no
;
1959 if (ofproto
->alloc_port_no
== end_port_no
) {
1964 bitmap_set1(ofproto
->ofp_port_ids
, port_idx
);
1965 return u16_to_ofp(port_idx
);
1969 dealloc_ofp_port(const struct ofproto
*ofproto
, ofp_port_t ofp_port
)
1971 if (ofp_to_u16(ofp_port
) < ofproto
->max_ports
) {
1972 bitmap_set0(ofproto
->ofp_port_ids
, ofp_to_u16(ofp_port
));
1976 /* Opens and returns a netdev for 'ofproto_port' in 'ofproto', or a null
1977 * pointer if the netdev cannot be opened. On success, also fills in
1979 static struct netdev
*
1980 ofport_open(struct ofproto
*ofproto
,
1981 struct ofproto_port
*ofproto_port
,
1982 struct ofputil_phy_port
*pp
)
1984 enum netdev_flags flags
;
1985 struct netdev
*netdev
;
1988 error
= netdev_open(ofproto_port
->name
, ofproto_port
->type
, &netdev
);
1990 VLOG_WARN_RL(&rl
, "%s: ignoring port %s (%"PRIu16
") because netdev %s "
1991 "cannot be opened (%s)",
1993 ofproto_port
->name
, ofproto_port
->ofp_port
,
1994 ofproto_port
->name
, ovs_strerror(error
));
1998 if (ofproto_port
->ofp_port
== OFPP_NONE
) {
1999 if (!strcmp(ofproto
->name
, ofproto_port
->name
)) {
2000 ofproto_port
->ofp_port
= OFPP_LOCAL
;
2002 ofproto_port
->ofp_port
= alloc_ofp_port(ofproto
,
2003 ofproto_port
->name
);
2006 pp
->port_no
= ofproto_port
->ofp_port
;
2007 netdev_get_etheraddr(netdev
, pp
->hw_addr
);
2008 ovs_strlcpy(pp
->name
, ofproto_port
->name
, sizeof pp
->name
);
2009 netdev_get_flags(netdev
, &flags
);
2010 pp
->config
= flags
& NETDEV_UP
? 0 : OFPUTIL_PC_PORT_DOWN
;
2011 pp
->state
= netdev_get_carrier(netdev
) ? 0 : OFPUTIL_PS_LINK_DOWN
;
2012 netdev_get_features(netdev
, &pp
->curr
, &pp
->advertised
,
2013 &pp
->supported
, &pp
->peer
);
2014 pp
->curr_speed
= netdev_features_to_bps(pp
->curr
, 0) / 1000;
2015 pp
->max_speed
= netdev_features_to_bps(pp
->supported
, 0) / 1000;
2020 /* Returns true if most fields of 'a' and 'b' are equal. Differences in name,
2021 * port number, and 'config' bits other than OFPUTIL_PS_LINK_DOWN are
2024 ofport_equal(const struct ofputil_phy_port
*a
,
2025 const struct ofputil_phy_port
*b
)
2027 return (eth_addr_equals(a
->hw_addr
, b
->hw_addr
)
2028 && a
->state
== b
->state
2029 && !((a
->config
^ b
->config
) & OFPUTIL_PC_PORT_DOWN
)
2030 && a
->curr
== b
->curr
2031 && a
->advertised
== b
->advertised
2032 && a
->supported
== b
->supported
2033 && a
->peer
== b
->peer
2034 && a
->curr_speed
== b
->curr_speed
2035 && a
->max_speed
== b
->max_speed
);
2038 /* Adds an ofport to 'p' initialized based on the given 'netdev' and 'opp'.
2039 * The caller must ensure that 'p' does not have a conflicting ofport (that is,
2040 * one with the same name or port number). */
2042 ofport_install(struct ofproto
*p
,
2043 struct netdev
*netdev
, const struct ofputil_phy_port
*pp
)
2045 const char *netdev_name
= netdev_get_name(netdev
);
2046 struct ofport
*ofport
;
2049 /* Create ofport. */
2050 ofport
= p
->ofproto_class
->port_alloc();
2055 ofport
->ofproto
= p
;
2056 ofport
->netdev
= netdev
;
2057 ofport
->change_seq
= netdev_change_seq(netdev
);
2059 ofport
->ofp_port
= pp
->port_no
;
2060 ofport
->created
= time_msec();
2062 /* Add port to 'p'. */
2063 hmap_insert(&p
->ports
, &ofport
->hmap_node
,
2064 hash_ofp_port(ofport
->ofp_port
));
2065 shash_add(&p
->port_by_name
, netdev_name
, ofport
);
2067 update_mtu(p
, ofport
);
2069 /* Let the ofproto_class initialize its private data. */
2070 error
= p
->ofproto_class
->port_construct(ofport
);
2074 connmgr_send_port_status(p
->connmgr
, pp
, OFPPR_ADD
);
2078 VLOG_WARN_RL(&rl
, "%s: could not add port %s (%s)",
2079 p
->name
, netdev_name
, ovs_strerror(error
));
2081 ofport_destroy__(ofport
);
2083 netdev_close(netdev
);
2087 /* Removes 'ofport' from 'p' and destroys it. */
2089 ofport_remove(struct ofport
*ofport
)
2091 connmgr_send_port_status(ofport
->ofproto
->connmgr
, &ofport
->pp
,
2093 ofport_destroy(ofport
);
2096 /* If 'ofproto' contains an ofport named 'name', removes it from 'ofproto' and
2099 ofport_remove_with_name(struct ofproto
*ofproto
, const char *name
)
2101 struct ofport
*port
= shash_find_data(&ofproto
->port_by_name
, name
);
2103 ofport_remove(port
);
2107 /* Updates 'port' with new 'pp' description.
2109 * Does not handle a name or port number change. The caller must implement
2110 * such a change as a delete followed by an add. */
2112 ofport_modified(struct ofport
*port
, struct ofputil_phy_port
*pp
)
2114 memcpy(port
->pp
.hw_addr
, pp
->hw_addr
, ETH_ADDR_LEN
);
2115 port
->pp
.config
= ((port
->pp
.config
& ~OFPUTIL_PC_PORT_DOWN
)
2116 | (pp
->config
& OFPUTIL_PC_PORT_DOWN
));
2117 port
->pp
.state
= pp
->state
;
2118 port
->pp
.curr
= pp
->curr
;
2119 port
->pp
.advertised
= pp
->advertised
;
2120 port
->pp
.supported
= pp
->supported
;
2121 port
->pp
.peer
= pp
->peer
;
2122 port
->pp
.curr_speed
= pp
->curr_speed
;
2123 port
->pp
.max_speed
= pp
->max_speed
;
2125 connmgr_send_port_status(port
->ofproto
->connmgr
, &port
->pp
, OFPPR_MODIFY
);
2128 /* Update OpenFlow 'state' in 'port' and notify controller. */
2130 ofproto_port_set_state(struct ofport
*port
, enum ofputil_port_state state
)
2132 if (port
->pp
.state
!= state
) {
2133 port
->pp
.state
= state
;
2134 connmgr_send_port_status(port
->ofproto
->connmgr
, &port
->pp
,
2140 ofproto_port_unregister(struct ofproto
*ofproto
, ofp_port_t ofp_port
)
2142 struct ofport
*port
= ofproto_get_port(ofproto
, ofp_port
);
2144 if (port
->ofproto
->ofproto_class
->set_realdev
) {
2145 port
->ofproto
->ofproto_class
->set_realdev(port
, 0, 0);
2147 if (port
->ofproto
->ofproto_class
->set_stp_port
) {
2148 port
->ofproto
->ofproto_class
->set_stp_port(port
, NULL
);
2150 if (port
->ofproto
->ofproto_class
->set_cfm
) {
2151 port
->ofproto
->ofproto_class
->set_cfm(port
, NULL
);
2153 if (port
->ofproto
->ofproto_class
->bundle_remove
) {
2154 port
->ofproto
->ofproto_class
->bundle_remove(port
);
2160 ofport_destroy__(struct ofport
*port
)
2162 struct ofproto
*ofproto
= port
->ofproto
;
2163 const char *name
= netdev_get_name(port
->netdev
);
2165 hmap_remove(&ofproto
->ports
, &port
->hmap_node
);
2166 shash_delete(&ofproto
->port_by_name
,
2167 shash_find(&ofproto
->port_by_name
, name
));
2169 netdev_close(port
->netdev
);
2170 ofproto
->ofproto_class
->port_dealloc(port
);
2174 ofport_destroy(struct ofport
*port
)
2177 dealloc_ofp_port(port
->ofproto
, port
->ofp_port
);
2178 port
->ofproto
->ofproto_class
->port_destruct(port
);
2179 ofport_destroy__(port
);
2184 ofproto_get_port(const struct ofproto
*ofproto
, ofp_port_t ofp_port
)
2186 struct ofport
*port
;
2188 HMAP_FOR_EACH_IN_BUCKET (port
, hmap_node
, hash_ofp_port(ofp_port
),
2190 if (port
->ofp_port
== ofp_port
) {
2198 ofproto_port_get_stats(const struct ofport
*port
, struct netdev_stats
*stats
)
2200 struct ofproto
*ofproto
= port
->ofproto
;
2203 if (ofproto
->ofproto_class
->port_get_stats
) {
2204 error
= ofproto
->ofproto_class
->port_get_stats(port
, stats
);
2213 update_port(struct ofproto
*ofproto
, const char *name
)
2215 struct ofproto_port ofproto_port
;
2216 struct ofputil_phy_port pp
;
2217 struct netdev
*netdev
;
2218 struct ofport
*port
;
2220 COVERAGE_INC(ofproto_update_port
);
2222 /* Fetch 'name''s location and properties from the datapath. */
2223 netdev
= (!ofproto_port_query_by_name(ofproto
, name
, &ofproto_port
)
2224 ? ofport_open(ofproto
, &ofproto_port
, &pp
)
2228 port
= ofproto_get_port(ofproto
, ofproto_port
.ofp_port
);
2229 if (port
&& !strcmp(netdev_get_name(port
->netdev
), name
)) {
2230 struct netdev
*old_netdev
= port
->netdev
;
2232 /* 'name' hasn't changed location. Any properties changed? */
2233 if (!ofport_equal(&port
->pp
, &pp
)) {
2234 ofport_modified(port
, &pp
);
2237 update_mtu(ofproto
, port
);
2239 /* Install the newly opened netdev in case it has changed.
2240 * Don't close the old netdev yet in case port_modified has to
2241 * remove a retained reference to it.*/
2242 port
->netdev
= netdev
;
2243 port
->change_seq
= netdev_change_seq(netdev
);
2245 if (port
->ofproto
->ofproto_class
->port_modified
) {
2246 port
->ofproto
->ofproto_class
->port_modified(port
);
2249 netdev_close(old_netdev
);
2251 /* If 'port' is nonnull then its name differs from 'name' and thus
2252 * we should delete it. If we think there's a port named 'name'
2253 * then its port number must be wrong now so delete it too. */
2255 ofport_remove(port
);
2257 ofport_remove_with_name(ofproto
, name
);
2258 ofport_install(ofproto
, netdev
, &pp
);
2261 /* Any port named 'name' is gone now. */
2262 ofport_remove_with_name(ofproto
, name
);
2264 ofproto_port_destroy(&ofproto_port
);
2268 init_ports(struct ofproto
*p
)
2270 struct ofproto_port_dump dump
;
2271 struct ofproto_port ofproto_port
;
2272 struct shash_node
*node
, *next
;
2274 OFPROTO_PORT_FOR_EACH (&ofproto_port
, &dump
, p
) {
2275 const char *name
= ofproto_port
.name
;
2277 if (shash_find(&p
->port_by_name
, name
)) {
2278 VLOG_WARN_RL(&rl
, "%s: ignoring duplicate device %s in datapath",
2281 struct ofputil_phy_port pp
;
2282 struct netdev
*netdev
;
2284 /* Check if an OpenFlow port number had been requested. */
2285 node
= shash_find(&init_ofp_ports
, name
);
2287 const struct iface_hint
*iface_hint
= node
->data
;
2288 simap_put(&p
->ofp_requests
, name
,
2289 ofp_to_u16(iface_hint
->ofp_port
));
2292 netdev
= ofport_open(p
, &ofproto_port
, &pp
);
2294 ofport_install(p
, netdev
, &pp
);
2295 if (ofp_to_u16(ofproto_port
.ofp_port
) < p
->max_ports
) {
2296 p
->alloc_port_no
= MAX(p
->alloc_port_no
,
2297 ofp_to_u16(ofproto_port
.ofp_port
));
2303 SHASH_FOR_EACH_SAFE(node
, next
, &init_ofp_ports
) {
2304 struct iface_hint
*iface_hint
= node
->data
;
2306 if (!strcmp(iface_hint
->br_name
, p
->name
)) {
2307 free(iface_hint
->br_name
);
2308 free(iface_hint
->br_type
);
2310 shash_delete(&init_ofp_ports
, node
);
2317 /* Find the minimum MTU of all non-datapath devices attached to 'p'.
2318 * Returns ETH_PAYLOAD_MAX or the minimum of the ports. */
2320 find_min_mtu(struct ofproto
*p
)
2322 struct ofport
*ofport
;
2325 HMAP_FOR_EACH (ofport
, hmap_node
, &p
->ports
) {
2326 struct netdev
*netdev
= ofport
->netdev
;
2329 /* Skip any internal ports, since that's what we're trying to
2331 if (!strcmp(netdev_get_type(netdev
), "internal")) {
2335 if (netdev_get_mtu(netdev
, &dev_mtu
)) {
2338 if (!mtu
|| dev_mtu
< mtu
) {
2343 return mtu
? mtu
: ETH_PAYLOAD_MAX
;
2346 /* Update MTU of all datapath devices on 'p' to the minimum of the
2347 * non-datapath ports in event of 'port' added or changed. */
2349 update_mtu(struct ofproto
*p
, struct ofport
*port
)
2351 struct ofport
*ofport
;
2352 struct netdev
*netdev
= port
->netdev
;
2353 int dev_mtu
, old_min
;
2355 if (netdev_get_mtu(netdev
, &dev_mtu
)) {
2359 if (!strcmp(netdev_get_type(port
->netdev
), "internal")) {
2360 if (dev_mtu
> p
->min_mtu
) {
2361 if (!netdev_set_mtu(port
->netdev
, p
->min_mtu
)) {
2362 dev_mtu
= p
->min_mtu
;
2365 port
->mtu
= dev_mtu
;
2369 /* For non-internal port find new min mtu. */
2370 old_min
= p
->min_mtu
;
2371 port
->mtu
= dev_mtu
;
2372 p
->min_mtu
= find_min_mtu(p
);
2373 if (p
->min_mtu
== old_min
) {
2377 HMAP_FOR_EACH (ofport
, hmap_node
, &p
->ports
) {
2378 struct netdev
*netdev
= ofport
->netdev
;
2380 if (!strcmp(netdev_get_type(netdev
), "internal")) {
2381 if (!netdev_set_mtu(netdev
, p
->min_mtu
)) {
2382 ofport
->mtu
= p
->min_mtu
;
2389 ofproto_rule_ref(struct rule
*rule
)
2394 atomic_add(&rule
->ref_count
, 1, &orig
);
2395 ovs_assert(orig
!= 0);
2400 ofproto_rule_unref(struct rule
*rule
)
2405 atomic_sub(&rule
->ref_count
, 1, &orig
);
2407 rule
->ofproto
->ofproto_class
->rule_destruct(rule
);
2408 ofproto_rule_destroy__(rule
);
2410 ovs_assert(orig
!= 0);
2415 struct rule_actions
*
2416 rule_get_actions(const struct rule
*rule
)
2417 OVS_EXCLUDED(rule
->mutex
)
2419 struct rule_actions
*actions
;
2421 ovs_mutex_lock(&rule
->mutex
);
2422 actions
= rule_get_actions__(rule
);
2423 ovs_mutex_unlock(&rule
->mutex
);
2428 struct rule_actions
*
2429 rule_get_actions__(const struct rule
*rule
)
2430 OVS_REQUIRES(rule
->mutex
)
2432 rule_actions_ref(rule
->actions
);
2433 return rule
->actions
;
2437 ofproto_rule_destroy__(struct rule
*rule
)
2438 OVS_NO_THREAD_SAFETY_ANALYSIS
2440 cls_rule_destroy(CONST_CAST(struct cls_rule
*, &rule
->cr
));
2441 rule_actions_unref(rule
->actions
);
2442 ovs_mutex_destroy(&rule
->mutex
);
2443 rule
->ofproto
->ofproto_class
->rule_dealloc(rule
);
2446 /* Creates and returns a new 'struct rule_actions', with a ref_count of 1,
2447 * whose actions are a copy of from the 'ofpacts_len' bytes of 'ofpacts'. */
2448 struct rule_actions
*
2449 rule_actions_create(const struct ofpact
*ofpacts
, size_t ofpacts_len
)
2451 struct rule_actions
*actions
;
2453 actions
= xmalloc(sizeof *actions
);
2454 atomic_init(&actions
->ref_count
, 1);
2455 actions
->ofpacts
= xmemdup(ofpacts
, ofpacts_len
);
2456 actions
->ofpacts_len
= ofpacts_len
;
2457 actions
->meter_id
= ofpacts_get_meter(ofpacts
, ofpacts_len
);
2461 /* Increments 'actions''s ref_count. */
2463 rule_actions_ref(struct rule_actions
*actions
)
2468 atomic_add(&actions
->ref_count
, 1, &orig
);
2469 ovs_assert(orig
!= 0);
2473 /* Decrements 'actions''s ref_count and frees 'actions' if the ref_count
2476 rule_actions_unref(struct rule_actions
*actions
)
2481 atomic_sub(&actions
->ref_count
, 1, &orig
);
2485 ovs_assert(orig
!= 0);
2490 /* Returns true if 'rule' has an OpenFlow OFPAT_OUTPUT or OFPAT_ENQUEUE action
2491 * that outputs to 'port' (output to OFPP_FLOOD and OFPP_ALL doesn't count). */
2493 ofproto_rule_has_out_port(const struct rule
*rule
, ofp_port_t port
)
2494 OVS_REQUIRES(ofproto_mutex
)
2496 return (port
== OFPP_ANY
2497 || ofpacts_output_to_port(rule
->actions
->ofpacts
,
2498 rule
->actions
->ofpacts_len
, port
));
2501 /* Returns true if 'rule' has group and equals group_id. */
2503 ofproto_rule_has_out_group(const struct rule
*rule
, uint32_t group_id
)
2504 OVS_REQUIRES(ofproto_mutex
)
2506 return (group_id
== OFPG11_ANY
2507 || ofpacts_output_to_group(rule
->actions
->ofpacts
,
2508 rule
->actions
->ofpacts_len
, group_id
));
2511 /* Returns true if a rule related to 'op' has an OpenFlow OFPAT_OUTPUT or
2512 * OFPAT_ENQUEUE action that outputs to 'out_port'. */
2514 ofoperation_has_out_port(const struct ofoperation
*op
, ofp_port_t out_port
)
2515 OVS_REQUIRES(ofproto_mutex
)
2517 if (ofproto_rule_has_out_port(op
->rule
, out_port
)) {
2522 case OFOPERATION_ADD
:
2523 case OFOPERATION_DELETE
:
2526 case OFOPERATION_MODIFY
:
2527 case OFOPERATION_REPLACE
:
2528 return ofpacts_output_to_port(op
->actions
->ofpacts
,
2529 op
->actions
->ofpacts_len
, out_port
);
2536 rule_execute_destroy(struct rule_execute
*e
)
2538 ofproto_rule_unref(e
->rule
);
2539 list_remove(&e
->list_node
);
2543 /* Executes all "rule_execute" operations queued up in ofproto->rule_executes,
2544 * by passing them to the ofproto provider. */
2546 run_rule_executes(struct ofproto
*ofproto
)
2547 OVS_EXCLUDED(ofproto_mutex
)
2549 struct rule_execute
*e
, *next
;
2550 struct list executes
;
2552 guarded_list_pop_all(&ofproto
->rule_executes
, &executes
);
2553 LIST_FOR_EACH_SAFE (e
, next
, list_node
, &executes
) {
2554 union flow_in_port in_port_
;
2557 in_port_
.ofp_port
= e
->in_port
;
2558 flow_extract(e
->packet
, 0, 0, NULL
, &in_port_
, &flow
);
2559 ofproto
->ofproto_class
->rule_execute(e
->rule
, &flow
, e
->packet
);
2561 rule_execute_destroy(e
);
2565 /* Destroys and discards all "rule_execute" operations queued up in
2566 * ofproto->rule_executes. */
2568 destroy_rule_executes(struct ofproto
*ofproto
)
2570 struct rule_execute
*e
, *next
;
2571 struct list executes
;
2573 guarded_list_pop_all(&ofproto
->rule_executes
, &executes
);
2574 LIST_FOR_EACH_SAFE (e
, next
, list_node
, &executes
) {
2575 ofpbuf_delete(e
->packet
);
2576 rule_execute_destroy(e
);
2580 /* Returns true if 'rule' should be hidden from the controller.
2582 * Rules with priority higher than UINT16_MAX are set up by ofproto itself
2583 * (e.g. by in-band control) and are intentionally hidden from the
2586 ofproto_rule_is_hidden(const struct rule
*rule
)
2588 return rule
->cr
.priority
> UINT16_MAX
;
2591 static enum oftable_flags
2592 rule_get_flags(const struct rule
*rule
)
2594 return rule
->ofproto
->tables
[rule
->table_id
].flags
;
2598 rule_is_modifiable(const struct rule
*rule
)
2600 return !(rule_get_flags(rule
) & OFTABLE_READONLY
);
2604 handle_echo_request(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
2606 ofconn_send_reply(ofconn
, make_echo_reply(oh
));
2611 handle_features_request(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
2613 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
2614 struct ofputil_switch_features features
;
2615 struct ofport
*port
;
2621 ofproto
->ofproto_class
->get_features(ofproto
, &arp_match_ip
,
2623 ovs_assert(features
.actions
& OFPUTIL_A_OUTPUT
); /* sanity check */
2625 /* Count only non-hidden tables in the number of tables. (Hidden tables,
2626 * if present, are always at the end.) */
2627 n_tables
= ofproto
->n_tables
;
2628 for (i
= 0; i
< ofproto
->n_tables
; i
++) {
2629 if (ofproto
->tables
[i
].flags
& OFTABLE_HIDDEN
) {
2635 features
.datapath_id
= ofproto
->datapath_id
;
2636 features
.n_buffers
= pktbuf_capacity();
2637 features
.n_tables
= n_tables
;
2638 features
.capabilities
= (OFPUTIL_C_FLOW_STATS
| OFPUTIL_C_TABLE_STATS
|
2639 OFPUTIL_C_PORT_STATS
| OFPUTIL_C_QUEUE_STATS
);
2641 features
.capabilities
|= OFPUTIL_C_ARP_MATCH_IP
;
2643 /* FIXME: Fill in proper features.auxiliary_id for auxiliary connections */
2644 features
.auxiliary_id
= 0;
2645 b
= ofputil_encode_switch_features(&features
, ofconn_get_protocol(ofconn
),
2647 HMAP_FOR_EACH (port
, hmap_node
, &ofproto
->ports
) {
2648 ofputil_put_switch_features_port(&port
->pp
, b
);
2651 ofconn_send_reply(ofconn
, b
);
2656 handle_get_config_request(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
2658 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
2659 struct ofp_switch_config
*osc
;
2660 enum ofp_config_flags flags
;
2664 buf
= ofpraw_alloc_reply(OFPRAW_OFPT_GET_CONFIG_REPLY
, oh
, 0);
2665 osc
= ofpbuf_put_uninit(buf
, sizeof *osc
);
2666 flags
= ofproto
->frag_handling
;
2667 /* OFPC_INVALID_TTL_TO_CONTROLLER is deprecated in OF 1.3 */
2668 if (oh
->version
< OFP13_VERSION
2669 && ofconn_get_invalid_ttl_to_controller(ofconn
)) {
2670 flags
|= OFPC_INVALID_TTL_TO_CONTROLLER
;
2672 osc
->flags
= htons(flags
);
2673 osc
->miss_send_len
= htons(ofconn_get_miss_send_len(ofconn
));
2674 ofconn_send_reply(ofconn
, buf
);
2680 handle_set_config(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
2682 const struct ofp_switch_config
*osc
= ofpmsg_body(oh
);
2683 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
2684 uint16_t flags
= ntohs(osc
->flags
);
2686 if (ofconn_get_type(ofconn
) != OFCONN_PRIMARY
2687 || ofconn_get_role(ofconn
) != OFPCR12_ROLE_SLAVE
) {
2688 enum ofp_config_flags cur
= ofproto
->frag_handling
;
2689 enum ofp_config_flags next
= flags
& OFPC_FRAG_MASK
;
2691 ovs_assert((cur
& OFPC_FRAG_MASK
) == cur
);
2693 if (ofproto
->ofproto_class
->set_frag_handling(ofproto
, next
)) {
2694 ofproto
->frag_handling
= next
;
2696 VLOG_WARN_RL(&rl
, "%s: unsupported fragment handling mode %s",
2698 ofputil_frag_handling_to_string(next
));
2702 /* OFPC_INVALID_TTL_TO_CONTROLLER is deprecated in OF 1.3 */
2703 ofconn_set_invalid_ttl_to_controller(ofconn
,
2704 (oh
->version
< OFP13_VERSION
2705 && flags
& OFPC_INVALID_TTL_TO_CONTROLLER
));
2707 ofconn_set_miss_send_len(ofconn
, ntohs(osc
->miss_send_len
));
2712 /* Checks whether 'ofconn' is a slave controller. If so, returns an OpenFlow
2713 * error message code for the caller to propagate upward. Otherwise, returns
2716 * The log message mentions 'msg_type'. */
2718 reject_slave_controller(struct ofconn
*ofconn
)
2720 if (ofconn_get_type(ofconn
) == OFCONN_PRIMARY
2721 && ofconn_get_role(ofconn
) == OFPCR12_ROLE_SLAVE
) {
2722 return OFPERR_OFPBRC_EPERM
;
2728 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are appropriate
2729 * for a packet with the prerequisites satisfied by 'flow' in table 'table_id'.
2730 * 'flow' may be temporarily modified, but is restored at return.
2733 ofproto_check_ofpacts(struct ofproto
*ofproto
,
2734 const struct ofpact ofpacts
[], size_t ofpacts_len
,
2735 struct flow
*flow
, uint8_t table_id
)
2740 error
= ofpacts_check(ofpacts
, ofpacts_len
, flow
,
2741 u16_to_ofp(ofproto
->max_ports
), table_id
);
2746 mid
= ofpacts_get_meter(ofpacts
, ofpacts_len
);
2747 if (mid
&& ofproto_get_provider_meter_id(ofproto
, mid
) == UINT32_MAX
) {
2748 return OFPERR_OFPMMFC_INVALID_METER
;
2754 handle_packet_out(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
2756 struct ofproto
*p
= ofconn_get_ofproto(ofconn
);
2757 struct ofputil_packet_out po
;
2758 struct ofpbuf
*payload
;
2759 uint64_t ofpacts_stub
[1024 / 8];
2760 struct ofpbuf ofpacts
;
2762 union flow_in_port in_port_
;
2765 COVERAGE_INC(ofproto_packet_out
);
2767 error
= reject_slave_controller(ofconn
);
2772 /* Decode message. */
2773 ofpbuf_use_stub(&ofpacts
, ofpacts_stub
, sizeof ofpacts_stub
);
2774 error
= ofputil_decode_packet_out(&po
, oh
, &ofpacts
);
2776 goto exit_free_ofpacts
;
2778 if (ofp_to_u16(po
.in_port
) >= p
->max_ports
2779 && ofp_to_u16(po
.in_port
) < ofp_to_u16(OFPP_MAX
)) {
2780 error
= OFPERR_OFPBRC_BAD_PORT
;
2781 goto exit_free_ofpacts
;
2786 if (po
.buffer_id
!= UINT32_MAX
) {
2787 error
= ofconn_pktbuf_retrieve(ofconn
, po
.buffer_id
, &payload
, NULL
);
2788 if (error
|| !payload
) {
2789 goto exit_free_ofpacts
;
2792 /* Ensure that the L3 header is 32-bit aligned. */
2793 payload
= ofpbuf_clone_data_with_headroom(po
.packet
, po
.packet_len
, 2);
2796 /* Verify actions against packet, then send packet if successful. */
2797 in_port_
.ofp_port
= po
.in_port
;
2798 flow_extract(payload
, 0, 0, NULL
, &in_port_
, &flow
);
2799 error
= ofproto_check_ofpacts(p
, po
.ofpacts
, po
.ofpacts_len
, &flow
, 0);
2801 error
= p
->ofproto_class
->packet_out(p
, payload
, &flow
,
2802 po
.ofpacts
, po
.ofpacts_len
);
2804 ofpbuf_delete(payload
);
2807 ofpbuf_uninit(&ofpacts
);
2813 update_port_config(struct ofport
*port
,
2814 enum ofputil_port_config config
,
2815 enum ofputil_port_config mask
)
2817 enum ofputil_port_config old_config
= port
->pp
.config
;
2818 enum ofputil_port_config toggle
;
2820 toggle
= (config
^ port
->pp
.config
) & mask
;
2821 if (toggle
& OFPUTIL_PC_PORT_DOWN
) {
2822 if (config
& OFPUTIL_PC_PORT_DOWN
) {
2823 netdev_turn_flags_off(port
->netdev
, NETDEV_UP
, NULL
);
2825 netdev_turn_flags_on(port
->netdev
, NETDEV_UP
, NULL
);
2827 toggle
&= ~OFPUTIL_PC_PORT_DOWN
;
2830 port
->pp
.config
^= toggle
;
2831 if (port
->pp
.config
!= old_config
) {
2832 port
->ofproto
->ofproto_class
->port_reconfigured(port
, old_config
);
2837 handle_port_mod(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
2839 struct ofproto
*p
= ofconn_get_ofproto(ofconn
);
2840 struct ofputil_port_mod pm
;
2841 struct ofport
*port
;
2844 error
= reject_slave_controller(ofconn
);
2849 error
= ofputil_decode_port_mod(oh
, &pm
);
2854 port
= ofproto_get_port(p
, pm
.port_no
);
2856 return OFPERR_OFPPMFC_BAD_PORT
;
2857 } else if (!eth_addr_equals(port
->pp
.hw_addr
, pm
.hw_addr
)) {
2858 return OFPERR_OFPPMFC_BAD_HW_ADDR
;
2860 update_port_config(port
, pm
.config
, pm
.mask
);
2862 netdev_set_advertisements(port
->netdev
, pm
.advertise
);
2869 handle_desc_stats_request(struct ofconn
*ofconn
,
2870 const struct ofp_header
*request
)
2872 static const char *default_mfr_desc
= "Nicira, Inc.";
2873 static const char *default_hw_desc
= "Open vSwitch";
2874 static const char *default_sw_desc
= VERSION
;
2875 static const char *default_serial_desc
= "None";
2876 static const char *default_dp_desc
= "None";
2878 struct ofproto
*p
= ofconn_get_ofproto(ofconn
);
2879 struct ofp_desc_stats
*ods
;
2882 msg
= ofpraw_alloc_stats_reply(request
, 0);
2883 ods
= ofpbuf_put_zeros(msg
, sizeof *ods
);
2884 ovs_strlcpy(ods
->mfr_desc
, p
->mfr_desc
? p
->mfr_desc
: default_mfr_desc
,
2885 sizeof ods
->mfr_desc
);
2886 ovs_strlcpy(ods
->hw_desc
, p
->hw_desc
? p
->hw_desc
: default_hw_desc
,
2887 sizeof ods
->hw_desc
);
2888 ovs_strlcpy(ods
->sw_desc
, p
->sw_desc
? p
->sw_desc
: default_sw_desc
,
2889 sizeof ods
->sw_desc
);
2890 ovs_strlcpy(ods
->serial_num
,
2891 p
->serial_desc
? p
->serial_desc
: default_serial_desc
,
2892 sizeof ods
->serial_num
);
2893 ovs_strlcpy(ods
->dp_desc
, p
->dp_desc
? p
->dp_desc
: default_dp_desc
,
2894 sizeof ods
->dp_desc
);
2895 ofconn_send_reply(ofconn
, msg
);
2901 handle_table_stats_request(struct ofconn
*ofconn
,
2902 const struct ofp_header
*request
)
2904 struct ofproto
*p
= ofconn_get_ofproto(ofconn
);
2905 struct ofp12_table_stats
*ots
;
2910 /* Set up default values.
2912 * ofp12_table_stats is used as a generic structure as
2913 * it is able to hold all the fields for ofp10_table_stats
2914 * and ofp11_table_stats (and of course itself).
2916 ots
= xcalloc(p
->n_tables
, sizeof *ots
);
2917 for (i
= 0; i
< p
->n_tables
; i
++) {
2918 ots
[i
].table_id
= i
;
2919 sprintf(ots
[i
].name
, "table%zu", i
);
2920 ots
[i
].match
= htonll(OFPXMT13_MASK
);
2921 ots
[i
].wildcards
= htonll(OFPXMT13_MASK
);
2922 ots
[i
].write_actions
= htonl(OFPAT11_OUTPUT
);
2923 ots
[i
].apply_actions
= htonl(OFPAT11_OUTPUT
);
2924 ots
[i
].write_setfields
= htonll(OFPXMT13_MASK
);
2925 ots
[i
].apply_setfields
= htonll(OFPXMT13_MASK
);
2926 ots
[i
].metadata_match
= htonll(UINT64_MAX
);
2927 ots
[i
].metadata_write
= htonll(UINT64_MAX
);
2928 ots
[i
].instructions
= htonl(OFPIT11_ALL
);
2929 ots
[i
].config
= htonl(OFPTC11_TABLE_MISS_MASK
);
2930 ots
[i
].max_entries
= htonl(1000000); /* An arbitrary big number. */
2931 ovs_rwlock_rdlock(&p
->tables
[i
].cls
.rwlock
);
2932 ots
[i
].active_count
= htonl(classifier_count(&p
->tables
[i
].cls
));
2933 ovs_rwlock_unlock(&p
->tables
[i
].cls
.rwlock
);
2936 p
->ofproto_class
->get_tables(p
, ots
);
2938 /* Post-process the tables, dropping hidden tables. */
2939 n_tables
= p
->n_tables
;
2940 for (i
= 0; i
< p
->n_tables
; i
++) {
2941 const struct oftable
*table
= &p
->tables
[i
];
2943 if (table
->flags
& OFTABLE_HIDDEN
) {
2949 ovs_strzcpy(ots
[i
].name
, table
->name
, sizeof ots
[i
].name
);
2952 if (table
->max_flows
< ntohl(ots
[i
].max_entries
)) {
2953 ots
[i
].max_entries
= htonl(table
->max_flows
);
2957 msg
= ofputil_encode_table_stats_reply(ots
, n_tables
, request
);
2958 ofconn_send_reply(ofconn
, msg
);
2966 append_port_stat(struct ofport
*port
, struct list
*replies
)
2968 struct ofputil_port_stats ops
= { .port_no
= port
->pp
.port_no
};
2970 calc_duration(port
->created
, time_msec(),
2971 &ops
.duration_sec
, &ops
.duration_nsec
);
2973 /* Intentionally ignore return value, since errors will set
2974 * 'stats' to all-1s, which is correct for OpenFlow, and
2975 * netdev_get_stats() will log errors. */
2976 ofproto_port_get_stats(port
, &ops
.stats
);
2978 ofputil_append_port_stat(replies
, &ops
);
2982 handle_port_stats_request(struct ofconn
*ofconn
,
2983 const struct ofp_header
*request
)
2985 struct ofproto
*p
= ofconn_get_ofproto(ofconn
);
2986 struct ofport
*port
;
2987 struct list replies
;
2991 error
= ofputil_decode_port_stats_request(request
, &port_no
);
2996 ofpmp_init(&replies
, request
);
2997 if (port_no
!= OFPP_ANY
) {
2998 port
= ofproto_get_port(p
, port_no
);
3000 append_port_stat(port
, &replies
);
3003 HMAP_FOR_EACH (port
, hmap_node
, &p
->ports
) {
3004 append_port_stat(port
, &replies
);
3008 ofconn_send_replies(ofconn
, &replies
);
3013 handle_port_desc_stats_request(struct ofconn
*ofconn
,
3014 const struct ofp_header
*request
)
3016 struct ofproto
*p
= ofconn_get_ofproto(ofconn
);
3017 enum ofp_version version
;
3018 struct ofport
*port
;
3019 struct list replies
;
3021 ofpmp_init(&replies
, request
);
3023 version
= ofputil_protocol_to_ofp_version(ofconn_get_protocol(ofconn
));
3024 HMAP_FOR_EACH (port
, hmap_node
, &p
->ports
) {
3025 ofputil_append_port_desc_stats_reply(version
, &port
->pp
, &replies
);
3028 ofconn_send_replies(ofconn
, &replies
);
3033 hash_cookie(ovs_be64 cookie
)
3035 return hash_2words((OVS_FORCE
uint64_t)cookie
>> 32,
3036 (OVS_FORCE
uint64_t)cookie
);
3040 cookies_insert(struct ofproto
*ofproto
, struct rule
*rule
)
3041 OVS_REQUIRES(ofproto_mutex
)
3043 hindex_insert(&ofproto
->cookies
, &rule
->cookie_node
,
3044 hash_cookie(rule
->flow_cookie
));
3048 cookies_remove(struct ofproto
*ofproto
, struct rule
*rule
)
3049 OVS_REQUIRES(ofproto_mutex
)
3051 hindex_remove(&ofproto
->cookies
, &rule
->cookie_node
);
3055 ofproto_rule_change_cookie(struct ofproto
*ofproto
, struct rule
*rule
,
3056 ovs_be64 new_cookie
)
3057 OVS_REQUIRES(ofproto_mutex
)
3059 if (new_cookie
!= rule
->flow_cookie
) {
3060 cookies_remove(ofproto
, rule
);
3062 ovs_mutex_lock(&rule
->mutex
);
3063 rule
->flow_cookie
= new_cookie
;
3064 ovs_mutex_unlock(&rule
->mutex
);
3066 cookies_insert(ofproto
, rule
);
3071 calc_duration(long long int start
, long long int now
,
3072 uint32_t *sec
, uint32_t *nsec
)
3074 long long int msecs
= now
- start
;
3075 *sec
= msecs
/ 1000;
3076 *nsec
= (msecs
% 1000) * (1000 * 1000);
3079 /* Checks whether 'table_id' is 0xff or a valid table ID in 'ofproto'. Returns
3080 * 0 if 'table_id' is OK, otherwise an OpenFlow error code. */
3082 check_table_id(const struct ofproto
*ofproto
, uint8_t table_id
)
3084 return (table_id
== 0xff || table_id
< ofproto
->n_tables
3086 : OFPERR_OFPBRC_BAD_TABLE_ID
);
3090 static struct oftable
*
3091 next_visible_table(const struct ofproto
*ofproto
, uint8_t table_id
)
3093 struct oftable
*table
;
3095 for (table
= &ofproto
->tables
[table_id
];
3096 table
< &ofproto
->tables
[ofproto
->n_tables
];
3098 if (!(table
->flags
& OFTABLE_HIDDEN
)) {
3106 static struct oftable
*
3107 first_matching_table(const struct ofproto
*ofproto
, uint8_t table_id
)
3109 if (table_id
== 0xff) {
3110 return next_visible_table(ofproto
, 0);
3111 } else if (table_id
< ofproto
->n_tables
) {
3112 return &ofproto
->tables
[table_id
];
3118 static struct oftable
*
3119 next_matching_table(const struct ofproto
*ofproto
,
3120 const struct oftable
*table
, uint8_t table_id
)
3122 return (table_id
== 0xff
3123 ? next_visible_table(ofproto
, (table
- ofproto
->tables
) + 1)
3127 /* Assigns TABLE to each oftable, in turn, that matches TABLE_ID in OFPROTO:
3129 * - If TABLE_ID is 0xff, this iterates over every classifier table in
3130 * OFPROTO, skipping tables marked OFTABLE_HIDDEN.
3132 * - If TABLE_ID is the number of a table in OFPROTO, then the loop iterates
3133 * only once, for that table. (This can be used to access tables marked
3136 * - Otherwise, TABLE_ID isn't valid for OFPROTO, so the loop won't be
3137 * entered at all. (Perhaps you should have validated TABLE_ID with
3138 * check_table_id().)
3140 * All parameters are evaluated multiple times.
3142 #define FOR_EACH_MATCHING_TABLE(TABLE, TABLE_ID, OFPROTO) \
3143 for ((TABLE) = first_matching_table(OFPROTO, TABLE_ID); \
3145 (TABLE) = next_matching_table(OFPROTO, TABLE, TABLE_ID))
3147 /* Initializes 'criteria' in a straightforward way based on the other
3150 * For "loose" matching, the 'priority' parameter is unimportant and may be
3153 rule_criteria_init(struct rule_criteria
*criteria
, uint8_t table_id
,
3154 const struct match
*match
, unsigned int priority
,
3155 ovs_be64 cookie
, ovs_be64 cookie_mask
,
3156 ofp_port_t out_port
, uint32_t out_group
)
3158 criteria
->table_id
= table_id
;
3159 cls_rule_init(&criteria
->cr
, match
, priority
);
3160 criteria
->cookie
= cookie
;
3161 criteria
->cookie_mask
= cookie_mask
;
3162 criteria
->out_port
= out_port
;
3163 criteria
->out_group
= out_group
;
3167 rule_criteria_destroy(struct rule_criteria
*criteria
)
3169 cls_rule_destroy(&criteria
->cr
);
3173 rule_collection_init(struct rule_collection
*rules
)
3175 rules
->rules
= rules
->stub
;
3177 rules
->capacity
= ARRAY_SIZE(rules
->stub
);
3181 rule_collection_add(struct rule_collection
*rules
, struct rule
*rule
)
3183 if (rules
->n
>= rules
->capacity
) {
3184 size_t old_size
, new_size
;
3186 old_size
= rules
->capacity
* sizeof *rules
->rules
;
3187 rules
->capacity
*= 2;
3188 new_size
= rules
->capacity
* sizeof *rules
->rules
;
3190 if (rules
->rules
== rules
->stub
) {
3191 rules
->rules
= xmalloc(new_size
);
3192 memcpy(rules
->rules
, rules
->stub
, old_size
);
3194 rules
->rules
= xrealloc(rules
->rules
, new_size
);
3198 rules
->rules
[rules
->n
++] = rule
;
3202 rule_collection_ref(struct rule_collection
*rules
)
3203 OVS_REQUIRES(ofproto_mutex
)
3207 for (i
= 0; i
< rules
->n
; i
++) {
3208 ofproto_rule_ref(rules
->rules
[i
]);
3213 rule_collection_unref(struct rule_collection
*rules
)
3217 for (i
= 0; i
< rules
->n
; i
++) {
3218 ofproto_rule_unref(rules
->rules
[i
]);
3223 rule_collection_destroy(struct rule_collection
*rules
)
3225 if (rules
->rules
!= rules
->stub
) {
3231 collect_rule(struct rule
*rule
, const struct rule_criteria
*c
,
3232 struct rule_collection
*rules
)
3233 OVS_REQUIRES(ofproto_mutex
)
3235 if (ofproto_rule_is_hidden(rule
)) {
3237 } else if (rule
->pending
) {
3238 return OFPROTO_POSTPONE
;
3240 if ((c
->table_id
== rule
->table_id
|| c
->table_id
== 0xff)
3241 && ofproto_rule_has_out_port(rule
, c
->out_port
)
3242 && ofproto_rule_has_out_group(rule
, c
->out_group
)
3243 && !((rule
->flow_cookie
^ c
->cookie
) & c
->cookie_mask
)) {
3244 rule_collection_add(rules
, rule
);
3250 /* Searches 'ofproto' for rules that match the criteria in 'criteria'. Matches
3251 * on classifiers rules are done in the "loose" way required for OpenFlow
3252 * OFPFC_MODIFY and OFPFC_DELETE requests. Puts the selected rules on list
3255 * Hidden rules are always omitted.
3257 * Returns 0 on success, otherwise an OpenFlow error code. */
3259 collect_rules_loose(struct ofproto
*ofproto
,
3260 const struct rule_criteria
*criteria
,
3261 struct rule_collection
*rules
)
3262 OVS_REQUIRES(ofproto_mutex
)
3264 struct oftable
*table
;
3267 rule_collection_init(rules
);
3269 error
= check_table_id(ofproto
, criteria
->table_id
);
3274 if (criteria
->cookie_mask
== htonll(UINT64_MAX
)) {
3277 HINDEX_FOR_EACH_WITH_HASH (rule
, cookie_node
,
3278 hash_cookie(criteria
->cookie
),
3279 &ofproto
->cookies
) {
3280 if (cls_rule_is_loose_match(&rule
->cr
, &criteria
->cr
.match
)) {
3281 error
= collect_rule(rule
, criteria
, rules
);
3288 FOR_EACH_MATCHING_TABLE (table
, criteria
->table_id
, ofproto
) {
3289 struct cls_cursor cursor
;
3292 ovs_rwlock_rdlock(&table
->cls
.rwlock
);
3293 cls_cursor_init(&cursor
, &table
->cls
, &criteria
->cr
);
3294 CLS_CURSOR_FOR_EACH (rule
, cr
, &cursor
) {
3295 error
= collect_rule(rule
, criteria
, rules
);
3300 ovs_rwlock_unlock(&table
->cls
.rwlock
);
3306 rule_collection_destroy(rules
);
3311 /* Searches 'ofproto' for rules that match the criteria in 'criteria'. Matches
3312 * on classifiers rules are done in the "strict" way required for OpenFlow
3313 * OFPFC_MODIFY_STRICT and OFPFC_DELETE_STRICT requests. Puts the selected
3314 * rules on list 'rules'.
3316 * Hidden rules are always omitted.
3318 * Returns 0 on success, otherwise an OpenFlow error code. */
3320 collect_rules_strict(struct ofproto
*ofproto
,
3321 const struct rule_criteria
*criteria
,
3322 struct rule_collection
*rules
)
3323 OVS_REQUIRES(ofproto_mutex
)
3325 struct oftable
*table
;
3328 rule_collection_init(rules
);
3330 error
= check_table_id(ofproto
, criteria
->table_id
);
3335 if (criteria
->cookie_mask
== htonll(UINT64_MAX
)) {
3338 HINDEX_FOR_EACH_WITH_HASH (rule
, cookie_node
,
3339 hash_cookie(criteria
->cookie
),
3340 &ofproto
->cookies
) {
3341 if (cls_rule_equal(&rule
->cr
, &criteria
->cr
)) {
3342 error
= collect_rule(rule
, criteria
, rules
);
3349 FOR_EACH_MATCHING_TABLE (table
, criteria
->table_id
, ofproto
) {
3352 ovs_rwlock_rdlock(&table
->cls
.rwlock
);
3353 rule
= rule_from_cls_rule(classifier_find_rule_exactly(
3354 &table
->cls
, &criteria
->cr
));
3355 ovs_rwlock_unlock(&table
->cls
.rwlock
);
3357 error
= collect_rule(rule
, criteria
, rules
);
3367 rule_collection_destroy(rules
);
3372 /* Returns 'age_ms' (a duration in milliseconds), converted to seconds and
3373 * forced into the range of a uint16_t. */
3375 age_secs(long long int age_ms
)
3377 return (age_ms
< 0 ? 0
3378 : age_ms
>= UINT16_MAX
* 1000 ? UINT16_MAX
3379 : (unsigned int) age_ms
/ 1000);
3383 handle_flow_stats_request(struct ofconn
*ofconn
,
3384 const struct ofp_header
*request
)
3385 OVS_EXCLUDED(ofproto_mutex
)
3387 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
3388 struct ofputil_flow_stats_request fsr
;
3389 struct rule_criteria criteria
;
3390 struct rule_collection rules
;
3391 struct list replies
;
3395 error
= ofputil_decode_flow_stats_request(&fsr
, request
);
3400 rule_criteria_init(&criteria
, fsr
.table_id
, &fsr
.match
, 0, fsr
.cookie
,
3401 fsr
.cookie_mask
, fsr
.out_port
, fsr
.out_group
);
3403 ovs_mutex_lock(&ofproto_mutex
);
3404 error
= collect_rules_loose(ofproto
, &criteria
, &rules
);
3405 rule_criteria_destroy(&criteria
);
3407 rule_collection_ref(&rules
);
3409 ovs_mutex_unlock(&ofproto_mutex
);
3415 ofpmp_init(&replies
, request
);
3416 for (i
= 0; i
< rules
.n
; i
++) {
3417 struct rule
*rule
= rules
.rules
[i
];
3418 long long int now
= time_msec();
3419 struct ofputil_flow_stats fs
;
3420 long long int created
, used
, modified
;
3421 struct rule_actions
*actions
;
3422 enum ofputil_flow_mod_flags flags
;
3424 ovs_mutex_lock(&rule
->mutex
);
3425 fs
.cookie
= rule
->flow_cookie
;
3426 fs
.idle_timeout
= rule
->idle_timeout
;
3427 fs
.hard_timeout
= rule
->hard_timeout
;
3428 created
= rule
->created
;
3430 modified
= rule
->modified
;
3431 actions
= rule_get_actions__(rule
);
3432 flags
= rule
->flags
;
3433 ovs_mutex_unlock(&rule
->mutex
);
3435 minimatch_expand(&rule
->cr
.match
, &fs
.match
);
3436 fs
.table_id
= rule
->table_id
;
3437 calc_duration(created
, now
, &fs
.duration_sec
, &fs
.duration_nsec
);
3438 fs
.priority
= rule
->cr
.priority
;
3439 fs
.idle_age
= age_secs(now
- used
);
3440 fs
.hard_age
= age_secs(now
- modified
);
3441 ofproto
->ofproto_class
->rule_get_stats(rule
, &fs
.packet_count
,
3443 fs
.ofpacts
= actions
->ofpacts
;
3444 fs
.ofpacts_len
= actions
->ofpacts_len
;
3447 ofputil_append_flow_stats_reply(&fs
, &replies
);
3449 rule_actions_unref(actions
);
3452 rule_collection_unref(&rules
);
3453 rule_collection_destroy(&rules
);
3455 ofconn_send_replies(ofconn
, &replies
);
3461 flow_stats_ds(struct rule
*rule
, struct ds
*results
)
3463 uint64_t packet_count
, byte_count
;
3464 struct rule_actions
*actions
;
3465 long long int created
;
3467 rule
->ofproto
->ofproto_class
->rule_get_stats(rule
,
3468 &packet_count
, &byte_count
);
3470 ovs_mutex_lock(&rule
->mutex
);
3471 actions
= rule_get_actions__(rule
);
3472 created
= rule
->created
;
3473 ovs_mutex_unlock(&rule
->mutex
);
3475 if (rule
->table_id
!= 0) {
3476 ds_put_format(results
, "table_id=%"PRIu8
", ", rule
->table_id
);
3478 ds_put_format(results
, "duration=%llds, ", (time_msec() - created
) / 1000);
3479 ds_put_format(results
, "priority=%u, ", rule
->cr
.priority
);
3480 ds_put_format(results
, "n_packets=%"PRIu64
", ", packet_count
);
3481 ds_put_format(results
, "n_bytes=%"PRIu64
", ", byte_count
);
3482 cls_rule_format(&rule
->cr
, results
);
3483 ds_put_char(results
, ',');
3485 ofpacts_format(actions
->ofpacts
, actions
->ofpacts_len
, results
);
3487 ds_put_cstr(results
, "\n");
3489 rule_actions_unref(actions
);
3492 /* Adds a pretty-printed description of all flows to 'results', including
3493 * hidden flows (e.g., set up by in-band control). */
3495 ofproto_get_all_flows(struct ofproto
*p
, struct ds
*results
)
3497 struct oftable
*table
;
3499 OFPROTO_FOR_EACH_TABLE (table
, p
) {
3500 struct cls_cursor cursor
;
3503 ovs_rwlock_rdlock(&table
->cls
.rwlock
);
3504 cls_cursor_init(&cursor
, &table
->cls
, NULL
);
3505 CLS_CURSOR_FOR_EACH (rule
, cr
, &cursor
) {
3506 flow_stats_ds(rule
, results
);
3508 ovs_rwlock_unlock(&table
->cls
.rwlock
);
3512 /* Obtains the NetFlow engine type and engine ID for 'ofproto' into
3513 * '*engine_type' and '*engine_id', respectively. */
3515 ofproto_get_netflow_ids(const struct ofproto
*ofproto
,
3516 uint8_t *engine_type
, uint8_t *engine_id
)
3518 ofproto
->ofproto_class
->get_netflow_ids(ofproto
, engine_type
, engine_id
);
3521 /* Checks the status of CFM configured on 'ofp_port' within 'ofproto'. Returns
3522 * true if the port's CFM status was successfully stored into '*status'.
3523 * Returns false if the port did not have CFM configured, in which case
3524 * '*status' is indeterminate.
3526 * The caller must provide and owns '*status', and must free 'status->rmps'. */
3528 ofproto_port_get_cfm_status(const struct ofproto
*ofproto
, ofp_port_t ofp_port
,
3529 struct ofproto_cfm_status
*status
)
3531 struct ofport
*ofport
= ofproto_get_port(ofproto
, ofp_port
);
3533 && ofproto
->ofproto_class
->get_cfm_status
3534 && ofproto
->ofproto_class
->get_cfm_status(ofport
, status
));
3538 handle_aggregate_stats_request(struct ofconn
*ofconn
,
3539 const struct ofp_header
*oh
)
3540 OVS_EXCLUDED(ofproto_mutex
)
3542 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
3543 struct ofputil_flow_stats_request request
;
3544 struct ofputil_aggregate_stats stats
;
3545 bool unknown_packets
, unknown_bytes
;
3546 struct rule_criteria criteria
;
3547 struct rule_collection rules
;
3548 struct ofpbuf
*reply
;
3552 error
= ofputil_decode_flow_stats_request(&request
, oh
);
3557 rule_criteria_init(&criteria
, request
.table_id
, &request
.match
, 0,
3558 request
.cookie
, request
.cookie_mask
,
3559 request
.out_port
, request
.out_group
);
3561 ovs_mutex_lock(&ofproto_mutex
);
3562 error
= collect_rules_loose(ofproto
, &criteria
, &rules
);
3563 rule_criteria_destroy(&criteria
);
3565 rule_collection_ref(&rules
);
3567 ovs_mutex_unlock(&ofproto_mutex
);
3573 memset(&stats
, 0, sizeof stats
);
3574 unknown_packets
= unknown_bytes
= false;
3575 for (i
= 0; i
< rules
.n
; i
++) {
3576 struct rule
*rule
= rules
.rules
[i
];
3577 uint64_t packet_count
;
3578 uint64_t byte_count
;
3580 ofproto
->ofproto_class
->rule_get_stats(rule
, &packet_count
,
3583 if (packet_count
== UINT64_MAX
) {
3584 unknown_packets
= true;
3586 stats
.packet_count
+= packet_count
;
3589 if (byte_count
== UINT64_MAX
) {
3590 unknown_bytes
= true;
3592 stats
.byte_count
+= byte_count
;
3597 if (unknown_packets
) {
3598 stats
.packet_count
= UINT64_MAX
;
3600 if (unknown_bytes
) {
3601 stats
.byte_count
= UINT64_MAX
;
3604 rule_collection_unref(&rules
);
3605 rule_collection_destroy(&rules
);
3607 reply
= ofputil_encode_aggregate_stats_reply(&stats
, oh
);
3608 ofconn_send_reply(ofconn
, reply
);
3613 struct queue_stats_cbdata
{
3614 struct ofport
*ofport
;
3615 struct list replies
;
3620 put_queue_stats(struct queue_stats_cbdata
*cbdata
, uint32_t queue_id
,
3621 const struct netdev_queue_stats
*stats
)
3623 struct ofputil_queue_stats oqs
;
3625 oqs
.port_no
= cbdata
->ofport
->pp
.port_no
;
3626 oqs
.queue_id
= queue_id
;
3627 oqs
.tx_bytes
= stats
->tx_bytes
;
3628 oqs
.tx_packets
= stats
->tx_packets
;
3629 oqs
.tx_errors
= stats
->tx_errors
;
3630 if (stats
->created
!= LLONG_MIN
) {
3631 calc_duration(stats
->created
, cbdata
->now
,
3632 &oqs
.duration_sec
, &oqs
.duration_nsec
);
3634 oqs
.duration_sec
= oqs
.duration_nsec
= UINT32_MAX
;
3636 ofputil_append_queue_stat(&cbdata
->replies
, &oqs
);
3640 handle_queue_stats_dump_cb(uint32_t queue_id
,
3641 struct netdev_queue_stats
*stats
,
3644 struct queue_stats_cbdata
*cbdata
= cbdata_
;
3646 put_queue_stats(cbdata
, queue_id
, stats
);
3650 handle_queue_stats_for_port(struct ofport
*port
, uint32_t queue_id
,
3651 struct queue_stats_cbdata
*cbdata
)
3653 cbdata
->ofport
= port
;
3654 if (queue_id
== OFPQ_ALL
) {
3655 netdev_dump_queue_stats(port
->netdev
,
3656 handle_queue_stats_dump_cb
, cbdata
);
3658 struct netdev_queue_stats stats
;
3660 if (!netdev_get_queue_stats(port
->netdev
, queue_id
, &stats
)) {
3661 put_queue_stats(cbdata
, queue_id
, &stats
);
3663 return OFPERR_OFPQOFC_BAD_QUEUE
;
3670 handle_queue_stats_request(struct ofconn
*ofconn
,
3671 const struct ofp_header
*rq
)
3673 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
3674 struct queue_stats_cbdata cbdata
;
3675 struct ofport
*port
;
3677 struct ofputil_queue_stats_request oqsr
;
3679 COVERAGE_INC(ofproto_queue_req
);
3681 ofpmp_init(&cbdata
.replies
, rq
);
3682 cbdata
.now
= time_msec();
3684 error
= ofputil_decode_queue_stats_request(rq
, &oqsr
);
3689 if (oqsr
.port_no
== OFPP_ANY
) {
3690 error
= OFPERR_OFPQOFC_BAD_QUEUE
;
3691 HMAP_FOR_EACH (port
, hmap_node
, &ofproto
->ports
) {
3692 if (!handle_queue_stats_for_port(port
, oqsr
.queue_id
, &cbdata
)) {
3697 port
= ofproto_get_port(ofproto
, oqsr
.port_no
);
3699 ? handle_queue_stats_for_port(port
, oqsr
.queue_id
, &cbdata
)
3700 : OFPERR_OFPQOFC_BAD_PORT
);
3703 ofconn_send_replies(ofconn
, &cbdata
.replies
);
3705 ofpbuf_list_delete(&cbdata
.replies
);
3712 is_flow_deletion_pending(const struct ofproto
*ofproto
,
3713 const struct cls_rule
*cls_rule
,
3715 OVS_REQUIRES(ofproto_mutex
)
3717 if (!hmap_is_empty(&ofproto
->deletions
)) {
3718 struct ofoperation
*op
;
3720 HMAP_FOR_EACH_WITH_HASH (op
, hmap_node
,
3721 cls_rule_hash(cls_rule
, table_id
),
3722 &ofproto
->deletions
) {
3723 if (cls_rule_equal(cls_rule
, &op
->rule
->cr
)) {
3733 should_evict_a_rule(struct oftable
*table
, unsigned int extra_space
)
3734 OVS_REQUIRES(ofproto_mutex
)
3735 OVS_NO_THREAD_SAFETY_ANALYSIS
3737 return classifier_count(&table
->cls
) + extra_space
> table
->max_flows
;
3741 evict_rules_from_table(struct ofproto
*ofproto
, struct oftable
*table
,
3742 unsigned int extra_space
)
3743 OVS_REQUIRES(ofproto_mutex
)
3745 while (should_evict_a_rule(table
, extra_space
)) {
3748 if (!choose_rule_to_evict(table
, &rule
)) {
3749 return OFPERR_OFPFMFC_TABLE_FULL
;
3750 } else if (rule
->pending
) {
3751 return OFPROTO_POSTPONE
;
3753 struct ofopgroup
*group
= ofopgroup_create_unattached(ofproto
);
3754 delete_flow__(rule
, group
, OFPRR_EVICTION
);
3755 ofopgroup_submit(group
);
3762 /* Implements OFPFC_ADD and the cases for OFPFC_MODIFY and OFPFC_MODIFY_STRICT
3763 * in which no matching flow already exists in the flow table.
3765 * Adds the flow specified by 'ofm', which is followed by 'n_actions'
3766 * ofp_actions, to the ofproto's flow table. Returns 0 on success, an OpenFlow
3767 * error code on failure, or OFPROTO_POSTPONE if the operation cannot be
3768 * initiated now but may be retried later.
3770 * The caller retains ownership of 'fm->ofpacts'.
3772 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
3775 add_flow(struct ofproto
*ofproto
, struct ofconn
*ofconn
,
3776 struct ofputil_flow_mod
*fm
, const struct ofp_header
*request
)
3777 OVS_REQUIRES(ofproto_mutex
)
3779 struct oftable
*table
;
3780 struct ofopgroup
*group
;
3786 error
= check_table_id(ofproto
, fm
->table_id
);
3792 if (fm
->table_id
== 0xff) {
3793 if (ofproto
->ofproto_class
->rule_choose_table
) {
3794 error
= ofproto
->ofproto_class
->rule_choose_table(ofproto
,
3800 ovs_assert(table_id
< ofproto
->n_tables
);
3804 } else if (fm
->table_id
< ofproto
->n_tables
) {
3805 table_id
= fm
->table_id
;
3807 return OFPERR_OFPBRC_BAD_TABLE_ID
;
3810 table
= &ofproto
->tables
[table_id
];
3812 if (table
->flags
& OFTABLE_READONLY
) {
3813 return OFPERR_OFPBRC_EPERM
;
3816 cls_rule_init(&cr
, &fm
->match
, fm
->priority
);
3818 /* Transform "add" into "modify" if there's an existing identical flow. */
3819 ovs_rwlock_rdlock(&table
->cls
.rwlock
);
3820 rule
= rule_from_cls_rule(classifier_find_rule_exactly(&table
->cls
, &cr
));
3821 ovs_rwlock_unlock(&table
->cls
.rwlock
);
3823 cls_rule_destroy(&cr
);
3824 if (!rule_is_modifiable(rule
)) {
3825 return OFPERR_OFPBRC_EPERM
;
3826 } else if (rule
->pending
) {
3827 return OFPROTO_POSTPONE
;
3829 struct rule_collection rules
;
3831 rule_collection_init(&rules
);
3832 rule_collection_add(&rules
, rule
);
3833 fm
->modify_cookie
= true;
3834 error
= modify_flows__(ofproto
, ofconn
, fm
, request
, &rules
);
3835 rule_collection_destroy(&rules
);
3841 /* Verify actions. */
3842 error
= ofproto_check_ofpacts(ofproto
, fm
->ofpacts
, fm
->ofpacts_len
,
3843 &fm
->match
.flow
, table_id
);
3845 cls_rule_destroy(&cr
);
3849 /* Serialize against pending deletion. */
3850 if (is_flow_deletion_pending(ofproto
, &cr
, table_id
)) {
3851 cls_rule_destroy(&cr
);
3852 return OFPROTO_POSTPONE
;
3855 /* Check for overlap, if requested. */
3856 if (fm
->flags
& OFPUTIL_FF_CHECK_OVERLAP
) {
3859 ovs_rwlock_rdlock(&table
->cls
.rwlock
);
3860 overlaps
= classifier_rule_overlaps(&table
->cls
, &cr
);
3861 ovs_rwlock_unlock(&table
->cls
.rwlock
);
3864 cls_rule_destroy(&cr
);
3865 return OFPERR_OFPFMFC_OVERLAP
;
3869 /* If necessary, evict an existing rule to clear out space. */
3870 error
= evict_rules_from_table(ofproto
, table
, 1);
3872 cls_rule_destroy(&cr
);
3876 /* Allocate new rule. */
3877 rule
= ofproto
->ofproto_class
->rule_alloc();
3879 cls_rule_destroy(&cr
);
3880 VLOG_WARN_RL(&rl
, "%s: failed to create rule (%s)",
3881 ofproto
->name
, ovs_strerror(error
));
3885 /* Initialize base state. */
3886 *CONST_CAST(struct ofproto
**, &rule
->ofproto
) = ofproto
;
3887 cls_rule_move(CONST_CAST(struct cls_rule
*, &rule
->cr
), &cr
);
3888 atomic_init(&rule
->ref_count
, 1);
3889 rule
->pending
= NULL
;
3890 rule
->flow_cookie
= fm
->new_cookie
;
3891 rule
->created
= rule
->modified
= rule
->used
= time_msec();
3893 ovs_mutex_init(&rule
->mutex
);
3894 ovs_mutex_lock(&rule
->mutex
);
3895 rule
->idle_timeout
= fm
->idle_timeout
;
3896 rule
->hard_timeout
= fm
->hard_timeout
;
3897 ovs_mutex_unlock(&rule
->mutex
);
3899 *CONST_CAST(uint8_t *, &rule
->table_id
) = table
- ofproto
->tables
;
3900 rule
->flags
= fm
->flags
& OFPUTIL_FF_STATE
;
3901 rule
->actions
= rule_actions_create(fm
->ofpacts
, fm
->ofpacts_len
);
3902 list_init(&rule
->meter_list_node
);
3903 rule
->eviction_group
= NULL
;
3904 list_init(&rule
->expirable
);
3905 rule
->monitor_flags
= 0;
3906 rule
->add_seqno
= 0;
3907 rule
->modify_seqno
= 0;
3909 /* Construct rule, initializing derived state. */
3910 error
= ofproto
->ofproto_class
->rule_construct(rule
);
3912 ofproto_rule_destroy__(rule
);
3917 oftable_insert_rule(rule
);
3919 group
= ofopgroup_create(ofproto
, ofconn
, request
, fm
->buffer_id
);
3920 ofoperation_create(group
, rule
, OFOPERATION_ADD
, 0);
3921 ofproto
->ofproto_class
->rule_insert(rule
);
3922 ofopgroup_submit(group
);
3927 /* OFPFC_MODIFY and OFPFC_MODIFY_STRICT. */
3929 /* Modifies the rules listed in 'rules', changing their actions to match those
3932 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
3935 * Returns 0 on success, otherwise an OpenFlow error code. */
3937 modify_flows__(struct ofproto
*ofproto
, struct ofconn
*ofconn
,
3938 struct ofputil_flow_mod
*fm
, const struct ofp_header
*request
,
3939 const struct rule_collection
*rules
)
3940 OVS_REQUIRES(ofproto_mutex
)
3942 enum ofoperation_type type
;
3943 struct ofopgroup
*group
;
3947 type
= fm
->command
== OFPFC_ADD
? OFOPERATION_REPLACE
: OFOPERATION_MODIFY
;
3948 group
= ofopgroup_create(ofproto
, ofconn
, request
, fm
->buffer_id
);
3949 error
= OFPERR_OFPBRC_EPERM
;
3950 for (i
= 0; i
< rules
->n
; i
++) {
3951 struct rule
*rule
= rules
->rules
[i
];
3952 struct ofoperation
*op
;
3953 bool actions_changed
;
3954 bool reset_counters
;
3956 /* FIXME: Implement OFPFUTIL_FF_RESET_COUNTS */
3958 if (rule_is_modifiable(rule
)) {
3959 /* At least one rule is modifiable, don't report EPERM error. */
3965 /* Verify actions. */
3966 error
= ofpacts_check(fm
->ofpacts
, fm
->ofpacts_len
, &fm
->match
.flow
,
3967 u16_to_ofp(ofproto
->max_ports
), rule
->table_id
);
3972 actions_changed
= !ofpacts_equal(fm
->ofpacts
, fm
->ofpacts_len
,
3973 rule
->actions
->ofpacts
,
3974 rule
->actions
->ofpacts_len
);
3976 op
= ofoperation_create(group
, rule
, type
, 0);
3978 if (fm
->modify_cookie
&& fm
->new_cookie
!= htonll(UINT64_MAX
)) {
3979 ofproto_rule_change_cookie(ofproto
, rule
, fm
->new_cookie
);
3981 if (type
== OFOPERATION_REPLACE
) {
3982 ovs_mutex_lock(&rule
->mutex
);
3983 rule
->idle_timeout
= fm
->idle_timeout
;
3984 rule
->hard_timeout
= fm
->hard_timeout
;
3985 ovs_mutex_unlock(&rule
->mutex
);
3987 rule
->flags
= fm
->flags
& OFPUTIL_FF_STATE
;
3988 if (fm
->idle_timeout
|| fm
->hard_timeout
) {
3989 if (!rule
->eviction_group
) {
3990 eviction_group_add_rule(rule
);
3993 eviction_group_remove_rule(rule
);
3997 reset_counters
= (fm
->flags
& OFPUTIL_FF_RESET_COUNTS
) != 0;
3998 if (actions_changed
|| reset_counters
) {
3999 struct rule_actions
*new_actions
;
4001 op
->actions
= rule
->actions
;
4002 new_actions
= rule_actions_create(fm
->ofpacts
, fm
->ofpacts_len
);
4004 ovs_mutex_lock(&rule
->mutex
);
4005 rule
->actions
= new_actions
;
4006 ovs_mutex_unlock(&rule
->mutex
);
4008 rule
->ofproto
->ofproto_class
->rule_modify_actions(rule
,
4011 ofoperation_complete(op
, 0);
4014 ofopgroup_submit(group
);
4020 modify_flows_add(struct ofproto
*ofproto
, struct ofconn
*ofconn
,
4021 struct ofputil_flow_mod
*fm
, const struct ofp_header
*request
)
4022 OVS_REQUIRES(ofproto_mutex
)
4024 if (fm
->cookie_mask
!= htonll(0) || fm
->new_cookie
== htonll(UINT64_MAX
)) {
4027 return add_flow(ofproto
, ofconn
, fm
, request
);
4030 /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code on
4033 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
4036 modify_flows_loose(struct ofproto
*ofproto
, struct ofconn
*ofconn
,
4037 struct ofputil_flow_mod
*fm
,
4038 const struct ofp_header
*request
)
4039 OVS_REQUIRES(ofproto_mutex
)
4041 struct rule_criteria criteria
;
4042 struct rule_collection rules
;
4045 rule_criteria_init(&criteria
, fm
->table_id
, &fm
->match
, 0,
4046 fm
->cookie
, fm
->cookie_mask
, OFPP_ANY
, OFPG11_ANY
);
4047 error
= collect_rules_loose(ofproto
, &criteria
, &rules
);
4048 rule_criteria_destroy(&criteria
);
4051 error
= (rules
.n
> 0
4052 ? modify_flows__(ofproto
, ofconn
, fm
, request
, &rules
)
4053 : modify_flows_add(ofproto
, ofconn
, fm
, request
));
4056 rule_collection_destroy(&rules
);
4061 /* Implements OFPFC_MODIFY_STRICT. Returns 0 on success or an OpenFlow error
4064 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
4067 modify_flow_strict(struct ofproto
*ofproto
, struct ofconn
*ofconn
,
4068 struct ofputil_flow_mod
*fm
,
4069 const struct ofp_header
*request
)
4070 OVS_REQUIRES(ofproto_mutex
)
4072 struct rule_criteria criteria
;
4073 struct rule_collection rules
;
4076 rule_criteria_init(&criteria
, fm
->table_id
, &fm
->match
, fm
->priority
,
4077 fm
->cookie
, fm
->cookie_mask
, OFPP_ANY
, OFPG11_ANY
);
4078 error
= collect_rules_strict(ofproto
, &criteria
, &rules
);
4079 rule_criteria_destroy(&criteria
);
4083 error
= modify_flows_add(ofproto
, ofconn
, fm
, request
);
4084 } else if (rules
.n
== 1) {
4085 error
= modify_flows__(ofproto
, ofconn
, fm
, request
, &rules
);
4089 rule_collection_destroy(&rules
);
4094 /* OFPFC_DELETE implementation. */
4097 delete_flow__(struct rule
*rule
, struct ofopgroup
*group
,
4098 enum ofp_flow_removed_reason reason
)
4099 OVS_REQUIRES(ofproto_mutex
)
4101 struct ofproto
*ofproto
= rule
->ofproto
;
4103 ofproto_rule_send_removed(rule
, reason
);
4105 ofoperation_create(group
, rule
, OFOPERATION_DELETE
, reason
);
4106 oftable_remove_rule(rule
);
4107 ofproto
->ofproto_class
->rule_delete(rule
);
4110 /* Deletes the rules listed in 'rules'.
4112 * Returns 0 on success, otherwise an OpenFlow error code. */
4114 delete_flows__(struct ofproto
*ofproto
, struct ofconn
*ofconn
,
4115 const struct ofp_header
*request
,
4116 const struct rule_collection
*rules
,
4117 enum ofp_flow_removed_reason reason
)
4118 OVS_REQUIRES(ofproto_mutex
)
4120 struct ofopgroup
*group
;
4123 group
= ofopgroup_create(ofproto
, ofconn
, request
, UINT32_MAX
);
4124 for (i
= 0; i
< rules
->n
; i
++) {
4125 delete_flow__(rules
->rules
[i
], group
, reason
);
4127 ofopgroup_submit(group
);
4132 /* Implements OFPFC_DELETE. */
4134 delete_flows_loose(struct ofproto
*ofproto
, struct ofconn
*ofconn
,
4135 const struct ofputil_flow_mod
*fm
,
4136 const struct ofp_header
*request
)
4137 OVS_REQUIRES(ofproto_mutex
)
4139 struct rule_criteria criteria
;
4140 struct rule_collection rules
;
4143 rule_criteria_init(&criteria
, fm
->table_id
, &fm
->match
, 0,
4144 fm
->cookie
, fm
->cookie_mask
,
4145 fm
->out_port
, fm
->out_group
);
4146 error
= collect_rules_loose(ofproto
, &criteria
, &rules
);
4147 rule_criteria_destroy(&criteria
);
4149 if (!error
&& rules
.n
> 0) {
4150 error
= delete_flows__(ofproto
, ofconn
, request
, &rules
, OFPRR_DELETE
);
4152 rule_collection_destroy(&rules
);
4157 /* Implements OFPFC_DELETE_STRICT. */
4159 delete_flow_strict(struct ofproto
*ofproto
, struct ofconn
*ofconn
,
4160 const struct ofputil_flow_mod
*fm
,
4161 const struct ofp_header
*request
)
4162 OVS_REQUIRES(ofproto_mutex
)
4164 struct rule_criteria criteria
;
4165 struct rule_collection rules
;
4168 rule_criteria_init(&criteria
, fm
->table_id
, &fm
->match
, fm
->priority
,
4169 fm
->cookie
, fm
->cookie_mask
,
4170 fm
->out_port
, fm
->out_group
);
4171 error
= collect_rules_strict(ofproto
, &criteria
, &rules
);
4172 rule_criteria_destroy(&criteria
);
4174 if (!error
&& rules
.n
> 0) {
4175 error
= delete_flows__(ofproto
, ofconn
, request
, &rules
, OFPRR_DELETE
);
4177 rule_collection_destroy(&rules
);
4183 ofproto_rule_send_removed(struct rule
*rule
, uint8_t reason
)
4184 OVS_REQUIRES(ofproto_mutex
)
4186 struct ofputil_flow_removed fr
;
4188 if (ofproto_rule_is_hidden(rule
) ||
4189 !(rule
->flags
& OFPUTIL_FF_SEND_FLOW_REM
)) {
4193 minimatch_expand(&rule
->cr
.match
, &fr
.match
);
4194 fr
.priority
= rule
->cr
.priority
;
4195 fr
.cookie
= rule
->flow_cookie
;
4197 fr
.table_id
= rule
->table_id
;
4198 calc_duration(rule
->created
, time_msec(),
4199 &fr
.duration_sec
, &fr
.duration_nsec
);
4200 ovs_mutex_lock(&rule
->mutex
);
4201 fr
.idle_timeout
= rule
->idle_timeout
;
4202 fr
.hard_timeout
= rule
->hard_timeout
;
4203 ovs_mutex_unlock(&rule
->mutex
);
4204 rule
->ofproto
->ofproto_class
->rule_get_stats(rule
, &fr
.packet_count
,
4207 connmgr_send_flow_removed(rule
->ofproto
->connmgr
, &fr
);
4210 /* Sends an OpenFlow "flow removed" message with the given 'reason' (either
4211 * OFPRR_HARD_TIMEOUT or OFPRR_IDLE_TIMEOUT), and then removes 'rule' from its
4214 * 'rule' must not have a pending operation (that is, 'rule->pending' must be
4217 * ofproto implementation ->run() functions should use this function to expire
4218 * OpenFlow flows. */
4220 ofproto_rule_expire(struct rule
*rule
, uint8_t reason
)
4221 OVS_REQUIRES(ofproto_mutex
)
4223 struct ofproto
*ofproto
= rule
->ofproto
;
4225 ovs_assert(reason
== OFPRR_HARD_TIMEOUT
|| reason
== OFPRR_IDLE_TIMEOUT
4226 || reason
== OFPRR_DELETE
|| reason
== OFPRR_GROUP_DELETE
);
4228 ofproto_rule_send_removed(rule
, reason
);
4229 ofproto_rule_delete__(ofproto
, rule
);
4232 /* Reduces '*timeout' to no more than 'max'. A value of zero in either case
4233 * means "infinite". */
4235 reduce_timeout(uint16_t max
, uint16_t *timeout
)
4237 if (max
&& (!*timeout
|| *timeout
> max
)) {
4242 /* If 'idle_timeout' is nonzero, and 'rule' has no idle timeout or an idle
4243 * timeout greater than 'idle_timeout', lowers 'rule''s idle timeout to
4244 * 'idle_timeout' seconds. Similarly for 'hard_timeout'.
4246 * Suitable for implementing OFPACT_FIN_TIMEOUT. */
4248 ofproto_rule_reduce_timeouts(struct rule
*rule
,
4249 uint16_t idle_timeout
, uint16_t hard_timeout
)
4250 OVS_EXCLUDED(ofproto_mutex
, rule
->mutex
)
4252 if (!idle_timeout
&& !hard_timeout
) {
4256 ovs_mutex_lock(&ofproto_mutex
);
4257 if (list_is_empty(&rule
->expirable
)) {
4258 list_insert(&rule
->ofproto
->expirable
, &rule
->expirable
);
4260 ovs_mutex_unlock(&ofproto_mutex
);
4262 ovs_mutex_lock(&rule
->mutex
);
4263 reduce_timeout(idle_timeout
, &rule
->idle_timeout
);
4264 reduce_timeout(hard_timeout
, &rule
->hard_timeout
);
4265 ovs_mutex_unlock(&rule
->mutex
);
4269 handle_flow_mod(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
4270 OVS_EXCLUDED(ofproto_mutex
)
4272 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
4273 struct ofputil_flow_mod fm
;
4274 uint64_t ofpacts_stub
[1024 / 8];
4275 struct ofpbuf ofpacts
;
4279 error
= reject_slave_controller(ofconn
);
4284 ofpbuf_use_stub(&ofpacts
, ofpacts_stub
, sizeof ofpacts_stub
);
4285 error
= ofputil_decode_flow_mod(&fm
, oh
, ofconn_get_protocol(ofconn
),
4288 error
= handle_flow_mod__(ofproto
, ofconn
, &fm
, oh
);
4291 goto exit_free_ofpacts
;
4294 /* Record the operation for logging a summary report. */
4295 switch (fm
.command
) {
4301 case OFPFC_MODIFY_STRICT
:
4302 ofproto
->n_modify
++;
4306 case OFPFC_DELETE_STRICT
:
4307 ofproto
->n_delete
++;
4312 if (ofproto
->next_op_report
== LLONG_MAX
) {
4313 ofproto
->first_op
= now
;
4314 ofproto
->next_op_report
= MAX(now
+ 10 * 1000,
4315 ofproto
->op_backoff
);
4316 ofproto
->op_backoff
= ofproto
->next_op_report
+ 60 * 1000;
4318 ofproto
->last_op
= now
;
4321 ofpbuf_uninit(&ofpacts
);
4327 handle_flow_mod__(struct ofproto
*ofproto
, struct ofconn
*ofconn
,
4328 struct ofputil_flow_mod
*fm
, const struct ofp_header
*oh
)
4329 OVS_EXCLUDED(ofproto_mutex
)
4333 ovs_mutex_lock(&ofproto_mutex
);
4334 if (ofproto
->n_pending
< 50) {
4335 switch (fm
->command
) {
4337 error
= add_flow(ofproto
, ofconn
, fm
, oh
);
4341 error
= modify_flows_loose(ofproto
, ofconn
, fm
, oh
);
4344 case OFPFC_MODIFY_STRICT
:
4345 error
= modify_flow_strict(ofproto
, ofconn
, fm
, oh
);
4349 error
= delete_flows_loose(ofproto
, ofconn
, fm
, oh
);
4352 case OFPFC_DELETE_STRICT
:
4353 error
= delete_flow_strict(ofproto
, ofconn
, fm
, oh
);
4357 if (fm
->command
> 0xff) {
4358 VLOG_WARN_RL(&rl
, "%s: flow_mod has explicit table_id but "
4359 "flow_mod_table_id extension is not enabled",
4362 error
= OFPERR_OFPFMFC_BAD_COMMAND
;
4366 ovs_assert(!list_is_empty(&ofproto
->pending
));
4367 error
= OFPROTO_POSTPONE
;
4369 ovs_mutex_unlock(&ofproto_mutex
);
4371 run_rule_executes(ofproto
);
4376 handle_role_request(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
4378 struct ofputil_role_request request
;
4379 struct ofputil_role_request reply
;
4383 error
= ofputil_decode_role_message(oh
, &request
);
4388 if (request
.role
!= OFPCR12_ROLE_NOCHANGE
) {
4389 if (ofconn_get_role(ofconn
) != request
.role
4390 && ofconn_has_pending_opgroups(ofconn
)) {
4391 return OFPROTO_POSTPONE
;
4394 if (request
.have_generation_id
4395 && !ofconn_set_master_election_id(ofconn
, request
.generation_id
)) {
4396 return OFPERR_OFPRRFC_STALE
;
4399 ofconn_set_role(ofconn
, request
.role
);
4402 reply
.role
= ofconn_get_role(ofconn
);
4403 reply
.have_generation_id
= ofconn_get_master_election_id(
4404 ofconn
, &reply
.generation_id
);
4405 buf
= ofputil_encode_role_reply(oh
, &reply
);
4406 ofconn_send_reply(ofconn
, buf
);
4412 handle_nxt_flow_mod_table_id(struct ofconn
*ofconn
,
4413 const struct ofp_header
*oh
)
4415 const struct nx_flow_mod_table_id
*msg
= ofpmsg_body(oh
);
4416 enum ofputil_protocol cur
, next
;
4418 cur
= ofconn_get_protocol(ofconn
);
4419 next
= ofputil_protocol_set_tid(cur
, msg
->set
!= 0);
4420 ofconn_set_protocol(ofconn
, next
);
4426 handle_nxt_set_flow_format(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
4428 const struct nx_set_flow_format
*msg
= ofpmsg_body(oh
);
4429 enum ofputil_protocol cur
, next
;
4430 enum ofputil_protocol next_base
;
4432 next_base
= ofputil_nx_flow_format_to_protocol(ntohl(msg
->format
));
4434 return OFPERR_OFPBRC_EPERM
;
4437 cur
= ofconn_get_protocol(ofconn
);
4438 next
= ofputil_protocol_set_base(cur
, next_base
);
4439 if (cur
!= next
&& ofconn_has_pending_opgroups(ofconn
)) {
4440 /* Avoid sending async messages in surprising protocol. */
4441 return OFPROTO_POSTPONE
;
4444 ofconn_set_protocol(ofconn
, next
);
4449 handle_nxt_set_packet_in_format(struct ofconn
*ofconn
,
4450 const struct ofp_header
*oh
)
4452 const struct nx_set_packet_in_format
*msg
= ofpmsg_body(oh
);
4455 format
= ntohl(msg
->format
);
4456 if (format
!= NXPIF_OPENFLOW10
&& format
!= NXPIF_NXM
) {
4457 return OFPERR_OFPBRC_EPERM
;
4460 if (format
!= ofconn_get_packet_in_format(ofconn
)
4461 && ofconn_has_pending_opgroups(ofconn
)) {
4462 /* Avoid sending async message in surprsing packet in format. */
4463 return OFPROTO_POSTPONE
;
4466 ofconn_set_packet_in_format(ofconn
, format
);
4471 handle_nxt_set_async_config(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
4473 const struct nx_async_config
*msg
= ofpmsg_body(oh
);
4474 uint32_t master
[OAM_N_TYPES
];
4475 uint32_t slave
[OAM_N_TYPES
];
4477 master
[OAM_PACKET_IN
] = ntohl(msg
->packet_in_mask
[0]);
4478 master
[OAM_PORT_STATUS
] = ntohl(msg
->port_status_mask
[0]);
4479 master
[OAM_FLOW_REMOVED
] = ntohl(msg
->flow_removed_mask
[0]);
4481 slave
[OAM_PACKET_IN
] = ntohl(msg
->packet_in_mask
[1]);
4482 slave
[OAM_PORT_STATUS
] = ntohl(msg
->port_status_mask
[1]);
4483 slave
[OAM_FLOW_REMOVED
] = ntohl(msg
->flow_removed_mask
[1]);
4485 ofconn_set_async_config(ofconn
, master
, slave
);
4486 if (ofconn_get_type(ofconn
) == OFCONN_SERVICE
&&
4487 !ofconn_get_miss_send_len(ofconn
)) {
4488 ofconn_set_miss_send_len(ofconn
, OFP_DEFAULT_MISS_SEND_LEN
);
4495 handle_nxt_get_async_request(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
4498 uint32_t master
[OAM_N_TYPES
];
4499 uint32_t slave
[OAM_N_TYPES
];
4500 struct nx_async_config
*msg
;
4502 ofconn_get_async_config(ofconn
, master
, slave
);
4503 buf
= ofpraw_alloc_reply(OFPRAW_OFPT13_GET_ASYNC_REPLY
, oh
, 0);
4504 msg
= ofpbuf_put_zeros(buf
, sizeof *msg
);
4506 msg
->packet_in_mask
[0] = htonl(master
[OAM_PACKET_IN
]);
4507 msg
->port_status_mask
[0] = htonl(master
[OAM_PORT_STATUS
]);
4508 msg
->flow_removed_mask
[0] = htonl(master
[OAM_FLOW_REMOVED
]);
4510 msg
->packet_in_mask
[1] = htonl(slave
[OAM_PACKET_IN
]);
4511 msg
->port_status_mask
[1] = htonl(slave
[OAM_PORT_STATUS
]);
4512 msg
->flow_removed_mask
[1] = htonl(slave
[OAM_FLOW_REMOVED
]);
4514 ofconn_send_reply(ofconn
, buf
);
4520 handle_nxt_set_controller_id(struct ofconn
*ofconn
,
4521 const struct ofp_header
*oh
)
4523 const struct nx_controller_id
*nci
= ofpmsg_body(oh
);
4525 if (!is_all_zeros(nci
->zero
, sizeof nci
->zero
)) {
4526 return OFPERR_NXBRC_MUST_BE_ZERO
;
4529 ofconn_set_controller_id(ofconn
, ntohs(nci
->controller_id
));
4534 handle_barrier_request(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
4538 if (ofconn_has_pending_opgroups(ofconn
)) {
4539 return OFPROTO_POSTPONE
;
4542 buf
= ofpraw_alloc_reply((oh
->version
== OFP10_VERSION
4543 ? OFPRAW_OFPT10_BARRIER_REPLY
4544 : OFPRAW_OFPT11_BARRIER_REPLY
), oh
, 0);
4545 ofconn_send_reply(ofconn
, buf
);
4550 ofproto_compose_flow_refresh_update(const struct rule
*rule
,
4551 enum nx_flow_monitor_flags flags
,
4553 OVS_REQUIRES(ofproto_mutex
)
4555 struct ofoperation
*op
= rule
->pending
;
4556 const struct rule_actions
*actions
;
4557 struct ofputil_flow_update fu
;
4560 if (op
&& op
->type
== OFOPERATION_ADD
) {
4561 /* We'll report the final flow when the operation completes. Reporting
4562 * it now would cause a duplicate report later. */
4566 fu
.event
= (flags
& (NXFMF_INITIAL
| NXFMF_ADD
)
4567 ? NXFME_ADDED
: NXFME_MODIFIED
);
4569 ovs_mutex_lock(&rule
->mutex
);
4570 fu
.idle_timeout
= rule
->idle_timeout
;
4571 fu
.hard_timeout
= rule
->hard_timeout
;
4572 ovs_mutex_unlock(&rule
->mutex
);
4573 fu
.table_id
= rule
->table_id
;
4574 fu
.cookie
= rule
->flow_cookie
;
4575 minimatch_expand(&rule
->cr
.match
, &match
);
4577 fu
.priority
= rule
->cr
.priority
;
4579 if (!(flags
& NXFMF_ACTIONS
)) {
4582 actions
= rule
->actions
;
4584 /* An operation is in progress. Use the previous version of the flow's
4585 * actions, so that when the operation commits we report the change. */
4587 case OFOPERATION_ADD
:
4590 case OFOPERATION_MODIFY
:
4591 case OFOPERATION_REPLACE
:
4592 actions
= op
->actions
? op
->actions
: rule
->actions
;
4595 case OFOPERATION_DELETE
:
4596 actions
= rule
->actions
;
4603 fu
.ofpacts
= actions
? actions
->ofpacts
: NULL
;
4604 fu
.ofpacts_len
= actions
? actions
->ofpacts_len
: 0;
4606 if (list_is_empty(msgs
)) {
4607 ofputil_start_flow_update(msgs
);
4609 ofputil_append_flow_update(&fu
, msgs
);
4613 ofmonitor_compose_refresh_updates(struct rule_collection
*rules
,
4615 OVS_REQUIRES(ofproto_mutex
)
4619 for (i
= 0; i
< rules
->n
; i
++) {
4620 struct rule
*rule
= rules
->rules
[i
];
4621 enum nx_flow_monitor_flags flags
= rule
->monitor_flags
;
4622 rule
->monitor_flags
= 0;
4624 ofproto_compose_flow_refresh_update(rule
, flags
, msgs
);
4629 ofproto_collect_ofmonitor_refresh_rule(const struct ofmonitor
*m
,
4630 struct rule
*rule
, uint64_t seqno
,
4631 struct rule_collection
*rules
)
4632 OVS_REQUIRES(ofproto_mutex
)
4634 enum nx_flow_monitor_flags update
;
4636 if (ofproto_rule_is_hidden(rule
)) {
4641 ? ofoperation_has_out_port(rule
->pending
, m
->out_port
)
4642 : ofproto_rule_has_out_port(rule
, m
->out_port
))) {
4647 if (rule
->add_seqno
> seqno
) {
4648 update
= NXFMF_ADD
| NXFMF_MODIFY
;
4649 } else if (rule
->modify_seqno
> seqno
) {
4650 update
= NXFMF_MODIFY
;
4655 if (!(m
->flags
& update
)) {
4659 update
= NXFMF_INITIAL
;
4662 if (!rule
->monitor_flags
) {
4663 rule_collection_add(rules
, rule
);
4665 rule
->monitor_flags
|= update
| (m
->flags
& NXFMF_ACTIONS
);
4669 ofproto_collect_ofmonitor_refresh_rules(const struct ofmonitor
*m
,
4671 struct rule_collection
*rules
)
4672 OVS_REQUIRES(ofproto_mutex
)
4674 const struct ofproto
*ofproto
= ofconn_get_ofproto(m
->ofconn
);
4675 const struct ofoperation
*op
;
4676 const struct oftable
*table
;
4677 struct cls_rule target
;
4679 cls_rule_init_from_minimatch(&target
, &m
->match
, 0);
4680 FOR_EACH_MATCHING_TABLE (table
, m
->table_id
, ofproto
) {
4681 struct cls_cursor cursor
;
4684 ovs_rwlock_rdlock(&table
->cls
.rwlock
);
4685 cls_cursor_init(&cursor
, &table
->cls
, &target
);
4686 CLS_CURSOR_FOR_EACH (rule
, cr
, &cursor
) {
4687 ovs_assert(!rule
->pending
); /* XXX */
4688 ofproto_collect_ofmonitor_refresh_rule(m
, rule
, seqno
, rules
);
4690 ovs_rwlock_unlock(&table
->cls
.rwlock
);
4693 HMAP_FOR_EACH (op
, hmap_node
, &ofproto
->deletions
) {
4694 struct rule
*rule
= op
->rule
;
4696 if (((m
->table_id
== 0xff
4697 ? !(ofproto
->tables
[rule
->table_id
].flags
& OFTABLE_HIDDEN
)
4698 : m
->table_id
== rule
->table_id
))
4699 && cls_rule_is_loose_match(&rule
->cr
, &target
.match
)) {
4700 ofproto_collect_ofmonitor_refresh_rule(m
, rule
, seqno
, rules
);
4703 cls_rule_destroy(&target
);
4707 ofproto_collect_ofmonitor_initial_rules(struct ofmonitor
*m
,
4708 struct rule_collection
*rules
)
4709 OVS_REQUIRES(ofproto_mutex
)
4711 if (m
->flags
& NXFMF_INITIAL
) {
4712 ofproto_collect_ofmonitor_refresh_rules(m
, 0, rules
);
4717 ofmonitor_collect_resume_rules(struct ofmonitor
*m
,
4718 uint64_t seqno
, struct rule_collection
*rules
)
4719 OVS_REQUIRES(ofproto_mutex
)
4721 ofproto_collect_ofmonitor_refresh_rules(m
, seqno
, rules
);
4725 handle_flow_monitor_request(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
4726 OVS_EXCLUDED(ofproto_mutex
)
4728 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
4729 struct ofmonitor
**monitors
;
4730 size_t n_monitors
, allocated_monitors
;
4731 struct rule_collection rules
;
4732 struct list replies
;
4738 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
4740 n_monitors
= allocated_monitors
= 0;
4742 ovs_mutex_lock(&ofproto_mutex
);
4744 struct ofputil_flow_monitor_request request
;
4745 struct ofmonitor
*m
;
4748 retval
= ofputil_decode_flow_monitor_request(&request
, &b
);
4749 if (retval
== EOF
) {
4751 } else if (retval
) {
4756 if (request
.table_id
!= 0xff
4757 && request
.table_id
>= ofproto
->n_tables
) {
4758 error
= OFPERR_OFPBRC_BAD_TABLE_ID
;
4762 error
= ofmonitor_create(&request
, ofconn
, &m
);
4767 if (n_monitors
>= allocated_monitors
) {
4768 monitors
= x2nrealloc(monitors
, &allocated_monitors
,
4771 monitors
[n_monitors
++] = m
;
4774 rule_collection_init(&rules
);
4775 for (i
= 0; i
< n_monitors
; i
++) {
4776 ofproto_collect_ofmonitor_initial_rules(monitors
[i
], &rules
);
4779 ofpmp_init(&replies
, oh
);
4780 ofmonitor_compose_refresh_updates(&rules
, &replies
);
4781 ovs_mutex_unlock(&ofproto_mutex
);
4783 rule_collection_destroy(&rules
);
4785 ofconn_send_replies(ofconn
, &replies
);
4791 ovs_mutex_unlock(&ofproto_mutex
);
4793 for (i
= 0; i
< n_monitors
; i
++) {
4794 ofmonitor_destroy(monitors
[i
]);
4801 handle_flow_monitor_cancel(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
4802 OVS_EXCLUDED(ofproto_mutex
)
4804 struct ofmonitor
*m
;
4808 id
= ofputil_decode_flow_monitor_cancel(oh
);
4810 ovs_mutex_lock(&ofproto_mutex
);
4811 m
= ofmonitor_lookup(ofconn
, id
);
4813 ofmonitor_destroy(m
);
4816 error
= OFPERR_NXBRC_FM_BAD_ID
;
4818 ovs_mutex_unlock(&ofproto_mutex
);
4823 /* Meters implementation.
4825 * Meter table entry, indexed by the OpenFlow meter_id.
4826 * These are always dynamically allocated to allocate enough space for
4828 * 'created' is used to compute the duration for meter stats.
4829 * 'list rules' is needed so that we can delete the dependent rules when the
4830 * meter table entry is deleted.
4831 * 'provider_meter_id' is for the provider's private use.
4834 long long int created
; /* Time created. */
4835 struct list rules
; /* List of "struct rule_dpif"s. */
4836 ofproto_meter_id provider_meter_id
;
4837 uint16_t flags
; /* Meter flags. */
4838 uint16_t n_bands
; /* Number of meter bands. */
4839 struct ofputil_meter_band
*bands
;
4843 * This is used in instruction validation at flow set-up time,
4844 * as flows may not use non-existing meters.
4845 * This is also used by ofproto-providers to translate OpenFlow meter_ids
4846 * in METER instructions to the corresponding provider meter IDs.
4847 * Return value of UINT32_MAX signifies an invalid meter.
4850 ofproto_get_provider_meter_id(const struct ofproto
* ofproto
,
4851 uint32_t of_meter_id
)
4853 if (of_meter_id
&& of_meter_id
<= ofproto
->meter_features
.max_meters
) {
4854 const struct meter
*meter
= ofproto
->meters
[of_meter_id
];
4856 return meter
->provider_meter_id
.uint32
;
4863 meter_update(struct meter
*meter
, const struct ofputil_meter_config
*config
)
4867 meter
->flags
= config
->flags
;
4868 meter
->n_bands
= config
->n_bands
;
4869 meter
->bands
= xmemdup(config
->bands
,
4870 config
->n_bands
* sizeof *meter
->bands
);
4873 static struct meter
*
4874 meter_create(const struct ofputil_meter_config
*config
,
4875 ofproto_meter_id provider_meter_id
)
4877 struct meter
*meter
;
4879 meter
= xzalloc(sizeof *meter
);
4880 meter
->provider_meter_id
= provider_meter_id
;
4881 meter
->created
= time_msec();
4882 list_init(&meter
->rules
);
4884 meter_update(meter
, config
);
4890 meter_delete(struct ofproto
*ofproto
, uint32_t first
, uint32_t last
)
4891 OVS_REQUIRES(ofproto_mutex
)
4894 for (mid
= first
; mid
<= last
; ++mid
) {
4895 struct meter
*meter
= ofproto
->meters
[mid
];
4897 ofproto
->meters
[mid
] = NULL
;
4898 ofproto
->ofproto_class
->meter_del(ofproto
,
4899 meter
->provider_meter_id
);
4907 handle_add_meter(struct ofproto
*ofproto
, struct ofputil_meter_mod
*mm
)
4909 ofproto_meter_id provider_meter_id
= { UINT32_MAX
};
4910 struct meter
**meterp
= &ofproto
->meters
[mm
->meter
.meter_id
];
4914 return OFPERR_OFPMMFC_METER_EXISTS
;
4917 error
= ofproto
->ofproto_class
->meter_set(ofproto
, &provider_meter_id
,
4920 ovs_assert(provider_meter_id
.uint32
!= UINT32_MAX
);
4921 *meterp
= meter_create(&mm
->meter
, provider_meter_id
);
4927 handle_modify_meter(struct ofproto
*ofproto
, struct ofputil_meter_mod
*mm
)
4929 struct meter
*meter
= ofproto
->meters
[mm
->meter
.meter_id
];
4933 return OFPERR_OFPMMFC_UNKNOWN_METER
;
4936 error
= ofproto
->ofproto_class
->meter_set(ofproto
,
4937 &meter
->provider_meter_id
,
4939 ovs_assert(meter
->provider_meter_id
.uint32
!= UINT32_MAX
);
4941 meter_update(meter
, &mm
->meter
);
4947 handle_delete_meter(struct ofconn
*ofconn
, const struct ofp_header
*oh
,
4948 struct ofputil_meter_mod
*mm
)
4949 OVS_EXCLUDED(ofproto_mutex
)
4951 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
4952 uint32_t meter_id
= mm
->meter
.meter_id
;
4953 struct rule_collection rules
;
4954 enum ofperr error
= 0;
4955 uint32_t first
, last
;
4957 if (meter_id
== OFPM13_ALL
) {
4959 last
= ofproto
->meter_features
.max_meters
;
4961 if (!meter_id
|| meter_id
> ofproto
->meter_features
.max_meters
) {
4964 first
= last
= meter_id
;
4967 /* First delete the rules that use this meter. If any of those rules are
4968 * currently being modified, postpone the whole operation until later. */
4969 rule_collection_init(&rules
);
4970 ovs_mutex_lock(&ofproto_mutex
);
4971 for (meter_id
= first
; meter_id
<= last
; ++meter_id
) {
4972 struct meter
*meter
= ofproto
->meters
[meter_id
];
4973 if (meter
&& !list_is_empty(&meter
->rules
)) {
4976 LIST_FOR_EACH (rule
, meter_list_node
, &meter
->rules
) {
4977 if (rule
->pending
) {
4978 error
= OFPROTO_POSTPONE
;
4981 rule_collection_add(&rules
, rule
);
4986 delete_flows__(ofproto
, ofconn
, oh
, &rules
, OFPRR_METER_DELETE
);
4989 /* Delete the meters. */
4990 meter_delete(ofproto
, first
, last
);
4993 ovs_mutex_unlock(&ofproto_mutex
);
4994 rule_collection_destroy(&rules
);
5000 handle_meter_mod(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
5002 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
5003 struct ofputil_meter_mod mm
;
5004 uint64_t bands_stub
[256 / 8];
5005 struct ofpbuf bands
;
5009 error
= reject_slave_controller(ofconn
);
5014 ofpbuf_use_stub(&bands
, bands_stub
, sizeof bands_stub
);
5016 error
= ofputil_decode_meter_mod(oh
, &mm
, &bands
);
5018 goto exit_free_bands
;
5021 meter_id
= mm
.meter
.meter_id
;
5023 if (mm
.command
!= OFPMC13_DELETE
) {
5024 /* Fails also when meters are not implemented by the provider. */
5025 if (meter_id
== 0 || meter_id
> OFPM13_MAX
) {
5026 error
= OFPERR_OFPMMFC_INVALID_METER
;
5027 goto exit_free_bands
;
5028 } else if (meter_id
> ofproto
->meter_features
.max_meters
) {
5029 error
= OFPERR_OFPMMFC_OUT_OF_METERS
;
5030 goto exit_free_bands
;
5032 if (mm
.meter
.n_bands
> ofproto
->meter_features
.max_bands
) {
5033 error
= OFPERR_OFPMMFC_OUT_OF_BANDS
;
5034 goto exit_free_bands
;
5038 switch (mm
.command
) {
5040 error
= handle_add_meter(ofproto
, &mm
);
5043 case OFPMC13_MODIFY
:
5044 error
= handle_modify_meter(ofproto
, &mm
);
5047 case OFPMC13_DELETE
:
5048 error
= handle_delete_meter(ofconn
, oh
, &mm
);
5052 error
= OFPERR_OFPMMFC_BAD_COMMAND
;
5057 ofpbuf_uninit(&bands
);
5062 handle_meter_features_request(struct ofconn
*ofconn
,
5063 const struct ofp_header
*request
)
5065 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
5066 struct ofputil_meter_features features
;
5069 if (ofproto
->ofproto_class
->meter_get_features
) {
5070 ofproto
->ofproto_class
->meter_get_features(ofproto
, &features
);
5072 memset(&features
, 0, sizeof features
);
5074 b
= ofputil_encode_meter_features_reply(&features
, request
);
5076 ofconn_send_reply(ofconn
, b
);
5081 handle_meter_request(struct ofconn
*ofconn
, const struct ofp_header
*request
,
5084 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
5085 struct list replies
;
5086 uint64_t bands_stub
[256 / 8];
5087 struct ofpbuf bands
;
5088 uint32_t meter_id
, first
, last
;
5090 ofputil_decode_meter_request(request
, &meter_id
);
5092 if (meter_id
== OFPM13_ALL
) {
5094 last
= ofproto
->meter_features
.max_meters
;
5096 if (!meter_id
|| meter_id
> ofproto
->meter_features
.max_meters
||
5097 !ofproto
->meters
[meter_id
]) {
5098 return OFPERR_OFPMMFC_UNKNOWN_METER
;
5100 first
= last
= meter_id
;
5103 ofpbuf_use_stub(&bands
, bands_stub
, sizeof bands_stub
);
5104 ofpmp_init(&replies
, request
);
5106 for (meter_id
= first
; meter_id
<= last
; ++meter_id
) {
5107 struct meter
*meter
= ofproto
->meters
[meter_id
];
5109 continue; /* Skip non-existing meters. */
5111 if (type
== OFPTYPE_METER_STATS_REQUEST
) {
5112 struct ofputil_meter_stats stats
;
5114 stats
.meter_id
= meter_id
;
5116 /* Provider sets the packet and byte counts, we do the rest. */
5117 stats
.flow_count
= list_size(&meter
->rules
);
5118 calc_duration(meter
->created
, time_msec(),
5119 &stats
.duration_sec
, &stats
.duration_nsec
);
5120 stats
.n_bands
= meter
->n_bands
;
5121 ofpbuf_clear(&bands
);
5123 = ofpbuf_put_uninit(&bands
,
5124 meter
->n_bands
* sizeof *stats
.bands
);
5126 if (!ofproto
->ofproto_class
->meter_get(ofproto
,
5127 meter
->provider_meter_id
,
5129 ofputil_append_meter_stats(&replies
, &stats
);
5131 } else { /* type == OFPTYPE_METER_CONFIG_REQUEST */
5132 struct ofputil_meter_config config
;
5134 config
.meter_id
= meter_id
;
5135 config
.flags
= meter
->flags
;
5136 config
.n_bands
= meter
->n_bands
;
5137 config
.bands
= meter
->bands
;
5138 ofputil_append_meter_config(&replies
, &config
);
5142 ofconn_send_replies(ofconn
, &replies
);
5143 ofpbuf_uninit(&bands
);
5148 ofproto_group_lookup(const struct ofproto
*ofproto
, uint32_t group_id
,
5149 struct ofgroup
**group
)
5150 OVS_TRY_RDLOCK(true, (*group
)->rwlock
)
5152 ovs_rwlock_rdlock(&ofproto
->groups_rwlock
);
5153 HMAP_FOR_EACH_IN_BUCKET (*group
, hmap_node
,
5154 hash_int(group_id
, 0), &ofproto
->groups
) {
5155 if ((*group
)->group_id
== group_id
) {
5156 ovs_rwlock_rdlock(&(*group
)->rwlock
);
5157 ovs_rwlock_unlock(&ofproto
->groups_rwlock
);
5161 ovs_rwlock_unlock(&ofproto
->groups_rwlock
);
5166 ofproto_group_release(struct ofgroup
*group
)
5167 OVS_RELEASES(group
->rwlock
)
5169 ovs_rwlock_unlock(&group
->rwlock
);
5173 ofproto_group_write_lookup(const struct ofproto
*ofproto
, uint32_t group_id
,
5174 struct ofgroup
**group
)
5175 OVS_TRY_WRLOCK(true, ofproto
->groups_rwlock
)
5176 OVS_TRY_WRLOCK(true, (*group
)->rwlock
)
5178 ovs_rwlock_wrlock(&ofproto
->groups_rwlock
);
5179 HMAP_FOR_EACH_IN_BUCKET (*group
, hmap_node
,
5180 hash_int(group_id
, 0), &ofproto
->groups
) {
5181 if ((*group
)->group_id
== group_id
) {
5182 ovs_rwlock_wrlock(&(*group
)->rwlock
);
5186 ovs_rwlock_unlock(&ofproto
->groups_rwlock
);
5191 ofproto_group_exists(const struct ofproto
*ofproto
, uint32_t group_id
)
5192 OVS_REQ_RDLOCK(ofproto
->groups_rwlock
)
5194 struct ofgroup
*grp
;
5196 HMAP_FOR_EACH_IN_BUCKET (grp
, hmap_node
,
5197 hash_int(group_id
, 0), &ofproto
->groups
) {
5198 if (grp
->group_id
== group_id
) {
5206 append_group_stats(struct ofgroup
*group
, struct list
*replies
)
5207 OVS_REQ_RDLOCK(group
->rwlock
)
5209 struct ofputil_group_stats ogs
;
5210 struct ofproto
*ofproto
= group
->ofproto
;
5211 long long int now
= time_msec();
5214 ogs
.bucket_stats
= xmalloc(group
->n_buckets
* sizeof *ogs
.bucket_stats
);
5216 error
= (ofproto
->ofproto_class
->group_get_stats
5217 ? ofproto
->ofproto_class
->group_get_stats(group
, &ogs
)
5220 ogs
.ref_count
= UINT32_MAX
;
5221 ogs
.packet_count
= UINT64_MAX
;
5222 ogs
.byte_count
= UINT64_MAX
;
5223 ogs
.n_buckets
= group
->n_buckets
;
5224 memset(ogs
.bucket_stats
, 0xff,
5225 ogs
.n_buckets
* sizeof *ogs
.bucket_stats
);
5228 ogs
.group_id
= group
->group_id
;
5229 calc_duration(group
->created
, now
, &ogs
.duration_sec
, &ogs
.duration_nsec
);
5231 ofputil_append_group_stats(replies
, &ogs
);
5233 free(ogs
.bucket_stats
);
5237 handle_group_stats_request(struct ofconn
*ofconn
,
5238 const struct ofp_header
*request
)
5240 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
5241 struct list replies
;
5243 struct ofgroup
*group
;
5246 error
= ofputil_decode_group_stats_request(request
, &group_id
);
5251 ofpmp_init(&replies
, request
);
5253 if (group_id
== OFPG_ALL
) {
5254 ovs_rwlock_rdlock(&ofproto
->groups_rwlock
);
5255 HMAP_FOR_EACH (group
, hmap_node
, &ofproto
->groups
) {
5256 ovs_rwlock_rdlock(&group
->rwlock
);
5257 append_group_stats(group
, &replies
);
5258 ovs_rwlock_unlock(&group
->rwlock
);
5260 ovs_rwlock_unlock(&ofproto
->groups_rwlock
);
5262 if (ofproto_group_lookup(ofproto
, group_id
, &group
)) {
5263 append_group_stats(group
, &replies
);
5264 ofproto_group_release(group
);
5268 ofconn_send_replies(ofconn
, &replies
);
5274 handle_group_desc_stats_request(struct ofconn
*ofconn
,
5275 const struct ofp_header
*request
)
5277 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
5278 struct list replies
;
5279 struct ofputil_group_desc gds
;
5280 struct ofgroup
*group
;
5282 ofpmp_init(&replies
, request
);
5284 ovs_rwlock_rdlock(&ofproto
->groups_rwlock
);
5285 HMAP_FOR_EACH (group
, hmap_node
, &ofproto
->groups
) {
5286 gds
.group_id
= group
->group_id
;
5287 gds
.type
= group
->type
;
5288 ofputil_append_group_desc_reply(&gds
, &group
->buckets
, &replies
);
5290 ovs_rwlock_unlock(&ofproto
->groups_rwlock
);
5292 ofconn_send_replies(ofconn
, &replies
);
5298 handle_group_features_stats_request(struct ofconn
*ofconn
,
5299 const struct ofp_header
*request
)
5301 struct ofproto
*p
= ofconn_get_ofproto(ofconn
);
5304 msg
= ofputil_encode_group_features_reply(&p
->ogf
, request
);
5306 ofconn_send_reply(ofconn
, msg
);
5312 /* Implements OFPGC11_ADD
5313 * in which no matching flow already exists in the flow table.
5315 * Adds the flow specified by 'ofm', which is followed by 'n_actions'
5316 * ofp_actions, to the ofproto's flow table. Returns 0 on success, an OpenFlow
5317 * error code on failure, or OFPROTO_POSTPONE if the operation cannot be
5318 * initiated now but may be retried later.
5320 * Upon successful return, takes ownership of 'fm->ofpacts'. On failure,
5321 * ownership remains with the caller.
5323 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
5326 add_group(struct ofproto
*ofproto
, struct ofputil_group_mod
*gm
)
5328 struct ofgroup
*ofgroup
;
5331 if (gm
->group_id
> OFPG_MAX
) {
5332 return OFPERR_OFPGMFC_INVALID_GROUP
;
5334 if (gm
->type
> OFPGT11_FF
) {
5335 return OFPERR_OFPGMFC_BAD_TYPE
;
5338 /* Allocate new group and initialize it. */
5339 ofgroup
= ofproto
->ofproto_class
->group_alloc();
5341 VLOG_WARN_RL(&rl
, "%s: failed to create group", ofproto
->name
);
5342 return OFPERR_OFPGMFC_OUT_OF_GROUPS
;
5345 ovs_rwlock_init(&ofgroup
->rwlock
);
5346 ofgroup
->ofproto
= ofproto
;
5347 ofgroup
->group_id
= gm
->group_id
;
5348 ofgroup
->type
= gm
->type
;
5349 ofgroup
->created
= ofgroup
->modified
= time_msec();
5351 list_move(&ofgroup
->buckets
, &gm
->buckets
);
5352 ofgroup
->n_buckets
= list_size(&ofgroup
->buckets
);
5354 /* Construct called BEFORE any locks are held. */
5355 error
= ofproto
->ofproto_class
->group_construct(ofgroup
);
5360 /* We wrlock as late as possible to minimize the time we jam any other
5361 * threads: No visible state changes before acquiring the lock. */
5362 ovs_rwlock_wrlock(&ofproto
->groups_rwlock
);
5364 if (ofproto
->n_groups
[gm
->type
] >= ofproto
->ogf
.max_groups
[gm
->type
]) {
5365 error
= OFPERR_OFPGMFC_OUT_OF_GROUPS
;
5369 if (ofproto_group_exists(ofproto
, gm
->group_id
)) {
5370 error
= OFPERR_OFPGMFC_GROUP_EXISTS
;
5375 /* Insert new group. */
5376 hmap_insert(&ofproto
->groups
, &ofgroup
->hmap_node
,
5377 hash_int(ofgroup
->group_id
, 0));
5378 ofproto
->n_groups
[ofgroup
->type
]++;
5380 ovs_rwlock_unlock(&ofproto
->groups_rwlock
);
5385 ovs_rwlock_unlock(&ofproto
->groups_rwlock
);
5386 ofproto
->ofproto_class
->group_destruct(ofgroup
);
5388 ofputil_bucket_list_destroy(&ofgroup
->buckets
);
5389 ofproto
->ofproto_class
->group_dealloc(ofgroup
);
5394 /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code on
5397 * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
5400 modify_group(struct ofproto
*ofproto
, struct ofputil_group_mod
*gm
)
5402 struct ofgroup
*ofgroup
;
5403 struct ofgroup
*victim
;
5406 if (gm
->group_id
> OFPG_MAX
) {
5407 return OFPERR_OFPGMFC_INVALID_GROUP
;
5410 if (gm
->type
> OFPGT11_FF
) {
5411 return OFPERR_OFPGMFC_BAD_TYPE
;
5414 victim
= ofproto
->ofproto_class
->group_alloc();
5416 VLOG_WARN_RL(&rl
, "%s: failed to allocate group", ofproto
->name
);
5417 return OFPERR_OFPGMFC_OUT_OF_GROUPS
;
5420 if (!ofproto_group_write_lookup(ofproto
, gm
->group_id
, &ofgroup
)) {
5421 error
= OFPERR_OFPGMFC_UNKNOWN_GROUP
;
5424 /* Both group's and its container's write locks held now.
5425 * Also, n_groups[] is protected by ofproto->groups_rwlock. */
5426 if (ofgroup
->type
!= gm
->type
5427 && ofproto
->n_groups
[gm
->type
] >= ofproto
->ogf
.max_groups
[gm
->type
]) {
5428 error
= OFPERR_OFPGMFC_OUT_OF_GROUPS
;
5433 list_move(&victim
->buckets
, &ofgroup
->buckets
);
5435 ofgroup
->type
= gm
->type
;
5436 list_move(&ofgroup
->buckets
, &gm
->buckets
);
5437 ofgroup
->n_buckets
= list_size(&ofgroup
->buckets
);
5439 error
= ofproto
->ofproto_class
->group_modify(ofgroup
, victim
);
5441 ofputil_bucket_list_destroy(&victim
->buckets
);
5442 ofproto
->n_groups
[victim
->type
]--;
5443 ofproto
->n_groups
[ofgroup
->type
]++;
5444 ofgroup
->modified
= time_msec();
5446 ofputil_bucket_list_destroy(&ofgroup
->buckets
);
5449 list_move(&ofgroup
->buckets
, &victim
->buckets
);
5453 ovs_rwlock_unlock(&ofgroup
->rwlock
);
5454 ovs_rwlock_unlock(&ofproto
->groups_rwlock
);
5456 ofproto
->ofproto_class
->group_dealloc(victim
);
5461 delete_group__(struct ofproto
*ofproto
, struct ofgroup
*ofgroup
)
5462 OVS_RELEASES(ofproto
->groups_rwlock
)
5464 /* Must wait until existing readers are done,
5465 * while holding the container's write lock at the same time. */
5466 ovs_rwlock_wrlock(&ofgroup
->rwlock
);
5467 hmap_remove(&ofproto
->groups
, &ofgroup
->hmap_node
);
5468 /* No-one can find this group any more. */
5469 ofproto
->n_groups
[ofgroup
->type
]--;
5470 ovs_rwlock_unlock(&ofproto
->groups_rwlock
);
5472 ofproto
->ofproto_class
->group_destruct(ofgroup
);
5473 ofputil_bucket_list_destroy(&ofgroup
->buckets
);
5474 ovs_rwlock_unlock(&ofgroup
->rwlock
);
5475 ovs_rwlock_destroy(&ofgroup
->rwlock
);
5476 ofproto
->ofproto_class
->group_dealloc(ofgroup
);
5479 /* Implements OFPGC_DELETE. */
5481 delete_group(struct ofproto
*ofproto
, uint32_t group_id
)
5483 struct ofgroup
*ofgroup
;
5485 ovs_rwlock_wrlock(&ofproto
->groups_rwlock
);
5486 if (group_id
== OFPG_ALL
) {
5488 struct hmap_node
*node
= hmap_first(&ofproto
->groups
);
5492 ofgroup
= CONTAINER_OF(node
, struct ofgroup
, hmap_node
);
5493 delete_group__(ofproto
, ofgroup
);
5494 /* Lock for each node separately, so that we will not jam the
5495 * other threads for too long time. */
5496 ovs_rwlock_wrlock(&ofproto
->groups_rwlock
);
5499 HMAP_FOR_EACH_IN_BUCKET (ofgroup
, hmap_node
,
5500 hash_int(group_id
, 0), &ofproto
->groups
) {
5501 if (ofgroup
->group_id
== group_id
) {
5502 delete_group__(ofproto
, ofgroup
);
5507 ovs_rwlock_unlock(&ofproto
->groups_rwlock
);
5511 handle_group_mod(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
5513 struct ofproto
*ofproto
= ofconn_get_ofproto(ofconn
);
5514 struct ofputil_group_mod gm
;
5517 error
= reject_slave_controller(ofconn
);
5522 error
= ofputil_decode_group_mod(oh
, &gm
);
5527 switch (gm
.command
) {
5529 return add_group(ofproto
, &gm
);
5531 case OFPGC11_MODIFY
:
5532 return modify_group(ofproto
, &gm
);
5534 case OFPGC11_DELETE
:
5535 delete_group(ofproto
, gm
.group_id
);
5539 if (gm
.command
> OFPGC11_DELETE
) {
5540 VLOG_WARN_RL(&rl
, "%s: Invalid group_mod command type %d",
5541 ofproto
->name
, gm
.command
);
5543 return OFPERR_OFPGMFC_BAD_COMMAND
;
5548 handle_table_mod(struct ofconn
*ofconn
, const struct ofp_header
*oh
)
5550 struct ofputil_table_mod tm
;
5553 error
= reject_slave_controller(ofconn
);
5558 error
= ofputil_decode_table_mod(oh
, &tm
);
5563 /* XXX Actual table mod support is not implemented yet. */
5568 handle_openflow__(struct ofconn
*ofconn
, const struct ofpbuf
*msg
)
5569 OVS_EXCLUDED(ofproto_mutex
)
5571 const struct ofp_header
*oh
= msg
->data
;
5575 error
= ofptype_decode(&type
, oh
);
5581 /* OpenFlow requests. */
5582 case OFPTYPE_ECHO_REQUEST
:
5583 return handle_echo_request(ofconn
, oh
);
5585 case OFPTYPE_FEATURES_REQUEST
:
5586 return handle_features_request(ofconn
, oh
);
5588 case OFPTYPE_GET_CONFIG_REQUEST
:
5589 return handle_get_config_request(ofconn
, oh
);
5591 case OFPTYPE_SET_CONFIG
:
5592 return handle_set_config(ofconn
, oh
);
5594 case OFPTYPE_PACKET_OUT
:
5595 return handle_packet_out(ofconn
, oh
);
5597 case OFPTYPE_PORT_MOD
:
5598 return handle_port_mod(ofconn
, oh
);
5600 case OFPTYPE_FLOW_MOD
:
5601 return handle_flow_mod(ofconn
, oh
);
5603 case OFPTYPE_GROUP_MOD
:
5604 return handle_group_mod(ofconn
, oh
);
5606 case OFPTYPE_TABLE_MOD
:
5607 return handle_table_mod(ofconn
, oh
);
5609 case OFPTYPE_METER_MOD
:
5610 return handle_meter_mod(ofconn
, oh
);
5612 case OFPTYPE_BARRIER_REQUEST
:
5613 return handle_barrier_request(ofconn
, oh
);
5615 case OFPTYPE_ROLE_REQUEST
:
5616 return handle_role_request(ofconn
, oh
);
5618 /* OpenFlow replies. */
5619 case OFPTYPE_ECHO_REPLY
:
5622 /* Nicira extension requests. */
5623 case OFPTYPE_FLOW_MOD_TABLE_ID
:
5624 return handle_nxt_flow_mod_table_id(ofconn
, oh
);
5626 case OFPTYPE_SET_FLOW_FORMAT
:
5627 return handle_nxt_set_flow_format(ofconn
, oh
);
5629 case OFPTYPE_SET_PACKET_IN_FORMAT
:
5630 return handle_nxt_set_packet_in_format(ofconn
, oh
);
5632 case OFPTYPE_SET_CONTROLLER_ID
:
5633 return handle_nxt_set_controller_id(ofconn
, oh
);
5635 case OFPTYPE_FLOW_AGE
:
5636 /* Nothing to do. */
5639 case OFPTYPE_FLOW_MONITOR_CANCEL
:
5640 return handle_flow_monitor_cancel(ofconn
, oh
);
5642 case OFPTYPE_SET_ASYNC_CONFIG
:
5643 return handle_nxt_set_async_config(ofconn
, oh
);
5645 case OFPTYPE_GET_ASYNC_REQUEST
:
5646 return handle_nxt_get_async_request(ofconn
, oh
);
5648 /* Statistics requests. */
5649 case OFPTYPE_DESC_STATS_REQUEST
:
5650 return handle_desc_stats_request(ofconn
, oh
);
5652 case OFPTYPE_FLOW_STATS_REQUEST
:
5653 return handle_flow_stats_request(ofconn
, oh
);
5655 case OFPTYPE_AGGREGATE_STATS_REQUEST
:
5656 return handle_aggregate_stats_request(ofconn
, oh
);
5658 case OFPTYPE_TABLE_STATS_REQUEST
:
5659 return handle_table_stats_request(ofconn
, oh
);
5661 case OFPTYPE_PORT_STATS_REQUEST
:
5662 return handle_port_stats_request(ofconn
, oh
);
5664 case OFPTYPE_QUEUE_STATS_REQUEST
:
5665 return handle_queue_stats_request(ofconn
, oh
);
5667 case OFPTYPE_PORT_DESC_STATS_REQUEST
:
5668 return handle_port_desc_stats_request(ofconn
, oh
);
5670 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST
:
5671 return handle_flow_monitor_request(ofconn
, oh
);
5673 case OFPTYPE_METER_STATS_REQUEST
:
5674 case OFPTYPE_METER_CONFIG_STATS_REQUEST
:
5675 return handle_meter_request(ofconn
, oh
, type
);
5677 case OFPTYPE_METER_FEATURES_STATS_REQUEST
:
5678 return handle_meter_features_request(ofconn
, oh
);
5680 case OFPTYPE_GROUP_STATS_REQUEST
:
5681 return handle_group_stats_request(ofconn
, oh
);
5683 case OFPTYPE_GROUP_DESC_STATS_REQUEST
:
5684 return handle_group_desc_stats_request(ofconn
, oh
);
5686 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST
:
5687 return handle_group_features_stats_request(ofconn
, oh
);
5689 /* FIXME: Change the following once they are implemented: */
5690 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST
:
5691 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST
:
5692 return OFPERR_OFPBRC_BAD_TYPE
;
5696 case OFPTYPE_FEATURES_REPLY
:
5697 case OFPTYPE_GET_CONFIG_REPLY
:
5698 case OFPTYPE_PACKET_IN
:
5699 case OFPTYPE_FLOW_REMOVED
:
5700 case OFPTYPE_PORT_STATUS
:
5701 case OFPTYPE_BARRIER_REPLY
:
5702 case OFPTYPE_QUEUE_GET_CONFIG_REPLY
:
5703 case OFPTYPE_DESC_STATS_REPLY
:
5704 case OFPTYPE_FLOW_STATS_REPLY
:
5705 case OFPTYPE_QUEUE_STATS_REPLY
:
5706 case OFPTYPE_PORT_STATS_REPLY
:
5707 case OFPTYPE_TABLE_STATS_REPLY
:
5708 case OFPTYPE_AGGREGATE_STATS_REPLY
:
5709 case OFPTYPE_PORT_DESC_STATS_REPLY
:
5710 case OFPTYPE_ROLE_REPLY
:
5711 case OFPTYPE_FLOW_MONITOR_PAUSED
:
5712 case OFPTYPE_FLOW_MONITOR_RESUMED
:
5713 case OFPTYPE_FLOW_MONITOR_STATS_REPLY
:
5714 case OFPTYPE_GET_ASYNC_REPLY
:
5715 case OFPTYPE_GROUP_STATS_REPLY
:
5716 case OFPTYPE_GROUP_DESC_STATS_REPLY
:
5717 case OFPTYPE_GROUP_FEATURES_STATS_REPLY
:
5718 case OFPTYPE_METER_STATS_REPLY
:
5719 case OFPTYPE_METER_CONFIG_STATS_REPLY
:
5720 case OFPTYPE_METER_FEATURES_STATS_REPLY
:
5721 case OFPTYPE_TABLE_FEATURES_STATS_REPLY
:
5723 return OFPERR_OFPBRC_BAD_TYPE
;
5728 handle_openflow(struct ofconn
*ofconn
, const struct ofpbuf
*ofp_msg
)
5729 OVS_EXCLUDED(ofproto_mutex
)
5731 int error
= handle_openflow__(ofconn
, ofp_msg
);
5732 if (error
&& error
!= OFPROTO_POSTPONE
) {
5733 ofconn_send_error(ofconn
, ofp_msg
->data
, error
);
5735 COVERAGE_INC(ofproto_recv_openflow
);
5736 return error
!= OFPROTO_POSTPONE
;
5739 /* Asynchronous operations. */
5741 /* Creates and returns a new ofopgroup that is not associated with any
5742 * OpenFlow connection.
5744 * The caller should add operations to the returned group with
5745 * ofoperation_create() and then submit it with ofopgroup_submit(). */
5746 static struct ofopgroup
*
5747 ofopgroup_create_unattached(struct ofproto
*ofproto
)
5748 OVS_REQUIRES(ofproto_mutex
)
5750 struct ofopgroup
*group
= xzalloc(sizeof *group
);
5751 group
->ofproto
= ofproto
;
5752 list_init(&group
->ofproto_node
);
5753 list_init(&group
->ops
);
5754 list_init(&group
->ofconn_node
);
5758 /* Creates and returns a new ofopgroup for 'ofproto'.
5760 * If 'ofconn' is NULL, the new ofopgroup is not associated with any OpenFlow
5761 * connection. The 'request' and 'buffer_id' arguments are ignored.
5763 * If 'ofconn' is nonnull, then the new ofopgroup is associated with 'ofconn'.
5764 * If the ofopgroup eventually fails, then the error reply will include
5765 * 'request'. If the ofopgroup eventually succeeds, then the packet with
5766 * buffer id 'buffer_id' on 'ofconn' will be sent by 'ofconn''s ofproto.
5768 * The caller should add operations to the returned group with
5769 * ofoperation_create() and then submit it with ofopgroup_submit(). */
5770 static struct ofopgroup
*
5771 ofopgroup_create(struct ofproto
*ofproto
, struct ofconn
*ofconn
,
5772 const struct ofp_header
*request
, uint32_t buffer_id
)
5773 OVS_REQUIRES(ofproto_mutex
)
5775 struct ofopgroup
*group
= ofopgroup_create_unattached(ofproto
);
5777 size_t request_len
= ntohs(request
->length
);
5779 ovs_assert(ofconn_get_ofproto(ofconn
) == ofproto
);
5781 ofconn_add_opgroup(ofconn
, &group
->ofconn_node
);
5782 group
->ofconn
= ofconn
;
5783 group
->request
= xmemdup(request
, MIN(request_len
, 64));
5784 group
->buffer_id
= buffer_id
;
5789 /* Submits 'group' for processing.
5791 * If 'group' contains no operations (e.g. none were ever added, or all of the
5792 * ones that were added completed synchronously), then it is destroyed
5793 * immediately. Otherwise it is added to the ofproto's list of pending
5796 ofopgroup_submit(struct ofopgroup
*group
)
5797 OVS_REQUIRES(ofproto_mutex
)
5799 if (!group
->n_running
) {
5800 ofopgroup_complete(group
);
5802 list_push_back(&group
->ofproto
->pending
, &group
->ofproto_node
);
5803 group
->ofproto
->n_pending
++;
5808 ofopgroup_complete(struct ofopgroup
*group
)
5809 OVS_REQUIRES(ofproto_mutex
)
5811 struct ofproto
*ofproto
= group
->ofproto
;
5813 struct ofconn
*abbrev_ofconn
;
5814 ovs_be32 abbrev_xid
;
5816 struct ofoperation
*op
, *next_op
;
5819 ovs_assert(!group
->n_running
);
5822 LIST_FOR_EACH (op
, group_node
, &group
->ops
) {
5829 if (!error
&& group
->ofconn
&& group
->buffer_id
!= UINT32_MAX
) {
5830 LIST_FOR_EACH (op
, group_node
, &group
->ops
) {
5831 if (op
->type
!= OFOPERATION_DELETE
) {
5832 struct ofpbuf
*packet
;
5835 error
= ofconn_pktbuf_retrieve(group
->ofconn
, group
->buffer_id
,
5838 struct rule_execute
*re
;
5842 ofproto_rule_ref(op
->rule
);
5844 re
= xmalloc(sizeof *re
);
5845 re
->rule
= op
->rule
;
5846 re
->in_port
= in_port
;
5847 re
->packet
= packet
;
5849 if (!guarded_list_push_back(&ofproto
->rule_executes
,
5850 &re
->list_node
, 1024)) {
5851 ofproto_rule_unref(op
->rule
);
5852 ofpbuf_delete(re
->packet
);
5861 if (!error
&& !list_is_empty(&group
->ofconn_node
)) {
5862 abbrev_ofconn
= group
->ofconn
;
5863 abbrev_xid
= group
->request
->xid
;
5865 abbrev_ofconn
= NULL
;
5866 abbrev_xid
= htonl(0);
5868 LIST_FOR_EACH_SAFE (op
, next_op
, group_node
, &group
->ops
) {
5869 struct rule
*rule
= op
->rule
;
5871 /* We generally want to report the change to active OpenFlow flow
5872 monitors (e.g. NXST_FLOW_MONITOR). There are three exceptions:
5874 - The operation failed.
5876 - The affected rule is not visible to controllers.
5878 - The operation's only effect was to update rule->modified. */
5880 || ofproto_rule_is_hidden(rule
)
5881 || (op
->type
== OFOPERATION_MODIFY
5883 && rule
->flow_cookie
== op
->flow_cookie
))) {
5884 /* Check that we can just cast from ofoperation_type to
5885 * nx_flow_update_event. */
5886 enum nx_flow_update_event event_type
;
5889 case OFOPERATION_ADD
:
5890 case OFOPERATION_REPLACE
:
5891 event_type
= NXFME_ADDED
;
5894 case OFOPERATION_DELETE
:
5895 event_type
= NXFME_DELETED
;
5898 case OFOPERATION_MODIFY
:
5899 event_type
= NXFME_MODIFIED
;
5906 ofmonitor_report(ofproto
->connmgr
, rule
, event_type
,
5907 op
->reason
, abbrev_ofconn
, abbrev_xid
);
5910 rule
->pending
= NULL
;
5913 case OFOPERATION_ADD
:
5917 vid_mask
= minimask_get_vid_mask(&rule
->cr
.match
.mask
);
5918 if (vid_mask
== VLAN_VID_MASK
) {
5919 if (ofproto
->vlan_bitmap
) {
5920 uint16_t vid
= miniflow_get_vid(&rule
->cr
.match
.flow
);
5921 if (!bitmap_is_set(ofproto
->vlan_bitmap
, vid
)) {
5922 bitmap_set1(ofproto
->vlan_bitmap
, vid
);
5923 ofproto
->vlans_changed
= true;
5926 ofproto
->vlans_changed
= true;
5930 oftable_remove_rule(rule
);
5931 ofproto_rule_unref(rule
);
5935 case OFOPERATION_DELETE
:
5936 ovs_assert(!op
->error
);
5937 ofproto_rule_unref(rule
);
5941 case OFOPERATION_MODIFY
:
5942 case OFOPERATION_REPLACE
:
5944 long long int now
= time_msec();
5946 rule
->modified
= now
;
5947 if (op
->type
== OFOPERATION_REPLACE
) {
5948 rule
->created
= rule
->used
= now
;
5951 ofproto_rule_change_cookie(ofproto
, rule
, op
->flow_cookie
);
5952 ovs_mutex_lock(&rule
->mutex
);
5953 rule
->idle_timeout
= op
->idle_timeout
;
5954 rule
->hard_timeout
= op
->hard_timeout
;
5955 ovs_mutex_unlock(&rule
->mutex
);
5957 struct rule_actions
*old_actions
;
5959 ovs_mutex_lock(&rule
->mutex
);
5960 old_actions
= rule
->actions
;
5961 rule
->actions
= op
->actions
;
5962 ovs_mutex_unlock(&rule
->mutex
);
5965 rule_actions_unref(old_actions
);
5967 rule
->flags
= op
->flags
;
5975 ofoperation_destroy(op
);
5978 ofmonitor_flush(ofproto
->connmgr
);
5980 if (!list_is_empty(&group
->ofproto_node
)) {
5981 ovs_assert(ofproto
->n_pending
> 0);
5982 ofproto
->n_pending
--;
5983 list_remove(&group
->ofproto_node
);
5985 if (!list_is_empty(&group
->ofconn_node
)) {
5986 list_remove(&group
->ofconn_node
);
5988 ofconn_send_error(group
->ofconn
, group
->request
, error
);
5990 connmgr_retry(ofproto
->connmgr
);
5992 free(group
->request
);
5996 /* Initiates a new operation on 'rule', of the specified 'type', within
5997 * 'group'. Prior to calling, 'rule' must not have any pending operation.
5999 * For a 'type' of OFOPERATION_DELETE, 'reason' should specify the reason that
6000 * the flow is being deleted. For other 'type's, 'reason' is ignored (use 0).
6002 * Returns the newly created ofoperation (which is also available as
6003 * rule->pending). */
6004 static struct ofoperation
*
6005 ofoperation_create(struct ofopgroup
*group
, struct rule
*rule
,
6006 enum ofoperation_type type
,
6007 enum ofp_flow_removed_reason reason
)
6008 OVS_REQUIRES(ofproto_mutex
)
6010 struct ofproto
*ofproto
= group
->ofproto
;
6011 struct ofoperation
*op
;
6013 ovs_assert(!rule
->pending
);
6015 op
= rule
->pending
= xzalloc(sizeof *op
);
6017 list_push_back(&group
->ops
, &op
->group_node
);
6020 op
->reason
= reason
;
6021 op
->flow_cookie
= rule
->flow_cookie
;
6022 ovs_mutex_lock(&rule
->mutex
);
6023 op
->idle_timeout
= rule
->idle_timeout
;
6024 op
->hard_timeout
= rule
->hard_timeout
;
6025 ovs_mutex_unlock(&rule
->mutex
);
6026 op
->flags
= rule
->flags
;
6030 if (type
== OFOPERATION_DELETE
) {
6031 hmap_insert(&ofproto
->deletions
, &op
->hmap_node
,
6032 cls_rule_hash(&rule
->cr
, rule
->table_id
));
6039 ofoperation_destroy(struct ofoperation
*op
)
6040 OVS_REQUIRES(ofproto_mutex
)
6042 struct ofopgroup
*group
= op
->group
;
6045 op
->rule
->pending
= NULL
;
6047 if (op
->type
== OFOPERATION_DELETE
) {
6048 hmap_remove(&group
->ofproto
->deletions
, &op
->hmap_node
);
6050 list_remove(&op
->group_node
);
6051 rule_actions_unref(op
->actions
);
6055 /* Indicates that 'op' completed with status 'error', which is either 0 to
6056 * indicate success or an OpenFlow error code on failure.
6058 * If 'error' is 0, indicating success, the operation will be committed
6059 * permanently to the flow table.
6061 * If 'error' is nonzero, then generally the operation will be rolled back:
6063 * - If 'op' is an "add flow" operation, ofproto removes the new rule or
6064 * restores the original rule. The caller must have uninitialized any
6065 * derived state in the new rule, as in step 5 of in the "Life Cycle" in
6066 * ofproto/ofproto-provider.h. ofoperation_complete() performs steps 6 and
6067 * and 7 for the new rule, calling its ->rule_dealloc() function.
6069 * - If 'op' is a "modify flow" operation, ofproto restores the original
6072 * - 'op' must not be a "delete flow" operation. Removing a rule is not
6073 * allowed to fail. It must always succeed.
6075 * Please see the large comment in ofproto/ofproto-provider.h titled
6076 * "Asynchronous Operation Support" for more information. */
6078 ofoperation_complete(struct ofoperation
*op
, enum ofperr error
)
6080 struct ofopgroup
*group
= op
->group
;
6082 ovs_assert(group
->n_running
> 0);
6083 ovs_assert(!error
|| op
->type
!= OFOPERATION_DELETE
);
6086 if (!--group
->n_running
&& !list_is_empty(&group
->ofproto_node
)) {
6087 /* This function can be called from ->rule_construct(), in which case
6088 * ofproto_mutex is held, or it can be called from ->run(), in which
6089 * case ofproto_mutex is not held. But only in the latter case can we
6090 * arrive here, so we can safely take ofproto_mutex now. */
6091 ovs_mutex_lock(&ofproto_mutex
);
6092 ovs_assert(op
->rule
->pending
== op
);
6093 ofopgroup_complete(group
);
6094 ovs_mutex_unlock(&ofproto_mutex
);
6099 pick_datapath_id(const struct ofproto
*ofproto
)
6101 const struct ofport
*port
;
6103 port
= ofproto_get_port(ofproto
, OFPP_LOCAL
);
6105 uint8_t ea
[ETH_ADDR_LEN
];
6108 error
= netdev_get_etheraddr(port
->netdev
, ea
);
6110 return eth_addr_to_uint64(ea
);
6112 VLOG_WARN("%s: could not get MAC address for %s (%s)",
6113 ofproto
->name
, netdev_get_name(port
->netdev
),
6114 ovs_strerror(error
));
6116 return ofproto
->fallback_dpid
;
6120 pick_fallback_dpid(void)
6122 uint8_t ea
[ETH_ADDR_LEN
];
6123 eth_addr_nicira_random(ea
);
6124 return eth_addr_to_uint64(ea
);
6127 /* Table overflow policy. */
6129 /* Chooses and updates 'rulep' with a rule to evict from 'table'. Sets 'rulep'
6130 * to NULL if the table is not configured to evict rules or if the table
6131 * contains no evictable rules. (Rules with a readlock on their evict rwlock,
6132 * or with no timeouts are not evictable.) */
6134 choose_rule_to_evict(struct oftable
*table
, struct rule
**rulep
)
6135 OVS_REQUIRES(ofproto_mutex
)
6137 struct eviction_group
*evg
;
6140 if (!table
->eviction_fields
) {
6144 /* In the common case, the outer and inner loops here will each be entered
6147 * - The inner loop normally "return"s in its first iteration. If the
6148 * eviction group has any evictable rules, then it always returns in
6151 * - The outer loop only iterates more than once if the largest eviction
6152 * group has no evictable rules.
6154 * - The outer loop can exit only if table's 'max_flows' is all filled up
6155 * by unevictable rules. */
6156 HEAP_FOR_EACH (evg
, size_node
, &table
->eviction_groups_by_size
) {
6159 HEAP_FOR_EACH (rule
, evg_node
, &evg
->rules
) {
6168 /* Searches 'ofproto' for tables that have more flows than their configured
6169 * maximum and that have flow eviction enabled, and evicts as many flows as
6170 * necessary and currently feasible from them.
6172 * This triggers only when an OpenFlow table has N flows in it and then the
6173 * client configures a maximum number of flows less than N. */
6175 ofproto_evict(struct ofproto
*ofproto
)
6177 struct oftable
*table
;
6179 ovs_mutex_lock(&ofproto_mutex
);
6180 OFPROTO_FOR_EACH_TABLE (table
, ofproto
) {
6181 evict_rules_from_table(ofproto
, table
, 0);
6183 ovs_mutex_unlock(&ofproto_mutex
);
6186 /* Eviction groups. */
6188 /* Returns the priority to use for an eviction_group that contains 'n_rules'
6189 * rules. The priority contains low-order random bits to ensure that eviction
6190 * groups with the same number of rules are prioritized randomly. */
6192 eviction_group_priority(size_t n_rules
)
6194 uint16_t size
= MIN(UINT16_MAX
, n_rules
);
6195 return (size
<< 16) | random_uint16();
6198 /* Updates 'evg', an eviction_group within 'table', following a change that
6199 * adds or removes rules in 'evg'. */
6201 eviction_group_resized(struct oftable
*table
, struct eviction_group
*evg
)
6202 OVS_REQUIRES(ofproto_mutex
)
6204 heap_change(&table
->eviction_groups_by_size
, &evg
->size_node
,
6205 eviction_group_priority(heap_count(&evg
->rules
)));
6208 /* Destroys 'evg', an eviction_group within 'table':
6210 * - Removes all the rules, if any, from 'evg'. (It doesn't destroy the
6211 * rules themselves, just removes them from the eviction group.)
6213 * - Removes 'evg' from 'table'.
6217 eviction_group_destroy(struct oftable
*table
, struct eviction_group
*evg
)
6218 OVS_REQUIRES(ofproto_mutex
)
6220 while (!heap_is_empty(&evg
->rules
)) {
6223 rule
= CONTAINER_OF(heap_pop(&evg
->rules
), struct rule
, evg_node
);
6224 rule
->eviction_group
= NULL
;
6226 hmap_remove(&table
->eviction_groups_by_id
, &evg
->id_node
);
6227 heap_remove(&table
->eviction_groups_by_size
, &evg
->size_node
);
6228 heap_destroy(&evg
->rules
);
6232 /* Removes 'rule' from its eviction group, if any. */
6234 eviction_group_remove_rule(struct rule
*rule
)
6235 OVS_REQUIRES(ofproto_mutex
)
6237 if (rule
->eviction_group
) {
6238 struct oftable
*table
= &rule
->ofproto
->tables
[rule
->table_id
];
6239 struct eviction_group
*evg
= rule
->eviction_group
;
6241 rule
->eviction_group
= NULL
;
6242 heap_remove(&evg
->rules
, &rule
->evg_node
);
6243 if (heap_is_empty(&evg
->rules
)) {
6244 eviction_group_destroy(table
, evg
);
6246 eviction_group_resized(table
, evg
);
6251 /* Hashes the 'rule''s values for the eviction_fields of 'rule''s table, and
6252 * returns the hash value. */
6254 eviction_group_hash_rule(struct rule
*rule
)
6255 OVS_REQUIRES(ofproto_mutex
)
6257 struct oftable
*table
= &rule
->ofproto
->tables
[rule
->table_id
];
6258 const struct mf_subfield
*sf
;
6262 hash
= table
->eviction_group_id_basis
;
6263 miniflow_expand(&rule
->cr
.match
.flow
, &flow
);
6264 for (sf
= table
->eviction_fields
;
6265 sf
< &table
->eviction_fields
[table
->n_eviction_fields
];
6268 if (mf_are_prereqs_ok(sf
->field
, &flow
)) {
6269 union mf_value value
;
6271 mf_get_value(sf
->field
, &flow
, &value
);
6273 bitwise_zero(&value
, sf
->field
->n_bytes
, 0, sf
->ofs
);
6275 if (sf
->ofs
+ sf
->n_bits
< sf
->field
->n_bytes
* 8) {
6276 unsigned int start
= sf
->ofs
+ sf
->n_bits
;
6277 bitwise_zero(&value
, sf
->field
->n_bytes
, start
,
6278 sf
->field
->n_bytes
* 8 - start
);
6280 hash
= hash_bytes(&value
, sf
->field
->n_bytes
, hash
);
6282 hash
= hash_int(hash
, 0);
6289 /* Returns an eviction group within 'table' with the given 'id', creating one
6291 static struct eviction_group
*
6292 eviction_group_find(struct oftable
*table
, uint32_t id
)
6293 OVS_REQUIRES(ofproto_mutex
)
6295 struct eviction_group
*evg
;
6297 HMAP_FOR_EACH_WITH_HASH (evg
, id_node
, id
, &table
->eviction_groups_by_id
) {
6301 evg
= xmalloc(sizeof *evg
);
6302 hmap_insert(&table
->eviction_groups_by_id
, &evg
->id_node
, id
);
6303 heap_insert(&table
->eviction_groups_by_size
, &evg
->size_node
,
6304 eviction_group_priority(0));
6305 heap_init(&evg
->rules
);
6310 /* Returns an eviction priority for 'rule'. The return value should be
6311 * interpreted so that higher priorities make a rule more attractive candidates
6314 rule_eviction_priority(struct rule
*rule
)
6315 OVS_REQUIRES(ofproto_mutex
)
6317 long long int hard_expiration
;
6318 long long int idle_expiration
;
6319 long long int expiration
;
6320 uint32_t expiration_offset
;
6322 /* Calculate time of expiration. */
6323 ovs_mutex_lock(&rule
->mutex
);
6324 hard_expiration
= (rule
->hard_timeout
6325 ? rule
->modified
+ rule
->hard_timeout
* 1000
6327 idle_expiration
= (rule
->idle_timeout
6328 ? rule
->used
+ rule
->idle_timeout
* 1000
6330 expiration
= MIN(hard_expiration
, idle_expiration
);
6331 ovs_mutex_unlock(&rule
->mutex
);
6332 if (expiration
== LLONG_MAX
) {
6336 /* Calculate the time of expiration as a number of (approximate) seconds
6337 * after program startup.
6339 * This should work OK for program runs that last UINT32_MAX seconds or
6340 * less. Therefore, please restart OVS at least once every 136 years. */
6341 expiration_offset
= (expiration
>> 10) - (time_boot_msec() >> 10);
6343 /* Invert the expiration offset because we're using a max-heap. */
6344 return UINT32_MAX
- expiration_offset
;
6347 /* Adds 'rule' to an appropriate eviction group for its oftable's
6348 * configuration. Does nothing if 'rule''s oftable doesn't have eviction
6349 * enabled, or if 'rule' is a permanent rule (one that will never expire on its
6352 * The caller must ensure that 'rule' is not already in an eviction group. */
6354 eviction_group_add_rule(struct rule
*rule
)
6355 OVS_REQUIRES(ofproto_mutex
)
6357 struct ofproto
*ofproto
= rule
->ofproto
;
6358 struct oftable
*table
= &ofproto
->tables
[rule
->table_id
];
6361 ovs_mutex_lock(&rule
->mutex
);
6362 has_timeout
= rule
->hard_timeout
|| rule
->idle_timeout
;
6363 ovs_mutex_unlock(&rule
->mutex
);
6365 if (table
->eviction_fields
&& has_timeout
) {
6366 struct eviction_group
*evg
;
6368 evg
= eviction_group_find(table
, eviction_group_hash_rule(rule
));
6370 rule
->eviction_group
= evg
;
6371 heap_insert(&evg
->rules
, &rule
->evg_node
,
6372 rule_eviction_priority(rule
));
6373 eviction_group_resized(table
, evg
);
6379 /* Initializes 'table'. */
6381 oftable_init(struct oftable
*table
)
6383 memset(table
, 0, sizeof *table
);
6384 classifier_init(&table
->cls
);
6385 table
->max_flows
= UINT_MAX
;
6388 /* Destroys 'table', including its classifier and eviction groups.
6390 * The caller is responsible for freeing 'table' itself. */
6392 oftable_destroy(struct oftable
*table
)
6394 ovs_rwlock_rdlock(&table
->cls
.rwlock
);
6395 ovs_assert(classifier_is_empty(&table
->cls
));
6396 ovs_rwlock_unlock(&table
->cls
.rwlock
);
6397 oftable_disable_eviction(table
);
6398 classifier_destroy(&table
->cls
);
6402 /* Changes the name of 'table' to 'name'. If 'name' is NULL or the empty
6403 * string, then 'table' will use its default name.
6405 * This only affects the name exposed for a table exposed through the OpenFlow
6406 * OFPST_TABLE (as printed by "ovs-ofctl dump-tables"). */
6408 oftable_set_name(struct oftable
*table
, const char *name
)
6410 if (name
&& name
[0]) {
6411 int len
= strnlen(name
, OFP_MAX_TABLE_NAME_LEN
);
6412 if (!table
->name
|| strncmp(name
, table
->name
, len
)) {
6414 table
->name
= xmemdup0(name
, len
);
6422 /* oftables support a choice of two policies when adding a rule would cause the
6423 * number of flows in the table to exceed the configured maximum number: either
6424 * they can refuse to add the new flow or they can evict some existing flow.
6425 * This function configures the former policy on 'table'. */
6427 oftable_disable_eviction(struct oftable
*table
)
6428 OVS_REQUIRES(ofproto_mutex
)
6430 if (table
->eviction_fields
) {
6431 struct eviction_group
*evg
, *next
;
6433 HMAP_FOR_EACH_SAFE (evg
, next
, id_node
,
6434 &table
->eviction_groups_by_id
) {
6435 eviction_group_destroy(table
, evg
);
6437 hmap_destroy(&table
->eviction_groups_by_id
);
6438 heap_destroy(&table
->eviction_groups_by_size
);
6440 free(table
->eviction_fields
);
6441 table
->eviction_fields
= NULL
;
6442 table
->n_eviction_fields
= 0;
6446 /* oftables support a choice of two policies when adding a rule would cause the
6447 * number of flows in the table to exceed the configured maximum number: either
6448 * they can refuse to add the new flow or they can evict some existing flow.
6449 * This function configures the latter policy on 'table', with fairness based
6450 * on the values of the 'n_fields' fields specified in 'fields'. (Specifying
6451 * 'n_fields' as 0 disables fairness.) */
6453 oftable_enable_eviction(struct oftable
*table
,
6454 const struct mf_subfield
*fields
, size_t n_fields
)
6455 OVS_REQUIRES(ofproto_mutex
)
6457 struct cls_cursor cursor
;
6460 if (table
->eviction_fields
6461 && n_fields
== table
->n_eviction_fields
6463 || !memcmp(fields
, table
->eviction_fields
,
6464 n_fields
* sizeof *fields
))) {
6469 oftable_disable_eviction(table
);
6471 table
->n_eviction_fields
= n_fields
;
6472 table
->eviction_fields
= xmemdup(fields
, n_fields
* sizeof *fields
);
6474 table
->eviction_group_id_basis
= random_uint32();
6475 hmap_init(&table
->eviction_groups_by_id
);
6476 heap_init(&table
->eviction_groups_by_size
);
6478 ovs_rwlock_rdlock(&table
->cls
.rwlock
);
6479 cls_cursor_init(&cursor
, &table
->cls
, NULL
);
6480 CLS_CURSOR_FOR_EACH (rule
, cr
, &cursor
) {
6481 eviction_group_add_rule(rule
);
6483 ovs_rwlock_unlock(&table
->cls
.rwlock
);
6486 /* Removes 'rule' from the oftable that contains it. */
6488 oftable_remove_rule__(struct ofproto
*ofproto
, struct rule
*rule
)
6489 OVS_REQUIRES(ofproto_mutex
)
6491 struct classifier
*cls
= &ofproto
->tables
[rule
->table_id
].cls
;
6493 ovs_rwlock_wrlock(&cls
->rwlock
);
6494 classifier_remove(cls
, CONST_CAST(struct cls_rule
*, &rule
->cr
));
6495 ovs_rwlock_unlock(&cls
->rwlock
);
6497 cookies_remove(ofproto
, rule
);
6499 eviction_group_remove_rule(rule
);
6500 if (!list_is_empty(&rule
->expirable
)) {
6501 list_remove(&rule
->expirable
);
6503 if (!list_is_empty(&rule
->meter_list_node
)) {
6504 list_remove(&rule
->meter_list_node
);
6505 list_init(&rule
->meter_list_node
);
6510 oftable_remove_rule(struct rule
*rule
)
6511 OVS_REQUIRES(ofproto_mutex
)
6513 oftable_remove_rule__(rule
->ofproto
, rule
);
6516 /* Inserts 'rule' into its oftable, which must not already contain any rule for
6517 * the same cls_rule. */
6519 oftable_insert_rule(struct rule
*rule
)
6520 OVS_REQUIRES(ofproto_mutex
)
6522 struct ofproto
*ofproto
= rule
->ofproto
;
6523 struct oftable
*table
= &ofproto
->tables
[rule
->table_id
];
6526 ovs_mutex_lock(&rule
->mutex
);
6527 may_expire
= rule
->hard_timeout
|| rule
->idle_timeout
;
6528 ovs_mutex_unlock(&rule
->mutex
);
6531 list_insert(&ofproto
->expirable
, &rule
->expirable
);
6534 cookies_insert(ofproto
, rule
);
6536 if (rule
->actions
->meter_id
) {
6537 struct meter
*meter
= ofproto
->meters
[rule
->actions
->meter_id
];
6538 list_insert(&meter
->rules
, &rule
->meter_list_node
);
6540 ovs_rwlock_wrlock(&table
->cls
.rwlock
);
6541 classifier_insert(&table
->cls
, CONST_CAST(struct cls_rule
*, &rule
->cr
));
6542 ovs_rwlock_unlock(&table
->cls
.rwlock
);
6543 eviction_group_add_rule(rule
);
6546 /* unixctl commands. */
6549 ofproto_lookup(const char *name
)
6551 struct ofproto
*ofproto
;
6553 HMAP_FOR_EACH_WITH_HASH (ofproto
, hmap_node
, hash_string(name
, 0),
6555 if (!strcmp(ofproto
->name
, name
)) {
6563 ofproto_unixctl_list(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
6564 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
6566 struct ofproto
*ofproto
;
6570 HMAP_FOR_EACH (ofproto
, hmap_node
, &all_ofprotos
) {
6571 ds_put_format(&results
, "%s\n", ofproto
->name
);
6573 unixctl_command_reply(conn
, ds_cstr(&results
));
6574 ds_destroy(&results
);
6578 ofproto_unixctl_init(void)
6580 static bool registered
;
6586 unixctl_command_register("ofproto/list", "", 0, 0,
6587 ofproto_unixctl_list
, NULL
);
6590 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
6592 * This is deprecated. It is only for compatibility with broken device drivers
6593 * in old versions of Linux that do not properly support VLANs when VLAN
6594 * devices are not used. When broken device drivers are no longer in
6595 * widespread use, we will delete these interfaces. */
6597 /* Sets a 1-bit in the 4096-bit 'vlan_bitmap' for each VLAN ID that is matched
6598 * (exactly) by an OpenFlow rule in 'ofproto'. */
6600 ofproto_get_vlan_usage(struct ofproto
*ofproto
, unsigned long int *vlan_bitmap
)
6602 const struct oftable
*oftable
;
6604 free(ofproto
->vlan_bitmap
);
6605 ofproto
->vlan_bitmap
= bitmap_allocate(4096);
6606 ofproto
->vlans_changed
= false;
6608 OFPROTO_FOR_EACH_TABLE (oftable
, ofproto
) {
6609 const struct cls_table
*table
;
6611 ovs_rwlock_rdlock(&oftable
->cls
.rwlock
);
6612 HMAP_FOR_EACH (table
, hmap_node
, &oftable
->cls
.tables
) {
6613 if (minimask_get_vid_mask(&table
->mask
) == VLAN_VID_MASK
) {
6614 const struct cls_rule
*rule
;
6616 HMAP_FOR_EACH (rule
, hmap_node
, &table
->rules
) {
6617 uint16_t vid
= miniflow_get_vid(&rule
->match
.flow
);
6618 bitmap_set1(vlan_bitmap
, vid
);
6619 bitmap_set1(ofproto
->vlan_bitmap
, vid
);
6623 ovs_rwlock_unlock(&oftable
->cls
.rwlock
);
6627 /* Returns true if new VLANs have come into use by the flow table since the
6628 * last call to ofproto_get_vlan_usage().
6630 * We don't track when old VLANs stop being used. */
6632 ofproto_has_vlan_usage_changed(const struct ofproto
*ofproto
)
6634 return ofproto
->vlans_changed
;
6637 /* Configures a VLAN splinter binding between the ports identified by OpenFlow
6638 * port numbers 'vlandev_ofp_port' and 'realdev_ofp_port'. If
6639 * 'realdev_ofp_port' is nonzero, then the VLAN device is enslaved to the real
6640 * device as a VLAN splinter for VLAN ID 'vid'. If 'realdev_ofp_port' is zero,
6641 * then the VLAN device is un-enslaved. */
6643 ofproto_port_set_realdev(struct ofproto
*ofproto
, ofp_port_t vlandev_ofp_port
,
6644 ofp_port_t realdev_ofp_port
, int vid
)
6646 struct ofport
*ofport
;
6649 ovs_assert(vlandev_ofp_port
!= realdev_ofp_port
);
6651 ofport
= ofproto_get_port(ofproto
, vlandev_ofp_port
);
6653 VLOG_WARN("%s: cannot set realdev on nonexistent port %"PRIu16
,
6654 ofproto
->name
, vlandev_ofp_port
);
6658 if (!ofproto
->ofproto_class
->set_realdev
) {
6659 if (!vlandev_ofp_port
) {
6662 VLOG_WARN("%s: vlan splinters not supported", ofproto
->name
);
6666 error
= ofproto
->ofproto_class
->set_realdev(ofport
, realdev_ofp_port
, vid
);
6668 VLOG_WARN("%s: setting realdev on port %"PRIu16
" (%s) failed (%s)",
6669 ofproto
->name
, vlandev_ofp_port
,
6670 netdev_get_name(ofport
->netdev
), ovs_strerror(error
));