2 * Copyright (c) 2009, 2010, 2011 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #include "fail-open.h"
30 #include "ofproto-provider.h"
32 #include "poll-loop.h"
41 VLOG_DEFINE_THIS_MODULE(connmgr);
42 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
44 /* An OpenFlow connection. */
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. */
51 bool flow_mod_table_id; /* NXT_FLOW_MOD_TABLE_ID enabled? */
53 /* Asynchronous flow table operation support. */
54 struct list opgroups; /* Contains pending "ofopgroups", if any. */
55 struct ofpbuf *blocked; /* Postponed OpenFlow message, if any. */
56 bool retry; /* True if 'blocked' is ready to try again. */
58 /* OFPT_PACKET_IN related data. */
59 struct rconn_packet_counter *packet_in_counter; /* # queued on 'rconn'. */
60 #define N_SCHEDULERS 2
61 struct pinsched *schedulers[N_SCHEDULERS];
62 struct pktbuf *pktbuf; /* OpenFlow packet buffers. */
63 int miss_send_len; /* Bytes to send of buffered packets. */
65 /* Number of OpenFlow messages queued on 'rconn' as replies to OpenFlow
66 * requests, and the maximum number before we stop reading OpenFlow
68 #define OFCONN_REPLY_MAX 100
69 struct rconn_packet_counter *reply_counter;
71 /* type == OFCONN_PRIMARY only. */
72 enum nx_role role; /* Role. */
73 struct hmap_node hmap_node; /* In struct connmgr's "controllers" map. */
74 enum ofproto_band band; /* In-band or out-of-band? */
77 static struct ofconn *ofconn_create(struct connmgr *, struct rconn *,
79 static void ofconn_destroy(struct ofconn *);
81 static void ofconn_reconfigure(struct ofconn *,
82 const struct ofproto_controller *);
84 static void ofconn_run(struct ofconn *,
85 bool (*handle_openflow)(struct ofconn *,
86 struct ofpbuf *ofp_msg));
87 static void ofconn_wait(struct ofconn *, bool handling_openflow);
89 static const char *ofconn_get_target(const struct ofconn *);
90 static char *ofconn_make_name(const struct connmgr *, const char *target);
92 static void ofconn_set_rate_limit(struct ofconn *, int rate, int burst);
94 static bool ofconn_receives_async_msgs(const struct ofconn *);
96 static void ofconn_send(const struct ofconn *, struct ofpbuf *,
97 struct rconn_packet_counter *);
99 static void do_send_packet_in(struct ofpbuf *, void *ofconn_);
101 /* A listener for incoming OpenFlow "service" connections. */
103 struct hmap_node node; /* In struct connmgr's "services" hmap. */
104 struct pvconn *pvconn; /* OpenFlow connection listener. */
106 /* These are not used by ofservice directly. They are settings for
107 * accepted "struct ofconn"s from the pvconn. */
108 int probe_interval; /* Max idle time before probing, in seconds. */
109 int rate_limit; /* Max packet-in rate in packets per second. */
110 int burst_limit; /* Limit on accumulating packet credits. */
113 static void ofservice_reconfigure(struct ofservice *,
114 const struct ofproto_controller *);
115 static int ofservice_create(struct connmgr *, const char *target);
116 static void ofservice_destroy(struct connmgr *, struct ofservice *);
117 static struct ofservice *ofservice_lookup(struct connmgr *,
120 /* Connection manager for an OpenFlow switch. */
122 struct ofproto *ofproto;
124 char *local_port_name;
126 /* OpenFlow connections. */
127 struct hmap controllers; /* Controller "struct ofconn"s. */
128 struct list all_conns; /* Contains "struct ofconn"s. */
130 /* OpenFlow listeners. */
131 struct hmap services; /* Contains "struct ofservice"s. */
132 struct pvconn **snoops;
136 struct fail_open *fail_open;
137 enum ofproto_fail_mode fail_mode;
139 /* In-band control. */
140 struct in_band *in_band;
141 struct sockaddr_in *extra_in_band_remotes;
142 size_t n_extra_remotes;
146 static void update_in_band_remotes(struct connmgr *);
147 static void add_snooper(struct connmgr *, struct vconn *);
149 /* Creates and returns a new connection manager owned by 'ofproto'. 'name' is
150 * a name for the ofproto suitable for using in log messages.
151 * 'local_port_name' is the name of the local port (OFPP_LOCAL) within
154 connmgr_create(struct ofproto *ofproto,
155 const char *name, const char *local_port_name)
159 mgr = xmalloc(sizeof *mgr);
160 mgr->ofproto = ofproto;
161 mgr->name = xstrdup(name);
162 mgr->local_port_name = xstrdup(local_port_name);
164 hmap_init(&mgr->controllers);
165 list_init(&mgr->all_conns);
167 hmap_init(&mgr->services);
171 mgr->fail_open = NULL;
172 mgr->fail_mode = OFPROTO_FAIL_SECURE;
175 mgr->extra_in_band_remotes = NULL;
176 mgr->n_extra_remotes = 0;
177 mgr->in_band_queue = -1;
182 /* Frees 'mgr' and all of its resources. */
184 connmgr_destroy(struct connmgr *mgr)
186 struct ofservice *ofservice, *next_ofservice;
187 struct ofconn *ofconn, *next_ofconn;
194 LIST_FOR_EACH_SAFE (ofconn, next_ofconn, node, &mgr->all_conns) {
195 ofconn_destroy(ofconn);
197 hmap_destroy(&mgr->controllers);
199 HMAP_FOR_EACH_SAFE (ofservice, next_ofservice, node, &mgr->services) {
200 ofservice_destroy(mgr, ofservice);
202 hmap_destroy(&mgr->services);
204 for (i = 0; i < mgr->n_snoops; i++) {
205 pvconn_close(mgr->snoops[i]);
209 fail_open_destroy(mgr->fail_open);
210 mgr->fail_open = NULL;
212 in_band_destroy(mgr->in_band);
214 free(mgr->extra_in_band_remotes);
216 free(mgr->local_port_name);
221 /* Does all of the periodic maintenance required by 'mgr'.
223 * If 'handle_openflow' is nonnull, calls 'handle_openflow' for each message
224 * received on an OpenFlow connection, passing along the OpenFlow connection
225 * itself and the message that was sent. If 'handle_openflow' returns true,
226 * the message is considered to be fully processed. If 'handle_openflow'
227 * returns false, the message is considered not to have been processed at all;
228 * it will be stored and re-presented to 'handle_openflow' following the next
229 * call to connmgr_retry(). 'handle_openflow' must not modify or free the
232 * If 'handle_openflow' is NULL, no OpenFlow messages will be processed and
233 * other activities that could affect the flow table (in-band processing,
234 * fail-open processing) are suppressed too. */
236 connmgr_run(struct connmgr *mgr,
237 bool (*handle_openflow)(struct ofconn *, struct ofpbuf *ofp_msg))
239 struct ofconn *ofconn, *next_ofconn;
240 struct ofservice *ofservice;
243 if (handle_openflow && mgr->in_band) {
244 if (!in_band_run(mgr->in_band)) {
245 in_band_destroy(mgr->in_band);
250 LIST_FOR_EACH_SAFE (ofconn, next_ofconn, node, &mgr->all_conns) {
251 ofconn_run(ofconn, handle_openflow);
254 /* Fail-open maintenance. Do this after processing the ofconns since
255 * fail-open checks the status of the controller rconn. */
256 if (handle_openflow && mgr->fail_open) {
257 fail_open_run(mgr->fail_open);
260 HMAP_FOR_EACH (ofservice, node, &mgr->services) {
264 retval = pvconn_accept(ofservice->pvconn, OFP_VERSION, &vconn);
269 rconn = rconn_create(ofservice->probe_interval, 0);
270 name = ofconn_make_name(mgr, vconn_get_name(vconn));
271 rconn_connect_unreliably(rconn, vconn, name);
274 ofconn = ofconn_create(mgr, rconn, OFCONN_SERVICE);
275 ofconn_set_rate_limit(ofconn, ofservice->rate_limit,
276 ofservice->burst_limit);
277 } else if (retval != EAGAIN) {
278 VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval));
282 for (i = 0; i < mgr->n_snoops; i++) {
286 retval = pvconn_accept(mgr->snoops[i], OFP_VERSION, &vconn);
288 add_snooper(mgr, vconn);
289 } else if (retval != EAGAIN) {
290 VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval));
295 /* Causes the poll loop to wake up when connmgr_run() needs to run.
297 * If 'handling_openflow' is true, arriving OpenFlow messages and other
298 * activities that affect the flow table will wake up the poll loop. If
299 * 'handling_openflow' is false, they will not. */
301 connmgr_wait(struct connmgr *mgr, bool handling_openflow)
303 struct ofservice *ofservice;
304 struct ofconn *ofconn;
307 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
308 ofconn_wait(ofconn, handling_openflow);
310 if (handling_openflow && mgr->in_band) {
311 in_band_wait(mgr->in_band);
313 if (handling_openflow && mgr->fail_open) {
314 fail_open_wait(mgr->fail_open);
316 HMAP_FOR_EACH (ofservice, node, &mgr->services) {
317 pvconn_wait(ofservice->pvconn);
319 for (i = 0; i < mgr->n_snoops; i++) {
320 pvconn_wait(mgr->snoops[i]);
324 /* Returns the ofproto that owns 'ofconn''s connmgr. */
326 ofconn_get_ofproto(const struct ofconn *ofconn)
328 return ofconn->connmgr->ofproto;
331 /* If processing of OpenFlow messages was blocked on any 'mgr' ofconns by
332 * returning false to the 'handle_openflow' callback to connmgr_run(), this
333 * re-enables them. */
335 connmgr_retry(struct connmgr *mgr)
337 struct ofconn *ofconn;
339 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
340 ofconn->retry = true;
344 /* OpenFlow configuration. */
346 static void add_controller(struct connmgr *, const char *target);
347 static struct ofconn *find_controller_by_target(struct connmgr *,
349 static void update_fail_open(struct connmgr *);
350 static int set_pvconns(struct pvconn ***pvconnsp, size_t *n_pvconnsp,
351 const struct sset *);
353 /* Returns true if 'mgr' has any configured primary controllers.
355 * Service controllers do not count, but configured primary controllers do
356 * count whether or not they are currently connected. */
358 connmgr_has_controllers(const struct connmgr *mgr)
360 return !hmap_is_empty(&mgr->controllers);
363 /* Initializes 'info' and populates it with information about each configured
364 * primary controller. The keys in 'info' are the controllers' targets; the
365 * data values are corresponding "struct ofproto_controller_info".
367 * The caller owns 'info' and everything in it and should free it when it is no
370 connmgr_get_controller_info(struct connmgr *mgr, struct shash *info)
372 const struct ofconn *ofconn;
374 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
375 const struct rconn *rconn = ofconn->rconn;
376 const char *target = rconn_get_target(rconn);
378 if (!shash_find(info, target)) {
379 struct ofproto_controller_info *cinfo = xmalloc(sizeof *cinfo);
380 time_t now = time_now();
381 time_t last_connection = rconn_get_last_connection(rconn);
382 time_t last_disconnect = rconn_get_last_disconnect(rconn);
383 int last_error = rconn_get_last_error(rconn);
385 shash_add(info, target, cinfo);
387 cinfo->is_connected = rconn_is_connected(rconn);
388 cinfo->role = ofconn->role;
393 cinfo->pairs.keys[cinfo->pairs.n] = "last_error";
394 cinfo->pairs.values[cinfo->pairs.n++]
395 = xstrdup(ovs_retval_to_string(last_error));
398 cinfo->pairs.keys[cinfo->pairs.n] = "state";
399 cinfo->pairs.values[cinfo->pairs.n++]
400 = xstrdup(rconn_get_state(rconn));
402 if (last_connection != TIME_MIN) {
403 cinfo->pairs.keys[cinfo->pairs.n] = "sec_since_connect";
404 cinfo->pairs.values[cinfo->pairs.n++]
405 = xasprintf("%ld", (long int) (now - last_connection));
408 if (last_disconnect != TIME_MIN) {
409 cinfo->pairs.keys[cinfo->pairs.n] = "sec_since_disconnect";
410 cinfo->pairs.values[cinfo->pairs.n++]
411 = xasprintf("%ld", (long int) (now - last_disconnect));
418 connmgr_free_controller_info(struct shash *info)
420 struct shash_node *node;
422 SHASH_FOR_EACH (node, info) {
423 struct ofproto_controller_info *cinfo = node->data;
424 while (cinfo->pairs.n) {
425 free((char *) cinfo->pairs.values[--cinfo->pairs.n]);
432 /* Changes 'mgr''s set of controllers to the 'n_controllers' controllers in
435 connmgr_set_controllers(struct connmgr *mgr,
436 const struct ofproto_controller *controllers,
437 size_t n_controllers)
439 bool had_controllers = connmgr_has_controllers(mgr);
440 struct shash new_controllers;
441 struct ofconn *ofconn, *next_ofconn;
442 struct ofservice *ofservice, *next_ofservice;
445 /* Create newly configured controllers and services.
446 * Create a name to ofproto_controller mapping in 'new_controllers'. */
447 shash_init(&new_controllers);
448 for (i = 0; i < n_controllers; i++) {
449 const struct ofproto_controller *c = &controllers[i];
451 if (!vconn_verify_name(c->target)) {
452 if (!find_controller_by_target(mgr, c->target)) {
453 add_controller(mgr, c->target);
455 } else if (!pvconn_verify_name(c->target)) {
456 if (!ofservice_lookup(mgr, c->target)) {
457 ofservice_create(mgr, c->target);
460 VLOG_WARN_RL(&rl, "%s: unsupported controller \"%s\"",
461 mgr->name, c->target);
465 shash_add_once(&new_controllers, c->target, &controllers[i]);
468 /* Delete controllers that are no longer configured.
469 * Update configuration of all now-existing controllers. */
470 HMAP_FOR_EACH_SAFE (ofconn, next_ofconn, hmap_node, &mgr->controllers) {
471 struct ofproto_controller *c;
473 c = shash_find_data(&new_controllers, ofconn_get_target(ofconn));
475 ofconn_destroy(ofconn);
477 ofconn_reconfigure(ofconn, c);
481 /* Delete services that are no longer configured.
482 * Update configuration of all now-existing services. */
483 HMAP_FOR_EACH_SAFE (ofservice, next_ofservice, node, &mgr->services) {
484 struct ofproto_controller *c;
486 c = shash_find_data(&new_controllers,
487 pvconn_get_name(ofservice->pvconn));
489 ofservice_destroy(mgr, ofservice);
491 ofservice_reconfigure(ofservice, c);
495 shash_destroy(&new_controllers);
497 update_in_band_remotes(mgr);
498 update_fail_open(mgr);
499 if (had_controllers != connmgr_has_controllers(mgr)) {
500 ofproto_flush_flows(mgr->ofproto);
504 /* Drops the connections between 'mgr' and all of its primary and secondary
505 * controllers, forcing them to reconnect. */
507 connmgr_reconnect(const struct connmgr *mgr)
509 struct ofconn *ofconn;
511 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
512 rconn_reconnect(ofconn->rconn);
516 /* Sets the "snoops" for 'mgr' to the pvconn targets listed in 'snoops'.
518 * A "snoop" is a pvconn to which every OpenFlow message to or from the most
519 * important controller on 'mgr' is mirrored. */
521 connmgr_set_snoops(struct connmgr *mgr, const struct sset *snoops)
523 return set_pvconns(&mgr->snoops, &mgr->n_snoops, snoops);
526 /* Adds each of the snoops currently configured on 'mgr' to 'snoops'. */
528 connmgr_get_snoops(const struct connmgr *mgr, struct sset *snoops)
532 for (i = 0; i < mgr->n_snoops; i++) {
533 sset_add(snoops, pvconn_get_name(mgr->snoops[i]));
537 /* Returns true if 'mgr' has at least one snoop, false if it has none. */
539 connmgr_has_snoops(const struct connmgr *mgr)
541 return mgr->n_snoops > 0;
544 /* Creates a new controller for 'target' in 'mgr'. update_controller() needs
545 * to be called later to finish the new ofconn's configuration. */
547 add_controller(struct connmgr *mgr, const char *target)
549 char *name = ofconn_make_name(mgr, target);
550 struct ofconn *ofconn;
552 ofconn = ofconn_create(mgr, rconn_create(5, 8), OFCONN_PRIMARY);
553 ofconn->pktbuf = pktbuf_create();
554 ofconn->miss_send_len = OFP_DEFAULT_MISS_SEND_LEN;
555 rconn_connect(ofconn->rconn, target, name);
556 hmap_insert(&mgr->controllers, &ofconn->hmap_node, hash_string(target, 0));
561 static struct ofconn *
562 find_controller_by_target(struct connmgr *mgr, const char *target)
564 struct ofconn *ofconn;
566 HMAP_FOR_EACH_WITH_HASH (ofconn, hmap_node,
567 hash_string(target, 0), &mgr->controllers) {
568 if (!strcmp(ofconn_get_target(ofconn), target)) {
576 update_in_band_remotes(struct connmgr *mgr)
578 struct sockaddr_in *addrs;
579 size_t max_addrs, n_addrs;
580 struct ofconn *ofconn;
583 /* Allocate enough memory for as many remotes as we could possibly have. */
584 max_addrs = mgr->n_extra_remotes + hmap_count(&mgr->controllers);
585 addrs = xmalloc(max_addrs * sizeof *addrs);
588 /* Add all the remotes. */
589 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
590 struct sockaddr_in *sin = &addrs[n_addrs];
591 const char *target = rconn_get_target(ofconn->rconn);
593 if (ofconn->band == OFPROTO_OUT_OF_BAND) {
597 if (stream_parse_target_with_default_ports(target,
604 for (i = 0; i < mgr->n_extra_remotes; i++) {
605 addrs[n_addrs++] = mgr->extra_in_band_remotes[i];
608 /* Create or update or destroy in-band. */
611 in_band_create(mgr->ofproto, mgr->local_port_name, &mgr->in_band);
613 in_band_set_queue(mgr->in_band, mgr->in_band_queue);
615 /* in_band_run() needs a chance to delete any existing in-band flows.
616 * We will destroy mgr->in_band after it's done with that. */
619 in_band_set_remotes(mgr->in_band, addrs, n_addrs);
627 update_fail_open(struct connmgr *mgr)
629 if (connmgr_has_controllers(mgr)
630 && mgr->fail_mode == OFPROTO_FAIL_STANDALONE) {
631 if (!mgr->fail_open) {
632 mgr->fail_open = fail_open_create(mgr->ofproto, mgr);
635 fail_open_destroy(mgr->fail_open);
636 mgr->fail_open = NULL;
641 set_pvconns(struct pvconn ***pvconnsp, size_t *n_pvconnsp,
642 const struct sset *sset)
644 struct pvconn **pvconns = *pvconnsp;
645 size_t n_pvconns = *n_pvconnsp;
650 for (i = 0; i < n_pvconns; i++) {
651 pvconn_close(pvconns[i]);
655 pvconns = xmalloc(sset_count(sset) * sizeof *pvconns);
657 SSET_FOR_EACH (name, sset) {
658 struct pvconn *pvconn;
661 error = pvconn_open(name, &pvconn);
663 pvconns[n_pvconns++] = pvconn;
665 VLOG_ERR("failed to listen on %s: %s", name, strerror(error));
673 *n_pvconnsp = n_pvconns;
678 /* Returns a "preference level" for snooping 'ofconn'. A higher return value
679 * means that 'ofconn' is more interesting for monitoring than a lower return
682 snoop_preference(const struct ofconn *ofconn)
684 switch (ofconn->role) {
692 /* Shouldn't happen. */
697 /* One of 'mgr''s "snoop" pvconns has accepted a new connection on 'vconn'.
698 * Connects this vconn to a controller. */
700 add_snooper(struct connmgr *mgr, struct vconn *vconn)
702 struct ofconn *ofconn, *best;
704 /* Pick a controller for monitoring. */
706 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
707 if (ofconn->type == OFCONN_PRIMARY
708 && (!best || snoop_preference(ofconn) > snoop_preference(best))) {
714 rconn_add_monitor(best->rconn, vconn);
716 VLOG_INFO_RL(&rl, "no controller connection to snoop");
721 /* Public ofconn functions. */
723 /* Returns the connection type, either OFCONN_PRIMARY or OFCONN_SERVICE. */
725 ofconn_get_type(const struct ofconn *ofconn)
730 /* Returns the role configured for 'ofconn'.
732 * The default role, if no other role has been set, is NX_ROLE_OTHER. */
734 ofconn_get_role(const struct ofconn *ofconn)
739 /* Changes 'ofconn''s role to 'role'. If 'role' is NX_ROLE_MASTER then any
740 * existing master is demoted to a slave. */
742 ofconn_set_role(struct ofconn *ofconn, enum nx_role role)
744 if (role == NX_ROLE_MASTER) {
745 struct ofconn *other;
747 HMAP_FOR_EACH (other, hmap_node, &ofconn->connmgr->controllers) {
748 if (other->role == NX_ROLE_MASTER) {
749 other->role = NX_ROLE_SLAVE;
756 /* Returns the currently configured flow format for 'ofconn', one of NXFF_*.
758 * The default, if no other format has been set, is NXFF_OPENFLOW10. */
760 ofconn_get_flow_format(struct ofconn *ofconn)
762 return ofconn->flow_format;
765 /* Sets the flow format for 'ofconn' to 'flow_format' (one of NXFF_*). */
767 ofconn_set_flow_format(struct ofconn *ofconn, enum nx_flow_format flow_format)
769 ofconn->flow_format = flow_format;
772 /* Returns true if the NXT_FLOW_MOD_TABLE_ID extension is enabled, false
775 * By default the extension is not enabled. */
777 ofconn_get_flow_mod_table_id(const struct ofconn *ofconn)
779 return ofconn->flow_mod_table_id;
782 /* Enables or disables (according to 'enable') the NXT_FLOW_MOD_TABLE_ID
783 * extension on 'ofconn'. */
785 ofconn_set_flow_mod_table_id(struct ofconn *ofconn, bool enable)
787 ofconn->flow_mod_table_id = enable;
790 /* Returns the default miss send length for 'ofconn'. */
792 ofconn_get_miss_send_len(const struct ofconn *ofconn)
794 return ofconn->miss_send_len;
797 /* Sets the default miss send length for 'ofconn' to 'miss_send_len'. */
799 ofconn_set_miss_send_len(struct ofconn *ofconn, int miss_send_len)
801 ofconn->miss_send_len = miss_send_len;
804 /* Sends 'msg' on 'ofconn', accounting it as a reply. (If there is a
805 * sufficient number of OpenFlow replies in-flight on a single ofconn, then the
806 * connmgr will stop accepting new OpenFlow requests on that ofconn until the
807 * controller has accepted some of the replies.) */
809 ofconn_send_reply(const struct ofconn *ofconn, struct ofpbuf *msg)
811 ofconn_send(ofconn, msg, ofconn->reply_counter);
814 /* Sends each of the messages in list 'replies' on 'ofconn' in order,
815 * accounting them as replies. */
817 ofconn_send_replies(const struct ofconn *ofconn, struct list *replies)
819 struct ofpbuf *reply, *next;
821 LIST_FOR_EACH_SAFE (reply, next, list_node, replies) {
822 list_remove(&reply->list_node);
823 ofconn_send_reply(ofconn, reply);
827 /* Sends 'error', which should be an OpenFlow error created with
828 * e.g. ofp_mkerr(), on 'ofconn', as a reply to 'request'. Only at most the
829 * first 64 bytes of 'request' are used. */
831 ofconn_send_error(const struct ofconn *ofconn,
832 const struct ofp_header *request, int error)
836 msg = ofputil_encode_error_msg(error, request);
838 static struct vlog_rate_limit err_rl = VLOG_RATE_LIMIT_INIT(10, 10);
840 if (!VLOG_DROP_INFO(&err_rl)) {
841 const struct ofputil_msg_type *type;
842 const char *type_name;
846 request_len = ntohs(request->length);
847 type_name = (!ofputil_decode_msg_type_partial(request,
848 MIN(64, request_len),
850 ? ofputil_msg_type_name(type)
853 error_s = ofputil_error_to_string(error);
854 VLOG_INFO("%s: sending %s error reply to %s message",
855 rconn_get_name(ofconn->rconn), error_s, type_name);
858 ofconn_send_reply(ofconn, msg);
862 /* Same as pktbuf_retrieve(), using the pktbuf owned by 'ofconn'. */
864 ofconn_pktbuf_retrieve(struct ofconn *ofconn, uint32_t id,
865 struct ofpbuf **bufferp, uint16_t *in_port)
867 return pktbuf_retrieve(ofconn->pktbuf, id, bufferp, in_port);
870 /* Returns true if 'ofconn' has any pending opgroups. */
872 ofconn_has_pending_opgroups(const struct ofconn *ofconn)
874 return !list_is_empty(&ofconn->opgroups);
877 /* Adds 'ofconn_node' to 'ofconn''s list of pending opgroups.
879 * If 'ofconn' is destroyed or its connection drops, then 'ofconn' will remove
880 * 'ofconn_node' from the list and re-initialize it with list_init(). The
881 * client may, therefore, use list_is_empty(ofconn_node) to determine whether
882 * 'ofconn_node' is still associated with an active ofconn.
884 * The client may also remove ofconn_node from the list itself, with
887 ofconn_add_opgroup(struct ofconn *ofconn, struct list *ofconn_node)
889 list_push_back(&ofconn->opgroups, ofconn_node);
892 /* Private ofconn functions. */
895 ofconn_get_target(const struct ofconn *ofconn)
897 return rconn_get_target(ofconn->rconn);
900 static struct ofconn *
901 ofconn_create(struct connmgr *mgr, struct rconn *rconn, enum ofconn_type type)
903 struct ofconn *ofconn = xzalloc(sizeof *ofconn);
904 ofconn->connmgr = mgr;
905 list_push_back(&mgr->all_conns, &ofconn->node);
906 ofconn->rconn = rconn;
908 ofconn->flow_format = NXFF_OPENFLOW10;
909 ofconn->flow_mod_table_id = false;
910 list_init(&ofconn->opgroups);
911 ofconn->role = NX_ROLE_OTHER;
912 ofconn->packet_in_counter = rconn_packet_counter_create ();
913 ofconn->pktbuf = NULL;
914 ofconn->miss_send_len = 0;
915 ofconn->reply_counter = rconn_packet_counter_create ();
919 /* Disassociates 'ofconn' from all of the ofopgroups that it initiated that
920 * have not yet completed. (Those ofopgroups will still run to completion in
921 * the usual way, but any errors that they run into will not be reported on any
924 * Also discards any blocked operation on 'ofconn'. */
926 ofconn_flush(struct ofconn *ofconn)
928 while (!list_is_empty(&ofconn->opgroups)) {
929 list_init(list_pop_front(&ofconn->opgroups));
931 ofpbuf_delete(ofconn->blocked);
932 ofconn->blocked = NULL;
936 ofconn_destroy(struct ofconn *ofconn)
938 ofconn_flush(ofconn);
940 if (ofconn->type == OFCONN_PRIMARY) {
941 hmap_remove(&ofconn->connmgr->controllers, &ofconn->hmap_node);
944 list_remove(&ofconn->node);
945 rconn_destroy(ofconn->rconn);
946 rconn_packet_counter_destroy(ofconn->packet_in_counter);
947 rconn_packet_counter_destroy(ofconn->reply_counter);
948 pktbuf_destroy(ofconn->pktbuf);
952 /* Reconfigures 'ofconn' to match 'c'. 'ofconn' and 'c' must have the same
955 ofconn_reconfigure(struct ofconn *ofconn, const struct ofproto_controller *c)
959 ofconn->band = c->band;
961 rconn_set_max_backoff(ofconn->rconn, c->max_backoff);
963 probe_interval = c->probe_interval ? MAX(c->probe_interval, 5) : 0;
964 rconn_set_probe_interval(ofconn->rconn, probe_interval);
966 ofconn_set_rate_limit(ofconn, c->rate_limit, c->burst_limit);
969 /* Returns true if it makes sense for 'ofconn' to receive and process OpenFlow
972 ofconn_may_recv(const struct ofconn *ofconn)
974 int count = rconn_packet_counter_read (ofconn->reply_counter);
975 return (!ofconn->blocked || ofconn->retry) && count < OFCONN_REPLY_MAX;
979 ofconn_run(struct ofconn *ofconn,
980 bool (*handle_openflow)(struct ofconn *, struct ofpbuf *ofp_msg))
982 struct connmgr *mgr = ofconn->connmgr;
985 for (i = 0; i < N_SCHEDULERS; i++) {
986 pinsched_run(ofconn->schedulers[i], do_send_packet_in, ofconn);
989 rconn_run(ofconn->rconn);
991 if (handle_openflow) {
992 /* Limit the number of iterations to avoid starving other tasks. */
993 for (i = 0; i < 50 && ofconn_may_recv(ofconn); i++) {
994 struct ofpbuf *of_msg;
996 of_msg = (ofconn->blocked
998 : rconn_recv(ofconn->rconn));
1002 if (mgr->fail_open) {
1003 fail_open_maybe_recover(mgr->fail_open);
1006 if (handle_openflow(ofconn, of_msg)) {
1007 ofpbuf_delete(of_msg);
1008 ofconn->blocked = NULL;
1010 ofconn->blocked = of_msg;
1011 ofconn->retry = false;
1016 if (!rconn_is_alive(ofconn->rconn)) {
1017 ofconn_destroy(ofconn);
1018 } else if (!rconn_is_connected(ofconn->rconn)) {
1019 ofconn_flush(ofconn);
1024 ofconn_wait(struct ofconn *ofconn, bool handling_openflow)
1028 for (i = 0; i < N_SCHEDULERS; i++) {
1029 pinsched_wait(ofconn->schedulers[i]);
1031 rconn_run_wait(ofconn->rconn);
1032 if (handling_openflow && ofconn_may_recv(ofconn)) {
1033 rconn_recv_wait(ofconn->rconn);
1037 /* Returns true if 'ofconn' should receive asynchronous messages. */
1039 ofconn_receives_async_msgs(const struct ofconn *ofconn)
1041 if (!rconn_is_connected(ofconn->rconn)) {
1043 } else if (ofconn->type == OFCONN_PRIMARY) {
1044 /* Primary controllers always get asynchronous messages unless they
1045 * have configured themselves as "slaves". */
1046 return ofconn->role != NX_ROLE_SLAVE;
1048 /* Service connections don't get asynchronous messages unless they have
1049 * explicitly asked for them by setting a nonzero miss send length. */
1050 return ofconn->miss_send_len > 0;
1054 /* Returns a human-readable name for an OpenFlow connection between 'mgr' and
1055 * 'target', suitable for use in log messages for identifying the connection.
1057 * The name is dynamically allocated. The caller should free it (with free())
1058 * when it is no longer needed. */
1060 ofconn_make_name(const struct connmgr *mgr, const char *target)
1062 return xasprintf("%s<->%s", mgr->name, target);
1066 ofconn_set_rate_limit(struct ofconn *ofconn, int rate, int burst)
1070 for (i = 0; i < N_SCHEDULERS; i++) {
1071 struct pinsched **s = &ofconn->schedulers[i];
1075 *s = pinsched_create(rate, burst);
1077 pinsched_set_limits(*s, rate, burst);
1080 pinsched_destroy(*s);
1087 ofconn_send(const struct ofconn *ofconn, struct ofpbuf *msg,
1088 struct rconn_packet_counter *counter)
1090 update_openflow_length(msg);
1091 if (rconn_send(ofconn->rconn, msg, counter)) {
1096 /* Sending asynchronous messages. */
1098 static void schedule_packet_in(struct ofconn *, struct ofputil_packet_in,
1099 const struct flow *);
1101 /* Sends an OFPT_PORT_STATUS message with 'opp' and 'reason' to appropriate
1102 * controllers managed by 'mgr'. */
1104 connmgr_send_port_status(struct connmgr *mgr, const struct ofp_phy_port *opp,
1107 /* XXX Should limit the number of queued port status change messages. */
1108 struct ofconn *ofconn;
1110 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1111 struct ofp_port_status *ops;
1114 /* Primary controllers, even slaves, should always get port status
1115 updates. Otherwise obey ofconn_receives_async_msgs(). */
1116 if (ofconn->type != OFCONN_PRIMARY
1117 && !ofconn_receives_async_msgs(ofconn)) {
1121 ops = make_openflow_xid(sizeof *ops, OFPT_PORT_STATUS, 0, &b);
1122 ops->reason = reason;
1124 ofconn_send(ofconn, b, NULL);
1128 /* Sends an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message based on 'fr' to
1129 * appropriate controllers managed by 'mgr'. */
1131 connmgr_send_flow_removed(struct connmgr *mgr,
1132 const struct ofputil_flow_removed *fr)
1134 struct ofconn *ofconn;
1136 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1139 if (!ofconn_receives_async_msgs(ofconn)) {
1143 /* Account flow expirations as replies to OpenFlow requests. That
1144 * works because preventing OpenFlow requests from being processed also
1145 * prevents new flows from being added (and expiring). (It also
1146 * prevents processing OpenFlow requests that would not add new flows,
1147 * so it is imperfect.) */
1148 msg = ofputil_encode_flow_removed(fr, ofconn->flow_format);
1149 ofconn_send_reply(ofconn, msg);
1153 /* Given 'pin', sends an OFPT_PACKET_IN message to each OpenFlow controller as
1154 * necessary according to their individual configurations. */
1156 connmgr_send_packet_in(struct connmgr *mgr,
1157 const struct ofputil_packet_in *pin,
1158 const struct flow *flow)
1160 struct ofconn *ofconn;
1162 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1163 if (ofconn_receives_async_msgs(ofconn)) {
1164 schedule_packet_in(ofconn, *pin, flow);
1169 /* pinsched callback for sending 'ofp_packet_in' on 'ofconn'. */
1171 do_send_packet_in(struct ofpbuf *ofp_packet_in, void *ofconn_)
1173 struct ofconn *ofconn = ofconn_;
1175 rconn_send_with_limit(ofconn->rconn, ofp_packet_in,
1176 ofconn->packet_in_counter, 100);
1179 /* Takes 'pin', whose packet has the flow specified by 'flow', composes an
1180 * OpenFlow packet-in message from it, and passes it to 'ofconn''s packet
1181 * scheduler for sending. */
1183 schedule_packet_in(struct ofconn *ofconn, struct ofputil_packet_in pin,
1184 const struct flow *flow)
1186 struct connmgr *mgr = ofconn->connmgr;
1188 /* Get OpenFlow buffer_id. */
1189 if (pin.reason == OFPR_ACTION) {
1190 pin.buffer_id = UINT32_MAX;
1191 } else if (mgr->fail_open && fail_open_is_active(mgr->fail_open)) {
1192 pin.buffer_id = pktbuf_get_null();
1193 } else if (!ofconn->pktbuf) {
1194 pin.buffer_id = UINT32_MAX;
1196 pin.buffer_id = pktbuf_save(ofconn->pktbuf, pin.packet, pin.packet_len,
1200 /* Figure out how much of the packet to send. */
1201 if (pin.reason == OFPR_NO_MATCH) {
1202 pin.send_len = pin.packet_len;
1204 /* Caller should have initialized 'send_len' to 'max_len' specified in
1205 * struct ofp_action_output. */
1207 if (pin.buffer_id != UINT32_MAX) {
1208 pin.send_len = MIN(pin.send_len, ofconn->miss_send_len);
1211 /* Make OFPT_PACKET_IN and hand over to packet scheduler. It might
1212 * immediately call into do_send_packet_in() or it might buffer it for a
1213 * while (until a later call to pinsched_run()). */
1214 pinsched_send(ofconn->schedulers[pin.reason == OFPR_NO_MATCH ? 0 : 1],
1215 flow->in_port, ofputil_encode_packet_in(&pin),
1216 do_send_packet_in, ofconn);
1219 /* Fail-open settings. */
1221 /* Returns the failure handling mode (OFPROTO_FAIL_SECURE or
1222 * OFPROTO_FAIL_STANDALONE) for 'mgr'. */
1223 enum ofproto_fail_mode
1224 connmgr_get_fail_mode(const struct connmgr *mgr)
1226 return mgr->fail_mode;
1229 /* Sets the failure handling mode for 'mgr' to 'fail_mode' (either
1230 * OFPROTO_FAIL_SECURE or OFPROTO_FAIL_STANDALONE). */
1232 connmgr_set_fail_mode(struct connmgr *mgr, enum ofproto_fail_mode fail_mode)
1234 if (mgr->fail_mode != fail_mode) {
1235 mgr->fail_mode = fail_mode;
1236 update_fail_open(mgr);
1237 if (!connmgr_has_controllers(mgr)) {
1238 ofproto_flush_flows(mgr->ofproto);
1243 /* Fail-open implementation. */
1245 /* Returns the longest probe interval among the primary controllers configured
1246 * on 'mgr'. Returns 0 if there are no primary controllers. */
1248 connmgr_get_max_probe_interval(const struct connmgr *mgr)
1250 const struct ofconn *ofconn;
1251 int max_probe_interval;
1253 max_probe_interval = 0;
1254 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1255 int probe_interval = rconn_get_probe_interval(ofconn->rconn);
1256 max_probe_interval = MAX(max_probe_interval, probe_interval);
1258 return max_probe_interval;
1261 /* Returns the number of seconds for which all of 'mgr's primary controllers
1262 * have been disconnected. Returns 0 if 'mgr' has no primary controllers. */
1264 connmgr_failure_duration(const struct connmgr *mgr)
1266 const struct ofconn *ofconn;
1267 int min_failure_duration;
1269 if (!connmgr_has_controllers(mgr)) {
1273 min_failure_duration = INT_MAX;
1274 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1275 int failure_duration = rconn_failure_duration(ofconn->rconn);
1276 min_failure_duration = MIN(min_failure_duration, failure_duration);
1278 return min_failure_duration;
1281 /* Returns true if at least one primary controller is connected (regardless of
1282 * whether those controllers are believed to have authenticated and accepted
1283 * this switch), false if none of them are connected. */
1285 connmgr_is_any_controller_connected(const struct connmgr *mgr)
1287 const struct ofconn *ofconn;
1289 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1290 if (rconn_is_connected(ofconn->rconn)) {
1297 /* Returns true if at least one primary controller is believed to have
1298 * authenticated and accepted this switch, false otherwise. */
1300 connmgr_is_any_controller_admitted(const struct connmgr *mgr)
1302 const struct ofconn *ofconn;
1304 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1305 if (rconn_is_admitted(ofconn->rconn)) {
1312 /* Sends 'packet' to each controller connected to 'mgr'. Takes ownership of
1315 connmgr_broadcast(struct connmgr *mgr, struct ofpbuf *packet)
1317 struct ofconn *ofconn, *prev;
1320 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1322 ofconn_send_reply(ofconn, ofpbuf_clone(packet));
1324 if (rconn_is_connected(ofconn->rconn)) {
1329 ofconn_send_reply(prev, packet);
1331 ofpbuf_delete(packet);
1335 /* In-band configuration. */
1337 static bool any_extras_changed(const struct connmgr *,
1338 const struct sockaddr_in *extras, size_t n);
1340 /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'mgr''s
1341 * in-band control should guarantee access, in the same way that in-band
1342 * control guarantees access to OpenFlow controllers. */
1344 connmgr_set_extra_in_band_remotes(struct connmgr *mgr,
1345 const struct sockaddr_in *extras, size_t n)
1347 if (!any_extras_changed(mgr, extras, n)) {
1351 free(mgr->extra_in_band_remotes);
1352 mgr->n_extra_remotes = n;
1353 mgr->extra_in_band_remotes = xmemdup(extras, n * sizeof *extras);
1355 update_in_band_remotes(mgr);
1358 /* Sets the OpenFlow queue used by flows set up by in-band control on
1359 * 'mgr' to 'queue_id'. If 'queue_id' is negative, then in-band control
1360 * flows will use the default queue. */
1362 connmgr_set_in_band_queue(struct connmgr *mgr, int queue_id)
1364 if (queue_id != mgr->in_band_queue) {
1365 mgr->in_band_queue = queue_id;
1366 update_in_band_remotes(mgr);
1371 any_extras_changed(const struct connmgr *mgr,
1372 const struct sockaddr_in *extras, size_t n)
1376 if (n != mgr->n_extra_remotes) {
1380 for (i = 0; i < n; i++) {
1381 const struct sockaddr_in *old = &mgr->extra_in_band_remotes[i];
1382 const struct sockaddr_in *new = &extras[i];
1384 if (old->sin_addr.s_addr != new->sin_addr.s_addr ||
1385 old->sin_port != new->sin_port) {
1393 /* In-band implementation. */
1396 connmgr_msg_in_hook(struct connmgr *mgr, const struct flow *flow,
1397 const struct ofpbuf *packet)
1399 return mgr->in_band && in_band_msg_in_hook(mgr->in_band, flow, packet);
1403 connmgr_may_set_up_flow(struct connmgr *mgr, const struct flow *flow,
1404 const struct nlattr *odp_actions,
1407 return !mgr->in_band || in_band_rule_check(flow, odp_actions, actions_len);
1410 /* Fail-open and in-band implementation. */
1412 /* Called by 'ofproto' after all flows have been flushed, to allow fail-open
1413 * and standalone mode to re-create their flows.
1415 * In-band control has more sophisticated code that manages flows itself. */
1417 connmgr_flushed(struct connmgr *mgr)
1419 if (mgr->fail_open) {
1420 fail_open_flushed(mgr->fail_open);
1423 /* If there are no controllers and we're in standalone mode, set up a flow
1424 * that matches every packet and directs them to OFPP_NORMAL (which goes to
1425 * us). Otherwise, the switch is in secure mode and we won't pass any
1426 * traffic until a controller has been defined and it tells us to do so. */
1427 if (!connmgr_has_controllers(mgr)
1428 && mgr->fail_mode == OFPROTO_FAIL_STANDALONE) {
1429 union ofp_action action;
1430 struct cls_rule rule;
1432 memset(&action, 0, sizeof action);
1433 action.type = htons(OFPAT_OUTPUT);
1434 action.output.len = htons(sizeof action);
1435 action.output.port = htons(OFPP_NORMAL);
1436 cls_rule_init_catchall(&rule, 0);
1437 ofproto_add_flow(mgr->ofproto, &rule, &action, 1);
1441 /* Creates a new ofservice for 'target' in 'mgr'. Returns 0 if successful,
1442 * otherwise a positive errno value.
1444 * ofservice_reconfigure() must be called to fully configure the new
1447 ofservice_create(struct connmgr *mgr, const char *target)
1449 struct ofservice *ofservice;
1450 struct pvconn *pvconn;
1453 error = pvconn_open(target, &pvconn);
1458 ofservice = xzalloc(sizeof *ofservice);
1459 hmap_insert(&mgr->services, &ofservice->node, hash_string(target, 0));
1460 ofservice->pvconn = pvconn;
1466 ofservice_destroy(struct connmgr *mgr, struct ofservice *ofservice)
1468 hmap_remove(&mgr->services, &ofservice->node);
1469 pvconn_close(ofservice->pvconn);
1474 ofservice_reconfigure(struct ofservice *ofservice,
1475 const struct ofproto_controller *c)
1477 ofservice->probe_interval = c->probe_interval;
1478 ofservice->rate_limit = c->rate_limit;
1479 ofservice->burst_limit = c->burst_limit;
1482 /* Finds and returns the ofservice within 'mgr' that has the given
1483 * 'target', or a null pointer if none exists. */
1484 static struct ofservice *
1485 ofservice_lookup(struct connmgr *mgr, const char *target)
1487 struct ofservice *ofservice;
1489 HMAP_FOR_EACH_WITH_HASH (ofservice, node, hash_string(target, 0),
1491 if (!strcmp(pvconn_get_name(ofservice->pvconn), target)) {