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 struct reconnect_stats prev;
44 unsigned int old_max_tries;
48 vlog_set_levels(VLM_reconnect, VLF_ANY_FACILITY, VLL_EMER);
51 reconnect = reconnect_create(now);
52 reconnect_set_name(reconnect, "remote");
53 reconnect_get_stats(reconnect, now, &prev);
54 printf("### t=%d ###\n", now);
56 old_max_tries = reconnect_get_max_tries(reconnect);
57 while (fgets(line, sizeof line, stdin)) {
58 struct reconnect_stats cur;
67 svec_parse_words(&args, line);
68 svec_terminate(&args);
69 if (!svec_is_empty(&args)) {
70 run_command(args.n, args.names, commands);
74 if (old_time != now) {
75 printf("\n### t=%d ###\n", now);
79 reconnect_get_stats(reconnect, now, &cur);
80 diff_stats(&prev, &cur);
82 if (reconnect_get_max_tries(reconnect) != old_max_tries) {
83 old_max_tries = reconnect_get_max_tries(reconnect);
84 printf(" %u tries left\n", old_max_tries);
92 do_enable(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
94 reconnect_enable(reconnect, now);
98 do_disable(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
100 reconnect_disable(reconnect, now);
104 do_force_reconnect(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
106 reconnect_force_reconnect(reconnect, now);
110 error_from_string(const char *s)
114 } else if (!strcmp(s, "ECONNREFUSED")) {
116 } else if (!strcmp(s, "EOF")) {
119 ovs_fatal(0, "unknown error '%s'", s);
124 do_disconnected(int argc OVS_UNUSED, char *argv[])
126 reconnect_disconnected(reconnect, now, error_from_string(argv[1]));
130 do_connecting(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
132 reconnect_connecting(reconnect, now);
136 do_connect_failed(int argc OVS_UNUSED, char *argv[])
138 reconnect_connect_failed(reconnect, now, error_from_string(argv[1]));
142 do_connected(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
144 reconnect_connected(reconnect, now);
148 do_received(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
150 reconnect_received(reconnect, now);
154 do_run(int argc, char *argv[])
156 enum reconnect_action action;
159 now += atoi(argv[1]);
162 action = reconnect_run(reconnect, now);
170 case RECONNECT_CONNECT:
171 printf(" should connect\n");
174 case RECONNECT_DISCONNECT:
175 printf(" should disconnect\n");
178 case RECONNECT_PROBE:
179 printf(" should send probe\n");
185 do_advance(int argc OVS_UNUSED, char *argv[])
187 now += atoi(argv[1]);
191 do_timeout(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
193 int timeout = reconnect_timeout(reconnect, now);
195 printf(" advance %d ms\n", timeout);
198 printf(" no timeout\n");
203 do_set_max_tries(int argc OVS_UNUSED, char *argv[])
205 reconnect_set_max_tries(reconnect, atoi(argv[1]));
209 diff_stats(const struct reconnect_stats *old,
210 const struct reconnect_stats *new)
212 if (old->state != new->state
213 || old->state_elapsed != new->state_elapsed
214 || old->backoff != new->backoff) {
215 printf(" in %s for %u ms (%d ms backoff)\n",
216 new->state, new->state_elapsed, new->backoff);
218 if (old->creation_time != new->creation_time
219 || old->last_received != new->last_received
220 || old->last_connected != new->last_connected) {
221 printf(" created %lld, last received %lld, last connected %lld\n",
222 new->creation_time, new->last_received, new->last_connected);
224 if (old->n_successful_connections != new->n_successful_connections
225 || old->n_attempted_connections != new->n_attempted_connections
226 || old->seqno != new->seqno) {
227 printf(" %u successful connections out of %u attempts, seqno %u\n",
228 new->n_successful_connections, new->n_attempted_connections,
231 if (old->is_connected != new->is_connected
232 || old->current_connection_duration != new->current_connection_duration
233 || old->total_connected_duration != new->total_connected_duration) {
234 printf(" %sconnected (%u ms), total %u ms connected\n",
235 new->is_connected ? "" : "not ",
236 new->current_connection_duration,
237 new->total_connected_duration);
242 do_set_passive(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
244 reconnect_set_passive(reconnect, true, now);
248 do_listening(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
250 reconnect_listening(reconnect, now);
254 do_listen_error(int argc OVS_UNUSED, char *argv[])
256 reconnect_listen_error(reconnect, now, atoi(argv[1]));
259 static const struct command commands[] = {
260 { "enable", 0, 0, do_enable },
261 { "disable", 0, 0, do_disable },
262 { "force-reconnect", 0, 0, do_force_reconnect },
263 { "disconnected", 0, 1, do_disconnected },
264 { "connecting", 0, 0, do_connecting },
265 { "connect-failed", 0, 1, do_connect_failed },
266 { "connected", 0, 0, do_connected },
267 { "received", 0, 0, do_received },
268 { "run", 0, 1, do_run },
269 { "advance", 1, 1, do_advance },
270 { "timeout", 0, 0, do_timeout },
271 { "set-max-tries", 1, 1, do_set_max_tries },
272 { "passive", 0, 0, do_set_passive },
273 { "listening", 0, 0, do_listening },
274 { "listen-error", 1, 1, do_listen_error },
275 { NULL, 0, 0, NULL },