Statistics
| Branch: | Revision:

wcn_emulator / wcn_simulator.py @ 8a2e0da7

History | View | Annotate | Download (4.6 KB)

1
#!/bin/python
2
import sys
3
sys.path.append('community_networks_analysis')
4

    
5
from mininet.net import Mininet
6
from mininet.log import setLogLevel
7
from mininet.node import OVSController
8
from mininet.cli import CLI
9
from mininet.log import info, error, debug, output
10
from mininet.node import CPULimitedHost
11
from mininet.link import TCLink
12

    
13
import networkx as nx
14
from matplotlib.pyplot import ion
15
from gengraphs import loadGraph
16
from misclibs import showGraph
17

    
18
class PowerNet(Mininet):
19
    def __init__(self,**params):
20
        if 'controller' not in params.keys():
21
            params['controller'] = OVSController
22
        if 'host' not in params.keys():
23
            params['host'] = CPULimitedHost
24
        if 'link' not in params.keys():
25
            params['link'] = TCLink
26
        super(PowerNet,self).__init__(**params)
27

    
28
    def enableForwarding(self):
29
        for node in self.values():
30
            node.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")
31

    
32
    def setNeighboursRoutes(self):
33
        for node in self.values():
34
            for intf in node.intfList():
35
                if intf.link:
36
                    rintf = self.remoteIntf(intf)
37
                    raddrs = self.getNodeAddrs(rintf.node)
38
                    for addr in raddrs:
39
                        node.setHostRoute(addr,intf.name)
40

    
41
    def getNodeAddrs(self,node):
42
        r = []
43
        for intf in node.intfList():
44
            if intf.link:
45
                r.append(intf.ip)
46
        return r
47

    
48
    def remoteIntf(self,intf):
49
        if intf.link:
50
            intfs = [ intf.link.intf1, intf.link.intf2 ]
51
            intfs.remove(intf)
52
            return intfs[0]
53
        return None
54

    
55

    
56
class GraphNet(PowerNet):
57
    def __init__(self,edges_file,draw=True,**params):
58
        super(GraphNet,self).__init__(**params)
59
        info("\nReading "+edges_file+"\n")
60

    
61
        g = loadGraph(edges_file, connected=True)
62

    
63
        nodeCounter = 0
64
        nodeMap = {}
65
        for name in g.nodes():
66
            nodeMap[name] = "h"+str(name)+"_"+str(nodeCounter)
67
            nodeCounter += 1
68

    
69
        self.gg = nx.relabel_nodes(g,nodeMap)
70

    
71
        self.hosts_port = {}
72

    
73
        # add nodes
74
        for n in self.gg.nodes():
75
            self.addHost(n)
76
            self.hosts_port[n] = 1 
77

    
78
        # add edges
79
        for e in self.gg.edges(data=True):
80
            # 10 Mbps, 5ms delay, 10% loss, 1000 packet queue
81
            # htp: Hierarchical Token Bucket rate limiter
82
            quality_params = {"bw":10,"delay":'5ms', "loss":100-100.0/e[2]['weight'], "max_queue_size":1000, "use_htb":True}
83
            self.insertLink(self.get(e[0]),self.get(e[1]),quality_params)
84

    
85
        if draw:
86
            ion()
87
            showGraph(self.gg)
88

    
89
    def pickHostAddrPort(self,node):
90
        port = self.hosts_port[node.name]
91
        addr = "10.0."+node.name.split('_')[-1]+"."+str(port)+"/8"
92
        self.hosts_port[node.name] += 1
93
        return addr,port
94

    
95
    def insertLink(self,n1,n2,quality_params={}):
96
        addr1, port1 = self.pickHostAddrPort(n1)
97
        addr2, port2 = self.pickHostAddrPort(n2)
98

    
99
        self.addLink(n1,n2,  \
100
                port1 = port1, \
101
                port2 = port2, \
102
                params1=dict([('ip',addr1)] + quality_params.items()), \
103
                params2=dict([('ip',addr2)] + quality_params.items()) \
104
                )
105

    
106
    def setShortestRoutes(self):
107
        self.setNeighboursRoutes()
108
        paths = nx.shortest_path(self.gg,weight='weight')
109
        for node1 in paths.keys():
110
            debug("Starting node: "+node1+'\n')
111
            debug("\tpaths: "+str(paths[node1])+'\n')
112
            for node2 in paths[node1].keys():
113
                if node2 != node1 and len(paths[node1][node2])>2:
114
                    debug("\tDestination node: "+node2+'\n')
115
                    nextHop = self.get(paths[node1][node2][1])
116
                    debug("\tNextHop node: "+nextHop.name+'\n')
117
                    dsts = self.getNodeAddrs(self.get(node2))
118
                    intfs = self.get(node1).connectionsTo(nextHop)
119
                    nextAddrs = [ couple[1].ip for couple in intfs ]
120
                    for dst in dsts:
121
                        for addr in nextAddrs:
122
                            self.get(node1).cmd("ip route add "+dst+" via "+addr)
123
                            debug("\tip route add "+dst+" via "+addr+'\n')
124
                
125

    
126
if __name__ == '__main__':
127
    setLogLevel('info')
128
    net = GraphNet("test.edges")
129
    net.start()
130
    net.enableForwarding()
131
    net.setShortestRoutes()
132
    CLI(net)
133
    net.stop()
134

    
135
#    setLogLevel('info')
136
#    topo = TopoFactory("minitest.edges")
137
#    net = Mininet(topo,controller = OVSController,host=CPULimitedHost, link=TCLink)
138
#    net.start()
139
#    CLI(net)
140
#    net.stop()