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