Revision b1497c9f network_builder.py

View differences:

network_builder.py
1 1
import re
2
import sys
3 2
import networkx as nx
3
import graph_utils as gu
4 4

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

  
13
sys.path.append('community_networks_analysis')
14

  
15
from gengraphs import loadGraph
16
from misclibs import showGraph
17 11

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

  
28 22
    def enableForwarding(self):
29 23
        for node in self.values():
......
36 30
                    rintf = self.remoteIntf(intf)
37 31
                    raddrs = self.getNodeAddrs(rintf.node)
38 32
                    for addr in raddrs:
39
                        node.setHostRoute(addr,intf.name)
33
                        node.setHostRoute(addr, intf.name)
40 34

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

  
48
    def remoteIntf(self,intf):
42
    def remoteIntf(self, intf):
49 43
        if intf.link:
50
            intfs = [ intf.link.intf1, intf.link.intf2 ]
44
            intfs = [intf.link.intf1, intf.link.intf2]
51 45
            intfs.remove(intf)
52 46
            return intfs[0]
53 47
        return None
......
59 53
        for h in hosts:
60 54
            intfs = h.intfList()
61 55
            for intf in intfs:
62
                if intf.link != None and intf.link not in links:
56
                if intf.link and intf.link not in links:
63 57
                    links.append(intf.link)
64 58
        return links
65 59

  
66
    def linkSentPackets(self,link):
67
        pstr1 = link.intf1.node.cmd("ifconfig ",link.intf1.name ,"| grep -Eo 'TX packets:[0-9]+' | cut -d':' -f 2")
68
        pstr2 = link.intf2.node.cmd("ifconfig ",link.intf2.name ,"| grep -Eo 'TX packets:[0-9]+' | cut -d':' -f 2")
69
	packets1 = int(pstr1.split("\n")[0])
70
	packets2 = int(pstr2.split("\n")[0])
60
    def linkSentPackets(self, link):
61
        pstr1 = link.intf1.node.cmd("ifconfig ", link.intf1.name, "| grep -Eo \
62
                                    'TX packets:[0-9]+' | cut -d':' -f 2")
63
        pstr2 = link.intf2.node.cmd("ifconfig ", link.intf2.name, "| grep -Eo \
64
                                    'TX packets:[0-9]+' | cut -d':' -f 2")
65
        packets1 = int(pstr1.split("\n")[0])
66
        packets2 = int(pstr2.split("\n")[0])
71 67
        return packets1+packets2
72 68

  
73
    def linkSentBytes(self,link):
74
        pstr1 = link.intf1.node.cmd("ifconfig ",link.intf1.name ,"| grep -Eo 'TX bytes:[0-9]+' | cut -d':' -f 2")
75
        pstr2 = link.intf2.node.cmd("ifconfig ",link.intf2.name ,"| grep -Eo 'TX bytes:[0-9]+' | cut -d':' -f 2")
76
	bytes1 = int(pstr1.split("\n")[0].split(" ")[0])
77
	bytes2 = int(pstr2.split("\n")[0].split(" ")[0])
69
    def linkSentBytes(self, link):
70
        pstr1 = link.intf1.node.cmd("ifconfig ", link.intf1.name, "| grep -Eo\
71
                                    'TX bytes:[0-9]+' | cut -d':' -f 2")
72
        pstr2 = link.intf2.node.cmd("ifconfig ", link.intf2.name, "| grep -Eo\
73
                                    'TX bytes:[0-9]+' | cut -d':' -f 2")
74
        bytes1 = int(pstr1.split("\n")[0].split(" ")[0])
75
        bytes2 = int(pstr2.split("\n")[0].split(" ")[0])
78 76
        return bytes2+bytes1
79 77

  
80
    def hostSentPackets(self,host):
78
    def hostSentPackets(self, host):
81 79
        sent_packets = 0
82 80
        sent_bytes = 0
83 81
        intfs = host.intfNames()
84 82
        for intf in intfs:
85
            host.cmd("ifconfig",intf ,"| grep -Eo 'TX bytes:[0-9]+' | cut -d':' -f 2")
86
            sent_bytes += int(re.findall(r'\d+',host.cmd("ifconfig",intf ,"| grep -Eo 'TX bytes:[0-9]+' | cut -d':' -f 2"))[0])
87
            sent_packets += int(re.findall(r'\d+',host.cmd("ifconfig ",intf ,"| grep -Eo 'TX packets:[0-9]+' | cut -d':' -f 2"))[0])
88
        return (sent_packets,sent_bytes)
83
            host.cmd("ifconfig", intf, "| grep -Eo 'TX bytes:[0-9]+' | \
84
                     cut -d':' -f 2")
85
            sent_bytes += int(re.findall(r'\d+', host.cmd("ifconfig", intf, "| grep -Eo 'TX bytes:[0-9]+' | cut -d':' -f 2"))[0])
86
            sent_packets += int(re.findall(r'\d+', host.cmd("ifconfig ", intf, "| grep -Eo 'TX packets:[0-9]+' | cut -d':' -f 2"))[0])
87
        return (sent_packets, sent_bytes)
89 88

  
90
    def hostReceivedPackets(self,host):
89
    def hostReceivedPackets(self, host):
91 90
        received_packets = 0
92 91
        received_bytes = 0
93 92
        intfs = host.intfNames()
94 93
        for intf in intfs:
95
            received_bytes += int(re.findall(r'\d+',host.cmd("ifconfig "+intf +" | grep -Eo 'RX bytes:[0-9]+' | cut -d':' -f 2"))[0])
96
            received_packets += int(re.findall(r'\d+',host.cmd("ifconfig "+intf +" | grep -Eo 'RX packets:[0-9]+' | cut -d':' -f 2"))[0])
97
        return (received_packets,received_bytes)
98
        
94
            received_bytes += int(re.findall(r'\d+', host.cmd("ifconfig " + intf + " | grep -Eo 'RX bytes:[0-9]+' | cut -d':' -f 2"))[0])
95
            received_packets += int(re.findall(r'\d+', host.cmd("ifconfig " + intf + " | grep -Eo 'RX packets:[0-9]+' | cut -d':' -f 2"))[0])
96
        return (received_packets, received_bytes)
97

  
99 98
    def sentPackets(self):
100 99
        # if you experience assertion errors, you should
101 100
        # try to make sleep the mininet thread for a second
......
103 102
        sent_bytes = 0
104 103
        hosts = self.values()
105 104
        for h in hosts:
106
            p,b = self.hostSentPackets(h)
105
            p, b = self.hostSentPackets(h)
107 106
            sent_packets += p
108 107
            sent_bytes += b
109
        return (sent_packets,sent_bytes)
108
        return (sent_packets, sent_bytes)
109

  
110 110

  
111 111
class GraphNet(PowerNet):
112 112
    def __init__(self, edges_file, draw=True, **params):
......
117 117
        super(GraphNet, self).__init__(**params)
118 118
        info("\nReading "+edges_file+"\n")
119 119

  
120
        g = loadGraph(edges_file, connected=True)
120
        g = gu.loadGraph(edges_file, connected=True)
121 121

  
122 122
        nodeCounter = 0
123 123
        nodeMap = {}
......
142 142
        for e in self.gg.edges(data=True):
143 143
            # 10 Mbps, 5ms delay, 10% loss, 1000 packet queue
144 144
            # htp: Hierarchical Token Bucket rate limiter
145
#            quality_params = {"bw":10,"delay":'5ms', "loss":100-100.0/e[2]['weight'], "use_htb":True}
146 145
            quality_params = {}
147 146
            quality_params.update(self.link_opts)
148
            #quality_params["bw"] = 10
149
            #quality_params["delay"] = '0.515ms'
150
            #quality_params["jitter"] = '0.284ms'
151
            #quality_params["delay_distribution"] = 'wifi_m0.515_s0.284'
147
            # quality_params["bw"] = 10
148
            # quality_params["delay"] = '0.515ms'
149
            # quality_params["jitter"] = '0.284ms'
150
            # quality_params["delay_distribution"] = 'wifi_m0.515_s0.284'
152 151
            if "loss" in quality_params.keys():
153 152
                if quality_params["loss"] == "wifi_loss":
154 153
                    quality_params["loss"] = \
......
162 161
            self.insertLink(self.get(e[0]), self.get(e[1]), quality_params)
163 162

  
164 163
        if draw:
165
            showGraph(self.gg)
164
            nx.draw(self.gg)
166 165

  
167 166
    def pickHostAddrPort(self, node):
168 167
        port = self.hosts_port[node.name]
......
193 192
                        debug("\tNextHop node: "+nextHop.name+'\n')
194 193
                        dsts = self.getNodeAddrs(self.get(node2))
195 194
                        intfs = host1.connectionsTo(nextHop)
196
                        nextAddrs = [ couple[1].ip for couple in intfs if couple[1].ip ]
197
                        rintf = intfs[0][0] # WARNING we just consider one link
195
                        nextAddrs = [couple[1].ip
196
                                     for couple in intfs if couple[1].ip]
197
                        rintf = intfs[0][0]  # WARNING we just consider one link
198 198
                        for dst in dsts:
199 199
                            for addr in nextAddrs:
200
                                debug("\tip route add "+str(dst)+" via "+str(addr)+'\n')
201
                                host1.cmd("ip route add "+dst+" via "+addr+" dev "+rintf.name)
202
                                debug("\tip route add "+dst+" via "+addr+'\n')
203
                    else :
200
                                debug("\tip route add "+str(dst) +
201
                                      " via " + str(addr)+'\n')
202
                                host1.cmd("ip route add " + dst +
203
                                          " via " + addr + " dev "+rintf.name)
204
                                debug("\tip route add " + dst +
205
                                      " via " + addr + '\n')
206
                    else:
204 207
                        host2 = self.get(node2)
205
                        intfs = [ couple[0] for couple in host1.connectionsTo(host2) ]
206
                        rintf = intfs[0] # WARNING we just consider one link
208
                        intfs = [couple[0]
209
                                 for couple in host1.connectionsTo(host2)]
210
                        rintf = intfs[0]  # WARNING we just consider one link
207 211
                        raddrs = self.getNodeAddrs(host2)
208 212
                        for addr in raddrs:
209
                            host1.setHostRoute(addr,rintf.name)
210

  
211

  
213
                            host1.setHostRoute(addr, rintf.name)

Also available in: Unified diff