2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
24 #include "fail-open.h"
27 #include "ofproto-provider.h"
28 #include "openvswitch/dynamic-string.h"
29 #include "openvswitch/ofp-actions.h"
30 #include "openvswitch/ofp-msgs.h"
31 #include "openvswitch/ofp-monitor.h"
32 #include "openvswitch/ofpbuf.h"
33 #include "openvswitch/vconn.h"
34 #include "openvswitch/vlog.h"
35 #include "ovs-atomic.h"
37 #include "openvswitch/poll-loop.h"
38 #include "openvswitch/rconn.h"
39 #include "openvswitch/shash.h"
46 VLOG_DEFINE_THIS_MODULE(connmgr
);
47 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
49 /* An OpenFlow connection.
55 * 'ofproto_mutex' must be held whenever an ofconn is created or destroyed or,
56 * more or less equivalently, whenever an ofconn is added to or removed from a
57 * connmgr. 'ofproto_mutex' doesn't protect the data inside the ofconn, except
58 * as specifically noted below. */
60 /* Configuration that persists from one connection to the next. */
62 struct ovs_list node
; /* In struct connmgr's "all_conns" list. */
63 struct hmap_node hmap_node
; /* In struct connmgr's "controllers" map. */
65 struct connmgr
*connmgr
; /* Connection's manager. */
66 struct rconn
*rconn
; /* OpenFlow connection. */
67 enum ofconn_type type
; /* Type. */
68 enum ofproto_band band
; /* In-band or out-of-band? */
69 bool enable_async_msgs
; /* Initially enable async messages? */
70 bool want_packet_in_on_miss
;
72 /* State that should be cleared from one connection to the next. */
75 enum ofp12_controller_role role
; /* Role. */
76 enum ofputil_protocol protocol
; /* Current protocol variant. */
77 enum ofputil_packet_in_format packet_in_format
;
79 /* OFPT_PACKET_IN related data. */
80 struct rconn_packet_counter
*packet_in_counter
; /* # queued on 'rconn'. */
81 #define N_SCHEDULERS 2
82 struct pinsched
*schedulers
[N_SCHEDULERS
];
83 int miss_send_len
; /* Bytes to send of buffered packets. */
84 uint16_t controller_id
; /* Connection controller ID. */
86 /* Number of OpenFlow messages queued on 'rconn' as replies to OpenFlow
87 * requests, and the maximum number before we stop reading OpenFlow
89 #define OFCONN_REPLY_MAX 100
90 struct rconn_packet_counter
*reply_counter
;
92 /* Asynchronous message configuration in each possible role.
94 * A 1-bit enables sending an asynchronous message for one possible reason
95 * that the message might be generated, a 0-bit disables it. */
96 struct ofputil_async_cfg
*async_cfg
;
98 /* Flow table operation logging. */
99 int n_add
, n_delete
, n_modify
; /* Number of unreported ops of each kind. */
100 long long int first_op
, last_op
; /* Range of times for unreported ops. */
101 long long int next_op_report
; /* Time to report ops, or LLONG_MAX. */
102 long long int op_backoff
; /* Earliest time to report ops again. */
104 /* Flow monitors (e.g. NXST_FLOW_MONITOR). */
106 /* Configuration. Contains "struct ofmonitor"s. */
107 struct hmap monitors
OVS_GUARDED_BY(ofproto_mutex
);
111 * When too many flow monitor notifications back up in the transmit buffer,
112 * we pause the transmission of further notifications. These members track
113 * the flow control state.
115 * When notifications are flowing, 'monitor_paused' is 0. When
116 * notifications are paused, 'monitor_paused' is the value of
117 * 'monitor_seqno' at the point we paused.
119 * 'monitor_counter' counts the OpenFlow messages and bytes currently in
120 * flight. This value growing too large triggers pausing. */
121 uint64_t monitor_paused
OVS_GUARDED_BY(ofproto_mutex
);
122 struct rconn_packet_counter
*monitor_counter
OVS_GUARDED_BY(ofproto_mutex
);
124 /* State of monitors for a single ongoing flow_mod.
126 * 'updates' is a list of "struct ofpbuf"s that contain
127 * NXST_FLOW_MONITOR_REPLY messages representing the changes made by the
130 * When 'updates' is nonempty, 'sent_abbrev_update' is true if 'updates'
131 * contains an update event of type NXFME_ABBREV and false otherwise.. */
132 struct ovs_list updates
OVS_GUARDED_BY(ofproto_mutex
);
133 bool sent_abbrev_update
OVS_GUARDED_BY(ofproto_mutex
);
135 /* Active bundles. Contains "struct ofp_bundle"s. */
137 long long int next_bundle_expiry_check
;
140 /* vswitchd/ovs-vswitchd.8.in documents the value of BUNDLE_IDLE_LIFETIME in
141 * seconds. That documentation must be kept in sync with the value below. */
142 #define BUNDLE_EXPIRY_INTERVAL 1000 /* Check bundle expiry every 1 sec. */
143 #define BUNDLE_IDLE_TIMEOUT_DEFAULT 10000 /* Expire idle bundles after
146 static unsigned int bundle_idle_timeout
= BUNDLE_IDLE_TIMEOUT_DEFAULT
;
148 static struct ofconn
*ofconn_create(struct connmgr
*, struct rconn
*,
149 enum ofconn_type
, bool enable_async_msgs
)
150 OVS_REQUIRES(ofproto_mutex
);
151 static void ofconn_destroy(struct ofconn
*) OVS_REQUIRES(ofproto_mutex
);
152 static void ofconn_flush(struct ofconn
*) OVS_REQUIRES(ofproto_mutex
);
154 static void ofconn_reconfigure(struct ofconn
*,
155 const struct ofproto_controller
*);
157 static void ofconn_run(struct ofconn
*,
158 void (*handle_openflow
)(struct ofconn
*,
159 const struct ofpbuf
*ofp_msg
));
160 static void ofconn_wait(struct ofconn
*);
162 static void ofconn_log_flow_mods(struct ofconn
*);
164 static const char *ofconn_get_target(const struct ofconn
*);
165 static char *ofconn_make_name(const struct connmgr
*, const char *target
);
167 static void ofconn_set_rate_limit(struct ofconn
*, int rate
, int burst
);
169 static void ofconn_send(const struct ofconn
*, struct ofpbuf
*,
170 struct rconn_packet_counter
*);
172 static void do_send_packet_ins(struct ofconn
*, struct ovs_list
*txq
);
174 /* A listener for incoming OpenFlow "service" connections. */
176 struct hmap_node node
; /* In struct connmgr's "services" hmap. */
177 struct pvconn
*pvconn
; /* OpenFlow connection listener. */
179 /* These are not used by ofservice directly. They are settings for
180 * accepted "struct ofconn"s from the pvconn. */
181 int probe_interval
; /* Max idle time before probing, in seconds. */
182 int rate_limit
; /* Max packet-in rate in packets per second. */
183 int burst_limit
; /* Limit on accumulating packet credits. */
184 bool enable_async_msgs
; /* Initially enable async messages? */
185 uint8_t dscp
; /* DSCP Value for controller connection */
186 uint32_t allowed_versions
; /* OpenFlow protocol versions that may
187 * be negotiated for a session. */
190 static void ofservice_reconfigure(struct ofservice
*,
191 const struct ofproto_controller
*);
192 static int ofservice_create(struct connmgr
*mgr
, const char *target
,
193 uint32_t allowed_versions
, uint8_t dscp
);
194 static void ofservice_destroy(struct connmgr
*, struct ofservice
*);
195 static struct ofservice
*ofservice_lookup(struct connmgr
*,
198 /* Connection manager for an OpenFlow switch. */
200 struct ofproto
*ofproto
;
202 char *local_port_name
;
204 /* OpenFlow connections. */
205 struct hmap controllers
; /* All OFCONN_PRIMARY controllers. */
206 struct ovs_list all_conns
; /* All controllers. All modifications are
207 protected by ofproto_mutex, so that any
208 traversals from other threads can be made
209 safe by holding the ofproto_mutex. */
210 uint64_t master_election_id
; /* monotonically increasing sequence number
211 * for master election */
212 bool master_election_id_defined
;
214 /* OpenFlow listeners. */
215 struct hmap services
; /* Contains "struct ofservice"s. */
216 struct pvconn
**snoops
;
220 struct fail_open
*fail_open
;
221 enum ofproto_fail_mode fail_mode
;
223 /* In-band control. */
224 struct in_band
*in_band
;
225 struct sockaddr_in
*extra_in_band_remotes
;
226 size_t n_extra_remotes
;
229 ATOMIC(int) want_packet_in_on_miss
; /* Sum of ofconns' values. */
232 static void update_in_band_remotes(struct connmgr
*);
233 static void add_snooper(struct connmgr
*, struct vconn
*);
234 static void ofmonitor_run(struct connmgr
*);
235 static void ofmonitor_wait(struct connmgr
*);
237 /* Creates and returns a new connection manager owned by 'ofproto'. 'name' is
238 * a name for the ofproto suitable for using in log messages.
239 * 'local_port_name' is the name of the local port (OFPP_LOCAL) within
242 connmgr_create(struct ofproto
*ofproto
,
243 const char *name
, const char *local_port_name
)
247 mgr
= xmalloc(sizeof *mgr
);
248 mgr
->ofproto
= ofproto
;
249 mgr
->name
= xstrdup(name
);
250 mgr
->local_port_name
= xstrdup(local_port_name
);
252 hmap_init(&mgr
->controllers
);
253 ovs_list_init(&mgr
->all_conns
);
254 mgr
->master_election_id
= 0;
255 mgr
->master_election_id_defined
= false;
257 hmap_init(&mgr
->services
);
261 mgr
->fail_open
= NULL
;
262 mgr
->fail_mode
= OFPROTO_FAIL_SECURE
;
265 mgr
->extra_in_band_remotes
= NULL
;
266 mgr
->n_extra_remotes
= 0;
267 mgr
->in_band_queue
= -1;
269 atomic_init(&mgr
->want_packet_in_on_miss
, 0);
274 /* The default "table-miss" behaviour for OpenFlow1.3+ is to drop the
275 * packet rather than to send the packet to the controller.
277 * This function maintains the count of pre-OpenFlow1.3 with controller_id 0,
278 * as we assume these are the controllers that should receive "table-miss"
281 update_want_packet_in_on_miss(struct ofconn
*ofconn
)
283 /* We want a packet-in on miss when controller_id is zero and OpenFlow is
284 * lower than version 1.3. */
285 enum ofputil_protocol p
= ofconn
->protocol
;
286 int new_want
= (ofconn
->controller_id
== 0 &&
287 (p
== OFPUTIL_P_NONE
||
288 ofputil_protocol_to_ofp_version(p
) < OFP13_VERSION
));
290 /* Update the setting and the count if necessary. */
291 int old_want
= ofconn
->want_packet_in_on_miss
;
292 if (old_want
!= new_want
) {
293 atomic_int
*dst
= &ofconn
->connmgr
->want_packet_in_on_miss
;
295 atomic_read_relaxed(dst
, &count
);
296 atomic_store_relaxed(dst
, count
- old_want
+ new_want
);
298 ofconn
->want_packet_in_on_miss
= new_want
;
302 /* Frees 'mgr' and all of its resources. */
304 connmgr_destroy(struct connmgr
*mgr
)
305 OVS_REQUIRES(ofproto_mutex
)
307 struct ofservice
*ofservice
, *next_ofservice
;
308 struct ofconn
*ofconn
, *next_ofconn
;
315 LIST_FOR_EACH_SAFE (ofconn
, next_ofconn
, node
, &mgr
->all_conns
) {
316 ofconn_destroy(ofconn
);
319 hmap_destroy(&mgr
->controllers
);
321 HMAP_FOR_EACH_SAFE (ofservice
, next_ofservice
, node
, &mgr
->services
) {
322 ofservice_destroy(mgr
, ofservice
);
324 hmap_destroy(&mgr
->services
);
326 for (i
= 0; i
< mgr
->n_snoops
; i
++) {
327 pvconn_close(mgr
->snoops
[i
]);
331 fail_open_destroy(mgr
->fail_open
);
332 mgr
->fail_open
= NULL
;
334 in_band_destroy(mgr
->in_band
);
336 free(mgr
->extra_in_band_remotes
);
338 free(mgr
->local_port_name
);
343 /* Does all of the periodic maintenance required by 'mgr'. Calls
344 * 'handle_openflow' for each message received on an OpenFlow connection,
345 * passing along the OpenFlow connection itself and the message that was sent.
346 * 'handle_openflow' must not modify or free the message. */
348 connmgr_run(struct connmgr
*mgr
,
349 void (*handle_openflow
)(struct ofconn
*,
350 const struct ofpbuf
*ofp_msg
))
351 OVS_EXCLUDED(ofproto_mutex
)
353 struct ofconn
*ofconn
, *next_ofconn
;
354 struct ofservice
*ofservice
;
358 if (!in_band_run(mgr
->in_band
)) {
359 in_band_destroy(mgr
->in_band
);
364 LIST_FOR_EACH_SAFE (ofconn
, next_ofconn
, node
, &mgr
->all_conns
) {
365 ofconn_run(ofconn
, handle_openflow
);
369 /* Fail-open maintenance. Do this after processing the ofconns since
370 * fail-open checks the status of the controller rconn. */
371 if (mgr
->fail_open
) {
372 fail_open_run(mgr
->fail_open
);
375 HMAP_FOR_EACH (ofservice
, node
, &mgr
->services
) {
379 retval
= pvconn_accept(ofservice
->pvconn
, &vconn
);
384 /* Passing default value for creation of the rconn */
385 rconn
= rconn_create(ofservice
->probe_interval
, 0, ofservice
->dscp
,
386 vconn_get_allowed_versions(vconn
));
387 name
= ofconn_make_name(mgr
, vconn_get_name(vconn
));
388 rconn_connect_unreliably(rconn
, vconn
, name
);
391 ovs_mutex_lock(&ofproto_mutex
);
392 ofconn
= ofconn_create(mgr
, rconn
, OFCONN_SERVICE
,
393 ofservice
->enable_async_msgs
);
394 ovs_mutex_unlock(&ofproto_mutex
);
396 ofconn_set_rate_limit(ofconn
, ofservice
->rate_limit
,
397 ofservice
->burst_limit
);
398 } else if (retval
!= EAGAIN
) {
399 VLOG_WARN_RL(&rl
, "accept failed (%s)", ovs_strerror(retval
));
403 for (i
= 0; i
< mgr
->n_snoops
; i
++) {
407 retval
= pvconn_accept(mgr
->snoops
[i
], &vconn
);
409 add_snooper(mgr
, vconn
);
410 } else if (retval
!= EAGAIN
) {
411 VLOG_WARN_RL(&rl
, "accept failed (%s)", ovs_strerror(retval
));
416 /* Causes the poll loop to wake up when connmgr_run() needs to run. */
418 connmgr_wait(struct connmgr
*mgr
)
420 struct ofservice
*ofservice
;
421 struct ofconn
*ofconn
;
424 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
429 in_band_wait(mgr
->in_band
);
431 if (mgr
->fail_open
) {
432 fail_open_wait(mgr
->fail_open
);
434 HMAP_FOR_EACH (ofservice
, node
, &mgr
->services
) {
435 pvconn_wait(ofservice
->pvconn
);
437 for (i
= 0; i
< mgr
->n_snoops
; i
++) {
438 pvconn_wait(mgr
->snoops
[i
]);
442 /* Adds some memory usage statistics for 'mgr' into 'usage', for use with
443 * memory_report(). */
445 connmgr_get_memory_usage(const struct connmgr
*mgr
, struct simap
*usage
)
447 const struct ofconn
*ofconn
;
448 unsigned int packets
= 0;
449 unsigned int ofconns
= 0;
451 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
456 packets
+= rconn_count_txqlen(ofconn
->rconn
);
457 for (i
= 0; i
< N_SCHEDULERS
; i
++) {
458 struct pinsched_stats stats
;
460 pinsched_get_stats(ofconn
->schedulers
[i
], &stats
);
461 packets
+= stats
.n_queued
;
464 simap_increase(usage
, "ofconns", ofconns
);
465 simap_increase(usage
, "packets", packets
);
468 /* Returns the ofproto that owns 'ofconn''s connmgr. */
470 ofconn_get_ofproto(const struct ofconn
*ofconn
)
472 return ofconn
->connmgr
->ofproto
;
475 /* Sets the bundle idle timeout to 'timeout' seconds, interpreting 0 as
476 * requesting the default timeout.
478 * The OpenFlow spec mandates the timeout to be at least one second; . */
480 connmgr_set_bundle_idle_timeout(unsigned timeout
)
482 bundle_idle_timeout
= (timeout
483 ? sat_mul(timeout
, 1000)
484 : BUNDLE_IDLE_TIMEOUT_DEFAULT
);
487 /* OpenFlow configuration. */
489 static void add_controller(struct connmgr
*, const char *target
, uint8_t dscp
,
490 uint32_t allowed_versions
)
491 OVS_REQUIRES(ofproto_mutex
);
492 static struct ofconn
*find_controller_by_target(struct connmgr
*,
494 static void update_fail_open(struct connmgr
*) OVS_EXCLUDED(ofproto_mutex
);
495 static int set_pvconns(struct pvconn
***pvconnsp
, size_t *n_pvconnsp
,
496 const struct sset
*);
498 /* Returns true if 'mgr' has any configured primary controllers.
500 * Service controllers do not count, but configured primary controllers do
501 * count whether or not they are currently connected. */
503 connmgr_has_controllers(const struct connmgr
*mgr
)
505 return !hmap_is_empty(&mgr
->controllers
);
508 /* Initializes 'info' and populates it with information about each configured
509 * primary controller. The keys in 'info' are the controllers' targets; the
510 * data values are corresponding "struct ofproto_controller_info".
512 * The caller owns 'info' and everything in it and should free it when it is no
515 connmgr_get_controller_info(struct connmgr
*mgr
, struct shash
*info
)
517 const struct ofconn
*ofconn
;
519 HMAP_FOR_EACH (ofconn
, hmap_node
, &mgr
->controllers
) {
520 const struct rconn
*rconn
= ofconn
->rconn
;
521 const char *target
= rconn_get_target(rconn
);
523 if (!shash_find(info
, target
)) {
524 struct ofproto_controller_info
*cinfo
= xmalloc(sizeof *cinfo
);
525 time_t now
= time_now();
526 time_t last_connection
= rconn_get_last_connection(rconn
);
527 time_t last_disconnect
= rconn_get_last_disconnect(rconn
);
528 int last_error
= rconn_get_last_error(rconn
);
531 shash_add(info
, target
, cinfo
);
533 cinfo
->is_connected
= rconn_is_connected(rconn
);
534 cinfo
->role
= ofconn
->role
;
536 smap_init(&cinfo
->pairs
);
538 smap_add(&cinfo
->pairs
, "last_error",
539 ovs_retval_to_string(last_error
));
542 smap_add(&cinfo
->pairs
, "state", rconn_get_state(rconn
));
544 if (last_connection
!= TIME_MIN
) {
545 smap_add_format(&cinfo
->pairs
, "sec_since_connect",
546 "%ld", (long int) (now
- last_connection
));
549 if (last_disconnect
!= TIME_MIN
) {
550 smap_add_format(&cinfo
->pairs
, "sec_since_disconnect",
551 "%ld", (long int) (now
- last_disconnect
));
554 for (i
= 0; i
< N_SCHEDULERS
; i
++) {
555 if (ofconn
->schedulers
[i
]) {
556 const char *name
= i
? "miss" : "action";
557 struct pinsched_stats stats
;
559 pinsched_get_stats(ofconn
->schedulers
[i
], &stats
);
560 smap_add_nocopy(&cinfo
->pairs
,
561 xasprintf("packet-in-%s-backlog", name
),
562 xasprintf("%u", stats
.n_queued
));
563 smap_add_nocopy(&cinfo
->pairs
,
564 xasprintf("packet-in-%s-bypassed", name
),
565 xasprintf("%llu", stats
.n_normal
));
566 smap_add_nocopy(&cinfo
->pairs
,
567 xasprintf("packet-in-%s-queued", name
),
568 xasprintf("%llu", stats
.n_limited
));
569 smap_add_nocopy(&cinfo
->pairs
,
570 xasprintf("packet-in-%s-dropped", name
),
571 xasprintf("%llu", stats
.n_queue_dropped
));
579 connmgr_free_controller_info(struct shash
*info
)
581 struct shash_node
*node
;
583 SHASH_FOR_EACH (node
, info
) {
584 struct ofproto_controller_info
*cinfo
= node
->data
;
585 smap_destroy(&cinfo
->pairs
);
591 /* Changes 'mgr''s set of controllers to the 'n_controllers' controllers in
594 connmgr_set_controllers(struct connmgr
*mgr
,
595 const struct ofproto_controller
*controllers
,
596 size_t n_controllers
, uint32_t allowed_versions
)
597 OVS_EXCLUDED(ofproto_mutex
)
599 bool had_controllers
= connmgr_has_controllers(mgr
);
600 struct shash new_controllers
;
601 struct ofconn
*ofconn
, *next_ofconn
;
602 struct ofservice
*ofservice
, *next_ofservice
;
605 /* Required to add and remove ofconns. This could probably be narrowed to
606 * cover a smaller amount of code, if that yielded some benefit. */
607 ovs_mutex_lock(&ofproto_mutex
);
609 /* Create newly configured controllers and services.
610 * Create a name to ofproto_controller mapping in 'new_controllers'. */
611 shash_init(&new_controllers
);
612 for (i
= 0; i
< n_controllers
; i
++) {
613 const struct ofproto_controller
*c
= &controllers
[i
];
615 if (!vconn_verify_name(c
->target
)) {
617 ofconn
= find_controller_by_target(mgr
, c
->target
);
619 VLOG_INFO("%s: added primary controller \"%s\"",
620 mgr
->name
, c
->target
);
622 } else if (rconn_get_allowed_versions(ofconn
->rconn
) !=
624 VLOG_INFO("%s: re-added primary controller \"%s\"",
625 mgr
->name
, c
->target
);
627 ofconn_destroy(ofconn
);
630 add_controller(mgr
, c
->target
, c
->dscp
, allowed_versions
);
632 } else if (!pvconn_verify_name(c
->target
)) {
634 ofservice
= ofservice_lookup(mgr
, c
->target
);
636 VLOG_INFO("%s: added service controller \"%s\"",
637 mgr
->name
, c
->target
);
639 } else if (ofservice
->allowed_versions
!= allowed_versions
) {
640 VLOG_INFO("%s: re-added service controller \"%s\"",
641 mgr
->name
, c
->target
);
642 ofservice_destroy(mgr
, ofservice
);
646 ofservice_create(mgr
, c
->target
, allowed_versions
, c
->dscp
);
649 VLOG_WARN_RL(&rl
, "%s: unsupported controller \"%s\"",
650 mgr
->name
, c
->target
);
654 shash_add_once(&new_controllers
, c
->target
, &controllers
[i
]);
657 /* Delete controllers that are no longer configured.
658 * Update configuration of all now-existing controllers. */
659 HMAP_FOR_EACH_SAFE (ofconn
, next_ofconn
, hmap_node
, &mgr
->controllers
) {
660 const char *target
= ofconn_get_target(ofconn
);
661 struct ofproto_controller
*c
;
663 c
= shash_find_data(&new_controllers
, target
);
665 VLOG_INFO("%s: removed primary controller \"%s\"",
667 ofconn_destroy(ofconn
);
669 ofconn_reconfigure(ofconn
, c
);
673 /* Delete services that are no longer configured.
674 * Update configuration of all now-existing services. */
675 HMAP_FOR_EACH_SAFE (ofservice
, next_ofservice
, node
, &mgr
->services
) {
676 const char *target
= pvconn_get_name(ofservice
->pvconn
);
677 struct ofproto_controller
*c
;
679 c
= shash_find_data(&new_controllers
, target
);
681 VLOG_INFO("%s: removed service controller \"%s\"",
683 ofservice_destroy(mgr
, ofservice
);
685 ofservice_reconfigure(ofservice
, c
);
689 shash_destroy(&new_controllers
);
691 ovs_mutex_unlock(&ofproto_mutex
);
693 update_in_band_remotes(mgr
);
694 update_fail_open(mgr
);
695 if (had_controllers
!= connmgr_has_controllers(mgr
)) {
696 ofproto_flush_flows(mgr
->ofproto
);
700 /* Drops the connections between 'mgr' and all of its primary and secondary
701 * controllers, forcing them to reconnect. */
703 connmgr_reconnect(const struct connmgr
*mgr
)
705 struct ofconn
*ofconn
;
707 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
708 rconn_reconnect(ofconn
->rconn
);
712 /* Sets the "snoops" for 'mgr' to the pvconn targets listed in 'snoops'.
714 * A "snoop" is a pvconn to which every OpenFlow message to or from the most
715 * important controller on 'mgr' is mirrored. */
717 connmgr_set_snoops(struct connmgr
*mgr
, const struct sset
*snoops
)
719 return set_pvconns(&mgr
->snoops
, &mgr
->n_snoops
, snoops
);
722 /* Adds each of the snoops currently configured on 'mgr' to 'snoops'. */
724 connmgr_get_snoops(const struct connmgr
*mgr
, struct sset
*snoops
)
728 for (i
= 0; i
< mgr
->n_snoops
; i
++) {
729 sset_add(snoops
, pvconn_get_name(mgr
->snoops
[i
]));
733 /* Returns true if 'mgr' has at least one snoop, false if it has none. */
735 connmgr_has_snoops(const struct connmgr
*mgr
)
737 return mgr
->n_snoops
> 0;
740 /* Creates a new controller for 'target' in 'mgr'. update_controller() needs
741 * to be called later to finish the new ofconn's configuration. */
743 add_controller(struct connmgr
*mgr
, const char *target
, uint8_t dscp
,
744 uint32_t allowed_versions
)
745 OVS_REQUIRES(ofproto_mutex
)
747 char *name
= ofconn_make_name(mgr
, target
);
748 struct ofconn
*ofconn
;
750 ofconn
= ofconn_create(mgr
, rconn_create(5, 8, dscp
, allowed_versions
),
751 OFCONN_PRIMARY
, true);
752 rconn_connect(ofconn
->rconn
, target
, name
);
753 hmap_insert(&mgr
->controllers
, &ofconn
->hmap_node
, hash_string(target
, 0));
758 static struct ofconn
*
759 find_controller_by_target(struct connmgr
*mgr
, const char *target
)
761 struct ofconn
*ofconn
;
763 HMAP_FOR_EACH_WITH_HASH (ofconn
, hmap_node
,
764 hash_string(target
, 0), &mgr
->controllers
) {
765 if (!strcmp(ofconn_get_target(ofconn
), target
)) {
773 update_in_band_remotes(struct connmgr
*mgr
)
775 struct sockaddr_in
*addrs
;
776 size_t max_addrs
, n_addrs
;
777 struct ofconn
*ofconn
;
780 /* Allocate enough memory for as many remotes as we could possibly have. */
781 max_addrs
= mgr
->n_extra_remotes
+ hmap_count(&mgr
->controllers
);
782 addrs
= xmalloc(max_addrs
* sizeof *addrs
);
785 /* Add all the remotes. */
786 HMAP_FOR_EACH (ofconn
, hmap_node
, &mgr
->controllers
) {
787 const char *target
= rconn_get_target(ofconn
->rconn
);
789 struct sockaddr_storage ss
;
790 struct sockaddr_in in
;
793 if (ofconn
->band
== OFPROTO_IN_BAND
794 && stream_parse_target_with_default_port(target
, OFP_PORT
, &sa
.ss
)
795 && sa
.ss
.ss_family
== AF_INET
) {
796 addrs
[n_addrs
++] = sa
.in
;
799 for (i
= 0; i
< mgr
->n_extra_remotes
; i
++) {
800 addrs
[n_addrs
++] = mgr
->extra_in_band_remotes
[i
];
803 /* Create or update or destroy in-band. */
806 in_band_create(mgr
->ofproto
, mgr
->local_port_name
, &mgr
->in_band
);
809 /* in_band_run() needs a chance to delete any existing in-band flows.
810 * We will destroy mgr->in_band after it's done with that. */
813 in_band_set_queue(mgr
->in_band
, mgr
->in_band_queue
);
814 in_band_set_remotes(mgr
->in_band
, addrs
, n_addrs
);
822 update_fail_open(struct connmgr
*mgr
)
823 OVS_EXCLUDED(ofproto_mutex
)
825 if (connmgr_has_controllers(mgr
)
826 && mgr
->fail_mode
== OFPROTO_FAIL_STANDALONE
) {
827 if (!mgr
->fail_open
) {
828 mgr
->fail_open
= fail_open_create(mgr
->ofproto
, mgr
);
831 ovs_mutex_lock(&ofproto_mutex
);
832 fail_open_destroy(mgr
->fail_open
);
833 ovs_mutex_unlock(&ofproto_mutex
);
834 mgr
->fail_open
= NULL
;
839 set_pvconns(struct pvconn
***pvconnsp
, size_t *n_pvconnsp
,
840 const struct sset
*sset
)
842 struct pvconn
**pvconns
= *pvconnsp
;
843 size_t n_pvconns
= *n_pvconnsp
;
848 for (i
= 0; i
< n_pvconns
; i
++) {
849 pvconn_close(pvconns
[i
]);
853 pvconns
= xmalloc(sset_count(sset
) * sizeof *pvconns
);
855 SSET_FOR_EACH (name
, sset
) {
856 struct pvconn
*pvconn
;
858 error
= pvconn_open(name
, 0, 0, &pvconn
);
860 pvconns
[n_pvconns
++] = pvconn
;
862 VLOG_ERR("failed to listen on %s: %s", name
, ovs_strerror(error
));
870 *n_pvconnsp
= n_pvconns
;
875 /* Returns a "preference level" for snooping 'ofconn'. A higher return value
876 * means that 'ofconn' is more interesting for monitoring than a lower return
879 snoop_preference(const struct ofconn
*ofconn
)
881 switch (ofconn
->role
) {
882 case OFPCR12_ROLE_MASTER
:
884 case OFPCR12_ROLE_EQUAL
:
886 case OFPCR12_ROLE_SLAVE
:
888 case OFPCR12_ROLE_NOCHANGE
:
890 /* Shouldn't happen. */
895 /* One of 'mgr''s "snoop" pvconns has accepted a new connection on 'vconn'.
896 * Connects this vconn to a controller. */
898 add_snooper(struct connmgr
*mgr
, struct vconn
*vconn
)
900 struct ofconn
*ofconn
, *best
;
902 /* Pick a controller for monitoring. */
904 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
905 if (ofconn
->type
== OFCONN_PRIMARY
906 && (!best
|| snoop_preference(ofconn
) > snoop_preference(best
))) {
912 rconn_add_monitor(best
->rconn
, vconn
);
914 VLOG_INFO_RL(&rl
, "no controller connection to snoop");
919 /* Public ofconn functions. */
921 /* Returns the connection type, either OFCONN_PRIMARY or OFCONN_SERVICE. */
923 ofconn_get_type(const struct ofconn
*ofconn
)
928 /* If a master election id is defined, stores it into '*idp' and returns
929 * true. Otherwise, stores UINT64_MAX into '*idp' and returns false. */
931 ofconn_get_master_election_id(const struct ofconn
*ofconn
, uint64_t *idp
)
933 *idp
= (ofconn
->connmgr
->master_election_id_defined
934 ? ofconn
->connmgr
->master_election_id
936 return ofconn
->connmgr
->master_election_id_defined
;
939 /* Sets the master election id.
941 * Returns true if successful, false if the id is stale
944 ofconn_set_master_election_id(struct ofconn
*ofconn
, uint64_t id
)
946 if (ofconn
->connmgr
->master_election_id_defined
948 /* Unsigned difference interpreted as a two's complement signed
950 (int64_t)(id
- ofconn
->connmgr
->master_election_id
) < 0) {
953 ofconn
->connmgr
->master_election_id
= id
;
954 ofconn
->connmgr
->master_election_id_defined
= true;
959 /* Returns the role configured for 'ofconn'.
961 * The default role, if no other role has been set, is OFPCR12_ROLE_EQUAL. */
962 enum ofp12_controller_role
963 ofconn_get_role(const struct ofconn
*ofconn
)
969 ofconn_send_role_status(struct ofconn
*ofconn
, uint32_t role
, uint8_t reason
)
971 struct ofputil_role_status status
;
974 status
.reason
= reason
;
976 ofconn_get_master_election_id(ofconn
, &status
.generation_id
);
978 buf
= ofputil_encode_role_status(&status
, ofconn_get_protocol(ofconn
));
980 ofconn_send(ofconn
, buf
, NULL
);
984 /* Changes 'ofconn''s role to 'role'. If 'role' is OFPCR12_ROLE_MASTER then
985 * any existing master is demoted to a slave. */
987 ofconn_set_role(struct ofconn
*ofconn
, enum ofp12_controller_role role
)
989 if (role
!= ofconn
->role
&& role
== OFPCR12_ROLE_MASTER
) {
990 struct ofconn
*other
;
992 LIST_FOR_EACH (other
, node
, &ofconn
->connmgr
->all_conns
) {
993 if (other
->role
== OFPCR12_ROLE_MASTER
) {
994 other
->role
= OFPCR12_ROLE_SLAVE
;
995 ofconn_send_role_status(other
, OFPCR12_ROLE_SLAVE
, OFPCRR_MASTER_REQUEST
);
1003 ofconn_set_invalid_ttl_to_controller(struct ofconn
*ofconn
, bool enable
)
1005 struct ofputil_async_cfg ac
= ofconn_get_async_config(ofconn
);
1006 uint32_t bit
= 1u << OFPR_INVALID_TTL
;
1008 ac
.master
[OAM_PACKET_IN
] |= bit
;
1010 ac
.master
[OAM_PACKET_IN
] &= ~bit
;
1012 ofconn_set_async_config(ofconn
, &ac
);
1016 ofconn_get_invalid_ttl_to_controller(struct ofconn
*ofconn
)
1018 struct ofputil_async_cfg ac
= ofconn_get_async_config(ofconn
);
1019 uint32_t bit
= 1u << OFPR_INVALID_TTL
;
1020 return (ac
.master
[OAM_PACKET_IN
] & bit
) != 0;
1023 /* Returns the currently configured protocol for 'ofconn', one of OFPUTIL_P_*.
1025 * Returns OFPUTIL_P_NONE, which is not a valid protocol, if 'ofconn' hasn't
1026 * completed version negotiation. This can't happen if at least one OpenFlow
1027 * message, other than OFPT_HELLO, has been received on the connection (such as
1028 * in ofproto.c's message handling code), since version negotiation is a
1029 * prerequisite for starting to receive messages. This means that
1030 * OFPUTIL_P_NONE is a special case that most callers need not worry about. */
1031 enum ofputil_protocol
1032 ofconn_get_protocol(const struct ofconn
*ofconn
)
1034 if (ofconn
->protocol
== OFPUTIL_P_NONE
&&
1035 rconn_is_connected(ofconn
->rconn
)) {
1036 int version
= rconn_get_version(ofconn
->rconn
);
1038 ofconn_set_protocol(CONST_CAST(struct ofconn
*, ofconn
),
1039 ofputil_protocol_from_ofp_version(version
));
1043 return ofconn
->protocol
;
1046 /* Sets the protocol for 'ofconn' to 'protocol' (one of OFPUTIL_P_*).
1048 * (This doesn't actually send anything to accomplish this. Presumably the
1049 * caller already did that.) */
1051 ofconn_set_protocol(struct ofconn
*ofconn
, enum ofputil_protocol protocol
)
1053 ofconn
->protocol
= protocol
;
1054 update_want_packet_in_on_miss(ofconn
);
1057 /* Returns the currently configured packet in format for 'ofconn', one of
1060 * The default, if no other format has been set, is NXPIF_STANDARD. */
1061 enum ofputil_packet_in_format
1062 ofconn_get_packet_in_format(struct ofconn
*ofconn
)
1064 return ofconn
->packet_in_format
;
1067 /* Sets the packet in format for 'ofconn' to 'packet_in_format' (one of
1070 ofconn_set_packet_in_format(struct ofconn
*ofconn
,
1071 enum ofputil_packet_in_format packet_in_format
)
1073 ofconn
->packet_in_format
= packet_in_format
;
1076 /* Sets the controller connection ID for 'ofconn' to 'controller_id'.
1078 * The connection controller ID is used for OFPP_CONTROLLER and
1079 * NXAST_CONTROLLER actions. See "struct nx_action_controller" for details. */
1081 ofconn_set_controller_id(struct ofconn
*ofconn
, uint16_t controller_id
)
1083 ofconn
->controller_id
= controller_id
;
1084 update_want_packet_in_on_miss(ofconn
);
1087 /* Returns the default miss send length for 'ofconn'. */
1089 ofconn_get_miss_send_len(const struct ofconn
*ofconn
)
1091 return ofconn
->miss_send_len
;
1094 /* Sets the default miss send length for 'ofconn' to 'miss_send_len'. */
1096 ofconn_set_miss_send_len(struct ofconn
*ofconn
, int miss_send_len
)
1098 ofconn
->miss_send_len
= miss_send_len
;
1102 ofconn_set_async_config(struct ofconn
*ofconn
,
1103 const struct ofputil_async_cfg
*ac
)
1105 if (!ofconn
->async_cfg
) {
1106 ofconn
->async_cfg
= xmalloc(sizeof *ofconn
->async_cfg
);
1108 *ofconn
->async_cfg
= *ac
;
1110 if (ofputil_protocol_to_ofp_version(ofconn_get_protocol(ofconn
))
1112 if (ofconn
->async_cfg
->master
[OAM_PACKET_IN
] & (1u << OFPR_ACTION
)) {
1113 ofconn
->async_cfg
->master
[OAM_PACKET_IN
] |= OFPR14_ACTION_BITS
;
1115 if (ofconn
->async_cfg
->slave
[OAM_PACKET_IN
] & (1u << OFPR_ACTION
)) {
1116 ofconn
->async_cfg
->slave
[OAM_PACKET_IN
] |= OFPR14_ACTION_BITS
;
1121 struct ofputil_async_cfg
1122 ofconn_get_async_config(const struct ofconn
*ofconn
)
1124 if (ofconn
->async_cfg
) {
1125 return *ofconn
->async_cfg
;
1128 int version
= rconn_get_version(ofconn
->rconn
);
1129 return (version
< 0 || !ofconn
->enable_async_msgs
1130 ? OFPUTIL_ASYNC_CFG_INIT
1131 : ofputil_async_cfg_default(version
));
1134 /* Sends 'msg' on 'ofconn', accounting it as a reply. (If there is a
1135 * sufficient number of OpenFlow replies in-flight on a single ofconn, then the
1136 * connmgr will stop accepting new OpenFlow requests on that ofconn until the
1137 * controller has accepted some of the replies.) */
1139 ofconn_send_reply(const struct ofconn
*ofconn
, struct ofpbuf
*msg
)
1141 ofconn_send(ofconn
, msg
, ofconn
->reply_counter
);
1144 /* Sends each of the messages in list 'replies' on 'ofconn' in order,
1145 * accounting them as replies. */
1147 ofconn_send_replies(const struct ofconn
*ofconn
, struct ovs_list
*replies
)
1149 struct ofpbuf
*reply
;
1151 LIST_FOR_EACH_POP (reply
, list_node
, replies
) {
1152 ofconn_send_reply(ofconn
, reply
);
1156 /* Sends 'error' on 'ofconn', as a reply to 'request'. */
1158 ofconn_send_error(const struct ofconn
*ofconn
,
1159 const struct ofp_header
*request
, enum ofperr error
)
1161 static struct vlog_rate_limit err_rl
= VLOG_RATE_LIMIT_INIT(10, 10);
1162 struct ofpbuf
*reply
;
1164 reply
= ofperr_encode_reply(error
, request
);
1165 if (!VLOG_DROP_INFO(&err_rl
)) {
1166 const char *type_name
;
1170 request_len
= ntohs(request
->length
);
1171 type_name
= (!ofpraw_decode_partial(&raw
, request
,
1172 MIN(64, request_len
))
1173 ? ofpraw_get_name(raw
)
1176 VLOG_INFO("%s: sending %s error reply to %s message",
1177 rconn_get_name(ofconn
->rconn
), ofperr_to_string(error
),
1180 ofconn_send_reply(ofconn
, reply
);
1183 /* Reports that a flow_mod operation of the type specified by 'command' was
1184 * successfully executed by 'ofconn', so that the connmgr can log it. */
1186 ofconn_report_flow_mod(struct ofconn
*ofconn
,
1187 enum ofp_flow_mod_command command
)
1197 case OFPFC_MODIFY_STRICT
:
1202 case OFPFC_DELETE_STRICT
:
1208 if (ofconn
->next_op_report
== LLONG_MAX
) {
1209 ofconn
->first_op
= now
;
1210 ofconn
->next_op_report
= MAX(now
+ 10 * 1000, ofconn
->op_backoff
);
1211 ofconn
->op_backoff
= ofconn
->next_op_report
+ 60 * 1000;
1213 ofconn
->last_op
= now
;
1216 /* OpenFlow 1.4 bundles. */
1218 static inline uint32_t
1219 bundle_hash(uint32_t id
)
1221 return hash_int(id
, 0);
1225 ofconn_get_bundle(struct ofconn
*ofconn
, uint32_t id
)
1227 struct ofp_bundle
*bundle
;
1229 HMAP_FOR_EACH_IN_BUCKET(bundle
, node
, bundle_hash(id
), &ofconn
->bundles
) {
1230 if (bundle
->id
== id
) {
1239 ofconn_insert_bundle(struct ofconn
*ofconn
, struct ofp_bundle
*bundle
)
1241 hmap_insert(&ofconn
->bundles
, &bundle
->node
, bundle_hash(bundle
->id
));
1245 ofconn_remove_bundle(struct ofconn
*ofconn
, struct ofp_bundle
*bundle
)
1247 hmap_remove(&ofconn
->bundles
, &bundle
->node
);
1251 bundle_remove_all(struct ofconn
*ofconn
)
1253 struct ofp_bundle
*b
, *next
;
1255 HMAP_FOR_EACH_SAFE (b
, next
, node
, &ofconn
->bundles
) {
1256 ofp_bundle_remove__(ofconn
, b
);
1261 bundle_remove_expired(struct ofconn
*ofconn
, long long int now
)
1263 struct ofp_bundle
*b
, *next
;
1264 long long int limit
= now
- bundle_idle_timeout
;
1266 HMAP_FOR_EACH_SAFE (b
, next
, node
, &ofconn
->bundles
) {
1267 if (b
->used
<= limit
) {
1268 ofconn_send_error(ofconn
, b
->msg
, OFPERR_OFPBFC_TIMEOUT
);
1269 ofp_bundle_remove__(ofconn
, b
);
1274 /* Private ofconn functions. */
1277 ofconn_get_target(const struct ofconn
*ofconn
)
1279 return rconn_get_target(ofconn
->rconn
);
1282 static struct ofconn
*
1283 ofconn_create(struct connmgr
*mgr
, struct rconn
*rconn
, enum ofconn_type type
,
1284 bool enable_async_msgs
)
1285 OVS_REQUIRES(ofproto_mutex
)
1287 struct ofconn
*ofconn
;
1289 ofconn
= xzalloc(sizeof *ofconn
);
1290 ofconn
->connmgr
= mgr
;
1291 ovs_list_push_back(&mgr
->all_conns
, &ofconn
->node
);
1292 ofconn
->rconn
= rconn
;
1293 ofconn
->type
= type
;
1294 ofconn
->enable_async_msgs
= enable_async_msgs
;
1296 hmap_init(&ofconn
->monitors
);
1297 ovs_list_init(&ofconn
->updates
);
1299 hmap_init(&ofconn
->bundles
);
1300 ofconn
->next_bundle_expiry_check
= time_msec() + BUNDLE_EXPIRY_INTERVAL
;
1302 ofconn_flush(ofconn
);
1307 /* Clears all of the state in 'ofconn' that should not persist from one
1308 * connection to the next. */
1310 ofconn_flush(struct ofconn
*ofconn
)
1311 OVS_REQUIRES(ofproto_mutex
)
1313 struct ofmonitor
*monitor
, *next_monitor
;
1316 ofconn_log_flow_mods(ofconn
);
1318 ofconn
->role
= OFPCR12_ROLE_EQUAL
;
1319 ofconn_set_protocol(ofconn
, OFPUTIL_P_NONE
);
1320 ofconn
->packet_in_format
= OFPUTIL_PACKET_IN_STD
;
1322 rconn_packet_counter_destroy(ofconn
->packet_in_counter
);
1323 ofconn
->packet_in_counter
= rconn_packet_counter_create();
1324 for (i
= 0; i
< N_SCHEDULERS
; i
++) {
1325 if (ofconn
->schedulers
[i
]) {
1328 pinsched_get_limits(ofconn
->schedulers
[i
], &rate
, &burst
);
1329 pinsched_destroy(ofconn
->schedulers
[i
]);
1330 ofconn
->schedulers
[i
] = pinsched_create(rate
, burst
);
1333 ofconn
->miss_send_len
= (ofconn
->type
== OFCONN_PRIMARY
1334 ? OFP_DEFAULT_MISS_SEND_LEN
1336 ofconn
->controller_id
= 0;
1338 rconn_packet_counter_destroy(ofconn
->reply_counter
);
1339 ofconn
->reply_counter
= rconn_packet_counter_create();
1341 free(ofconn
->async_cfg
);
1342 ofconn
->async_cfg
= NULL
;
1344 ofconn
->n_add
= ofconn
->n_delete
= ofconn
->n_modify
= 0;
1345 ofconn
->first_op
= ofconn
->last_op
= LLONG_MIN
;
1346 ofconn
->next_op_report
= LLONG_MAX
;
1347 ofconn
->op_backoff
= LLONG_MIN
;
1349 HMAP_FOR_EACH_SAFE (monitor
, next_monitor
, ofconn_node
,
1350 &ofconn
->monitors
) {
1351 ofmonitor_destroy(monitor
);
1353 rconn_packet_counter_destroy(ofconn
->monitor_counter
);
1354 ofconn
->monitor_counter
= rconn_packet_counter_create();
1355 ofpbuf_list_delete(&ofconn
->updates
); /* ...but it should be empty. */
1359 ofconn_destroy(struct ofconn
*ofconn
)
1360 OVS_REQUIRES(ofproto_mutex
)
1362 ofconn_flush(ofconn
);
1364 /* Force clearing of want_packet_in_on_miss to keep the global count
1366 ofconn
->controller_id
= 1;
1367 update_want_packet_in_on_miss(ofconn
);
1369 if (ofconn
->type
== OFCONN_PRIMARY
) {
1370 hmap_remove(&ofconn
->connmgr
->controllers
, &ofconn
->hmap_node
);
1373 bundle_remove_all(ofconn
);
1374 hmap_destroy(&ofconn
->bundles
);
1376 hmap_destroy(&ofconn
->monitors
);
1377 ovs_list_remove(&ofconn
->node
);
1378 rconn_destroy(ofconn
->rconn
);
1379 rconn_packet_counter_destroy(ofconn
->packet_in_counter
);
1380 rconn_packet_counter_destroy(ofconn
->reply_counter
);
1381 rconn_packet_counter_destroy(ofconn
->monitor_counter
);
1385 /* Reconfigures 'ofconn' to match 'c'. 'ofconn' and 'c' must have the same
1388 ofconn_reconfigure(struct ofconn
*ofconn
, const struct ofproto_controller
*c
)
1392 ofconn
->band
= c
->band
;
1393 ofconn
->enable_async_msgs
= c
->enable_async_msgs
;
1395 rconn_set_max_backoff(ofconn
->rconn
, c
->max_backoff
);
1397 probe_interval
= c
->probe_interval
? MAX(c
->probe_interval
, 5) : 0;
1398 rconn_set_probe_interval(ofconn
->rconn
, probe_interval
);
1400 ofconn_set_rate_limit(ofconn
, c
->rate_limit
, c
->burst_limit
);
1402 /* If dscp value changed reconnect. */
1403 if (c
->dscp
!= rconn_get_dscp(ofconn
->rconn
)) {
1404 rconn_set_dscp(ofconn
->rconn
, c
->dscp
);
1405 rconn_reconnect(ofconn
->rconn
);
1409 /* Returns true if it makes sense for 'ofconn' to receive and process OpenFlow
1412 ofconn_may_recv(const struct ofconn
*ofconn
)
1414 int count
= rconn_packet_counter_n_packets(ofconn
->reply_counter
);
1415 return count
< OFCONN_REPLY_MAX
;
1419 ofconn_run(struct ofconn
*ofconn
,
1420 void (*handle_openflow
)(struct ofconn
*,
1421 const struct ofpbuf
*ofp_msg
))
1423 struct connmgr
*mgr
= ofconn
->connmgr
;
1426 for (i
= 0; i
< N_SCHEDULERS
; i
++) {
1427 struct ovs_list txq
;
1429 pinsched_run(ofconn
->schedulers
[i
], &txq
);
1430 do_send_packet_ins(ofconn
, &txq
);
1433 rconn_run(ofconn
->rconn
);
1435 /* Limit the number of iterations to avoid starving other tasks. */
1436 for (i
= 0; i
< 50 && ofconn_may_recv(ofconn
); i
++) {
1437 struct ofpbuf
*of_msg
= rconn_recv(ofconn
->rconn
);
1442 if (mgr
->fail_open
) {
1443 fail_open_maybe_recover(mgr
->fail_open
);
1446 handle_openflow(ofconn
, of_msg
);
1447 ofpbuf_delete(of_msg
);
1450 long long int now
= time_msec();
1452 if (now
>= ofconn
->next_bundle_expiry_check
) {
1453 ofconn
->next_bundle_expiry_check
= now
+ BUNDLE_EXPIRY_INTERVAL
;
1454 bundle_remove_expired(ofconn
, now
);
1457 if (now
>= ofconn
->next_op_report
) {
1458 ofconn_log_flow_mods(ofconn
);
1461 ovs_mutex_lock(&ofproto_mutex
);
1462 if (!rconn_is_alive(ofconn
->rconn
)) {
1463 ofconn_destroy(ofconn
);
1464 } else if (!rconn_is_connected(ofconn
->rconn
)) {
1465 ofconn_flush(ofconn
);
1467 ovs_mutex_unlock(&ofproto_mutex
);
1471 ofconn_wait(struct ofconn
*ofconn
)
1475 for (i
= 0; i
< N_SCHEDULERS
; i
++) {
1476 pinsched_wait(ofconn
->schedulers
[i
]);
1478 rconn_run_wait(ofconn
->rconn
);
1479 if (ofconn_may_recv(ofconn
)) {
1480 rconn_recv_wait(ofconn
->rconn
);
1482 if (ofconn
->next_op_report
!= LLONG_MAX
) {
1483 poll_timer_wait_until(ofconn
->next_op_report
);
1488 ofconn_log_flow_mods(struct ofconn
*ofconn
)
1490 int n_flow_mods
= ofconn
->n_add
+ ofconn
->n_delete
+ ofconn
->n_modify
;
1492 long long int ago
= (time_msec() - ofconn
->first_op
) / 1000;
1493 long long int interval
= (ofconn
->last_op
- ofconn
->first_op
) / 1000;
1497 ds_put_format(&s
, "%d flow_mods ", n_flow_mods
);
1498 if (interval
== ago
) {
1499 ds_put_format(&s
, "in the last %lld s", ago
);
1500 } else if (interval
) {
1501 ds_put_format(&s
, "in the %lld s starting %lld s ago",
1504 ds_put_format(&s
, "%lld s ago", ago
);
1507 ds_put_cstr(&s
, " (");
1508 if (ofconn
->n_add
) {
1509 ds_put_format(&s
, "%d adds, ", ofconn
->n_add
);
1511 if (ofconn
->n_delete
) {
1512 ds_put_format(&s
, "%d deletes, ", ofconn
->n_delete
);
1514 if (ofconn
->n_modify
) {
1515 ds_put_format(&s
, "%d modifications, ", ofconn
->n_modify
);
1518 ds_put_char(&s
, ')');
1520 VLOG_INFO("%s: %s", rconn_get_name(ofconn
->rconn
), ds_cstr(&s
));
1523 ofconn
->n_add
= ofconn
->n_delete
= ofconn
->n_modify
= 0;
1525 ofconn
->next_op_report
= LLONG_MAX
;
1528 /* Returns true if 'ofconn' should receive asynchronous messages of the given
1529 * OAM_* 'type' and 'reason', which should be a OFPR_* value for OAM_PACKET_IN,
1530 * a OFPPR_* value for OAM_PORT_STATUS, or an OFPRR_* value for
1531 * OAM_FLOW_REMOVED. Returns false if the message should not be sent on
1534 ofconn_receives_async_msg(const struct ofconn
*ofconn
,
1535 enum ofputil_async_msg_type type
,
1536 unsigned int reason
)
1538 ovs_assert(reason
< 32);
1539 ovs_assert((unsigned int) type
< OAM_N_TYPES
);
1541 /* Keep the following code in sync with the documentation in the
1542 * "Asynchronous Messages" section in 'topics/design' */
1544 if (ofconn
->type
== OFCONN_SERVICE
&& !ofconn
->miss_send_len
) {
1545 /* Service connections don't get asynchronous messages unless they have
1546 * explicitly asked for them by setting a nonzero miss send length. */
1550 struct ofputil_async_cfg ac
= ofconn_get_async_config(ofconn
);
1551 uint32_t *masks
= (ofconn
->role
== OFPCR12_ROLE_SLAVE
1554 return (masks
[type
] & (1u << reason
)) != 0;
1557 /* This function returns true to indicate that a packet_in message
1558 * for a "table-miss" should be sent to at least one controller.
1560 * False otherwise. */
1562 connmgr_wants_packet_in_on_miss(struct connmgr
*mgr
)
1566 atomic_read_relaxed(&mgr
->want_packet_in_on_miss
, &count
);
1570 /* Returns a human-readable name for an OpenFlow connection between 'mgr' and
1571 * 'target', suitable for use in log messages for identifying the connection.
1573 * The name is dynamically allocated. The caller should free it (with free())
1574 * when it is no longer needed. */
1576 ofconn_make_name(const struct connmgr
*mgr
, const char *target
)
1578 return xasprintf("%s<->%s", mgr
->name
, target
);
1582 ofconn_set_rate_limit(struct ofconn
*ofconn
, int rate
, int burst
)
1586 for (i
= 0; i
< N_SCHEDULERS
; i
++) {
1587 struct pinsched
**s
= &ofconn
->schedulers
[i
];
1591 *s
= pinsched_create(rate
, burst
);
1593 pinsched_set_limits(*s
, rate
, burst
);
1596 pinsched_destroy(*s
);
1603 ofconn_send(const struct ofconn
*ofconn
, struct ofpbuf
*msg
,
1604 struct rconn_packet_counter
*counter
)
1606 ofpmsg_update_length(msg
);
1607 rconn_send(ofconn
->rconn
, msg
, counter
);
1610 /* Sending asynchronous messages. */
1612 /* Sends an OFPT_PORT_STATUS message with 'new_pp' and 'reason' to appropriate
1613 * controllers managed by 'mgr'. For messages caused by a controller
1614 * OFPT_PORT_MOD, specify 'source' as the controller connection that sent the
1615 * request; otherwise, specify 'source' as NULL.
1617 * If 'reason' is OFPPR_MODIFY and 'old_pp' is nonnull, then messages are
1618 * suppressed in the case where the change would not be visible to a particular
1619 * controller. For example, OpenFlow 1.0 does not have the OFPPS_LIVE flag, so
1620 * this would suppress a change solely to that flag from being sent to an
1621 * OpenFlow 1.0 controller. */
1623 connmgr_send_port_status(struct connmgr
*mgr
, struct ofconn
*source
,
1624 const struct ofputil_phy_port
*old_pp
,
1625 const struct ofputil_phy_port
*new_pp
,
1628 /* XXX Should limit the number of queued port status change messages. */
1629 struct ofputil_port_status new_ps
= { reason
, *new_pp
};
1631 struct ofconn
*ofconn
;
1632 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
1633 if (ofconn_receives_async_msg(ofconn
, OAM_PORT_STATUS
, reason
)) {
1634 /* Before 1.5, OpenFlow specified that OFPT_PORT_MOD should not
1635 * generate OFPT_PORT_STATUS messages. That requirement was a
1636 * relic of how OpenFlow originally supported a single controller,
1637 * so that one could expect the controller to already know the
1638 * changes it had made.
1640 * EXT-338 changes OpenFlow 1.5 OFPT_PORT_MOD to send
1641 * OFPT_PORT_STATUS messages to every controller. This is
1642 * obviously more useful in the multi-controller case. We could
1643 * always implement it that way in OVS, but that would risk
1644 * confusing controllers that are intended for single-controller
1645 * use only. (Imagine a controller that generates an OFPT_PORT_MOD
1646 * in response to any OFPT_PORT_STATUS!)
1648 * So this compromises: for OpenFlow 1.4 and earlier, it generates
1649 * OFPT_PORT_STATUS for OFPT_PORT_MOD, but not back to the
1650 * originating controller. In a single-controller environment, in
1651 * particular, this means that it will never generate
1652 * OFPT_PORT_STATUS for OFPT_PORT_MOD at all. */
1653 if (ofconn
== source
1654 && rconn_get_version(ofconn
->rconn
) < OFP15_VERSION
) {
1658 enum ofputil_protocol protocol
= ofconn_get_protocol(ofconn
);
1659 struct ofpbuf
*msg
= ofputil_encode_port_status(&new_ps
, protocol
);
1660 if (reason
== OFPPR_MODIFY
&& old_pp
) {
1661 struct ofputil_port_status old_ps
= { reason
, *old_pp
};
1662 struct ofpbuf
*old_msg
= ofputil_encode_port_status(&old_ps
,
1664 bool suppress
= ofpbuf_equal(msg
, old_msg
);
1665 ofpbuf_delete(old_msg
);
1673 ofconn_send(ofconn
, msg
, NULL
);
1678 /* Sends an OFPT_REQUESTFORWARD message with 'request' and 'reason' to
1679 * appropriate controllers managed by 'mgr'. For messages caused by a
1680 * controller OFPT_GROUP_MOD and OFPT_METER_MOD, specify 'source' as the
1681 * controller connection that sent the request; otherwise, specify 'source'
1684 connmgr_send_requestforward(struct connmgr
*mgr
, const struct ofconn
*source
,
1685 const struct ofputil_requestforward
*rf
)
1687 struct ofconn
*ofconn
;
1689 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
1690 if (ofconn_receives_async_msg(ofconn
, OAM_REQUESTFORWARD
, rf
->reason
)
1691 && rconn_get_version(ofconn
->rconn
) >= OFP14_VERSION
1692 && ofconn
!= source
) {
1693 enum ofputil_protocol protocol
= ofconn_get_protocol(ofconn
);
1694 ofconn_send(ofconn
, ofputil_encode_requestforward(rf
, protocol
),
1700 /* Sends an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message based on 'fr' to
1701 * appropriate controllers managed by 'mgr'.
1703 * This may be called from the RCU thread. */
1705 connmgr_send_flow_removed(struct connmgr
*mgr
,
1706 const struct ofputil_flow_removed
*fr
)
1707 OVS_REQUIRES(ofproto_mutex
)
1709 struct ofconn
*ofconn
;
1711 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
1712 if (ofconn_receives_async_msg(ofconn
, OAM_FLOW_REMOVED
, fr
->reason
)) {
1715 /* Account flow expirations as replies to OpenFlow requests. That
1716 * works because preventing OpenFlow requests from being processed
1717 * also prevents new flows from being added (and expiring). (It
1718 * also prevents processing OpenFlow requests that would not add
1719 * new flows, so it is imperfect.) */
1720 msg
= ofputil_encode_flow_removed(fr
, ofconn_get_protocol(ofconn
));
1721 ofconn_send_reply(ofconn
, msg
);
1726 /* Sends an OFPT_TABLE_STATUS message with 'reason' to appropriate controllers
1727 * managed by 'mgr'. When the table state changes, the controller needs to be
1728 * informed with the OFPT_TABLE_STATUS message. The reason values
1729 * OFPTR_VACANCY_DOWN and OFPTR_VACANCY_UP identify a vacancy message. The
1730 * vacancy events are generated when the remaining space in the flow table
1731 * changes and crosses one of the vacancy thereshold specified by
1732 * OFPT_TABLE_MOD. */
1734 connmgr_send_table_status(struct connmgr
*mgr
,
1735 const struct ofputil_table_desc
*td
,
1738 struct ofputil_table_status ts
;
1739 struct ofconn
*ofconn
;
1744 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
1745 if (ofconn_receives_async_msg(ofconn
, OAM_TABLE_STATUS
, reason
)) {
1748 msg
= ofputil_encode_table_status(&ts
,
1749 ofconn_get_protocol(ofconn
));
1751 ofconn_send(ofconn
, msg
, NULL
);
1757 /* Given 'pin', sends an OFPT_PACKET_IN message to each OpenFlow controller as
1758 * necessary according to their individual configurations. */
1760 connmgr_send_async_msg(struct connmgr
*mgr
,
1761 const struct ofproto_async_msg
*am
)
1763 struct ofconn
*ofconn
;
1765 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
1766 enum ofputil_protocol protocol
= ofconn_get_protocol(ofconn
);
1767 if (protocol
== OFPUTIL_P_NONE
|| !rconn_is_connected(ofconn
->rconn
)
1768 || ofconn
->controller_id
!= am
->controller_id
1769 || !ofconn_receives_async_msg(ofconn
, am
->oam
,
1770 am
->pin
.up
.base
.reason
)) {
1774 struct ofpbuf
*msg
= ofputil_encode_packet_in_private(
1775 &am
->pin
.up
, protocol
, ofconn
->packet_in_format
);
1777 struct ovs_list txq
;
1778 bool is_miss
= (am
->pin
.up
.base
.reason
== OFPR_NO_MATCH
||
1779 am
->pin
.up
.base
.reason
== OFPR_EXPLICIT_MISS
||
1780 am
->pin
.up
.base
.reason
== OFPR_IMPLICIT_MISS
);
1781 pinsched_send(ofconn
->schedulers
[is_miss
],
1782 am
->pin
.up
.base
.flow_metadata
.flow
.in_port
.ofp_port
,
1784 do_send_packet_ins(ofconn
, &txq
);
1789 do_send_packet_ins(struct ofconn
*ofconn
, struct ovs_list
*txq
)
1793 LIST_FOR_EACH_POP (pin
, list_node
, txq
) {
1794 if (rconn_send_with_limit(ofconn
->rconn
, pin
,
1795 ofconn
->packet_in_counter
, 100) == EAGAIN
) {
1796 static struct vlog_rate_limit rll
= VLOG_RATE_LIMIT_INIT(5, 5);
1798 VLOG_INFO_RL(&rll
, "%s: dropping packet-in due to queue overflow",
1799 rconn_get_name(ofconn
->rconn
));
1804 /* Fail-open settings. */
1806 /* Returns the failure handling mode (OFPROTO_FAIL_SECURE or
1807 * OFPROTO_FAIL_STANDALONE) for 'mgr'. */
1808 enum ofproto_fail_mode
1809 connmgr_get_fail_mode(const struct connmgr
*mgr
)
1811 return mgr
->fail_mode
;
1814 /* Sets the failure handling mode for 'mgr' to 'fail_mode' (either
1815 * OFPROTO_FAIL_SECURE or OFPROTO_FAIL_STANDALONE). */
1817 connmgr_set_fail_mode(struct connmgr
*mgr
, enum ofproto_fail_mode fail_mode
)
1819 if (mgr
->fail_mode
!= fail_mode
) {
1820 mgr
->fail_mode
= fail_mode
;
1821 update_fail_open(mgr
);
1822 if (!connmgr_has_controllers(mgr
)) {
1823 ofproto_flush_flows(mgr
->ofproto
);
1828 /* Fail-open implementation. */
1830 /* Returns the longest probe interval among the primary controllers configured
1831 * on 'mgr'. Returns 0 if there are no primary controllers. */
1833 connmgr_get_max_probe_interval(const struct connmgr
*mgr
)
1835 const struct ofconn
*ofconn
;
1836 int max_probe_interval
;
1838 max_probe_interval
= 0;
1839 HMAP_FOR_EACH (ofconn
, hmap_node
, &mgr
->controllers
) {
1840 int probe_interval
= rconn_get_probe_interval(ofconn
->rconn
);
1841 max_probe_interval
= MAX(max_probe_interval
, probe_interval
);
1843 return max_probe_interval
;
1846 /* Returns the number of seconds for which all of 'mgr's primary controllers
1847 * have been disconnected. Returns 0 if 'mgr' has no primary controllers. */
1849 connmgr_failure_duration(const struct connmgr
*mgr
)
1851 const struct ofconn
*ofconn
;
1852 int min_failure_duration
;
1854 if (!connmgr_has_controllers(mgr
)) {
1858 min_failure_duration
= INT_MAX
;
1859 HMAP_FOR_EACH (ofconn
, hmap_node
, &mgr
->controllers
) {
1860 int failure_duration
= rconn_failure_duration(ofconn
->rconn
);
1861 min_failure_duration
= MIN(min_failure_duration
, failure_duration
);
1863 return min_failure_duration
;
1866 /* Returns true if at least one primary controller is connected (regardless of
1867 * whether those controllers are believed to have authenticated and accepted
1868 * this switch), false if none of them are connected. */
1870 connmgr_is_any_controller_connected(const struct connmgr
*mgr
)
1872 const struct ofconn
*ofconn
;
1874 HMAP_FOR_EACH (ofconn
, hmap_node
, &mgr
->controllers
) {
1875 if (rconn_is_connected(ofconn
->rconn
)) {
1882 /* Returns true if at least one primary controller is believed to have
1883 * authenticated and accepted this switch, false otherwise. */
1885 connmgr_is_any_controller_admitted(const struct connmgr
*mgr
)
1887 const struct ofconn
*ofconn
;
1889 HMAP_FOR_EACH (ofconn
, hmap_node
, &mgr
->controllers
) {
1890 if (rconn_is_admitted(ofconn
->rconn
)) {
1897 /* In-band configuration. */
1899 static bool any_extras_changed(const struct connmgr
*,
1900 const struct sockaddr_in
*extras
, size_t n
);
1902 /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'mgr''s
1903 * in-band control should guarantee access, in the same way that in-band
1904 * control guarantees access to OpenFlow controllers. */
1906 connmgr_set_extra_in_band_remotes(struct connmgr
*mgr
,
1907 const struct sockaddr_in
*extras
, size_t n
)
1909 if (!any_extras_changed(mgr
, extras
, n
)) {
1913 free(mgr
->extra_in_band_remotes
);
1914 mgr
->n_extra_remotes
= n
;
1915 mgr
->extra_in_band_remotes
= xmemdup(extras
, n
* sizeof *extras
);
1917 update_in_band_remotes(mgr
);
1920 /* Sets the OpenFlow queue used by flows set up by in-band control on
1921 * 'mgr' to 'queue_id'. If 'queue_id' is negative, then in-band control
1922 * flows will use the default queue. */
1924 connmgr_set_in_band_queue(struct connmgr
*mgr
, int queue_id
)
1926 if (queue_id
!= mgr
->in_band_queue
) {
1927 mgr
->in_band_queue
= queue_id
;
1928 update_in_band_remotes(mgr
);
1933 any_extras_changed(const struct connmgr
*mgr
,
1934 const struct sockaddr_in
*extras
, size_t n
)
1938 if (n
!= mgr
->n_extra_remotes
) {
1942 for (i
= 0; i
< n
; i
++) {
1943 const struct sockaddr_in
*old
= &mgr
->extra_in_band_remotes
[i
];
1944 const struct sockaddr_in
*new = &extras
[i
];
1946 if (old
->sin_addr
.s_addr
!= new->sin_addr
.s_addr
||
1947 old
->sin_port
!= new->sin_port
) {
1955 /* In-band implementation. */
1958 connmgr_has_in_band(struct connmgr
*mgr
)
1960 return mgr
->in_band
!= NULL
;
1963 /* Fail-open and in-band implementation. */
1965 /* Called by 'ofproto' after all flows have been flushed, to allow fail-open
1966 * and standalone mode to re-create their flows.
1968 * In-band control has more sophisticated code that manages flows itself. */
1970 connmgr_flushed(struct connmgr
*mgr
)
1971 OVS_EXCLUDED(ofproto_mutex
)
1973 if (mgr
->fail_open
) {
1974 fail_open_flushed(mgr
->fail_open
);
1977 /* If there are no controllers and we're in standalone mode, set up a flow
1978 * that matches every packet and directs them to OFPP_NORMAL (which goes to
1979 * us). Otherwise, the switch is in secure mode and we won't pass any
1980 * traffic until a controller has been defined and it tells us to do so. */
1981 if (!connmgr_has_controllers(mgr
)
1982 && mgr
->fail_mode
== OFPROTO_FAIL_STANDALONE
) {
1983 struct ofpbuf ofpacts
;
1986 ofpbuf_init(&ofpacts
, OFPACT_OUTPUT_SIZE
);
1987 ofpact_put_OUTPUT(&ofpacts
)->port
= OFPP_NORMAL
;
1989 match_init_catchall(&match
);
1990 ofproto_add_flow(mgr
->ofproto
, &match
, 0, ofpacts
.data
,
1993 ofpbuf_uninit(&ofpacts
);
1997 /* Returns the number of hidden rules created by the in-band and fail-open
1998 * implementations in table 0. (Subtracting this count from the number of
1999 * rules in the table 0 classifier, as maintained in struct oftable, yields
2000 * the number of flows that OVS should report via OpenFlow for table 0.) */
2002 connmgr_count_hidden_rules(const struct connmgr
*mgr
)
2006 n_hidden
+= in_band_count_rules(mgr
->in_band
);
2008 if (mgr
->fail_open
) {
2009 n_hidden
+= fail_open_count_rules(mgr
->fail_open
);
2014 /* Creates a new ofservice for 'target' in 'mgr'. Returns 0 if successful,
2015 * otherwise a positive errno value.
2017 * ofservice_reconfigure() must be called to fully configure the new
2020 ofservice_create(struct connmgr
*mgr
, const char *target
,
2021 uint32_t allowed_versions
, uint8_t dscp
)
2023 struct ofservice
*ofservice
;
2024 struct pvconn
*pvconn
;
2027 error
= pvconn_open(target
, allowed_versions
, dscp
, &pvconn
);
2032 ofservice
= xzalloc(sizeof *ofservice
);
2033 hmap_insert(&mgr
->services
, &ofservice
->node
, hash_string(target
, 0));
2034 ofservice
->pvconn
= pvconn
;
2035 ofservice
->allowed_versions
= allowed_versions
;
2041 ofservice_destroy(struct connmgr
*mgr
, struct ofservice
*ofservice
)
2043 hmap_remove(&mgr
->services
, &ofservice
->node
);
2044 pvconn_close(ofservice
->pvconn
);
2049 ofservice_reconfigure(struct ofservice
*ofservice
,
2050 const struct ofproto_controller
*c
)
2052 ofservice
->probe_interval
= c
->probe_interval
;
2053 ofservice
->rate_limit
= c
->rate_limit
;
2054 ofservice
->burst_limit
= c
->burst_limit
;
2055 ofservice
->enable_async_msgs
= c
->enable_async_msgs
;
2056 ofservice
->dscp
= c
->dscp
;
2059 /* Finds and returns the ofservice within 'mgr' that has the given
2060 * 'target', or a null pointer if none exists. */
2061 static struct ofservice
*
2062 ofservice_lookup(struct connmgr
*mgr
, const char *target
)
2064 struct ofservice
*ofservice
;
2066 HMAP_FOR_EACH_WITH_HASH (ofservice
, node
, hash_string(target
, 0),
2068 if (!strcmp(pvconn_get_name(ofservice
->pvconn
), target
)) {
2075 /* Flow monitors (NXST_FLOW_MONITOR). */
2077 /* A counter incremented when something significant happens to an OpenFlow
2080 * - When a rule is added, its 'add_seqno' and 'modify_seqno' are set to
2081 * the current value (which is then incremented).
2083 * - When a rule is modified, its 'modify_seqno' is set to the current
2084 * value (which is then incremented).
2086 * Thus, by comparing an old value of monitor_seqno against a rule's
2087 * 'add_seqno', one can tell whether the rule was added before or after the old
2088 * value was read, and similarly for 'modify_seqno'.
2090 * 32 bits should normally be sufficient (and would be nice, to save space in
2091 * each rule) but then we'd have to have some special cases for wraparound.
2093 * We initialize monitor_seqno to 1 to allow 0 to be used as an invalid
2095 static uint64_t monitor_seqno
= 1;
2097 COVERAGE_DEFINE(ofmonitor_pause
);
2098 COVERAGE_DEFINE(ofmonitor_resume
);
2101 ofmonitor_create(const struct ofputil_flow_monitor_request
*request
,
2102 struct ofconn
*ofconn
, struct ofmonitor
**monitorp
)
2103 OVS_REQUIRES(ofproto_mutex
)
2105 struct ofmonitor
*m
;
2109 m
= ofmonitor_lookup(ofconn
, request
->id
);
2111 return OFPERR_OFPMOFC_MONITOR_EXISTS
;
2114 m
= xmalloc(sizeof *m
);
2116 hmap_insert(&ofconn
->monitors
, &m
->ofconn_node
, hash_int(request
->id
, 0));
2117 m
->id
= request
->id
;
2118 m
->flags
= request
->flags
;
2119 m
->out_port
= request
->out_port
;
2120 m
->table_id
= request
->table_id
;
2121 minimatch_init(&m
->match
, &request
->match
);
2128 ofmonitor_lookup(struct ofconn
*ofconn
, uint32_t id
)
2129 OVS_REQUIRES(ofproto_mutex
)
2131 struct ofmonitor
*m
;
2133 HMAP_FOR_EACH_IN_BUCKET (m
, ofconn_node
, hash_int(id
, 0),
2134 &ofconn
->monitors
) {
2143 ofmonitor_destroy(struct ofmonitor
*m
)
2144 OVS_REQUIRES(ofproto_mutex
)
2147 minimatch_destroy(&m
->match
);
2148 hmap_remove(&m
->ofconn
->monitors
, &m
->ofconn_node
);
2154 ofmonitor_report(struct connmgr
*mgr
, struct rule
*rule
,
2155 enum nx_flow_update_event event
,
2156 enum ofp_flow_removed_reason reason
,
2157 const struct ofconn
*abbrev_ofconn
, ovs_be32 abbrev_xid
,
2158 const struct rule_actions
*old_actions
)
2159 OVS_REQUIRES(ofproto_mutex
)
2161 enum nx_flow_monitor_flags update
;
2162 struct ofconn
*ofconn
;
2164 if (rule_is_hidden(rule
)) {
2171 rule
->add_seqno
= rule
->modify_seqno
= monitor_seqno
++;
2175 update
= NXFMF_DELETE
;
2178 case NXFME_MODIFIED
:
2179 update
= NXFMF_MODIFY
;
2180 rule
->modify_seqno
= monitor_seqno
++;
2188 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
2189 enum nx_flow_monitor_flags flags
= 0;
2190 struct ofmonitor
*m
;
2192 if (ofconn
->monitor_paused
) {
2193 /* Only send NXFME_DELETED notifications for flows that were added
2194 * before we paused. */
2195 if (event
!= NXFME_DELETED
2196 || rule
->add_seqno
> ofconn
->monitor_paused
) {
2201 HMAP_FOR_EACH (m
, ofconn_node
, &ofconn
->monitors
) {
2202 if (m
->flags
& update
2203 && (m
->table_id
== 0xff || m
->table_id
== rule
->table_id
)
2204 && (ofproto_rule_has_out_port(rule
, m
->out_port
)
2206 && ofpacts_output_to_port(old_actions
->ofpacts
,
2207 old_actions
->ofpacts_len
,
2209 && cls_rule_is_loose_match(&rule
->cr
, &m
->match
)) {
2215 if (ovs_list_is_empty(&ofconn
->updates
)) {
2216 ofputil_start_flow_update(&ofconn
->updates
);
2217 ofconn
->sent_abbrev_update
= false;
2220 if (flags
& NXFMF_OWN
|| ofconn
!= abbrev_ofconn
2221 || ofconn
->monitor_paused
) {
2222 struct ofputil_flow_update fu
;
2225 fu
.reason
= event
== NXFME_DELETED
? reason
: 0;
2226 fu
.table_id
= rule
->table_id
;
2227 fu
.cookie
= rule
->flow_cookie
;
2228 minimatch_expand(&rule
->cr
.match
, &fu
.match
);
2229 fu
.priority
= rule
->cr
.priority
;
2231 ovs_mutex_lock(&rule
->mutex
);
2232 fu
.idle_timeout
= rule
->idle_timeout
;
2233 fu
.hard_timeout
= rule
->hard_timeout
;
2234 ovs_mutex_unlock(&rule
->mutex
);
2236 if (flags
& NXFMF_ACTIONS
) {
2237 const struct rule_actions
*actions
= rule_get_actions(rule
);
2238 fu
.ofpacts
= actions
->ofpacts
;
2239 fu
.ofpacts_len
= actions
->ofpacts_len
;
2244 ofputil_append_flow_update(&fu
, &ofconn
->updates
,
2245 ofproto_get_tun_tab(rule
->ofproto
));
2246 } else if (!ofconn
->sent_abbrev_update
) {
2247 struct ofputil_flow_update fu
;
2249 fu
.event
= NXFME_ABBREV
;
2250 fu
.xid
= abbrev_xid
;
2251 ofputil_append_flow_update(&fu
, &ofconn
->updates
,
2252 ofproto_get_tun_tab(rule
->ofproto
));
2254 ofconn
->sent_abbrev_update
= true;
2261 ofmonitor_flush(struct connmgr
*mgr
)
2262 OVS_REQUIRES(ofproto_mutex
)
2264 struct ofconn
*ofconn
;
2266 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
2267 struct rconn_packet_counter
*counter
= ofconn
->monitor_counter
;
2270 LIST_FOR_EACH_POP (msg
, list_node
, &ofconn
->updates
) {
2271 ofconn_send(ofconn
, msg
, counter
);
2274 if (!ofconn
->monitor_paused
2275 && rconn_packet_counter_n_bytes(counter
) > 128 * 1024) {
2276 struct ofpbuf
*pause
;
2278 COVERAGE_INC(ofmonitor_pause
);
2279 ofconn
->monitor_paused
= monitor_seqno
++;
2280 pause
= ofpraw_alloc_xid(OFPRAW_NXT_FLOW_MONITOR_PAUSED
,
2281 OFP10_VERSION
, htonl(0), 0);
2282 ofconn_send(ofconn
, pause
, counter
);
2288 ofmonitor_resume(struct ofconn
*ofconn
)
2289 OVS_REQUIRES(ofproto_mutex
)
2291 struct rule_collection rules
;
2292 struct ofpbuf
*resumed
;
2293 struct ofmonitor
*m
;
2294 struct ovs_list msgs
;
2296 rule_collection_init(&rules
);
2297 HMAP_FOR_EACH (m
, ofconn_node
, &ofconn
->monitors
) {
2298 ofmonitor_collect_resume_rules(m
, ofconn
->monitor_paused
, &rules
);
2301 ovs_list_init(&msgs
);
2302 ofmonitor_compose_refresh_updates(&rules
, &msgs
);
2304 resumed
= ofpraw_alloc_xid(OFPRAW_NXT_FLOW_MONITOR_RESUMED
, OFP10_VERSION
,
2306 ovs_list_push_back(&msgs
, &resumed
->list_node
);
2307 ofconn_send_replies(ofconn
, &msgs
);
2309 ofconn
->monitor_paused
= 0;
2313 ofmonitor_may_resume(const struct ofconn
*ofconn
)
2314 OVS_REQUIRES(ofproto_mutex
)
2316 return (ofconn
->monitor_paused
!= 0
2317 && !rconn_packet_counter_n_packets(ofconn
->monitor_counter
));
2321 ofmonitor_run(struct connmgr
*mgr
)
2323 struct ofconn
*ofconn
;
2325 ovs_mutex_lock(&ofproto_mutex
);
2326 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
2327 if (ofmonitor_may_resume(ofconn
)) {
2328 COVERAGE_INC(ofmonitor_resume
);
2329 ofmonitor_resume(ofconn
);
2332 ovs_mutex_unlock(&ofproto_mutex
);
2336 ofmonitor_wait(struct connmgr
*mgr
)
2338 struct ofconn
*ofconn
;
2340 ovs_mutex_lock(&ofproto_mutex
);
2341 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
2342 if (ofmonitor_may_resume(ofconn
)) {
2343 poll_immediate_wake();
2346 ovs_mutex_unlock(&ofproto_mutex
);
2350 ofproto_async_msg_free(struct ofproto_async_msg
*am
)
2352 free(am
->pin
.up
.base
.packet
);
2353 free(am
->pin
.up
.base
.userdata
);
2354 free(am
->pin
.up
.stack
);
2355 free(am
->pin
.up
.actions
);
2356 free(am
->pin
.up
.action_set
);