2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 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.
25 #include "dynamic-string.h"
26 #include "fail-open.h"
29 #include "ofp-actions.h"
33 #include "ofproto-provider.h"
35 #include "poll-loop.h"
47 VLOG_DEFINE_THIS_MODULE(connmgr);
48 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
50 /* An OpenFlow connection.
56 * 'ofproto_mutex' must be held whenever an ofconn is created or destroyed or,
57 * more or less equivalently, whenever an ofconn is added to or removed from a
58 * connmgr. 'ofproto_mutex' doesn't protect the data inside the ofconn, except
59 * as specifically noted below. */
61 /* Configuration that persists from one connection to the next. */
63 struct list node; /* In struct connmgr's "all_conns" list. */
64 struct hmap_node hmap_node; /* In struct connmgr's "controllers" map. */
66 struct connmgr *connmgr; /* Connection's manager. */
67 struct rconn *rconn; /* OpenFlow connection. */
68 enum ofconn_type type; /* Type. */
69 enum ofproto_band band; /* In-band or out-of-band? */
70 bool enable_async_msgs; /* Initially enable async messages? */
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 nx_packet_in_format packet_in_format; /* OFPT_PACKET_IN format. */
79 /* Asynchronous flow table operation support. */
80 struct list opgroups; /* Contains pending "ofopgroups", if any. */
81 struct ofpbuf *blocked; /* Postponed OpenFlow message, if any. */
82 bool retry; /* True if 'blocked' is ready to try again. */
84 /* OFPT_PACKET_IN related data. */
85 struct rconn_packet_counter *packet_in_counter; /* # queued on 'rconn'. */
86 #define N_SCHEDULERS 2
87 struct pinsched *schedulers[N_SCHEDULERS];
88 struct pktbuf *pktbuf; /* OpenFlow packet buffers. */
89 int miss_send_len; /* Bytes to send of buffered packets. */
90 uint16_t controller_id; /* Connection controller ID. */
92 /* Number of OpenFlow messages queued on 'rconn' as replies to OpenFlow
93 * requests, and the maximum number before we stop reading OpenFlow
95 #define OFCONN_REPLY_MAX 100
96 struct rconn_packet_counter *reply_counter;
98 /* Asynchronous message configuration in each possible roles.
100 * A 1-bit enables sending an asynchronous message for one possible reason
101 * that the message might be generated, a 0-bit disables it. */
102 uint32_t master_async_config[OAM_N_TYPES]; /* master, other */
103 uint32_t slave_async_config[OAM_N_TYPES]; /* slave */
105 /* Flow table operation logging. */
106 int n_add, n_delete, n_modify; /* Number of unreported ops of each kind. */
107 long long int first_op, last_op; /* Range of times for unreported ops. */
108 long long int next_op_report; /* Time to report ops, or LLONG_MAX. */
109 long long int op_backoff; /* Earliest time to report ops again. */
111 /* Flow monitors (e.g. NXST_FLOW_MONITOR). */
113 /* Configuration. Contains "struct ofmonitor"s. */
114 struct hmap monitors OVS_GUARDED_BY(ofproto_mutex);
118 * When too many flow monitor notifications back up in the transmit buffer,
119 * we pause the transmission of further notifications. These members track
120 * the flow control state.
122 * When notifications are flowing, 'monitor_paused' is 0. When
123 * notifications are paused, 'monitor_paused' is the value of
124 * 'monitor_seqno' at the point we paused.
126 * 'monitor_counter' counts the OpenFlow messages and bytes currently in
127 * flight. This value growing too large triggers pausing. */
128 uint64_t monitor_paused OVS_GUARDED_BY(ofproto_mutex);
129 struct rconn_packet_counter *monitor_counter OVS_GUARDED_BY(ofproto_mutex);
131 /* State of monitors for a single ongoing flow_mod.
133 * 'updates' is a list of "struct ofpbuf"s that contain
134 * NXST_FLOW_MONITOR_REPLY messages representing the changes made by the
137 * When 'updates' is nonempty, 'sent_abbrev_update' is true if 'updates'
138 * contains an update event of type NXFME_ABBREV and false otherwise.. */
139 struct list updates OVS_GUARDED_BY(ofproto_mutex);
140 bool sent_abbrev_update OVS_GUARDED_BY(ofproto_mutex);
142 /* Active bundles. Contains "struct ofp_bundle"s. */
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 bool (*handle_openflow)(struct ofconn *,
157 const struct ofpbuf *ofp_msg));
158 static void ofconn_wait(struct ofconn *, bool handling_openflow);
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 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; /* Controller "struct ofconn"s. */
204 struct list all_conns; /* Contains "struct ofconn"s. */
205 uint64_t master_election_id; /* monotonically increasing sequence number
206 * for master election */
207 bool master_election_id_defined;
209 /* OpenFlow listeners. */
210 struct hmap services; /* Contains "struct ofservice"s. */
211 struct pvconn **snoops;
215 struct fail_open *fail_open;
216 enum ofproto_fail_mode fail_mode;
218 /* In-band control. */
219 struct in_band *in_band;
220 struct sockaddr_in *extra_in_band_remotes;
221 size_t n_extra_remotes;
225 static void update_in_band_remotes(struct connmgr *);
226 static void add_snooper(struct connmgr *, struct vconn *);
227 static void ofmonitor_run(struct connmgr *);
228 static void ofmonitor_wait(struct connmgr *);
230 /* Creates and returns a new connection manager owned by 'ofproto'. 'name' is
231 * a name for the ofproto suitable for using in log messages.
232 * 'local_port_name' is the name of the local port (OFPP_LOCAL) within
235 connmgr_create(struct ofproto *ofproto,
236 const char *name, const char *local_port_name)
240 mgr = xmalloc(sizeof *mgr);
241 mgr->ofproto = ofproto;
242 mgr->name = xstrdup(name);
243 mgr->local_port_name = xstrdup(local_port_name);
245 hmap_init(&mgr->controllers);
246 list_init(&mgr->all_conns);
247 mgr->master_election_id = 0;
248 mgr->master_election_id_defined = false;
250 hmap_init(&mgr->services);
254 mgr->fail_open = NULL;
255 mgr->fail_mode = OFPROTO_FAIL_SECURE;
258 mgr->extra_in_band_remotes = NULL;
259 mgr->n_extra_remotes = 0;
260 mgr->in_band_queue = -1;
265 /* Frees 'mgr' and all of its resources. */
267 connmgr_destroy(struct connmgr *mgr)
269 struct ofservice *ofservice, *next_ofservice;
270 struct ofconn *ofconn, *next_ofconn;
277 ovs_mutex_lock(&ofproto_mutex);
278 LIST_FOR_EACH_SAFE (ofconn, next_ofconn, node, &mgr->all_conns) {
279 ofconn_destroy(ofconn);
281 ovs_mutex_unlock(&ofproto_mutex);
283 hmap_destroy(&mgr->controllers);
285 HMAP_FOR_EACH_SAFE (ofservice, next_ofservice, node, &mgr->services) {
286 ofservice_destroy(mgr, ofservice);
288 hmap_destroy(&mgr->services);
290 for (i = 0; i < mgr->n_snoops; i++) {
291 pvconn_close(mgr->snoops[i]);
295 fail_open_destroy(mgr->fail_open);
296 mgr->fail_open = NULL;
298 in_band_destroy(mgr->in_band);
300 free(mgr->extra_in_band_remotes);
302 free(mgr->local_port_name);
307 /* Does all of the periodic maintenance required by 'mgr'.
309 * If 'handle_openflow' is nonnull, calls 'handle_openflow' for each message
310 * received on an OpenFlow connection, passing along the OpenFlow connection
311 * itself and the message that was sent. If 'handle_openflow' returns true,
312 * the message is considered to be fully processed. If 'handle_openflow'
313 * returns false, the message is considered not to have been processed at all;
314 * it will be stored and re-presented to 'handle_openflow' following the next
315 * call to connmgr_retry(). 'handle_openflow' must not modify or free the
318 * If 'handle_openflow' is NULL, no OpenFlow messages will be processed and
319 * other activities that could affect the flow table (in-band processing,
320 * fail-open processing) are suppressed too. */
322 connmgr_run(struct connmgr *mgr,
323 bool (*handle_openflow)(struct ofconn *,
324 const struct ofpbuf *ofp_msg))
325 OVS_EXCLUDED(ofproto_mutex)
327 struct ofconn *ofconn, *next_ofconn;
328 struct ofservice *ofservice;
331 if (handle_openflow && mgr->in_band) {
332 if (!in_band_run(mgr->in_band)) {
333 in_band_destroy(mgr->in_band);
338 LIST_FOR_EACH_SAFE (ofconn, next_ofconn, node, &mgr->all_conns) {
339 ofconn_run(ofconn, handle_openflow);
343 /* Fail-open maintenance. Do this after processing the ofconns since
344 * fail-open checks the status of the controller rconn. */
345 if (handle_openflow && mgr->fail_open) {
346 fail_open_run(mgr->fail_open);
349 HMAP_FOR_EACH (ofservice, node, &mgr->services) {
353 retval = pvconn_accept(ofservice->pvconn, &vconn);
358 /* Passing default value for creation of the rconn */
359 rconn = rconn_create(ofservice->probe_interval, 0, ofservice->dscp,
360 vconn_get_allowed_versions(vconn));
361 name = ofconn_make_name(mgr, vconn_get_name(vconn));
362 rconn_connect_unreliably(rconn, vconn, name);
365 ovs_mutex_lock(&ofproto_mutex);
366 ofconn = ofconn_create(mgr, rconn, OFCONN_SERVICE,
367 ofservice->enable_async_msgs);
368 ovs_mutex_unlock(&ofproto_mutex);
370 ofconn_set_rate_limit(ofconn, ofservice->rate_limit,
371 ofservice->burst_limit);
372 } else if (retval != EAGAIN) {
373 VLOG_WARN_RL(&rl, "accept failed (%s)", ovs_strerror(retval));
377 for (i = 0; i < mgr->n_snoops; i++) {
381 retval = pvconn_accept(mgr->snoops[i], &vconn);
383 add_snooper(mgr, vconn);
384 } else if (retval != EAGAIN) {
385 VLOG_WARN_RL(&rl, "accept failed (%s)", ovs_strerror(retval));
390 /* Causes the poll loop to wake up when connmgr_run() needs to run.
392 * If 'handling_openflow' is true, arriving OpenFlow messages and other
393 * activities that affect the flow table will wake up the poll loop. If
394 * 'handling_openflow' is false, they will not. */
396 connmgr_wait(struct connmgr *mgr, bool handling_openflow)
398 struct ofservice *ofservice;
399 struct ofconn *ofconn;
402 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
403 ofconn_wait(ofconn, handling_openflow);
406 if (handling_openflow && mgr->in_band) {
407 in_band_wait(mgr->in_band);
409 if (handling_openflow && mgr->fail_open) {
410 fail_open_wait(mgr->fail_open);
412 HMAP_FOR_EACH (ofservice, node, &mgr->services) {
413 pvconn_wait(ofservice->pvconn);
415 for (i = 0; i < mgr->n_snoops; i++) {
416 pvconn_wait(mgr->snoops[i]);
420 /* Adds some memory usage statistics for 'mgr' into 'usage', for use with
421 * memory_report(). */
423 connmgr_get_memory_usage(const struct connmgr *mgr, struct simap *usage)
425 const struct ofconn *ofconn;
426 unsigned int packets = 0;
427 unsigned int ofconns = 0;
429 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
434 packets += rconn_count_txqlen(ofconn->rconn);
435 for (i = 0; i < N_SCHEDULERS; i++) {
436 packets += pinsched_count_txqlen(ofconn->schedulers[i]);
438 packets += pktbuf_count_packets(ofconn->pktbuf);
440 simap_increase(usage, "ofconns", ofconns);
441 simap_increase(usage, "packets", packets);
444 /* Returns the ofproto that owns 'ofconn''s connmgr. */
446 ofconn_get_ofproto(const struct ofconn *ofconn)
448 return ofconn->connmgr->ofproto;
451 /* If processing of OpenFlow messages was blocked on any 'mgr' ofconns by
452 * returning false to the 'handle_openflow' callback to connmgr_run(), this
453 * re-enables them. */
455 connmgr_retry(struct connmgr *mgr)
457 struct ofconn *ofconn;
459 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
460 ofconn->retry = true;
464 /* OpenFlow configuration. */
466 static void add_controller(struct connmgr *, const char *target, uint8_t dscp,
467 uint32_t allowed_versions)
468 OVS_REQUIRES(ofproto_mutex);
469 static struct ofconn *find_controller_by_target(struct connmgr *,
471 static void update_fail_open(struct connmgr *) OVS_EXCLUDED(ofproto_mutex);
472 static int set_pvconns(struct pvconn ***pvconnsp, size_t *n_pvconnsp,
473 const struct sset *);
475 /* Returns true if 'mgr' has any configured primary controllers.
477 * Service controllers do not count, but configured primary controllers do
478 * count whether or not they are currently connected. */
480 connmgr_has_controllers(const struct connmgr *mgr)
482 return !hmap_is_empty(&mgr->controllers);
485 /* Initializes 'info' and populates it with information about each configured
486 * primary controller. The keys in 'info' are the controllers' targets; the
487 * data values are corresponding "struct ofproto_controller_info".
489 * The caller owns 'info' and everything in it and should free it when it is no
492 connmgr_get_controller_info(struct connmgr *mgr, struct shash *info)
494 const struct ofconn *ofconn;
496 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
497 const struct rconn *rconn = ofconn->rconn;
498 const char *target = rconn_get_target(rconn);
500 if (!shash_find(info, target)) {
501 struct ofproto_controller_info *cinfo = xmalloc(sizeof *cinfo);
502 time_t now = time_now();
503 time_t last_connection = rconn_get_last_connection(rconn);
504 time_t last_disconnect = rconn_get_last_disconnect(rconn);
505 int last_error = rconn_get_last_error(rconn);
507 shash_add(info, target, cinfo);
509 cinfo->is_connected = rconn_is_connected(rconn);
510 cinfo->role = ofconn->role;
515 cinfo->pairs.keys[cinfo->pairs.n] = "last_error";
516 cinfo->pairs.values[cinfo->pairs.n++]
517 = xstrdup(ovs_retval_to_string(last_error));
520 cinfo->pairs.keys[cinfo->pairs.n] = "state";
521 cinfo->pairs.values[cinfo->pairs.n++]
522 = xstrdup(rconn_get_state(rconn));
524 if (last_connection != TIME_MIN) {
525 cinfo->pairs.keys[cinfo->pairs.n] = "sec_since_connect";
526 cinfo->pairs.values[cinfo->pairs.n++]
527 = xasprintf("%ld", (long int) (now - last_connection));
530 if (last_disconnect != TIME_MIN) {
531 cinfo->pairs.keys[cinfo->pairs.n] = "sec_since_disconnect";
532 cinfo->pairs.values[cinfo->pairs.n++]
533 = xasprintf("%ld", (long int) (now - last_disconnect));
540 connmgr_free_controller_info(struct shash *info)
542 struct shash_node *node;
544 SHASH_FOR_EACH (node, info) {
545 struct ofproto_controller_info *cinfo = node->data;
546 while (cinfo->pairs.n) {
547 free(CONST_CAST(char *, cinfo->pairs.values[--cinfo->pairs.n]));
554 /* Changes 'mgr''s set of controllers to the 'n_controllers' controllers in
557 connmgr_set_controllers(struct connmgr *mgr,
558 const struct ofproto_controller *controllers,
559 size_t n_controllers, uint32_t allowed_versions)
560 OVS_EXCLUDED(ofproto_mutex)
562 bool had_controllers = connmgr_has_controllers(mgr);
563 struct shash new_controllers;
564 struct ofconn *ofconn, *next_ofconn;
565 struct ofservice *ofservice, *next_ofservice;
568 /* Required to add and remove ofconns. This could probably be narrowed to
569 * cover a smaller amount of code, if that yielded some benefit. */
570 ovs_mutex_lock(&ofproto_mutex);
572 /* Create newly configured controllers and services.
573 * Create a name to ofproto_controller mapping in 'new_controllers'. */
574 shash_init(&new_controllers);
575 for (i = 0; i < n_controllers; i++) {
576 const struct ofproto_controller *c = &controllers[i];
578 if (!vconn_verify_name(c->target)) {
580 ofconn = find_controller_by_target(mgr, c->target);
582 VLOG_INFO("%s: added primary controller \"%s\"",
583 mgr->name, c->target);
585 } else if (rconn_get_allowed_versions(ofconn->rconn) !=
587 VLOG_INFO("%s: re-added primary controller \"%s\"",
588 mgr->name, c->target);
590 ofconn_destroy(ofconn);
593 add_controller(mgr, c->target, c->dscp, allowed_versions);
595 } else if (!pvconn_verify_name(c->target)) {
597 ofservice = ofservice_lookup(mgr, c->target);
599 VLOG_INFO("%s: added service controller \"%s\"",
600 mgr->name, c->target);
602 } else if (ofservice->allowed_versions != allowed_versions) {
603 VLOG_INFO("%s: re-added service controller \"%s\"",
604 mgr->name, c->target);
605 ofservice_destroy(mgr, ofservice);
609 ofservice_create(mgr, c->target, allowed_versions, c->dscp);
612 VLOG_WARN_RL(&rl, "%s: unsupported controller \"%s\"",
613 mgr->name, c->target);
617 shash_add_once(&new_controllers, c->target, &controllers[i]);
620 /* Delete controllers that are no longer configured.
621 * Update configuration of all now-existing controllers. */
622 HMAP_FOR_EACH_SAFE (ofconn, next_ofconn, hmap_node, &mgr->controllers) {
623 const char *target = ofconn_get_target(ofconn);
624 struct ofproto_controller *c;
626 c = shash_find_data(&new_controllers, target);
628 VLOG_INFO("%s: removed primary controller \"%s\"",
630 ofconn_destroy(ofconn);
632 ofconn_reconfigure(ofconn, c);
636 /* Delete services that are no longer configured.
637 * Update configuration of all now-existing services. */
638 HMAP_FOR_EACH_SAFE (ofservice, next_ofservice, node, &mgr->services) {
639 const char *target = pvconn_get_name(ofservice->pvconn);
640 struct ofproto_controller *c;
642 c = shash_find_data(&new_controllers, target);
644 VLOG_INFO("%s: removed service controller \"%s\"",
646 ofservice_destroy(mgr, ofservice);
648 ofservice_reconfigure(ofservice, c);
652 shash_destroy(&new_controllers);
654 ovs_mutex_unlock(&ofproto_mutex);
656 update_in_band_remotes(mgr);
657 update_fail_open(mgr);
658 if (had_controllers != connmgr_has_controllers(mgr)) {
659 ofproto_flush_flows(mgr->ofproto);
663 /* Drops the connections between 'mgr' and all of its primary and secondary
664 * controllers, forcing them to reconnect. */
666 connmgr_reconnect(const struct connmgr *mgr)
668 struct ofconn *ofconn;
670 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
671 rconn_reconnect(ofconn->rconn);
675 /* Sets the "snoops" for 'mgr' to the pvconn targets listed in 'snoops'.
677 * A "snoop" is a pvconn to which every OpenFlow message to or from the most
678 * important controller on 'mgr' is mirrored. */
680 connmgr_set_snoops(struct connmgr *mgr, const struct sset *snoops)
682 return set_pvconns(&mgr->snoops, &mgr->n_snoops, snoops);
685 /* Adds each of the snoops currently configured on 'mgr' to 'snoops'. */
687 connmgr_get_snoops(const struct connmgr *mgr, struct sset *snoops)
691 for (i = 0; i < mgr->n_snoops; i++) {
692 sset_add(snoops, pvconn_get_name(mgr->snoops[i]));
696 /* Returns true if 'mgr' has at least one snoop, false if it has none. */
698 connmgr_has_snoops(const struct connmgr *mgr)
700 return mgr->n_snoops > 0;
703 /* Creates a new controller for 'target' in 'mgr'. update_controller() needs
704 * to be called later to finish the new ofconn's configuration. */
706 add_controller(struct connmgr *mgr, const char *target, uint8_t dscp,
707 uint32_t allowed_versions)
708 OVS_REQUIRES(ofproto_mutex)
710 char *name = ofconn_make_name(mgr, target);
711 struct ofconn *ofconn;
713 ofconn = ofconn_create(mgr, rconn_create(5, 8, dscp, allowed_versions),
714 OFCONN_PRIMARY, true);
715 ofconn->pktbuf = pktbuf_create();
716 rconn_connect(ofconn->rconn, target, name);
717 hmap_insert(&mgr->controllers, &ofconn->hmap_node, hash_string(target, 0));
722 static struct ofconn *
723 find_controller_by_target(struct connmgr *mgr, const char *target)
725 struct ofconn *ofconn;
727 HMAP_FOR_EACH_WITH_HASH (ofconn, hmap_node,
728 hash_string(target, 0), &mgr->controllers) {
729 if (!strcmp(ofconn_get_target(ofconn), target)) {
737 update_in_band_remotes(struct connmgr *mgr)
739 struct sockaddr_in *addrs;
740 size_t max_addrs, n_addrs;
741 struct ofconn *ofconn;
744 /* Allocate enough memory for as many remotes as we could possibly have. */
745 max_addrs = mgr->n_extra_remotes + hmap_count(&mgr->controllers);
746 addrs = xmalloc(max_addrs * sizeof *addrs);
749 /* Add all the remotes. */
750 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
751 const char *target = rconn_get_target(ofconn->rconn);
752 struct sockaddr_storage ss;
754 if (ofconn->band == OFPROTO_IN_BAND
755 && stream_parse_target_with_default_port(target, OFP_OLD_PORT, &ss)
756 && ss.ss_family == AF_INET) {
757 addrs[n_addrs++] = *(struct sockaddr_in *) &ss;
760 for (i = 0; i < mgr->n_extra_remotes; i++) {
761 addrs[n_addrs++] = mgr->extra_in_band_remotes[i];
764 /* Create or update or destroy in-band. */
767 in_band_create(mgr->ofproto, mgr->local_port_name, &mgr->in_band);
769 in_band_set_queue(mgr->in_band, mgr->in_band_queue);
771 /* in_band_run() needs a chance to delete any existing in-band flows.
772 * We will destroy mgr->in_band after it's done with that. */
775 in_band_set_remotes(mgr->in_band, addrs, n_addrs);
783 update_fail_open(struct connmgr *mgr)
784 OVS_EXCLUDED(ofproto_mutex)
786 if (connmgr_has_controllers(mgr)
787 && mgr->fail_mode == OFPROTO_FAIL_STANDALONE) {
788 if (!mgr->fail_open) {
789 mgr->fail_open = fail_open_create(mgr->ofproto, mgr);
792 fail_open_destroy(mgr->fail_open);
793 mgr->fail_open = NULL;
798 set_pvconns(struct pvconn ***pvconnsp, size_t *n_pvconnsp,
799 const struct sset *sset)
801 struct pvconn **pvconns = *pvconnsp;
802 size_t n_pvconns = *n_pvconnsp;
807 for (i = 0; i < n_pvconns; i++) {
808 pvconn_close(pvconns[i]);
812 pvconns = xmalloc(sset_count(sset) * sizeof *pvconns);
814 SSET_FOR_EACH (name, sset) {
815 struct pvconn *pvconn;
817 error = pvconn_open(name, 0, 0, &pvconn);
819 pvconns[n_pvconns++] = pvconn;
821 VLOG_ERR("failed to listen on %s: %s", name, ovs_strerror(error));
829 *n_pvconnsp = n_pvconns;
834 /* Returns a "preference level" for snooping 'ofconn'. A higher return value
835 * means that 'ofconn' is more interesting for monitoring than a lower return
838 snoop_preference(const struct ofconn *ofconn)
840 switch (ofconn->role) {
841 case OFPCR12_ROLE_MASTER:
843 case OFPCR12_ROLE_EQUAL:
845 case OFPCR12_ROLE_SLAVE:
847 case OFPCR12_ROLE_NOCHANGE:
849 /* Shouldn't happen. */
854 /* One of 'mgr''s "snoop" pvconns has accepted a new connection on 'vconn'.
855 * Connects this vconn to a controller. */
857 add_snooper(struct connmgr *mgr, struct vconn *vconn)
859 struct ofconn *ofconn, *best;
861 /* Pick a controller for monitoring. */
863 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
864 if (ofconn->type == OFCONN_PRIMARY
865 && (!best || snoop_preference(ofconn) > snoop_preference(best))) {
871 rconn_add_monitor(best->rconn, vconn);
873 VLOG_INFO_RL(&rl, "no controller connection to snoop");
878 /* Public ofconn functions. */
880 /* Returns the connection type, either OFCONN_PRIMARY or OFCONN_SERVICE. */
882 ofconn_get_type(const struct ofconn *ofconn)
887 /* If a master election id is defined, stores it into '*idp' and returns
888 * true. Otherwise, stores UINT64_MAX into '*idp' and returns false. */
890 ofconn_get_master_election_id(const struct ofconn *ofconn, uint64_t *idp)
892 *idp = (ofconn->connmgr->master_election_id_defined
893 ? ofconn->connmgr->master_election_id
895 return ofconn->connmgr->master_election_id_defined;
898 /* Sets the master election id.
900 * Returns true if successful, false if the id is stale
903 ofconn_set_master_election_id(struct ofconn *ofconn, uint64_t id)
905 if (ofconn->connmgr->master_election_id_defined
907 /* Unsigned difference interpreted as a two's complement signed
909 (int64_t)(id - ofconn->connmgr->master_election_id) < 0) {
912 ofconn->connmgr->master_election_id = id;
913 ofconn->connmgr->master_election_id_defined = true;
918 /* Returns the role configured for 'ofconn'.
920 * The default role, if no other role has been set, is OFPCR12_ROLE_EQUAL. */
921 enum ofp12_controller_role
922 ofconn_get_role(const struct ofconn *ofconn)
928 ofconn_send_role_status(struct ofconn *ofconn, uint32_t role, uint8_t reason)
930 struct ofputil_role_status status;
933 status.reason = reason;
935 ofconn_get_master_election_id(ofconn, &status.generation_id);
937 buf = ofputil_encode_role_status(&status, ofconn_get_protocol(ofconn));
939 ofconn_send(ofconn, buf, NULL);
942 /* Changes 'ofconn''s role to 'role'. If 'role' is OFPCR12_ROLE_MASTER then
943 * any existing master is demoted to a slave. */
945 ofconn_set_role(struct ofconn *ofconn, enum ofp12_controller_role role)
947 if (role != ofconn->role && role == OFPCR12_ROLE_MASTER) {
948 struct ofconn *other;
950 HMAP_FOR_EACH (other, hmap_node, &ofconn->connmgr->controllers) {
951 if (other->role == OFPCR12_ROLE_MASTER) {
952 other->role = OFPCR12_ROLE_SLAVE;
953 ofconn_send_role_status(other, OFPCR12_ROLE_SLAVE, OFPCRR_MASTER_REQUEST);
961 ofconn_set_invalid_ttl_to_controller(struct ofconn *ofconn, bool enable)
963 uint32_t bit = 1u << OFPR_INVALID_TTL;
965 ofconn->master_async_config[OAM_PACKET_IN] |= bit;
967 ofconn->master_async_config[OAM_PACKET_IN] &= ~bit;
972 ofconn_get_invalid_ttl_to_controller(struct ofconn *ofconn)
974 uint32_t bit = 1u << OFPR_INVALID_TTL;
975 return (ofconn->master_async_config[OAM_PACKET_IN] & bit) != 0;
978 /* Returns the currently configured protocol for 'ofconn', one of OFPUTIL_P_*.
980 * Returns OFPUTIL_P_NONE, which is not a valid protocol, if 'ofconn' hasn't
981 * completed version negotiation. This can't happen if at least one OpenFlow
982 * message, other than OFPT_HELLO, has been received on the connection (such as
983 * in ofproto.c's message handling code), since version negotiation is a
984 * prerequisite for starting to receive messages. This means that
985 * OFPUTIL_P_NONE is a special case that most callers need not worry about. */
986 enum ofputil_protocol
987 ofconn_get_protocol(const struct ofconn *ofconn)
989 if (ofconn->protocol == OFPUTIL_P_NONE &&
990 rconn_is_connected(ofconn->rconn)) {
991 int version = rconn_get_version(ofconn->rconn);
993 ofconn_set_protocol(CONST_CAST(struct ofconn *, ofconn),
994 ofputil_protocol_from_ofp_version(version));
998 return ofconn->protocol;
1001 /* Sets the protocol for 'ofconn' to 'protocol' (one of OFPUTIL_P_*).
1003 * (This doesn't actually send anything to accomplish this. Presumably the
1004 * caller already did that.) */
1006 ofconn_set_protocol(struct ofconn *ofconn, enum ofputil_protocol protocol)
1008 ofconn->protocol = protocol;
1011 /* Returns the currently configured packet in format for 'ofconn', one of
1014 * The default, if no other format has been set, is NXPIF_OPENFLOW10. */
1015 enum nx_packet_in_format
1016 ofconn_get_packet_in_format(struct ofconn *ofconn)
1018 return ofconn->packet_in_format;
1021 /* Sets the packet in format for 'ofconn' to 'packet_in_format' (one of
1024 ofconn_set_packet_in_format(struct ofconn *ofconn,
1025 enum nx_packet_in_format packet_in_format)
1027 ofconn->packet_in_format = packet_in_format;
1030 /* Sets the controller connection ID for 'ofconn' to 'controller_id'.
1032 * The connection controller ID is used for OFPP_CONTROLLER and
1033 * NXAST_CONTROLLER actions. See "struct nx_action_controller" for details. */
1035 ofconn_set_controller_id(struct ofconn *ofconn, uint16_t controller_id)
1037 ofconn->controller_id = controller_id;
1040 /* Returns the default miss send length for 'ofconn'. */
1042 ofconn_get_miss_send_len(const struct ofconn *ofconn)
1044 return ofconn->miss_send_len;
1047 /* Sets the default miss send length for 'ofconn' to 'miss_send_len'. */
1049 ofconn_set_miss_send_len(struct ofconn *ofconn, int miss_send_len)
1051 ofconn->miss_send_len = miss_send_len;
1055 ofconn_set_async_config(struct ofconn *ofconn,
1056 const uint32_t master_masks[OAM_N_TYPES],
1057 const uint32_t slave_masks[OAM_N_TYPES])
1059 size_t size = sizeof ofconn->master_async_config;
1060 memcpy(ofconn->master_async_config, master_masks, size);
1061 memcpy(ofconn->slave_async_config, slave_masks, size);
1065 ofconn_get_async_config(struct ofconn *ofconn,
1066 uint32_t *master_masks, uint32_t *slave_masks)
1068 size_t size = sizeof ofconn->master_async_config;
1069 memcpy(master_masks, ofconn->master_async_config, size);
1070 memcpy(slave_masks, ofconn->slave_async_config, size);
1073 /* Sends 'msg' on 'ofconn', accounting it as a reply. (If there is a
1074 * sufficient number of OpenFlow replies in-flight on a single ofconn, then the
1075 * connmgr will stop accepting new OpenFlow requests on that ofconn until the
1076 * controller has accepted some of the replies.) */
1078 ofconn_send_reply(const struct ofconn *ofconn, struct ofpbuf *msg)
1080 ofconn_send(ofconn, msg, ofconn->reply_counter);
1083 /* Sends each of the messages in list 'replies' on 'ofconn' in order,
1084 * accounting them as replies. */
1086 ofconn_send_replies(const struct ofconn *ofconn, struct list *replies)
1088 struct ofpbuf *reply, *next;
1090 LIST_FOR_EACH_SAFE (reply, next, list_node, replies) {
1091 list_remove(&reply->list_node);
1092 ofconn_send_reply(ofconn, reply);
1096 /* Sends 'error' on 'ofconn', as a reply to 'request'. Only at most the
1097 * first 64 bytes of 'request' are used. */
1099 ofconn_send_error(const struct ofconn *ofconn,
1100 const struct ofp_header *request, enum ofperr error)
1102 static struct vlog_rate_limit err_rl = VLOG_RATE_LIMIT_INIT(10, 10);
1103 struct ofpbuf *reply;
1105 reply = ofperr_encode_reply(error, request);
1106 if (!VLOG_DROP_INFO(&err_rl)) {
1107 const char *type_name;
1111 request_len = ntohs(request->length);
1112 type_name = (!ofpraw_decode_partial(&raw, request,
1113 MIN(64, request_len))
1114 ? ofpraw_get_name(raw)
1117 VLOG_INFO("%s: sending %s error reply to %s message",
1118 rconn_get_name(ofconn->rconn), ofperr_to_string(error),
1121 ofconn_send_reply(ofconn, reply);
1124 /* Same as pktbuf_retrieve(), using the pktbuf owned by 'ofconn'. */
1126 ofconn_pktbuf_retrieve(struct ofconn *ofconn, uint32_t id,
1127 struct ofpbuf **bufferp, ofp_port_t *in_port)
1129 return pktbuf_retrieve(ofconn->pktbuf, id, bufferp, in_port);
1132 /* Reports that a flow_mod operation of the type specified by 'command' was
1133 * successfully executed by 'ofconn', so that the connmgr can log it. */
1135 ofconn_report_flow_mod(struct ofconn *ofconn,
1136 enum ofp_flow_mod_command command)
1146 case OFPFC_MODIFY_STRICT:
1151 case OFPFC_DELETE_STRICT:
1157 if (ofconn->next_op_report == LLONG_MAX) {
1158 ofconn->first_op = now;
1159 ofconn->next_op_report = MAX(now + 10 * 1000, ofconn->op_backoff);
1160 ofconn->op_backoff = ofconn->next_op_report + 60 * 1000;
1162 ofconn->last_op = now;
1165 /* Returns true if 'ofconn' has any pending opgroups. */
1167 ofconn_has_pending_opgroups(const struct ofconn *ofconn)
1169 return !list_is_empty(&ofconn->opgroups);
1172 /* Adds 'ofconn_node' to 'ofconn''s list of pending opgroups.
1174 * If 'ofconn' is destroyed or its connection drops, then 'ofconn' will remove
1175 * 'ofconn_node' from the list and re-initialize it with list_init(). The
1176 * client may, therefore, use list_is_empty(ofconn_node) to determine whether
1177 * 'ofconn_node' is still associated with an active ofconn.
1179 * The client may also remove ofconn_node from the list itself, with
1182 ofconn_add_opgroup(struct ofconn *ofconn, struct list *ofconn_node)
1184 list_push_back(&ofconn->opgroups, ofconn_node);
1188 ofconn_get_bundles(struct ofconn *ofconn)
1190 return &ofconn->bundles;
1194 /* Private ofconn functions. */
1197 ofconn_get_target(const struct ofconn *ofconn)
1199 return rconn_get_target(ofconn->rconn);
1202 static struct ofconn *
1203 ofconn_create(struct connmgr *mgr, struct rconn *rconn, enum ofconn_type type,
1204 bool enable_async_msgs)
1206 struct ofconn *ofconn;
1208 ofconn = xzalloc(sizeof *ofconn);
1209 ofconn->connmgr = mgr;
1210 list_push_back(&mgr->all_conns, &ofconn->node);
1211 ofconn->rconn = rconn;
1212 ofconn->type = type;
1213 ofconn->enable_async_msgs = enable_async_msgs;
1215 list_init(&ofconn->opgroups);
1217 hmap_init(&ofconn->monitors);
1218 list_init(&ofconn->updates);
1220 hmap_init(&ofconn->bundles);
1222 ofconn_flush(ofconn);
1227 /* Clears all of the state in 'ofconn' that should not persist from one
1228 * connection to the next. */
1230 ofconn_flush(struct ofconn *ofconn)
1231 OVS_REQUIRES(ofproto_mutex)
1233 struct ofmonitor *monitor, *next_monitor;
1236 ofconn_log_flow_mods(ofconn);
1238 ofconn->role = OFPCR12_ROLE_EQUAL;
1239 ofconn_set_protocol(ofconn, OFPUTIL_P_NONE);
1240 ofconn->packet_in_format = NXPIF_OPENFLOW10;
1242 /* Disassociate 'ofconn' from all of the ofopgroups that it initiated that
1243 * have not yet completed. (Those ofopgroups will still run to completion
1244 * in the usual way, but any errors that they run into will not be reported
1245 * on any OpenFlow channel.)
1247 * Also discard any blocked operation on 'ofconn'. */
1248 while (!list_is_empty(&ofconn->opgroups)) {
1249 list_init(list_pop_front(&ofconn->opgroups));
1251 ofpbuf_delete(ofconn->blocked);
1252 ofconn->blocked = NULL;
1254 rconn_packet_counter_destroy(ofconn->packet_in_counter);
1255 ofconn->packet_in_counter = rconn_packet_counter_create();
1256 for (i = 0; i < N_SCHEDULERS; i++) {
1257 if (ofconn->schedulers[i]) {
1260 pinsched_get_limits(ofconn->schedulers[i], &rate, &burst);
1261 pinsched_destroy(ofconn->schedulers[i]);
1262 ofconn->schedulers[i] = pinsched_create(rate, burst);
1265 if (ofconn->pktbuf) {
1266 pktbuf_destroy(ofconn->pktbuf);
1267 ofconn->pktbuf = pktbuf_create();
1269 ofconn->miss_send_len = (ofconn->type == OFCONN_PRIMARY
1270 ? OFP_DEFAULT_MISS_SEND_LEN
1272 ofconn->controller_id = 0;
1274 rconn_packet_counter_destroy(ofconn->reply_counter);
1275 ofconn->reply_counter = rconn_packet_counter_create();
1277 if (ofconn->enable_async_msgs) {
1278 uint32_t *master = ofconn->master_async_config;
1279 uint32_t *slave = ofconn->slave_async_config;
1281 /* "master" and "other" roles get all asynchronous messages by default,
1282 * except that the controller needs to enable nonstandard "packet-in"
1283 * reasons itself. */
1284 master[OAM_PACKET_IN] = (1u << OFPR_NO_MATCH) | (1u << OFPR_ACTION);
1285 master[OAM_PORT_STATUS] = ((1u << OFPPR_ADD)
1286 | (1u << OFPPR_DELETE)
1287 | (1u << OFPPR_MODIFY));
1288 master[OAM_FLOW_REMOVED] = ((1u << OFPRR_IDLE_TIMEOUT)
1289 | (1u << OFPRR_HARD_TIMEOUT)
1290 | (1u << OFPRR_DELETE));
1292 /* "slave" role gets port status updates by default. */
1293 slave[OAM_PACKET_IN] = 0;
1294 slave[OAM_PORT_STATUS] = ((1u << OFPPR_ADD)
1295 | (1u << OFPPR_DELETE)
1296 | (1u << OFPPR_MODIFY));
1297 slave[OAM_FLOW_REMOVED] = 0;
1299 memset(ofconn->master_async_config, 0,
1300 sizeof ofconn->master_async_config);
1301 memset(ofconn->slave_async_config, 0,
1302 sizeof ofconn->slave_async_config);
1305 ofconn->n_add = ofconn->n_delete = ofconn->n_modify = 0;
1306 ofconn->first_op = ofconn->last_op = LLONG_MIN;
1307 ofconn->next_op_report = LLONG_MAX;
1308 ofconn->op_backoff = LLONG_MIN;
1310 HMAP_FOR_EACH_SAFE (monitor, next_monitor, ofconn_node,
1311 &ofconn->monitors) {
1312 ofmonitor_destroy(monitor);
1314 rconn_packet_counter_destroy(ofconn->monitor_counter);
1315 ofconn->monitor_counter = rconn_packet_counter_create();
1316 ofpbuf_list_delete(&ofconn->updates); /* ...but it should be empty. */
1320 ofconn_destroy(struct ofconn *ofconn)
1321 OVS_REQUIRES(ofproto_mutex)
1323 ofconn_flush(ofconn);
1325 if (ofconn->type == OFCONN_PRIMARY) {
1326 hmap_remove(&ofconn->connmgr->controllers, &ofconn->hmap_node);
1329 ofp_bundle_remove_all(ofconn);
1331 hmap_destroy(&ofconn->monitors);
1332 list_remove(&ofconn->node);
1333 rconn_destroy(ofconn->rconn);
1334 rconn_packet_counter_destroy(ofconn->packet_in_counter);
1335 rconn_packet_counter_destroy(ofconn->reply_counter);
1336 pktbuf_destroy(ofconn->pktbuf);
1337 rconn_packet_counter_destroy(ofconn->monitor_counter);
1341 /* Reconfigures 'ofconn' to match 'c'. 'ofconn' and 'c' must have the same
1344 ofconn_reconfigure(struct ofconn *ofconn, const struct ofproto_controller *c)
1348 ofconn->band = c->band;
1349 ofconn->enable_async_msgs = c->enable_async_msgs;
1351 rconn_set_max_backoff(ofconn->rconn, c->max_backoff);
1353 probe_interval = c->probe_interval ? MAX(c->probe_interval, 5) : 0;
1354 rconn_set_probe_interval(ofconn->rconn, probe_interval);
1356 ofconn_set_rate_limit(ofconn, c->rate_limit, c->burst_limit);
1358 /* If dscp value changed reconnect. */
1359 if (c->dscp != rconn_get_dscp(ofconn->rconn)) {
1360 rconn_set_dscp(ofconn->rconn, c->dscp);
1361 rconn_reconnect(ofconn->rconn);
1365 /* Returns true if it makes sense for 'ofconn' to receive and process OpenFlow
1368 ofconn_may_recv(const struct ofconn *ofconn)
1370 int count = rconn_packet_counter_n_packets(ofconn->reply_counter);
1371 return (!ofconn->blocked || ofconn->retry) && count < OFCONN_REPLY_MAX;
1375 ofconn_run(struct ofconn *ofconn,
1376 bool (*handle_openflow)(struct ofconn *,
1377 const struct ofpbuf *ofp_msg))
1379 struct connmgr *mgr = ofconn->connmgr;
1382 for (i = 0; i < N_SCHEDULERS; i++) {
1385 pinsched_run(ofconn->schedulers[i], &txq);
1386 do_send_packet_ins(ofconn, &txq);
1389 rconn_run(ofconn->rconn);
1391 if (handle_openflow) {
1392 /* Limit the number of iterations to avoid starving other tasks. */
1393 for (i = 0; i < 50 && ofconn_may_recv(ofconn); i++) {
1394 struct ofpbuf *of_msg;
1396 of_msg = (ofconn->blocked
1398 : rconn_recv(ofconn->rconn));
1402 if (mgr->fail_open) {
1403 fail_open_maybe_recover(mgr->fail_open);
1406 if (handle_openflow(ofconn, of_msg)) {
1407 ofpbuf_delete(of_msg);
1408 ofconn->blocked = NULL;
1410 ofconn->blocked = of_msg;
1411 ofconn->retry = false;
1417 if (time_msec() >= ofconn->next_op_report) {
1418 ofconn_log_flow_mods(ofconn);
1421 ovs_mutex_lock(&ofproto_mutex);
1422 if (!rconn_is_alive(ofconn->rconn)) {
1423 ofconn_destroy(ofconn);
1424 } else if (!rconn_is_connected(ofconn->rconn)) {
1425 ofconn_flush(ofconn);
1427 ovs_mutex_unlock(&ofproto_mutex);
1431 ofconn_wait(struct ofconn *ofconn, bool handling_openflow)
1435 for (i = 0; i < N_SCHEDULERS; i++) {
1436 pinsched_wait(ofconn->schedulers[i]);
1438 rconn_run_wait(ofconn->rconn);
1439 if (handling_openflow && ofconn_may_recv(ofconn)) {
1440 rconn_recv_wait(ofconn->rconn);
1442 if (ofconn->next_op_report != LLONG_MAX) {
1443 poll_timer_wait_until(ofconn->next_op_report);
1448 ofconn_log_flow_mods(struct ofconn *ofconn)
1450 int n_flow_mods = ofconn->n_add + ofconn->n_delete + ofconn->n_modify;
1452 long long int ago = (time_msec() - ofconn->first_op) / 1000;
1453 long long int interval = (ofconn->last_op - ofconn->first_op) / 1000;
1457 ds_put_format(&s, "%d flow_mods ", n_flow_mods);
1458 if (interval == ago) {
1459 ds_put_format(&s, "in the last %lld s", ago);
1460 } else if (interval) {
1461 ds_put_format(&s, "in the %lld s starting %lld s ago",
1464 ds_put_format(&s, "%lld s ago", ago);
1467 ds_put_cstr(&s, " (");
1468 if (ofconn->n_add) {
1469 ds_put_format(&s, "%d adds, ", ofconn->n_add);
1471 if (ofconn->n_delete) {
1472 ds_put_format(&s, "%d deletes, ", ofconn->n_delete);
1474 if (ofconn->n_modify) {
1475 ds_put_format(&s, "%d modifications, ", ofconn->n_modify);
1478 ds_put_char(&s, ')');
1480 VLOG_INFO("%s: %s", rconn_get_name(ofconn->rconn), ds_cstr(&s));
1483 ofconn->n_add = ofconn->n_delete = ofconn->n_modify = 0;
1485 ofconn->next_op_report = LLONG_MAX;
1488 /* Returns true if 'ofconn' should receive asynchronous messages of the given
1489 * OAM_* 'type' and 'reason', which should be a OFPR_* value for OAM_PACKET_IN,
1490 * a OFPPR_* value for OAM_PORT_STATUS, or an OFPRR_* value for
1491 * OAM_FLOW_REMOVED. Returns false if the message should not be sent on
1494 ofconn_receives_async_msg(const struct ofconn *ofconn,
1495 enum ofconn_async_msg_type type,
1496 unsigned int reason)
1498 const uint32_t *async_config;
1500 ovs_assert(reason < 32);
1501 ovs_assert((unsigned int) type < OAM_N_TYPES);
1503 if (ofconn_get_protocol(ofconn) == OFPUTIL_P_NONE
1504 || !rconn_is_connected(ofconn->rconn)) {
1508 /* Keep the following code in sync with the documentation in the
1509 * "Asynchronous Messages" section in DESIGN. */
1511 if (ofconn->type == OFCONN_SERVICE && !ofconn->miss_send_len) {
1512 /* Service connections don't get asynchronous messages unless they have
1513 * explicitly asked for them by setting a nonzero miss send length. */
1517 async_config = (ofconn->role == OFPCR12_ROLE_SLAVE
1518 ? ofconn->slave_async_config
1519 : ofconn->master_async_config);
1520 if (!(async_config[type] & (1u << reason))) {
1527 /* The default "table-miss" behaviour for OpenFlow1.3+ is to drop the
1528 * packet rather than to send the packet to the controller.
1530 * This function returns false to indicate the packet should be dropped if
1531 * the controller action was the result of the default table-miss behaviour
1532 * and the controller is using OpenFlow1.3+.
1534 * Otherwise true is returned to indicate the packet should be forwarded to
1537 ofconn_wants_packet_in_on_miss(struct ofconn *ofconn,
1538 const struct ofproto_packet_in *pin)
1540 if (pin->miss_type == OFPROTO_PACKET_IN_MISS_WITHOUT_FLOW) {
1541 enum ofputil_protocol protocol = ofconn_get_protocol(ofconn);
1543 if (protocol != OFPUTIL_P_NONE
1544 && ofputil_protocol_to_ofp_version(protocol) >= OFP13_VERSION) {
1545 enum ofproto_table_config config;
1547 config = ofproto_table_get_config(ofconn->connmgr->ofproto,
1549 if (config == OFPROTO_TABLE_MISS_DEFAULT) {
1557 /* The default "table-miss" behaviour for OpenFlow1.3+ is to drop the
1558 * packet rather than to send the packet to the controller.
1560 * This function returns false to indicate that a packet_in message
1561 * for a "table-miss" should be sent to at least one controller.
1562 * That is there is at least one controller with controller_id 0
1563 * which connected using an OpenFlow version earlier than OpenFlow1.3.
1567 * This logic assumes that "table-miss" packet_in messages
1568 * are always sent to controller_id 0. */
1570 connmgr_wants_packet_in_on_miss(struct connmgr *mgr)
1572 struct ofconn *ofconn;
1574 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1575 enum ofputil_protocol protocol = ofconn_get_protocol(ofconn);
1577 if (ofconn->controller_id == 0 &&
1578 (protocol == OFPUTIL_P_NONE ||
1579 ofputil_protocol_to_ofp_version(protocol) < OFP13_VERSION)) {
1586 /* Returns a human-readable name for an OpenFlow connection between 'mgr' and
1587 * 'target', suitable for use in log messages for identifying the connection.
1589 * The name is dynamically allocated. The caller should free it (with free())
1590 * when it is no longer needed. */
1592 ofconn_make_name(const struct connmgr *mgr, const char *target)
1594 return xasprintf("%s<->%s", mgr->name, target);
1598 ofconn_set_rate_limit(struct ofconn *ofconn, int rate, int burst)
1602 for (i = 0; i < N_SCHEDULERS; i++) {
1603 struct pinsched **s = &ofconn->schedulers[i];
1607 *s = pinsched_create(rate, burst);
1609 pinsched_set_limits(*s, rate, burst);
1612 pinsched_destroy(*s);
1619 ofconn_send(const struct ofconn *ofconn, struct ofpbuf *msg,
1620 struct rconn_packet_counter *counter)
1622 ofpmsg_update_length(msg);
1623 rconn_send(ofconn->rconn, msg, counter);
1626 /* Sending asynchronous messages. */
1628 static void schedule_packet_in(struct ofconn *, struct ofproto_packet_in,
1629 enum ofp_packet_in_reason wire_reason);
1631 /* Sends an OFPT_PORT_STATUS message with 'opp' and 'reason' to appropriate
1632 * controllers managed by 'mgr'. For messages caused by a controller
1633 * OFPT_PORT_MOD, specify 'source' as the controller connection that sent the
1634 * request; otherwise, specify 'source' as NULL. */
1636 connmgr_send_port_status(struct connmgr *mgr, struct ofconn *source,
1637 const struct ofputil_phy_port *pp, uint8_t reason)
1639 /* XXX Should limit the number of queued port status change messages. */
1640 struct ofputil_port_status ps;
1641 struct ofconn *ofconn;
1645 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1646 if (ofconn_receives_async_msg(ofconn, OAM_PORT_STATUS, reason)) {
1649 /* Before 1.5, OpenFlow specified that OFPT_PORT_MOD should not
1650 * generate OFPT_PORT_STATUS messages. That requirement was a
1651 * relic of how OpenFlow originally supported a single controller,
1652 * so that one could expect the controller to already know the
1653 * changes it had made.
1655 * EXT-338 changes OpenFlow 1.5 OFPT_PORT_MOD to send
1656 * OFPT_PORT_STATUS messages to every controller. This is
1657 * obviously more useful in the multi-controller case. We could
1658 * always implement it that way in OVS, but that would risk
1659 * confusing controllers that are intended for single-controller
1660 * use only. (Imagine a controller that generates an OFPT_PORT_MOD
1661 * in response to any OFPT_PORT_STATUS!)
1663 * So this compromises: for OpenFlow 1.4 and earlier, it generates
1664 * OFPT_PORT_STATUS for OFPT_PORT_MOD, but not back to the
1665 * originating controller. In a single-controller environment, in
1666 * particular, this means that it will never generate
1667 * OFPT_PORT_STATUS for OFPT_PORT_MOD at all. */
1668 if (ofconn == source
1669 && rconn_get_version(ofconn->rconn) < OFP15_VERSION) {
1673 msg = ofputil_encode_port_status(&ps, ofconn_get_protocol(ofconn));
1674 ofconn_send(ofconn, msg, NULL);
1679 /* Sends an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message based on 'fr' to
1680 * appropriate controllers managed by 'mgr'. */
1682 connmgr_send_flow_removed(struct connmgr *mgr,
1683 const struct ofputil_flow_removed *fr)
1685 struct ofconn *ofconn;
1687 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1688 if (ofconn_receives_async_msg(ofconn, OAM_FLOW_REMOVED, fr->reason)) {
1691 /* Account flow expirations as replies to OpenFlow requests. That
1692 * works because preventing OpenFlow requests from being processed
1693 * also prevents new flows from being added (and expiring). (It
1694 * also prevents processing OpenFlow requests that would not add
1695 * new flows, so it is imperfect.) */
1696 msg = ofputil_encode_flow_removed(fr, ofconn_get_protocol(ofconn));
1697 ofconn_send_reply(ofconn, msg);
1702 /* Normally a send-to-controller action uses reason OFPR_ACTION. However, in
1703 * OpenFlow 1.3 and later, packet_ins generated by a send-to-controller action
1704 * in a "table-miss" flow (one with priority 0 and completely wildcarded) are
1705 * sent as OFPR_NO_MATCH. This function returns the reason that should
1706 * actually be sent on 'ofconn' for 'pin'. */
1707 static enum ofp_packet_in_reason
1708 wire_reason(struct ofconn *ofconn, const struct ofproto_packet_in *pin)
1710 if (pin->miss_type == OFPROTO_PACKET_IN_MISS_FLOW
1711 && pin->up.reason == OFPR_ACTION) {
1712 enum ofputil_protocol protocol = ofconn_get_protocol(ofconn);
1714 if (protocol != OFPUTIL_P_NONE
1715 && ofputil_protocol_to_ofp_version(protocol) >= OFP13_VERSION) {
1716 return OFPR_NO_MATCH;
1719 return pin->up.reason;
1722 /* Given 'pin', sends an OFPT_PACKET_IN message to each OpenFlow controller as
1723 * necessary according to their individual configurations.
1725 * The caller doesn't need to fill in pin->buffer_id or pin->total_len. */
1727 connmgr_send_packet_in(struct connmgr *mgr,
1728 const struct ofproto_packet_in *pin)
1730 struct ofconn *ofconn;
1732 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1733 enum ofp_packet_in_reason reason = wire_reason(ofconn, pin);
1735 if (ofconn_wants_packet_in_on_miss(ofconn, pin)
1736 && ofconn_receives_async_msg(ofconn, OAM_PACKET_IN, pin->up.reason)
1737 && ofconn->controller_id == pin->controller_id) {
1738 schedule_packet_in(ofconn, *pin, reason);
1744 do_send_packet_ins(struct ofconn *ofconn, struct list *txq)
1746 struct ofpbuf *pin, *next_pin;
1748 LIST_FOR_EACH_SAFE (pin, next_pin, list_node, txq) {
1749 list_remove(&pin->list_node);
1751 if (rconn_send_with_limit(ofconn->rconn, pin,
1752 ofconn->packet_in_counter, 100) == EAGAIN) {
1753 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
1755 VLOG_INFO_RL(&rl, "%s: dropping packet-in due to queue overflow",
1756 rconn_get_name(ofconn->rconn));
1761 /* Takes 'pin', composes an OpenFlow packet-in message from it, and passes it
1762 * to 'ofconn''s packet scheduler for sending. */
1764 schedule_packet_in(struct ofconn *ofconn, struct ofproto_packet_in pin,
1765 enum ofp_packet_in_reason wire_reason)
1767 struct connmgr *mgr = ofconn->connmgr;
1768 uint16_t controller_max_len;
1771 pin.up.total_len = pin.up.packet_len;
1773 pin.up.reason = wire_reason;
1774 if (pin.up.reason == OFPR_ACTION) {
1775 controller_max_len = pin.send_len; /* max_len */
1777 controller_max_len = ofconn->miss_send_len;
1780 /* Get OpenFlow buffer_id.
1781 * For OpenFlow 1.2+, OFPCML_NO_BUFFER (== UINT16_MAX) specifies
1782 * unbuffered. This behaviour doesn't violate prior versions, too. */
1783 if (controller_max_len == UINT16_MAX) {
1784 pin.up.buffer_id = UINT32_MAX;
1785 } else if (mgr->fail_open && fail_open_is_active(mgr->fail_open)) {
1786 pin.up.buffer_id = pktbuf_get_null();
1787 } else if (!ofconn->pktbuf) {
1788 pin.up.buffer_id = UINT32_MAX;
1790 pin.up.buffer_id = pktbuf_save(ofconn->pktbuf,
1791 pin.up.packet, pin.up.packet_len,
1792 pin.up.fmd.in_port);
1795 /* Figure out how much of the packet to send.
1796 * If not buffered, send the entire packet. Otherwise, depending on
1797 * the reason of packet-in, send what requested by the controller. */
1798 if (pin.up.buffer_id != UINT32_MAX
1799 && controller_max_len < pin.up.packet_len) {
1800 pin.up.packet_len = controller_max_len;
1803 /* Make OFPT_PACKET_IN and hand over to packet scheduler. */
1804 pinsched_send(ofconn->schedulers[pin.up.reason == OFPR_NO_MATCH ? 0 : 1],
1806 ofputil_encode_packet_in(&pin.up,
1807 ofconn_get_protocol(ofconn),
1808 ofconn->packet_in_format),
1810 do_send_packet_ins(ofconn, &txq);
1813 /* Fail-open settings. */
1815 /* Returns the failure handling mode (OFPROTO_FAIL_SECURE or
1816 * OFPROTO_FAIL_STANDALONE) for 'mgr'. */
1817 enum ofproto_fail_mode
1818 connmgr_get_fail_mode(const struct connmgr *mgr)
1820 return mgr->fail_mode;
1823 /* Sets the failure handling mode for 'mgr' to 'fail_mode' (either
1824 * OFPROTO_FAIL_SECURE or OFPROTO_FAIL_STANDALONE). */
1826 connmgr_set_fail_mode(struct connmgr *mgr, enum ofproto_fail_mode fail_mode)
1828 if (mgr->fail_mode != fail_mode) {
1829 mgr->fail_mode = fail_mode;
1830 update_fail_open(mgr);
1831 if (!connmgr_has_controllers(mgr)) {
1832 ofproto_flush_flows(mgr->ofproto);
1837 /* Fail-open implementation. */
1839 /* Returns the longest probe interval among the primary controllers configured
1840 * on 'mgr'. Returns 0 if there are no primary controllers. */
1842 connmgr_get_max_probe_interval(const struct connmgr *mgr)
1844 const struct ofconn *ofconn;
1845 int max_probe_interval;
1847 max_probe_interval = 0;
1848 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1849 int probe_interval = rconn_get_probe_interval(ofconn->rconn);
1850 max_probe_interval = MAX(max_probe_interval, probe_interval);
1852 return max_probe_interval;
1855 /* Returns the number of seconds for which all of 'mgr's primary controllers
1856 * have been disconnected. Returns 0 if 'mgr' has no primary controllers. */
1858 connmgr_failure_duration(const struct connmgr *mgr)
1860 const struct ofconn *ofconn;
1861 int min_failure_duration;
1863 if (!connmgr_has_controllers(mgr)) {
1867 min_failure_duration = INT_MAX;
1868 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1869 int failure_duration = rconn_failure_duration(ofconn->rconn);
1870 min_failure_duration = MIN(min_failure_duration, failure_duration);
1872 return min_failure_duration;
1875 /* Returns true if at least one primary controller is connected (regardless of
1876 * whether those controllers are believed to have authenticated and accepted
1877 * this switch), false if none of them are connected. */
1879 connmgr_is_any_controller_connected(const struct connmgr *mgr)
1881 const struct ofconn *ofconn;
1883 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1884 if (rconn_is_connected(ofconn->rconn)) {
1891 /* Returns true if at least one primary controller is believed to have
1892 * authenticated and accepted this switch, false otherwise. */
1894 connmgr_is_any_controller_admitted(const struct connmgr *mgr)
1896 const struct ofconn *ofconn;
1898 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1899 if (rconn_is_admitted(ofconn->rconn)) {
1906 /* In-band configuration. */
1908 static bool any_extras_changed(const struct connmgr *,
1909 const struct sockaddr_in *extras, size_t n);
1911 /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'mgr''s
1912 * in-band control should guarantee access, in the same way that in-band
1913 * control guarantees access to OpenFlow controllers. */
1915 connmgr_set_extra_in_band_remotes(struct connmgr *mgr,
1916 const struct sockaddr_in *extras, size_t n)
1918 if (!any_extras_changed(mgr, extras, n)) {
1922 free(mgr->extra_in_band_remotes);
1923 mgr->n_extra_remotes = n;
1924 mgr->extra_in_band_remotes = xmemdup(extras, n * sizeof *extras);
1926 update_in_band_remotes(mgr);
1929 /* Sets the OpenFlow queue used by flows set up by in-band control on
1930 * 'mgr' to 'queue_id'. If 'queue_id' is negative, then in-band control
1931 * flows will use the default queue. */
1933 connmgr_set_in_band_queue(struct connmgr *mgr, int queue_id)
1935 if (queue_id != mgr->in_band_queue) {
1936 mgr->in_band_queue = queue_id;
1937 update_in_band_remotes(mgr);
1942 any_extras_changed(const struct connmgr *mgr,
1943 const struct sockaddr_in *extras, size_t n)
1947 if (n != mgr->n_extra_remotes) {
1951 for (i = 0; i < n; i++) {
1952 const struct sockaddr_in *old = &mgr->extra_in_band_remotes[i];
1953 const struct sockaddr_in *new = &extras[i];
1955 if (old->sin_addr.s_addr != new->sin_addr.s_addr ||
1956 old->sin_port != new->sin_port) {
1964 /* In-band implementation. */
1967 connmgr_has_in_band(struct connmgr *mgr)
1969 return mgr->in_band != NULL;
1972 /* Fail-open and in-band implementation. */
1974 /* Called by 'ofproto' after all flows have been flushed, to allow fail-open
1975 * and standalone mode to re-create their flows.
1977 * In-band control has more sophisticated code that manages flows itself. */
1979 connmgr_flushed(struct connmgr *mgr)
1980 OVS_EXCLUDED(ofproto_mutex)
1982 if (mgr->fail_open) {
1983 fail_open_flushed(mgr->fail_open);
1986 /* If there are no controllers and we're in standalone mode, set up a flow
1987 * that matches every packet and directs them to OFPP_NORMAL (which goes to
1988 * us). Otherwise, the switch is in secure mode and we won't pass any
1989 * traffic until a controller has been defined and it tells us to do so. */
1990 if (!connmgr_has_controllers(mgr)
1991 && mgr->fail_mode == OFPROTO_FAIL_STANDALONE) {
1992 struct ofpbuf ofpacts;
1995 ofpbuf_init(&ofpacts, OFPACT_OUTPUT_SIZE);
1996 ofpact_put_OUTPUT(&ofpacts)->port = OFPP_NORMAL;
1997 ofpact_pad(&ofpacts);
1999 match_init_catchall(&match);
2000 ofproto_add_flow(mgr->ofproto, &match, 0, ofpbuf_data(&ofpacts),
2001 ofpbuf_size(&ofpacts));
2003 ofpbuf_uninit(&ofpacts);
2007 /* Creates a new ofservice for 'target' in 'mgr'. Returns 0 if successful,
2008 * otherwise a positive errno value.
2010 * ofservice_reconfigure() must be called to fully configure the new
2013 ofservice_create(struct connmgr *mgr, const char *target,
2014 uint32_t allowed_versions, uint8_t dscp)
2016 struct ofservice *ofservice;
2017 struct pvconn *pvconn;
2020 error = pvconn_open(target, allowed_versions, dscp, &pvconn);
2025 ofservice = xzalloc(sizeof *ofservice);
2026 hmap_insert(&mgr->services, &ofservice->node, hash_string(target, 0));
2027 ofservice->pvconn = pvconn;
2028 ofservice->allowed_versions = allowed_versions;
2034 ofservice_destroy(struct connmgr *mgr, struct ofservice *ofservice)
2036 hmap_remove(&mgr->services, &ofservice->node);
2037 pvconn_close(ofservice->pvconn);
2042 ofservice_reconfigure(struct ofservice *ofservice,
2043 const struct ofproto_controller *c)
2045 ofservice->probe_interval = c->probe_interval;
2046 ofservice->rate_limit = c->rate_limit;
2047 ofservice->burst_limit = c->burst_limit;
2048 ofservice->enable_async_msgs = c->enable_async_msgs;
2049 ofservice->dscp = c->dscp;
2052 /* Finds and returns the ofservice within 'mgr' that has the given
2053 * 'target', or a null pointer if none exists. */
2054 static struct ofservice *
2055 ofservice_lookup(struct connmgr *mgr, const char *target)
2057 struct ofservice *ofservice;
2059 HMAP_FOR_EACH_WITH_HASH (ofservice, node, hash_string(target, 0),
2061 if (!strcmp(pvconn_get_name(ofservice->pvconn), target)) {
2068 /* Flow monitors (NXST_FLOW_MONITOR). */
2070 /* A counter incremented when something significant happens to an OpenFlow
2073 * - When a rule is added, its 'add_seqno' and 'modify_seqno' are set to
2074 * the current value (which is then incremented).
2076 * - When a rule is modified, its 'modify_seqno' is set to the current
2077 * value (which is then incremented).
2079 * Thus, by comparing an old value of monitor_seqno against a rule's
2080 * 'add_seqno', one can tell whether the rule was added before or after the old
2081 * value was read, and similarly for 'modify_seqno'.
2083 * 32 bits should normally be sufficient (and would be nice, to save space in
2084 * each rule) but then we'd have to have some special cases for wraparound.
2086 * We initialize monitor_seqno to 1 to allow 0 to be used as an invalid
2088 static uint64_t monitor_seqno = 1;
2090 COVERAGE_DEFINE(ofmonitor_pause);
2091 COVERAGE_DEFINE(ofmonitor_resume);
2094 ofmonitor_create(const struct ofputil_flow_monitor_request *request,
2095 struct ofconn *ofconn, struct ofmonitor **monitorp)
2096 OVS_REQUIRES(ofproto_mutex)
2098 struct ofmonitor *m;
2102 m = ofmonitor_lookup(ofconn, request->id);
2104 return OFPERR_NXBRC_FM_DUPLICATE_ID;
2107 m = xmalloc(sizeof *m);
2109 hmap_insert(&ofconn->monitors, &m->ofconn_node, hash_int(request->id, 0));
2110 m->id = request->id;
2111 m->flags = request->flags;
2112 m->out_port = request->out_port;
2113 m->table_id = request->table_id;
2114 minimatch_init(&m->match, &request->match);
2121 ofmonitor_lookup(struct ofconn *ofconn, uint32_t id)
2122 OVS_REQUIRES(ofproto_mutex)
2124 struct ofmonitor *m;
2126 HMAP_FOR_EACH_IN_BUCKET (m, ofconn_node, hash_int(id, 0),
2127 &ofconn->monitors) {
2136 ofmonitor_destroy(struct ofmonitor *m)
2137 OVS_REQUIRES(ofproto_mutex)
2140 minimatch_destroy(&m->match);
2141 hmap_remove(&m->ofconn->monitors, &m->ofconn_node);
2147 ofmonitor_report(struct connmgr *mgr, struct rule *rule,
2148 enum nx_flow_update_event event,
2149 enum ofp_flow_removed_reason reason,
2150 const struct ofconn *abbrev_ofconn, ovs_be32 abbrev_xid)
2151 OVS_REQUIRES(ofproto_mutex)
2153 enum nx_flow_monitor_flags update;
2154 struct ofconn *ofconn;
2159 rule->add_seqno = rule->modify_seqno = monitor_seqno++;
2163 update = NXFMF_DELETE;
2166 case NXFME_MODIFIED:
2167 update = NXFMF_MODIFY;
2168 rule->modify_seqno = monitor_seqno++;
2176 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
2177 enum nx_flow_monitor_flags flags = 0;
2178 struct ofmonitor *m;
2180 if (ofconn->monitor_paused) {
2181 /* Only send NXFME_DELETED notifications for flows that were added
2182 * before we paused. */
2183 if (event != NXFME_DELETED
2184 || rule->add_seqno > ofconn->monitor_paused) {
2189 HMAP_FOR_EACH (m, ofconn_node, &ofconn->monitors) {
2190 if (m->flags & update
2191 && (m->table_id == 0xff || m->table_id == rule->table_id)
2192 && ofoperation_has_out_port(rule->pending, m->out_port)
2193 && cls_rule_is_loose_match(&rule->cr, &m->match)) {
2199 if (list_is_empty(&ofconn->updates)) {
2200 ofputil_start_flow_update(&ofconn->updates);
2201 ofconn->sent_abbrev_update = false;
2204 if (ofconn != abbrev_ofconn || ofconn->monitor_paused) {
2205 struct ofputil_flow_update fu;
2209 fu.reason = event == NXFME_DELETED ? reason : 0;
2210 fu.table_id = rule->table_id;
2211 fu.cookie = rule->flow_cookie;
2212 minimatch_expand(&rule->cr.match, &match);
2214 fu.priority = rule->cr.priority;
2216 ovs_mutex_lock(&rule->mutex);
2217 fu.idle_timeout = rule->idle_timeout;
2218 fu.hard_timeout = rule->hard_timeout;
2219 ovs_mutex_unlock(&rule->mutex);
2221 if (flags & NXFMF_ACTIONS) {
2222 const struct rule_actions *actions = rule_get_actions(rule);
2223 fu.ofpacts = actions->ofpacts;
2224 fu.ofpacts_len = actions->ofpacts_len;
2229 ofputil_append_flow_update(&fu, &ofconn->updates);
2230 } else if (!ofconn->sent_abbrev_update) {
2231 struct ofputil_flow_update fu;
2233 fu.event = NXFME_ABBREV;
2234 fu.xid = abbrev_xid;
2235 ofputil_append_flow_update(&fu, &ofconn->updates);
2237 ofconn->sent_abbrev_update = true;
2244 ofmonitor_flush(struct connmgr *mgr)
2245 OVS_REQUIRES(ofproto_mutex)
2247 struct ofconn *ofconn;
2249 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
2250 struct ofpbuf *msg, *next;
2252 LIST_FOR_EACH_SAFE (msg, next, list_node, &ofconn->updates) {
2253 unsigned int n_bytes;
2255 list_remove(&msg->list_node);
2256 ofconn_send(ofconn, msg, ofconn->monitor_counter);
2257 n_bytes = rconn_packet_counter_n_bytes(ofconn->monitor_counter);
2258 if (!ofconn->monitor_paused && n_bytes > 128 * 1024) {
2259 struct ofpbuf *pause;
2261 COVERAGE_INC(ofmonitor_pause);
2262 ofconn->monitor_paused = monitor_seqno++;
2263 pause = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_MONITOR_PAUSED,
2264 OFP10_VERSION, htonl(0), 0);
2265 ofconn_send(ofconn, pause, ofconn->monitor_counter);
2272 ofmonitor_resume(struct ofconn *ofconn)
2273 OVS_REQUIRES(ofproto_mutex)
2275 struct rule_collection rules;
2276 struct ofpbuf *resumed;
2277 struct ofmonitor *m;
2280 rule_collection_init(&rules);
2281 HMAP_FOR_EACH (m, ofconn_node, &ofconn->monitors) {
2282 ofmonitor_collect_resume_rules(m, ofconn->monitor_paused, &rules);
2286 ofmonitor_compose_refresh_updates(&rules, &msgs);
2288 resumed = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_MONITOR_RESUMED, OFP10_VERSION,
2290 list_push_back(&msgs, &resumed->list_node);
2291 ofconn_send_replies(ofconn, &msgs);
2293 ofconn->monitor_paused = 0;
2297 ofmonitor_may_resume(const struct ofconn *ofconn)
2298 OVS_REQUIRES(ofproto_mutex)
2300 return (ofconn->monitor_paused != 0
2301 && !rconn_packet_counter_n_packets(ofconn->monitor_counter));
2305 ofmonitor_run(struct connmgr *mgr)
2307 struct ofconn *ofconn;
2309 ovs_mutex_lock(&ofproto_mutex);
2310 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
2311 if (ofmonitor_may_resume(ofconn)) {
2312 COVERAGE_INC(ofmonitor_resume);
2313 ofmonitor_resume(ofconn);
2316 ovs_mutex_unlock(&ofproto_mutex);
2320 ofmonitor_wait(struct connmgr *mgr)
2322 struct ofconn *ofconn;
2324 ovs_mutex_lock(&ofproto_mutex);
2325 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
2326 if (ofmonitor_may_resume(ofconn)) {
2327 poll_immediate_wake();
2330 ovs_mutex_unlock(&ofproto_mutex);