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 enum nx_packet_in_format packet_in_format; /* OFPT_PACKET_IN format. */
52 bool flow_mod_table_id; /* NXT_FLOW_MOD_TABLE_ID enabled? */
54 /* Asynchronous flow table operation support. */
55 struct list opgroups; /* Contains pending "ofopgroups", if any. */
56 struct ofpbuf *blocked; /* Postponed OpenFlow message, if any. */
57 bool retry; /* True if 'blocked' is ready to try again. */
59 /* OFPT_PACKET_IN related data. */
60 struct rconn_packet_counter *packet_in_counter; /* # queued on 'rconn'. */
61 #define N_SCHEDULERS 2
62 struct pinsched *schedulers[N_SCHEDULERS];
63 struct pktbuf *pktbuf; /* OpenFlow packet buffers. */
64 int miss_send_len; /* Bytes to send of buffered packets. */
66 /* Number of OpenFlow messages queued on 'rconn' as replies to OpenFlow
67 * requests, and the maximum number before we stop reading OpenFlow
69 #define OFCONN_REPLY_MAX 100
70 struct rconn_packet_counter *reply_counter;
72 /* type == OFCONN_PRIMARY only. */
73 enum nx_role role; /* Role. */
74 struct hmap_node hmap_node; /* In struct connmgr's "controllers" map. */
75 enum ofproto_band band; /* In-band or out-of-band? */
78 static struct ofconn *ofconn_create(struct connmgr *, struct rconn *,
80 static void ofconn_destroy(struct ofconn *);
82 static void ofconn_reconfigure(struct ofconn *,
83 const struct ofproto_controller *);
85 static void ofconn_run(struct ofconn *,
86 bool (*handle_openflow)(struct ofconn *,
87 struct ofpbuf *ofp_msg));
88 static void ofconn_wait(struct ofconn *, bool handling_openflow);
90 static const char *ofconn_get_target(const struct ofconn *);
91 static char *ofconn_make_name(const struct connmgr *, const char *target);
93 static void ofconn_set_rate_limit(struct ofconn *, int rate, int burst);
95 static bool ofconn_receives_async_msgs(const struct ofconn *);
97 static void ofconn_send(const struct ofconn *, struct ofpbuf *,
98 struct rconn_packet_counter *);
100 static void do_send_packet_in(struct ofpbuf *, void *ofconn_);
102 /* A listener for incoming OpenFlow "service" connections. */
104 struct hmap_node node; /* In struct connmgr's "services" hmap. */
105 struct pvconn *pvconn; /* OpenFlow connection listener. */
107 /* These are not used by ofservice directly. They are settings for
108 * accepted "struct ofconn"s from the pvconn. */
109 int probe_interval; /* Max idle time before probing, in seconds. */
110 int rate_limit; /* Max packet-in rate in packets per second. */
111 int burst_limit; /* Limit on accumulating packet credits. */
114 static void ofservice_reconfigure(struct ofservice *,
115 const struct ofproto_controller *);
116 static int ofservice_create(struct connmgr *, const char *target);
117 static void ofservice_destroy(struct connmgr *, struct ofservice *);
118 static struct ofservice *ofservice_lookup(struct connmgr *,
121 /* Connection manager for an OpenFlow switch. */
123 struct ofproto *ofproto;
125 char *local_port_name;
127 /* OpenFlow connections. */
128 struct hmap controllers; /* Controller "struct ofconn"s. */
129 struct list all_conns; /* Contains "struct ofconn"s. */
131 /* OpenFlow listeners. */
132 struct hmap services; /* Contains "struct ofservice"s. */
133 struct pvconn **snoops;
137 struct fail_open *fail_open;
138 enum ofproto_fail_mode fail_mode;
140 /* In-band control. */
141 struct in_band *in_band;
142 struct sockaddr_in *extra_in_band_remotes;
143 size_t n_extra_remotes;
147 static void update_in_band_remotes(struct connmgr *);
148 static void add_snooper(struct connmgr *, struct vconn *);
150 /* Creates and returns a new connection manager owned by 'ofproto'. 'name' is
151 * a name for the ofproto suitable for using in log messages.
152 * 'local_port_name' is the name of the local port (OFPP_LOCAL) within
155 connmgr_create(struct ofproto *ofproto,
156 const char *name, const char *local_port_name)
160 mgr = xmalloc(sizeof *mgr);
161 mgr->ofproto = ofproto;
162 mgr->name = xstrdup(name);
163 mgr->local_port_name = xstrdup(local_port_name);
165 hmap_init(&mgr->controllers);
166 list_init(&mgr->all_conns);
168 hmap_init(&mgr->services);
172 mgr->fail_open = NULL;
173 mgr->fail_mode = OFPROTO_FAIL_SECURE;
176 mgr->extra_in_band_remotes = NULL;
177 mgr->n_extra_remotes = 0;
178 mgr->in_band_queue = -1;
183 /* Frees 'mgr' and all of its resources. */
185 connmgr_destroy(struct connmgr *mgr)
187 struct ofservice *ofservice, *next_ofservice;
188 struct ofconn *ofconn, *next_ofconn;
195 LIST_FOR_EACH_SAFE (ofconn, next_ofconn, node, &mgr->all_conns) {
196 ofconn_destroy(ofconn);
198 hmap_destroy(&mgr->controllers);
200 HMAP_FOR_EACH_SAFE (ofservice, next_ofservice, node, &mgr->services) {
201 ofservice_destroy(mgr, ofservice);
203 hmap_destroy(&mgr->services);
205 for (i = 0; i < mgr->n_snoops; i++) {
206 pvconn_close(mgr->snoops[i]);
210 fail_open_destroy(mgr->fail_open);
211 mgr->fail_open = NULL;
213 in_band_destroy(mgr->in_band);
215 free(mgr->extra_in_band_remotes);
217 free(mgr->local_port_name);
222 /* Does all of the periodic maintenance required by 'mgr'.
224 * If 'handle_openflow' is nonnull, calls 'handle_openflow' for each message
225 * received on an OpenFlow connection, passing along the OpenFlow connection
226 * itself and the message that was sent. If 'handle_openflow' returns true,
227 * the message is considered to be fully processed. If 'handle_openflow'
228 * returns false, the message is considered not to have been processed at all;
229 * it will be stored and re-presented to 'handle_openflow' following the next
230 * call to connmgr_retry(). 'handle_openflow' must not modify or free the
233 * If 'handle_openflow' is NULL, no OpenFlow messages will be processed and
234 * other activities that could affect the flow table (in-band processing,
235 * fail-open processing) are suppressed too. */
237 connmgr_run(struct connmgr *mgr,
238 bool (*handle_openflow)(struct ofconn *, struct ofpbuf *ofp_msg))
240 struct ofconn *ofconn, *next_ofconn;
241 struct ofservice *ofservice;
244 if (handle_openflow && mgr->in_band) {
245 if (!in_band_run(mgr->in_band)) {
246 in_band_destroy(mgr->in_band);
251 LIST_FOR_EACH_SAFE (ofconn, next_ofconn, node, &mgr->all_conns) {
252 ofconn_run(ofconn, handle_openflow);
255 /* Fail-open maintenance. Do this after processing the ofconns since
256 * fail-open checks the status of the controller rconn. */
257 if (handle_openflow && mgr->fail_open) {
258 fail_open_run(mgr->fail_open);
261 HMAP_FOR_EACH (ofservice, node, &mgr->services) {
265 retval = pvconn_accept(ofservice->pvconn, OFP_VERSION, &vconn);
270 rconn = rconn_create(ofservice->probe_interval, 0);
271 name = ofconn_make_name(mgr, vconn_get_name(vconn));
272 rconn_connect_unreliably(rconn, vconn, name);
275 ofconn = ofconn_create(mgr, rconn, OFCONN_SERVICE);
276 ofconn_set_rate_limit(ofconn, ofservice->rate_limit,
277 ofservice->burst_limit);
278 } else if (retval != EAGAIN) {
279 VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval));
283 for (i = 0; i < mgr->n_snoops; i++) {
287 retval = pvconn_accept(mgr->snoops[i], OFP_VERSION, &vconn);
289 add_snooper(mgr, vconn);
290 } else if (retval != EAGAIN) {
291 VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval));
296 /* Causes the poll loop to wake up when connmgr_run() needs to run.
298 * If 'handling_openflow' is true, arriving OpenFlow messages and other
299 * activities that affect the flow table will wake up the poll loop. If
300 * 'handling_openflow' is false, they will not. */
302 connmgr_wait(struct connmgr *mgr, bool handling_openflow)
304 struct ofservice *ofservice;
305 struct ofconn *ofconn;
308 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
309 ofconn_wait(ofconn, handling_openflow);
311 if (handling_openflow && mgr->in_band) {
312 in_band_wait(mgr->in_band);
314 if (handling_openflow && mgr->fail_open) {
315 fail_open_wait(mgr->fail_open);
317 HMAP_FOR_EACH (ofservice, node, &mgr->services) {
318 pvconn_wait(ofservice->pvconn);
320 for (i = 0; i < mgr->n_snoops; i++) {
321 pvconn_wait(mgr->snoops[i]);
325 /* Returns the ofproto that owns 'ofconn''s connmgr. */
327 ofconn_get_ofproto(const struct ofconn *ofconn)
329 return ofconn->connmgr->ofproto;
332 /* If processing of OpenFlow messages was blocked on any 'mgr' ofconns by
333 * returning false to the 'handle_openflow' callback to connmgr_run(), this
334 * re-enables them. */
336 connmgr_retry(struct connmgr *mgr)
338 struct ofconn *ofconn;
340 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
341 ofconn->retry = true;
345 /* OpenFlow configuration. */
347 static void add_controller(struct connmgr *, const char *target);
348 static struct ofconn *find_controller_by_target(struct connmgr *,
350 static void update_fail_open(struct connmgr *);
351 static int set_pvconns(struct pvconn ***pvconnsp, size_t *n_pvconnsp,
352 const struct sset *);
354 /* Returns true if 'mgr' has any configured primary controllers.
356 * Service controllers do not count, but configured primary controllers do
357 * count whether or not they are currently connected. */
359 connmgr_has_controllers(const struct connmgr *mgr)
361 return !hmap_is_empty(&mgr->controllers);
364 /* Initializes 'info' and populates it with information about each configured
365 * primary controller. The keys in 'info' are the controllers' targets; the
366 * data values are corresponding "struct ofproto_controller_info".
368 * The caller owns 'info' and everything in it and should free it when it is no
371 connmgr_get_controller_info(struct connmgr *mgr, struct shash *info)
373 const struct ofconn *ofconn;
375 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
376 const struct rconn *rconn = ofconn->rconn;
377 const char *target = rconn_get_target(rconn);
379 if (!shash_find(info, target)) {
380 struct ofproto_controller_info *cinfo = xmalloc(sizeof *cinfo);
381 time_t now = time_now();
382 time_t last_connection = rconn_get_last_connection(rconn);
383 time_t last_disconnect = rconn_get_last_disconnect(rconn);
384 int last_error = rconn_get_last_error(rconn);
386 shash_add(info, target, cinfo);
388 cinfo->is_connected = rconn_is_connected(rconn);
389 cinfo->role = ofconn->role;
394 cinfo->pairs.keys[cinfo->pairs.n] = "last_error";
395 cinfo->pairs.values[cinfo->pairs.n++]
396 = xstrdup(ovs_retval_to_string(last_error));
399 cinfo->pairs.keys[cinfo->pairs.n] = "state";
400 cinfo->pairs.values[cinfo->pairs.n++]
401 = xstrdup(rconn_get_state(rconn));
403 if (last_connection != TIME_MIN) {
404 cinfo->pairs.keys[cinfo->pairs.n] = "sec_since_connect";
405 cinfo->pairs.values[cinfo->pairs.n++]
406 = xasprintf("%ld", (long int) (now - last_connection));
409 if (last_disconnect != TIME_MIN) {
410 cinfo->pairs.keys[cinfo->pairs.n] = "sec_since_disconnect";
411 cinfo->pairs.values[cinfo->pairs.n++]
412 = xasprintf("%ld", (long int) (now - last_disconnect));
419 connmgr_free_controller_info(struct shash *info)
421 struct shash_node *node;
423 SHASH_FOR_EACH (node, info) {
424 struct ofproto_controller_info *cinfo = node->data;
425 while (cinfo->pairs.n) {
426 free((char *) cinfo->pairs.values[--cinfo->pairs.n]);
433 /* Changes 'mgr''s set of controllers to the 'n_controllers' controllers in
436 connmgr_set_controllers(struct connmgr *mgr,
437 const struct ofproto_controller *controllers,
438 size_t n_controllers)
440 bool had_controllers = connmgr_has_controllers(mgr);
441 struct shash new_controllers;
442 struct ofconn *ofconn, *next_ofconn;
443 struct ofservice *ofservice, *next_ofservice;
446 /* Create newly configured controllers and services.
447 * Create a name to ofproto_controller mapping in 'new_controllers'. */
448 shash_init(&new_controllers);
449 for (i = 0; i < n_controllers; i++) {
450 const struct ofproto_controller *c = &controllers[i];
452 if (!vconn_verify_name(c->target)) {
453 if (!find_controller_by_target(mgr, c->target)) {
454 add_controller(mgr, c->target);
456 } else if (!pvconn_verify_name(c->target)) {
457 if (!ofservice_lookup(mgr, c->target)) {
458 ofservice_create(mgr, c->target);
461 VLOG_WARN_RL(&rl, "%s: unsupported controller \"%s\"",
462 mgr->name, c->target);
466 shash_add_once(&new_controllers, c->target, &controllers[i]);
469 /* Delete controllers that are no longer configured.
470 * Update configuration of all now-existing controllers. */
471 HMAP_FOR_EACH_SAFE (ofconn, next_ofconn, hmap_node, &mgr->controllers) {
472 struct ofproto_controller *c;
474 c = shash_find_data(&new_controllers, ofconn_get_target(ofconn));
476 ofconn_destroy(ofconn);
478 ofconn_reconfigure(ofconn, c);
482 /* Delete services that are no longer configured.
483 * Update configuration of all now-existing services. */
484 HMAP_FOR_EACH_SAFE (ofservice, next_ofservice, node, &mgr->services) {
485 struct ofproto_controller *c;
487 c = shash_find_data(&new_controllers,
488 pvconn_get_name(ofservice->pvconn));
490 ofservice_destroy(mgr, ofservice);
492 ofservice_reconfigure(ofservice, c);
496 shash_destroy(&new_controllers);
498 update_in_band_remotes(mgr);
499 update_fail_open(mgr);
500 if (had_controllers != connmgr_has_controllers(mgr)) {
501 ofproto_flush_flows(mgr->ofproto);
505 /* Drops the connections between 'mgr' and all of its primary and secondary
506 * controllers, forcing them to reconnect. */
508 connmgr_reconnect(const struct connmgr *mgr)
510 struct ofconn *ofconn;
512 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
513 rconn_reconnect(ofconn->rconn);
517 /* Sets the "snoops" for 'mgr' to the pvconn targets listed in 'snoops'.
519 * A "snoop" is a pvconn to which every OpenFlow message to or from the most
520 * important controller on 'mgr' is mirrored. */
522 connmgr_set_snoops(struct connmgr *mgr, const struct sset *snoops)
524 return set_pvconns(&mgr->snoops, &mgr->n_snoops, snoops);
527 /* Adds each of the snoops currently configured on 'mgr' to 'snoops'. */
529 connmgr_get_snoops(const struct connmgr *mgr, struct sset *snoops)
533 for (i = 0; i < mgr->n_snoops; i++) {
534 sset_add(snoops, pvconn_get_name(mgr->snoops[i]));
538 /* Returns true if 'mgr' has at least one snoop, false if it has none. */
540 connmgr_has_snoops(const struct connmgr *mgr)
542 return mgr->n_snoops > 0;
545 /* Creates a new controller for 'target' in 'mgr'. update_controller() needs
546 * to be called later to finish the new ofconn's configuration. */
548 add_controller(struct connmgr *mgr, const char *target)
550 char *name = ofconn_make_name(mgr, target);
551 struct ofconn *ofconn;
553 ofconn = ofconn_create(mgr, rconn_create(5, 8), OFCONN_PRIMARY);
554 ofconn->pktbuf = pktbuf_create();
555 ofconn->miss_send_len = OFP_DEFAULT_MISS_SEND_LEN;
556 rconn_connect(ofconn->rconn, target, name);
557 hmap_insert(&mgr->controllers, &ofconn->hmap_node, hash_string(target, 0));
562 static struct ofconn *
563 find_controller_by_target(struct connmgr *mgr, const char *target)
565 struct ofconn *ofconn;
567 HMAP_FOR_EACH_WITH_HASH (ofconn, hmap_node,
568 hash_string(target, 0), &mgr->controllers) {
569 if (!strcmp(ofconn_get_target(ofconn), target)) {
577 update_in_band_remotes(struct connmgr *mgr)
579 struct sockaddr_in *addrs;
580 size_t max_addrs, n_addrs;
581 struct ofconn *ofconn;
584 /* Allocate enough memory for as many remotes as we could possibly have. */
585 max_addrs = mgr->n_extra_remotes + hmap_count(&mgr->controllers);
586 addrs = xmalloc(max_addrs * sizeof *addrs);
589 /* Add all the remotes. */
590 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
591 struct sockaddr_in *sin = &addrs[n_addrs];
592 const char *target = rconn_get_target(ofconn->rconn);
594 if (ofconn->band == OFPROTO_OUT_OF_BAND) {
598 if (stream_parse_target_with_default_ports(target,
605 for (i = 0; i < mgr->n_extra_remotes; i++) {
606 addrs[n_addrs++] = mgr->extra_in_band_remotes[i];
609 /* Create or update or destroy in-band. */
612 in_band_create(mgr->ofproto, mgr->local_port_name, &mgr->in_band);
614 in_band_set_queue(mgr->in_band, mgr->in_band_queue);
616 /* in_band_run() needs a chance to delete any existing in-band flows.
617 * We will destroy mgr->in_band after it's done with that. */
620 in_band_set_remotes(mgr->in_band, addrs, n_addrs);
628 update_fail_open(struct connmgr *mgr)
630 if (connmgr_has_controllers(mgr)
631 && mgr->fail_mode == OFPROTO_FAIL_STANDALONE) {
632 if (!mgr->fail_open) {
633 mgr->fail_open = fail_open_create(mgr->ofproto, mgr);
636 fail_open_destroy(mgr->fail_open);
637 mgr->fail_open = NULL;
642 set_pvconns(struct pvconn ***pvconnsp, size_t *n_pvconnsp,
643 const struct sset *sset)
645 struct pvconn **pvconns = *pvconnsp;
646 size_t n_pvconns = *n_pvconnsp;
651 for (i = 0; i < n_pvconns; i++) {
652 pvconn_close(pvconns[i]);
656 pvconns = xmalloc(sset_count(sset) * sizeof *pvconns);
658 SSET_FOR_EACH (name, sset) {
659 struct pvconn *pvconn;
662 error = pvconn_open(name, &pvconn);
664 pvconns[n_pvconns++] = pvconn;
666 VLOG_ERR("failed to listen on %s: %s", name, strerror(error));
674 *n_pvconnsp = n_pvconns;
679 /* Returns a "preference level" for snooping 'ofconn'. A higher return value
680 * means that 'ofconn' is more interesting for monitoring than a lower return
683 snoop_preference(const struct ofconn *ofconn)
685 switch (ofconn->role) {
693 /* Shouldn't happen. */
698 /* One of 'mgr''s "snoop" pvconns has accepted a new connection on 'vconn'.
699 * Connects this vconn to a controller. */
701 add_snooper(struct connmgr *mgr, struct vconn *vconn)
703 struct ofconn *ofconn, *best;
705 /* Pick a controller for monitoring. */
707 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
708 if (ofconn->type == OFCONN_PRIMARY
709 && (!best || snoop_preference(ofconn) > snoop_preference(best))) {
715 rconn_add_monitor(best->rconn, vconn);
717 VLOG_INFO_RL(&rl, "no controller connection to snoop");
722 /* Public ofconn functions. */
724 /* Returns the connection type, either OFCONN_PRIMARY or OFCONN_SERVICE. */
726 ofconn_get_type(const struct ofconn *ofconn)
731 /* Returns the role configured for 'ofconn'.
733 * The default role, if no other role has been set, is NX_ROLE_OTHER. */
735 ofconn_get_role(const struct ofconn *ofconn)
740 /* Changes 'ofconn''s role to 'role'. If 'role' is NX_ROLE_MASTER then any
741 * existing master is demoted to a slave. */
743 ofconn_set_role(struct ofconn *ofconn, enum nx_role role)
745 if (role == NX_ROLE_MASTER) {
746 struct ofconn *other;
748 HMAP_FOR_EACH (other, hmap_node, &ofconn->connmgr->controllers) {
749 if (other->role == NX_ROLE_MASTER) {
750 other->role = NX_ROLE_SLAVE;
757 /* Returns the currently configured flow format for 'ofconn', one of NXFF_*.
759 * The default, if no other format has been set, is NXFF_OPENFLOW10. */
761 ofconn_get_flow_format(struct ofconn *ofconn)
763 return ofconn->flow_format;
766 /* Sets the flow format for 'ofconn' to 'flow_format' (one of NXFF_*). */
768 ofconn_set_flow_format(struct ofconn *ofconn, enum nx_flow_format flow_format)
770 ofconn->flow_format = flow_format;
773 /* Returns the currently configured packet in format for 'ofconn', one of
776 * The default, if no other format has been set, is NXPIF_OPENFLOW10. */
777 enum nx_packet_in_format
778 ofconn_get_packet_in_format(struct ofconn *ofconn)
780 return ofconn->packet_in_format;
783 /* Sets the packet in format for 'ofconn' to 'packet_in_format' (one of
786 ofconn_set_packet_in_format(struct ofconn *ofconn,
787 enum nx_packet_in_format packet_in_format)
789 ofconn->packet_in_format = packet_in_format;
792 /* Returns true if the NXT_FLOW_MOD_TABLE_ID extension is enabled, false
795 * By default the extension is not enabled. */
797 ofconn_get_flow_mod_table_id(const struct ofconn *ofconn)
799 return ofconn->flow_mod_table_id;
802 /* Enables or disables (according to 'enable') the NXT_FLOW_MOD_TABLE_ID
803 * extension on 'ofconn'. */
805 ofconn_set_flow_mod_table_id(struct ofconn *ofconn, bool enable)
807 ofconn->flow_mod_table_id = enable;
810 /* Returns the default miss send length for 'ofconn'. */
812 ofconn_get_miss_send_len(const struct ofconn *ofconn)
814 return ofconn->miss_send_len;
817 /* Sets the default miss send length for 'ofconn' to 'miss_send_len'. */
819 ofconn_set_miss_send_len(struct ofconn *ofconn, int miss_send_len)
821 ofconn->miss_send_len = miss_send_len;
824 /* Sends 'msg' on 'ofconn', accounting it as a reply. (If there is a
825 * sufficient number of OpenFlow replies in-flight on a single ofconn, then the
826 * connmgr will stop accepting new OpenFlow requests on that ofconn until the
827 * controller has accepted some of the replies.) */
829 ofconn_send_reply(const struct ofconn *ofconn, struct ofpbuf *msg)
831 ofconn_send(ofconn, msg, ofconn->reply_counter);
834 /* Sends each of the messages in list 'replies' on 'ofconn' in order,
835 * accounting them as replies. */
837 ofconn_send_replies(const struct ofconn *ofconn, struct list *replies)
839 struct ofpbuf *reply, *next;
841 LIST_FOR_EACH_SAFE (reply, next, list_node, replies) {
842 list_remove(&reply->list_node);
843 ofconn_send_reply(ofconn, reply);
847 /* Sends 'error' on 'ofconn', as a reply to 'request'. Only at most the
848 * first 64 bytes of 'request' are used. */
850 ofconn_send_error(const struct ofconn *ofconn,
851 const struct ofp_header *request, enum ofperr error)
853 struct ofpbuf *reply;
855 reply = ofperr_encode_reply(error, request);
857 static struct vlog_rate_limit err_rl = VLOG_RATE_LIMIT_INIT(10, 10);
859 if (!VLOG_DROP_INFO(&err_rl)) {
860 const struct ofputil_msg_type *type;
861 const char *type_name;
864 request_len = ntohs(request->length);
865 type_name = (!ofputil_decode_msg_type_partial(request,
866 MIN(64, request_len),
868 ? ofputil_msg_type_name(type)
871 VLOG_INFO("%s: sending %s error reply to %s message",
872 rconn_get_name(ofconn->rconn), ofperr_to_string(error),
875 ofconn_send_reply(ofconn, reply);
879 /* Same as pktbuf_retrieve(), using the pktbuf owned by 'ofconn'. */
881 ofconn_pktbuf_retrieve(struct ofconn *ofconn, uint32_t id,
882 struct ofpbuf **bufferp, uint16_t *in_port)
884 return pktbuf_retrieve(ofconn->pktbuf, id, bufferp, in_port);
887 /* Returns true if 'ofconn' has any pending opgroups. */
889 ofconn_has_pending_opgroups(const struct ofconn *ofconn)
891 return !list_is_empty(&ofconn->opgroups);
894 /* Adds 'ofconn_node' to 'ofconn''s list of pending opgroups.
896 * If 'ofconn' is destroyed or its connection drops, then 'ofconn' will remove
897 * 'ofconn_node' from the list and re-initialize it with list_init(). The
898 * client may, therefore, use list_is_empty(ofconn_node) to determine whether
899 * 'ofconn_node' is still associated with an active ofconn.
901 * The client may also remove ofconn_node from the list itself, with
904 ofconn_add_opgroup(struct ofconn *ofconn, struct list *ofconn_node)
906 list_push_back(&ofconn->opgroups, ofconn_node);
909 /* Private ofconn functions. */
912 ofconn_get_target(const struct ofconn *ofconn)
914 return rconn_get_target(ofconn->rconn);
917 static struct ofconn *
918 ofconn_create(struct connmgr *mgr, struct rconn *rconn, enum ofconn_type type)
920 struct ofconn *ofconn = xzalloc(sizeof *ofconn);
921 ofconn->connmgr = mgr;
922 list_push_back(&mgr->all_conns, &ofconn->node);
923 ofconn->rconn = rconn;
925 ofconn->flow_format = NXFF_OPENFLOW10;
926 ofconn->packet_in_format = NXPIF_OPENFLOW10;
927 ofconn->flow_mod_table_id = false;
928 list_init(&ofconn->opgroups);
929 ofconn->role = NX_ROLE_OTHER;
930 ofconn->packet_in_counter = rconn_packet_counter_create ();
931 ofconn->pktbuf = NULL;
932 ofconn->miss_send_len = 0;
933 ofconn->reply_counter = rconn_packet_counter_create ();
937 /* Disassociates 'ofconn' from all of the ofopgroups that it initiated that
938 * have not yet completed. (Those ofopgroups will still run to completion in
939 * the usual way, but any errors that they run into will not be reported on any
942 * Also discards any blocked operation on 'ofconn'. */
944 ofconn_flush(struct ofconn *ofconn)
946 while (!list_is_empty(&ofconn->opgroups)) {
947 list_init(list_pop_front(&ofconn->opgroups));
949 ofpbuf_delete(ofconn->blocked);
950 ofconn->blocked = NULL;
954 ofconn_destroy(struct ofconn *ofconn)
956 ofconn_flush(ofconn);
958 if (ofconn->type == OFCONN_PRIMARY) {
959 hmap_remove(&ofconn->connmgr->controllers, &ofconn->hmap_node);
962 list_remove(&ofconn->node);
963 rconn_destroy(ofconn->rconn);
964 rconn_packet_counter_destroy(ofconn->packet_in_counter);
965 rconn_packet_counter_destroy(ofconn->reply_counter);
966 pktbuf_destroy(ofconn->pktbuf);
970 /* Reconfigures 'ofconn' to match 'c'. 'ofconn' and 'c' must have the same
973 ofconn_reconfigure(struct ofconn *ofconn, const struct ofproto_controller *c)
977 ofconn->band = c->band;
979 rconn_set_max_backoff(ofconn->rconn, c->max_backoff);
981 probe_interval = c->probe_interval ? MAX(c->probe_interval, 5) : 0;
982 rconn_set_probe_interval(ofconn->rconn, probe_interval);
984 ofconn_set_rate_limit(ofconn, c->rate_limit, c->burst_limit);
987 /* Returns true if it makes sense for 'ofconn' to receive and process OpenFlow
990 ofconn_may_recv(const struct ofconn *ofconn)
992 int count = rconn_packet_counter_read (ofconn->reply_counter);
993 return (!ofconn->blocked || ofconn->retry) && count < OFCONN_REPLY_MAX;
997 ofconn_run(struct ofconn *ofconn,
998 bool (*handle_openflow)(struct ofconn *, struct ofpbuf *ofp_msg))
1000 struct connmgr *mgr = ofconn->connmgr;
1003 for (i = 0; i < N_SCHEDULERS; i++) {
1004 pinsched_run(ofconn->schedulers[i], do_send_packet_in, ofconn);
1007 rconn_run(ofconn->rconn);
1009 if (handle_openflow) {
1010 /* Limit the number of iterations to avoid starving other tasks. */
1011 for (i = 0; i < 50 && ofconn_may_recv(ofconn); i++) {
1012 struct ofpbuf *of_msg;
1014 of_msg = (ofconn->blocked
1016 : rconn_recv(ofconn->rconn));
1020 if (mgr->fail_open) {
1021 fail_open_maybe_recover(mgr->fail_open);
1024 if (handle_openflow(ofconn, of_msg)) {
1025 ofpbuf_delete(of_msg);
1026 ofconn->blocked = NULL;
1028 ofconn->blocked = of_msg;
1029 ofconn->retry = false;
1034 if (!rconn_is_alive(ofconn->rconn)) {
1035 ofconn_destroy(ofconn);
1036 } else if (!rconn_is_connected(ofconn->rconn)) {
1037 ofconn_flush(ofconn);
1042 ofconn_wait(struct ofconn *ofconn, bool handling_openflow)
1046 for (i = 0; i < N_SCHEDULERS; i++) {
1047 pinsched_wait(ofconn->schedulers[i]);
1049 rconn_run_wait(ofconn->rconn);
1050 if (handling_openflow && ofconn_may_recv(ofconn)) {
1051 rconn_recv_wait(ofconn->rconn);
1055 /* Returns true if 'ofconn' should receive asynchronous messages. */
1057 ofconn_receives_async_msgs(const struct ofconn *ofconn)
1059 if (!rconn_is_connected(ofconn->rconn)) {
1061 } else if (ofconn->type == OFCONN_PRIMARY) {
1062 /* Primary controllers always get asynchronous messages unless they
1063 * have configured themselves as "slaves". */
1064 return ofconn->role != NX_ROLE_SLAVE;
1066 /* Service connections don't get asynchronous messages unless they have
1067 * explicitly asked for them by setting a nonzero miss send length. */
1068 return ofconn->miss_send_len > 0;
1072 /* Returns a human-readable name for an OpenFlow connection between 'mgr' and
1073 * 'target', suitable for use in log messages for identifying the connection.
1075 * The name is dynamically allocated. The caller should free it (with free())
1076 * when it is no longer needed. */
1078 ofconn_make_name(const struct connmgr *mgr, const char *target)
1080 return xasprintf("%s<->%s", mgr->name, target);
1084 ofconn_set_rate_limit(struct ofconn *ofconn, int rate, int burst)
1088 for (i = 0; i < N_SCHEDULERS; i++) {
1089 struct pinsched **s = &ofconn->schedulers[i];
1093 *s = pinsched_create(rate, burst);
1095 pinsched_set_limits(*s, rate, burst);
1098 pinsched_destroy(*s);
1105 ofconn_send(const struct ofconn *ofconn, struct ofpbuf *msg,
1106 struct rconn_packet_counter *counter)
1108 update_openflow_length(msg);
1109 if (rconn_send(ofconn->rconn, msg, counter)) {
1114 /* Sending asynchronous messages. */
1116 static void schedule_packet_in(struct ofconn *, struct ofputil_packet_in,
1117 const struct flow *);
1119 /* Sends an OFPT_PORT_STATUS message with 'opp' and 'reason' to appropriate
1120 * controllers managed by 'mgr'. */
1122 connmgr_send_port_status(struct connmgr *mgr, const struct ofp_phy_port *opp,
1125 /* XXX Should limit the number of queued port status change messages. */
1126 struct ofconn *ofconn;
1128 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1129 struct ofp_port_status *ops;
1132 /* Primary controllers, even slaves, should always get port status
1133 updates. Otherwise obey ofconn_receives_async_msgs(). */
1134 if (ofconn->type != OFCONN_PRIMARY
1135 && !ofconn_receives_async_msgs(ofconn)) {
1139 ops = make_openflow_xid(sizeof *ops, OFPT_PORT_STATUS, 0, &b);
1140 ops->reason = reason;
1142 ofconn_send(ofconn, b, NULL);
1146 /* Sends an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message based on 'fr' to
1147 * appropriate controllers managed by 'mgr'. */
1149 connmgr_send_flow_removed(struct connmgr *mgr,
1150 const struct ofputil_flow_removed *fr)
1152 struct ofconn *ofconn;
1154 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1157 if (!ofconn_receives_async_msgs(ofconn)) {
1161 /* Account flow expirations as replies to OpenFlow requests. That
1162 * works because preventing OpenFlow requests from being processed also
1163 * prevents new flows from being added (and expiring). (It also
1164 * prevents processing OpenFlow requests that would not add new flows,
1165 * so it is imperfect.) */
1166 msg = ofputil_encode_flow_removed(fr, ofconn->flow_format);
1167 ofconn_send_reply(ofconn, msg);
1171 /* Given 'pin', sends an OFPT_PACKET_IN message to each OpenFlow controller as
1172 * necessary according to their individual configurations. */
1174 connmgr_send_packet_in(struct connmgr *mgr,
1175 const struct ofputil_packet_in *pin,
1176 const struct flow *flow)
1178 struct ofconn *ofconn;
1180 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1181 if (ofconn_receives_async_msgs(ofconn)) {
1182 schedule_packet_in(ofconn, *pin, flow);
1187 /* pinsched callback for sending 'ofp_packet_in' on 'ofconn'. */
1189 do_send_packet_in(struct ofpbuf *ofp_packet_in, void *ofconn_)
1191 struct ofconn *ofconn = ofconn_;
1193 rconn_send_with_limit(ofconn->rconn, ofp_packet_in,
1194 ofconn->packet_in_counter, 100);
1197 /* Takes 'pin', whose packet has the flow specified by 'flow', composes an
1198 * OpenFlow packet-in message from it, and passes it to 'ofconn''s packet
1199 * scheduler for sending. */
1201 schedule_packet_in(struct ofconn *ofconn, struct ofputil_packet_in pin,
1202 const struct flow *flow)
1204 struct connmgr *mgr = ofconn->connmgr;
1206 /* Get OpenFlow buffer_id. */
1207 if (pin.reason == OFPR_ACTION) {
1208 pin.buffer_id = UINT32_MAX;
1209 } else if (mgr->fail_open && fail_open_is_active(mgr->fail_open)) {
1210 pin.buffer_id = pktbuf_get_null();
1211 } else if (!ofconn->pktbuf) {
1212 pin.buffer_id = UINT32_MAX;
1214 pin.buffer_id = pktbuf_save(ofconn->pktbuf, pin.packet, pin.packet_len,
1218 /* Figure out how much of the packet to send. */
1219 if (pin.reason == OFPR_NO_MATCH) {
1220 pin.send_len = pin.packet_len;
1222 /* Caller should have initialized 'send_len' to 'max_len' specified in
1223 * struct ofp_action_output. */
1225 if (pin.buffer_id != UINT32_MAX) {
1226 pin.send_len = MIN(pin.send_len, ofconn->miss_send_len);
1229 /* Make OFPT_PACKET_IN and hand over to packet scheduler. It might
1230 * immediately call into do_send_packet_in() or it might buffer it for a
1231 * while (until a later call to pinsched_run()). */
1232 pinsched_send(ofconn->schedulers[pin.reason == OFPR_NO_MATCH ? 0 : 1],
1234 ofputil_encode_packet_in(&pin, ofconn->packet_in_format),
1235 do_send_packet_in, ofconn);
1238 /* Fail-open settings. */
1240 /* Returns the failure handling mode (OFPROTO_FAIL_SECURE or
1241 * OFPROTO_FAIL_STANDALONE) for 'mgr'. */
1242 enum ofproto_fail_mode
1243 connmgr_get_fail_mode(const struct connmgr *mgr)
1245 return mgr->fail_mode;
1248 /* Sets the failure handling mode for 'mgr' to 'fail_mode' (either
1249 * OFPROTO_FAIL_SECURE or OFPROTO_FAIL_STANDALONE). */
1251 connmgr_set_fail_mode(struct connmgr *mgr, enum ofproto_fail_mode fail_mode)
1253 if (mgr->fail_mode != fail_mode) {
1254 mgr->fail_mode = fail_mode;
1255 update_fail_open(mgr);
1256 if (!connmgr_has_controllers(mgr)) {
1257 ofproto_flush_flows(mgr->ofproto);
1262 /* Fail-open implementation. */
1264 /* Returns the longest probe interval among the primary controllers configured
1265 * on 'mgr'. Returns 0 if there are no primary controllers. */
1267 connmgr_get_max_probe_interval(const struct connmgr *mgr)
1269 const struct ofconn *ofconn;
1270 int max_probe_interval;
1272 max_probe_interval = 0;
1273 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1274 int probe_interval = rconn_get_probe_interval(ofconn->rconn);
1275 max_probe_interval = MAX(max_probe_interval, probe_interval);
1277 return max_probe_interval;
1280 /* Returns the number of seconds for which all of 'mgr's primary controllers
1281 * have been disconnected. Returns 0 if 'mgr' has no primary controllers. */
1283 connmgr_failure_duration(const struct connmgr *mgr)
1285 const struct ofconn *ofconn;
1286 int min_failure_duration;
1288 if (!connmgr_has_controllers(mgr)) {
1292 min_failure_duration = INT_MAX;
1293 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1294 int failure_duration = rconn_failure_duration(ofconn->rconn);
1295 min_failure_duration = MIN(min_failure_duration, failure_duration);
1297 return min_failure_duration;
1300 /* Returns true if at least one primary controller is connected (regardless of
1301 * whether those controllers are believed to have authenticated and accepted
1302 * this switch), false if none of them are connected. */
1304 connmgr_is_any_controller_connected(const struct connmgr *mgr)
1306 const struct ofconn *ofconn;
1308 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1309 if (rconn_is_connected(ofconn->rconn)) {
1316 /* Returns true if at least one primary controller is believed to have
1317 * authenticated and accepted this switch, false otherwise. */
1319 connmgr_is_any_controller_admitted(const struct connmgr *mgr)
1321 const struct ofconn *ofconn;
1323 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1324 if (rconn_is_admitted(ofconn->rconn)) {
1331 /* Sends 'packet' to each controller connected to 'mgr'. Takes ownership of
1334 connmgr_broadcast(struct connmgr *mgr, struct ofpbuf *packet)
1336 struct ofconn *ofconn, *prev;
1339 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1341 ofconn_send_reply(ofconn, ofpbuf_clone(packet));
1343 if (rconn_is_connected(ofconn->rconn)) {
1348 ofconn_send_reply(prev, packet);
1350 ofpbuf_delete(packet);
1354 /* In-band configuration. */
1356 static bool any_extras_changed(const struct connmgr *,
1357 const struct sockaddr_in *extras, size_t n);
1359 /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'mgr''s
1360 * in-band control should guarantee access, in the same way that in-band
1361 * control guarantees access to OpenFlow controllers. */
1363 connmgr_set_extra_in_band_remotes(struct connmgr *mgr,
1364 const struct sockaddr_in *extras, size_t n)
1366 if (!any_extras_changed(mgr, extras, n)) {
1370 free(mgr->extra_in_band_remotes);
1371 mgr->n_extra_remotes = n;
1372 mgr->extra_in_band_remotes = xmemdup(extras, n * sizeof *extras);
1374 update_in_band_remotes(mgr);
1377 /* Sets the OpenFlow queue used by flows set up by in-band control on
1378 * 'mgr' to 'queue_id'. If 'queue_id' is negative, then in-band control
1379 * flows will use the default queue. */
1381 connmgr_set_in_band_queue(struct connmgr *mgr, int queue_id)
1383 if (queue_id != mgr->in_band_queue) {
1384 mgr->in_band_queue = queue_id;
1385 update_in_band_remotes(mgr);
1390 any_extras_changed(const struct connmgr *mgr,
1391 const struct sockaddr_in *extras, size_t n)
1395 if (n != mgr->n_extra_remotes) {
1399 for (i = 0; i < n; i++) {
1400 const struct sockaddr_in *old = &mgr->extra_in_band_remotes[i];
1401 const struct sockaddr_in *new = &extras[i];
1403 if (old->sin_addr.s_addr != new->sin_addr.s_addr ||
1404 old->sin_port != new->sin_port) {
1412 /* In-band implementation. */
1415 connmgr_msg_in_hook(struct connmgr *mgr, const struct flow *flow,
1416 const struct ofpbuf *packet)
1418 return mgr->in_band && in_band_msg_in_hook(mgr->in_band, flow, packet);
1422 connmgr_may_set_up_flow(struct connmgr *mgr, const struct flow *flow,
1423 const struct nlattr *odp_actions,
1426 return !mgr->in_band || in_band_rule_check(flow, odp_actions, actions_len);
1429 /* Fail-open and in-band implementation. */
1431 /* Called by 'ofproto' after all flows have been flushed, to allow fail-open
1432 * and standalone mode to re-create their flows.
1434 * In-band control has more sophisticated code that manages flows itself. */
1436 connmgr_flushed(struct connmgr *mgr)
1438 if (mgr->fail_open) {
1439 fail_open_flushed(mgr->fail_open);
1442 /* If there are no controllers and we're in standalone mode, set up a flow
1443 * that matches every packet and directs them to OFPP_NORMAL (which goes to
1444 * us). Otherwise, the switch is in secure mode and we won't pass any
1445 * traffic until a controller has been defined and it tells us to do so. */
1446 if (!connmgr_has_controllers(mgr)
1447 && mgr->fail_mode == OFPROTO_FAIL_STANDALONE) {
1448 union ofp_action action;
1449 struct cls_rule rule;
1451 memset(&action, 0, sizeof action);
1452 action.type = htons(OFPAT_OUTPUT);
1453 action.output.len = htons(sizeof action);
1454 action.output.port = htons(OFPP_NORMAL);
1455 cls_rule_init_catchall(&rule, 0);
1456 ofproto_add_flow(mgr->ofproto, &rule, &action, 1);
1460 /* Creates a new ofservice for 'target' in 'mgr'. Returns 0 if successful,
1461 * otherwise a positive errno value.
1463 * ofservice_reconfigure() must be called to fully configure the new
1466 ofservice_create(struct connmgr *mgr, const char *target)
1468 struct ofservice *ofservice;
1469 struct pvconn *pvconn;
1472 error = pvconn_open(target, &pvconn);
1477 ofservice = xzalloc(sizeof *ofservice);
1478 hmap_insert(&mgr->services, &ofservice->node, hash_string(target, 0));
1479 ofservice->pvconn = pvconn;
1485 ofservice_destroy(struct connmgr *mgr, struct ofservice *ofservice)
1487 hmap_remove(&mgr->services, &ofservice->node);
1488 pvconn_close(ofservice->pvconn);
1493 ofservice_reconfigure(struct ofservice *ofservice,
1494 const struct ofproto_controller *c)
1496 ofservice->probe_interval = c->probe_interval;
1497 ofservice->rate_limit = c->rate_limit;
1498 ofservice->burst_limit = c->burst_limit;
1501 /* Finds and returns the ofservice within 'mgr' that has the given
1502 * 'target', or a null pointer if none exists. */
1503 static struct ofservice *
1504 ofservice_lookup(struct connmgr *mgr, const char *target)
1506 struct ofservice *ofservice;
1508 HMAP_FOR_EACH_WITH_HASH (ofservice, node, hash_string(target, 0),
1510 if (!strcmp(pvconn_get_name(ofservice->pvconn), target)) {