Skip to content

Commit ba1956c

Browse files
authored
Merge pull request #16 from netgroup/add-dns-solution
Add a DNS solution to the Mininet emulation
2 parents 74c0653 + 556996c commit ba1956c

File tree

10 files changed

+304
-41
lines changed

10 files changed

+304
-41
lines changed

nets/8r-1c-in-band-isis/etc-hosts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
127.0.0.1 localhost
2-
127.0.1.1 rose-srv6
3-
4-
# The following lines are desirable for IPv6 capable hosts
5-
::1 ip6-localhost ip6-loopback
6-
fe00::0 ip6-localnet
7-
ff00::0 ip6-mcastprefix
8-
ff02::1 ip6-allnodes
9-
ff02::2 ip6-allrouters
10-
111
# Mininet hosts
2+
3+
# Controller
124
fcff:2:c::2 controller
5+
6+
# Routers
137
fcff:1::1 r1
148
fcff:2::1 r2
159
fcff:3::1 r3
@@ -18,6 +12,8 @@ fcff:5::1 r5
1812
fcff:6::1 r6
1913
fcff:7::1 r7
2014
fcff:8::1 r8
15+
16+
# Hosts
2117
fd00:0:11::2 h11
2218
fd00:0:12::2 h12
2319
fd00:0:13::2 h13
@@ -30,6 +26,8 @@ fd00:0:53::2 h53
3026
fd00:0:81::2 h81
3127
fd00:0:82::2 h82
3228
fd00:0:83::2 h83
29+
30+
# Data-centers
3331
fcff:2:1::2 hdc1
3432
fcff:8:1::2 hdc2
3533
fcff:5:1::2 hdc3

nets/8r-1c-in-band-isis/isis8d.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
if os.path.exists('.venv'):
1111
with open('.venv', 'r') as venv_file:
1212
# Get virtualenv path from .venv file
13-
venv_path = venv_file.read()
13+
# and remove trailing newline chars
14+
venv_path = venv_file.read().rstrip()
1415
# Get path of the activation script
1516
venv_path = os.path.join(venv_path, 'bin/activate_this.py')
1617
if not os.path.exists(venv_path):
@@ -24,6 +25,7 @@
2425
exec(code, {'__file__': venv_path})
2526

2627
from argparse import ArgumentParser
28+
import python_hosts
2729
import shutil
2830
from dotenv import load_dotenv
2931
from mininet.topo import Topo
@@ -40,6 +42,14 @@
4042

4143
PRIVDIR = '/var/priv'
4244

45+
# Path of the file containing the entries (ip-hostname)
46+
# to be added to /etc/hosts
47+
ETC_HOSTS_FILE = './etc-hosts'
48+
49+
# Define whether to add Mininet nodes to /etc/hosts file or not
50+
ADD_ETC_HOSTS = True
51+
52+
# Define whether to start the node managers on the routers or not
4353
START_NODE_MANAGERS = False
4454

4555
# Load environment variables from .env file
@@ -225,6 +235,28 @@ def create_topo(my_net):
225235
add_link(my_net, controller,r2)
226236

227237

238+
def add_nodes_to_etc_hosts():
239+
# Get /etc/hosts
240+
etc_hosts = python_hosts.hosts.Hosts()
241+
# Import host-ip mapping defined in etc-hosts file
242+
count = etc_hosts.import_file(ETC_HOSTS_FILE)
243+
# Print results
244+
count = count['add_result']['ipv6_count'] + count['add_result']['ipv4_count']
245+
print('*** Added %s entries to /etc/hosts\n' % count)
246+
247+
248+
def remove_nodes_from_etc_hosts(net):
249+
print('*** Removing entries from /etc/hosts\n')
250+
# Get /etc/hosts
251+
etc_hosts = python_hosts.hosts.Hosts()
252+
for host in net.hosts:
253+
# Remove all the nodes from /etc/hosts
254+
etc_hosts.remove_all_matching(name=str(host))
255+
# Write changes to /etc/hosts
256+
etc_hosts.write()
257+
258+
259+
228260
def stopAll():
229261
# Clean Mininet emulation environment
230262
os.system('sudo mn -c')
@@ -258,7 +290,16 @@ def simpleTest():
258290
for host in net.hosts:
259291
file.write("%s %d\n" % (host, extractHostPid( repr(host) )) )
260292

293+
# Add Mininet nodes to /etc/hosts
294+
if ADD_ETC_HOSTS:
295+
add_nodes_to_etc_hosts()
296+
261297
CLI( net )
298+
299+
# Remove Mininet nodes from /etc/hosts
300+
if ADD_ETC_HOSTS:
301+
remove_nodes_from_etc_hosts(net)
302+
262303
net.stop()
263304
stopAll()
264305

@@ -274,6 +315,11 @@ def parse_arguments():
274315
action='store_true', default=False,
275316
help='Define whether to start node manager on routers or not'
276317
)
318+
parser.add_argument(
319+
'--no-etc-hosts', dest='add_etc_hosts',
320+
action='store_false', default=True,
321+
help='Define whether to add Mininet nodes to /etc/hosts file or not'
322+
)
277323
# Parse input parameters
278324
args = parser.parse_args()
279325
# Return the arguments
@@ -302,6 +348,8 @@ def parse_arguments():
302348
'NODE_MANAGER_GRPC_PORT variable')
303349
print('NODE_MANAGER_GRPC_PORT variable not set in .env file\n')
304350
exit(-2)
351+
# Define whether to add Mininet nodes to /etc/hosts file or not
352+
ADD_ETC_HOSTS = args.add_etc_hosts
305353
# Tell mininet to print useful information
306354
setLogLevel('info')
307355
simpleTest()

nets/8r-1c-out-band-isis/etc-hosts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
1-
127.0.0.1 localhost
2-
127.0.1.1 rose-srv6
3-
4-
# The following lines are desirable for IPv6 capable hosts
5-
::1 ip6-localhost ip6-loopback
6-
fe00::0 ip6-localnet
7-
ff00::0 ip6-mcastprefix
8-
ff02::1 ip6-allnodes
9-
ff02::2 ip6-allrouters
10-
111
# Mininet hosts
2+
3+
# Routers
124
fcff:1::1 r1
135
fcff:2::1 r2
146
fcff:3::1 r3
@@ -17,6 +9,8 @@ fcff:5::1 r5
179
fcff:6::1 r6
1810
fcff:7::1 r7
1911
fcff:8::1 r8
12+
13+
# Hosts
2014
fd00:0:11::2 h11
2115
fd00:0:12::2 h12
2216
fd00:0:13::2 h13
@@ -29,10 +23,13 @@ fd00:0:53::2 h53
2923
fd00:0:81::2 h81
3024
fd00:0:82::2 h82
3125
fd00:0:83::2 h83
26+
27+
# Data-centers
3228
fcff:2:1::2 hdc1
3329
fcff:8:1::2 hdc2
3430
fcff:5:1::2 hdc3
3531

32+
# Management network
3633
fcfd:0:0:fd::1 controller.m
3734
fcfd:0:0:1::1 r1.m
3835
fcfd:0:0:2::1 r2.m

nets/8r-1c-out-band-isis/isis8d.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
exec(code, {'__file__': venv_path})
2626

2727
from argparse import ArgumentParser
28+
import python_hosts
2829
import shutil
2930
from dotenv import load_dotenv
3031
from mininet.topo import Topo
@@ -41,6 +42,14 @@
4142

4243
PRIVDIR = '/var/priv'
4344

45+
# Path of the file containing the entries (ip-hostname)
46+
# to be added to /etc/hosts
47+
ETC_HOSTS_FILE = './etc-hosts'
48+
49+
# Define whether to add Mininet nodes to /etc/hosts file or not
50+
ADD_ETC_HOSTS = True
51+
52+
# Define whether to start the node managers on the routers or not
4453
START_NODE_MANAGERS = False
4554

4655
# Load environment variables from .env file
@@ -275,6 +284,32 @@ def create_topo(my_net):
275284
add_link(my_net, r8, sw)
276285

277286

287+
def add_nodes_to_etc_hosts():
288+
# Get /etc/hosts
289+
etc_hosts = python_hosts.hosts.Hosts()
290+
# Import host-ip mapping defined in etc-hosts file
291+
count = etc_hosts.import_file(ETC_HOSTS_FILE)
292+
# Print results
293+
count = count['add_result']['ipv6_count'] + count['add_result']['ipv4_count']
294+
print('*** Added %s entries to /etc/hosts\n' % count)
295+
296+
297+
def remove_nodes_from_etc_hosts(net):
298+
print('*** Removing entries from /etc/hosts\n')
299+
# Get /etc/hosts
300+
etc_hosts = python_hosts.hosts.Hosts()
301+
for host in net.hosts:
302+
# Remove all the nodes from /etc/hosts
303+
etc_hosts.remove_all_matching(name=str(host))
304+
# Remove entries related to the management network
305+
# These entries are in the form *.m (e.g. r1.m, controller.m)
306+
# therefore they are not removed during the previous loop
307+
for host in net.hosts:
308+
etc_hosts.remove_all_matching(name='%s.m' % host)
309+
# Write changes to /etc/hosts
310+
etc_hosts.write()
311+
312+
278313
def stopAll():
279314
# Clean Mininet emulation environment
280315
os.system('sudo mn -c')
@@ -308,7 +343,16 @@ def simpleTest():
308343
for host in net.hosts:
309344
file.write("%s %d\n" % (host, extractHostPid( repr(host) )) )
310345

346+
# Add Mininet nodes to /etc/hosts
347+
if ADD_ETC_HOSTS:
348+
add_nodes_to_etc_hosts()
349+
311350
CLI( net )
351+
352+
# Remove Mininet nodes from /etc/hosts
353+
if ADD_ETC_HOSTS:
354+
remove_nodes_from_etc_hosts(net)
355+
312356
net.stop()
313357
stopAll()
314358

@@ -324,6 +368,11 @@ def parse_arguments():
324368
action='store_true', default=False,
325369
help='Define whether to start node manager on routers or not'
326370
)
371+
parser.add_argument(
372+
'--no-etc-hosts', dest='add_etc_hosts',
373+
action='store_false', default=True,
374+
help='Define whether to add Mininet nodes to /etc/hosts file or not'
375+
)
327376
# Parse input parameters
328377
args = parser.parse_args()
329378
# Return the arguments
@@ -352,6 +401,8 @@ def parse_arguments():
352401
'NODE_MANAGER_GRPC_PORT variable')
353402
print('NODE_MANAGER_GRPC_PORT variable not set in .env file\n')
354403
exit(-2)
404+
# Define whether to add Mininet nodes to /etc/hosts file or not
405+
ADD_ETC_HOSTS = args.add_etc_hosts
355406
# Tell mininet to print useful information
356407
setLogLevel('info')
357408
simpleTest()

nets/8r-1c-srv6-pm/isis8d.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
exec(code, {'__file__': venv_path})
2626

2727
from argparse import ArgumentParser
28+
import python_hosts
2829
import shutil
2930
from dotenv import load_dotenv
3031
from mininet.topo import Topo
@@ -41,6 +42,14 @@
4142

4243
PRIVDIR = '/var/priv'
4344

45+
# Path of the file containing the entries (ip-hostname)
46+
# to be added to /etc/hosts
47+
ETC_HOSTS_FILE = './etc-hosts'
48+
49+
# Define whether to add Mininet nodes to /etc/hosts file or not
50+
ADD_ETC_HOSTS = True
51+
52+
# Define whether to start the node managers on the routers or not
4453
START_NODE_MANAGERS = False
4554

4655
# Load environment variables from .env file
@@ -275,6 +284,32 @@ def create_topo(my_net):
275284
add_link(my_net, r8, sw)
276285

277286

287+
def add_nodes_to_etc_hosts():
288+
# Get /etc/hosts
289+
etc_hosts = python_hosts.hosts.Hosts()
290+
# Import host-ip mapping defined in etc-hosts file
291+
count = etc_hosts.import_file(ETC_HOSTS_FILE)
292+
# Print results
293+
count = count['add_result']['ipv6_count'] + count['add_result']['ipv4_count']
294+
print('*** Added %s entries to /etc/hosts\n' % count)
295+
296+
297+
def remove_nodes_from_etc_hosts(net):
298+
print('*** Removing entries from /etc/hosts\n')
299+
# Get /etc/hosts
300+
etc_hosts = python_hosts.hosts.Hosts()
301+
for host in net.hosts:
302+
# Remove all the nodes from /etc/hosts
303+
etc_hosts.remove_all_matching(name=str(host))
304+
# Remove entries related to the management network
305+
# These entries are in the form *.m (e.g. r1.m, controller.m)
306+
# therefore they are not removed during the previous loop
307+
for host in net.hosts:
308+
etc_hosts.remove_all_matching(name='%s.m' % host)
309+
# Write changes to /etc/hosts
310+
etc_hosts.write()
311+
312+
278313
def stopAll():
279314
# Clean Mininet emulation environment
280315
os.system('sudo mn -c')
@@ -308,7 +343,16 @@ def simpleTest():
308343
for host in net.hosts:
309344
file.write("%s %d\n" % (host, extractHostPid( repr(host) )) )
310345

346+
# Add Mininet nodes to /etc/hosts
347+
if ADD_ETC_HOSTS:
348+
add_nodes_to_etc_hosts()
349+
311350
CLI( net )
351+
352+
# Remove Mininet nodes from /etc/hosts
353+
if ADD_ETC_HOSTS:
354+
remove_nodes_from_etc_hosts(net)
355+
312356
net.stop()
313357
stopAll()
314358

@@ -324,6 +368,11 @@ def parse_arguments():
324368
action='store_true', default=False,
325369
help='Define whether to start node manager on routers or not'
326370
)
371+
parser.add_argument(
372+
'--no-etc-hosts', dest='add_etc_hosts',
373+
action='store_false', default=True,
374+
help='Define whether to add Mininet nodes to /etc/hosts file or not'
375+
)
327376
# Parse input parameters
328377
args = parser.parse_args()
329378
# Return the arguments
@@ -352,6 +401,8 @@ def parse_arguments():
352401
'NODE_MANAGER_GRPC_PORT variable')
353402
print('NODE_MANAGER_GRPC_PORT variable not set in .env file\n')
354403
exit(-2)
404+
# Define whether to add Mininet nodes to /etc/hosts file or not
405+
ADD_ETC_HOSTS = args.add_etc_hosts
355406
# Tell mininet to print useful information
356407
setLogLevel('info')
357408
simpleTest()

0 commit comments

Comments
 (0)