Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 1 | # -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | # |
awlane | f8a0a46 | 2025-05-30 12:49:17 -0500 | [diff] [blame] | 3 | # Copyright (C) 2015-2025, The University of Memphis, |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -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/>. |
awlane | 593ac19 | 2024-04-04 13:15:55 -0500 | [diff] [blame] | 23 | import json |
| 24 | import os |
awlane | f8a0a46 | 2025-05-30 12:49:17 -0500 | [diff] [blame] | 25 | from shlex import quote |
| 26 | from typing import List, Optional, Tuple, Union |
| 27 | |
| 28 | from mininet.clean import sh |
| 29 | from mininet.log import debug |
| 30 | from mininet.node import Node |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 31 | |
| 32 | from minindn.apps.application import Application |
| 33 | from minindn.util import copyExistentFile |
| 34 | from minindn.minindn import Minindn |
| 35 | |
| 36 | class Nfd(Application): |
awlane | f8a0a46 | 2025-05-30 12:49:17 -0500 | [diff] [blame] | 37 | def __init__(self, node: Node, logLevel: str = 'NONE', csSize: int = 65536, |
| 38 | csPolicy: str = 'lru', csUnsolicitedPolicy: str = 'drop-all', |
| 39 | infoeditChanges: Optional[List[Union[Tuple[str, str], Tuple[str, str, str]]]] = None): |
| 40 | """ |
| 41 | Set up NFD application through wrapper on node. These arguments are directly from nfd.conf, |
| 42 | so please reference that documentation for more information. |
| 43 | |
| 44 | :param node: Mininet or Mininet-Wifi node object |
| 45 | :param logLevel: NFD log level set as default (default "NONE") |
| 46 | :param csSize: ContentStore size in packets (default 65536) |
| 47 | :param csPolicy: ContentStore replacement policy (default "lru") |
| 48 | :param csUnsolicitedPolicy: Policy for handling unsolicited data (default "drop-all") |
| 49 | :param infoeditChanges: Commands passed to infoedit other than the most commonly used arguments. |
| 50 | These either expect (key, value) (using the `section` command) or otherwise |
| 51 | (key, value, put|section|delete). |
| 52 | """ |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 53 | Application.__init__(self, node) |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 54 | self.logLevel = node.params['params'].get('nfd-log-level', logLevel) |
| 55 | |
| 56 | self.confFile = '{}/nfd.conf'.format(self.homeDir) |
| 57 | self.logFile = 'nfd.log' |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 58 | self.ndnFolder = '{}/.ndn'.format(self.homeDir) |
| 59 | self.clientConf = '{}/client.conf'.format(self.ndnFolder) |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 60 | # Copy nfd.conf file from /usr/local/etc/ndn or /etc/ndn to the node's home directory |
| 61 | # Use nfd.conf as default configuration for NFD, else use the sample |
awlane | 593ac19 | 2024-04-04 13:15:55 -0500 | [diff] [blame] | 62 | possibleConfPaths = ['/usr/local/etc/ndn/nfd.conf', '/usr/local/etc/ndn/nfd.conf.sample', |
| 63 | '/etc/ndn/nfd.conf', '/etc/ndn/nfd.conf.sample'] |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 64 | copyExistentFile(node, possibleConfPaths, self.confFile) |
| 65 | |
awlane | 593ac19 | 2024-04-04 13:15:55 -0500 | [diff] [blame] | 66 | # Using infoconv, we convert the local nfd.conf file to JSON and parse it into an object |
| 67 | conf_file = json.loads(node.cmd("infoconv info2json < {}".format(self.confFile))) |
| 68 | |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 69 | # Set log level |
awlane | 593ac19 | 2024-04-04 13:15:55 -0500 | [diff] [blame] | 70 | conf_file["log"]["default_level"] = self.logLevel |
| 71 | |
| 72 | # Set socket file name and path |
| 73 | # Retrieve the default socket path from the conf file; this avoids issues from #5316 |
| 74 | default_socket_path = os.path.dirname(conf_file["face_system"]["unix"]["path"]) |
| 75 | self.sockFile = '{}/{}.sock'.format(default_socket_path, node.name) |
| 76 | # Set socket path in conf file to new socket |
| 77 | conf_file["face_system"]["unix"]["path"] = self.sockFile |
| 78 | # Create client configuration for host to ensure socket path is consistent |
| 79 | # Suppress error if working directory exists from prior run |
| 80 | os.makedirs(self.ndnFolder, exist_ok=True) |
| 81 | # This will overwrite any existing client.conf files, which should not be an issue |
| 82 | with open(self.clientConf, "w") as client_conf_file: |
| 83 | client_conf_file.write("transport=unix://{}\n".format(self.sockFile)) |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 84 | |
| 85 | # Set CS parameters |
awlane | 593ac19 | 2024-04-04 13:15:55 -0500 | [diff] [blame] | 86 | conf_file["tables"]["cs_max_packets"] = csSize |
| 87 | conf_file["tables"]["cs_policy"] = csPolicy |
| 88 | conf_file["tables"]["cs_unsolicited_policy"] = csUnsolicitedPolicy |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 89 | |
awlane | 593ac19 | 2024-04-04 13:15:55 -0500 | [diff] [blame] | 90 | # To avoid complicated Bash piping, we write the JSON to a temporary file |
| 91 | with open("{}/temp_nfd_conf.json".format(self.homeDir), "w") as temp_file: |
| 92 | json.dump(conf_file, temp_file) |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 93 | |
awlane | 593ac19 | 2024-04-04 13:15:55 -0500 | [diff] [blame] | 94 | # Convert our modified intermediate file and write to the new conf file |
| 95 | node.cmd("infoconv json2info < {}/temp_nfd_conf.json > {}".format(self.homeDir, self.confFile)) |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 96 | |
awlane | 593ac19 | 2024-04-04 13:15:55 -0500 | [diff] [blame] | 97 | # Remove the intermediate JSON file |
| 98 | os.remove("{}/temp_nfd_conf.json".format(self.homeDir)) |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 99 | |
awlane | f8a0a46 | 2025-05-30 12:49:17 -0500 | [diff] [blame] | 100 | self.infocmd = 'infoedit -f nfd.conf' |
| 101 | # Apply custom infoedit changes |
| 102 | # EXPECTED FORMAT: [<section>, <key>] OR [<section>, <key>, <operation>] |
| 103 | # Default behavior will replace all values for section with key |
| 104 | # Deletion only works for unique keys |
| 105 | INFOEDIT_COMMANDS = {"put": "-p", "delete": "-d", "section": "-s"} |
| 106 | if infoeditChanges: |
| 107 | for infoeditChange in infoeditChanges: |
| 108 | command = "-s" |
| 109 | if len(infoeditChange) > 2: |
| 110 | if infoeditChange[2] == "delete": |
| 111 | debug(f'{self.infocmd} -d {quote(infoeditChange[0])}\n') |
| 112 | debug(self.node.cmd(f'{self.infocmd} -d {quote(infoeditChange[0])}\n')) |
| 113 | continue |
| 114 | else: |
| 115 | command = INFOEDIT_COMMANDS[infoeditChange[2]] |
| 116 | debug(f'{self.infocmd} {command} {quote(infoeditChange[0])} -v {quote(infoeditChange[1])}\n') |
| 117 | debug(self.node.cmd(f'{self.infocmd} {command} {quote(infoeditChange[0])} -v {quote(infoeditChange[1])}\n')) |
| 118 | |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 119 | if not Minindn.ndnSecurityDisabled: |
| 120 | # Generate key and install cert for /localhost/operator to be used by NFD |
suraviregmi | 9665f5c | 2023-11-09 20:05:26 +0000 | [diff] [blame] | 121 | node.cmd('ndnsec-key-gen /localhost/operator | ndnsec-cert-install -') |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 122 | |
| 123 | def start(self): |
| 124 | Application.start(self, 'nfd --config {}'.format(self.confFile), logfile=self.logFile) |
Varun Patil | 63a330d | 2022-05-18 14:23:13 -0700 | [diff] [blame] | 125 | Minindn.sleep(0.5) |