blob: 20647fb433d1c9829b009ef5769735b6265ecc1a [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/*
Eric Newberrycb6551e2020-03-02 14:12:16 -08003 * Copyright (c) 2014-2020, 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
33namespace nfd {
34namespace face {
35
Junxiao Shi57df2882015-11-11 06:12:35 -070036/** \brief counters provided by LinkService
37 * \note The type name 'LinkServiceCounters' is implementation detail.
38 * Use 'LinkService::Counters' in public API.
39 */
40class LinkServiceCounters
41{
42public:
43 /** \brief count of incoming Interests
44 */
45 PacketCounter nInInterests;
46
47 /** \brief count of outgoing Interests
48 */
49 PacketCounter nOutInterests;
50
Eric Newberry41aba102017-11-01 16:42:13 -070051 /** \brief count of Interests dropped by reliability system for exceeding allowed number of retx
52 */
Eric Newberry9d283ad2020-04-12 23:37:17 -070053 PacketCounter nInterestsExceededRetx;
Eric Newberry41aba102017-11-01 16:42:13 -070054
55 /** \brief count of incoming Data packets
Junxiao Shi57df2882015-11-11 06:12:35 -070056 */
57 PacketCounter nInData;
58
Eric Newberry41aba102017-11-01 16:42:13 -070059 /** \brief count of outgoing Data packets
Junxiao Shi57df2882015-11-11 06:12:35 -070060 */
61 PacketCounter nOutData;
62
63 /** \brief count of incoming Nacks
64 */
65 PacketCounter nInNacks;
66
67 /** \brief count of outgoing Nacks
68 */
69 PacketCounter nOutNacks;
70};
71
Junxiao Shicde37ad2015-12-24 01:02:05 -070072/** \brief the upper part of a Face
73 * \sa Face
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:
Junxiao Shi57df2882015-11-11 06:12:35 -070078 /** \brief counters provided by LinkService
79 */
80 typedef LinkServiceCounters Counters;
81
82public:
Eric Newberrya98bf932015-09-21 00:58:47 -070083 LinkService();
84
85 virtual
86 ~LinkService();
87
88 /** \brief set Face and Transport for LinkService
89 * \pre setFaceAndTransport has not been called
90 */
91 void
Junxiao Shicde37ad2015-12-24 01:02:05 -070092 setFaceAndTransport(Face& face, Transport& transport);
Eric Newberrya98bf932015-09-21 00:58:47 -070093
94 /** \return Face to which this LinkService is attached
95 */
Junxiao Shicde37ad2015-12-24 01:02:05 -070096 const Face*
Eric Newberrya98bf932015-09-21 00:58:47 -070097 getFace() const;
98
99 /** \return Transport to which this LinkService is attached
100 */
101 const Transport*
102 getTransport() const;
103
104 /** \return Transport to which this LinkService is attached
105 */
106 Transport*
107 getTransport();
108
Junxiao Shi57df2882015-11-11 06:12:35 -0700109 virtual const Counters&
110 getCounters() const;
111
Eric Newberrycb6551e2020-03-02 14:12:16 -0800112 virtual ssize_t
113 getEffectiveMtu() const;
114
Eric Newberrya98bf932015-09-21 00:58:47 -0700115public: // upper interface to be used by forwarding
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700116 /** \brief Send Interest
Eric Newberrya98bf932015-09-21 00:58:47 -0700117 * \pre setTransport has been called
118 */
119 void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700120 sendInterest(const Interest& interest);
Eric Newberrya98bf932015-09-21 00:58:47 -0700121
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700122 /** \brief Send Data
Eric Newberrya98bf932015-09-21 00:58:47 -0700123 * \pre setTransport has been called
124 */
125 void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700126 sendData(const Data& data);
Eric Newberrya98bf932015-09-21 00:58:47 -0700127
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700128 /** \brief Send Nack
Eric Newberrya98bf932015-09-21 00:58:47 -0700129 * \pre setTransport has been called
130 */
131 void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700132 sendNack(const ndn::lp::Nack& nack);
Eric Newberrya98bf932015-09-21 00:58:47 -0700133
134 /** \brief signals on Interest received
135 */
ashiqopu075bb7d2019-03-10 01:38:21 +0000136 signal::Signal<LinkService, Interest, EndpointId> afterReceiveInterest;
Eric Newberrya98bf932015-09-21 00:58:47 -0700137
138 /** \brief signals on Data received
139 */
ashiqopu075bb7d2019-03-10 01:38:21 +0000140 signal::Signal<LinkService, Data, EndpointId> afterReceiveData;
Eric Newberrya98bf932015-09-21 00:58:47 -0700141
142 /** \brief signals on Nack received
143 */
ashiqopu075bb7d2019-03-10 01:38:21 +0000144 signal::Signal<LinkService, lp::Nack, EndpointId> afterReceiveNack;
Eric Newberrya98bf932015-09-21 00:58:47 -0700145
Eric Newberry41aba102017-11-01 16:42:13 -0700146 /** \brief signals on Interest dropped by reliability system for exceeding allowed number of retx
147 */
148 signal::Signal<LinkService, Interest> onDroppedInterest;
149
Spyridon Mastorakis97209372016-09-20 19:49:21 -0700150 /** \brief signals on Interest sent
151 */
152 signal::Signal<LinkService, Interest> afterSendInterest;
153
154 /** \brief signals on Data sent
155 */
156 signal::Signal<LinkService, Data> afterSendData;
157
158 /** \brief signals on Nack sent
159 */
160 signal::Signal<LinkService, lp::Nack> afterSendNack;
161
Junxiao Shi57df2882015-11-11 06:12:35 -0700162public: // lower interface to be invoked by Transport
163 /** \brief performs LinkService specific operations to receive a lower-layer packet
Eric Newberrya98bf932015-09-21 00:58:47 -0700164 */
Junxiao Shi57df2882015-11-11 06:12:35 -0700165 void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400166 receivePacket(const Block& packet, const EndpointId& endpoint);
Eric Newberrya98bf932015-09-21 00:58:47 -0700167
168protected: // upper interface to be invoked in subclass (receive path termination)
169 /** \brief delivers received Interest to forwarding
170 */
171 void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400172 receiveInterest(const Interest& interest, const EndpointId& endpoint);
Eric Newberrya98bf932015-09-21 00:58:47 -0700173
174 /** \brief delivers received Data to forwarding
175 */
176 void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400177 receiveData(const Data& data, const EndpointId& endpoint);
Eric Newberrya98bf932015-09-21 00:58:47 -0700178
179 /** \brief delivers received Nack to forwarding
180 */
181 void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400182 receiveNack(const lp::Nack& nack, const EndpointId& endpoint);
Eric Newberrya98bf932015-09-21 00:58:47 -0700183
Eric Newberrya98bf932015-09-21 00:58:47 -0700184protected: // lower interface to be invoked in subclass (send path termination)
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700185 /** \brief send a lower-layer packet via Transport
Eric Newberrya98bf932015-09-21 00:58:47 -0700186 */
187 void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700188 sendPacket(const Block& packet);
Eric Newberrya98bf932015-09-21 00:58:47 -0700189
Eric Newberry41aba102017-11-01 16:42:13 -0700190protected:
191 void
192 notifyDroppedInterest(const Interest& packet);
193
Junxiao Shi57df2882015-11-11 06:12:35 -0700194private: // upper interface to be overridden in subclass (send path entrypoint)
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700195 /** \brief performs LinkService specific operations to send an Interest
Junxiao Shi57df2882015-11-11 06:12:35 -0700196 */
197 virtual void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700198 doSendInterest(const Interest& interest) = 0;
Junxiao Shi57df2882015-11-11 06:12:35 -0700199
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700200 /** \brief performs LinkService specific operations to send a Data
Junxiao Shi57df2882015-11-11 06:12:35 -0700201 */
202 virtual void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700203 doSendData(const Data& data) = 0;
Junxiao Shi57df2882015-11-11 06:12:35 -0700204
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700205 /** \brief performs LinkService specific operations to send a Nack
Junxiao Shi57df2882015-11-11 06:12:35 -0700206 */
207 virtual void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700208 doSendNack(const lp::Nack& nack) = 0;
Junxiao Shi57df2882015-11-11 06:12:35 -0700209
Eric Newberrya98bf932015-09-21 00:58:47 -0700210private: // lower interface to be overridden in subclass
211 virtual void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400212 doReceivePacket(const Block& packet, const EndpointId& endpoint) = 0;
Eric Newberrya98bf932015-09-21 00:58:47 -0700213
214private:
Junxiao Shicde37ad2015-12-24 01:02:05 -0700215 Face* m_face;
Eric Newberrya98bf932015-09-21 00:58:47 -0700216 Transport* m_transport;
Eric Newberrya98bf932015-09-21 00:58:47 -0700217};
218
Junxiao Shicde37ad2015-12-24 01:02:05 -0700219inline const Face*
Eric Newberrya98bf932015-09-21 00:58:47 -0700220LinkService::getFace() const
221{
222 return m_face;
223}
224
225inline const Transport*
226LinkService::getTransport() const
227{
228 return m_transport;
229}
230
231inline Transport*
232LinkService::getTransport()
233{
234 return m_transport;
235}
236
Junxiao Shi57df2882015-11-11 06:12:35 -0700237inline const LinkService::Counters&
238LinkService::getCounters() const
239{
240 return *this;
241}
242
Eric Newberrycb6551e2020-03-02 14:12:16 -0800243inline ssize_t
244LinkService::getEffectiveMtu() const
245{
246 return m_transport->getMtu();
247}
248
Eric Newberrya98bf932015-09-21 00:58:47 -0700249inline void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400250LinkService::receivePacket(const Block& packet, const EndpointId& endpoint)
Eric Newberrya98bf932015-09-21 00:58:47 -0700251{
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400252 doReceivePacket(packet, endpoint);
Eric Newberrya98bf932015-09-21 00:58:47 -0700253}
254
255inline void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700256LinkService::sendPacket(const Block& packet)
Eric Newberrya98bf932015-09-21 00:58:47 -0700257{
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700258 m_transport->send(packet);
Eric Newberrya98bf932015-09-21 00:58:47 -0700259}
260
261std::ostream&
262operator<<(std::ostream& os, const FaceLogHelper<LinkService>& flh);
263
264template<typename T>
265typename std::enable_if<std::is_base_of<LinkService, T>::value &&
266 !std::is_same<LinkService, T>::value, std::ostream&>::type
267operator<<(std::ostream& os, const FaceLogHelper<T>& flh)
268{
269 return os << FaceLogHelper<LinkService>(flh.obj);
270}
271
272} // namespace face
273} // namespace nfd
274
275#endif // NFD_DAEMON_FACE_LINK_SERVICE_HPP