blob: 9468d8b682f2427d81ecdae050c5a804584d3725 [file] [log] [blame]
Alexander Lane6f7a64f2018-05-17 15:01:14 -05001# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2#
Saurab Dulal576a4192020-08-25 00:55:22 -05003# Copyright (C) 2015-2020, The University of Memphis,
Alexander Lane6f7a64f2018-05-17 15:01:14 -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
tylerliu86647792023-03-03 15:18:48 -080024from mininet.log import debug, warn
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050025from minindn.minindn import Minindn
Saurab Dulal8ae870a2018-07-31 05:17:49 +000026
Varun Patil63a330d2022-05-18 14:23:13 -070027# If needed (e.g. to speed up the process), use a smaller (or larger value)
28# based on your machines resource (CPU, memory)
tylerliu86647792023-03-03 15:18:48 -080029SLEEP_TIME = 0.0015
Alexander Lane6f7a64f2018-05-17 15:01:14 -050030
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050031class Nfdc(object):
32 STRATEGY_ASF = 'asf'
33 STRATEGY_BEST_ROUTE = 'best-route'
34 STRATEGY_MULTICAST = 'multicast'
35 STRATEGY_NCC = 'ncc'
36 PROTOCOL_UDP = 'udp'
37 PROTOCOL_TCP = 'tcp'
38 PROTOCOL_ETHER = 'ether'
Alexander Lane6f7a64f2018-05-17 15:01:14 -050039
40 @staticmethod
Alex Lane1d3c0a82021-07-22 17:28:16 -050041 def registerRoute(node, namePrefix, remoteNode, protocol=PROTOCOL_UDP, origin=255,
Ashlesh Gawande6651a742019-01-03 18:13:06 -060042 cost=0, inheritFlag=True, captureFlag=False, expirationInMillis=None):
Alex Lane1d3c0a82021-07-22 17:28:16 -050043 cmd = ""
44 if remoteNode.isdigit() and not protocol == "fd":
45 cmd = ('nfdc route add {} {} origin {} cost {} {}{}{}').format(
46 namePrefix,
47 remoteNode,
48 origin,
49 cost,
50 'no-inherit ' if not inheritFlag else '',
51 'capture ' if captureFlag else '',
52 'expires {}'.format(expirationInMillis) if expirationInMillis else ''
53 )
54 else:
55 cmd = ('nfdc route add {} {}://{} origin {} cost {} {}{}{}').format(
56 namePrefix,
57 protocol,
58 remoteNode,
59 origin,
60 cost,
61 'no-inherit ' if not inheritFlag else '',
62 'capture ' if captureFlag else '',
63 'expires {}'.format(expirationInMillis) if expirationInMillis else ''
64 )
dulalsaurab2b899532018-10-25 18:02:15 +000065 debug(node.cmd(cmd))
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050066 Minindn.sleep(SLEEP_TIME)
Alexander Lane6f7a64f2018-05-17 15:01:14 -050067
68 @staticmethod
Alex Lane1d3c0a82021-07-22 17:28:16 -050069 def unregisterRoute(node, namePrefix, remoteNode, origin=255):
70 cmd = ""
71 if remoteNode.isdigit() and not protocol == "fd":
72 cmd = 'nfdc route remove {} {} {}'.format(namePrefix, remoteNode, origin)
73 else:
74 cmd = 'nfdc route remove {} {} {}'.format(namePrefix, remoteNode, origin)
dulalsaurab0dcdb322018-08-15 20:39:07 +000075 debug(node.cmd(cmd))
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050076 Minindn.sleep(SLEEP_TIME)
Alexander Lane6f7a64f2018-05-17 15:01:14 -050077
78 @staticmethod
Varun Patilc69041f2022-05-18 14:17:54 -070079 def createFace(node, remoteNodeAddress, protocol='udp', isPermanent=False, allowExisting=True):
Alex Lane1d3c0a82021-07-22 17:28:16 -050080 '''Create face in node's NFD instance. Returns FaceID of created face or -1 if failed.'''
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050081 cmd = ('nfdc face create {}://{} {}'.format(
Alexander Lane6f7a64f2018-05-17 15:01:14 -050082 protocol,
Ashlesh Gawande6651a742019-01-03 18:13:06 -060083 remoteNodeAddress,
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050084 'permanent' if isPermanent else 'persistent'
Alexander Lane6f7a64f2018-05-17 15:01:14 -050085 ))
Alex Lane1d3c0a82021-07-22 17:28:16 -050086 output = node.cmd(cmd)
87 debug(output)
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050088 Minindn.sleep(SLEEP_TIME)
Varun Patilc69041f2022-05-18 14:17:54 -070089 if "face-created" in output or (allowExisting and "face-exists" in output):
90 faceID = output.split(" ")[1][3:]
91 return faceID
tylerliu86647792023-03-03 15:18:48 -080092 warn("["+ node.name + "] Face register failed: " + output)
Varun Patilc69041f2022-05-18 14:17:54 -070093 return -1
Alexander Lane6f7a64f2018-05-17 15:01:14 -050094
95 @staticmethod
Alex Lane1d3c0a82021-07-22 17:28:16 -050096 def destroyFace(node, remoteNode, protocol='udp'):
97 if remoteNode.isdigit() and not protocol == "fd":
98 debug(node.cmd('nfdc face destroy {}'.format(protocol, remoteNode)))
99 else:
100 debug(node.cmd('nfdc face destroy {}://{}'.format(protocol, remoteNode)))
Ashlesh Gawande6c86e302019-09-17 22:27:05 -0500101 Minindn.sleep(SLEEP_TIME)
Alexander Lane6f7a64f2018-05-17 15:01:14 -0500102
103 @staticmethod
104 def setStrategy(node, namePrefix, strategy):
Ashlesh Gawande6c86e302019-09-17 22:27:05 -0500105 cmd = 'nfdc strategy set {} ndn:/localhost/nfd/strategy/{}'.format(namePrefix, strategy)
tylerliu86647792023-03-03 15:18:48 -0800106 out = node.cmd(cmd)
107 if out.find('error') != -1:
108 warn("[" + node.name + "] Error on strategy set out: " + out)
Ashlesh Gawande6c86e302019-09-17 22:27:05 -0500109 Minindn.sleep(SLEEP_TIME)
Alexander Lane6f7a64f2018-05-17 15:01:14 -0500110
111 @staticmethod
112 def unsetStrategy(node, namePrefix):
dulalsaurab2b899532018-10-25 18:02:15 +0000113 debug(node.cmd("nfdc strategy unset {}".format(namePrefix)))
Ashlesh Gawande6c86e302019-09-17 22:27:05 -0500114 Minindn.sleep(SLEEP_TIME)
Alex Lane1d3c0a82021-07-22 17:28:16 -0500115
116 @staticmethod
117 def getFaceId(node, remoteNodeAddress, localEndpoint=None, protocol="udp", portNum="6363"):
118 '''Returns the faceId for a remote node based on FaceURI, or -1 if a face is not found'''
119 #Should this be cached or is the hit not worth it?
120 local = ""
121 if localEndpoint:
122 local = " local {}".format(localEndpoint)
123 output = node.cmd("nfdc face list remote {}://{}:{}{}".format(protocol, remoteNodeAddress, portNum, local))
124 debug(output)
125 Minindn.sleep(SLEEP_TIME)
126 # This is fragile but we don't have that many better options
127 if "faceid=" not in output:
128 return -1
129 faceId = output.split(" ")[0][7:]
tylerliu86647792023-03-03 15:18:48 -0800130 return faceId