]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
accaecc4 | 2 | * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc. |
064af421 | 3 | * |
a14bc59f BP |
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: | |
064af421 | 7 | * |
a14bc59f BP |
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. | |
064af421 BP |
15 | */ |
16 | ||
17 | #include <config.h> | |
18 | #include "rconn.h" | |
064af421 BP |
19 | #include <errno.h> |
20 | #include <limits.h> | |
21 | #include <stdlib.h> | |
22 | #include <string.h> | |
23 | #include "coverage.h" | |
982697a4 | 24 | #include "ofp-msgs.h" |
fa37b408 | 25 | #include "ofp-util.h" |
064af421 BP |
26 | #include "ofpbuf.h" |
27 | #include "openflow/openflow.h" | |
28 | #include "poll-loop.h" | |
29 | #include "sat-math.h" | |
30 | #include "timeval.h" | |
31 | #include "util.h" | |
32 | #include "vconn.h" | |
064af421 BP |
33 | #include "vlog.h" |
34 | ||
d98e6007 | 35 | VLOG_DEFINE_THIS_MODULE(rconn); |
5136ce49 | 36 | |
d76f09ea BP |
37 | COVERAGE_DEFINE(rconn_discarded); |
38 | COVERAGE_DEFINE(rconn_overflow); | |
39 | COVERAGE_DEFINE(rconn_queued); | |
40 | COVERAGE_DEFINE(rconn_sent); | |
41 | ||
064af421 BP |
42 | #define STATES \ |
43 | STATE(VOID, 1 << 0) \ | |
44 | STATE(BACKOFF, 1 << 1) \ | |
45 | STATE(CONNECTING, 1 << 2) \ | |
46 | STATE(ACTIVE, 1 << 3) \ | |
47 | STATE(IDLE, 1 << 4) | |
48 | enum state { | |
49 | #define STATE(NAME, VALUE) S_##NAME = VALUE, | |
50 | STATES | |
51 | #undef STATE | |
52 | }; | |
53 | ||
54 | static const char * | |
55 | state_name(enum state state) | |
56 | { | |
57 | switch (state) { | |
58 | #define STATE(NAME, VALUE) case S_##NAME: return #NAME; | |
59 | STATES | |
60 | #undef STATE | |
61 | } | |
62 | return "***ERROR***"; | |
63 | } | |
64 | ||
65 | /* A reliable connection to an OpenFlow switch or controller. | |
66 | * | |
67 | * See the large comment in rconn.h for more information. */ | |
68 | struct rconn { | |
9f5e8906 BP |
69 | struct ovs_mutex mutex; |
70 | ||
064af421 BP |
71 | enum state state; |
72 | time_t state_entered; | |
73 | ||
74 | struct vconn *vconn; | |
eb15cdbb BP |
75 | char *name; /* Human-readable descriptive name. */ |
76 | char *target; /* vconn name, passed to vconn_open(). */ | |
064af421 BP |
77 | bool reliable; |
78 | ||
b3907fbc | 79 | struct list txq; /* Contains "struct ofpbuf"s. */ |
064af421 BP |
80 | |
81 | int backoff; | |
82 | int max_backoff; | |
83 | time_t backoff_deadline; | |
064af421 | 84 | time_t last_connected; |
2cdcb898 | 85 | time_t last_disconnected; |
064af421 BP |
86 | unsigned int packets_sent; |
87 | unsigned int seqno; | |
88a20d6e | 88 | int last_error; |
064af421 BP |
89 | |
90 | /* In S_ACTIVE and S_IDLE, probably_admitted reports whether we believe | |
91 | * that the peer has made a (positive) admission control decision on our | |
92 | * connection. If we have not yet been (probably) admitted, then the | |
93 | * connection does not reset the timer used for deciding whether the switch | |
94 | * should go into fail-open mode. | |
95 | * | |
96 | * last_admitted reports the last time we believe such a positive admission | |
97 | * control decision was made. */ | |
98 | bool probably_admitted; | |
99 | time_t last_admitted; | |
100 | ||
101 | /* These values are simply for statistics reporting, not used directly by | |
8cd4882f | 102 | * anything internal to the rconn (or ofproto for that matter). */ |
064af421 BP |
103 | unsigned int packets_received; |
104 | unsigned int n_attempted_connections, n_successful_connections; | |
105 | time_t creation_time; | |
106 | unsigned long int total_time_connected; | |
107 | ||
133f2dc9 BP |
108 | /* Throughout this file, "probe" is shorthand for "inactivity probe". When |
109 | * no activity has been observed from the peer for a while, we send out an | |
110 | * echo request as an inactivity probe packet. We should receive back a | |
111 | * response. | |
112 | * | |
113 | * "Activity" is defined as either receiving an OpenFlow message from the | |
114 | * peer or successfully sending a message that had been in 'txq'. */ | |
064af421 | 115 | int probe_interval; /* Secs of inactivity before sending probe. */ |
133f2dc9 | 116 | time_t last_activity; /* Last time we saw some activity. */ |
064af421 | 117 | |
f125905c | 118 | uint8_t dscp; |
19d1ab55 | 119 | |
064af421 BP |
120 | /* Messages sent or received are copied to the monitor connections. */ |
121 | #define MAX_MONITORS 8 | |
122 | struct vconn *monitors[8]; | |
123 | size_t n_monitors; | |
6042457b SH |
124 | |
125 | uint32_t allowed_versions; | |
064af421 BP |
126 | }; |
127 | ||
e182670b SH |
128 | uint32_t rconn_get_allowed_versions(const struct rconn *rconn) |
129 | { | |
130 | return rconn->allowed_versions; | |
131 | } | |
132 | ||
9f5e8906 BP |
133 | static unsigned int elapsed_in_this_state(const struct rconn *rc) |
134 | OVS_REQUIRES(rc->mutex); | |
135 | static unsigned int timeout(const struct rconn *rc) OVS_REQUIRES(rc->mutex); | |
136 | static bool timed_out(const struct rconn *rc) OVS_REQUIRES(rc->mutex); | |
137 | static void state_transition(struct rconn *rc, enum state) | |
138 | OVS_REQUIRES(rc->mutex); | |
139 | static void rconn_set_target__(struct rconn *rc, | |
140 | const char *target, const char *name) | |
141 | OVS_REQUIRES(rc->mutex); | |
142 | static int rconn_send__(struct rconn *rc, struct ofpbuf *, | |
143 | struct rconn_packet_counter *) | |
144 | OVS_REQUIRES(rc->mutex); | |
145 | static int try_send(struct rconn *rc) OVS_REQUIRES(rc->mutex); | |
146 | static void reconnect(struct rconn *rc) OVS_REQUIRES(rc->mutex); | |
147 | static void report_error(struct rconn *rc, int error) OVS_REQUIRES(rc->mutex); | |
148 | static void rconn_disconnect__(struct rconn *rc) OVS_REQUIRES(rc->mutex); | |
149 | static void disconnect(struct rconn *rc, int error) OVS_REQUIRES(rc->mutex); | |
150 | static void flush_queue(struct rconn *rc) OVS_REQUIRES(rc->mutex); | |
151 | static void close_monitor(struct rconn *rc, size_t idx, int retval) | |
152 | OVS_REQUIRES(rc->mutex); | |
064af421 BP |
153 | static void copy_to_monitor(struct rconn *, const struct ofpbuf *); |
154 | static bool is_connected_state(enum state); | |
155 | static bool is_admitted_msg(const struct ofpbuf *); | |
9f5e8906 BP |
156 | static bool rconn_logging_connection_attempts__(const struct rconn *rc) |
157 | OVS_REQUIRES(rc->mutex); | |
158 | static int rconn_get_version__(const struct rconn *rconn) | |
159 | OVS_REQUIRES(rconn->mutex); | |
160 | ||
161 | /* The following prototypes duplicate those in rconn.h, but there we weren't | |
162 | * able to add the OVS_EXCLUDED annotations because the definition of struct | |
163 | * rconn was not visible. */ | |
164 | ||
165 | void rconn_set_max_backoff(struct rconn *rc, int max_backoff) | |
166 | OVS_EXCLUDED(rc->mutex); | |
167 | void rconn_connect(struct rconn *rc, const char *target, const char *name) | |
168 | OVS_EXCLUDED(rc->mutex); | |
169 | void rconn_connect_unreliably(struct rconn *rc, | |
170 | struct vconn *vconn, const char *name) | |
171 | OVS_EXCLUDED(rc->mutex); | |
172 | void rconn_reconnect(struct rconn *rc) OVS_EXCLUDED(rc->mutex); | |
173 | void rconn_disconnect(struct rconn *rc) OVS_EXCLUDED(rc->mutex); | |
174 | void rconn_run(struct rconn *rc) OVS_EXCLUDED(rc->mutex); | |
175 | void rconn_run_wait(struct rconn *rc) OVS_EXCLUDED(rc->mutex); | |
176 | struct ofpbuf *rconn_recv(struct rconn *rc) OVS_EXCLUDED(rc->mutex); | |
177 | void rconn_recv_wait(struct rconn *rc) OVS_EXCLUDED(rc->mutex); | |
178 | int rconn_send(struct rconn *rc, struct ofpbuf *b, | |
179 | struct rconn_packet_counter *counter) | |
180 | OVS_EXCLUDED(rc->mutex); | |
181 | int rconn_send_with_limit(struct rconn *rc, struct ofpbuf *b, | |
182 | struct rconn_packet_counter *counter, | |
183 | int queue_limit) | |
184 | OVS_EXCLUDED(rc->mutex); | |
185 | void rconn_add_monitor(struct rconn *rc, struct vconn *vconn) | |
186 | OVS_EXCLUDED(rc->mutex); | |
187 | void rconn_set_name(struct rconn *rc, const char *new_name) | |
188 | OVS_EXCLUDED(rc->mutex); | |
189 | bool rconn_is_admitted(const struct rconn *rconn) OVS_EXCLUDED(rconn->mutex); | |
190 | int rconn_failure_duration(const struct rconn *rconn) | |
191 | OVS_EXCLUDED(rconn->mutex); | |
192 | ovs_be16 rconn_get_local_port(const struct rconn *rconn) | |
193 | OVS_EXCLUDED(rconn->mutex); | |
194 | int rconn_get_version(const struct rconn *rconn) OVS_EXCLUDED(rconn->mutex); | |
195 | unsigned int rconn_count_txqlen(const struct rconn *rc) | |
196 | OVS_EXCLUDED(rc->mutex); | |
197 | ||
064af421 | 198 | |
064af421 BP |
199 | /* Creates and returns a new rconn. |
200 | * | |
201 | * 'probe_interval' is a number of seconds. If the interval passes once | |
202 | * without an OpenFlow message being received from the peer, the rconn sends | |
203 | * out an "echo request" message. If the interval passes again without a | |
204 | * message being received, the rconn disconnects and re-connects to the peer. | |
205 | * Setting 'probe_interval' to 0 disables this behavior. | |
206 | * | |
207 | * 'max_backoff' is the maximum number of seconds between attempts to connect | |
208 | * to the peer. The actual interval starts at 1 second and doubles on each | |
209 | * failure until it reaches 'max_backoff'. If 0 is specified, the default of | |
9794e806 BP |
210 | * 8 seconds is used. |
211 | * | |
212 | * The new rconn is initially unconnected. Use rconn_connect() or | |
6042457b SH |
213 | * rconn_connect_unreliably() to connect it. |
214 | * | |
215 | * Connections made by the rconn will automatically negotiate an OpenFlow | |
216 | * protocol version acceptable to both peers on the connection. The version | |
5b8ab80c BP |
217 | * negotiated will be one of those in the 'allowed_versions' bitmap: version |
218 | * 'x' is allowed if allowed_versions & (1 << x) is nonzero. (The underlying | |
219 | * vconn will treat an 'allowed_versions' of 0 as OFPUTIL_DEFAULT_VERSIONS.) | |
220 | */ | |
064af421 | 221 | struct rconn * |
6042457b SH |
222 | rconn_create(int probe_interval, int max_backoff, uint8_t dscp, |
223 | uint32_t allowed_versions) | |
064af421 | 224 | { |
ec6fde61 | 225 | struct rconn *rc = xzalloc(sizeof *rc); |
064af421 | 226 | |
9f5e8906 BP |
227 | ovs_mutex_init(&rc->mutex); |
228 | ||
064af421 BP |
229 | rc->state = S_VOID; |
230 | rc->state_entered = time_now(); | |
231 | ||
232 | rc->vconn = NULL; | |
233 | rc->name = xstrdup("void"); | |
eb15cdbb | 234 | rc->target = xstrdup("void"); |
064af421 BP |
235 | rc->reliable = false; |
236 | ||
b3907fbc | 237 | list_init(&rc->txq); |
064af421 BP |
238 | |
239 | rc->backoff = 0; | |
c9aaa877 | 240 | rc->max_backoff = max_backoff ? max_backoff : 8; |
064af421 | 241 | rc->backoff_deadline = TIME_MIN; |
2cdcb898 AE |
242 | rc->last_connected = TIME_MIN; |
243 | rc->last_disconnected = TIME_MIN; | |
064af421 BP |
244 | rc->seqno = 0; |
245 | ||
246 | rc->packets_sent = 0; | |
247 | ||
248 | rc->probably_admitted = false; | |
249 | rc->last_admitted = time_now(); | |
250 | ||
251 | rc->packets_received = 0; | |
252 | rc->n_attempted_connections = 0; | |
253 | rc->n_successful_connections = 0; | |
254 | rc->creation_time = time_now(); | |
255 | rc->total_time_connected = 0; | |
256 | ||
133f2dc9 BP |
257 | rc->last_activity = time_now(); |
258 | ||
064af421 | 259 | rconn_set_probe_interval(rc, probe_interval); |
f125905c | 260 | rconn_set_dscp(rc, dscp); |
064af421 BP |
261 | |
262 | rc->n_monitors = 0; | |
5b8ab80c | 263 | rc->allowed_versions = allowed_versions; |
064af421 BP |
264 | |
265 | return rc; | |
266 | } | |
267 | ||
268 | void | |
269 | rconn_set_max_backoff(struct rconn *rc, int max_backoff) | |
9f5e8906 | 270 | OVS_EXCLUDED(rc->mutex) |
064af421 | 271 | { |
9f5e8906 | 272 | ovs_mutex_lock(&rc->mutex); |
064af421 BP |
273 | rc->max_backoff = MAX(1, max_backoff); |
274 | if (rc->state == S_BACKOFF && rc->backoff > max_backoff) { | |
275 | rc->backoff = max_backoff; | |
276 | if (rc->backoff_deadline > time_now() + max_backoff) { | |
277 | rc->backoff_deadline = time_now() + max_backoff; | |
278 | } | |
279 | } | |
9f5e8906 | 280 | ovs_mutex_unlock(&rc->mutex); |
064af421 BP |
281 | } |
282 | ||
283 | int | |
284 | rconn_get_max_backoff(const struct rconn *rc) | |
285 | { | |
286 | return rc->max_backoff; | |
287 | } | |
288 | ||
f125905c MM |
289 | void |
290 | rconn_set_dscp(struct rconn *rc, uint8_t dscp) | |
291 | { | |
292 | rc->dscp = dscp; | |
293 | } | |
294 | ||
0442efd9 MM |
295 | uint8_t |
296 | rconn_get_dscp(const struct rconn *rc) | |
297 | { | |
298 | return rc->dscp; | |
299 | } | |
300 | ||
064af421 BP |
301 | void |
302 | rconn_set_probe_interval(struct rconn *rc, int probe_interval) | |
303 | { | |
304 | rc->probe_interval = probe_interval ? MAX(5, probe_interval) : 0; | |
305 | } | |
306 | ||
307 | int | |
308 | rconn_get_probe_interval(const struct rconn *rc) | |
309 | { | |
310 | return rc->probe_interval; | |
311 | } | |
312 | ||
eb15cdbb BP |
313 | /* Drops any existing connection on 'rc', then sets up 'rc' to connect to |
314 | * 'target' and reconnect as needed. 'target' should be a remote OpenFlow | |
315 | * target in a form acceptable to vconn_open(). | |
316 | * | |
317 | * If 'name' is nonnull, then it is used in log messages in place of 'target'. | |
318 | * It should presumably give more information to a human reader than 'target', | |
319 | * but it need not be acceptable to vconn_open(). */ | |
d4cbfb19 | 320 | void |
eb15cdbb | 321 | rconn_connect(struct rconn *rc, const char *target, const char *name) |
9f5e8906 | 322 | OVS_EXCLUDED(rc->mutex) |
064af421 | 323 | { |
9f5e8906 BP |
324 | ovs_mutex_lock(&rc->mutex); |
325 | rconn_disconnect__(rc); | |
eb15cdbb | 326 | rconn_set_target__(rc, target, name); |
064af421 | 327 | rc->reliable = true; |
d4cbfb19 | 328 | reconnect(rc); |
9f5e8906 | 329 | ovs_mutex_unlock(&rc->mutex); |
064af421 BP |
330 | } |
331 | ||
eb15cdbb BP |
332 | /* Drops any existing connection on 'rc', then configures 'rc' to use |
333 | * 'vconn'. If the connection on 'vconn' drops, 'rc' will not reconnect on it | |
334 | * own. | |
335 | * | |
336 | * By default, the target obtained from vconn_get_name(vconn) is used in log | |
337 | * messages. If 'name' is nonnull, then it is used instead. It should | |
338 | * presumably give more information to a human reader than the target, but it | |
339 | * need not be acceptable to vconn_open(). */ | |
064af421 | 340 | void |
eb15cdbb BP |
341 | rconn_connect_unreliably(struct rconn *rc, |
342 | struct vconn *vconn, const char *name) | |
9f5e8906 | 343 | OVS_EXCLUDED(rc->mutex) |
064af421 | 344 | { |
cb22974d | 345 | ovs_assert(vconn != NULL); |
9f5e8906 BP |
346 | |
347 | ovs_mutex_lock(&rc->mutex); | |
348 | rconn_disconnect__(rc); | |
eb15cdbb | 349 | rconn_set_target__(rc, vconn_get_name(vconn), name); |
064af421 BP |
350 | rc->reliable = false; |
351 | rc->vconn = vconn; | |
352 | rc->last_connected = time_now(); | |
353 | state_transition(rc, S_ACTIVE); | |
9f5e8906 | 354 | ovs_mutex_unlock(&rc->mutex); |
064af421 BP |
355 | } |
356 | ||
357 | /* If 'rc' is connected, forces it to drop the connection and reconnect. */ | |
358 | void | |
359 | rconn_reconnect(struct rconn *rc) | |
9f5e8906 | 360 | OVS_EXCLUDED(rc->mutex) |
064af421 | 361 | { |
9f5e8906 | 362 | ovs_mutex_lock(&rc->mutex); |
064af421 | 363 | if (rc->state & (S_ACTIVE | S_IDLE)) { |
b97ba90b | 364 | VLOG_INFO("%s: disconnecting", rc->name); |
064af421 BP |
365 | disconnect(rc, 0); |
366 | } | |
9f5e8906 | 367 | ovs_mutex_unlock(&rc->mutex); |
064af421 BP |
368 | } |
369 | ||
9f5e8906 BP |
370 | static void |
371 | rconn_disconnect__(struct rconn *rc) | |
372 | OVS_REQUIRES(rc->mutex) | |
064af421 BP |
373 | { |
374 | if (rc->state != S_VOID) { | |
375 | if (rc->vconn) { | |
376 | vconn_close(rc->vconn); | |
377 | rc->vconn = NULL; | |
378 | } | |
eb15cdbb | 379 | rconn_set_target__(rc, "void", NULL); |
064af421 BP |
380 | rc->reliable = false; |
381 | ||
382 | rc->backoff = 0; | |
383 | rc->backoff_deadline = TIME_MIN; | |
384 | ||
385 | state_transition(rc, S_VOID); | |
386 | } | |
387 | } | |
388 | ||
9f5e8906 BP |
389 | void |
390 | rconn_disconnect(struct rconn *rc) | |
391 | OVS_EXCLUDED(rc->mutex) | |
392 | { | |
393 | ovs_mutex_lock(&rc->mutex); | |
394 | rconn_disconnect__(rc); | |
395 | ovs_mutex_unlock(&rc->mutex); | |
396 | } | |
397 | ||
064af421 BP |
398 | /* Disconnects 'rc' and frees the underlying storage. */ |
399 | void | |
400 | rconn_destroy(struct rconn *rc) | |
401 | { | |
402 | if (rc) { | |
403 | size_t i; | |
404 | ||
9f5e8906 | 405 | ovs_mutex_lock(&rc->mutex); |
064af421 | 406 | free(rc->name); |
eb15cdbb | 407 | free(rc->target); |
064af421 BP |
408 | vconn_close(rc->vconn); |
409 | flush_queue(rc); | |
b3907fbc | 410 | ofpbuf_list_delete(&rc->txq); |
064af421 BP |
411 | for (i = 0; i < rc->n_monitors; i++) { |
412 | vconn_close(rc->monitors[i]); | |
413 | } | |
9f5e8906 BP |
414 | ovs_mutex_unlock(&rc->mutex); |
415 | ovs_mutex_destroy(&rc->mutex); | |
416 | ||
064af421 BP |
417 | free(rc); |
418 | } | |
419 | } | |
420 | ||
421 | static unsigned int | |
67a4917b | 422 | timeout_VOID(const struct rconn *rc OVS_UNUSED) |
9f5e8906 | 423 | OVS_REQUIRES(rc->mutex) |
064af421 BP |
424 | { |
425 | return UINT_MAX; | |
426 | } | |
427 | ||
428 | static void | |
67a4917b | 429 | run_VOID(struct rconn *rc OVS_UNUSED) |
9f5e8906 | 430 | OVS_REQUIRES(rc->mutex) |
064af421 BP |
431 | { |
432 | /* Nothing to do. */ | |
433 | } | |
434 | ||
d4cbfb19 | 435 | static void |
064af421 | 436 | reconnect(struct rconn *rc) |
9f5e8906 | 437 | OVS_REQUIRES(rc->mutex) |
064af421 BP |
438 | { |
439 | int retval; | |
440 | ||
07c8c80d BP |
441 | if (rconn_logging_connection_attempts__(rc)) { |
442 | VLOG_INFO("%s: connecting...", rc->name); | |
443 | } | |
064af421 | 444 | rc->n_attempted_connections++; |
82c8c53c BP |
445 | retval = vconn_open(rc->target, rc->allowed_versions, rc->dscp, |
446 | &rc->vconn); | |
064af421 BP |
447 | if (!retval) { |
448 | rc->backoff_deadline = time_now() + rc->backoff; | |
449 | state_transition(rc, S_CONNECTING); | |
450 | } else { | |
10a89ef0 BP |
451 | VLOG_WARN("%s: connection failed (%s)", |
452 | rc->name, ovs_strerror(retval)); | |
064af421 | 453 | rc->backoff_deadline = TIME_MAX; /* Prevent resetting backoff. */ |
88a20d6e | 454 | disconnect(rc, retval); |
064af421 | 455 | } |
064af421 BP |
456 | } |
457 | ||
458 | static unsigned int | |
459 | timeout_BACKOFF(const struct rconn *rc) | |
9f5e8906 | 460 | OVS_REQUIRES(rc->mutex) |
064af421 BP |
461 | { |
462 | return rc->backoff; | |
463 | } | |
464 | ||
465 | static void | |
466 | run_BACKOFF(struct rconn *rc) | |
9f5e8906 | 467 | OVS_REQUIRES(rc->mutex) |
064af421 BP |
468 | { |
469 | if (timed_out(rc)) { | |
470 | reconnect(rc); | |
471 | } | |
472 | } | |
473 | ||
474 | static unsigned int | |
475 | timeout_CONNECTING(const struct rconn *rc) | |
9f5e8906 | 476 | OVS_REQUIRES(rc->mutex) |
064af421 BP |
477 | { |
478 | return MAX(1, rc->backoff); | |
479 | } | |
480 | ||
481 | static void | |
482 | run_CONNECTING(struct rconn *rc) | |
9f5e8906 | 483 | OVS_REQUIRES(rc->mutex) |
064af421 BP |
484 | { |
485 | int retval = vconn_connect(rc->vconn); | |
486 | if (!retval) { | |
487 | VLOG_INFO("%s: connected", rc->name); | |
488 | rc->n_successful_connections++; | |
489 | state_transition(rc, S_ACTIVE); | |
490 | rc->last_connected = rc->state_entered; | |
491 | } else if (retval != EAGAIN) { | |
07c8c80d BP |
492 | if (rconn_logging_connection_attempts__(rc)) { |
493 | VLOG_INFO("%s: connection failed (%s)", | |
10a89ef0 | 494 | rc->name, ovs_strerror(retval)); |
07c8c80d | 495 | } |
064af421 BP |
496 | disconnect(rc, retval); |
497 | } else if (timed_out(rc)) { | |
07c8c80d BP |
498 | if (rconn_logging_connection_attempts__(rc)) { |
499 | VLOG_INFO("%s: connection timed out", rc->name); | |
500 | } | |
064af421 | 501 | rc->backoff_deadline = TIME_MAX; /* Prevent resetting backoff. */ |
88a20d6e | 502 | disconnect(rc, ETIMEDOUT); |
064af421 BP |
503 | } |
504 | } | |
505 | ||
506 | static void | |
507 | do_tx_work(struct rconn *rc) | |
9f5e8906 | 508 | OVS_REQUIRES(rc->mutex) |
064af421 | 509 | { |
b3907fbc | 510 | if (list_is_empty(&rc->txq)) { |
064af421 BP |
511 | return; |
512 | } | |
b3907fbc | 513 | while (!list_is_empty(&rc->txq)) { |
064af421 BP |
514 | int error = try_send(rc); |
515 | if (error) { | |
516 | break; | |
517 | } | |
133f2dc9 | 518 | rc->last_activity = time_now(); |
064af421 | 519 | } |
b3907fbc | 520 | if (list_is_empty(&rc->txq)) { |
064af421 BP |
521 | poll_immediate_wake(); |
522 | } | |
523 | } | |
524 | ||
525 | static unsigned int | |
526 | timeout_ACTIVE(const struct rconn *rc) | |
9f5e8906 | 527 | OVS_REQUIRES(rc->mutex) |
064af421 BP |
528 | { |
529 | if (rc->probe_interval) { | |
133f2dc9 | 530 | unsigned int base = MAX(rc->last_activity, rc->state_entered); |
064af421 BP |
531 | unsigned int arg = base + rc->probe_interval - rc->state_entered; |
532 | return arg; | |
533 | } | |
534 | return UINT_MAX; | |
535 | } | |
536 | ||
537 | static void | |
538 | run_ACTIVE(struct rconn *rc) | |
9f5e8906 | 539 | OVS_REQUIRES(rc->mutex) |
064af421 BP |
540 | { |
541 | if (timed_out(rc)) { | |
133f2dc9 | 542 | unsigned int base = MAX(rc->last_activity, rc->state_entered); |
1a126c0c SH |
543 | int version; |
544 | ||
064af421 BP |
545 | VLOG_DBG("%s: idle %u seconds, sending inactivity probe", |
546 | rc->name, (unsigned int) (time_now() - base)); | |
547 | ||
9f5e8906 | 548 | version = rconn_get_version__(rc); |
cb22974d | 549 | ovs_assert(version >= 0 && version <= 0xff); |
1a126c0c | 550 | |
064af421 BP |
551 | /* Ordering is important here: rconn_send() can transition to BACKOFF, |
552 | * and we don't want to transition back to IDLE if so, because then we | |
553 | * can end up queuing a packet with vconn == NULL and then *boom*. */ | |
554 | state_transition(rc, S_IDLE); | |
9f5e8906 | 555 | rconn_send__(rc, make_echo_request(version), NULL); |
064af421 BP |
556 | return; |
557 | } | |
558 | ||
559 | do_tx_work(rc); | |
560 | } | |
561 | ||
562 | static unsigned int | |
563 | timeout_IDLE(const struct rconn *rc) | |
9f5e8906 | 564 | OVS_REQUIRES(rc->mutex) |
064af421 BP |
565 | { |
566 | return rc->probe_interval; | |
567 | } | |
568 | ||
569 | static void | |
570 | run_IDLE(struct rconn *rc) | |
9f5e8906 | 571 | OVS_REQUIRES(rc->mutex) |
064af421 BP |
572 | { |
573 | if (timed_out(rc)) { | |
064af421 BP |
574 | VLOG_ERR("%s: no response to inactivity probe after %u " |
575 | "seconds, disconnecting", | |
576 | rc->name, elapsed_in_this_state(rc)); | |
88a20d6e | 577 | disconnect(rc, ETIMEDOUT); |
064af421 BP |
578 | } else { |
579 | do_tx_work(rc); | |
580 | } | |
581 | } | |
582 | ||
583 | /* Performs whatever activities are necessary to maintain 'rc': if 'rc' is | |
584 | * disconnected, attempts to (re)connect, backing off as necessary; if 'rc' is | |
585 | * connected, attempts to send packets in the send queue, if any. */ | |
586 | void | |
587 | rconn_run(struct rconn *rc) | |
9f5e8906 | 588 | OVS_EXCLUDED(rc->mutex) |
064af421 BP |
589 | { |
590 | int old_state; | |
60cb3eb8 BP |
591 | size_t i; |
592 | ||
9f5e8906 | 593 | ovs_mutex_lock(&rc->mutex); |
60cb3eb8 | 594 | if (rc->vconn) { |
accaecc4 BP |
595 | int error; |
596 | ||
60cb3eb8 | 597 | vconn_run(rc->vconn); |
accaecc4 BP |
598 | |
599 | error = vconn_get_status(rc->vconn); | |
600 | if (error) { | |
601 | report_error(rc, error); | |
602 | disconnect(rc, error); | |
603 | } | |
60cb3eb8 | 604 | } |
f44c5146 BP |
605 | for (i = 0; i < rc->n_monitors; ) { |
606 | struct ofpbuf *msg; | |
607 | int retval; | |
608 | ||
60cb3eb8 | 609 | vconn_run(rc->monitors[i]); |
f44c5146 BP |
610 | |
611 | /* Drain any stray message that came in on the monitor connection. */ | |
612 | retval = vconn_recv(rc->monitors[i], &msg); | |
613 | if (!retval) { | |
614 | ofpbuf_delete(msg); | |
615 | } else if (retval != EAGAIN) { | |
616 | close_monitor(rc, i, retval); | |
617 | continue; | |
618 | } | |
619 | i++; | |
60cb3eb8 BP |
620 | } |
621 | ||
064af421 BP |
622 | do { |
623 | old_state = rc->state; | |
624 | switch (rc->state) { | |
625 | #define STATE(NAME, VALUE) case S_##NAME: run_##NAME(rc); break; | |
626 | STATES | |
627 | #undef STATE | |
628 | default: | |
428b2edd | 629 | OVS_NOT_REACHED(); |
064af421 BP |
630 | } |
631 | } while (rc->state != old_state); | |
9f5e8906 | 632 | ovs_mutex_unlock(&rc->mutex); |
064af421 BP |
633 | } |
634 | ||
635 | /* Causes the next call to poll_block() to wake up when rconn_run() should be | |
636 | * called on 'rc'. */ | |
637 | void | |
638 | rconn_run_wait(struct rconn *rc) | |
9f5e8906 | 639 | OVS_EXCLUDED(rc->mutex) |
064af421 | 640 | { |
60cb3eb8 BP |
641 | unsigned int timeo; |
642 | size_t i; | |
643 | ||
9f5e8906 | 644 | ovs_mutex_lock(&rc->mutex); |
60cb3eb8 BP |
645 | if (rc->vconn) { |
646 | vconn_run_wait(rc->vconn); | |
82c2b79d BP |
647 | if ((rc->state & (S_ACTIVE | S_IDLE)) && !list_is_empty(&rc->txq)) { |
648 | vconn_wait(rc->vconn, WAIT_SEND); | |
649 | } | |
60cb3eb8 BP |
650 | } |
651 | for (i = 0; i < rc->n_monitors; i++) { | |
652 | vconn_run_wait(rc->monitors[i]); | |
f44c5146 | 653 | vconn_recv_wait(rc->monitors[i]); |
60cb3eb8 BP |
654 | } |
655 | ||
656 | timeo = timeout(rc); | |
064af421 | 657 | if (timeo != UINT_MAX) { |
7cf8b266 BP |
658 | long long int expires = sat_add(rc->state_entered, timeo); |
659 | poll_timer_wait_until(expires * 1000); | |
064af421 | 660 | } |
9f5e8906 | 661 | ovs_mutex_unlock(&rc->mutex); |
064af421 BP |
662 | } |
663 | ||
664 | /* Attempts to receive a packet from 'rc'. If successful, returns the packet; | |
665 | * otherwise, returns a null pointer. The caller is responsible for freeing | |
666 | * the packet (with ofpbuf_delete()). */ | |
667 | struct ofpbuf * | |
668 | rconn_recv(struct rconn *rc) | |
9f5e8906 | 669 | OVS_EXCLUDED(rc->mutex) |
064af421 | 670 | { |
9f5e8906 BP |
671 | struct ofpbuf *buffer = NULL; |
672 | ||
673 | ovs_mutex_lock(&rc->mutex); | |
064af421 | 674 | if (rc->state & (S_ACTIVE | S_IDLE)) { |
064af421 BP |
675 | int error = vconn_recv(rc->vconn, &buffer); |
676 | if (!error) { | |
677 | copy_to_monitor(rc, buffer); | |
7778bd15 | 678 | if (rc->probably_admitted || is_admitted_msg(buffer) |
064af421 BP |
679 | || time_now() - rc->last_connected >= 30) { |
680 | rc->probably_admitted = true; | |
681 | rc->last_admitted = time_now(); | |
682 | } | |
133f2dc9 | 683 | rc->last_activity = time_now(); |
064af421 BP |
684 | rc->packets_received++; |
685 | if (rc->state == S_IDLE) { | |
686 | state_transition(rc, S_ACTIVE); | |
687 | } | |
064af421 | 688 | } else if (error != EAGAIN) { |
b97ba90b | 689 | report_error(rc, error); |
064af421 BP |
690 | disconnect(rc, error); |
691 | } | |
692 | } | |
9f5e8906 BP |
693 | ovs_mutex_unlock(&rc->mutex); |
694 | ||
695 | return buffer; | |
064af421 BP |
696 | } |
697 | ||
698 | /* Causes the next call to poll_block() to wake up when a packet may be ready | |
699 | * to be received by vconn_recv() on 'rc'. */ | |
700 | void | |
701 | rconn_recv_wait(struct rconn *rc) | |
9f5e8906 | 702 | OVS_EXCLUDED(rc->mutex) |
064af421 | 703 | { |
9f5e8906 | 704 | ovs_mutex_lock(&rc->mutex); |
064af421 BP |
705 | if (rc->vconn) { |
706 | vconn_wait(rc->vconn, WAIT_RECV); | |
707 | } | |
9f5e8906 | 708 | ovs_mutex_unlock(&rc->mutex); |
064af421 BP |
709 | } |
710 | ||
9f5e8906 BP |
711 | static int |
712 | rconn_send__(struct rconn *rc, struct ofpbuf *b, | |
064af421 | 713 | struct rconn_packet_counter *counter) |
9f5e8906 | 714 | OVS_REQUIRES(rc->mutex) |
064af421 BP |
715 | { |
716 | if (rconn_is_connected(rc)) { | |
717 | COVERAGE_INC(rconn_queued); | |
718 | copy_to_monitor(rc, b); | |
d45e9c65 | 719 | b->private_p = counter; |
064af421 | 720 | if (counter) { |
a6441685 | 721 | rconn_packet_counter_inc(counter, b->size); |
064af421 | 722 | } |
b3907fbc | 723 | list_push_back(&rc->txq, &b->list_node); |
064af421 BP |
724 | |
725 | /* If the queue was empty before we added 'b', try to send some | |
726 | * packets. (But if the queue had packets in it, it's because the | |
727 | * vconn is backlogged and there's no point in stuffing more into it | |
728 | * now. We'll get back to that in rconn_run().) */ | |
b3907fbc | 729 | if (rc->txq.next == &b->list_node) { |
064af421 BP |
730 | try_send(rc); |
731 | } | |
732 | return 0; | |
733 | } else { | |
acb9da40 | 734 | ofpbuf_delete(b); |
064af421 BP |
735 | return ENOTCONN; |
736 | } | |
737 | } | |
738 | ||
9f5e8906 BP |
739 | /* Sends 'b' on 'rc'. Returns 0 if successful, or ENOTCONN if 'rc' is not |
740 | * currently connected. Takes ownership of 'b'. | |
741 | * | |
742 | * If 'counter' is non-null, then 'counter' will be incremented while the | |
743 | * packet is in flight, then decremented when it has been sent (or discarded | |
744 | * due to disconnection). Because 'b' may be sent (or discarded) before this | |
745 | * function returns, the caller may not be able to observe any change in | |
746 | * 'counter'. | |
747 | * | |
748 | * There is no rconn_send_wait() function: an rconn has a send queue that it | |
749 | * takes care of sending if you call rconn_run(), which will have the side | |
750 | * effect of waking up poll_block(). */ | |
751 | int | |
752 | rconn_send(struct rconn *rc, struct ofpbuf *b, | |
753 | struct rconn_packet_counter *counter) | |
754 | OVS_EXCLUDED(rc->mutex) | |
755 | { | |
756 | int error; | |
757 | ||
758 | ovs_mutex_lock(&rc->mutex); | |
759 | error = rconn_send__(rc, b, counter); | |
760 | ovs_mutex_unlock(&rc->mutex); | |
761 | ||
762 | return error; | |
763 | } | |
764 | ||
064af421 BP |
765 | /* Sends 'b' on 'rc'. Increments 'counter' while the packet is in flight; it |
766 | * will be decremented when it has been sent (or discarded due to | |
767 | * disconnection). Returns 0 if successful, EAGAIN if 'counter->n' is already | |
768 | * at least as large as 'queue_limit', or ENOTCONN if 'rc' is not currently | |
769 | * connected. Regardless of return value, 'b' is destroyed. | |
770 | * | |
771 | * Because 'b' may be sent (or discarded) before this function returns, the | |
772 | * caller may not be able to observe any change in 'counter'. | |
773 | * | |
774 | * There is no rconn_send_wait() function: an rconn has a send queue that it | |
775 | * takes care of sending if you call rconn_run(), which will have the side | |
776 | * effect of waking up poll_block(). */ | |
777 | int | |
778 | rconn_send_with_limit(struct rconn *rc, struct ofpbuf *b, | |
779 | struct rconn_packet_counter *counter, int queue_limit) | |
9f5e8906 | 780 | OVS_EXCLUDED(rc->mutex) |
064af421 | 781 | { |
9f5e8906 BP |
782 | int error; |
783 | ||
784 | ovs_mutex_lock(&rc->mutex); | |
a3d1ff00 | 785 | if (rconn_packet_counter_n_packets(counter) < queue_limit) { |
9f5e8906 | 786 | error = rconn_send__(rc, b, counter); |
b8dddecf | 787 | } else { |
064af421 | 788 | COVERAGE_INC(rconn_overflow); |
b8dddecf | 789 | ofpbuf_delete(b); |
9f5e8906 | 790 | error = EAGAIN; |
064af421 | 791 | } |
9f5e8906 BP |
792 | ovs_mutex_unlock(&rc->mutex); |
793 | ||
794 | return error; | |
064af421 BP |
795 | } |
796 | ||
797 | /* Returns the total number of packets successfully sent on the underlying | |
798 | * vconn. A packet is not counted as sent while it is still queued in the | |
799 | * rconn, only when it has been successfuly passed to the vconn. */ | |
800 | unsigned int | |
801 | rconn_packets_sent(const struct rconn *rc) | |
802 | { | |
803 | return rc->packets_sent; | |
804 | } | |
805 | ||
806 | /* Adds 'vconn' to 'rc' as a monitoring connection, to which all messages sent | |
807 | * and received on 'rconn' will be copied. 'rc' takes ownership of 'vconn'. */ | |
808 | void | |
809 | rconn_add_monitor(struct rconn *rc, struct vconn *vconn) | |
9f5e8906 | 810 | OVS_EXCLUDED(rc->mutex) |
064af421 | 811 | { |
9f5e8906 | 812 | ovs_mutex_lock(&rc->mutex); |
064af421 BP |
813 | if (rc->n_monitors < ARRAY_SIZE(rc->monitors)) { |
814 | VLOG_INFO("new monitor connection from %s", vconn_get_name(vconn)); | |
815 | rc->monitors[rc->n_monitors++] = vconn; | |
816 | } else { | |
817 | VLOG_DBG("too many monitor connections, discarding %s", | |
818 | vconn_get_name(vconn)); | |
819 | vconn_close(vconn); | |
820 | } | |
9f5e8906 | 821 | ovs_mutex_unlock(&rc->mutex); |
064af421 BP |
822 | } |
823 | ||
eb15cdbb BP |
824 | /* Returns 'rc''s name. This is a name for human consumption, appropriate for |
825 | * use in log messages. It is not necessarily a name that may be passed | |
826 | * directly to, e.g., vconn_open(). */ | |
064af421 BP |
827 | const char * |
828 | rconn_get_name(const struct rconn *rc) | |
829 | { | |
830 | return rc->name; | |
831 | } | |
832 | ||
eb15cdbb BP |
833 | /* Sets 'rc''s name to 'new_name'. */ |
834 | void | |
835 | rconn_set_name(struct rconn *rc, const char *new_name) | |
9f5e8906 | 836 | OVS_EXCLUDED(rc->mutex) |
eb15cdbb | 837 | { |
9f5e8906 | 838 | ovs_mutex_lock(&rc->mutex); |
eb15cdbb BP |
839 | free(rc->name); |
840 | rc->name = xstrdup(new_name); | |
9f5e8906 | 841 | ovs_mutex_unlock(&rc->mutex); |
eb15cdbb BP |
842 | } |
843 | ||
844 | /* Returns 'rc''s target. This is intended to be a string that may be passed | |
845 | * directly to, e.g., vconn_open(). */ | |
846 | const char * | |
847 | rconn_get_target(const struct rconn *rc) | |
848 | { | |
849 | return rc->target; | |
850 | } | |
851 | ||
064af421 BP |
852 | /* Returns true if 'rconn' is connected or in the process of reconnecting, |
853 | * false if 'rconn' is disconnected and will not reconnect on its own. */ | |
854 | bool | |
855 | rconn_is_alive(const struct rconn *rconn) | |
856 | { | |
857 | return rconn->state != S_VOID; | |
858 | } | |
859 | ||
860 | /* Returns true if 'rconn' is connected, false otherwise. */ | |
861 | bool | |
862 | rconn_is_connected(const struct rconn *rconn) | |
863 | { | |
864 | return is_connected_state(rconn->state); | |
865 | } | |
866 | ||
9f5e8906 BP |
867 | static bool |
868 | rconn_is_admitted__(const struct rconn *rconn) | |
869 | OVS_REQUIRES(rconn->mutex) | |
870 | { | |
871 | return (rconn_is_connected(rconn) | |
872 | && rconn->last_admitted >= rconn->last_connected); | |
873 | } | |
874 | ||
7778bd15 BP |
875 | /* Returns true if 'rconn' is connected and thought to have been accepted by |
876 | * the peer's admission-control policy. */ | |
877 | bool | |
878 | rconn_is_admitted(const struct rconn *rconn) | |
9f5e8906 | 879 | OVS_EXCLUDED(rconn->mutex) |
7778bd15 | 880 | { |
9f5e8906 BP |
881 | bool admitted; |
882 | ||
883 | ovs_mutex_lock(&rconn->mutex); | |
884 | admitted = rconn_is_admitted__(rconn); | |
885 | ovs_mutex_unlock(&rconn->mutex); | |
886 | ||
887 | return admitted; | |
7778bd15 BP |
888 | } |
889 | ||
890 | /* Returns 0 if 'rconn' is currently connected and considered to have been | |
891 | * accepted by the peer's admission-control policy, otherwise the number of | |
892 | * seconds since 'rconn' was last in such a state. */ | |
064af421 BP |
893 | int |
894 | rconn_failure_duration(const struct rconn *rconn) | |
9f5e8906 | 895 | OVS_EXCLUDED(rconn->mutex) |
064af421 | 896 | { |
9f5e8906 BP |
897 | int duration; |
898 | ||
899 | ovs_mutex_lock(&rconn->mutex); | |
900 | duration = (rconn_is_admitted__(rconn) | |
901 | ? 0 | |
902 | : time_now() - rconn->last_admitted); | |
903 | ovs_mutex_unlock(&rconn->mutex); | |
904 | ||
905 | return duration; | |
064af421 BP |
906 | } |
907 | ||
9f5e8906 BP |
908 | static int |
909 | rconn_get_version__(const struct rconn *rconn) | |
910 | OVS_REQUIRES(rconn->mutex) | |
911 | { | |
912 | return rconn->vconn ? vconn_get_version(rconn->vconn) : -1; | |
064af421 BP |
913 | } |
914 | ||
27527aa0 BP |
915 | /* Returns the OpenFlow version negotiated with the peer, or -1 if there is |
916 | * currently no connection or if version negotiation is not yet complete. */ | |
917 | int | |
918 | rconn_get_version(const struct rconn *rconn) | |
9f5e8906 | 919 | OVS_EXCLUDED(rconn->mutex) |
27527aa0 | 920 | { |
9f5e8906 BP |
921 | int version; |
922 | ||
923 | ovs_mutex_lock(&rconn->mutex); | |
924 | version = rconn_get_version__(rconn); | |
925 | ovs_mutex_unlock(&rconn->mutex); | |
926 | ||
927 | return version; | |
27527aa0 BP |
928 | } |
929 | ||
064af421 BP |
930 | /* Returns the total number of packets successfully received by the underlying |
931 | * vconn. */ | |
932 | unsigned int | |
933 | rconn_packets_received(const struct rconn *rc) | |
934 | { | |
935 | return rc->packets_received; | |
936 | } | |
937 | ||
938 | /* Returns a string representing the internal state of 'rc'. The caller must | |
939 | * not modify or free the string. */ | |
940 | const char * | |
941 | rconn_get_state(const struct rconn *rc) | |
942 | { | |
943 | return state_name(rc->state); | |
944 | } | |
945 | ||
064af421 | 946 | /* Returns the time at which the last successful connection was made by |
2cdcb898 | 947 | * 'rc'. Returns TIME_MIN if never connected. */ |
064af421 BP |
948 | time_t |
949 | rconn_get_last_connection(const struct rconn *rc) | |
950 | { | |
951 | return rc->last_connected; | |
952 | } | |
953 | ||
2cdcb898 AE |
954 | /* Returns the time at which 'rc' was last disconnected. Returns TIME_MIN |
955 | * if never disconnected. */ | |
956 | time_t | |
957 | rconn_get_last_disconnect(const struct rconn *rc) | |
958 | { | |
959 | return rc->last_disconnected; | |
960 | } | |
961 | ||
064af421 BP |
962 | /* Returns 'rc''s current connection sequence number, a number that changes |
963 | * every time that 'rconn' connects or disconnects. */ | |
964 | unsigned int | |
965 | rconn_get_connection_seqno(const struct rconn *rc) | |
966 | { | |
967 | return rc->seqno; | |
968 | } | |
88a20d6e BP |
969 | |
970 | /* Returns a value that explains why 'rc' last disconnected: | |
971 | * | |
972 | * - 0 means that the last disconnection was caused by a call to | |
973 | * rconn_disconnect(), or that 'rc' is new and has not yet completed its | |
974 | * initial connection or connection attempt. | |
975 | * | |
976 | * - EOF means that the connection was closed in the normal way by the peer. | |
977 | * | |
978 | * - A positive integer is an errno value that represents the error. | |
979 | */ | |
980 | int | |
981 | rconn_get_last_error(const struct rconn *rc) | |
982 | { | |
983 | return rc->last_error; | |
984 | } | |
0d085684 BP |
985 | |
986 | /* Returns the number of messages queued for transmission on 'rc'. */ | |
987 | unsigned int | |
988 | rconn_count_txqlen(const struct rconn *rc) | |
9f5e8906 | 989 | OVS_EXCLUDED(rc->mutex) |
0d085684 | 990 | { |
9f5e8906 BP |
991 | unsigned int len; |
992 | ||
993 | ovs_mutex_lock(&rc->mutex); | |
994 | len = list_size(&rc->txq); | |
995 | ovs_mutex_unlock(&rc->mutex); | |
996 | ||
997 | return len; | |
0d085684 | 998 | } |
064af421 BP |
999 | \f |
1000 | struct rconn_packet_counter * | |
1001 | rconn_packet_counter_create(void) | |
1002 | { | |
a6441685 | 1003 | struct rconn_packet_counter *c = xzalloc(sizeof *c); |
a3d1ff00 BP |
1004 | ovs_mutex_init(&c->mutex); |
1005 | ovs_mutex_lock(&c->mutex); | |
064af421 | 1006 | c->ref_cnt = 1; |
a3d1ff00 | 1007 | ovs_mutex_unlock(&c->mutex); |
064af421 BP |
1008 | return c; |
1009 | } | |
1010 | ||
1011 | void | |
1012 | rconn_packet_counter_destroy(struct rconn_packet_counter *c) | |
1013 | { | |
1014 | if (c) { | |
a3d1ff00 BP |
1015 | bool dead; |
1016 | ||
1017 | ovs_mutex_lock(&c->mutex); | |
cb22974d | 1018 | ovs_assert(c->ref_cnt > 0); |
a3d1ff00 BP |
1019 | dead = !--c->ref_cnt && !c->n_packets; |
1020 | ovs_mutex_unlock(&c->mutex); | |
1021 | ||
1022 | if (dead) { | |
1023 | ovs_mutex_destroy(&c->mutex); | |
064af421 BP |
1024 | free(c); |
1025 | } | |
1026 | } | |
1027 | } | |
1028 | ||
1029 | void | |
a6441685 | 1030 | rconn_packet_counter_inc(struct rconn_packet_counter *c, unsigned int n_bytes) |
064af421 | 1031 | { |
a3d1ff00 | 1032 | ovs_mutex_lock(&c->mutex); |
a6441685 BP |
1033 | c->n_packets++; |
1034 | c->n_bytes += n_bytes; | |
a3d1ff00 | 1035 | ovs_mutex_unlock(&c->mutex); |
064af421 BP |
1036 | } |
1037 | ||
1038 | void | |
a6441685 | 1039 | rconn_packet_counter_dec(struct rconn_packet_counter *c, unsigned int n_bytes) |
064af421 | 1040 | { |
a3d1ff00 | 1041 | bool dead = false; |
a6441685 | 1042 | |
a3d1ff00 BP |
1043 | ovs_mutex_lock(&c->mutex); |
1044 | ovs_assert(c->n_packets > 0); | |
1045 | ovs_assert(c->n_packets == 1 | |
1046 | ? c->n_bytes == n_bytes | |
1047 | : c->n_bytes > n_bytes); | |
a6441685 | 1048 | c->n_packets--; |
a3d1ff00 BP |
1049 | c->n_bytes -= n_bytes; |
1050 | dead = !c->n_packets && !c->ref_cnt; | |
1051 | ovs_mutex_unlock(&c->mutex); | |
1052 | ||
1053 | if (dead) { | |
1054 | ovs_mutex_destroy(&c->mutex); | |
1055 | free(c); | |
064af421 BP |
1056 | } |
1057 | } | |
a3d1ff00 BP |
1058 | |
1059 | unsigned int | |
1060 | rconn_packet_counter_n_packets(const struct rconn_packet_counter *c) | |
1061 | { | |
1062 | unsigned int n; | |
1063 | ||
1064 | ovs_mutex_lock(&c->mutex); | |
1065 | n = c->n_packets; | |
1066 | ovs_mutex_unlock(&c->mutex); | |
1067 | ||
1068 | return n; | |
1069 | } | |
1070 | ||
1071 | unsigned int | |
1072 | rconn_packet_counter_n_bytes(const struct rconn_packet_counter *c) | |
1073 | { | |
1074 | unsigned int n; | |
1075 | ||
1076 | ovs_mutex_lock(&c->mutex); | |
1077 | n = c->n_bytes; | |
1078 | ovs_mutex_unlock(&c->mutex); | |
1079 | ||
1080 | return n; | |
1081 | } | |
064af421 | 1082 | \f |
eb15cdbb BP |
1083 | /* Set rc->target and rc->name to 'target' and 'name', respectively. If 'name' |
1084 | * is null, 'target' is used. | |
1085 | * | |
1086 | * Also, clear out the cached IP address and port information, since changing | |
1087 | * the target also likely changes these values. */ | |
19d1ab55 | 1088 | static void |
eb15cdbb | 1089 | rconn_set_target__(struct rconn *rc, const char *target, const char *name) |
9f5e8906 | 1090 | OVS_REQUIRES(rc->mutex) |
19d1ab55 BP |
1091 | { |
1092 | free(rc->name); | |
eb15cdbb BP |
1093 | rc->name = xstrdup(name ? name : target); |
1094 | free(rc->target); | |
1095 | rc->target = xstrdup(target); | |
19d1ab55 BP |
1096 | } |
1097 | ||
064af421 BP |
1098 | /* Tries to send a packet from 'rc''s send buffer. Returns 0 if successful, |
1099 | * otherwise a positive errno value. */ | |
1100 | static int | |
1101 | try_send(struct rconn *rc) | |
9f5e8906 | 1102 | OVS_REQUIRES(rc->mutex) |
064af421 | 1103 | { |
b3907fbc | 1104 | struct ofpbuf *msg = ofpbuf_from_list(rc->txq.next); |
a6441685 | 1105 | unsigned int n_bytes = msg->size; |
b3907fbc BP |
1106 | struct rconn_packet_counter *counter = msg->private_p; |
1107 | int retval; | |
1108 | ||
1109 | /* Eagerly remove 'msg' from the txq. We can't remove it from the list | |
1110 | * after sending, if sending is successful, because it is then owned by the | |
1111 | * vconn, which might have freed it already. */ | |
1112 | list_remove(&msg->list_node); | |
1113 | ||
1114 | retval = vconn_send(rc->vconn, msg); | |
064af421 | 1115 | if (retval) { |
b3907fbc | 1116 | list_push_front(&rc->txq, &msg->list_node); |
064af421 | 1117 | if (retval != EAGAIN) { |
b97ba90b | 1118 | report_error(rc, retval); |
064af421 BP |
1119 | disconnect(rc, retval); |
1120 | } | |
1121 | return retval; | |
1122 | } | |
1123 | COVERAGE_INC(rconn_sent); | |
1124 | rc->packets_sent++; | |
1125 | if (counter) { | |
a6441685 | 1126 | rconn_packet_counter_dec(counter, n_bytes); |
064af421 | 1127 | } |
064af421 BP |
1128 | return 0; |
1129 | } | |
1130 | ||
b97ba90b BP |
1131 | /* Reports that 'error' caused 'rc' to disconnect. 'error' may be a positive |
1132 | * errno value, or it may be EOF to indicate that the connection was closed | |
1133 | * normally. */ | |
064af421 | 1134 | static void |
b97ba90b | 1135 | report_error(struct rconn *rc, int error) |
9f5e8906 | 1136 | OVS_REQUIRES(rc->mutex) |
b97ba90b BP |
1137 | { |
1138 | if (error == EOF) { | |
1139 | /* If 'rc' isn't reliable, then we don't really expect this connection | |
1140 | * to last forever anyway (probably it's a connection that we received | |
1141 | * via accept()), so use DBG level to avoid cluttering the logs. */ | |
1142 | enum vlog_level level = rc->reliable ? VLL_INFO : VLL_DBG; | |
1143 | VLOG(level, "%s: connection closed by peer", rc->name); | |
1144 | } else { | |
10a89ef0 BP |
1145 | VLOG_WARN("%s: connection dropped (%s)", |
1146 | rc->name, ovs_strerror(error)); | |
b97ba90b BP |
1147 | } |
1148 | } | |
1149 | ||
88a20d6e BP |
1150 | /* Disconnects 'rc' and records 'error' as the error that caused 'rc''s last |
1151 | * disconnection: | |
1152 | * | |
1153 | * - 0 means that this disconnection is due to a request by 'rc''s client, | |
1154 | * not due to any kind of network error. | |
1155 | * | |
1156 | * - EOF means that the connection was closed in the normal way by the peer. | |
1157 | * | |
1158 | * - A positive integer is an errno value that represents the error. | |
1159 | */ | |
064af421 BP |
1160 | static void |
1161 | disconnect(struct rconn *rc, int error) | |
9f5e8906 | 1162 | OVS_REQUIRES(rc->mutex) |
064af421 | 1163 | { |
88a20d6e | 1164 | rc->last_error = error; |
064af421 BP |
1165 | if (rc->reliable) { |
1166 | time_t now = time_now(); | |
1167 | ||
1168 | if (rc->state & (S_CONNECTING | S_ACTIVE | S_IDLE)) { | |
2cdcb898 | 1169 | rc->last_disconnected = now; |
064af421 BP |
1170 | vconn_close(rc->vconn); |
1171 | rc->vconn = NULL; | |
1172 | flush_queue(rc); | |
1173 | } | |
1174 | ||
1175 | if (now >= rc->backoff_deadline) { | |
1176 | rc->backoff = 1; | |
07c8c80d BP |
1177 | } else if (rc->backoff < rc->max_backoff / 2) { |
1178 | rc->backoff = MAX(1, 2 * rc->backoff); | |
46816c34 | 1179 | VLOG_INFO("%s: waiting %d seconds before reconnect", |
064af421 | 1180 | rc->name, rc->backoff); |
07c8c80d BP |
1181 | } else { |
1182 | if (rconn_logging_connection_attempts__(rc)) { | |
1183 | VLOG_INFO("%s: continuing to retry connections in the " | |
1184 | "background but suppressing further logging", | |
1185 | rc->name); | |
1186 | } | |
1187 | rc->backoff = rc->max_backoff; | |
064af421 BP |
1188 | } |
1189 | rc->backoff_deadline = now + rc->backoff; | |
1190 | state_transition(rc, S_BACKOFF); | |
064af421 | 1191 | } else { |
2cdcb898 | 1192 | rc->last_disconnected = time_now(); |
9f5e8906 | 1193 | rconn_disconnect__(rc); |
064af421 BP |
1194 | } |
1195 | } | |
1196 | ||
1197 | /* Drops all the packets from 'rc''s send queue and decrements their queue | |
1198 | * counts. */ | |
1199 | static void | |
1200 | flush_queue(struct rconn *rc) | |
9f5e8906 | 1201 | OVS_REQUIRES(rc->mutex) |
064af421 | 1202 | { |
b3907fbc | 1203 | if (list_is_empty(&rc->txq)) { |
064af421 BP |
1204 | return; |
1205 | } | |
b3907fbc BP |
1206 | while (!list_is_empty(&rc->txq)) { |
1207 | struct ofpbuf *b = ofpbuf_from_list(list_pop_front(&rc->txq)); | |
d45e9c65 | 1208 | struct rconn_packet_counter *counter = b->private_p; |
064af421 | 1209 | if (counter) { |
a6441685 | 1210 | rconn_packet_counter_dec(counter, b->size); |
064af421 BP |
1211 | } |
1212 | COVERAGE_INC(rconn_discarded); | |
1213 | ofpbuf_delete(b); | |
1214 | } | |
1215 | poll_immediate_wake(); | |
1216 | } | |
1217 | ||
1218 | static unsigned int | |
1219 | elapsed_in_this_state(const struct rconn *rc) | |
9f5e8906 | 1220 | OVS_REQUIRES(rc->mutex) |
064af421 BP |
1221 | { |
1222 | return time_now() - rc->state_entered; | |
1223 | } | |
1224 | ||
1225 | static unsigned int | |
1226 | timeout(const struct rconn *rc) | |
9f5e8906 | 1227 | OVS_REQUIRES(rc->mutex) |
064af421 BP |
1228 | { |
1229 | switch (rc->state) { | |
1230 | #define STATE(NAME, VALUE) case S_##NAME: return timeout_##NAME(rc); | |
1231 | STATES | |
1232 | #undef STATE | |
1233 | default: | |
428b2edd | 1234 | OVS_NOT_REACHED(); |
064af421 BP |
1235 | } |
1236 | } | |
1237 | ||
1238 | static bool | |
1239 | timed_out(const struct rconn *rc) | |
9f5e8906 | 1240 | OVS_REQUIRES(rc->mutex) |
064af421 BP |
1241 | { |
1242 | return time_now() >= sat_add(rc->state_entered, timeout(rc)); | |
1243 | } | |
1244 | ||
1245 | static void | |
1246 | state_transition(struct rconn *rc, enum state state) | |
9f5e8906 | 1247 | OVS_REQUIRES(rc->mutex) |
064af421 BP |
1248 | { |
1249 | rc->seqno += (rc->state == S_ACTIVE) != (state == S_ACTIVE); | |
1250 | if (is_connected_state(state) && !is_connected_state(rc->state)) { | |
1251 | rc->probably_admitted = false; | |
1252 | } | |
1253 | if (rconn_is_connected(rc)) { | |
1254 | rc->total_time_connected += elapsed_in_this_state(rc); | |
1255 | } | |
1256 | VLOG_DBG("%s: entering %s", rc->name, state_name(state)); | |
1257 | rc->state = state; | |
1258 | rc->state_entered = time_now(); | |
1259 | } | |
1260 | ||
5ac7c4dc BP |
1261 | static void |
1262 | close_monitor(struct rconn *rc, size_t idx, int retval) | |
9f5e8906 | 1263 | OVS_REQUIRES(rc->mutex) |
5ac7c4dc BP |
1264 | { |
1265 | VLOG_DBG("%s: closing monitor connection to %s: %s", | |
1266 | rconn_get_name(rc), vconn_get_name(rc->monitors[idx]), | |
1267 | ovs_retval_to_string(retval)); | |
1268 | rc->monitors[idx] = rc->monitors[--rc->n_monitors]; | |
1269 | } | |
1270 | ||
064af421 BP |
1271 | static void |
1272 | copy_to_monitor(struct rconn *rc, const struct ofpbuf *b) | |
9f5e8906 | 1273 | OVS_REQUIRES(rc->mutex) |
064af421 BP |
1274 | { |
1275 | struct ofpbuf *clone = NULL; | |
1276 | int retval; | |
1277 | size_t i; | |
1278 | ||
1279 | for (i = 0; i < rc->n_monitors; ) { | |
1280 | struct vconn *vconn = rc->monitors[i]; | |
1281 | ||
1282 | if (!clone) { | |
1283 | clone = ofpbuf_clone(b); | |
1284 | } | |
1285 | retval = vconn_send(vconn, clone); | |
1286 | if (!retval) { | |
1287 | clone = NULL; | |
1288 | } else if (retval != EAGAIN) { | |
5ac7c4dc | 1289 | close_monitor(rc, i, retval); |
064af421 BP |
1290 | continue; |
1291 | } | |
1292 | i++; | |
1293 | } | |
1294 | ofpbuf_delete(clone); | |
1295 | } | |
1296 | ||
1297 | static bool | |
d295e8e9 | 1298 | is_connected_state(enum state state) |
064af421 BP |
1299 | { |
1300 | return (state & (S_ACTIVE | S_IDLE)) != 0; | |
1301 | } | |
1302 | ||
81196254 BP |
1303 | /* When a switch initially connects to a controller, the controller may spend a |
1304 | * little time examining the switch, looking at, for example, its datapath ID, | |
1305 | * before it decides whether it is willing to control that switch. At that | |
1306 | * point, it either disconnects or starts controlling the switch. | |
1307 | * | |
1308 | * This function returns a guess to its caller about whether 'b' is OpenFlow | |
1309 | * message that indicates that the controller has decided to control the | |
1310 | * switch. It returns false if the message is one that a controller typically | |
1311 | * uses to determine whether a switch is admissible, true if the message is one | |
1312 | * that would typically be used only after the controller has admitted the | |
1313 | * switch. */ | |
064af421 BP |
1314 | static bool |
1315 | is_admitted_msg(const struct ofpbuf *b) | |
1316 | { | |
982697a4 BP |
1317 | enum ofptype type; |
1318 | enum ofperr error; | |
1319 | ||
1320 | error = ofptype_decode(&type, b->data); | |
1321 | if (error) { | |
1322 | return false; | |
1323 | } | |
1324 | ||
1325 | switch (type) { | |
1326 | case OFPTYPE_HELLO: | |
1327 | case OFPTYPE_ERROR: | |
1328 | case OFPTYPE_ECHO_REQUEST: | |
1329 | case OFPTYPE_ECHO_REPLY: | |
1330 | case OFPTYPE_FEATURES_REQUEST: | |
1331 | case OFPTYPE_FEATURES_REPLY: | |
1332 | case OFPTYPE_GET_CONFIG_REQUEST: | |
1333 | case OFPTYPE_GET_CONFIG_REPLY: | |
1334 | case OFPTYPE_SET_CONFIG: | |
c545d38d JR |
1335 | case OFPTYPE_QUEUE_GET_CONFIG_REQUEST: |
1336 | case OFPTYPE_QUEUE_GET_CONFIG_REPLY: | |
2e1ae200 JR |
1337 | case OFPTYPE_GET_ASYNC_REQUEST: |
1338 | case OFPTYPE_GET_ASYNC_REPLY: | |
261bd854 BP |
1339 | case OFPTYPE_GROUP_STATS_REQUEST: |
1340 | case OFPTYPE_GROUP_STATS_REPLY: | |
1341 | case OFPTYPE_GROUP_DESC_STATS_REQUEST: | |
1342 | case OFPTYPE_GROUP_DESC_STATS_REPLY: | |
1343 | case OFPTYPE_GROUP_FEATURES_STATS_REQUEST: | |
1344 | case OFPTYPE_GROUP_FEATURES_STATS_REPLY: | |
1345 | case OFPTYPE_TABLE_FEATURES_STATS_REQUEST: | |
1346 | case OFPTYPE_TABLE_FEATURES_STATS_REPLY: | |
982697a4 BP |
1347 | return false; |
1348 | ||
1349 | case OFPTYPE_PACKET_IN: | |
1350 | case OFPTYPE_FLOW_REMOVED: | |
1351 | case OFPTYPE_PORT_STATUS: | |
1352 | case OFPTYPE_PACKET_OUT: | |
1353 | case OFPTYPE_FLOW_MOD: | |
7395c052 | 1354 | case OFPTYPE_GROUP_MOD: |
982697a4 | 1355 | case OFPTYPE_PORT_MOD: |
918f2b82 | 1356 | case OFPTYPE_TABLE_MOD: |
9cae45dc | 1357 | case OFPTYPE_METER_MOD: |
982697a4 BP |
1358 | case OFPTYPE_BARRIER_REQUEST: |
1359 | case OFPTYPE_BARRIER_REPLY: | |
1360 | case OFPTYPE_DESC_STATS_REQUEST: | |
1361 | case OFPTYPE_DESC_STATS_REPLY: | |
1362 | case OFPTYPE_FLOW_STATS_REQUEST: | |
1363 | case OFPTYPE_FLOW_STATS_REPLY: | |
1364 | case OFPTYPE_AGGREGATE_STATS_REQUEST: | |
1365 | case OFPTYPE_AGGREGATE_STATS_REPLY: | |
1366 | case OFPTYPE_TABLE_STATS_REQUEST: | |
1367 | case OFPTYPE_TABLE_STATS_REPLY: | |
1368 | case OFPTYPE_PORT_STATS_REQUEST: | |
1369 | case OFPTYPE_PORT_STATS_REPLY: | |
1370 | case OFPTYPE_QUEUE_STATS_REQUEST: | |
1371 | case OFPTYPE_QUEUE_STATS_REPLY: | |
1372 | case OFPTYPE_PORT_DESC_STATS_REQUEST: | |
1373 | case OFPTYPE_PORT_DESC_STATS_REPLY: | |
261bd854 BP |
1374 | case OFPTYPE_METER_STATS_REQUEST: |
1375 | case OFPTYPE_METER_STATS_REPLY: | |
1376 | case OFPTYPE_METER_CONFIG_STATS_REQUEST: | |
1377 | case OFPTYPE_METER_CONFIG_STATS_REPLY: | |
1378 | case OFPTYPE_METER_FEATURES_STATS_REQUEST: | |
1379 | case OFPTYPE_METER_FEATURES_STATS_REPLY: | |
982697a4 BP |
1380 | case OFPTYPE_ROLE_REQUEST: |
1381 | case OFPTYPE_ROLE_REPLY: | |
252f3411 | 1382 | case OFPTYPE_ROLE_STATUS: |
982697a4 BP |
1383 | case OFPTYPE_SET_FLOW_FORMAT: |
1384 | case OFPTYPE_FLOW_MOD_TABLE_ID: | |
1385 | case OFPTYPE_SET_PACKET_IN_FORMAT: | |
1386 | case OFPTYPE_FLOW_AGE: | |
1387 | case OFPTYPE_SET_ASYNC_CONFIG: | |
1388 | case OFPTYPE_SET_CONTROLLER_ID: | |
1389 | case OFPTYPE_FLOW_MONITOR_STATS_REQUEST: | |
1390 | case OFPTYPE_FLOW_MONITOR_STATS_REPLY: | |
1391 | case OFPTYPE_FLOW_MONITOR_CANCEL: | |
1392 | case OFPTYPE_FLOW_MONITOR_PAUSED: | |
1393 | case OFPTYPE_FLOW_MONITOR_RESUMED: | |
1394 | default: | |
1395 | return true; | |
1396 | } | |
064af421 | 1397 | } |
07c8c80d BP |
1398 | |
1399 | /* Returns true if 'rc' is currently logging information about connection | |
1400 | * attempts, false if logging should be suppressed because 'rc' hasn't | |
1401 | * successuflly connected in too long. */ | |
1402 | static bool | |
1403 | rconn_logging_connection_attempts__(const struct rconn *rc) | |
9f5e8906 | 1404 | OVS_REQUIRES(rc->mutex) |
07c8c80d BP |
1405 | { |
1406 | return rc->backoff < rc->max_backoff; | |
1407 | } |