2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 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"
46 VLOG_DEFINE_THIS_MODULE(connmgr);
47 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
49 /* An OpenFlow connection.
55 * 'ofproto_mutex' must be held whenever an ofconn is created or destroyed or,
56 * more or less equivalently, whenever an ofconn is added to or removed from a
57 * connmgr. 'ofproto_mutex' doesn't protect the data inside the ofconn, except
58 * as specifically noted below. */
60 /* Configuration that persists from one connection to the next. */
62 struct list node; /* In struct connmgr's "all_conns" list. */
63 struct hmap_node hmap_node; /* In struct connmgr's "controllers" map. */
65 struct connmgr *connmgr; /* Connection's manager. */
66 struct rconn *rconn; /* OpenFlow connection. */
67 enum ofconn_type type; /* Type. */
68 enum ofproto_band band; /* In-band or out-of-band? */
69 bool enable_async_msgs; /* Initially enable async messages? */
71 /* State that should be cleared from one connection to the next. */
74 enum ofp12_controller_role role; /* Role. */
75 enum ofputil_protocol protocol; /* Current protocol variant. */
76 enum nx_packet_in_format packet_in_format; /* OFPT_PACKET_IN format. */
78 /* Asynchronous flow table operation support. */
79 struct list opgroups; /* Contains pending "ofopgroups", if any. */
80 struct ofpbuf *blocked; /* Postponed OpenFlow message, if any. */
81 bool retry; /* True if 'blocked' is ready to try again. */
83 /* OFPT_PACKET_IN related data. */
84 struct rconn_packet_counter *packet_in_counter; /* # queued on 'rconn'. */
85 #define N_SCHEDULERS 2
86 struct pinsched *schedulers[N_SCHEDULERS];
87 struct pktbuf *pktbuf; /* OpenFlow packet buffers. */
88 int miss_send_len; /* Bytes to send of buffered packets. */
89 uint16_t controller_id; /* Connection controller ID. */
91 /* Number of OpenFlow messages queued on 'rconn' as replies to OpenFlow
92 * requests, and the maximum number before we stop reading OpenFlow
94 #define OFCONN_REPLY_MAX 100
95 struct rconn_packet_counter *reply_counter;
97 /* Asynchronous message configuration in each possible roles.
99 * A 1-bit enables sending an asynchronous message for one possible reason
100 * that the message might be generated, a 0-bit disables it. */
101 uint32_t master_async_config[OAM_N_TYPES]; /* master, other */
102 uint32_t slave_async_config[OAM_N_TYPES]; /* slave */
104 /* Flow monitors (e.g. NXST_FLOW_MONITOR). */
106 /* Configuration. Contains "struct ofmonitor"s. */
107 struct hmap monitors OVS_GUARDED_BY(ofproto_mutex);
111 * When too many flow monitor notifications back up in the transmit buffer,
112 * we pause the transmission of further notifications. These members track
113 * the flow control state.
115 * When notifications are flowing, 'monitor_paused' is 0. When
116 * notifications are paused, 'monitor_paused' is the value of
117 * 'monitor_seqno' at the point we paused.
119 * 'monitor_counter' counts the OpenFlow messages and bytes currently in
120 * flight. This value growing too large triggers pausing. */
121 uint64_t monitor_paused OVS_GUARDED_BY(ofproto_mutex);
122 struct rconn_packet_counter *monitor_counter OVS_GUARDED_BY(ofproto_mutex);
124 /* State of monitors for a single ongoing flow_mod.
126 * 'updates' is a list of "struct ofpbuf"s that contain
127 * NXST_FLOW_MONITOR_REPLY messages representing the changes made by the
130 * When 'updates' is nonempty, 'sent_abbrev_update' is true if 'updates'
131 * contains an update event of type NXFME_ABBREV and false otherwise.. */
132 struct list updates OVS_GUARDED_BY(ofproto_mutex);
133 bool sent_abbrev_update OVS_GUARDED_BY(ofproto_mutex);
135 /* Active bundles. Contains "struct ofp_bundle"s. */
139 static struct ofconn *ofconn_create(struct connmgr *, struct rconn *,
140 enum ofconn_type, bool enable_async_msgs)
141 OVS_REQUIRES(ofproto_mutex);
142 static void ofconn_destroy(struct ofconn *) OVS_REQUIRES(ofproto_mutex);
143 static void ofconn_flush(struct ofconn *) OVS_REQUIRES(ofproto_mutex);
145 static void ofconn_reconfigure(struct ofconn *,
146 const struct ofproto_controller *);
148 static void ofconn_run(struct ofconn *,
149 bool (*handle_openflow)(struct ofconn *,
150 const struct ofpbuf *ofp_msg));
151 static void ofconn_wait(struct ofconn *, bool handling_openflow);
153 static const char *ofconn_get_target(const struct ofconn *);
154 static char *ofconn_make_name(const struct connmgr *, const char *target);
156 static void ofconn_set_rate_limit(struct ofconn *, int rate, int burst);
158 static void ofconn_send(const struct ofconn *, struct ofpbuf *,
159 struct rconn_packet_counter *);
161 static void do_send_packet_ins(struct ofconn *, struct list *txq);
163 /* A listener for incoming OpenFlow "service" connections. */
165 struct hmap_node node; /* In struct connmgr's "services" hmap. */
166 struct pvconn *pvconn; /* OpenFlow connection listener. */
168 /* These are not used by ofservice directly. They are settings for
169 * accepted "struct ofconn"s from the pvconn. */
170 int probe_interval; /* Max idle time before probing, in seconds. */
171 int rate_limit; /* Max packet-in rate in packets per second. */
172 int burst_limit; /* Limit on accumulating packet credits. */
173 bool enable_async_msgs; /* Initially enable async messages? */
174 uint8_t dscp; /* DSCP Value for controller connection */
175 uint32_t allowed_versions; /* OpenFlow protocol versions that may
176 * be negotiated for a session. */
179 static void ofservice_reconfigure(struct ofservice *,
180 const struct ofproto_controller *);
181 static int ofservice_create(struct connmgr *mgr, const char *target,
182 uint32_t allowed_versions, uint8_t dscp);
183 static void ofservice_destroy(struct connmgr *, struct ofservice *);
184 static struct ofservice *ofservice_lookup(struct connmgr *,
187 /* Connection manager for an OpenFlow switch. */
189 struct ofproto *ofproto;
191 char *local_port_name;
193 /* OpenFlow connections. */
194 struct hmap controllers; /* Controller "struct ofconn"s. */
195 struct list all_conns; /* Contains "struct ofconn"s. */
196 uint64_t master_election_id; /* monotonically increasing sequence number
197 * for master election */
198 bool master_election_id_defined;
200 /* OpenFlow listeners. */
201 struct hmap services; /* Contains "struct ofservice"s. */
202 struct pvconn **snoops;
206 struct fail_open *fail_open;
207 enum ofproto_fail_mode fail_mode;
209 /* In-band control. */
210 struct in_band *in_band;
211 struct sockaddr_in *extra_in_band_remotes;
212 size_t n_extra_remotes;
216 static void update_in_band_remotes(struct connmgr *);
217 static void add_snooper(struct connmgr *, struct vconn *);
218 static void ofmonitor_run(struct connmgr *);
219 static void ofmonitor_wait(struct connmgr *);
221 /* Creates and returns a new connection manager owned by 'ofproto'. 'name' is
222 * a name for the ofproto suitable for using in log messages.
223 * 'local_port_name' is the name of the local port (OFPP_LOCAL) within
226 connmgr_create(struct ofproto *ofproto,
227 const char *name, const char *local_port_name)
231 mgr = xmalloc(sizeof *mgr);
232 mgr->ofproto = ofproto;
233 mgr->name = xstrdup(name);
234 mgr->local_port_name = xstrdup(local_port_name);
236 hmap_init(&mgr->controllers);
237 list_init(&mgr->all_conns);
238 mgr->master_election_id = 0;
239 mgr->master_election_id_defined = false;
241 hmap_init(&mgr->services);
245 mgr->fail_open = NULL;
246 mgr->fail_mode = OFPROTO_FAIL_SECURE;
249 mgr->extra_in_band_remotes = NULL;
250 mgr->n_extra_remotes = 0;
251 mgr->in_band_queue = -1;
256 /* Frees 'mgr' and all of its resources. */
258 connmgr_destroy(struct connmgr *mgr)
260 struct ofservice *ofservice, *next_ofservice;
261 struct ofconn *ofconn, *next_ofconn;
268 ovs_mutex_lock(&ofproto_mutex);
269 LIST_FOR_EACH_SAFE (ofconn, next_ofconn, node, &mgr->all_conns) {
270 ofconn_destroy(ofconn);
272 ovs_mutex_unlock(&ofproto_mutex);
274 hmap_destroy(&mgr->controllers);
276 HMAP_FOR_EACH_SAFE (ofservice, next_ofservice, node, &mgr->services) {
277 ofservice_destroy(mgr, ofservice);
279 hmap_destroy(&mgr->services);
281 for (i = 0; i < mgr->n_snoops; i++) {
282 pvconn_close(mgr->snoops[i]);
286 fail_open_destroy(mgr->fail_open);
287 mgr->fail_open = NULL;
289 in_band_destroy(mgr->in_band);
291 free(mgr->extra_in_band_remotes);
293 free(mgr->local_port_name);
298 /* Does all of the periodic maintenance required by 'mgr'.
300 * If 'handle_openflow' is nonnull, calls 'handle_openflow' for each message
301 * received on an OpenFlow connection, passing along the OpenFlow connection
302 * itself and the message that was sent. If 'handle_openflow' returns true,
303 * the message is considered to be fully processed. If 'handle_openflow'
304 * returns false, the message is considered not to have been processed at all;
305 * it will be stored and re-presented to 'handle_openflow' following the next
306 * call to connmgr_retry(). 'handle_openflow' must not modify or free the
309 * If 'handle_openflow' is NULL, no OpenFlow messages will be processed and
310 * other activities that could affect the flow table (in-band processing,
311 * fail-open processing) are suppressed too. */
313 connmgr_run(struct connmgr *mgr,
314 bool (*handle_openflow)(struct ofconn *,
315 const struct ofpbuf *ofp_msg))
316 OVS_EXCLUDED(ofproto_mutex)
318 struct ofconn *ofconn, *next_ofconn;
319 struct ofservice *ofservice;
322 if (handle_openflow && mgr->in_band) {
323 if (!in_band_run(mgr->in_band)) {
324 in_band_destroy(mgr->in_band);
329 LIST_FOR_EACH_SAFE (ofconn, next_ofconn, node, &mgr->all_conns) {
330 ofconn_run(ofconn, handle_openflow);
334 /* Fail-open maintenance. Do this after processing the ofconns since
335 * fail-open checks the status of the controller rconn. */
336 if (handle_openflow && mgr->fail_open) {
337 fail_open_run(mgr->fail_open);
340 HMAP_FOR_EACH (ofservice, node, &mgr->services) {
344 retval = pvconn_accept(ofservice->pvconn, &vconn);
349 /* Passing default value for creation of the rconn */
350 rconn = rconn_create(ofservice->probe_interval, 0, ofservice->dscp,
351 vconn_get_allowed_versions(vconn));
352 name = ofconn_make_name(mgr, vconn_get_name(vconn));
353 rconn_connect_unreliably(rconn, vconn, name);
356 ovs_mutex_lock(&ofproto_mutex);
357 ofconn = ofconn_create(mgr, rconn, OFCONN_SERVICE,
358 ofservice->enable_async_msgs);
359 ovs_mutex_unlock(&ofproto_mutex);
361 ofconn_set_rate_limit(ofconn, ofservice->rate_limit,
362 ofservice->burst_limit);
363 } else if (retval != EAGAIN) {
364 VLOG_WARN_RL(&rl, "accept failed (%s)", ovs_strerror(retval));
368 for (i = 0; i < mgr->n_snoops; i++) {
372 retval = pvconn_accept(mgr->snoops[i], &vconn);
374 add_snooper(mgr, vconn);
375 } else if (retval != EAGAIN) {
376 VLOG_WARN_RL(&rl, "accept failed (%s)", ovs_strerror(retval));
381 /* Causes the poll loop to wake up when connmgr_run() needs to run.
383 * If 'handling_openflow' is true, arriving OpenFlow messages and other
384 * activities that affect the flow table will wake up the poll loop. If
385 * 'handling_openflow' is false, they will not. */
387 connmgr_wait(struct connmgr *mgr, bool handling_openflow)
389 struct ofservice *ofservice;
390 struct ofconn *ofconn;
393 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
394 ofconn_wait(ofconn, handling_openflow);
397 if (handling_openflow && mgr->in_band) {
398 in_band_wait(mgr->in_band);
400 if (handling_openflow && mgr->fail_open) {
401 fail_open_wait(mgr->fail_open);
403 HMAP_FOR_EACH (ofservice, node, &mgr->services) {
404 pvconn_wait(ofservice->pvconn);
406 for (i = 0; i < mgr->n_snoops; i++) {
407 pvconn_wait(mgr->snoops[i]);
411 /* Adds some memory usage statistics for 'mgr' into 'usage', for use with
412 * memory_report(). */
414 connmgr_get_memory_usage(const struct connmgr *mgr, struct simap *usage)
416 const struct ofconn *ofconn;
417 unsigned int packets = 0;
418 unsigned int ofconns = 0;
420 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
425 packets += rconn_count_txqlen(ofconn->rconn);
426 for (i = 0; i < N_SCHEDULERS; i++) {
427 packets += pinsched_count_txqlen(ofconn->schedulers[i]);
429 packets += pktbuf_count_packets(ofconn->pktbuf);
431 simap_increase(usage, "ofconns", ofconns);
432 simap_increase(usage, "packets", packets);
435 /* Returns the ofproto that owns 'ofconn''s connmgr. */
437 ofconn_get_ofproto(const struct ofconn *ofconn)
439 return ofconn->connmgr->ofproto;
442 /* If processing of OpenFlow messages was blocked on any 'mgr' ofconns by
443 * returning false to the 'handle_openflow' callback to connmgr_run(), this
444 * re-enables them. */
446 connmgr_retry(struct connmgr *mgr)
448 struct ofconn *ofconn;
450 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
451 ofconn->retry = true;
455 /* OpenFlow configuration. */
457 static void add_controller(struct connmgr *, const char *target, uint8_t dscp,
458 uint32_t allowed_versions)
459 OVS_REQUIRES(ofproto_mutex);
460 static struct ofconn *find_controller_by_target(struct connmgr *,
462 static void update_fail_open(struct connmgr *) OVS_EXCLUDED(ofproto_mutex);
463 static int set_pvconns(struct pvconn ***pvconnsp, size_t *n_pvconnsp,
464 const struct sset *);
466 /* Returns true if 'mgr' has any configured primary controllers.
468 * Service controllers do not count, but configured primary controllers do
469 * count whether or not they are currently connected. */
471 connmgr_has_controllers(const struct connmgr *mgr)
473 return !hmap_is_empty(&mgr->controllers);
476 /* Initializes 'info' and populates it with information about each configured
477 * primary controller. The keys in 'info' are the controllers' targets; the
478 * data values are corresponding "struct ofproto_controller_info".
480 * The caller owns 'info' and everything in it and should free it when it is no
483 connmgr_get_controller_info(struct connmgr *mgr, struct shash *info)
485 const struct ofconn *ofconn;
487 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
488 const struct rconn *rconn = ofconn->rconn;
489 const char *target = rconn_get_target(rconn);
491 if (!shash_find(info, target)) {
492 struct ofproto_controller_info *cinfo = xmalloc(sizeof *cinfo);
493 time_t now = time_now();
494 time_t last_connection = rconn_get_last_connection(rconn);
495 time_t last_disconnect = rconn_get_last_disconnect(rconn);
496 int last_error = rconn_get_last_error(rconn);
498 shash_add(info, target, cinfo);
500 cinfo->is_connected = rconn_is_connected(rconn);
501 cinfo->role = ofconn->role;
506 cinfo->pairs.keys[cinfo->pairs.n] = "last_error";
507 cinfo->pairs.values[cinfo->pairs.n++]
508 = xstrdup(ovs_retval_to_string(last_error));
511 cinfo->pairs.keys[cinfo->pairs.n] = "state";
512 cinfo->pairs.values[cinfo->pairs.n++]
513 = xstrdup(rconn_get_state(rconn));
515 if (last_connection != TIME_MIN) {
516 cinfo->pairs.keys[cinfo->pairs.n] = "sec_since_connect";
517 cinfo->pairs.values[cinfo->pairs.n++]
518 = xasprintf("%ld", (long int) (now - last_connection));
521 if (last_disconnect != TIME_MIN) {
522 cinfo->pairs.keys[cinfo->pairs.n] = "sec_since_disconnect";
523 cinfo->pairs.values[cinfo->pairs.n++]
524 = xasprintf("%ld", (long int) (now - last_disconnect));
531 connmgr_free_controller_info(struct shash *info)
533 struct shash_node *node;
535 SHASH_FOR_EACH (node, info) {
536 struct ofproto_controller_info *cinfo = node->data;
537 while (cinfo->pairs.n) {
538 free(CONST_CAST(char *, cinfo->pairs.values[--cinfo->pairs.n]));
545 /* Changes 'mgr''s set of controllers to the 'n_controllers' controllers in
548 connmgr_set_controllers(struct connmgr *mgr,
549 const struct ofproto_controller *controllers,
550 size_t n_controllers, uint32_t allowed_versions)
551 OVS_EXCLUDED(ofproto_mutex)
553 bool had_controllers = connmgr_has_controllers(mgr);
554 struct shash new_controllers;
555 struct ofconn *ofconn, *next_ofconn;
556 struct ofservice *ofservice, *next_ofservice;
559 /* Required to add and remove ofconns. This could probably be narrowed to
560 * cover a smaller amount of code, if that yielded some benefit. */
561 ovs_mutex_lock(&ofproto_mutex);
563 /* Create newly configured controllers and services.
564 * Create a name to ofproto_controller mapping in 'new_controllers'. */
565 shash_init(&new_controllers);
566 for (i = 0; i < n_controllers; i++) {
567 const struct ofproto_controller *c = &controllers[i];
569 if (!vconn_verify_name(c->target)) {
571 ofconn = find_controller_by_target(mgr, c->target);
573 VLOG_INFO("%s: added primary controller \"%s\"",
574 mgr->name, c->target);
576 } else if (rconn_get_allowed_versions(ofconn->rconn) !=
578 VLOG_INFO("%s: re-added primary controller \"%s\"",
579 mgr->name, c->target);
581 ofconn_destroy(ofconn);
584 add_controller(mgr, c->target, c->dscp, allowed_versions);
586 } else if (!pvconn_verify_name(c->target)) {
588 ofservice = ofservice_lookup(mgr, c->target);
590 VLOG_INFO("%s: added service controller \"%s\"",
591 mgr->name, c->target);
593 } else if (ofservice->allowed_versions != allowed_versions) {
594 VLOG_INFO("%s: re-added service controller \"%s\"",
595 mgr->name, c->target);
596 ofservice_destroy(mgr, ofservice);
600 ofservice_create(mgr, c->target, allowed_versions, c->dscp);
603 VLOG_WARN_RL(&rl, "%s: unsupported controller \"%s\"",
604 mgr->name, c->target);
608 shash_add_once(&new_controllers, c->target, &controllers[i]);
611 /* Delete controllers that are no longer configured.
612 * Update configuration of all now-existing controllers. */
613 HMAP_FOR_EACH_SAFE (ofconn, next_ofconn, hmap_node, &mgr->controllers) {
614 const char *target = ofconn_get_target(ofconn);
615 struct ofproto_controller *c;
617 c = shash_find_data(&new_controllers, target);
619 VLOG_INFO("%s: removed primary controller \"%s\"",
621 ofconn_destroy(ofconn);
623 ofconn_reconfigure(ofconn, c);
627 /* Delete services that are no longer configured.
628 * Update configuration of all now-existing services. */
629 HMAP_FOR_EACH_SAFE (ofservice, next_ofservice, node, &mgr->services) {
630 const char *target = pvconn_get_name(ofservice->pvconn);
631 struct ofproto_controller *c;
633 c = shash_find_data(&new_controllers, target);
635 VLOG_INFO("%s: removed service controller \"%s\"",
637 ofservice_destroy(mgr, ofservice);
639 ofservice_reconfigure(ofservice, c);
643 shash_destroy(&new_controllers);
645 ovs_mutex_unlock(&ofproto_mutex);
647 update_in_band_remotes(mgr);
648 update_fail_open(mgr);
649 if (had_controllers != connmgr_has_controllers(mgr)) {
650 ofproto_flush_flows(mgr->ofproto);
654 /* Drops the connections between 'mgr' and all of its primary and secondary
655 * controllers, forcing them to reconnect. */
657 connmgr_reconnect(const struct connmgr *mgr)
659 struct ofconn *ofconn;
661 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
662 rconn_reconnect(ofconn->rconn);
666 /* Sets the "snoops" for 'mgr' to the pvconn targets listed in 'snoops'.
668 * A "snoop" is a pvconn to which every OpenFlow message to or from the most
669 * important controller on 'mgr' is mirrored. */
671 connmgr_set_snoops(struct connmgr *mgr, const struct sset *snoops)
673 return set_pvconns(&mgr->snoops, &mgr->n_snoops, snoops);
676 /* Adds each of the snoops currently configured on 'mgr' to 'snoops'. */
678 connmgr_get_snoops(const struct connmgr *mgr, struct sset *snoops)
682 for (i = 0; i < mgr->n_snoops; i++) {
683 sset_add(snoops, pvconn_get_name(mgr->snoops[i]));
687 /* Returns true if 'mgr' has at least one snoop, false if it has none. */
689 connmgr_has_snoops(const struct connmgr *mgr)
691 return mgr->n_snoops > 0;
694 /* Creates a new controller for 'target' in 'mgr'. update_controller() needs
695 * to be called later to finish the new ofconn's configuration. */
697 add_controller(struct connmgr *mgr, const char *target, uint8_t dscp,
698 uint32_t allowed_versions)
699 OVS_REQUIRES(ofproto_mutex)
701 char *name = ofconn_make_name(mgr, target);
702 struct ofconn *ofconn;
704 ofconn = ofconn_create(mgr, rconn_create(5, 8, dscp, allowed_versions),
705 OFCONN_PRIMARY, true);
706 ofconn->pktbuf = pktbuf_create();
707 rconn_connect(ofconn->rconn, target, name);
708 hmap_insert(&mgr->controllers, &ofconn->hmap_node, hash_string(target, 0));
713 static struct ofconn *
714 find_controller_by_target(struct connmgr *mgr, const char *target)
716 struct ofconn *ofconn;
718 HMAP_FOR_EACH_WITH_HASH (ofconn, hmap_node,
719 hash_string(target, 0), &mgr->controllers) {
720 if (!strcmp(ofconn_get_target(ofconn), target)) {
728 update_in_band_remotes(struct connmgr *mgr)
730 struct sockaddr_in *addrs;
731 size_t max_addrs, n_addrs;
732 struct ofconn *ofconn;
735 /* Allocate enough memory for as many remotes as we could possibly have. */
736 max_addrs = mgr->n_extra_remotes + hmap_count(&mgr->controllers);
737 addrs = xmalloc(max_addrs * sizeof *addrs);
740 /* Add all the remotes. */
741 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
742 const char *target = rconn_get_target(ofconn->rconn);
743 struct sockaddr_storage ss;
745 if (ofconn->band == OFPROTO_IN_BAND
746 && stream_parse_target_with_default_port(target, OFP_OLD_PORT, &ss)
747 && ss.ss_family == AF_INET) {
748 addrs[n_addrs++] = *(struct sockaddr_in *) &ss;
751 for (i = 0; i < mgr->n_extra_remotes; i++) {
752 addrs[n_addrs++] = mgr->extra_in_band_remotes[i];
755 /* Create or update or destroy in-band. */
758 in_band_create(mgr->ofproto, mgr->local_port_name, &mgr->in_band);
760 in_band_set_queue(mgr->in_band, mgr->in_band_queue);
762 /* in_band_run() needs a chance to delete any existing in-band flows.
763 * We will destroy mgr->in_band after it's done with that. */
766 in_band_set_remotes(mgr->in_band, addrs, n_addrs);
774 update_fail_open(struct connmgr *mgr)
775 OVS_EXCLUDED(ofproto_mutex)
777 if (connmgr_has_controllers(mgr)
778 && mgr->fail_mode == OFPROTO_FAIL_STANDALONE) {
779 if (!mgr->fail_open) {
780 mgr->fail_open = fail_open_create(mgr->ofproto, mgr);
783 fail_open_destroy(mgr->fail_open);
784 mgr->fail_open = NULL;
789 set_pvconns(struct pvconn ***pvconnsp, size_t *n_pvconnsp,
790 const struct sset *sset)
792 struct pvconn **pvconns = *pvconnsp;
793 size_t n_pvconns = *n_pvconnsp;
798 for (i = 0; i < n_pvconns; i++) {
799 pvconn_close(pvconns[i]);
803 pvconns = xmalloc(sset_count(sset) * sizeof *pvconns);
805 SSET_FOR_EACH (name, sset) {
806 struct pvconn *pvconn;
808 error = pvconn_open(name, 0, 0, &pvconn);
810 pvconns[n_pvconns++] = pvconn;
812 VLOG_ERR("failed to listen on %s: %s", name, ovs_strerror(error));
820 *n_pvconnsp = n_pvconns;
825 /* Returns a "preference level" for snooping 'ofconn'. A higher return value
826 * means that 'ofconn' is more interesting for monitoring than a lower return
829 snoop_preference(const struct ofconn *ofconn)
831 switch (ofconn->role) {
832 case OFPCR12_ROLE_MASTER:
834 case OFPCR12_ROLE_EQUAL:
836 case OFPCR12_ROLE_SLAVE:
838 case OFPCR12_ROLE_NOCHANGE:
840 /* Shouldn't happen. */
845 /* One of 'mgr''s "snoop" pvconns has accepted a new connection on 'vconn'.
846 * Connects this vconn to a controller. */
848 add_snooper(struct connmgr *mgr, struct vconn *vconn)
850 struct ofconn *ofconn, *best;
852 /* Pick a controller for monitoring. */
854 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
855 if (ofconn->type == OFCONN_PRIMARY
856 && (!best || snoop_preference(ofconn) > snoop_preference(best))) {
862 rconn_add_monitor(best->rconn, vconn);
864 VLOG_INFO_RL(&rl, "no controller connection to snoop");
869 /* Public ofconn functions. */
871 /* Returns the connection type, either OFCONN_PRIMARY or OFCONN_SERVICE. */
873 ofconn_get_type(const struct ofconn *ofconn)
878 /* If a master election id is defined, stores it into '*idp' and returns
879 * true. Otherwise, stores UINT64_MAX into '*idp' and returns false. */
881 ofconn_get_master_election_id(const struct ofconn *ofconn, uint64_t *idp)
883 *idp = (ofconn->connmgr->master_election_id_defined
884 ? ofconn->connmgr->master_election_id
886 return ofconn->connmgr->master_election_id_defined;
889 /* Sets the master election id.
891 * Returns true if successful, false if the id is stale
894 ofconn_set_master_election_id(struct ofconn *ofconn, uint64_t id)
896 if (ofconn->connmgr->master_election_id_defined
898 /* Unsigned difference interpreted as a two's complement signed
900 (int64_t)(id - ofconn->connmgr->master_election_id) < 0) {
903 ofconn->connmgr->master_election_id = id;
904 ofconn->connmgr->master_election_id_defined = true;
909 /* Returns the role configured for 'ofconn'.
911 * The default role, if no other role has been set, is OFPCR12_ROLE_EQUAL. */
912 enum ofp12_controller_role
913 ofconn_get_role(const struct ofconn *ofconn)
919 ofconn_send_role_status(struct ofconn *ofconn, uint32_t role, uint8_t reason)
921 struct ofputil_role_status status;
924 status.reason = reason;
926 ofconn_get_master_election_id(ofconn, &status.generation_id);
928 buf = ofputil_encode_role_status(&status, ofconn_get_protocol(ofconn));
930 ofconn_send(ofconn, buf, NULL);
933 /* Changes 'ofconn''s role to 'role'. If 'role' is OFPCR12_ROLE_MASTER then
934 * any existing master is demoted to a slave. */
936 ofconn_set_role(struct ofconn *ofconn, enum ofp12_controller_role role)
938 if (role != ofconn->role && role == OFPCR12_ROLE_MASTER) {
939 struct ofconn *other;
941 HMAP_FOR_EACH (other, hmap_node, &ofconn->connmgr->controllers) {
942 if (other->role == OFPCR12_ROLE_MASTER) {
943 other->role = OFPCR12_ROLE_SLAVE;
944 ofconn_send_role_status(other, OFPCR12_ROLE_SLAVE, OFPCRR_MASTER_REQUEST);
952 ofconn_set_invalid_ttl_to_controller(struct ofconn *ofconn, bool enable)
954 uint32_t bit = 1u << OFPR_INVALID_TTL;
956 ofconn->master_async_config[OAM_PACKET_IN] |= bit;
958 ofconn->master_async_config[OAM_PACKET_IN] &= ~bit;
963 ofconn_get_invalid_ttl_to_controller(struct ofconn *ofconn)
965 uint32_t bit = 1u << OFPR_INVALID_TTL;
966 return (ofconn->master_async_config[OAM_PACKET_IN] & bit) != 0;
969 /* Returns the currently configured protocol for 'ofconn', one of OFPUTIL_P_*.
971 * Returns OFPUTIL_P_NONE, which is not a valid protocol, if 'ofconn' hasn't
972 * completed version negotiation. This can't happen if at least one OpenFlow
973 * message, other than OFPT_HELLO, has been received on the connection (such as
974 * in ofproto.c's message handling code), since version negotiation is a
975 * prerequisite for starting to receive messages. This means that
976 * OFPUTIL_P_NONE is a special case that most callers need not worry about. */
977 enum ofputil_protocol
978 ofconn_get_protocol(const struct ofconn *ofconn)
980 if (ofconn->protocol == OFPUTIL_P_NONE &&
981 rconn_is_connected(ofconn->rconn)) {
982 int version = rconn_get_version(ofconn->rconn);
984 ofconn_set_protocol(CONST_CAST(struct ofconn *, ofconn),
985 ofputil_protocol_from_ofp_version(version));
989 return ofconn->protocol;
992 /* Sets the protocol for 'ofconn' to 'protocol' (one of OFPUTIL_P_*).
994 * (This doesn't actually send anything to accomplish this. Presumably the
995 * caller already did that.) */
997 ofconn_set_protocol(struct ofconn *ofconn, enum ofputil_protocol protocol)
999 ofconn->protocol = protocol;
1002 /* Returns the currently configured packet in format for 'ofconn', one of
1005 * The default, if no other format has been set, is NXPIF_OPENFLOW10. */
1006 enum nx_packet_in_format
1007 ofconn_get_packet_in_format(struct ofconn *ofconn)
1009 return ofconn->packet_in_format;
1012 /* Sets the packet in format for 'ofconn' to 'packet_in_format' (one of
1015 ofconn_set_packet_in_format(struct ofconn *ofconn,
1016 enum nx_packet_in_format packet_in_format)
1018 ofconn->packet_in_format = packet_in_format;
1021 /* Sets the controller connection ID for 'ofconn' to 'controller_id'.
1023 * The connection controller ID is used for OFPP_CONTROLLER and
1024 * NXAST_CONTROLLER actions. See "struct nx_action_controller" for details. */
1026 ofconn_set_controller_id(struct ofconn *ofconn, uint16_t controller_id)
1028 ofconn->controller_id = controller_id;
1031 /* Returns the default miss send length for 'ofconn'. */
1033 ofconn_get_miss_send_len(const struct ofconn *ofconn)
1035 return ofconn->miss_send_len;
1038 /* Sets the default miss send length for 'ofconn' to 'miss_send_len'. */
1040 ofconn_set_miss_send_len(struct ofconn *ofconn, int miss_send_len)
1042 ofconn->miss_send_len = miss_send_len;
1046 ofconn_set_async_config(struct ofconn *ofconn,
1047 const uint32_t master_masks[OAM_N_TYPES],
1048 const uint32_t slave_masks[OAM_N_TYPES])
1050 size_t size = sizeof ofconn->master_async_config;
1051 memcpy(ofconn->master_async_config, master_masks, size);
1052 memcpy(ofconn->slave_async_config, slave_masks, size);
1056 ofconn_get_async_config(struct ofconn *ofconn,
1057 uint32_t *master_masks, uint32_t *slave_masks)
1059 size_t size = sizeof ofconn->master_async_config;
1060 memcpy(master_masks, ofconn->master_async_config, size);
1061 memcpy(slave_masks, ofconn->slave_async_config, size);
1064 /* Sends 'msg' on 'ofconn', accounting it as a reply. (If there is a
1065 * sufficient number of OpenFlow replies in-flight on a single ofconn, then the
1066 * connmgr will stop accepting new OpenFlow requests on that ofconn until the
1067 * controller has accepted some of the replies.) */
1069 ofconn_send_reply(const struct ofconn *ofconn, struct ofpbuf *msg)
1071 ofconn_send(ofconn, msg, ofconn->reply_counter);
1074 /* Sends each of the messages in list 'replies' on 'ofconn' in order,
1075 * accounting them as replies. */
1077 ofconn_send_replies(const struct ofconn *ofconn, struct list *replies)
1079 struct ofpbuf *reply, *next;
1081 LIST_FOR_EACH_SAFE (reply, next, list_node, replies) {
1082 list_remove(&reply->list_node);
1083 ofconn_send_reply(ofconn, reply);
1087 /* Sends 'error' on 'ofconn', as a reply to 'request'. Only at most the
1088 * first 64 bytes of 'request' are used. */
1090 ofconn_send_error(const struct ofconn *ofconn,
1091 const struct ofp_header *request, enum ofperr error)
1093 static struct vlog_rate_limit err_rl = VLOG_RATE_LIMIT_INIT(10, 10);
1094 struct ofpbuf *reply;
1096 reply = ofperr_encode_reply(error, request);
1097 if (!VLOG_DROP_INFO(&err_rl)) {
1098 const char *type_name;
1102 request_len = ntohs(request->length);
1103 type_name = (!ofpraw_decode_partial(&raw, request,
1104 MIN(64, request_len))
1105 ? ofpraw_get_name(raw)
1108 VLOG_INFO("%s: sending %s error reply to %s message",
1109 rconn_get_name(ofconn->rconn), ofperr_to_string(error),
1112 ofconn_send_reply(ofconn, reply);
1115 /* Same as pktbuf_retrieve(), using the pktbuf owned by 'ofconn'. */
1117 ofconn_pktbuf_retrieve(struct ofconn *ofconn, uint32_t id,
1118 struct ofpbuf **bufferp, ofp_port_t *in_port)
1120 return pktbuf_retrieve(ofconn->pktbuf, id, bufferp, in_port);
1123 /* Returns true if 'ofconn' has any pending opgroups. */
1125 ofconn_has_pending_opgroups(const struct ofconn *ofconn)
1127 return !list_is_empty(&ofconn->opgroups);
1130 /* Adds 'ofconn_node' to 'ofconn''s list of pending opgroups.
1132 * If 'ofconn' is destroyed or its connection drops, then 'ofconn' will remove
1133 * 'ofconn_node' from the list and re-initialize it with list_init(). The
1134 * client may, therefore, use list_is_empty(ofconn_node) to determine whether
1135 * 'ofconn_node' is still associated with an active ofconn.
1137 * The client may also remove ofconn_node from the list itself, with
1140 ofconn_add_opgroup(struct ofconn *ofconn, struct list *ofconn_node)
1142 list_push_back(&ofconn->opgroups, ofconn_node);
1146 ofconn_get_bundles(struct ofconn *ofconn)
1148 return &ofconn->bundles;
1152 /* Private ofconn functions. */
1155 ofconn_get_target(const struct ofconn *ofconn)
1157 return rconn_get_target(ofconn->rconn);
1160 static struct ofconn *
1161 ofconn_create(struct connmgr *mgr, struct rconn *rconn, enum ofconn_type type,
1162 bool enable_async_msgs)
1164 struct ofconn *ofconn;
1166 ofconn = xzalloc(sizeof *ofconn);
1167 ofconn->connmgr = mgr;
1168 list_push_back(&mgr->all_conns, &ofconn->node);
1169 ofconn->rconn = rconn;
1170 ofconn->type = type;
1171 ofconn->enable_async_msgs = enable_async_msgs;
1173 list_init(&ofconn->opgroups);
1175 hmap_init(&ofconn->monitors);
1176 list_init(&ofconn->updates);
1178 hmap_init(&ofconn->bundles);
1180 ofconn_flush(ofconn);
1185 /* Clears all of the state in 'ofconn' that should not persist from one
1186 * connection to the next. */
1188 ofconn_flush(struct ofconn *ofconn)
1189 OVS_REQUIRES(ofproto_mutex)
1191 struct ofmonitor *monitor, *next_monitor;
1194 ofconn->role = OFPCR12_ROLE_EQUAL;
1195 ofconn_set_protocol(ofconn, OFPUTIL_P_NONE);
1196 ofconn->packet_in_format = NXPIF_OPENFLOW10;
1198 /* Disassociate 'ofconn' from all of the ofopgroups that it initiated that
1199 * have not yet completed. (Those ofopgroups will still run to completion
1200 * in the usual way, but any errors that they run into will not be reported
1201 * on any OpenFlow channel.)
1203 * Also discard any blocked operation on 'ofconn'. */
1204 while (!list_is_empty(&ofconn->opgroups)) {
1205 list_init(list_pop_front(&ofconn->opgroups));
1207 ofpbuf_delete(ofconn->blocked);
1208 ofconn->blocked = NULL;
1210 rconn_packet_counter_destroy(ofconn->packet_in_counter);
1211 ofconn->packet_in_counter = rconn_packet_counter_create();
1212 for (i = 0; i < N_SCHEDULERS; i++) {
1213 if (ofconn->schedulers[i]) {
1216 pinsched_get_limits(ofconn->schedulers[i], &rate, &burst);
1217 pinsched_destroy(ofconn->schedulers[i]);
1218 ofconn->schedulers[i] = pinsched_create(rate, burst);
1221 if (ofconn->pktbuf) {
1222 pktbuf_destroy(ofconn->pktbuf);
1223 ofconn->pktbuf = pktbuf_create();
1225 ofconn->miss_send_len = (ofconn->type == OFCONN_PRIMARY
1226 ? OFP_DEFAULT_MISS_SEND_LEN
1228 ofconn->controller_id = 0;
1230 rconn_packet_counter_destroy(ofconn->reply_counter);
1231 ofconn->reply_counter = rconn_packet_counter_create();
1233 if (ofconn->enable_async_msgs) {
1234 uint32_t *master = ofconn->master_async_config;
1235 uint32_t *slave = ofconn->slave_async_config;
1237 /* "master" and "other" roles get all asynchronous messages by default,
1238 * except that the controller needs to enable nonstandard "packet-in"
1239 * reasons itself. */
1240 master[OAM_PACKET_IN] = (1u << OFPR_NO_MATCH) | (1u << OFPR_ACTION);
1241 master[OAM_PORT_STATUS] = ((1u << OFPPR_ADD)
1242 | (1u << OFPPR_DELETE)
1243 | (1u << OFPPR_MODIFY));
1244 master[OAM_FLOW_REMOVED] = ((1u << OFPRR_IDLE_TIMEOUT)
1245 | (1u << OFPRR_HARD_TIMEOUT)
1246 | (1u << OFPRR_DELETE));
1248 /* "slave" role gets port status updates by default. */
1249 slave[OAM_PACKET_IN] = 0;
1250 slave[OAM_PORT_STATUS] = ((1u << OFPPR_ADD)
1251 | (1u << OFPPR_DELETE)
1252 | (1u << OFPPR_MODIFY));
1253 slave[OAM_FLOW_REMOVED] = 0;
1255 memset(ofconn->master_async_config, 0,
1256 sizeof ofconn->master_async_config);
1257 memset(ofconn->slave_async_config, 0,
1258 sizeof ofconn->slave_async_config);
1261 HMAP_FOR_EACH_SAFE (monitor, next_monitor, ofconn_node,
1262 &ofconn->monitors) {
1263 ofmonitor_destroy(monitor);
1265 rconn_packet_counter_destroy(ofconn->monitor_counter);
1266 ofconn->monitor_counter = rconn_packet_counter_create();
1267 ofpbuf_list_delete(&ofconn->updates); /* ...but it should be empty. */
1271 ofconn_destroy(struct ofconn *ofconn)
1272 OVS_REQUIRES(ofproto_mutex)
1274 ofconn_flush(ofconn);
1276 if (ofconn->type == OFCONN_PRIMARY) {
1277 hmap_remove(&ofconn->connmgr->controllers, &ofconn->hmap_node);
1280 ofp_bundle_remove_all(ofconn);
1282 hmap_destroy(&ofconn->monitors);
1283 list_remove(&ofconn->node);
1284 rconn_destroy(ofconn->rconn);
1285 rconn_packet_counter_destroy(ofconn->packet_in_counter);
1286 rconn_packet_counter_destroy(ofconn->reply_counter);
1287 pktbuf_destroy(ofconn->pktbuf);
1288 rconn_packet_counter_destroy(ofconn->monitor_counter);
1292 /* Reconfigures 'ofconn' to match 'c'. 'ofconn' and 'c' must have the same
1295 ofconn_reconfigure(struct ofconn *ofconn, const struct ofproto_controller *c)
1299 ofconn->band = c->band;
1300 ofconn->enable_async_msgs = c->enable_async_msgs;
1302 rconn_set_max_backoff(ofconn->rconn, c->max_backoff);
1304 probe_interval = c->probe_interval ? MAX(c->probe_interval, 5) : 0;
1305 rconn_set_probe_interval(ofconn->rconn, probe_interval);
1307 ofconn_set_rate_limit(ofconn, c->rate_limit, c->burst_limit);
1309 /* If dscp value changed reconnect. */
1310 if (c->dscp != rconn_get_dscp(ofconn->rconn)) {
1311 rconn_set_dscp(ofconn->rconn, c->dscp);
1312 rconn_reconnect(ofconn->rconn);
1316 /* Returns true if it makes sense for 'ofconn' to receive and process OpenFlow
1319 ofconn_may_recv(const struct ofconn *ofconn)
1321 int count = rconn_packet_counter_n_packets(ofconn->reply_counter);
1322 return (!ofconn->blocked || ofconn->retry) && count < OFCONN_REPLY_MAX;
1326 ofconn_run(struct ofconn *ofconn,
1327 bool (*handle_openflow)(struct ofconn *,
1328 const struct ofpbuf *ofp_msg))
1330 struct connmgr *mgr = ofconn->connmgr;
1333 for (i = 0; i < N_SCHEDULERS; i++) {
1336 pinsched_run(ofconn->schedulers[i], &txq);
1337 do_send_packet_ins(ofconn, &txq);
1340 rconn_run(ofconn->rconn);
1342 if (handle_openflow) {
1343 /* Limit the number of iterations to avoid starving other tasks. */
1344 for (i = 0; i < 50 && ofconn_may_recv(ofconn); i++) {
1345 struct ofpbuf *of_msg;
1347 of_msg = (ofconn->blocked
1349 : rconn_recv(ofconn->rconn));
1353 if (mgr->fail_open) {
1354 fail_open_maybe_recover(mgr->fail_open);
1357 if (handle_openflow(ofconn, of_msg)) {
1358 ofpbuf_delete(of_msg);
1359 ofconn->blocked = NULL;
1361 ofconn->blocked = of_msg;
1362 ofconn->retry = false;
1367 ovs_mutex_lock(&ofproto_mutex);
1368 if (!rconn_is_alive(ofconn->rconn)) {
1369 ofconn_destroy(ofconn);
1370 } else if (!rconn_is_connected(ofconn->rconn)) {
1371 ofconn_flush(ofconn);
1373 ovs_mutex_unlock(&ofproto_mutex);
1377 ofconn_wait(struct ofconn *ofconn, bool handling_openflow)
1381 for (i = 0; i < N_SCHEDULERS; i++) {
1382 pinsched_wait(ofconn->schedulers[i]);
1384 rconn_run_wait(ofconn->rconn);
1385 if (handling_openflow && ofconn_may_recv(ofconn)) {
1386 rconn_recv_wait(ofconn->rconn);
1390 /* Returns true if 'ofconn' should receive asynchronous messages of the given
1391 * OAM_* 'type' and 'reason', which should be a OFPR_* value for OAM_PACKET_IN,
1392 * a OFPPR_* value for OAM_PORT_STATUS, or an OFPRR_* value for
1393 * OAM_FLOW_REMOVED. Returns false if the message should not be sent on
1396 ofconn_receives_async_msg(const struct ofconn *ofconn,
1397 enum ofconn_async_msg_type type,
1398 unsigned int reason)
1400 const uint32_t *async_config;
1402 ovs_assert(reason < 32);
1403 ovs_assert((unsigned int) type < OAM_N_TYPES);
1405 if (ofconn_get_protocol(ofconn) == OFPUTIL_P_NONE
1406 || !rconn_is_connected(ofconn->rconn)) {
1410 /* Keep the following code in sync with the documentation in the
1411 * "Asynchronous Messages" section in DESIGN. */
1413 if (ofconn->type == OFCONN_SERVICE && !ofconn->miss_send_len) {
1414 /* Service connections don't get asynchronous messages unless they have
1415 * explicitly asked for them by setting a nonzero miss send length. */
1419 async_config = (ofconn->role == OFPCR12_ROLE_SLAVE
1420 ? ofconn->slave_async_config
1421 : ofconn->master_async_config);
1422 if (!(async_config[type] & (1u << reason))) {
1429 /* The default "table-miss" behaviour for OpenFlow1.3+ is to drop the
1430 * packet rather than to send the packet to the controller.
1432 * This function returns false to indicate the packet should be dropped if
1433 * the controller action was the result of the default table-miss behaviour
1434 * and the controller is using OpenFlow1.3+.
1436 * Otherwise true is returned to indicate the packet should be forwarded to
1439 ofconn_wants_packet_in_on_miss(struct ofconn *ofconn,
1440 const struct ofproto_packet_in *pin)
1442 if (pin->miss_type == OFPROTO_PACKET_IN_MISS_WITHOUT_FLOW) {
1443 enum ofputil_protocol protocol = ofconn_get_protocol(ofconn);
1445 if (protocol != OFPUTIL_P_NONE
1446 && ofputil_protocol_to_ofp_version(protocol) >= OFP13_VERSION) {
1447 enum ofproto_table_config config;
1449 config = ofproto_table_get_config(ofconn->connmgr->ofproto,
1451 if (config == OFPROTO_TABLE_MISS_DEFAULT) {
1459 /* The default "table-miss" behaviour for OpenFlow1.3+ is to drop the
1460 * packet rather than to send the packet to the controller.
1462 * This function returns false to indicate that a packet_in message
1463 * for a "table-miss" should be sent to at least one controller.
1464 * That is there is at least one controller with controller_id 0
1465 * which connected using an OpenFlow version earlier than OpenFlow1.3.
1469 * This logic assumes that "table-miss" packet_in messages
1470 * are always sent to controller_id 0. */
1472 connmgr_wants_packet_in_on_miss(struct connmgr *mgr)
1474 struct ofconn *ofconn;
1476 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1477 enum ofputil_protocol protocol = ofconn_get_protocol(ofconn);
1479 if (ofconn->controller_id == 0 &&
1480 (protocol == OFPUTIL_P_NONE ||
1481 ofputil_protocol_to_ofp_version(protocol) < OFP13_VERSION)) {
1488 /* Returns a human-readable name for an OpenFlow connection between 'mgr' and
1489 * 'target', suitable for use in log messages for identifying the connection.
1491 * The name is dynamically allocated. The caller should free it (with free())
1492 * when it is no longer needed. */
1494 ofconn_make_name(const struct connmgr *mgr, const char *target)
1496 return xasprintf("%s<->%s", mgr->name, target);
1500 ofconn_set_rate_limit(struct ofconn *ofconn, int rate, int burst)
1504 for (i = 0; i < N_SCHEDULERS; i++) {
1505 struct pinsched **s = &ofconn->schedulers[i];
1509 *s = pinsched_create(rate, burst);
1511 pinsched_set_limits(*s, rate, burst);
1514 pinsched_destroy(*s);
1521 ofconn_send(const struct ofconn *ofconn, struct ofpbuf *msg,
1522 struct rconn_packet_counter *counter)
1524 ofpmsg_update_length(msg);
1525 rconn_send(ofconn->rconn, msg, counter);
1528 /* Sending asynchronous messages. */
1530 static void schedule_packet_in(struct ofconn *, struct ofproto_packet_in,
1531 enum ofp_packet_in_reason wire_reason);
1533 /* Sends an OFPT_PORT_STATUS message with 'opp' and 'reason' to appropriate
1534 * controllers managed by 'mgr'. For messages caused by a controller
1535 * OFPT_PORT_MOD, specify 'source' as the controller connection that sent the
1536 * request; otherwise, specify 'source' as NULL. */
1538 connmgr_send_port_status(struct connmgr *mgr, struct ofconn *source,
1539 const struct ofputil_phy_port *pp, uint8_t reason)
1541 /* XXX Should limit the number of queued port status change messages. */
1542 struct ofputil_port_status ps;
1543 struct ofconn *ofconn;
1547 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1548 if (ofconn_receives_async_msg(ofconn, OAM_PORT_STATUS, reason)) {
1551 /* Before 1.5, OpenFlow specified that OFPT_PORT_MOD should not
1552 * generate OFPT_PORT_STATUS messages. That requirement was a
1553 * relic of how OpenFlow originally supported a single controller,
1554 * so that one could expect the controller to already know the
1555 * changes it had made.
1557 * EXT-338 changes OpenFlow 1.5 OFPT_PORT_MOD to send
1558 * OFPT_PORT_STATUS messages to every controller. This is
1559 * obviously more useful in the multi-controller case. We could
1560 * always implement it that way in OVS, but that would risk
1561 * confusing controllers that are intended for single-controller
1562 * use only. (Imagine a controller that generates an OFPT_PORT_MOD
1563 * in response to any OFPT_PORT_STATUS!)
1565 * So this compromises: for OpenFlow 1.4 and earlier, it generates
1566 * OFPT_PORT_STATUS for OFPT_PORT_MOD, but not back to the
1567 * originating controller. In a single-controller environment, in
1568 * particular, this means that it will never generate
1569 * OFPT_PORT_STATUS for OFPT_PORT_MOD at all. */
1570 if (ofconn == source
1571 && rconn_get_version(ofconn->rconn) < OFP15_VERSION) {
1575 msg = ofputil_encode_port_status(&ps, ofconn_get_protocol(ofconn));
1576 ofconn_send(ofconn, msg, NULL);
1581 /* Sends an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message based on 'fr' to
1582 * appropriate controllers managed by 'mgr'. */
1584 connmgr_send_flow_removed(struct connmgr *mgr,
1585 const struct ofputil_flow_removed *fr)
1587 struct ofconn *ofconn;
1589 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1590 if (ofconn_receives_async_msg(ofconn, OAM_FLOW_REMOVED, fr->reason)) {
1593 /* Account flow expirations as replies to OpenFlow requests. That
1594 * works because preventing OpenFlow requests from being processed
1595 * also prevents new flows from being added (and expiring). (It
1596 * also prevents processing OpenFlow requests that would not add
1597 * new flows, so it is imperfect.) */
1598 msg = ofputil_encode_flow_removed(fr, ofconn_get_protocol(ofconn));
1599 ofconn_send_reply(ofconn, msg);
1604 /* Normally a send-to-controller action uses reason OFPR_ACTION. However, in
1605 * OpenFlow 1.3 and later, packet_ins generated by a send-to-controller action
1606 * in a "table-miss" flow (one with priority 0 and completely wildcarded) are
1607 * sent as OFPR_NO_MATCH. This function returns the reason that should
1608 * actually be sent on 'ofconn' for 'pin'. */
1609 static enum ofp_packet_in_reason
1610 wire_reason(struct ofconn *ofconn, const struct ofproto_packet_in *pin)
1612 if (pin->miss_type == OFPROTO_PACKET_IN_MISS_FLOW
1613 && pin->up.reason == OFPR_ACTION) {
1614 enum ofputil_protocol protocol = ofconn_get_protocol(ofconn);
1616 if (protocol != OFPUTIL_P_NONE
1617 && ofputil_protocol_to_ofp_version(protocol) >= OFP13_VERSION) {
1618 return OFPR_NO_MATCH;
1621 return pin->up.reason;
1624 /* Given 'pin', sends an OFPT_PACKET_IN message to each OpenFlow controller as
1625 * necessary according to their individual configurations.
1627 * The caller doesn't need to fill in pin->buffer_id or pin->total_len. */
1629 connmgr_send_packet_in(struct connmgr *mgr,
1630 const struct ofproto_packet_in *pin)
1632 struct ofconn *ofconn;
1634 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1635 enum ofp_packet_in_reason reason = wire_reason(ofconn, pin);
1637 if (ofconn_wants_packet_in_on_miss(ofconn, pin)
1638 && ofconn_receives_async_msg(ofconn, OAM_PACKET_IN, pin->up.reason)
1639 && ofconn->controller_id == pin->controller_id) {
1640 schedule_packet_in(ofconn, *pin, reason);
1646 do_send_packet_ins(struct ofconn *ofconn, struct list *txq)
1648 struct ofpbuf *pin, *next_pin;
1650 LIST_FOR_EACH_SAFE (pin, next_pin, list_node, txq) {
1651 list_remove(&pin->list_node);
1653 if (rconn_send_with_limit(ofconn->rconn, pin,
1654 ofconn->packet_in_counter, 100) == EAGAIN) {
1655 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
1657 VLOG_INFO_RL(&rl, "%s: dropping packet-in due to queue overflow",
1658 rconn_get_name(ofconn->rconn));
1663 /* Takes 'pin', composes an OpenFlow packet-in message from it, and passes it
1664 * to 'ofconn''s packet scheduler for sending. */
1666 schedule_packet_in(struct ofconn *ofconn, struct ofproto_packet_in pin,
1667 enum ofp_packet_in_reason wire_reason)
1669 struct connmgr *mgr = ofconn->connmgr;
1670 uint16_t controller_max_len;
1673 pin.up.total_len = pin.up.packet_len;
1675 pin.up.reason = wire_reason;
1676 if (pin.up.reason == OFPR_ACTION) {
1677 controller_max_len = pin.send_len; /* max_len */
1679 controller_max_len = ofconn->miss_send_len;
1682 /* Get OpenFlow buffer_id.
1683 * For OpenFlow 1.2+, OFPCML_NO_BUFFER (== UINT16_MAX) specifies
1684 * unbuffered. This behaviour doesn't violate prior versions, too. */
1685 if (controller_max_len == UINT16_MAX) {
1686 pin.up.buffer_id = UINT32_MAX;
1687 } else if (mgr->fail_open && fail_open_is_active(mgr->fail_open)) {
1688 pin.up.buffer_id = pktbuf_get_null();
1689 } else if (!ofconn->pktbuf) {
1690 pin.up.buffer_id = UINT32_MAX;
1692 pin.up.buffer_id = pktbuf_save(ofconn->pktbuf,
1693 pin.up.packet, pin.up.packet_len,
1694 pin.up.fmd.in_port);
1697 /* Figure out how much of the packet to send.
1698 * If not buffered, send the entire packet. Otherwise, depending on
1699 * the reason of packet-in, send what requested by the controller. */
1700 if (pin.up.buffer_id != UINT32_MAX
1701 && controller_max_len < pin.up.packet_len) {
1702 pin.up.packet_len = controller_max_len;
1705 /* Make OFPT_PACKET_IN and hand over to packet scheduler. */
1706 pinsched_send(ofconn->schedulers[pin.up.reason == OFPR_NO_MATCH ? 0 : 1],
1708 ofputil_encode_packet_in(&pin.up,
1709 ofconn_get_protocol(ofconn),
1710 ofconn->packet_in_format),
1712 do_send_packet_ins(ofconn, &txq);
1715 /* Fail-open settings. */
1717 /* Returns the failure handling mode (OFPROTO_FAIL_SECURE or
1718 * OFPROTO_FAIL_STANDALONE) for 'mgr'. */
1719 enum ofproto_fail_mode
1720 connmgr_get_fail_mode(const struct connmgr *mgr)
1722 return mgr->fail_mode;
1725 /* Sets the failure handling mode for 'mgr' to 'fail_mode' (either
1726 * OFPROTO_FAIL_SECURE or OFPROTO_FAIL_STANDALONE). */
1728 connmgr_set_fail_mode(struct connmgr *mgr, enum ofproto_fail_mode fail_mode)
1730 if (mgr->fail_mode != fail_mode) {
1731 mgr->fail_mode = fail_mode;
1732 update_fail_open(mgr);
1733 if (!connmgr_has_controllers(mgr)) {
1734 ofproto_flush_flows(mgr->ofproto);
1739 /* Fail-open implementation. */
1741 /* Returns the longest probe interval among the primary controllers configured
1742 * on 'mgr'. Returns 0 if there are no primary controllers. */
1744 connmgr_get_max_probe_interval(const struct connmgr *mgr)
1746 const struct ofconn *ofconn;
1747 int max_probe_interval;
1749 max_probe_interval = 0;
1750 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1751 int probe_interval = rconn_get_probe_interval(ofconn->rconn);
1752 max_probe_interval = MAX(max_probe_interval, probe_interval);
1754 return max_probe_interval;
1757 /* Returns the number of seconds for which all of 'mgr's primary controllers
1758 * have been disconnected. Returns 0 if 'mgr' has no primary controllers. */
1760 connmgr_failure_duration(const struct connmgr *mgr)
1762 const struct ofconn *ofconn;
1763 int min_failure_duration;
1765 if (!connmgr_has_controllers(mgr)) {
1769 min_failure_duration = INT_MAX;
1770 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1771 int failure_duration = rconn_failure_duration(ofconn->rconn);
1772 min_failure_duration = MIN(min_failure_duration, failure_duration);
1774 return min_failure_duration;
1777 /* Returns true if at least one primary controller is connected (regardless of
1778 * whether those controllers are believed to have authenticated and accepted
1779 * this switch), false if none of them are connected. */
1781 connmgr_is_any_controller_connected(const struct connmgr *mgr)
1783 const struct ofconn *ofconn;
1785 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1786 if (rconn_is_connected(ofconn->rconn)) {
1793 /* Returns true if at least one primary controller is believed to have
1794 * authenticated and accepted this switch, false otherwise. */
1796 connmgr_is_any_controller_admitted(const struct connmgr *mgr)
1798 const struct ofconn *ofconn;
1800 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1801 if (rconn_is_admitted(ofconn->rconn)) {
1808 /* In-band configuration. */
1810 static bool any_extras_changed(const struct connmgr *,
1811 const struct sockaddr_in *extras, size_t n);
1813 /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'mgr''s
1814 * in-band control should guarantee access, in the same way that in-band
1815 * control guarantees access to OpenFlow controllers. */
1817 connmgr_set_extra_in_band_remotes(struct connmgr *mgr,
1818 const struct sockaddr_in *extras, size_t n)
1820 if (!any_extras_changed(mgr, extras, n)) {
1824 free(mgr->extra_in_band_remotes);
1825 mgr->n_extra_remotes = n;
1826 mgr->extra_in_band_remotes = xmemdup(extras, n * sizeof *extras);
1828 update_in_band_remotes(mgr);
1831 /* Sets the OpenFlow queue used by flows set up by in-band control on
1832 * 'mgr' to 'queue_id'. If 'queue_id' is negative, then in-band control
1833 * flows will use the default queue. */
1835 connmgr_set_in_band_queue(struct connmgr *mgr, int queue_id)
1837 if (queue_id != mgr->in_band_queue) {
1838 mgr->in_band_queue = queue_id;
1839 update_in_band_remotes(mgr);
1844 any_extras_changed(const struct connmgr *mgr,
1845 const struct sockaddr_in *extras, size_t n)
1849 if (n != mgr->n_extra_remotes) {
1853 for (i = 0; i < n; i++) {
1854 const struct sockaddr_in *old = &mgr->extra_in_band_remotes[i];
1855 const struct sockaddr_in *new = &extras[i];
1857 if (old->sin_addr.s_addr != new->sin_addr.s_addr ||
1858 old->sin_port != new->sin_port) {
1866 /* In-band implementation. */
1869 connmgr_has_in_band(struct connmgr *mgr)
1871 return mgr->in_band != NULL;
1874 /* Fail-open and in-band implementation. */
1876 /* Called by 'ofproto' after all flows have been flushed, to allow fail-open
1877 * and standalone mode to re-create their flows.
1879 * In-band control has more sophisticated code that manages flows itself. */
1881 connmgr_flushed(struct connmgr *mgr)
1882 OVS_EXCLUDED(ofproto_mutex)
1884 if (mgr->fail_open) {
1885 fail_open_flushed(mgr->fail_open);
1888 /* If there are no controllers and we're in standalone mode, set up a flow
1889 * that matches every packet and directs them to OFPP_NORMAL (which goes to
1890 * us). Otherwise, the switch is in secure mode and we won't pass any
1891 * traffic until a controller has been defined and it tells us to do so. */
1892 if (!connmgr_has_controllers(mgr)
1893 && mgr->fail_mode == OFPROTO_FAIL_STANDALONE) {
1894 struct ofpbuf ofpacts;
1897 ofpbuf_init(&ofpacts, OFPACT_OUTPUT_SIZE);
1898 ofpact_put_OUTPUT(&ofpacts)->port = OFPP_NORMAL;
1899 ofpact_pad(&ofpacts);
1901 match_init_catchall(&match);
1902 ofproto_add_flow(mgr->ofproto, &match, 0, ofpbuf_data(&ofpacts),
1903 ofpbuf_size(&ofpacts));
1905 ofpbuf_uninit(&ofpacts);
1909 /* Creates a new ofservice for 'target' in 'mgr'. Returns 0 if successful,
1910 * otherwise a positive errno value.
1912 * ofservice_reconfigure() must be called to fully configure the new
1915 ofservice_create(struct connmgr *mgr, const char *target,
1916 uint32_t allowed_versions, uint8_t dscp)
1918 struct ofservice *ofservice;
1919 struct pvconn *pvconn;
1922 error = pvconn_open(target, allowed_versions, dscp, &pvconn);
1927 ofservice = xzalloc(sizeof *ofservice);
1928 hmap_insert(&mgr->services, &ofservice->node, hash_string(target, 0));
1929 ofservice->pvconn = pvconn;
1930 ofservice->allowed_versions = allowed_versions;
1936 ofservice_destroy(struct connmgr *mgr, struct ofservice *ofservice)
1938 hmap_remove(&mgr->services, &ofservice->node);
1939 pvconn_close(ofservice->pvconn);
1944 ofservice_reconfigure(struct ofservice *ofservice,
1945 const struct ofproto_controller *c)
1947 ofservice->probe_interval = c->probe_interval;
1948 ofservice->rate_limit = c->rate_limit;
1949 ofservice->burst_limit = c->burst_limit;
1950 ofservice->enable_async_msgs = c->enable_async_msgs;
1951 ofservice->dscp = c->dscp;
1954 /* Finds and returns the ofservice within 'mgr' that has the given
1955 * 'target', or a null pointer if none exists. */
1956 static struct ofservice *
1957 ofservice_lookup(struct connmgr *mgr, const char *target)
1959 struct ofservice *ofservice;
1961 HMAP_FOR_EACH_WITH_HASH (ofservice, node, hash_string(target, 0),
1963 if (!strcmp(pvconn_get_name(ofservice->pvconn), target)) {
1970 /* Flow monitors (NXST_FLOW_MONITOR). */
1972 /* A counter incremented when something significant happens to an OpenFlow
1975 * - When a rule is added, its 'add_seqno' and 'modify_seqno' are set to
1976 * the current value (which is then incremented).
1978 * - When a rule is modified, its 'modify_seqno' is set to the current
1979 * value (which is then incremented).
1981 * Thus, by comparing an old value of monitor_seqno against a rule's
1982 * 'add_seqno', one can tell whether the rule was added before or after the old
1983 * value was read, and similarly for 'modify_seqno'.
1985 * 32 bits should normally be sufficient (and would be nice, to save space in
1986 * each rule) but then we'd have to have some special cases for wraparound.
1988 * We initialize monitor_seqno to 1 to allow 0 to be used as an invalid
1990 static uint64_t monitor_seqno = 1;
1992 COVERAGE_DEFINE(ofmonitor_pause);
1993 COVERAGE_DEFINE(ofmonitor_resume);
1996 ofmonitor_create(const struct ofputil_flow_monitor_request *request,
1997 struct ofconn *ofconn, struct ofmonitor **monitorp)
1998 OVS_REQUIRES(ofproto_mutex)
2000 struct ofmonitor *m;
2004 m = ofmonitor_lookup(ofconn, request->id);
2006 return OFPERR_NXBRC_FM_DUPLICATE_ID;
2009 m = xmalloc(sizeof *m);
2011 hmap_insert(&ofconn->monitors, &m->ofconn_node, hash_int(request->id, 0));
2012 m->id = request->id;
2013 m->flags = request->flags;
2014 m->out_port = request->out_port;
2015 m->table_id = request->table_id;
2016 minimatch_init(&m->match, &request->match);
2023 ofmonitor_lookup(struct ofconn *ofconn, uint32_t id)
2024 OVS_REQUIRES(ofproto_mutex)
2026 struct ofmonitor *m;
2028 HMAP_FOR_EACH_IN_BUCKET (m, ofconn_node, hash_int(id, 0),
2029 &ofconn->monitors) {
2038 ofmonitor_destroy(struct ofmonitor *m)
2039 OVS_REQUIRES(ofproto_mutex)
2042 minimatch_destroy(&m->match);
2043 hmap_remove(&m->ofconn->monitors, &m->ofconn_node);
2049 ofmonitor_report(struct connmgr *mgr, struct rule *rule,
2050 enum nx_flow_update_event event,
2051 enum ofp_flow_removed_reason reason,
2052 const struct ofconn *abbrev_ofconn, ovs_be32 abbrev_xid)
2053 OVS_REQUIRES(ofproto_mutex)
2055 enum nx_flow_monitor_flags update;
2056 struct ofconn *ofconn;
2061 rule->add_seqno = rule->modify_seqno = monitor_seqno++;
2065 update = NXFMF_DELETE;
2068 case NXFME_MODIFIED:
2069 update = NXFMF_MODIFY;
2070 rule->modify_seqno = monitor_seqno++;
2078 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
2079 enum nx_flow_monitor_flags flags = 0;
2080 struct ofmonitor *m;
2082 if (ofconn->monitor_paused) {
2083 /* Only send NXFME_DELETED notifications for flows that were added
2084 * before we paused. */
2085 if (event != NXFME_DELETED
2086 || rule->add_seqno > ofconn->monitor_paused) {
2091 HMAP_FOR_EACH (m, ofconn_node, &ofconn->monitors) {
2092 if (m->flags & update
2093 && (m->table_id == 0xff || m->table_id == rule->table_id)
2094 && ofoperation_has_out_port(rule->pending, m->out_port)
2095 && cls_rule_is_loose_match(&rule->cr, &m->match)) {
2101 if (list_is_empty(&ofconn->updates)) {
2102 ofputil_start_flow_update(&ofconn->updates);
2103 ofconn->sent_abbrev_update = false;
2106 if (ofconn != abbrev_ofconn || ofconn->monitor_paused) {
2107 struct ofputil_flow_update fu;
2111 fu.reason = event == NXFME_DELETED ? reason : 0;
2112 fu.table_id = rule->table_id;
2113 fu.cookie = rule->flow_cookie;
2114 minimatch_expand(&rule->cr.match, &match);
2116 fu.priority = rule->cr.priority;
2118 ovs_mutex_lock(&rule->mutex);
2119 fu.idle_timeout = rule->idle_timeout;
2120 fu.hard_timeout = rule->hard_timeout;
2121 ovs_mutex_unlock(&rule->mutex);
2123 if (flags & NXFMF_ACTIONS) {
2124 const struct rule_actions *actions = rule_get_actions(rule);
2125 fu.ofpacts = actions->ofpacts;
2126 fu.ofpacts_len = actions->ofpacts_len;
2131 ofputil_append_flow_update(&fu, &ofconn->updates);
2132 } else if (!ofconn->sent_abbrev_update) {
2133 struct ofputil_flow_update fu;
2135 fu.event = NXFME_ABBREV;
2136 fu.xid = abbrev_xid;
2137 ofputil_append_flow_update(&fu, &ofconn->updates);
2139 ofconn->sent_abbrev_update = true;
2146 ofmonitor_flush(struct connmgr *mgr)
2147 OVS_REQUIRES(ofproto_mutex)
2149 struct ofconn *ofconn;
2151 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
2152 struct ofpbuf *msg, *next;
2154 LIST_FOR_EACH_SAFE (msg, next, list_node, &ofconn->updates) {
2155 unsigned int n_bytes;
2157 list_remove(&msg->list_node);
2158 ofconn_send(ofconn, msg, ofconn->monitor_counter);
2159 n_bytes = rconn_packet_counter_n_bytes(ofconn->monitor_counter);
2160 if (!ofconn->monitor_paused && n_bytes > 128 * 1024) {
2161 struct ofpbuf *pause;
2163 COVERAGE_INC(ofmonitor_pause);
2164 ofconn->monitor_paused = monitor_seqno++;
2165 pause = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_MONITOR_PAUSED,
2166 OFP10_VERSION, htonl(0), 0);
2167 ofconn_send(ofconn, pause, ofconn->monitor_counter);
2174 ofmonitor_resume(struct ofconn *ofconn)
2175 OVS_REQUIRES(ofproto_mutex)
2177 struct rule_collection rules;
2178 struct ofpbuf *resumed;
2179 struct ofmonitor *m;
2182 rule_collection_init(&rules);
2183 HMAP_FOR_EACH (m, ofconn_node, &ofconn->monitors) {
2184 ofmonitor_collect_resume_rules(m, ofconn->monitor_paused, &rules);
2188 ofmonitor_compose_refresh_updates(&rules, &msgs);
2190 resumed = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_MONITOR_RESUMED, OFP10_VERSION,
2192 list_push_back(&msgs, &resumed->list_node);
2193 ofconn_send_replies(ofconn, &msgs);
2195 ofconn->monitor_paused = 0;
2199 ofmonitor_may_resume(const struct ofconn *ofconn)
2200 OVS_REQUIRES(ofproto_mutex)
2202 return (ofconn->monitor_paused != 0
2203 && !rconn_packet_counter_n_packets(ofconn->monitor_counter));
2207 ofmonitor_run(struct connmgr *mgr)
2209 struct ofconn *ofconn;
2211 ovs_mutex_lock(&ofproto_mutex);
2212 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
2213 if (ofmonitor_may_resume(ofconn)) {
2214 COVERAGE_INC(ofmonitor_resume);
2215 ofmonitor_resume(ofconn);
2218 ovs_mutex_unlock(&ofproto_mutex);
2222 ofmonitor_wait(struct connmgr *mgr)
2224 struct ofconn *ofconn;
2226 ovs_mutex_lock(&ofproto_mutex);
2227 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
2228 if (ofmonitor_may_resume(ofconn)) {
2229 poll_immediate_wake();
2232 ovs_mutex_unlock(&ofproto_mutex);