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-util.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"
45 VLOG_DEFINE_THIS_MODULE(connmgr
);
46 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
48 /* An OpenFlow connection.
54 * 'ofproto_mutex' must be held whenever an ofconn is created or destroyed or,
55 * more or less equivalently, whenever an ofconn is added to or removed from a
56 * connmgr. 'ofproto_mutex' doesn't protect the data inside the ofconn, except
57 * as specifically noted below. */
59 /* Configuration that persists from one connection to the next. */
61 struct ovs_list node
; /* In struct connmgr's "all_conns" list. */
62 struct hmap_node hmap_node
; /* In struct connmgr's "controllers" map. */
64 struct connmgr
*connmgr
; /* Connection's manager. */
65 struct rconn
*rconn
; /* OpenFlow connection. */
66 enum ofconn_type type
; /* Type. */
67 enum ofproto_band band
; /* In-band or out-of-band? */
68 bool enable_async_msgs
; /* Initially enable async messages? */
69 bool want_packet_in_on_miss
;
71 /* State that should be cleared from one connection to the next. */
74 enum ofp12_controller_role role
; /* Role. */
75 enum ofputil_protocol protocol
; /* Current protocol variant. */
76 enum nx_packet_in_format packet_in_format
; /* OFPT_PACKET_IN format. */
78 /* OFPT_PACKET_IN related data. */
79 struct rconn_packet_counter
*packet_in_counter
; /* # queued on 'rconn'. */
80 #define N_SCHEDULERS 2
81 struct pinsched
*schedulers
[N_SCHEDULERS
];
82 int miss_send_len
; /* Bytes to send of buffered packets. */
83 uint16_t controller_id
; /* Connection controller ID. */
85 /* Number of OpenFlow messages queued on 'rconn' as replies to OpenFlow
86 * requests, and the maximum number before we stop reading OpenFlow
88 #define OFCONN_REPLY_MAX 100
89 struct rconn_packet_counter
*reply_counter
;
91 /* Asynchronous message configuration in each possible role.
93 * A 1-bit enables sending an asynchronous message for one possible reason
94 * that the message might be generated, a 0-bit disables it. */
95 struct ofputil_async_cfg
*async_cfg
;
97 /* Flow table operation logging. */
98 int n_add
, n_delete
, n_modify
; /* Number of unreported ops of each kind. */
99 long long int first_op
, last_op
; /* Range of times for unreported ops. */
100 long long int next_op_report
; /* Time to report ops, or LLONG_MAX. */
101 long long int op_backoff
; /* Earliest time to report ops again. */
103 /* Flow monitors (e.g. NXST_FLOW_MONITOR). */
105 /* Configuration. Contains "struct ofmonitor"s. */
106 struct hmap monitors
OVS_GUARDED_BY(ofproto_mutex
);
110 * When too many flow monitor notifications back up in the transmit buffer,
111 * we pause the transmission of further notifications. These members track
112 * the flow control state.
114 * When notifications are flowing, 'monitor_paused' is 0. When
115 * notifications are paused, 'monitor_paused' is the value of
116 * 'monitor_seqno' at the point we paused.
118 * 'monitor_counter' counts the OpenFlow messages and bytes currently in
119 * flight. This value growing too large triggers pausing. */
120 uint64_t monitor_paused
OVS_GUARDED_BY(ofproto_mutex
);
121 struct rconn_packet_counter
*monitor_counter
OVS_GUARDED_BY(ofproto_mutex
);
123 /* State of monitors for a single ongoing flow_mod.
125 * 'updates' is a list of "struct ofpbuf"s that contain
126 * NXST_FLOW_MONITOR_REPLY messages representing the changes made by the
129 * When 'updates' is nonempty, 'sent_abbrev_update' is true if 'updates'
130 * contains an update event of type NXFME_ABBREV and false otherwise.. */
131 struct ovs_list updates
OVS_GUARDED_BY(ofproto_mutex
);
132 bool sent_abbrev_update
OVS_GUARDED_BY(ofproto_mutex
);
134 /* Active bundles. Contains "struct ofp_bundle"s. */
136 long long int next_bundle_expiry_check
;
139 /* vswitchd/ovs-vswitchd.8.in documents the value of BUNDLE_IDLE_LIFETIME in
140 * seconds. That documentation must be kept in sync with the value below. */
142 BUNDLE_EXPIRY_INTERVAL
= 1000, /* Check bundle expiry every 1 sec. */
143 BUNDLE_IDLE_TIMEOUT
= 10000, /* Expire idle bundles after 10 seconds. */
146 static struct ofconn
*ofconn_create(struct connmgr
*, struct rconn
*,
147 enum ofconn_type
, bool enable_async_msgs
)
148 OVS_REQUIRES(ofproto_mutex
);
149 static void ofconn_destroy(struct ofconn
*) OVS_REQUIRES(ofproto_mutex
);
150 static void ofconn_flush(struct ofconn
*) OVS_REQUIRES(ofproto_mutex
);
152 static void ofconn_reconfigure(struct ofconn
*,
153 const struct ofproto_controller
*);
155 static void ofconn_run(struct ofconn
*,
156 void (*handle_openflow
)(struct ofconn
*,
157 const struct ofpbuf
*ofp_msg
));
158 static void ofconn_wait(struct ofconn
*);
160 static void ofconn_log_flow_mods(struct ofconn
*);
162 static const char *ofconn_get_target(const struct ofconn
*);
163 static char *ofconn_make_name(const struct connmgr
*, const char *target
);
165 static void ofconn_set_rate_limit(struct ofconn
*, int rate
, int burst
);
167 static void ofconn_send(const struct ofconn
*, struct ofpbuf
*,
168 struct rconn_packet_counter
*);
170 static void do_send_packet_ins(struct ofconn
*, struct ovs_list
*txq
);
172 /* A listener for incoming OpenFlow "service" connections. */
174 struct hmap_node node
; /* In struct connmgr's "services" hmap. */
175 struct pvconn
*pvconn
; /* OpenFlow connection listener. */
177 /* These are not used by ofservice directly. They are settings for
178 * accepted "struct ofconn"s from the pvconn. */
179 int probe_interval
; /* Max idle time before probing, in seconds. */
180 int rate_limit
; /* Max packet-in rate in packets per second. */
181 int burst_limit
; /* Limit on accumulating packet credits. */
182 bool enable_async_msgs
; /* Initially enable async messages? */
183 uint8_t dscp
; /* DSCP Value for controller connection */
184 uint32_t allowed_versions
; /* OpenFlow protocol versions that may
185 * be negotiated for a session. */
188 static void ofservice_reconfigure(struct ofservice
*,
189 const struct ofproto_controller
*);
190 static int ofservice_create(struct connmgr
*mgr
, const char *target
,
191 uint32_t allowed_versions
, uint8_t dscp
);
192 static void ofservice_destroy(struct connmgr
*, struct ofservice
*);
193 static struct ofservice
*ofservice_lookup(struct connmgr
*,
196 /* Connection manager for an OpenFlow switch. */
198 struct ofproto
*ofproto
;
200 char *local_port_name
;
202 /* OpenFlow connections. */
203 struct hmap controllers
; /* All OFCONN_PRIMARY controllers. */
204 struct ovs_list all_conns
; /* All controllers. All modifications are
205 protected by ofproto_mutex, so that any
206 traversals from other threads can be made
207 safe by holding the ofproto_mutex. */
208 uint64_t master_election_id
; /* monotonically increasing sequence number
209 * for master election */
210 bool master_election_id_defined
;
212 /* OpenFlow listeners. */
213 struct hmap services
; /* Contains "struct ofservice"s. */
214 struct pvconn
**snoops
;
218 struct fail_open
*fail_open
;
219 enum ofproto_fail_mode fail_mode
;
221 /* In-band control. */
222 struct in_band
*in_band
;
223 struct sockaddr_in
*extra_in_band_remotes
;
224 size_t n_extra_remotes
;
227 ATOMIC(int) want_packet_in_on_miss
; /* Sum of ofconns' values. */
230 static void update_in_band_remotes(struct connmgr
*);
231 static void add_snooper(struct connmgr
*, struct vconn
*);
232 static void ofmonitor_run(struct connmgr
*);
233 static void ofmonitor_wait(struct connmgr
*);
235 /* Creates and returns a new connection manager owned by 'ofproto'. 'name' is
236 * a name for the ofproto suitable for using in log messages.
237 * 'local_port_name' is the name of the local port (OFPP_LOCAL) within
240 connmgr_create(struct ofproto
*ofproto
,
241 const char *name
, const char *local_port_name
)
245 mgr
= xmalloc(sizeof *mgr
);
246 mgr
->ofproto
= ofproto
;
247 mgr
->name
= xstrdup(name
);
248 mgr
->local_port_name
= xstrdup(local_port_name
);
250 hmap_init(&mgr
->controllers
);
251 ovs_list_init(&mgr
->all_conns
);
252 mgr
->master_election_id
= 0;
253 mgr
->master_election_id_defined
= false;
255 hmap_init(&mgr
->services
);
259 mgr
->fail_open
= NULL
;
260 mgr
->fail_mode
= OFPROTO_FAIL_SECURE
;
263 mgr
->extra_in_band_remotes
= NULL
;
264 mgr
->n_extra_remotes
= 0;
265 mgr
->in_band_queue
= -1;
267 atomic_init(&mgr
->want_packet_in_on_miss
, 0);
272 /* The default "table-miss" behaviour for OpenFlow1.3+ is to drop the
273 * packet rather than to send the packet to the controller.
275 * This function maintains the count of pre-OpenFlow1.3 with controller_id 0,
276 * as we assume these are the controllers that should receive "table-miss"
279 update_want_packet_in_on_miss(struct ofconn
*ofconn
)
281 /* We want a packet-in on miss when controller_id is zero and OpenFlow is
282 * lower than version 1.3. */
283 enum ofputil_protocol p
= ofconn
->protocol
;
284 int new_want
= (ofconn
->controller_id
== 0 &&
285 (p
== OFPUTIL_P_NONE
||
286 ofputil_protocol_to_ofp_version(p
) < OFP13_VERSION
));
288 /* Update the setting and the count if necessary. */
289 int old_want
= ofconn
->want_packet_in_on_miss
;
290 if (old_want
!= new_want
) {
291 atomic_int
*dst
= &ofconn
->connmgr
->want_packet_in_on_miss
;
293 atomic_read_relaxed(dst
, &count
);
294 atomic_store_relaxed(dst
, count
- old_want
+ new_want
);
296 ofconn
->want_packet_in_on_miss
= new_want
;
300 /* Frees 'mgr' and all of its resources. */
302 connmgr_destroy(struct connmgr
*mgr
)
303 OVS_REQUIRES(ofproto_mutex
)
305 struct ofservice
*ofservice
, *next_ofservice
;
306 struct ofconn
*ofconn
, *next_ofconn
;
313 LIST_FOR_EACH_SAFE (ofconn
, next_ofconn
, node
, &mgr
->all_conns
) {
314 ofconn_destroy(ofconn
);
317 hmap_destroy(&mgr
->controllers
);
319 HMAP_FOR_EACH_SAFE (ofservice
, next_ofservice
, node
, &mgr
->services
) {
320 ofservice_destroy(mgr
, ofservice
);
322 hmap_destroy(&mgr
->services
);
324 for (i
= 0; i
< mgr
->n_snoops
; i
++) {
325 pvconn_close(mgr
->snoops
[i
]);
329 fail_open_destroy(mgr
->fail_open
);
330 mgr
->fail_open
= NULL
;
332 in_band_destroy(mgr
->in_band
);
334 free(mgr
->extra_in_band_remotes
);
336 free(mgr
->local_port_name
);
341 /* Does all of the periodic maintenance required by 'mgr'. Calls
342 * 'handle_openflow' for each message received on an OpenFlow connection,
343 * passing along the OpenFlow connection itself and the message that was sent.
344 * 'handle_openflow' must not modify or free the message. */
346 connmgr_run(struct connmgr
*mgr
,
347 void (*handle_openflow
)(struct ofconn
*,
348 const struct ofpbuf
*ofp_msg
))
349 OVS_EXCLUDED(ofproto_mutex
)
351 struct ofconn
*ofconn
, *next_ofconn
;
352 struct ofservice
*ofservice
;
356 if (!in_band_run(mgr
->in_band
)) {
357 in_band_destroy(mgr
->in_band
);
362 LIST_FOR_EACH_SAFE (ofconn
, next_ofconn
, node
, &mgr
->all_conns
) {
363 ofconn_run(ofconn
, handle_openflow
);
367 /* Fail-open maintenance. Do this after processing the ofconns since
368 * fail-open checks the status of the controller rconn. */
369 if (mgr
->fail_open
) {
370 fail_open_run(mgr
->fail_open
);
373 HMAP_FOR_EACH (ofservice
, node
, &mgr
->services
) {
377 retval
= pvconn_accept(ofservice
->pvconn
, &vconn
);
382 /* Passing default value for creation of the rconn */
383 rconn
= rconn_create(ofservice
->probe_interval
, 0, ofservice
->dscp
,
384 vconn_get_allowed_versions(vconn
));
385 name
= ofconn_make_name(mgr
, vconn_get_name(vconn
));
386 rconn_connect_unreliably(rconn
, vconn
, name
);
389 ovs_mutex_lock(&ofproto_mutex
);
390 ofconn
= ofconn_create(mgr
, rconn
, OFCONN_SERVICE
,
391 ofservice
->enable_async_msgs
);
392 ovs_mutex_unlock(&ofproto_mutex
);
394 ofconn_set_rate_limit(ofconn
, ofservice
->rate_limit
,
395 ofservice
->burst_limit
);
396 } else if (retval
!= EAGAIN
) {
397 VLOG_WARN_RL(&rl
, "accept failed (%s)", ovs_strerror(retval
));
401 for (i
= 0; i
< mgr
->n_snoops
; i
++) {
405 retval
= pvconn_accept(mgr
->snoops
[i
], &vconn
);
407 add_snooper(mgr
, vconn
);
408 } else if (retval
!= EAGAIN
) {
409 VLOG_WARN_RL(&rl
, "accept failed (%s)", ovs_strerror(retval
));
414 /* Causes the poll loop to wake up when connmgr_run() needs to run. */
416 connmgr_wait(struct connmgr
*mgr
)
418 struct ofservice
*ofservice
;
419 struct ofconn
*ofconn
;
422 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
427 in_band_wait(mgr
->in_band
);
429 if (mgr
->fail_open
) {
430 fail_open_wait(mgr
->fail_open
);
432 HMAP_FOR_EACH (ofservice
, node
, &mgr
->services
) {
433 pvconn_wait(ofservice
->pvconn
);
435 for (i
= 0; i
< mgr
->n_snoops
; i
++) {
436 pvconn_wait(mgr
->snoops
[i
]);
440 /* Adds some memory usage statistics for 'mgr' into 'usage', for use with
441 * memory_report(). */
443 connmgr_get_memory_usage(const struct connmgr
*mgr
, struct simap
*usage
)
445 const struct ofconn
*ofconn
;
446 unsigned int packets
= 0;
447 unsigned int ofconns
= 0;
449 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
454 packets
+= rconn_count_txqlen(ofconn
->rconn
);
455 for (i
= 0; i
< N_SCHEDULERS
; i
++) {
456 struct pinsched_stats stats
;
458 pinsched_get_stats(ofconn
->schedulers
[i
], &stats
);
459 packets
+= stats
.n_queued
;
462 simap_increase(usage
, "ofconns", ofconns
);
463 simap_increase(usage
, "packets", packets
);
466 /* Returns the ofproto that owns 'ofconn''s connmgr. */
468 ofconn_get_ofproto(const struct ofconn
*ofconn
)
470 return ofconn
->connmgr
->ofproto
;
473 /* OpenFlow configuration. */
475 static void add_controller(struct connmgr
*, const char *target
, uint8_t dscp
,
476 uint32_t allowed_versions
)
477 OVS_REQUIRES(ofproto_mutex
);
478 static struct ofconn
*find_controller_by_target(struct connmgr
*,
480 static void update_fail_open(struct connmgr
*) OVS_EXCLUDED(ofproto_mutex
);
481 static int set_pvconns(struct pvconn
***pvconnsp
, size_t *n_pvconnsp
,
482 const struct sset
*);
484 /* Returns true if 'mgr' has any configured primary controllers.
486 * Service controllers do not count, but configured primary controllers do
487 * count whether or not they are currently connected. */
489 connmgr_has_controllers(const struct connmgr
*mgr
)
491 return !hmap_is_empty(&mgr
->controllers
);
494 /* Initializes 'info' and populates it with information about each configured
495 * primary controller. The keys in 'info' are the controllers' targets; the
496 * data values are corresponding "struct ofproto_controller_info".
498 * The caller owns 'info' and everything in it and should free it when it is no
501 connmgr_get_controller_info(struct connmgr
*mgr
, struct shash
*info
)
503 const struct ofconn
*ofconn
;
505 HMAP_FOR_EACH (ofconn
, hmap_node
, &mgr
->controllers
) {
506 const struct rconn
*rconn
= ofconn
->rconn
;
507 const char *target
= rconn_get_target(rconn
);
509 if (!shash_find(info
, target
)) {
510 struct ofproto_controller_info
*cinfo
= xmalloc(sizeof *cinfo
);
511 time_t now
= time_now();
512 time_t last_connection
= rconn_get_last_connection(rconn
);
513 time_t last_disconnect
= rconn_get_last_disconnect(rconn
);
514 int last_error
= rconn_get_last_error(rconn
);
517 shash_add(info
, target
, cinfo
);
519 cinfo
->is_connected
= rconn_is_connected(rconn
);
520 cinfo
->role
= ofconn
->role
;
522 smap_init(&cinfo
->pairs
);
524 smap_add(&cinfo
->pairs
, "last_error",
525 ovs_retval_to_string(last_error
));
528 smap_add(&cinfo
->pairs
, "state", rconn_get_state(rconn
));
530 if (last_connection
!= TIME_MIN
) {
531 smap_add_format(&cinfo
->pairs
, "sec_since_connect",
532 "%ld", (long int) (now
- last_connection
));
535 if (last_disconnect
!= TIME_MIN
) {
536 smap_add_format(&cinfo
->pairs
, "sec_since_disconnect",
537 "%ld", (long int) (now
- last_disconnect
));
540 for (i
= 0; i
< N_SCHEDULERS
; i
++) {
541 if (ofconn
->schedulers
[i
]) {
542 const char *name
= i
? "miss" : "action";
543 struct pinsched_stats stats
;
545 pinsched_get_stats(ofconn
->schedulers
[i
], &stats
);
546 smap_add_nocopy(&cinfo
->pairs
,
547 xasprintf("packet-in-%s-backlog", name
),
548 xasprintf("%u", stats
.n_queued
));
549 smap_add_nocopy(&cinfo
->pairs
,
550 xasprintf("packet-in-%s-bypassed", name
),
551 xasprintf("%llu", stats
.n_normal
));
552 smap_add_nocopy(&cinfo
->pairs
,
553 xasprintf("packet-in-%s-queued", name
),
554 xasprintf("%llu", stats
.n_limited
));
555 smap_add_nocopy(&cinfo
->pairs
,
556 xasprintf("packet-in-%s-dropped", name
),
557 xasprintf("%llu", stats
.n_queue_dropped
));
565 connmgr_free_controller_info(struct shash
*info
)
567 struct shash_node
*node
;
569 SHASH_FOR_EACH (node
, info
) {
570 struct ofproto_controller_info
*cinfo
= node
->data
;
571 smap_destroy(&cinfo
->pairs
);
577 /* Changes 'mgr''s set of controllers to the 'n_controllers' controllers in
580 connmgr_set_controllers(struct connmgr
*mgr
,
581 const struct ofproto_controller
*controllers
,
582 size_t n_controllers
, uint32_t allowed_versions
)
583 OVS_EXCLUDED(ofproto_mutex
)
585 bool had_controllers
= connmgr_has_controllers(mgr
);
586 struct shash new_controllers
;
587 struct ofconn
*ofconn
, *next_ofconn
;
588 struct ofservice
*ofservice
, *next_ofservice
;
591 /* Required to add and remove ofconns. This could probably be narrowed to
592 * cover a smaller amount of code, if that yielded some benefit. */
593 ovs_mutex_lock(&ofproto_mutex
);
595 /* Create newly configured controllers and services.
596 * Create a name to ofproto_controller mapping in 'new_controllers'. */
597 shash_init(&new_controllers
);
598 for (i
= 0; i
< n_controllers
; i
++) {
599 const struct ofproto_controller
*c
= &controllers
[i
];
601 if (!vconn_verify_name(c
->target
)) {
603 ofconn
= find_controller_by_target(mgr
, c
->target
);
605 VLOG_INFO("%s: added primary controller \"%s\"",
606 mgr
->name
, c
->target
);
608 } else if (rconn_get_allowed_versions(ofconn
->rconn
) !=
610 VLOG_INFO("%s: re-added primary controller \"%s\"",
611 mgr
->name
, c
->target
);
613 ofconn_destroy(ofconn
);
616 add_controller(mgr
, c
->target
, c
->dscp
, allowed_versions
);
618 } else if (!pvconn_verify_name(c
->target
)) {
620 ofservice
= ofservice_lookup(mgr
, c
->target
);
622 VLOG_INFO("%s: added service controller \"%s\"",
623 mgr
->name
, c
->target
);
625 } else if (ofservice
->allowed_versions
!= allowed_versions
) {
626 VLOG_INFO("%s: re-added service controller \"%s\"",
627 mgr
->name
, c
->target
);
628 ofservice_destroy(mgr
, ofservice
);
632 ofservice_create(mgr
, c
->target
, allowed_versions
, c
->dscp
);
635 VLOG_WARN_RL(&rl
, "%s: unsupported controller \"%s\"",
636 mgr
->name
, c
->target
);
640 shash_add_once(&new_controllers
, c
->target
, &controllers
[i
]);
643 /* Delete controllers that are no longer configured.
644 * Update configuration of all now-existing controllers. */
645 HMAP_FOR_EACH_SAFE (ofconn
, next_ofconn
, hmap_node
, &mgr
->controllers
) {
646 const char *target
= ofconn_get_target(ofconn
);
647 struct ofproto_controller
*c
;
649 c
= shash_find_data(&new_controllers
, target
);
651 VLOG_INFO("%s: removed primary controller \"%s\"",
653 ofconn_destroy(ofconn
);
655 ofconn_reconfigure(ofconn
, c
);
659 /* Delete services that are no longer configured.
660 * Update configuration of all now-existing services. */
661 HMAP_FOR_EACH_SAFE (ofservice
, next_ofservice
, node
, &mgr
->services
) {
662 const char *target
= pvconn_get_name(ofservice
->pvconn
);
663 struct ofproto_controller
*c
;
665 c
= shash_find_data(&new_controllers
, target
);
667 VLOG_INFO("%s: removed service controller \"%s\"",
669 ofservice_destroy(mgr
, ofservice
);
671 ofservice_reconfigure(ofservice
, c
);
675 shash_destroy(&new_controllers
);
677 ovs_mutex_unlock(&ofproto_mutex
);
679 update_in_band_remotes(mgr
);
680 update_fail_open(mgr
);
681 if (had_controllers
!= connmgr_has_controllers(mgr
)) {
682 ofproto_flush_flows(mgr
->ofproto
);
686 /* Drops the connections between 'mgr' and all of its primary and secondary
687 * controllers, forcing them to reconnect. */
689 connmgr_reconnect(const struct connmgr
*mgr
)
691 struct ofconn
*ofconn
;
693 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
694 rconn_reconnect(ofconn
->rconn
);
698 /* Sets the "snoops" for 'mgr' to the pvconn targets listed in 'snoops'.
700 * A "snoop" is a pvconn to which every OpenFlow message to or from the most
701 * important controller on 'mgr' is mirrored. */
703 connmgr_set_snoops(struct connmgr
*mgr
, const struct sset
*snoops
)
705 return set_pvconns(&mgr
->snoops
, &mgr
->n_snoops
, snoops
);
708 /* Adds each of the snoops currently configured on 'mgr' to 'snoops'. */
710 connmgr_get_snoops(const struct connmgr
*mgr
, struct sset
*snoops
)
714 for (i
= 0; i
< mgr
->n_snoops
; i
++) {
715 sset_add(snoops
, pvconn_get_name(mgr
->snoops
[i
]));
719 /* Returns true if 'mgr' has at least one snoop, false if it has none. */
721 connmgr_has_snoops(const struct connmgr
*mgr
)
723 return mgr
->n_snoops
> 0;
726 /* Creates a new controller for 'target' in 'mgr'. update_controller() needs
727 * to be called later to finish the new ofconn's configuration. */
729 add_controller(struct connmgr
*mgr
, const char *target
, uint8_t dscp
,
730 uint32_t allowed_versions
)
731 OVS_REQUIRES(ofproto_mutex
)
733 char *name
= ofconn_make_name(mgr
, target
);
734 struct ofconn
*ofconn
;
736 ofconn
= ofconn_create(mgr
, rconn_create(5, 8, dscp
, allowed_versions
),
737 OFCONN_PRIMARY
, true);
738 rconn_connect(ofconn
->rconn
, target
, name
);
739 hmap_insert(&mgr
->controllers
, &ofconn
->hmap_node
, hash_string(target
, 0));
744 static struct ofconn
*
745 find_controller_by_target(struct connmgr
*mgr
, const char *target
)
747 struct ofconn
*ofconn
;
749 HMAP_FOR_EACH_WITH_HASH (ofconn
, hmap_node
,
750 hash_string(target
, 0), &mgr
->controllers
) {
751 if (!strcmp(ofconn_get_target(ofconn
), target
)) {
759 update_in_band_remotes(struct connmgr
*mgr
)
761 struct sockaddr_in
*addrs
;
762 size_t max_addrs
, n_addrs
;
763 struct ofconn
*ofconn
;
766 /* Allocate enough memory for as many remotes as we could possibly have. */
767 max_addrs
= mgr
->n_extra_remotes
+ hmap_count(&mgr
->controllers
);
768 addrs
= xmalloc(max_addrs
* sizeof *addrs
);
771 /* Add all the remotes. */
772 HMAP_FOR_EACH (ofconn
, hmap_node
, &mgr
->controllers
) {
773 const char *target
= rconn_get_target(ofconn
->rconn
);
775 struct sockaddr_storage ss
;
776 struct sockaddr_in in
;
779 if (ofconn
->band
== OFPROTO_IN_BAND
780 && stream_parse_target_with_default_port(target
, OFP_PORT
, &sa
.ss
)
781 && sa
.ss
.ss_family
== AF_INET
) {
782 addrs
[n_addrs
++] = sa
.in
;
785 for (i
= 0; i
< mgr
->n_extra_remotes
; i
++) {
786 addrs
[n_addrs
++] = mgr
->extra_in_band_remotes
[i
];
789 /* Create or update or destroy in-band. */
792 in_band_create(mgr
->ofproto
, mgr
->local_port_name
, &mgr
->in_band
);
795 /* in_band_run() needs a chance to delete any existing in-band flows.
796 * We will destroy mgr->in_band after it's done with that. */
799 in_band_set_queue(mgr
->in_band
, mgr
->in_band_queue
);
800 in_band_set_remotes(mgr
->in_band
, addrs
, n_addrs
);
808 update_fail_open(struct connmgr
*mgr
)
809 OVS_EXCLUDED(ofproto_mutex
)
811 if (connmgr_has_controllers(mgr
)
812 && mgr
->fail_mode
== OFPROTO_FAIL_STANDALONE
) {
813 if (!mgr
->fail_open
) {
814 mgr
->fail_open
= fail_open_create(mgr
->ofproto
, mgr
);
817 ovs_mutex_lock(&ofproto_mutex
);
818 fail_open_destroy(mgr
->fail_open
);
819 ovs_mutex_unlock(&ofproto_mutex
);
820 mgr
->fail_open
= NULL
;
825 set_pvconns(struct pvconn
***pvconnsp
, size_t *n_pvconnsp
,
826 const struct sset
*sset
)
828 struct pvconn
**pvconns
= *pvconnsp
;
829 size_t n_pvconns
= *n_pvconnsp
;
834 for (i
= 0; i
< n_pvconns
; i
++) {
835 pvconn_close(pvconns
[i
]);
839 pvconns
= xmalloc(sset_count(sset
) * sizeof *pvconns
);
841 SSET_FOR_EACH (name
, sset
) {
842 struct pvconn
*pvconn
;
844 error
= pvconn_open(name
, 0, 0, &pvconn
);
846 pvconns
[n_pvconns
++] = pvconn
;
848 VLOG_ERR("failed to listen on %s: %s", name
, ovs_strerror(error
));
856 *n_pvconnsp
= n_pvconns
;
861 /* Returns a "preference level" for snooping 'ofconn'. A higher return value
862 * means that 'ofconn' is more interesting for monitoring than a lower return
865 snoop_preference(const struct ofconn
*ofconn
)
867 switch (ofconn
->role
) {
868 case OFPCR12_ROLE_MASTER
:
870 case OFPCR12_ROLE_EQUAL
:
872 case OFPCR12_ROLE_SLAVE
:
874 case OFPCR12_ROLE_NOCHANGE
:
876 /* Shouldn't happen. */
881 /* One of 'mgr''s "snoop" pvconns has accepted a new connection on 'vconn'.
882 * Connects this vconn to a controller. */
884 add_snooper(struct connmgr
*mgr
, struct vconn
*vconn
)
886 struct ofconn
*ofconn
, *best
;
888 /* Pick a controller for monitoring. */
890 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
891 if (ofconn
->type
== OFCONN_PRIMARY
892 && (!best
|| snoop_preference(ofconn
) > snoop_preference(best
))) {
898 rconn_add_monitor(best
->rconn
, vconn
);
900 VLOG_INFO_RL(&rl
, "no controller connection to snoop");
905 /* Public ofconn functions. */
907 /* Returns the connection type, either OFCONN_PRIMARY or OFCONN_SERVICE. */
909 ofconn_get_type(const struct ofconn
*ofconn
)
914 /* If a master election id is defined, stores it into '*idp' and returns
915 * true. Otherwise, stores UINT64_MAX into '*idp' and returns false. */
917 ofconn_get_master_election_id(const struct ofconn
*ofconn
, uint64_t *idp
)
919 *idp
= (ofconn
->connmgr
->master_election_id_defined
920 ? ofconn
->connmgr
->master_election_id
922 return ofconn
->connmgr
->master_election_id_defined
;
925 /* Sets the master election id.
927 * Returns true if successful, false if the id is stale
930 ofconn_set_master_election_id(struct ofconn
*ofconn
, uint64_t id
)
932 if (ofconn
->connmgr
->master_election_id_defined
934 /* Unsigned difference interpreted as a two's complement signed
936 (int64_t)(id
- ofconn
->connmgr
->master_election_id
) < 0) {
939 ofconn
->connmgr
->master_election_id
= id
;
940 ofconn
->connmgr
->master_election_id_defined
= true;
945 /* Returns the role configured for 'ofconn'.
947 * The default role, if no other role has been set, is OFPCR12_ROLE_EQUAL. */
948 enum ofp12_controller_role
949 ofconn_get_role(const struct ofconn
*ofconn
)
955 ofconn_send_role_status(struct ofconn
*ofconn
, uint32_t role
, uint8_t reason
)
957 struct ofputil_role_status status
;
960 status
.reason
= reason
;
962 ofconn_get_master_election_id(ofconn
, &status
.generation_id
);
964 buf
= ofputil_encode_role_status(&status
, ofconn_get_protocol(ofconn
));
966 ofconn_send(ofconn
, buf
, NULL
);
970 /* Changes 'ofconn''s role to 'role'. If 'role' is OFPCR12_ROLE_MASTER then
971 * any existing master is demoted to a slave. */
973 ofconn_set_role(struct ofconn
*ofconn
, enum ofp12_controller_role role
)
975 if (role
!= ofconn
->role
&& role
== OFPCR12_ROLE_MASTER
) {
976 struct ofconn
*other
;
978 LIST_FOR_EACH (other
, node
, &ofconn
->connmgr
->all_conns
) {
979 if (other
->role
== OFPCR12_ROLE_MASTER
) {
980 other
->role
= OFPCR12_ROLE_SLAVE
;
981 ofconn_send_role_status(other
, OFPCR12_ROLE_SLAVE
, OFPCRR_MASTER_REQUEST
);
989 ofconn_set_invalid_ttl_to_controller(struct ofconn
*ofconn
, bool enable
)
991 struct ofputil_async_cfg ac
= ofconn_get_async_config(ofconn
);
992 uint32_t bit
= 1u << OFPR_INVALID_TTL
;
994 ac
.master
[OAM_PACKET_IN
] |= bit
;
996 ac
.master
[OAM_PACKET_IN
] &= ~bit
;
998 ofconn_set_async_config(ofconn
, &ac
);
1002 ofconn_get_invalid_ttl_to_controller(struct ofconn
*ofconn
)
1004 struct ofputil_async_cfg ac
= ofconn_get_async_config(ofconn
);
1005 uint32_t bit
= 1u << OFPR_INVALID_TTL
;
1006 return (ac
.master
[OAM_PACKET_IN
] & bit
) != 0;
1009 /* Returns the currently configured protocol for 'ofconn', one of OFPUTIL_P_*.
1011 * Returns OFPUTIL_P_NONE, which is not a valid protocol, if 'ofconn' hasn't
1012 * completed version negotiation. This can't happen if at least one OpenFlow
1013 * message, other than OFPT_HELLO, has been received on the connection (such as
1014 * in ofproto.c's message handling code), since version negotiation is a
1015 * prerequisite for starting to receive messages. This means that
1016 * OFPUTIL_P_NONE is a special case that most callers need not worry about. */
1017 enum ofputil_protocol
1018 ofconn_get_protocol(const struct ofconn
*ofconn
)
1020 if (ofconn
->protocol
== OFPUTIL_P_NONE
&&
1021 rconn_is_connected(ofconn
->rconn
)) {
1022 int version
= rconn_get_version(ofconn
->rconn
);
1024 ofconn_set_protocol(CONST_CAST(struct ofconn
*, ofconn
),
1025 ofputil_protocol_from_ofp_version(version
));
1029 return ofconn
->protocol
;
1032 /* Sets the protocol for 'ofconn' to 'protocol' (one of OFPUTIL_P_*).
1034 * (This doesn't actually send anything to accomplish this. Presumably the
1035 * caller already did that.) */
1037 ofconn_set_protocol(struct ofconn
*ofconn
, enum ofputil_protocol protocol
)
1039 ofconn
->protocol
= protocol
;
1040 update_want_packet_in_on_miss(ofconn
);
1043 /* Returns the currently configured packet in format for 'ofconn', one of
1046 * The default, if no other format has been set, is NXPIF_STANDARD. */
1047 enum nx_packet_in_format
1048 ofconn_get_packet_in_format(struct ofconn
*ofconn
)
1050 return ofconn
->packet_in_format
;
1053 /* Sets the packet in format for 'ofconn' to 'packet_in_format' (one of
1056 ofconn_set_packet_in_format(struct ofconn
*ofconn
,
1057 enum nx_packet_in_format packet_in_format
)
1059 ofconn
->packet_in_format
= packet_in_format
;
1062 /* Sets the controller connection ID for 'ofconn' to 'controller_id'.
1064 * The connection controller ID is used for OFPP_CONTROLLER and
1065 * NXAST_CONTROLLER actions. See "struct nx_action_controller" for details. */
1067 ofconn_set_controller_id(struct ofconn
*ofconn
, uint16_t controller_id
)
1069 ofconn
->controller_id
= controller_id
;
1070 update_want_packet_in_on_miss(ofconn
);
1073 /* Returns the default miss send length for 'ofconn'. */
1075 ofconn_get_miss_send_len(const struct ofconn
*ofconn
)
1077 return ofconn
->miss_send_len
;
1080 /* Sets the default miss send length for 'ofconn' to 'miss_send_len'. */
1082 ofconn_set_miss_send_len(struct ofconn
*ofconn
, int miss_send_len
)
1084 ofconn
->miss_send_len
= miss_send_len
;
1088 ofconn_set_async_config(struct ofconn
*ofconn
,
1089 const struct ofputil_async_cfg
*ac
)
1091 if (!ofconn
->async_cfg
) {
1092 ofconn
->async_cfg
= xmalloc(sizeof *ofconn
->async_cfg
);
1094 *ofconn
->async_cfg
= *ac
;
1096 if (ofputil_protocol_to_ofp_version(ofconn_get_protocol(ofconn
))
1098 if (ofconn
->async_cfg
->master
[OAM_PACKET_IN
] & (1u << OFPR_ACTION
)) {
1099 ofconn
->async_cfg
->master
[OAM_PACKET_IN
] |= OFPR14_ACTION_BITS
;
1101 if (ofconn
->async_cfg
->slave
[OAM_PACKET_IN
] & (1u << OFPR_ACTION
)) {
1102 ofconn
->async_cfg
->slave
[OAM_PACKET_IN
] |= OFPR14_ACTION_BITS
;
1107 struct ofputil_async_cfg
1108 ofconn_get_async_config(const struct ofconn
*ofconn
)
1110 if (ofconn
->async_cfg
) {
1111 return *ofconn
->async_cfg
;
1114 int version
= rconn_get_version(ofconn
->rconn
);
1115 return (version
< 0 || !ofconn
->enable_async_msgs
1116 ? OFPUTIL_ASYNC_CFG_INIT
1117 : ofputil_async_cfg_default(version
));
1120 /* Sends 'msg' on 'ofconn', accounting it as a reply. (If there is a
1121 * sufficient number of OpenFlow replies in-flight on a single ofconn, then the
1122 * connmgr will stop accepting new OpenFlow requests on that ofconn until the
1123 * controller has accepted some of the replies.) */
1125 ofconn_send_reply(const struct ofconn
*ofconn
, struct ofpbuf
*msg
)
1127 ofconn_send(ofconn
, msg
, ofconn
->reply_counter
);
1130 /* Sends each of the messages in list 'replies' on 'ofconn' in order,
1131 * accounting them as replies. */
1133 ofconn_send_replies(const struct ofconn
*ofconn
, struct ovs_list
*replies
)
1135 struct ofpbuf
*reply
;
1137 LIST_FOR_EACH_POP (reply
, list_node
, replies
) {
1138 ofconn_send_reply(ofconn
, reply
);
1142 /* Sends 'error' on 'ofconn', as a reply to 'request'. */
1144 ofconn_send_error(const struct ofconn
*ofconn
,
1145 const struct ofp_header
*request
, enum ofperr error
)
1147 static struct vlog_rate_limit err_rl
= VLOG_RATE_LIMIT_INIT(10, 10);
1148 struct ofpbuf
*reply
;
1150 reply
= ofperr_encode_reply(error
, request
);
1151 if (!VLOG_DROP_INFO(&err_rl
)) {
1152 const char *type_name
;
1156 request_len
= ntohs(request
->length
);
1157 type_name
= (!ofpraw_decode_partial(&raw
, request
,
1158 MIN(64, request_len
))
1159 ? ofpraw_get_name(raw
)
1162 VLOG_INFO("%s: sending %s error reply to %s message",
1163 rconn_get_name(ofconn
->rconn
), ofperr_to_string(error
),
1166 ofconn_send_reply(ofconn
, reply
);
1169 /* Reports that a flow_mod operation of the type specified by 'command' was
1170 * successfully executed by 'ofconn', so that the connmgr can log it. */
1172 ofconn_report_flow_mod(struct ofconn
*ofconn
,
1173 enum ofp_flow_mod_command command
)
1183 case OFPFC_MODIFY_STRICT
:
1188 case OFPFC_DELETE_STRICT
:
1194 if (ofconn
->next_op_report
== LLONG_MAX
) {
1195 ofconn
->first_op
= now
;
1196 ofconn
->next_op_report
= MAX(now
+ 10 * 1000, ofconn
->op_backoff
);
1197 ofconn
->op_backoff
= ofconn
->next_op_report
+ 60 * 1000;
1199 ofconn
->last_op
= now
;
1202 /* OpenFlow 1.4 bundles. */
1204 static inline uint32_t
1205 bundle_hash(uint32_t id
)
1207 return hash_int(id
, 0);
1211 ofconn_get_bundle(struct ofconn
*ofconn
, uint32_t id
)
1213 struct ofp_bundle
*bundle
;
1215 HMAP_FOR_EACH_IN_BUCKET(bundle
, node
, bundle_hash(id
), &ofconn
->bundles
) {
1216 if (bundle
->id
== id
) {
1225 ofconn_insert_bundle(struct ofconn
*ofconn
, struct ofp_bundle
*bundle
)
1227 hmap_insert(&ofconn
->bundles
, &bundle
->node
, bundle_hash(bundle
->id
));
1231 ofconn_remove_bundle(struct ofconn
*ofconn
, struct ofp_bundle
*bundle
)
1233 hmap_remove(&ofconn
->bundles
, &bundle
->node
);
1237 bundle_remove_all(struct ofconn
*ofconn
)
1239 struct ofp_bundle
*b
, *next
;
1241 HMAP_FOR_EACH_SAFE (b
, next
, node
, &ofconn
->bundles
) {
1242 ofp_bundle_remove__(ofconn
, b
);
1247 bundle_remove_expired(struct ofconn
*ofconn
, long long int now
)
1249 struct ofp_bundle
*b
, *next
;
1250 long long int limit
= now
- BUNDLE_IDLE_TIMEOUT
;
1252 HMAP_FOR_EACH_SAFE (b
, next
, node
, &ofconn
->bundles
) {
1253 if (b
->used
<= limit
) {
1254 ofconn_send_error(ofconn
, b
->msg
, OFPERR_OFPBFC_TIMEOUT
);
1255 ofp_bundle_remove__(ofconn
, b
);
1260 /* Private ofconn functions. */
1263 ofconn_get_target(const struct ofconn
*ofconn
)
1265 return rconn_get_target(ofconn
->rconn
);
1268 static struct ofconn
*
1269 ofconn_create(struct connmgr
*mgr
, struct rconn
*rconn
, enum ofconn_type type
,
1270 bool enable_async_msgs
)
1271 OVS_REQUIRES(ofproto_mutex
)
1273 struct ofconn
*ofconn
;
1275 ofconn
= xzalloc(sizeof *ofconn
);
1276 ofconn
->connmgr
= mgr
;
1277 ovs_list_push_back(&mgr
->all_conns
, &ofconn
->node
);
1278 ofconn
->rconn
= rconn
;
1279 ofconn
->type
= type
;
1280 ofconn
->enable_async_msgs
= enable_async_msgs
;
1282 hmap_init(&ofconn
->monitors
);
1283 ovs_list_init(&ofconn
->updates
);
1285 hmap_init(&ofconn
->bundles
);
1286 ofconn
->next_bundle_expiry_check
= time_msec() + BUNDLE_EXPIRY_INTERVAL
;
1288 ofconn_flush(ofconn
);
1293 /* Clears all of the state in 'ofconn' that should not persist from one
1294 * connection to the next. */
1296 ofconn_flush(struct ofconn
*ofconn
)
1297 OVS_REQUIRES(ofproto_mutex
)
1299 struct ofmonitor
*monitor
, *next_monitor
;
1302 ofconn_log_flow_mods(ofconn
);
1304 ofconn
->role
= OFPCR12_ROLE_EQUAL
;
1305 ofconn_set_protocol(ofconn
, OFPUTIL_P_NONE
);
1306 ofconn
->packet_in_format
= NXPIF_STANDARD
;
1308 rconn_packet_counter_destroy(ofconn
->packet_in_counter
);
1309 ofconn
->packet_in_counter
= rconn_packet_counter_create();
1310 for (i
= 0; i
< N_SCHEDULERS
; i
++) {
1311 if (ofconn
->schedulers
[i
]) {
1314 pinsched_get_limits(ofconn
->schedulers
[i
], &rate
, &burst
);
1315 pinsched_destroy(ofconn
->schedulers
[i
]);
1316 ofconn
->schedulers
[i
] = pinsched_create(rate
, burst
);
1319 ofconn
->miss_send_len
= (ofconn
->type
== OFCONN_PRIMARY
1320 ? OFP_DEFAULT_MISS_SEND_LEN
1322 ofconn
->controller_id
= 0;
1324 rconn_packet_counter_destroy(ofconn
->reply_counter
);
1325 ofconn
->reply_counter
= rconn_packet_counter_create();
1327 free(ofconn
->async_cfg
);
1328 ofconn
->async_cfg
= NULL
;
1330 ofconn
->n_add
= ofconn
->n_delete
= ofconn
->n_modify
= 0;
1331 ofconn
->first_op
= ofconn
->last_op
= LLONG_MIN
;
1332 ofconn
->next_op_report
= LLONG_MAX
;
1333 ofconn
->op_backoff
= LLONG_MIN
;
1335 HMAP_FOR_EACH_SAFE (monitor
, next_monitor
, ofconn_node
,
1336 &ofconn
->monitors
) {
1337 ofmonitor_destroy(monitor
);
1339 rconn_packet_counter_destroy(ofconn
->monitor_counter
);
1340 ofconn
->monitor_counter
= rconn_packet_counter_create();
1341 ofpbuf_list_delete(&ofconn
->updates
); /* ...but it should be empty. */
1345 ofconn_destroy(struct ofconn
*ofconn
)
1346 OVS_REQUIRES(ofproto_mutex
)
1348 ofconn_flush(ofconn
);
1350 /* Force clearing of want_packet_in_on_miss to keep the global count
1352 ofconn
->controller_id
= 1;
1353 update_want_packet_in_on_miss(ofconn
);
1355 if (ofconn
->type
== OFCONN_PRIMARY
) {
1356 hmap_remove(&ofconn
->connmgr
->controllers
, &ofconn
->hmap_node
);
1359 bundle_remove_all(ofconn
);
1360 hmap_destroy(&ofconn
->bundles
);
1362 hmap_destroy(&ofconn
->monitors
);
1363 ovs_list_remove(&ofconn
->node
);
1364 rconn_destroy(ofconn
->rconn
);
1365 rconn_packet_counter_destroy(ofconn
->packet_in_counter
);
1366 rconn_packet_counter_destroy(ofconn
->reply_counter
);
1367 rconn_packet_counter_destroy(ofconn
->monitor_counter
);
1371 /* Reconfigures 'ofconn' to match 'c'. 'ofconn' and 'c' must have the same
1374 ofconn_reconfigure(struct ofconn
*ofconn
, const struct ofproto_controller
*c
)
1378 ofconn
->band
= c
->band
;
1379 ofconn
->enable_async_msgs
= c
->enable_async_msgs
;
1381 rconn_set_max_backoff(ofconn
->rconn
, c
->max_backoff
);
1383 probe_interval
= c
->probe_interval
? MAX(c
->probe_interval
, 5) : 0;
1384 rconn_set_probe_interval(ofconn
->rconn
, probe_interval
);
1386 ofconn_set_rate_limit(ofconn
, c
->rate_limit
, c
->burst_limit
);
1388 /* If dscp value changed reconnect. */
1389 if (c
->dscp
!= rconn_get_dscp(ofconn
->rconn
)) {
1390 rconn_set_dscp(ofconn
->rconn
, c
->dscp
);
1391 rconn_reconnect(ofconn
->rconn
);
1395 /* Returns true if it makes sense for 'ofconn' to receive and process OpenFlow
1398 ofconn_may_recv(const struct ofconn
*ofconn
)
1400 int count
= rconn_packet_counter_n_packets(ofconn
->reply_counter
);
1401 return count
< OFCONN_REPLY_MAX
;
1405 ofconn_run(struct ofconn
*ofconn
,
1406 void (*handle_openflow
)(struct ofconn
*,
1407 const struct ofpbuf
*ofp_msg
))
1409 struct connmgr
*mgr
= ofconn
->connmgr
;
1412 for (i
= 0; i
< N_SCHEDULERS
; i
++) {
1413 struct ovs_list txq
;
1415 pinsched_run(ofconn
->schedulers
[i
], &txq
);
1416 do_send_packet_ins(ofconn
, &txq
);
1419 rconn_run(ofconn
->rconn
);
1421 /* Limit the number of iterations to avoid starving other tasks. */
1422 for (i
= 0; i
< 50 && ofconn_may_recv(ofconn
); i
++) {
1423 struct ofpbuf
*of_msg
= rconn_recv(ofconn
->rconn
);
1428 if (mgr
->fail_open
) {
1429 fail_open_maybe_recover(mgr
->fail_open
);
1432 handle_openflow(ofconn
, of_msg
);
1433 ofpbuf_delete(of_msg
);
1436 long long int now
= time_msec();
1438 if (now
>= ofconn
->next_bundle_expiry_check
) {
1439 ofconn
->next_bundle_expiry_check
= now
+ BUNDLE_EXPIRY_INTERVAL
;
1440 bundle_remove_expired(ofconn
, now
);
1443 if (now
>= ofconn
->next_op_report
) {
1444 ofconn_log_flow_mods(ofconn
);
1447 ovs_mutex_lock(&ofproto_mutex
);
1448 if (!rconn_is_alive(ofconn
->rconn
)) {
1449 ofconn_destroy(ofconn
);
1450 } else if (!rconn_is_connected(ofconn
->rconn
)) {
1451 ofconn_flush(ofconn
);
1453 ovs_mutex_unlock(&ofproto_mutex
);
1457 ofconn_wait(struct ofconn
*ofconn
)
1461 for (i
= 0; i
< N_SCHEDULERS
; i
++) {
1462 pinsched_wait(ofconn
->schedulers
[i
]);
1464 rconn_run_wait(ofconn
->rconn
);
1465 if (ofconn_may_recv(ofconn
)) {
1466 rconn_recv_wait(ofconn
->rconn
);
1468 if (ofconn
->next_op_report
!= LLONG_MAX
) {
1469 poll_timer_wait_until(ofconn
->next_op_report
);
1474 ofconn_log_flow_mods(struct ofconn
*ofconn
)
1476 int n_flow_mods
= ofconn
->n_add
+ ofconn
->n_delete
+ ofconn
->n_modify
;
1478 long long int ago
= (time_msec() - ofconn
->first_op
) / 1000;
1479 long long int interval
= (ofconn
->last_op
- ofconn
->first_op
) / 1000;
1483 ds_put_format(&s
, "%d flow_mods ", n_flow_mods
);
1484 if (interval
== ago
) {
1485 ds_put_format(&s
, "in the last %lld s", ago
);
1486 } else if (interval
) {
1487 ds_put_format(&s
, "in the %lld s starting %lld s ago",
1490 ds_put_format(&s
, "%lld s ago", ago
);
1493 ds_put_cstr(&s
, " (");
1494 if (ofconn
->n_add
) {
1495 ds_put_format(&s
, "%d adds, ", ofconn
->n_add
);
1497 if (ofconn
->n_delete
) {
1498 ds_put_format(&s
, "%d deletes, ", ofconn
->n_delete
);
1500 if (ofconn
->n_modify
) {
1501 ds_put_format(&s
, "%d modifications, ", ofconn
->n_modify
);
1504 ds_put_char(&s
, ')');
1506 VLOG_INFO("%s: %s", rconn_get_name(ofconn
->rconn
), ds_cstr(&s
));
1509 ofconn
->n_add
= ofconn
->n_delete
= ofconn
->n_modify
= 0;
1511 ofconn
->next_op_report
= LLONG_MAX
;
1514 /* Returns true if 'ofconn' should receive asynchronous messages of the given
1515 * OAM_* 'type' and 'reason', which should be a OFPR_* value for OAM_PACKET_IN,
1516 * a OFPPR_* value for OAM_PORT_STATUS, or an OFPRR_* value for
1517 * OAM_FLOW_REMOVED. Returns false if the message should not be sent on
1520 ofconn_receives_async_msg(const struct ofconn
*ofconn
,
1521 enum ofputil_async_msg_type type
,
1522 unsigned int reason
)
1524 ovs_assert(reason
< 32);
1525 ovs_assert((unsigned int) type
< OAM_N_TYPES
);
1527 /* Keep the following code in sync with the documentation in the
1528 * "Asynchronous Messages" section in 'topics/design' */
1530 if (ofconn
->type
== OFCONN_SERVICE
&& !ofconn
->miss_send_len
) {
1531 /* Service connections don't get asynchronous messages unless they have
1532 * explicitly asked for them by setting a nonzero miss send length. */
1536 struct ofputil_async_cfg ac
= ofconn_get_async_config(ofconn
);
1537 uint32_t *masks
= (ofconn
->role
== OFPCR12_ROLE_SLAVE
1540 return (masks
[type
] & (1u << reason
)) != 0;
1543 /* This function returns true to indicate that a packet_in message
1544 * for a "table-miss" should be sent to at least one controller.
1546 * False otherwise. */
1548 connmgr_wants_packet_in_on_miss(struct connmgr
*mgr
)
1552 atomic_read_relaxed(&mgr
->want_packet_in_on_miss
, &count
);
1556 /* Returns a human-readable name for an OpenFlow connection between 'mgr' and
1557 * 'target', suitable for use in log messages for identifying the connection.
1559 * The name is dynamically allocated. The caller should free it (with free())
1560 * when it is no longer needed. */
1562 ofconn_make_name(const struct connmgr
*mgr
, const char *target
)
1564 return xasprintf("%s<->%s", mgr
->name
, target
);
1568 ofconn_set_rate_limit(struct ofconn
*ofconn
, int rate
, int burst
)
1572 for (i
= 0; i
< N_SCHEDULERS
; i
++) {
1573 struct pinsched
**s
= &ofconn
->schedulers
[i
];
1577 *s
= pinsched_create(rate
, burst
);
1579 pinsched_set_limits(*s
, rate
, burst
);
1582 pinsched_destroy(*s
);
1589 ofconn_send(const struct ofconn
*ofconn
, struct ofpbuf
*msg
,
1590 struct rconn_packet_counter
*counter
)
1592 ofpmsg_update_length(msg
);
1593 rconn_send(ofconn
->rconn
, msg
, counter
);
1596 /* Sending asynchronous messages. */
1598 /* Sends an OFPT_PORT_STATUS message with 'opp' and 'reason' to appropriate
1599 * controllers managed by 'mgr'. For messages caused by a controller
1600 * OFPT_PORT_MOD, specify 'source' as the controller connection that sent the
1601 * request; otherwise, specify 'source' as NULL. */
1603 connmgr_send_port_status(struct connmgr
*mgr
, struct ofconn
*source
,
1604 const struct ofputil_phy_port
*pp
, uint8_t reason
)
1606 /* XXX Should limit the number of queued port status change messages. */
1607 struct ofputil_port_status ps
;
1608 struct ofconn
*ofconn
;
1612 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
1613 if (ofconn_receives_async_msg(ofconn
, OAM_PORT_STATUS
, reason
)) {
1616 /* Before 1.5, OpenFlow specified that OFPT_PORT_MOD should not
1617 * generate OFPT_PORT_STATUS messages. That requirement was a
1618 * relic of how OpenFlow originally supported a single controller,
1619 * so that one could expect the controller to already know the
1620 * changes it had made.
1622 * EXT-338 changes OpenFlow 1.5 OFPT_PORT_MOD to send
1623 * OFPT_PORT_STATUS messages to every controller. This is
1624 * obviously more useful in the multi-controller case. We could
1625 * always implement it that way in OVS, but that would risk
1626 * confusing controllers that are intended for single-controller
1627 * use only. (Imagine a controller that generates an OFPT_PORT_MOD
1628 * in response to any OFPT_PORT_STATUS!)
1630 * So this compromises: for OpenFlow 1.4 and earlier, it generates
1631 * OFPT_PORT_STATUS for OFPT_PORT_MOD, but not back to the
1632 * originating controller. In a single-controller environment, in
1633 * particular, this means that it will never generate
1634 * OFPT_PORT_STATUS for OFPT_PORT_MOD at all. */
1635 if (ofconn
== source
1636 && rconn_get_version(ofconn
->rconn
) < OFP15_VERSION
) {
1640 msg
= ofputil_encode_port_status(&ps
, ofconn_get_protocol(ofconn
));
1641 ofconn_send(ofconn
, msg
, NULL
);
1646 /* Sends an OFPT_REQUESTFORWARD message with 'request' and 'reason' to
1647 * appropriate controllers managed by 'mgr'. For messages caused by a
1648 * controller OFPT_GROUP_MOD and OFPT_METER_MOD, specify 'source' as the
1649 * controller connection that sent the request; otherwise, specify 'source'
1652 connmgr_send_requestforward(struct connmgr
*mgr
, const struct ofconn
*source
,
1653 const struct ofputil_requestforward
*rf
)
1655 struct ofconn
*ofconn
;
1657 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
1658 if (ofconn_receives_async_msg(ofconn
, OAM_REQUESTFORWARD
, rf
->reason
)
1659 && rconn_get_version(ofconn
->rconn
) >= OFP14_VERSION
1660 && ofconn
!= source
) {
1661 enum ofputil_protocol protocol
= ofconn_get_protocol(ofconn
);
1662 ofconn_send(ofconn
, ofputil_encode_requestforward(rf
, protocol
),
1668 /* Sends an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message based on 'fr' to
1669 * appropriate controllers managed by 'mgr'.
1671 * This may be called from the RCU thread. */
1673 connmgr_send_flow_removed(struct connmgr
*mgr
,
1674 const struct ofputil_flow_removed
*fr
)
1675 OVS_REQUIRES(ofproto_mutex
)
1677 struct ofconn
*ofconn
;
1679 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
1680 if (ofconn_receives_async_msg(ofconn
, OAM_FLOW_REMOVED
, fr
->reason
)) {
1683 /* Account flow expirations as replies to OpenFlow requests. That
1684 * works because preventing OpenFlow requests from being processed
1685 * also prevents new flows from being added (and expiring). (It
1686 * also prevents processing OpenFlow requests that would not add
1687 * new flows, so it is imperfect.) */
1688 msg
= ofputil_encode_flow_removed(fr
, ofconn_get_protocol(ofconn
));
1689 ofconn_send_reply(ofconn
, msg
);
1694 /* Sends an OFPT_TABLE_STATUS message with 'reason' to appropriate controllers
1695 * managed by 'mgr'. When the table state changes, the controller needs to be
1696 * informed with the OFPT_TABLE_STATUS message. The reason values
1697 * OFPTR_VACANCY_DOWN and OFPTR_VACANCY_UP identify a vacancy message. The
1698 * vacancy events are generated when the remaining space in the flow table
1699 * changes and crosses one of the vacancy thereshold specified by
1700 * OFPT_TABLE_MOD. */
1702 connmgr_send_table_status(struct connmgr
*mgr
,
1703 const struct ofputil_table_desc
*td
,
1706 struct ofputil_table_status ts
;
1707 struct ofconn
*ofconn
;
1712 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
1713 if (ofconn_receives_async_msg(ofconn
, OAM_TABLE_STATUS
, reason
)) {
1716 msg
= ofputil_encode_table_status(&ts
,
1717 ofconn_get_protocol(ofconn
));
1719 ofconn_send(ofconn
, msg
, NULL
);
1725 /* Given 'pin', sends an OFPT_PACKET_IN message to each OpenFlow controller as
1726 * necessary according to their individual configurations. */
1728 connmgr_send_async_msg(struct connmgr
*mgr
,
1729 const struct ofproto_async_msg
*am
)
1731 struct ofconn
*ofconn
;
1733 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
1734 enum ofputil_protocol protocol
= ofconn_get_protocol(ofconn
);
1735 if (protocol
== OFPUTIL_P_NONE
|| !rconn_is_connected(ofconn
->rconn
)
1736 || ofconn
->controller_id
!= am
->controller_id
1737 || !ofconn_receives_async_msg(ofconn
, am
->oam
,
1738 am
->pin
.up
.base
.reason
)) {
1742 struct ofpbuf
*msg
= ofputil_encode_packet_in_private(
1743 &am
->pin
.up
, protocol
, ofconn
->packet_in_format
);
1745 struct ovs_list txq
;
1746 bool is_miss
= (am
->pin
.up
.base
.reason
== OFPR_NO_MATCH
||
1747 am
->pin
.up
.base
.reason
== OFPR_EXPLICIT_MISS
||
1748 am
->pin
.up
.base
.reason
== OFPR_IMPLICIT_MISS
);
1749 pinsched_send(ofconn
->schedulers
[is_miss
],
1750 am
->pin
.up
.base
.flow_metadata
.flow
.in_port
.ofp_port
,
1752 do_send_packet_ins(ofconn
, &txq
);
1757 do_send_packet_ins(struct ofconn
*ofconn
, struct ovs_list
*txq
)
1761 LIST_FOR_EACH_POP (pin
, list_node
, txq
) {
1762 if (rconn_send_with_limit(ofconn
->rconn
, pin
,
1763 ofconn
->packet_in_counter
, 100) == EAGAIN
) {
1764 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
1766 VLOG_INFO_RL(&rl
, "%s: dropping packet-in due to queue overflow",
1767 rconn_get_name(ofconn
->rconn
));
1772 /* Fail-open settings. */
1774 /* Returns the failure handling mode (OFPROTO_FAIL_SECURE or
1775 * OFPROTO_FAIL_STANDALONE) for 'mgr'. */
1776 enum ofproto_fail_mode
1777 connmgr_get_fail_mode(const struct connmgr
*mgr
)
1779 return mgr
->fail_mode
;
1782 /* Sets the failure handling mode for 'mgr' to 'fail_mode' (either
1783 * OFPROTO_FAIL_SECURE or OFPROTO_FAIL_STANDALONE). */
1785 connmgr_set_fail_mode(struct connmgr
*mgr
, enum ofproto_fail_mode fail_mode
)
1787 if (mgr
->fail_mode
!= fail_mode
) {
1788 mgr
->fail_mode
= fail_mode
;
1789 update_fail_open(mgr
);
1790 if (!connmgr_has_controllers(mgr
)) {
1791 ofproto_flush_flows(mgr
->ofproto
);
1796 /* Fail-open implementation. */
1798 /* Returns the longest probe interval among the primary controllers configured
1799 * on 'mgr'. Returns 0 if there are no primary controllers. */
1801 connmgr_get_max_probe_interval(const struct connmgr
*mgr
)
1803 const struct ofconn
*ofconn
;
1804 int max_probe_interval
;
1806 max_probe_interval
= 0;
1807 HMAP_FOR_EACH (ofconn
, hmap_node
, &mgr
->controllers
) {
1808 int probe_interval
= rconn_get_probe_interval(ofconn
->rconn
);
1809 max_probe_interval
= MAX(max_probe_interval
, probe_interval
);
1811 return max_probe_interval
;
1814 /* Returns the number of seconds for which all of 'mgr's primary controllers
1815 * have been disconnected. Returns 0 if 'mgr' has no primary controllers. */
1817 connmgr_failure_duration(const struct connmgr
*mgr
)
1819 const struct ofconn
*ofconn
;
1820 int min_failure_duration
;
1822 if (!connmgr_has_controllers(mgr
)) {
1826 min_failure_duration
= INT_MAX
;
1827 HMAP_FOR_EACH (ofconn
, hmap_node
, &mgr
->controllers
) {
1828 int failure_duration
= rconn_failure_duration(ofconn
->rconn
);
1829 min_failure_duration
= MIN(min_failure_duration
, failure_duration
);
1831 return min_failure_duration
;
1834 /* Returns true if at least one primary controller is connected (regardless of
1835 * whether those controllers are believed to have authenticated and accepted
1836 * this switch), false if none of them are connected. */
1838 connmgr_is_any_controller_connected(const struct connmgr
*mgr
)
1840 const struct ofconn
*ofconn
;
1842 HMAP_FOR_EACH (ofconn
, hmap_node
, &mgr
->controllers
) {
1843 if (rconn_is_connected(ofconn
->rconn
)) {
1850 /* Returns true if at least one primary controller is believed to have
1851 * authenticated and accepted this switch, false otherwise. */
1853 connmgr_is_any_controller_admitted(const struct connmgr
*mgr
)
1855 const struct ofconn
*ofconn
;
1857 HMAP_FOR_EACH (ofconn
, hmap_node
, &mgr
->controllers
) {
1858 if (rconn_is_admitted(ofconn
->rconn
)) {
1865 /* In-band configuration. */
1867 static bool any_extras_changed(const struct connmgr
*,
1868 const struct sockaddr_in
*extras
, size_t n
);
1870 /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'mgr''s
1871 * in-band control should guarantee access, in the same way that in-band
1872 * control guarantees access to OpenFlow controllers. */
1874 connmgr_set_extra_in_band_remotes(struct connmgr
*mgr
,
1875 const struct sockaddr_in
*extras
, size_t n
)
1877 if (!any_extras_changed(mgr
, extras
, n
)) {
1881 free(mgr
->extra_in_band_remotes
);
1882 mgr
->n_extra_remotes
= n
;
1883 mgr
->extra_in_band_remotes
= xmemdup(extras
, n
* sizeof *extras
);
1885 update_in_band_remotes(mgr
);
1888 /* Sets the OpenFlow queue used by flows set up by in-band control on
1889 * 'mgr' to 'queue_id'. If 'queue_id' is negative, then in-band control
1890 * flows will use the default queue. */
1892 connmgr_set_in_band_queue(struct connmgr
*mgr
, int queue_id
)
1894 if (queue_id
!= mgr
->in_band_queue
) {
1895 mgr
->in_band_queue
= queue_id
;
1896 update_in_band_remotes(mgr
);
1901 any_extras_changed(const struct connmgr
*mgr
,
1902 const struct sockaddr_in
*extras
, size_t n
)
1906 if (n
!= mgr
->n_extra_remotes
) {
1910 for (i
= 0; i
< n
; i
++) {
1911 const struct sockaddr_in
*old
= &mgr
->extra_in_band_remotes
[i
];
1912 const struct sockaddr_in
*new = &extras
[i
];
1914 if (old
->sin_addr
.s_addr
!= new->sin_addr
.s_addr
||
1915 old
->sin_port
!= new->sin_port
) {
1923 /* In-band implementation. */
1926 connmgr_has_in_band(struct connmgr
*mgr
)
1928 return mgr
->in_band
!= NULL
;
1931 /* Fail-open and in-band implementation. */
1933 /* Called by 'ofproto' after all flows have been flushed, to allow fail-open
1934 * and standalone mode to re-create their flows.
1936 * In-band control has more sophisticated code that manages flows itself. */
1938 connmgr_flushed(struct connmgr
*mgr
)
1939 OVS_EXCLUDED(ofproto_mutex
)
1941 if (mgr
->fail_open
) {
1942 fail_open_flushed(mgr
->fail_open
);
1945 /* If there are no controllers and we're in standalone mode, set up a flow
1946 * that matches every packet and directs them to OFPP_NORMAL (which goes to
1947 * us). Otherwise, the switch is in secure mode and we won't pass any
1948 * traffic until a controller has been defined and it tells us to do so. */
1949 if (!connmgr_has_controllers(mgr
)
1950 && mgr
->fail_mode
== OFPROTO_FAIL_STANDALONE
) {
1951 struct ofpbuf ofpacts
;
1954 ofpbuf_init(&ofpacts
, OFPACT_OUTPUT_SIZE
);
1955 ofpact_put_OUTPUT(&ofpacts
)->port
= OFPP_NORMAL
;
1957 match_init_catchall(&match
);
1958 ofproto_add_flow(mgr
->ofproto
, &match
, 0, ofpacts
.data
,
1961 ofpbuf_uninit(&ofpacts
);
1965 /* Returns the number of hidden rules created by the in-band and fail-open
1966 * implementations in table 0. (Subtracting this count from the number of
1967 * rules in the table 0 classifier, as maintained in struct oftable, yields
1968 * the number of flows that OVS should report via OpenFlow for table 0.) */
1970 connmgr_count_hidden_rules(const struct connmgr
*mgr
)
1974 n_hidden
+= in_band_count_rules(mgr
->in_band
);
1976 if (mgr
->fail_open
) {
1977 n_hidden
+= fail_open_count_rules(mgr
->fail_open
);
1982 /* Creates a new ofservice for 'target' in 'mgr'. Returns 0 if successful,
1983 * otherwise a positive errno value.
1985 * ofservice_reconfigure() must be called to fully configure the new
1988 ofservice_create(struct connmgr
*mgr
, const char *target
,
1989 uint32_t allowed_versions
, uint8_t dscp
)
1991 struct ofservice
*ofservice
;
1992 struct pvconn
*pvconn
;
1995 error
= pvconn_open(target
, allowed_versions
, dscp
, &pvconn
);
2000 ofservice
= xzalloc(sizeof *ofservice
);
2001 hmap_insert(&mgr
->services
, &ofservice
->node
, hash_string(target
, 0));
2002 ofservice
->pvconn
= pvconn
;
2003 ofservice
->allowed_versions
= allowed_versions
;
2009 ofservice_destroy(struct connmgr
*mgr
, struct ofservice
*ofservice
)
2011 hmap_remove(&mgr
->services
, &ofservice
->node
);
2012 pvconn_close(ofservice
->pvconn
);
2017 ofservice_reconfigure(struct ofservice
*ofservice
,
2018 const struct ofproto_controller
*c
)
2020 ofservice
->probe_interval
= c
->probe_interval
;
2021 ofservice
->rate_limit
= c
->rate_limit
;
2022 ofservice
->burst_limit
= c
->burst_limit
;
2023 ofservice
->enable_async_msgs
= c
->enable_async_msgs
;
2024 ofservice
->dscp
= c
->dscp
;
2027 /* Finds and returns the ofservice within 'mgr' that has the given
2028 * 'target', or a null pointer if none exists. */
2029 static struct ofservice
*
2030 ofservice_lookup(struct connmgr
*mgr
, const char *target
)
2032 struct ofservice
*ofservice
;
2034 HMAP_FOR_EACH_WITH_HASH (ofservice
, node
, hash_string(target
, 0),
2036 if (!strcmp(pvconn_get_name(ofservice
->pvconn
), target
)) {
2043 /* Flow monitors (NXST_FLOW_MONITOR). */
2045 /* A counter incremented when something significant happens to an OpenFlow
2048 * - When a rule is added, its 'add_seqno' and 'modify_seqno' are set to
2049 * the current value (which is then incremented).
2051 * - When a rule is modified, its 'modify_seqno' is set to the current
2052 * value (which is then incremented).
2054 * Thus, by comparing an old value of monitor_seqno against a rule's
2055 * 'add_seqno', one can tell whether the rule was added before or after the old
2056 * value was read, and similarly for 'modify_seqno'.
2058 * 32 bits should normally be sufficient (and would be nice, to save space in
2059 * each rule) but then we'd have to have some special cases for wraparound.
2061 * We initialize monitor_seqno to 1 to allow 0 to be used as an invalid
2063 static uint64_t monitor_seqno
= 1;
2065 COVERAGE_DEFINE(ofmonitor_pause
);
2066 COVERAGE_DEFINE(ofmonitor_resume
);
2069 ofmonitor_create(const struct ofputil_flow_monitor_request
*request
,
2070 struct ofconn
*ofconn
, struct ofmonitor
**monitorp
)
2071 OVS_REQUIRES(ofproto_mutex
)
2073 struct ofmonitor
*m
;
2077 m
= ofmonitor_lookup(ofconn
, request
->id
);
2079 return OFPERR_OFPMOFC_MONITOR_EXISTS
;
2082 m
= xmalloc(sizeof *m
);
2084 hmap_insert(&ofconn
->monitors
, &m
->ofconn_node
, hash_int(request
->id
, 0));
2085 m
->id
= request
->id
;
2086 m
->flags
= request
->flags
;
2087 m
->out_port
= request
->out_port
;
2088 m
->table_id
= request
->table_id
;
2089 minimatch_init(&m
->match
, &request
->match
);
2096 ofmonitor_lookup(struct ofconn
*ofconn
, uint32_t id
)
2097 OVS_REQUIRES(ofproto_mutex
)
2099 struct ofmonitor
*m
;
2101 HMAP_FOR_EACH_IN_BUCKET (m
, ofconn_node
, hash_int(id
, 0),
2102 &ofconn
->monitors
) {
2111 ofmonitor_destroy(struct ofmonitor
*m
)
2112 OVS_REQUIRES(ofproto_mutex
)
2115 minimatch_destroy(&m
->match
);
2116 hmap_remove(&m
->ofconn
->monitors
, &m
->ofconn_node
);
2122 ofmonitor_report(struct connmgr
*mgr
, struct rule
*rule
,
2123 enum nx_flow_update_event event
,
2124 enum ofp_flow_removed_reason reason
,
2125 const struct ofconn
*abbrev_ofconn
, ovs_be32 abbrev_xid
,
2126 const struct rule_actions
*old_actions
)
2127 OVS_REQUIRES(ofproto_mutex
)
2129 enum nx_flow_monitor_flags update
;
2130 struct ofconn
*ofconn
;
2132 if (rule_is_hidden(rule
)) {
2139 rule
->add_seqno
= rule
->modify_seqno
= monitor_seqno
++;
2143 update
= NXFMF_DELETE
;
2146 case NXFME_MODIFIED
:
2147 update
= NXFMF_MODIFY
;
2148 rule
->modify_seqno
= monitor_seqno
++;
2156 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
2157 enum nx_flow_monitor_flags flags
= 0;
2158 struct ofmonitor
*m
;
2160 if (ofconn
->monitor_paused
) {
2161 /* Only send NXFME_DELETED notifications for flows that were added
2162 * before we paused. */
2163 if (event
!= NXFME_DELETED
2164 || rule
->add_seqno
> ofconn
->monitor_paused
) {
2169 HMAP_FOR_EACH (m
, ofconn_node
, &ofconn
->monitors
) {
2170 if (m
->flags
& update
2171 && (m
->table_id
== 0xff || m
->table_id
== rule
->table_id
)
2172 && (ofproto_rule_has_out_port(rule
, m
->out_port
)
2174 && ofpacts_output_to_port(old_actions
->ofpacts
,
2175 old_actions
->ofpacts_len
,
2177 && cls_rule_is_loose_match(&rule
->cr
, &m
->match
)) {
2183 if (ovs_list_is_empty(&ofconn
->updates
)) {
2184 ofputil_start_flow_update(&ofconn
->updates
);
2185 ofconn
->sent_abbrev_update
= false;
2188 if (flags
& NXFMF_OWN
|| ofconn
!= abbrev_ofconn
2189 || ofconn
->monitor_paused
) {
2190 struct ofputil_flow_update fu
;
2193 fu
.reason
= event
== NXFME_DELETED
? reason
: 0;
2194 fu
.table_id
= rule
->table_id
;
2195 fu
.cookie
= rule
->flow_cookie
;
2196 minimatch_expand(&rule
->cr
.match
, &fu
.match
);
2197 fu
.priority
= rule
->cr
.priority
;
2199 ovs_mutex_lock(&rule
->mutex
);
2200 fu
.idle_timeout
= rule
->idle_timeout
;
2201 fu
.hard_timeout
= rule
->hard_timeout
;
2202 ovs_mutex_unlock(&rule
->mutex
);
2204 if (flags
& NXFMF_ACTIONS
) {
2205 const struct rule_actions
*actions
= rule_get_actions(rule
);
2206 fu
.ofpacts
= actions
->ofpacts
;
2207 fu
.ofpacts_len
= actions
->ofpacts_len
;
2212 ofputil_append_flow_update(&fu
, &ofconn
->updates
,
2213 ofproto_get_tun_tab(rule
->ofproto
));
2214 } else if (!ofconn
->sent_abbrev_update
) {
2215 struct ofputil_flow_update fu
;
2217 fu
.event
= NXFME_ABBREV
;
2218 fu
.xid
= abbrev_xid
;
2219 ofputil_append_flow_update(&fu
, &ofconn
->updates
,
2220 ofproto_get_tun_tab(rule
->ofproto
));
2222 ofconn
->sent_abbrev_update
= true;
2229 ofmonitor_flush(struct connmgr
*mgr
)
2230 OVS_REQUIRES(ofproto_mutex
)
2232 struct ofconn
*ofconn
;
2234 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
2235 struct rconn_packet_counter
*counter
= ofconn
->monitor_counter
;
2238 LIST_FOR_EACH_POP (msg
, list_node
, &ofconn
->updates
) {
2239 ofconn_send(ofconn
, msg
, counter
);
2242 if (!ofconn
->monitor_paused
2243 && rconn_packet_counter_n_bytes(counter
) > 128 * 1024) {
2244 struct ofpbuf
*pause
;
2246 COVERAGE_INC(ofmonitor_pause
);
2247 ofconn
->monitor_paused
= monitor_seqno
++;
2248 pause
= ofpraw_alloc_xid(OFPRAW_NXT_FLOW_MONITOR_PAUSED
,
2249 OFP10_VERSION
, htonl(0), 0);
2250 ofconn_send(ofconn
, pause
, counter
);
2256 ofmonitor_resume(struct ofconn
*ofconn
)
2257 OVS_REQUIRES(ofproto_mutex
)
2259 struct rule_collection rules
;
2260 struct ofpbuf
*resumed
;
2261 struct ofmonitor
*m
;
2262 struct ovs_list msgs
;
2264 rule_collection_init(&rules
);
2265 HMAP_FOR_EACH (m
, ofconn_node
, &ofconn
->monitors
) {
2266 ofmonitor_collect_resume_rules(m
, ofconn
->monitor_paused
, &rules
);
2269 ovs_list_init(&msgs
);
2270 ofmonitor_compose_refresh_updates(&rules
, &msgs
);
2272 resumed
= ofpraw_alloc_xid(OFPRAW_NXT_FLOW_MONITOR_RESUMED
, OFP10_VERSION
,
2274 ovs_list_push_back(&msgs
, &resumed
->list_node
);
2275 ofconn_send_replies(ofconn
, &msgs
);
2277 ofconn
->monitor_paused
= 0;
2281 ofmonitor_may_resume(const struct ofconn
*ofconn
)
2282 OVS_REQUIRES(ofproto_mutex
)
2284 return (ofconn
->monitor_paused
!= 0
2285 && !rconn_packet_counter_n_packets(ofconn
->monitor_counter
));
2289 ofmonitor_run(struct connmgr
*mgr
)
2291 struct ofconn
*ofconn
;
2293 ovs_mutex_lock(&ofproto_mutex
);
2294 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
2295 if (ofmonitor_may_resume(ofconn
)) {
2296 COVERAGE_INC(ofmonitor_resume
);
2297 ofmonitor_resume(ofconn
);
2300 ovs_mutex_unlock(&ofproto_mutex
);
2304 ofmonitor_wait(struct connmgr
*mgr
)
2306 struct ofconn
*ofconn
;
2308 ovs_mutex_lock(&ofproto_mutex
);
2309 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
2310 if (ofmonitor_may_resume(ofconn
)) {
2311 poll_immediate_wake();
2314 ovs_mutex_unlock(&ofproto_mutex
);
2318 ofproto_async_msg_free(struct ofproto_async_msg
*am
)
2320 free(am
->pin
.up
.base
.packet
);
2321 free(am
->pin
.up
.base
.userdata
);
2322 free(am
->pin
.up
.stack
);
2323 free(am
->pin
.up
.actions
);
2324 free(am
->pin
.up
.action_set
);