blob: da752e6208b245182e0e3e7aab3221b31c7f7698 [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 Pesaventob7bfcb92022-05-22 23:55:23 -04003 * Copyright (c) 2014-2022, 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 Pesaventoaa9e3b22022-10-21 17:00:07 -040035/** \brief Counters provided by LinkService.
36 * \note The type name LinkServiceCounters is an implementation detail.
37 * Use LinkService::Counters in public API.
Junxiao Shi57df2882015-11-11 06:12:35 -070038 */
39class LinkServiceCounters
40{
41public:
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040042 /** \brief Count of incoming Interests.
Junxiao Shi57df2882015-11-11 06:12:35 -070043 */
44 PacketCounter nInInterests;
45
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040046 /** \brief Count of outgoing Interests.
Junxiao Shi57df2882015-11-11 06:12:35 -070047 */
48 PacketCounter nOutInterests;
49
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040050 /** \brief Count of Interests dropped by reliability system for exceeding allowed number of retx.
Eric Newberry41aba102017-11-01 16:42:13 -070051 */
Eric Newberry9d283ad2020-04-12 23:37:17 -070052 PacketCounter nInterestsExceededRetx;
Eric Newberry41aba102017-11-01 16:42:13 -070053
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040054 /** \brief Count of incoming Data packets.
Junxiao Shi57df2882015-11-11 06:12:35 -070055 */
56 PacketCounter nInData;
57
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040058 /** \brief Count of outgoing Data packets.
Junxiao Shi57df2882015-11-11 06:12:35 -070059 */
60 PacketCounter nOutData;
61
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040062 /** \brief Count of incoming Nacks.
Junxiao Shi57df2882015-11-11 06:12:35 -070063 */
64 PacketCounter nInNacks;
65
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040066 /** \brief Count of outgoing Nacks.
Junxiao Shi57df2882015-11-11 06:12:35 -070067 */
68 PacketCounter nOutNacks;
69};
70
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040071/**
72 * \brief The upper half of a Face.
73 * \sa Face, Transport
Eric Newberrya98bf932015-09-21 00:58:47 -070074 */
Junxiao Shi57df2882015-11-11 06:12:35 -070075class LinkService : protected virtual LinkServiceCounters, noncopyable
Eric Newberrya98bf932015-09-21 00:58:47 -070076{
77public:
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040078 /**
79 * \brief %Counters provided by LinkService.
Junxiao Shi57df2882015-11-11 06:12:35 -070080 */
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040081 using Counters = LinkServiceCounters;
Junxiao Shi57df2882015-11-11 06:12:35 -070082
83public:
Eric Newberrya98bf932015-09-21 00:58:47 -070084 virtual
85 ~LinkService();
86
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040087 /**
88 * \brief Set Face and Transport for this LinkService.
89 * \pre setFaceAndTransport() has not been called.
Eric Newberrya98bf932015-09-21 00:58:47 -070090 */
91 void
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040092 setFaceAndTransport(Face& face, Transport& transport) noexcept;
Eric Newberrya98bf932015-09-21 00:58:47 -070093
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040094 /**
95 * \brief Returns the Face to which this LinkService is attached.
Eric Newberrya98bf932015-09-21 00:58:47 -070096 */
Junxiao Shicde37ad2015-12-24 01:02:05 -070097 const Face*
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040098 getFace() const noexcept
99 {
100 return m_face;
101 }
Eric Newberrya98bf932015-09-21 00:58:47 -0700102
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400103 /**
104 * \brief Returns the Transport to which this LinkService is attached.
Eric Newberrya98bf932015-09-21 00:58:47 -0700105 */
106 const Transport*
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400107 getTransport() const noexcept
108 {
109 return m_transport;
110 }
Eric Newberrya98bf932015-09-21 00:58:47 -0700111
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400112 /**
113 * \brief Returns the Transport to which this LinkService is attached.
Eric Newberrya98bf932015-09-21 00:58:47 -0700114 */
115 Transport*
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400116 getTransport() noexcept
117 {
118 return m_transport;
119 }
Eric Newberrya98bf932015-09-21 00:58:47 -0700120
Junxiao Shi57df2882015-11-11 06:12:35 -0700121 virtual const Counters&
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400122 getCounters() const
123 {
124 return *this;
125 }
Junxiao Shi57df2882015-11-11 06:12:35 -0700126
Eric Newberrycb6551e2020-03-02 14:12:16 -0800127 virtual ssize_t
128 getEffectiveMtu() const;
129
Eric Newberrya98bf932015-09-21 00:58:47 -0700130public: // upper interface to be used by forwarding
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400131 /** \brief Send Interest.
132 * \pre setFaceAndTransport() has been called.
Eric Newberrya98bf932015-09-21 00:58:47 -0700133 */
134 void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700135 sendInterest(const Interest& interest);
Eric Newberrya98bf932015-09-21 00:58:47 -0700136
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400137 /** \brief Send Data.
138 * \pre setFaceAndTransport() has been called.
Eric Newberrya98bf932015-09-21 00:58:47 -0700139 */
140 void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700141 sendData(const Data& data);
Eric Newberrya98bf932015-09-21 00:58:47 -0700142
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400143 /** \brief Send Nack.
144 * \pre setFaceAndTransport() has been called.
Eric Newberrya98bf932015-09-21 00:58:47 -0700145 */
146 void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700147 sendNack(const ndn::lp::Nack& nack);
Eric Newberrya98bf932015-09-21 00:58:47 -0700148
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400149 /** \brief Signals on Interest received.
Eric Newberrya98bf932015-09-21 00:58:47 -0700150 */
ashiqopu075bb7d2019-03-10 01:38:21 +0000151 signal::Signal<LinkService, Interest, EndpointId> afterReceiveInterest;
Eric Newberrya98bf932015-09-21 00:58:47 -0700152
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400153 /** \brief Signals on Data received.
Eric Newberrya98bf932015-09-21 00:58:47 -0700154 */
ashiqopu075bb7d2019-03-10 01:38:21 +0000155 signal::Signal<LinkService, Data, EndpointId> afterReceiveData;
Eric Newberrya98bf932015-09-21 00:58:47 -0700156
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400157 /** \brief Signals on Nack received.
Eric Newberrya98bf932015-09-21 00:58:47 -0700158 */
ashiqopu075bb7d2019-03-10 01:38:21 +0000159 signal::Signal<LinkService, lp::Nack, EndpointId> afterReceiveNack;
Eric Newberrya98bf932015-09-21 00:58:47 -0700160
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400161 /** \brief Signals on Interest dropped by reliability system for exceeding allowed number of retx.
Eric Newberry41aba102017-11-01 16:42:13 -0700162 */
163 signal::Signal<LinkService, Interest> onDroppedInterest;
164
Junxiao Shi57df2882015-11-11 06:12:35 -0700165public: // lower interface to be invoked by Transport
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400166 /** \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 Pesaventob3a23ca2019-05-04 20:40:21 -0400169 receivePacket(const Block& packet, const EndpointId& endpoint);
Eric Newberrya98bf932015-09-21 00:58:47 -0700170
171protected: // upper interface to be invoked in subclass (receive path termination)
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400172 /** \brief Delivers received Interest to forwarding.
Eric Newberrya98bf932015-09-21 00:58:47 -0700173 */
174 void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400175 receiveInterest(const Interest& interest, const EndpointId& endpoint);
Eric Newberrya98bf932015-09-21 00:58:47 -0700176
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400177 /** \brief Delivers received Data to forwarding.
Eric Newberrya98bf932015-09-21 00:58:47 -0700178 */
179 void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400180 receiveData(const Data& data, const EndpointId& endpoint);
Eric Newberrya98bf932015-09-21 00:58:47 -0700181
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400182 /** \brief Delivers received Nack to forwarding.
Eric Newberrya98bf932015-09-21 00:58:47 -0700183 */
184 void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400185 receiveNack(const lp::Nack& nack, const EndpointId& endpoint);
Eric Newberrya98bf932015-09-21 00:58:47 -0700186
Eric Newberrya98bf932015-09-21 00:58:47 -0700187protected: // lower interface to be invoked in subclass (send path termination)
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400188 /** \brief Send a lower-layer packet via Transport.
Eric Newberrya98bf932015-09-21 00:58:47 -0700189 */
190 void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700191 sendPacket(const Block& packet);
Eric Newberrya98bf932015-09-21 00:58:47 -0700192
Eric Newberry41aba102017-11-01 16:42:13 -0700193protected:
194 void
195 notifyDroppedInterest(const Interest& packet);
196
Junxiao Shi57df2882015-11-11 06:12:35 -0700197private: // upper interface to be overridden in subclass (send path entrypoint)
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400198 /** \brief Performs LinkService specific operations to send an Interest.
Junxiao Shi57df2882015-11-11 06:12:35 -0700199 */
200 virtual void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700201 doSendInterest(const Interest& interest) = 0;
Junxiao Shi57df2882015-11-11 06:12:35 -0700202
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400203 /** \brief Performs LinkService specific operations to send a Data.
Junxiao Shi57df2882015-11-11 06:12:35 -0700204 */
205 virtual void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700206 doSendData(const Data& data) = 0;
Junxiao Shi57df2882015-11-11 06:12:35 -0700207
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400208 /** \brief Performs LinkService specific operations to send a Nack.
Junxiao Shi57df2882015-11-11 06:12:35 -0700209 */
210 virtual void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700211 doSendNack(const lp::Nack& nack) = 0;
Junxiao Shi57df2882015-11-11 06:12:35 -0700212
Eric Newberrya98bf932015-09-21 00:58:47 -0700213private: // lower interface to be overridden in subclass
214 virtual void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400215 doReceivePacket(const Block& packet, const EndpointId& endpoint) = 0;
Eric Newberrya98bf932015-09-21 00:58:47 -0700216
217private:
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400218 Face* m_face = nullptr;
219 Transport* m_transport = nullptr;
Eric Newberrya98bf932015-09-21 00:58:47 -0700220};
221
Eric Newberrycb6551e2020-03-02 14:12:16 -0800222inline ssize_t
223LinkService::getEffectiveMtu() const
224{
225 return m_transport->getMtu();
226}
227
Eric Newberrya98bf932015-09-21 00:58:47 -0700228inline void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400229LinkService::receivePacket(const Block& packet, const EndpointId& endpoint)
Eric Newberrya98bf932015-09-21 00:58:47 -0700230{
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400231 doReceivePacket(packet, endpoint);
Eric Newberrya98bf932015-09-21 00:58:47 -0700232}
233
234inline void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700235LinkService::sendPacket(const Block& packet)
Eric Newberrya98bf932015-09-21 00:58:47 -0700236{
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700237 m_transport->send(packet);
Eric Newberrya98bf932015-09-21 00:58:47 -0700238}
239
240std::ostream&
241operator<<(std::ostream& os, const FaceLogHelper<LinkService>& flh);
242
243template<typename T>
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400244std::enable_if_t<std::is_base_of_v<LinkService, T> && !std::is_same_v<LinkService, T>,
245 std::ostream&>
Eric Newberrya98bf932015-09-21 00:58:47 -0700246operator<<(std::ostream& os, const FaceLogHelper<T>& flh)
247{
248 return os << FaceLogHelper<LinkService>(flh.obj);
249}
250
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400251} // namespace nfd::face
Eric Newberrya98bf932015-09-21 00:58:47 -0700252
253#endif // NFD_DAEMON_FACE_LINK_SERVICE_HPP