Configure DNS for all nodes in the network by updating /etc/hosts
in each node's namespace. Include interface IPs with descriptive names.
Source code in emulation/mnet/run_mn.py
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111 | def configure_dns(net, graph):
'''
Configure DNS for all nodes in the network by updating /etc/hosts
in each node's namespace. Include interface IPs with descriptive names.
'''
# First, collect all IP addresses and hostnames
hosts_entries = []
# Add satellite nodes loopback addresses
for name in torus_topo.satellites(graph):
node = graph.nodes[name]
if "ip" in node:
hosts_entries.append(f"{format(node['ip'].ip)}\t{name}")
# Add interface IPs with descriptive names
for neighbor in graph.adj[name]:
edge = graph.adj[name][neighbor]
local_ip = edge["ip"][name]
remote_ip = edge["ip"][neighbor]
local_intf = edge["intf"][name]
remote_intf = edge["intf"][neighbor]
# Add entries for both local and remote interfaces
# Format: IP devicename-intf devicename-TO-neighborname
hosts_entries.append(f"{format(local_ip.ip)}\t{local_intf} {name}-TO-{neighbor}")
hosts_entries.append(f"{format(remote_ip.ip)}\t{remote_intf} {neighbor}-TO-{name}")
# Add ground stations
for name in torus_topo.ground_stations(graph):
node = graph.nodes[name]
if "ip" in node:
hosts_entries.append(f"{format(node['ip'].ip)}\t{name}")
# Create hosts file content
hosts_content = "\n".join([
"127.0.0.1\tlocalhost",
"::1\tlocalhost ip6-localhost ip6-loopback",
"fe00::0\tip6-localnet",
"ff00::0\tip6-mcastprefix",
"ff02::1\tip6-allnodes",
"ff02::2\tip6-allrouters",
"\n# Network hosts",
*hosts_entries
])
# Update /etc/hosts in each node's namespace
for node in net.hosts:
# Create a temporary hosts file
with open('/tmp/hosts.temp', 'w') as f:
f.write(hosts_content)
# Copy the file to the node's namespace
node.cmd(f'mkdir -p /etc/netns/{node.name}')
node.cmd(f'cp /tmp/hosts.temp /etc/netns/{node.name}/hosts')
# Also update the current namespace's hosts file
node.cmd('cp /tmp/hosts.temp /etc/hosts')
# Clean up
node.cmd('rm /tmp/hosts.temp')
# Configure resolv.conf to use the hosts file
resolv_content = "nameserver 127.0.0.1\nsearch mininet"
node.cmd(f'echo "{resolv_content}" > /etc/netns/{node.name}/resolv.conf')
node.cmd(f'echo "{resolv_content}" > /etc/resolv.conf')
|