2 * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
25 #include "fail-open.h"
30 #include "ofproto-provider.h"
32 #include "poll-loop.h"
41 VLOG_DEFINE_THIS_MODULE(connmgr);
42 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
44 /* An OpenFlow connection. */
46 /* Configuration that persists from one connection to the next. */
48 struct list node; /* In struct connmgr's "all_conns" list. */
49 struct hmap_node hmap_node; /* In struct connmgr's "controllers" map. */
51 struct connmgr *connmgr; /* Connection's manager. */
52 struct rconn *rconn; /* OpenFlow connection. */
53 enum ofconn_type type; /* Type. */
54 enum ofproto_band band; /* In-band or out-of-band? */
55 bool enable_async_msgs; /* Initially enable async messages? */
57 /* State that should be cleared from one connection to the next. */
60 enum nx_role role; /* Role. */
61 enum ofputil_protocol protocol; /* Current protocol variant. */
62 enum nx_packet_in_format packet_in_format; /* OFPT_PACKET_IN format. */
64 /* Asynchronous flow table operation support. */
65 struct list opgroups; /* Contains pending "ofopgroups", if any. */
66 struct ofpbuf *blocked; /* Postponed OpenFlow message, if any. */
67 bool retry; /* True if 'blocked' is ready to try again. */
69 /* OFPT_PACKET_IN related data. */
70 struct rconn_packet_counter *packet_in_counter; /* # queued on 'rconn'. */
71 #define N_SCHEDULERS 2
72 struct pinsched *schedulers[N_SCHEDULERS];
73 struct pktbuf *pktbuf; /* OpenFlow packet buffers. */
74 int miss_send_len; /* Bytes to send of buffered packets. */
75 uint16_t controller_id; /* Connection controller ID. */
77 /* Number of OpenFlow messages queued on 'rconn' as replies to OpenFlow
78 * requests, and the maximum number before we stop reading OpenFlow
80 #define OFCONN_REPLY_MAX 100
81 struct rconn_packet_counter *reply_counter;
83 /* Asynchronous message configuration in each possible roles.
85 * A 1-bit enables sending an asynchronous message for one possible reason
86 * that the message might be generated, a 0-bit disables it. */
87 uint32_t master_async_config[OAM_N_TYPES]; /* master, other */
88 uint32_t slave_async_config[OAM_N_TYPES]; /* slave */
91 static struct ofconn *ofconn_create(struct connmgr *, struct rconn *,
92 enum ofconn_type, bool enable_async_msgs);
93 static void ofconn_destroy(struct ofconn *);
94 static void ofconn_flush(struct ofconn *);
96 static void ofconn_reconfigure(struct ofconn *,
97 const struct ofproto_controller *);
99 static void ofconn_run(struct ofconn *,
100 bool (*handle_openflow)(struct ofconn *,
101 struct ofpbuf *ofp_msg));
102 static void ofconn_wait(struct ofconn *, bool handling_openflow);
104 static const char *ofconn_get_target(const struct ofconn *);
105 static char *ofconn_make_name(const struct connmgr *, const char *target);
107 static void ofconn_set_rate_limit(struct ofconn *, int rate, int burst);
109 static void ofconn_send(const struct ofconn *, struct ofpbuf *,
110 struct rconn_packet_counter *);
112 static void do_send_packet_in(struct ofpbuf *, void *ofconn_);
114 /* A listener for incoming OpenFlow "service" connections. */
116 struct hmap_node node; /* In struct connmgr's "services" hmap. */
117 struct pvconn *pvconn; /* OpenFlow connection listener. */
119 /* These are not used by ofservice directly. They are settings for
120 * accepted "struct ofconn"s from the pvconn. */
121 int probe_interval; /* Max idle time before probing, in seconds. */
122 int rate_limit; /* Max packet-in rate in packets per second. */
123 int burst_limit; /* Limit on accumulating packet credits. */
124 bool enable_async_msgs; /* Initially enable async messages? */
125 uint8_t dscp; /* DSCP Value for controller connection */
128 static void ofservice_reconfigure(struct ofservice *,
129 const struct ofproto_controller *);
130 static int ofservice_create(struct connmgr *, const char *target, uint8_t dscp);
131 static void ofservice_destroy(struct connmgr *, struct ofservice *);
132 static struct ofservice *ofservice_lookup(struct connmgr *,
135 /* Connection manager for an OpenFlow switch. */
137 struct ofproto *ofproto;
139 char *local_port_name;
141 /* OpenFlow connections. */
142 struct hmap controllers; /* Controller "struct ofconn"s. */
143 struct list all_conns; /* Contains "struct ofconn"s. */
145 /* OpenFlow listeners. */
146 struct hmap services; /* Contains "struct ofservice"s. */
147 struct pvconn **snoops;
151 struct fail_open *fail_open;
152 enum ofproto_fail_mode fail_mode;
154 /* In-band control. */
155 struct in_band *in_band;
156 struct sockaddr_in *extra_in_band_remotes;
157 size_t n_extra_remotes;
161 static void update_in_band_remotes(struct connmgr *);
162 static void add_snooper(struct connmgr *, struct vconn *);
164 /* Creates and returns a new connection manager owned by 'ofproto'. 'name' is
165 * a name for the ofproto suitable for using in log messages.
166 * 'local_port_name' is the name of the local port (OFPP_LOCAL) within
169 connmgr_create(struct ofproto *ofproto,
170 const char *name, const char *local_port_name)
174 mgr = xmalloc(sizeof *mgr);
175 mgr->ofproto = ofproto;
176 mgr->name = xstrdup(name);
177 mgr->local_port_name = xstrdup(local_port_name);
179 hmap_init(&mgr->controllers);
180 list_init(&mgr->all_conns);
182 hmap_init(&mgr->services);
186 mgr->fail_open = NULL;
187 mgr->fail_mode = OFPROTO_FAIL_SECURE;
190 mgr->extra_in_band_remotes = NULL;
191 mgr->n_extra_remotes = 0;
192 mgr->in_band_queue = -1;
197 /* Frees 'mgr' and all of its resources. */
199 connmgr_destroy(struct connmgr *mgr)
201 struct ofservice *ofservice, *next_ofservice;
202 struct ofconn *ofconn, *next_ofconn;
209 LIST_FOR_EACH_SAFE (ofconn, next_ofconn, node, &mgr->all_conns) {
210 ofconn_destroy(ofconn);
212 hmap_destroy(&mgr->controllers);
214 HMAP_FOR_EACH_SAFE (ofservice, next_ofservice, node, &mgr->services) {
215 ofservice_destroy(mgr, ofservice);
217 hmap_destroy(&mgr->services);
219 for (i = 0; i < mgr->n_snoops; i++) {
220 pvconn_close(mgr->snoops[i]);
224 fail_open_destroy(mgr->fail_open);
225 mgr->fail_open = NULL;
227 in_band_destroy(mgr->in_band);
229 free(mgr->extra_in_band_remotes);
231 free(mgr->local_port_name);
236 /* Does all of the periodic maintenance required by 'mgr'.
238 * If 'handle_openflow' is nonnull, calls 'handle_openflow' for each message
239 * received on an OpenFlow connection, passing along the OpenFlow connection
240 * itself and the message that was sent. If 'handle_openflow' returns true,
241 * the message is considered to be fully processed. If 'handle_openflow'
242 * returns false, the message is considered not to have been processed at all;
243 * it will be stored and re-presented to 'handle_openflow' following the next
244 * call to connmgr_retry(). 'handle_openflow' must not modify or free the
247 * If 'handle_openflow' is NULL, no OpenFlow messages will be processed and
248 * other activities that could affect the flow table (in-band processing,
249 * fail-open processing) are suppressed too. */
251 connmgr_run(struct connmgr *mgr,
252 bool (*handle_openflow)(struct ofconn *, struct ofpbuf *ofp_msg))
254 struct ofconn *ofconn, *next_ofconn;
255 struct ofservice *ofservice;
258 if (handle_openflow && mgr->in_band) {
259 if (!in_band_run(mgr->in_band)) {
260 in_band_destroy(mgr->in_band);
265 LIST_FOR_EACH_SAFE (ofconn, next_ofconn, node, &mgr->all_conns) {
266 ofconn_run(ofconn, handle_openflow);
269 /* Fail-open maintenance. Do this after processing the ofconns since
270 * fail-open checks the status of the controller rconn. */
271 if (handle_openflow && mgr->fail_open) {
272 fail_open_run(mgr->fail_open);
275 HMAP_FOR_EACH (ofservice, node, &mgr->services) {
279 retval = pvconn_accept(ofservice->pvconn, OFP10_VERSION, &vconn);
284 /* Passing default value for creation of the rconn */
285 rconn = rconn_create(ofservice->probe_interval, 0, ofservice->dscp);
286 name = ofconn_make_name(mgr, vconn_get_name(vconn));
287 rconn_connect_unreliably(rconn, vconn, name);
290 ofconn = ofconn_create(mgr, rconn, OFCONN_SERVICE,
291 ofservice->enable_async_msgs);
292 ofconn_set_rate_limit(ofconn, ofservice->rate_limit,
293 ofservice->burst_limit);
294 } else if (retval != EAGAIN) {
295 VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval));
299 for (i = 0; i < mgr->n_snoops; i++) {
303 retval = pvconn_accept(mgr->snoops[i], OFP10_VERSION, &vconn);
305 add_snooper(mgr, vconn);
306 } else if (retval != EAGAIN) {
307 VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval));
312 /* Causes the poll loop to wake up when connmgr_run() needs to run.
314 * If 'handling_openflow' is true, arriving OpenFlow messages and other
315 * activities that affect the flow table will wake up the poll loop. If
316 * 'handling_openflow' is false, they will not. */
318 connmgr_wait(struct connmgr *mgr, bool handling_openflow)
320 struct ofservice *ofservice;
321 struct ofconn *ofconn;
324 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
325 ofconn_wait(ofconn, handling_openflow);
327 if (handling_openflow && mgr->in_band) {
328 in_band_wait(mgr->in_band);
330 if (handling_openflow && mgr->fail_open) {
331 fail_open_wait(mgr->fail_open);
333 HMAP_FOR_EACH (ofservice, node, &mgr->services) {
334 pvconn_wait(ofservice->pvconn);
336 for (i = 0; i < mgr->n_snoops; i++) {
337 pvconn_wait(mgr->snoops[i]);
341 /* Returns the ofproto that owns 'ofconn''s connmgr. */
343 ofconn_get_ofproto(const struct ofconn *ofconn)
345 return ofconn->connmgr->ofproto;
348 /* If processing of OpenFlow messages was blocked on any 'mgr' ofconns by
349 * returning false to the 'handle_openflow' callback to connmgr_run(), this
350 * re-enables them. */
352 connmgr_retry(struct connmgr *mgr)
354 struct ofconn *ofconn;
356 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
357 ofconn->retry = true;
361 /* OpenFlow configuration. */
363 static void add_controller(struct connmgr *, const char *target, uint8_t dscp);
364 static struct ofconn *find_controller_by_target(struct connmgr *,
366 static void update_fail_open(struct connmgr *);
367 static int set_pvconns(struct pvconn ***pvconnsp, size_t *n_pvconnsp,
368 const struct sset *);
370 /* Returns true if 'mgr' has any configured primary controllers.
372 * Service controllers do not count, but configured primary controllers do
373 * count whether or not they are currently connected. */
375 connmgr_has_controllers(const struct connmgr *mgr)
377 return !hmap_is_empty(&mgr->controllers);
380 /* Initializes 'info' and populates it with information about each configured
381 * primary controller. The keys in 'info' are the controllers' targets; the
382 * data values are corresponding "struct ofproto_controller_info".
384 * The caller owns 'info' and everything in it and should free it when it is no
387 connmgr_get_controller_info(struct connmgr *mgr, struct shash *info)
389 const struct ofconn *ofconn;
391 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
392 const struct rconn *rconn = ofconn->rconn;
393 const char *target = rconn_get_target(rconn);
395 if (!shash_find(info, target)) {
396 struct ofproto_controller_info *cinfo = xmalloc(sizeof *cinfo);
397 time_t now = time_now();
398 time_t last_connection = rconn_get_last_connection(rconn);
399 time_t last_disconnect = rconn_get_last_disconnect(rconn);
400 int last_error = rconn_get_last_error(rconn);
402 shash_add(info, target, cinfo);
404 cinfo->is_connected = rconn_is_connected(rconn);
405 cinfo->role = ofconn->role;
410 cinfo->pairs.keys[cinfo->pairs.n] = "last_error";
411 cinfo->pairs.values[cinfo->pairs.n++]
412 = xstrdup(ovs_retval_to_string(last_error));
415 cinfo->pairs.keys[cinfo->pairs.n] = "state";
416 cinfo->pairs.values[cinfo->pairs.n++]
417 = xstrdup(rconn_get_state(rconn));
419 if (last_connection != TIME_MIN) {
420 cinfo->pairs.keys[cinfo->pairs.n] = "sec_since_connect";
421 cinfo->pairs.values[cinfo->pairs.n++]
422 = xasprintf("%ld", (long int) (now - last_connection));
425 if (last_disconnect != TIME_MIN) {
426 cinfo->pairs.keys[cinfo->pairs.n] = "sec_since_disconnect";
427 cinfo->pairs.values[cinfo->pairs.n++]
428 = xasprintf("%ld", (long int) (now - last_disconnect));
435 connmgr_free_controller_info(struct shash *info)
437 struct shash_node *node;
439 SHASH_FOR_EACH (node, info) {
440 struct ofproto_controller_info *cinfo = node->data;
441 while (cinfo->pairs.n) {
442 free((char *) cinfo->pairs.values[--cinfo->pairs.n]);
449 /* Changes 'mgr''s set of controllers to the 'n_controllers' controllers in
452 connmgr_set_controllers(struct connmgr *mgr,
453 const struct ofproto_controller *controllers,
454 size_t n_controllers)
456 bool had_controllers = connmgr_has_controllers(mgr);
457 struct shash new_controllers;
458 struct ofconn *ofconn, *next_ofconn;
459 struct ofservice *ofservice, *next_ofservice;
462 /* Create newly configured controllers and services.
463 * Create a name to ofproto_controller mapping in 'new_controllers'. */
464 shash_init(&new_controllers);
465 for (i = 0; i < n_controllers; i++) {
466 const struct ofproto_controller *c = &controllers[i];
468 if (!vconn_verify_name(c->target)) {
469 if (!find_controller_by_target(mgr, c->target)) {
470 VLOG_INFO("%s: added primary controller \"%s\"",
471 mgr->name, c->target);
472 add_controller(mgr, c->target, c->dscp);
474 } else if (!pvconn_verify_name(c->target)) {
475 if (!ofservice_lookup(mgr, c->target)) {
476 VLOG_INFO("%s: added service controller \"%s\"",
477 mgr->name, c->target);
478 ofservice_create(mgr, c->target, c->dscp);
481 VLOG_WARN_RL(&rl, "%s: unsupported controller \"%s\"",
482 mgr->name, c->target);
486 shash_add_once(&new_controllers, c->target, &controllers[i]);
489 /* Delete controllers that are no longer configured.
490 * Update configuration of all now-existing controllers. */
491 HMAP_FOR_EACH_SAFE (ofconn, next_ofconn, hmap_node, &mgr->controllers) {
492 const char *target = ofconn_get_target(ofconn);
493 struct ofproto_controller *c;
495 c = shash_find_data(&new_controllers, target);
497 VLOG_INFO("%s: removed primary controller \"%s\"",
499 ofconn_destroy(ofconn);
501 ofconn_reconfigure(ofconn, c);
505 /* Delete services that are no longer configured.
506 * Update configuration of all now-existing services. */
507 HMAP_FOR_EACH_SAFE (ofservice, next_ofservice, node, &mgr->services) {
508 const char *target = pvconn_get_name(ofservice->pvconn);
509 struct ofproto_controller *c;
511 c = shash_find_data(&new_controllers, target);
513 VLOG_INFO("%s: removed service controller \"%s\"",
515 ofservice_destroy(mgr, ofservice);
517 ofservice_reconfigure(ofservice, c);
521 shash_destroy(&new_controllers);
523 update_in_band_remotes(mgr);
524 update_fail_open(mgr);
525 if (had_controllers != connmgr_has_controllers(mgr)) {
526 ofproto_flush_flows(mgr->ofproto);
530 /* Drops the connections between 'mgr' and all of its primary and secondary
531 * controllers, forcing them to reconnect. */
533 connmgr_reconnect(const struct connmgr *mgr)
535 struct ofconn *ofconn;
537 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
538 rconn_reconnect(ofconn->rconn);
542 /* Sets the "snoops" for 'mgr' to the pvconn targets listed in 'snoops'.
544 * A "snoop" is a pvconn to which every OpenFlow message to or from the most
545 * important controller on 'mgr' is mirrored. */
547 connmgr_set_snoops(struct connmgr *mgr, const struct sset *snoops)
549 return set_pvconns(&mgr->snoops, &mgr->n_snoops, snoops);
552 /* Adds each of the snoops currently configured on 'mgr' to 'snoops'. */
554 connmgr_get_snoops(const struct connmgr *mgr, struct sset *snoops)
558 for (i = 0; i < mgr->n_snoops; i++) {
559 sset_add(snoops, pvconn_get_name(mgr->snoops[i]));
563 /* Returns true if 'mgr' has at least one snoop, false if it has none. */
565 connmgr_has_snoops(const struct connmgr *mgr)
567 return mgr->n_snoops > 0;
570 /* Creates a new controller for 'target' in 'mgr'. update_controller() needs
571 * to be called later to finish the new ofconn's configuration. */
573 add_controller(struct connmgr *mgr, const char *target, uint8_t dscp)
575 char *name = ofconn_make_name(mgr, target);
576 struct ofconn *ofconn;
578 ofconn = ofconn_create(mgr, rconn_create(5, 8, dscp), OFCONN_PRIMARY, true);
579 ofconn->pktbuf = pktbuf_create();
580 rconn_connect(ofconn->rconn, target, name);
581 hmap_insert(&mgr->controllers, &ofconn->hmap_node, hash_string(target, 0));
586 static struct ofconn *
587 find_controller_by_target(struct connmgr *mgr, const char *target)
589 struct ofconn *ofconn;
591 HMAP_FOR_EACH_WITH_HASH (ofconn, hmap_node,
592 hash_string(target, 0), &mgr->controllers) {
593 if (!strcmp(ofconn_get_target(ofconn), target)) {
601 update_in_band_remotes(struct connmgr *mgr)
603 struct sockaddr_in *addrs;
604 size_t max_addrs, n_addrs;
605 struct ofconn *ofconn;
608 /* Allocate enough memory for as many remotes as we could possibly have. */
609 max_addrs = mgr->n_extra_remotes + hmap_count(&mgr->controllers);
610 addrs = xmalloc(max_addrs * sizeof *addrs);
613 /* Add all the remotes. */
614 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
615 struct sockaddr_in *sin = &addrs[n_addrs];
616 const char *target = rconn_get_target(ofconn->rconn);
618 if (ofconn->band == OFPROTO_OUT_OF_BAND) {
622 if (stream_parse_target_with_default_ports(target,
629 for (i = 0; i < mgr->n_extra_remotes; i++) {
630 addrs[n_addrs++] = mgr->extra_in_band_remotes[i];
633 /* Create or update or destroy in-band. */
636 in_band_create(mgr->ofproto, mgr->local_port_name, &mgr->in_band);
638 in_band_set_queue(mgr->in_band, mgr->in_band_queue);
640 /* in_band_run() needs a chance to delete any existing in-band flows.
641 * We will destroy mgr->in_band after it's done with that. */
644 in_band_set_remotes(mgr->in_band, addrs, n_addrs);
652 update_fail_open(struct connmgr *mgr)
654 if (connmgr_has_controllers(mgr)
655 && mgr->fail_mode == OFPROTO_FAIL_STANDALONE) {
656 if (!mgr->fail_open) {
657 mgr->fail_open = fail_open_create(mgr->ofproto, mgr);
660 fail_open_destroy(mgr->fail_open);
661 mgr->fail_open = NULL;
666 set_pvconns(struct pvconn ***pvconnsp, size_t *n_pvconnsp,
667 const struct sset *sset)
669 struct pvconn **pvconns = *pvconnsp;
670 size_t n_pvconns = *n_pvconnsp;
675 for (i = 0; i < n_pvconns; i++) {
676 pvconn_close(pvconns[i]);
680 pvconns = xmalloc(sset_count(sset) * sizeof *pvconns);
682 SSET_FOR_EACH (name, sset) {
683 struct pvconn *pvconn;
686 error = pvconn_open(name, &pvconn, 0);
688 pvconns[n_pvconns++] = pvconn;
690 VLOG_ERR("failed to listen on %s: %s", name, strerror(error));
698 *n_pvconnsp = n_pvconns;
703 /* Returns a "preference level" for snooping 'ofconn'. A higher return value
704 * means that 'ofconn' is more interesting for monitoring than a lower return
707 snoop_preference(const struct ofconn *ofconn)
709 switch (ofconn->role) {
717 /* Shouldn't happen. */
722 /* One of 'mgr''s "snoop" pvconns has accepted a new connection on 'vconn'.
723 * Connects this vconn to a controller. */
725 add_snooper(struct connmgr *mgr, struct vconn *vconn)
727 struct ofconn *ofconn, *best;
729 /* Pick a controller for monitoring. */
731 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
732 if (ofconn->type == OFCONN_PRIMARY
733 && (!best || snoop_preference(ofconn) > snoop_preference(best))) {
739 rconn_add_monitor(best->rconn, vconn);
741 VLOG_INFO_RL(&rl, "no controller connection to snoop");
746 /* Public ofconn functions. */
748 /* Returns the connection type, either OFCONN_PRIMARY or OFCONN_SERVICE. */
750 ofconn_get_type(const struct ofconn *ofconn)
755 /* Returns the role configured for 'ofconn'.
757 * The default role, if no other role has been set, is NX_ROLE_OTHER. */
759 ofconn_get_role(const struct ofconn *ofconn)
764 /* Changes 'ofconn''s role to 'role'. If 'role' is NX_ROLE_MASTER then any
765 * existing master is demoted to a slave. */
767 ofconn_set_role(struct ofconn *ofconn, enum nx_role role)
769 if (role == NX_ROLE_MASTER) {
770 struct ofconn *other;
772 HMAP_FOR_EACH (other, hmap_node, &ofconn->connmgr->controllers) {
773 if (other->role == NX_ROLE_MASTER) {
774 other->role = NX_ROLE_SLAVE;
782 ofconn_set_invalid_ttl_to_controller(struct ofconn *ofconn, bool enable)
784 uint32_t bit = 1u << OFPR_INVALID_TTL;
786 ofconn->master_async_config[OAM_PACKET_IN] |= bit;
788 ofconn->master_async_config[OAM_PACKET_IN] &= ~bit;
793 ofconn_get_invalid_ttl_to_controller(struct ofconn *ofconn)
795 uint32_t bit = 1u << OFPR_INVALID_TTL;
796 return (ofconn->master_async_config[OAM_PACKET_IN] & bit) != 0;
799 /* Returns the currently configured protocol for 'ofconn', one of OFPUTIL_P_*.
801 * The default, if no other format has been set, is OFPUTIL_P_OPENFLOW10. */
802 enum ofputil_protocol
803 ofconn_get_protocol(struct ofconn *ofconn)
805 return ofconn->protocol;
808 /* Sets the protocol for 'ofconn' to 'protocol' (one of OFPUTIL_P_*).
810 * (This doesn't actually send anything to accomplish this. Presumably the
811 * caller already did that.) */
813 ofconn_set_protocol(struct ofconn *ofconn, enum ofputil_protocol protocol)
815 ofconn->protocol = protocol;
818 /* Returns the currently configured packet in format for 'ofconn', one of
821 * The default, if no other format has been set, is NXPIF_OPENFLOW10. */
822 enum nx_packet_in_format
823 ofconn_get_packet_in_format(struct ofconn *ofconn)
825 return ofconn->packet_in_format;
828 /* Sets the packet in format for 'ofconn' to 'packet_in_format' (one of
831 ofconn_set_packet_in_format(struct ofconn *ofconn,
832 enum nx_packet_in_format packet_in_format)
834 ofconn->packet_in_format = packet_in_format;
837 /* Sets the controller connection ID for 'ofconn' to 'controller_id'.
839 * The connection controller ID is used for OFPP_CONTROLLER and
840 * NXAST_CONTROLLER actions. See "struct nx_action_controller" for details. */
842 ofconn_set_controller_id(struct ofconn *ofconn, uint16_t controller_id)
844 ofconn->controller_id = controller_id;
847 /* Returns the default miss send length for 'ofconn'. */
849 ofconn_get_miss_send_len(const struct ofconn *ofconn)
851 return ofconn->miss_send_len;
854 /* Sets the default miss send length for 'ofconn' to 'miss_send_len'. */
856 ofconn_set_miss_send_len(struct ofconn *ofconn, int miss_send_len)
858 ofconn->miss_send_len = miss_send_len;
862 ofconn_set_async_config(struct ofconn *ofconn,
863 const uint32_t master_masks[OAM_N_TYPES],
864 const uint32_t slave_masks[OAM_N_TYPES])
866 size_t size = sizeof ofconn->master_async_config;
867 memcpy(ofconn->master_async_config, master_masks, size);
868 memcpy(ofconn->slave_async_config, slave_masks, size);
871 /* Sends 'msg' on 'ofconn', accounting it as a reply. (If there is a
872 * sufficient number of OpenFlow replies in-flight on a single ofconn, then the
873 * connmgr will stop accepting new OpenFlow requests on that ofconn until the
874 * controller has accepted some of the replies.) */
876 ofconn_send_reply(const struct ofconn *ofconn, struct ofpbuf *msg)
878 ofconn_send(ofconn, msg, ofconn->reply_counter);
881 /* Sends each of the messages in list 'replies' on 'ofconn' in order,
882 * accounting them as replies. */
884 ofconn_send_replies(const struct ofconn *ofconn, struct list *replies)
886 struct ofpbuf *reply, *next;
888 LIST_FOR_EACH_SAFE (reply, next, list_node, replies) {
889 list_remove(&reply->list_node);
890 ofconn_send_reply(ofconn, reply);
894 /* Sends 'error' on 'ofconn', as a reply to 'request'. Only at most the
895 * first 64 bytes of 'request' are used. */
897 ofconn_send_error(const struct ofconn *ofconn,
898 const struct ofp_header *request, enum ofperr error)
900 struct ofpbuf *reply;
902 reply = ofperr_encode_reply(error, request);
904 static struct vlog_rate_limit err_rl = VLOG_RATE_LIMIT_INIT(10, 10);
906 if (!VLOG_DROP_INFO(&err_rl)) {
907 const struct ofputil_msg_type *type;
908 const char *type_name;
911 request_len = ntohs(request->length);
912 type_name = (!ofputil_decode_msg_type_partial(request,
913 MIN(64, request_len),
915 ? ofputil_msg_type_name(type)
918 VLOG_INFO("%s: sending %s error reply to %s message",
919 rconn_get_name(ofconn->rconn), ofperr_to_string(error),
922 ofconn_send_reply(ofconn, reply);
926 /* Same as pktbuf_retrieve(), using the pktbuf owned by 'ofconn'. */
928 ofconn_pktbuf_retrieve(struct ofconn *ofconn, uint32_t id,
929 struct ofpbuf **bufferp, uint16_t *in_port)
931 return pktbuf_retrieve(ofconn->pktbuf, id, bufferp, in_port);
934 /* Returns true if 'ofconn' has any pending opgroups. */
936 ofconn_has_pending_opgroups(const struct ofconn *ofconn)
938 return !list_is_empty(&ofconn->opgroups);
941 /* Adds 'ofconn_node' to 'ofconn''s list of pending opgroups.
943 * If 'ofconn' is destroyed or its connection drops, then 'ofconn' will remove
944 * 'ofconn_node' from the list and re-initialize it with list_init(). The
945 * client may, therefore, use list_is_empty(ofconn_node) to determine whether
946 * 'ofconn_node' is still associated with an active ofconn.
948 * The client may also remove ofconn_node from the list itself, with
951 ofconn_add_opgroup(struct ofconn *ofconn, struct list *ofconn_node)
953 list_push_back(&ofconn->opgroups, ofconn_node);
956 /* Private ofconn functions. */
959 ofconn_get_target(const struct ofconn *ofconn)
961 return rconn_get_target(ofconn->rconn);
964 static struct ofconn *
965 ofconn_create(struct connmgr *mgr, struct rconn *rconn, enum ofconn_type type,
966 bool enable_async_msgs)
968 struct ofconn *ofconn;
970 ofconn = xzalloc(sizeof *ofconn);
971 ofconn->connmgr = mgr;
972 list_push_back(&mgr->all_conns, &ofconn->node);
973 ofconn->rconn = rconn;
975 ofconn->enable_async_msgs = enable_async_msgs;
977 list_init(&ofconn->opgroups);
979 ofconn_flush(ofconn);
984 /* Clears all of the state in 'ofconn' that should not persist from one
985 * connection to the next. */
987 ofconn_flush(struct ofconn *ofconn)
991 ofconn->role = NX_ROLE_OTHER;
992 ofconn->protocol = OFPUTIL_P_OF10;
993 ofconn->packet_in_format = NXPIF_OPENFLOW10;
995 /* Disassociate 'ofconn' from all of the ofopgroups that it initiated that
996 * have not yet completed. (Those ofopgroups will still run to completion
997 * in the usual way, but any errors that they run into will not be reported
998 * on any OpenFlow channel.)
1000 * Also discard any blocked operation on 'ofconn'. */
1001 while (!list_is_empty(&ofconn->opgroups)) {
1002 list_init(list_pop_front(&ofconn->opgroups));
1004 ofpbuf_delete(ofconn->blocked);
1005 ofconn->blocked = NULL;
1007 rconn_packet_counter_destroy(ofconn->packet_in_counter);
1008 ofconn->packet_in_counter = rconn_packet_counter_create();
1009 for (i = 0; i < N_SCHEDULERS; i++) {
1010 if (ofconn->schedulers[i]) {
1013 pinsched_get_limits(ofconn->schedulers[i], &rate, &burst);
1014 pinsched_destroy(ofconn->schedulers[i]);
1015 ofconn->schedulers[i] = pinsched_create(rate, burst);
1018 if (ofconn->pktbuf) {
1019 pktbuf_destroy(ofconn->pktbuf);
1020 ofconn->pktbuf = pktbuf_create();
1022 ofconn->miss_send_len = (ofconn->type == OFCONN_PRIMARY
1023 ? OFP_DEFAULT_MISS_SEND_LEN
1025 ofconn->controller_id = 0;
1027 rconn_packet_counter_destroy(ofconn->reply_counter);
1028 ofconn->reply_counter = rconn_packet_counter_create();
1030 if (ofconn->enable_async_msgs) {
1031 uint32_t *master = ofconn->master_async_config;
1032 uint32_t *slave = ofconn->slave_async_config;
1034 /* "master" and "other" roles get all asynchronous messages by default,
1035 * except that the controller needs to enable nonstandard "packet-in"
1036 * reasons itself. */
1037 master[OAM_PACKET_IN] = (1u << OFPR_NO_MATCH) | (1u << OFPR_ACTION);
1038 master[OAM_PORT_STATUS] = ((1u << OFPPR_ADD)
1039 | (1u << OFPPR_DELETE)
1040 | (1u << OFPPR_MODIFY));
1041 master[OAM_FLOW_REMOVED] = ((1u << OFPRR_IDLE_TIMEOUT)
1042 | (1u << OFPRR_HARD_TIMEOUT)
1043 | (1u << OFPRR_DELETE));
1045 /* "slave" role gets port status updates by default. */
1046 slave[OAM_PACKET_IN] = 0;
1047 slave[OAM_PORT_STATUS] = ((1u << OFPPR_ADD)
1048 | (1u << OFPPR_DELETE)
1049 | (1u << OFPPR_MODIFY));
1050 slave[OAM_FLOW_REMOVED] = 0;
1052 memset(ofconn->master_async_config, 0,
1053 sizeof ofconn->master_async_config);
1054 memset(ofconn->slave_async_config, 0,
1055 sizeof ofconn->slave_async_config);
1060 ofconn_destroy(struct ofconn *ofconn)
1062 ofconn_flush(ofconn);
1064 if (ofconn->type == OFCONN_PRIMARY) {
1065 hmap_remove(&ofconn->connmgr->controllers, &ofconn->hmap_node);
1068 list_remove(&ofconn->node);
1069 rconn_destroy(ofconn->rconn);
1070 rconn_packet_counter_destroy(ofconn->packet_in_counter);
1071 rconn_packet_counter_destroy(ofconn->reply_counter);
1072 pktbuf_destroy(ofconn->pktbuf);
1076 /* Reconfigures 'ofconn' to match 'c'. 'ofconn' and 'c' must have the same
1079 ofconn_reconfigure(struct ofconn *ofconn, const struct ofproto_controller *c)
1083 ofconn->band = c->band;
1084 ofconn->enable_async_msgs = c->enable_async_msgs;
1086 rconn_set_max_backoff(ofconn->rconn, c->max_backoff);
1088 probe_interval = c->probe_interval ? MAX(c->probe_interval, 5) : 0;
1089 rconn_set_probe_interval(ofconn->rconn, probe_interval);
1091 ofconn_set_rate_limit(ofconn, c->rate_limit, c->burst_limit);
1094 /* Returns true if it makes sense for 'ofconn' to receive and process OpenFlow
1097 ofconn_may_recv(const struct ofconn *ofconn)
1099 int count = rconn_packet_counter_read (ofconn->reply_counter);
1100 return (!ofconn->blocked || ofconn->retry) && count < OFCONN_REPLY_MAX;
1104 ofconn_run(struct ofconn *ofconn,
1105 bool (*handle_openflow)(struct ofconn *, struct ofpbuf *ofp_msg))
1107 struct connmgr *mgr = ofconn->connmgr;
1110 for (i = 0; i < N_SCHEDULERS; i++) {
1111 pinsched_run(ofconn->schedulers[i], do_send_packet_in, ofconn);
1114 rconn_run(ofconn->rconn);
1116 if (handle_openflow) {
1117 /* Limit the number of iterations to avoid starving other tasks. */
1118 for (i = 0; i < 50 && ofconn_may_recv(ofconn); i++) {
1119 struct ofpbuf *of_msg;
1121 of_msg = (ofconn->blocked
1123 : rconn_recv(ofconn->rconn));
1127 if (mgr->fail_open) {
1128 fail_open_maybe_recover(mgr->fail_open);
1131 if (handle_openflow(ofconn, of_msg)) {
1132 ofpbuf_delete(of_msg);
1133 ofconn->blocked = NULL;
1135 ofconn->blocked = of_msg;
1136 ofconn->retry = false;
1141 if (!rconn_is_alive(ofconn->rconn)) {
1142 ofconn_destroy(ofconn);
1143 } else if (!rconn_is_connected(ofconn->rconn)) {
1144 ofconn_flush(ofconn);
1149 ofconn_wait(struct ofconn *ofconn, bool handling_openflow)
1153 for (i = 0; i < N_SCHEDULERS; i++) {
1154 pinsched_wait(ofconn->schedulers[i]);
1156 rconn_run_wait(ofconn->rconn);
1157 if (handling_openflow && ofconn_may_recv(ofconn)) {
1158 rconn_recv_wait(ofconn->rconn);
1162 /* Returns true if 'ofconn' should receive asynchronous messages of the given
1163 * OAM_* 'type' and 'reason', which should be a OFPR_* value for OAM_PACKET_IN,
1164 * a OFPPR_* value for OAM_PORT_STATUS, or an OFPRR_* value for
1165 * OAM_FLOW_REMOVED. Returns false if the message should not be sent on
1168 ofconn_receives_async_msg(const struct ofconn *ofconn,
1169 enum ofconn_async_msg_type type,
1170 unsigned int reason)
1172 const uint32_t *async_config;
1174 assert(reason < 32);
1175 assert((unsigned int) type < OAM_N_TYPES);
1177 if (!rconn_is_connected(ofconn->rconn)) {
1181 /* Keep the following code in sync with the documentation in the
1182 * "Asynchronous Messages" section in DESIGN. */
1184 if (ofconn->type == OFCONN_SERVICE && !ofconn->miss_send_len) {
1185 /* Service connections don't get asynchronous messages unless they have
1186 * explicitly asked for them by setting a nonzero miss send length. */
1190 async_config = (ofconn->role == NX_ROLE_SLAVE
1191 ? ofconn->slave_async_config
1192 : ofconn->master_async_config);
1193 if (!(async_config[type] & (1u << reason))) {
1200 /* Returns a human-readable name for an OpenFlow connection between 'mgr' and
1201 * 'target', suitable for use in log messages for identifying the connection.
1203 * The name is dynamically allocated. The caller should free it (with free())
1204 * when it is no longer needed. */
1206 ofconn_make_name(const struct connmgr *mgr, const char *target)
1208 return xasprintf("%s<->%s", mgr->name, target);
1212 ofconn_set_rate_limit(struct ofconn *ofconn, int rate, int burst)
1216 for (i = 0; i < N_SCHEDULERS; i++) {
1217 struct pinsched **s = &ofconn->schedulers[i];
1221 *s = pinsched_create(rate, burst);
1223 pinsched_set_limits(*s, rate, burst);
1226 pinsched_destroy(*s);
1233 ofconn_send(const struct ofconn *ofconn, struct ofpbuf *msg,
1234 struct rconn_packet_counter *counter)
1236 update_openflow_length(msg);
1237 rconn_send(ofconn->rconn, msg, counter);
1240 /* Sending asynchronous messages. */
1242 static void schedule_packet_in(struct ofconn *, struct ofputil_packet_in);
1244 /* Sends an OFPT_PORT_STATUS message with 'opp' and 'reason' to appropriate
1245 * controllers managed by 'mgr'. */
1247 connmgr_send_port_status(struct connmgr *mgr,
1248 const struct ofputil_phy_port *pp, uint8_t reason)
1250 /* XXX Should limit the number of queued port status change messages. */
1251 struct ofputil_port_status ps;
1252 struct ofconn *ofconn;
1256 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1257 if (ofconn_receives_async_msg(ofconn, OAM_PORT_STATUS, reason)) {
1260 msg = ofputil_encode_port_status(&ps, ofconn->protocol);
1261 ofconn_send(ofconn, msg, NULL);
1266 /* Sends an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message based on 'fr' to
1267 * appropriate controllers managed by 'mgr'. */
1269 connmgr_send_flow_removed(struct connmgr *mgr,
1270 const struct ofputil_flow_removed *fr)
1272 struct ofconn *ofconn;
1274 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1275 if (ofconn_receives_async_msg(ofconn, OAM_FLOW_REMOVED, fr->reason)) {
1278 /* Account flow expirations as replies to OpenFlow requests. That
1279 * works because preventing OpenFlow requests from being processed
1280 * also prevents new flows from being added (and expiring). (It
1281 * also prevents processing OpenFlow requests that would not add
1282 * new flows, so it is imperfect.) */
1283 msg = ofputil_encode_flow_removed(fr, ofconn->protocol);
1284 ofconn_send_reply(ofconn, msg);
1289 /* Given 'pin', sends an OFPT_PACKET_IN message to each OpenFlow controller as
1290 * necessary according to their individual configurations.
1292 * The caller doesn't need to fill in pin->buffer_id or pin->total_len. */
1294 connmgr_send_packet_in(struct connmgr *mgr,
1295 const struct ofputil_packet_in *pin)
1297 struct ofconn *ofconn;
1299 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1300 if (ofconn_receives_async_msg(ofconn, OAM_PACKET_IN, pin->reason)
1301 && ofconn->controller_id == pin->controller_id) {
1302 schedule_packet_in(ofconn, *pin);
1307 /* pinsched callback for sending 'ofp_packet_in' on 'ofconn'. */
1309 do_send_packet_in(struct ofpbuf *ofp_packet_in, void *ofconn_)
1311 struct ofconn *ofconn = ofconn_;
1313 rconn_send_with_limit(ofconn->rconn, ofp_packet_in,
1314 ofconn->packet_in_counter, 100);
1317 /* Takes 'pin', composes an OpenFlow packet-in message from it, and passes it
1318 * to 'ofconn''s packet scheduler for sending. */
1320 schedule_packet_in(struct ofconn *ofconn, struct ofputil_packet_in pin)
1322 struct connmgr *mgr = ofconn->connmgr;
1324 pin.total_len = pin.packet_len;
1326 /* Get OpenFlow buffer_id. */
1327 if (pin.reason == OFPR_ACTION) {
1328 pin.buffer_id = UINT32_MAX;
1329 } else if (mgr->fail_open && fail_open_is_active(mgr->fail_open)) {
1330 pin.buffer_id = pktbuf_get_null();
1331 } else if (!ofconn->pktbuf) {
1332 pin.buffer_id = UINT32_MAX;
1334 pin.buffer_id = pktbuf_save(ofconn->pktbuf, pin.packet, pin.packet_len,
1338 /* Figure out how much of the packet to send. */
1339 if (pin.reason == OFPR_NO_MATCH) {
1340 pin.send_len = pin.packet_len;
1342 /* Caller should have initialized 'send_len' to 'max_len' specified in
1343 * struct ofp_action_output. */
1345 if (pin.buffer_id != UINT32_MAX) {
1346 pin.send_len = MIN(pin.send_len, ofconn->miss_send_len);
1349 /* Make OFPT_PACKET_IN and hand over to packet scheduler. It might
1350 * immediately call into do_send_packet_in() or it might buffer it for a
1351 * while (until a later call to pinsched_run()). */
1352 pinsched_send(ofconn->schedulers[pin.reason == OFPR_NO_MATCH ? 0 : 1],
1354 ofputil_encode_packet_in(&pin, ofconn->packet_in_format),
1355 do_send_packet_in, ofconn);
1358 /* Fail-open settings. */
1360 /* Returns the failure handling mode (OFPROTO_FAIL_SECURE or
1361 * OFPROTO_FAIL_STANDALONE) for 'mgr'. */
1362 enum ofproto_fail_mode
1363 connmgr_get_fail_mode(const struct connmgr *mgr)
1365 return mgr->fail_mode;
1368 /* Sets the failure handling mode for 'mgr' to 'fail_mode' (either
1369 * OFPROTO_FAIL_SECURE or OFPROTO_FAIL_STANDALONE). */
1371 connmgr_set_fail_mode(struct connmgr *mgr, enum ofproto_fail_mode fail_mode)
1373 if (mgr->fail_mode != fail_mode) {
1374 mgr->fail_mode = fail_mode;
1375 update_fail_open(mgr);
1376 if (!connmgr_has_controllers(mgr)) {
1377 ofproto_flush_flows(mgr->ofproto);
1382 /* Fail-open implementation. */
1384 /* Returns the longest probe interval among the primary controllers configured
1385 * on 'mgr'. Returns 0 if there are no primary controllers. */
1387 connmgr_get_max_probe_interval(const struct connmgr *mgr)
1389 const struct ofconn *ofconn;
1390 int max_probe_interval;
1392 max_probe_interval = 0;
1393 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1394 int probe_interval = rconn_get_probe_interval(ofconn->rconn);
1395 max_probe_interval = MAX(max_probe_interval, probe_interval);
1397 return max_probe_interval;
1400 /* Returns the number of seconds for which all of 'mgr's primary controllers
1401 * have been disconnected. Returns 0 if 'mgr' has no primary controllers. */
1403 connmgr_failure_duration(const struct connmgr *mgr)
1405 const struct ofconn *ofconn;
1406 int min_failure_duration;
1408 if (!connmgr_has_controllers(mgr)) {
1412 min_failure_duration = INT_MAX;
1413 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1414 int failure_duration = rconn_failure_duration(ofconn->rconn);
1415 min_failure_duration = MIN(min_failure_duration, failure_duration);
1417 return min_failure_duration;
1420 /* Returns true if at least one primary controller is connected (regardless of
1421 * whether those controllers are believed to have authenticated and accepted
1422 * this switch), false if none of them are connected. */
1424 connmgr_is_any_controller_connected(const struct connmgr *mgr)
1426 const struct ofconn *ofconn;
1428 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1429 if (rconn_is_connected(ofconn->rconn)) {
1436 /* Returns true if at least one primary controller is believed to have
1437 * authenticated and accepted this switch, false otherwise. */
1439 connmgr_is_any_controller_admitted(const struct connmgr *mgr)
1441 const struct ofconn *ofconn;
1443 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1444 if (rconn_is_admitted(ofconn->rconn)) {
1451 /* In-band configuration. */
1453 static bool any_extras_changed(const struct connmgr *,
1454 const struct sockaddr_in *extras, size_t n);
1456 /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'mgr''s
1457 * in-band control should guarantee access, in the same way that in-band
1458 * control guarantees access to OpenFlow controllers. */
1460 connmgr_set_extra_in_band_remotes(struct connmgr *mgr,
1461 const struct sockaddr_in *extras, size_t n)
1463 if (!any_extras_changed(mgr, extras, n)) {
1467 free(mgr->extra_in_band_remotes);
1468 mgr->n_extra_remotes = n;
1469 mgr->extra_in_band_remotes = xmemdup(extras, n * sizeof *extras);
1471 update_in_band_remotes(mgr);
1474 /* Sets the OpenFlow queue used by flows set up by in-band control on
1475 * 'mgr' to 'queue_id'. If 'queue_id' is negative, then in-band control
1476 * flows will use the default queue. */
1478 connmgr_set_in_band_queue(struct connmgr *mgr, int queue_id)
1480 if (queue_id != mgr->in_band_queue) {
1481 mgr->in_band_queue = queue_id;
1482 update_in_band_remotes(mgr);
1487 any_extras_changed(const struct connmgr *mgr,
1488 const struct sockaddr_in *extras, size_t n)
1492 if (n != mgr->n_extra_remotes) {
1496 for (i = 0; i < n; i++) {
1497 const struct sockaddr_in *old = &mgr->extra_in_band_remotes[i];
1498 const struct sockaddr_in *new = &extras[i];
1500 if (old->sin_addr.s_addr != new->sin_addr.s_addr ||
1501 old->sin_port != new->sin_port) {
1509 /* In-band implementation. */
1512 connmgr_msg_in_hook(struct connmgr *mgr, const struct flow *flow,
1513 const struct ofpbuf *packet)
1515 return mgr->in_band && in_band_msg_in_hook(mgr->in_band, flow, packet);
1519 connmgr_may_set_up_flow(struct connmgr *mgr, const struct flow *flow,
1520 const struct nlattr *odp_actions,
1523 return !mgr->in_band || in_band_rule_check(flow, odp_actions, actions_len);
1526 /* Fail-open and in-band implementation. */
1528 /* Called by 'ofproto' after all flows have been flushed, to allow fail-open
1529 * and standalone mode to re-create their flows.
1531 * In-band control has more sophisticated code that manages flows itself. */
1533 connmgr_flushed(struct connmgr *mgr)
1535 if (mgr->fail_open) {
1536 fail_open_flushed(mgr->fail_open);
1539 /* If there are no controllers and we're in standalone mode, set up a flow
1540 * that matches every packet and directs them to OFPP_NORMAL (which goes to
1541 * us). Otherwise, the switch is in secure mode and we won't pass any
1542 * traffic until a controller has been defined and it tells us to do so. */
1543 if (!connmgr_has_controllers(mgr)
1544 && mgr->fail_mode == OFPROTO_FAIL_STANDALONE) {
1545 union ofp_action action;
1546 struct cls_rule rule;
1548 memset(&action, 0, sizeof action);
1549 action.type = htons(OFPAT10_OUTPUT);
1550 action.output.len = htons(sizeof action);
1551 action.output.port = htons(OFPP_NORMAL);
1552 cls_rule_init_catchall(&rule, 0);
1553 ofproto_add_flow(mgr->ofproto, &rule, &action, 1);
1557 /* Creates a new ofservice for 'target' in 'mgr'. Returns 0 if successful,
1558 * otherwise a positive errno value.
1560 * ofservice_reconfigure() must be called to fully configure the new
1563 ofservice_create(struct connmgr *mgr, const char *target, uint8_t dscp)
1565 struct ofservice *ofservice;
1566 struct pvconn *pvconn;
1569 error = pvconn_open(target, &pvconn, dscp);
1574 ofservice = xzalloc(sizeof *ofservice);
1575 hmap_insert(&mgr->services, &ofservice->node, hash_string(target, 0));
1576 ofservice->pvconn = pvconn;
1582 ofservice_destroy(struct connmgr *mgr, struct ofservice *ofservice)
1584 hmap_remove(&mgr->services, &ofservice->node);
1585 pvconn_close(ofservice->pvconn);
1590 ofservice_reconfigure(struct ofservice *ofservice,
1591 const struct ofproto_controller *c)
1593 ofservice->probe_interval = c->probe_interval;
1594 ofservice->rate_limit = c->rate_limit;
1595 ofservice->burst_limit = c->burst_limit;
1596 ofservice->enable_async_msgs = c->enable_async_msgs;
1597 ofservice->dscp = c->dscp;
1600 /* Finds and returns the ofservice within 'mgr' that has the given
1601 * 'target', or a null pointer if none exists. */
1602 static struct ofservice *
1603 ofservice_lookup(struct connmgr *mgr, const char *target)
1605 struct ofservice *ofservice;
1607 HMAP_FOR_EACH_WITH_HASH (ofservice, node, hash_string(target, 0),
1609 if (!strcmp(pvconn_get_name(ofservice->pvconn), target)) {