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