Merge branch 'master' of https://git.erp5.org/repos/vifibnet
[re6stnet.git] / tunnel.py
1 import os, random, traceback, time
2 import plib, utils, db
3
4 log = None
5 smooth = 0.3
6
7 class Connection:
8 def __init__(self, ip, write_pipe, hello, port, proto, iface, peer_id,
9 ovpn_args):
10 self.process = plib.client(ip, write_pipe, hello,
11 '--dev', iface, '--proto', proto, '--rport', str(port),
12 *ovpn_args, stdout=os.open(os.path.join(log,
13 'vifibnet.client.%s.log' % (peer_id,)),
14 os.O_WRONLY|os.O_CREAT|os.O_TRUNC) )
15
16 self.iface = iface
17 self._lastTrafic = self._getTrafic()
18 self._bandwidth = None
19
20 # TODO : update the stats
21 def refresh(self):
22 # Check that the connection is alive
23 if self.process.poll() != None:
24 utils.log('Connection with %s has failed with return code %s'
25 % (id, self.process.returncode), 3)
26 return False
27
28 trafic = self._getTrafic()
29 if self._bandwidth == None:
30 self._bandwidth = trafic - self._lastTrafic
31 else:
32 self._bandwidth = (1-smooth)*self._bandwidth + smooth*trafic
33 self._lastTrafic = trafic
34 utils.log('New bandwidth calculated on iface %s : %sb' % self._bandwidth, 4)
35
36 return True
37
38 def _getTrafic(self):
39 try:
40 f_rx = open('/sys/class/net/%s/statistics/rx_bytes' % self.iface, 'r')
41 f_tx = open('/sys/class/net/%s/statistics/tx_bytes' % self.iface, 'r')
42 return int(f_rx.read()) + int(f_tx.read())
43 except Exception: # TODO : change this
44 return 0
45
46 class TunnelManager:
47
48 def __init__(self, write_pipe, peer_db, openvpn_args, hello_interval,
49 refresh, connection_count, refresh_rate):
50 self._write_pipe = write_pipe
51 self._peer_db = peer_db
52 self._connection_dict = {}
53 self._ovpn_args = openvpn_args
54 self._hello = hello_interval
55 self._refresh_time = refresh
56 self.free_interface_set = set(('client1', 'client2', 'client3',
57 'client4', 'client5', 'client6',
58 'client7', 'client8', 'client9',
59 'client10', 'client11', 'client12'))
60 self.next_refresh = time.time()
61
62 self._client_count = connection_count/2
63 self._refresh_count = refresh_rate*self._client_count
64
65 def refresh(self):
66 utils.log('Refreshing the tunnels', 2)
67 self._cleanDeads()
68 self._removeSomeTunnels()
69 self._makeNewTunnels()
70 self.next_refresh = time.time() + self._refresh_time
71
72 def _cleanDeads(self):
73 for id in self._connection_dict.keys():
74 if not self._connection_dict[id].refresh():
75 self._kill(id)
76
77 def _removeSomeTunnels(self):
78 for i in range(0, max(0, len(self._connection_dict) -
79 self._client_count + self._refresh_count)):
80 peer_id = random.choice(self._connection_dict.keys())
81 self._kill(peer_id)
82
83 def _kill(self, peer_id):
84 utils.log('Killing the connection with id ' + str(peer_id), 2)
85 connection = self._connection_dict.pop(peer_id)
86 try:
87 connection.process.kill()
88 except OSError:
89 # If the process is already exited
90 pass
91 self.free_interface_set.add(connection.iface)
92 self._peer_db.unusePeer(peer_id)
93
94 def _makeNewTunnels(self):
95 utils.log('Trying to make %i new tunnels' %
96 (self._client_count - len(self._connection_dict)), 3)
97 try:
98 for peer_id, ip, port, proto in self._peer_db.getUnusedPeers(
99 self._client_count - len(self._connection_dict)):
100 utils.log('Establishing a connection with id %s (%s:%s)'
101 % (peer_id, ip, port), 2)
102 iface = self.free_interface_set.pop()
103 self._connection_dict[peer_id] = Connection(ip,
104 self._write_pipe, self._hello, port, proto, iface,
105 peer_id, self._ovpn_args)
106 self._peer_db.usePeer(peer_id)
107 except KeyError:
108 utils.log("Can't establish connection with %s"
109 ": no available interface" % ip, 2)
110 except Exception:
111 traceback.print_exc()