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