Revision b1497c9f

View differences:

.gitmodules
1
[submodule "community_networks_analysis"]
2
	path = community_networks_analysis
3
	url = https://ans.disi.unitn.it/redmine/community-networks-analyser.git
community_networks_analysis
1
Subproject commit ec05b0d21fa86e6715fdadb5b20f082081da4880
graph_utils.py
1
import json
2
import sys
3
import networkx as nx
4

  
5

  
6
def load_json(json_file):
7
    """ import a json file in NetJSON format, convert to Graph class
8
    Parameters
9
    ----------
10
    json_file : string with file path
11
    """
12

  
13
    try:
14
        file_p = open(json_file, "r")
15
    except IOError:
16
        raise
17
    try:
18
        netjson_net = json.load(file_p)
19
    except ValueError as err:
20
        print "Could not decode file", err
21
    # TODO add a schema to validate the subset of features we are
22
    # able to consider
23

  
24
    G = nx.Graph()
25
    cost_label = ""
26
    if "metric" in netjson_net and netjson_net["metric"] == "ETX":
27
        cost_label = "cost"
28
    for node in netjson_net["nodes"]:
29
        G.add_node(node["id"])
30
    for link in netjson_net["links"]:
31
        if cost_label:
32
            cost = float(link["cost"])
33
        else:
34
            cost = 1.0
35
        G.add_edge(link["source"], link["target"],
36
                   {"weight": cost})
37
    return G
38

  
39

  
40
def loadGraph(fname, remap=False, connected=True, silent=False):
41
    """ Parameters
42
    --------------
43
    fname : string
44
        filname to open
45
    remap : bool
46
        remap the labels to a sequence of integers
47
    connected : bool
48
        return only the larges component subgraph
49

  
50
    """
51
    G = nx.Graph()
52
    if not silent:
53
        print "Loading/Generating Graph"
54
    # load a file using networkX adjacency matrix structure
55
    if fname.lower().endswith(".adj"):
56
        try:
57
            G = nx.read_adjlist(fname, nodetype=int)
58
        except IOError as err:
59
            print
60
            print err
61
            sys.exit(1)
62
    # load a file using networkX .edges structure
63
    elif fname.lower().endswith(".edges"):
64
        try:
65
            G = nx.read_weighted_edgelist(fname, nodetype=int)
66
        except IOError as err:
67
            print
68
            print err
69
            sys.exit(1)
70
    # load a a network in NetJSON format
71
    elif fname.lower().endswith(".json"):
72
        try:
73
            G = load_json(fname)
74
        except IOError as err:
75
            print
76
            print err
77
            sys.exit(1)
78
    else:
79
        print >> sys.stderr, "Error: Allowed file extensions are .adj for",\
80
            "adjacency matrix, .json for netjson and .edges for edge-list"
81
        sys.exit(1)
82
    if connected:
83
        C = sorted(list(nx.connected_component_subgraphs(G)),
84
                   key=len, reverse=True)[0]
85
        G = C
86
    if not silent:
87
        print >> sys.stderr, "Graph", fname, "loaded",
88
    # remap node labels so we don't have "holes" in the numbering
89
    if remap:
90
        mapping = dict(zip(G.nodes(), range(G.order())))
91
        H = nx.relabel_nodes(G, mapping)
92
        return H
93
    return G
nepa_test.py
11 11
from parameters_parser import parameters
12 12
from network_builder import *
13 13
from test_code import *
14
from mininet.log import setLogLevel
14 15

  
15 16

  
16 17

  
......
36 37
        self.parser = ConfigParser.SafeConfigParser()
37 38
        self.parser.read(fileName)
38 39

  
39
        self.testName = stanza 
40
        self.testName = stanza
40 41
        if stanza not in self.parser.sections():
41 42
            error("Can not find configuration " + stanza \
42 43
                    + " in file " + fileName + "\n")
......
140 141
        net.setShortestRoutes()
141 142
    #CLI(net)
142 143
    graphname = networkGraph.split('/')[-1].split('.')[0]
143
    testPath = testName + "_" + graphname + "_" + str(int(time())) 
144
    testPath = testName + "_" + graphname + "_" + str(int(time()))
144 145
    for i in range(int(C.getConfigurations("times"))):
145 146
        info("+++++++ Round: "+str(i+1) + '\n')
146 147
        test = C.className(net, testPath, C.confParams)
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