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