2 * Copyright (c) 2009 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"
31 static struct reconnect *reconnect;
34 static const struct command commands[];
36 static void diff_stats(const struct reconnect_stats *old,
37 const struct reconnect_stats *new);
42 struct reconnect_stats prev;
47 reconnect = reconnect_create(now);
48 reconnect_set_name(reconnect, "remote");
49 reconnect_get_stats(reconnect, now, &prev);
50 printf("### t=%d ###\n", now);
52 while (fgets(line, sizeof line, stdin)) {
53 struct reconnect_stats cur;
62 svec_parse_words(&args, line);
63 svec_terminate(&args);
64 if (!svec_is_empty(&args)) {
65 run_command(args.n, args.names, commands);
69 if (old_time != now) {
70 printf("\n### t=%d ###\n", now);
74 reconnect_get_stats(reconnect, now, &cur);
75 diff_stats(&prev, &cur);
83 do_enable(int argc UNUSED, char *argv[] UNUSED)
85 reconnect_enable(reconnect, now);
89 do_disable(int argc UNUSED, char *argv[] UNUSED)
91 reconnect_disable(reconnect, now);
95 do_force_reconnect(int argc UNUSED, char *argv[] UNUSED)
97 reconnect_force_reconnect(reconnect, now);
101 error_from_string(const char *s)
105 } else if (!strcmp(s, "ECONNREFUSED")) {
107 } else if (!strcmp(s, "EOF")) {
110 ovs_fatal(0, "unknown error '%s'", s);
115 do_disconnected(int argc UNUSED, char *argv[])
117 reconnect_disconnected(reconnect, now, error_from_string(argv[1]));
121 do_connecting(int argc UNUSED, char *argv[] UNUSED)
123 reconnect_connecting(reconnect, now);
127 do_connect_failed(int argc UNUSED, char *argv[])
129 reconnect_connect_failed(reconnect, now, error_from_string(argv[1]));
133 do_connected(int argc UNUSED, char *argv[] UNUSED)
135 reconnect_connected(reconnect, now);
139 do_received(int argc UNUSED, char *argv[] UNUSED)
141 reconnect_received(reconnect, now);
145 do_run(int argc, char *argv[])
147 enum reconnect_action action;
150 now += atoi(argv[1]);
153 action = reconnect_run(reconnect, now);
161 case RECONNECT_CONNECT:
162 printf(" should connect\n");
165 case RECONNECT_DISCONNECT:
166 printf(" should disconnect\n");
169 case RECONNECT_PROBE:
170 printf(" should send probe\n");
176 do_advance(int argc UNUSED, char *argv[])
178 now += atoi(argv[1]);
182 do_timeout(int argc UNUSED, char *argv[] UNUSED)
184 int timeout = reconnect_timeout(reconnect, now);
186 printf(" advance %d ms\n", timeout);
189 printf(" no timeout\n");
194 diff_stats(const struct reconnect_stats *old,
195 const struct reconnect_stats *new)
197 if (old->state != new->state
198 || old->state_elapsed != new->state_elapsed
199 || old->backoff != new->backoff) {
200 printf(" in %s for %u ms (%d ms backoff)\n",
201 new->state, new->state_elapsed, new->backoff);
203 if (old->creation_time != new->creation_time
204 || old->last_received != new->last_received
205 || old->last_connected != new->last_connected) {
206 printf(" created %lld, last received %lld, last connected %lld\n",
207 new->creation_time, new->last_received, new->last_connected);
209 if (old->n_successful_connections != new->n_successful_connections
210 || old->n_attempted_connections != new->n_attempted_connections
211 || old->seqno != new->seqno) {
212 printf(" %u successful connections out of %u attempts, seqno %u\n",
213 new->n_successful_connections, new->n_attempted_connections,
216 if (old->is_connected != new->is_connected
217 || old->current_connection_duration != new->current_connection_duration
218 || old->total_connected_duration != new->total_connected_duration) {
219 printf(" %sconnected (%u ms), total %u ms connected\n",
220 new->is_connected ? "" : "not ",
221 new->current_connection_duration,
222 new->total_connected_duration);
226 static const struct command commands[] = {
227 { "enable", 0, 0, do_enable },
228 { "disable", 0, 0, do_disable },
229 { "force-reconnect", 0, 0, do_force_reconnect },
230 { "disconnected", 0, 1, do_disconnected },
231 { "connecting", 0, 0, do_connecting },
232 { "connect-failed", 0, 1, do_connect_failed },
233 { "connected", 0, 0, do_connected },
234 { "received", 0, 0, do_received },
235 { "run", 0, 1, do_run },
236 { "advance", 1, 1, do_advance },
237 { "timeout", 0, 0, do_timeout },
238 { NULL, 0, 0, NULL },