Alexander Lane | 6f7a64f | 2018-05-17 15:01:14 -0500 | [diff] [blame] | 1 | # -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | # |
Saurab Dulal | 576a419 | 2020-08-25 00:55:22 -0500 | [diff] [blame] | 3 | # Copyright (C) 2015-2020, The University of Memphis, |
Alexander Lane | 6f7a64f | 2018-05-17 15:01:14 -0500 | [diff] [blame] | 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 | |
Saurab Dulal | 8ae870a | 2018-07-31 05:17:49 +0000 | [diff] [blame] | 24 | from mininet.log import debug |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 25 | from minindn.minindn import Minindn |
Saurab Dulal | 8ae870a | 2018-07-31 05:17:49 +0000 | [diff] [blame] | 26 | |
Varun Patil | 63a330d | 2022-05-18 14:23:13 -0700 | [diff] [blame^] | 27 | # If needed (e.g. to speed up the process), use a smaller (or larger value) |
| 28 | # based on your machines resource (CPU, memory) |
| 29 | SLEEP_TIME = 0.1 |
Alexander Lane | 6f7a64f | 2018-05-17 15:01:14 -0500 | [diff] [blame] | 30 | |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 31 | class 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 Lane | 6f7a64f | 2018-05-17 15:01:14 -0500 | [diff] [blame] | 39 | |
| 40 | @staticmethod |
Alex Lane | 1d3c0a8 | 2021-07-22 17:28:16 -0500 | [diff] [blame] | 41 | def registerRoute(node, namePrefix, remoteNode, protocol=PROTOCOL_UDP, origin=255, |
Ashlesh Gawande | 6651a74 | 2019-01-03 18:13:06 -0600 | [diff] [blame] | 42 | cost=0, inheritFlag=True, captureFlag=False, expirationInMillis=None): |
Alex Lane | 1d3c0a8 | 2021-07-22 17:28:16 -0500 | [diff] [blame] | 43 | 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 | ) |
dulalsaurab | 2b89953 | 2018-10-25 18:02:15 +0000 | [diff] [blame] | 65 | debug(node.cmd(cmd)) |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 66 | Minindn.sleep(SLEEP_TIME) |
Alexander Lane | 6f7a64f | 2018-05-17 15:01:14 -0500 | [diff] [blame] | 67 | |
| 68 | @staticmethod |
Alex Lane | 1d3c0a8 | 2021-07-22 17:28:16 -0500 | [diff] [blame] | 69 | 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) |
dulalsaurab | 0dcdb32 | 2018-08-15 20:39:07 +0000 | [diff] [blame] | 75 | debug(node.cmd(cmd)) |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 76 | Minindn.sleep(SLEEP_TIME) |
Alexander Lane | 6f7a64f | 2018-05-17 15:01:14 -0500 | [diff] [blame] | 77 | |
| 78 | @staticmethod |
Varun Patil | c69041f | 2022-05-18 14:17:54 -0700 | [diff] [blame] | 79 | def createFace(node, remoteNodeAddress, protocol='udp', isPermanent=False, allowExisting=True): |
Alex Lane | 1d3c0a8 | 2021-07-22 17:28:16 -0500 | [diff] [blame] | 80 | '''Create face in node's NFD instance. Returns FaceID of created face or -1 if failed.''' |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 81 | cmd = ('nfdc face create {}://{} {}'.format( |
Alexander Lane | 6f7a64f | 2018-05-17 15:01:14 -0500 | [diff] [blame] | 82 | protocol, |
Ashlesh Gawande | 6651a74 | 2019-01-03 18:13:06 -0600 | [diff] [blame] | 83 | remoteNodeAddress, |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 84 | 'permanent' if isPermanent else 'persistent' |
Alexander Lane | 6f7a64f | 2018-05-17 15:01:14 -0500 | [diff] [blame] | 85 | )) |
Alex Lane | 1d3c0a8 | 2021-07-22 17:28:16 -0500 | [diff] [blame] | 86 | output = node.cmd(cmd) |
| 87 | debug(output) |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 88 | Minindn.sleep(SLEEP_TIME) |
Varun Patil | c69041f | 2022-05-18 14:17:54 -0700 | [diff] [blame] | 89 | if "face-created" in output or (allowExisting and "face-exists" in output): |
| 90 | faceID = output.split(" ")[1][3:] |
| 91 | return faceID |
| 92 | return -1 |
Alexander Lane | 6f7a64f | 2018-05-17 15:01:14 -0500 | [diff] [blame] | 93 | |
| 94 | @staticmethod |
Alex Lane | 1d3c0a8 | 2021-07-22 17:28:16 -0500 | [diff] [blame] | 95 | def destroyFace(node, remoteNode, protocol='udp'): |
| 96 | if remoteNode.isdigit() and not protocol == "fd": |
| 97 | debug(node.cmd('nfdc face destroy {}'.format(protocol, remoteNode))) |
| 98 | else: |
| 99 | debug(node.cmd('nfdc face destroy {}://{}'.format(protocol, remoteNode))) |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 100 | Minindn.sleep(SLEEP_TIME) |
Alexander Lane | 6f7a64f | 2018-05-17 15:01:14 -0500 | [diff] [blame] | 101 | |
| 102 | @staticmethod |
| 103 | def setStrategy(node, namePrefix, strategy): |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 104 | cmd = 'nfdc strategy set {} ndn:/localhost/nfd/strategy/{}'.format(namePrefix, strategy) |
dulalsaurab | 2b89953 | 2018-10-25 18:02:15 +0000 | [diff] [blame] | 105 | debug(node.cmd(cmd)) |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 106 | Minindn.sleep(SLEEP_TIME) |
Alexander Lane | 6f7a64f | 2018-05-17 15:01:14 -0500 | [diff] [blame] | 107 | |
| 108 | @staticmethod |
| 109 | def unsetStrategy(node, namePrefix): |
dulalsaurab | 2b89953 | 2018-10-25 18:02:15 +0000 | [diff] [blame] | 110 | debug(node.cmd("nfdc strategy unset {}".format(namePrefix))) |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 111 | Minindn.sleep(SLEEP_TIME) |
Alex Lane | 1d3c0a8 | 2021-07-22 17:28:16 -0500 | [diff] [blame] | 112 | |
| 113 | @staticmethod |
| 114 | def getFaceId(node, remoteNodeAddress, localEndpoint=None, protocol="udp", portNum="6363"): |
| 115 | '''Returns the faceId for a remote node based on FaceURI, or -1 if a face is not found''' |
| 116 | #Should this be cached or is the hit not worth it? |
| 117 | local = "" |
| 118 | if localEndpoint: |
| 119 | local = " local {}".format(localEndpoint) |
| 120 | output = node.cmd("nfdc face list remote {}://{}:{}{}".format(protocol, remoteNodeAddress, portNum, local)) |
| 121 | debug(output) |
| 122 | Minindn.sleep(SLEEP_TIME) |
| 123 | # This is fragile but we don't have that many better options |
| 124 | if "faceid=" not in output: |
| 125 | return -1 |
| 126 | faceId = output.split(" ")[0][7:] |
| 127 | return faceId |