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