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