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