blob: 08409f75f86015853307493986330887d1a9d332 [file] [log] [blame]
Junxiao Shi38f4ce92016-08-04 10:01:52 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Eric Newberry84d3adc2017-08-09 23:31:40 -04002/*
Davide Pesaventob7bfcb92022-05-22 23:55:23 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Junxiao Shi38f4ce92016-08-04 10:01:52 +00004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
Junxiao Shi38f4ce92016-08-04 10:01:52 +000026#include "face-module.hpp"
Eric Newberryd656aff2020-04-03 00:30:38 -070027#include "canonizer.hpp"
Junxiao Shi05dd4442017-02-06 22:50:07 +000028#include "find-face.hpp"
Junxiao Shi38f4ce92016-08-04 10:01:52 +000029
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040030namespace nfd::tools::nfdc {
Junxiao Shi38f4ce92016-08-04 10:01:52 +000031
32void
Junxiao Shi1f481fa2017-01-26 15:14:43 +000033FaceModule::registerCommands(CommandParser& parser)
34{
Junxiao Shi36e54292017-02-17 18:43:16 +000035 CommandDefinition defFaceList("face", "list");
36 defFaceList
37 .setTitle("print face list")
38 .addArg("remote", ArgValueType::FACE_URI, Required::NO, Positional::YES)
39 .addArg("local", ArgValueType::FACE_URI, Required::NO, Positional::NO)
40 .addArg("scheme", ArgValueType::STRING, Required::NO, Positional::NO, "scheme");
41 parser.addCommand(defFaceList, &FaceModule::list);
Davide Pesaventod2147442018-02-19 23:58:17 -050042 parser.addAlias("face", "list", "");
Junxiao Shi36e54292017-02-17 18:43:16 +000043
Junxiao Shi1f481fa2017-01-26 15:14:43 +000044 CommandDefinition defFaceShow("face", "show");
45 defFaceShow
46 .setTitle("show face information")
47 .addArg("id", ArgValueType::UNSIGNED, Required::YES, Positional::YES);
48 parser.addCommand(defFaceShow, &FaceModule::show);
Junxiao Shi1d7fef52017-02-02 05:33:14 +000049
50 CommandDefinition defFaceCreate("face", "create");
51 defFaceCreate
52 .setTitle("create a face")
53 .addArg("remote", ArgValueType::FACE_URI, Required::YES, Positional::YES)
Junxiao Shi0d976922017-04-01 14:35:21 +000054 .addArg("persistency", ArgValueType::FACE_PERSISTENCY, Required::NO, Positional::YES)
Eric Newberry84d3adc2017-08-09 23:31:40 -040055 .addArg("local", ArgValueType::FACE_URI, Required::NO, Positional::NO)
Eric Newberryde332452018-01-30 11:45:32 -070056 .addArg("reliability", ArgValueType::BOOLEAN, Required::NO, Positional::NO)
57 .addArg("congestion-marking", ArgValueType::BOOLEAN, Required::NO, Positional::NO)
58 .addArg("congestion-marking-interval", ArgValueType::UNSIGNED, Required::NO, Positional::NO)
Eric Newberry4f8dd962018-06-17 21:32:07 -070059 .addArg("default-congestion-threshold", ArgValueType::UNSIGNED, Required::NO, Positional::NO)
Eric Newberry13ff2592020-03-06 17:32:29 -080060 .addArg("mtu", ArgValueType::STRING, Required::NO, Positional::NO);
Junxiao Shi1d7fef52017-02-02 05:33:14 +000061 parser.addCommand(defFaceCreate, &FaceModule::create);
Junxiao Shi05dd4442017-02-06 22:50:07 +000062
63 CommandDefinition defFaceDestroy("face", "destroy");
64 defFaceDestroy
65 .setTitle("destroy a face")
66 .addArg("face", ArgValueType::FACE_ID_OR_URI, Required::YES, Positional::YES);
67 parser.addCommand(defFaceDestroy, &FaceModule::destroy);
Junxiao Shi1f481fa2017-01-26 15:14:43 +000068}
69
70void
Junxiao Shi36e54292017-02-17 18:43:16 +000071FaceModule::list(ExecuteContext& ctx)
72{
73 auto remoteUri = ctx.args.getOptional<FaceUri>("remote");
74 auto localUri = ctx.args.getOptional<FaceUri>("local");
75 auto uriScheme = ctx.args.getOptional<std::string>("scheme");
76
77 FaceQueryFilter filter;
78 if (remoteUri) {
79 filter.setRemoteUri(remoteUri->toString());
80 }
81 if (localUri) {
82 filter.setLocalUri(localUri->toString());
83 }
84 if (uriScheme) {
85 filter.setUriScheme(*uriScheme);
86 }
87
88 FindFace findFace(ctx);
89 FindFace::Code res = findFace.execute(filter, true);
90
91 ctx.exitCode = static_cast<int>(res);
92 switch (res) {
93 case FindFace::Code::OK:
94 for (const FaceStatus& item : findFace.getResults()) {
95 formatItemText(ctx.out, item, false);
96 ctx.out << '\n';
97 }
98 break;
99 case FindFace::Code::ERROR:
100 case FindFace::Code::NOT_FOUND:
101 case FindFace::Code::CANONIZE_ERROR:
102 ctx.err << findFace.getErrorReason() << '\n';
103 break;
104 default:
105 BOOST_ASSERT_MSG(false, "unexpected FindFace result");
106 break;
107 }
108}
109
110void
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000111FaceModule::show(ExecuteContext& ctx)
112{
113 uint64_t faceId = ctx.args.get<uint64_t>("id");
114
Junxiao Shi8f803f22017-02-10 03:04:28 +0000115 FindFace findFace(ctx);
116 FindFace::Code res = findFace.execute(faceId);
Junxiao Shi1d7fef52017-02-02 05:33:14 +0000117
Junxiao Shi8f803f22017-02-10 03:04:28 +0000118 ctx.exitCode = static_cast<int>(res);
119 switch (res) {
120 case FindFace::Code::OK:
121 formatItemText(ctx.out, findFace.getFaceStatus(), true);
122 break;
123 case FindFace::Code::ERROR:
124 case FindFace::Code::NOT_FOUND:
125 ctx.err << findFace.getErrorReason() << '\n';
126 break;
127 default:
128 BOOST_ASSERT_MSG(false, "unexpected FindFace result");
129 break;
130 }
Junxiao Shi1d7fef52017-02-02 05:33:14 +0000131}
132
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000133/** \brief order persistency in NONE < ON_DEMAND < PERSISTENCY < PERMANENT
134 */
135static bool
136persistencyLessThan(FacePersistency x, FacePersistency y)
137{
138 switch (x) {
139 case FacePersistency::FACE_PERSISTENCY_NONE:
140 return y != FacePersistency::FACE_PERSISTENCY_NONE;
141 case FacePersistency::FACE_PERSISTENCY_ON_DEMAND:
142 return y == FacePersistency::FACE_PERSISTENCY_PERSISTENT ||
143 y == FacePersistency::FACE_PERSISTENCY_PERMANENT;
144 case FacePersistency::FACE_PERSISTENCY_PERSISTENT:
145 return y == FacePersistency::FACE_PERSISTENCY_PERMANENT;
146 case FacePersistency::FACE_PERSISTENCY_PERMANENT:
147 return false;
148 }
149 return static_cast<int>(x) < static_cast<int>(y);
150}
151
Junxiao Shi1d7fef52017-02-02 05:33:14 +0000152void
153FaceModule::create(ExecuteContext& ctx)
154{
Junxiao Shi0d976922017-04-01 14:35:21 +0000155 auto remoteUri = ctx.args.get<FaceUri>("remote");
156 auto localUri = ctx.args.getOptional<FaceUri>("local");
Junxiao Shi1d7fef52017-02-02 05:33:14 +0000157 auto persistency = ctx.args.get<FacePersistency>("persistency", FacePersistency::FACE_PERSISTENCY_PERSISTENT);
Eric Newberry84d3adc2017-08-09 23:31:40 -0400158 auto lpReliability = ctx.args.getTribool("reliability");
Eric Newberryde332452018-01-30 11:45:32 -0700159 auto congestionMarking = ctx.args.getTribool("congestion-marking");
160 auto baseCongestionMarkingIntervalMs = ctx.args.getOptional<uint64_t>("congestion-marking-interval");
161 auto defaultCongestionThreshold = ctx.args.getOptional<uint64_t>("default-congestion-threshold");
Eric Newberry13ff2592020-03-06 17:32:29 -0800162 auto mtuArg = ctx.args.getOptional<std::string>("mtu");
163
164 // MTU is nominally a uint64_t, but can be the string value 'auto' to unset an override MTU
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400165 std::optional<uint64_t> mtu;
Eric Newberry13ff2592020-03-06 17:32:29 -0800166 if (mtuArg == "auto") {
167 mtu = std::numeric_limits<uint64_t>::max();
168 }
169 else if (mtuArg) {
170 // boost::lexical_cast<uint64_t> will accept negative numbers
171 int64_t v = -1;
172 if (!boost::conversion::try_lexical_convert<int64_t>(*mtuArg, v) || v < 0) {
173 ctx.exitCode = 2;
174 ctx.err << "MTU must either be a non-negative integer or 'auto'\n";
175 return;
176 }
177
178 mtu = static_cast<uint64_t>(v);
179 }
Eric Newberry84d3adc2017-08-09 23:31:40 -0400180
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400181 std::optional<FaceUri> canonicalRemote;
182 std::optional<FaceUri> canonicalLocal;
Junxiao Shi0d976922017-04-01 14:35:21 +0000183
Eric Newberryd656aff2020-04-03 00:30:38 -0700184 auto updateFace = [&] (ControlParameters respParams, ControlParameters resp) {
Eric Newberry84d3adc2017-08-09 23:31:40 -0400185 // faces/update response does not have FaceUris, copy from faces/create response
186 resp.setLocalUri(respParams.getLocalUri())
187 .setUri(respParams.getUri());
Eric Newberryd656aff2020-04-03 00:30:38 -0700188 printSuccess(ctx.out, "face-updated", resp);
Eric Newberry84d3adc2017-08-09 23:31:40 -0400189 };
190
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000191 auto handle409 = [&] (const ControlResponse& resp) {
192 ControlParameters respParams(resp.getBody());
Eric Newberryd656aff2020-04-03 00:30:38 -0700193 if (respParams.getUri() != canonicalRemote->toString()) {
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000194 // we are conflicting with a different face, which is a general error
195 return false;
196 }
197
Eric Newberry13ff2592020-03-06 17:32:29 -0800198 bool isChangingParams = false;
199 ControlParameters params;
200 params.setFaceId(respParams.getFaceId());
201
Eric Newberry4f8dd962018-06-17 21:32:07 -0700202 if (mtu && (!respParams.hasMtu() || respParams.getMtu() != *mtu)) {
Eric Newberry13ff2592020-03-06 17:32:29 -0800203 isChangingParams = true;
204 params.setMtu(*mtu);
Eric Newberry4f8dd962018-06-17 21:32:07 -0700205 }
Eric Newberry13ff2592020-03-06 17:32:29 -0800206
207 if (persistencyLessThan(respParams.getFacePersistency(), persistency)) {
208 isChangingParams = true;
209 params.setFacePersistency(persistency);
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000210 }
Eric Newberryde332452018-01-30 11:45:32 -0700211
Eric Newberry13ff2592020-03-06 17:32:29 -0800212 if (!boost::logic::indeterminate(lpReliability) &&
213 lpReliability != respParams.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)) {
214 isChangingParams = true;
215 params.setFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED, bool(lpReliability));
216 }
Eric Newberryde332452018-01-30 11:45:32 -0700217
Eric Newberry13ff2592020-03-06 17:32:29 -0800218 if (!boost::logic::indeterminate(congestionMarking) &&
219 congestionMarking != respParams.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)) {
220 isChangingParams = true;
221 params.setFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED, bool(congestionMarking));
222 }
Eric Newberryde332452018-01-30 11:45:32 -0700223
Eric Newberry13ff2592020-03-06 17:32:29 -0800224 if (baseCongestionMarkingIntervalMs) {
225 isChangingParams = true;
226 params.setBaseCongestionMarkingInterval(time::milliseconds(*baseCongestionMarkingIntervalMs));
227 }
Eric Newberryde332452018-01-30 11:45:32 -0700228
Eric Newberry13ff2592020-03-06 17:32:29 -0800229 if (defaultCongestionThreshold) {
230 isChangingParams = true;
231 params.setDefaultCongestionThreshold(*defaultCongestionThreshold);
232 }
233
234 if (isChangingParams) {
Eric Newberry84d3adc2017-08-09 23:31:40 -0400235 ctx.controller.start<ndn::nfd::FaceUpdateCommand>(
Eric Newberry13ff2592020-03-06 17:32:29 -0800236 params,
Davide Pesavento412c9822021-07-02 00:21:05 -0400237 [=, &updateFace] (const auto& cp) { updateFace(respParams, cp); },
Eric Newberry13ff2592020-03-06 17:32:29 -0800238 ctx.makeCommandFailureHandler("updating face"),
239 ctx.makeCommandOptions());
Eric Newberry84d3adc2017-08-09 23:31:40 -0400240 }
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000241 else {
Eric Newberry84d3adc2017-08-09 23:31:40 -0400242 // don't do anything
Eric Newberryd656aff2020-04-03 00:30:38 -0700243 printSuccess(ctx.out, "face-exists", respParams);
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000244 }
Eric Newberry13ff2592020-03-06 17:32:29 -0800245
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000246 return true;
247 };
Junxiao Shi1d7fef52017-02-02 05:33:14 +0000248
Junxiao Shi0d976922017-04-01 14:35:21 +0000249 auto doCreateFace = [&] {
250 ControlParameters params;
Eric Newberryd656aff2020-04-03 00:30:38 -0700251 params.setUri(canonicalRemote->toString());
Junxiao Shi0d976922017-04-01 14:35:21 +0000252 if (canonicalLocal) {
253 params.setLocalUri(canonicalLocal->toString());
254 }
255 params.setFacePersistency(persistency);
Eric Newberry84d3adc2017-08-09 23:31:40 -0400256 if (!boost::logic::indeterminate(lpReliability)) {
Davide Pesaventofa2aa502019-03-22 13:30:02 -0400257 params.setFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED, bool(lpReliability));
Eric Newberry84d3adc2017-08-09 23:31:40 -0400258 }
Eric Newberryde332452018-01-30 11:45:32 -0700259 if (!boost::logic::indeterminate(congestionMarking)) {
Davide Pesaventofa2aa502019-03-22 13:30:02 -0400260 params.setFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED, bool(congestionMarking));
Eric Newberryde332452018-01-30 11:45:32 -0700261 }
262 if (baseCongestionMarkingIntervalMs) {
263 params.setBaseCongestionMarkingInterval(time::milliseconds(*baseCongestionMarkingIntervalMs));
264 }
265 if (defaultCongestionThreshold) {
266 params.setDefaultCongestionThreshold(*defaultCongestionThreshold);
267 }
Eric Newberry4f8dd962018-06-17 21:32:07 -0700268 if (mtu) {
269 params.setMtu(*mtu);
270 }
Junxiao Shi0d976922017-04-01 14:35:21 +0000271
272 ctx.controller.start<ndn::nfd::FaceCreateCommand>(
273 params,
Eric Newberryd656aff2020-04-03 00:30:38 -0700274 [&] (const ControlParameters& resp) {
275 printSuccess(ctx.out, "face-created", resp);
276 },
Junxiao Shi0d976922017-04-01 14:35:21 +0000277 [&] (const ControlResponse& resp) {
278 if (resp.getCode() == 409 && handle409(resp)) {
279 return;
280 }
281 ctx.makeCommandFailureHandler("creating face")(resp); // invoke general error handler
282 },
283 ctx.makeCommandOptions());
284 };
285
Eric Newberryd656aff2020-04-03 00:30:38 -0700286 std::string error;
287 std::tie(canonicalRemote, error) = canonize(ctx, remoteUri);
288 if (canonicalRemote) {
289 // RemoteUri canonization successful
290 if (localUri) {
291 std::tie(canonicalLocal, error) = canonize(ctx, *localUri);
292 if (canonicalLocal) {
293 // LocalUri canonization successful
Junxiao Shi0d976922017-04-01 14:35:21 +0000294 doCreateFace();
295 }
Eric Newberryd656aff2020-04-03 00:30:38 -0700296 else {
297 // LocalUri canonization failure
298 auto canonizationError = canonizeErrorHelper(*localUri, error, "local FaceUri");
299 ctx.exitCode = static_cast<int>(canonizationError.first);
300 ctx.err << canonizationError.second << '\n';
301 }
302 }
303 else {
304 doCreateFace();
305 }
306 }
307 else {
308 // RemoteUri canonization failure
309 auto canonizationError = canonizeErrorHelper(remoteUri, error, "remote FaceUri");
310 ctx.exitCode = static_cast<int>(canonizationError.first);
311 ctx.err << canonizationError.second << '\n';
312 }
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000313
314 ctx.face.processEvents();
315}
316
317void
Junxiao Shi05dd4442017-02-06 22:50:07 +0000318FaceModule::destroy(ExecuteContext& ctx)
319{
Junxiao Shi05dd4442017-02-06 22:50:07 +0000320 FindFace findFace(ctx);
Davide Pesavento8b663a92018-11-21 22:57:20 -0500321 FindFace::Code res = findFace.execute(ctx.args.at("face"));
Junxiao Shi05dd4442017-02-06 22:50:07 +0000322
323 ctx.exitCode = static_cast<int>(res);
324 switch (res) {
325 case FindFace::Code::OK:
326 break;
327 case FindFace::Code::ERROR:
328 case FindFace::Code::CANONIZE_ERROR:
329 case FindFace::Code::NOT_FOUND:
330 ctx.err << findFace.getErrorReason() << '\n';
331 return;
332 case FindFace::Code::AMBIGUOUS:
333 ctx.err << "Multiple faces match specified remote FaceUri. Re-run the command with a FaceId:";
334 findFace.printDisambiguation(ctx.err, FindFace::DisambiguationStyle::LOCAL_URI);
335 ctx.err << '\n';
336 return;
337 default:
338 BOOST_ASSERT_MSG(false, "unexpected FindFace result");
339 return;
340 }
341
342 const FaceStatus& face = findFace.getFaceStatus();
343
344 ctx.controller.start<ndn::nfd::FaceDestroyCommand>(
345 ControlParameters().setFaceId(face.getFaceId()),
346 [&] (const ControlParameters& resp) {
Eric Newberryd656aff2020-04-03 00:30:38 -0700347 // We can't use printSuccess because some face attributes come from FaceStatus not ControlResponse
Junxiao Shi05dd4442017-02-06 22:50:07 +0000348 ctx.out << "face-destroyed ";
349 text::ItemAttributes ia;
350 ctx.out << ia("id") << face.getFaceId()
351 << ia("local") << face.getLocalUri()
352 << ia("remote") << face.getRemoteUri()
Eric Newberryde332452018-01-30 11:45:32 -0700353 << ia("persistency") << face.getFacePersistency();
354 printFaceParams(ctx.out, ia, resp);
Junxiao Shi05dd4442017-02-06 22:50:07 +0000355 },
356 ctx.makeCommandFailureHandler("destroying face"),
357 ctx.makeCommandOptions());
358
359 ctx.face.processEvents();
360}
361
362void
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000363FaceModule::fetchStatus(Controller& controller,
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400364 const std::function<void()>& onSuccess,
Junxiao Shi29b41282016-08-22 03:47:02 +0000365 const Controller::DatasetFailCallback& onFailure,
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000366 const CommandOptions& options)
367{
368 controller.fetch<ndn::nfd::FaceDataset>(
369 [this, onSuccess] (const std::vector<FaceStatus>& result) {
370 m_status = result;
371 onSuccess();
372 },
373 onFailure, options);
374}
375
376void
377FaceModule::formatStatusXml(std::ostream& os) const
378{
379 os << "<faces>";
380 for (const FaceStatus& item : m_status) {
381 this->formatItemXml(os, item);
382 }
383 os << "</faces>";
384}
385
386void
387FaceModule::formatItemXml(std::ostream& os, const FaceStatus& item) const
388{
389 os << "<face>";
390
391 os << "<faceId>" << item.getFaceId() << "</faceId>";
392 os << "<remoteUri>" << xml::Text{item.getRemoteUri()} << "</remoteUri>";
393 os << "<localUri>" << xml::Text{item.getLocalUri()} << "</localUri>";
394
395 if (item.hasExpirationPeriod()) {
396 os << "<expirationPeriod>" << xml::formatDuration(item.getExpirationPeriod())
397 << "</expirationPeriod>";
398 }
399 os << "<faceScope>" << item.getFaceScope() << "</faceScope>";
400 os << "<facePersistency>" << item.getFacePersistency() << "</facePersistency>";
401 os << "<linkType>" << item.getLinkType() << "</linkType>";
402
Eric Newberryde332452018-01-30 11:45:32 -0700403 if (!item.hasBaseCongestionMarkingInterval() && !item.hasDefaultCongestionThreshold()) {
404 os << "<congestion/>";
405 }
406 else {
407 os << "<congestion>";
408 if (item.hasBaseCongestionMarkingInterval()) {
409 os << "<baseMarkingInterval>" << xml::formatDuration(item.getBaseCongestionMarkingInterval())
410 << "</baseMarkingInterval>";
411 }
412 if (item.hasDefaultCongestionThreshold()) {
413 os << "<defaultThreshold>" << item.getDefaultCongestionThreshold() << "</defaultThreshold>";
414 }
415 os << "</congestion>";
416 }
417
Eric Newberry4f8dd962018-06-17 21:32:07 -0700418 if (item.hasMtu()) {
419 os << "<mtu>" << item.getMtu() << "</mtu>";
420 }
421
Eric Newberry6d932e82016-11-24 05:05:43 +0000422 if (item.getFlags() == 0) {
423 os << "<flags/>";
424 }
425 else {
426 os << "<flags>";
Junxiao Shi7a36ac72018-03-21 15:23:22 +0000427 os << xml::Flag{"localFieldsEnabled", item.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)};
428 os << xml::Flag{"lpReliabilityEnabled", item.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)};
429 os << xml::Flag{"congestionMarkingEnabled", item.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)};
Eric Newberry6d932e82016-11-24 05:05:43 +0000430 os << "</flags>";
431 }
432
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000433 os << "<packetCounters>";
434 os << "<incomingPackets>"
435 << "<nInterests>" << item.getNInInterests() << "</nInterests>"
Junxiao Shif03d4792017-04-06 16:41:22 +0000436 << "<nData>" << item.getNInData() << "</nData>"
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000437 << "<nNacks>" << item.getNInNacks() << "</nNacks>"
438 << "</incomingPackets>";
439 os << "<outgoingPackets>"
440 << "<nInterests>" << item.getNOutInterests() << "</nInterests>"
Junxiao Shif03d4792017-04-06 16:41:22 +0000441 << "<nData>" << item.getNOutData() << "</nData>"
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000442 << "<nNacks>" << item.getNOutNacks() << "</nNacks>"
443 << "</outgoingPackets>";
444 os << "</packetCounters>";
445
446 os << "<byteCounters>";
447 os << "<incomingBytes>" << item.getNInBytes() << "</incomingBytes>";
448 os << "<outgoingBytes>" << item.getNOutBytes() << "</outgoingBytes>";
449 os << "</byteCounters>";
450
451 os << "</face>";
452}
453
454void
455FaceModule::formatStatusText(std::ostream& os) const
456{
457 os << "Faces:\n";
458 for (const FaceStatus& item : m_status) {
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000459 os << " ";
460 formatItemText(os, item, false);
461 os << '\n';
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000462 }
463}
464
465void
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000466FaceModule::formatItemText(std::ostream& os, const FaceStatus& item, bool wantMultiLine)
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000467{
Eric Newberryde332452018-01-30 11:45:32 -0700468 text::ItemAttributes ia(wantMultiLine, 10);
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000469
470 os << ia("faceid") << item.getFaceId();
471 os << ia("remote") << item.getRemoteUri();
472 os << ia("local") << item.getLocalUri();
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000473
474 if (item.hasExpirationPeriod()) {
Eric Newberryde332452018-01-30 11:45:32 -0700475 os << ia("expires") << text::formatDuration<time::seconds>(item.getExpirationPeriod());
476 }
477
478 if (item.hasBaseCongestionMarkingInterval() || item.hasDefaultCongestionThreshold()) {
479 os << ia("congestion") << "{";
480 text::Separator congestionSep("", " ");
481 if (item.hasBaseCongestionMarkingInterval()) {
482 os << congestionSep << "base-marking-interval="
483 << text::formatDuration<time::milliseconds>(item.getBaseCongestionMarkingInterval());
484 }
485 if (item.hasDefaultCongestionThreshold()) {
486 os << congestionSep << "default-threshold=" << item.getDefaultCongestionThreshold() << "B";
487 }
488 os << "}";
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000489 }
490
Eric Newberry4f8dd962018-06-17 21:32:07 -0700491 if (item.hasMtu()) {
492 os << ia("mtu") << item.getMtu();
493 }
494
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000495 os << ia("counters")
496 << "{in={"
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000497 << item.getNInInterests() << "i "
Junxiao Shif03d4792017-04-06 16:41:22 +0000498 << item.getNInData() << "d "
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000499 << item.getNInNacks() << "n "
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000500 << item.getNInBytes() << "B} "
501 << "out={"
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000502 << item.getNOutInterests() << "i "
Junxiao Shif03d4792017-04-06 16:41:22 +0000503 << item.getNOutData() << "d "
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000504 << item.getNOutNacks() << "n "
505 << item.getNOutBytes() << "B}}";
506
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000507 os << ia("flags") << '{';
508 text::Separator flagSep("", " ");
509 os << flagSep << item.getFaceScope();
510 os << flagSep << item.getFacePersistency();
511 os << flagSep << item.getLinkType();
Eric Newberry6d932e82016-11-24 05:05:43 +0000512 if (item.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)) {
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000513 os << flagSep << "local-fields";
Eric Newberry6d932e82016-11-24 05:05:43 +0000514 }
Eric Newberry84d3adc2017-08-09 23:31:40 -0400515 if (item.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)) {
516 os << flagSep << "lp-reliability";
517 }
Eric Newberryde332452018-01-30 11:45:32 -0700518 if (item.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)) {
519 os << flagSep << "congestion-marking";
520 }
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000521 os << '}';
Eric Newberry6d932e82016-11-24 05:05:43 +0000522
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000523 os << ia.end();
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000524}
525
Eric Newberryde332452018-01-30 11:45:32 -0700526void
Eric Newberryd656aff2020-04-03 00:30:38 -0700527FaceModule::printSuccess(std::ostream& os,
528 const std::string& actionSummary,
529 const ControlParameters& resp)
530{
531 text::ItemAttributes ia;
532 os << actionSummary << ' '
533 << ia("id") << resp.getFaceId()
534 << ia("local") << resp.getLocalUri()
535 << ia("remote") << resp.getUri()
536 << ia("persistency") << resp.getFacePersistency();
537 printFaceParams(os, ia, resp);
538}
539
540void
Eric Newberryde332452018-01-30 11:45:32 -0700541FaceModule::printFaceParams(std::ostream& os, text::ItemAttributes& ia, const ControlParameters& resp)
542{
Junxiao Shi8dc473a2018-03-02 15:10:18 +0000543 os << ia("reliability") << text::OnOff{resp.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)}
544 << ia("congestion-marking") << text::OnOff{resp.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)};
Eric Newberryde332452018-01-30 11:45:32 -0700545 if (resp.hasBaseCongestionMarkingInterval()) {
546 os << ia("congestion-marking-interval")
547 << text::formatDuration<time::milliseconds>(resp.getBaseCongestionMarkingInterval());
548 }
549 if (resp.hasDefaultCongestionThreshold()) {
550 os << ia("default-congestion-threshold") << resp.getDefaultCongestionThreshold() << "B";
551 }
Eric Newberry4f8dd962018-06-17 21:32:07 -0700552 if (resp.hasMtu()) {
553 os << ia("mtu") << resp.getMtu();
554 }
Eric Newberryde332452018-01-30 11:45:32 -0700555 os << '\n';
556}
557
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400558} // namespace nfd::tools::nfdc