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