blob: f1a9765ceeddb5c2eebf65a69cac1d4556ba2c80 [file] [log] [blame]
Junxiao Shi2c29f3a2014-01-24 19:59:00 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Eric Newberry41aba102017-11-01 16:42:13 -07002/*
Davide Pesaventoe0b67df2024-02-17 19:14:24 -05003 * Copyright (c) 2014-2024, Regents of the University of California,
Alexander Afanasyev319f2c82015-01-07 14:56:53 -08004 * 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.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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/>.
Junxiao Shi33152f12014-07-16 19:54:32 -070024 */
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070025
Eric Newberry41aba102017-11-01 16:42:13 -070026#ifndef NFD_DAEMON_FACE_FACE_HPP
27#define NFD_DAEMON_FACE_FACE_HPP
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070028
Davide Pesaventocb425e82019-07-14 21:48:22 -040029#include "face-common.hpp"
Eric Newberry41aba102017-11-01 16:42:13 -070030#include "link-service.hpp"
31#include "transport.hpp"
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070032
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080033namespace nfd {
Junxiao Shicde37ad2015-12-24 01:02:05 -070034namespace face {
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070035
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -040036class Channel;
37
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040038/**
39 * \brief Indicates the state of a face.
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070040 */
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040041using FaceState = TransportState;
Junxiao Shicde37ad2015-12-24 01:02:05 -070042
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050043/**
44 * \brief Gives access to the counters provided by Face.
Junxiao Shicde37ad2015-12-24 01:02:05 -070045 *
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050046 * This type is a facade that exposes common counters of a Face.
Junxiao Shicde37ad2015-12-24 01:02:05 -070047 *
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050048 * get<T>() can be used to access extended counters provided by
49 * LinkService or Transport of the Face.
50 */
51class FaceCounters
52{
53public:
54 FaceCounters(const LinkService::Counters& linkServiceCounters,
55 const Transport::Counters& transportCounters);
56
57 /**
58 * \brief Returns the counters provided by (a subclass of) LinkService.
59 * \tparam T The desired counters type
60 * \throw std::bad_cast counters type mismatch
61 */
62 template<typename T>
63 std::enable_if_t<std::is_base_of_v<LinkService::Counters, T>, const T&>
64 get() const
65 {
66 return dynamic_cast<const T&>(m_linkServiceCounters);
67 }
68
69 /**
70 * \brief Returns the counters provided by (a subclass of) Transport.
71 * \tparam T The desired counters type
72 * \throw std::bad_cast counters type mismatch
73 */
74 template<typename T>
75 std::enable_if_t<std::is_base_of_v<Transport::Counters, T>, const T&>
76 get() const
77 {
78 return dynamic_cast<const T&>(m_transportCounters);
79 }
80
81public:
82 const PacketCounter& nInInterests; ///< \copydoc LinkService::Counters::nInInterests
83 const PacketCounter& nOutInterests; ///< \copydoc LinkService::Counters::nOutInterests
84 const PacketCounter& nInterestsExceededRetx; ///< \copydoc LinkService::Counters::nInterestsExceededRetx
85 const PacketCounter& nInData; ///< \copydoc LinkService::Counters::nInData
86 const PacketCounter& nOutData; ///< \copydoc LinkService::Counters::nOutData
87 const PacketCounter& nInNacks; ///< \copydoc LinkService::Counters::nInNacks
88 const PacketCounter& nOutNacks; ///< \copydoc LinkService::Counters::nOutNacks
89
90 const PacketCounter& nInPackets; ///< \copydoc Transport::Counters::nInPackets
91 const PacketCounter& nOutPackets; ///< \copydoc Transport::Counters::nOutPackets
92 const ByteCounter& nInBytes; ///< \copydoc Transport::Counters::nInBytes
93 const ByteCounter& nOutBytes; ///< \copydoc Transport::Counters::nOutBytes
94
95 /// Count of incoming Interests dropped due to HopLimit == 0.
96 PacketCounter nInHopLimitZero;
97 /// Count of outgoing Interests dropped due to HopLimit == 0 on non-local faces.
98 PacketCounter nOutHopLimitZero;
99
100private:
101 const LinkService::Counters& m_linkServiceCounters;
102 const Transport::Counters& m_transportCounters;
103};
104
105/**
106 * \brief Generalization of a network interface.
107 *
108 * A face generalizes a network interface.
109 * It provides best-effort network-layer packet delivery services
110 * on a physical interface, an overlay tunnel, or a link to a local application.
111 *
112 * A face combines two parts: LinkService and Transport.
113 * Transport is the lower part, which provides best-effort TLV block deliveries.
114 * LinkService is the upper part, which translates between network-layer packets
115 * and TLV blocks, and may provide additional services such as fragmentation and reassembly.
Junxiao Shicde37ad2015-12-24 01:02:05 -0700116 */
Davide Pesavento264af772021-02-09 21:48:24 -0500117class Face NFD_FINAL_UNLESS_WITH_TESTS : public std::enable_shared_from_this<Face>, noncopyable
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700118{
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700119public:
Junxiao Shicde37ad2015-12-24 01:02:05 -0700120 Face(unique_ptr<LinkService> service, unique_ptr<Transport> transport);
121
122 LinkService*
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400123 getLinkService() const noexcept
124 {
125 return m_service.get();
126 }
Junxiao Shicde37ad2015-12-24 01:02:05 -0700127
128 Transport*
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400129 getTransport() const noexcept
130 {
131 return m_transport.get();
132 }
Junxiao Shicde37ad2015-12-24 01:02:05 -0700133
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500134 /**
135 * \brief Request that the face be closed.
Eric Newberrycb6551e2020-03-02 14:12:16 -0800136 *
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500137 * This operation is effective only if face is in the UP or DOWN state; otherwise,
138 * it has no effect. The face will change state to CLOSING, and then perform a
139 * cleanup procedure. When the cleanup is complete, the state will be changed to
140 * CLOSED, which may happen synchronously or asynchronously.
Eric Newberrycb6551e2020-03-02 14:12:16 -0800141 *
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500142 * \warning The face must not be deallocated until its state changes to CLOSED.
Eric Newberrycb6551e2020-03-02 14:12:16 -0800143 */
144 void
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500145 close()
146 {
147 m_transport->close();
148 }
Eric Newberrycb6551e2020-03-02 14:12:16 -0800149
Junxiao Shicde37ad2015-12-24 01:02:05 -0700150public: // upper interface connected to forwarding
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500151 /**
152 * \brief Send Interest.
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800153 */
Junxiao Shicde37ad2015-12-24 01:02:05 -0700154 void
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500155 sendInterest(const Interest& interest)
156 {
157 m_service->sendInterest(interest);
158 }
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800159
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500160 /**
161 * \brief Send Data.
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800162 */
Junxiao Shicde37ad2015-12-24 01:02:05 -0700163 void
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500164 sendData(const Data& data)
165 {
166 m_service->sendData(data);
167 }
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700168
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500169 /**
170 * \brief Send Nack.
Junxiao Shicde37ad2015-12-24 01:02:05 -0700171 */
172 void
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500173 sendNack(const lp::Nack& nack)
174 {
175 m_service->sendNack(nack);
176 }
Junxiao Shicde37ad2015-12-24 01:02:05 -0700177
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500178 /// \copydoc LinkService::afterReceiveInterest
ashiqopu075bb7d2019-03-10 01:38:21 +0000179 signal::Signal<LinkService, Interest, EndpointId>& afterReceiveInterest;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700180
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500181 /// \copydoc LinkService::afterReceiveData
ashiqopu075bb7d2019-03-10 01:38:21 +0000182 signal::Signal<LinkService, Data, EndpointId>& afterReceiveData;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700183
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500184 /// \copydoc LinkService::afterReceiveNack
ashiqopu075bb7d2019-03-10 01:38:21 +0000185 signal::Signal<LinkService, lp::Nack, EndpointId>& afterReceiveNack;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700186
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500187 /// \copydoc LinkService::onDroppedInterest
Eric Newberry41aba102017-11-01 16:42:13 -0700188 signal::Signal<LinkService, Interest>& onDroppedInterest;
189
Eric Newberrycb6551e2020-03-02 14:12:16 -0800190public: // properties
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400191 /**
192 * \brief Returns the face ID.
Junxiao Shicde37ad2015-12-24 01:02:05 -0700193 */
Junxiao Shi79494162014-04-02 18:25:11 -0700194 FaceId
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400195 getId() const noexcept
196 {
197 return m_id;
198 }
Junxiao Shi79494162014-04-02 18:25:11 -0700199
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400200 /**
201 * \brief Sets the face ID.
202 * \note Normally, this should only be invoked by FaceTable.
Davide Pesavento94279412015-02-27 01:29:32 +0100203 */
204 void
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400205 setId(FaceId id) noexcept
206 {
207 m_id = id;
208 }
Davide Pesavento94279412015-02-27 01:29:32 +0100209
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400210 /**
211 * \brief Returns a FaceUri representing the local endpoint.
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100212 */
Junxiao Shicde37ad2015-12-24 01:02:05 -0700213 FaceUri
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500214 getLocalUri() const noexcept
215 {
216 return m_transport->getLocalUri();
217 }
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100218
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400219 /**
220 * \brief Returns a FaceUri representing the remote endpoint.
Junxiao Shicde37ad2015-12-24 01:02:05 -0700221 */
222 FaceUri
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500223 getRemoteUri() const noexcept
224 {
225 return m_transport->getRemoteUri();
226 }
Junxiao Shicde37ad2015-12-24 01:02:05 -0700227
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400228 /**
229 * \brief Returns whether the face is local or non-local for scope control purposes.
Junxiao Shicde37ad2015-12-24 01:02:05 -0700230 */
231 ndn::nfd::FaceScope
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500232 getScope() const noexcept
233 {
234 return m_transport->getScope();
235 }
Junxiao Shicde37ad2015-12-24 01:02:05 -0700236
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400237 /**
238 * \brief Returns the current persistency setting of the face.
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700239 */
Yukai Tu731f0d72015-07-04 11:14:44 +0800240 ndn::nfd::FacePersistency
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500241 getPersistency() const noexcept
242 {
243 return m_transport->getPersistency();
244 }
Davide Pesavento94279412015-02-27 01:29:32 +0100245
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400246 /**
247 * \brief Changes the face persistency setting.
Junxiao Shi08d07a72014-06-09 23:17:57 -0700248 */
249 void
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500250 setPersistency(ndn::nfd::FacePersistency persistency)
251 {
252 return m_transport->setPersistency(persistency);
253 }
Junxiao Shi08d07a72014-06-09 23:17:57 -0700254
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400255 /**
256 * \brief Returns the link type of the face (point-to-point, multi-access, ...).
Junxiao Shicde37ad2015-12-24 01:02:05 -0700257 */
258 ndn::nfd::LinkType
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500259 getLinkType() const noexcept
260 {
261 return m_transport->getLinkType();
262 }
Davide Pesavento94279412015-02-27 01:29:32 +0100263
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400264 /**
265 * \brief Returns the effective MTU of the face.
Eric Newberrycb6551e2020-03-02 14:12:16 -0800266 *
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400267 * This function is a wrapper. The effective MTU of a face is determined by the link service.
Eric Newberrycb6551e2020-03-02 14:12:16 -0800268 */
269 ssize_t
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500270 getMtu() const
271 {
272 return m_service->getEffectiveMtu();
273 }
Eric Newberrycb6551e2020-03-02 14:12:16 -0800274
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400275 /**
276 * \brief Returns the face state.
Junxiao Shicde37ad2015-12-24 01:02:05 -0700277 */
278 FaceState
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500279 getState() const noexcept
280 {
281 return m_transport->getState();
282 }
Junxiao Shic099ddb2014-12-25 20:53:20 -0700283
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500284 /// \copydoc Transport::afterStateChange
285 signal::Signal<Transport, FaceState /*old*/, FaceState /*new*/>& afterStateChange;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700286
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400287 /**
288 * \brief Returns the expiration time of the face.
289 * \retval time::steady_clock::time_point::max() The face has an indefinite lifetime.
Eric Newberryc64d30a2015-12-26 11:07:27 -0700290 */
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400291 time::steady_clock::time_point
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500292 getExpirationTime() const noexcept
293 {
294 return m_transport->getExpirationTime();
295 }
Eric Newberryc64d30a2015-12-26 11:07:27 -0700296
Junxiao Shicde37ad2015-12-24 01:02:05 -0700297 const FaceCounters&
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400298 getCounters() const noexcept
Davide Pesavento0498ce82021-06-14 02:02:21 -0400299 {
300 return m_counters;
301 }
302
303 FaceCounters&
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400304 getCounters() noexcept
Davide Pesavento0498ce82021-06-14 02:02:21 -0400305 {
306 return m_counters;
307 }
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700308
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -0400309 /**
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400310 * \brief Get channel on which face was created (unicast) or the associated channel (multicast).
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -0400311 */
312 weak_ptr<Channel>
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500313 getChannel() const noexcept
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -0400314 {
315 return m_channel;
316 }
317
318 /**
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400319 * \brief Set channel on which face was created (unicast) or the associated channel (multicast).
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -0400320 */
321 void
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500322 setChannel(weak_ptr<Channel> channel) noexcept
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -0400323 {
324 m_channel = std::move(channel);
325 }
326
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700327private:
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400328 FaceId m_id = INVALID_FACEID;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700329 unique_ptr<LinkService> m_service;
330 unique_ptr<Transport> m_transport;
331 FaceCounters m_counters;
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -0400332 weak_ptr<Channel> m_channel;
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700333};
334
Junxiao Shicde37ad2015-12-24 01:02:05 -0700335std::ostream&
336operator<<(std::ostream& os, const FaceLogHelper<Face>& flh);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000337
Junxiao Shicde37ad2015-12-24 01:02:05 -0700338} // namespace face
339
Junxiao Shicde37ad2015-12-24 01:02:05 -0700340using face::Face;
341
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800342} // namespace nfd
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700343
Eric Newberry41aba102017-11-01 16:42:13 -0700344#endif // NFD_DAEMON_FACE_FACE_HPP