Commit 17d121a8 authored by Ulysse Beaugnon's avatar Ulysse Beaugnon

Tunnel manager can now see how many routes goes to each tunnel. This is still...

Tunnel manager can now see how many routes goes to each tunnel. This is still not very acurate since I don't filter on the size oof the subnet.
parent 3825276f
import os, random, traceback, time import os, random, traceback, time, struct
import plib, utils, db import plib, utils, db
log = None log = None
...@@ -10,7 +10,7 @@ class Connection: ...@@ -10,7 +10,7 @@ class Connection:
self.process = plib.client(address, write_pipe, hello, '--dev', iface, self.process = plib.client(address, write_pipe, hello, '--dev', iface,
*ovpn_args, stdout=os.open(os.path.join(log, *ovpn_args, stdout=os.open(os.path.join(log,
'vifibnet.client.%s.log' % (prefix,)), 'vifibnet.client.%s.log' % (prefix,)),
os.O_WRONLY|os.O_CREAT|os.O_TRUNC) ) os.O_WRONLY|os.O_CREAT|os.O_TRUNC))
self.iface = iface self.iface = iface
self._prefix = prefix self._prefix = prefix
...@@ -63,6 +63,7 @@ class TunnelManager: ...@@ -63,6 +63,7 @@ class TunnelManager:
self._write_pipe = write_pipe self._write_pipe = write_pipe
self._peer_db = peer_db self._peer_db = peer_db
self._connection_dict = {} self._connection_dict = {}
self._route_count = {}
self._ovpn_args = openvpn_args self._ovpn_args = openvpn_args
self._hello = hello_interval self._hello = hello_interval
self._refresh_time = refresh self._refresh_time = refresh
...@@ -78,6 +79,7 @@ class TunnelManager: ...@@ -78,6 +79,7 @@ class TunnelManager:
def refresh(self): def refresh(self):
utils.log('Refreshing the tunnels', 2) utils.log('Refreshing the tunnels', 2)
self._cleanDeads() self._cleanDeads()
self._countRoutes()
self._removeSomeTunnels() self._removeSomeTunnels()
self._makeNewTunnels() self._makeNewTunnels()
self.next_refresh = time.time() + self._refresh_time self.next_refresh = time.time() + self._refresh_time
...@@ -103,6 +105,7 @@ class TunnelManager: ...@@ -103,6 +105,7 @@ class TunnelManager:
pass pass
self.free_interface_set.add(connection.iface) self.free_interface_set.add(connection.iface)
self._peer_db.unusePeer(prefix) self._peer_db.unusePeer(prefix)
del self._route_count[connection.iface]
def _makeNewTunnels(self): def _makeNewTunnels(self):
utils.log('Trying to make %i new tunnels' % utils.log('Trying to make %i new tunnels' %
...@@ -115,9 +118,26 @@ class TunnelManager: ...@@ -115,9 +118,26 @@ class TunnelManager:
self._connection_dict[prefix] = Connection(address, self._connection_dict[prefix] = Connection(address,
self._write_pipe, self._hello, iface, self._write_pipe, self._hello, iface,
prefix, self._ovpn_args) prefix, self._ovpn_args)
self._route_count[iface] = 0
self._peer_db.usePeer(prefix) self._peer_db.usePeer(prefix)
except KeyError: except KeyError:
utils.log("""Can't establish connection with %s utils.log("""Can't establish connection with %s
: no available interface""" % prefix, 2) : no available interface""" % prefix, 2)
except Exception: except Exception:
traceback.print_exc() traceback.print_exc()
def _countRoutes(self):
utils.log('Starting to count the routes on each interface', 3)
for iface in self._route_count.keys():
self._route_count[iface] = 0
f = open('/proc/net/ipv6_route', 'r')
for line in f:
ip, subnet_size, iface = struct.unpack("""32s x 2s x 32x x 2x x
32x x 8x x 8x x 8x x 8x x %ss x""" % (len(line)-142), line)
iface = iface.replace(' ', '')
if iface in self._route_count.keys():
self._route_count[iface] += 1
for iface in self._route_count.keys():
utils.log('Routes on iface %s : %s' % (iface,self._route_count[iface] ), 5)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment