]> git.proxmox.com Git - ovs.git/blame - ofproto/connmgr.c
New function ovs_strerror() as a thread-safe replacement for strerror().
[ovs.git] / ofproto / connmgr.c
CommitLineData
19a87e36 1/*
82c8c53c 2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 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. */
f4f1ea7e 63 enum ofp12_controller_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 *,
e03248b7 111 const 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 */
90ef0206
SH
136 uint32_t allowed_versions; /* OpenFlow protocol versions that may
137 * be negotiated for a session. */
19a87e36
BP
138};
139
140static void ofservice_reconfigure(struct ofservice *,
141 const struct ofproto_controller *);
00401ef0
SH
142static int ofservice_create(struct connmgr *mgr, const char *target,
143 uint32_t allowed_versions, uint8_t dscp);
19a87e36
BP
144static void ofservice_destroy(struct connmgr *, struct ofservice *);
145static struct ofservice *ofservice_lookup(struct connmgr *,
146 const char *target);
147
148/* Connection manager for an OpenFlow switch. */
149struct 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. */
6ea4776b
JR
157 uint64_t master_election_id; /* monotonically increasing sequence number
158 * for master election */
159 bool master_election_id_defined;
19a87e36
BP
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;
19a87e36
BP
172 struct sockaddr_in *extra_in_band_remotes;
173 size_t n_extra_remotes;
174 int in_band_queue;
175};
176
177static void update_in_band_remotes(struct connmgr *);
178static void add_snooper(struct connmgr *, struct vconn *);
2b07c8b1
BP
179static void ofmonitor_run(struct connmgr *);
180static void ofmonitor_wait(struct connmgr *);
19a87e36
BP
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'. */
186struct connmgr *
187connmgr_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);
6ea4776b
JR
199 mgr->master_election_id = 0;
200 mgr->master_election_id_defined = false;
19a87e36
BP
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;
19a87e36
BP
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. */
218void
219connmgr_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
7ee20df1
BP
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. */
19a87e36
BP
270void
271connmgr_run(struct connmgr *mgr,
e03248b7
JR
272 bool (*handle_openflow)(struct ofconn *,
273 const struct ofpbuf *ofp_msg))
19a87e36
BP
274{
275 struct ofconn *ofconn, *next_ofconn;
276 struct ofservice *ofservice;
277 size_t i;
278
7ee20df1 279 if (handle_openflow && mgr->in_band) {
6da1e809
BP
280 if (!in_band_run(mgr->in_band)) {
281 in_band_destroy(mgr->in_band);
282 mgr->in_band = NULL;
283 }
19a87e36
BP
284 }
285
286 LIST_FOR_EACH_SAFE (ofconn, next_ofconn, node, &mgr->all_conns) {
287 ofconn_run(ofconn, handle_openflow);
288 }
2b07c8b1 289 ofmonitor_run(mgr);
19a87e36
BP
290
291 /* Fail-open maintenance. Do this after processing the ofconns since
292 * fail-open checks the status of the controller rconn. */
7ee20df1 293 if (handle_openflow && mgr->fail_open) {
19a87e36
BP
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
7a25bd99 301 retval = pvconn_accept(ofservice->pvconn, &vconn);
19a87e36
BP
302 if (!retval) {
303 struct rconn *rconn;
304 char *name;
305
f125905c 306 /* Passing default value for creation of the rconn */
1d9ffc17
SH
307 rconn = rconn_create(ofservice->probe_interval, 0, ofservice->dscp,
308 vconn_get_allowed_versions(vconn));
19a87e36
BP
309 name = ofconn_make_name(mgr, vconn_get_name(vconn));
310 rconn_connect_unreliably(rconn, vconn, name);
311 free(name);
312
9886b662
BP
313 ofconn = ofconn_create(mgr, rconn, OFCONN_SERVICE,
314 ofservice->enable_async_msgs);
19a87e36
BP
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)", strerror(retval));
319 }
320 }
321
322 for (i = 0; i < mgr->n_snoops; i++) {
323 struct vconn *vconn;
324 int retval;
325
7a25bd99 326 retval = pvconn_accept(mgr->snoops[i], &vconn);
19a87e36
BP
327 if (!retval) {
328 add_snooper(mgr, vconn);
329 } else if (retval != EAGAIN) {
330 VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval));
331 }
332 }
333}
334
7ee20df1
BP
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. */
19a87e36 340void
7ee20df1 341connmgr_wait(struct connmgr *mgr, bool handling_openflow)
19a87e36
BP
342{
343 struct ofservice *ofservice;
344 struct ofconn *ofconn;
345 size_t i;
346
347 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
7ee20df1 348 ofconn_wait(ofconn, handling_openflow);
19a87e36 349 }
2b07c8b1 350 ofmonitor_wait(mgr);
7ee20df1 351 if (handling_openflow && mgr->in_band) {
19a87e36
BP
352 in_band_wait(mgr->in_band);
353 }
7ee20df1 354 if (handling_openflow && mgr->fail_open) {
19a87e36
BP
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
0d085684
BP
365/* Adds some memory usage statistics for 'mgr' into 'usage', for use with
366 * memory_report(). */
367void
368connmgr_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
19a87e36
BP
389/* Returns the ofproto that owns 'ofconn''s connmgr. */
390struct ofproto *
391ofconn_get_ofproto(const struct ofconn *ofconn)
392{
393 return ofconn->connmgr->ofproto;
394}
7ee20df1
BP
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. */
399void
400connmgr_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}
19a87e36
BP
408\f
409/* OpenFlow configuration. */
410
1d9ffc17
SH
411static void add_controller(struct connmgr *, const char *target, uint8_t dscp,
412 uint32_t allowed_versions);
19a87e36
BP
413static struct ofconn *find_controller_by_target(struct connmgr *,
414 const char *target);
415static void update_fail_open(struct connmgr *);
416static int set_pvconns(struct pvconn ***pvconnsp, size_t *n_pvconnsp,
81e2083f 417 const struct sset *);
19a87e36
BP
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. */
423bool
424connmgr_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. */
435void
436connmgr_get_controller_info(struct connmgr *mgr, struct shash *info)
437{
438 const struct ofconn *ofconn;
439
19a87e36
BP
440 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
441 const struct rconn *rconn = ofconn->rconn;
5d279086 442 const char *target = rconn_get_target(rconn);
19a87e36 443
5d279086
AE
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);
19a87e36 450
5d279086 451 shash_add(info, target, cinfo);
19a87e36 452
5d279086
AE
453 cinfo->is_connected = rconn_is_connected(rconn);
454 cinfo->role = ofconn->role;
19a87e36 455
5d279086 456 cinfo->pairs.n = 0;
19a87e36 457
5d279086
AE
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 }
19a87e36 463
5d279086 464 cinfo->pairs.keys[cinfo->pairs.n] = "state";
19a87e36 465 cinfo->pairs.values[cinfo->pairs.n++]
5d279086
AE
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 }
19a87e36
BP
479 }
480 }
481}
482
72ba2ed3
AE
483void
484connmgr_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) {
ebc56baa 491 free(CONST_CAST(char *, cinfo->pairs.values[--cinfo->pairs.n]));
72ba2ed3
AE
492 }
493 free(cinfo);
494 }
495 shash_destroy(info);
496}
497
19a87e36
BP
498/* Changes 'mgr''s set of controllers to the 'n_controllers' controllers in
499 * 'controllers'. */
500void
501connmgr_set_controllers(struct connmgr *mgr,
502 const struct ofproto_controller *controllers,
1d9ffc17 503 size_t n_controllers, uint32_t allowed_versions)
19a87e36 504{
08fd19f0 505 bool had_controllers = connmgr_has_controllers(mgr);
19a87e36
BP
506 struct shash new_controllers;
507 struct ofconn *ofconn, *next_ofconn;
508 struct ofservice *ofservice, *next_ofservice;
19a87e36
BP
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)) {
90ef0206
SH
518 bool add = false;
519 ofconn = find_controller_by_target(mgr, c->target);
520 if (!ofconn) {
1ea9e609
BP
521 VLOG_INFO("%s: added primary controller \"%s\"",
522 mgr->name, c->target);
90ef0206
SH
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) {
1d9ffc17 532 add_controller(mgr, c->target, c->dscp, allowed_versions);
19a87e36
BP
533 }
534 } else if (!pvconn_verify_name(c->target)) {
90ef0206
SH
535 bool add = false;
536 ofservice = ofservice_lookup(mgr, c->target);
537 if (!ofservice) {
1ea9e609
BP
538 VLOG_INFO("%s: added service controller \"%s\"",
539 mgr->name, c->target);
90ef0206
SH
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) {
1d9ffc17 548 ofservice_create(mgr, c->target, allowed_versions, c->dscp);
19a87e36
BP
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. */
19a87e36 561 HMAP_FOR_EACH_SAFE (ofconn, next_ofconn, hmap_node, &mgr->controllers) {
1ea9e609 562 const char *target = ofconn_get_target(ofconn);
19a87e36
BP
563 struct ofproto_controller *c;
564
1ea9e609 565 c = shash_find_data(&new_controllers, target);
19a87e36 566 if (!c) {
1ea9e609
BP
567 VLOG_INFO("%s: removed primary controller \"%s\"",
568 mgr->name, target);
19a87e36
BP
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) {
1ea9e609 578 const char *target = pvconn_get_name(ofservice->pvconn);
19a87e36
BP
579 struct ofproto_controller *c;
580
1ea9e609 581 c = shash_find_data(&new_controllers, target);
19a87e36 582 if (!c) {
1ea9e609
BP
583 VLOG_INFO("%s: removed service controller \"%s\"",
584 mgr->name, target);
19a87e36
BP
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);
08fd19f0
BP
595 if (had_controllers != connmgr_has_controllers(mgr)) {
596 ofproto_flush_flows(mgr->ofproto);
597 }
19a87e36
BP
598}
599
600/* Drops the connections between 'mgr' and all of its primary and secondary
601 * controllers, forcing them to reconnect. */
602void
603connmgr_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. */
616int
81e2083f 617connmgr_set_snoops(struct connmgr *mgr, const struct sset *snoops)
19a87e36
BP
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'. */
623void
81e2083f 624connmgr_get_snoops(const struct connmgr *mgr, struct sset *snoops)
19a87e36
BP
625{
626 size_t i;
627
628 for (i = 0; i < mgr->n_snoops; i++) {
81e2083f 629 sset_add(snoops, pvconn_get_name(mgr->snoops[i]));
19a87e36
BP
630 }
631}
632
81e2083f
BP
633/* Returns true if 'mgr' has at least one snoop, false if it has none. */
634bool
635connmgr_has_snoops(const struct connmgr *mgr)
636{
637 return mgr->n_snoops > 0;
638}
639
19a87e36
BP
640/* Creates a new controller for 'target' in 'mgr'. update_controller() needs
641 * to be called later to finish the new ofconn's configuration. */
642static void
1d9ffc17
SH
643add_controller(struct connmgr *mgr, const char *target, uint8_t dscp,
644 uint32_t allowed_versions)
19a87e36
BP
645{
646 char *name = ofconn_make_name(mgr, target);
647 struct ofconn *ofconn;
648
1d9ffc17 649 ofconn = ofconn_create(mgr, rconn_create(5, 8, dscp, allowed_versions),
6042457b 650 OFCONN_PRIMARY, true);
19a87e36 651 ofconn->pktbuf = pktbuf_create();
19a87e36
BP
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
658static struct ofconn *
659find_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
672static void
673update_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];
ac4c900d 688 const char *target = rconn_get_target(ofconn->rconn);
19a87e36
BP
689
690 if (ofconn->band == OFPROTO_OUT_OF_BAND) {
691 continue;
692 }
693
ac4c900d
AA
694 if (stream_parse_target_with_default_ports(target,
695 OFP_TCP_PORT,
696 OFP_SSL_PORT,
697 sin)) {
19a87e36
BP
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 }
19a87e36 710 in_band_set_queue(mgr->in_band, mgr->in_band_queue);
19a87e36 711 } else {
6da1e809
BP
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);
19a87e36
BP
717 }
718
719 /* Clean up. */
720 free(addrs);
721}
722
723static void
724update_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
737static int
738set_pvconns(struct pvconn ***pvconnsp, size_t *n_pvconnsp,
81e2083f 739 const struct sset *sset)
19a87e36
BP
740{
741 struct pvconn **pvconns = *pvconnsp;
742 size_t n_pvconns = *n_pvconnsp;
81e2083f 743 const char *name;
19a87e36
BP
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
81e2083f 752 pvconns = xmalloc(sset_count(sset) * sizeof *pvconns);
19a87e36 753 n_pvconns = 0;
81e2083f 754 SSET_FOR_EACH (name, sset) {
19a87e36
BP
755 struct pvconn *pvconn;
756 int error;
82c8c53c 757 error = pvconn_open(name, 0, 0, &pvconn);
19a87e36
BP
758 if (!error) {
759 pvconns[n_pvconns++] = pvconn;
760 } else {
761 VLOG_ERR("failed to listen on %s: %s", name, 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. */
777static int
778snoop_preference(const struct ofconn *ofconn)
779{
780 switch (ofconn->role) {
f4f1ea7e 781 case OFPCR12_ROLE_MASTER:
19a87e36 782 return 3;
f4f1ea7e 783 case OFPCR12_ROLE_EQUAL:
19a87e36 784 return 2;
f4f1ea7e 785 case OFPCR12_ROLE_SLAVE:
19a87e36 786 return 1;
f4f1ea7e 787 case OFPCR12_ROLE_NOCHANGE:
19a87e36
BP
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. */
796static void
797add_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. */
821enum ofconn_type
822ofconn_get_type(const struct ofconn *ofconn)
823{
824 return ofconn->type;
825}
826
147cc9d3
BP
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. */
829bool
830ofconn_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
6ea4776b
JR
838/* Sets the master election id.
839 *
840 * Returns true if successful, false if the id is stale
841 */
842bool
843ofconn_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
19a87e36
BP
858/* Returns the role configured for 'ofconn'.
859 *
f4f1ea7e
BP
860 * The default role, if no other role has been set, is OFPCR12_ROLE_EQUAL. */
861enum ofp12_controller_role
19a87e36
BP
862ofconn_get_role(const struct ofconn *ofconn)
863{
864 return ofconn->role;
865}
866
f4f1ea7e
BP
867/* Changes 'ofconn''s role to 'role'. If 'role' is OFPCR12_ROLE_MASTER then
868 * any existing master is demoted to a slave. */
19a87e36 869void
f4f1ea7e 870ofconn_set_role(struct ofconn *ofconn, enum ofp12_controller_role role)
19a87e36 871{
f4f1ea7e 872 if (role == OFPCR12_ROLE_MASTER) {
19a87e36
BP
873 struct ofconn *other;
874
875 HMAP_FOR_EACH (other, hmap_node, &ofconn->connmgr->controllers) {
f4f1ea7e
BP
876 if (other->role == OFPCR12_ROLE_MASTER) {
877 other->role = OFPCR12_ROLE_SLAVE;
19a87e36
BP
878 }
879 }
880 }
881 ofconn->role = role;
882}
883
f0fd1a17 884void
80d5aefd 885ofconn_set_invalid_ttl_to_controller(struct ofconn *ofconn, bool enable)
f0fd1a17 886{
80d5aefd
BP
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 }
f0fd1a17
PS
893}
894
895bool
896ofconn_get_invalid_ttl_to_controller(struct ofconn *ofconn)
897{
80d5aefd
BP
898 uint32_t bit = 1u << OFPR_INVALID_TTL;
899 return (ofconn->master_async_config[OAM_PACKET_IN] & bit) != 0;
f0fd1a17
PS
900}
901
27527aa0 902/* Returns the currently configured protocol for 'ofconn', one of OFPUTIL_P_*.
19a87e36 903 *
eb12aa89
BP
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. */
27527aa0 910enum ofputil_protocol
e3d560db 911ofconn_get_protocol(const struct ofconn *ofconn)
19a87e36 912{
eb12aa89
BP
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
27527aa0 922 return ofconn->protocol;
19a87e36
BP
923}
924
27527aa0
BP
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.) */
19a87e36 929void
27527aa0 930ofconn_set_protocol(struct ofconn *ofconn, enum ofputil_protocol protocol)
19a87e36 931{
27527aa0 932 ofconn->protocol = protocol;
19a87e36
BP
933}
934
54834960
EJ
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. */
939enum nx_packet_in_format
940ofconn_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_*). */
947void
948ofconn_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
a7349929
BP
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. */
958void
959ofconn_set_controller_id(struct ofconn *ofconn, uint16_t controller_id)
960{
961 ofconn->controller_id = controller_id;
962}
963
19a87e36
BP
964/* Returns the default miss send length for 'ofconn'. */
965int
966ofconn_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'. */
972void
973ofconn_set_miss_send_len(struct ofconn *ofconn, int miss_send_len)
974{
975 ofconn->miss_send_len = miss_send_len;
976}
977
80d5aefd
BP
978void
979ofconn_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
19a87e36
BP
988/* Sends 'msg' on 'ofconn', accounting it as a reply. (If there is a
989 * sufficient number of OpenFlow replies in-flight on a single ofconn, then the
990 * connmgr will stop accepting new OpenFlow requests on that ofconn until the
991 * controller has accepted some of the replies.) */
992void
993ofconn_send_reply(const struct ofconn *ofconn, struct ofpbuf *msg)
994{
995 ofconn_send(ofconn, msg, ofconn->reply_counter);
996}
997
63f2140a
BP
998/* Sends each of the messages in list 'replies' on 'ofconn' in order,
999 * accounting them as replies. */
1000void
1001ofconn_send_replies(const struct ofconn *ofconn, struct list *replies)
1002{
1003 struct ofpbuf *reply, *next;
1004
1005 LIST_FOR_EACH_SAFE (reply, next, list_node, replies) {
1006 list_remove(&reply->list_node);
1007 ofconn_send_reply(ofconn, reply);
1008 }
1009}
1010
90bf1e07 1011/* Sends 'error' on 'ofconn', as a reply to 'request'. Only at most the
1be5ff75
BP
1012 * first 64 bytes of 'request' are used. */
1013void
1014ofconn_send_error(const struct ofconn *ofconn,
90bf1e07 1015 const struct ofp_header *request, enum ofperr error)
1be5ff75 1016{
07c8ec21 1017 static struct vlog_rate_limit err_rl = VLOG_RATE_LIMIT_INIT(10, 10);
90bf1e07 1018 struct ofpbuf *reply;
76589937 1019
90bf1e07 1020 reply = ofperr_encode_reply(error, request);
07c8ec21
BP
1021 if (!VLOG_DROP_INFO(&err_rl)) {
1022 const char *type_name;
1023 size_t request_len;
1024 enum ofpraw raw;
1025
1026 request_len = ntohs(request->length);
1027 type_name = (!ofpraw_decode_partial(&raw, request,
1028 MIN(64, request_len))
1029 ? ofpraw_get_name(raw)
1030 : "invalid");
1031
1032 VLOG_INFO("%s: sending %s error reply to %s message",
1033 rconn_get_name(ofconn->rconn), ofperr_to_string(error),
1034 type_name);
1035 }
1036 ofconn_send_reply(ofconn, reply);
1be5ff75
BP
1037}
1038
19a87e36 1039/* Same as pktbuf_retrieve(), using the pktbuf owned by 'ofconn'. */
90bf1e07 1040enum ofperr
19a87e36 1041ofconn_pktbuf_retrieve(struct ofconn *ofconn, uint32_t id,
4e022ec0 1042 struct ofpbuf **bufferp, ofp_port_t *in_port)
19a87e36
BP
1043{
1044 return pktbuf_retrieve(ofconn->pktbuf, id, bufferp, in_port);
1045}
7ee20df1
BP
1046
1047/* Returns true if 'ofconn' has any pending opgroups. */
1048bool
1049ofconn_has_pending_opgroups(const struct ofconn *ofconn)
1050{
1051 return !list_is_empty(&ofconn->opgroups);
1052}
1053
7ee20df1
BP
1054/* Adds 'ofconn_node' to 'ofconn''s list of pending opgroups.
1055 *
1056 * If 'ofconn' is destroyed or its connection drops, then 'ofconn' will remove
1057 * 'ofconn_node' from the list and re-initialize it with list_init(). The
1058 * client may, therefore, use list_is_empty(ofconn_node) to determine whether
1059 * 'ofconn_node' is still associated with an active ofconn.
1060 *
1061 * The client may also remove ofconn_node from the list itself, with
1062 * list_remove(). */
1063void
1064ofconn_add_opgroup(struct ofconn *ofconn, struct list *ofconn_node)
1065{
1066 list_push_back(&ofconn->opgroups, ofconn_node);
1067}
19a87e36
BP
1068\f
1069/* Private ofconn functions. */
1070
1071static const char *
1072ofconn_get_target(const struct ofconn *ofconn)
1073{
1074 return rconn_get_target(ofconn->rconn);
1075}
1076
1077static struct ofconn *
9886b662
BP
1078ofconn_create(struct connmgr *mgr, struct rconn *rconn, enum ofconn_type type,
1079 bool enable_async_msgs)
19a87e36 1080{
c4b31872
BP
1081 struct ofconn *ofconn;
1082
1083 ofconn = xzalloc(sizeof *ofconn);
19a87e36
BP
1084 ofconn->connmgr = mgr;
1085 list_push_back(&mgr->all_conns, &ofconn->node);
1086 ofconn->rconn = rconn;
1087 ofconn->type = type;
9886b662 1088 ofconn->enable_async_msgs = enable_async_msgs;
c4b31872 1089
7ee20df1 1090 list_init(&ofconn->opgroups);
c4b31872 1091
2b07c8b1
BP
1092 hmap_init(&ofconn->monitors);
1093 list_init(&ofconn->updates);
1094
c4b31872
BP
1095 ofconn_flush(ofconn);
1096
19a87e36
BP
1097 return ofconn;
1098}
1099
c4b31872
BP
1100/* Clears all of the state in 'ofconn' that should not persist from one
1101 * connection to the next. */
7ee20df1
BP
1102static void
1103ofconn_flush(struct ofconn *ofconn)
1104{
2b07c8b1 1105 struct ofmonitor *monitor, *next_monitor;
c4b31872
BP
1106 int i;
1107
f4f1ea7e 1108 ofconn->role = OFPCR12_ROLE_EQUAL;
eb12aa89 1109 ofconn_set_protocol(ofconn, OFPUTIL_P_NONE);
c4b31872 1110 ofconn->packet_in_format = NXPIF_OPENFLOW10;
c4b31872
BP
1111
1112 /* Disassociate 'ofconn' from all of the ofopgroups that it initiated that
1113 * have not yet completed. (Those ofopgroups will still run to completion
1114 * in the usual way, but any errors that they run into will not be reported
1115 * on any OpenFlow channel.)
1116 *
1117 * Also discard any blocked operation on 'ofconn'. */
7ee20df1
BP
1118 while (!list_is_empty(&ofconn->opgroups)) {
1119 list_init(list_pop_front(&ofconn->opgroups));
1120 }
1121 ofpbuf_delete(ofconn->blocked);
1122 ofconn->blocked = NULL;
c4b31872
BP
1123
1124 rconn_packet_counter_destroy(ofconn->packet_in_counter);
1125 ofconn->packet_in_counter = rconn_packet_counter_create();
1126 for (i = 0; i < N_SCHEDULERS; i++) {
1127 if (ofconn->schedulers[i]) {
1128 int rate, burst;
1129
1130 pinsched_get_limits(ofconn->schedulers[i], &rate, &burst);
1131 pinsched_destroy(ofconn->schedulers[i]);
1132 ofconn->schedulers[i] = pinsched_create(rate, burst);
1133 }
1134 }
1135 if (ofconn->pktbuf) {
1136 pktbuf_destroy(ofconn->pktbuf);
1137 ofconn->pktbuf = pktbuf_create();
1138 }
1139 ofconn->miss_send_len = (ofconn->type == OFCONN_PRIMARY
1140 ? OFP_DEFAULT_MISS_SEND_LEN
1141 : 0);
a7349929 1142 ofconn->controller_id = 0;
c4b31872
BP
1143
1144 rconn_packet_counter_destroy(ofconn->reply_counter);
1145 ofconn->reply_counter = rconn_packet_counter_create();
80d5aefd 1146
9886b662
BP
1147 if (ofconn->enable_async_msgs) {
1148 uint32_t *master = ofconn->master_async_config;
1149 uint32_t *slave = ofconn->slave_async_config;
1150
1151 /* "master" and "other" roles get all asynchronous messages by default,
1152 * except that the controller needs to enable nonstandard "packet-in"
1153 * reasons itself. */
1154 master[OAM_PACKET_IN] = (1u << OFPR_NO_MATCH) | (1u << OFPR_ACTION);
1155 master[OAM_PORT_STATUS] = ((1u << OFPPR_ADD)
1156 | (1u << OFPPR_DELETE)
1157 | (1u << OFPPR_MODIFY));
1158 master[OAM_FLOW_REMOVED] = ((1u << OFPRR_IDLE_TIMEOUT)
1159 | (1u << OFPRR_HARD_TIMEOUT)
1160 | (1u << OFPRR_DELETE));
1161
1162 /* "slave" role gets port status updates by default. */
1163 slave[OAM_PACKET_IN] = 0;
1164 slave[OAM_PORT_STATUS] = ((1u << OFPPR_ADD)
1165 | (1u << OFPPR_DELETE)
1166 | (1u << OFPPR_MODIFY));
1167 slave[OAM_FLOW_REMOVED] = 0;
1168 } else {
1169 memset(ofconn->master_async_config, 0,
1170 sizeof ofconn->master_async_config);
1171 memset(ofconn->slave_async_config, 0,
1172 sizeof ofconn->slave_async_config);
1173 }
2b07c8b1
BP
1174
1175 HMAP_FOR_EACH_SAFE (monitor, next_monitor, ofconn_node,
1176 &ofconn->monitors) {
1177 ofmonitor_destroy(monitor);
1178 }
1179 rconn_packet_counter_destroy(ofconn->monitor_counter);
1180 ofconn->monitor_counter = rconn_packet_counter_create();
1181 ofpbuf_list_delete(&ofconn->updates); /* ...but it should be empty. */
7ee20df1
BP
1182}
1183
19a87e36
BP
1184static void
1185ofconn_destroy(struct ofconn *ofconn)
1186{
7ee20df1
BP
1187 ofconn_flush(ofconn);
1188
19a87e36
BP
1189 if (ofconn->type == OFCONN_PRIMARY) {
1190 hmap_remove(&ofconn->connmgr->controllers, &ofconn->hmap_node);
1191 }
1192
92bbc96a 1193 hmap_destroy(&ofconn->monitors);
19a87e36
BP
1194 list_remove(&ofconn->node);
1195 rconn_destroy(ofconn->rconn);
1196 rconn_packet_counter_destroy(ofconn->packet_in_counter);
1197 rconn_packet_counter_destroy(ofconn->reply_counter);
1198 pktbuf_destroy(ofconn->pktbuf);
2b07c8b1 1199 rconn_packet_counter_destroy(ofconn->monitor_counter);
19a87e36
BP
1200 free(ofconn);
1201}
1202
1203/* Reconfigures 'ofconn' to match 'c'. 'ofconn' and 'c' must have the same
1204 * target. */
1205static void
1206ofconn_reconfigure(struct ofconn *ofconn, const struct ofproto_controller *c)
1207{
1208 int probe_interval;
1209
1210 ofconn->band = c->band;
065825e9 1211 ofconn->enable_async_msgs = c->enable_async_msgs;
19a87e36
BP
1212
1213 rconn_set_max_backoff(ofconn->rconn, c->max_backoff);
1214
1215 probe_interval = c->probe_interval ? MAX(c->probe_interval, 5) : 0;
1216 rconn_set_probe_interval(ofconn->rconn, probe_interval);
1217
1218 ofconn_set_rate_limit(ofconn, c->rate_limit, c->burst_limit);
0442efd9
MM
1219
1220 /* If dscp value changed reconnect. */
1221 if (c->dscp != rconn_get_dscp(ofconn->rconn)) {
1222 rconn_set_dscp(ofconn->rconn, c->dscp);
1223 rconn_reconnect(ofconn->rconn);
1224 }
19a87e36
BP
1225}
1226
7ee20df1
BP
1227/* Returns true if it makes sense for 'ofconn' to receive and process OpenFlow
1228 * messages. */
1229static bool
1230ofconn_may_recv(const struct ofconn *ofconn)
1231{
a6441685 1232 int count = ofconn->reply_counter->n_packets;
7ee20df1
BP
1233 return (!ofconn->blocked || ofconn->retry) && count < OFCONN_REPLY_MAX;
1234}
1235
19a87e36
BP
1236static void
1237ofconn_run(struct ofconn *ofconn,
e03248b7
JR
1238 bool (*handle_openflow)(struct ofconn *,
1239 const struct ofpbuf *ofp_msg))
19a87e36
BP
1240{
1241 struct connmgr *mgr = ofconn->connmgr;
19a87e36
BP
1242 size_t i;
1243
1244 for (i = 0; i < N_SCHEDULERS; i++) {
1245 pinsched_run(ofconn->schedulers[i], do_send_packet_in, ofconn);
1246 }
1247
1248 rconn_run(ofconn->rconn);
1249
7ee20df1
BP
1250 if (handle_openflow) {
1251 /* Limit the number of iterations to avoid starving other tasks. */
1252 for (i = 0; i < 50 && ofconn_may_recv(ofconn); i++) {
1253 struct ofpbuf *of_msg;
1254
1255 of_msg = (ofconn->blocked
1256 ? ofconn->blocked
1257 : rconn_recv(ofconn->rconn));
19a87e36
BP
1258 if (!of_msg) {
1259 break;
1260 }
1261 if (mgr->fail_open) {
1262 fail_open_maybe_recover(mgr->fail_open);
1263 }
7ee20df1
BP
1264
1265 if (handle_openflow(ofconn, of_msg)) {
1266 ofpbuf_delete(of_msg);
1267 ofconn->blocked = NULL;
1268 } else {
1269 ofconn->blocked = of_msg;
1270 ofconn->retry = false;
1271 }
19a87e36
BP
1272 }
1273 }
1274
1275 if (!rconn_is_alive(ofconn->rconn)) {
1276 ofconn_destroy(ofconn);
7ee20df1
BP
1277 } else if (!rconn_is_connected(ofconn->rconn)) {
1278 ofconn_flush(ofconn);
19a87e36
BP
1279 }
1280}
1281
1282static void
7ee20df1 1283ofconn_wait(struct ofconn *ofconn, bool handling_openflow)
19a87e36
BP
1284{
1285 int i;
1286
1287 for (i = 0; i < N_SCHEDULERS; i++) {
1288 pinsched_wait(ofconn->schedulers[i]);
1289 }
1290 rconn_run_wait(ofconn->rconn);
7ee20df1 1291 if (handling_openflow && ofconn_may_recv(ofconn)) {
19a87e36 1292 rconn_recv_wait(ofconn->rconn);
19a87e36
BP
1293 }
1294}
1295
80d5aefd
BP
1296/* Returns true if 'ofconn' should receive asynchronous messages of the given
1297 * OAM_* 'type' and 'reason', which should be a OFPR_* value for OAM_PACKET_IN,
1298 * a OFPPR_* value for OAM_PORT_STATUS, or an OFPRR_* value for
1299 * OAM_FLOW_REMOVED. Returns false if the message should not be sent on
1300 * 'ofconn'. */
19a87e36 1301static bool
80d5aefd
BP
1302ofconn_receives_async_msg(const struct ofconn *ofconn,
1303 enum ofconn_async_msg_type type,
1304 unsigned int reason)
19a87e36 1305{
80d5aefd
BP
1306 const uint32_t *async_config;
1307
cb22974d
BP
1308 ovs_assert(reason < 32);
1309 ovs_assert((unsigned int) type < OAM_N_TYPES);
19a87e36 1310
eb12aa89
BP
1311 if (ofconn_get_protocol(ofconn) == OFPUTIL_P_NONE
1312 || !rconn_is_connected(ofconn->rconn)) {
f0fd1a17 1313 return false;
f0fd1a17 1314 }
f0fd1a17 1315
80d5aefd
BP
1316 /* Keep the following code in sync with the documentation in the
1317 * "Asynchronous Messages" section in DESIGN. */
1318
1319 if (ofconn->type == OFCONN_SERVICE && !ofconn->miss_send_len) {
1320 /* Service connections don't get asynchronous messages unless they have
1321 * explicitly asked for them by setting a nonzero miss send length. */
f0fd1a17 1322 return false;
f0fd1a17 1323 }
80d5aefd 1324
f4f1ea7e 1325 async_config = (ofconn->role == OFPCR12_ROLE_SLAVE
80d5aefd
BP
1326 ? ofconn->slave_async_config
1327 : ofconn->master_async_config);
1328 if (!(async_config[type] & (1u << reason))) {
1329 return false;
1330 }
1331
1332 return true;
f0fd1a17
PS
1333}
1334
19a87e36
BP
1335/* Returns a human-readable name for an OpenFlow connection between 'mgr' and
1336 * 'target', suitable for use in log messages for identifying the connection.
1337 *
1338 * The name is dynamically allocated. The caller should free it (with free())
1339 * when it is no longer needed. */
1340static char *
1341ofconn_make_name(const struct connmgr *mgr, const char *target)
1342{
1343 return xasprintf("%s<->%s", mgr->name, target);
1344}
1345
1346static void
1347ofconn_set_rate_limit(struct ofconn *ofconn, int rate, int burst)
1348{
1349 int i;
1350
1351 for (i = 0; i < N_SCHEDULERS; i++) {
1352 struct pinsched **s = &ofconn->schedulers[i];
1353
1354 if (rate > 0) {
1355 if (!*s) {
1356 *s = pinsched_create(rate, burst);
1357 } else {
1358 pinsched_set_limits(*s, rate, burst);
1359 }
1360 } else {
1361 pinsched_destroy(*s);
1362 *s = NULL;
1363 }
1364 }
1365}
1366
1367static void
1368ofconn_send(const struct ofconn *ofconn, struct ofpbuf *msg,
1369 struct rconn_packet_counter *counter)
1370{
982697a4 1371 ofpmsg_update_length(msg);
acb9da40 1372 rconn_send(ofconn->rconn, msg, counter);
19a87e36
BP
1373}
1374\f
1375/* Sending asynchronous messages. */
1376
d8653c38 1377static void schedule_packet_in(struct ofconn *, struct ofputil_packet_in);
19a87e36
BP
1378
1379/* Sends an OFPT_PORT_STATUS message with 'opp' and 'reason' to appropriate
9cfcdadf 1380 * controllers managed by 'mgr'. */
19a87e36 1381void
9e1fd49b
BP
1382connmgr_send_port_status(struct connmgr *mgr,
1383 const struct ofputil_phy_port *pp, uint8_t reason)
19a87e36
BP
1384{
1385 /* XXX Should limit the number of queued port status change messages. */
9e1fd49b 1386 struct ofputil_port_status ps;
19a87e36
BP
1387 struct ofconn *ofconn;
1388
9e1fd49b
BP
1389 ps.reason = reason;
1390 ps.desc = *pp;
19a87e36 1391 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
80d5aefd 1392 if (ofconn_receives_async_msg(ofconn, OAM_PORT_STATUS, reason)) {
9e1fd49b 1393 struct ofpbuf *msg;
80d5aefd 1394
eb12aa89 1395 msg = ofputil_encode_port_status(&ps, ofconn_get_protocol(ofconn));
9e1fd49b 1396 ofconn_send(ofconn, msg, NULL);
19a87e36 1397 }
19a87e36
BP
1398 }
1399}
1400
1401/* Sends an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message based on 'fr' to
1402 * appropriate controllers managed by 'mgr'. */
1403void
1404connmgr_send_flow_removed(struct connmgr *mgr,
1405 const struct ofputil_flow_removed *fr)
1406{
1407 struct ofconn *ofconn;
1408
1409 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
80d5aefd
BP
1410 if (ofconn_receives_async_msg(ofconn, OAM_FLOW_REMOVED, fr->reason)) {
1411 struct ofpbuf *msg;
1412
1413 /* Account flow expirations as replies to OpenFlow requests. That
1414 * works because preventing OpenFlow requests from being processed
1415 * also prevents new flows from being added (and expiring). (It
1416 * also prevents processing OpenFlow requests that would not add
1417 * new flows, so it is imperfect.) */
eb12aa89 1418 msg = ofputil_encode_flow_removed(fr, ofconn_get_protocol(ofconn));
80d5aefd 1419 ofconn_send_reply(ofconn, msg);
19a87e36 1420 }
19a87e36
BP
1421 }
1422}
1423
78bd1cd0 1424/* Given 'pin', sends an OFPT_PACKET_IN message to each OpenFlow controller as
2ed1202f
BP
1425 * necessary according to their individual configurations.
1426 *
1427 * The caller doesn't need to fill in pin->buffer_id or pin->total_len. */
19a87e36 1428void
78bd1cd0 1429connmgr_send_packet_in(struct connmgr *mgr,
d8653c38 1430 const struct ofputil_packet_in *pin)
19a87e36 1431{
29ebe880 1432 struct ofconn *ofconn;
19a87e36 1433
19a87e36 1434 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
a7349929
BP
1435 if (ofconn_receives_async_msg(ofconn, OAM_PACKET_IN, pin->reason)
1436 && ofconn->controller_id == pin->controller_id) {
d8653c38 1437 schedule_packet_in(ofconn, *pin);
19a87e36
BP
1438 }
1439 }
19a87e36
BP
1440}
1441
1442/* pinsched callback for sending 'ofp_packet_in' on 'ofconn'. */
1443static void
1444do_send_packet_in(struct ofpbuf *ofp_packet_in, void *ofconn_)
1445{
1446 struct ofconn *ofconn = ofconn_;
1447
1448 rconn_send_with_limit(ofconn->rconn, ofp_packet_in,
1449 ofconn->packet_in_counter, 100);
1450}
1451
d8653c38
BP
1452/* Takes 'pin', composes an OpenFlow packet-in message from it, and passes it
1453 * to 'ofconn''s packet scheduler for sending. */
19a87e36 1454static void
d8653c38 1455schedule_packet_in(struct ofconn *ofconn, struct ofputil_packet_in pin)
19a87e36
BP
1456{
1457 struct connmgr *mgr = ofconn->connmgr;
19a87e36 1458
2ed1202f
BP
1459 pin.total_len = pin.packet_len;
1460
19a87e36 1461 /* Get OpenFlow buffer_id. */
78bd1cd0 1462 if (pin.reason == OFPR_ACTION) {
19a87e36
BP
1463 pin.buffer_id = UINT32_MAX;
1464 } else if (mgr->fail_open && fail_open_is_active(mgr->fail_open)) {
1465 pin.buffer_id = pktbuf_get_null();
1466 } else if (!ofconn->pktbuf) {
1467 pin.buffer_id = UINT32_MAX;
1468 } else {
3e3252fa 1469 pin.buffer_id = pktbuf_save(ofconn->pktbuf, pin.packet, pin.packet_len,
d8653c38 1470 pin.fmd.in_port);
19a87e36
BP
1471 }
1472
1473 /* Figure out how much of the packet to send. */
78bd1cd0 1474 if (pin.reason == OFPR_NO_MATCH) {
3e3252fa 1475 pin.send_len = pin.packet_len;
78bd1cd0
BP
1476 } else {
1477 /* Caller should have initialized 'send_len' to 'max_len' specified in
d01c980f 1478 * output action. */
78bd1cd0 1479 }
19a87e36
BP
1480 if (pin.buffer_id != UINT32_MAX) {
1481 pin.send_len = MIN(pin.send_len, ofconn->miss_send_len);
1482 }
19a87e36
BP
1483
1484 /* Make OFPT_PACKET_IN and hand over to packet scheduler. It might
1485 * immediately call into do_send_packet_in() or it might buffer it for a
1486 * while (until a later call to pinsched_run()). */
78bd1cd0 1487 pinsched_send(ofconn->schedulers[pin.reason == OFPR_NO_MATCH ? 0 : 1],
d8653c38 1488 pin.fmd.in_port,
eb12aa89 1489 ofputil_encode_packet_in(&pin, ofconn_get_protocol(ofconn),
d94240ec 1490 ofconn->packet_in_format),
19a87e36
BP
1491 do_send_packet_in, ofconn);
1492}
1493\f
1494/* Fail-open settings. */
1495
1496/* Returns the failure handling mode (OFPROTO_FAIL_SECURE or
1497 * OFPROTO_FAIL_STANDALONE) for 'mgr'. */
1498enum ofproto_fail_mode
1499connmgr_get_fail_mode(const struct connmgr *mgr)
1500{
1501 return mgr->fail_mode;
1502}
1503
1504/* Sets the failure handling mode for 'mgr' to 'fail_mode' (either
1505 * OFPROTO_FAIL_SECURE or OFPROTO_FAIL_STANDALONE). */
1506void
1507connmgr_set_fail_mode(struct connmgr *mgr, enum ofproto_fail_mode fail_mode)
1508{
08fd19f0
BP
1509 if (mgr->fail_mode != fail_mode) {
1510 mgr->fail_mode = fail_mode;
1511 update_fail_open(mgr);
1512 if (!connmgr_has_controllers(mgr)) {
1513 ofproto_flush_flows(mgr->ofproto);
1514 }
1515 }
19a87e36
BP
1516}
1517\f
1518/* Fail-open implementation. */
1519
1520/* Returns the longest probe interval among the primary controllers configured
1521 * on 'mgr'. Returns 0 if there are no primary controllers. */
1522int
1523connmgr_get_max_probe_interval(const struct connmgr *mgr)
1524{
1525 const struct ofconn *ofconn;
1526 int max_probe_interval;
1527
1528 max_probe_interval = 0;
1529 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1530 int probe_interval = rconn_get_probe_interval(ofconn->rconn);
1531 max_probe_interval = MAX(max_probe_interval, probe_interval);
1532 }
1533 return max_probe_interval;
1534}
1535
1536/* Returns the number of seconds for which all of 'mgr's primary controllers
1537 * have been disconnected. Returns 0 if 'mgr' has no primary controllers. */
1538int
1539connmgr_failure_duration(const struct connmgr *mgr)
1540{
1541 const struct ofconn *ofconn;
1542 int min_failure_duration;
1543
1544 if (!connmgr_has_controllers(mgr)) {
1545 return 0;
1546 }
1547
1548 min_failure_duration = INT_MAX;
1549 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1550 int failure_duration = rconn_failure_duration(ofconn->rconn);
1551 min_failure_duration = MIN(min_failure_duration, failure_duration);
1552 }
1553 return min_failure_duration;
1554}
1555
1556/* Returns true if at least one primary controller is connected (regardless of
1557 * whether those controllers are believed to have authenticated and accepted
1558 * this switch), false if none of them are connected. */
1559bool
1560connmgr_is_any_controller_connected(const struct connmgr *mgr)
1561{
1562 const struct ofconn *ofconn;
1563
1564 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1565 if (rconn_is_connected(ofconn->rconn)) {
1566 return true;
1567 }
1568 }
1569 return false;
1570}
1571
1572/* Returns true if at least one primary controller is believed to have
1573 * authenticated and accepted this switch, false otherwise. */
1574bool
1575connmgr_is_any_controller_admitted(const struct connmgr *mgr)
1576{
1577 const struct ofconn *ofconn;
1578
1579 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1580 if (rconn_is_admitted(ofconn->rconn)) {
1581 return true;
1582 }
1583 }
1584 return false;
1585}
19a87e36
BP
1586\f
1587/* In-band configuration. */
1588
1589static bool any_extras_changed(const struct connmgr *,
1590 const struct sockaddr_in *extras, size_t n);
1591
1592/* Sets the 'n' TCP port addresses in 'extras' as ones to which 'mgr''s
1593 * in-band control should guarantee access, in the same way that in-band
1594 * control guarantees access to OpenFlow controllers. */
1595void
1596connmgr_set_extra_in_band_remotes(struct connmgr *mgr,
1597 const struct sockaddr_in *extras, size_t n)
1598{
1599 if (!any_extras_changed(mgr, extras, n)) {
1600 return;
1601 }
1602
1603 free(mgr->extra_in_band_remotes);
1604 mgr->n_extra_remotes = n;
1605 mgr->extra_in_band_remotes = xmemdup(extras, n * sizeof *extras);
1606
1607 update_in_band_remotes(mgr);
1608}
1609
1610/* Sets the OpenFlow queue used by flows set up by in-band control on
1611 * 'mgr' to 'queue_id'. If 'queue_id' is negative, then in-band control
1612 * flows will use the default queue. */
1613void
1614connmgr_set_in_band_queue(struct connmgr *mgr, int queue_id)
1615{
1616 if (queue_id != mgr->in_band_queue) {
1617 mgr->in_band_queue = queue_id;
1618 update_in_band_remotes(mgr);
1619 }
1620}
1621
1622static bool
1623any_extras_changed(const struct connmgr *mgr,
1624 const struct sockaddr_in *extras, size_t n)
1625{
1626 size_t i;
1627
1628 if (n != mgr->n_extra_remotes) {
1629 return true;
1630 }
1631
1632 for (i = 0; i < n; i++) {
1633 const struct sockaddr_in *old = &mgr->extra_in_band_remotes[i];
1634 const struct sockaddr_in *new = &extras[i];
1635
1636 if (old->sin_addr.s_addr != new->sin_addr.s_addr ||
1637 old->sin_port != new->sin_port) {
1638 return true;
1639 }
1640 }
1641
1642 return false;
1643}
1644\f
1645/* In-band implementation. */
1646
1647bool
f7f1ea29 1648connmgr_has_in_band(struct connmgr *mgr)
19a87e36 1649{
f7f1ea29 1650 return mgr->in_band != NULL;
19a87e36
BP
1651}
1652\f
1653/* Fail-open and in-band implementation. */
1654
1655/* Called by 'ofproto' after all flows have been flushed, to allow fail-open
7ee20df1
BP
1656 * and standalone mode to re-create their flows.
1657 *
1658 * In-band control has more sophisticated code that manages flows itself. */
19a87e36
BP
1659void
1660connmgr_flushed(struct connmgr *mgr)
1661{
19a87e36
BP
1662 if (mgr->fail_open) {
1663 fail_open_flushed(mgr->fail_open);
1664 }
08fd19f0
BP
1665
1666 /* If there are no controllers and we're in standalone mode, set up a flow
1667 * that matches every packet and directs them to OFPP_NORMAL (which goes to
1668 * us). Otherwise, the switch is in secure mode and we won't pass any
1669 * traffic until a controller has been defined and it tells us to do so. */
1670 if (!connmgr_has_controllers(mgr)
1671 && mgr->fail_mode == OFPROTO_FAIL_STANDALONE) {
f25d0cf3 1672 struct ofpbuf ofpacts;
81a76618 1673 struct match match;
08fd19f0 1674
f25d0cf3
BP
1675 ofpbuf_init(&ofpacts, OFPACT_OUTPUT_SIZE);
1676 ofpact_put_OUTPUT(&ofpacts)->port = OFPP_NORMAL;
1677 ofpact_pad(&ofpacts);
1678
81a76618
BP
1679 match_init_catchall(&match);
1680 ofproto_add_flow(mgr->ofproto, &match, 0, ofpacts.data, ofpacts.size);
f25d0cf3
BP
1681
1682 ofpbuf_uninit(&ofpacts);
08fd19f0 1683 }
19a87e36
BP
1684}
1685\f
1686/* Creates a new ofservice for 'target' in 'mgr'. Returns 0 if successful,
1687 * otherwise a positive errno value.
1688 *
1689 * ofservice_reconfigure() must be called to fully configure the new
1690 * ofservice. */
1691static int
00401ef0
SH
1692ofservice_create(struct connmgr *mgr, const char *target,
1693 uint32_t allowed_versions, uint8_t dscp)
19a87e36
BP
1694{
1695 struct ofservice *ofservice;
1696 struct pvconn *pvconn;
1697 int error;
1698
82c8c53c 1699 error = pvconn_open(target, allowed_versions, dscp, &pvconn);
19a87e36
BP
1700 if (error) {
1701 return error;
1702 }
1703
1704 ofservice = xzalloc(sizeof *ofservice);
1705 hmap_insert(&mgr->services, &ofservice->node, hash_string(target, 0));
1706 ofservice->pvconn = pvconn;
90ef0206 1707 ofservice->allowed_versions = allowed_versions;
19a87e36
BP
1708
1709 return 0;
1710}
1711
1712static void
1713ofservice_destroy(struct connmgr *mgr, struct ofservice *ofservice)
1714{
1715 hmap_remove(&mgr->services, &ofservice->node);
1716 pvconn_close(ofservice->pvconn);
1717 free(ofservice);
1718}
1719
1720static void
1721ofservice_reconfigure(struct ofservice *ofservice,
1722 const struct ofproto_controller *c)
1723{
1724 ofservice->probe_interval = c->probe_interval;
1725 ofservice->rate_limit = c->rate_limit;
1726 ofservice->burst_limit = c->burst_limit;
9886b662 1727 ofservice->enable_async_msgs = c->enable_async_msgs;
f125905c 1728 ofservice->dscp = c->dscp;
19a87e36
BP
1729}
1730
1731/* Finds and returns the ofservice within 'mgr' that has the given
1732 * 'target', or a null pointer if none exists. */
1733static struct ofservice *
1734ofservice_lookup(struct connmgr *mgr, const char *target)
1735{
1736 struct ofservice *ofservice;
1737
1738 HMAP_FOR_EACH_WITH_HASH (ofservice, node, hash_string(target, 0),
1739 &mgr->services) {
1740 if (!strcmp(pvconn_get_name(ofservice->pvconn), target)) {
1741 return ofservice;
1742 }
1743 }
1744 return NULL;
1745}
2b07c8b1
BP
1746\f
1747/* Flow monitors (NXST_FLOW_MONITOR). */
1748
1749/* A counter incremented when something significant happens to an OpenFlow
1750 * rule.
1751 *
1752 * - When a rule is added, its 'add_seqno' and 'modify_seqno' are set to
1753 * the current value (which is then incremented).
1754 *
1755 * - When a rule is modified, its 'modify_seqno' is set to the current
1756 * value (which is then incremented).
1757 *
1758 * Thus, by comparing an old value of monitor_seqno against a rule's
1759 * 'add_seqno', one can tell whether the rule was added before or after the old
1760 * value was read, and similarly for 'modify_seqno'.
1761 *
1762 * 32 bits should normally be sufficient (and would be nice, to save space in
1763 * each rule) but then we'd have to have some special cases for wraparound.
1764 *
1765 * We initialize monitor_seqno to 1 to allow 0 to be used as an invalid
1766 * value. */
1767static uint64_t monitor_seqno = 1;
1768
1769COVERAGE_DEFINE(ofmonitor_pause);
1770COVERAGE_DEFINE(ofmonitor_resume);
1771
1772enum ofperr
1773ofmonitor_create(const struct ofputil_flow_monitor_request *request,
1774 struct ofconn *ofconn, struct ofmonitor **monitorp)
1775{
1776 struct ofmonitor *m;
1777
1778 *monitorp = NULL;
1779
1780 m = ofmonitor_lookup(ofconn, request->id);
1781 if (m) {
1782 return OFPERR_NXBRC_FM_DUPLICATE_ID;
1783 }
1784
1785 m = xmalloc(sizeof *m);
1786 m->ofconn = ofconn;
1787 hmap_insert(&ofconn->monitors, &m->ofconn_node, hash_int(request->id, 0));
1788 m->id = request->id;
1789 m->flags = request->flags;
1790 m->out_port = request->out_port;
1791 m->table_id = request->table_id;
5cb7a798 1792 minimatch_init(&m->match, &request->match);
2b07c8b1
BP
1793
1794 *monitorp = m;
1795 return 0;
1796}
1797
1798struct ofmonitor *
1799ofmonitor_lookup(struct ofconn *ofconn, uint32_t id)
1800{
1801 struct ofmonitor *m;
1802
1803 HMAP_FOR_EACH_IN_BUCKET (m, ofconn_node, hash_int(id, 0),
1804 &ofconn->monitors) {
1805 if (m->id == id) {
1806 return m;
1807 }
1808 }
1809 return NULL;
1810}
1811
1812void
1813ofmonitor_destroy(struct ofmonitor *m)
1814{
1815 if (m) {
5684eb74 1816 minimatch_destroy(&m->match);
2b07c8b1
BP
1817 hmap_remove(&m->ofconn->monitors, &m->ofconn_node);
1818 free(m);
1819 }
1820}
1821
1822void
1823ofmonitor_report(struct connmgr *mgr, struct rule *rule,
1824 enum nx_flow_update_event event,
1825 enum ofp_flow_removed_reason reason,
1826 const struct ofconn *abbrev_ofconn, ovs_be32 abbrev_xid)
1827{
1828 enum nx_flow_monitor_flags update;
1829 struct ofconn *ofconn;
1830
1831 switch (event) {
1832 case NXFME_ADDED:
1833 update = NXFMF_ADD;
1834 rule->add_seqno = rule->modify_seqno = monitor_seqno++;
1835 break;
1836
1837 case NXFME_DELETED:
1838 update = NXFMF_DELETE;
1839 break;
1840
1841 case NXFME_MODIFIED:
1842 update = NXFMF_MODIFY;
1843 rule->modify_seqno = monitor_seqno++;
1844 break;
1845
1846 default:
1847 case NXFME_ABBREV:
1848 NOT_REACHED();
1849 }
1850
1851 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1852 enum nx_flow_monitor_flags flags = 0;
1853 struct ofmonitor *m;
1854
1855 if (ofconn->monitor_paused) {
1856 /* Only send NXFME_DELETED notifications for flows that were added
1857 * before we paused. */
1858 if (event != NXFME_DELETED
1859 || rule->add_seqno > ofconn->monitor_paused) {
1860 continue;
1861 }
1862 }
1863
1864 HMAP_FOR_EACH (m, ofconn_node, &ofconn->monitors) {
1865 if (m->flags & update
1866 && (m->table_id == 0xff || m->table_id == rule->table_id)
1867 && ofoperation_has_out_port(rule->pending, m->out_port)
1868 && cls_rule_is_loose_match(&rule->cr, &m->match)) {
1869 flags |= m->flags;
1870 }
1871 }
1872
1873 if (flags) {
1874 if (list_is_empty(&ofconn->updates)) {
1875 ofputil_start_flow_update(&ofconn->updates);
1876 ofconn->sent_abbrev_update = false;
1877 }
1878
1879 if (ofconn != abbrev_ofconn || ofconn->monitor_paused) {
1880 struct ofputil_flow_update fu;
5cb7a798 1881 struct match match;
2b07c8b1
BP
1882
1883 fu.event = event;
1884 fu.reason = event == NXFME_DELETED ? reason : 0;
1885 fu.idle_timeout = rule->idle_timeout;
1886 fu.hard_timeout = rule->hard_timeout;
1887 fu.table_id = rule->table_id;
1888 fu.cookie = rule->flow_cookie;
5cb7a798
BP
1889 minimatch_expand(&rule->cr.match, &match);
1890 fu.match = &match;
6b140a4e 1891 fu.priority = rule->cr.priority;
2b07c8b1
BP
1892 if (flags & NXFMF_ACTIONS) {
1893 fu.ofpacts = rule->ofpacts;
1894 fu.ofpacts_len = rule->ofpacts_len;
1895 } else {
1896 fu.ofpacts = NULL;
1897 fu.ofpacts_len = 0;
1898 }
1899 ofputil_append_flow_update(&fu, &ofconn->updates);
1900 } else if (!ofconn->sent_abbrev_update) {
1901 struct ofputil_flow_update fu;
1902
1903 fu.event = NXFME_ABBREV;
1904 fu.xid = abbrev_xid;
1905 ofputil_append_flow_update(&fu, &ofconn->updates);
1906
1907 ofconn->sent_abbrev_update = true;
1908 }
1909 }
1910 }
1911}
1912
1913void
1914ofmonitor_flush(struct connmgr *mgr)
1915{
1916 struct ofconn *ofconn;
1917
1918 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1919 struct ofpbuf *msg, *next;
1920
1921 LIST_FOR_EACH_SAFE (msg, next, list_node, &ofconn->updates) {
1922 list_remove(&msg->list_node);
1923 ofconn_send(ofconn, msg, ofconn->monitor_counter);
1924 if (!ofconn->monitor_paused
1925 && ofconn->monitor_counter->n_bytes > 128 * 1024) {
1926 struct ofpbuf *pause;
1927
1928 COVERAGE_INC(ofmonitor_pause);
1929 ofconn->monitor_paused = monitor_seqno++;
982697a4
BP
1930 pause = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_MONITOR_PAUSED,
1931 OFP10_VERSION, htonl(0), 0);
2b07c8b1
BP
1932 ofconn_send(ofconn, pause, ofconn->monitor_counter);
1933 }
1934 }
1935 }
1936}
1937
1938static void
1939ofmonitor_resume(struct ofconn *ofconn)
1940{
982697a4 1941 struct ofpbuf *resumed;
2b07c8b1
BP
1942 struct ofmonitor *m;
1943 struct list rules;
1944 struct list msgs;
1945
1946 list_init(&rules);
1947 HMAP_FOR_EACH (m, ofconn_node, &ofconn->monitors) {
1948 ofmonitor_collect_resume_rules(m, ofconn->monitor_paused, &rules);
1949 }
1950
1951 list_init(&msgs);
1952 ofmonitor_compose_refresh_updates(&rules, &msgs);
1953
982697a4
BP
1954 resumed = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_MONITOR_RESUMED, OFP10_VERSION,
1955 htonl(0), 0);
1956 list_push_back(&msgs, &resumed->list_node);
2b07c8b1
BP
1957 ofconn_send_replies(ofconn, &msgs);
1958
1959 ofconn->monitor_paused = 0;
1960}
1961
1962static void
1963ofmonitor_run(struct connmgr *mgr)
1964{
1965 struct ofconn *ofconn;
1966
1967 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1968 if (ofconn->monitor_paused && !ofconn->monitor_counter->n_packets) {
1969 COVERAGE_INC(ofmonitor_resume);
1970 ofmonitor_resume(ofconn);
1971 }
1972 }
1973}
1974
1975static void
1976ofmonitor_wait(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 poll_immediate_wake();
1983 }
1984 }
1985}