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