2 * Copyright (c) 2009, 2010 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "reconnect.h"
26 #include "command-line.h"
32 static struct reconnect *reconnect;
35 static const struct command commands[];
37 static void diff_stats(const struct reconnect_stats *old,
38 const struct reconnect_stats *new);
43 extern struct vlog_module VLM_reconnect;
44 struct reconnect_stats prev;
45 unsigned int old_max_tries;
49 vlog_set_levels(&VLM_reconnect, VLF_ANY_FACILITY, VLL_EMER);
52 reconnect = reconnect_create(now);
53 reconnect_set_name(reconnect, "remote");
54 reconnect_get_stats(reconnect, now, &prev);
55 printf("### t=%d ###\n", now);
57 old_max_tries = reconnect_get_max_tries(reconnect);
58 while (fgets(line, sizeof line, stdin)) {
59 struct reconnect_stats cur;
68 svec_parse_words(&args, line);
69 svec_terminate(&args);
70 if (!svec_is_empty(&args)) {
71 run_command(args.n, args.names, commands);
75 if (old_time != now) {
76 printf("\n### t=%d ###\n", now);
80 reconnect_get_stats(reconnect, now, &cur);
81 diff_stats(&prev, &cur);
83 if (reconnect_get_max_tries(reconnect) != old_max_tries) {
84 old_max_tries = reconnect_get_max_tries(reconnect);
85 printf(" %u tries left\n", old_max_tries);
93 do_enable(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
95 reconnect_enable(reconnect, now);
99 do_disable(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
101 reconnect_disable(reconnect, now);
105 do_force_reconnect(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
107 reconnect_force_reconnect(reconnect, now);
111 error_from_string(const char *s)
115 } else if (!strcmp(s, "ECONNREFUSED")) {
117 } else if (!strcmp(s, "EOF")) {
120 ovs_fatal(0, "unknown error '%s'", s);
125 do_disconnected(int argc OVS_UNUSED, char *argv[])
127 reconnect_disconnected(reconnect, now, error_from_string(argv[1]));
131 do_connecting(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
133 reconnect_connecting(reconnect, now);
137 do_connect_failed(int argc OVS_UNUSED, char *argv[])
139 reconnect_connect_failed(reconnect, now, error_from_string(argv[1]));
143 do_connected(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
145 reconnect_connected(reconnect, now);
149 do_received(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
151 reconnect_received(reconnect, now);
155 do_run(int argc, char *argv[])
157 enum reconnect_action action;
160 now += atoi(argv[1]);
163 action = reconnect_run(reconnect, now);
171 case RECONNECT_CONNECT:
172 printf(" should connect\n");
175 case RECONNECT_DISCONNECT:
176 printf(" should disconnect\n");
179 case RECONNECT_PROBE:
180 printf(" should send probe\n");
186 do_advance(int argc OVS_UNUSED, char *argv[])
188 now += atoi(argv[1]);
192 do_timeout(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
194 int timeout = reconnect_timeout(reconnect, now);
196 printf(" advance %d ms\n", timeout);
199 printf(" no timeout\n");
204 do_set_max_tries(int argc OVS_UNUSED, char *argv[])
206 reconnect_set_max_tries(reconnect, atoi(argv[1]));
210 diff_stats(const struct reconnect_stats *old,
211 const struct reconnect_stats *new)
213 if (old->state != new->state
214 || old->state_elapsed != new->state_elapsed
215 || old->backoff != new->backoff) {
216 printf(" in %s for %u ms (%d ms backoff)\n",
217 new->state, new->state_elapsed, new->backoff);
219 if (old->creation_time != new->creation_time
220 || old->last_received != new->last_received
221 || old->last_connected != new->last_connected) {
222 printf(" created %lld, last received %lld, last connected %lld\n",
223 new->creation_time, new->last_received, new->last_connected);
225 if (old->n_successful_connections != new->n_successful_connections
226 || old->n_attempted_connections != new->n_attempted_connections
227 || old->seqno != new->seqno) {
228 printf(" %u successful connections out of %u attempts, seqno %u\n",
229 new->n_successful_connections, new->n_attempted_connections,
232 if (old->is_connected != new->is_connected
233 || old->current_connection_duration != new->current_connection_duration
234 || old->total_connected_duration != new->total_connected_duration) {
235 printf(" %sconnected (%u ms), total %u ms connected\n",
236 new->is_connected ? "" : "not ",
237 new->current_connection_duration,
238 new->total_connected_duration);
240 if (old->last_disconnected != new->last_disconnected) {
241 printf(" disconnected at %llu ms (%u ms ago)\n",
242 new->last_disconnected, new->current_disconnect_duration);
244 if (old->current_disconnect_duration != new->current_disconnect_duration) {
245 printf(" disconnected for %u ms\n", new->current_disconnect_duration);
250 do_set_passive(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
252 reconnect_set_passive(reconnect, true, now);
256 do_listening(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
258 reconnect_listening(reconnect, now);
262 do_listen_error(int argc OVS_UNUSED, char *argv[])
264 reconnect_listen_error(reconnect, now, atoi(argv[1]));
267 static const struct command commands[] = {
268 { "enable", 0, 0, do_enable },
269 { "disable", 0, 0, do_disable },
270 { "force-reconnect", 0, 0, do_force_reconnect },
271 { "disconnected", 0, 1, do_disconnected },
272 { "connecting", 0, 0, do_connecting },
273 { "connect-failed", 0, 1, do_connect_failed },
274 { "connected", 0, 0, do_connected },
275 { "received", 0, 0, do_received },
276 { "run", 0, 1, do_run },
277 { "advance", 1, 1, do_advance },
278 { "timeout", 0, 0, do_timeout },
279 { "set-max-tries", 1, 1, do_set_max_tries },
280 { "passive", 0, 0, do_set_passive },
281 { "listening", 0, 0, do_listening },
282 { "listen-error", 1, 1, do_listen_error },
283 { NULL, 0, 0, NULL },