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"
28 #include "ofp-actions.h"
32 #include "ofproto-provider.h"
34 #include "poll-loop.h"
44 VLOG_DEFINE_THIS_MODULE(connmgr);
45 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
47 /* An OpenFlow connection. */
49 /* Configuration that persists from one connection to the next. */
51 struct list node; /* In struct connmgr's "all_conns" list. */
52 struct hmap_node hmap_node; /* In struct connmgr's "controllers" map. */
54 struct connmgr *connmgr; /* Connection's manager. */
55 struct rconn *rconn; /* OpenFlow connection. */
56 enum ofconn_type type; /* Type. */
57 enum ofproto_band band; /* In-band or out-of-band? */
58 bool enable_async_msgs; /* Initially enable async messages? */
60 /* State that should be cleared from one connection to the next. */
63 enum nx_role role; /* Role. */
64 enum ofputil_protocol protocol; /* Current protocol variant. */
65 enum nx_packet_in_format packet_in_format; /* OFPT_PACKET_IN format. */
67 /* Asynchronous flow table operation support. */
68 struct list opgroups; /* Contains pending "ofopgroups", if any. */
69 struct ofpbuf *blocked; /* Postponed OpenFlow message, if any. */
70 bool retry; /* True if 'blocked' is ready to try again. */
72 /* OFPT_PACKET_IN related data. */
73 struct rconn_packet_counter *packet_in_counter; /* # queued on 'rconn'. */
74 #define N_SCHEDULERS 2
75 struct pinsched *schedulers[N_SCHEDULERS];
76 struct pktbuf *pktbuf; /* OpenFlow packet buffers. */
77 int miss_send_len; /* Bytes to send of buffered packets. */
78 uint16_t controller_id; /* Connection controller ID. */
80 /* Number of OpenFlow messages queued on 'rconn' as replies to OpenFlow
81 * requests, and the maximum number before we stop reading OpenFlow
83 #define OFCONN_REPLY_MAX 100
84 struct rconn_packet_counter *reply_counter;
86 /* Asynchronous message configuration in each possible roles.
88 * A 1-bit enables sending an asynchronous message for one possible reason
89 * that the message might be generated, a 0-bit disables it. */
90 uint32_t master_async_config[OAM_N_TYPES]; /* master, other */
91 uint32_t slave_async_config[OAM_N_TYPES]; /* slave */
94 struct hmap monitors; /* Contains "struct ofmonitor"s. */
95 struct list updates; /* List of "struct ofpbuf"s. */
96 bool sent_abbrev_update; /* Does 'updates' contain NXFME_ABBREV? */
97 struct rconn_packet_counter *monitor_counter;
98 uint64_t monitor_paused;
101 static struct ofconn *ofconn_create(struct connmgr *, struct rconn *,
102 enum ofconn_type, bool enable_async_msgs);
103 static void ofconn_destroy(struct ofconn *);
104 static void ofconn_flush(struct ofconn *);
106 static void ofconn_reconfigure(struct ofconn *,
107 const struct ofproto_controller *);
109 static void ofconn_run(struct ofconn *,
110 bool (*handle_openflow)(struct ofconn *,
111 struct ofpbuf *ofp_msg));
112 static void ofconn_wait(struct ofconn *, bool handling_openflow);
114 static const char *ofconn_get_target(const struct ofconn *);
115 static char *ofconn_make_name(const struct connmgr *, const char *target);
117 static void ofconn_set_rate_limit(struct ofconn *, int rate, int burst);
119 static void ofconn_send(const struct ofconn *, struct ofpbuf *,
120 struct rconn_packet_counter *);
122 static void do_send_packet_in(struct ofpbuf *, void *ofconn_);
124 /* A listener for incoming OpenFlow "service" connections. */
126 struct hmap_node node; /* In struct connmgr's "services" hmap. */
127 struct pvconn *pvconn; /* OpenFlow connection listener. */
129 /* These are not used by ofservice directly. They are settings for
130 * accepted "struct ofconn"s from the pvconn. */
131 int probe_interval; /* Max idle time before probing, in seconds. */
132 int rate_limit; /* Max packet-in rate in packets per second. */
133 int burst_limit; /* Limit on accumulating packet credits. */
134 bool enable_async_msgs; /* Initially enable async messages? */
135 uint8_t dscp; /* DSCP Value for controller connection */
138 static void ofservice_reconfigure(struct ofservice *,
139 const struct ofproto_controller *);
140 static int ofservice_create(struct connmgr *, const char *target, uint8_t dscp);
141 static void ofservice_destroy(struct connmgr *, struct ofservice *);
142 static struct ofservice *ofservice_lookup(struct connmgr *,
145 /* Connection manager for an OpenFlow switch. */
147 struct ofproto *ofproto;
149 char *local_port_name;
151 /* OpenFlow connections. */
152 struct hmap controllers; /* Controller "struct ofconn"s. */
153 struct list all_conns; /* Contains "struct ofconn"s. */
155 /* OpenFlow listeners. */
156 struct hmap services; /* Contains "struct ofservice"s. */
157 struct pvconn **snoops;
161 struct fail_open *fail_open;
162 enum ofproto_fail_mode fail_mode;
164 /* In-band control. */
165 struct in_band *in_band;
166 struct sockaddr_in *extra_in_band_remotes;
167 size_t n_extra_remotes;
171 static void update_in_band_remotes(struct connmgr *);
172 static void add_snooper(struct connmgr *, struct vconn *);
173 static void ofmonitor_run(struct connmgr *);
174 static void ofmonitor_wait(struct connmgr *);
176 /* Creates and returns a new connection manager owned by 'ofproto'. 'name' is
177 * a name for the ofproto suitable for using in log messages.
178 * 'local_port_name' is the name of the local port (OFPP_LOCAL) within
181 connmgr_create(struct ofproto *ofproto,
182 const char *name, const char *local_port_name)
186 mgr = xmalloc(sizeof *mgr);
187 mgr->ofproto = ofproto;
188 mgr->name = xstrdup(name);
189 mgr->local_port_name = xstrdup(local_port_name);
191 hmap_init(&mgr->controllers);
192 list_init(&mgr->all_conns);
194 hmap_init(&mgr->services);
198 mgr->fail_open = NULL;
199 mgr->fail_mode = OFPROTO_FAIL_SECURE;
202 mgr->extra_in_band_remotes = NULL;
203 mgr->n_extra_remotes = 0;
204 mgr->in_band_queue = -1;
209 /* Frees 'mgr' and all of its resources. */
211 connmgr_destroy(struct connmgr *mgr)
213 struct ofservice *ofservice, *next_ofservice;
214 struct ofconn *ofconn, *next_ofconn;
221 LIST_FOR_EACH_SAFE (ofconn, next_ofconn, node, &mgr->all_conns) {
222 ofconn_destroy(ofconn);
224 hmap_destroy(&mgr->controllers);
226 HMAP_FOR_EACH_SAFE (ofservice, next_ofservice, node, &mgr->services) {
227 ofservice_destroy(mgr, ofservice);
229 hmap_destroy(&mgr->services);
231 for (i = 0; i < mgr->n_snoops; i++) {
232 pvconn_close(mgr->snoops[i]);
236 fail_open_destroy(mgr->fail_open);
237 mgr->fail_open = NULL;
239 in_band_destroy(mgr->in_band);
241 free(mgr->extra_in_band_remotes);
243 free(mgr->local_port_name);
248 /* Does all of the periodic maintenance required by 'mgr'.
250 * If 'handle_openflow' is nonnull, calls 'handle_openflow' for each message
251 * received on an OpenFlow connection, passing along the OpenFlow connection
252 * itself and the message that was sent. If 'handle_openflow' returns true,
253 * the message is considered to be fully processed. If 'handle_openflow'
254 * returns false, the message is considered not to have been processed at all;
255 * it will be stored and re-presented to 'handle_openflow' following the next
256 * call to connmgr_retry(). 'handle_openflow' must not modify or free the
259 * If 'handle_openflow' is NULL, no OpenFlow messages will be processed and
260 * other activities that could affect the flow table (in-band processing,
261 * fail-open processing) are suppressed too. */
263 connmgr_run(struct connmgr *mgr,
264 bool (*handle_openflow)(struct ofconn *, struct ofpbuf *ofp_msg))
266 struct ofconn *ofconn, *next_ofconn;
267 struct ofservice *ofservice;
270 if (handle_openflow && mgr->in_band) {
271 if (!in_band_run(mgr->in_band)) {
272 in_band_destroy(mgr->in_band);
277 LIST_FOR_EACH_SAFE (ofconn, next_ofconn, node, &mgr->all_conns) {
278 ofconn_run(ofconn, handle_openflow);
282 /* Fail-open maintenance. Do this after processing the ofconns since
283 * fail-open checks the status of the controller rconn. */
284 if (handle_openflow && mgr->fail_open) {
285 fail_open_run(mgr->fail_open);
288 HMAP_FOR_EACH (ofservice, node, &mgr->services) {
292 retval = pvconn_accept(ofservice->pvconn, OFP10_VERSION, &vconn);
297 /* Passing default value for creation of the rconn */
298 rconn = rconn_create(ofservice->probe_interval, 0, ofservice->dscp);
299 name = ofconn_make_name(mgr, vconn_get_name(vconn));
300 rconn_connect_unreliably(rconn, vconn, name);
303 ofconn = ofconn_create(mgr, rconn, OFCONN_SERVICE,
304 ofservice->enable_async_msgs);
305 ofconn_set_rate_limit(ofconn, ofservice->rate_limit,
306 ofservice->burst_limit);
307 } else if (retval != EAGAIN) {
308 VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval));
312 for (i = 0; i < mgr->n_snoops; i++) {
316 retval = pvconn_accept(mgr->snoops[i], OFP10_VERSION, &vconn);
318 add_snooper(mgr, vconn);
319 } else if (retval != EAGAIN) {
320 VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval));
325 /* Causes the poll loop to wake up when connmgr_run() needs to run.
327 * If 'handling_openflow' is true, arriving OpenFlow messages and other
328 * activities that affect the flow table will wake up the poll loop. If
329 * 'handling_openflow' is false, they will not. */
331 connmgr_wait(struct connmgr *mgr, bool handling_openflow)
333 struct ofservice *ofservice;
334 struct ofconn *ofconn;
337 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
338 ofconn_wait(ofconn, handling_openflow);
341 if (handling_openflow && mgr->in_band) {
342 in_band_wait(mgr->in_band);
344 if (handling_openflow && mgr->fail_open) {
345 fail_open_wait(mgr->fail_open);
347 HMAP_FOR_EACH (ofservice, node, &mgr->services) {
348 pvconn_wait(ofservice->pvconn);
350 for (i = 0; i < mgr->n_snoops; i++) {
351 pvconn_wait(mgr->snoops[i]);
355 /* Adds some memory usage statistics for 'mgr' into 'usage', for use with
356 * memory_report(). */
358 connmgr_get_memory_usage(const struct connmgr *mgr, struct simap *usage)
360 const struct ofconn *ofconn;
361 unsigned int packets = 0;
362 unsigned int ofconns = 0;
364 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
369 packets += rconn_count_txqlen(ofconn->rconn);
370 for (i = 0; i < N_SCHEDULERS; i++) {
371 packets += pinsched_count_txqlen(ofconn->schedulers[i]);
373 packets += pktbuf_count_packets(ofconn->pktbuf);
375 simap_increase(usage, "ofconns", ofconns);
376 simap_increase(usage, "packets", packets);
379 /* Returns the ofproto that owns 'ofconn''s connmgr. */
381 ofconn_get_ofproto(const struct ofconn *ofconn)
383 return ofconn->connmgr->ofproto;
386 /* If processing of OpenFlow messages was blocked on any 'mgr' ofconns by
387 * returning false to the 'handle_openflow' callback to connmgr_run(), this
388 * re-enables them. */
390 connmgr_retry(struct connmgr *mgr)
392 struct ofconn *ofconn;
394 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
395 ofconn->retry = true;
399 /* OpenFlow configuration. */
401 static void add_controller(struct connmgr *, const char *target, uint8_t dscp);
402 static struct ofconn *find_controller_by_target(struct connmgr *,
404 static void update_fail_open(struct connmgr *);
405 static int set_pvconns(struct pvconn ***pvconnsp, size_t *n_pvconnsp,
406 const struct sset *);
408 /* Returns true if 'mgr' has any configured primary controllers.
410 * Service controllers do not count, but configured primary controllers do
411 * count whether or not they are currently connected. */
413 connmgr_has_controllers(const struct connmgr *mgr)
415 return !hmap_is_empty(&mgr->controllers);
418 /* Initializes 'info' and populates it with information about each configured
419 * primary controller. The keys in 'info' are the controllers' targets; the
420 * data values are corresponding "struct ofproto_controller_info".
422 * The caller owns 'info' and everything in it and should free it when it is no
425 connmgr_get_controller_info(struct connmgr *mgr, struct shash *info)
427 const struct ofconn *ofconn;
429 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
430 const struct rconn *rconn = ofconn->rconn;
431 const char *target = rconn_get_target(rconn);
433 if (!shash_find(info, target)) {
434 struct ofproto_controller_info *cinfo = xmalloc(sizeof *cinfo);
435 time_t now = time_now();
436 time_t last_connection = rconn_get_last_connection(rconn);
437 time_t last_disconnect = rconn_get_last_disconnect(rconn);
438 int last_error = rconn_get_last_error(rconn);
440 shash_add(info, target, cinfo);
442 cinfo->is_connected = rconn_is_connected(rconn);
443 cinfo->role = ofconn->role;
448 cinfo->pairs.keys[cinfo->pairs.n] = "last_error";
449 cinfo->pairs.values[cinfo->pairs.n++]
450 = xstrdup(ovs_retval_to_string(last_error));
453 cinfo->pairs.keys[cinfo->pairs.n] = "state";
454 cinfo->pairs.values[cinfo->pairs.n++]
455 = xstrdup(rconn_get_state(rconn));
457 if (last_connection != TIME_MIN) {
458 cinfo->pairs.keys[cinfo->pairs.n] = "sec_since_connect";
459 cinfo->pairs.values[cinfo->pairs.n++]
460 = xasprintf("%ld", (long int) (now - last_connection));
463 if (last_disconnect != TIME_MIN) {
464 cinfo->pairs.keys[cinfo->pairs.n] = "sec_since_disconnect";
465 cinfo->pairs.values[cinfo->pairs.n++]
466 = xasprintf("%ld", (long int) (now - last_disconnect));
473 connmgr_free_controller_info(struct shash *info)
475 struct shash_node *node;
477 SHASH_FOR_EACH (node, info) {
478 struct ofproto_controller_info *cinfo = node->data;
479 while (cinfo->pairs.n) {
480 free(CONST_CAST(char *, cinfo->pairs.values[--cinfo->pairs.n]));
487 /* Changes 'mgr''s set of controllers to the 'n_controllers' controllers in
490 connmgr_set_controllers(struct connmgr *mgr,
491 const struct ofproto_controller *controllers,
492 size_t n_controllers)
494 bool had_controllers = connmgr_has_controllers(mgr);
495 struct shash new_controllers;
496 struct ofconn *ofconn, *next_ofconn;
497 struct ofservice *ofservice, *next_ofservice;
500 /* Create newly configured controllers and services.
501 * Create a name to ofproto_controller mapping in 'new_controllers'. */
502 shash_init(&new_controllers);
503 for (i = 0; i < n_controllers; i++) {
504 const struct ofproto_controller *c = &controllers[i];
506 if (!vconn_verify_name(c->target)) {
507 if (!find_controller_by_target(mgr, c->target)) {
508 VLOG_INFO("%s: added primary controller \"%s\"",
509 mgr->name, c->target);
510 add_controller(mgr, c->target, c->dscp);
512 } else if (!pvconn_verify_name(c->target)) {
513 if (!ofservice_lookup(mgr, c->target)) {
514 VLOG_INFO("%s: added service controller \"%s\"",
515 mgr->name, c->target);
516 ofservice_create(mgr, c->target, c->dscp);
519 VLOG_WARN_RL(&rl, "%s: unsupported controller \"%s\"",
520 mgr->name, c->target);
524 shash_add_once(&new_controllers, c->target, &controllers[i]);
527 /* Delete controllers that are no longer configured.
528 * Update configuration of all now-existing controllers. */
529 HMAP_FOR_EACH_SAFE (ofconn, next_ofconn, hmap_node, &mgr->controllers) {
530 const char *target = ofconn_get_target(ofconn);
531 struct ofproto_controller *c;
533 c = shash_find_data(&new_controllers, target);
535 VLOG_INFO("%s: removed primary controller \"%s\"",
537 ofconn_destroy(ofconn);
539 ofconn_reconfigure(ofconn, c);
543 /* Delete services that are no longer configured.
544 * Update configuration of all now-existing services. */
545 HMAP_FOR_EACH_SAFE (ofservice, next_ofservice, node, &mgr->services) {
546 const char *target = pvconn_get_name(ofservice->pvconn);
547 struct ofproto_controller *c;
549 c = shash_find_data(&new_controllers, target);
551 VLOG_INFO("%s: removed service controller \"%s\"",
553 ofservice_destroy(mgr, ofservice);
555 ofservice_reconfigure(ofservice, c);
559 shash_destroy(&new_controllers);
561 update_in_band_remotes(mgr);
562 update_fail_open(mgr);
563 if (had_controllers != connmgr_has_controllers(mgr)) {
564 ofproto_flush_flows(mgr->ofproto);
568 /* Drops the connections between 'mgr' and all of its primary and secondary
569 * controllers, forcing them to reconnect. */
571 connmgr_reconnect(const struct connmgr *mgr)
573 struct ofconn *ofconn;
575 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
576 rconn_reconnect(ofconn->rconn);
580 /* Sets the "snoops" for 'mgr' to the pvconn targets listed in 'snoops'.
582 * A "snoop" is a pvconn to which every OpenFlow message to or from the most
583 * important controller on 'mgr' is mirrored. */
585 connmgr_set_snoops(struct connmgr *mgr, const struct sset *snoops)
587 return set_pvconns(&mgr->snoops, &mgr->n_snoops, snoops);
590 /* Adds each of the snoops currently configured on 'mgr' to 'snoops'. */
592 connmgr_get_snoops(const struct connmgr *mgr, struct sset *snoops)
596 for (i = 0; i < mgr->n_snoops; i++) {
597 sset_add(snoops, pvconn_get_name(mgr->snoops[i]));
601 /* Returns true if 'mgr' has at least one snoop, false if it has none. */
603 connmgr_has_snoops(const struct connmgr *mgr)
605 return mgr->n_snoops > 0;
608 /* Creates a new controller for 'target' in 'mgr'. update_controller() needs
609 * to be called later to finish the new ofconn's configuration. */
611 add_controller(struct connmgr *mgr, const char *target, uint8_t dscp)
613 char *name = ofconn_make_name(mgr, target);
614 struct ofconn *ofconn;
616 ofconn = ofconn_create(mgr, rconn_create(5, 8, dscp), OFCONN_PRIMARY, true);
617 ofconn->pktbuf = pktbuf_create();
618 rconn_connect(ofconn->rconn, target, name);
619 hmap_insert(&mgr->controllers, &ofconn->hmap_node, hash_string(target, 0));
624 static struct ofconn *
625 find_controller_by_target(struct connmgr *mgr, const char *target)
627 struct ofconn *ofconn;
629 HMAP_FOR_EACH_WITH_HASH (ofconn, hmap_node,
630 hash_string(target, 0), &mgr->controllers) {
631 if (!strcmp(ofconn_get_target(ofconn), target)) {
639 update_in_band_remotes(struct connmgr *mgr)
641 struct sockaddr_in *addrs;
642 size_t max_addrs, n_addrs;
643 struct ofconn *ofconn;
646 /* Allocate enough memory for as many remotes as we could possibly have. */
647 max_addrs = mgr->n_extra_remotes + hmap_count(&mgr->controllers);
648 addrs = xmalloc(max_addrs * sizeof *addrs);
651 /* Add all the remotes. */
652 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
653 struct sockaddr_in *sin = &addrs[n_addrs];
654 const char *target = rconn_get_target(ofconn->rconn);
656 if (ofconn->band == OFPROTO_OUT_OF_BAND) {
660 if (stream_parse_target_with_default_ports(target,
667 for (i = 0; i < mgr->n_extra_remotes; i++) {
668 addrs[n_addrs++] = mgr->extra_in_band_remotes[i];
671 /* Create or update or destroy in-band. */
674 in_band_create(mgr->ofproto, mgr->local_port_name, &mgr->in_band);
676 in_band_set_queue(mgr->in_band, mgr->in_band_queue);
678 /* in_band_run() needs a chance to delete any existing in-band flows.
679 * We will destroy mgr->in_band after it's done with that. */
682 in_band_set_remotes(mgr->in_band, addrs, n_addrs);
690 update_fail_open(struct connmgr *mgr)
692 if (connmgr_has_controllers(mgr)
693 && mgr->fail_mode == OFPROTO_FAIL_STANDALONE) {
694 if (!mgr->fail_open) {
695 mgr->fail_open = fail_open_create(mgr->ofproto, mgr);
698 fail_open_destroy(mgr->fail_open);
699 mgr->fail_open = NULL;
704 set_pvconns(struct pvconn ***pvconnsp, size_t *n_pvconnsp,
705 const struct sset *sset)
707 struct pvconn **pvconns = *pvconnsp;
708 size_t n_pvconns = *n_pvconnsp;
713 for (i = 0; i < n_pvconns; i++) {
714 pvconn_close(pvconns[i]);
718 pvconns = xmalloc(sset_count(sset) * sizeof *pvconns);
720 SSET_FOR_EACH (name, sset) {
721 struct pvconn *pvconn;
724 error = pvconn_open(name, &pvconn, 0);
726 pvconns[n_pvconns++] = pvconn;
728 VLOG_ERR("failed to listen on %s: %s", name, strerror(error));
736 *n_pvconnsp = n_pvconns;
741 /* Returns a "preference level" for snooping 'ofconn'. A higher return value
742 * means that 'ofconn' is more interesting for monitoring than a lower return
745 snoop_preference(const struct ofconn *ofconn)
747 switch (ofconn->role) {
755 /* Shouldn't happen. */
760 /* One of 'mgr''s "snoop" pvconns has accepted a new connection on 'vconn'.
761 * Connects this vconn to a controller. */
763 add_snooper(struct connmgr *mgr, struct vconn *vconn)
765 struct ofconn *ofconn, *best;
767 /* Pick a controller for monitoring. */
769 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
770 if (ofconn->type == OFCONN_PRIMARY
771 && (!best || snoop_preference(ofconn) > snoop_preference(best))) {
777 rconn_add_monitor(best->rconn, vconn);
779 VLOG_INFO_RL(&rl, "no controller connection to snoop");
784 /* Public ofconn functions. */
786 /* Returns the connection type, either OFCONN_PRIMARY or OFCONN_SERVICE. */
788 ofconn_get_type(const struct ofconn *ofconn)
793 /* Returns the role configured for 'ofconn'.
795 * The default role, if no other role has been set, is NX_ROLE_OTHER. */
797 ofconn_get_role(const struct ofconn *ofconn)
802 /* Changes 'ofconn''s role to 'role'. If 'role' is NX_ROLE_MASTER then any
803 * existing master is demoted to a slave. */
805 ofconn_set_role(struct ofconn *ofconn, enum nx_role role)
807 if (role == NX_ROLE_MASTER) {
808 struct ofconn *other;
810 HMAP_FOR_EACH (other, hmap_node, &ofconn->connmgr->controllers) {
811 if (other->role == NX_ROLE_MASTER) {
812 other->role = NX_ROLE_SLAVE;
820 ofconn_set_invalid_ttl_to_controller(struct ofconn *ofconn, bool enable)
822 uint32_t bit = 1u << OFPR_INVALID_TTL;
824 ofconn->master_async_config[OAM_PACKET_IN] |= bit;
826 ofconn->master_async_config[OAM_PACKET_IN] &= ~bit;
831 ofconn_get_invalid_ttl_to_controller(struct ofconn *ofconn)
833 uint32_t bit = 1u << OFPR_INVALID_TTL;
834 return (ofconn->master_async_config[OAM_PACKET_IN] & bit) != 0;
837 /* Returns the currently configured protocol for 'ofconn', one of OFPUTIL_P_*.
839 * The default, if no other format has been set, is OFPUTIL_P_OPENFLOW10. */
840 enum ofputil_protocol
841 ofconn_get_protocol(struct ofconn *ofconn)
843 return ofconn->protocol;
846 /* Sets the protocol for 'ofconn' to 'protocol' (one of OFPUTIL_P_*).
848 * (This doesn't actually send anything to accomplish this. Presumably the
849 * caller already did that.) */
851 ofconn_set_protocol(struct ofconn *ofconn, enum ofputil_protocol protocol)
853 ofconn->protocol = protocol;
856 /* Returns the currently configured packet in format for 'ofconn', one of
859 * The default, if no other format has been set, is NXPIF_OPENFLOW10. */
860 enum nx_packet_in_format
861 ofconn_get_packet_in_format(struct ofconn *ofconn)
863 return ofconn->packet_in_format;
866 /* Sets the packet in format for 'ofconn' to 'packet_in_format' (one of
869 ofconn_set_packet_in_format(struct ofconn *ofconn,
870 enum nx_packet_in_format packet_in_format)
872 ofconn->packet_in_format = packet_in_format;
875 /* Sets the controller connection ID for 'ofconn' to 'controller_id'.
877 * The connection controller ID is used for OFPP_CONTROLLER and
878 * NXAST_CONTROLLER actions. See "struct nx_action_controller" for details. */
880 ofconn_set_controller_id(struct ofconn *ofconn, uint16_t controller_id)
882 ofconn->controller_id = controller_id;
885 /* Returns the default miss send length for 'ofconn'. */
887 ofconn_get_miss_send_len(const struct ofconn *ofconn)
889 return ofconn->miss_send_len;
892 /* Sets the default miss send length for 'ofconn' to 'miss_send_len'. */
894 ofconn_set_miss_send_len(struct ofconn *ofconn, int miss_send_len)
896 ofconn->miss_send_len = miss_send_len;
900 ofconn_set_async_config(struct ofconn *ofconn,
901 const uint32_t master_masks[OAM_N_TYPES],
902 const uint32_t slave_masks[OAM_N_TYPES])
904 size_t size = sizeof ofconn->master_async_config;
905 memcpy(ofconn->master_async_config, master_masks, size);
906 memcpy(ofconn->slave_async_config, slave_masks, size);
909 /* Sends 'msg' on 'ofconn', accounting it as a reply. (If there is a
910 * sufficient number of OpenFlow replies in-flight on a single ofconn, then the
911 * connmgr will stop accepting new OpenFlow requests on that ofconn until the
912 * controller has accepted some of the replies.) */
914 ofconn_send_reply(const struct ofconn *ofconn, struct ofpbuf *msg)
916 ofconn_send(ofconn, msg, ofconn->reply_counter);
919 /* Sends each of the messages in list 'replies' on 'ofconn' in order,
920 * accounting them as replies. */
922 ofconn_send_replies(const struct ofconn *ofconn, struct list *replies)
924 struct ofpbuf *reply, *next;
926 LIST_FOR_EACH_SAFE (reply, next, list_node, replies) {
927 list_remove(&reply->list_node);
928 ofconn_send_reply(ofconn, reply);
932 /* Sends 'error' on 'ofconn', as a reply to 'request'. Only at most the
933 * first 64 bytes of 'request' are used. */
935 ofconn_send_error(const struct ofconn *ofconn,
936 const struct ofp_header *request, enum ofperr error)
938 struct ofpbuf *reply;
940 reply = ofperr_encode_reply(error, request);
942 static struct vlog_rate_limit err_rl = VLOG_RATE_LIMIT_INIT(10, 10);
944 if (!VLOG_DROP_INFO(&err_rl)) {
945 const char *type_name;
949 request_len = ntohs(request->length);
950 type_name = (!ofpraw_decode_partial(&raw, request,
951 MIN(64, request_len))
952 ? ofpraw_get_name(raw)
955 VLOG_INFO("%s: sending %s error reply to %s message",
956 rconn_get_name(ofconn->rconn), ofperr_to_string(error),
959 ofconn_send_reply(ofconn, reply);
963 /* Same as pktbuf_retrieve(), using the pktbuf owned by 'ofconn'. */
965 ofconn_pktbuf_retrieve(struct ofconn *ofconn, uint32_t id,
966 struct ofpbuf **bufferp, uint16_t *in_port)
968 return pktbuf_retrieve(ofconn->pktbuf, id, bufferp, in_port);
971 /* Returns true if 'ofconn' has any pending opgroups. */
973 ofconn_has_pending_opgroups(const struct ofconn *ofconn)
975 return !list_is_empty(&ofconn->opgroups);
978 /* Adds 'ofconn_node' to 'ofconn''s list of pending opgroups.
980 * If 'ofconn' is destroyed or its connection drops, then 'ofconn' will remove
981 * 'ofconn_node' from the list and re-initialize it with list_init(). The
982 * client may, therefore, use list_is_empty(ofconn_node) to determine whether
983 * 'ofconn_node' is still associated with an active ofconn.
985 * The client may also remove ofconn_node from the list itself, with
988 ofconn_add_opgroup(struct ofconn *ofconn, struct list *ofconn_node)
990 list_push_back(&ofconn->opgroups, ofconn_node);
993 /* Private ofconn functions. */
996 ofconn_get_target(const struct ofconn *ofconn)
998 return rconn_get_target(ofconn->rconn);
1001 static struct ofconn *
1002 ofconn_create(struct connmgr *mgr, struct rconn *rconn, enum ofconn_type type,
1003 bool enable_async_msgs)
1005 struct ofconn *ofconn;
1007 ofconn = xzalloc(sizeof *ofconn);
1008 ofconn->connmgr = mgr;
1009 list_push_back(&mgr->all_conns, &ofconn->node);
1010 ofconn->rconn = rconn;
1011 ofconn->type = type;
1012 ofconn->enable_async_msgs = enable_async_msgs;
1014 list_init(&ofconn->opgroups);
1016 hmap_init(&ofconn->monitors);
1017 list_init(&ofconn->updates);
1019 ofconn_flush(ofconn);
1024 /* Clears all of the state in 'ofconn' that should not persist from one
1025 * connection to the next. */
1027 ofconn_flush(struct ofconn *ofconn)
1029 struct ofmonitor *monitor, *next_monitor;
1032 ofconn->role = NX_ROLE_OTHER;
1033 ofconn->protocol = OFPUTIL_P_OF10;
1034 ofconn->packet_in_format = NXPIF_OPENFLOW10;
1036 /* Disassociate 'ofconn' from all of the ofopgroups that it initiated that
1037 * have not yet completed. (Those ofopgroups will still run to completion
1038 * in the usual way, but any errors that they run into will not be reported
1039 * on any OpenFlow channel.)
1041 * Also discard any blocked operation on 'ofconn'. */
1042 while (!list_is_empty(&ofconn->opgroups)) {
1043 list_init(list_pop_front(&ofconn->opgroups));
1045 ofpbuf_delete(ofconn->blocked);
1046 ofconn->blocked = NULL;
1048 rconn_packet_counter_destroy(ofconn->packet_in_counter);
1049 ofconn->packet_in_counter = rconn_packet_counter_create();
1050 for (i = 0; i < N_SCHEDULERS; i++) {
1051 if (ofconn->schedulers[i]) {
1054 pinsched_get_limits(ofconn->schedulers[i], &rate, &burst);
1055 pinsched_destroy(ofconn->schedulers[i]);
1056 ofconn->schedulers[i] = pinsched_create(rate, burst);
1059 if (ofconn->pktbuf) {
1060 pktbuf_destroy(ofconn->pktbuf);
1061 ofconn->pktbuf = pktbuf_create();
1063 ofconn->miss_send_len = (ofconn->type == OFCONN_PRIMARY
1064 ? OFP_DEFAULT_MISS_SEND_LEN
1066 ofconn->controller_id = 0;
1068 rconn_packet_counter_destroy(ofconn->reply_counter);
1069 ofconn->reply_counter = rconn_packet_counter_create();
1071 if (ofconn->enable_async_msgs) {
1072 uint32_t *master = ofconn->master_async_config;
1073 uint32_t *slave = ofconn->slave_async_config;
1075 /* "master" and "other" roles get all asynchronous messages by default,
1076 * except that the controller needs to enable nonstandard "packet-in"
1077 * reasons itself. */
1078 master[OAM_PACKET_IN] = (1u << OFPR_NO_MATCH) | (1u << OFPR_ACTION);
1079 master[OAM_PORT_STATUS] = ((1u << OFPPR_ADD)
1080 | (1u << OFPPR_DELETE)
1081 | (1u << OFPPR_MODIFY));
1082 master[OAM_FLOW_REMOVED] = ((1u << OFPRR_IDLE_TIMEOUT)
1083 | (1u << OFPRR_HARD_TIMEOUT)
1084 | (1u << OFPRR_DELETE));
1086 /* "slave" role gets port status updates by default. */
1087 slave[OAM_PACKET_IN] = 0;
1088 slave[OAM_PORT_STATUS] = ((1u << OFPPR_ADD)
1089 | (1u << OFPPR_DELETE)
1090 | (1u << OFPPR_MODIFY));
1091 slave[OAM_FLOW_REMOVED] = 0;
1093 memset(ofconn->master_async_config, 0,
1094 sizeof ofconn->master_async_config);
1095 memset(ofconn->slave_async_config, 0,
1096 sizeof ofconn->slave_async_config);
1099 HMAP_FOR_EACH_SAFE (monitor, next_monitor, ofconn_node,
1100 &ofconn->monitors) {
1101 ofmonitor_destroy(monitor);
1103 rconn_packet_counter_destroy(ofconn->monitor_counter);
1104 ofconn->monitor_counter = rconn_packet_counter_create();
1105 ofpbuf_list_delete(&ofconn->updates); /* ...but it should be empty. */
1109 ofconn_destroy(struct ofconn *ofconn)
1111 ofconn_flush(ofconn);
1113 if (ofconn->type == OFCONN_PRIMARY) {
1114 hmap_remove(&ofconn->connmgr->controllers, &ofconn->hmap_node);
1117 hmap_destroy(&ofconn->monitors);
1118 list_remove(&ofconn->node);
1119 rconn_destroy(ofconn->rconn);
1120 rconn_packet_counter_destroy(ofconn->packet_in_counter);
1121 rconn_packet_counter_destroy(ofconn->reply_counter);
1122 pktbuf_destroy(ofconn->pktbuf);
1123 rconn_packet_counter_destroy(ofconn->monitor_counter);
1127 /* Reconfigures 'ofconn' to match 'c'. 'ofconn' and 'c' must have the same
1130 ofconn_reconfigure(struct ofconn *ofconn, const struct ofproto_controller *c)
1134 ofconn->band = c->band;
1135 ofconn->enable_async_msgs = c->enable_async_msgs;
1137 rconn_set_max_backoff(ofconn->rconn, c->max_backoff);
1139 probe_interval = c->probe_interval ? MAX(c->probe_interval, 5) : 0;
1140 rconn_set_probe_interval(ofconn->rconn, probe_interval);
1142 ofconn_set_rate_limit(ofconn, c->rate_limit, c->burst_limit);
1144 /* If dscp value changed reconnect. */
1145 if (c->dscp != rconn_get_dscp(ofconn->rconn)) {
1146 rconn_set_dscp(ofconn->rconn, c->dscp);
1147 rconn_reconnect(ofconn->rconn);
1151 /* Returns true if it makes sense for 'ofconn' to receive and process OpenFlow
1154 ofconn_may_recv(const struct ofconn *ofconn)
1156 int count = ofconn->reply_counter->n_packets;
1157 return (!ofconn->blocked || ofconn->retry) && count < OFCONN_REPLY_MAX;
1161 ofconn_run(struct ofconn *ofconn,
1162 bool (*handle_openflow)(struct ofconn *, struct ofpbuf *ofp_msg))
1164 struct connmgr *mgr = ofconn->connmgr;
1167 for (i = 0; i < N_SCHEDULERS; i++) {
1168 pinsched_run(ofconn->schedulers[i], do_send_packet_in, ofconn);
1171 rconn_run(ofconn->rconn);
1173 if (handle_openflow) {
1174 /* Limit the number of iterations to avoid starving other tasks. */
1175 for (i = 0; i < 50 && ofconn_may_recv(ofconn); i++) {
1176 struct ofpbuf *of_msg;
1178 of_msg = (ofconn->blocked
1180 : rconn_recv(ofconn->rconn));
1184 if (mgr->fail_open) {
1185 fail_open_maybe_recover(mgr->fail_open);
1188 if (handle_openflow(ofconn, of_msg)) {
1189 ofpbuf_delete(of_msg);
1190 ofconn->blocked = NULL;
1192 ofconn->blocked = of_msg;
1193 ofconn->retry = false;
1198 if (!rconn_is_alive(ofconn->rconn)) {
1199 ofconn_destroy(ofconn);
1200 } else if (!rconn_is_connected(ofconn->rconn)) {
1201 ofconn_flush(ofconn);
1206 ofconn_wait(struct ofconn *ofconn, bool handling_openflow)
1210 for (i = 0; i < N_SCHEDULERS; i++) {
1211 pinsched_wait(ofconn->schedulers[i]);
1213 rconn_run_wait(ofconn->rconn);
1214 if (handling_openflow && ofconn_may_recv(ofconn)) {
1215 rconn_recv_wait(ofconn->rconn);
1219 /* Returns true if 'ofconn' should receive asynchronous messages of the given
1220 * OAM_* 'type' and 'reason', which should be a OFPR_* value for OAM_PACKET_IN,
1221 * a OFPPR_* value for OAM_PORT_STATUS, or an OFPRR_* value for
1222 * OAM_FLOW_REMOVED. Returns false if the message should not be sent on
1225 ofconn_receives_async_msg(const struct ofconn *ofconn,
1226 enum ofconn_async_msg_type type,
1227 unsigned int reason)
1229 const uint32_t *async_config;
1231 assert(reason < 32);
1232 assert((unsigned int) type < OAM_N_TYPES);
1234 if (!rconn_is_connected(ofconn->rconn)) {
1238 /* Keep the following code in sync with the documentation in the
1239 * "Asynchronous Messages" section in DESIGN. */
1241 if (ofconn->type == OFCONN_SERVICE && !ofconn->miss_send_len) {
1242 /* Service connections don't get asynchronous messages unless they have
1243 * explicitly asked for them by setting a nonzero miss send length. */
1247 async_config = (ofconn->role == NX_ROLE_SLAVE
1248 ? ofconn->slave_async_config
1249 : ofconn->master_async_config);
1250 if (!(async_config[type] & (1u << reason))) {
1257 /* Returns a human-readable name for an OpenFlow connection between 'mgr' and
1258 * 'target', suitable for use in log messages for identifying the connection.
1260 * The name is dynamically allocated. The caller should free it (with free())
1261 * when it is no longer needed. */
1263 ofconn_make_name(const struct connmgr *mgr, const char *target)
1265 return xasprintf("%s<->%s", mgr->name, target);
1269 ofconn_set_rate_limit(struct ofconn *ofconn, int rate, int burst)
1273 for (i = 0; i < N_SCHEDULERS; i++) {
1274 struct pinsched **s = &ofconn->schedulers[i];
1278 *s = pinsched_create(rate, burst);
1280 pinsched_set_limits(*s, rate, burst);
1283 pinsched_destroy(*s);
1290 ofconn_send(const struct ofconn *ofconn, struct ofpbuf *msg,
1291 struct rconn_packet_counter *counter)
1293 ofpmsg_update_length(msg);
1294 rconn_send(ofconn->rconn, msg, counter);
1297 /* Sending asynchronous messages. */
1299 static void schedule_packet_in(struct ofconn *, struct ofputil_packet_in);
1301 /* Sends an OFPT_PORT_STATUS message with 'opp' and 'reason' to appropriate
1302 * controllers managed by 'mgr'. */
1304 connmgr_send_port_status(struct connmgr *mgr,
1305 const struct ofputil_phy_port *pp, uint8_t reason)
1307 /* XXX Should limit the number of queued port status change messages. */
1308 struct ofputil_port_status ps;
1309 struct ofconn *ofconn;
1313 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1314 if (ofconn_receives_async_msg(ofconn, OAM_PORT_STATUS, reason)) {
1317 msg = ofputil_encode_port_status(&ps, ofconn->protocol);
1318 ofconn_send(ofconn, msg, NULL);
1323 /* Sends an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message based on 'fr' to
1324 * appropriate controllers managed by 'mgr'. */
1326 connmgr_send_flow_removed(struct connmgr *mgr,
1327 const struct ofputil_flow_removed *fr)
1329 struct ofconn *ofconn;
1331 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1332 if (ofconn_receives_async_msg(ofconn, OAM_FLOW_REMOVED, fr->reason)) {
1335 /* Account flow expirations as replies to OpenFlow requests. That
1336 * works because preventing OpenFlow requests from being processed
1337 * also prevents new flows from being added (and expiring). (It
1338 * also prevents processing OpenFlow requests that would not add
1339 * new flows, so it is imperfect.) */
1340 msg = ofputil_encode_flow_removed(fr, ofconn->protocol);
1341 ofconn_send_reply(ofconn, msg);
1346 /* Given 'pin', sends an OFPT_PACKET_IN message to each OpenFlow controller as
1347 * necessary according to their individual configurations.
1349 * The caller doesn't need to fill in pin->buffer_id or pin->total_len. */
1351 connmgr_send_packet_in(struct connmgr *mgr,
1352 const struct ofputil_packet_in *pin)
1354 struct ofconn *ofconn;
1356 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1357 if (ofconn_receives_async_msg(ofconn, OAM_PACKET_IN, pin->reason)
1358 && ofconn->controller_id == pin->controller_id) {
1359 schedule_packet_in(ofconn, *pin);
1364 /* pinsched callback for sending 'ofp_packet_in' on 'ofconn'. */
1366 do_send_packet_in(struct ofpbuf *ofp_packet_in, void *ofconn_)
1368 struct ofconn *ofconn = ofconn_;
1370 rconn_send_with_limit(ofconn->rconn, ofp_packet_in,
1371 ofconn->packet_in_counter, 100);
1374 /* Takes 'pin', composes an OpenFlow packet-in message from it, and passes it
1375 * to 'ofconn''s packet scheduler for sending. */
1377 schedule_packet_in(struct ofconn *ofconn, struct ofputil_packet_in pin)
1379 struct connmgr *mgr = ofconn->connmgr;
1381 pin.total_len = pin.packet_len;
1383 /* Get OpenFlow buffer_id. */
1384 if (pin.reason == OFPR_ACTION) {
1385 pin.buffer_id = UINT32_MAX;
1386 } else if (mgr->fail_open && fail_open_is_active(mgr->fail_open)) {
1387 pin.buffer_id = pktbuf_get_null();
1388 } else if (!ofconn->pktbuf) {
1389 pin.buffer_id = UINT32_MAX;
1391 pin.buffer_id = pktbuf_save(ofconn->pktbuf, pin.packet, pin.packet_len,
1395 /* Figure out how much of the packet to send. */
1396 if (pin.reason == OFPR_NO_MATCH) {
1397 pin.send_len = pin.packet_len;
1399 /* Caller should have initialized 'send_len' to 'max_len' specified in
1402 if (pin.buffer_id != UINT32_MAX) {
1403 pin.send_len = MIN(pin.send_len, ofconn->miss_send_len);
1406 /* Make OFPT_PACKET_IN and hand over to packet scheduler. It might
1407 * immediately call into do_send_packet_in() or it might buffer it for a
1408 * while (until a later call to pinsched_run()). */
1409 pinsched_send(ofconn->schedulers[pin.reason == OFPR_NO_MATCH ? 0 : 1],
1411 ofputil_encode_packet_in(&pin, ofconn->protocol,
1412 ofconn->packet_in_format),
1413 do_send_packet_in, ofconn);
1416 /* Fail-open settings. */
1418 /* Returns the failure handling mode (OFPROTO_FAIL_SECURE or
1419 * OFPROTO_FAIL_STANDALONE) for 'mgr'. */
1420 enum ofproto_fail_mode
1421 connmgr_get_fail_mode(const struct connmgr *mgr)
1423 return mgr->fail_mode;
1426 /* Sets the failure handling mode for 'mgr' to 'fail_mode' (either
1427 * OFPROTO_FAIL_SECURE or OFPROTO_FAIL_STANDALONE). */
1429 connmgr_set_fail_mode(struct connmgr *mgr, enum ofproto_fail_mode fail_mode)
1431 if (mgr->fail_mode != fail_mode) {
1432 mgr->fail_mode = fail_mode;
1433 update_fail_open(mgr);
1434 if (!connmgr_has_controllers(mgr)) {
1435 ofproto_flush_flows(mgr->ofproto);
1440 /* Fail-open implementation. */
1442 /* Returns the longest probe interval among the primary controllers configured
1443 * on 'mgr'. Returns 0 if there are no primary controllers. */
1445 connmgr_get_max_probe_interval(const struct connmgr *mgr)
1447 const struct ofconn *ofconn;
1448 int max_probe_interval;
1450 max_probe_interval = 0;
1451 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1452 int probe_interval = rconn_get_probe_interval(ofconn->rconn);
1453 max_probe_interval = MAX(max_probe_interval, probe_interval);
1455 return max_probe_interval;
1458 /* Returns the number of seconds for which all of 'mgr's primary controllers
1459 * have been disconnected. Returns 0 if 'mgr' has no primary controllers. */
1461 connmgr_failure_duration(const struct connmgr *mgr)
1463 const struct ofconn *ofconn;
1464 int min_failure_duration;
1466 if (!connmgr_has_controllers(mgr)) {
1470 min_failure_duration = INT_MAX;
1471 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1472 int failure_duration = rconn_failure_duration(ofconn->rconn);
1473 min_failure_duration = MIN(min_failure_duration, failure_duration);
1475 return min_failure_duration;
1478 /* Returns true if at least one primary controller is connected (regardless of
1479 * whether those controllers are believed to have authenticated and accepted
1480 * this switch), false if none of them are connected. */
1482 connmgr_is_any_controller_connected(const struct connmgr *mgr)
1484 const struct ofconn *ofconn;
1486 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1487 if (rconn_is_connected(ofconn->rconn)) {
1494 /* Returns true if at least one primary controller is believed to have
1495 * authenticated and accepted this switch, false otherwise. */
1497 connmgr_is_any_controller_admitted(const struct connmgr *mgr)
1499 const struct ofconn *ofconn;
1501 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1502 if (rconn_is_admitted(ofconn->rconn)) {
1509 /* In-band configuration. */
1511 static bool any_extras_changed(const struct connmgr *,
1512 const struct sockaddr_in *extras, size_t n);
1514 /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'mgr''s
1515 * in-band control should guarantee access, in the same way that in-band
1516 * control guarantees access to OpenFlow controllers. */
1518 connmgr_set_extra_in_band_remotes(struct connmgr *mgr,
1519 const struct sockaddr_in *extras, size_t n)
1521 if (!any_extras_changed(mgr, extras, n)) {
1525 free(mgr->extra_in_band_remotes);
1526 mgr->n_extra_remotes = n;
1527 mgr->extra_in_band_remotes = xmemdup(extras, n * sizeof *extras);
1529 update_in_band_remotes(mgr);
1532 /* Sets the OpenFlow queue used by flows set up by in-band control on
1533 * 'mgr' to 'queue_id'. If 'queue_id' is negative, then in-band control
1534 * flows will use the default queue. */
1536 connmgr_set_in_band_queue(struct connmgr *mgr, int queue_id)
1538 if (queue_id != mgr->in_band_queue) {
1539 mgr->in_band_queue = queue_id;
1540 update_in_band_remotes(mgr);
1545 any_extras_changed(const struct connmgr *mgr,
1546 const struct sockaddr_in *extras, size_t n)
1550 if (n != mgr->n_extra_remotes) {
1554 for (i = 0; i < n; i++) {
1555 const struct sockaddr_in *old = &mgr->extra_in_band_remotes[i];
1556 const struct sockaddr_in *new = &extras[i];
1558 if (old->sin_addr.s_addr != new->sin_addr.s_addr ||
1559 old->sin_port != new->sin_port) {
1567 /* In-band implementation. */
1570 connmgr_msg_in_hook(struct connmgr *mgr, const struct flow *flow,
1571 const struct ofpbuf *packet)
1573 return mgr->in_band && in_band_msg_in_hook(mgr->in_band, flow, packet);
1577 connmgr_may_set_up_flow(struct connmgr *mgr, const struct flow *flow,
1578 const struct nlattr *odp_actions,
1581 return !mgr->in_band || in_band_rule_check(flow, odp_actions, actions_len);
1584 /* Fail-open and in-band implementation. */
1586 /* Called by 'ofproto' after all flows have been flushed, to allow fail-open
1587 * and standalone mode to re-create their flows.
1589 * In-band control has more sophisticated code that manages flows itself. */
1591 connmgr_flushed(struct connmgr *mgr)
1593 if (mgr->fail_open) {
1594 fail_open_flushed(mgr->fail_open);
1597 /* If there are no controllers and we're in standalone mode, set up a flow
1598 * that matches every packet and directs them to OFPP_NORMAL (which goes to
1599 * us). Otherwise, the switch is in secure mode and we won't pass any
1600 * traffic until a controller has been defined and it tells us to do so. */
1601 if (!connmgr_has_controllers(mgr)
1602 && mgr->fail_mode == OFPROTO_FAIL_STANDALONE) {
1603 struct ofpbuf ofpacts;
1606 ofpbuf_init(&ofpacts, OFPACT_OUTPUT_SIZE);
1607 ofpact_put_OUTPUT(&ofpacts)->port = OFPP_NORMAL;
1608 ofpact_pad(&ofpacts);
1610 match_init_catchall(&match);
1611 ofproto_add_flow(mgr->ofproto, &match, 0, ofpacts.data, ofpacts.size);
1613 ofpbuf_uninit(&ofpacts);
1617 /* Creates a new ofservice for 'target' in 'mgr'. Returns 0 if successful,
1618 * otherwise a positive errno value.
1620 * ofservice_reconfigure() must be called to fully configure the new
1623 ofservice_create(struct connmgr *mgr, const char *target, uint8_t dscp)
1625 struct ofservice *ofservice;
1626 struct pvconn *pvconn;
1629 error = pvconn_open(target, &pvconn, dscp);
1634 ofservice = xzalloc(sizeof *ofservice);
1635 hmap_insert(&mgr->services, &ofservice->node, hash_string(target, 0));
1636 ofservice->pvconn = pvconn;
1642 ofservice_destroy(struct connmgr *mgr, struct ofservice *ofservice)
1644 hmap_remove(&mgr->services, &ofservice->node);
1645 pvconn_close(ofservice->pvconn);
1650 ofservice_reconfigure(struct ofservice *ofservice,
1651 const struct ofproto_controller *c)
1653 ofservice->probe_interval = c->probe_interval;
1654 ofservice->rate_limit = c->rate_limit;
1655 ofservice->burst_limit = c->burst_limit;
1656 ofservice->enable_async_msgs = c->enable_async_msgs;
1657 ofservice->dscp = c->dscp;
1660 /* Finds and returns the ofservice within 'mgr' that has the given
1661 * 'target', or a null pointer if none exists. */
1662 static struct ofservice *
1663 ofservice_lookup(struct connmgr *mgr, const char *target)
1665 struct ofservice *ofservice;
1667 HMAP_FOR_EACH_WITH_HASH (ofservice, node, hash_string(target, 0),
1669 if (!strcmp(pvconn_get_name(ofservice->pvconn), target)) {
1676 /* Flow monitors (NXST_FLOW_MONITOR). */
1678 /* A counter incremented when something significant happens to an OpenFlow
1681 * - When a rule is added, its 'add_seqno' and 'modify_seqno' are set to
1682 * the current value (which is then incremented).
1684 * - When a rule is modified, its 'modify_seqno' is set to the current
1685 * value (which is then incremented).
1687 * Thus, by comparing an old value of monitor_seqno against a rule's
1688 * 'add_seqno', one can tell whether the rule was added before or after the old
1689 * value was read, and similarly for 'modify_seqno'.
1691 * 32 bits should normally be sufficient (and would be nice, to save space in
1692 * each rule) but then we'd have to have some special cases for wraparound.
1694 * We initialize monitor_seqno to 1 to allow 0 to be used as an invalid
1696 static uint64_t monitor_seqno = 1;
1698 COVERAGE_DEFINE(ofmonitor_pause);
1699 COVERAGE_DEFINE(ofmonitor_resume);
1702 ofmonitor_create(const struct ofputil_flow_monitor_request *request,
1703 struct ofconn *ofconn, struct ofmonitor **monitorp)
1705 struct ofmonitor *m;
1709 m = ofmonitor_lookup(ofconn, request->id);
1711 return OFPERR_NXBRC_FM_DUPLICATE_ID;
1714 m = xmalloc(sizeof *m);
1716 hmap_insert(&ofconn->monitors, &m->ofconn_node, hash_int(request->id, 0));
1717 m->id = request->id;
1718 m->flags = request->flags;
1719 m->out_port = request->out_port;
1720 m->table_id = request->table_id;
1721 minimatch_init(&m->match, &request->match);
1728 ofmonitor_lookup(struct ofconn *ofconn, uint32_t id)
1730 struct ofmonitor *m;
1732 HMAP_FOR_EACH_IN_BUCKET (m, ofconn_node, hash_int(id, 0),
1733 &ofconn->monitors) {
1742 ofmonitor_destroy(struct ofmonitor *m)
1745 hmap_remove(&m->ofconn->monitors, &m->ofconn_node);
1751 ofmonitor_report(struct connmgr *mgr, struct rule *rule,
1752 enum nx_flow_update_event event,
1753 enum ofp_flow_removed_reason reason,
1754 const struct ofconn *abbrev_ofconn, ovs_be32 abbrev_xid)
1756 enum nx_flow_monitor_flags update;
1757 struct ofconn *ofconn;
1762 rule->add_seqno = rule->modify_seqno = monitor_seqno++;
1766 update = NXFMF_DELETE;
1769 case NXFME_MODIFIED:
1770 update = NXFMF_MODIFY;
1771 rule->modify_seqno = monitor_seqno++;
1779 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1780 enum nx_flow_monitor_flags flags = 0;
1781 struct ofmonitor *m;
1783 if (ofconn->monitor_paused) {
1784 /* Only send NXFME_DELETED notifications for flows that were added
1785 * before we paused. */
1786 if (event != NXFME_DELETED
1787 || rule->add_seqno > ofconn->monitor_paused) {
1792 HMAP_FOR_EACH (m, ofconn_node, &ofconn->monitors) {
1793 if (m->flags & update
1794 && (m->table_id == 0xff || m->table_id == rule->table_id)
1795 && ofoperation_has_out_port(rule->pending, m->out_port)
1796 && cls_rule_is_loose_match(&rule->cr, &m->match)) {
1802 if (list_is_empty(&ofconn->updates)) {
1803 ofputil_start_flow_update(&ofconn->updates);
1804 ofconn->sent_abbrev_update = false;
1807 if (ofconn != abbrev_ofconn || ofconn->monitor_paused) {
1808 struct ofputil_flow_update fu;
1812 fu.reason = event == NXFME_DELETED ? reason : 0;
1813 fu.idle_timeout = rule->idle_timeout;
1814 fu.hard_timeout = rule->hard_timeout;
1815 fu.table_id = rule->table_id;
1816 fu.cookie = rule->flow_cookie;
1817 minimatch_expand(&rule->cr.match, &match);
1819 fu.priority = rule->cr.priority;
1820 if (flags & NXFMF_ACTIONS) {
1821 fu.ofpacts = rule->ofpacts;
1822 fu.ofpacts_len = rule->ofpacts_len;
1827 ofputil_append_flow_update(&fu, &ofconn->updates);
1828 } else if (!ofconn->sent_abbrev_update) {
1829 struct ofputil_flow_update fu;
1831 fu.event = NXFME_ABBREV;
1832 fu.xid = abbrev_xid;
1833 ofputil_append_flow_update(&fu, &ofconn->updates);
1835 ofconn->sent_abbrev_update = true;
1842 ofmonitor_flush(struct connmgr *mgr)
1844 struct ofconn *ofconn;
1846 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1847 struct ofpbuf *msg, *next;
1849 LIST_FOR_EACH_SAFE (msg, next, list_node, &ofconn->updates) {
1850 list_remove(&msg->list_node);
1851 ofconn_send(ofconn, msg, ofconn->monitor_counter);
1852 if (!ofconn->monitor_paused
1853 && ofconn->monitor_counter->n_bytes > 128 * 1024) {
1854 struct ofpbuf *pause;
1856 COVERAGE_INC(ofmonitor_pause);
1857 ofconn->monitor_paused = monitor_seqno++;
1858 pause = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_MONITOR_PAUSED,
1859 OFP10_VERSION, htonl(0), 0);
1860 ofconn_send(ofconn, pause, ofconn->monitor_counter);
1867 ofmonitor_resume(struct ofconn *ofconn)
1869 struct ofpbuf *resumed;
1870 struct ofmonitor *m;
1875 HMAP_FOR_EACH (m, ofconn_node, &ofconn->monitors) {
1876 ofmonitor_collect_resume_rules(m, ofconn->monitor_paused, &rules);
1880 ofmonitor_compose_refresh_updates(&rules, &msgs);
1882 resumed = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_MONITOR_RESUMED, OFP10_VERSION,
1884 list_push_back(&msgs, &resumed->list_node);
1885 ofconn_send_replies(ofconn, &msgs);
1887 ofconn->monitor_paused = 0;
1891 ofmonitor_run(struct connmgr *mgr)
1893 struct ofconn *ofconn;
1895 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1896 if (ofconn->monitor_paused && !ofconn->monitor_counter->n_packets) {
1897 COVERAGE_INC(ofmonitor_resume);
1898 ofmonitor_resume(ofconn);
1904 ofmonitor_wait(struct connmgr *mgr)
1906 struct ofconn *ofconn;
1908 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1909 if (ofconn->monitor_paused && !ofconn->monitor_counter->n_packets) {
1910 poll_immediate_wake();