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