blob: d09d3b8c07b29e45c416a9f63066093f89f2eef2 [file] [log] [blame]
Alexander Laneea2d5d62019-10-04 16:48:52 -05001# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2#
dulalsaurab20855442021-05-21 20:37:03 +00003# Copyright (C) 2015-2021, The University of Memphis,
Alexander Laneea2d5d62019-10-04 16:48:52 -05004# Arizona Board of Regents,
5# Regents of the University of California.
6#
7# This file is part of Mini-NDN.
8# See AUTHORS.md for a complete list of Mini-NDN authors and contributors.
9#
10# Mini-NDN is free software: you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation, either version 3 of the License, or
13# (at your option) any later version.
14#
15# Mini-NDN is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with Mini-NDN, e.g., in COPYING.md file.
22# If not, see <http://www.gnu.org/licenses/>.
23
24import argparse
25import sys
Alexander Laneea2d5d62019-10-04 16:48:52 -050026import configparser
dulalsaurab20855442021-05-21 20:37:03 +000027from subprocess import Popen, PIPE
Alexander Laneea2d5d62019-10-04 16:48:52 -050028
Alexander Laneea2d5d62019-10-04 16:48:52 -050029from mininet.log import info, debug
30
31from mn_wifi.topo import Topo as Topo_WiFi
32from mn_wifi.net import Mininet_wifi
Alexander Laneea2d5d62019-10-04 16:48:52 -050033from mn_wifi.link import WirelessLink
34
35from minindn.minindn import Minindn
36
37class MinindnWifi(Minindn):
Alex Lane407c5f02021-03-09 22:13:23 -060038 """ Class for handling default args, Mininet-wifi object and home directories """
39 def __init__(self, parser=argparse.ArgumentParser(), topo=None, topoFile=None, noTopo=False, link=WirelessLink, **mininetParams):
Alexander Laneea2d5d62019-10-04 16:48:52 -050040 """Create Mini-NDN-Wifi object
41 parser: Parent parser of Mini-NDN-Wifi parser (use to specify experiment arguments)
42 topo: Mininet topo object (optional)
43 topoFile: topology file location (optional)
Alex Lane407c5f02021-03-09 22:13:23 -060044 noTopo: Allows specification of topology after network object is initialized (optional)
45 link: Allows specification of default Mininet/Mininet-Wifi link type for connections between nodes (optional)
Alexander Laneea2d5d62019-10-04 16:48:52 -050046 mininetParams: Any params to pass to Mininet-WiFi
47 """
48 self.parser = self.parseArgs(parser)
49 self.args = self.parser.parse_args()
50
51 Minindn.workDir = self.args.workDir
52 Minindn.resultDir = self.args.resultDir
53
54 self.topoFile = None
55 if not topoFile:
56 # Args has default topology if none specified
57 self.topoFile = self.args.topoFile
58 else:
59 self.topoFile = topoFile
60
Alex Lane407c5f02021-03-09 22:13:23 -060061 if topo is None and not noTopo:
Alexander Laneea2d5d62019-10-04 16:48:52 -050062 try:
63 info('Using topology file {}\n'.format(self.topoFile))
64 self.topo = self.processTopo(self.topoFile)
65 except configparser.NoSectionError as e:
66 info('Error reading config file: {}\n'.format(e))
67 sys.exit(1)
68 else:
69 self.topo = topo
70
Alex Lane407c5f02021-03-09 22:13:23 -060071 if not noTopo:
72 self.net = Mininet_wifi(topo=self.topo, ifb=self.args.ifb, link=link, **mininetParams)
73 else:
74 self.net = Mininet_wifi(ifb=self.args.ifb, link=link, **mininetParams)
Alexander Laneea2d5d62019-10-04 16:48:52 -050075
Alex Lane407c5f02021-03-09 22:13:23 -060076 # Prevents crashes running mixed topos
77 nodes = self.net.stations + self.net.hosts + self.net.cars
78 self.initParams(nodes)
79
Alexander Laneea2d5d62019-10-04 16:48:52 -050080 try:
81 process = Popen(['ndnsec-get-default', '-k'], stdout=PIPE, stderr=PIPE)
82 output, error = process.communicate()
83 if process.returncode == 0:
Alex Lane407c5f02021-03-09 22:13:23 -060084 Minindn.ndnSecurityDisabled = '/dummy/KEY/-%9C%28r%B8%AA%3B%60' in output
85 info('Dummy key chain patch is installed in ndn-cxx. Security will be disabled.\n')
Alexander Laneea2d5d62019-10-04 16:48:52 -050086 else:
Alex Lane407c5f02021-03-09 22:13:23 -060087 debug(error)
Alexander Laneea2d5d62019-10-04 16:48:52 -050088 except:
89 pass
90
91 self.cleanups = []
92
93 @staticmethod
94 def parseArgs(parent):
95 parser = argparse.ArgumentParser(prog='minindn-wifi', parents=[parent], add_help=False)
96
97 # nargs='?' required here since optional argument
98 parser.add_argument('topoFile', nargs='?', default='/usr/local/etc/mini-ndn/singleap-topology.conf',
99 help='If no template_file is given, topologies/wifi/singleap-topology.conf will be used.')
100
101 parser.add_argument('--work-dir', action='store', dest='workDir', default='/tmp/minindn',
102 help='Specify the working directory; default is /tmp/minindn')
103
104 parser.add_argument('--result-dir', action='store', dest='resultDir', default=None,
105 help='Specify the full path destination folder where experiment results will be moved')
106
107 parser.add_argument('--mobility',action='store_true',dest='mobility',default=False,
108 help='Enable custom mobility for topology (defined in topology file)')
109
110 parser.add_argument('--model-mob',action='store_true',dest='modelMob',default=False,
111 help='Enable model mobility for topology (defined in topology file)')
112
113 parser.add_argument('--ifb',action='store_true',dest='ifb',default=False,
114 help='Simulate delay on receiver-side by use of virtual IFB devices (see docs)')
115
116 return parser
117
118 @staticmethod
119 def processTopo(topoFile):
120 config = configparser.ConfigParser(delimiters=' ')
121 config.read(topoFile)
122 topo = Topo_WiFi()
123
124 items = config.items('stations')
125 debug("Stations")
126 for item in items:
127 debug(item[0].split(':'))
128 name = item[0].split(':')[0]
129 params = {}
130 for param in item[1].split(' '):
131 if param == "_":
132 continue
133 key = param.split('=')[0]
134 value = param.split('=')[1]
135 if key in ['range']:
136 value = int(value)
137 params[key] = value
138
139 topo.addStation(name, **params)
140
141 try:
142 debug("Switches")
143 items = config.items('switches')
144 for item in items:
145 debug(item[0].split(':'))
146 name = item[0].split(':')[0]
147 topo.addSwitch(name)
148 except configparser.NoSectionError:
149 debug("Switches are optional")
150 pass
151
152 try:
153 debug("APs")
154 items = config.items('accessPoints')
155 for item in items:
156 debug(item[0].split(':'))
157 name = item[0].split(':')[0]
158 ap_params = {}
159 for param in item[1].split(' '):
160 if param == "_":
161 continue
162 key = param.split('=')[0]
163 value = param.split('=')[1]
164 if key in ['range']:
165 value = int(value)
166 ap_params[key] = value
167 topo.addAccessPoint(name, **ap_params)
168 except configparser.NoSectionError:
169 debug("APs are optional")
170 pass
171
172 items = config.items('links')
173 debug("Links")
174 for item in items:
175 link = item[0].split(':')
176 debug(link)
177 params = {}
178 for param in item[1].split(' '):
179 if param == "_":
180 continue
181 key = param.split('=')[0]
182 value = param.split('=')[1]
183 if key in ['bw', 'jitter', 'max_queue_size']:
184 value = int(value)
185 if key == 'loss':
186 value = float(value)
187 params[key] = value
188
189 topo.addLink(link[0], link[1], **params)
190
191 return topo
192
193 def startMobility(self, max_x=1000, max_y=1000, **kwargs):
194 """ Method to run a basic mobility setup on your net"""
195 self.net.plotGraph(max_x=max_x, max_y=max_y)
196 self.net.startMobility(**kwargs)
197
198 def startMobilityModel(self, max_x=1000, max_y=1000, **kwargs):
199 """ Method to run a mobility model on your net until exited"""
200 self.net.plotGraph(max_x=max_x, max_y=max_y)
201 self.net.setMobilityModel(**kwargs)