Repository navigation
Expand file tree
/
Copy pathnlsr.py
More file actions
346 lines (287 loc) · 16 KB
/
Copy pathnlsr.py
File metadata and controls
346 lines (287 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
#
# Copyright (C) 2015-2025, The University of Memphis,
# Arizona Board of Regents,
# Regents of the University of California.
#
# This file is part of Mini-NDN.
# See AUTHORS.md for a complete list of Mini-NDN authors and contributors.
#
# Mini-NDN is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Mini-NDN is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Mini-NDN, e.g., in COPYING.md file.
# If not, see <http://www.gnu.org/licenses/>.
import os
from shlex import quote
import shutil
import sys
from typing import Dict, List, Optional, Tuple, Union
from mininet.clean import sh
from mininet.examples.cluster import RemoteMixin
from mininet.log import debug, warn
from mininet.node import Node, Switch
from minindn.apps.application import Application
from minindn.util import scp, copyExistentFile, MACToEther
from minindn.helpers.nfdc import Nfdc
from minindn.minindn import Minindn
class Nlsr(Application):
ROUTING_LINK_STATE = 'link-state'
ROUTING_HYPERBOLIC = 'hr'
ROUTING_DRY_RUN = 'dry'
SYNC_PSYNC = 'psync'
def __init__(self, node: Node, logLevel: str ='NONE', security: bool = False, sync: str = SYNC_PSYNC,
faceType: str = Nfdc.PROTOCOL_UDP, nFaces: int = 3, routingType: str = ROUTING_LINK_STATE,
faceDict: Optional[Dict[Node, List[Tuple[str, str, str]]]] = None,
infoeditChanges: Optional[List[Union[Tuple[str, str], Tuple[str, str, str]]]] = None):
"""
Set NLSR NFD application through wrapper on node. Most arguments are directly from nlsr.conf,
so please reference that documentation for more information.
:param node: Mininet or Mininet-Wifi node object
:param logLevel: NLSR log level set as default (default "NONE")
:param security: Whether or not to set up signing and packet validation.
NDN security features are typically disabled in Mini-NDN for performance (default false)
:param sync: What sync to use with NLSR. PSync is the only one tested, but ndn-svs or chronosync
may be specified manually (default "psync")
:param faceType: What protocol to use to create for faces to neighbors during setup.
UDP faces are typically used in Mini-NDN (default "udp")
:param nFaces: Number of faces to maintain for each prefix after routing calculations (default 3)
:param routingType: Whether to use link state or hyperbolic routing. The latter requires additional setup and
is not recommended for novice users.
:param faceDict: If not creating faces via the included helper, this contains information on faces
to use as neighbors. This is helpful for running NLSR in any case where direct links between NDN routers
are not present in the topology (adhoc or infrastructure wifi, networks using IP switches or routers, etc).
:param infoeditChanges: Commands passed to infoedit other than the most commonly used arguments.
These either expect (key, value) (using the `section` command) or otherwise
(key, value, put|section|delete).
"""
Application.__init__(self, node)
try:
from mn_wifi.node import Node_wifi
if isinstance(node, Node_wifi) and faceDict == None:
warn("Wifi nodes need to have faces configured manually. Please see \
documentation on provided helper methods.\r\n")
sys.exit(1)
except ImportError:
pass
self.network = '/ndn/'
self.node = node
self.parameters = self.node.params['params']
if self.parameters.get('nlsr-log-level', None) != None:
logLevel = self.parameters.get('nlsr-log-level')
if logLevel in ['NONE', 'WARN', 'INFO', 'DEBUG', 'TRACE']:
self.envDict = {'NDN_LOG': 'nlsr.*={}'.format(logLevel)}
else:
self.envDict = {'NDN_LOG': logLevel}
self.logFile = 'nlsr.log'
self.routerName = '/{}C1.Router/cs/{}'.format('%', node.name)
self.confFile = '{}/nlsr.conf'.format(self.homeDir)
self.security = security
self.sync = sync
self.faceType = faceType
self.infocmd = 'infoedit -f nlsr.conf'
# Expected format- node : tuple (node name, IP, cost)
self.faceDict = faceDict
self.infoeditManualChanges = infoeditChanges
self.parameters = self.node.params['params']
self.nFaces = nFaces
if routingType == Nlsr.ROUTING_HYPERBOLIC:
self.hyperbolicState = 'on'
elif routingType == Nlsr.ROUTING_DRY_RUN:
self.hyperbolicState = 'dry-run'
else:
self.hyperbolicState = 'off'
self.hyperRadius = self.parameters.get('radius', 0.0)
self.hyperAngle = self.parameters.get('angle', 0.0)
if ((self.hyperbolicState == 'on' or self.hyperbolicState == 'dry-run') and
(self.hyperRadius == 0.0 or self.hyperAngle == 0.0)):
warn('Hyperbolic coordinates in topology file are either missing or misconfigured.')
warn('Check that each node has one radius value and one or two angle value(s).')
sys.exit(1)
self.neighborLocations = []
self.interfaceForNeighbor = dict()
possibleConfPaths = ['/usr/local/etc/ndn/nlsr.conf.sample', '/etc/ndn/nlsr.conf.sample']
copyExistentFile(node, possibleConfPaths, '{}/nlsr.conf'.format(self.homeDir))
self.createConfigFile()
if security and not Minindn.ndnSecurityDisabled:
self.createKeysAndCertificates()
def start(self):
self.createFaces()
Application.start(self, 'nlsr -f {}'.format(self.confFile), self.logFile, self.envDict)
Minindn.sleep(1)
def createFaces(self):
for location in self.neighborLocations:
if self.faceType == Nfdc.PROTOCOL_ETHER:
localIntf = self.interfaceForNeighbor[location]
Nfdc.createFace(self.node, location, self.faceType, localInterface=localIntf, isPermanent=True)
else:
Nfdc.createFace(self.node, location, self.faceType, isPermanent=True)
@staticmethod
def createKey(host: Node, name: str, outputFile: Union[str, bytes, os.PathLike]):
host.cmd('ndnsec-key-gen {} > {}'.format(name, outputFile))
@staticmethod
def createCertificate(host: Node, signer: str, keyFile: str, outputFile: Union[str, bytes, os.PathLike] ):
host.cmd('ndnsec-cert-gen -s {} -r {} > {}'.format(signer, keyFile, outputFile))
def createKeysAndCertificates(self):
securityDir = '{}/security'.format(Minindn.workDir)
if not os.path.exists(securityDir):
os.mkdir(securityDir)
rootName = self.network
rootCertFile = '{}/root.cert'.format(securityDir)
if not os.path.isfile(rootCertFile):
# Create root certificate
sh('ndnsec-key-gen {}'.format(rootName)) # Installs a self-signed cert into the system
sh('ndnsec-cert-dump -i {} > {}'.format(rootName, rootCertFile))
# Create necessary certificates for each site
nodeSecurityFolder = '{}/security'.format(self.homeDir)
self.node.cmd('mkdir -p {}'.format(nodeSecurityFolder))
# Create temp folders for remote nodes on this machine (localhost) to store site.key file
# from RemoteNodes
if not os.path.exists(nodeSecurityFolder) and \
isinstance(self.node, RemoteMixin) and self.node.isRemote:
os.makedirs(nodeSecurityFolder)
shutil.copyfile('{}/root.cert'.format(securityDir),
'{}/root.cert'.format(nodeSecurityFolder))
# Create site certificate
siteName = '{}{}-site'.format(self.network, self.node.name)
siteKeyFile = '{}/site.keys'.format(nodeSecurityFolder)
siteCertFile = '{}/site.cert'.format(nodeSecurityFolder)
Nlsr.createKey(self.node, siteName, siteKeyFile)
# Copy siteKeyFile from remote for ndnsec-cert-gen
if isinstance(self.node, RemoteMixin) and self.node.isRemote:
login = 'mininet@{}'.format(self.node.server)
src = '{}:{}'.format(login, siteKeyFile)
dst = siteKeyFile
scp(src, dst)
# Root key is in root namespace, must sign site key and then install on host
sh('ndnsec-cert-gen -s {} -r {} > {}'.format(rootName, siteKeyFile, siteCertFile))
# Copy root.cert and site.cert from localhost to remote host
if isinstance(self.node, RemoteMixin) and self.node.isRemote:
login = 'mininet@{}'.format(self.node.server)
src = '{}/site.cert'.format(nodeSecurityFolder)
src2 = '{}/root.cert'.format(nodeSecurityFolder)
dst = '{}:/tmp/'.format(login)
scp(src, src2, dst)
self.node.cmd('mv /tmp/*.cert {}'.format(nodeSecurityFolder))
self.node.cmd('ndnsec-cert-install -f {}'.format(siteCertFile))
# Create and install operator certificate
opName = '{}/%C1.Operator/op'.format(siteName)
opKeyFile = '{}/op.keys'.format(nodeSecurityFolder)
opCertFile = '{}/op.cert'.format(nodeSecurityFolder)
Nlsr.createKey(self.node, opName, opKeyFile)
Nlsr.createCertificate(self.node, siteName, opKeyFile, opCertFile)
self.node.cmd('ndnsec-cert-install -f {}'.format(opCertFile))
# Create and install router certificate
routerName = '{}/%C1.Router/cs/{}'.format(siteName, self.node.name)
routerKeyFile = '{}/router.keys'.format(nodeSecurityFolder)
routerCertFile = '{}/router.cert'.format(nodeSecurityFolder)
Nlsr.createKey(self.node, routerName, routerKeyFile)
Nlsr.createCertificate(self.node, opName, routerKeyFile, routerCertFile)
self.node.cmd('ndnsec-cert-install -f {}'.format(routerCertFile))
def createConfigFile(self):
self.__editGeneralSection()
if self.faceDict:
self.__editNeighborsSectionManual()
else:
self.__editNeighborsSection()
self.__editHyperbolicSection()
self.__editFibSection()
self.__editAdvertisingSection()
self.__editSecuritySection()
if self.infoeditManualChanges:
self.__editCustom()
def __editGeneralSection(self):
self.node.cmd('{} -s general.network -v {}'.format(self.infocmd, self.network))
self.node.cmd('{} -s general.site -v /{}-site'.format(self.infocmd, self.node.name))
self.node.cmd('{} -s general.router -v /%C1.Router/cs/{}'.format(self.infocmd, self.node.name))
self.node.cmd('{} -s general.state-dir -v {}/log'.format(self.infocmd, self.homeDir))
self.node.cmd('{} -s general.sync-protocol -v {}'.format(self.infocmd, self.sync))
def __editNeighborsSection(self):
self.node.cmd('{} -d neighbors.neighbor'.format(self.infocmd))
for intf in self.node.intfList():
link = intf.link
if not link:
continue
node1, node2 = link.intf1.node, link.intf2.node
# Todo: add some switch support
if isinstance(node1, Switch) or isinstance(node2, Switch):
continue
if node1 == self.node:
other = node2
if self.faceType == Nfdc.PROTOCOL_ETHER:
location = MACToEther(link.intf2.MAC())
else:
location = link.intf2.IP()
else:
other = node1
if self.faceType == Nfdc.PROTOCOL_ETHER:
location = MACToEther(link.intf1.MAC())
else:
location = link.intf1.IP()
linkCost = intf.params.get('delay', '10ms').replace('ms', '')
self.neighborLocations.append(location)
if self.faceType == Nfdc.PROTOCOL_ETHER:
self.interfaceForNeighbor[location] = intf
self.node.cmd('{} -a neighbors.neighbor \
<<<\'name {}{}-site/%C1.Router/cs/{} face-uri {}://{}\n link-cost {}\''
.format(self.infocmd, self.network, other.name, other.name,
self.faceType, location, linkCost))
def __editNeighborsSectionManual(self):
self.node.cmd('{} -d neighbors.neighbor'.format(self.infocmd))
if self.node not in self.faceDict:
return
for link in self.faceDict[self.node]:
nodeName = link[0]
nodeIP = link[1]
linkCost = link[2]
self.node.cmd('{} -a neighbors.neighbor \
<<<\'name {}{}-site/%C1.Router/cs/{} face-uri {}://{}\n link-cost {}\''
.format(self.infocmd, self.network, nodeName, nodeName,
self.faceType, nodeIP, linkCost))
def __editHyperbolicSection(self):
self.node.cmd('{} -s hyperbolic.state -v {}'.format(self.infocmd, self.hyperbolicState))
self.node.cmd('{} -s hyperbolic.radius -v {}'.format(self.infocmd, self.hyperRadius))
self.node.cmd('{} -s hyperbolic.angle -v {}'.format(self.infocmd, self.hyperAngle))
def __editFibSection(self):
self.node.cmd('{} -s fib.max-faces-per-prefix -v {}'.format(self.infocmd, self.nFaces))
def __editAdvertisingSection(self):
self.node.cmd('{} -d advertising'.format(self.infocmd))
self.node.cmd('{} -s advertising.{}{}-site/{} -v 0'
.format(self.infocmd, self.network, self.node.name, self.node.name))
def __editSecuritySection(self):
self.node.cmd('{} -d security.cert-to-publish'.format(self.infocmd))
if not self.security:
self.node.cmd('{} -s security.validator.trust-anchor.type -v any'.format(self.infocmd))
self.node.cmd('{} -d security.validator.trust-anchor.file-name'.format(self.infocmd))
self.node.cmd('{} -s security.prefix-update-validator.trust-anchor.type -v any'.format(self.infocmd))
self.node.cmd('{} -d security.prefix-update-validator.trust-anchor.file-name'.format(self.infocmd))
else:
self.node.cmd('{} -s security.validator.trust-anchor.file-name -v security/root.cert'.format(self.infocmd))
self.node.cmd('{} -s security.prefix-update-validator.trust-anchor.file-name -v security/site.cert'.format(self.infocmd))
self.node.cmd('{} -p security.cert-to-publish -v security/site.cert'.format(self.infocmd))
self.node.cmd('{} -p security.cert-to-publ
4296
ish -v security/op.cert'.format(self.infocmd))
self.node.cmd('{} -p security.cert-to-publish -v security/router.cert'.format(self.infocmd))
def __editCustom(self):
# EXPECTED FORMAT: [<section>, <key>] OR [<section>, <key>, <operation>]
# Default behavior will replace all values for section with key
# Deletion only works for unique keys
INFOEDIT_COMMANDS = {"put": "-p", "delete": "-d", "section": "-s"}
for infoeditChange in self.infoeditManualChanges:
command = "-s"
if len(infoeditChange) > 2:
if infoeditChange[2] == "delete":
debug(f'{self.infocmd} -d {quote(infoeditChange[0])}\n')
debug(self.node.cmd(f'{self.infocmd} -d {quote(infoeditChange[0])}\n'))
continue
else:
command = INFOEDIT_COMMANDS[infoeditChange[2]]
debug(f'{self.infocmd} {command} {quote(infoeditChange[0])} -v {quote(infoeditChange[1])}\n')
debug(self.node.cmd(f'{self.infocmd} {command} {quote(infoeditChange[0])} -v {quote(infoeditChange[1])}\n'))