blob: a8d5c2389ad6feecba740065347c75efecdc6517 [file] [log] [blame]
Ashlesh Gawande6c86e302019-09-17 22:27:05 -05001# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2#
awlanef8a0a462025-05-30 12:49:17 -05003# Copyright (C) 2015-2025, The University of Memphis,
Ashlesh Gawande6c86e302019-09-17 22:27:05 -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/>.
awlane593ac192024-04-04 13:15:55 -050023import json
24import os
awlanef8a0a462025-05-30 12:49:17 -050025from shlex import quote
26from typing import List, Optional, Tuple, Union
27
28from mininet.clean import sh
29from mininet.log import debug
30from mininet.node import Node
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050031
32from minindn.apps.application import Application
33from minindn.util import copyExistentFile
34from minindn.minindn import Minindn
35
36class Nfd(Application):
awlanef8a0a462025-05-30 12:49:17 -050037 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 Gawande6c86e302019-09-17 22:27:05 -050053 Application.__init__(self, node)
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050054 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 Gawande6c86e302019-09-17 22:27:05 -050058 self.ndnFolder = '{}/.ndn'.format(self.homeDir)
59 self.clientConf = '{}/client.conf'.format(self.ndnFolder)
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050060 # 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
awlane593ac192024-04-04 13:15:55 -050062 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 Gawande6c86e302019-09-17 22:27:05 -050064 copyExistentFile(node, possibleConfPaths, self.confFile)
65
awlane593ac192024-04-04 13:15:55 -050066 # 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 Gawande6c86e302019-09-17 22:27:05 -050069 # Set log level
awlane593ac192024-04-04 13:15:55 -050070 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 Gawande6c86e302019-09-17 22:27:05 -050084
85 # Set CS parameters
awlane593ac192024-04-04 13:15:55 -050086 conf_file["tables"]["cs_max_packets"] = csSize
87 conf_file["tables"]["cs_policy"] = csPolicy
88 conf_file["tables"]["cs_unsolicited_policy"] = csUnsolicitedPolicy
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050089
awlane593ac192024-04-04 13:15:55 -050090 # 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 Gawande6c86e302019-09-17 22:27:05 -050093
awlane593ac192024-04-04 13:15:55 -050094 # 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 Gawande6c86e302019-09-17 22:27:05 -050096
awlane593ac192024-04-04 13:15:55 -050097 # Remove the intermediate JSON file
98 os.remove("{}/temp_nfd_conf.json".format(self.homeDir))
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050099
awlanef8a0a462025-05-30 12:49:17 -0500100 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 Gawande6c86e302019-09-17 22:27:05 -0500119 if not Minindn.ndnSecurityDisabled:
120 # Generate key and install cert for /localhost/operator to be used by NFD
suraviregmi9665f5c2023-11-09 20:05:26 +0000121 node.cmd('ndnsec-key-gen /localhost/operator | ndnsec-cert-install -')
Ashlesh Gawande6c86e302019-09-17 22:27:05 -0500122
123 def start(self):
124 Application.start(self, 'nfd --config {}'.format(self.confFile), logfile=self.logFile)
Varun Patil63a330d2022-05-18 14:23:13 -0700125 Minindn.sleep(0.5)