2 * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
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"
42 VLOG_DEFINE_THIS_MODULE(connmgr);
43 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
45 /* An OpenFlow connection. */
47 /* Configuration that persists from one connection to the next. */
49 struct list node; /* In struct connmgr's "all_conns" list. */
50 struct hmap_node hmap_node; /* In struct connmgr's "controllers" map. */
52 struct connmgr *connmgr; /* Connection's manager. */
53 struct rconn *rconn; /* OpenFlow connection. */
54 enum ofconn_type type; /* Type. */
55 enum ofproto_band band; /* In-band or out-of-band? */
56 bool enable_async_msgs; /* Initially enable async messages? */
58 /* State that should be cleared from one connection to the next. */
61 enum nx_role role; /* Role. */
62 enum ofputil_protocol protocol; /* Current protocol variant. */
63 enum nx_packet_in_format packet_in_format; /* OFPT_PACKET_IN format. */
65 /* Asynchronous flow table operation support. */
66 struct list opgroups; /* Contains pending "ofopgroups", if any. */
67 struct ofpbuf *blocked; /* Postponed OpenFlow message, if any. */
68 bool retry; /* True if 'blocked' is ready to try again. */
70 /* OFPT_PACKET_IN related data. */
71 struct rconn_packet_counter *packet_in_counter; /* # queued on 'rconn'. */
72 #define N_SCHEDULERS 2
73 struct pinsched *schedulers[N_SCHEDULERS];
74 struct pktbuf *pktbuf; /* OpenFlow packet buffers. */
75 int miss_send_len; /* Bytes to send of buffered packets. */
76 uint16_t controller_id; /* Connection controller ID. */
78 /* Number of OpenFlow messages queued on 'rconn' as replies to OpenFlow
79 * requests, and the maximum number before we stop reading OpenFlow
81 #define OFCONN_REPLY_MAX 100
82 struct rconn_packet_counter *reply_counter;
84 /* Asynchronous message configuration in each possible roles.
86 * A 1-bit enables sending an asynchronous message for one possible reason
87 * that the message might be generated, a 0-bit disables it. */
88 uint32_t master_async_config[OAM_N_TYPES]; /* master, other */
89 uint32_t slave_async_config[OAM_N_TYPES]; /* slave */
92 static struct ofconn *ofconn_create(struct connmgr *, struct rconn *,
93 enum ofconn_type, bool enable_async_msgs);
94 static void ofconn_destroy(struct ofconn *);
95 static void ofconn_flush(struct ofconn *);
97 static void ofconn_reconfigure(struct ofconn *,
98 const struct ofproto_controller *);
100 static void ofconn_run(struct ofconn *,
101 bool (*handle_openflow)(struct ofconn *,
102 struct ofpbuf *ofp_msg));
103 static void ofconn_wait(struct ofconn *, bool handling_openflow);
105 static const char *ofconn_get_target(const struct ofconn *);
106 static char *ofconn_make_name(const struct connmgr *, const char *target);
108 static void ofconn_set_rate_limit(struct ofconn *, int rate, int burst);
110 static void ofconn_send(const struct ofconn *, struct ofpbuf *,
111 struct rconn_packet_counter *);
113 static void do_send_packet_in(struct ofpbuf *, void *ofconn_);
115 /* A listener for incoming OpenFlow "service" connections. */
117 struct hmap_node node; /* In struct connmgr's "services" hmap. */
118 struct pvconn *pvconn; /* OpenFlow connection listener. */
120 /* These are not used by ofservice directly. They are settings for
121 * accepted "struct ofconn"s from the pvconn. */
122 int probe_interval; /* Max idle time before probing, in seconds. */
123 int rate_limit; /* Max packet-in rate in packets per second. */
124 int burst_limit; /* Limit on accumulating packet credits. */
125 bool enable_async_msgs; /* Initially enable async messages? */
126 uint8_t dscp; /* DSCP Value for controller connection */
129 static void ofservice_reconfigure(struct ofservice *,
130 const struct ofproto_controller *);
131 static int ofservice_create(struct connmgr *, const char *target, uint8_t dscp);
132 static void ofservice_destroy(struct connmgr *, struct ofservice *);
133 static struct ofservice *ofservice_lookup(struct connmgr *,
136 /* Connection manager for an OpenFlow switch. */
138 struct ofproto *ofproto;
140 char *local_port_name;
142 /* OpenFlow connections. */
143 struct hmap controllers; /* Controller "struct ofconn"s. */
144 struct list all_conns; /* Contains "struct ofconn"s. */
146 /* OpenFlow listeners. */
147 struct hmap services; /* Contains "struct ofservice"s. */
148 struct pvconn **snoops;
152 struct fail_open *fail_open;
153 enum ofproto_fail_mode fail_mode;
155 /* In-band control. */
156 struct in_band *in_band;
157 struct sockaddr_in *extra_in_band_remotes;
158 size_t n_extra_remotes;
162 static void update_in_band_remotes(struct connmgr *);
163 static void add_snooper(struct connmgr *, struct vconn *);
165 /* Creates and returns a new connection manager owned by 'ofproto'. 'name' is
166 * a name for the ofproto suitable for using in log messages.
167 * 'local_port_name' is the name of the local port (OFPP_LOCAL) within
170 connmgr_create(struct ofproto *ofproto,
171 const char *name, const char *local_port_name)
175 mgr = xmalloc(sizeof *mgr);
176 mgr->ofproto = ofproto;
177 mgr->name = xstrdup(name);
178 mgr->local_port_name = xstrdup(local_port_name);
180 hmap_init(&mgr->controllers);
181 list_init(&mgr->all_conns);
183 hmap_init(&mgr->services);
187 mgr->fail_open = NULL;
188 mgr->fail_mode = OFPROTO_FAIL_SECURE;
191 mgr->extra_in_band_remotes = NULL;
192 mgr->n_extra_remotes = 0;
193 mgr->in_band_queue = -1;
198 /* Frees 'mgr' and all of its resources. */
200 connmgr_destroy(struct connmgr *mgr)
202 struct ofservice *ofservice, *next_ofservice;
203 struct ofconn *ofconn, *next_ofconn;
210 LIST_FOR_EACH_SAFE (ofconn, next_ofconn, node, &mgr->all_conns) {
211 ofconn_destroy(ofconn);
213 hmap_destroy(&mgr->controllers);
215 HMAP_FOR_EACH_SAFE (ofservice, next_ofservice, node, &mgr->services) {
216 ofservice_destroy(mgr, ofservice);
218 hmap_destroy(&mgr->services);
220 for (i = 0; i < mgr->n_snoops; i++) {
221 pvconn_close(mgr->snoops[i]);
225 fail_open_destroy(mgr->fail_open);
226 mgr->fail_open = NULL;
228 in_band_destroy(mgr->in_band);
230 free(mgr->extra_in_band_remotes);
232 free(mgr->local_port_name);
237 /* Does all of the periodic maintenance required by 'mgr'.
239 * If 'handle_openflow' is nonnull, calls 'handle_openflow' for each message
240 * received on an OpenFlow connection, passing along the OpenFlow connection
241 * itself and the message that was sent. If 'handle_openflow' returns true,
242 * the message is considered to be fully processed. If 'handle_openflow'
243 * returns false, the message is considered not to have been processed at all;
244 * it will be stored and re-presented to 'handle_openflow' following the next
245 * call to connmgr_retry(). 'handle_openflow' must not modify or free the
248 * If 'handle_openflow' is NULL, no OpenFlow messages will be processed and
249 * other activities that could affect the flow table (in-band processing,
250 * fail-open processing) are suppressed too. */
252 connmgr_run(struct connmgr *mgr,
253 bool (*handle_openflow)(struct ofconn *, struct ofpbuf *ofp_msg))
255 struct ofconn *ofconn, *next_ofconn;
256 struct ofservice *ofservice;
259 if (handle_openflow && mgr->in_band) {
260 if (!in_band_run(mgr->in_band)) {
261 in_band_destroy(mgr->in_band);
266 LIST_FOR_EACH_SAFE (ofconn, next_ofconn, node, &mgr->all_conns) {
267 ofconn_run(ofconn, handle_openflow);
270 /* Fail-open maintenance. Do this after processing the ofconns since
271 * fail-open checks the status of the controller rconn. */
272 if (handle_openflow && mgr->fail_open) {
273 fail_open_run(mgr->fail_open);
276 HMAP_FOR_EACH (ofservice, node, &mgr->services) {
280 retval = pvconn_accept(ofservice->pvconn, OFP10_VERSION, &vconn);
285 /* Passing default value for creation of the rconn */
286 rconn = rconn_create(ofservice->probe_interval, 0, ofservice->dscp);
287 name = ofconn_make_name(mgr, vconn_get_name(vconn));
288 rconn_connect_unreliably(rconn, vconn, name);
291 ofconn = ofconn_create(mgr, rconn, OFCONN_SERVICE,
292 ofservice->enable_async_msgs);
293 ofconn_set_rate_limit(ofconn, ofservice->rate_limit,
294 ofservice->burst_limit);
295 } else if (retval != EAGAIN) {
296 VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval));
300 for (i = 0; i < mgr->n_snoops; i++) {
304 retval = pvconn_accept(mgr->snoops[i], OFP10_VERSION, &vconn);
306 add_snooper(mgr, vconn);
307 } else if (retval != EAGAIN) {
308 VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval));
313 /* Causes the poll loop to wake up when connmgr_run() needs to run.
315 * If 'handling_openflow' is true, arriving OpenFlow messages and other
316 * activities that affect the flow table will wake up the poll loop. If
317 * 'handling_openflow' is false, they will not. */
319 connmgr_wait(struct connmgr *mgr, bool handling_openflow)
321 struct ofservice *ofservice;
322 struct ofconn *ofconn;
325 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
326 ofconn_wait(ofconn, handling_openflow);
328 if (handling_openflow && mgr->in_band) {
329 in_band_wait(mgr->in_band);
331 if (handling_openflow && mgr->fail_open) {
332 fail_open_wait(mgr->fail_open);
334 HMAP_FOR_EACH (ofservice, node, &mgr->services) {
335 pvconn_wait(ofservice->pvconn);
337 for (i = 0; i < mgr->n_snoops; i++) {
338 pvconn_wait(mgr->snoops[i]);
342 /* Adds some memory usage statistics for 'mgr' into 'usage', for use with
343 * memory_report(). */
345 connmgr_get_memory_usage(const struct connmgr *mgr, struct simap *usage)
347 const struct ofconn *ofconn;
348 unsigned int packets = 0;
349 unsigned int ofconns = 0;
351 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
356 packets += rconn_count_txqlen(ofconn->rconn);
357 for (i = 0; i < N_SCHEDULERS; i++) {
358 packets += pinsched_count_txqlen(ofconn->schedulers[i]);
360 packets += pktbuf_count_packets(ofconn->pktbuf);
362 simap_increase(usage, "ofconns", ofconns);
363 simap_increase(usage, "packets", packets);
366 /* Returns the ofproto that owns 'ofconn''s connmgr. */
368 ofconn_get_ofproto(const struct ofconn *ofconn)
370 return ofconn->connmgr->ofproto;
373 /* If processing of OpenFlow messages was blocked on any 'mgr' ofconns by
374 * returning false to the 'handle_openflow' callback to connmgr_run(), this
375 * re-enables them. */
377 connmgr_retry(struct connmgr *mgr)
379 struct ofconn *ofconn;
381 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
382 ofconn->retry = true;
386 /* OpenFlow configuration. */
388 static void add_controller(struct connmgr *, const char *target, uint8_t dscp);
389 static struct ofconn *find_controller_by_target(struct connmgr *,
391 static void update_fail_open(struct connmgr *);
392 static int set_pvconns(struct pvconn ***pvconnsp, size_t *n_pvconnsp,
393 const struct sset *);
395 /* Returns true if 'mgr' has any configured primary controllers.
397 * Service controllers do not count, but configured primary controllers do
398 * count whether or not they are currently connected. */
400 connmgr_has_controllers(const struct connmgr *mgr)
402 return !hmap_is_empty(&mgr->controllers);
405 /* Initializes 'info' and populates it with information about each configured
406 * primary controller. The keys in 'info' are the controllers' targets; the
407 * data values are corresponding "struct ofproto_controller_info".
409 * The caller owns 'info' and everything in it and should free it when it is no
412 connmgr_get_controller_info(struct connmgr *mgr, struct shash *info)
414 const struct ofconn *ofconn;
416 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
417 const struct rconn *rconn = ofconn->rconn;
418 const char *target = rconn_get_target(rconn);
420 if (!shash_find(info, target)) {
421 struct ofproto_controller_info *cinfo = xmalloc(sizeof *cinfo);
422 time_t now = time_now();
423 time_t last_connection = rconn_get_last_connection(rconn);
424 time_t last_disconnect = rconn_get_last_disconnect(rconn);
425 int last_error = rconn_get_last_error(rconn);
427 shash_add(info, target, cinfo);
429 cinfo->is_connected = rconn_is_connected(rconn);
430 cinfo->role = ofconn->role;
435 cinfo->pairs.keys[cinfo->pairs.n] = "last_error";
436 cinfo->pairs.values[cinfo->pairs.n++]
437 = xstrdup(ovs_retval_to_string(last_error));
440 cinfo->pairs.keys[cinfo->pairs.n] = "state";
441 cinfo->pairs.values[cinfo->pairs.n++]
442 = xstrdup(rconn_get_state(rconn));
444 if (last_connection != TIME_MIN) {
445 cinfo->pairs.keys[cinfo->pairs.n] = "sec_since_connect";
446 cinfo->pairs.values[cinfo->pairs.n++]
447 = xasprintf("%ld", (long int) (now - last_connection));
450 if (last_disconnect != TIME_MIN) {
451 cinfo->pairs.keys[cinfo->pairs.n] = "sec_since_disconnect";
452 cinfo->pairs.values[cinfo->pairs.n++]
453 = xasprintf("%ld", (long int) (now - last_disconnect));
460 connmgr_free_controller_info(struct shash *info)
462 struct shash_node *node;
464 SHASH_FOR_EACH (node, info) {
465 struct ofproto_controller_info *cinfo = node->data;
466 while (cinfo->pairs.n) {
467 free((char *) cinfo->pairs.values[--cinfo->pairs.n]);
474 /* Changes 'mgr''s set of controllers to the 'n_controllers' controllers in
477 connmgr_set_controllers(struct connmgr *mgr,
478 const struct ofproto_controller *controllers,
479 size_t n_controllers)
481 bool had_controllers = connmgr_has_controllers(mgr);
482 struct shash new_controllers;
483 struct ofconn *ofconn, *next_ofconn;
484 struct ofservice *ofservice, *next_ofservice;
487 /* Create newly configured controllers and services.
488 * Create a name to ofproto_controller mapping in 'new_controllers'. */
489 shash_init(&new_controllers);
490 for (i = 0; i < n_controllers; i++) {
491 const struct ofproto_controller *c = &controllers[i];
493 if (!vconn_verify_name(c->target)) {
494 if (!find_controller_by_target(mgr, c->target)) {
495 VLOG_INFO("%s: added primary controller \"%s\"",
496 mgr->name, c->target);
497 add_controller(mgr, c->target, c->dscp);
499 } else if (!pvconn_verify_name(c->target)) {
500 if (!ofservice_lookup(mgr, c->target)) {
501 VLOG_INFO("%s: added service controller \"%s\"",
502 mgr->name, c->target);
503 ofservice_create(mgr, c->target, c->dscp);
506 VLOG_WARN_RL(&rl, "%s: unsupported controller \"%s\"",
507 mgr->name, c->target);
511 shash_add_once(&new_controllers, c->target, &controllers[i]);
514 /* Delete controllers that are no longer configured.
515 * Update configuration of all now-existing controllers. */
516 HMAP_FOR_EACH_SAFE (ofconn, next_ofconn, hmap_node, &mgr->controllers) {
517 const char *target = ofconn_get_target(ofconn);
518 struct ofproto_controller *c;
520 c = shash_find_data(&new_controllers, target);
522 VLOG_INFO("%s: removed primary controller \"%s\"",
524 ofconn_destroy(ofconn);
526 ofconn_reconfigure(ofconn, c);
530 /* Delete services that are no longer configured.
531 * Update configuration of all now-existing services. */
532 HMAP_FOR_EACH_SAFE (ofservice, next_ofservice, node, &mgr->services) {
533 const char *target = pvconn_get_name(ofservice->pvconn);
534 struct ofproto_controller *c;
536 c = shash_find_data(&new_controllers, target);
538 VLOG_INFO("%s: removed service controller \"%s\"",
540 ofservice_destroy(mgr, ofservice);
542 ofservice_reconfigure(ofservice, c);
546 shash_destroy(&new_controllers);
548 update_in_band_remotes(mgr);
549 update_fail_open(mgr);
550 if (had_controllers != connmgr_has_controllers(mgr)) {
551 ofproto_flush_flows(mgr->ofproto);
555 /* Drops the connections between 'mgr' and all of its primary and secondary
556 * controllers, forcing them to reconnect. */
558 connmgr_reconnect(const struct connmgr *mgr)
560 struct ofconn *ofconn;
562 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
563 rconn_reconnect(ofconn->rconn);
567 /* Sets the "snoops" for 'mgr' to the pvconn targets listed in 'snoops'.
569 * A "snoop" is a pvconn to which every OpenFlow message to or from the most
570 * important controller on 'mgr' is mirrored. */
572 connmgr_set_snoops(struct connmgr *mgr, const struct sset *snoops)
574 return set_pvconns(&mgr->snoops, &mgr->n_snoops, snoops);
577 /* Adds each of the snoops currently configured on 'mgr' to 'snoops'. */
579 connmgr_get_snoops(const struct connmgr *mgr, struct sset *snoops)
583 for (i = 0; i < mgr->n_snoops; i++) {
584 sset_add(snoops, pvconn_get_name(mgr->snoops[i]));
588 /* Returns true if 'mgr' has at least one snoop, false if it has none. */
590 connmgr_has_snoops(const struct connmgr *mgr)
592 return mgr->n_snoops > 0;
595 /* Creates a new controller for 'target' in 'mgr'. update_controller() needs
596 * to be called later to finish the new ofconn's configuration. */
598 add_controller(struct connmgr *mgr, const char *target, uint8_t dscp)
600 char *name = ofconn_make_name(mgr, target);
601 struct ofconn *ofconn;
603 ofconn = ofconn_create(mgr, rconn_create(5, 8, dscp), OFCONN_PRIMARY, true);
604 ofconn->pktbuf = pktbuf_create();
605 rconn_connect(ofconn->rconn, target, name);
606 hmap_insert(&mgr->controllers, &ofconn->hmap_node, hash_string(target, 0));
611 static struct ofconn *
612 find_controller_by_target(struct connmgr *mgr, const char *target)
614 struct ofconn *ofconn;
616 HMAP_FOR_EACH_WITH_HASH (ofconn, hmap_node,
617 hash_string(target, 0), &mgr->controllers) {
618 if (!strcmp(ofconn_get_target(ofconn), target)) {
626 update_in_band_remotes(struct connmgr *mgr)
628 struct sockaddr_in *addrs;
629 size_t max_addrs, n_addrs;
630 struct ofconn *ofconn;
633 /* Allocate enough memory for as many remotes as we could possibly have. */
634 max_addrs = mgr->n_extra_remotes + hmap_count(&mgr->controllers);
635 addrs = xmalloc(max_addrs * sizeof *addrs);
638 /* Add all the remotes. */
639 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
640 struct sockaddr_in *sin = &addrs[n_addrs];
641 const char *target = rconn_get_target(ofconn->rconn);
643 if (ofconn->band == OFPROTO_OUT_OF_BAND) {
647 if (stream_parse_target_with_default_ports(target,
654 for (i = 0; i < mgr->n_extra_remotes; i++) {
655 addrs[n_addrs++] = mgr->extra_in_band_remotes[i];
658 /* Create or update or destroy in-band. */
661 in_band_create(mgr->ofproto, mgr->local_port_name, &mgr->in_band);
663 in_band_set_queue(mgr->in_band, mgr->in_band_queue);
665 /* in_band_run() needs a chance to delete any existing in-band flows.
666 * We will destroy mgr->in_band after it's done with that. */
669 in_band_set_remotes(mgr->in_band, addrs, n_addrs);
677 update_fail_open(struct connmgr *mgr)
679 if (connmgr_has_controllers(mgr)
680 && mgr->fail_mode == OFPROTO_FAIL_STANDALONE) {
681 if (!mgr->fail_open) {
682 mgr->fail_open = fail_open_create(mgr->ofproto, mgr);
685 fail_open_destroy(mgr->fail_open);
686 mgr->fail_open = NULL;
691 set_pvconns(struct pvconn ***pvconnsp, size_t *n_pvconnsp,
692 const struct sset *sset)
694 struct pvconn **pvconns = *pvconnsp;
695 size_t n_pvconns = *n_pvconnsp;
700 for (i = 0; i < n_pvconns; i++) {
701 pvconn_close(pvconns[i]);
705 pvconns = xmalloc(sset_count(sset) * sizeof *pvconns);
707 SSET_FOR_EACH (name, sset) {
708 struct pvconn *pvconn;
711 error = pvconn_open(name, &pvconn, 0);
713 pvconns[n_pvconns++] = pvconn;
715 VLOG_ERR("failed to listen on %s: %s", name, strerror(error));
723 *n_pvconnsp = n_pvconns;
728 /* Returns a "preference level" for snooping 'ofconn'. A higher return value
729 * means that 'ofconn' is more interesting for monitoring than a lower return
732 snoop_preference(const struct ofconn *ofconn)
734 switch (ofconn->role) {
742 /* Shouldn't happen. */
747 /* One of 'mgr''s "snoop" pvconns has accepted a new connection on 'vconn'.
748 * Connects this vconn to a controller. */
750 add_snooper(struct connmgr *mgr, struct vconn *vconn)
752 struct ofconn *ofconn, *best;
754 /* Pick a controller for monitoring. */
756 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
757 if (ofconn->type == OFCONN_PRIMARY
758 && (!best || snoop_preference(ofconn) > snoop_preference(best))) {
764 rconn_add_monitor(best->rconn, vconn);
766 VLOG_INFO_RL(&rl, "no controller connection to snoop");
771 /* Public ofconn functions. */
773 /* Returns the connection type, either OFCONN_PRIMARY or OFCONN_SERVICE. */
775 ofconn_get_type(const struct ofconn *ofconn)
780 /* Returns the role configured for 'ofconn'.
782 * The default role, if no other role has been set, is NX_ROLE_OTHER. */
784 ofconn_get_role(const struct ofconn *ofconn)
789 /* Changes 'ofconn''s role to 'role'. If 'role' is NX_ROLE_MASTER then any
790 * existing master is demoted to a slave. */
792 ofconn_set_role(struct ofconn *ofconn, enum nx_role role)
794 if (role == NX_ROLE_MASTER) {
795 struct ofconn *other;
797 HMAP_FOR_EACH (other, hmap_node, &ofconn->connmgr->controllers) {
798 if (other->role == NX_ROLE_MASTER) {
799 other->role = NX_ROLE_SLAVE;
807 ofconn_set_invalid_ttl_to_controller(struct ofconn *ofconn, bool enable)
809 uint32_t bit = 1u << OFPR_INVALID_TTL;
811 ofconn->master_async_config[OAM_PACKET_IN] |= bit;
813 ofconn->master_async_config[OAM_PACKET_IN] &= ~bit;
818 ofconn_get_invalid_ttl_to_controller(struct ofconn *ofconn)
820 uint32_t bit = 1u << OFPR_INVALID_TTL;
821 return (ofconn->master_async_config[OAM_PACKET_IN] & bit) != 0;
824 /* Returns the currently configured protocol for 'ofconn', one of OFPUTIL_P_*.
826 * The default, if no other format has been set, is OFPUTIL_P_OPENFLOW10. */
827 enum ofputil_protocol
828 ofconn_get_protocol(struct ofconn *ofconn)
830 return ofconn->protocol;
833 /* Sets the protocol for 'ofconn' to 'protocol' (one of OFPUTIL_P_*).
835 * (This doesn't actually send anything to accomplish this. Presumably the
836 * caller already did that.) */
838 ofconn_set_protocol(struct ofconn *ofconn, enum ofputil_protocol protocol)
840 ofconn->protocol = protocol;
843 /* Returns the currently configured packet in format for 'ofconn', one of
846 * The default, if no other format has been set, is NXPIF_OPENFLOW10. */
847 enum nx_packet_in_format
848 ofconn_get_packet_in_format(struct ofconn *ofconn)
850 return ofconn->packet_in_format;
853 /* Sets the packet in format for 'ofconn' to 'packet_in_format' (one of
856 ofconn_set_packet_in_format(struct ofconn *ofconn,
857 enum nx_packet_in_format packet_in_format)
859 ofconn->packet_in_format = packet_in_format;
862 /* Sets the controller connection ID for 'ofconn' to 'controller_id'.
864 * The connection controller ID is used for OFPP_CONTROLLER and
865 * NXAST_CONTROLLER actions. See "struct nx_action_controller" for details. */
867 ofconn_set_controller_id(struct ofconn *ofconn, uint16_t controller_id)
869 ofconn->controller_id = controller_id;
872 /* Returns the default miss send length for 'ofconn'. */
874 ofconn_get_miss_send_len(const struct ofconn *ofconn)
876 return ofconn->miss_send_len;
879 /* Sets the default miss send length for 'ofconn' to 'miss_send_len'. */
881 ofconn_set_miss_send_len(struct ofconn *ofconn, int miss_send_len)
883 ofconn->miss_send_len = miss_send_len;
887 ofconn_set_async_config(struct ofconn *ofconn,
888 const uint32_t master_masks[OAM_N_TYPES],
889 const uint32_t slave_masks[OAM_N_TYPES])
891 size_t size = sizeof ofconn->master_async_config;
892 memcpy(ofconn->master_async_config, master_masks, size);
893 memcpy(ofconn->slave_async_config, slave_masks, size);
896 /* Sends 'msg' on 'ofconn', accounting it as a reply. (If there is a
897 * sufficient number of OpenFlow replies in-flight on a single ofconn, then the
898 * connmgr will stop accepting new OpenFlow requests on that ofconn until the
899 * controller has accepted some of the replies.) */
901 ofconn_send_reply(const struct ofconn *ofconn, struct ofpbuf *msg)
903 ofconn_send(ofconn, msg, ofconn->reply_counter);
906 /* Sends each of the messages in list 'replies' on 'ofconn' in order,
907 * accounting them as replies. */
909 ofconn_send_replies(const struct ofconn *ofconn, struct list *replies)
911 struct ofpbuf *reply, *next;
913 LIST_FOR_EACH_SAFE (reply, next, list_node, replies) {
914 list_remove(&reply->list_node);
915 ofconn_send_reply(ofconn, reply);
919 /* Sends 'error' on 'ofconn', as a reply to 'request'. Only at most the
920 * first 64 bytes of 'request' are used. */
922 ofconn_send_error(const struct ofconn *ofconn,
923 const struct ofp_header *request, enum ofperr error)
925 struct ofpbuf *reply;
927 reply = ofperr_encode_reply(error, request);
929 static struct vlog_rate_limit err_rl = VLOG_RATE_LIMIT_INIT(10, 10);
931 if (!VLOG_DROP_INFO(&err_rl)) {
932 const struct ofputil_msg_type *type;
933 const char *type_name;
936 request_len = ntohs(request->length);
937 type_name = (!ofputil_decode_msg_type_partial(request,
938 MIN(64, request_len),
940 ? ofputil_msg_type_name(type)
943 VLOG_INFO("%s: sending %s error reply to %s message",
944 rconn_get_name(ofconn->rconn), ofperr_to_string(error),
947 ofconn_send_reply(ofconn, reply);
951 /* Same as pktbuf_retrieve(), using the pktbuf owned by 'ofconn'. */
953 ofconn_pktbuf_retrieve(struct ofconn *ofconn, uint32_t id,
954 struct ofpbuf **bufferp, uint16_t *in_port)
956 return pktbuf_retrieve(ofconn->pktbuf, id, bufferp, in_port);
959 /* Returns true if 'ofconn' has any pending opgroups. */
961 ofconn_has_pending_opgroups(const struct ofconn *ofconn)
963 return !list_is_empty(&ofconn->opgroups);
966 /* Adds 'ofconn_node' to 'ofconn''s list of pending opgroups.
968 * If 'ofconn' is destroyed or its connection drops, then 'ofconn' will remove
969 * 'ofconn_node' from the list and re-initialize it with list_init(). The
970 * client may, therefore, use list_is_empty(ofconn_node) to determine whether
971 * 'ofconn_node' is still associated with an active ofconn.
973 * The client may also remove ofconn_node from the list itself, with
976 ofconn_add_opgroup(struct ofconn *ofconn, struct list *ofconn_node)
978 list_push_back(&ofconn->opgroups, ofconn_node);
981 /* Private ofconn functions. */
984 ofconn_get_target(const struct ofconn *ofconn)
986 return rconn_get_target(ofconn->rconn);
989 static struct ofconn *
990 ofconn_create(struct connmgr *mgr, struct rconn *rconn, enum ofconn_type type,
991 bool enable_async_msgs)
993 struct ofconn *ofconn;
995 ofconn = xzalloc(sizeof *ofconn);
996 ofconn->connmgr = mgr;
997 list_push_back(&mgr->all_conns, &ofconn->node);
998 ofconn->rconn = rconn;
1000 ofconn->enable_async_msgs = enable_async_msgs;
1002 list_init(&ofconn->opgroups);
1004 ofconn_flush(ofconn);
1009 /* Clears all of the state in 'ofconn' that should not persist from one
1010 * connection to the next. */
1012 ofconn_flush(struct ofconn *ofconn)
1016 ofconn->role = NX_ROLE_OTHER;
1017 ofconn->protocol = OFPUTIL_P_OF10;
1018 ofconn->packet_in_format = NXPIF_OPENFLOW10;
1020 /* Disassociate 'ofconn' from all of the ofopgroups that it initiated that
1021 * have not yet completed. (Those ofopgroups will still run to completion
1022 * in the usual way, but any errors that they run into will not be reported
1023 * on any OpenFlow channel.)
1025 * Also discard any blocked operation on 'ofconn'. */
1026 while (!list_is_empty(&ofconn->opgroups)) {
1027 list_init(list_pop_front(&ofconn->opgroups));
1029 ofpbuf_delete(ofconn->blocked);
1030 ofconn->blocked = NULL;
1032 rconn_packet_counter_destroy(ofconn->packet_in_counter);
1033 ofconn->packet_in_counter = rconn_packet_counter_create();
1034 for (i = 0; i < N_SCHEDULERS; i++) {
1035 if (ofconn->schedulers[i]) {
1038 pinsched_get_limits(ofconn->schedulers[i], &rate, &burst);
1039 pinsched_destroy(ofconn->schedulers[i]);
1040 ofconn->schedulers[i] = pinsched_create(rate, burst);
1043 if (ofconn->pktbuf) {
1044 pktbuf_destroy(ofconn->pktbuf);
1045 ofconn->pktbuf = pktbuf_create();
1047 ofconn->miss_send_len = (ofconn->type == OFCONN_PRIMARY
1048 ? OFP_DEFAULT_MISS_SEND_LEN
1050 ofconn->controller_id = 0;
1052 rconn_packet_counter_destroy(ofconn->reply_counter);
1053 ofconn->reply_counter = rconn_packet_counter_create();
1055 if (ofconn->enable_async_msgs) {
1056 uint32_t *master = ofconn->master_async_config;
1057 uint32_t *slave = ofconn->slave_async_config;
1059 /* "master" and "other" roles get all asynchronous messages by default,
1060 * except that the controller needs to enable nonstandard "packet-in"
1061 * reasons itself. */
1062 master[OAM_PACKET_IN] = (1u << OFPR_NO_MATCH) | (1u << OFPR_ACTION);
1063 master[OAM_PORT_STATUS] = ((1u << OFPPR_ADD)
1064 | (1u << OFPPR_DELETE)
1065 | (1u << OFPPR_MODIFY));
1066 master[OAM_FLOW_REMOVED] = ((1u << OFPRR_IDLE_TIMEOUT)
1067 | (1u << OFPRR_HARD_TIMEOUT)
1068 | (1u << OFPRR_DELETE));
1070 /* "slave" role gets port status updates by default. */
1071 slave[OAM_PACKET_IN] = 0;
1072 slave[OAM_PORT_STATUS] = ((1u << OFPPR_ADD)
1073 | (1u << OFPPR_DELETE)
1074 | (1u << OFPPR_MODIFY));
1075 slave[OAM_FLOW_REMOVED] = 0;
1077 memset(ofconn->master_async_config, 0,
1078 sizeof ofconn->master_async_config);
1079 memset(ofconn->slave_async_config, 0,
1080 sizeof ofconn->slave_async_config);
1085 ofconn_destroy(struct ofconn *ofconn)
1087 ofconn_flush(ofconn);
1089 if (ofconn->type == OFCONN_PRIMARY) {
1090 hmap_remove(&ofconn->connmgr->controllers, &ofconn->hmap_node);
1093 list_remove(&ofconn->node);
1094 rconn_destroy(ofconn->rconn);
1095 rconn_packet_counter_destroy(ofconn->packet_in_counter);
1096 rconn_packet_counter_destroy(ofconn->reply_counter);
1097 pktbuf_destroy(ofconn->pktbuf);
1101 /* Reconfigures 'ofconn' to match 'c'. 'ofconn' and 'c' must have the same
1104 ofconn_reconfigure(struct ofconn *ofconn, const struct ofproto_controller *c)
1108 ofconn->band = c->band;
1109 ofconn->enable_async_msgs = c->enable_async_msgs;
1111 rconn_set_max_backoff(ofconn->rconn, c->max_backoff);
1113 probe_interval = c->probe_interval ? MAX(c->probe_interval, 5) : 0;
1114 rconn_set_probe_interval(ofconn->rconn, probe_interval);
1116 ofconn_set_rate_limit(ofconn, c->rate_limit, c->burst_limit);
1118 /* If dscp value changed reconnect. */
1119 if (c->dscp != rconn_get_dscp(ofconn->rconn)) {
1120 rconn_set_dscp(ofconn->rconn, c->dscp);
1121 rconn_reconnect(ofconn->rconn);
1125 /* Returns true if it makes sense for 'ofconn' to receive and process OpenFlow
1128 ofconn_may_recv(const struct ofconn *ofconn)
1130 int count = rconn_packet_counter_read (ofconn->reply_counter);
1131 return (!ofconn->blocked || ofconn->retry) && count < OFCONN_REPLY_MAX;
1135 ofconn_run(struct ofconn *ofconn,
1136 bool (*handle_openflow)(struct ofconn *, struct ofpbuf *ofp_msg))
1138 struct connmgr *mgr = ofconn->connmgr;
1141 for (i = 0; i < N_SCHEDULERS; i++) {
1142 pinsched_run(ofconn->schedulers[i], do_send_packet_in, ofconn);
1145 rconn_run(ofconn->rconn);
1147 if (handle_openflow) {
1148 /* Limit the number of iterations to avoid starving other tasks. */
1149 for (i = 0; i < 50 && ofconn_may_recv(ofconn); i++) {
1150 struct ofpbuf *of_msg;
1152 of_msg = (ofconn->blocked
1154 : rconn_recv(ofconn->rconn));
1158 if (mgr->fail_open) {
1159 fail_open_maybe_recover(mgr->fail_open);
1162 if (handle_openflow(ofconn, of_msg)) {
1163 ofpbuf_delete(of_msg);
1164 ofconn->blocked = NULL;
1166 ofconn->blocked = of_msg;
1167 ofconn->retry = false;
1172 if (!rconn_is_alive(ofconn->rconn)) {
1173 ofconn_destroy(ofconn);
1174 } else if (!rconn_is_connected(ofconn->rconn)) {
1175 ofconn_flush(ofconn);
1180 ofconn_wait(struct ofconn *ofconn, bool handling_openflow)
1184 for (i = 0; i < N_SCHEDULERS; i++) {
1185 pinsched_wait(ofconn->schedulers[i]);
1187 rconn_run_wait(ofconn->rconn);
1188 if (handling_openflow && ofconn_may_recv(ofconn)) {
1189 rconn_recv_wait(ofconn->rconn);
1193 /* Returns true if 'ofconn' should receive asynchronous messages of the given
1194 * OAM_* 'type' and 'reason', which should be a OFPR_* value for OAM_PACKET_IN,
1195 * a OFPPR_* value for OAM_PORT_STATUS, or an OFPRR_* value for
1196 * OAM_FLOW_REMOVED. Returns false if the message should not be sent on
1199 ofconn_receives_async_msg(const struct ofconn *ofconn,
1200 enum ofconn_async_msg_type type,
1201 unsigned int reason)
1203 const uint32_t *async_config;
1205 assert(reason < 32);
1206 assert((unsigned int) type < OAM_N_TYPES);
1208 if (!rconn_is_connected(ofconn->rconn)) {
1212 /* Keep the following code in sync with the documentation in the
1213 * "Asynchronous Messages" section in DESIGN. */
1215 if (ofconn->type == OFCONN_SERVICE && !ofconn->miss_send_len) {
1216 /* Service connections don't get asynchronous messages unless they have
1217 * explicitly asked for them by setting a nonzero miss send length. */
1221 async_config = (ofconn->role == NX_ROLE_SLAVE
1222 ? ofconn->slave_async_config
1223 : ofconn->master_async_config);
1224 if (!(async_config[type] & (1u << reason))) {
1231 /* Returns a human-readable name for an OpenFlow connection between 'mgr' and
1232 * 'target', suitable for use in log messages for identifying the connection.
1234 * The name is dynamically allocated. The caller should free it (with free())
1235 * when it is no longer needed. */
1237 ofconn_make_name(const struct connmgr *mgr, const char *target)
1239 return xasprintf("%s<->%s", mgr->name, target);
1243 ofconn_set_rate_limit(struct ofconn *ofconn, int rate, int burst)
1247 for (i = 0; i < N_SCHEDULERS; i++) {
1248 struct pinsched **s = &ofconn->schedulers[i];
1252 *s = pinsched_create(rate, burst);
1254 pinsched_set_limits(*s, rate, burst);
1257 pinsched_destroy(*s);
1264 ofconn_send(const struct ofconn *ofconn, struct ofpbuf *msg,
1265 struct rconn_packet_counter *counter)
1267 update_openflow_length(msg);
1268 rconn_send(ofconn->rconn, msg, counter);
1271 /* Sending asynchronous messages. */
1273 static void schedule_packet_in(struct ofconn *, struct ofputil_packet_in);
1275 /* Sends an OFPT_PORT_STATUS message with 'opp' and 'reason' to appropriate
1276 * controllers managed by 'mgr'. */
1278 connmgr_send_port_status(struct connmgr *mgr,
1279 const struct ofputil_phy_port *pp, uint8_t reason)
1281 /* XXX Should limit the number of queued port status change messages. */
1282 struct ofputil_port_status ps;
1283 struct ofconn *ofconn;
1287 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1288 if (ofconn_receives_async_msg(ofconn, OAM_PORT_STATUS, reason)) {
1291 msg = ofputil_encode_port_status(&ps, ofconn->protocol);
1292 ofconn_send(ofconn, msg, NULL);
1297 /* Sends an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message based on 'fr' to
1298 * appropriate controllers managed by 'mgr'. */
1300 connmgr_send_flow_removed(struct connmgr *mgr,
1301 const struct ofputil_flow_removed *fr)
1303 struct ofconn *ofconn;
1305 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1306 if (ofconn_receives_async_msg(ofconn, OAM_FLOW_REMOVED, fr->reason)) {
1309 /* Account flow expirations as replies to OpenFlow requests. That
1310 * works because preventing OpenFlow requests from being processed
1311 * also prevents new flows from being added (and expiring). (It
1312 * also prevents processing OpenFlow requests that would not add
1313 * new flows, so it is imperfect.) */
1314 msg = ofputil_encode_flow_removed(fr, ofconn->protocol);
1315 ofconn_send_reply(ofconn, msg);
1320 /* Given 'pin', sends an OFPT_PACKET_IN message to each OpenFlow controller as
1321 * necessary according to their individual configurations.
1323 * The caller doesn't need to fill in pin->buffer_id or pin->total_len. */
1325 connmgr_send_packet_in(struct connmgr *mgr,
1326 const struct ofputil_packet_in *pin)
1328 struct ofconn *ofconn;
1330 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1331 if (ofconn_receives_async_msg(ofconn, OAM_PACKET_IN, pin->reason)
1332 && ofconn->controller_id == pin->controller_id) {
1333 schedule_packet_in(ofconn, *pin);
1338 /* pinsched callback for sending 'ofp_packet_in' on 'ofconn'. */
1340 do_send_packet_in(struct ofpbuf *ofp_packet_in, void *ofconn_)
1342 struct ofconn *ofconn = ofconn_;
1344 rconn_send_with_limit(ofconn->rconn, ofp_packet_in,
1345 ofconn->packet_in_counter, 100);
1348 /* Takes 'pin', composes an OpenFlow packet-in message from it, and passes it
1349 * to 'ofconn''s packet scheduler for sending. */
1351 schedule_packet_in(struct ofconn *ofconn, struct ofputil_packet_in pin)
1353 struct connmgr *mgr = ofconn->connmgr;
1355 pin.total_len = pin.packet_len;
1357 /* Get OpenFlow buffer_id. */
1358 if (pin.reason == OFPR_ACTION) {
1359 pin.buffer_id = UINT32_MAX;
1360 } else if (mgr->fail_open && fail_open_is_active(mgr->fail_open)) {
1361 pin.buffer_id = pktbuf_get_null();
1362 } else if (!ofconn->pktbuf) {
1363 pin.buffer_id = UINT32_MAX;
1365 pin.buffer_id = pktbuf_save(ofconn->pktbuf, pin.packet, pin.packet_len,
1369 /* Figure out how much of the packet to send. */
1370 if (pin.reason == OFPR_NO_MATCH) {
1371 pin.send_len = pin.packet_len;
1373 /* Caller should have initialized 'send_len' to 'max_len' specified in
1374 * struct ofp_action_output. */
1376 if (pin.buffer_id != UINT32_MAX) {
1377 pin.send_len = MIN(pin.send_len, ofconn->miss_send_len);
1380 /* Make OFPT_PACKET_IN and hand over to packet scheduler. It might
1381 * immediately call into do_send_packet_in() or it might buffer it for a
1382 * while (until a later call to pinsched_run()). */
1383 pinsched_send(ofconn->schedulers[pin.reason == OFPR_NO_MATCH ? 0 : 1],
1385 ofputil_encode_packet_in(&pin, ofconn->packet_in_format),
1386 do_send_packet_in, ofconn);
1389 /* Fail-open settings. */
1391 /* Returns the failure handling mode (OFPROTO_FAIL_SECURE or
1392 * OFPROTO_FAIL_STANDALONE) for 'mgr'. */
1393 enum ofproto_fail_mode
1394 connmgr_get_fail_mode(const struct connmgr *mgr)
1396 return mgr->fail_mode;
1399 /* Sets the failure handling mode for 'mgr' to 'fail_mode' (either
1400 * OFPROTO_FAIL_SECURE or OFPROTO_FAIL_STANDALONE). */
1402 connmgr_set_fail_mode(struct connmgr *mgr, enum ofproto_fail_mode fail_mode)
1404 if (mgr->fail_mode != fail_mode) {
1405 mgr->fail_mode = fail_mode;
1406 update_fail_open(mgr);
1407 if (!connmgr_has_controllers(mgr)) {
1408 ofproto_flush_flows(mgr->ofproto);
1413 /* Fail-open implementation. */
1415 /* Returns the longest probe interval among the primary controllers configured
1416 * on 'mgr'. Returns 0 if there are no primary controllers. */
1418 connmgr_get_max_probe_interval(const struct connmgr *mgr)
1420 const struct ofconn *ofconn;
1421 int max_probe_interval;
1423 max_probe_interval = 0;
1424 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1425 int probe_interval = rconn_get_probe_interval(ofconn->rconn);
1426 max_probe_interval = MAX(max_probe_interval, probe_interval);
1428 return max_probe_interval;
1431 /* Returns the number of seconds for which all of 'mgr's primary controllers
1432 * have been disconnected. Returns 0 if 'mgr' has no primary controllers. */
1434 connmgr_failure_duration(const struct connmgr *mgr)
1436 const struct ofconn *ofconn;
1437 int min_failure_duration;
1439 if (!connmgr_has_controllers(mgr)) {
1443 min_failure_duration = INT_MAX;
1444 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1445 int failure_duration = rconn_failure_duration(ofconn->rconn);
1446 min_failure_duration = MIN(min_failure_duration, failure_duration);
1448 return min_failure_duration;
1451 /* Returns true if at least one primary controller is connected (regardless of
1452 * whether those controllers are believed to have authenticated and accepted
1453 * this switch), false if none of them are connected. */
1455 connmgr_is_any_controller_connected(const struct connmgr *mgr)
1457 const struct ofconn *ofconn;
1459 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1460 if (rconn_is_connected(ofconn->rconn)) {
1467 /* Returns true if at least one primary controller is believed to have
1468 * authenticated and accepted this switch, false otherwise. */
1470 connmgr_is_any_controller_admitted(const struct connmgr *mgr)
1472 const struct ofconn *ofconn;
1474 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1475 if (rconn_is_admitted(ofconn->rconn)) {
1482 /* In-band configuration. */
1484 static bool any_extras_changed(const struct connmgr *,
1485 const struct sockaddr_in *extras, size_t n);
1487 /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'mgr''s
1488 * in-band control should guarantee access, in the same way that in-band
1489 * control guarantees access to OpenFlow controllers. */
1491 connmgr_set_extra_in_band_remotes(struct connmgr *mgr,
1492 const struct sockaddr_in *extras, size_t n)
1494 if (!any_extras_changed(mgr, extras, n)) {
1498 free(mgr->extra_in_band_remotes);
1499 mgr->n_extra_remotes = n;
1500 mgr->extra_in_band_remotes = xmemdup(extras, n * sizeof *extras);
1502 update_in_band_remotes(mgr);
1505 /* Sets the OpenFlow queue used by flows set up by in-band control on
1506 * 'mgr' to 'queue_id'. If 'queue_id' is negative, then in-band control
1507 * flows will use the default queue. */
1509 connmgr_set_in_band_queue(struct connmgr *mgr, int queue_id)
1511 if (queue_id != mgr->in_band_queue) {
1512 mgr->in_band_queue = queue_id;
1513 update_in_band_remotes(mgr);
1518 any_extras_changed(const struct connmgr *mgr,
1519 const struct sockaddr_in *extras, size_t n)
1523 if (n != mgr->n_extra_remotes) {
1527 for (i = 0; i < n; i++) {
1528 const struct sockaddr_in *old = &mgr->extra_in_band_remotes[i];
1529 const struct sockaddr_in *new = &extras[i];
1531 if (old->sin_addr.s_addr != new->sin_addr.s_addr ||
1532 old->sin_port != new->sin_port) {
1540 /* In-band implementation. */
1543 connmgr_msg_in_hook(struct connmgr *mgr, const struct flow *flow,
1544 const struct ofpbuf *packet)
1546 return mgr->in_band && in_band_msg_in_hook(mgr->in_band, flow, packet);
1550 connmgr_may_set_up_flow(struct connmgr *mgr, const struct flow *flow,
1551 const struct nlattr *odp_actions,
1554 return !mgr->in_band || in_band_rule_check(flow, odp_actions, actions_len);
1557 /* Fail-open and in-band implementation. */
1559 /* Called by 'ofproto' after all flows have been flushed, to allow fail-open
1560 * and standalone mode to re-create their flows.
1562 * In-band control has more sophisticated code that manages flows itself. */
1564 connmgr_flushed(struct connmgr *mgr)
1566 if (mgr->fail_open) {
1567 fail_open_flushed(mgr->fail_open);
1570 /* If there are no controllers and we're in standalone mode, set up a flow
1571 * that matches every packet and directs them to OFPP_NORMAL (which goes to
1572 * us). Otherwise, the switch is in secure mode and we won't pass any
1573 * traffic until a controller has been defined and it tells us to do so. */
1574 if (!connmgr_has_controllers(mgr)
1575 && mgr->fail_mode == OFPROTO_FAIL_STANDALONE) {
1576 union ofp_action action;
1577 struct cls_rule rule;
1579 memset(&action, 0, sizeof action);
1580 action.type = htons(OFPAT10_OUTPUT);
1581 action.output.len = htons(sizeof action);
1582 action.output.port = htons(OFPP_NORMAL);
1583 cls_rule_init_catchall(&rule, 0);
1584 ofproto_add_flow(mgr->ofproto, &rule, &action, 1);
1588 /* Creates a new ofservice for 'target' in 'mgr'. Returns 0 if successful,
1589 * otherwise a positive errno value.
1591 * ofservice_reconfigure() must be called to fully configure the new
1594 ofservice_create(struct connmgr *mgr, const char *target, uint8_t dscp)
1596 struct ofservice *ofservice;
1597 struct pvconn *pvconn;
1600 error = pvconn_open(target, &pvconn, dscp);
1605 ofservice = xzalloc(sizeof *ofservice);
1606 hmap_insert(&mgr->services, &ofservice->node, hash_string(target, 0));
1607 ofservice->pvconn = pvconn;
1613 ofservice_destroy(struct connmgr *mgr, struct ofservice *ofservice)
1615 hmap_remove(&mgr->services, &ofservice->node);
1616 pvconn_close(ofservice->pvconn);
1621 ofservice_reconfigure(struct ofservice *ofservice,
1622 const struct ofproto_controller *c)
1624 ofservice->probe_interval = c->probe_interval;
1625 ofservice->rate_limit = c->rate_limit;
1626 ofservice->burst_limit = c->burst_limit;
1627 ofservice->enable_async_msgs = c->enable_async_msgs;
1628 ofservice->dscp = c->dscp;
1631 /* Finds and returns the ofservice within 'mgr' that has the given
1632 * 'target', or a null pointer if none exists. */
1633 static struct ofservice *
1634 ofservice_lookup(struct connmgr *mgr, const char *target)
1636 struct ofservice *ofservice;
1638 HMAP_FOR_EACH_WITH_HASH (ofservice, node, hash_string(target, 0),
1640 if (!strcmp(pvconn_get_name(ofservice->pvconn), target)) {