]> git.proxmox.com Git - ovs.git/blame - tests/test-reconnect.c
Fix ovs-dpctl-top by removing 3 wrong hunks in py3-compat.patch.
[ovs.git] / tests / test-reconnect.c
CommitLineData
3ed497fc 1/*
922fed06 2 * Copyright (c) 2009, 2010, 2011, 2012, 2014, 2016 Nicira, Inc.
3ed497fc
BP
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>
3f636c7e 18#undef NDEBUG
3ed497fc 19#include "reconnect.h"
3ed497fc
BP
20#include <errno.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
3ed497fc
BP
24#include "command-line.h"
25#include "compiler.h"
3f636c7e 26#include "ovstest.h"
3ed497fc
BP
27#include "svec.h"
28#include "util.h"
e6211adc 29#include "openvswitch/vlog.h"
3ed497fc
BP
30
31static struct reconnect *reconnect;
32static int now;
33
3ed497fc 34static void diff_stats(const struct reconnect_stats *old,
5eda645e
AE
35 const struct reconnect_stats *new,
36 int delta);
5f383751 37static const struct ovs_cmdl_command *get_all_commands(void);
3ed497fc 38
eadd1644
AZ
39static void
40test_reconnect_main(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
3ed497fc
BP
41{
42 struct reconnect_stats prev;
a85c0bbc 43 unsigned int old_max_tries;
3ed497fc
BP
44 int old_time;
45 char line[128];
46
45863ce5 47 vlog_set_levels_from_string_assert("reconnect:off");
1e8cf0f7 48
3ed497fc
BP
49 now = 1000;
50 reconnect = reconnect_create(now);
51 reconnect_set_name(reconnect, "remote");
52 reconnect_get_stats(reconnect, now, &prev);
53 printf("### t=%d ###\n", now);
54 old_time = now;
a85c0bbc 55 old_max_tries = reconnect_get_max_tries(reconnect);
3ed497fc
BP
56 while (fgets(line, sizeof line, stdin)) {
57 struct reconnect_stats cur;
58 struct svec args;
59
60 fputs(line, stdout);
61 if (line[0] == '#') {
62 continue;
63 }
64
65 svec_init(&args);
66 svec_parse_words(&args, line);
67 svec_terminate(&args);
68 if (!svec_is_empty(&args)) {
1636c761
RB
69 struct ovs_cmdl_context ctx = {
70 .argc = args.n,
71 .argv = args.names,
72 };
73 ovs_cmdl_run_command(&ctx, get_all_commands());
3ed497fc
BP
74 }
75 svec_destroy(&args);
76
77 if (old_time != now) {
78 printf("\n### t=%d ###\n", now);
3ed497fc
BP
79 }
80
81 reconnect_get_stats(reconnect, now, &cur);
5eda645e 82 diff_stats(&prev, &cur, now - old_time);
3ed497fc 83 prev = cur;
a85c0bbc
BP
84 if (reconnect_get_max_tries(reconnect) != old_max_tries) {
85 old_max_tries = reconnect_get_max_tries(reconnect);
86 printf(" %u tries left\n", old_max_tries);
87 }
5eda645e
AE
88
89 old_time = now;
3ed497fc 90 }
3ed497fc
BP
91}
92
93static void
1636c761 94do_enable(struct ovs_cmdl_context *ctx OVS_UNUSED)
3ed497fc
BP
95{
96 reconnect_enable(reconnect, now);
97}
98
99static void
1636c761 100do_disable(struct ovs_cmdl_context *ctx OVS_UNUSED)
3ed497fc
BP
101{
102 reconnect_disable(reconnect, now);
103}
104
105static void
1636c761 106do_force_reconnect(struct ovs_cmdl_context *ctx OVS_UNUSED)
3ed497fc
BP
107{
108 reconnect_force_reconnect(reconnect, now);
109}
110
111static int
112error_from_string(const char *s)
113{
114 if (!s) {
115 return 0;
116 } else if (!strcmp(s, "ECONNREFUSED")) {
117 return ECONNREFUSED;
118 } else if (!strcmp(s, "EOF")) {
119 return EOF;
120 } else {
121 ovs_fatal(0, "unknown error '%s'", s);
122 }
123}
124
125static void
1636c761 126do_disconnected(struct ovs_cmdl_context *ctx)
3ed497fc 127{
1636c761 128 reconnect_disconnected(reconnect, now, error_from_string(ctx->argv[1]));
3ed497fc
BP
129}
130
131static void
1636c761 132do_connecting(struct ovs_cmdl_context *ctx OVS_UNUSED)
3ed497fc
BP
133{
134 reconnect_connecting(reconnect, now);
135}
136
137static void
1636c761 138do_connect_failed(struct ovs_cmdl_context *ctx)
3ed497fc 139{
1636c761 140 reconnect_connect_failed(reconnect, now, error_from_string(ctx->argv[1]));
3ed497fc
BP
141}
142
143static void
1636c761 144do_connected(struct ovs_cmdl_context *ctx OVS_UNUSED)
3ed497fc
BP
145{
146 reconnect_connected(reconnect, now);
147}
148
149static void
1636c761 150do_activity(struct ovs_cmdl_context *ctx OVS_UNUSED)
3ed497fc 151{
a6f639f8 152 reconnect_activity(reconnect, now);
3ed497fc
BP
153}
154
155static void
1636c761 156do_run(struct ovs_cmdl_context *ctx)
3ed497fc
BP
157{
158 enum reconnect_action action;
159
1636c761
RB
160 if (ctx->argc > 1) {
161 now += atoi(ctx->argv[1]);
3ed497fc
BP
162 }
163
164 action = reconnect_run(reconnect, now);
165 switch (action) {
166 default:
167 if (action != 0) {
428b2edd 168 OVS_NOT_REACHED();
3ed497fc
BP
169 }
170 break;
171
172 case RECONNECT_CONNECT:
173 printf(" should connect\n");
174 break;
175
176 case RECONNECT_DISCONNECT:
177 printf(" should disconnect\n");
178 break;
179
180 case RECONNECT_PROBE:
181 printf(" should send probe\n");
182 break;
183 }
184}
185
186static void
1636c761 187do_advance(struct ovs_cmdl_context *ctx)
3ed497fc 188{
1636c761 189 now += atoi(ctx->argv[1]);
3ed497fc
BP
190}
191
192static void
1636c761 193do_timeout(struct ovs_cmdl_context *ctx OVS_UNUSED)
3ed497fc
BP
194{
195 int timeout = reconnect_timeout(reconnect, now);
196 if (timeout >= 0) {
197 printf(" advance %d ms\n", timeout);
198 now += timeout;
199 } else {
200 printf(" no timeout\n");
201 }
202}
203
a85c0bbc 204static void
1636c761 205do_set_max_tries(struct ovs_cmdl_context *ctx)
a85c0bbc 206{
1636c761 207 reconnect_set_max_tries(reconnect, atoi(ctx->argv[1]));
a85c0bbc
BP
208}
209
5ee527e2
BP
210static void
211do_set_backoff_free_tries(struct ovs_cmdl_context *ctx)
212{
213 reconnect_set_backoff_free_tries(reconnect, atoi(ctx->argv[1]));
214}
215
3ed497fc
BP
216static void
217diff_stats(const struct reconnect_stats *old,
5eda645e
AE
218 const struct reconnect_stats *new,
219 int delta)
3ed497fc
BP
220{
221 if (old->state != new->state
222 || old->state_elapsed != new->state_elapsed
223 || old->backoff != new->backoff) {
224 printf(" in %s for %u ms (%d ms backoff)\n",
225 new->state, new->state_elapsed, new->backoff);
226 }
227 if (old->creation_time != new->creation_time
a6f639f8 228 || old->last_activity != new->last_activity
3ed497fc 229 || old->last_connected != new->last_connected) {
a6f639f8
BP
230 printf(" created %lld, last activity %lld, last connected %lld\n",
231 new->creation_time, new->last_activity, new->last_connected);
3ed497fc
BP
232 }
233 if (old->n_successful_connections != new->n_successful_connections
234 || old->n_attempted_connections != new->n_attempted_connections
235 || old->seqno != new->seqno) {
236 printf(" %u successful connections out of %u attempts, seqno %u\n",
237 new->n_successful_connections, new->n_attempted_connections,
238 new->seqno);
239 }
5eda645e
AE
240 if (old->is_connected != new->is_connected) {
241 printf(" %sconnected\n", new->is_connected ? "" : "dis");
3ed497fc 242 }
5eda645e
AE
243 if (old->last_connected != new->last_connected
244 || (old->msec_since_connect != new->msec_since_connect - delta
245 && !(old->msec_since_connect == UINT_MAX
246 && new->msec_since_connect == UINT_MAX))
247 || (old->total_connected_duration != new->total_connected_duration - delta
248 && !(old->total_connected_duration == 0
249 && new->total_connected_duration == 0))) {
250 printf(" last connected %u ms ago, connected %u ms total\n",
251 new->msec_since_connect, new->total_connected_duration);
eba18f00 252 }
5eda645e
AE
253 if (old->last_disconnected != new->last_disconnected
254 || (old->msec_since_disconnect != new->msec_since_disconnect - delta
255 && !(old->msec_since_disconnect == UINT_MAX
256 && new->msec_since_disconnect == UINT_MAX))) {
257 printf(" disconnected at %llu ms (%u ms ago)\n",
258 new->last_disconnected, new->msec_since_disconnect);
eba18f00 259 }
3ed497fc
BP
260}
261
19df7f51 262static void
1636c761 263do_set_passive(struct ovs_cmdl_context *ctx OVS_UNUSED)
19df7f51
BP
264{
265 reconnect_set_passive(reconnect, true, now);
266}
267
268static void
1636c761 269do_listening(struct ovs_cmdl_context *ctx OVS_UNUSED)
19df7f51
BP
270{
271 reconnect_listening(reconnect, now);
272}
273
274static void
1636c761 275do_listen_error(struct ovs_cmdl_context *ctx)
19df7f51 276{
1636c761 277 reconnect_listen_error(reconnect, now, atoi(ctx->argv[1]));
19df7f51
BP
278}
279
fcf281b0
BP
280static void
281do_receive_attempted(struct ovs_cmdl_context *ctx OVS_UNUSED)
282{
283 if (!strcmp(ctx->argv[1], "now")) {
284 reconnect_receive_attempted(reconnect, now);
285 } else if (!strcmp(ctx->argv[1], "LLONG_MAX")) {
286 reconnect_receive_attempted(reconnect, LLONG_MAX);
287 } else {
288 ovs_fatal(0, "%s: bad argument %s", ctx->argv[0], ctx->argv[1]);
289 }
290}
291
5f383751 292static const struct ovs_cmdl_command all_commands[] = {
1f4a7252
RM
293 { "enable", NULL, 0, 0, do_enable, OVS_RO },
294 { "disable", NULL, 0, 0, do_disable, OVS_RO },
295 { "force-reconnect", NULL, 0, 0, do_force_reconnect, OVS_RO },
296 { "disconnected", NULL, 0, 1, do_disconnected, OVS_RO },
297 { "connecting", NULL, 0, 0, do_connecting, OVS_RO },
298 { "connect-failed", NULL, 0, 1, do_connect_failed, OVS_RO },
299 { "connected", NULL, 0, 0, do_connected, OVS_RO },
300 { "activity", NULL, 0, 0, do_activity, OVS_RO },
301 { "run", NULL, 0, 1, do_run, OVS_RO },
302 { "advance", NULL, 1, 1, do_advance, OVS_RO },
303 { "timeout", NULL, 0, 0, do_timeout, OVS_RO },
304 { "set-max-tries", NULL, 1, 1, do_set_max_tries, OVS_RO },
5ee527e2
BP
305 { "set-backoff-free-tries", NULL, 1, 1, do_set_backoff_free_tries,
306 OVS_RO },
1f4a7252
RM
307 { "passive", NULL, 0, 0, do_set_passive, OVS_RO },
308 { "listening", NULL, 0, 0, do_listening, OVS_RO },
309 { "listen-error", NULL, 1, 1, do_listen_error, OVS_RO },
fcf281b0 310 { "receive-attempted", NULL, 1, 1, do_receive_attempted, OVS_RO },
1f4a7252 311 { NULL, NULL, 0, 0, NULL, OVS_RO },
3ed497fc 312};
eadd1644 313
5f383751 314static const struct ovs_cmdl_command *
d2586fce
GS
315get_all_commands(void)
316{
317 return all_commands;
318}
319
eadd1644 320OVSTEST_REGISTER("test-reconnect", test_reconnect_main);