]> git.proxmox.com Git - ovs.git/blame - ofproto/connmgr.c
hash: Introduce an implementation of murmurhash.
[ovs.git] / ofproto / connmgr.c
CommitLineData
19a87e36 1/*
e0edde6f 2 * Copyright (c) 2009, 2010, 2011, 2012 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"
19a87e36
BP
25#include "fail-open.h"
26#include "in-band.h"
27#include "odp-util.h"
f25d0cf3 28#include "ofp-actions.h"
982697a4 29#include "ofp-msgs.h"
19a87e36
BP
30#include "ofp-util.h"
31#include "ofpbuf.h"
5bee6e26 32#include "ofproto-provider.h"
19a87e36
BP
33#include "pinsched.h"
34#include "poll-loop.h"
35#include "pktbuf.h"
36#include "rconn.h"
37#include "shash.h"
0d085684 38#include "simap.h"
5bd31620 39#include "stream.h"
19a87e36
BP
40#include "timeval.h"
41#include "vconn.h"
42#include "vlog.h"
43
44VLOG_DEFINE_THIS_MODULE(connmgr);
45static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
46
19a87e36
BP
47/* An OpenFlow connection. */
48struct ofconn {
c4b31872
BP
49/* Configuration that persists from one connection to the next. */
50
19a87e36 51 struct list node; /* In struct connmgr's "all_conns" list. */
c4b31872
BP
52 struct hmap_node hmap_node; /* In struct connmgr's "controllers" map. */
53
54 struct connmgr *connmgr; /* Connection's manager. */
19a87e36
BP
55 struct rconn *rconn; /* OpenFlow connection. */
56 enum ofconn_type type; /* Type. */
c4b31872 57 enum ofproto_band band; /* In-band or out-of-band? */
9886b662 58 bool enable_async_msgs; /* Initially enable async messages? */
c4b31872
BP
59
60/* State that should be cleared from one connection to the next. */
61
62 /* OpenFlow state. */
63 enum nx_role role; /* Role. */
27527aa0 64 enum ofputil_protocol protocol; /* Current protocol variant. */
54834960 65 enum nx_packet_in_format packet_in_format; /* OFPT_PACKET_IN format. */
19a87e36 66
7ee20df1
BP
67 /* Asynchronous flow table operation support. */
68 struct list opgroups; /* Contains pending "ofopgroups", if any. */
69 struct ofpbuf *blocked; /* Postponed OpenFlow message, if any. */
70 bool retry; /* True if 'blocked' is ready to try again. */
71
19a87e36
BP
72 /* OFPT_PACKET_IN related data. */
73 struct rconn_packet_counter *packet_in_counter; /* # queued on 'rconn'. */
74#define N_SCHEDULERS 2
75 struct pinsched *schedulers[N_SCHEDULERS];
76 struct pktbuf *pktbuf; /* OpenFlow packet buffers. */
77 int miss_send_len; /* Bytes to send of buffered packets. */
a7349929 78 uint16_t controller_id; /* Connection controller ID. */
19a87e36
BP
79
80 /* Number of OpenFlow messages queued on 'rconn' as replies to OpenFlow
81 * requests, and the maximum number before we stop reading OpenFlow
82 * requests. */
83#define OFCONN_REPLY_MAX 100
84 struct rconn_packet_counter *reply_counter;
80d5aefd
BP
85
86 /* Asynchronous message configuration in each possible roles.
87 *
88 * A 1-bit enables sending an asynchronous message for one possible reason
89 * that the message might be generated, a 0-bit disables it. */
90 uint32_t master_async_config[OAM_N_TYPES]; /* master, other */
91 uint32_t slave_async_config[OAM_N_TYPES]; /* slave */
2b07c8b1
BP
92
93 /* Flow monitors. */
94 struct hmap monitors; /* Contains "struct ofmonitor"s. */
95 struct list updates; /* List of "struct ofpbuf"s. */
96 bool sent_abbrev_update; /* Does 'updates' contain NXFME_ABBREV? */
97 struct rconn_packet_counter *monitor_counter;
98 uint64_t monitor_paused;
19a87e36
BP
99};
100
101static struct ofconn *ofconn_create(struct connmgr *, struct rconn *,
9886b662 102 enum ofconn_type, bool enable_async_msgs);
19a87e36 103static void ofconn_destroy(struct ofconn *);
c4b31872 104static void ofconn_flush(struct ofconn *);
19a87e36
BP
105
106static void ofconn_reconfigure(struct ofconn *,
107 const struct ofproto_controller *);
108
109static void ofconn_run(struct ofconn *,
7ee20df1 110 bool (*handle_openflow)(struct ofconn *,
19a87e36 111 struct ofpbuf *ofp_msg));
7ee20df1 112static void ofconn_wait(struct ofconn *, bool handling_openflow);
19a87e36
BP
113
114static const char *ofconn_get_target(const struct ofconn *);
115static char *ofconn_make_name(const struct connmgr *, const char *target);
116
117static void ofconn_set_rate_limit(struct ofconn *, int rate, int burst);
118
19a87e36
BP
119static void ofconn_send(const struct ofconn *, struct ofpbuf *,
120 struct rconn_packet_counter *);
121
122static void do_send_packet_in(struct ofpbuf *, void *ofconn_);
123
124/* A listener for incoming OpenFlow "service" connections. */
125struct ofservice {
126 struct hmap_node node; /* In struct connmgr's "services" hmap. */
127 struct pvconn *pvconn; /* OpenFlow connection listener. */
128
129 /* These are not used by ofservice directly. They are settings for
130 * accepted "struct ofconn"s from the pvconn. */
131 int probe_interval; /* Max idle time before probing, in seconds. */
132 int rate_limit; /* Max packet-in rate in packets per second. */
133 int burst_limit; /* Limit on accumulating packet credits. */
9886b662 134 bool enable_async_msgs; /* Initially enable async messages? */
f125905c 135 uint8_t dscp; /* DSCP Value for controller connection */
19a87e36
BP
136};
137
138static void ofservice_reconfigure(struct ofservice *,
139 const struct ofproto_controller *);
f125905c 140static int ofservice_create(struct connmgr *, const char *target, uint8_t dscp);
19a87e36
BP
141static void ofservice_destroy(struct connmgr *, struct ofservice *);
142static struct ofservice *ofservice_lookup(struct connmgr *,
143 const char *target);
144
145/* Connection manager for an OpenFlow switch. */
146struct connmgr {
147 struct ofproto *ofproto;
148 char *name;
149 char *local_port_name;
150
151 /* OpenFlow connections. */
152 struct hmap controllers; /* Controller "struct ofconn"s. */
153 struct list all_conns; /* Contains "struct ofconn"s. */
154
155 /* OpenFlow listeners. */
156 struct hmap services; /* Contains "struct ofservice"s. */
157 struct pvconn **snoops;
158 size_t n_snoops;
159
160 /* Fail open. */
161 struct fail_open *fail_open;
162 enum ofproto_fail_mode fail_mode;
163
164 /* In-band control. */
165 struct in_band *in_band;
19a87e36
BP
166 struct sockaddr_in *extra_in_band_remotes;
167 size_t n_extra_remotes;
168 int in_band_queue;
169};
170
171static void update_in_band_remotes(struct connmgr *);
172static void add_snooper(struct connmgr *, struct vconn *);
2b07c8b1
BP
173static void ofmonitor_run(struct connmgr *);
174static void ofmonitor_wait(struct connmgr *);
19a87e36
BP
175
176/* Creates and returns a new connection manager owned by 'ofproto'. 'name' is
177 * a name for the ofproto suitable for using in log messages.
178 * 'local_port_name' is the name of the local port (OFPP_LOCAL) within
179 * 'ofproto'. */
180struct connmgr *
181connmgr_create(struct ofproto *ofproto,
182 const char *name, const char *local_port_name)
183{
184 struct connmgr *mgr;
185
186 mgr = xmalloc(sizeof *mgr);
187 mgr->ofproto = ofproto;
188 mgr->name = xstrdup(name);
189 mgr->local_port_name = xstrdup(local_port_name);
190
191 hmap_init(&mgr->controllers);
192 list_init(&mgr->all_conns);
193
194 hmap_init(&mgr->services);
195 mgr->snoops = NULL;
196 mgr->n_snoops = 0;
197
198 mgr->fail_open = NULL;
199 mgr->fail_mode = OFPROTO_FAIL_SECURE;
200
201 mgr->in_band = NULL;
19a87e36
BP
202 mgr->extra_in_band_remotes = NULL;
203 mgr->n_extra_remotes = 0;
204 mgr->in_band_queue = -1;
205
206 return mgr;
207}
208
209/* Frees 'mgr' and all of its resources. */
210void
211connmgr_destroy(struct connmgr *mgr)
212{
213 struct ofservice *ofservice, *next_ofservice;
214 struct ofconn *ofconn, *next_ofconn;
215 size_t i;
216
217 if (!mgr) {
218 return;
219 }
220
221 LIST_FOR_EACH_SAFE (ofconn, next_ofconn, node, &mgr->all_conns) {
222 ofconn_destroy(ofconn);
223 }
224 hmap_destroy(&mgr->controllers);
225
226 HMAP_FOR_EACH_SAFE (ofservice, next_ofservice, node, &mgr->services) {
227 ofservice_destroy(mgr, ofservice);
228 }
229 hmap_destroy(&mgr->services);
230
231 for (i = 0; i < mgr->n_snoops; i++) {
232 pvconn_close(mgr->snoops[i]);
233 }
234 free(mgr->snoops);
235
236 fail_open_destroy(mgr->fail_open);
237 mgr->fail_open = NULL;
238
239 in_band_destroy(mgr->in_band);
240 mgr->in_band = NULL;
241 free(mgr->extra_in_band_remotes);
242 free(mgr->name);
243 free(mgr->local_port_name);
244
245 free(mgr);
246}
247
7ee20df1
BP
248/* Does all of the periodic maintenance required by 'mgr'.
249 *
250 * If 'handle_openflow' is nonnull, calls 'handle_openflow' for each message
251 * received on an OpenFlow connection, passing along the OpenFlow connection
252 * itself and the message that was sent. If 'handle_openflow' returns true,
253 * the message is considered to be fully processed. If 'handle_openflow'
254 * returns false, the message is considered not to have been processed at all;
255 * it will be stored and re-presented to 'handle_openflow' following the next
256 * call to connmgr_retry(). 'handle_openflow' must not modify or free the
257 * message.
258 *
259 * If 'handle_openflow' is NULL, no OpenFlow messages will be processed and
260 * other activities that could affect the flow table (in-band processing,
261 * fail-open processing) are suppressed too. */
19a87e36
BP
262void
263connmgr_run(struct connmgr *mgr,
7ee20df1 264 bool (*handle_openflow)(struct ofconn *, struct ofpbuf *ofp_msg))
19a87e36
BP
265{
266 struct ofconn *ofconn, *next_ofconn;
267 struct ofservice *ofservice;
268 size_t i;
269
7ee20df1 270 if (handle_openflow && mgr->in_band) {
6da1e809
BP
271 if (!in_band_run(mgr->in_band)) {
272 in_band_destroy(mgr->in_band);
273 mgr->in_band = NULL;
274 }
19a87e36
BP
275 }
276
277 LIST_FOR_EACH_SAFE (ofconn, next_ofconn, node, &mgr->all_conns) {
278 ofconn_run(ofconn, handle_openflow);
279 }
2b07c8b1 280 ofmonitor_run(mgr);
19a87e36
BP
281
282 /* Fail-open maintenance. Do this after processing the ofconns since
283 * fail-open checks the status of the controller rconn. */
7ee20df1 284 if (handle_openflow && mgr->fail_open) {
19a87e36
BP
285 fail_open_run(mgr->fail_open);
286 }
287
288 HMAP_FOR_EACH (ofservice, node, &mgr->services) {
289 struct vconn *vconn;
290 int retval;
291
87ea5e5e 292 retval = pvconn_accept(ofservice->pvconn, OFP10_VERSION, &vconn);
19a87e36
BP
293 if (!retval) {
294 struct rconn *rconn;
295 char *name;
296
f125905c
MM
297 /* Passing default value for creation of the rconn */
298 rconn = rconn_create(ofservice->probe_interval, 0, ofservice->dscp);
19a87e36
BP
299 name = ofconn_make_name(mgr, vconn_get_name(vconn));
300 rconn_connect_unreliably(rconn, vconn, name);
301 free(name);
302
9886b662
BP
303 ofconn = ofconn_create(mgr, rconn, OFCONN_SERVICE,
304 ofservice->enable_async_msgs);
19a87e36
BP
305 ofconn_set_rate_limit(ofconn, ofservice->rate_limit,
306 ofservice->burst_limit);
307 } else if (retval != EAGAIN) {
308 VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval));
309 }
310 }
311
312 for (i = 0; i < mgr->n_snoops; i++) {
313 struct vconn *vconn;
314 int retval;
315
87ea5e5e 316 retval = pvconn_accept(mgr->snoops[i], OFP10_VERSION, &vconn);
19a87e36
BP
317 if (!retval) {
318 add_snooper(mgr, vconn);
319 } else if (retval != EAGAIN) {
320 VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval));
321 }
322 }
323}
324
7ee20df1
BP
325/* Causes the poll loop to wake up when connmgr_run() needs to run.
326 *
327 * If 'handling_openflow' is true, arriving OpenFlow messages and other
328 * activities that affect the flow table will wake up the poll loop. If
329 * 'handling_openflow' is false, they will not. */
19a87e36 330void
7ee20df1 331connmgr_wait(struct connmgr *mgr, bool handling_openflow)
19a87e36
BP
332{
333 struct ofservice *ofservice;
334 struct ofconn *ofconn;
335 size_t i;
336
337 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
7ee20df1 338 ofconn_wait(ofconn, handling_openflow);
19a87e36 339 }
2b07c8b1 340 ofmonitor_wait(mgr);
7ee20df1 341 if (handling_openflow && mgr->in_band) {
19a87e36
BP
342 in_band_wait(mgr->in_band);
343 }
7ee20df1 344 if (handling_openflow && mgr->fail_open) {
19a87e36
BP
345 fail_open_wait(mgr->fail_open);
346 }
347 HMAP_FOR_EACH (ofservice, node, &mgr->services) {
348 pvconn_wait(ofservice->pvconn);
349 }
350 for (i = 0; i < mgr->n_snoops; i++) {
351 pvconn_wait(mgr->snoops[i]);
352 }
353}
354
0d085684
BP
355/* Adds some memory usage statistics for 'mgr' into 'usage', for use with
356 * memory_report(). */
357void
358connmgr_get_memory_usage(const struct connmgr *mgr, struct simap *usage)
359{
360 const struct ofconn *ofconn;
361 unsigned int packets = 0;
362 unsigned int ofconns = 0;
363
364 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
365 int i;
366
367 ofconns++;
368
369 packets += rconn_count_txqlen(ofconn->rconn);
370 for (i = 0; i < N_SCHEDULERS; i++) {
371 packets += pinsched_count_txqlen(ofconn->schedulers[i]);
372 }
373 packets += pktbuf_count_packets(ofconn->pktbuf);
374 }
375 simap_increase(usage, "ofconns", ofconns);
376 simap_increase(usage, "packets", packets);
377}
378
19a87e36
BP
379/* Returns the ofproto that owns 'ofconn''s connmgr. */
380struct ofproto *
381ofconn_get_ofproto(const struct ofconn *ofconn)
382{
383 return ofconn->connmgr->ofproto;
384}
7ee20df1
BP
385
386/* If processing of OpenFlow messages was blocked on any 'mgr' ofconns by
387 * returning false to the 'handle_openflow' callback to connmgr_run(), this
388 * re-enables them. */
389void
390connmgr_retry(struct connmgr *mgr)
391{
392 struct ofconn *ofconn;
393
394 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
395 ofconn->retry = true;
396 }
397}
19a87e36
BP
398\f
399/* OpenFlow configuration. */
400
f125905c 401static void add_controller(struct connmgr *, const char *target, uint8_t dscp);
19a87e36
BP
402static struct ofconn *find_controller_by_target(struct connmgr *,
403 const char *target);
404static void update_fail_open(struct connmgr *);
405static int set_pvconns(struct pvconn ***pvconnsp, size_t *n_pvconnsp,
81e2083f 406 const struct sset *);
19a87e36
BP
407
408/* Returns true if 'mgr' has any configured primary controllers.
409 *
410 * Service controllers do not count, but configured primary controllers do
411 * count whether or not they are currently connected. */
412bool
413connmgr_has_controllers(const struct connmgr *mgr)
414{
415 return !hmap_is_empty(&mgr->controllers);
416}
417
418/* Initializes 'info' and populates it with information about each configured
419 * primary controller. The keys in 'info' are the controllers' targets; the
420 * data values are corresponding "struct ofproto_controller_info".
421 *
422 * The caller owns 'info' and everything in it and should free it when it is no
423 * longer needed. */
424void
425connmgr_get_controller_info(struct connmgr *mgr, struct shash *info)
426{
427 const struct ofconn *ofconn;
428
19a87e36
BP
429 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
430 const struct rconn *rconn = ofconn->rconn;
5d279086 431 const char *target = rconn_get_target(rconn);
19a87e36 432
5d279086
AE
433 if (!shash_find(info, target)) {
434 struct ofproto_controller_info *cinfo = xmalloc(sizeof *cinfo);
435 time_t now = time_now();
436 time_t last_connection = rconn_get_last_connection(rconn);
437 time_t last_disconnect = rconn_get_last_disconnect(rconn);
438 int last_error = rconn_get_last_error(rconn);
19a87e36 439
5d279086 440 shash_add(info, target, cinfo);
19a87e36 441
5d279086
AE
442 cinfo->is_connected = rconn_is_connected(rconn);
443 cinfo->role = ofconn->role;
19a87e36 444
5d279086 445 cinfo->pairs.n = 0;
19a87e36 446
5d279086
AE
447 if (last_error) {
448 cinfo->pairs.keys[cinfo->pairs.n] = "last_error";
449 cinfo->pairs.values[cinfo->pairs.n++]
450 = xstrdup(ovs_retval_to_string(last_error));
451 }
19a87e36 452
5d279086 453 cinfo->pairs.keys[cinfo->pairs.n] = "state";
19a87e36 454 cinfo->pairs.values[cinfo->pairs.n++]
5d279086
AE
455 = xstrdup(rconn_get_state(rconn));
456
457 if (last_connection != TIME_MIN) {
458 cinfo->pairs.keys[cinfo->pairs.n] = "sec_since_connect";
459 cinfo->pairs.values[cinfo->pairs.n++]
460 = xasprintf("%ld", (long int) (now - last_connection));
461 }
462
463 if (last_disconnect != TIME_MIN) {
464 cinfo->pairs.keys[cinfo->pairs.n] = "sec_since_disconnect";
465 cinfo->pairs.values[cinfo->pairs.n++]
466 = xasprintf("%ld", (long int) (now - last_disconnect));
467 }
19a87e36
BP
468 }
469 }
470}
471
72ba2ed3
AE
472void
473connmgr_free_controller_info(struct shash *info)
474{
475 struct shash_node *node;
476
477 SHASH_FOR_EACH (node, info) {
478 struct ofproto_controller_info *cinfo = node->data;
479 while (cinfo->pairs.n) {
ebc56baa 480 free(CONST_CAST(char *, cinfo->pairs.values[--cinfo->pairs.n]));
72ba2ed3
AE
481 }
482 free(cinfo);
483 }
484 shash_destroy(info);
485}
486
19a87e36
BP
487/* Changes 'mgr''s set of controllers to the 'n_controllers' controllers in
488 * 'controllers'. */
489void
490connmgr_set_controllers(struct connmgr *mgr,
491 const struct ofproto_controller *controllers,
492 size_t n_controllers)
493{
08fd19f0 494 bool had_controllers = connmgr_has_controllers(mgr);
19a87e36
BP
495 struct shash new_controllers;
496 struct ofconn *ofconn, *next_ofconn;
497 struct ofservice *ofservice, *next_ofservice;
19a87e36
BP
498 size_t i;
499
500 /* Create newly configured controllers and services.
501 * Create a name to ofproto_controller mapping in 'new_controllers'. */
502 shash_init(&new_controllers);
503 for (i = 0; i < n_controllers; i++) {
504 const struct ofproto_controller *c = &controllers[i];
505
506 if (!vconn_verify_name(c->target)) {
507 if (!find_controller_by_target(mgr, c->target)) {
1ea9e609
BP
508 VLOG_INFO("%s: added primary controller \"%s\"",
509 mgr->name, c->target);
f125905c 510 add_controller(mgr, c->target, c->dscp);
19a87e36
BP
511 }
512 } else if (!pvconn_verify_name(c->target)) {
513 if (!ofservice_lookup(mgr, c->target)) {
1ea9e609
BP
514 VLOG_INFO("%s: added service controller \"%s\"",
515 mgr->name, c->target);
f125905c 516 ofservice_create(mgr, c->target, c->dscp);
19a87e36
BP
517 }
518 } else {
519 VLOG_WARN_RL(&rl, "%s: unsupported controller \"%s\"",
520 mgr->name, c->target);
521 continue;
522 }
523
524 shash_add_once(&new_controllers, c->target, &controllers[i]);
525 }
526
527 /* Delete controllers that are no longer configured.
528 * Update configuration of all now-existing controllers. */
19a87e36 529 HMAP_FOR_EACH_SAFE (ofconn, next_ofconn, hmap_node, &mgr->controllers) {
1ea9e609 530 const char *target = ofconn_get_target(ofconn);
19a87e36
BP
531 struct ofproto_controller *c;
532
1ea9e609 533 c = shash_find_data(&new_controllers, target);
19a87e36 534 if (!c) {
1ea9e609
BP
535 VLOG_INFO("%s: removed primary controller \"%s\"",
536 mgr->name, target);
19a87e36
BP
537 ofconn_destroy(ofconn);
538 } else {
539 ofconn_reconfigure(ofconn, c);
540 }
541 }
542
543 /* Delete services that are no longer configured.
544 * Update configuration of all now-existing services. */
545 HMAP_FOR_EACH_SAFE (ofservice, next_ofservice, node, &mgr->services) {
1ea9e609 546 const char *target = pvconn_get_name(ofservice->pvconn);
19a87e36
BP
547 struct ofproto_controller *c;
548
1ea9e609 549 c = shash_find_data(&new_controllers, target);
19a87e36 550 if (!c) {
1ea9e609
BP
551 VLOG_INFO("%s: removed service controller \"%s\"",
552 mgr->name, target);
19a87e36
BP
553 ofservice_destroy(mgr, ofservice);
554 } else {
555 ofservice_reconfigure(ofservice, c);
556 }
557 }
558
559 shash_destroy(&new_controllers);
560
561 update_in_band_remotes(mgr);
562 update_fail_open(mgr);
08fd19f0
BP
563 if (had_controllers != connmgr_has_controllers(mgr)) {
564 ofproto_flush_flows(mgr->ofproto);
565 }
19a87e36
BP
566}
567
568/* Drops the connections between 'mgr' and all of its primary and secondary
569 * controllers, forcing them to reconnect. */
570void
571connmgr_reconnect(const struct connmgr *mgr)
572{
573 struct ofconn *ofconn;
574
575 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
576 rconn_reconnect(ofconn->rconn);
577 }
578}
579
580/* Sets the "snoops" for 'mgr' to the pvconn targets listed in 'snoops'.
581 *
582 * A "snoop" is a pvconn to which every OpenFlow message to or from the most
583 * important controller on 'mgr' is mirrored. */
584int
81e2083f 585connmgr_set_snoops(struct connmgr *mgr, const struct sset *snoops)
19a87e36
BP
586{
587 return set_pvconns(&mgr->snoops, &mgr->n_snoops, snoops);
588}
589
590/* Adds each of the snoops currently configured on 'mgr' to 'snoops'. */
591void
81e2083f 592connmgr_get_snoops(const struct connmgr *mgr, struct sset *snoops)
19a87e36
BP
593{
594 size_t i;
595
596 for (i = 0; i < mgr->n_snoops; i++) {
81e2083f 597 sset_add(snoops, pvconn_get_name(mgr->snoops[i]));
19a87e36
BP
598 }
599}
600
81e2083f
BP
601/* Returns true if 'mgr' has at least one snoop, false if it has none. */
602bool
603connmgr_has_snoops(const struct connmgr *mgr)
604{
605 return mgr->n_snoops > 0;
606}
607
19a87e36
BP
608/* Creates a new controller for 'target' in 'mgr'. update_controller() needs
609 * to be called later to finish the new ofconn's configuration. */
610static void
f125905c 611add_controller(struct connmgr *mgr, const char *target, uint8_t dscp)
19a87e36
BP
612{
613 char *name = ofconn_make_name(mgr, target);
614 struct ofconn *ofconn;
615
f125905c 616 ofconn = ofconn_create(mgr, rconn_create(5, 8, dscp), OFCONN_PRIMARY, true);
19a87e36 617 ofconn->pktbuf = pktbuf_create();
19a87e36
BP
618 rconn_connect(ofconn->rconn, target, name);
619 hmap_insert(&mgr->controllers, &ofconn->hmap_node, hash_string(target, 0));
620
621 free(name);
622}
623
624static struct ofconn *
625find_controller_by_target(struct connmgr *mgr, const char *target)
626{
627 struct ofconn *ofconn;
628
629 HMAP_FOR_EACH_WITH_HASH (ofconn, hmap_node,
630 hash_string(target, 0), &mgr->controllers) {
631 if (!strcmp(ofconn_get_target(ofconn), target)) {
632 return ofconn;
633 }
634 }
635 return NULL;
636}
637
638static void
639update_in_band_remotes(struct connmgr *mgr)
640{
641 struct sockaddr_in *addrs;
642 size_t max_addrs, n_addrs;
643 struct ofconn *ofconn;
644 size_t i;
645
646 /* Allocate enough memory for as many remotes as we could possibly have. */
647 max_addrs = mgr->n_extra_remotes + hmap_count(&mgr->controllers);
648 addrs = xmalloc(max_addrs * sizeof *addrs);
649 n_addrs = 0;
650
651 /* Add all the remotes. */
652 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
653 struct sockaddr_in *sin = &addrs[n_addrs];
ac4c900d 654 const char *target = rconn_get_target(ofconn->rconn);
19a87e36
BP
655
656 if (ofconn->band == OFPROTO_OUT_OF_BAND) {
657 continue;
658 }
659
ac4c900d
AA
660 if (stream_parse_target_with_default_ports(target,
661 OFP_TCP_PORT,
662 OFP_SSL_PORT,
663 sin)) {
19a87e36
BP
664 n_addrs++;
665 }
666 }
667 for (i = 0; i < mgr->n_extra_remotes; i++) {
668 addrs[n_addrs++] = mgr->extra_in_band_remotes[i];
669 }
670
671 /* Create or update or destroy in-band. */
672 if (n_addrs) {
673 if (!mgr->in_band) {
674 in_band_create(mgr->ofproto, mgr->local_port_name, &mgr->in_band);
675 }
19a87e36 676 in_band_set_queue(mgr->in_band, mgr->in_band_queue);
19a87e36 677 } else {
6da1e809
BP
678 /* in_band_run() needs a chance to delete any existing in-band flows.
679 * We will destroy mgr->in_band after it's done with that. */
680 }
681 if (mgr->in_band) {
682 in_band_set_remotes(mgr->in_band, addrs, n_addrs);
19a87e36
BP
683 }
684
685 /* Clean up. */
686 free(addrs);
687}
688
689static void
690update_fail_open(struct connmgr *mgr)
691{
692 if (connmgr_has_controllers(mgr)
693 && mgr->fail_mode == OFPROTO_FAIL_STANDALONE) {
694 if (!mgr->fail_open) {
695 mgr->fail_open = fail_open_create(mgr->ofproto, mgr);
696 }
697 } else {
698 fail_open_destroy(mgr->fail_open);
699 mgr->fail_open = NULL;
700 }
701}
702
703static int
704set_pvconns(struct pvconn ***pvconnsp, size_t *n_pvconnsp,
81e2083f 705 const struct sset *sset)
19a87e36
BP
706{
707 struct pvconn **pvconns = *pvconnsp;
708 size_t n_pvconns = *n_pvconnsp;
81e2083f 709 const char *name;
19a87e36
BP
710 int retval = 0;
711 size_t i;
712
713 for (i = 0; i < n_pvconns; i++) {
714 pvconn_close(pvconns[i]);
715 }
716 free(pvconns);
717
81e2083f 718 pvconns = xmalloc(sset_count(sset) * sizeof *pvconns);
19a87e36 719 n_pvconns = 0;
81e2083f 720 SSET_FOR_EACH (name, sset) {
19a87e36
BP
721 struct pvconn *pvconn;
722 int error;
723
ef8a3d14 724 error = pvconn_open(name, &pvconn, 0);
19a87e36
BP
725 if (!error) {
726 pvconns[n_pvconns++] = pvconn;
727 } else {
728 VLOG_ERR("failed to listen on %s: %s", name, strerror(error));
729 if (!retval) {
730 retval = error;
731 }
732 }
733 }
734
735 *pvconnsp = pvconns;
736 *n_pvconnsp = n_pvconns;
737
738 return retval;
739}
740
741/* Returns a "preference level" for snooping 'ofconn'. A higher return value
742 * means that 'ofconn' is more interesting for monitoring than a lower return
743 * value. */
744static int
745snoop_preference(const struct ofconn *ofconn)
746{
747 switch (ofconn->role) {
748 case NX_ROLE_MASTER:
749 return 3;
750 case NX_ROLE_OTHER:
751 return 2;
752 case NX_ROLE_SLAVE:
753 return 1;
754 default:
755 /* Shouldn't happen. */
756 return 0;
757 }
758}
759
760/* One of 'mgr''s "snoop" pvconns has accepted a new connection on 'vconn'.
761 * Connects this vconn to a controller. */
762static void
763add_snooper(struct connmgr *mgr, struct vconn *vconn)
764{
765 struct ofconn *ofconn, *best;
766
767 /* Pick a controller for monitoring. */
768 best = NULL;
769 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
770 if (ofconn->type == OFCONN_PRIMARY
771 && (!best || snoop_preference(ofconn) > snoop_preference(best))) {
772 best = ofconn;
773 }
774 }
775
776 if (best) {
777 rconn_add_monitor(best->rconn, vconn);
778 } else {
779 VLOG_INFO_RL(&rl, "no controller connection to snoop");
780 vconn_close(vconn);
781 }
782}
783\f
784/* Public ofconn functions. */
785
786/* Returns the connection type, either OFCONN_PRIMARY or OFCONN_SERVICE. */
787enum ofconn_type
788ofconn_get_type(const struct ofconn *ofconn)
789{
790 return ofconn->type;
791}
792
793/* Returns the role configured for 'ofconn'.
794 *
795 * The default role, if no other role has been set, is NX_ROLE_OTHER. */
796enum nx_role
797ofconn_get_role(const struct ofconn *ofconn)
798{
799 return ofconn->role;
800}
801
802/* Changes 'ofconn''s role to 'role'. If 'role' is NX_ROLE_MASTER then any
803 * existing master is demoted to a slave. */
804void
805ofconn_set_role(struct ofconn *ofconn, enum nx_role role)
806{
807 if (role == NX_ROLE_MASTER) {
808 struct ofconn *other;
809
810 HMAP_FOR_EACH (other, hmap_node, &ofconn->connmgr->controllers) {
811 if (other->role == NX_ROLE_MASTER) {
812 other->role = NX_ROLE_SLAVE;
813 }
814 }
815 }
816 ofconn->role = role;
817}
818
f0fd1a17 819void
80d5aefd 820ofconn_set_invalid_ttl_to_controller(struct ofconn *ofconn, bool enable)
f0fd1a17 821{
80d5aefd
BP
822 uint32_t bit = 1u << OFPR_INVALID_TTL;
823 if (enable) {
824 ofconn->master_async_config[OAM_PACKET_IN] |= bit;
825 } else {
826 ofconn->master_async_config[OAM_PACKET_IN] &= ~bit;
827 }
f0fd1a17
PS
828}
829
830bool
831ofconn_get_invalid_ttl_to_controller(struct ofconn *ofconn)
832{
80d5aefd
BP
833 uint32_t bit = 1u << OFPR_INVALID_TTL;
834 return (ofconn->master_async_config[OAM_PACKET_IN] & bit) != 0;
f0fd1a17
PS
835}
836
27527aa0 837/* Returns the currently configured protocol for 'ofconn', one of OFPUTIL_P_*.
19a87e36 838 *
27527aa0
BP
839 * The default, if no other format has been set, is OFPUTIL_P_OPENFLOW10. */
840enum ofputil_protocol
841ofconn_get_protocol(struct ofconn *ofconn)
19a87e36 842{
27527aa0 843 return ofconn->protocol;
19a87e36
BP
844}
845
27527aa0
BP
846/* Sets the protocol for 'ofconn' to 'protocol' (one of OFPUTIL_P_*).
847 *
848 * (This doesn't actually send anything to accomplish this. Presumably the
849 * caller already did that.) */
19a87e36 850void
27527aa0 851ofconn_set_protocol(struct ofconn *ofconn, enum ofputil_protocol protocol)
19a87e36 852{
27527aa0 853 ofconn->protocol = protocol;
19a87e36
BP
854}
855
54834960
EJ
856/* Returns the currently configured packet in format for 'ofconn', one of
857 * NXPIF_*.
858 *
859 * The default, if no other format has been set, is NXPIF_OPENFLOW10. */
860enum nx_packet_in_format
861ofconn_get_packet_in_format(struct ofconn *ofconn)
862{
863 return ofconn->packet_in_format;
864}
865
866/* Sets the packet in format for 'ofconn' to 'packet_in_format' (one of
867 * NXPIF_*). */
868void
869ofconn_set_packet_in_format(struct ofconn *ofconn,
870 enum nx_packet_in_format packet_in_format)
871{
872 ofconn->packet_in_format = packet_in_format;
873}
874
a7349929
BP
875/* Sets the controller connection ID for 'ofconn' to 'controller_id'.
876 *
877 * The connection controller ID is used for OFPP_CONTROLLER and
878 * NXAST_CONTROLLER actions. See "struct nx_action_controller" for details. */
879void
880ofconn_set_controller_id(struct ofconn *ofconn, uint16_t controller_id)
881{
882 ofconn->controller_id = controller_id;
883}
884
19a87e36
BP
885/* Returns the default miss send length for 'ofconn'. */
886int
887ofconn_get_miss_send_len(const struct ofconn *ofconn)
888{
889 return ofconn->miss_send_len;
890}
891
892/* Sets the default miss send length for 'ofconn' to 'miss_send_len'. */
893void
894ofconn_set_miss_send_len(struct ofconn *ofconn, int miss_send_len)
895{
896 ofconn->miss_send_len = miss_send_len;
897}
898
80d5aefd
BP
899void
900ofconn_set_async_config(struct ofconn *ofconn,
901 const uint32_t master_masks[OAM_N_TYPES],
902 const uint32_t slave_masks[OAM_N_TYPES])
903{
904 size_t size = sizeof ofconn->master_async_config;
905 memcpy(ofconn->master_async_config, master_masks, size);
906 memcpy(ofconn->slave_async_config, slave_masks, size);
907}
908
19a87e36
BP
909/* Sends 'msg' on 'ofconn', accounting it as a reply. (If there is a
910 * sufficient number of OpenFlow replies in-flight on a single ofconn, then the
911 * connmgr will stop accepting new OpenFlow requests on that ofconn until the
912 * controller has accepted some of the replies.) */
913void
914ofconn_send_reply(const struct ofconn *ofconn, struct ofpbuf *msg)
915{
916 ofconn_send(ofconn, msg, ofconn->reply_counter);
917}
918
63f2140a
BP
919/* Sends each of the messages in list 'replies' on 'ofconn' in order,
920 * accounting them as replies. */
921void
922ofconn_send_replies(const struct ofconn *ofconn, struct list *replies)
923{
924 struct ofpbuf *reply, *next;
925
926 LIST_FOR_EACH_SAFE (reply, next, list_node, replies) {
927 list_remove(&reply->list_node);
928 ofconn_send_reply(ofconn, reply);
929 }
930}
931
90bf1e07 932/* Sends 'error' on 'ofconn', as a reply to 'request'. Only at most the
1be5ff75
BP
933 * first 64 bytes of 'request' are used. */
934void
935ofconn_send_error(const struct ofconn *ofconn,
90bf1e07 936 const struct ofp_header *request, enum ofperr error)
1be5ff75 937{
90bf1e07 938 struct ofpbuf *reply;
76589937 939
90bf1e07
BP
940 reply = ofperr_encode_reply(error, request);
941 if (reply) {
76589937
BP
942 static struct vlog_rate_limit err_rl = VLOG_RATE_LIMIT_INIT(10, 10);
943
944 if (!VLOG_DROP_INFO(&err_rl)) {
76589937
BP
945 const char *type_name;
946 size_t request_len;
982697a4 947 enum ofpraw raw;
76589937
BP
948
949 request_len = ntohs(request->length);
982697a4
BP
950 type_name = (!ofpraw_decode_partial(&raw, request,
951 MIN(64, request_len))
952 ? ofpraw_get_name(raw)
76589937
BP
953 : "invalid");
954
76589937 955 VLOG_INFO("%s: sending %s error reply to %s message",
90bf1e07
BP
956 rconn_get_name(ofconn->rconn), ofperr_to_string(error),
957 type_name);
76589937 958 }
90bf1e07 959 ofconn_send_reply(ofconn, reply);
1be5ff75
BP
960 }
961}
962
19a87e36 963/* Same as pktbuf_retrieve(), using the pktbuf owned by 'ofconn'. */
90bf1e07 964enum ofperr
19a87e36
BP
965ofconn_pktbuf_retrieve(struct ofconn *ofconn, uint32_t id,
966 struct ofpbuf **bufferp, uint16_t *in_port)
967{
968 return pktbuf_retrieve(ofconn->pktbuf, id, bufferp, in_port);
969}
7ee20df1
BP
970
971/* Returns true if 'ofconn' has any pending opgroups. */
972bool
973ofconn_has_pending_opgroups(const struct ofconn *ofconn)
974{
975 return !list_is_empty(&ofconn->opgroups);
976}
977
7ee20df1
BP
978/* Adds 'ofconn_node' to 'ofconn''s list of pending opgroups.
979 *
980 * If 'ofconn' is destroyed or its connection drops, then 'ofconn' will remove
981 * 'ofconn_node' from the list and re-initialize it with list_init(). The
982 * client may, therefore, use list_is_empty(ofconn_node) to determine whether
983 * 'ofconn_node' is still associated with an active ofconn.
984 *
985 * The client may also remove ofconn_node from the list itself, with
986 * list_remove(). */
987void
988ofconn_add_opgroup(struct ofconn *ofconn, struct list *ofconn_node)
989{
990 list_push_back(&ofconn->opgroups, ofconn_node);
991}
19a87e36
BP
992\f
993/* Private ofconn functions. */
994
995static const char *
996ofconn_get_target(const struct ofconn *ofconn)
997{
998 return rconn_get_target(ofconn->rconn);
999}
1000
1001static struct ofconn *
9886b662
BP
1002ofconn_create(struct connmgr *mgr, struct rconn *rconn, enum ofconn_type type,
1003 bool enable_async_msgs)
19a87e36 1004{
c4b31872
BP
1005 struct ofconn *ofconn;
1006
1007 ofconn = xzalloc(sizeof *ofconn);
19a87e36
BP
1008 ofconn->connmgr = mgr;
1009 list_push_back(&mgr->all_conns, &ofconn->node);
1010 ofconn->rconn = rconn;
1011 ofconn->type = type;
9886b662 1012 ofconn->enable_async_msgs = enable_async_msgs;
c4b31872 1013
7ee20df1 1014 list_init(&ofconn->opgroups);
c4b31872 1015
2b07c8b1
BP
1016 hmap_init(&ofconn->monitors);
1017 list_init(&ofconn->updates);
1018
c4b31872
BP
1019 ofconn_flush(ofconn);
1020
19a87e36
BP
1021 return ofconn;
1022}
1023
c4b31872
BP
1024/* Clears all of the state in 'ofconn' that should not persist from one
1025 * connection to the next. */
7ee20df1
BP
1026static void
1027ofconn_flush(struct ofconn *ofconn)
1028{
2b07c8b1 1029 struct ofmonitor *monitor, *next_monitor;
c4b31872
BP
1030 int i;
1031
1032 ofconn->role = NX_ROLE_OTHER;
27527aa0 1033 ofconn->protocol = OFPUTIL_P_OF10;
c4b31872 1034 ofconn->packet_in_format = NXPIF_OPENFLOW10;
c4b31872
BP
1035
1036 /* Disassociate 'ofconn' from all of the ofopgroups that it initiated that
1037 * have not yet completed. (Those ofopgroups will still run to completion
1038 * in the usual way, but any errors that they run into will not be reported
1039 * on any OpenFlow channel.)
1040 *
1041 * Also discard any blocked operation on 'ofconn'. */
7ee20df1
BP
1042 while (!list_is_empty(&ofconn->opgroups)) {
1043 list_init(list_pop_front(&ofconn->opgroups));
1044 }
1045 ofpbuf_delete(ofconn->blocked);
1046 ofconn->blocked = NULL;
c4b31872
BP
1047
1048 rconn_packet_counter_destroy(ofconn->packet_in_counter);
1049 ofconn->packet_in_counter = rconn_packet_counter_create();
1050 for (i = 0; i < N_SCHEDULERS; i++) {
1051 if (ofconn->schedulers[i]) {
1052 int rate, burst;
1053
1054 pinsched_get_limits(ofconn->schedulers[i], &rate, &burst);
1055 pinsched_destroy(ofconn->schedulers[i]);
1056 ofconn->schedulers[i] = pinsched_create(rate, burst);
1057 }
1058 }
1059 if (ofconn->pktbuf) {
1060 pktbuf_destroy(ofconn->pktbuf);
1061 ofconn->pktbuf = pktbuf_create();
1062 }
1063 ofconn->miss_send_len = (ofconn->type == OFCONN_PRIMARY
1064 ? OFP_DEFAULT_MISS_SEND_LEN
1065 : 0);
a7349929 1066 ofconn->controller_id = 0;
c4b31872
BP
1067
1068 rconn_packet_counter_destroy(ofconn->reply_counter);
1069 ofconn->reply_counter = rconn_packet_counter_create();
80d5aefd 1070
9886b662
BP
1071 if (ofconn->enable_async_msgs) {
1072 uint32_t *master = ofconn->master_async_config;
1073 uint32_t *slave = ofconn->slave_async_config;
1074
1075 /* "master" and "other" roles get all asynchronous messages by default,
1076 * except that the controller needs to enable nonstandard "packet-in"
1077 * reasons itself. */
1078 master[OAM_PACKET_IN] = (1u << OFPR_NO_MATCH) | (1u << OFPR_ACTION);
1079 master[OAM_PORT_STATUS] = ((1u << OFPPR_ADD)
1080 | (1u << OFPPR_DELETE)
1081 | (1u << OFPPR_MODIFY));
1082 master[OAM_FLOW_REMOVED] = ((1u << OFPRR_IDLE_TIMEOUT)
1083 | (1u << OFPRR_HARD_TIMEOUT)
1084 | (1u << OFPRR_DELETE));
1085
1086 /* "slave" role gets port status updates by default. */
1087 slave[OAM_PACKET_IN] = 0;
1088 slave[OAM_PORT_STATUS] = ((1u << OFPPR_ADD)
1089 | (1u << OFPPR_DELETE)
1090 | (1u << OFPPR_MODIFY));
1091 slave[OAM_FLOW_REMOVED] = 0;
1092 } else {
1093 memset(ofconn->master_async_config, 0,
1094 sizeof ofconn->master_async_config);
1095 memset(ofconn->slave_async_config, 0,
1096 sizeof ofconn->slave_async_config);
1097 }
2b07c8b1
BP
1098
1099 HMAP_FOR_EACH_SAFE (monitor, next_monitor, ofconn_node,
1100 &ofconn->monitors) {
1101 ofmonitor_destroy(monitor);
1102 }
1103 rconn_packet_counter_destroy(ofconn->monitor_counter);
1104 ofconn->monitor_counter = rconn_packet_counter_create();
1105 ofpbuf_list_delete(&ofconn->updates); /* ...but it should be empty. */
7ee20df1
BP
1106}
1107
19a87e36
BP
1108static void
1109ofconn_destroy(struct ofconn *ofconn)
1110{
7ee20df1
BP
1111 ofconn_flush(ofconn);
1112
19a87e36
BP
1113 if (ofconn->type == OFCONN_PRIMARY) {
1114 hmap_remove(&ofconn->connmgr->controllers, &ofconn->hmap_node);
1115 }
1116
1117 list_remove(&ofconn->node);
1118 rconn_destroy(ofconn->rconn);
1119 rconn_packet_counter_destroy(ofconn->packet_in_counter);
1120 rconn_packet_counter_destroy(ofconn->reply_counter);
1121 pktbuf_destroy(ofconn->pktbuf);
2b07c8b1 1122 rconn_packet_counter_destroy(ofconn->monitor_counter);
19a87e36
BP
1123 free(ofconn);
1124}
1125
1126/* Reconfigures 'ofconn' to match 'c'. 'ofconn' and 'c' must have the same
1127 * target. */
1128static void
1129ofconn_reconfigure(struct ofconn *ofconn, const struct ofproto_controller *c)
1130{
1131 int probe_interval;
1132
1133 ofconn->band = c->band;
065825e9 1134 ofconn->enable_async_msgs = c->enable_async_msgs;
19a87e36
BP
1135
1136 rconn_set_max_backoff(ofconn->rconn, c->max_backoff);
1137
1138 probe_interval = c->probe_interval ? MAX(c->probe_interval, 5) : 0;
1139 rconn_set_probe_interval(ofconn->rconn, probe_interval);
1140
1141 ofconn_set_rate_limit(ofconn, c->rate_limit, c->burst_limit);
0442efd9
MM
1142
1143 /* If dscp value changed reconnect. */
1144 if (c->dscp != rconn_get_dscp(ofconn->rconn)) {
1145 rconn_set_dscp(ofconn->rconn, c->dscp);
1146 rconn_reconnect(ofconn->rconn);
1147 }
19a87e36
BP
1148}
1149
7ee20df1
BP
1150/* Returns true if it makes sense for 'ofconn' to receive and process OpenFlow
1151 * messages. */
1152static bool
1153ofconn_may_recv(const struct ofconn *ofconn)
1154{
a6441685 1155 int count = ofconn->reply_counter->n_packets;
7ee20df1
BP
1156 return (!ofconn->blocked || ofconn->retry) && count < OFCONN_REPLY_MAX;
1157}
1158
19a87e36
BP
1159static void
1160ofconn_run(struct ofconn *ofconn,
7ee20df1 1161 bool (*handle_openflow)(struct ofconn *, struct ofpbuf *ofp_msg))
19a87e36
BP
1162{
1163 struct connmgr *mgr = ofconn->connmgr;
19a87e36
BP
1164 size_t i;
1165
1166 for (i = 0; i < N_SCHEDULERS; i++) {
1167 pinsched_run(ofconn->schedulers[i], do_send_packet_in, ofconn);
1168 }
1169
1170 rconn_run(ofconn->rconn);
1171
7ee20df1
BP
1172 if (handle_openflow) {
1173 /* Limit the number of iterations to avoid starving other tasks. */
1174 for (i = 0; i < 50 && ofconn_may_recv(ofconn); i++) {
1175 struct ofpbuf *of_msg;
1176
1177 of_msg = (ofconn->blocked
1178 ? ofconn->blocked
1179 : rconn_recv(ofconn->rconn));
19a87e36
BP
1180 if (!of_msg) {
1181 break;
1182 }
1183 if (mgr->fail_open) {
1184 fail_open_maybe_recover(mgr->fail_open);
1185 }
7ee20df1
BP
1186
1187 if (handle_openflow(ofconn, of_msg)) {
1188 ofpbuf_delete(of_msg);
1189 ofconn->blocked = NULL;
1190 } else {
1191 ofconn->blocked = of_msg;
1192 ofconn->retry = false;
1193 }
19a87e36
BP
1194 }
1195 }
1196
1197 if (!rconn_is_alive(ofconn->rconn)) {
1198 ofconn_destroy(ofconn);
7ee20df1
BP
1199 } else if (!rconn_is_connected(ofconn->rconn)) {
1200 ofconn_flush(ofconn);
19a87e36
BP
1201 }
1202}
1203
1204static void
7ee20df1 1205ofconn_wait(struct ofconn *ofconn, bool handling_openflow)
19a87e36
BP
1206{
1207 int i;
1208
1209 for (i = 0; i < N_SCHEDULERS; i++) {
1210 pinsched_wait(ofconn->schedulers[i]);
1211 }
1212 rconn_run_wait(ofconn->rconn);
7ee20df1 1213 if (handling_openflow && ofconn_may_recv(ofconn)) {
19a87e36 1214 rconn_recv_wait(ofconn->rconn);
19a87e36
BP
1215 }
1216}
1217
80d5aefd
BP
1218/* Returns true if 'ofconn' should receive asynchronous messages of the given
1219 * OAM_* 'type' and 'reason', which should be a OFPR_* value for OAM_PACKET_IN,
1220 * a OFPPR_* value for OAM_PORT_STATUS, or an OFPRR_* value for
1221 * OAM_FLOW_REMOVED. Returns false if the message should not be sent on
1222 * 'ofconn'. */
19a87e36 1223static bool
80d5aefd
BP
1224ofconn_receives_async_msg(const struct ofconn *ofconn,
1225 enum ofconn_async_msg_type type,
1226 unsigned int reason)
19a87e36 1227{
80d5aefd
BP
1228 const uint32_t *async_config;
1229
1230 assert(reason < 32);
1231 assert((unsigned int) type < OAM_N_TYPES);
19a87e36 1232
f0fd1a17
PS
1233 if (!rconn_is_connected(ofconn->rconn)) {
1234 return false;
f0fd1a17 1235 }
f0fd1a17 1236
80d5aefd
BP
1237 /* Keep the following code in sync with the documentation in the
1238 * "Asynchronous Messages" section in DESIGN. */
1239
1240 if (ofconn->type == OFCONN_SERVICE && !ofconn->miss_send_len) {
1241 /* Service connections don't get asynchronous messages unless they have
1242 * explicitly asked for them by setting a nonzero miss send length. */
f0fd1a17 1243 return false;
f0fd1a17 1244 }
80d5aefd
BP
1245
1246 async_config = (ofconn->role == NX_ROLE_SLAVE
1247 ? ofconn->slave_async_config
1248 : ofconn->master_async_config);
1249 if (!(async_config[type] & (1u << reason))) {
1250 return false;
1251 }
1252
1253 return true;
f0fd1a17
PS
1254}
1255
19a87e36
BP
1256/* Returns a human-readable name for an OpenFlow connection between 'mgr' and
1257 * 'target', suitable for use in log messages for identifying the connection.
1258 *
1259 * The name is dynamically allocated. The caller should free it (with free())
1260 * when it is no longer needed. */
1261static char *
1262ofconn_make_name(const struct connmgr *mgr, const char *target)
1263{
1264 return xasprintf("%s<->%s", mgr->name, target);
1265}
1266
1267static void
1268ofconn_set_rate_limit(struct ofconn *ofconn, int rate, int burst)
1269{
1270 int i;
1271
1272 for (i = 0; i < N_SCHEDULERS; i++) {
1273 struct pinsched **s = &ofconn->schedulers[i];
1274
1275 if (rate > 0) {
1276 if (!*s) {
1277 *s = pinsched_create(rate, burst);
1278 } else {
1279 pinsched_set_limits(*s, rate, burst);
1280 }
1281 } else {
1282 pinsched_destroy(*s);
1283 *s = NULL;
1284 }
1285 }
1286}
1287
1288static void
1289ofconn_send(const struct ofconn *ofconn, struct ofpbuf *msg,
1290 struct rconn_packet_counter *counter)
1291{
982697a4 1292 ofpmsg_update_length(msg);
acb9da40 1293 rconn_send(ofconn->rconn, msg, counter);
19a87e36
BP
1294}
1295\f
1296/* Sending asynchronous messages. */
1297
d8653c38 1298static void schedule_packet_in(struct ofconn *, struct ofputil_packet_in);
19a87e36
BP
1299
1300/* Sends an OFPT_PORT_STATUS message with 'opp' and 'reason' to appropriate
9cfcdadf 1301 * controllers managed by 'mgr'. */
19a87e36 1302void
9e1fd49b
BP
1303connmgr_send_port_status(struct connmgr *mgr,
1304 const struct ofputil_phy_port *pp, uint8_t reason)
19a87e36
BP
1305{
1306 /* XXX Should limit the number of queued port status change messages. */
9e1fd49b 1307 struct ofputil_port_status ps;
19a87e36
BP
1308 struct ofconn *ofconn;
1309
9e1fd49b
BP
1310 ps.reason = reason;
1311 ps.desc = *pp;
19a87e36 1312 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
80d5aefd 1313 if (ofconn_receives_async_msg(ofconn, OAM_PORT_STATUS, reason)) {
9e1fd49b 1314 struct ofpbuf *msg;
80d5aefd 1315
9e1fd49b
BP
1316 msg = ofputil_encode_port_status(&ps, ofconn->protocol);
1317 ofconn_send(ofconn, msg, NULL);
19a87e36 1318 }
19a87e36
BP
1319 }
1320}
1321
1322/* Sends an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message based on 'fr' to
1323 * appropriate controllers managed by 'mgr'. */
1324void
1325connmgr_send_flow_removed(struct connmgr *mgr,
1326 const struct ofputil_flow_removed *fr)
1327{
1328 struct ofconn *ofconn;
1329
1330 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
80d5aefd
BP
1331 if (ofconn_receives_async_msg(ofconn, OAM_FLOW_REMOVED, fr->reason)) {
1332 struct ofpbuf *msg;
1333
1334 /* Account flow expirations as replies to OpenFlow requests. That
1335 * works because preventing OpenFlow requests from being processed
1336 * also prevents new flows from being added (and expiring). (It
1337 * also prevents processing OpenFlow requests that would not add
1338 * new flows, so it is imperfect.) */
27527aa0 1339 msg = ofputil_encode_flow_removed(fr, ofconn->protocol);
80d5aefd 1340 ofconn_send_reply(ofconn, msg);
19a87e36 1341 }
19a87e36
BP
1342 }
1343}
1344
78bd1cd0 1345/* Given 'pin', sends an OFPT_PACKET_IN message to each OpenFlow controller as
2ed1202f
BP
1346 * necessary according to their individual configurations.
1347 *
1348 * The caller doesn't need to fill in pin->buffer_id or pin->total_len. */
19a87e36 1349void
78bd1cd0 1350connmgr_send_packet_in(struct connmgr *mgr,
d8653c38 1351 const struct ofputil_packet_in *pin)
19a87e36 1352{
29ebe880 1353 struct ofconn *ofconn;
19a87e36 1354
19a87e36 1355 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
a7349929
BP
1356 if (ofconn_receives_async_msg(ofconn, OAM_PACKET_IN, pin->reason)
1357 && ofconn->controller_id == pin->controller_id) {
d8653c38 1358 schedule_packet_in(ofconn, *pin);
19a87e36
BP
1359 }
1360 }
19a87e36
BP
1361}
1362
1363/* pinsched callback for sending 'ofp_packet_in' on 'ofconn'. */
1364static void
1365do_send_packet_in(struct ofpbuf *ofp_packet_in, void *ofconn_)
1366{
1367 struct ofconn *ofconn = ofconn_;
1368
1369 rconn_send_with_limit(ofconn->rconn, ofp_packet_in,
1370 ofconn->packet_in_counter, 100);
1371}
1372
d8653c38
BP
1373/* Takes 'pin', composes an OpenFlow packet-in message from it, and passes it
1374 * to 'ofconn''s packet scheduler for sending. */
19a87e36 1375static void
d8653c38 1376schedule_packet_in(struct ofconn *ofconn, struct ofputil_packet_in pin)
19a87e36
BP
1377{
1378 struct connmgr *mgr = ofconn->connmgr;
19a87e36 1379
2ed1202f
BP
1380 pin.total_len = pin.packet_len;
1381
19a87e36 1382 /* Get OpenFlow buffer_id. */
78bd1cd0 1383 if (pin.reason == OFPR_ACTION) {
19a87e36
BP
1384 pin.buffer_id = UINT32_MAX;
1385 } else if (mgr->fail_open && fail_open_is_active(mgr->fail_open)) {
1386 pin.buffer_id = pktbuf_get_null();
1387 } else if (!ofconn->pktbuf) {
1388 pin.buffer_id = UINT32_MAX;
1389 } else {
3e3252fa 1390 pin.buffer_id = pktbuf_save(ofconn->pktbuf, pin.packet, pin.packet_len,
d8653c38 1391 pin.fmd.in_port);
19a87e36
BP
1392 }
1393
1394 /* Figure out how much of the packet to send. */
78bd1cd0 1395 if (pin.reason == OFPR_NO_MATCH) {
3e3252fa 1396 pin.send_len = pin.packet_len;
78bd1cd0
BP
1397 } else {
1398 /* Caller should have initialized 'send_len' to 'max_len' specified in
d01c980f 1399 * output action. */
78bd1cd0 1400 }
19a87e36
BP
1401 if (pin.buffer_id != UINT32_MAX) {
1402 pin.send_len = MIN(pin.send_len, ofconn->miss_send_len);
1403 }
19a87e36
BP
1404
1405 /* Make OFPT_PACKET_IN and hand over to packet scheduler. It might
1406 * immediately call into do_send_packet_in() or it might buffer it for a
1407 * while (until a later call to pinsched_run()). */
78bd1cd0 1408 pinsched_send(ofconn->schedulers[pin.reason == OFPR_NO_MATCH ? 0 : 1],
d8653c38 1409 pin.fmd.in_port,
d94240ec
SH
1410 ofputil_encode_packet_in(&pin, ofconn->protocol,
1411 ofconn->packet_in_format),
19a87e36
BP
1412 do_send_packet_in, ofconn);
1413}
1414\f
1415/* Fail-open settings. */
1416
1417/* Returns the failure handling mode (OFPROTO_FAIL_SECURE or
1418 * OFPROTO_FAIL_STANDALONE) for 'mgr'. */
1419enum ofproto_fail_mode
1420connmgr_get_fail_mode(const struct connmgr *mgr)
1421{
1422 return mgr->fail_mode;
1423}
1424
1425/* Sets the failure handling mode for 'mgr' to 'fail_mode' (either
1426 * OFPROTO_FAIL_SECURE or OFPROTO_FAIL_STANDALONE). */
1427void
1428connmgr_set_fail_mode(struct connmgr *mgr, enum ofproto_fail_mode fail_mode)
1429{
08fd19f0
BP
1430 if (mgr->fail_mode != fail_mode) {
1431 mgr->fail_mode = fail_mode;
1432 update_fail_open(mgr);
1433 if (!connmgr_has_controllers(mgr)) {
1434 ofproto_flush_flows(mgr->ofproto);
1435 }
1436 }
19a87e36
BP
1437}
1438\f
1439/* Fail-open implementation. */
1440
1441/* Returns the longest probe interval among the primary controllers configured
1442 * on 'mgr'. Returns 0 if there are no primary controllers. */
1443int
1444connmgr_get_max_probe_interval(const struct connmgr *mgr)
1445{
1446 const struct ofconn *ofconn;
1447 int max_probe_interval;
1448
1449 max_probe_interval = 0;
1450 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1451 int probe_interval = rconn_get_probe_interval(ofconn->rconn);
1452 max_probe_interval = MAX(max_probe_interval, probe_interval);
1453 }
1454 return max_probe_interval;
1455}
1456
1457/* Returns the number of seconds for which all of 'mgr's primary controllers
1458 * have been disconnected. Returns 0 if 'mgr' has no primary controllers. */
1459int
1460connmgr_failure_duration(const struct connmgr *mgr)
1461{
1462 const struct ofconn *ofconn;
1463 int min_failure_duration;
1464
1465 if (!connmgr_has_controllers(mgr)) {
1466 return 0;
1467 }
1468
1469 min_failure_duration = INT_MAX;
1470 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1471 int failure_duration = rconn_failure_duration(ofconn->rconn);
1472 min_failure_duration = MIN(min_failure_duration, failure_duration);
1473 }
1474 return min_failure_duration;
1475}
1476
1477/* Returns true if at least one primary controller is connected (regardless of
1478 * whether those controllers are believed to have authenticated and accepted
1479 * this switch), false if none of them are connected. */
1480bool
1481connmgr_is_any_controller_connected(const struct connmgr *mgr)
1482{
1483 const struct ofconn *ofconn;
1484
1485 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1486 if (rconn_is_connected(ofconn->rconn)) {
1487 return true;
1488 }
1489 }
1490 return false;
1491}
1492
1493/* Returns true if at least one primary controller is believed to have
1494 * authenticated and accepted this switch, false otherwise. */
1495bool
1496connmgr_is_any_controller_admitted(const struct connmgr *mgr)
1497{
1498 const struct ofconn *ofconn;
1499
1500 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1501 if (rconn_is_admitted(ofconn->rconn)) {
1502 return true;
1503 }
1504 }
1505 return false;
1506}
19a87e36
BP
1507\f
1508/* In-band configuration. */
1509
1510static bool any_extras_changed(const struct connmgr *,
1511 const struct sockaddr_in *extras, size_t n);
1512
1513/* Sets the 'n' TCP port addresses in 'extras' as ones to which 'mgr''s
1514 * in-band control should guarantee access, in the same way that in-band
1515 * control guarantees access to OpenFlow controllers. */
1516void
1517connmgr_set_extra_in_band_remotes(struct connmgr *mgr,
1518 const struct sockaddr_in *extras, size_t n)
1519{
1520 if (!any_extras_changed(mgr, extras, n)) {
1521 return;
1522 }
1523
1524 free(mgr->extra_in_band_remotes);
1525 mgr->n_extra_remotes = n;
1526 mgr->extra_in_band_remotes = xmemdup(extras, n * sizeof *extras);
1527
1528 update_in_band_remotes(mgr);
1529}
1530
1531/* Sets the OpenFlow queue used by flows set up by in-band control on
1532 * 'mgr' to 'queue_id'. If 'queue_id' is negative, then in-band control
1533 * flows will use the default queue. */
1534void
1535connmgr_set_in_band_queue(struct connmgr *mgr, int queue_id)
1536{
1537 if (queue_id != mgr->in_band_queue) {
1538 mgr->in_band_queue = queue_id;
1539 update_in_band_remotes(mgr);
1540 }
1541}
1542
1543static bool
1544any_extras_changed(const struct connmgr *mgr,
1545 const struct sockaddr_in *extras, size_t n)
1546{
1547 size_t i;
1548
1549 if (n != mgr->n_extra_remotes) {
1550 return true;
1551 }
1552
1553 for (i = 0; i < n; i++) {
1554 const struct sockaddr_in *old = &mgr->extra_in_band_remotes[i];
1555 const struct sockaddr_in *new = &extras[i];
1556
1557 if (old->sin_addr.s_addr != new->sin_addr.s_addr ||
1558 old->sin_port != new->sin_port) {
1559 return true;
1560 }
1561 }
1562
1563 return false;
1564}
1565\f
1566/* In-band implementation. */
1567
1568bool
1569connmgr_msg_in_hook(struct connmgr *mgr, const struct flow *flow,
1570 const struct ofpbuf *packet)
1571{
1572 return mgr->in_band && in_band_msg_in_hook(mgr->in_band, flow, packet);
1573}
1574
1575bool
1576connmgr_may_set_up_flow(struct connmgr *mgr, const struct flow *flow,
1577 const struct nlattr *odp_actions,
1578 size_t actions_len)
1579{
1580 return !mgr->in_band || in_band_rule_check(flow, odp_actions, actions_len);
1581}
1582\f
1583/* Fail-open and in-band implementation. */
1584
1585/* Called by 'ofproto' after all flows have been flushed, to allow fail-open
7ee20df1
BP
1586 * and standalone mode to re-create their flows.
1587 *
1588 * In-band control has more sophisticated code that manages flows itself. */
19a87e36
BP
1589void
1590connmgr_flushed(struct connmgr *mgr)
1591{
19a87e36
BP
1592 if (mgr->fail_open) {
1593 fail_open_flushed(mgr->fail_open);
1594 }
08fd19f0
BP
1595
1596 /* If there are no controllers and we're in standalone mode, set up a flow
1597 * that matches every packet and directs them to OFPP_NORMAL (which goes to
1598 * us). Otherwise, the switch is in secure mode and we won't pass any
1599 * traffic until a controller has been defined and it tells us to do so. */
1600 if (!connmgr_has_controllers(mgr)
1601 && mgr->fail_mode == OFPROTO_FAIL_STANDALONE) {
f25d0cf3 1602 struct ofpbuf ofpacts;
81a76618 1603 struct match match;
08fd19f0 1604
f25d0cf3
BP
1605 ofpbuf_init(&ofpacts, OFPACT_OUTPUT_SIZE);
1606 ofpact_put_OUTPUT(&ofpacts)->port = OFPP_NORMAL;
1607 ofpact_pad(&ofpacts);
1608
81a76618
BP
1609 match_init_catchall(&match);
1610 ofproto_add_flow(mgr->ofproto, &match, 0, ofpacts.data, ofpacts.size);
f25d0cf3
BP
1611
1612 ofpbuf_uninit(&ofpacts);
08fd19f0 1613 }
19a87e36
BP
1614}
1615\f
1616/* Creates a new ofservice for 'target' in 'mgr'. Returns 0 if successful,
1617 * otherwise a positive errno value.
1618 *
1619 * ofservice_reconfigure() must be called to fully configure the new
1620 * ofservice. */
1621static int
f125905c 1622ofservice_create(struct connmgr *mgr, const char *target, uint8_t dscp)
19a87e36
BP
1623{
1624 struct ofservice *ofservice;
1625 struct pvconn *pvconn;
1626 int error;
1627
f125905c 1628 error = pvconn_open(target, &pvconn, dscp);
19a87e36
BP
1629 if (error) {
1630 return error;
1631 }
1632
1633 ofservice = xzalloc(sizeof *ofservice);
1634 hmap_insert(&mgr->services, &ofservice->node, hash_string(target, 0));
1635 ofservice->pvconn = pvconn;
1636
1637 return 0;
1638}
1639
1640static void
1641ofservice_destroy(struct connmgr *mgr, struct ofservice *ofservice)
1642{
1643 hmap_remove(&mgr->services, &ofservice->node);
1644 pvconn_close(ofservice->pvconn);
1645 free(ofservice);
1646}
1647
1648static void
1649ofservice_reconfigure(struct ofservice *ofservice,
1650 const struct ofproto_controller *c)
1651{
1652 ofservice->probe_interval = c->probe_interval;
1653 ofservice->rate_limit = c->rate_limit;
1654 ofservice->burst_limit = c->burst_limit;
9886b662 1655 ofservice->enable_async_msgs = c->enable_async_msgs;
f125905c 1656 ofservice->dscp = c->dscp;
19a87e36
BP
1657}
1658
1659/* Finds and returns the ofservice within 'mgr' that has the given
1660 * 'target', or a null pointer if none exists. */
1661static struct ofservice *
1662ofservice_lookup(struct connmgr *mgr, const char *target)
1663{
1664 struct ofservice *ofservice;
1665
1666 HMAP_FOR_EACH_WITH_HASH (ofservice, node, hash_string(target, 0),
1667 &mgr->services) {
1668 if (!strcmp(pvconn_get_name(ofservice->pvconn), target)) {
1669 return ofservice;
1670 }
1671 }
1672 return NULL;
1673}
2b07c8b1
BP
1674\f
1675/* Flow monitors (NXST_FLOW_MONITOR). */
1676
1677/* A counter incremented when something significant happens to an OpenFlow
1678 * rule.
1679 *
1680 * - When a rule is added, its 'add_seqno' and 'modify_seqno' are set to
1681 * the current value (which is then incremented).
1682 *
1683 * - When a rule is modified, its 'modify_seqno' is set to the current
1684 * value (which is then incremented).
1685 *
1686 * Thus, by comparing an old value of monitor_seqno against a rule's
1687 * 'add_seqno', one can tell whether the rule was added before or after the old
1688 * value was read, and similarly for 'modify_seqno'.
1689 *
1690 * 32 bits should normally be sufficient (and would be nice, to save space in
1691 * each rule) but then we'd have to have some special cases for wraparound.
1692 *
1693 * We initialize monitor_seqno to 1 to allow 0 to be used as an invalid
1694 * value. */
1695static uint64_t monitor_seqno = 1;
1696
1697COVERAGE_DEFINE(ofmonitor_pause);
1698COVERAGE_DEFINE(ofmonitor_resume);
1699
1700enum ofperr
1701ofmonitor_create(const struct ofputil_flow_monitor_request *request,
1702 struct ofconn *ofconn, struct ofmonitor **monitorp)
1703{
1704 struct ofmonitor *m;
1705
1706 *monitorp = NULL;
1707
1708 m = ofmonitor_lookup(ofconn, request->id);
1709 if (m) {
1710 return OFPERR_NXBRC_FM_DUPLICATE_ID;
1711 }
1712
1713 m = xmalloc(sizeof *m);
1714 m->ofconn = ofconn;
1715 hmap_insert(&ofconn->monitors, &m->ofconn_node, hash_int(request->id, 0));
1716 m->id = request->id;
1717 m->flags = request->flags;
1718 m->out_port = request->out_port;
1719 m->table_id = request->table_id;
1720 m->match = request->match;
1721
1722 *monitorp = m;
1723 return 0;
1724}
1725
1726struct ofmonitor *
1727ofmonitor_lookup(struct ofconn *ofconn, uint32_t id)
1728{
1729 struct ofmonitor *m;
1730
1731 HMAP_FOR_EACH_IN_BUCKET (m, ofconn_node, hash_int(id, 0),
1732 &ofconn->monitors) {
1733 if (m->id == id) {
1734 return m;
1735 }
1736 }
1737 return NULL;
1738}
1739
1740void
1741ofmonitor_destroy(struct ofmonitor *m)
1742{
1743 if (m) {
1744 hmap_remove(&m->ofconn->monitors, &m->ofconn_node);
1745 free(m);
1746 }
1747}
1748
1749void
1750ofmonitor_report(struct connmgr *mgr, struct rule *rule,
1751 enum nx_flow_update_event event,
1752 enum ofp_flow_removed_reason reason,
1753 const struct ofconn *abbrev_ofconn, ovs_be32 abbrev_xid)
1754{
1755 enum nx_flow_monitor_flags update;
1756 struct ofconn *ofconn;
1757
1758 switch (event) {
1759 case NXFME_ADDED:
1760 update = NXFMF_ADD;
1761 rule->add_seqno = rule->modify_seqno = monitor_seqno++;
1762 break;
1763
1764 case NXFME_DELETED:
1765 update = NXFMF_DELETE;
1766 break;
1767
1768 case NXFME_MODIFIED:
1769 update = NXFMF_MODIFY;
1770 rule->modify_seqno = monitor_seqno++;
1771 break;
1772
1773 default:
1774 case NXFME_ABBREV:
1775 NOT_REACHED();
1776 }
1777
1778 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1779 enum nx_flow_monitor_flags flags = 0;
1780 struct ofmonitor *m;
1781
1782 if (ofconn->monitor_paused) {
1783 /* Only send NXFME_DELETED notifications for flows that were added
1784 * before we paused. */
1785 if (event != NXFME_DELETED
1786 || rule->add_seqno > ofconn->monitor_paused) {
1787 continue;
1788 }
1789 }
1790
1791 HMAP_FOR_EACH (m, ofconn_node, &ofconn->monitors) {
1792 if (m->flags & update
1793 && (m->table_id == 0xff || m->table_id == rule->table_id)
1794 && ofoperation_has_out_port(rule->pending, m->out_port)
1795 && cls_rule_is_loose_match(&rule->cr, &m->match)) {
1796 flags |= m->flags;
1797 }
1798 }
1799
1800 if (flags) {
1801 if (list_is_empty(&ofconn->updates)) {
1802 ofputil_start_flow_update(&ofconn->updates);
1803 ofconn->sent_abbrev_update = false;
1804 }
1805
1806 if (ofconn != abbrev_ofconn || ofconn->monitor_paused) {
1807 struct ofputil_flow_update fu;
1808
1809 fu.event = event;
1810 fu.reason = event == NXFME_DELETED ? reason : 0;
1811 fu.idle_timeout = rule->idle_timeout;
1812 fu.hard_timeout = rule->hard_timeout;
1813 fu.table_id = rule->table_id;
1814 fu.cookie = rule->flow_cookie;
81a76618 1815 fu.match = &rule->cr.match;
2b07c8b1
BP
1816 if (flags & NXFMF_ACTIONS) {
1817 fu.ofpacts = rule->ofpacts;
1818 fu.ofpacts_len = rule->ofpacts_len;
1819 } else {
1820 fu.ofpacts = NULL;
1821 fu.ofpacts_len = 0;
1822 }
1823 ofputil_append_flow_update(&fu, &ofconn->updates);
1824 } else if (!ofconn->sent_abbrev_update) {
1825 struct ofputil_flow_update fu;
1826
1827 fu.event = NXFME_ABBREV;
1828 fu.xid = abbrev_xid;
1829 ofputil_append_flow_update(&fu, &ofconn->updates);
1830
1831 ofconn->sent_abbrev_update = true;
1832 }
1833 }
1834 }
1835}
1836
1837void
1838ofmonitor_flush(struct connmgr *mgr)
1839{
1840 struct ofconn *ofconn;
1841
1842 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1843 struct ofpbuf *msg, *next;
1844
1845 LIST_FOR_EACH_SAFE (msg, next, list_node, &ofconn->updates) {
1846 list_remove(&msg->list_node);
1847 ofconn_send(ofconn, msg, ofconn->monitor_counter);
1848 if (!ofconn->monitor_paused
1849 && ofconn->monitor_counter->n_bytes > 128 * 1024) {
1850 struct ofpbuf *pause;
1851
1852 COVERAGE_INC(ofmonitor_pause);
1853 ofconn->monitor_paused = monitor_seqno++;
982697a4
BP
1854 pause = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_MONITOR_PAUSED,
1855 OFP10_VERSION, htonl(0), 0);
2b07c8b1
BP
1856 ofconn_send(ofconn, pause, ofconn->monitor_counter);
1857 }
1858 }
1859 }
1860}
1861
1862static void
1863ofmonitor_resume(struct ofconn *ofconn)
1864{
982697a4 1865 struct ofpbuf *resumed;
2b07c8b1
BP
1866 struct ofmonitor *m;
1867 struct list rules;
1868 struct list msgs;
1869
1870 list_init(&rules);
1871 HMAP_FOR_EACH (m, ofconn_node, &ofconn->monitors) {
1872 ofmonitor_collect_resume_rules(m, ofconn->monitor_paused, &rules);
1873 }
1874
1875 list_init(&msgs);
1876 ofmonitor_compose_refresh_updates(&rules, &msgs);
1877
982697a4
BP
1878 resumed = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_MONITOR_RESUMED, OFP10_VERSION,
1879 htonl(0), 0);
1880 list_push_back(&msgs, &resumed->list_node);
2b07c8b1
BP
1881 ofconn_send_replies(ofconn, &msgs);
1882
1883 ofconn->monitor_paused = 0;
1884}
1885
1886static void
1887ofmonitor_run(struct connmgr *mgr)
1888{
1889 struct ofconn *ofconn;
1890
1891 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1892 if (ofconn->monitor_paused && !ofconn->monitor_counter->n_packets) {
1893 COVERAGE_INC(ofmonitor_resume);
1894 ofmonitor_resume(ofconn);
1895 }
1896 }
1897}
1898
1899static void
1900ofmonitor_wait(struct connmgr *mgr)
1901{
1902 struct ofconn *ofconn;
1903
1904 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1905 if (ofconn->monitor_paused && !ofconn->monitor_counter->n_packets) {
1906 poll_immediate_wake();
1907 }
1908 }
1909}