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