]> git.proxmox.com Git - ovs.git/blame - ofproto/connmgr.c
dpif-netdev: Remove redundant hash action handling.
[ovs.git] / ofproto / connmgr.c
CommitLineData
19a87e36 1/*
e731d71b 2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
19a87e36
BP
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#include <config.h>
18
19#include "connmgr.h"
20
21#include <errno.h>
22#include <stdlib.h>
23
24#include "coverage.h"
696d1bcf 25#include "dynamic-string.h"
19a87e36
BP
26#include "fail-open.h"
27#include "in-band.h"
28#include "odp-util.h"
f25d0cf3 29#include "ofp-actions.h"
982697a4 30#include "ofp-msgs.h"
19a87e36
BP
31#include "ofp-util.h"
32#include "ofpbuf.h"
5bee6e26 33#include "ofproto-provider.h"
19a87e36
BP
34#include "pinsched.h"
35#include "poll-loop.h"
36#include "pktbuf.h"
37#include "rconn.h"
38#include "shash.h"
0d085684 39#include "simap.h"
5bd31620 40#include "stream.h"
19a87e36
BP
41#include "timeval.h"
42#include "vconn.h"
43#include "vlog.h"
44
777af88d
AC
45#include "bundles.h"
46
19a87e36
BP
47VLOG_DEFINE_THIS_MODULE(connmgr);
48static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
49
3b0c6b56
BP
50/* An OpenFlow connection.
51 *
52 *
53 * Thread-safety
54 * =============
55 *
56 * 'ofproto_mutex' must be held whenever an ofconn is created or destroyed or,
57 * more or less equivalently, whenever an ofconn is added to or removed from a
4cd1bc9d
BP
58 * connmgr. 'ofproto_mutex' doesn't protect the data inside the ofconn, except
59 * as specifically noted below. */
19a87e36 60struct ofconn {
c4b31872
BP
61/* Configuration that persists from one connection to the next. */
62
19a87e36 63 struct list node; /* In struct connmgr's "all_conns" list. */
c4b31872
BP
64 struct hmap_node hmap_node; /* In struct connmgr's "controllers" map. */
65
66 struct connmgr *connmgr; /* Connection's manager. */
19a87e36
BP
67 struct rconn *rconn; /* OpenFlow connection. */
68 enum ofconn_type type; /* Type. */
c4b31872 69 enum ofproto_band band; /* In-band or out-of-band? */
9886b662 70 bool enable_async_msgs; /* Initially enable async messages? */
c4b31872
BP
71
72/* State that should be cleared from one connection to the next. */
73
74 /* OpenFlow state. */
f4f1ea7e 75 enum ofp12_controller_role role; /* Role. */
27527aa0 76 enum ofputil_protocol protocol; /* Current protocol variant. */
54834960 77 enum nx_packet_in_format packet_in_format; /* OFPT_PACKET_IN format. */
19a87e36
BP
78
79 /* OFPT_PACKET_IN related data. */
80 struct rconn_packet_counter *packet_in_counter; /* # queued on 'rconn'. */
81#define N_SCHEDULERS 2
82 struct pinsched *schedulers[N_SCHEDULERS];
83 struct pktbuf *pktbuf; /* OpenFlow packet buffers. */
84 int miss_send_len; /* Bytes to send of buffered packets. */
a7349929 85 uint16_t controller_id; /* Connection controller ID. */
19a87e36
BP
86
87 /* Number of OpenFlow messages queued on 'rconn' as replies to OpenFlow
88 * requests, and the maximum number before we stop reading OpenFlow
89 * requests. */
90#define OFCONN_REPLY_MAX 100
91 struct rconn_packet_counter *reply_counter;
80d5aefd
BP
92
93 /* Asynchronous message configuration in each possible roles.
94 *
95 * A 1-bit enables sending an asynchronous message for one possible reason
96 * that the message might be generated, a 0-bit disables it. */
97 uint32_t master_async_config[OAM_N_TYPES]; /* master, other */
98 uint32_t slave_async_config[OAM_N_TYPES]; /* slave */
2b07c8b1 99
696d1bcf
BP
100 /* Flow table operation logging. */
101 int n_add, n_delete, n_modify; /* Number of unreported ops of each kind. */
102 long long int first_op, last_op; /* Range of times for unreported ops. */
103 long long int next_op_report; /* Time to report ops, or LLONG_MAX. */
104 long long int op_backoff; /* Earliest time to report ops again. */
105
4cd1bc9d
BP
106/* Flow monitors (e.g. NXST_FLOW_MONITOR). */
107
108 /* Configuration. Contains "struct ofmonitor"s. */
109 struct hmap monitors OVS_GUARDED_BY(ofproto_mutex);
110
111 /* Flow control.
112 *
113 * When too many flow monitor notifications back up in the transmit buffer,
114 * we pause the transmission of further notifications. These members track
115 * the flow control state.
116 *
117 * When notifications are flowing, 'monitor_paused' is 0. When
118 * notifications are paused, 'monitor_paused' is the value of
119 * 'monitor_seqno' at the point we paused.
120 *
121 * 'monitor_counter' counts the OpenFlow messages and bytes currently in
122 * flight. This value growing too large triggers pausing. */
123 uint64_t monitor_paused OVS_GUARDED_BY(ofproto_mutex);
124 struct rconn_packet_counter *monitor_counter OVS_GUARDED_BY(ofproto_mutex);
125
126 /* State of monitors for a single ongoing flow_mod.
127 *
128 * 'updates' is a list of "struct ofpbuf"s that contain
129 * NXST_FLOW_MONITOR_REPLY messages representing the changes made by the
130 * current flow_mod.
131 *
132 * When 'updates' is nonempty, 'sent_abbrev_update' is true if 'updates'
133 * contains an update event of type NXFME_ABBREV and false otherwise.. */
134 struct list updates OVS_GUARDED_BY(ofproto_mutex);
135 bool sent_abbrev_update OVS_GUARDED_BY(ofproto_mutex);
777af88d
AC
136
137 /* Active bundles. Contains "struct ofp_bundle"s. */
138 struct hmap bundles;
19a87e36
BP
139};
140
141static struct ofconn *ofconn_create(struct connmgr *, struct rconn *,
3b0c6b56
BP
142 enum ofconn_type, bool enable_async_msgs)
143 OVS_REQUIRES(ofproto_mutex);
144static void ofconn_destroy(struct ofconn *) OVS_REQUIRES(ofproto_mutex);
145static void ofconn_flush(struct ofconn *) OVS_REQUIRES(ofproto_mutex);
19a87e36
BP
146
147static void ofconn_reconfigure(struct ofconn *,
148 const struct ofproto_controller *);
149
150static void ofconn_run(struct ofconn *,
b20f4073 151 void (*handle_openflow)(struct ofconn *,
e03248b7 152 const struct ofpbuf *ofp_msg));
b20f4073 153static void ofconn_wait(struct ofconn *);
19a87e36 154
696d1bcf
BP
155static void ofconn_log_flow_mods(struct ofconn *);
156
19a87e36
BP
157static const char *ofconn_get_target(const struct ofconn *);
158static char *ofconn_make_name(const struct connmgr *, const char *target);
159
160static void ofconn_set_rate_limit(struct ofconn *, int rate, int burst);
161
19a87e36
BP
162static void ofconn_send(const struct ofconn *, struct ofpbuf *,
163 struct rconn_packet_counter *);
164
a6f75961 165static void do_send_packet_ins(struct ofconn *, struct list *txq);
19a87e36
BP
166
167/* A listener for incoming OpenFlow "service" connections. */
168struct ofservice {
169 struct hmap_node node; /* In struct connmgr's "services" hmap. */
170 struct pvconn *pvconn; /* OpenFlow connection listener. */
171
172 /* These are not used by ofservice directly. They are settings for
173 * accepted "struct ofconn"s from the pvconn. */
174 int probe_interval; /* Max idle time before probing, in seconds. */
175 int rate_limit; /* Max packet-in rate in packets per second. */
176 int burst_limit; /* Limit on accumulating packet credits. */
9886b662 177 bool enable_async_msgs; /* Initially enable async messages? */
f125905c 178 uint8_t dscp; /* DSCP Value for controller connection */
90ef0206
SH
179 uint32_t allowed_versions; /* OpenFlow protocol versions that may
180 * be negotiated for a session. */
19a87e36
BP
181};
182
183static void ofservice_reconfigure(struct ofservice *,
184 const struct ofproto_controller *);
00401ef0
SH
185static int ofservice_create(struct connmgr *mgr, const char *target,
186 uint32_t allowed_versions, uint8_t dscp);
19a87e36
BP
187static void ofservice_destroy(struct connmgr *, struct ofservice *);
188static struct ofservice *ofservice_lookup(struct connmgr *,
189 const char *target);
190
191/* Connection manager for an OpenFlow switch. */
192struct connmgr {
193 struct ofproto *ofproto;
194 char *name;
195 char *local_port_name;
196
197 /* OpenFlow connections. */
ab66e29f
BP
198 struct hmap controllers; /* All OFCONN_PRIMARY controllers. */
199 struct list all_conns; /* All controllers. */
6ea4776b
JR
200 uint64_t master_election_id; /* monotonically increasing sequence number
201 * for master election */
202 bool master_election_id_defined;
19a87e36
BP
203
204 /* OpenFlow listeners. */
205 struct hmap services; /* Contains "struct ofservice"s. */
206 struct pvconn **snoops;
207 size_t n_snoops;
208
209 /* Fail open. */
210 struct fail_open *fail_open;
211 enum ofproto_fail_mode fail_mode;
212
213 /* In-band control. */
214 struct in_band *in_band;
19a87e36
BP
215 struct sockaddr_in *extra_in_band_remotes;
216 size_t n_extra_remotes;
217 int in_band_queue;
218};
219
220static void update_in_band_remotes(struct connmgr *);
221static void add_snooper(struct connmgr *, struct vconn *);
2b07c8b1
BP
222static void ofmonitor_run(struct connmgr *);
223static void ofmonitor_wait(struct connmgr *);
19a87e36
BP
224
225/* Creates and returns a new connection manager owned by 'ofproto'. 'name' is
226 * a name for the ofproto suitable for using in log messages.
227 * 'local_port_name' is the name of the local port (OFPP_LOCAL) within
228 * 'ofproto'. */
229struct connmgr *
230connmgr_create(struct ofproto *ofproto,
231 const char *name, const char *local_port_name)
232{
233 struct connmgr *mgr;
234
235 mgr = xmalloc(sizeof *mgr);
236 mgr->ofproto = ofproto;
237 mgr->name = xstrdup(name);
238 mgr->local_port_name = xstrdup(local_port_name);
239
240 hmap_init(&mgr->controllers);
241 list_init(&mgr->all_conns);
6ea4776b
JR
242 mgr->master_election_id = 0;
243 mgr->master_election_id_defined = false;
19a87e36
BP
244
245 hmap_init(&mgr->services);
246 mgr->snoops = NULL;
247 mgr->n_snoops = 0;
248
249 mgr->fail_open = NULL;
250 mgr->fail_mode = OFPROTO_FAIL_SECURE;
251
252 mgr->in_band = NULL;
19a87e36
BP
253 mgr->extra_in_band_remotes = NULL;
254 mgr->n_extra_remotes = 0;
255 mgr->in_band_queue = -1;
256
257 return mgr;
258}
259
260/* Frees 'mgr' and all of its resources. */
261void
262connmgr_destroy(struct connmgr *mgr)
263{
264 struct ofservice *ofservice, *next_ofservice;
265 struct ofconn *ofconn, *next_ofconn;
266 size_t i;
267
268 if (!mgr) {
269 return;
270 }
271
3b0c6b56 272 ovs_mutex_lock(&ofproto_mutex);
19a87e36
BP
273 LIST_FOR_EACH_SAFE (ofconn, next_ofconn, node, &mgr->all_conns) {
274 ofconn_destroy(ofconn);
275 }
3b0c6b56
BP
276 ovs_mutex_unlock(&ofproto_mutex);
277
19a87e36
BP
278 hmap_destroy(&mgr->controllers);
279
280 HMAP_FOR_EACH_SAFE (ofservice, next_ofservice, node, &mgr->services) {
281 ofservice_destroy(mgr, ofservice);
282 }
283 hmap_destroy(&mgr->services);
284
285 for (i = 0; i < mgr->n_snoops; i++) {
286 pvconn_close(mgr->snoops[i]);
287 }
288 free(mgr->snoops);
289
290 fail_open_destroy(mgr->fail_open);
291 mgr->fail_open = NULL;
292
293 in_band_destroy(mgr->in_band);
294 mgr->in_band = NULL;
295 free(mgr->extra_in_band_remotes);
296 free(mgr->name);
297 free(mgr->local_port_name);
298
299 free(mgr);
300}
301
b20f4073
BP
302/* Does all of the periodic maintenance required by 'mgr'. Calls
303 * 'handle_openflow' for each message received on an OpenFlow connection,
304 * passing along the OpenFlow connection itself and the message that was sent.
305 * 'handle_openflow' must not modify or free the message. */
19a87e36
BP
306void
307connmgr_run(struct connmgr *mgr,
b20f4073 308 void (*handle_openflow)(struct ofconn *,
e03248b7 309 const struct ofpbuf *ofp_msg))
15aaf599 310 OVS_EXCLUDED(ofproto_mutex)
19a87e36
BP
311{
312 struct ofconn *ofconn, *next_ofconn;
313 struct ofservice *ofservice;
314 size_t i;
315
b20f4073 316 if (mgr->in_band) {
6da1e809
BP
317 if (!in_band_run(mgr->in_band)) {
318 in_band_destroy(mgr->in_band);
319 mgr->in_band = NULL;
320 }
19a87e36
BP
321 }
322
323 LIST_FOR_EACH_SAFE (ofconn, next_ofconn, node, &mgr->all_conns) {
324 ofconn_run(ofconn, handle_openflow);
325 }
2b07c8b1 326 ofmonitor_run(mgr);
19a87e36
BP
327
328 /* Fail-open maintenance. Do this after processing the ofconns since
329 * fail-open checks the status of the controller rconn. */
b20f4073 330 if (mgr->fail_open) {
19a87e36
BP
331 fail_open_run(mgr->fail_open);
332 }
333
334 HMAP_FOR_EACH (ofservice, node, &mgr->services) {
335 struct vconn *vconn;
336 int retval;
337
7a25bd99 338 retval = pvconn_accept(ofservice->pvconn, &vconn);
19a87e36
BP
339 if (!retval) {
340 struct rconn *rconn;
341 char *name;
342
f125905c 343 /* Passing default value for creation of the rconn */
1d9ffc17
SH
344 rconn = rconn_create(ofservice->probe_interval, 0, ofservice->dscp,
345 vconn_get_allowed_versions(vconn));
19a87e36
BP
346 name = ofconn_make_name(mgr, vconn_get_name(vconn));
347 rconn_connect_unreliably(rconn, vconn, name);
348 free(name);
349
3b0c6b56 350 ovs_mutex_lock(&ofproto_mutex);
9886b662
BP
351 ofconn = ofconn_create(mgr, rconn, OFCONN_SERVICE,
352 ofservice->enable_async_msgs);
3b0c6b56
BP
353 ovs_mutex_unlock(&ofproto_mutex);
354
19a87e36
BP
355 ofconn_set_rate_limit(ofconn, ofservice->rate_limit,
356 ofservice->burst_limit);
357 } else if (retval != EAGAIN) {
10a89ef0 358 VLOG_WARN_RL(&rl, "accept failed (%s)", ovs_strerror(retval));
19a87e36
BP
359 }
360 }
361
362 for (i = 0; i < mgr->n_snoops; i++) {
363 struct vconn *vconn;
364 int retval;
365
7a25bd99 366 retval = pvconn_accept(mgr->snoops[i], &vconn);
19a87e36
BP
367 if (!retval) {
368 add_snooper(mgr, vconn);
369 } else if (retval != EAGAIN) {
10a89ef0 370 VLOG_WARN_RL(&rl, "accept failed (%s)", ovs_strerror(retval));
19a87e36
BP
371 }
372 }
373}
374
b20f4073 375/* Causes the poll loop to wake up when connmgr_run() needs to run. */
19a87e36 376void
b20f4073 377connmgr_wait(struct connmgr *mgr)
19a87e36
BP
378{
379 struct ofservice *ofservice;
380 struct ofconn *ofconn;
381 size_t i;
382
383 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
b20f4073 384 ofconn_wait(ofconn);
19a87e36 385 }
2b07c8b1 386 ofmonitor_wait(mgr);
b20f4073 387 if (mgr->in_band) {
19a87e36
BP
388 in_band_wait(mgr->in_band);
389 }
b20f4073 390 if (mgr->fail_open) {
19a87e36
BP
391 fail_open_wait(mgr->fail_open);
392 }
393 HMAP_FOR_EACH (ofservice, node, &mgr->services) {
394 pvconn_wait(ofservice->pvconn);
395 }
396 for (i = 0; i < mgr->n_snoops; i++) {
397 pvconn_wait(mgr->snoops[i]);
398 }
399}
400
0d085684
BP
401/* Adds some memory usage statistics for 'mgr' into 'usage', for use with
402 * memory_report(). */
403void
404connmgr_get_memory_usage(const struct connmgr *mgr, struct simap *usage)
405{
406 const struct ofconn *ofconn;
407 unsigned int packets = 0;
408 unsigned int ofconns = 0;
409
410 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
411 int i;
412
413 ofconns++;
414
415 packets += rconn_count_txqlen(ofconn->rconn);
416 for (i = 0; i < N_SCHEDULERS; i++) {
a413195e
BP
417 struct pinsched_stats stats;
418
419 pinsched_get_stats(ofconn->schedulers[i], &stats);
420 packets += stats.n_queued;;
0d085684
BP
421 }
422 packets += pktbuf_count_packets(ofconn->pktbuf);
423 }
424 simap_increase(usage, "ofconns", ofconns);
425 simap_increase(usage, "packets", packets);
426}
427
19a87e36
BP
428/* Returns the ofproto that owns 'ofconn''s connmgr. */
429struct ofproto *
430ofconn_get_ofproto(const struct ofconn *ofconn)
431{
432 return ofconn->connmgr->ofproto;
433}
434\f
435/* OpenFlow configuration. */
436
1d9ffc17 437static void add_controller(struct connmgr *, const char *target, uint8_t dscp,
3b0c6b56
BP
438 uint32_t allowed_versions)
439 OVS_REQUIRES(ofproto_mutex);
19a87e36
BP
440static struct ofconn *find_controller_by_target(struct connmgr *,
441 const char *target);
feb8a80b 442static void update_fail_open(struct connmgr *) OVS_EXCLUDED(ofproto_mutex);
19a87e36 443static int set_pvconns(struct pvconn ***pvconnsp, size_t *n_pvconnsp,
81e2083f 444 const struct sset *);
19a87e36
BP
445
446/* Returns true if 'mgr' has any configured primary controllers.
447 *
448 * Service controllers do not count, but configured primary controllers do
449 * count whether or not they are currently connected. */
450bool
451connmgr_has_controllers(const struct connmgr *mgr)
452{
453 return !hmap_is_empty(&mgr->controllers);
454}
455
456/* Initializes 'info' and populates it with information about each configured
457 * primary controller. The keys in 'info' are the controllers' targets; the
458 * data values are corresponding "struct ofproto_controller_info".
459 *
460 * The caller owns 'info' and everything in it and should free it when it is no
461 * longer needed. */
462void
463connmgr_get_controller_info(struct connmgr *mgr, struct shash *info)
464{
465 const struct ofconn *ofconn;
466
19a87e36
BP
467 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
468 const struct rconn *rconn = ofconn->rconn;
5d279086 469 const char *target = rconn_get_target(rconn);
19a87e36 470
5d279086
AE
471 if (!shash_find(info, target)) {
472 struct ofproto_controller_info *cinfo = xmalloc(sizeof *cinfo);
473 time_t now = time_now();
474 time_t last_connection = rconn_get_last_connection(rconn);
475 time_t last_disconnect = rconn_get_last_disconnect(rconn);
476 int last_error = rconn_get_last_error(rconn);
a413195e 477 int i;
19a87e36 478
5d279086 479 shash_add(info, target, cinfo);
19a87e36 480
5d279086
AE
481 cinfo->is_connected = rconn_is_connected(rconn);
482 cinfo->role = ofconn->role;
19a87e36 483
f5ad2e5f 484 smap_init(&cinfo->pairs);
5d279086 485 if (last_error) {
f5ad2e5f
BP
486 smap_add(&cinfo->pairs, "last_error",
487 ovs_retval_to_string(last_error));
5d279086 488 }
19a87e36 489
f5ad2e5f 490 smap_add(&cinfo->pairs, "state", rconn_get_state(rconn));
5d279086
AE
491
492 if (last_connection != TIME_MIN) {
f5ad2e5f
BP
493 smap_add_format(&cinfo->pairs, "sec_since_connect",
494 "%ld", (long int) (now - last_connection));
5d279086
AE
495 }
496
497 if (last_disconnect != TIME_MIN) {
f5ad2e5f
BP
498 smap_add_format(&cinfo->pairs, "sec_since_disconnect",
499 "%ld", (long int) (now - last_disconnect));
5d279086 500 }
a413195e
BP
501
502 for (i = 0; i < N_SCHEDULERS; i++) {
503 if (ofconn->schedulers[i]) {
504 const char *name = i ? "miss" : "action";
505 struct pinsched_stats stats;
506
507 pinsched_get_stats(ofconn->schedulers[i], &stats);
508 smap_add_nocopy(&cinfo->pairs,
509 xasprintf("packet-in-%s-backlog", name),
510 xasprintf("%u", stats.n_queued));
511 smap_add_nocopy(&cinfo->pairs,
512 xasprintf("packet-in-%s-bypassed", name),
513 xasprintf("%llu", stats.n_normal));
514 smap_add_nocopy(&cinfo->pairs,
515 xasprintf("packet-in-%s-queued", name),
516 xasprintf("%llu", stats.n_limited));
517 smap_add_nocopy(&cinfo->pairs,
518 xasprintf("packet-in-%s-dropped", name),
519 xasprintf("%llu", stats.n_queue_dropped));
520 }
521 }
19a87e36
BP
522 }
523 }
524}
525
72ba2ed3
AE
526void
527connmgr_free_controller_info(struct shash *info)
528{
529 struct shash_node *node;
530
531 SHASH_FOR_EACH (node, info) {
532 struct ofproto_controller_info *cinfo = node->data;
f5ad2e5f 533 smap_destroy(&cinfo->pairs);
72ba2ed3
AE
534 free(cinfo);
535 }
536 shash_destroy(info);
537}
538
19a87e36
BP
539/* Changes 'mgr''s set of controllers to the 'n_controllers' controllers in
540 * 'controllers'. */
541void
542connmgr_set_controllers(struct connmgr *mgr,
543 const struct ofproto_controller *controllers,
1d9ffc17 544 size_t n_controllers, uint32_t allowed_versions)
3b0c6b56 545 OVS_EXCLUDED(ofproto_mutex)
19a87e36 546{
08fd19f0 547 bool had_controllers = connmgr_has_controllers(mgr);
19a87e36
BP
548 struct shash new_controllers;
549 struct ofconn *ofconn, *next_ofconn;
550 struct ofservice *ofservice, *next_ofservice;
19a87e36
BP
551 size_t i;
552
3b0c6b56
BP
553 /* Required to add and remove ofconns. This could probably be narrowed to
554 * cover a smaller amount of code, if that yielded some benefit. */
555 ovs_mutex_lock(&ofproto_mutex);
556
19a87e36
BP
557 /* Create newly configured controllers and services.
558 * Create a name to ofproto_controller mapping in 'new_controllers'. */
559 shash_init(&new_controllers);
560 for (i = 0; i < n_controllers; i++) {
561 const struct ofproto_controller *c = &controllers[i];
562
563 if (!vconn_verify_name(c->target)) {
90ef0206
SH
564 bool add = false;
565 ofconn = find_controller_by_target(mgr, c->target);
566 if (!ofconn) {
1ea9e609
BP
567 VLOG_INFO("%s: added primary controller \"%s\"",
568 mgr->name, c->target);
90ef0206
SH
569 add = true;
570 } else if (rconn_get_allowed_versions(ofconn->rconn) !=
571 allowed_versions) {
572 VLOG_INFO("%s: re-added primary controller \"%s\"",
573 mgr->name, c->target);
574 add = true;
575 ofconn_destroy(ofconn);
576 }
577 if (add) {
1d9ffc17 578 add_controller(mgr, c->target, c->dscp, allowed_versions);
19a87e36
BP
579 }
580 } else if (!pvconn_verify_name(c->target)) {
90ef0206
SH
581 bool add = false;
582 ofservice = ofservice_lookup(mgr, c->target);
583 if (!ofservice) {
1ea9e609
BP
584 VLOG_INFO("%s: added service controller \"%s\"",
585 mgr->name, c->target);
90ef0206
SH
586 add = true;
587 } else if (ofservice->allowed_versions != allowed_versions) {
588 VLOG_INFO("%s: re-added service controller \"%s\"",
589 mgr->name, c->target);
590 ofservice_destroy(mgr, ofservice);
591 add = true;
592 }
593 if (add) {
1d9ffc17 594 ofservice_create(mgr, c->target, allowed_versions, c->dscp);
19a87e36
BP
595 }
596 } else {
597 VLOG_WARN_RL(&rl, "%s: unsupported controller \"%s\"",
598 mgr->name, c->target);
599 continue;
600 }
601
602 shash_add_once(&new_controllers, c->target, &controllers[i]);
603 }
604
605 /* Delete controllers that are no longer configured.
606 * Update configuration of all now-existing controllers. */
19a87e36 607 HMAP_FOR_EACH_SAFE (ofconn, next_ofconn, hmap_node, &mgr->controllers) {
1ea9e609 608 const char *target = ofconn_get_target(ofconn);
19a87e36
BP
609 struct ofproto_controller *c;
610
1ea9e609 611 c = shash_find_data(&new_controllers, target);
19a87e36 612 if (!c) {
1ea9e609
BP
613 VLOG_INFO("%s: removed primary controller \"%s\"",
614 mgr->name, target);
19a87e36
BP
615 ofconn_destroy(ofconn);
616 } else {
617 ofconn_reconfigure(ofconn, c);
618 }
619 }
620
621 /* Delete services that are no longer configured.
622 * Update configuration of all now-existing services. */
623 HMAP_FOR_EACH_SAFE (ofservice, next_ofservice, node, &mgr->services) {
1ea9e609 624 const char *target = pvconn_get_name(ofservice->pvconn);
19a87e36
BP
625 struct ofproto_controller *c;
626
1ea9e609 627 c = shash_find_data(&new_controllers, target);
19a87e36 628 if (!c) {
1ea9e609
BP
629 VLOG_INFO("%s: removed service controller \"%s\"",
630 mgr->name, target);
19a87e36
BP
631 ofservice_destroy(mgr, ofservice);
632 } else {
633 ofservice_reconfigure(ofservice, c);
634 }
635 }
636
637 shash_destroy(&new_controllers);
638
c7be3f55
BP
639 ovs_mutex_unlock(&ofproto_mutex);
640
19a87e36
BP
641 update_in_band_remotes(mgr);
642 update_fail_open(mgr);
08fd19f0
BP
643 if (had_controllers != connmgr_has_controllers(mgr)) {
644 ofproto_flush_flows(mgr->ofproto);
645 }
19a87e36
BP
646}
647
648/* Drops the connections between 'mgr' and all of its primary and secondary
649 * controllers, forcing them to reconnect. */
650void
651connmgr_reconnect(const struct connmgr *mgr)
652{
653 struct ofconn *ofconn;
654
655 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
656 rconn_reconnect(ofconn->rconn);
657 }
658}
659
660/* Sets the "snoops" for 'mgr' to the pvconn targets listed in 'snoops'.
661 *
662 * A "snoop" is a pvconn to which every OpenFlow message to or from the most
663 * important controller on 'mgr' is mirrored. */
664int
81e2083f 665connmgr_set_snoops(struct connmgr *mgr, const struct sset *snoops)
19a87e36
BP
666{
667 return set_pvconns(&mgr->snoops, &mgr->n_snoops, snoops);
668}
669
670/* Adds each of the snoops currently configured on 'mgr' to 'snoops'. */
671void
81e2083f 672connmgr_get_snoops(const struct connmgr *mgr, struct sset *snoops)
19a87e36
BP
673{
674 size_t i;
675
676 for (i = 0; i < mgr->n_snoops; i++) {
81e2083f 677 sset_add(snoops, pvconn_get_name(mgr->snoops[i]));
19a87e36
BP
678 }
679}
680
81e2083f
BP
681/* Returns true if 'mgr' has at least one snoop, false if it has none. */
682bool
683connmgr_has_snoops(const struct connmgr *mgr)
684{
685 return mgr->n_snoops > 0;
686}
687
19a87e36
BP
688/* Creates a new controller for 'target' in 'mgr'. update_controller() needs
689 * to be called later to finish the new ofconn's configuration. */
690static void
1d9ffc17
SH
691add_controller(struct connmgr *mgr, const char *target, uint8_t dscp,
692 uint32_t allowed_versions)
3b0c6b56 693 OVS_REQUIRES(ofproto_mutex)
19a87e36
BP
694{
695 char *name = ofconn_make_name(mgr, target);
696 struct ofconn *ofconn;
697
1d9ffc17 698 ofconn = ofconn_create(mgr, rconn_create(5, 8, dscp, allowed_versions),
6042457b 699 OFCONN_PRIMARY, true);
19a87e36 700 ofconn->pktbuf = pktbuf_create();
19a87e36
BP
701 rconn_connect(ofconn->rconn, target, name);
702 hmap_insert(&mgr->controllers, &ofconn->hmap_node, hash_string(target, 0));
703
704 free(name);
705}
706
707static struct ofconn *
708find_controller_by_target(struct connmgr *mgr, const char *target)
709{
710 struct ofconn *ofconn;
711
712 HMAP_FOR_EACH_WITH_HASH (ofconn, hmap_node,
713 hash_string(target, 0), &mgr->controllers) {
714 if (!strcmp(ofconn_get_target(ofconn), target)) {
715 return ofconn;
716 }
717 }
718 return NULL;
719}
720
721static void
722update_in_band_remotes(struct connmgr *mgr)
723{
724 struct sockaddr_in *addrs;
725 size_t max_addrs, n_addrs;
726 struct ofconn *ofconn;
727 size_t i;
728
729 /* Allocate enough memory for as many remotes as we could possibly have. */
730 max_addrs = mgr->n_extra_remotes + hmap_count(&mgr->controllers);
731 addrs = xmalloc(max_addrs * sizeof *addrs);
732 n_addrs = 0;
733
734 /* Add all the remotes. */
735 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
ac4c900d 736 const char *target = rconn_get_target(ofconn->rconn);
02334943
JR
737 union {
738 struct sockaddr_storage ss;
739 struct sockaddr_in in;
740 } sa;
19a87e36 741
e731d71b 742 if (ofconn->band == OFPROTO_IN_BAND
02334943
JR
743 && stream_parse_target_with_default_port(target, OFP_OLD_PORT,
744 &sa.ss)
745 && sa.ss.ss_family == AF_INET) {
746 addrs[n_addrs++] = sa.in;
19a87e36
BP
747 }
748 }
749 for (i = 0; i < mgr->n_extra_remotes; i++) {
750 addrs[n_addrs++] = mgr->extra_in_band_remotes[i];
751 }
752
753 /* Create or update or destroy in-band. */
754 if (n_addrs) {
755 if (!mgr->in_band) {
756 in_band_create(mgr->ofproto, mgr->local_port_name, &mgr->in_band);
757 }
19a87e36 758 in_band_set_queue(mgr->in_band, mgr->in_band_queue);
19a87e36 759 } else {
6da1e809
BP
760 /* in_band_run() needs a chance to delete any existing in-band flows.
761 * We will destroy mgr->in_band after it's done with that. */
762 }
763 if (mgr->in_band) {
764 in_band_set_remotes(mgr->in_band, addrs, n_addrs);
19a87e36
BP
765 }
766
767 /* Clean up. */
768 free(addrs);
769}
770
771static void
772update_fail_open(struct connmgr *mgr)
feb8a80b 773 OVS_EXCLUDED(ofproto_mutex)
19a87e36
BP
774{
775 if (connmgr_has_controllers(mgr)
776 && mgr->fail_mode == OFPROTO_FAIL_STANDALONE) {
777 if (!mgr->fail_open) {
778 mgr->fail_open = fail_open_create(mgr->ofproto, mgr);
779 }
780 } else {
781 fail_open_destroy(mgr->fail_open);
782 mgr->fail_open = NULL;
783 }
784}
785
786static int
787set_pvconns(struct pvconn ***pvconnsp, size_t *n_pvconnsp,
81e2083f 788 const struct sset *sset)
19a87e36
BP
789{
790 struct pvconn **pvconns = *pvconnsp;
791 size_t n_pvconns = *n_pvconnsp;
81e2083f 792 const char *name;
19a87e36
BP
793 int retval = 0;
794 size_t i;
795
796 for (i = 0; i < n_pvconns; i++) {
797 pvconn_close(pvconns[i]);
798 }
799 free(pvconns);
800
81e2083f 801 pvconns = xmalloc(sset_count(sset) * sizeof *pvconns);
19a87e36 802 n_pvconns = 0;
81e2083f 803 SSET_FOR_EACH (name, sset) {
19a87e36
BP
804 struct pvconn *pvconn;
805 int error;
82c8c53c 806 error = pvconn_open(name, 0, 0, &pvconn);
19a87e36
BP
807 if (!error) {
808 pvconns[n_pvconns++] = pvconn;
809 } else {
10a89ef0 810 VLOG_ERR("failed to listen on %s: %s", name, ovs_strerror(error));
19a87e36
BP
811 if (!retval) {
812 retval = error;
813 }
814 }
815 }
816
817 *pvconnsp = pvconns;
818 *n_pvconnsp = n_pvconns;
819
820 return retval;
821}
822
823/* Returns a "preference level" for snooping 'ofconn'. A higher return value
824 * means that 'ofconn' is more interesting for monitoring than a lower return
825 * value. */
826static int
827snoop_preference(const struct ofconn *ofconn)
828{
829 switch (ofconn->role) {
f4f1ea7e 830 case OFPCR12_ROLE_MASTER:
19a87e36 831 return 3;
f4f1ea7e 832 case OFPCR12_ROLE_EQUAL:
19a87e36 833 return 2;
f4f1ea7e 834 case OFPCR12_ROLE_SLAVE:
19a87e36 835 return 1;
f4f1ea7e 836 case OFPCR12_ROLE_NOCHANGE:
19a87e36
BP
837 default:
838 /* Shouldn't happen. */
839 return 0;
840 }
841}
842
843/* One of 'mgr''s "snoop" pvconns has accepted a new connection on 'vconn'.
844 * Connects this vconn to a controller. */
845static void
846add_snooper(struct connmgr *mgr, struct vconn *vconn)
847{
848 struct ofconn *ofconn, *best;
849
850 /* Pick a controller for monitoring. */
851 best = NULL;
852 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
853 if (ofconn->type == OFCONN_PRIMARY
854 && (!best || snoop_preference(ofconn) > snoop_preference(best))) {
855 best = ofconn;
856 }
857 }
858
859 if (best) {
860 rconn_add_monitor(best->rconn, vconn);
861 } else {
862 VLOG_INFO_RL(&rl, "no controller connection to snoop");
863 vconn_close(vconn);
864 }
865}
866\f
867/* Public ofconn functions. */
868
869/* Returns the connection type, either OFCONN_PRIMARY or OFCONN_SERVICE. */
870enum ofconn_type
871ofconn_get_type(const struct ofconn *ofconn)
872{
873 return ofconn->type;
874}
875
147cc9d3
BP
876/* If a master election id is defined, stores it into '*idp' and returns
877 * true. Otherwise, stores UINT64_MAX into '*idp' and returns false. */
878bool
879ofconn_get_master_election_id(const struct ofconn *ofconn, uint64_t *idp)
880{
881 *idp = (ofconn->connmgr->master_election_id_defined
882 ? ofconn->connmgr->master_election_id
883 : UINT64_MAX);
884 return ofconn->connmgr->master_election_id_defined;
885}
886
6ea4776b
JR
887/* Sets the master election id.
888 *
889 * Returns true if successful, false if the id is stale
890 */
891bool
892ofconn_set_master_election_id(struct ofconn *ofconn, uint64_t id)
893{
894 if (ofconn->connmgr->master_election_id_defined
895 &&
896 /* Unsigned difference interpreted as a two's complement signed
897 * value */
898 (int64_t)(id - ofconn->connmgr->master_election_id) < 0) {
899 return false;
900 }
901 ofconn->connmgr->master_election_id = id;
902 ofconn->connmgr->master_election_id_defined = true;
903
904 return true;
905}
906
19a87e36
BP
907/* Returns the role configured for 'ofconn'.
908 *
f4f1ea7e
BP
909 * The default role, if no other role has been set, is OFPCR12_ROLE_EQUAL. */
910enum ofp12_controller_role
19a87e36
BP
911ofconn_get_role(const struct ofconn *ofconn)
912{
913 return ofconn->role;
914}
915
00467f73
AC
916void
917ofconn_send_role_status(struct ofconn *ofconn, uint32_t role, uint8_t reason)
918{
919 struct ofputil_role_status status;
920 struct ofpbuf *buf;
921
922 status.reason = reason;
923 status.role = role;
924 ofconn_get_master_election_id(ofconn, &status.generation_id);
925
926 buf = ofputil_encode_role_status(&status, ofconn_get_protocol(ofconn));
6751a4b4
BP
927 if (buf) {
928 ofconn_send(ofconn, buf, NULL);
929 }
00467f73
AC
930}
931
f4f1ea7e
BP
932/* Changes 'ofconn''s role to 'role'. If 'role' is OFPCR12_ROLE_MASTER then
933 * any existing master is demoted to a slave. */
19a87e36 934void
f4f1ea7e 935ofconn_set_role(struct ofconn *ofconn, enum ofp12_controller_role role)
19a87e36 936{
00467f73 937 if (role != ofconn->role && role == OFPCR12_ROLE_MASTER) {
19a87e36
BP
938 struct ofconn *other;
939
ab66e29f 940 LIST_FOR_EACH (other, node, &ofconn->connmgr->all_conns) {
f4f1ea7e
BP
941 if (other->role == OFPCR12_ROLE_MASTER) {
942 other->role = OFPCR12_ROLE_SLAVE;
00467f73 943 ofconn_send_role_status(other, OFPCR12_ROLE_SLAVE, OFPCRR_MASTER_REQUEST);
19a87e36
BP
944 }
945 }
946 }
947 ofconn->role = role;
948}
949
f0fd1a17 950void
80d5aefd 951ofconn_set_invalid_ttl_to_controller(struct ofconn *ofconn, bool enable)
f0fd1a17 952{
80d5aefd
BP
953 uint32_t bit = 1u << OFPR_INVALID_TTL;
954 if (enable) {
955 ofconn->master_async_config[OAM_PACKET_IN] |= bit;
956 } else {
957 ofconn->master_async_config[OAM_PACKET_IN] &= ~bit;
958 }
f0fd1a17
PS
959}
960
961bool
962ofconn_get_invalid_ttl_to_controller(struct ofconn *ofconn)
963{
80d5aefd
BP
964 uint32_t bit = 1u << OFPR_INVALID_TTL;
965 return (ofconn->master_async_config[OAM_PACKET_IN] & bit) != 0;
f0fd1a17
PS
966}
967
27527aa0 968/* Returns the currently configured protocol for 'ofconn', one of OFPUTIL_P_*.
19a87e36 969 *
eb12aa89
BP
970 * Returns OFPUTIL_P_NONE, which is not a valid protocol, if 'ofconn' hasn't
971 * completed version negotiation. This can't happen if at least one OpenFlow
972 * message, other than OFPT_HELLO, has been received on the connection (such as
973 * in ofproto.c's message handling code), since version negotiation is a
974 * prerequisite for starting to receive messages. This means that
975 * OFPUTIL_P_NONE is a special case that most callers need not worry about. */
27527aa0 976enum ofputil_protocol
e3d560db 977ofconn_get_protocol(const struct ofconn *ofconn)
19a87e36 978{
eb12aa89
BP
979 if (ofconn->protocol == OFPUTIL_P_NONE &&
980 rconn_is_connected(ofconn->rconn)) {
981 int version = rconn_get_version(ofconn->rconn);
982 if (version > 0) {
983 ofconn_set_protocol(CONST_CAST(struct ofconn *, ofconn),
984 ofputil_protocol_from_ofp_version(version));
985 }
986 }
987
27527aa0 988 return ofconn->protocol;
19a87e36
BP
989}
990
27527aa0
BP
991/* Sets the protocol for 'ofconn' to 'protocol' (one of OFPUTIL_P_*).
992 *
993 * (This doesn't actually send anything to accomplish this. Presumably the
994 * caller already did that.) */
19a87e36 995void
27527aa0 996ofconn_set_protocol(struct ofconn *ofconn, enum ofputil_protocol protocol)
19a87e36 997{
27527aa0 998 ofconn->protocol = protocol;
19a87e36
BP
999}
1000
54834960
EJ
1001/* Returns the currently configured packet in format for 'ofconn', one of
1002 * NXPIF_*.
1003 *
1004 * The default, if no other format has been set, is NXPIF_OPENFLOW10. */
1005enum nx_packet_in_format
1006ofconn_get_packet_in_format(struct ofconn *ofconn)
1007{
1008 return ofconn->packet_in_format;
1009}
1010
1011/* Sets the packet in format for 'ofconn' to 'packet_in_format' (one of
1012 * NXPIF_*). */
1013void
1014ofconn_set_packet_in_format(struct ofconn *ofconn,
1015 enum nx_packet_in_format packet_in_format)
1016{
1017 ofconn->packet_in_format = packet_in_format;
1018}
1019
a7349929
BP
1020/* Sets the controller connection ID for 'ofconn' to 'controller_id'.
1021 *
1022 * The connection controller ID is used for OFPP_CONTROLLER and
1023 * NXAST_CONTROLLER actions. See "struct nx_action_controller" for details. */
1024void
1025ofconn_set_controller_id(struct ofconn *ofconn, uint16_t controller_id)
1026{
1027 ofconn->controller_id = controller_id;
1028}
1029
19a87e36
BP
1030/* Returns the default miss send length for 'ofconn'. */
1031int
1032ofconn_get_miss_send_len(const struct ofconn *ofconn)
1033{
1034 return ofconn->miss_send_len;
1035}
1036
1037/* Sets the default miss send length for 'ofconn' to 'miss_send_len'. */
1038void
1039ofconn_set_miss_send_len(struct ofconn *ofconn, int miss_send_len)
1040{
1041 ofconn->miss_send_len = miss_send_len;
1042}
1043
80d5aefd
BP
1044void
1045ofconn_set_async_config(struct ofconn *ofconn,
1046 const uint32_t master_masks[OAM_N_TYPES],
1047 const uint32_t slave_masks[OAM_N_TYPES])
1048{
1049 size_t size = sizeof ofconn->master_async_config;
1050 memcpy(ofconn->master_async_config, master_masks, size);
1051 memcpy(ofconn->slave_async_config, slave_masks, size);
1052}
1053
c423b3b3
AC
1054void
1055ofconn_get_async_config(struct ofconn *ofconn,
1056 uint32_t *master_masks, uint32_t *slave_masks)
1057{
1058 size_t size = sizeof ofconn->master_async_config;
1059 memcpy(master_masks, ofconn->master_async_config, size);
1060 memcpy(slave_masks, ofconn->slave_async_config, size);
1061}
1062
19a87e36
BP
1063/* Sends 'msg' on 'ofconn', accounting it as a reply. (If there is a
1064 * sufficient number of OpenFlow replies in-flight on a single ofconn, then the
1065 * connmgr will stop accepting new OpenFlow requests on that ofconn until the
1066 * controller has accepted some of the replies.) */
1067void
1068ofconn_send_reply(const struct ofconn *ofconn, struct ofpbuf *msg)
1069{
1070 ofconn_send(ofconn, msg, ofconn->reply_counter);
1071}
1072
63f2140a
BP
1073/* Sends each of the messages in list 'replies' on 'ofconn' in order,
1074 * accounting them as replies. */
1075void
1076ofconn_send_replies(const struct ofconn *ofconn, struct list *replies)
1077{
1078 struct ofpbuf *reply, *next;
1079
1080 LIST_FOR_EACH_SAFE (reply, next, list_node, replies) {
1081 list_remove(&reply->list_node);
1082 ofconn_send_reply(ofconn, reply);
1083 }
1084}
1085
90bf1e07 1086/* Sends 'error' on 'ofconn', as a reply to 'request'. Only at most the
1be5ff75
BP
1087 * first 64 bytes of 'request' are used. */
1088void
1089ofconn_send_error(const struct ofconn *ofconn,
90bf1e07 1090 const struct ofp_header *request, enum ofperr error)
1be5ff75 1091{
07c8ec21 1092 static struct vlog_rate_limit err_rl = VLOG_RATE_LIMIT_INIT(10, 10);
90bf1e07 1093 struct ofpbuf *reply;
76589937 1094
90bf1e07 1095 reply = ofperr_encode_reply(error, request);
07c8ec21
BP
1096 if (!VLOG_DROP_INFO(&err_rl)) {
1097 const char *type_name;
1098 size_t request_len;
1099 enum ofpraw raw;
1100
1101 request_len = ntohs(request->length);
1102 type_name = (!ofpraw_decode_partial(&raw, request,
1103 MIN(64, request_len))
1104 ? ofpraw_get_name(raw)
1105 : "invalid");
1106
1107 VLOG_INFO("%s: sending %s error reply to %s message",
1108 rconn_get_name(ofconn->rconn), ofperr_to_string(error),
1109 type_name);
1110 }
1111 ofconn_send_reply(ofconn, reply);
1be5ff75
BP
1112}
1113
19a87e36 1114/* Same as pktbuf_retrieve(), using the pktbuf owned by 'ofconn'. */
90bf1e07 1115enum ofperr
19a87e36 1116ofconn_pktbuf_retrieve(struct ofconn *ofconn, uint32_t id,
4e022ec0 1117 struct ofpbuf **bufferp, ofp_port_t *in_port)
19a87e36
BP
1118{
1119 return pktbuf_retrieve(ofconn->pktbuf, id, bufferp, in_port);
1120}
7ee20df1 1121
696d1bcf
BP
1122/* Reports that a flow_mod operation of the type specified by 'command' was
1123 * successfully executed by 'ofconn', so that the connmgr can log it. */
1124void
1125ofconn_report_flow_mod(struct ofconn *ofconn,
1126 enum ofp_flow_mod_command command)
1127{
1128 long long int now;
1129
1130 switch (command) {
1131 case OFPFC_ADD:
1132 ofconn->n_add++;
1133 break;
1134
1135 case OFPFC_MODIFY:
1136 case OFPFC_MODIFY_STRICT:
1137 ofconn->n_modify++;
1138 break;
1139
1140 case OFPFC_DELETE:
1141 case OFPFC_DELETE_STRICT:
1142 ofconn->n_delete++;
1143 break;
1144 }
1145
1146 now = time_msec();
1147 if (ofconn->next_op_report == LLONG_MAX) {
1148 ofconn->first_op = now;
1149 ofconn->next_op_report = MAX(now + 10 * 1000, ofconn->op_backoff);
1150 ofconn->op_backoff = ofconn->next_op_report + 60 * 1000;
1151 }
1152 ofconn->last_op = now;
1153}
1154
777af88d
AC
1155struct hmap *
1156ofconn_get_bundles(struct ofconn *ofconn)
1157{
1158 return &ofconn->bundles;
1159}
1160
19a87e36
BP
1161\f
1162/* Private ofconn functions. */
1163
1164static const char *
1165ofconn_get_target(const struct ofconn *ofconn)
1166{
1167 return rconn_get_target(ofconn->rconn);
1168}
1169
1170static struct ofconn *
9886b662
BP
1171ofconn_create(struct connmgr *mgr, struct rconn *rconn, enum ofconn_type type,
1172 bool enable_async_msgs)
19a87e36 1173{
c4b31872
BP
1174 struct ofconn *ofconn;
1175
1176 ofconn = xzalloc(sizeof *ofconn);
19a87e36
BP
1177 ofconn->connmgr = mgr;
1178 list_push_back(&mgr->all_conns, &ofconn->node);
1179 ofconn->rconn = rconn;
1180 ofconn->type = type;
9886b662 1181 ofconn->enable_async_msgs = enable_async_msgs;
c4b31872 1182
2b07c8b1
BP
1183 hmap_init(&ofconn->monitors);
1184 list_init(&ofconn->updates);
1185
777af88d
AC
1186 hmap_init(&ofconn->bundles);
1187
c4b31872
BP
1188 ofconn_flush(ofconn);
1189
19a87e36
BP
1190 return ofconn;
1191}
1192
c4b31872
BP
1193/* Clears all of the state in 'ofconn' that should not persist from one
1194 * connection to the next. */
7ee20df1
BP
1195static void
1196ofconn_flush(struct ofconn *ofconn)
3b0c6b56 1197 OVS_REQUIRES(ofproto_mutex)
7ee20df1 1198{
2b07c8b1 1199 struct ofmonitor *monitor, *next_monitor;
c4b31872
BP
1200 int i;
1201
696d1bcf
BP
1202 ofconn_log_flow_mods(ofconn);
1203
f4f1ea7e 1204 ofconn->role = OFPCR12_ROLE_EQUAL;
eb12aa89 1205 ofconn_set_protocol(ofconn, OFPUTIL_P_NONE);
c4b31872 1206 ofconn->packet_in_format = NXPIF_OPENFLOW10;
c4b31872 1207
c4b31872
BP
1208 rconn_packet_counter_destroy(ofconn->packet_in_counter);
1209 ofconn->packet_in_counter = rconn_packet_counter_create();
1210 for (i = 0; i < N_SCHEDULERS; i++) {
1211 if (ofconn->schedulers[i]) {
1212 int rate, burst;
1213
1214 pinsched_get_limits(ofconn->schedulers[i], &rate, &burst);
1215 pinsched_destroy(ofconn->schedulers[i]);
1216 ofconn->schedulers[i] = pinsched_create(rate, burst);
1217 }
1218 }
1219 if (ofconn->pktbuf) {
1220 pktbuf_destroy(ofconn->pktbuf);
1221 ofconn->pktbuf = pktbuf_create();
1222 }
1223 ofconn->miss_send_len = (ofconn->type == OFCONN_PRIMARY
1224 ? OFP_DEFAULT_MISS_SEND_LEN
1225 : 0);
a7349929 1226 ofconn->controller_id = 0;
c4b31872
BP
1227
1228 rconn_packet_counter_destroy(ofconn->reply_counter);
1229 ofconn->reply_counter = rconn_packet_counter_create();
80d5aefd 1230
9886b662
BP
1231 if (ofconn->enable_async_msgs) {
1232 uint32_t *master = ofconn->master_async_config;
1233 uint32_t *slave = ofconn->slave_async_config;
1234
1235 /* "master" and "other" roles get all asynchronous messages by default,
1236 * except that the controller needs to enable nonstandard "packet-in"
1237 * reasons itself. */
1238 master[OAM_PACKET_IN] = (1u << OFPR_NO_MATCH) | (1u << OFPR_ACTION);
1239 master[OAM_PORT_STATUS] = ((1u << OFPPR_ADD)
1240 | (1u << OFPPR_DELETE)
1241 | (1u << OFPPR_MODIFY));
1242 master[OAM_FLOW_REMOVED] = ((1u << OFPRR_IDLE_TIMEOUT)
1243 | (1u << OFPRR_HARD_TIMEOUT)
1244 | (1u << OFPRR_DELETE));
1245
1246 /* "slave" role gets port status updates by default. */
1247 slave[OAM_PACKET_IN] = 0;
1248 slave[OAM_PORT_STATUS] = ((1u << OFPPR_ADD)
1249 | (1u << OFPPR_DELETE)
1250 | (1u << OFPPR_MODIFY));
1251 slave[OAM_FLOW_REMOVED] = 0;
1252 } else {
1253 memset(ofconn->master_async_config, 0,
1254 sizeof ofconn->master_async_config);
1255 memset(ofconn->slave_async_config, 0,
1256 sizeof ofconn->slave_async_config);
1257 }
2b07c8b1 1258
696d1bcf
BP
1259 ofconn->n_add = ofconn->n_delete = ofconn->n_modify = 0;
1260 ofconn->first_op = ofconn->last_op = LLONG_MIN;
1261 ofconn->next_op_report = LLONG_MAX;
1262 ofconn->op_backoff = LLONG_MIN;
1263
2b07c8b1
BP
1264 HMAP_FOR_EACH_SAFE (monitor, next_monitor, ofconn_node,
1265 &ofconn->monitors) {
1266 ofmonitor_destroy(monitor);
1267 }
1268 rconn_packet_counter_destroy(ofconn->monitor_counter);
1269 ofconn->monitor_counter = rconn_packet_counter_create();
1270 ofpbuf_list_delete(&ofconn->updates); /* ...but it should be empty. */
7ee20df1
BP
1271}
1272
19a87e36
BP
1273static void
1274ofconn_destroy(struct ofconn *ofconn)
3b0c6b56 1275 OVS_REQUIRES(ofproto_mutex)
19a87e36 1276{
7ee20df1
BP
1277 ofconn_flush(ofconn);
1278
19a87e36
BP
1279 if (ofconn->type == OFCONN_PRIMARY) {
1280 hmap_remove(&ofconn->connmgr->controllers, &ofconn->hmap_node);
1281 }
1282
777af88d
AC
1283 ofp_bundle_remove_all(ofconn);
1284
92bbc96a 1285 hmap_destroy(&ofconn->monitors);
19a87e36
BP
1286 list_remove(&ofconn->node);
1287 rconn_destroy(ofconn->rconn);
1288 rconn_packet_counter_destroy(ofconn->packet_in_counter);
1289 rconn_packet_counter_destroy(ofconn->reply_counter);
1290 pktbuf_destroy(ofconn->pktbuf);
2b07c8b1 1291 rconn_packet_counter_destroy(ofconn->monitor_counter);
19a87e36
BP
1292 free(ofconn);
1293}
1294
1295/* Reconfigures 'ofconn' to match 'c'. 'ofconn' and 'c' must have the same
1296 * target. */
1297static void
1298ofconn_reconfigure(struct ofconn *ofconn, const struct ofproto_controller *c)
1299{
1300 int probe_interval;
1301
1302 ofconn->band = c->band;
065825e9 1303 ofconn->enable_async_msgs = c->enable_async_msgs;
19a87e36
BP
1304
1305 rconn_set_max_backoff(ofconn->rconn, c->max_backoff);
1306
1307 probe_interval = c->probe_interval ? MAX(c->probe_interval, 5) : 0;
1308 rconn_set_probe_interval(ofconn->rconn, probe_interval);
1309
1310 ofconn_set_rate_limit(ofconn, c->rate_limit, c->burst_limit);
0442efd9
MM
1311
1312 /* If dscp value changed reconnect. */
1313 if (c->dscp != rconn_get_dscp(ofconn->rconn)) {
1314 rconn_set_dscp(ofconn->rconn, c->dscp);
1315 rconn_reconnect(ofconn->rconn);
1316 }
19a87e36
BP
1317}
1318
7ee20df1
BP
1319/* Returns true if it makes sense for 'ofconn' to receive and process OpenFlow
1320 * messages. */
1321static bool
1322ofconn_may_recv(const struct ofconn *ofconn)
1323{
a3d1ff00 1324 int count = rconn_packet_counter_n_packets(ofconn->reply_counter);
b20f4073 1325 return count < OFCONN_REPLY_MAX;
7ee20df1
BP
1326}
1327
19a87e36
BP
1328static void
1329ofconn_run(struct ofconn *ofconn,
b20f4073 1330 void (*handle_openflow)(struct ofconn *,
e03248b7 1331 const struct ofpbuf *ofp_msg))
19a87e36
BP
1332{
1333 struct connmgr *mgr = ofconn->connmgr;
19a87e36
BP
1334 size_t i;
1335
1336 for (i = 0; i < N_SCHEDULERS; i++) {
a6f75961
BP
1337 struct list txq;
1338
1339 pinsched_run(ofconn->schedulers[i], &txq);
1340 do_send_packet_ins(ofconn, &txq);
19a87e36
BP
1341 }
1342
1343 rconn_run(ofconn->rconn);
1344
b20f4073
BP
1345 /* Limit the number of iterations to avoid starving other tasks. */
1346 for (i = 0; i < 50 && ofconn_may_recv(ofconn); i++) {
1347 struct ofpbuf *of_msg = rconn_recv(ofconn->rconn);
1348 if (!of_msg) {
1349 break;
1350 }
7ee20df1 1351
b20f4073
BP
1352 if (mgr->fail_open) {
1353 fail_open_maybe_recover(mgr->fail_open);
19a87e36 1354 }
19a87e36 1355
b20f4073
BP
1356 handle_openflow(ofconn, of_msg);
1357 ofpbuf_delete(of_msg);
1358 }
696d1bcf
BP
1359
1360 if (time_msec() >= ofconn->next_op_report) {
1361 ofconn_log_flow_mods(ofconn);
1362 }
1363
3b0c6b56 1364 ovs_mutex_lock(&ofproto_mutex);
19a87e36
BP
1365 if (!rconn_is_alive(ofconn->rconn)) {
1366 ofconn_destroy(ofconn);
7ee20df1
BP
1367 } else if (!rconn_is_connected(ofconn->rconn)) {
1368 ofconn_flush(ofconn);
19a87e36 1369 }
3b0c6b56 1370 ovs_mutex_unlock(&ofproto_mutex);
19a87e36
BP
1371}
1372
1373static void
b20f4073 1374ofconn_wait(struct ofconn *ofconn)
19a87e36
BP
1375{
1376 int i;
1377
1378 for (i = 0; i < N_SCHEDULERS; i++) {
1379 pinsched_wait(ofconn->schedulers[i]);
1380 }
1381 rconn_run_wait(ofconn->rconn);
b20f4073 1382 if (ofconn_may_recv(ofconn)) {
19a87e36 1383 rconn_recv_wait(ofconn->rconn);
19a87e36 1384 }
696d1bcf
BP
1385 if (ofconn->next_op_report != LLONG_MAX) {
1386 poll_timer_wait_until(ofconn->next_op_report);
1387 }
1388}
1389
1390static void
1391ofconn_log_flow_mods(struct ofconn *ofconn)
1392{
1393 int n_flow_mods = ofconn->n_add + ofconn->n_delete + ofconn->n_modify;
1394 if (n_flow_mods) {
1395 long long int ago = (time_msec() - ofconn->first_op) / 1000;
1396 long long int interval = (ofconn->last_op - ofconn->first_op) / 1000;
1397 struct ds s;
1398
1399 ds_init(&s);
1400 ds_put_format(&s, "%d flow_mods ", n_flow_mods);
1401 if (interval == ago) {
1402 ds_put_format(&s, "in the last %lld s", ago);
1403 } else if (interval) {
1404 ds_put_format(&s, "in the %lld s starting %lld s ago",
1405 interval, ago);
1406 } else {
1407 ds_put_format(&s, "%lld s ago", ago);
1408 }
1409
1410 ds_put_cstr(&s, " (");
1411 if (ofconn->n_add) {
1412 ds_put_format(&s, "%d adds, ", ofconn->n_add);
1413 }
1414 if (ofconn->n_delete) {
1415 ds_put_format(&s, "%d deletes, ", ofconn->n_delete);
1416 }
1417 if (ofconn->n_modify) {
1418 ds_put_format(&s, "%d modifications, ", ofconn->n_modify);
1419 }
1420 s.length -= 2;
1421 ds_put_char(&s, ')');
1422
1423 VLOG_INFO("%s: %s", rconn_get_name(ofconn->rconn), ds_cstr(&s));
1424 ds_destroy(&s);
1425
1426 ofconn->n_add = ofconn->n_delete = ofconn->n_modify = 0;
1427 }
1428 ofconn->next_op_report = LLONG_MAX;
19a87e36
BP
1429}
1430
80d5aefd
BP
1431/* Returns true if 'ofconn' should receive asynchronous messages of the given
1432 * OAM_* 'type' and 'reason', which should be a OFPR_* value for OAM_PACKET_IN,
1433 * a OFPPR_* value for OAM_PORT_STATUS, or an OFPRR_* value for
1434 * OAM_FLOW_REMOVED. Returns false if the message should not be sent on
1435 * 'ofconn'. */
19a87e36 1436static bool
80d5aefd
BP
1437ofconn_receives_async_msg(const struct ofconn *ofconn,
1438 enum ofconn_async_msg_type type,
1439 unsigned int reason)
19a87e36 1440{
80d5aefd
BP
1441 const uint32_t *async_config;
1442
cb22974d
BP
1443 ovs_assert(reason < 32);
1444 ovs_assert((unsigned int) type < OAM_N_TYPES);
19a87e36 1445
eb12aa89
BP
1446 if (ofconn_get_protocol(ofconn) == OFPUTIL_P_NONE
1447 || !rconn_is_connected(ofconn->rconn)) {
f0fd1a17 1448 return false;
f0fd1a17 1449 }
f0fd1a17 1450
80d5aefd
BP
1451 /* Keep the following code in sync with the documentation in the
1452 * "Asynchronous Messages" section in DESIGN. */
1453
1454 if (ofconn->type == OFCONN_SERVICE && !ofconn->miss_send_len) {
1455 /* Service connections don't get asynchronous messages unless they have
1456 * explicitly asked for them by setting a nonzero miss send length. */
f0fd1a17 1457 return false;
f0fd1a17 1458 }
80d5aefd 1459
f4f1ea7e 1460 async_config = (ofconn->role == OFPCR12_ROLE_SLAVE
80d5aefd
BP
1461 ? ofconn->slave_async_config
1462 : ofconn->master_async_config);
1463 if (!(async_config[type] & (1u << reason))) {
1464 return false;
1465 }
1466
1467 return true;
f0fd1a17
PS
1468}
1469
6b83a3c5
SH
1470/* The default "table-miss" behaviour for OpenFlow1.3+ is to drop the
1471 * packet rather than to send the packet to the controller.
1472 *
1473 * This function returns false to indicate the packet should be dropped if
1474 * the controller action was the result of the default table-miss behaviour
1475 * and the controller is using OpenFlow1.3+.
1476 *
1477 * Otherwise true is returned to indicate the packet should be forwarded to
1478 * the controller */
1479static bool
1480ofconn_wants_packet_in_on_miss(struct ofconn *ofconn,
1481 const struct ofproto_packet_in *pin)
1482{
1483 if (pin->miss_type == OFPROTO_PACKET_IN_MISS_WITHOUT_FLOW) {
1484 enum ofputil_protocol protocol = ofconn_get_protocol(ofconn);
1485
1486 if (protocol != OFPUTIL_P_NONE
3c1bb396
BP
1487 && ofputil_protocol_to_ofp_version(protocol) >= OFP13_VERSION
1488 && (ofproto_table_get_miss_config(ofconn->connmgr->ofproto,
1489 pin->up.table_id)
1490 == OFPUTIL_TABLE_MISS_DEFAULT)) {
1491 return false;
6b83a3c5
SH
1492 }
1493 }
1494 return true;
1495}
1496
1497/* The default "table-miss" behaviour for OpenFlow1.3+ is to drop the
1498 * packet rather than to send the packet to the controller.
1499 *
0474cca3 1500 * This function returns true to indicate that a packet_in message
6b83a3c5
SH
1501 * for a "table-miss" should be sent to at least one controller.
1502 * That is there is at least one controller with controller_id 0
1503 * which connected using an OpenFlow version earlier than OpenFlow1.3.
1504 *
1505 * False otherwise.
1506 *
1507 * This logic assumes that "table-miss" packet_in messages
1508 * are always sent to controller_id 0. */
1509bool
3fc76ec0 1510connmgr_wants_packet_in_on_miss(struct connmgr *mgr) OVS_EXCLUDED(ofproto_mutex)
6b83a3c5
SH
1511{
1512 struct ofconn *ofconn;
1513
3fc76ec0 1514 ovs_mutex_lock(&ofproto_mutex);
6b83a3c5
SH
1515 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1516 enum ofputil_protocol protocol = ofconn_get_protocol(ofconn);
1517
1518 if (ofconn->controller_id == 0 &&
1519 (protocol == OFPUTIL_P_NONE ||
1520 ofputil_protocol_to_ofp_version(protocol) < OFP13_VERSION)) {
3fc76ec0 1521 ovs_mutex_unlock(&ofproto_mutex);
6b83a3c5
SH
1522 return true;
1523 }
1524 }
3fc76ec0
AW
1525 ovs_mutex_unlock(&ofproto_mutex);
1526
6b83a3c5
SH
1527 return false;
1528}
1529
19a87e36
BP
1530/* Returns a human-readable name for an OpenFlow connection between 'mgr' and
1531 * 'target', suitable for use in log messages for identifying the connection.
1532 *
1533 * The name is dynamically allocated. The caller should free it (with free())
1534 * when it is no longer needed. */
1535static char *
1536ofconn_make_name(const struct connmgr *mgr, const char *target)
1537{
1538 return xasprintf("%s<->%s", mgr->name, target);
1539}
1540
1541static void
1542ofconn_set_rate_limit(struct ofconn *ofconn, int rate, int burst)
1543{
1544 int i;
1545
1546 for (i = 0; i < N_SCHEDULERS; i++) {
1547 struct pinsched **s = &ofconn->schedulers[i];
1548
1549 if (rate > 0) {
1550 if (!*s) {
1551 *s = pinsched_create(rate, burst);
1552 } else {
1553 pinsched_set_limits(*s, rate, burst);
1554 }
1555 } else {
1556 pinsched_destroy(*s);
1557 *s = NULL;
1558 }
1559 }
1560}
1561
1562static void
1563ofconn_send(const struct ofconn *ofconn, struct ofpbuf *msg,
1564 struct rconn_packet_counter *counter)
1565{
982697a4 1566 ofpmsg_update_length(msg);
acb9da40 1567 rconn_send(ofconn->rconn, msg, counter);
19a87e36
BP
1568}
1569\f
1570/* Sending asynchronous messages. */
1571
cfa955b0
YT
1572static void schedule_packet_in(struct ofconn *, struct ofproto_packet_in,
1573 enum ofp_packet_in_reason wire_reason);
19a87e36
BP
1574
1575/* Sends an OFPT_PORT_STATUS message with 'opp' and 'reason' to appropriate
2a6f78e0
BP
1576 * controllers managed by 'mgr'. For messages caused by a controller
1577 * OFPT_PORT_MOD, specify 'source' as the controller connection that sent the
1578 * request; otherwise, specify 'source' as NULL. */
19a87e36 1579void
2a6f78e0 1580connmgr_send_port_status(struct connmgr *mgr, struct ofconn *source,
9e1fd49b 1581 const struct ofputil_phy_port *pp, uint8_t reason)
19a87e36
BP
1582{
1583 /* XXX Should limit the number of queued port status change messages. */
9e1fd49b 1584 struct ofputil_port_status ps;
19a87e36
BP
1585 struct ofconn *ofconn;
1586
9e1fd49b
BP
1587 ps.reason = reason;
1588 ps.desc = *pp;
19a87e36 1589 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
80d5aefd 1590 if (ofconn_receives_async_msg(ofconn, OAM_PORT_STATUS, reason)) {
9e1fd49b 1591 struct ofpbuf *msg;
80d5aefd 1592
2a6f78e0
BP
1593 /* Before 1.5, OpenFlow specified that OFPT_PORT_MOD should not
1594 * generate OFPT_PORT_STATUS messages. That requirement was a
1595 * relic of how OpenFlow originally supported a single controller,
1596 * so that one could expect the controller to already know the
1597 * changes it had made.
1598 *
1599 * EXT-338 changes OpenFlow 1.5 OFPT_PORT_MOD to send
1600 * OFPT_PORT_STATUS messages to every controller. This is
1601 * obviously more useful in the multi-controller case. We could
1602 * always implement it that way in OVS, but that would risk
1603 * confusing controllers that are intended for single-controller
1604 * use only. (Imagine a controller that generates an OFPT_PORT_MOD
1605 * in response to any OFPT_PORT_STATUS!)
1606 *
1607 * So this compromises: for OpenFlow 1.4 and earlier, it generates
1608 * OFPT_PORT_STATUS for OFPT_PORT_MOD, but not back to the
1609 * originating controller. In a single-controller environment, in
1610 * particular, this means that it will never generate
1611 * OFPT_PORT_STATUS for OFPT_PORT_MOD at all. */
1612 if (ofconn == source
1613 && rconn_get_version(ofconn->rconn) < OFP15_VERSION) {
1614 continue;
1615 }
1616
eb12aa89 1617 msg = ofputil_encode_port_status(&ps, ofconn_get_protocol(ofconn));
9e1fd49b 1618 ofconn_send(ofconn, msg, NULL);
19a87e36 1619 }
19a87e36
BP
1620 }
1621}
1622
1623/* Sends an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message based on 'fr' to
1624 * appropriate controllers managed by 'mgr'. */
1625void
1626connmgr_send_flow_removed(struct connmgr *mgr,
1627 const struct ofputil_flow_removed *fr)
1628{
1629 struct ofconn *ofconn;
1630
1631 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
80d5aefd
BP
1632 if (ofconn_receives_async_msg(ofconn, OAM_FLOW_REMOVED, fr->reason)) {
1633 struct ofpbuf *msg;
1634
1635 /* Account flow expirations as replies to OpenFlow requests. That
1636 * works because preventing OpenFlow requests from being processed
1637 * also prevents new flows from being added (and expiring). (It
1638 * also prevents processing OpenFlow requests that would not add
1639 * new flows, so it is imperfect.) */
eb12aa89 1640 msg = ofputil_encode_flow_removed(fr, ofconn_get_protocol(ofconn));
80d5aefd 1641 ofconn_send_reply(ofconn, msg);
19a87e36 1642 }
19a87e36
BP
1643 }
1644}
1645
cfa955b0
YT
1646/* Normally a send-to-controller action uses reason OFPR_ACTION. However, in
1647 * OpenFlow 1.3 and later, packet_ins generated by a send-to-controller action
1648 * in a "table-miss" flow (one with priority 0 and completely wildcarded) are
1649 * sent as OFPR_NO_MATCH. This function returns the reason that should
1650 * actually be sent on 'ofconn' for 'pin'. */
1651static enum ofp_packet_in_reason
1652wire_reason(struct ofconn *ofconn, const struct ofproto_packet_in *pin)
1653{
32260212
SH
1654 if (pin->miss_type == OFPROTO_PACKET_IN_MISS_FLOW
1655 && pin->up.reason == OFPR_ACTION) {
cfa955b0 1656 enum ofputil_protocol protocol = ofconn_get_protocol(ofconn);
cfa955b0 1657
b9b00872
YT
1658 if (protocol != OFPUTIL_P_NONE
1659 && ofputil_protocol_to_ofp_version(protocol) >= OFP13_VERSION) {
cfa955b0
YT
1660 return OFPR_NO_MATCH;
1661 }
1662 }
1663 return pin->up.reason;
1664}
1665
78bd1cd0 1666/* Given 'pin', sends an OFPT_PACKET_IN message to each OpenFlow controller as
2ed1202f
BP
1667 * necessary according to their individual configurations.
1668 *
1669 * The caller doesn't need to fill in pin->buffer_id or pin->total_len. */
19a87e36 1670void
78bd1cd0 1671connmgr_send_packet_in(struct connmgr *mgr,
0fb7792a 1672 const struct ofproto_packet_in *pin)
19a87e36 1673{
29ebe880 1674 struct ofconn *ofconn;
19a87e36 1675
19a87e36 1676 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
cfa955b0
YT
1677 enum ofp_packet_in_reason reason = wire_reason(ofconn, pin);
1678
6b83a3c5
SH
1679 if (ofconn_wants_packet_in_on_miss(ofconn, pin)
1680 && ofconn_receives_async_msg(ofconn, OAM_PACKET_IN, pin->up.reason)
f11c7538 1681 && ofconn->controller_id == pin->controller_id) {
cfa955b0 1682 schedule_packet_in(ofconn, *pin, reason);
19a87e36
BP
1683 }
1684 }
19a87e36
BP
1685}
1686
19a87e36 1687static void
a6f75961 1688do_send_packet_ins(struct ofconn *ofconn, struct list *txq)
19a87e36 1689{
a6f75961
BP
1690 struct ofpbuf *pin, *next_pin;
1691
1692 LIST_FOR_EACH_SAFE (pin, next_pin, list_node, txq) {
1693 list_remove(&pin->list_node);
19a87e36 1694
e95f1d01
BP
1695 if (rconn_send_with_limit(ofconn->rconn, pin,
1696 ofconn->packet_in_counter, 100) == EAGAIN) {
1697 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
1698
1699 VLOG_INFO_RL(&rl, "%s: dropping packet-in due to queue overflow",
1700 rconn_get_name(ofconn->rconn));
1701 }
a6f75961 1702 }
19a87e36
BP
1703}
1704
d8653c38
BP
1705/* Takes 'pin', composes an OpenFlow packet-in message from it, and passes it
1706 * to 'ofconn''s packet scheduler for sending. */
19a87e36 1707static void
cfa955b0
YT
1708schedule_packet_in(struct ofconn *ofconn, struct ofproto_packet_in pin,
1709 enum ofp_packet_in_reason wire_reason)
19a87e36
BP
1710{
1711 struct connmgr *mgr = ofconn->connmgr;
a1192496 1712 uint16_t controller_max_len;
a6f75961 1713 struct list txq;
19a87e36 1714
0fb7792a 1715 pin.up.total_len = pin.up.packet_len;
2ed1202f 1716
cfa955b0 1717 pin.up.reason = wire_reason;
0fb7792a 1718 if (pin.up.reason == OFPR_ACTION) {
d38a3c7b 1719 controller_max_len = pin.send_len; /* max_len */
a1192496
YT
1720 } else {
1721 controller_max_len = ofconn->miss_send_len;
1722 }
1723
1724 /* Get OpenFlow buffer_id.
1725 * For OpenFlow 1.2+, OFPCML_NO_BUFFER (== UINT16_MAX) specifies
1726 * unbuffered. This behaviour doesn't violate prior versions, too. */
1727 if (controller_max_len == UINT16_MAX) {
0fb7792a 1728 pin.up.buffer_id = UINT32_MAX;
19a87e36 1729 } else if (mgr->fail_open && fail_open_is_active(mgr->fail_open)) {
0fb7792a 1730 pin.up.buffer_id = pktbuf_get_null();
19a87e36 1731 } else if (!ofconn->pktbuf) {
0fb7792a 1732 pin.up.buffer_id = UINT32_MAX;
19a87e36 1733 } else {
0fb7792a
BP
1734 pin.up.buffer_id = pktbuf_save(ofconn->pktbuf,
1735 pin.up.packet, pin.up.packet_len,
1736 pin.up.fmd.in_port);
19a87e36
BP
1737 }
1738
a1192496
YT
1739 /* Figure out how much of the packet to send.
1740 * If not buffered, send the entire packet. Otherwise, depending on
1741 * the reason of packet-in, send what requested by the controller. */
d38a3c7b
BP
1742 if (pin.up.buffer_id != UINT32_MAX
1743 && controller_max_len < pin.up.packet_len) {
1744 pin.up.packet_len = controller_max_len;
19a87e36 1745 }
19a87e36 1746
a6f75961 1747 /* Make OFPT_PACKET_IN and hand over to packet scheduler. */
0fb7792a
BP
1748 pinsched_send(ofconn->schedulers[pin.up.reason == OFPR_NO_MATCH ? 0 : 1],
1749 pin.up.fmd.in_port,
1750 ofputil_encode_packet_in(&pin.up,
1751 ofconn_get_protocol(ofconn),
d94240ec 1752 ofconn->packet_in_format),
a6f75961
BP
1753 &txq);
1754 do_send_packet_ins(ofconn, &txq);
19a87e36
BP
1755}
1756\f
1757/* Fail-open settings. */
1758
1759/* Returns the failure handling mode (OFPROTO_FAIL_SECURE or
1760 * OFPROTO_FAIL_STANDALONE) for 'mgr'. */
1761enum ofproto_fail_mode
1762connmgr_get_fail_mode(const struct connmgr *mgr)
1763{
1764 return mgr->fail_mode;
1765}
1766
1767/* Sets the failure handling mode for 'mgr' to 'fail_mode' (either
1768 * OFPROTO_FAIL_SECURE or OFPROTO_FAIL_STANDALONE). */
1769void
1770connmgr_set_fail_mode(struct connmgr *mgr, enum ofproto_fail_mode fail_mode)
1771{
08fd19f0
BP
1772 if (mgr->fail_mode != fail_mode) {
1773 mgr->fail_mode = fail_mode;
1774 update_fail_open(mgr);
1775 if (!connmgr_has_controllers(mgr)) {
1776 ofproto_flush_flows(mgr->ofproto);
1777 }
1778 }
19a87e36
BP
1779}
1780\f
1781/* Fail-open implementation. */
1782
1783/* Returns the longest probe interval among the primary controllers configured
1784 * on 'mgr'. Returns 0 if there are no primary controllers. */
1785int
1786connmgr_get_max_probe_interval(const struct connmgr *mgr)
1787{
1788 const struct ofconn *ofconn;
1789 int max_probe_interval;
1790
1791 max_probe_interval = 0;
1792 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1793 int probe_interval = rconn_get_probe_interval(ofconn->rconn);
1794 max_probe_interval = MAX(max_probe_interval, probe_interval);
1795 }
1796 return max_probe_interval;
1797}
1798
1799/* Returns the number of seconds for which all of 'mgr's primary controllers
1800 * have been disconnected. Returns 0 if 'mgr' has no primary controllers. */
1801int
1802connmgr_failure_duration(const struct connmgr *mgr)
1803{
1804 const struct ofconn *ofconn;
1805 int min_failure_duration;
1806
1807 if (!connmgr_has_controllers(mgr)) {
1808 return 0;
1809 }
1810
1811 min_failure_duration = INT_MAX;
1812 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1813 int failure_duration = rconn_failure_duration(ofconn->rconn);
1814 min_failure_duration = MIN(min_failure_duration, failure_duration);
1815 }
1816 return min_failure_duration;
1817}
1818
1819/* Returns true if at least one primary controller is connected (regardless of
1820 * whether those controllers are believed to have authenticated and accepted
1821 * this switch), false if none of them are connected. */
1822bool
1823connmgr_is_any_controller_connected(const struct connmgr *mgr)
1824{
1825 const struct ofconn *ofconn;
1826
1827 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1828 if (rconn_is_connected(ofconn->rconn)) {
1829 return true;
1830 }
1831 }
1832 return false;
1833}
1834
1835/* Returns true if at least one primary controller is believed to have
1836 * authenticated and accepted this switch, false otherwise. */
1837bool
1838connmgr_is_any_controller_admitted(const struct connmgr *mgr)
1839{
1840 const struct ofconn *ofconn;
1841
1842 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1843 if (rconn_is_admitted(ofconn->rconn)) {
1844 return true;
1845 }
1846 }
1847 return false;
1848}
19a87e36
BP
1849\f
1850/* In-band configuration. */
1851
1852static bool any_extras_changed(const struct connmgr *,
1853 const struct sockaddr_in *extras, size_t n);
1854
1855/* Sets the 'n' TCP port addresses in 'extras' as ones to which 'mgr''s
1856 * in-band control should guarantee access, in the same way that in-band
1857 * control guarantees access to OpenFlow controllers. */
1858void
1859connmgr_set_extra_in_band_remotes(struct connmgr *mgr,
1860 const struct sockaddr_in *extras, size_t n)
1861{
1862 if (!any_extras_changed(mgr, extras, n)) {
1863 return;
1864 }
1865
1866 free(mgr->extra_in_band_remotes);
1867 mgr->n_extra_remotes = n;
1868 mgr->extra_in_band_remotes = xmemdup(extras, n * sizeof *extras);
1869
1870 update_in_band_remotes(mgr);
1871}
1872
1873/* Sets the OpenFlow queue used by flows set up by in-band control on
1874 * 'mgr' to 'queue_id'. If 'queue_id' is negative, then in-band control
1875 * flows will use the default queue. */
1876void
1877connmgr_set_in_band_queue(struct connmgr *mgr, int queue_id)
1878{
1879 if (queue_id != mgr->in_band_queue) {
1880 mgr->in_band_queue = queue_id;
1881 update_in_band_remotes(mgr);
1882 }
1883}
1884
1885static bool
1886any_extras_changed(const struct connmgr *mgr,
1887 const struct sockaddr_in *extras, size_t n)
1888{
1889 size_t i;
1890
1891 if (n != mgr->n_extra_remotes) {
1892 return true;
1893 }
1894
1895 for (i = 0; i < n; i++) {
1896 const struct sockaddr_in *old = &mgr->extra_in_band_remotes[i];
1897 const struct sockaddr_in *new = &extras[i];
1898
1899 if (old->sin_addr.s_addr != new->sin_addr.s_addr ||
1900 old->sin_port != new->sin_port) {
1901 return true;
1902 }
1903 }
1904
1905 return false;
1906}
1907\f
1908/* In-band implementation. */
1909
1910bool
f7f1ea29 1911connmgr_has_in_band(struct connmgr *mgr)
19a87e36 1912{
f7f1ea29 1913 return mgr->in_band != NULL;
19a87e36
BP
1914}
1915\f
1916/* Fail-open and in-band implementation. */
1917
1918/* Called by 'ofproto' after all flows have been flushed, to allow fail-open
7ee20df1
BP
1919 * and standalone mode to re-create their flows.
1920 *
1921 * In-band control has more sophisticated code that manages flows itself. */
19a87e36
BP
1922void
1923connmgr_flushed(struct connmgr *mgr)
15aaf599 1924 OVS_EXCLUDED(ofproto_mutex)
19a87e36 1925{
19a87e36
BP
1926 if (mgr->fail_open) {
1927 fail_open_flushed(mgr->fail_open);
1928 }
08fd19f0
BP
1929
1930 /* If there are no controllers and we're in standalone mode, set up a flow
1931 * that matches every packet and directs them to OFPP_NORMAL (which goes to
1932 * us). Otherwise, the switch is in secure mode and we won't pass any
1933 * traffic until a controller has been defined and it tells us to do so. */
1934 if (!connmgr_has_controllers(mgr)
1935 && mgr->fail_mode == OFPROTO_FAIL_STANDALONE) {
f25d0cf3 1936 struct ofpbuf ofpacts;
81a76618 1937 struct match match;
08fd19f0 1938
f25d0cf3
BP
1939 ofpbuf_init(&ofpacts, OFPACT_OUTPUT_SIZE);
1940 ofpact_put_OUTPUT(&ofpacts)->port = OFPP_NORMAL;
1941 ofpact_pad(&ofpacts);
1942
81a76618 1943 match_init_catchall(&match);
1f317cb5
PS
1944 ofproto_add_flow(mgr->ofproto, &match, 0, ofpbuf_data(&ofpacts),
1945 ofpbuf_size(&ofpacts));
f25d0cf3
BP
1946
1947 ofpbuf_uninit(&ofpacts);
08fd19f0 1948 }
19a87e36
BP
1949}
1950\f
1951/* Creates a new ofservice for 'target' in 'mgr'. Returns 0 if successful,
1952 * otherwise a positive errno value.
1953 *
1954 * ofservice_reconfigure() must be called to fully configure the new
1955 * ofservice. */
1956static int
00401ef0
SH
1957ofservice_create(struct connmgr *mgr, const char *target,
1958 uint32_t allowed_versions, uint8_t dscp)
19a87e36
BP
1959{
1960 struct ofservice *ofservice;
1961 struct pvconn *pvconn;
1962 int error;
1963
82c8c53c 1964 error = pvconn_open(target, allowed_versions, dscp, &pvconn);
19a87e36
BP
1965 if (error) {
1966 return error;
1967 }
1968
1969 ofservice = xzalloc(sizeof *ofservice);
1970 hmap_insert(&mgr->services, &ofservice->node, hash_string(target, 0));
1971 ofservice->pvconn = pvconn;
90ef0206 1972 ofservice->allowed_versions = allowed_versions;
19a87e36
BP
1973
1974 return 0;
1975}
1976
1977static void
1978ofservice_destroy(struct connmgr *mgr, struct ofservice *ofservice)
1979{
1980 hmap_remove(&mgr->services, &ofservice->node);
1981 pvconn_close(ofservice->pvconn);
1982 free(ofservice);
1983}
1984
1985static void
1986ofservice_reconfigure(struct ofservice *ofservice,
1987 const struct ofproto_controller *c)
1988{
1989 ofservice->probe_interval = c->probe_interval;
1990 ofservice->rate_limit = c->rate_limit;
1991 ofservice->burst_limit = c->burst_limit;
9886b662 1992 ofservice->enable_async_msgs = c->enable_async_msgs;
f125905c 1993 ofservice->dscp = c->dscp;
19a87e36
BP
1994}
1995
1996/* Finds and returns the ofservice within 'mgr' that has the given
1997 * 'target', or a null pointer if none exists. */
1998static struct ofservice *
1999ofservice_lookup(struct connmgr *mgr, const char *target)
2000{
2001 struct ofservice *ofservice;
2002
2003 HMAP_FOR_EACH_WITH_HASH (ofservice, node, hash_string(target, 0),
2004 &mgr->services) {
2005 if (!strcmp(pvconn_get_name(ofservice->pvconn), target)) {
2006 return ofservice;
2007 }
2008 }
2009 return NULL;
2010}
2b07c8b1
BP
2011\f
2012/* Flow monitors (NXST_FLOW_MONITOR). */
2013
2014/* A counter incremented when something significant happens to an OpenFlow
2015 * rule.
2016 *
2017 * - When a rule is added, its 'add_seqno' and 'modify_seqno' are set to
2018 * the current value (which is then incremented).
2019 *
2020 * - When a rule is modified, its 'modify_seqno' is set to the current
2021 * value (which is then incremented).
2022 *
2023 * Thus, by comparing an old value of monitor_seqno against a rule's
2024 * 'add_seqno', one can tell whether the rule was added before or after the old
2025 * value was read, and similarly for 'modify_seqno'.
2026 *
2027 * 32 bits should normally be sufficient (and would be nice, to save space in
2028 * each rule) but then we'd have to have some special cases for wraparound.
2029 *
2030 * We initialize monitor_seqno to 1 to allow 0 to be used as an invalid
2031 * value. */
2032static uint64_t monitor_seqno = 1;
2033
2034COVERAGE_DEFINE(ofmonitor_pause);
2035COVERAGE_DEFINE(ofmonitor_resume);
2036
2037enum ofperr
2038ofmonitor_create(const struct ofputil_flow_monitor_request *request,
2039 struct ofconn *ofconn, struct ofmonitor **monitorp)
4cd1bc9d 2040 OVS_REQUIRES(ofproto_mutex)
2b07c8b1
BP
2041{
2042 struct ofmonitor *m;
2043
2044 *monitorp = NULL;
2045
2046 m = ofmonitor_lookup(ofconn, request->id);
2047 if (m) {
04f9f286 2048 return OFPERR_OFPMOFC_MONITOR_EXISTS;
2b07c8b1
BP
2049 }
2050
2051 m = xmalloc(sizeof *m);
2052 m->ofconn = ofconn;
2053 hmap_insert(&ofconn->monitors, &m->ofconn_node, hash_int(request->id, 0));
2054 m->id = request->id;
2055 m->flags = request->flags;
2056 m->out_port = request->out_port;
2057 m->table_id = request->table_id;
5cb7a798 2058 minimatch_init(&m->match, &request->match);
2b07c8b1
BP
2059
2060 *monitorp = m;
2061 return 0;
2062}
2063
2064struct ofmonitor *
2065ofmonitor_lookup(struct ofconn *ofconn, uint32_t id)
4cd1bc9d 2066 OVS_REQUIRES(ofproto_mutex)
2b07c8b1
BP
2067{
2068 struct ofmonitor *m;
2069
2070 HMAP_FOR_EACH_IN_BUCKET (m, ofconn_node, hash_int(id, 0),
2071 &ofconn->monitors) {
2072 if (m->id == id) {
2073 return m;
2074 }
2075 }
2076 return NULL;
2077}
2078
2079void
2080ofmonitor_destroy(struct ofmonitor *m)
4cd1bc9d 2081 OVS_REQUIRES(ofproto_mutex)
2b07c8b1
BP
2082{
2083 if (m) {
5684eb74 2084 minimatch_destroy(&m->match);
2b07c8b1
BP
2085 hmap_remove(&m->ofconn->monitors, &m->ofconn_node);
2086 free(m);
2087 }
2088}
2089
2090void
2091ofmonitor_report(struct connmgr *mgr, struct rule *rule,
2092 enum nx_flow_update_event event,
2093 enum ofp_flow_removed_reason reason,
cdbdeeda
SH
2094 const struct ofconn *abbrev_ofconn, ovs_be32 abbrev_xid,
2095 const struct rule_actions *old_actions)
15aaf599 2096 OVS_REQUIRES(ofproto_mutex)
2b07c8b1
BP
2097{
2098 enum nx_flow_monitor_flags update;
2099 struct ofconn *ofconn;
2100
834fe5cb
BP
2101 if (rule_is_hidden(rule)) {
2102 return;
2103 }
2104
2b07c8b1
BP
2105 switch (event) {
2106 case NXFME_ADDED:
2107 update = NXFMF_ADD;
2108 rule->add_seqno = rule->modify_seqno = monitor_seqno++;
2109 break;
2110
2111 case NXFME_DELETED:
2112 update = NXFMF_DELETE;
2113 break;
2114
2115 case NXFME_MODIFIED:
2116 update = NXFMF_MODIFY;
2117 rule->modify_seqno = monitor_seqno++;
2118 break;
2119
2120 default:
2121 case NXFME_ABBREV:
428b2edd 2122 OVS_NOT_REACHED();
2b07c8b1
BP
2123 }
2124
2125 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
2126 enum nx_flow_monitor_flags flags = 0;
2127 struct ofmonitor *m;
2128
2129 if (ofconn->monitor_paused) {
2130 /* Only send NXFME_DELETED notifications for flows that were added
2131 * before we paused. */
2132 if (event != NXFME_DELETED
2133 || rule->add_seqno > ofconn->monitor_paused) {
2134 continue;
2135 }
2136 }
2137
2138 HMAP_FOR_EACH (m, ofconn_node, &ofconn->monitors) {
2139 if (m->flags & update
2140 && (m->table_id == 0xff || m->table_id == rule->table_id)
cdbdeeda
SH
2141 && (ofproto_rule_has_out_port(rule, m->out_port)
2142 || (old_actions
2143 && ofpacts_output_to_port(old_actions->ofpacts,
2144 old_actions->ofpacts_len,
2145 m->out_port)))
2b07c8b1
BP
2146 && cls_rule_is_loose_match(&rule->cr, &m->match)) {
2147 flags |= m->flags;
2148 }
2149 }
2150
2151 if (flags) {
2152 if (list_is_empty(&ofconn->updates)) {
2153 ofputil_start_flow_update(&ofconn->updates);
2154 ofconn->sent_abbrev_update = false;
2155 }
2156
200d6ac4
SH
2157 if (flags & NXFMF_OWN || ofconn != abbrev_ofconn
2158 || ofconn->monitor_paused) {
2b07c8b1 2159 struct ofputil_flow_update fu;
5cb7a798 2160 struct match match;
2b07c8b1
BP
2161
2162 fu.event = event;
2163 fu.reason = event == NXFME_DELETED ? reason : 0;
2b07c8b1
BP
2164 fu.table_id = rule->table_id;
2165 fu.cookie = rule->flow_cookie;
5cb7a798
BP
2166 minimatch_expand(&rule->cr.match, &match);
2167 fu.match = &match;
6b140a4e 2168 fu.priority = rule->cr.priority;
a3779dbc 2169
d10976b7 2170 ovs_mutex_lock(&rule->mutex);
a3779dbc
EJ
2171 fu.idle_timeout = rule->idle_timeout;
2172 fu.hard_timeout = rule->hard_timeout;
d10976b7 2173 ovs_mutex_unlock(&rule->mutex);
a3779dbc 2174
2b07c8b1 2175 if (flags & NXFMF_ACTIONS) {
dc723c44 2176 const struct rule_actions *actions = rule_get_actions(rule);
06a0f3e2
BP
2177 fu.ofpacts = actions->ofpacts;
2178 fu.ofpacts_len = actions->ofpacts_len;
2b07c8b1
BP
2179 } else {
2180 fu.ofpacts = NULL;
2181 fu.ofpacts_len = 0;
2182 }
2183 ofputil_append_flow_update(&fu, &ofconn->updates);
2184 } else if (!ofconn->sent_abbrev_update) {
2185 struct ofputil_flow_update fu;
2186
2187 fu.event = NXFME_ABBREV;
2188 fu.xid = abbrev_xid;
2189 ofputil_append_flow_update(&fu, &ofconn->updates);
2190
2191 ofconn->sent_abbrev_update = true;
2192 }
2193 }
2194 }
2195}
2196
2197void
2198ofmonitor_flush(struct connmgr *mgr)
4cd1bc9d 2199 OVS_REQUIRES(ofproto_mutex)
2b07c8b1
BP
2200{
2201 struct ofconn *ofconn;
2202
2203 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
2204 struct ofpbuf *msg, *next;
2205
2206 LIST_FOR_EACH_SAFE (msg, next, list_node, &ofconn->updates) {
a3d1ff00
BP
2207 unsigned int n_bytes;
2208
2b07c8b1
BP
2209 list_remove(&msg->list_node);
2210 ofconn_send(ofconn, msg, ofconn->monitor_counter);
a3d1ff00
BP
2211 n_bytes = rconn_packet_counter_n_bytes(ofconn->monitor_counter);
2212 if (!ofconn->monitor_paused && n_bytes > 128 * 1024) {
2b07c8b1
BP
2213 struct ofpbuf *pause;
2214
2215 COVERAGE_INC(ofmonitor_pause);
2216 ofconn->monitor_paused = monitor_seqno++;
982697a4
BP
2217 pause = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_MONITOR_PAUSED,
2218 OFP10_VERSION, htonl(0), 0);
2b07c8b1
BP
2219 ofconn_send(ofconn, pause, ofconn->monitor_counter);
2220 }
2221 }
2222 }
2223}
2224
2225static void
2226ofmonitor_resume(struct ofconn *ofconn)
4cd1bc9d 2227 OVS_REQUIRES(ofproto_mutex)
2b07c8b1 2228{
a8e547c1 2229 struct rule_collection rules;
982697a4 2230 struct ofpbuf *resumed;
2b07c8b1 2231 struct ofmonitor *m;
2b07c8b1
BP
2232 struct list msgs;
2233
a8e547c1 2234 rule_collection_init(&rules);
2b07c8b1
BP
2235 HMAP_FOR_EACH (m, ofconn_node, &ofconn->monitors) {
2236 ofmonitor_collect_resume_rules(m, ofconn->monitor_paused, &rules);
2237 }
2238
2239 list_init(&msgs);
2240 ofmonitor_compose_refresh_updates(&rules, &msgs);
2241
982697a4
BP
2242 resumed = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_MONITOR_RESUMED, OFP10_VERSION,
2243 htonl(0), 0);
2244 list_push_back(&msgs, &resumed->list_node);
2b07c8b1
BP
2245 ofconn_send_replies(ofconn, &msgs);
2246
2247 ofconn->monitor_paused = 0;
2248}
2249
4cd1bc9d
BP
2250static bool
2251ofmonitor_may_resume(const struct ofconn *ofconn)
2252 OVS_REQUIRES(ofproto_mutex)
2253{
2254 return (ofconn->monitor_paused != 0
2255 && !rconn_packet_counter_n_packets(ofconn->monitor_counter));
2256}
2257
2b07c8b1
BP
2258static void
2259ofmonitor_run(struct connmgr *mgr)
2260{
2261 struct ofconn *ofconn;
2262
4cd1bc9d 2263 ovs_mutex_lock(&ofproto_mutex);
2b07c8b1 2264 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
4cd1bc9d 2265 if (ofmonitor_may_resume(ofconn)) {
2b07c8b1
BP
2266 COVERAGE_INC(ofmonitor_resume);
2267 ofmonitor_resume(ofconn);
2268 }
2269 }
4cd1bc9d 2270 ovs_mutex_unlock(&ofproto_mutex);
2b07c8b1
BP
2271}
2272
2273static void
2274ofmonitor_wait(struct connmgr *mgr)
2275{
2276 struct ofconn *ofconn;
2277
4cd1bc9d 2278 ovs_mutex_lock(&ofproto_mutex);
2b07c8b1 2279 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
4cd1bc9d 2280 if (ofmonitor_may_resume(ofconn)) {
2b07c8b1
BP
2281 poll_immediate_wake();
2282 }
2283 }
4cd1bc9d 2284 ovs_mutex_unlock(&ofproto_mutex);
2b07c8b1 2285}