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