netdev-vport: Warn on IPsec tunnels when ovs-monitor-ipsec not running.
[openvswitch] / tests / test-reconnect.py
1 # Copyright (c) 2009, 2010 Nicira Networks.
2
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6
7 #     http://www.apache.org/licenses/LICENSE-2.0
8
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import errno
16 import logging
17 import sys
18
19 import ovs.reconnect
20
21 def do_enable(arg):
22     r.enable(now)
23
24 def do_disable(arg):
25     r.disable(now)
26
27 def do_force_reconnect(arg):
28     r.force_reconnect(now)
29
30 def error_from_string(s):
31     if not s:
32         return 0
33     elif s == "ECONNREFUSED":
34         return errno.ECONNREFUSED
35     elif s == "EOF":
36         return EOF
37     else:
38         sys.stderr.write("unknown error '%s'\n" % s)
39         sys.exit(1)
40
41 def do_disconnected(arg):
42     r.disconnected(now, error_from_string(arg))
43     
44 def do_connecting(arg):
45     r.connecting(now)
46
47 def do_connect_failed(arg):
48     r.connect_failed(now, error_from_string(arg))
49
50 def do_connected(arg):
51     r.connected(now)
52
53 def do_received(arg):
54     r.received(now)
55
56 def do_run(arg):
57     global now
58     if arg is not None:
59         now += int(arg)
60
61     action = r.run(now)
62     if action is None:
63         pass
64     elif action == ovs.reconnect.CONNECT:
65         print "  should connect"
66     elif action == ovs.reconnect.DISCONNECT:
67         print "  should disconnect"
68     elif action == ovs.reconnect.PROBE:
69         print "  should send probe"
70     else:
71         assert False
72
73 def do_advance(arg):
74     global now
75     now += int(arg)
76
77 def do_timeout(arg):
78     global now
79     timeout = r.timeout(now)
80     if timeout >= 0:
81         print "  advance %d ms" % timeout
82         now += timeout
83     else:
84         print "  no timeout"
85
86 def do_set_max_tries(arg):
87     r.set_max_tries(int(arg))
88
89 def diff_stats(old, new):
90     if (old.state != new.state or
91         old.state_elapsed != new.state_elapsed or
92         old.backoff != new.backoff):
93         print("  in %s for %d ms (%d ms backoff)"
94               % (new.state, new.state_elapsed, new.backoff))
95
96     if (old.creation_time != new.creation_time or
97         old.last_received != new.last_received or
98         old.last_connected != new.last_connected):
99         print("  created %d, last received %d, last connected %d"
100               % (new.creation_time, new.last_received, new.last_connected))
101
102     if (old.n_successful_connections != new.n_successful_connections or
103         old.n_attempted_connections != new.n_attempted_connections or
104         old.seqno != new.seqno):
105         print("  %d successful connections out of %d attempts, seqno %d"
106               % (new.n_successful_connections, new.n_attempted_connections,
107                  new.seqno))
108
109     if (old.is_connected != new.is_connected or
110         old.current_connection_duration != new.current_connection_duration or
111         old.total_connected_duration != new.total_connected_duration):
112         if new.is_connected:
113             negate = ""
114         else:
115             negate = "not "
116         print("  %sconnected (%d ms), total %d ms connected"
117               % (negate, new.current_connection_duration,
118                  new.total_connected_duration))
119
120     if (old.last_disconnected != new.last_disconnected):
121         print("  disconnected at %d ms (%d ms ago)"
122               % (new.last_disconnected, new.current_disconnect_duration))
123
124     if (old.current_disconnect_duration != new.current_disconnect_duration):
125         print("  disconnected for %d ms" % (new.current_disconnect_duration))
126
127 def do_set_passive(arg):
128     r.set_passive(True, now)
129
130 def do_listening(arg):
131     r.listening(now)
132
133 def do_listen_error(arg):
134     r.listen_error(now, int(arg))
135
136 def main():
137     commands = {
138         "enable": do_enable,
139         "disable": do_disable,
140         "force-reconnect": do_force_reconnect,
141         "disconnected": do_disconnected,
142         "connecting": do_connecting,
143         "connect-failed": do_connect_failed,
144         "connected": do_connected,
145         "received": do_received,
146         "run": do_run,
147         "advance": do_advance,
148         "timeout": do_timeout,
149         "set-max-tries": do_set_max_tries,
150         "passive": do_set_passive,
151         "listening": do_listening,
152         "listen-error": do_listen_error
153     }
154
155     logging.basicConfig(level=logging.CRITICAL)
156
157     global now
158     global r
159
160     now = 1000
161     r = ovs.reconnect.Reconnect(now)
162     r.set_name("remote")
163     prev = r.get_stats(now)
164     print "### t=%d ###" % now
165     old_time = now
166     old_max_tries = r.get_max_tries()
167     while True:
168         line = sys.stdin.readline()
169         if line == "":
170             break
171
172         print line[:-1]
173         if line[0] == "#":
174             continue
175
176         args = line.split()
177         if len(args) == 0:
178             continue
179
180         command = args[0]
181         if len(args) > 1:
182             op = args[1]
183         else:
184             op = None
185         commands[command](op)
186
187         if old_time != now:
188             print
189             print "### t=%d ###" % now
190             old_time = now
191
192         cur = r.get_stats(now)
193         diff_stats(prev, cur)
194         prev = cur
195         if r.get_max_tries() != old_max_tries:
196             old_max_tries = r.get_max_tries()
197             print "  %d tries left" % old_max_tries
198
199 if __name__ == '__main__':
200     main()
201
202