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 | |
tylerliu | 8664779 | 2023-03-03 15:18:48 -0800 | [diff] [blame] | 24 | from mininet.log import debug, warn |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 25 | from minindn.minindn import Minindn |
awlane | 21acd05 | 2024-06-13 21:12:51 -0500 | [diff] [blame^] | 26 | from minindn.util import MACToEther |
Saurab Dulal | 8ae870a | 2018-07-31 05:17:49 +0000 | [diff] [blame] | 27 | |
Varun Patil | 63a330d | 2022-05-18 14:23:13 -0700 | [diff] [blame] | 28 | # If needed (e.g. to speed up the process), use a smaller (or larger value) |
| 29 | # based on your machines resource (CPU, memory) |
tylerliu | 8664779 | 2023-03-03 15:18:48 -0800 | [diff] [blame] | 30 | SLEEP_TIME = 0.0015 |
Alexander Lane | 6f7a64f | 2018-05-17 15:01:14 -0500 | [diff] [blame] | 31 | |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 32 | class Nfdc(object): |
| 33 | STRATEGY_ASF = 'asf' |
| 34 | STRATEGY_BEST_ROUTE = 'best-route' |
| 35 | STRATEGY_MULTICAST = 'multicast' |
| 36 | STRATEGY_NCC = 'ncc' |
| 37 | PROTOCOL_UDP = 'udp' |
| 38 | PROTOCOL_TCP = 'tcp' |
| 39 | PROTOCOL_ETHER = 'ether' |
Alexander Lane | 6f7a64f | 2018-05-17 15:01:14 -0500 | [diff] [blame] | 40 | |
| 41 | @staticmethod |
Alex Lane | 1d3c0a8 | 2021-07-22 17:28:16 -0500 | [diff] [blame] | 42 | def registerRoute(node, namePrefix, remoteNode, protocol=PROTOCOL_UDP, origin=255, |
Ashlesh Gawande | 6651a74 | 2019-01-03 18:13:06 -0600 | [diff] [blame] | 43 | cost=0, inheritFlag=True, captureFlag=False, expirationInMillis=None): |
Alex Lane | 1d3c0a8 | 2021-07-22 17:28:16 -0500 | [diff] [blame] | 44 | cmd = "" |
| 45 | if remoteNode.isdigit() and not protocol == "fd": |
| 46 | cmd = ('nfdc route add {} {} origin {} cost {} {}{}{}').format( |
| 47 | namePrefix, |
| 48 | remoteNode, |
| 49 | origin, |
| 50 | cost, |
| 51 | 'no-inherit ' if not inheritFlag else '', |
| 52 | 'capture ' if captureFlag else '', |
| 53 | 'expires {}'.format(expirationInMillis) if expirationInMillis else '' |
| 54 | ) |
| 55 | else: |
awlane | 21acd05 | 2024-06-13 21:12:51 -0500 | [diff] [blame^] | 56 | if protocol == "ether": |
| 57 | remoteNode = MACToEther(remoteNode) |
Alex Lane | 1d3c0a8 | 2021-07-22 17:28:16 -0500 | [diff] [blame] | 58 | cmd = ('nfdc route add {} {}://{} origin {} cost {} {}{}{}').format( |
| 59 | namePrefix, |
| 60 | protocol, |
| 61 | remoteNode, |
| 62 | origin, |
| 63 | cost, |
| 64 | 'no-inherit ' if not inheritFlag else '', |
| 65 | 'capture ' if captureFlag else '', |
| 66 | 'expires {}'.format(expirationInMillis) if expirationInMillis else '' |
| 67 | ) |
dulalsaurab | 2b89953 | 2018-10-25 18:02:15 +0000 | [diff] [blame] | 68 | debug(node.cmd(cmd)) |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 69 | Minindn.sleep(SLEEP_TIME) |
Alexander Lane | 6f7a64f | 2018-05-17 15:01:14 -0500 | [diff] [blame] | 70 | |
| 71 | @staticmethod |
awlane | 21acd05 | 2024-06-13 21:12:51 -0500 | [diff] [blame^] | 72 | def unregisterRoute(node, namePrefix, remoteNode, protocol=PROTOCOL_UDP, origin=255): |
Alex Lane | 1d3c0a8 | 2021-07-22 17:28:16 -0500 | [diff] [blame] | 73 | cmd = "" |
| 74 | if remoteNode.isdigit() and not protocol == "fd": |
awlane | 21acd05 | 2024-06-13 21:12:51 -0500 | [diff] [blame^] | 75 | cmd = 'nfdc route remove {} {} origin {}'.format(namePrefix, remoteNode, origin) |
Alex Lane | 1d3c0a8 | 2021-07-22 17:28:16 -0500 | [diff] [blame] | 76 | else: |
awlane | 21acd05 | 2024-06-13 21:12:51 -0500 | [diff] [blame^] | 77 | if protocol == "ether": |
| 78 | remoteNode = MACToEther(remoteNode) |
| 79 | cmd = 'nfdc route remove {} {}://{} origin {}'.format(namePrefix, protocol, remoteNode, origin) |
dulalsaurab | 0dcdb32 | 2018-08-15 20:39:07 +0000 | [diff] [blame] | 80 | debug(node.cmd(cmd)) |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 81 | Minindn.sleep(SLEEP_TIME) |
Alexander Lane | 6f7a64f | 2018-05-17 15:01:14 -0500 | [diff] [blame] | 82 | |
| 83 | @staticmethod |
awlane | 21acd05 | 2024-06-13 21:12:51 -0500 | [diff] [blame^] | 84 | def createFace(node, remoteNodeAddress, protocol=PROTOCOL_UDP, isPermanent=False, localInterface='', allowExisting=True): |
Alex Lane | 1d3c0a8 | 2021-07-22 17:28:16 -0500 | [diff] [blame] | 85 | '''Create face in node's NFD instance. Returns FaceID of created face or -1 if failed.''' |
awlane | 21acd05 | 2024-06-13 21:12:51 -0500 | [diff] [blame^] | 86 | if protocol == "ether" and not localInterface: |
| 87 | warn("Cannot create ethernet face without local interface!") |
| 88 | return |
| 89 | elif protocol != "ether" and localInterface: |
| 90 | warn("Cannot create non-ethernet face with local interface specified!") |
| 91 | return |
| 92 | elif protocol == "ether" and localInterface: |
| 93 | remoteNodeAddress = MACToEther(remoteNodeAddress) |
| 94 | cmd = ('nfdc face create {}://{} {}{}'.format( |
Alexander Lane | 6f7a64f | 2018-05-17 15:01:14 -0500 | [diff] [blame] | 95 | protocol, |
Ashlesh Gawande | 6651a74 | 2019-01-03 18:13:06 -0600 | [diff] [blame] | 96 | remoteNodeAddress, |
awlane | 21acd05 | 2024-06-13 21:12:51 -0500 | [diff] [blame^] | 97 | 'local dev://{} '.format(localInterface) if localInterface else '', |
| 98 | 'persistency permanent' if isPermanent else 'persistency persistent' |
Alexander Lane | 6f7a64f | 2018-05-17 15:01:14 -0500 | [diff] [blame] | 99 | )) |
Alex Lane | 1d3c0a8 | 2021-07-22 17:28:16 -0500 | [diff] [blame] | 100 | output = node.cmd(cmd) |
| 101 | debug(output) |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 102 | Minindn.sleep(SLEEP_TIME) |
awlane | a43c241 | 2024-04-02 14:14:45 -0500 | [diff] [blame] | 103 | if "face-created" in output or (allowExisting and ("face-exists" in output or "face-updated" in output)): |
Varun Patil | c69041f | 2022-05-18 14:17:54 -0700 | [diff] [blame] | 104 | faceID = output.split(" ")[1][3:] |
awlane | a43c241 | 2024-04-02 14:14:45 -0500 | [diff] [blame] | 105 | if "face-exists" in output or "face-updated" in output: |
| 106 | debug("[{}] Existing face found: {}\n".format(node.name, faceID)) |
Varun Patil | c69041f | 2022-05-18 14:17:54 -0700 | [diff] [blame] | 107 | return faceID |
awlane | a43c241 | 2024-04-02 14:14:45 -0500 | [diff] [blame] | 108 | warn("[{}] Face register failed: {}\n".format(node.name, output)) |
Varun Patil | c69041f | 2022-05-18 14:17:54 -0700 | [diff] [blame] | 109 | return -1 |
Alexander Lane | 6f7a64f | 2018-05-17 15:01:14 -0500 | [diff] [blame] | 110 | |
| 111 | @staticmethod |
awlane | 21acd05 | 2024-06-13 21:12:51 -0500 | [diff] [blame^] | 112 | def destroyFace(node, remoteNode, protocol=PROTOCOL_UDP): |
Alex Lane | 1d3c0a8 | 2021-07-22 17:28:16 -0500 | [diff] [blame] | 113 | if remoteNode.isdigit() and not protocol == "fd": |
awlane | 21acd05 | 2024-06-13 21:12:51 -0500 | [diff] [blame^] | 114 | debug(node.cmd('nfdc face destroy {}'.format(remoteNode))) |
Alex Lane | 1d3c0a8 | 2021-07-22 17:28:16 -0500 | [diff] [blame] | 115 | else: |
awlane | 21acd05 | 2024-06-13 21:12:51 -0500 | [diff] [blame^] | 116 | if protocol == "ether": |
| 117 | remoteNode = MACToEther(remoteNode) |
Alex Lane | 1d3c0a8 | 2021-07-22 17:28:16 -0500 | [diff] [blame] | 118 | debug(node.cmd('nfdc face destroy {}://{}'.format(protocol, remoteNode))) |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 119 | Minindn.sleep(SLEEP_TIME) |
Alexander Lane | 6f7a64f | 2018-05-17 15:01:14 -0500 | [diff] [blame] | 120 | |
| 121 | @staticmethod |
| 122 | def setStrategy(node, namePrefix, strategy): |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 123 | cmd = 'nfdc strategy set {} ndn:/localhost/nfd/strategy/{}'.format(namePrefix, strategy) |
tylerliu | 8664779 | 2023-03-03 15:18:48 -0800 | [diff] [blame] | 124 | out = node.cmd(cmd) |
| 125 | if out.find('error') != -1: |
| 126 | warn("[" + node.name + "] Error on strategy set out: " + out) |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 127 | Minindn.sleep(SLEEP_TIME) |
Alexander Lane | 6f7a64f | 2018-05-17 15:01:14 -0500 | [diff] [blame] | 128 | |
| 129 | @staticmethod |
| 130 | def unsetStrategy(node, namePrefix): |
dulalsaurab | 2b89953 | 2018-10-25 18:02:15 +0000 | [diff] [blame] | 131 | debug(node.cmd("nfdc strategy unset {}".format(namePrefix))) |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 132 | Minindn.sleep(SLEEP_TIME) |
Alex Lane | 1d3c0a8 | 2021-07-22 17:28:16 -0500 | [diff] [blame] | 133 | |
| 134 | @staticmethod |
awlane | 21acd05 | 2024-06-13 21:12:51 -0500 | [diff] [blame^] | 135 | def getFaceId(node, remoteNodeAddress, localEndpoint=None, protocol=PROTOCOL_UDP, portNum="6363"): |
Alex Lane | 1d3c0a8 | 2021-07-22 17:28:16 -0500 | [diff] [blame] | 136 | '''Returns the faceId for a remote node based on FaceURI, or -1 if a face is not found''' |
| 137 | #Should this be cached or is the hit not worth it? |
| 138 | local = "" |
| 139 | if localEndpoint: |
| 140 | local = " local {}".format(localEndpoint) |
awlane | 21acd05 | 2024-06-13 21:12:51 -0500 | [diff] [blame^] | 141 | if protocol == "ether": |
| 142 | remoteNodeAddress = MACToEther(remoteNodeAddress) |
| 143 | output = node.cmd("nfdc face list remote {}://{}{}".format(protocol, remoteNodeAddress, local)) |
| 144 | else: |
| 145 | output = node.cmd("nfdc face list remote {}://{}:{}{}".format(protocol, remoteNodeAddress, portNum, local)) |
Alex Lane | 1d3c0a8 | 2021-07-22 17:28:16 -0500 | [diff] [blame] | 146 | debug(output) |
| 147 | Minindn.sleep(SLEEP_TIME) |
| 148 | # This is fragile but we don't have that many better options |
| 149 | if "faceid=" not in output: |
| 150 | return -1 |
| 151 | faceId = output.split(" ")[0][7:] |
tylerliu | 8664779 | 2023-03-03 15:18:48 -0800 | [diff] [blame] | 152 | return faceId |