blob: 66dab05f5b1a35a23c14bdbbf5a748d7cb3c2fe5 [file] [log] [blame]
Eric Newberrya98bf932015-09-21 00:58:47 -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,
Eric Newberrya98bf932015-09-21 00:58:47 -07004 * 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
26#ifndef NFD_DAEMON_FACE_LINK_SERVICE_HPP
27#define NFD_DAEMON_FACE_LINK_SERVICE_HPP
28
Davide Pesaventocb425e82019-07-14 21:48:22 -040029#include "face-common.hpp"
Eric Newberry41aba102017-11-01 16:42:13 -070030#include "transport.hpp"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040031#include "common/counter.hpp"
Eric Newberrya98bf932015-09-21 00:58:47 -070032
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040033namespace nfd::face {
Eric Newberrya98bf932015-09-21 00:58:47 -070034
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050035/**
36 * \brief Counters provided by LinkService.
37 * \note The type name LinkServiceCounters is an implementation detail.
38 * Use LinkService::Counters in public API.
Junxiao Shi57df2882015-11-11 06:12:35 -070039 */
40class LinkServiceCounters
41{
42public:
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050043 /// Count of incoming Interest packets.
Junxiao Shi57df2882015-11-11 06:12:35 -070044 PacketCounter nInInterests;
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050045 /// Count of outgoing Interest packets.
Junxiao Shi57df2882015-11-11 06:12:35 -070046 PacketCounter nOutInterests;
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050047 /// Count of Interests dropped by reliability system for exceeding allowed number of retx.
Eric Newberry9d283ad2020-04-12 23:37:17 -070048 PacketCounter nInterestsExceededRetx;
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050049 /// Count of incoming Data packets.
Junxiao Shi57df2882015-11-11 06:12:35 -070050 PacketCounter nInData;
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050051 /// Count of outgoing Data packets.
Junxiao Shi57df2882015-11-11 06:12:35 -070052 PacketCounter nOutData;
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050053 /// Count of incoming Nack packets.
Junxiao Shi57df2882015-11-11 06:12:35 -070054 PacketCounter nInNacks;
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050055 /// Count of outgoing Nack packets.
Junxiao Shi57df2882015-11-11 06:12:35 -070056 PacketCounter nOutNacks;
57};
58
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040059/**
60 * \brief The upper half of a Face.
61 * \sa Face, Transport
Eric Newberrya98bf932015-09-21 00:58:47 -070062 */
Junxiao Shi57df2882015-11-11 06:12:35 -070063class LinkService : protected virtual LinkServiceCounters, noncopyable
Eric Newberrya98bf932015-09-21 00:58:47 -070064{
65public:
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040066 /**
67 * \brief %Counters provided by LinkService.
Junxiao Shi57df2882015-11-11 06:12:35 -070068 */
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040069 using Counters = LinkServiceCounters;
Junxiao Shi57df2882015-11-11 06:12:35 -070070
71public:
Eric Newberrya98bf932015-09-21 00:58:47 -070072 virtual
73 ~LinkService();
74
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040075 /**
76 * \brief Set Face and Transport for this LinkService.
77 * \pre setFaceAndTransport() has not been called.
Eric Newberrya98bf932015-09-21 00:58:47 -070078 */
79 void
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040080 setFaceAndTransport(Face& face, Transport& transport) noexcept;
Eric Newberrya98bf932015-09-21 00:58:47 -070081
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040082 /**
83 * \brief Returns the Face to which this LinkService is attached.
Eric Newberrya98bf932015-09-21 00:58:47 -070084 */
Junxiao Shicde37ad2015-12-24 01:02:05 -070085 const Face*
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040086 getFace() const noexcept
87 {
88 return m_face;
89 }
Eric Newberrya98bf932015-09-21 00:58:47 -070090
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040091 /**
92 * \brief Returns the Transport to which this LinkService is attached.
Eric Newberrya98bf932015-09-21 00:58:47 -070093 */
94 const Transport*
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040095 getTransport() const noexcept
96 {
97 return m_transport;
98 }
Eric Newberrya98bf932015-09-21 00:58:47 -070099
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400100 /**
101 * \brief Returns the Transport to which this LinkService is attached.
Eric Newberrya98bf932015-09-21 00:58:47 -0700102 */
103 Transport*
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400104 getTransport() noexcept
105 {
106 return m_transport;
107 }
Eric Newberrya98bf932015-09-21 00:58:47 -0700108
Junxiao Shi57df2882015-11-11 06:12:35 -0700109 virtual const Counters&
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400110 getCounters() const
111 {
112 return *this;
113 }
Junxiao Shi57df2882015-11-11 06:12:35 -0700114
Eric Newberrycb6551e2020-03-02 14:12:16 -0800115 virtual ssize_t
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500116 getEffectiveMtu() const
117 {
118 return m_transport->getMtu();
119 }
Eric Newberrycb6551e2020-03-02 14:12:16 -0800120
Eric Newberrya98bf932015-09-21 00:58:47 -0700121public: // upper interface to be used by forwarding
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500122 /**
123 * \brief Send Interest.
124 * \pre setFaceAndTransport() has been called.
Eric Newberrya98bf932015-09-21 00:58:47 -0700125 */
126 void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700127 sendInterest(const Interest& interest);
Eric Newberrya98bf932015-09-21 00:58:47 -0700128
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500129 /**
130 * \brief Send Data.
131 * \pre setFaceAndTransport() has been called.
Eric Newberrya98bf932015-09-21 00:58:47 -0700132 */
133 void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700134 sendData(const Data& data);
Eric Newberrya98bf932015-09-21 00:58:47 -0700135
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500136 /**
137 * \brief Send Nack.
138 * \pre setFaceAndTransport() has been called.
Eric Newberrya98bf932015-09-21 00:58:47 -0700139 */
140 void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700141 sendNack(const ndn::lp::Nack& nack);
Eric Newberrya98bf932015-09-21 00:58:47 -0700142
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500143 /**
144 * \brief Called when an Interest packet is received.
Eric Newberrya98bf932015-09-21 00:58:47 -0700145 */
ashiqopu075bb7d2019-03-10 01:38:21 +0000146 signal::Signal<LinkService, Interest, EndpointId> afterReceiveInterest;
Eric Newberrya98bf932015-09-21 00:58:47 -0700147
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500148 /**
149 * \brief Called when a Data packet is received.
Eric Newberrya98bf932015-09-21 00:58:47 -0700150 */
ashiqopu075bb7d2019-03-10 01:38:21 +0000151 signal::Signal<LinkService, Data, EndpointId> afterReceiveData;
Eric Newberrya98bf932015-09-21 00:58:47 -0700152
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500153 /**
154 * \brief Called when a Nack packet is received.
Eric Newberrya98bf932015-09-21 00:58:47 -0700155 */
ashiqopu075bb7d2019-03-10 01:38:21 +0000156 signal::Signal<LinkService, lp::Nack, EndpointId> afterReceiveNack;
Eric Newberrya98bf932015-09-21 00:58:47 -0700157
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500158 /**
159 * \brief Called when an Interest is dropped by the reliability system
160 * for exceeding the allowed number of retransmissions.
Eric Newberry41aba102017-11-01 16:42:13 -0700161 */
162 signal::Signal<LinkService, Interest> onDroppedInterest;
163
Junxiao Shi57df2882015-11-11 06:12:35 -0700164public: // lower interface to be invoked by Transport
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500165 /**
166 * \brief Performs LinkService-specific operations to receive a lower-layer packet.
Eric Newberrya98bf932015-09-21 00:58:47 -0700167 */
Junxiao Shi57df2882015-11-11 06:12:35 -0700168 void
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500169 receivePacket(const Block& packet, const EndpointId& endpoint)
170 {
171 doReceivePacket(packet, endpoint);
172 }
Eric Newberrya98bf932015-09-21 00:58:47 -0700173
174protected: // upper interface to be invoked in subclass (receive path termination)
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500175 /**
176 * \brief Delivers received Interest to forwarding.
Eric Newberrya98bf932015-09-21 00:58:47 -0700177 */
178 void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400179 receiveInterest(const Interest& interest, const EndpointId& endpoint);
Eric Newberrya98bf932015-09-21 00:58:47 -0700180
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500181 /**
182 * \brief Delivers received Data to forwarding.
Eric Newberrya98bf932015-09-21 00:58:47 -0700183 */
184 void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400185 receiveData(const Data& data, const EndpointId& endpoint);
Eric Newberrya98bf932015-09-21 00:58:47 -0700186
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500187 /**
188 * \brief Delivers received Nack to forwarding.
Eric Newberrya98bf932015-09-21 00:58:47 -0700189 */
190 void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400191 receiveNack(const lp::Nack& nack, const EndpointId& endpoint);
Eric Newberrya98bf932015-09-21 00:58:47 -0700192
Eric Newberrya98bf932015-09-21 00:58:47 -0700193protected: // lower interface to be invoked in subclass (send path termination)
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500194 /**
195 * \brief Send a lower-layer packet via Transport.
Eric Newberrya98bf932015-09-21 00:58:47 -0700196 */
197 void
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500198 sendPacket(const Block& packet)
199 {
200 m_transport->send(packet);
201 }
Eric Newberrya98bf932015-09-21 00:58:47 -0700202
Eric Newberry41aba102017-11-01 16:42:13 -0700203protected:
204 void
205 notifyDroppedInterest(const Interest& packet);
206
Junxiao Shi57df2882015-11-11 06:12:35 -0700207private: // upper interface to be overridden in subclass (send path entrypoint)
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500208 /**
209 * \brief Performs LinkService-specific operations to send an Interest.
Junxiao Shi57df2882015-11-11 06:12:35 -0700210 */
211 virtual void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700212 doSendInterest(const Interest& interest) = 0;
Junxiao Shi57df2882015-11-11 06:12:35 -0700213
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500214 /**
215 * \brief Performs LinkService-specific operations to send a Data.
Junxiao Shi57df2882015-11-11 06:12:35 -0700216 */
217 virtual void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700218 doSendData(const Data& data) = 0;
Junxiao Shi57df2882015-11-11 06:12:35 -0700219
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500220 /**
221 * \brief Performs LinkService-specific operations to send a Nack.
Junxiao Shi57df2882015-11-11 06:12:35 -0700222 */
223 virtual void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700224 doSendNack(const lp::Nack& nack) = 0;
Junxiao Shi57df2882015-11-11 06:12:35 -0700225
Eric Newberrya98bf932015-09-21 00:58:47 -0700226private: // lower interface to be overridden in subclass
227 virtual void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400228 doReceivePacket(const Block& packet, const EndpointId& endpoint) = 0;
Eric Newberrya98bf932015-09-21 00:58:47 -0700229
230private:
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400231 Face* m_face = nullptr;
232 Transport* m_transport = nullptr;
Eric Newberrya98bf932015-09-21 00:58:47 -0700233};
234
Eric Newberrya98bf932015-09-21 00:58:47 -0700235std::ostream&
236operator<<(std::ostream& os, const FaceLogHelper<LinkService>& flh);
237
238template<typename T>
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400239std::enable_if_t<std::is_base_of_v<LinkService, T> && !std::is_same_v<LinkService, T>,
240 std::ostream&>
Eric Newberrya98bf932015-09-21 00:58:47 -0700241operator<<(std::ostream& os, const FaceLogHelper<T>& flh)
242{
243 return os << FaceLogHelper<LinkService>(flh.obj);
244}
245
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400246} // namespace nfd::face
Eric Newberrya98bf932015-09-21 00:58:47 -0700247
248#endif // NFD_DAEMON_FACE_LINK_SERVICE_HPP