Statistics
| Branch: | Revision:

wcn_emulator / network_builder.py @ c09f8d98

History | View | Annotate | Download (4.88 KB)

1

    
2
import sys
3
import networkx as nx
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
sys.path.append('community_networks_analysis')
14

    
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'], "use_htb":True}
83
            quality_params = {}
84
            quality_params["bw"] = 10
85
#            quality_params["delay"] = '5ms'
86
#            quality_params["loss"] = 100-100.0/e[2]['weight']
87
            quality_params["use_htb"] = True
88
            self.insertLink(self.get(e[0]),self.get(e[1]),quality_params)
89

    
90
        if draw:
91
            showGraph(self.gg)
92

    
93
    def pickHostAddrPort(self, node):
94
        port = self.hosts_port[node.name]
95
        addr = "10.0."+node.name.split('_')[-1]+"."+str(port)+"/8"
96
        self.hosts_port[node.name] += 1
97
        return addr,port
98

    
99
    def insertLink(self, n1, n2, quality_params={}):
100
        addr1, port1 = self.pickHostAddrPort(n1)
101
        addr2, port2 = self.pickHostAddrPort(n2)
102

    
103
        self.addLink(n1, n2,  \
104
                port1 = port1, \
105
                port2 = port2, \
106
                params1=dict([('ip',addr1)] + quality_params.items()), \
107
                params2=dict([('ip',addr2)] + quality_params.items()) \
108
                )
109

    
110
    def setShortestRoutes(self):
111
        paths = nx.shortest_path(self.gg,weight='weight')
112
        for node1 in paths.keys():
113
            host1 = self.get(node1)
114
            debug("Starting node: "+node1+'\n')
115
            debug("\tpaths: "+str(paths[node1])+'\n')
116
            for node2 in paths[node1].keys():
117
                if node2 != node1 :
118
                    if len(paths[node1][node2])>2:
119
                        debug("\tDestination node: "+node2+'\n')
120
                        nextHop = self.get(paths[node1][node2][1])
121
                        debug("\tNextHop node: "+nextHop.name+'\n')
122
                        dsts = self.getNodeAddrs(self.get(node2))
123
                        intfs = host1.connectionsTo(nextHop)
124
                        nextAddrs = [ couple[1].ip for couple in intfs ]
125
                        rintf = intfs[0][0] # WARNING we just consider one link
126
                        for dst in dsts:
127
                            for addr in nextAddrs:
128
                                host1.cmd("ip route add "+dst+" via "+addr+" dev "+rintf.name)
129
                                debug("\tip route add "+dst+" via "+addr+'\n')
130
                    else :
131
                        host2 = self.get(node2)
132
                        intfs = [ couple[0] for couple in host1.connectionsTo(host2) ]
133
                        rintf = intfs[0] # WARNING we just consider one link
134
                        raddrs = self.getNodeAddrs(host2)
135
                        for addr in raddrs:
136
                            host1.setHostRoute(addr,rintf.name)
137

    
138