ofp-errors: Make every error encodable.
[sliver-openvswitch.git] / ofproto / connmgr.c
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <config.h>
18
19 #include "connmgr.h"
20
21 #include <errno.h>
22 #include <stdlib.h>
23
24 #include "coverage.h"
25 #include "fail-open.h"
26 #include "in-band.h"
27 #include "odp-util.h"
28 #include "ofp-actions.h"
29 #include "ofp-msgs.h"
30 #include "ofp-util.h"
31 #include "ofpbuf.h"
32 #include "ofproto-provider.h"
33 #include "pinsched.h"
34 #include "poll-loop.h"
35 #include "pktbuf.h"
36 #include "rconn.h"
37 #include "shash.h"
38 #include "simap.h"
39 #include "stream.h"
40 #include "timeval.h"
41 #include "vconn.h"
42 #include "vlog.h"
43
44 VLOG_DEFINE_THIS_MODULE(connmgr);
45 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
46
47 /* An OpenFlow connection. */
48 struct ofconn {
49 /* Configuration that persists from one connection to the next. */
50
51     struct list node;           /* In struct connmgr's "all_conns" list. */
52     struct hmap_node hmap_node; /* In struct connmgr's "controllers" map. */
53
54     struct connmgr *connmgr;    /* Connection's manager. */
55     struct rconn *rconn;        /* OpenFlow connection. */
56     enum ofconn_type type;      /* Type. */
57     enum ofproto_band band;     /* In-band or out-of-band? */
58     bool enable_async_msgs;     /* Initially enable async messages? */
59
60 /* State that should be cleared from one connection to the next. */
61
62     /* OpenFlow state. */
63     enum nx_role role;           /* Role. */
64     enum ofputil_protocol protocol; /* Current protocol variant. */
65     enum nx_packet_in_format packet_in_format; /* OFPT_PACKET_IN format. */
66
67     /* Asynchronous flow table operation support. */
68     struct list opgroups;       /* Contains pending "ofopgroups", if any. */
69     struct ofpbuf *blocked;     /* Postponed OpenFlow message, if any. */
70     bool retry;                 /* True if 'blocked' is ready to try again. */
71
72     /* OFPT_PACKET_IN related data. */
73     struct rconn_packet_counter *packet_in_counter; /* # queued on 'rconn'. */
74 #define N_SCHEDULERS 2
75     struct pinsched *schedulers[N_SCHEDULERS];
76     struct pktbuf *pktbuf;         /* OpenFlow packet buffers. */
77     int miss_send_len;             /* Bytes to send of buffered packets. */
78     uint16_t controller_id;     /* Connection controller ID. */
79
80     /* Number of OpenFlow messages queued on 'rconn' as replies to OpenFlow
81      * requests, and the maximum number before we stop reading OpenFlow
82      * requests.  */
83 #define OFCONN_REPLY_MAX 100
84     struct rconn_packet_counter *reply_counter;
85
86     /* Asynchronous message configuration in each possible roles.
87      *
88      * A 1-bit enables sending an asynchronous message for one possible reason
89      * that the message might be generated, a 0-bit disables it. */
90     uint32_t master_async_config[OAM_N_TYPES]; /* master, other */
91     uint32_t slave_async_config[OAM_N_TYPES];  /* slave */
92
93     /* Flow monitors. */
94     struct hmap monitors;       /* Contains "struct ofmonitor"s. */
95     struct list updates;        /* List of "struct ofpbuf"s. */
96     bool sent_abbrev_update;    /* Does 'updates' contain NXFME_ABBREV? */
97     struct rconn_packet_counter *monitor_counter;
98     uint64_t monitor_paused;
99 };
100
101 static struct ofconn *ofconn_create(struct connmgr *, struct rconn *,
102                                     enum ofconn_type, bool enable_async_msgs);
103 static void ofconn_destroy(struct ofconn *);
104 static void ofconn_flush(struct ofconn *);
105
106 static void ofconn_reconfigure(struct ofconn *,
107                                const struct ofproto_controller *);
108
109 static void ofconn_run(struct ofconn *,
110                        bool (*handle_openflow)(struct ofconn *,
111                                                struct ofpbuf *ofp_msg));
112 static void ofconn_wait(struct ofconn *, bool handling_openflow);
113
114 static const char *ofconn_get_target(const struct ofconn *);
115 static char *ofconn_make_name(const struct connmgr *, const char *target);
116
117 static void ofconn_set_rate_limit(struct ofconn *, int rate, int burst);
118
119 static void ofconn_send(const struct ofconn *, struct ofpbuf *,
120                         struct rconn_packet_counter *);
121
122 static void do_send_packet_in(struct ofpbuf *, void *ofconn_);
123
124 /* A listener for incoming OpenFlow "service" connections. */
125 struct ofservice {
126     struct hmap_node node;      /* In struct connmgr's "services" hmap. */
127     struct pvconn *pvconn;      /* OpenFlow connection listener. */
128
129     /* These are not used by ofservice directly.  They are settings for
130      * accepted "struct ofconn"s from the pvconn. */
131     int probe_interval;         /* Max idle time before probing, in seconds. */
132     int rate_limit;             /* Max packet-in rate in packets per second. */
133     int burst_limit;            /* Limit on accumulating packet credits. */
134     bool enable_async_msgs;     /* Initially enable async messages? */
135     uint8_t dscp;               /* DSCP Value for controller connection */
136     uint32_t allowed_versions;  /* OpenFlow protocol versions that may
137                                  * be negotiated for a session. */
138 };
139
140 static void ofservice_reconfigure(struct ofservice *,
141                                   const struct ofproto_controller *);
142 static int ofservice_create(struct connmgr *mgr, const char *target,
143                             uint32_t allowed_versions, uint8_t dscp);
144 static void ofservice_destroy(struct connmgr *, struct ofservice *);
145 static struct ofservice *ofservice_lookup(struct connmgr *,
146                                           const char *target);
147
148 /* Connection manager for an OpenFlow switch. */
149 struct connmgr {
150     struct ofproto *ofproto;
151     char *name;
152     char *local_port_name;
153
154     /* OpenFlow connections. */
155     struct hmap controllers;   /* Controller "struct ofconn"s. */
156     struct list all_conns;     /* Contains "struct ofconn"s. */
157
158     /* OpenFlow listeners. */
159     struct hmap services;       /* Contains "struct ofservice"s. */
160     struct pvconn **snoops;
161     size_t n_snoops;
162
163     /* Fail open. */
164     struct fail_open *fail_open;
165     enum ofproto_fail_mode fail_mode;
166
167     /* In-band control. */
168     struct in_band *in_band;
169     struct sockaddr_in *extra_in_band_remotes;
170     size_t n_extra_remotes;
171     int in_band_queue;
172 };
173
174 static void update_in_band_remotes(struct connmgr *);
175 static void add_snooper(struct connmgr *, struct vconn *);
176 static void ofmonitor_run(struct connmgr *);
177 static void ofmonitor_wait(struct connmgr *);
178
179 /* Creates and returns a new connection manager owned by 'ofproto'.  'name' is
180  * a name for the ofproto suitable for using in log messages.
181  * 'local_port_name' is the name of the local port (OFPP_LOCAL) within
182  * 'ofproto'. */
183 struct connmgr *
184 connmgr_create(struct ofproto *ofproto,
185                const char *name, const char *local_port_name)
186 {
187     struct connmgr *mgr;
188
189     mgr = xmalloc(sizeof *mgr);
190     mgr->ofproto = ofproto;
191     mgr->name = xstrdup(name);
192     mgr->local_port_name = xstrdup(local_port_name);
193
194     hmap_init(&mgr->controllers);
195     list_init(&mgr->all_conns);
196
197     hmap_init(&mgr->services);
198     mgr->snoops = NULL;
199     mgr->n_snoops = 0;
200
201     mgr->fail_open = NULL;
202     mgr->fail_mode = OFPROTO_FAIL_SECURE;
203
204     mgr->in_band = NULL;
205     mgr->extra_in_band_remotes = NULL;
206     mgr->n_extra_remotes = 0;
207     mgr->in_band_queue = -1;
208
209     return mgr;
210 }
211
212 /* Frees 'mgr' and all of its resources. */
213 void
214 connmgr_destroy(struct connmgr *mgr)
215 {
216     struct ofservice *ofservice, *next_ofservice;
217     struct ofconn *ofconn, *next_ofconn;
218     size_t i;
219
220     if (!mgr) {
221         return;
222     }
223
224     LIST_FOR_EACH_SAFE (ofconn, next_ofconn, node, &mgr->all_conns) {
225         ofconn_destroy(ofconn);
226     }
227     hmap_destroy(&mgr->controllers);
228
229     HMAP_FOR_EACH_SAFE (ofservice, next_ofservice, node, &mgr->services) {
230         ofservice_destroy(mgr, ofservice);
231     }
232     hmap_destroy(&mgr->services);
233
234     for (i = 0; i < mgr->n_snoops; i++) {
235         pvconn_close(mgr->snoops[i]);
236     }
237     free(mgr->snoops);
238
239     fail_open_destroy(mgr->fail_open);
240     mgr->fail_open = NULL;
241
242     in_band_destroy(mgr->in_band);
243     mgr->in_band = NULL;
244     free(mgr->extra_in_band_remotes);
245     free(mgr->name);
246     free(mgr->local_port_name);
247
248     free(mgr);
249 }
250
251 /* Does all of the periodic maintenance required by 'mgr'.
252  *
253  * If 'handle_openflow' is nonnull, calls 'handle_openflow' for each message
254  * received on an OpenFlow connection, passing along the OpenFlow connection
255  * itself and the message that was sent.  If 'handle_openflow' returns true,
256  * the message is considered to be fully processed.  If 'handle_openflow'
257  * returns false, the message is considered not to have been processed at all;
258  * it will be stored and re-presented to 'handle_openflow' following the next
259  * call to connmgr_retry().  'handle_openflow' must not modify or free the
260  * message.
261  *
262  * If 'handle_openflow' is NULL, no OpenFlow messages will be processed and
263  * other activities that could affect the flow table (in-band processing,
264  * fail-open processing) are suppressed too. */
265 void
266 connmgr_run(struct connmgr *mgr,
267             bool (*handle_openflow)(struct ofconn *, struct ofpbuf *ofp_msg))
268 {
269     struct ofconn *ofconn, *next_ofconn;
270     struct ofservice *ofservice;
271     size_t i;
272
273     if (handle_openflow && mgr->in_band) {
274         if (!in_band_run(mgr->in_band)) {
275             in_band_destroy(mgr->in_band);
276             mgr->in_band = NULL;
277         }
278     }
279
280     LIST_FOR_EACH_SAFE (ofconn, next_ofconn, node, &mgr->all_conns) {
281         ofconn_run(ofconn, handle_openflow);
282     }
283     ofmonitor_run(mgr);
284
285     /* Fail-open maintenance.  Do this after processing the ofconns since
286      * fail-open checks the status of the controller rconn. */
287     if (handle_openflow && mgr->fail_open) {
288         fail_open_run(mgr->fail_open);
289     }
290
291     HMAP_FOR_EACH (ofservice, node, &mgr->services) {
292         struct vconn *vconn;
293         int retval;
294
295         retval = pvconn_accept(ofservice->pvconn, &vconn);
296         if (!retval) {
297             struct rconn *rconn;
298             char *name;
299
300             /* Passing default value for creation of the rconn */
301             rconn = rconn_create(ofservice->probe_interval, 0, ofservice->dscp,
302                                  vconn_get_allowed_versions(vconn));
303             name = ofconn_make_name(mgr, vconn_get_name(vconn));
304             rconn_connect_unreliably(rconn, vconn, name);
305             free(name);
306
307             ofconn = ofconn_create(mgr, rconn, OFCONN_SERVICE,
308                                    ofservice->enable_async_msgs);
309             ofconn_set_rate_limit(ofconn, ofservice->rate_limit,
310                                   ofservice->burst_limit);
311         } else if (retval != EAGAIN) {
312             VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval));
313         }
314     }
315
316     for (i = 0; i < mgr->n_snoops; i++) {
317         struct vconn *vconn;
318         int retval;
319
320         retval = pvconn_accept(mgr->snoops[i], &vconn);
321         if (!retval) {
322             add_snooper(mgr, vconn);
323         } else if (retval != EAGAIN) {
324             VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval));
325         }
326     }
327 }
328
329 /* Causes the poll loop to wake up when connmgr_run() needs to run.
330  *
331  * If 'handling_openflow' is true, arriving OpenFlow messages and other
332  * activities that affect the flow table will wake up the poll loop.  If
333  * 'handling_openflow' is false, they will not. */
334 void
335 connmgr_wait(struct connmgr *mgr, bool handling_openflow)
336 {
337     struct ofservice *ofservice;
338     struct ofconn *ofconn;
339     size_t i;
340
341     LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
342         ofconn_wait(ofconn, handling_openflow);
343     }
344     ofmonitor_wait(mgr);
345     if (handling_openflow && mgr->in_band) {
346         in_band_wait(mgr->in_band);
347     }
348     if (handling_openflow && mgr->fail_open) {
349         fail_open_wait(mgr->fail_open);
350     }
351     HMAP_FOR_EACH (ofservice, node, &mgr->services) {
352         pvconn_wait(ofservice->pvconn);
353     }
354     for (i = 0; i < mgr->n_snoops; i++) {
355         pvconn_wait(mgr->snoops[i]);
356     }
357 }
358
359 /* Adds some memory usage statistics for 'mgr' into 'usage', for use with
360  * memory_report(). */
361 void
362 connmgr_get_memory_usage(const struct connmgr *mgr, struct simap *usage)
363 {
364     const struct ofconn *ofconn;
365     unsigned int packets = 0;
366     unsigned int ofconns = 0;
367
368     LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
369         int i;
370
371         ofconns++;
372
373         packets += rconn_count_txqlen(ofconn->rconn);
374         for (i = 0; i < N_SCHEDULERS; i++) {
375             packets += pinsched_count_txqlen(ofconn->schedulers[i]);
376         }
377         packets += pktbuf_count_packets(ofconn->pktbuf);
378     }
379     simap_increase(usage, "ofconns", ofconns);
380     simap_increase(usage, "packets", packets);
381 }
382
383 /* Returns the ofproto that owns 'ofconn''s connmgr. */
384 struct ofproto *
385 ofconn_get_ofproto(const struct ofconn *ofconn)
386 {
387     return ofconn->connmgr->ofproto;
388 }
389
390 /* If processing of OpenFlow messages was blocked on any 'mgr' ofconns by
391  * returning false to the 'handle_openflow' callback to connmgr_run(), this
392  * re-enables them. */
393 void
394 connmgr_retry(struct connmgr *mgr)
395 {
396     struct ofconn *ofconn;
397
398     LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
399         ofconn->retry = true;
400     }
401 }
402 \f
403 /* OpenFlow configuration. */
404
405 static void add_controller(struct connmgr *, const char *target, uint8_t dscp,
406                            uint32_t allowed_versions);
407 static struct ofconn *find_controller_by_target(struct connmgr *,
408                                                 const char *target);
409 static void update_fail_open(struct connmgr *);
410 static int set_pvconns(struct pvconn ***pvconnsp, size_t *n_pvconnsp,
411                        const struct sset *);
412
413 /* Returns true if 'mgr' has any configured primary controllers.
414  *
415  * Service controllers do not count, but configured primary controllers do
416  * count whether or not they are currently connected. */
417 bool
418 connmgr_has_controllers(const struct connmgr *mgr)
419 {
420     return !hmap_is_empty(&mgr->controllers);
421 }
422
423 /* Initializes 'info' and populates it with information about each configured
424  * primary controller.  The keys in 'info' are the controllers' targets; the
425  * data values are corresponding "struct ofproto_controller_info".
426  *
427  * The caller owns 'info' and everything in it and should free it when it is no
428  * longer needed. */
429 void
430 connmgr_get_controller_info(struct connmgr *mgr, struct shash *info)
431 {
432     const struct ofconn *ofconn;
433
434     HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
435         const struct rconn *rconn = ofconn->rconn;
436         const char *target = rconn_get_target(rconn);
437
438         if (!shash_find(info, target)) {
439             struct ofproto_controller_info *cinfo = xmalloc(sizeof *cinfo);
440             time_t now = time_now();
441             time_t last_connection = rconn_get_last_connection(rconn);
442             time_t last_disconnect = rconn_get_last_disconnect(rconn);
443             int last_error = rconn_get_last_error(rconn);
444
445             shash_add(info, target, cinfo);
446
447             cinfo->is_connected = rconn_is_connected(rconn);
448             cinfo->role = ofconn->role;
449
450             cinfo->pairs.n = 0;
451
452             if (last_error) {
453                 cinfo->pairs.keys[cinfo->pairs.n] = "last_error";
454                 cinfo->pairs.values[cinfo->pairs.n++]
455                     = xstrdup(ovs_retval_to_string(last_error));
456             }
457
458             cinfo->pairs.keys[cinfo->pairs.n] = "state";
459             cinfo->pairs.values[cinfo->pairs.n++]
460                 = xstrdup(rconn_get_state(rconn));
461
462             if (last_connection != TIME_MIN) {
463                 cinfo->pairs.keys[cinfo->pairs.n] = "sec_since_connect";
464                 cinfo->pairs.values[cinfo->pairs.n++]
465                     = xasprintf("%ld", (long int) (now - last_connection));
466             }
467
468             if (last_disconnect != TIME_MIN) {
469                 cinfo->pairs.keys[cinfo->pairs.n] = "sec_since_disconnect";
470                 cinfo->pairs.values[cinfo->pairs.n++]
471                     = xasprintf("%ld", (long int) (now - last_disconnect));
472             }
473         }
474     }
475 }
476
477 void
478 connmgr_free_controller_info(struct shash *info)
479 {
480     struct shash_node *node;
481
482     SHASH_FOR_EACH (node, info) {
483         struct ofproto_controller_info *cinfo = node->data;
484         while (cinfo->pairs.n) {
485             free(CONST_CAST(char *, cinfo->pairs.values[--cinfo->pairs.n]));
486         }
487         free(cinfo);
488     }
489     shash_destroy(info);
490 }
491
492 /* Changes 'mgr''s set of controllers to the 'n_controllers' controllers in
493  * 'controllers'. */
494 void
495 connmgr_set_controllers(struct connmgr *mgr,
496                         const struct ofproto_controller *controllers,
497                         size_t n_controllers, uint32_t allowed_versions)
498 {
499     bool had_controllers = connmgr_has_controllers(mgr);
500     struct shash new_controllers;
501     struct ofconn *ofconn, *next_ofconn;
502     struct ofservice *ofservice, *next_ofservice;
503     size_t i;
504
505     /* Create newly configured controllers and services.
506      * Create a name to ofproto_controller mapping in 'new_controllers'. */
507     shash_init(&new_controllers);
508     for (i = 0; i < n_controllers; i++) {
509         const struct ofproto_controller *c = &controllers[i];
510
511         if (!vconn_verify_name(c->target)) {
512             bool add = false;
513             ofconn = find_controller_by_target(mgr, c->target);
514             if (!ofconn) {
515                 VLOG_INFO("%s: added primary controller \"%s\"",
516                           mgr->name, c->target);
517                 add = true;
518             } else if (rconn_get_allowed_versions(ofconn->rconn) !=
519                        allowed_versions) {
520                 VLOG_INFO("%s: re-added primary controller \"%s\"",
521                           mgr->name, c->target);
522                 add = true;
523                 ofconn_destroy(ofconn);
524             }
525             if (add) {
526                 add_controller(mgr, c->target, c->dscp, allowed_versions);
527             }
528         } else if (!pvconn_verify_name(c->target)) {
529             bool add = false;
530             ofservice = ofservice_lookup(mgr, c->target);
531             if (!ofservice) {
532                 VLOG_INFO("%s: added service controller \"%s\"",
533                           mgr->name, c->target);
534                 add = true;
535             } else if (ofservice->allowed_versions != allowed_versions) {
536                 VLOG_INFO("%s: re-added service controller \"%s\"",
537                           mgr->name, c->target);
538                 ofservice_destroy(mgr, ofservice);
539                 add = true;
540             }
541             if (add) {
542                 ofservice_create(mgr, c->target, allowed_versions, c->dscp);
543             }
544         } else {
545             VLOG_WARN_RL(&rl, "%s: unsupported controller \"%s\"",
546                          mgr->name, c->target);
547             continue;
548         }
549
550         shash_add_once(&new_controllers, c->target, &controllers[i]);
551     }
552
553     /* Delete controllers that are no longer configured.
554      * Update configuration of all now-existing controllers. */
555     HMAP_FOR_EACH_SAFE (ofconn, next_ofconn, hmap_node, &mgr->controllers) {
556         const char *target = ofconn_get_target(ofconn);
557         struct ofproto_controller *c;
558
559         c = shash_find_data(&new_controllers, target);
560         if (!c) {
561             VLOG_INFO("%s: removed primary controller \"%s\"",
562                       mgr->name, target);
563             ofconn_destroy(ofconn);
564         } else {
565             ofconn_reconfigure(ofconn, c);
566         }
567     }
568
569     /* Delete services that are no longer configured.
570      * Update configuration of all now-existing services. */
571     HMAP_FOR_EACH_SAFE (ofservice, next_ofservice, node, &mgr->services) {
572         const char *target = pvconn_get_name(ofservice->pvconn);
573         struct ofproto_controller *c;
574
575         c = shash_find_data(&new_controllers, target);
576         if (!c) {
577             VLOG_INFO("%s: removed service controller \"%s\"",
578                       mgr->name, target);
579             ofservice_destroy(mgr, ofservice);
580         } else {
581             ofservice_reconfigure(ofservice, c);
582         }
583     }
584
585     shash_destroy(&new_controllers);
586
587     update_in_band_remotes(mgr);
588     update_fail_open(mgr);
589     if (had_controllers != connmgr_has_controllers(mgr)) {
590         ofproto_flush_flows(mgr->ofproto);
591     }
592 }
593
594 /* Drops the connections between 'mgr' and all of its primary and secondary
595  * controllers, forcing them to reconnect. */
596 void
597 connmgr_reconnect(const struct connmgr *mgr)
598 {
599     struct ofconn *ofconn;
600
601     LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
602         rconn_reconnect(ofconn->rconn);
603     }
604 }
605
606 /* Sets the "snoops" for 'mgr' to the pvconn targets listed in 'snoops'.
607  *
608  * A "snoop" is a pvconn to which every OpenFlow message to or from the most
609  * important controller on 'mgr' is mirrored. */
610 int
611 connmgr_set_snoops(struct connmgr *mgr, const struct sset *snoops)
612 {
613     return set_pvconns(&mgr->snoops, &mgr->n_snoops, snoops);
614 }
615
616 /* Adds each of the snoops currently configured on 'mgr' to 'snoops'. */
617 void
618 connmgr_get_snoops(const struct connmgr *mgr, struct sset *snoops)
619 {
620     size_t i;
621
622     for (i = 0; i < mgr->n_snoops; i++) {
623         sset_add(snoops, pvconn_get_name(mgr->snoops[i]));
624     }
625 }
626
627 /* Returns true if 'mgr' has at least one snoop, false if it has none. */
628 bool
629 connmgr_has_snoops(const struct connmgr *mgr)
630 {
631     return mgr->n_snoops > 0;
632 }
633
634 /* Creates a new controller for 'target' in 'mgr'.  update_controller() needs
635  * to be called later to finish the new ofconn's configuration. */
636 static void
637 add_controller(struct connmgr *mgr, const char *target, uint8_t dscp,
638                uint32_t allowed_versions)
639 {
640     char *name = ofconn_make_name(mgr, target);
641     struct ofconn *ofconn;
642
643     ofconn = ofconn_create(mgr, rconn_create(5, 8, dscp, allowed_versions),
644                            OFCONN_PRIMARY, true);
645     ofconn->pktbuf = pktbuf_create();
646     rconn_connect(ofconn->rconn, target, name);
647     hmap_insert(&mgr->controllers, &ofconn->hmap_node, hash_string(target, 0));
648
649     free(name);
650 }
651
652 static struct ofconn *
653 find_controller_by_target(struct connmgr *mgr, const char *target)
654 {
655     struct ofconn *ofconn;
656
657     HMAP_FOR_EACH_WITH_HASH (ofconn, hmap_node,
658                              hash_string(target, 0), &mgr->controllers) {
659         if (!strcmp(ofconn_get_target(ofconn), target)) {
660             return ofconn;
661         }
662     }
663     return NULL;
664 }
665
666 static void
667 update_in_band_remotes(struct connmgr *mgr)
668 {
669     struct sockaddr_in *addrs;
670     size_t max_addrs, n_addrs;
671     struct ofconn *ofconn;
672     size_t i;
673
674     /* Allocate enough memory for as many remotes as we could possibly have. */
675     max_addrs = mgr->n_extra_remotes + hmap_count(&mgr->controllers);
676     addrs = xmalloc(max_addrs * sizeof *addrs);
677     n_addrs = 0;
678
679     /* Add all the remotes. */
680     HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
681         struct sockaddr_in *sin = &addrs[n_addrs];
682         const char *target = rconn_get_target(ofconn->rconn);
683
684         if (ofconn->band == OFPROTO_OUT_OF_BAND) {
685             continue;
686         }
687
688         if (stream_parse_target_with_default_ports(target,
689                                                    OFP_TCP_PORT,
690                                                    OFP_SSL_PORT,
691                                                    sin)) {
692             n_addrs++;
693         }
694     }
695     for (i = 0; i < mgr->n_extra_remotes; i++) {
696         addrs[n_addrs++] = mgr->extra_in_band_remotes[i];
697     }
698
699     /* Create or update or destroy in-band. */
700     if (n_addrs) {
701         if (!mgr->in_band) {
702             in_band_create(mgr->ofproto, mgr->local_port_name, &mgr->in_band);
703         }
704         in_band_set_queue(mgr->in_band, mgr->in_band_queue);
705     } else {
706         /* in_band_run() needs a chance to delete any existing in-band flows.
707          * We will destroy mgr->in_band after it's done with that. */
708     }
709     if (mgr->in_band) {
710         in_band_set_remotes(mgr->in_band, addrs, n_addrs);
711     }
712
713     /* Clean up. */
714     free(addrs);
715 }
716
717 static void
718 update_fail_open(struct connmgr *mgr)
719 {
720     if (connmgr_has_controllers(mgr)
721         && mgr->fail_mode == OFPROTO_FAIL_STANDALONE) {
722         if (!mgr->fail_open) {
723             mgr->fail_open = fail_open_create(mgr->ofproto, mgr);
724         }
725     } else {
726         fail_open_destroy(mgr->fail_open);
727         mgr->fail_open = NULL;
728     }
729 }
730
731 static int
732 set_pvconns(struct pvconn ***pvconnsp, size_t *n_pvconnsp,
733             const struct sset *sset)
734 {
735     struct pvconn **pvconns = *pvconnsp;
736     size_t n_pvconns = *n_pvconnsp;
737     const char *name;
738     int retval = 0;
739     size_t i;
740
741     for (i = 0; i < n_pvconns; i++) {
742         pvconn_close(pvconns[i]);
743     }
744     free(pvconns);
745
746     pvconns = xmalloc(sset_count(sset) * sizeof *pvconns);
747     n_pvconns = 0;
748     SSET_FOR_EACH (name, sset) {
749         struct pvconn *pvconn;
750         int error;
751         error = pvconn_open(name, 0, &pvconn, 0);
752         if (!error) {
753             pvconns[n_pvconns++] = pvconn;
754         } else {
755             VLOG_ERR("failed to listen on %s: %s", name, strerror(error));
756             if (!retval) {
757                 retval = error;
758             }
759         }
760     }
761
762     *pvconnsp = pvconns;
763     *n_pvconnsp = n_pvconns;
764
765     return retval;
766 }
767
768 /* Returns a "preference level" for snooping 'ofconn'.  A higher return value
769  * means that 'ofconn' is more interesting for monitoring than a lower return
770  * value. */
771 static int
772 snoop_preference(const struct ofconn *ofconn)
773 {
774     switch (ofconn->role) {
775     case NX_ROLE_MASTER:
776         return 3;
777     case NX_ROLE_OTHER:
778         return 2;
779     case NX_ROLE_SLAVE:
780         return 1;
781     default:
782         /* Shouldn't happen. */
783         return 0;
784     }
785 }
786
787 /* One of 'mgr''s "snoop" pvconns has accepted a new connection on 'vconn'.
788  * Connects this vconn to a controller. */
789 static void
790 add_snooper(struct connmgr *mgr, struct vconn *vconn)
791 {
792     struct ofconn *ofconn, *best;
793
794     /* Pick a controller for monitoring. */
795     best = NULL;
796     LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
797         if (ofconn->type == OFCONN_PRIMARY
798             && (!best || snoop_preference(ofconn) > snoop_preference(best))) {
799             best = ofconn;
800         }
801     }
802
803     if (best) {
804         rconn_add_monitor(best->rconn, vconn);
805     } else {
806         VLOG_INFO_RL(&rl, "no controller connection to snoop");
807         vconn_close(vconn);
808     }
809 }
810 \f
811 /* Public ofconn functions. */
812
813 /* Returns the connection type, either OFCONN_PRIMARY or OFCONN_SERVICE. */
814 enum ofconn_type
815 ofconn_get_type(const struct ofconn *ofconn)
816 {
817     return ofconn->type;
818 }
819
820 /* Returns the role configured for 'ofconn'.
821  *
822  * The default role, if no other role has been set, is NX_ROLE_OTHER. */
823 enum nx_role
824 ofconn_get_role(const struct ofconn *ofconn)
825 {
826     return ofconn->role;
827 }
828
829 /* Changes 'ofconn''s role to 'role'.  If 'role' is NX_ROLE_MASTER then any
830  * existing master is demoted to a slave. */
831 void
832 ofconn_set_role(struct ofconn *ofconn, enum nx_role role)
833 {
834     if (role == NX_ROLE_MASTER) {
835         struct ofconn *other;
836
837         HMAP_FOR_EACH (other, hmap_node, &ofconn->connmgr->controllers) {
838             if (other->role == NX_ROLE_MASTER) {
839                 other->role = NX_ROLE_SLAVE;
840             }
841         }
842     }
843     ofconn->role = role;
844 }
845
846 void
847 ofconn_set_invalid_ttl_to_controller(struct ofconn *ofconn, bool enable)
848 {
849     uint32_t bit = 1u << OFPR_INVALID_TTL;
850     if (enable) {
851         ofconn->master_async_config[OAM_PACKET_IN] |= bit;
852     } else {
853         ofconn->master_async_config[OAM_PACKET_IN] &= ~bit;
854     }
855 }
856
857 bool
858 ofconn_get_invalid_ttl_to_controller(struct ofconn *ofconn)
859 {
860     uint32_t bit = 1u << OFPR_INVALID_TTL;
861     return (ofconn->master_async_config[OAM_PACKET_IN] & bit) != 0;
862 }
863
864 /* Returns the currently configured protocol for 'ofconn', one of OFPUTIL_P_*.
865  *
866  * Returns OFPUTIL_P_NONE, which is not a valid protocol, if 'ofconn' hasn't
867  * completed version negotiation.  This can't happen if at least one OpenFlow
868  * message, other than OFPT_HELLO, has been received on the connection (such as
869  * in ofproto.c's message handling code), since version negotiation is a
870  * prerequisite for starting to receive messages.  This means that
871  * OFPUTIL_P_NONE is a special case that most callers need not worry about. */
872 enum ofputil_protocol
873 ofconn_get_protocol(const struct ofconn *ofconn)
874 {
875     if (ofconn->protocol == OFPUTIL_P_NONE &&
876         rconn_is_connected(ofconn->rconn)) {
877         int version = rconn_get_version(ofconn->rconn);
878         if (version > 0) {
879             ofconn_set_protocol(CONST_CAST(struct ofconn *, ofconn),
880                                 ofputil_protocol_from_ofp_version(version));
881         }
882     }
883
884     return ofconn->protocol;
885 }
886
887 /* Sets the protocol for 'ofconn' to 'protocol' (one of OFPUTIL_P_*).
888  *
889  * (This doesn't actually send anything to accomplish this.  Presumably the
890  * caller already did that.) */
891 void
892 ofconn_set_protocol(struct ofconn *ofconn, enum ofputil_protocol protocol)
893 {
894     ofconn->protocol = protocol;
895 }
896
897 /* Returns the currently configured packet in format for 'ofconn', one of
898  * NXPIF_*.
899  *
900  * The default, if no other format has been set, is NXPIF_OPENFLOW10. */
901 enum nx_packet_in_format
902 ofconn_get_packet_in_format(struct ofconn *ofconn)
903 {
904     return ofconn->packet_in_format;
905 }
906
907 /* Sets the packet in format for 'ofconn' to 'packet_in_format' (one of
908  * NXPIF_*). */
909 void
910 ofconn_set_packet_in_format(struct ofconn *ofconn,
911                             enum nx_packet_in_format packet_in_format)
912 {
913     ofconn->packet_in_format = packet_in_format;
914 }
915
916 /* Sets the controller connection ID for 'ofconn' to 'controller_id'.
917  *
918  * The connection controller ID is used for OFPP_CONTROLLER and
919  * NXAST_CONTROLLER actions.  See "struct nx_action_controller" for details. */
920 void
921 ofconn_set_controller_id(struct ofconn *ofconn, uint16_t controller_id)
922 {
923     ofconn->controller_id = controller_id;
924 }
925
926 /* Returns the default miss send length for 'ofconn'. */
927 int
928 ofconn_get_miss_send_len(const struct ofconn *ofconn)
929 {
930     return ofconn->miss_send_len;
931 }
932
933 /* Sets the default miss send length for 'ofconn' to 'miss_send_len'. */
934 void
935 ofconn_set_miss_send_len(struct ofconn *ofconn, int miss_send_len)
936 {
937     ofconn->miss_send_len = miss_send_len;
938 }
939
940 void
941 ofconn_set_async_config(struct ofconn *ofconn,
942                         const uint32_t master_masks[OAM_N_TYPES],
943                         const uint32_t slave_masks[OAM_N_TYPES])
944 {
945     size_t size = sizeof ofconn->master_async_config;
946     memcpy(ofconn->master_async_config, master_masks, size);
947     memcpy(ofconn->slave_async_config, slave_masks, size);
948 }
949
950 /* Sends 'msg' on 'ofconn', accounting it as a reply.  (If there is a
951  * sufficient number of OpenFlow replies in-flight on a single ofconn, then the
952  * connmgr will stop accepting new OpenFlow requests on that ofconn until the
953  * controller has accepted some of the replies.) */
954 void
955 ofconn_send_reply(const struct ofconn *ofconn, struct ofpbuf *msg)
956 {
957     ofconn_send(ofconn, msg, ofconn->reply_counter);
958 }
959
960 /* Sends each of the messages in list 'replies' on 'ofconn' in order,
961  * accounting them as replies. */
962 void
963 ofconn_send_replies(const struct ofconn *ofconn, struct list *replies)
964 {
965     struct ofpbuf *reply, *next;
966
967     LIST_FOR_EACH_SAFE (reply, next, list_node, replies) {
968         list_remove(&reply->list_node);
969         ofconn_send_reply(ofconn, reply);
970     }
971 }
972
973 /* Sends 'error' on 'ofconn', as a reply to 'request'.  Only at most the
974  * first 64 bytes of 'request' are used. */
975 void
976 ofconn_send_error(const struct ofconn *ofconn,
977                   const struct ofp_header *request, enum ofperr error)
978 {
979     static struct vlog_rate_limit err_rl = VLOG_RATE_LIMIT_INIT(10, 10);
980     struct ofpbuf *reply;
981
982     reply = ofperr_encode_reply(error, request);
983     if (!VLOG_DROP_INFO(&err_rl)) {
984         const char *type_name;
985         size_t request_len;
986         enum ofpraw raw;
987
988         request_len = ntohs(request->length);
989         type_name = (!ofpraw_decode_partial(&raw, request,
990                                             MIN(64, request_len))
991                      ? ofpraw_get_name(raw)
992                      : "invalid");
993
994         VLOG_INFO("%s: sending %s error reply to %s message",
995                   rconn_get_name(ofconn->rconn), ofperr_to_string(error),
996                   type_name);
997     }
998     ofconn_send_reply(ofconn, reply);
999 }
1000
1001 /* Same as pktbuf_retrieve(), using the pktbuf owned by 'ofconn'. */
1002 enum ofperr
1003 ofconn_pktbuf_retrieve(struct ofconn *ofconn, uint32_t id,
1004                        struct ofpbuf **bufferp, uint16_t *in_port)
1005 {
1006     return pktbuf_retrieve(ofconn->pktbuf, id, bufferp, in_port);
1007 }
1008
1009 /* Returns true if 'ofconn' has any pending opgroups. */
1010 bool
1011 ofconn_has_pending_opgroups(const struct ofconn *ofconn)
1012 {
1013     return !list_is_empty(&ofconn->opgroups);
1014 }
1015
1016 /* Adds 'ofconn_node' to 'ofconn''s list of pending opgroups.
1017  *
1018  * If 'ofconn' is destroyed or its connection drops, then 'ofconn' will remove
1019  * 'ofconn_node' from the list and re-initialize it with list_init().  The
1020  * client may, therefore, use list_is_empty(ofconn_node) to determine whether
1021  * 'ofconn_node' is still associated with an active ofconn.
1022  *
1023  * The client may also remove ofconn_node from the list itself, with
1024  * list_remove(). */
1025 void
1026 ofconn_add_opgroup(struct ofconn *ofconn, struct list *ofconn_node)
1027 {
1028     list_push_back(&ofconn->opgroups, ofconn_node);
1029 }
1030 \f
1031 /* Private ofconn functions. */
1032
1033 static const char *
1034 ofconn_get_target(const struct ofconn *ofconn)
1035 {
1036     return rconn_get_target(ofconn->rconn);
1037 }
1038
1039 static struct ofconn *
1040 ofconn_create(struct connmgr *mgr, struct rconn *rconn, enum ofconn_type type,
1041               bool enable_async_msgs)
1042 {
1043     struct ofconn *ofconn;
1044
1045     ofconn = xzalloc(sizeof *ofconn);
1046     ofconn->connmgr = mgr;
1047     list_push_back(&mgr->all_conns, &ofconn->node);
1048     ofconn->rconn = rconn;
1049     ofconn->type = type;
1050     ofconn->enable_async_msgs = enable_async_msgs;
1051
1052     list_init(&ofconn->opgroups);
1053
1054     hmap_init(&ofconn->monitors);
1055     list_init(&ofconn->updates);
1056
1057     ofconn_flush(ofconn);
1058
1059     return ofconn;
1060 }
1061
1062 /* Clears all of the state in 'ofconn' that should not persist from one
1063  * connection to the next. */
1064 static void
1065 ofconn_flush(struct ofconn *ofconn)
1066 {
1067     struct ofmonitor *monitor, *next_monitor;
1068     int i;
1069
1070     ofconn->role = NX_ROLE_OTHER;
1071     ofconn_set_protocol(ofconn, OFPUTIL_P_NONE);
1072     ofconn->packet_in_format = NXPIF_OPENFLOW10;
1073
1074     /* Disassociate 'ofconn' from all of the ofopgroups that it initiated that
1075      * have not yet completed.  (Those ofopgroups will still run to completion
1076      * in the usual way, but any errors that they run into will not be reported
1077      * on any OpenFlow channel.)
1078      *
1079      * Also discard any blocked operation on 'ofconn'. */
1080     while (!list_is_empty(&ofconn->opgroups)) {
1081         list_init(list_pop_front(&ofconn->opgroups));
1082     }
1083     ofpbuf_delete(ofconn->blocked);
1084     ofconn->blocked = NULL;
1085
1086     rconn_packet_counter_destroy(ofconn->packet_in_counter);
1087     ofconn->packet_in_counter = rconn_packet_counter_create();
1088     for (i = 0; i < N_SCHEDULERS; i++) {
1089         if (ofconn->schedulers[i]) {
1090             int rate, burst;
1091
1092             pinsched_get_limits(ofconn->schedulers[i], &rate, &burst);
1093             pinsched_destroy(ofconn->schedulers[i]);
1094             ofconn->schedulers[i] = pinsched_create(rate, burst);
1095         }
1096     }
1097     if (ofconn->pktbuf) {
1098         pktbuf_destroy(ofconn->pktbuf);
1099         ofconn->pktbuf = pktbuf_create();
1100     }
1101     ofconn->miss_send_len = (ofconn->type == OFCONN_PRIMARY
1102                              ? OFP_DEFAULT_MISS_SEND_LEN
1103                              : 0);
1104     ofconn->controller_id = 0;
1105
1106     rconn_packet_counter_destroy(ofconn->reply_counter);
1107     ofconn->reply_counter = rconn_packet_counter_create();
1108
1109     if (ofconn->enable_async_msgs) {
1110         uint32_t *master = ofconn->master_async_config;
1111         uint32_t *slave = ofconn->slave_async_config;
1112
1113         /* "master" and "other" roles get all asynchronous messages by default,
1114          * except that the controller needs to enable nonstandard "packet-in"
1115          * reasons itself. */
1116         master[OAM_PACKET_IN] = (1u << OFPR_NO_MATCH) | (1u << OFPR_ACTION);
1117         master[OAM_PORT_STATUS] = ((1u << OFPPR_ADD)
1118                                    | (1u << OFPPR_DELETE)
1119                                    | (1u << OFPPR_MODIFY));
1120         master[OAM_FLOW_REMOVED] = ((1u << OFPRR_IDLE_TIMEOUT)
1121                                     | (1u << OFPRR_HARD_TIMEOUT)
1122                                     | (1u << OFPRR_DELETE));
1123
1124         /* "slave" role gets port status updates by default. */
1125         slave[OAM_PACKET_IN] = 0;
1126         slave[OAM_PORT_STATUS] = ((1u << OFPPR_ADD)
1127                                   | (1u << OFPPR_DELETE)
1128                                   | (1u << OFPPR_MODIFY));
1129         slave[OAM_FLOW_REMOVED] = 0;
1130     } else {
1131         memset(ofconn->master_async_config, 0,
1132                sizeof ofconn->master_async_config);
1133         memset(ofconn->slave_async_config, 0,
1134                sizeof ofconn->slave_async_config);
1135     }
1136
1137     HMAP_FOR_EACH_SAFE (monitor, next_monitor, ofconn_node,
1138                         &ofconn->monitors) {
1139         ofmonitor_destroy(monitor);
1140     }
1141     rconn_packet_counter_destroy(ofconn->monitor_counter);
1142     ofconn->monitor_counter = rconn_packet_counter_create();
1143     ofpbuf_list_delete(&ofconn->updates); /* ...but it should be empty. */
1144 }
1145
1146 static void
1147 ofconn_destroy(struct ofconn *ofconn)
1148 {
1149     ofconn_flush(ofconn);
1150
1151     if (ofconn->type == OFCONN_PRIMARY) {
1152         hmap_remove(&ofconn->connmgr->controllers, &ofconn->hmap_node);
1153     }
1154
1155     list_remove(&ofconn->node);
1156     rconn_destroy(ofconn->rconn);
1157     rconn_packet_counter_destroy(ofconn->packet_in_counter);
1158     rconn_packet_counter_destroy(ofconn->reply_counter);
1159     pktbuf_destroy(ofconn->pktbuf);
1160     rconn_packet_counter_destroy(ofconn->monitor_counter);
1161     free(ofconn);
1162 }
1163
1164 /* Reconfigures 'ofconn' to match 'c'.  'ofconn' and 'c' must have the same
1165  * target. */
1166 static void
1167 ofconn_reconfigure(struct ofconn *ofconn, const struct ofproto_controller *c)
1168 {
1169     int probe_interval;
1170
1171     ofconn->band = c->band;
1172     ofconn->enable_async_msgs = c->enable_async_msgs;
1173
1174     rconn_set_max_backoff(ofconn->rconn, c->max_backoff);
1175
1176     probe_interval = c->probe_interval ? MAX(c->probe_interval, 5) : 0;
1177     rconn_set_probe_interval(ofconn->rconn, probe_interval);
1178
1179     ofconn_set_rate_limit(ofconn, c->rate_limit, c->burst_limit);
1180
1181     /* If dscp value changed reconnect. */
1182     if (c->dscp != rconn_get_dscp(ofconn->rconn)) {
1183         rconn_set_dscp(ofconn->rconn, c->dscp);
1184         rconn_reconnect(ofconn->rconn);
1185     }
1186 }
1187
1188 /* Returns true if it makes sense for 'ofconn' to receive and process OpenFlow
1189  * messages. */
1190 static bool
1191 ofconn_may_recv(const struct ofconn *ofconn)
1192 {
1193     int count = ofconn->reply_counter->n_packets;
1194     return (!ofconn->blocked || ofconn->retry) && count < OFCONN_REPLY_MAX;
1195 }
1196
1197 static void
1198 ofconn_run(struct ofconn *ofconn,
1199            bool (*handle_openflow)(struct ofconn *, struct ofpbuf *ofp_msg))
1200 {
1201     struct connmgr *mgr = ofconn->connmgr;
1202     size_t i;
1203
1204     for (i = 0; i < N_SCHEDULERS; i++) {
1205         pinsched_run(ofconn->schedulers[i], do_send_packet_in, ofconn);
1206     }
1207
1208     rconn_run(ofconn->rconn);
1209
1210     if (handle_openflow) {
1211         /* Limit the number of iterations to avoid starving other tasks. */
1212         for (i = 0; i < 50 && ofconn_may_recv(ofconn); i++) {
1213             struct ofpbuf *of_msg;
1214
1215             of_msg = (ofconn->blocked
1216                       ? ofconn->blocked
1217                       : rconn_recv(ofconn->rconn));
1218             if (!of_msg) {
1219                 break;
1220             }
1221             if (mgr->fail_open) {
1222                 fail_open_maybe_recover(mgr->fail_open);
1223             }
1224
1225             if (handle_openflow(ofconn, of_msg)) {
1226                 ofpbuf_delete(of_msg);
1227                 ofconn->blocked = NULL;
1228             } else {
1229                 ofconn->blocked = of_msg;
1230                 ofconn->retry = false;
1231             }
1232         }
1233     }
1234
1235     if (!rconn_is_alive(ofconn->rconn)) {
1236         ofconn_destroy(ofconn);
1237     } else if (!rconn_is_connected(ofconn->rconn)) {
1238         ofconn_flush(ofconn);
1239     }
1240 }
1241
1242 static void
1243 ofconn_wait(struct ofconn *ofconn, bool handling_openflow)
1244 {
1245     int i;
1246
1247     for (i = 0; i < N_SCHEDULERS; i++) {
1248         pinsched_wait(ofconn->schedulers[i]);
1249     }
1250     rconn_run_wait(ofconn->rconn);
1251     if (handling_openflow && ofconn_may_recv(ofconn)) {
1252         rconn_recv_wait(ofconn->rconn);
1253     }
1254 }
1255
1256 /* Returns true if 'ofconn' should receive asynchronous messages of the given
1257  * OAM_* 'type' and 'reason', which should be a OFPR_* value for OAM_PACKET_IN,
1258  * a OFPPR_* value for OAM_PORT_STATUS, or an OFPRR_* value for
1259  * OAM_FLOW_REMOVED.  Returns false if the message should not be sent on
1260  * 'ofconn'. */
1261 static bool
1262 ofconn_receives_async_msg(const struct ofconn *ofconn,
1263                           enum ofconn_async_msg_type type,
1264                           unsigned int reason)
1265 {
1266     const uint32_t *async_config;
1267
1268     assert(reason < 32);
1269     assert((unsigned int) type < OAM_N_TYPES);
1270
1271     if (ofconn_get_protocol(ofconn) == OFPUTIL_P_NONE
1272         || !rconn_is_connected(ofconn->rconn)) {
1273         return false;
1274     }
1275
1276     /* Keep the following code in sync with the documentation in the
1277      * "Asynchronous Messages" section in DESIGN. */
1278
1279     if (ofconn->type == OFCONN_SERVICE && !ofconn->miss_send_len) {
1280         /* Service connections don't get asynchronous messages unless they have
1281          * explicitly asked for them by setting a nonzero miss send length. */
1282         return false;
1283     }
1284
1285     async_config = (ofconn->role == NX_ROLE_SLAVE
1286                     ? ofconn->slave_async_config
1287                     : ofconn->master_async_config);
1288     if (!(async_config[type] & (1u << reason))) {
1289         return false;
1290     }
1291
1292     return true;
1293 }
1294
1295 /* Returns a human-readable name for an OpenFlow connection between 'mgr' and
1296  * 'target', suitable for use in log messages for identifying the connection.
1297  *
1298  * The name is dynamically allocated.  The caller should free it (with free())
1299  * when it is no longer needed. */
1300 static char *
1301 ofconn_make_name(const struct connmgr *mgr, const char *target)
1302 {
1303     return xasprintf("%s<->%s", mgr->name, target);
1304 }
1305
1306 static void
1307 ofconn_set_rate_limit(struct ofconn *ofconn, int rate, int burst)
1308 {
1309     int i;
1310
1311     for (i = 0; i < N_SCHEDULERS; i++) {
1312         struct pinsched **s = &ofconn->schedulers[i];
1313
1314         if (rate > 0) {
1315             if (!*s) {
1316                 *s = pinsched_create(rate, burst);
1317             } else {
1318                 pinsched_set_limits(*s, rate, burst);
1319             }
1320         } else {
1321             pinsched_destroy(*s);
1322             *s = NULL;
1323         }
1324     }
1325 }
1326
1327 static void
1328 ofconn_send(const struct ofconn *ofconn, struct ofpbuf *msg,
1329             struct rconn_packet_counter *counter)
1330 {
1331     ofpmsg_update_length(msg);
1332     rconn_send(ofconn->rconn, msg, counter);
1333 }
1334 \f
1335 /* Sending asynchronous messages. */
1336
1337 static void schedule_packet_in(struct ofconn *, struct ofputil_packet_in);
1338
1339 /* Sends an OFPT_PORT_STATUS message with 'opp' and 'reason' to appropriate
1340  * controllers managed by 'mgr'. */
1341 void
1342 connmgr_send_port_status(struct connmgr *mgr,
1343                          const struct ofputil_phy_port *pp, uint8_t reason)
1344 {
1345     /* XXX Should limit the number of queued port status change messages. */
1346     struct ofputil_port_status ps;
1347     struct ofconn *ofconn;
1348
1349     ps.reason = reason;
1350     ps.desc = *pp;
1351     LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1352         if (ofconn_receives_async_msg(ofconn, OAM_PORT_STATUS, reason)) {
1353             struct ofpbuf *msg;
1354
1355             msg = ofputil_encode_port_status(&ps, ofconn_get_protocol(ofconn));
1356             ofconn_send(ofconn, msg, NULL);
1357         }
1358     }
1359 }
1360
1361 /* Sends an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message based on 'fr' to
1362  * appropriate controllers managed by 'mgr'. */
1363 void
1364 connmgr_send_flow_removed(struct connmgr *mgr,
1365                           const struct ofputil_flow_removed *fr)
1366 {
1367     struct ofconn *ofconn;
1368
1369     LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1370         if (ofconn_receives_async_msg(ofconn, OAM_FLOW_REMOVED, fr->reason)) {
1371             struct ofpbuf *msg;
1372
1373             /* Account flow expirations as replies to OpenFlow requests.  That
1374              * works because preventing OpenFlow requests from being processed
1375              * also prevents new flows from being added (and expiring).  (It
1376              * also prevents processing OpenFlow requests that would not add
1377              * new flows, so it is imperfect.) */
1378             msg = ofputil_encode_flow_removed(fr, ofconn_get_protocol(ofconn));
1379             ofconn_send_reply(ofconn, msg);
1380         }
1381     }
1382 }
1383
1384 /* Given 'pin', sends an OFPT_PACKET_IN message to each OpenFlow controller as
1385  * necessary according to their individual configurations.
1386  *
1387  * The caller doesn't need to fill in pin->buffer_id or pin->total_len. */
1388 void
1389 connmgr_send_packet_in(struct connmgr *mgr,
1390                        const struct ofputil_packet_in *pin)
1391 {
1392     struct ofconn *ofconn;
1393
1394     LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1395         if (ofconn_receives_async_msg(ofconn, OAM_PACKET_IN, pin->reason)
1396             && ofconn->controller_id == pin->controller_id) {
1397             schedule_packet_in(ofconn, *pin);
1398         }
1399     }
1400 }
1401
1402 /* pinsched callback for sending 'ofp_packet_in' on 'ofconn'. */
1403 static void
1404 do_send_packet_in(struct ofpbuf *ofp_packet_in, void *ofconn_)
1405 {
1406     struct ofconn *ofconn = ofconn_;
1407
1408     rconn_send_with_limit(ofconn->rconn, ofp_packet_in,
1409                           ofconn->packet_in_counter, 100);
1410 }
1411
1412 /* Takes 'pin', composes an OpenFlow packet-in message from it, and passes it
1413  * to 'ofconn''s packet scheduler for sending. */
1414 static void
1415 schedule_packet_in(struct ofconn *ofconn, struct ofputil_packet_in pin)
1416 {
1417     struct connmgr *mgr = ofconn->connmgr;
1418
1419     pin.total_len = pin.packet_len;
1420
1421     /* Get OpenFlow buffer_id. */
1422     if (pin.reason == OFPR_ACTION) {
1423         pin.buffer_id = UINT32_MAX;
1424     } else if (mgr->fail_open && fail_open_is_active(mgr->fail_open)) {
1425         pin.buffer_id = pktbuf_get_null();
1426     } else if (!ofconn->pktbuf) {
1427         pin.buffer_id = UINT32_MAX;
1428     } else {
1429         pin.buffer_id = pktbuf_save(ofconn->pktbuf, pin.packet, pin.packet_len,
1430                                     pin.fmd.in_port);
1431     }
1432
1433     /* Figure out how much of the packet to send. */
1434     if (pin.reason == OFPR_NO_MATCH) {
1435         pin.send_len = pin.packet_len;
1436     } else {
1437         /* Caller should have initialized 'send_len' to 'max_len' specified in
1438          * output action. */
1439     }
1440     if (pin.buffer_id != UINT32_MAX) {
1441         pin.send_len = MIN(pin.send_len, ofconn->miss_send_len);
1442     }
1443
1444     /* Make OFPT_PACKET_IN and hand over to packet scheduler.  It might
1445      * immediately call into do_send_packet_in() or it might buffer it for a
1446      * while (until a later call to pinsched_run()). */
1447     pinsched_send(ofconn->schedulers[pin.reason == OFPR_NO_MATCH ? 0 : 1],
1448                   pin.fmd.in_port,
1449                   ofputil_encode_packet_in(&pin, ofconn_get_protocol(ofconn),
1450                                            ofconn->packet_in_format),
1451                   do_send_packet_in, ofconn);
1452 }
1453 \f
1454 /* Fail-open settings. */
1455
1456 /* Returns the failure handling mode (OFPROTO_FAIL_SECURE or
1457  * OFPROTO_FAIL_STANDALONE) for 'mgr'. */
1458 enum ofproto_fail_mode
1459 connmgr_get_fail_mode(const struct connmgr *mgr)
1460 {
1461     return mgr->fail_mode;
1462 }
1463
1464 /* Sets the failure handling mode for 'mgr' to 'fail_mode' (either
1465  * OFPROTO_FAIL_SECURE or OFPROTO_FAIL_STANDALONE). */
1466 void
1467 connmgr_set_fail_mode(struct connmgr *mgr, enum ofproto_fail_mode fail_mode)
1468 {
1469     if (mgr->fail_mode != fail_mode) {
1470         mgr->fail_mode = fail_mode;
1471         update_fail_open(mgr);
1472         if (!connmgr_has_controllers(mgr)) {
1473             ofproto_flush_flows(mgr->ofproto);
1474         }
1475     }
1476 }
1477 \f
1478 /* Fail-open implementation. */
1479
1480 /* Returns the longest probe interval among the primary controllers configured
1481  * on 'mgr'.  Returns 0 if there are no primary controllers. */
1482 int
1483 connmgr_get_max_probe_interval(const struct connmgr *mgr)
1484 {
1485     const struct ofconn *ofconn;
1486     int max_probe_interval;
1487
1488     max_probe_interval = 0;
1489     HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1490         int probe_interval = rconn_get_probe_interval(ofconn->rconn);
1491         max_probe_interval = MAX(max_probe_interval, probe_interval);
1492     }
1493     return max_probe_interval;
1494 }
1495
1496 /* Returns the number of seconds for which all of 'mgr's primary controllers
1497  * have been disconnected.  Returns 0 if 'mgr' has no primary controllers. */
1498 int
1499 connmgr_failure_duration(const struct connmgr *mgr)
1500 {
1501     const struct ofconn *ofconn;
1502     int min_failure_duration;
1503
1504     if (!connmgr_has_controllers(mgr)) {
1505         return 0;
1506     }
1507
1508     min_failure_duration = INT_MAX;
1509     HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1510         int failure_duration = rconn_failure_duration(ofconn->rconn);
1511         min_failure_duration = MIN(min_failure_duration, failure_duration);
1512     }
1513     return min_failure_duration;
1514 }
1515
1516 /* Returns true if at least one primary controller is connected (regardless of
1517  * whether those controllers are believed to have authenticated and accepted
1518  * this switch), false if none of them are connected. */
1519 bool
1520 connmgr_is_any_controller_connected(const struct connmgr *mgr)
1521 {
1522     const struct ofconn *ofconn;
1523
1524     HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1525         if (rconn_is_connected(ofconn->rconn)) {
1526             return true;
1527         }
1528     }
1529     return false;
1530 }
1531
1532 /* Returns true if at least one primary controller is believed to have
1533  * authenticated and accepted this switch, false otherwise. */
1534 bool
1535 connmgr_is_any_controller_admitted(const struct connmgr *mgr)
1536 {
1537     const struct ofconn *ofconn;
1538
1539     HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1540         if (rconn_is_admitted(ofconn->rconn)) {
1541             return true;
1542         }
1543     }
1544     return false;
1545 }
1546 \f
1547 /* In-band configuration. */
1548
1549 static bool any_extras_changed(const struct connmgr *,
1550                                const struct sockaddr_in *extras, size_t n);
1551
1552 /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'mgr''s
1553  * in-band control should guarantee access, in the same way that in-band
1554  * control guarantees access to OpenFlow controllers. */
1555 void
1556 connmgr_set_extra_in_band_remotes(struct connmgr *mgr,
1557                                   const struct sockaddr_in *extras, size_t n)
1558 {
1559     if (!any_extras_changed(mgr, extras, n)) {
1560         return;
1561     }
1562
1563     free(mgr->extra_in_band_remotes);
1564     mgr->n_extra_remotes = n;
1565     mgr->extra_in_band_remotes = xmemdup(extras, n * sizeof *extras);
1566
1567     update_in_band_remotes(mgr);
1568 }
1569
1570 /* Sets the OpenFlow queue used by flows set up by in-band control on
1571  * 'mgr' to 'queue_id'.  If 'queue_id' is negative, then in-band control
1572  * flows will use the default queue. */
1573 void
1574 connmgr_set_in_band_queue(struct connmgr *mgr, int queue_id)
1575 {
1576     if (queue_id != mgr->in_band_queue) {
1577         mgr->in_band_queue = queue_id;
1578         update_in_band_remotes(mgr);
1579     }
1580 }
1581
1582 static bool
1583 any_extras_changed(const struct connmgr *mgr,
1584                    const struct sockaddr_in *extras, size_t n)
1585 {
1586     size_t i;
1587
1588     if (n != mgr->n_extra_remotes) {
1589         return true;
1590     }
1591
1592     for (i = 0; i < n; i++) {
1593         const struct sockaddr_in *old = &mgr->extra_in_band_remotes[i];
1594         const struct sockaddr_in *new = &extras[i];
1595
1596         if (old->sin_addr.s_addr != new->sin_addr.s_addr ||
1597             old->sin_port != new->sin_port) {
1598             return true;
1599         }
1600     }
1601
1602     return false;
1603 }
1604 \f
1605 /* In-band implementation. */
1606
1607 bool
1608 connmgr_msg_in_hook(struct connmgr *mgr, const struct flow *flow,
1609                     const struct ofpbuf *packet)
1610 {
1611     return mgr->in_band && in_band_msg_in_hook(mgr->in_band, flow, packet);
1612 }
1613
1614 bool
1615 connmgr_may_set_up_flow(struct connmgr *mgr, const struct flow *flow,
1616                         const struct nlattr *odp_actions,
1617                         size_t actions_len)
1618 {
1619     return !mgr->in_band || in_band_rule_check(flow, odp_actions, actions_len);
1620 }
1621 \f
1622 /* Fail-open and in-band implementation. */
1623
1624 /* Called by 'ofproto' after all flows have been flushed, to allow fail-open
1625  * and standalone mode to re-create their flows.
1626  *
1627  * In-band control has more sophisticated code that manages flows itself. */
1628 void
1629 connmgr_flushed(struct connmgr *mgr)
1630 {
1631     if (mgr->fail_open) {
1632         fail_open_flushed(mgr->fail_open);
1633     }
1634
1635     /* If there are no controllers and we're in standalone mode, set up a flow
1636      * that matches every packet and directs them to OFPP_NORMAL (which goes to
1637      * us).  Otherwise, the switch is in secure mode and we won't pass any
1638      * traffic until a controller has been defined and it tells us to do so. */
1639     if (!connmgr_has_controllers(mgr)
1640         && mgr->fail_mode == OFPROTO_FAIL_STANDALONE) {
1641         struct ofpbuf ofpacts;
1642         struct match match;
1643
1644         ofpbuf_init(&ofpacts, OFPACT_OUTPUT_SIZE);
1645         ofpact_put_OUTPUT(&ofpacts)->port = OFPP_NORMAL;
1646         ofpact_pad(&ofpacts);
1647
1648         match_init_catchall(&match);
1649         ofproto_add_flow(mgr->ofproto, &match, 0, ofpacts.data, ofpacts.size);
1650
1651         ofpbuf_uninit(&ofpacts);
1652     }
1653 }
1654 \f
1655 /* Creates a new ofservice for 'target' in 'mgr'.  Returns 0 if successful,
1656  * otherwise a positive errno value.
1657  *
1658  * ofservice_reconfigure() must be called to fully configure the new
1659  * ofservice. */
1660 static int
1661 ofservice_create(struct connmgr *mgr, const char *target,
1662                  uint32_t allowed_versions, uint8_t dscp)
1663 {
1664     struct ofservice *ofservice;
1665     struct pvconn *pvconn;
1666     int error;
1667
1668     error = pvconn_open(target, allowed_versions, &pvconn, dscp);
1669     if (error) {
1670         return error;
1671     }
1672
1673     ofservice = xzalloc(sizeof *ofservice);
1674     hmap_insert(&mgr->services, &ofservice->node, hash_string(target, 0));
1675     ofservice->pvconn = pvconn;
1676     ofservice->allowed_versions = allowed_versions;
1677
1678     return 0;
1679 }
1680
1681 static void
1682 ofservice_destroy(struct connmgr *mgr, struct ofservice *ofservice)
1683 {
1684     hmap_remove(&mgr->services, &ofservice->node);
1685     pvconn_close(ofservice->pvconn);
1686     free(ofservice);
1687 }
1688
1689 static void
1690 ofservice_reconfigure(struct ofservice *ofservice,
1691                       const struct ofproto_controller *c)
1692 {
1693     ofservice->probe_interval = c->probe_interval;
1694     ofservice->rate_limit = c->rate_limit;
1695     ofservice->burst_limit = c->burst_limit;
1696     ofservice->enable_async_msgs = c->enable_async_msgs;
1697     ofservice->dscp = c->dscp;
1698 }
1699
1700 /* Finds and returns the ofservice within 'mgr' that has the given
1701  * 'target', or a null pointer if none exists. */
1702 static struct ofservice *
1703 ofservice_lookup(struct connmgr *mgr, const char *target)
1704 {
1705     struct ofservice *ofservice;
1706
1707     HMAP_FOR_EACH_WITH_HASH (ofservice, node, hash_string(target, 0),
1708                              &mgr->services) {
1709         if (!strcmp(pvconn_get_name(ofservice->pvconn), target)) {
1710             return ofservice;
1711         }
1712     }
1713     return NULL;
1714 }
1715 \f
1716 /* Flow monitors (NXST_FLOW_MONITOR). */
1717
1718 /* A counter incremented when something significant happens to an OpenFlow
1719  * rule.
1720  *
1721  *     - When a rule is added, its 'add_seqno' and 'modify_seqno' are set to
1722  *       the current value (which is then incremented).
1723  *
1724  *     - When a rule is modified, its 'modify_seqno' is set to the current
1725  *       value (which is then incremented).
1726  *
1727  * Thus, by comparing an old value of monitor_seqno against a rule's
1728  * 'add_seqno', one can tell whether the rule was added before or after the old
1729  * value was read, and similarly for 'modify_seqno'.
1730  *
1731  * 32 bits should normally be sufficient (and would be nice, to save space in
1732  * each rule) but then we'd have to have some special cases for wraparound.
1733  *
1734  * We initialize monitor_seqno to 1 to allow 0 to be used as an invalid
1735  * value. */
1736 static uint64_t monitor_seqno = 1;
1737
1738 COVERAGE_DEFINE(ofmonitor_pause);
1739 COVERAGE_DEFINE(ofmonitor_resume);
1740
1741 enum ofperr
1742 ofmonitor_create(const struct ofputil_flow_monitor_request *request,
1743                  struct ofconn *ofconn, struct ofmonitor **monitorp)
1744 {
1745     struct ofmonitor *m;
1746
1747     *monitorp = NULL;
1748
1749     m = ofmonitor_lookup(ofconn, request->id);
1750     if (m) {
1751         return OFPERR_NXBRC_FM_DUPLICATE_ID;
1752     }
1753
1754     m = xmalloc(sizeof *m);
1755     m->ofconn = ofconn;
1756     hmap_insert(&ofconn->monitors, &m->ofconn_node, hash_int(request->id, 0));
1757     m->id = request->id;
1758     m->flags = request->flags;
1759     m->out_port = request->out_port;
1760     m->table_id = request->table_id;
1761     minimatch_init(&m->match, &request->match);
1762
1763     *monitorp = m;
1764     return 0;
1765 }
1766
1767 struct ofmonitor *
1768 ofmonitor_lookup(struct ofconn *ofconn, uint32_t id)
1769 {
1770     struct ofmonitor *m;
1771
1772     HMAP_FOR_EACH_IN_BUCKET (m, ofconn_node, hash_int(id, 0),
1773                              &ofconn->monitors) {
1774         if (m->id == id) {
1775             return m;
1776         }
1777     }
1778     return NULL;
1779 }
1780
1781 void
1782 ofmonitor_destroy(struct ofmonitor *m)
1783 {
1784     if (m) {
1785         hmap_remove(&m->ofconn->monitors, &m->ofconn_node);
1786         free(m);
1787     }
1788 }
1789
1790 void
1791 ofmonitor_report(struct connmgr *mgr, struct rule *rule,
1792                  enum nx_flow_update_event event,
1793                  enum ofp_flow_removed_reason reason,
1794                  const struct ofconn *abbrev_ofconn, ovs_be32 abbrev_xid)
1795 {
1796     enum nx_flow_monitor_flags update;
1797     struct ofconn *ofconn;
1798
1799     switch (event) {
1800     case NXFME_ADDED:
1801         update = NXFMF_ADD;
1802         rule->add_seqno = rule->modify_seqno = monitor_seqno++;
1803         break;
1804
1805     case NXFME_DELETED:
1806         update = NXFMF_DELETE;
1807         break;
1808
1809     case NXFME_MODIFIED:
1810         update = NXFMF_MODIFY;
1811         rule->modify_seqno = monitor_seqno++;
1812         break;
1813
1814     default:
1815     case NXFME_ABBREV:
1816         NOT_REACHED();
1817     }
1818
1819     LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1820         enum nx_flow_monitor_flags flags = 0;
1821         struct ofmonitor *m;
1822
1823         if (ofconn->monitor_paused) {
1824             /* Only send NXFME_DELETED notifications for flows that were added
1825              * before we paused. */
1826             if (event != NXFME_DELETED
1827                 || rule->add_seqno > ofconn->monitor_paused) {
1828                 continue;
1829             }
1830         }
1831
1832         HMAP_FOR_EACH (m, ofconn_node, &ofconn->monitors) {
1833             if (m->flags & update
1834                 && (m->table_id == 0xff || m->table_id == rule->table_id)
1835                 && ofoperation_has_out_port(rule->pending, m->out_port)
1836                 && cls_rule_is_loose_match(&rule->cr, &m->match)) {
1837                 flags |= m->flags;
1838             }
1839         }
1840
1841         if (flags) {
1842             if (list_is_empty(&ofconn->updates)) {
1843                 ofputil_start_flow_update(&ofconn->updates);
1844                 ofconn->sent_abbrev_update = false;
1845             }
1846
1847             if (ofconn != abbrev_ofconn || ofconn->monitor_paused) {
1848                 struct ofputil_flow_update fu;
1849                 struct match match;
1850
1851                 fu.event = event;
1852                 fu.reason = event == NXFME_DELETED ? reason : 0;
1853                 fu.idle_timeout = rule->idle_timeout;
1854                 fu.hard_timeout = rule->hard_timeout;
1855                 fu.table_id = rule->table_id;
1856                 fu.cookie = rule->flow_cookie;
1857                 minimatch_expand(&rule->cr.match, &match);
1858                 fu.match = &match;
1859                 fu.priority = rule->cr.priority;
1860                 if (flags & NXFMF_ACTIONS) {
1861                     fu.ofpacts = rule->ofpacts;
1862                     fu.ofpacts_len = rule->ofpacts_len;
1863                 } else {
1864                     fu.ofpacts = NULL;
1865                     fu.ofpacts_len = 0;
1866                 }
1867                 ofputil_append_flow_update(&fu, &ofconn->updates);
1868             } else if (!ofconn->sent_abbrev_update) {
1869                 struct ofputil_flow_update fu;
1870
1871                 fu.event = NXFME_ABBREV;
1872                 fu.xid = abbrev_xid;
1873                 ofputil_append_flow_update(&fu, &ofconn->updates);
1874
1875                 ofconn->sent_abbrev_update = true;
1876             }
1877         }
1878     }
1879 }
1880
1881 void
1882 ofmonitor_flush(struct connmgr *mgr)
1883 {
1884     struct ofconn *ofconn;
1885
1886     LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1887         struct ofpbuf *msg, *next;
1888
1889         LIST_FOR_EACH_SAFE (msg, next, list_node, &ofconn->updates) {
1890             list_remove(&msg->list_node);
1891             ofconn_send(ofconn, msg, ofconn->monitor_counter);
1892             if (!ofconn->monitor_paused
1893                 && ofconn->monitor_counter->n_bytes > 128 * 1024) {
1894                 struct ofpbuf *pause;
1895
1896                 COVERAGE_INC(ofmonitor_pause);
1897                 ofconn->monitor_paused = monitor_seqno++;
1898                 pause = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_MONITOR_PAUSED,
1899                                          OFP10_VERSION, htonl(0), 0);
1900                 ofconn_send(ofconn, pause, ofconn->monitor_counter);
1901             }
1902         }
1903     }
1904 }
1905
1906 static void
1907 ofmonitor_resume(struct ofconn *ofconn)
1908 {
1909     struct ofpbuf *resumed;
1910     struct ofmonitor *m;
1911     struct list rules;
1912     struct list msgs;
1913
1914     list_init(&rules);
1915     HMAP_FOR_EACH (m, ofconn_node, &ofconn->monitors) {
1916         ofmonitor_collect_resume_rules(m, ofconn->monitor_paused, &rules);
1917     }
1918
1919     list_init(&msgs);
1920     ofmonitor_compose_refresh_updates(&rules, &msgs);
1921
1922     resumed = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_MONITOR_RESUMED, OFP10_VERSION,
1923                                htonl(0), 0);
1924     list_push_back(&msgs, &resumed->list_node);
1925     ofconn_send_replies(ofconn, &msgs);
1926
1927     ofconn->monitor_paused = 0;
1928 }
1929
1930 static void
1931 ofmonitor_run(struct connmgr *mgr)
1932 {
1933     struct ofconn *ofconn;
1934
1935     LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1936         if (ofconn->monitor_paused && !ofconn->monitor_counter->n_packets) {
1937             COVERAGE_INC(ofmonitor_resume);
1938             ofmonitor_resume(ofconn);
1939         }
1940     }
1941 }
1942
1943 static void
1944 ofmonitor_wait(struct connmgr *mgr)
1945 {
1946     struct ofconn *ofconn;
1947
1948     LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1949         if (ofconn->monitor_paused && !ofconn->monitor_counter->n_packets) {
1950             poll_immediate_wake();
1951         }
1952     }
1953 }