connmgr: New function ofconn_send_error().
[sliver-openvswitch.git] / ofproto / connmgr.c
1 /*
2  * Copyright (c) 2009, 2010, 2011 Nicira Networks.
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-util.h"
29 #include "ofpbuf.h"
30 #include "pinsched.h"
31 #include "poll-loop.h"
32 #include "pktbuf.h"
33 #include "private.h"
34 #include "rconn.h"
35 #include "shash.h"
36 #include "timeval.h"
37 #include "vconn.h"
38 #include "vlog.h"
39
40 VLOG_DEFINE_THIS_MODULE(connmgr);
41 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
42
43 COVERAGE_DEFINE(ofconn_stuck);
44
45 /* An OpenFlow connection. */
46 struct ofconn {
47     struct connmgr *connmgr;    /* Connection's manager. */
48     struct list node;           /* In struct connmgr's "all_conns" list. */
49     struct rconn *rconn;        /* OpenFlow connection. */
50     enum ofconn_type type;      /* Type. */
51     enum nx_flow_format flow_format; /* Currently selected flow format. */
52     bool flow_mod_table_id;     /* NXT_FLOW_MOD_TABLE_ID enabled? */
53
54     /* OFPT_PACKET_IN related data. */
55     struct rconn_packet_counter *packet_in_counter; /* # queued on 'rconn'. */
56 #define N_SCHEDULERS 2
57     struct pinsched *schedulers[N_SCHEDULERS];
58     struct pktbuf *pktbuf;         /* OpenFlow packet buffers. */
59     int miss_send_len;             /* Bytes to send of buffered packets. */
60
61     /* Number of OpenFlow messages queued on 'rconn' as replies to OpenFlow
62      * requests, and the maximum number before we stop reading OpenFlow
63      * requests.  */
64 #define OFCONN_REPLY_MAX 100
65     struct rconn_packet_counter *reply_counter;
66
67     /* type == OFCONN_PRIMARY only. */
68     enum nx_role role;           /* Role. */
69     struct hmap_node hmap_node;  /* In struct connmgr's "controllers" map. */
70     enum ofproto_band band;      /* In-band or out-of-band? */
71 };
72
73 static struct ofconn *ofconn_create(struct connmgr *, struct rconn *,
74                                     enum ofconn_type);
75 static void ofconn_destroy(struct ofconn *);
76
77 static void ofconn_reconfigure(struct ofconn *,
78                                const struct ofproto_controller *);
79
80 static void ofconn_run(struct ofconn *,
81                        void (*handle_openflow)(struct ofconn *,
82                                                struct ofpbuf *ofp_msg));
83 static void ofconn_wait(struct ofconn *);
84
85 static const char *ofconn_get_target(const struct ofconn *);
86 static char *ofconn_make_name(const struct connmgr *, const char *target);
87
88 static void ofconn_set_rate_limit(struct ofconn *, int rate, int burst);
89
90 static bool ofconn_receives_async_msgs(const struct ofconn *);
91
92 static void ofconn_send(const struct ofconn *, struct ofpbuf *,
93                         struct rconn_packet_counter *);
94
95 static void do_send_packet_in(struct ofpbuf *, void *ofconn_);
96
97 /* A listener for incoming OpenFlow "service" connections. */
98 struct ofservice {
99     struct hmap_node node;      /* In struct connmgr's "services" hmap. */
100     struct pvconn *pvconn;      /* OpenFlow connection listener. */
101
102     /* These are not used by ofservice directly.  They are settings for
103      * accepted "struct ofconn"s from the pvconn. */
104     int probe_interval;         /* Max idle time before probing, in seconds. */
105     int rate_limit;             /* Max packet-in rate in packets per second. */
106     int burst_limit;            /* Limit on accumulating packet credits. */
107 };
108
109 static void ofservice_reconfigure(struct ofservice *,
110                                   const struct ofproto_controller *);
111 static int ofservice_create(struct connmgr *, const char *target);
112 static void ofservice_destroy(struct connmgr *, struct ofservice *);
113 static struct ofservice *ofservice_lookup(struct connmgr *,
114                                           const char *target);
115
116 /* Connection manager for an OpenFlow switch. */
117 struct connmgr {
118     struct ofproto *ofproto;
119     char *name;
120     char *local_port_name;
121
122     /* OpenFlow connections. */
123     struct hmap controllers;   /* Controller "struct ofconn"s. */
124     struct list all_conns;     /* Contains "struct ofconn"s. */
125
126     /* OpenFlow listeners. */
127     struct hmap services;       /* Contains "struct ofservice"s. */
128     struct pvconn **snoops;
129     size_t n_snoops;
130
131     /* Fail open. */
132     struct fail_open *fail_open;
133     enum ofproto_fail_mode fail_mode;
134
135     /* In-band control. */
136     struct in_band *in_band;
137     long long int next_in_band_update;
138     struct sockaddr_in *extra_in_band_remotes;
139     size_t n_extra_remotes;
140     int in_band_queue;
141 };
142
143 static void update_in_band_remotes(struct connmgr *);
144 static void add_snooper(struct connmgr *, struct vconn *);
145
146 /* Creates and returns a new connection manager owned by 'ofproto'.  'name' is
147  * a name for the ofproto suitable for using in log messages.
148  * 'local_port_name' is the name of the local port (OFPP_LOCAL) within
149  * 'ofproto'. */
150 struct connmgr *
151 connmgr_create(struct ofproto *ofproto,
152                const char *name, const char *local_port_name)
153 {
154     struct connmgr *mgr;
155
156     mgr = xmalloc(sizeof *mgr);
157     mgr->ofproto = ofproto;
158     mgr->name = xstrdup(name);
159     mgr->local_port_name = xstrdup(local_port_name);
160
161     hmap_init(&mgr->controllers);
162     list_init(&mgr->all_conns);
163
164     hmap_init(&mgr->services);
165     mgr->snoops = NULL;
166     mgr->n_snoops = 0;
167
168     mgr->fail_open = NULL;
169     mgr->fail_mode = OFPROTO_FAIL_SECURE;
170
171     mgr->in_band = NULL;
172     mgr->next_in_band_update = LLONG_MAX;
173     mgr->extra_in_band_remotes = NULL;
174     mgr->n_extra_remotes = 0;
175     mgr->in_band_queue = -1;
176
177     return mgr;
178 }
179
180 /* Frees 'mgr' and all of its resources. */
181 void
182 connmgr_destroy(struct connmgr *mgr)
183 {
184     struct ofservice *ofservice, *next_ofservice;
185     struct ofconn *ofconn, *next_ofconn;
186     size_t i;
187
188     if (!mgr) {
189         return;
190     }
191
192     LIST_FOR_EACH_SAFE (ofconn, next_ofconn, node, &mgr->all_conns) {
193         ofconn_destroy(ofconn);
194     }
195     hmap_destroy(&mgr->controllers);
196
197     HMAP_FOR_EACH_SAFE (ofservice, next_ofservice, node, &mgr->services) {
198         ofservice_destroy(mgr, ofservice);
199     }
200     hmap_destroy(&mgr->services);
201
202     for (i = 0; i < mgr->n_snoops; i++) {
203         pvconn_close(mgr->snoops[i]);
204     }
205     free(mgr->snoops);
206
207     fail_open_destroy(mgr->fail_open);
208     mgr->fail_open = NULL;
209
210     in_band_destroy(mgr->in_band);
211     mgr->in_band = NULL;
212     free(mgr->extra_in_band_remotes);
213     free(mgr->name);
214     free(mgr->local_port_name);
215
216     free(mgr);
217 }
218
219 /* Does all of the periodic maintenance required by 'mgr'.  Calls
220  * 'handle_openflow' for each message received on an OpenFlow connection,
221  * passing along the OpenFlow connection itself and the message that was sent.
222  * The 'handle_openflow' callback must not free the message. */
223 void
224 connmgr_run(struct connmgr *mgr,
225             void (*handle_openflow)(struct ofconn *, struct ofpbuf *ofp_msg))
226 {
227     struct ofconn *ofconn, *next_ofconn;
228     struct ofservice *ofservice;
229     size_t i;
230
231     if (mgr->in_band) {
232         if (time_msec() >= mgr->next_in_band_update) {
233             update_in_band_remotes(mgr);
234         }
235         in_band_run(mgr->in_band);
236     }
237
238     LIST_FOR_EACH_SAFE (ofconn, next_ofconn, node, &mgr->all_conns) {
239         ofconn_run(ofconn, handle_openflow);
240     }
241
242     /* Fail-open maintenance.  Do this after processing the ofconns since
243      * fail-open checks the status of the controller rconn. */
244     if (mgr->fail_open) {
245         fail_open_run(mgr->fail_open);
246     }
247
248     HMAP_FOR_EACH (ofservice, node, &mgr->services) {
249         struct vconn *vconn;
250         int retval;
251
252         retval = pvconn_accept(ofservice->pvconn, OFP_VERSION, &vconn);
253         if (!retval) {
254             struct rconn *rconn;
255             char *name;
256
257             rconn = rconn_create(ofservice->probe_interval, 0);
258             name = ofconn_make_name(mgr, vconn_get_name(vconn));
259             rconn_connect_unreliably(rconn, vconn, name);
260             free(name);
261
262             ofconn = ofconn_create(mgr, rconn, OFCONN_SERVICE);
263             ofconn_set_rate_limit(ofconn, ofservice->rate_limit,
264                                   ofservice->burst_limit);
265         } else if (retval != EAGAIN) {
266             VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval));
267         }
268     }
269
270     for (i = 0; i < mgr->n_snoops; i++) {
271         struct vconn *vconn;
272         int retval;
273
274         retval = pvconn_accept(mgr->snoops[i], OFP_VERSION, &vconn);
275         if (!retval) {
276             add_snooper(mgr, vconn);
277         } else if (retval != EAGAIN) {
278             VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval));
279         }
280     }
281 }
282
283 /* Causes the poll loop to wake up when connmgr_run() needs to run. */
284 void
285 connmgr_wait(struct connmgr *mgr)
286 {
287     struct ofservice *ofservice;
288     struct ofconn *ofconn;
289     size_t i;
290
291     LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
292         ofconn_wait(ofconn);
293     }
294     if (mgr->in_band) {
295         poll_timer_wait_until(mgr->next_in_band_update);
296         in_band_wait(mgr->in_band);
297     }
298     if (mgr->fail_open) {
299         fail_open_wait(mgr->fail_open);
300     }
301     HMAP_FOR_EACH (ofservice, node, &mgr->services) {
302         pvconn_wait(ofservice->pvconn);
303     }
304     for (i = 0; i < mgr->n_snoops; i++) {
305         pvconn_wait(mgr->snoops[i]);
306     }
307 }
308
309 /* Returns the ofproto that owns 'ofconn''s connmgr. */
310 struct ofproto *
311 ofconn_get_ofproto(const struct ofconn *ofconn)
312 {
313     return ofconn->connmgr->ofproto;
314 }
315 \f
316 /* OpenFlow configuration. */
317
318 static void add_controller(struct connmgr *, const char *target);
319 static struct ofconn *find_controller_by_target(struct connmgr *,
320                                                 const char *target);
321 static void update_fail_open(struct connmgr *);
322 static int set_pvconns(struct pvconn ***pvconnsp, size_t *n_pvconnsp,
323                        const struct sset *);
324
325 /* Returns true if 'mgr' has any configured primary controllers.
326  *
327  * Service controllers do not count, but configured primary controllers do
328  * count whether or not they are currently connected. */
329 bool
330 connmgr_has_controllers(const struct connmgr *mgr)
331 {
332     return !hmap_is_empty(&mgr->controllers);
333 }
334
335 /* Initializes 'info' and populates it with information about each configured
336  * primary controller.  The keys in 'info' are the controllers' targets; the
337  * data values are corresponding "struct ofproto_controller_info".
338  *
339  * The caller owns 'info' and everything in it and should free it when it is no
340  * longer needed. */
341 void
342 connmgr_get_controller_info(struct connmgr *mgr, struct shash *info)
343 {
344     const struct ofconn *ofconn;
345
346     shash_init(info);
347
348     HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
349         const struct rconn *rconn = ofconn->rconn;
350         time_t now = time_now();
351         time_t last_connection = rconn_get_last_connection(rconn);
352         time_t last_disconnect = rconn_get_last_disconnect(rconn);
353         int last_error = rconn_get_last_error(rconn);
354         struct ofproto_controller_info *cinfo = xmalloc(sizeof *cinfo);
355
356         shash_add(info, rconn_get_target(rconn), cinfo);
357
358         cinfo->is_connected = rconn_is_connected(rconn);
359         cinfo->role = ofconn->role;
360
361         cinfo->pairs.n = 0;
362
363         if (last_error) {
364             cinfo->pairs.keys[cinfo->pairs.n] = "last_error";
365             cinfo->pairs.values[cinfo->pairs.n++] =
366                 xstrdup(ovs_retval_to_string(last_error));
367         }
368
369         cinfo->pairs.keys[cinfo->pairs.n] = "state";
370         cinfo->pairs.values[cinfo->pairs.n++] =
371             xstrdup(rconn_get_state(rconn));
372
373         if (last_connection != TIME_MIN) {
374             cinfo->pairs.keys[cinfo->pairs.n] = "sec_since_connect";
375             cinfo->pairs.values[cinfo->pairs.n++]
376                 = xasprintf("%ld", (long int) (now - last_connection));
377         }
378
379         if (last_disconnect != TIME_MIN) {
380             cinfo->pairs.keys[cinfo->pairs.n] = "sec_since_disconnect";
381             cinfo->pairs.values[cinfo->pairs.n++]
382                 = xasprintf("%ld", (long int) (now - last_disconnect));
383         }
384     }
385 }
386
387 /* Changes 'mgr''s set of controllers to the 'n_controllers' controllers in
388  * 'controllers'. */
389 void
390 connmgr_set_controllers(struct connmgr *mgr,
391                         const struct ofproto_controller *controllers,
392                         size_t n_controllers)
393 {
394     bool had_controllers = connmgr_has_controllers(mgr);
395     struct shash new_controllers;
396     struct ofconn *ofconn, *next_ofconn;
397     struct ofservice *ofservice, *next_ofservice;
398     size_t i;
399
400     /* Create newly configured controllers and services.
401      * Create a name to ofproto_controller mapping in 'new_controllers'. */
402     shash_init(&new_controllers);
403     for (i = 0; i < n_controllers; i++) {
404         const struct ofproto_controller *c = &controllers[i];
405
406         if (!vconn_verify_name(c->target)) {
407             if (!find_controller_by_target(mgr, c->target)) {
408                 add_controller(mgr, c->target);
409             }
410         } else if (!pvconn_verify_name(c->target)) {
411             if (!ofservice_lookup(mgr, c->target)) {
412                 ofservice_create(mgr, c->target);
413             }
414         } else {
415             VLOG_WARN_RL(&rl, "%s: unsupported controller \"%s\"",
416                          mgr->name, c->target);
417             continue;
418         }
419
420         shash_add_once(&new_controllers, c->target, &controllers[i]);
421     }
422
423     /* Delete controllers that are no longer configured.
424      * Update configuration of all now-existing controllers. */
425     HMAP_FOR_EACH_SAFE (ofconn, next_ofconn, hmap_node, &mgr->controllers) {
426         struct ofproto_controller *c;
427
428         c = shash_find_data(&new_controllers, ofconn_get_target(ofconn));
429         if (!c) {
430             ofconn_destroy(ofconn);
431         } else {
432             ofconn_reconfigure(ofconn, c);
433         }
434     }
435
436     /* Delete services that are no longer configured.
437      * Update configuration of all now-existing services. */
438     HMAP_FOR_EACH_SAFE (ofservice, next_ofservice, node, &mgr->services) {
439         struct ofproto_controller *c;
440
441         c = shash_find_data(&new_controllers,
442                             pvconn_get_name(ofservice->pvconn));
443         if (!c) {
444             ofservice_destroy(mgr, ofservice);
445         } else {
446             ofservice_reconfigure(ofservice, c);
447         }
448     }
449
450     shash_destroy(&new_controllers);
451
452     update_in_band_remotes(mgr);
453     update_fail_open(mgr);
454     if (had_controllers != connmgr_has_controllers(mgr)) {
455         ofproto_flush_flows(mgr->ofproto);
456     }
457 }
458
459 /* Drops the connections between 'mgr' and all of its primary and secondary
460  * controllers, forcing them to reconnect. */
461 void
462 connmgr_reconnect(const struct connmgr *mgr)
463 {
464     struct ofconn *ofconn;
465
466     LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
467         rconn_reconnect(ofconn->rconn);
468     }
469 }
470
471 /* Sets the "snoops" for 'mgr' to the pvconn targets listed in 'snoops'.
472  *
473  * A "snoop" is a pvconn to which every OpenFlow message to or from the most
474  * important controller on 'mgr' is mirrored. */
475 int
476 connmgr_set_snoops(struct connmgr *mgr, const struct sset *snoops)
477 {
478     return set_pvconns(&mgr->snoops, &mgr->n_snoops, snoops);
479 }
480
481 /* Adds each of the snoops currently configured on 'mgr' to 'snoops'. */
482 void
483 connmgr_get_snoops(const struct connmgr *mgr, struct sset *snoops)
484 {
485     size_t i;
486
487     for (i = 0; i < mgr->n_snoops; i++) {
488         sset_add(snoops, pvconn_get_name(mgr->snoops[i]));
489     }
490 }
491
492 /* Returns true if 'mgr' has at least one snoop, false if it has none. */
493 bool
494 connmgr_has_snoops(const struct connmgr *mgr)
495 {
496     return mgr->n_snoops > 0;
497 }
498
499 /* Creates a new controller for 'target' in 'mgr'.  update_controller() needs
500  * to be called later to finish the new ofconn's configuration. */
501 static void
502 add_controller(struct connmgr *mgr, const char *target)
503 {
504     char *name = ofconn_make_name(mgr, target);
505     struct ofconn *ofconn;
506
507     ofconn = ofconn_create(mgr, rconn_create(5, 8), OFCONN_PRIMARY);
508     ofconn->pktbuf = pktbuf_create();
509     ofconn->miss_send_len = OFP_DEFAULT_MISS_SEND_LEN;
510     rconn_connect(ofconn->rconn, target, name);
511     hmap_insert(&mgr->controllers, &ofconn->hmap_node, hash_string(target, 0));
512
513     free(name);
514 }
515
516 static struct ofconn *
517 find_controller_by_target(struct connmgr *mgr, const char *target)
518 {
519     struct ofconn *ofconn;
520
521     HMAP_FOR_EACH_WITH_HASH (ofconn, hmap_node,
522                              hash_string(target, 0), &mgr->controllers) {
523         if (!strcmp(ofconn_get_target(ofconn), target)) {
524             return ofconn;
525         }
526     }
527     return NULL;
528 }
529
530 static void
531 update_in_band_remotes(struct connmgr *mgr)
532 {
533     struct sockaddr_in *addrs;
534     size_t max_addrs, n_addrs;
535     struct ofconn *ofconn;
536     size_t i;
537
538     /* Allocate enough memory for as many remotes as we could possibly have. */
539     max_addrs = mgr->n_extra_remotes + hmap_count(&mgr->controllers);
540     addrs = xmalloc(max_addrs * sizeof *addrs);
541     n_addrs = 0;
542
543     /* Add all the remotes. */
544     HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
545         struct sockaddr_in *sin = &addrs[n_addrs];
546
547         if (ofconn->band == OFPROTO_OUT_OF_BAND) {
548             continue;
549         }
550
551         sin->sin_addr.s_addr = rconn_get_remote_ip(ofconn->rconn);
552         if (sin->sin_addr.s_addr) {
553             sin->sin_port = rconn_get_remote_port(ofconn->rconn);
554             n_addrs++;
555         }
556     }
557     for (i = 0; i < mgr->n_extra_remotes; i++) {
558         addrs[n_addrs++] = mgr->extra_in_band_remotes[i];
559     }
560
561     /* Create or update or destroy in-band. */
562     if (n_addrs) {
563         if (!mgr->in_band) {
564             in_band_create(mgr->ofproto, mgr->local_port_name, &mgr->in_band);
565         }
566         if (mgr->in_band) {
567             in_band_set_remotes(mgr->in_band, addrs, n_addrs);
568         }
569         in_band_set_queue(mgr->in_band, mgr->in_band_queue);
570         mgr->next_in_band_update = time_msec() + 1000;
571     } else {
572         in_band_destroy(mgr->in_band);
573         mgr->in_band = NULL;
574     }
575
576     /* Clean up. */
577     free(addrs);
578 }
579
580 static void
581 update_fail_open(struct connmgr *mgr)
582 {
583     if (connmgr_has_controllers(mgr)
584         && mgr->fail_mode == OFPROTO_FAIL_STANDALONE) {
585         if (!mgr->fail_open) {
586             mgr->fail_open = fail_open_create(mgr->ofproto, mgr);
587         }
588     } else {
589         fail_open_destroy(mgr->fail_open);
590         mgr->fail_open = NULL;
591     }
592 }
593
594 static int
595 set_pvconns(struct pvconn ***pvconnsp, size_t *n_pvconnsp,
596             const struct sset *sset)
597 {
598     struct pvconn **pvconns = *pvconnsp;
599     size_t n_pvconns = *n_pvconnsp;
600     const char *name;
601     int retval = 0;
602     size_t i;
603
604     for (i = 0; i < n_pvconns; i++) {
605         pvconn_close(pvconns[i]);
606     }
607     free(pvconns);
608
609     pvconns = xmalloc(sset_count(sset) * sizeof *pvconns);
610     n_pvconns = 0;
611     SSET_FOR_EACH (name, sset) {
612         struct pvconn *pvconn;
613         int error;
614
615         error = pvconn_open(name, &pvconn);
616         if (!error) {
617             pvconns[n_pvconns++] = pvconn;
618         } else {
619             VLOG_ERR("failed to listen on %s: %s", name, strerror(error));
620             if (!retval) {
621                 retval = error;
622             }
623         }
624     }
625
626     *pvconnsp = pvconns;
627     *n_pvconnsp = n_pvconns;
628
629     return retval;
630 }
631
632 /* Returns a "preference level" for snooping 'ofconn'.  A higher return value
633  * means that 'ofconn' is more interesting for monitoring than a lower return
634  * value. */
635 static int
636 snoop_preference(const struct ofconn *ofconn)
637 {
638     switch (ofconn->role) {
639     case NX_ROLE_MASTER:
640         return 3;
641     case NX_ROLE_OTHER:
642         return 2;
643     case NX_ROLE_SLAVE:
644         return 1;
645     default:
646         /* Shouldn't happen. */
647         return 0;
648     }
649 }
650
651 /* One of 'mgr''s "snoop" pvconns has accepted a new connection on 'vconn'.
652  * Connects this vconn to a controller. */
653 static void
654 add_snooper(struct connmgr *mgr, struct vconn *vconn)
655 {
656     struct ofconn *ofconn, *best;
657
658     /* Pick a controller for monitoring. */
659     best = NULL;
660     LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
661         if (ofconn->type == OFCONN_PRIMARY
662             && (!best || snoop_preference(ofconn) > snoop_preference(best))) {
663             best = ofconn;
664         }
665     }
666
667     if (best) {
668         rconn_add_monitor(best->rconn, vconn);
669     } else {
670         VLOG_INFO_RL(&rl, "no controller connection to snoop");
671         vconn_close(vconn);
672     }
673 }
674 \f
675 /* Public ofconn functions. */
676
677 /* Returns the connection type, either OFCONN_PRIMARY or OFCONN_SERVICE. */
678 enum ofconn_type
679 ofconn_get_type(const struct ofconn *ofconn)
680 {
681     return ofconn->type;
682 }
683
684 /* Returns the role configured for 'ofconn'.
685  *
686  * The default role, if no other role has been set, is NX_ROLE_OTHER. */
687 enum nx_role
688 ofconn_get_role(const struct ofconn *ofconn)
689 {
690     return ofconn->role;
691 }
692
693 /* Changes 'ofconn''s role to 'role'.  If 'role' is NX_ROLE_MASTER then any
694  * existing master is demoted to a slave. */
695 void
696 ofconn_set_role(struct ofconn *ofconn, enum nx_role role)
697 {
698     if (role == NX_ROLE_MASTER) {
699         struct ofconn *other;
700
701         HMAP_FOR_EACH (other, hmap_node, &ofconn->connmgr->controllers) {
702             if (other->role == NX_ROLE_MASTER) {
703                 other->role = NX_ROLE_SLAVE;
704             }
705         }
706     }
707     ofconn->role = role;
708 }
709
710 /* Returns the currently configured flow format for 'ofconn', one of NXFF_*.
711  *
712  * The default, if no other format has been set, is NXFF_OPENFLOW10. */
713 enum nx_flow_format
714 ofconn_get_flow_format(struct ofconn *ofconn)
715 {
716     return ofconn->flow_format;
717 }
718
719 /* Sets the flow format for 'ofconn' to 'flow_format' (one of NXFF_*). */
720 void
721 ofconn_set_flow_format(struct ofconn *ofconn, enum nx_flow_format flow_format)
722 {
723     ofconn->flow_format = flow_format;
724 }
725
726 /* Returns true if the NXT_FLOW_MOD_TABLE_ID extension is enabled, false
727  * otherwise.
728  *
729  * By default the extension is not enabled. */
730 bool
731 ofconn_get_flow_mod_table_id(const struct ofconn *ofconn)
732 {
733     return ofconn->flow_mod_table_id;
734 }
735
736 /* Enables or disables (according to 'enable') the NXT_FLOW_MOD_TABLE_ID
737  * extension on 'ofconn'. */
738 void
739 ofconn_set_flow_mod_table_id(struct ofconn *ofconn, bool enable)
740 {
741     ofconn->flow_mod_table_id = enable;
742 }
743
744 /* Returns the default miss send length for 'ofconn'. */
745 int
746 ofconn_get_miss_send_len(const struct ofconn *ofconn)
747 {
748     return ofconn->miss_send_len;
749 }
750
751 /* Sets the default miss send length for 'ofconn' to 'miss_send_len'. */
752 void
753 ofconn_set_miss_send_len(struct ofconn *ofconn, int miss_send_len)
754 {
755     ofconn->miss_send_len = miss_send_len;
756 }
757
758 /* Sends 'msg' on 'ofconn', accounting it as a reply.  (If there is a
759  * sufficient number of OpenFlow replies in-flight on a single ofconn, then the
760  * connmgr will stop accepting new OpenFlow requests on that ofconn until the
761  * controller has accepted some of the replies.) */
762 void
763 ofconn_send_reply(const struct ofconn *ofconn, struct ofpbuf *msg)
764 {
765     ofconn_send(ofconn, msg, ofconn->reply_counter);
766 }
767
768 /* Sends each of the messages in list 'replies' on 'ofconn' in order,
769  * accounting them as replies. */
770 void
771 ofconn_send_replies(const struct ofconn *ofconn, struct list *replies)
772 {
773     struct ofpbuf *reply, *next;
774
775     LIST_FOR_EACH_SAFE (reply, next, list_node, replies) {
776         list_remove(&reply->list_node);
777         ofconn_send_reply(ofconn, reply);
778     }
779 }
780
781 /* Sends 'error', which should be an OpenFlow error created with
782  * e.g. ofp_mkerr(), on 'ofconn', as a reply to 'request'.  Only at most the
783  * first 64 bytes of 'request' are used. */
784 void
785 ofconn_send_error(const struct ofconn *ofconn,
786                   const struct ofp_header *request, int error)
787 {
788     struct ofpbuf *msg = ofputil_encode_error_msg(error, request);
789     if (msg) {
790         ofconn_send_reply(ofconn, msg);
791     }
792 }
793
794 /* Same as pktbuf_retrieve(), using the pktbuf owned by 'ofconn'. */
795 int
796 ofconn_pktbuf_retrieve(struct ofconn *ofconn, uint32_t id,
797                        struct ofpbuf **bufferp, uint16_t *in_port)
798 {
799     return pktbuf_retrieve(ofconn->pktbuf, id, bufferp, in_port);
800 }
801 \f
802 /* Private ofconn functions. */
803
804 static const char *
805 ofconn_get_target(const struct ofconn *ofconn)
806 {
807     return rconn_get_target(ofconn->rconn);
808 }
809
810 static struct ofconn *
811 ofconn_create(struct connmgr *mgr, struct rconn *rconn, enum ofconn_type type)
812 {
813     struct ofconn *ofconn = xzalloc(sizeof *ofconn);
814     ofconn->connmgr = mgr;
815     list_push_back(&mgr->all_conns, &ofconn->node);
816     ofconn->rconn = rconn;
817     ofconn->type = type;
818     ofconn->flow_format = NXFF_OPENFLOW10;
819     ofconn->flow_mod_table_id = false;
820     ofconn->role = NX_ROLE_OTHER;
821     ofconn->packet_in_counter = rconn_packet_counter_create ();
822     ofconn->pktbuf = NULL;
823     ofconn->miss_send_len = 0;
824     ofconn->reply_counter = rconn_packet_counter_create ();
825     return ofconn;
826 }
827
828 static void
829 ofconn_destroy(struct ofconn *ofconn)
830 {
831     if (ofconn->type == OFCONN_PRIMARY) {
832         hmap_remove(&ofconn->connmgr->controllers, &ofconn->hmap_node);
833     }
834
835     list_remove(&ofconn->node);
836     rconn_destroy(ofconn->rconn);
837     rconn_packet_counter_destroy(ofconn->packet_in_counter);
838     rconn_packet_counter_destroy(ofconn->reply_counter);
839     pktbuf_destroy(ofconn->pktbuf);
840     free(ofconn);
841 }
842
843 /* Reconfigures 'ofconn' to match 'c'.  'ofconn' and 'c' must have the same
844  * target. */
845 static void
846 ofconn_reconfigure(struct ofconn *ofconn, const struct ofproto_controller *c)
847 {
848     int probe_interval;
849
850     ofconn->band = c->band;
851
852     rconn_set_max_backoff(ofconn->rconn, c->max_backoff);
853
854     probe_interval = c->probe_interval ? MAX(c->probe_interval, 5) : 0;
855     rconn_set_probe_interval(ofconn->rconn, probe_interval);
856
857     ofconn_set_rate_limit(ofconn, c->rate_limit, c->burst_limit);
858 }
859
860 static void
861 ofconn_run(struct ofconn *ofconn,
862            void (*handle_openflow)(struct ofconn *, struct ofpbuf *ofp_msg))
863 {
864     struct connmgr *mgr = ofconn->connmgr;
865     int iteration;
866     size_t i;
867
868     for (i = 0; i < N_SCHEDULERS; i++) {
869         pinsched_run(ofconn->schedulers[i], do_send_packet_in, ofconn);
870     }
871
872     rconn_run(ofconn->rconn);
873
874     if (rconn_packet_counter_read (ofconn->reply_counter) < OFCONN_REPLY_MAX) {
875         /* Limit the number of iterations to prevent other tasks from
876          * starving. */
877         for (iteration = 0; iteration < 50; iteration++) {
878             struct ofpbuf *of_msg = rconn_recv(ofconn->rconn);
879             if (!of_msg) {
880                 break;
881             }
882             if (mgr->fail_open) {
883                 fail_open_maybe_recover(mgr->fail_open);
884             }
885             handle_openflow(ofconn, of_msg);
886             ofpbuf_delete(of_msg);
887         }
888     }
889
890     if (!rconn_is_alive(ofconn->rconn)) {
891         ofconn_destroy(ofconn);
892     }
893 }
894
895 static void
896 ofconn_wait(struct ofconn *ofconn)
897 {
898     int i;
899
900     for (i = 0; i < N_SCHEDULERS; i++) {
901         pinsched_wait(ofconn->schedulers[i]);
902     }
903     rconn_run_wait(ofconn->rconn);
904     if (rconn_packet_counter_read (ofconn->reply_counter) < OFCONN_REPLY_MAX) {
905         rconn_recv_wait(ofconn->rconn);
906     } else {
907         COVERAGE_INC(ofconn_stuck);
908     }
909 }
910
911 /* Returns true if 'ofconn' should receive asynchronous messages. */
912 static bool
913 ofconn_receives_async_msgs(const struct ofconn *ofconn)
914 {
915     if (!rconn_is_connected(ofconn->rconn)) {
916         return false;
917     } else if (ofconn->type == OFCONN_PRIMARY) {
918         /* Primary controllers always get asynchronous messages unless they
919          * have configured themselves as "slaves".  */
920         return ofconn->role != NX_ROLE_SLAVE;
921     } else {
922         /* Service connections don't get asynchronous messages unless they have
923          * explicitly asked for them by setting a nonzero miss send length. */
924         return ofconn->miss_send_len > 0;
925     }
926 }
927
928 /* Returns a human-readable name for an OpenFlow connection between 'mgr' and
929  * 'target', suitable for use in log messages for identifying the connection.
930  *
931  * The name is dynamically allocated.  The caller should free it (with free())
932  * when it is no longer needed. */
933 static char *
934 ofconn_make_name(const struct connmgr *mgr, const char *target)
935 {
936     return xasprintf("%s<->%s", mgr->name, target);
937 }
938
939 static void
940 ofconn_set_rate_limit(struct ofconn *ofconn, int rate, int burst)
941 {
942     int i;
943
944     for (i = 0; i < N_SCHEDULERS; i++) {
945         struct pinsched **s = &ofconn->schedulers[i];
946
947         if (rate > 0) {
948             if (!*s) {
949                 *s = pinsched_create(rate, burst);
950             } else {
951                 pinsched_set_limits(*s, rate, burst);
952             }
953         } else {
954             pinsched_destroy(*s);
955             *s = NULL;
956         }
957     }
958 }
959
960 static void
961 ofconn_send(const struct ofconn *ofconn, struct ofpbuf *msg,
962             struct rconn_packet_counter *counter)
963 {
964     update_openflow_length(msg);
965     if (rconn_send(ofconn->rconn, msg, counter)) {
966         ofpbuf_delete(msg);
967     }
968 }
969 \f
970 /* Sending asynchronous messages. */
971
972 static void schedule_packet_in(struct ofconn *, struct ofputil_packet_in,
973                                const struct flow *, struct ofpbuf *rw_packet);
974
975 /* Sends an OFPT_PORT_STATUS message with 'opp' and 'reason' to appropriate
976  * controllers managed by 'mgr'. */
977 void
978 connmgr_send_port_status(struct connmgr *mgr, const struct ofp_phy_port *opp,
979                          uint8_t reason)
980 {
981     /* XXX Should limit the number of queued port status change messages. */
982     struct ofconn *ofconn;
983
984     LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
985         struct ofp_port_status *ops;
986         struct ofpbuf *b;
987
988         /* Primary controllers, even slaves, should always get port status
989            updates.  Otherwise obey ofconn_receives_async_msgs(). */
990         if (ofconn->type != OFCONN_PRIMARY
991             && !ofconn_receives_async_msgs(ofconn)) {
992             continue;
993         }
994
995         ops = make_openflow_xid(sizeof *ops, OFPT_PORT_STATUS, 0, &b);
996         ops->reason = reason;
997         ops->desc = *opp;
998         ofconn_send(ofconn, b, NULL);
999     }
1000 }
1001
1002 /* Sends an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message based on 'fr' to
1003  * appropriate controllers managed by 'mgr'. */
1004 void
1005 connmgr_send_flow_removed(struct connmgr *mgr,
1006                           const struct ofputil_flow_removed *fr)
1007 {
1008     struct ofconn *ofconn;
1009
1010     LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1011         struct ofpbuf *msg;
1012
1013         if (!ofconn_receives_async_msgs(ofconn)) {
1014             continue;
1015         }
1016
1017         /* Account flow expirations as replies to OpenFlow requests.  That
1018          * works because preventing OpenFlow requests from being processed also
1019          * prevents new flows from being added (and expiring).  (It also
1020          * prevents processing OpenFlow requests that would not add new flows,
1021          * so it is imperfect.) */
1022         msg = ofputil_encode_flow_removed(fr, ofconn->flow_format);
1023         ofconn_send_reply(ofconn, msg);
1024     }
1025 }
1026
1027 /* Given 'pin', sends an OFPT_PACKET_IN message to each OpenFlow controller as
1028  * necessary according to their individual configurations.
1029  *
1030  * 'rw_packet' may be NULL.  Otherwise, 'rw_packet' must contain the same data
1031  * as pin->packet.  (rw_packet == pin->packet is also valid.)  Ownership of
1032  * 'rw_packet' is transferred to this function. */
1033 void
1034 connmgr_send_packet_in(struct connmgr *mgr,
1035                        const struct ofputil_packet_in *pin,
1036                        const struct flow *flow, struct ofpbuf *rw_packet)
1037 {
1038     struct ofconn *ofconn, *prev;
1039
1040     prev = NULL;
1041     LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1042         if (ofconn_receives_async_msgs(ofconn)) {
1043             if (prev) {
1044                 schedule_packet_in(prev, *pin, flow, NULL);
1045             }
1046             prev = ofconn;
1047         }
1048     }
1049     if (prev) {
1050         schedule_packet_in(prev, *pin, flow, rw_packet);
1051     } else {
1052         ofpbuf_delete(rw_packet);
1053     }
1054 }
1055
1056 /* pinsched callback for sending 'ofp_packet_in' on 'ofconn'. */
1057 static void
1058 do_send_packet_in(struct ofpbuf *ofp_packet_in, void *ofconn_)
1059 {
1060     struct ofconn *ofconn = ofconn_;
1061
1062     rconn_send_with_limit(ofconn->rconn, ofp_packet_in,
1063                           ofconn->packet_in_counter, 100);
1064 }
1065
1066 /* Takes 'pin', whose packet has the flow specified by 'flow', composes an
1067  * OpenFlow packet-in message from it, and passes it to 'ofconn''s packet
1068  * scheduler for sending.
1069  *
1070  * 'rw_packet' may be NULL.  Otherwise, 'rw_packet' must contain the same data
1071  * as pin->packet.  (rw_packet == pin->packet is also valid.)  Ownership of
1072  * 'rw_packet' is transferred to this function. */
1073 static void
1074 schedule_packet_in(struct ofconn *ofconn, struct ofputil_packet_in pin,
1075                    const struct flow *flow, struct ofpbuf *rw_packet)
1076 {
1077     struct connmgr *mgr = ofconn->connmgr;
1078
1079     /* Get OpenFlow buffer_id. */
1080     if (pin.reason == OFPR_ACTION) {
1081         pin.buffer_id = UINT32_MAX;
1082     } else if (mgr->fail_open && fail_open_is_active(mgr->fail_open)) {
1083         pin.buffer_id = pktbuf_get_null();
1084     } else if (!ofconn->pktbuf) {
1085         pin.buffer_id = UINT32_MAX;
1086     } else {
1087         pin.buffer_id = pktbuf_save(ofconn->pktbuf, pin.packet, flow->in_port);
1088     }
1089
1090     /* Figure out how much of the packet to send. */
1091     if (pin.reason == OFPR_NO_MATCH) {
1092         pin.send_len = pin.packet->size;
1093     } else {
1094         /* Caller should have initialized 'send_len' to 'max_len' specified in
1095          * struct ofp_action_output. */
1096     }
1097     if (pin.buffer_id != UINT32_MAX) {
1098         pin.send_len = MIN(pin.send_len, ofconn->miss_send_len);
1099     }
1100
1101     /* Make OFPT_PACKET_IN and hand over to packet scheduler.  It might
1102      * immediately call into do_send_packet_in() or it might buffer it for a
1103      * while (until a later call to pinsched_run()). */
1104     pinsched_send(ofconn->schedulers[pin.reason == OFPR_NO_MATCH ? 0 : 1],
1105                   flow->in_port, ofputil_encode_packet_in(&pin, rw_packet),
1106                   do_send_packet_in, ofconn);
1107 }
1108 \f
1109 /* Fail-open settings. */
1110
1111 /* Returns the failure handling mode (OFPROTO_FAIL_SECURE or
1112  * OFPROTO_FAIL_STANDALONE) for 'mgr'. */
1113 enum ofproto_fail_mode
1114 connmgr_get_fail_mode(const struct connmgr *mgr)
1115 {
1116     return mgr->fail_mode;
1117 }
1118
1119 /* Sets the failure handling mode for 'mgr' to 'fail_mode' (either
1120  * OFPROTO_FAIL_SECURE or OFPROTO_FAIL_STANDALONE). */
1121 void
1122 connmgr_set_fail_mode(struct connmgr *mgr, enum ofproto_fail_mode fail_mode)
1123 {
1124     if (mgr->fail_mode != fail_mode) {
1125         mgr->fail_mode = fail_mode;
1126         update_fail_open(mgr);
1127         if (!connmgr_has_controllers(mgr)) {
1128             ofproto_flush_flows(mgr->ofproto);
1129         }
1130     }
1131 }
1132 \f
1133 /* Fail-open implementation. */
1134
1135 /* Returns the longest probe interval among the primary controllers configured
1136  * on 'mgr'.  Returns 0 if there are no primary controllers. */
1137 int
1138 connmgr_get_max_probe_interval(const struct connmgr *mgr)
1139 {
1140     const struct ofconn *ofconn;
1141     int max_probe_interval;
1142
1143     max_probe_interval = 0;
1144     HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1145         int probe_interval = rconn_get_probe_interval(ofconn->rconn);
1146         max_probe_interval = MAX(max_probe_interval, probe_interval);
1147     }
1148     return max_probe_interval;
1149 }
1150
1151 /* Returns the number of seconds for which all of 'mgr's primary controllers
1152  * have been disconnected.  Returns 0 if 'mgr' has no primary controllers. */
1153 int
1154 connmgr_failure_duration(const struct connmgr *mgr)
1155 {
1156     const struct ofconn *ofconn;
1157     int min_failure_duration;
1158
1159     if (!connmgr_has_controllers(mgr)) {
1160         return 0;
1161     }
1162
1163     min_failure_duration = INT_MAX;
1164     HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1165         int failure_duration = rconn_failure_duration(ofconn->rconn);
1166         min_failure_duration = MIN(min_failure_duration, failure_duration);
1167     }
1168     return min_failure_duration;
1169 }
1170
1171 /* Returns true if at least one primary controller is connected (regardless of
1172  * whether those controllers are believed to have authenticated and accepted
1173  * this switch), false if none of them are connected. */
1174 bool
1175 connmgr_is_any_controller_connected(const struct connmgr *mgr)
1176 {
1177     const struct ofconn *ofconn;
1178
1179     HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1180         if (rconn_is_connected(ofconn->rconn)) {
1181             return true;
1182         }
1183     }
1184     return false;
1185 }
1186
1187 /* Returns true if at least one primary controller is believed to have
1188  * authenticated and accepted this switch, false otherwise. */
1189 bool
1190 connmgr_is_any_controller_admitted(const struct connmgr *mgr)
1191 {
1192     const struct ofconn *ofconn;
1193
1194     HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1195         if (rconn_is_admitted(ofconn->rconn)) {
1196             return true;
1197         }
1198     }
1199     return false;
1200 }
1201
1202 /* Sends 'packet' to each controller connected to 'mgr'.  Takes ownership of
1203  * 'packet'. */
1204 void
1205 connmgr_broadcast(struct connmgr *mgr, struct ofpbuf *packet)
1206 {
1207     struct ofconn *ofconn, *prev;
1208
1209     prev = NULL;
1210     LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1211         if (prev) {
1212             ofconn_send_reply(ofconn, ofpbuf_clone(packet));
1213         }
1214         if (rconn_is_connected(ofconn->rconn)) {
1215             prev = ofconn;
1216         }
1217     }
1218     if (prev) {
1219         ofconn_send_reply(prev, packet);
1220     } else {
1221         ofpbuf_delete(packet);
1222     }
1223 }
1224 \f
1225 /* In-band configuration. */
1226
1227 static bool any_extras_changed(const struct connmgr *,
1228                                const struct sockaddr_in *extras, size_t n);
1229
1230 /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'mgr''s
1231  * in-band control should guarantee access, in the same way that in-band
1232  * control guarantees access to OpenFlow controllers. */
1233 void
1234 connmgr_set_extra_in_band_remotes(struct connmgr *mgr,
1235                                   const struct sockaddr_in *extras, size_t n)
1236 {
1237     if (!any_extras_changed(mgr, extras, n)) {
1238         return;
1239     }
1240
1241     free(mgr->extra_in_band_remotes);
1242     mgr->n_extra_remotes = n;
1243     mgr->extra_in_band_remotes = xmemdup(extras, n * sizeof *extras);
1244
1245     update_in_band_remotes(mgr);
1246 }
1247
1248 /* Sets the OpenFlow queue used by flows set up by in-band control on
1249  * 'mgr' to 'queue_id'.  If 'queue_id' is negative, then in-band control
1250  * flows will use the default queue. */
1251 void
1252 connmgr_set_in_band_queue(struct connmgr *mgr, int queue_id)
1253 {
1254     if (queue_id != mgr->in_band_queue) {
1255         mgr->in_band_queue = queue_id;
1256         update_in_band_remotes(mgr);
1257     }
1258 }
1259
1260 static bool
1261 any_extras_changed(const struct connmgr *mgr,
1262                    const struct sockaddr_in *extras, size_t n)
1263 {
1264     size_t i;
1265
1266     if (n != mgr->n_extra_remotes) {
1267         return true;
1268     }
1269
1270     for (i = 0; i < n; i++) {
1271         const struct sockaddr_in *old = &mgr->extra_in_band_remotes[i];
1272         const struct sockaddr_in *new = &extras[i];
1273
1274         if (old->sin_addr.s_addr != new->sin_addr.s_addr ||
1275             old->sin_port != new->sin_port) {
1276             return true;
1277         }
1278     }
1279
1280     return false;
1281 }
1282 \f
1283 /* In-band implementation. */
1284
1285 bool
1286 connmgr_msg_in_hook(struct connmgr *mgr, const struct flow *flow,
1287                     const struct ofpbuf *packet)
1288 {
1289     return mgr->in_band && in_band_msg_in_hook(mgr->in_band, flow, packet);
1290 }
1291
1292 bool
1293 connmgr_may_set_up_flow(struct connmgr *mgr, const struct flow *flow,
1294                         const struct nlattr *odp_actions,
1295                         size_t actions_len)
1296 {
1297     return !mgr->in_band || in_band_rule_check(flow, odp_actions, actions_len);
1298 }
1299 \f
1300 /* Fail-open and in-band implementation. */
1301
1302 /* Called by 'ofproto' after all flows have been flushed, to allow fail-open
1303  * and in-band control to re-create their flows. */
1304 void
1305 connmgr_flushed(struct connmgr *mgr)
1306 {
1307     if (mgr->in_band) {
1308         in_band_flushed(mgr->in_band);
1309     }
1310     if (mgr->fail_open) {
1311         fail_open_flushed(mgr->fail_open);
1312     }
1313
1314     /* If there are no controllers and we're in standalone mode, set up a flow
1315      * that matches every packet and directs them to OFPP_NORMAL (which goes to
1316      * us).  Otherwise, the switch is in secure mode and we won't pass any
1317      * traffic until a controller has been defined and it tells us to do so. */
1318     if (!connmgr_has_controllers(mgr)
1319         && mgr->fail_mode == OFPROTO_FAIL_STANDALONE) {
1320         union ofp_action action;
1321         struct cls_rule rule;
1322
1323         memset(&action, 0, sizeof action);
1324         action.type = htons(OFPAT_OUTPUT);
1325         action.output.len = htons(sizeof action);
1326         action.output.port = htons(OFPP_NORMAL);
1327         cls_rule_init_catchall(&rule, 0);
1328         ofproto_add_flow(mgr->ofproto, &rule, &action, 1);
1329     }
1330 }
1331 \f
1332 /* Creates a new ofservice for 'target' in 'mgr'.  Returns 0 if successful,
1333  * otherwise a positive errno value.
1334  *
1335  * ofservice_reconfigure() must be called to fully configure the new
1336  * ofservice. */
1337 static int
1338 ofservice_create(struct connmgr *mgr, const char *target)
1339 {
1340     struct ofservice *ofservice;
1341     struct pvconn *pvconn;
1342     int error;
1343
1344     error = pvconn_open(target, &pvconn);
1345     if (error) {
1346         return error;
1347     }
1348
1349     ofservice = xzalloc(sizeof *ofservice);
1350     hmap_insert(&mgr->services, &ofservice->node, hash_string(target, 0));
1351     ofservice->pvconn = pvconn;
1352
1353     return 0;
1354 }
1355
1356 static void
1357 ofservice_destroy(struct connmgr *mgr, struct ofservice *ofservice)
1358 {
1359     hmap_remove(&mgr->services, &ofservice->node);
1360     pvconn_close(ofservice->pvconn);
1361     free(ofservice);
1362 }
1363
1364 static void
1365 ofservice_reconfigure(struct ofservice *ofservice,
1366                       const struct ofproto_controller *c)
1367 {
1368     ofservice->probe_interval = c->probe_interval;
1369     ofservice->rate_limit = c->rate_limit;
1370     ofservice->burst_limit = c->burst_limit;
1371 }
1372
1373 /* Finds and returns the ofservice within 'mgr' that has the given
1374  * 'target', or a null pointer if none exists. */
1375 static struct ofservice *
1376 ofservice_lookup(struct connmgr *mgr, const char *target)
1377 {
1378     struct ofservice *ofservice;
1379
1380     HMAP_FOR_EACH_WITH_HASH (ofservice, node, hash_string(target, 0),
1381                              &mgr->services) {
1382         if (!strcmp(pvconn_get_name(ofservice->pvconn), target)) {
1383             return ofservice;
1384         }
1385     }
1386     return NULL;
1387 }