blob: d1ab0a980406580b5b467867442c3e786f526813 [file] [log] [blame]
Alexander Laneea2d5d62019-10-04 16:48:52 -05001# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2#
3# Copyright (C) 2015-2020, The University of Memphis,
4# 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
26import time
27import os
28import configparser
29from subprocess import call, check_output, Popen
30from sys import exit
31
32from mininet.link import TCLink
33from mininet.node import Switch
34from mininet.util import ipStr, ipParse
35from mininet.log import info, debug
36
37from mn_wifi.topo import Topo as Topo_WiFi
38from mn_wifi.net import Mininet_wifi
39from mn_wifi.node import OVSKernelAP
40from mn_wifi.link import WirelessLink
41
42from minindn.minindn import Minindn
43
44class MinindnWifi(Minindn):
Alex Lane407c5f02021-03-09 22:13:23 -060045 """ Class for handling default args, Mininet-wifi object and home directories """
46 def __init__(self, parser=argparse.ArgumentParser(), topo=None, topoFile=None, noTopo=False, link=WirelessLink, **mininetParams):
Alexander Laneea2d5d62019-10-04 16:48:52 -050047 """Create Mini-NDN-Wifi object
48 parser: Parent parser of Mini-NDN-Wifi parser (use to specify experiment arguments)
49 topo: Mininet topo object (optional)
50 topoFile: topology file location (optional)
Alex Lane407c5f02021-03-09 22:13:23 -060051 noTopo: Allows specification of topology after network object is initialized (optional)
52 link: Allows specification of default Mininet/Mininet-Wifi link type for connections between nodes (optional)
Alexander Laneea2d5d62019-10-04 16:48:52 -050053 mininetParams: Any params to pass to Mininet-WiFi
54 """
55 self.parser = self.parseArgs(parser)
56 self.args = self.parser.parse_args()
57
58 Minindn.workDir = self.args.workDir
59 Minindn.resultDir = self.args.resultDir
60
61 self.topoFile = None
62 if not topoFile:
63 # Args has default topology if none specified
64 self.topoFile = self.args.topoFile
65 else:
66 self.topoFile = topoFile
67
Alex Lane407c5f02021-03-09 22:13:23 -060068 if topo is None and not noTopo:
Alexander Laneea2d5d62019-10-04 16:48:52 -050069 try:
70 info('Using topology file {}\n'.format(self.topoFile))
71 self.topo = self.processTopo(self.topoFile)
72 except configparser.NoSectionError as e:
73 info('Error reading config file: {}\n'.format(e))
74 sys.exit(1)
75 else:
76 self.topo = topo
77
Alex Lane407c5f02021-03-09 22:13:23 -060078 if not noTopo:
79 self.net = Mininet_wifi(topo=self.topo, ifb=self.args.ifb, link=link, **mininetParams)
80 else:
81 self.net = Mininet_wifi(ifb=self.args.ifb, link=link, **mininetParams)
Alexander Laneea2d5d62019-10-04 16:48:52 -050082
Alex Lane407c5f02021-03-09 22:13:23 -060083 # Prevents crashes running mixed topos
84 nodes = self.net.stations + self.net.hosts + self.net.cars
85 self.initParams(nodes)
86
Alexander Laneea2d5d62019-10-04 16:48:52 -050087 try:
88 process = Popen(['ndnsec-get-default', '-k'], stdout=PIPE, stderr=PIPE)
89 output, error = process.communicate()
90 if process.returncode == 0:
Alex Lane407c5f02021-03-09 22:13:23 -060091 Minindn.ndnSecurityDisabled = '/dummy/KEY/-%9C%28r%B8%AA%3B%60' in output
92 info('Dummy key chain patch is installed in ndn-cxx. Security will be disabled.\n')
Alexander Laneea2d5d62019-10-04 16:48:52 -050093 else:
Alex Lane407c5f02021-03-09 22:13:23 -060094 debug(error)
Alexander Laneea2d5d62019-10-04 16:48:52 -050095 except:
96 pass
97
98 self.cleanups = []
99
100 @staticmethod
101 def parseArgs(parent):
102 parser = argparse.ArgumentParser(prog='minindn-wifi', parents=[parent], add_help=False)
103
104 # nargs='?' required here since optional argument
105 parser.add_argument('topoFile', nargs='?', default='/usr/local/etc/mini-ndn/singleap-topology.conf',
106 help='If no template_file is given, topologies/wifi/singleap-topology.conf will be used.')
107
108 parser.add_argument('--work-dir', action='store', dest='workDir', default='/tmp/minindn',
109 help='Specify the working directory; default is /tmp/minindn')
110
111 parser.add_argument('--result-dir', action='store', dest='resultDir', default=None,
112 help='Specify the full path destination folder where experiment results will be moved')
113
114 parser.add_argument('--mobility',action='store_true',dest='mobility',default=False,
115 help='Enable custom mobility for topology (defined in topology file)')
116
117 parser.add_argument('--model-mob',action='store_true',dest='modelMob',default=False,
118 help='Enable model mobility for topology (defined in topology file)')
119
120 parser.add_argument('--ifb',action='store_true',dest='ifb',default=False,
121 help='Simulate delay on receiver-side by use of virtual IFB devices (see docs)')
122
123 return parser
124
125 @staticmethod
126 def processTopo(topoFile):
127 config = configparser.ConfigParser(delimiters=' ')
128 config.read(topoFile)
129 topo = Topo_WiFi()
130
131 items = config.items('stations')
132 debug("Stations")
133 for item in items:
134 debug(item[0].split(':'))
135 name = item[0].split(':')[0]
136 params = {}
137 for param in item[1].split(' '):
138 if param == "_":
139 continue
140 key = param.split('=')[0]
141 value = param.split('=')[1]
142 if key in ['range']:
143 value = int(value)
144 params[key] = value
145
146 topo.addStation(name, **params)
147
148 try:
149 debug("Switches")
150 items = config.items('switches')
151 for item in items:
152 debug(item[0].split(':'))
153 name = item[0].split(':')[0]
154 topo.addSwitch(name)
155 except configparser.NoSectionError:
156 debug("Switches are optional")
157 pass
158
159 try:
160 debug("APs")
161 items = config.items('accessPoints')
162 for item in items:
163 debug(item[0].split(':'))
164 name = item[0].split(':')[0]
165 ap_params = {}
166 for param in item[1].split(' '):
167 if param == "_":
168 continue
169 key = param.split('=')[0]
170 value = param.split('=')[1]
171 if key in ['range']:
172 value = int(value)
173 ap_params[key] = value
174 topo.addAccessPoint(name, **ap_params)
175 except configparser.NoSectionError:
176 debug("APs are optional")
177 pass
178
179 items = config.items('links')
180 debug("Links")
181 for item in items:
182 link = item[0].split(':')
183 debug(link)
184 params = {}
185 for param in item[1].split(' '):
186 if param == "_":
187 continue
188 key = param.split('=')[0]
189 value = param.split('=')[1]
190 if key in ['bw', 'jitter', 'max_queue_size']:
191 value = int(value)
192 if key == 'loss':
193 value = float(value)
194 params[key] = value
195
196 topo.addLink(link[0], link[1], **params)
197
198 return topo
199
200 def startMobility(self, max_x=1000, max_y=1000, **kwargs):
201 """ Method to run a basic mobility setup on your net"""
202 self.net.plotGraph(max_x=max_x, max_y=max_y)
203 self.net.startMobility(**kwargs)
204
205 def startMobilityModel(self, max_x=1000, max_y=1000, **kwargs):
206 """ Method to run a mobility model on your net until exited"""
207 self.net.plotGraph(max_x=max_x, max_y=max_y)
208 self.net.setMobilityModel(**kwargs)