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"
40 VLOG_DEFINE_THIS_MODULE(connmgr);
41 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
43 /* An OpenFlow connection. */
45 struct connmgr *connmgr; /* Connection's manager. */
46 struct list node; /* In struct connmgr's "all_conns" list. */
47 struct rconn *rconn; /* OpenFlow connection. */
48 enum ofconn_type type; /* Type. */
49 enum nx_flow_format flow_format; /* Currently selected flow format. */
50 bool flow_mod_table_id; /* NXT_FLOW_MOD_TABLE_ID enabled? */
52 /* Asynchronous flow table operation support. */
53 struct list opgroups; /* Contains pending "ofopgroups", if any. */
54 struct ofpbuf *blocked; /* Postponed OpenFlow message, if any. */
55 bool retry; /* True if 'blocked' is ready to try again. */
57 /* OFPT_PACKET_IN related data. */
58 struct rconn_packet_counter *packet_in_counter; /* # queued on 'rconn'. */
59 #define N_SCHEDULERS 2
60 struct pinsched *schedulers[N_SCHEDULERS];
61 struct pktbuf *pktbuf; /* OpenFlow packet buffers. */
62 int miss_send_len; /* Bytes to send of buffered packets. */
64 /* Number of OpenFlow messages queued on 'rconn' as replies to OpenFlow
65 * requests, and the maximum number before we stop reading OpenFlow
67 #define OFCONN_REPLY_MAX 100
68 struct rconn_packet_counter *reply_counter;
70 /* type == OFCONN_PRIMARY only. */
71 enum nx_role role; /* Role. */
72 struct hmap_node hmap_node; /* In struct connmgr's "controllers" map. */
73 enum ofproto_band band; /* In-band or out-of-band? */
76 static struct ofconn *ofconn_create(struct connmgr *, struct rconn *,
78 static void ofconn_destroy(struct ofconn *);
80 static void ofconn_reconfigure(struct ofconn *,
81 const struct ofproto_controller *);
83 static void ofconn_run(struct ofconn *,
84 bool (*handle_openflow)(struct ofconn *,
85 struct ofpbuf *ofp_msg));
86 static void ofconn_wait(struct ofconn *, bool handling_openflow);
88 static const char *ofconn_get_target(const struct ofconn *);
89 static char *ofconn_make_name(const struct connmgr *, const char *target);
91 static void ofconn_set_rate_limit(struct ofconn *, int rate, int burst);
93 static bool ofconn_receives_async_msgs(const struct ofconn *);
95 static void ofconn_send(const struct ofconn *, struct ofpbuf *,
96 struct rconn_packet_counter *);
98 static void do_send_packet_in(struct ofpbuf *, void *ofconn_);
100 /* A listener for incoming OpenFlow "service" connections. */
102 struct hmap_node node; /* In struct connmgr's "services" hmap. */
103 struct pvconn *pvconn; /* OpenFlow connection listener. */
105 /* These are not used by ofservice directly. They are settings for
106 * accepted "struct ofconn"s from the pvconn. */
107 int probe_interval; /* Max idle time before probing, in seconds. */
108 int rate_limit; /* Max packet-in rate in packets per second. */
109 int burst_limit; /* Limit on accumulating packet credits. */
112 static void ofservice_reconfigure(struct ofservice *,
113 const struct ofproto_controller *);
114 static int ofservice_create(struct connmgr *, const char *target);
115 static void ofservice_destroy(struct connmgr *, struct ofservice *);
116 static struct ofservice *ofservice_lookup(struct connmgr *,
119 /* Connection manager for an OpenFlow switch. */
121 struct ofproto *ofproto;
123 char *local_port_name;
125 /* OpenFlow connections. */
126 struct hmap controllers; /* Controller "struct ofconn"s. */
127 struct list all_conns; /* Contains "struct ofconn"s. */
129 /* OpenFlow listeners. */
130 struct hmap services; /* Contains "struct ofservice"s. */
131 struct pvconn **snoops;
135 struct fail_open *fail_open;
136 enum ofproto_fail_mode fail_mode;
138 /* In-band control. */
139 struct in_band *in_band;
140 struct sockaddr_in *extra_in_band_remotes;
141 size_t n_extra_remotes;
145 static void update_in_band_remotes(struct connmgr *);
146 static void add_snooper(struct connmgr *, struct vconn *);
148 /* Creates and returns a new connection manager owned by 'ofproto'. 'name' is
149 * a name for the ofproto suitable for using in log messages.
150 * 'local_port_name' is the name of the local port (OFPP_LOCAL) within
153 connmgr_create(struct ofproto *ofproto,
154 const char *name, const char *local_port_name)
158 mgr = xmalloc(sizeof *mgr);
159 mgr->ofproto = ofproto;
160 mgr->name = xstrdup(name);
161 mgr->local_port_name = xstrdup(local_port_name);
163 hmap_init(&mgr->controllers);
164 list_init(&mgr->all_conns);
166 hmap_init(&mgr->services);
170 mgr->fail_open = NULL;
171 mgr->fail_mode = OFPROTO_FAIL_SECURE;
174 mgr->extra_in_band_remotes = NULL;
175 mgr->n_extra_remotes = 0;
176 mgr->in_band_queue = -1;
181 /* Frees 'mgr' and all of its resources. */
183 connmgr_destroy(struct connmgr *mgr)
185 struct ofservice *ofservice, *next_ofservice;
186 struct ofconn *ofconn, *next_ofconn;
193 LIST_FOR_EACH_SAFE (ofconn, next_ofconn, node, &mgr->all_conns) {
194 ofconn_destroy(ofconn);
196 hmap_destroy(&mgr->controllers);
198 HMAP_FOR_EACH_SAFE (ofservice, next_ofservice, node, &mgr->services) {
199 ofservice_destroy(mgr, ofservice);
201 hmap_destroy(&mgr->services);
203 for (i = 0; i < mgr->n_snoops; i++) {
204 pvconn_close(mgr->snoops[i]);
208 fail_open_destroy(mgr->fail_open);
209 mgr->fail_open = NULL;
211 in_band_destroy(mgr->in_band);
213 free(mgr->extra_in_band_remotes);
215 free(mgr->local_port_name);
220 /* Does all of the periodic maintenance required by 'mgr'.
222 * If 'handle_openflow' is nonnull, calls 'handle_openflow' for each message
223 * received on an OpenFlow connection, passing along the OpenFlow connection
224 * itself and the message that was sent. If 'handle_openflow' returns true,
225 * the message is considered to be fully processed. If 'handle_openflow'
226 * returns false, the message is considered not to have been processed at all;
227 * it will be stored and re-presented to 'handle_openflow' following the next
228 * call to connmgr_retry(). 'handle_openflow' must not modify or free the
231 * If 'handle_openflow' is NULL, no OpenFlow messages will be processed and
232 * other activities that could affect the flow table (in-band processing,
233 * fail-open processing) are suppressed too. */
235 connmgr_run(struct connmgr *mgr,
236 bool (*handle_openflow)(struct ofconn *, struct ofpbuf *ofp_msg))
238 struct ofconn *ofconn, *next_ofconn;
239 struct ofservice *ofservice;
242 if (handle_openflow && mgr->in_band) {
243 if (!in_band_run(mgr->in_band)) {
244 in_band_destroy(mgr->in_band);
249 LIST_FOR_EACH_SAFE (ofconn, next_ofconn, node, &mgr->all_conns) {
250 ofconn_run(ofconn, handle_openflow);
253 /* Fail-open maintenance. Do this after processing the ofconns since
254 * fail-open checks the status of the controller rconn. */
255 if (handle_openflow && mgr->fail_open) {
256 fail_open_run(mgr->fail_open);
259 HMAP_FOR_EACH (ofservice, node, &mgr->services) {
263 retval = pvconn_accept(ofservice->pvconn, OFP_VERSION, &vconn);
268 rconn = rconn_create(ofservice->probe_interval, 0);
269 name = ofconn_make_name(mgr, vconn_get_name(vconn));
270 rconn_connect_unreliably(rconn, vconn, name);
273 ofconn = ofconn_create(mgr, rconn, OFCONN_SERVICE);
274 ofconn_set_rate_limit(ofconn, ofservice->rate_limit,
275 ofservice->burst_limit);
276 } else if (retval != EAGAIN) {
277 VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval));
281 for (i = 0; i < mgr->n_snoops; i++) {
285 retval = pvconn_accept(mgr->snoops[i], OFP_VERSION, &vconn);
287 add_snooper(mgr, vconn);
288 } else if (retval != EAGAIN) {
289 VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval));
294 /* Causes the poll loop to wake up when connmgr_run() needs to run.
296 * If 'handling_openflow' is true, arriving OpenFlow messages and other
297 * activities that affect the flow table will wake up the poll loop. If
298 * 'handling_openflow' is false, they will not. */
300 connmgr_wait(struct connmgr *mgr, bool handling_openflow)
302 struct ofservice *ofservice;
303 struct ofconn *ofconn;
306 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
307 ofconn_wait(ofconn, handling_openflow);
309 if (handling_openflow && mgr->in_band) {
310 in_band_wait(mgr->in_band);
312 if (handling_openflow && mgr->fail_open) {
313 fail_open_wait(mgr->fail_open);
315 HMAP_FOR_EACH (ofservice, node, &mgr->services) {
316 pvconn_wait(ofservice->pvconn);
318 for (i = 0; i < mgr->n_snoops; i++) {
319 pvconn_wait(mgr->snoops[i]);
323 /* Returns the ofproto that owns 'ofconn''s connmgr. */
325 ofconn_get_ofproto(const struct ofconn *ofconn)
327 return ofconn->connmgr->ofproto;
330 /* If processing of OpenFlow messages was blocked on any 'mgr' ofconns by
331 * returning false to the 'handle_openflow' callback to connmgr_run(), this
332 * re-enables them. */
334 connmgr_retry(struct connmgr *mgr)
336 struct ofconn *ofconn;
338 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
339 ofconn->retry = true;
343 /* OpenFlow configuration. */
345 static void add_controller(struct connmgr *, const char *target);
346 static struct ofconn *find_controller_by_target(struct connmgr *,
348 static void update_fail_open(struct connmgr *);
349 static int set_pvconns(struct pvconn ***pvconnsp, size_t *n_pvconnsp,
350 const struct sset *);
352 /* Returns true if 'mgr' has any configured primary controllers.
354 * Service controllers do not count, but configured primary controllers do
355 * count whether or not they are currently connected. */
357 connmgr_has_controllers(const struct connmgr *mgr)
359 return !hmap_is_empty(&mgr->controllers);
362 /* Initializes 'info' and populates it with information about each configured
363 * primary controller. The keys in 'info' are the controllers' targets; the
364 * data values are corresponding "struct ofproto_controller_info".
366 * The caller owns 'info' and everything in it and should free it when it is no
369 connmgr_get_controller_info(struct connmgr *mgr, struct shash *info)
371 const struct ofconn *ofconn;
373 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
374 const struct rconn *rconn = ofconn->rconn;
375 const char *target = rconn_get_target(rconn);
377 if (!shash_find(info, target)) {
378 struct ofproto_controller_info *cinfo = xmalloc(sizeof *cinfo);
379 time_t now = time_now();
380 time_t last_connection = rconn_get_last_connection(rconn);
381 time_t last_disconnect = rconn_get_last_disconnect(rconn);
382 int last_error = rconn_get_last_error(rconn);
384 shash_add(info, target, cinfo);
386 cinfo->is_connected = rconn_is_connected(rconn);
387 cinfo->role = ofconn->role;
392 cinfo->pairs.keys[cinfo->pairs.n] = "last_error";
393 cinfo->pairs.values[cinfo->pairs.n++]
394 = xstrdup(ovs_retval_to_string(last_error));
397 cinfo->pairs.keys[cinfo->pairs.n] = "state";
398 cinfo->pairs.values[cinfo->pairs.n++]
399 = xstrdup(rconn_get_state(rconn));
401 if (last_connection != TIME_MIN) {
402 cinfo->pairs.keys[cinfo->pairs.n] = "sec_since_connect";
403 cinfo->pairs.values[cinfo->pairs.n++]
404 = xasprintf("%ld", (long int) (now - last_connection));
407 if (last_disconnect != TIME_MIN) {
408 cinfo->pairs.keys[cinfo->pairs.n] = "sec_since_disconnect";
409 cinfo->pairs.values[cinfo->pairs.n++]
410 = xasprintf("%ld", (long int) (now - last_disconnect));
417 connmgr_free_controller_info(struct shash *info)
419 struct shash_node *node;
421 SHASH_FOR_EACH (node, info) {
422 struct ofproto_controller_info *cinfo = node->data;
423 while (cinfo->pairs.n) {
424 free((char *) cinfo->pairs.values[--cinfo->pairs.n]);
431 /* Changes 'mgr''s set of controllers to the 'n_controllers' controllers in
434 connmgr_set_controllers(struct connmgr *mgr,
435 const struct ofproto_controller *controllers,
436 size_t n_controllers)
438 bool had_controllers = connmgr_has_controllers(mgr);
439 struct shash new_controllers;
440 struct ofconn *ofconn, *next_ofconn;
441 struct ofservice *ofservice, *next_ofservice;
444 /* Create newly configured controllers and services.
445 * Create a name to ofproto_controller mapping in 'new_controllers'. */
446 shash_init(&new_controllers);
447 for (i = 0; i < n_controllers; i++) {
448 const struct ofproto_controller *c = &controllers[i];
450 if (!vconn_verify_name(c->target)) {
451 if (!find_controller_by_target(mgr, c->target)) {
452 add_controller(mgr, c->target);
454 } else if (!pvconn_verify_name(c->target)) {
455 if (!ofservice_lookup(mgr, c->target)) {
456 ofservice_create(mgr, c->target);
459 VLOG_WARN_RL(&rl, "%s: unsupported controller \"%s\"",
460 mgr->name, c->target);
464 shash_add_once(&new_controllers, c->target, &controllers[i]);
467 /* Delete controllers that are no longer configured.
468 * Update configuration of all now-existing controllers. */
469 HMAP_FOR_EACH_SAFE (ofconn, next_ofconn, hmap_node, &mgr->controllers) {
470 struct ofproto_controller *c;
472 c = shash_find_data(&new_controllers, ofconn_get_target(ofconn));
474 ofconn_destroy(ofconn);
476 ofconn_reconfigure(ofconn, c);
480 /* Delete services that are no longer configured.
481 * Update configuration of all now-existing services. */
482 HMAP_FOR_EACH_SAFE (ofservice, next_ofservice, node, &mgr->services) {
483 struct ofproto_controller *c;
485 c = shash_find_data(&new_controllers,
486 pvconn_get_name(ofservice->pvconn));
488 ofservice_destroy(mgr, ofservice);
490 ofservice_reconfigure(ofservice, c);
494 shash_destroy(&new_controllers);
496 update_in_band_remotes(mgr);
497 update_fail_open(mgr);
498 if (had_controllers != connmgr_has_controllers(mgr)) {
499 ofproto_flush_flows(mgr->ofproto);
503 /* Drops the connections between 'mgr' and all of its primary and secondary
504 * controllers, forcing them to reconnect. */
506 connmgr_reconnect(const struct connmgr *mgr)
508 struct ofconn *ofconn;
510 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
511 rconn_reconnect(ofconn->rconn);
515 /* Sets the "snoops" for 'mgr' to the pvconn targets listed in 'snoops'.
517 * A "snoop" is a pvconn to which every OpenFlow message to or from the most
518 * important controller on 'mgr' is mirrored. */
520 connmgr_set_snoops(struct connmgr *mgr, const struct sset *snoops)
522 return set_pvconns(&mgr->snoops, &mgr->n_snoops, snoops);
525 /* Adds each of the snoops currently configured on 'mgr' to 'snoops'. */
527 connmgr_get_snoops(const struct connmgr *mgr, struct sset *snoops)
531 for (i = 0; i < mgr->n_snoops; i++) {
532 sset_add(snoops, pvconn_get_name(mgr->snoops[i]));
536 /* Returns true if 'mgr' has at least one snoop, false if it has none. */
538 connmgr_has_snoops(const struct connmgr *mgr)
540 return mgr->n_snoops > 0;
543 /* Creates a new controller for 'target' in 'mgr'. update_controller() needs
544 * to be called later to finish the new ofconn's configuration. */
546 add_controller(struct connmgr *mgr, const char *target)
548 char *name = ofconn_make_name(mgr, target);
549 struct ofconn *ofconn;
551 ofconn = ofconn_create(mgr, rconn_create(5, 8), OFCONN_PRIMARY);
552 ofconn->pktbuf = pktbuf_create();
553 ofconn->miss_send_len = OFP_DEFAULT_MISS_SEND_LEN;
554 rconn_connect(ofconn->rconn, target, name);
555 hmap_insert(&mgr->controllers, &ofconn->hmap_node, hash_string(target, 0));
560 static struct ofconn *
561 find_controller_by_target(struct connmgr *mgr, const char *target)
563 struct ofconn *ofconn;
565 HMAP_FOR_EACH_WITH_HASH (ofconn, hmap_node,
566 hash_string(target, 0), &mgr->controllers) {
567 if (!strcmp(ofconn_get_target(ofconn), target)) {
575 update_in_band_remotes(struct connmgr *mgr)
577 struct sockaddr_in *addrs;
578 size_t max_addrs, n_addrs;
579 struct ofconn *ofconn;
582 /* Allocate enough memory for as many remotes as we could possibly have. */
583 max_addrs = mgr->n_extra_remotes + hmap_count(&mgr->controllers);
584 addrs = xmalloc(max_addrs * sizeof *addrs);
587 /* Add all the remotes. */
588 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
589 struct sockaddr_in *sin = &addrs[n_addrs];
591 if (ofconn->band == OFPROTO_OUT_OF_BAND) {
595 sin->sin_addr.s_addr = rconn_get_remote_ip(ofconn->rconn);
596 if (sin->sin_addr.s_addr) {
597 sin->sin_port = rconn_get_remote_port(ofconn->rconn);
601 for (i = 0; i < mgr->n_extra_remotes; i++) {
602 addrs[n_addrs++] = mgr->extra_in_band_remotes[i];
605 /* Create or update or destroy in-band. */
608 in_band_create(mgr->ofproto, mgr->local_port_name, &mgr->in_band);
610 in_band_set_queue(mgr->in_band, mgr->in_band_queue);
612 /* in_band_run() needs a chance to delete any existing in-band flows.
613 * We will destroy mgr->in_band after it's done with that. */
616 in_band_set_remotes(mgr->in_band, addrs, n_addrs);
624 update_fail_open(struct connmgr *mgr)
626 if (connmgr_has_controllers(mgr)
627 && mgr->fail_mode == OFPROTO_FAIL_STANDALONE) {
628 if (!mgr->fail_open) {
629 mgr->fail_open = fail_open_create(mgr->ofproto, mgr);
632 fail_open_destroy(mgr->fail_open);
633 mgr->fail_open = NULL;
638 set_pvconns(struct pvconn ***pvconnsp, size_t *n_pvconnsp,
639 const struct sset *sset)
641 struct pvconn **pvconns = *pvconnsp;
642 size_t n_pvconns = *n_pvconnsp;
647 for (i = 0; i < n_pvconns; i++) {
648 pvconn_close(pvconns[i]);
652 pvconns = xmalloc(sset_count(sset) * sizeof *pvconns);
654 SSET_FOR_EACH (name, sset) {
655 struct pvconn *pvconn;
658 error = pvconn_open(name, &pvconn);
660 pvconns[n_pvconns++] = pvconn;
662 VLOG_ERR("failed to listen on %s: %s", name, strerror(error));
670 *n_pvconnsp = n_pvconns;
675 /* Returns a "preference level" for snooping 'ofconn'. A higher return value
676 * means that 'ofconn' is more interesting for monitoring than a lower return
679 snoop_preference(const struct ofconn *ofconn)
681 switch (ofconn->role) {
689 /* Shouldn't happen. */
694 /* One of 'mgr''s "snoop" pvconns has accepted a new connection on 'vconn'.
695 * Connects this vconn to a controller. */
697 add_snooper(struct connmgr *mgr, struct vconn *vconn)
699 struct ofconn *ofconn, *best;
701 /* Pick a controller for monitoring. */
703 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
704 if (ofconn->type == OFCONN_PRIMARY
705 && (!best || snoop_preference(ofconn) > snoop_preference(best))) {
711 rconn_add_monitor(best->rconn, vconn);
713 VLOG_INFO_RL(&rl, "no controller connection to snoop");
718 /* Public ofconn functions. */
720 /* Returns the connection type, either OFCONN_PRIMARY or OFCONN_SERVICE. */
722 ofconn_get_type(const struct ofconn *ofconn)
727 /* Returns the role configured for 'ofconn'.
729 * The default role, if no other role has been set, is NX_ROLE_OTHER. */
731 ofconn_get_role(const struct ofconn *ofconn)
736 /* Changes 'ofconn''s role to 'role'. If 'role' is NX_ROLE_MASTER then any
737 * existing master is demoted to a slave. */
739 ofconn_set_role(struct ofconn *ofconn, enum nx_role role)
741 if (role == NX_ROLE_MASTER) {
742 struct ofconn *other;
744 HMAP_FOR_EACH (other, hmap_node, &ofconn->connmgr->controllers) {
745 if (other->role == NX_ROLE_MASTER) {
746 other->role = NX_ROLE_SLAVE;
753 /* Returns the currently configured flow format for 'ofconn', one of NXFF_*.
755 * The default, if no other format has been set, is NXFF_OPENFLOW10. */
757 ofconn_get_flow_format(struct ofconn *ofconn)
759 return ofconn->flow_format;
762 /* Sets the flow format for 'ofconn' to 'flow_format' (one of NXFF_*). */
764 ofconn_set_flow_format(struct ofconn *ofconn, enum nx_flow_format flow_format)
766 ofconn->flow_format = flow_format;
769 /* Returns true if the NXT_FLOW_MOD_TABLE_ID extension is enabled, false
772 * By default the extension is not enabled. */
774 ofconn_get_flow_mod_table_id(const struct ofconn *ofconn)
776 return ofconn->flow_mod_table_id;
779 /* Enables or disables (according to 'enable') the NXT_FLOW_MOD_TABLE_ID
780 * extension on 'ofconn'. */
782 ofconn_set_flow_mod_table_id(struct ofconn *ofconn, bool enable)
784 ofconn->flow_mod_table_id = enable;
787 /* Returns the default miss send length for 'ofconn'. */
789 ofconn_get_miss_send_len(const struct ofconn *ofconn)
791 return ofconn->miss_send_len;
794 /* Sets the default miss send length for 'ofconn' to 'miss_send_len'. */
796 ofconn_set_miss_send_len(struct ofconn *ofconn, int miss_send_len)
798 ofconn->miss_send_len = miss_send_len;
801 /* Sends 'msg' on 'ofconn', accounting it as a reply. (If there is a
802 * sufficient number of OpenFlow replies in-flight on a single ofconn, then the
803 * connmgr will stop accepting new OpenFlow requests on that ofconn until the
804 * controller has accepted some of the replies.) */
806 ofconn_send_reply(const struct ofconn *ofconn, struct ofpbuf *msg)
808 ofconn_send(ofconn, msg, ofconn->reply_counter);
811 /* Sends each of the messages in list 'replies' on 'ofconn' in order,
812 * accounting them as replies. */
814 ofconn_send_replies(const struct ofconn *ofconn, struct list *replies)
816 struct ofpbuf *reply, *next;
818 LIST_FOR_EACH_SAFE (reply, next, list_node, replies) {
819 list_remove(&reply->list_node);
820 ofconn_send_reply(ofconn, reply);
824 /* Sends 'error', which should be an OpenFlow error created with
825 * e.g. ofp_mkerr(), on 'ofconn', as a reply to 'request'. Only at most the
826 * first 64 bytes of 'request' are used. */
828 ofconn_send_error(const struct ofconn *ofconn,
829 const struct ofp_header *request, int error)
831 struct ofpbuf *msg = ofputil_encode_error_msg(error, request);
833 ofconn_send_reply(ofconn, msg);
837 /* Same as pktbuf_retrieve(), using the pktbuf owned by 'ofconn'. */
839 ofconn_pktbuf_retrieve(struct ofconn *ofconn, uint32_t id,
840 struct ofpbuf **bufferp, uint16_t *in_port)
842 return pktbuf_retrieve(ofconn->pktbuf, id, bufferp, in_port);
845 /* Returns true if 'ofconn' has any pending opgroups. */
847 ofconn_has_pending_opgroups(const struct ofconn *ofconn)
849 return !list_is_empty(&ofconn->opgroups);
852 /* Returns the number of pending opgroups on 'ofconn'. */
854 ofconn_n_pending_opgroups(const struct ofconn *ofconn)
856 return list_size(&ofconn->opgroups);
859 /* Adds 'ofconn_node' to 'ofconn''s list of pending opgroups.
861 * If 'ofconn' is destroyed or its connection drops, then 'ofconn' will remove
862 * 'ofconn_node' from the list and re-initialize it with list_init(). The
863 * client may, therefore, use list_is_empty(ofconn_node) to determine whether
864 * 'ofconn_node' is still associated with an active ofconn.
866 * The client may also remove ofconn_node from the list itself, with
869 ofconn_add_opgroup(struct ofconn *ofconn, struct list *ofconn_node)
871 list_push_back(&ofconn->opgroups, ofconn_node);
874 /* Private ofconn functions. */
877 ofconn_get_target(const struct ofconn *ofconn)
879 return rconn_get_target(ofconn->rconn);
882 static struct ofconn *
883 ofconn_create(struct connmgr *mgr, struct rconn *rconn, enum ofconn_type type)
885 struct ofconn *ofconn = xzalloc(sizeof *ofconn);
886 ofconn->connmgr = mgr;
887 list_push_back(&mgr->all_conns, &ofconn->node);
888 ofconn->rconn = rconn;
890 ofconn->flow_format = NXFF_OPENFLOW10;
891 ofconn->flow_mod_table_id = false;
892 list_init(&ofconn->opgroups);
893 ofconn->role = NX_ROLE_OTHER;
894 ofconn->packet_in_counter = rconn_packet_counter_create ();
895 ofconn->pktbuf = NULL;
896 ofconn->miss_send_len = 0;
897 ofconn->reply_counter = rconn_packet_counter_create ();
901 /* Disassociates 'ofconn' from all of the ofopgroups that it initiated that
902 * have not yet completed. (Those ofopgroups will still run to completion in
903 * the usual way, but any errors that they run into will not be reported on any
906 * Also discards any blocked operation on 'ofconn'. */
908 ofconn_flush(struct ofconn *ofconn)
910 while (!list_is_empty(&ofconn->opgroups)) {
911 list_init(list_pop_front(&ofconn->opgroups));
913 ofpbuf_delete(ofconn->blocked);
914 ofconn->blocked = NULL;
918 ofconn_destroy(struct ofconn *ofconn)
920 ofconn_flush(ofconn);
922 if (ofconn->type == OFCONN_PRIMARY) {
923 hmap_remove(&ofconn->connmgr->controllers, &ofconn->hmap_node);
926 list_remove(&ofconn->node);
927 rconn_destroy(ofconn->rconn);
928 rconn_packet_counter_destroy(ofconn->packet_in_counter);
929 rconn_packet_counter_destroy(ofconn->reply_counter);
930 pktbuf_destroy(ofconn->pktbuf);
934 /* Reconfigures 'ofconn' to match 'c'. 'ofconn' and 'c' must have the same
937 ofconn_reconfigure(struct ofconn *ofconn, const struct ofproto_controller *c)
941 ofconn->band = c->band;
943 rconn_set_max_backoff(ofconn->rconn, c->max_backoff);
945 probe_interval = c->probe_interval ? MAX(c->probe_interval, 5) : 0;
946 rconn_set_probe_interval(ofconn->rconn, probe_interval);
948 ofconn_set_rate_limit(ofconn, c->rate_limit, c->burst_limit);
951 /* Returns true if it makes sense for 'ofconn' to receive and process OpenFlow
954 ofconn_may_recv(const struct ofconn *ofconn)
956 int count = rconn_packet_counter_read (ofconn->reply_counter);
957 return (!ofconn->blocked || ofconn->retry) && count < OFCONN_REPLY_MAX;
961 ofconn_run(struct ofconn *ofconn,
962 bool (*handle_openflow)(struct ofconn *, struct ofpbuf *ofp_msg))
964 struct connmgr *mgr = ofconn->connmgr;
967 for (i = 0; i < N_SCHEDULERS; i++) {
968 pinsched_run(ofconn->schedulers[i], do_send_packet_in, ofconn);
971 rconn_run(ofconn->rconn);
973 if (handle_openflow) {
974 /* Limit the number of iterations to avoid starving other tasks. */
975 for (i = 0; i < 50 && ofconn_may_recv(ofconn); i++) {
976 struct ofpbuf *of_msg;
978 of_msg = (ofconn->blocked
980 : rconn_recv(ofconn->rconn));
984 if (mgr->fail_open) {
985 fail_open_maybe_recover(mgr->fail_open);
988 if (handle_openflow(ofconn, of_msg)) {
989 ofpbuf_delete(of_msg);
990 ofconn->blocked = NULL;
992 ofconn->blocked = of_msg;
993 ofconn->retry = false;
998 if (!rconn_is_alive(ofconn->rconn)) {
999 ofconn_destroy(ofconn);
1000 } else if (!rconn_is_connected(ofconn->rconn)) {
1001 ofconn_flush(ofconn);
1006 ofconn_wait(struct ofconn *ofconn, bool handling_openflow)
1010 for (i = 0; i < N_SCHEDULERS; i++) {
1011 pinsched_wait(ofconn->schedulers[i]);
1013 rconn_run_wait(ofconn->rconn);
1014 if (handling_openflow && ofconn_may_recv(ofconn)) {
1015 rconn_recv_wait(ofconn->rconn);
1019 /* Returns true if 'ofconn' should receive asynchronous messages. */
1021 ofconn_receives_async_msgs(const struct ofconn *ofconn)
1023 if (!rconn_is_connected(ofconn->rconn)) {
1025 } else if (ofconn->type == OFCONN_PRIMARY) {
1026 /* Primary controllers always get asynchronous messages unless they
1027 * have configured themselves as "slaves". */
1028 return ofconn->role != NX_ROLE_SLAVE;
1030 /* Service connections don't get asynchronous messages unless they have
1031 * explicitly asked for them by setting a nonzero miss send length. */
1032 return ofconn->miss_send_len > 0;
1036 /* Returns a human-readable name for an OpenFlow connection between 'mgr' and
1037 * 'target', suitable for use in log messages for identifying the connection.
1039 * The name is dynamically allocated. The caller should free it (with free())
1040 * when it is no longer needed. */
1042 ofconn_make_name(const struct connmgr *mgr, const char *target)
1044 return xasprintf("%s<->%s", mgr->name, target);
1048 ofconn_set_rate_limit(struct ofconn *ofconn, int rate, int burst)
1052 for (i = 0; i < N_SCHEDULERS; i++) {
1053 struct pinsched **s = &ofconn->schedulers[i];
1057 *s = pinsched_create(rate, burst);
1059 pinsched_set_limits(*s, rate, burst);
1062 pinsched_destroy(*s);
1069 ofconn_send(const struct ofconn *ofconn, struct ofpbuf *msg,
1070 struct rconn_packet_counter *counter)
1072 update_openflow_length(msg);
1073 if (rconn_send(ofconn->rconn, msg, counter)) {
1078 /* Sending asynchronous messages. */
1080 static void schedule_packet_in(struct ofconn *, struct ofputil_packet_in,
1081 const struct flow *, struct ofpbuf *rw_packet);
1083 /* Sends an OFPT_PORT_STATUS message with 'opp' and 'reason' to appropriate
1084 * controllers managed by 'mgr'. */
1086 connmgr_send_port_status(struct connmgr *mgr, const struct ofp_phy_port *opp,
1089 /* XXX Should limit the number of queued port status change messages. */
1090 struct ofconn *ofconn;
1092 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1093 struct ofp_port_status *ops;
1096 /* Primary controllers, even slaves, should always get port status
1097 updates. Otherwise obey ofconn_receives_async_msgs(). */
1098 if (ofconn->type != OFCONN_PRIMARY
1099 && !ofconn_receives_async_msgs(ofconn)) {
1103 ops = make_openflow_xid(sizeof *ops, OFPT_PORT_STATUS, 0, &b);
1104 ops->reason = reason;
1106 ofconn_send(ofconn, b, NULL);
1110 /* Sends an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message based on 'fr' to
1111 * appropriate controllers managed by 'mgr'. */
1113 connmgr_send_flow_removed(struct connmgr *mgr,
1114 const struct ofputil_flow_removed *fr)
1116 struct ofconn *ofconn;
1118 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1121 if (!ofconn_receives_async_msgs(ofconn)) {
1125 /* Account flow expirations as replies to OpenFlow requests. That
1126 * works because preventing OpenFlow requests from being processed also
1127 * prevents new flows from being added (and expiring). (It also
1128 * prevents processing OpenFlow requests that would not add new flows,
1129 * so it is imperfect.) */
1130 msg = ofputil_encode_flow_removed(fr, ofconn->flow_format);
1131 ofconn_send_reply(ofconn, msg);
1135 /* Given 'pin', sends an OFPT_PACKET_IN message to each OpenFlow controller as
1136 * necessary according to their individual configurations.
1138 * 'rw_packet' may be NULL. Otherwise, 'rw_packet' must contain the same data
1139 * as pin->packet. (rw_packet == pin->packet is also valid.) Ownership of
1140 * 'rw_packet' is transferred to this function. */
1142 connmgr_send_packet_in(struct connmgr *mgr,
1143 const struct ofputil_packet_in *pin,
1144 const struct flow *flow, struct ofpbuf *rw_packet)
1146 struct ofconn *ofconn, *prev;
1149 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1150 if (ofconn_receives_async_msgs(ofconn)) {
1152 schedule_packet_in(prev, *pin, flow, NULL);
1158 schedule_packet_in(prev, *pin, flow, rw_packet);
1160 ofpbuf_delete(rw_packet);
1164 /* pinsched callback for sending 'ofp_packet_in' on 'ofconn'. */
1166 do_send_packet_in(struct ofpbuf *ofp_packet_in, void *ofconn_)
1168 struct ofconn *ofconn = ofconn_;
1170 rconn_send_with_limit(ofconn->rconn, ofp_packet_in,
1171 ofconn->packet_in_counter, 100);
1174 /* Takes 'pin', whose packet has the flow specified by 'flow', composes an
1175 * OpenFlow packet-in message from it, and passes it to 'ofconn''s packet
1176 * scheduler for sending.
1178 * 'rw_packet' may be NULL. Otherwise, 'rw_packet' must contain the same data
1179 * as pin->packet. (rw_packet == pin->packet is also valid.) Ownership of
1180 * 'rw_packet' is transferred to this function. */
1182 schedule_packet_in(struct ofconn *ofconn, struct ofputil_packet_in pin,
1183 const struct flow *flow, struct ofpbuf *rw_packet)
1185 struct connmgr *mgr = ofconn->connmgr;
1187 /* Get OpenFlow buffer_id. */
1188 if (pin.reason == OFPR_ACTION) {
1189 pin.buffer_id = UINT32_MAX;
1190 } else if (mgr->fail_open && fail_open_is_active(mgr->fail_open)) {
1191 pin.buffer_id = pktbuf_get_null();
1192 } else if (!ofconn->pktbuf) {
1193 pin.buffer_id = UINT32_MAX;
1195 pin.buffer_id = pktbuf_save(ofconn->pktbuf, pin.packet, flow->in_port);
1198 /* Figure out how much of the packet to send. */
1199 if (pin.reason == OFPR_NO_MATCH) {
1200 pin.send_len = pin.packet->size;
1202 /* Caller should have initialized 'send_len' to 'max_len' specified in
1203 * struct ofp_action_output. */
1205 if (pin.buffer_id != UINT32_MAX) {
1206 pin.send_len = MIN(pin.send_len, ofconn->miss_send_len);
1209 /* Make OFPT_PACKET_IN and hand over to packet scheduler. It might
1210 * immediately call into do_send_packet_in() or it might buffer it for a
1211 * while (until a later call to pinsched_run()). */
1212 pinsched_send(ofconn->schedulers[pin.reason == OFPR_NO_MATCH ? 0 : 1],
1213 flow->in_port, ofputil_encode_packet_in(&pin, rw_packet),
1214 do_send_packet_in, ofconn);
1217 /* Fail-open settings. */
1219 /* Returns the failure handling mode (OFPROTO_FAIL_SECURE or
1220 * OFPROTO_FAIL_STANDALONE) for 'mgr'. */
1221 enum ofproto_fail_mode
1222 connmgr_get_fail_mode(const struct connmgr *mgr)
1224 return mgr->fail_mode;
1227 /* Sets the failure handling mode for 'mgr' to 'fail_mode' (either
1228 * OFPROTO_FAIL_SECURE or OFPROTO_FAIL_STANDALONE). */
1230 connmgr_set_fail_mode(struct connmgr *mgr, enum ofproto_fail_mode fail_mode)
1232 if (mgr->fail_mode != fail_mode) {
1233 mgr->fail_mode = fail_mode;
1234 update_fail_open(mgr);
1235 if (!connmgr_has_controllers(mgr)) {
1236 ofproto_flush_flows(mgr->ofproto);
1241 /* Fail-open implementation. */
1243 /* Returns the longest probe interval among the primary controllers configured
1244 * on 'mgr'. Returns 0 if there are no primary controllers. */
1246 connmgr_get_max_probe_interval(const struct connmgr *mgr)
1248 const struct ofconn *ofconn;
1249 int max_probe_interval;
1251 max_probe_interval = 0;
1252 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1253 int probe_interval = rconn_get_probe_interval(ofconn->rconn);
1254 max_probe_interval = MAX(max_probe_interval, probe_interval);
1256 return max_probe_interval;
1259 /* Returns the number of seconds for which all of 'mgr's primary controllers
1260 * have been disconnected. Returns 0 if 'mgr' has no primary controllers. */
1262 connmgr_failure_duration(const struct connmgr *mgr)
1264 const struct ofconn *ofconn;
1265 int min_failure_duration;
1267 if (!connmgr_has_controllers(mgr)) {
1271 min_failure_duration = INT_MAX;
1272 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1273 int failure_duration = rconn_failure_duration(ofconn->rconn);
1274 min_failure_duration = MIN(min_failure_duration, failure_duration);
1276 return min_failure_duration;
1279 /* Returns true if at least one primary controller is connected (regardless of
1280 * whether those controllers are believed to have authenticated and accepted
1281 * this switch), false if none of them are connected. */
1283 connmgr_is_any_controller_connected(const struct connmgr *mgr)
1285 const struct ofconn *ofconn;
1287 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1288 if (rconn_is_connected(ofconn->rconn)) {
1295 /* Returns true if at least one primary controller is believed to have
1296 * authenticated and accepted this switch, false otherwise. */
1298 connmgr_is_any_controller_admitted(const struct connmgr *mgr)
1300 const struct ofconn *ofconn;
1302 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1303 if (rconn_is_admitted(ofconn->rconn)) {
1310 /* Sends 'packet' to each controller connected to 'mgr'. Takes ownership of
1313 connmgr_broadcast(struct connmgr *mgr, struct ofpbuf *packet)
1315 struct ofconn *ofconn, *prev;
1318 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1320 ofconn_send_reply(ofconn, ofpbuf_clone(packet));
1322 if (rconn_is_connected(ofconn->rconn)) {
1327 ofconn_send_reply(prev, packet);
1329 ofpbuf_delete(packet);
1333 /* In-band configuration. */
1335 static bool any_extras_changed(const struct connmgr *,
1336 const struct sockaddr_in *extras, size_t n);
1338 /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'mgr''s
1339 * in-band control should guarantee access, in the same way that in-band
1340 * control guarantees access to OpenFlow controllers. */
1342 connmgr_set_extra_in_band_remotes(struct connmgr *mgr,
1343 const struct sockaddr_in *extras, size_t n)
1345 if (!any_extras_changed(mgr, extras, n)) {
1349 free(mgr->extra_in_band_remotes);
1350 mgr->n_extra_remotes = n;
1351 mgr->extra_in_band_remotes = xmemdup(extras, n * sizeof *extras);
1353 update_in_band_remotes(mgr);
1356 /* Sets the OpenFlow queue used by flows set up by in-band control on
1357 * 'mgr' to 'queue_id'. If 'queue_id' is negative, then in-band control
1358 * flows will use the default queue. */
1360 connmgr_set_in_band_queue(struct connmgr *mgr, int queue_id)
1362 if (queue_id != mgr->in_band_queue) {
1363 mgr->in_band_queue = queue_id;
1364 update_in_band_remotes(mgr);
1369 any_extras_changed(const struct connmgr *mgr,
1370 const struct sockaddr_in *extras, size_t n)
1374 if (n != mgr->n_extra_remotes) {
1378 for (i = 0; i < n; i++) {
1379 const struct sockaddr_in *old = &mgr->extra_in_band_remotes[i];
1380 const struct sockaddr_in *new = &extras[i];
1382 if (old->sin_addr.s_addr != new->sin_addr.s_addr ||
1383 old->sin_port != new->sin_port) {
1391 /* In-band implementation. */
1394 connmgr_msg_in_hook(struct connmgr *mgr, const struct flow *flow,
1395 const struct ofpbuf *packet)
1397 return mgr->in_band && in_band_msg_in_hook(mgr->in_band, flow, packet);
1401 connmgr_may_set_up_flow(struct connmgr *mgr, const struct flow *flow,
1402 const struct nlattr *odp_actions,
1405 return !mgr->in_band || in_band_rule_check(flow, odp_actions, actions_len);
1408 /* Fail-open and in-band implementation. */
1410 /* Called by 'ofproto' after all flows have been flushed, to allow fail-open
1411 * and standalone mode to re-create their flows.
1413 * In-band control has more sophisticated code that manages flows itself. */
1415 connmgr_flushed(struct connmgr *mgr)
1417 if (mgr->fail_open) {
1418 fail_open_flushed(mgr->fail_open);
1421 /* If there are no controllers and we're in standalone mode, set up a flow
1422 * that matches every packet and directs them to OFPP_NORMAL (which goes to
1423 * us). Otherwise, the switch is in secure mode and we won't pass any
1424 * traffic until a controller has been defined and it tells us to do so. */
1425 if (!connmgr_has_controllers(mgr)
1426 && mgr->fail_mode == OFPROTO_FAIL_STANDALONE) {
1427 union ofp_action action;
1428 struct cls_rule rule;
1430 memset(&action, 0, sizeof action);
1431 action.type = htons(OFPAT_OUTPUT);
1432 action.output.len = htons(sizeof action);
1433 action.output.port = htons(OFPP_NORMAL);
1434 cls_rule_init_catchall(&rule, 0);
1435 ofproto_add_flow(mgr->ofproto, &rule, &action, 1);
1439 /* Creates a new ofservice for 'target' in 'mgr'. Returns 0 if successful,
1440 * otherwise a positive errno value.
1442 * ofservice_reconfigure() must be called to fully configure the new
1445 ofservice_create(struct connmgr *mgr, const char *target)
1447 struct ofservice *ofservice;
1448 struct pvconn *pvconn;
1451 error = pvconn_open(target, &pvconn);
1456 ofservice = xzalloc(sizeof *ofservice);
1457 hmap_insert(&mgr->services, &ofservice->node, hash_string(target, 0));
1458 ofservice->pvconn = pvconn;
1464 ofservice_destroy(struct connmgr *mgr, struct ofservice *ofservice)
1466 hmap_remove(&mgr->services, &ofservice->node);
1467 pvconn_close(ofservice->pvconn);
1472 ofservice_reconfigure(struct ofservice *ofservice,
1473 const struct ofproto_controller *c)
1475 ofservice->probe_interval = c->probe_interval;
1476 ofservice->rate_limit = c->rate_limit;
1477 ofservice->burst_limit = c->burst_limit;
1480 /* Finds and returns the ofservice within 'mgr' that has the given
1481 * 'target', or a null pointer if none exists. */
1482 static struct ofservice *
1483 ofservice_lookup(struct connmgr *mgr, const char *target)
1485 struct ofservice *ofservice;
1487 HMAP_FOR_EACH_WITH_HASH (ofservice, node, hash_string(target, 0),
1489 if (!strcmp(pvconn_get_name(ofservice->pvconn), target)) {