blob: a922cbe726bb5b356cc24091b8f2dd382d2ba95f [file] [log] [blame]
Eric Newberrya98bf932015-09-21 00:58:47 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shicbc8e942016-09-06 03:17:45 +00003 * Copyright (c) 2014-2016, 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#include "generic-link-service.hpp"
Junxiao Shicbc8e942016-09-06 03:17:45 +000027#include <ndn-cxx/lp/tags.hpp>
Eric Newberrya98bf932015-09-21 00:58:47 -070028
29namespace nfd {
30namespace face {
31
32NFD_LOG_INIT("GenericLinkService");
33
Eric Newberry4c3e6b82015-11-10 16:48:42 -070034GenericLinkServiceCounters::GenericLinkServiceCounters(const LpReassembler& reassembler)
35 : nReassembling(reassembler)
36{
37}
38
Eric Newberry86d31872015-09-23 16:24:59 -070039GenericLinkService::Options::Options()
40 : allowLocalFields(false)
Eric Newberry4c3e6b82015-11-10 16:48:42 -070041 , allowFragmentation(false)
42 , allowReassembly(false)
Eric Newberry86d31872015-09-23 16:24:59 -070043{
44}
45
46GenericLinkService::GenericLinkService(const GenericLinkService::Options& options)
Eric Newberry4c3e6b82015-11-10 16:48:42 -070047 : GenericLinkServiceCounters(m_reassembler)
48 , m_options(options)
49 , m_fragmenter(m_options.fragmenterOptions, this)
50 , m_reassembler(m_options.reassemblerOptions, this)
51 , m_lastSeqNo(-2)
Eric Newberry86d31872015-09-23 16:24:59 -070052{
Junxiao Shi0de23a22015-12-03 20:07:02 +000053 m_reassembler.beforeTimeout.connect(bind([this] { ++this->nReassemblyTimeouts; }));
Eric Newberry86d31872015-09-23 16:24:59 -070054}
55
Eric Newberrya98bf932015-09-21 00:58:47 -070056void
57GenericLinkService::doSendInterest(const Interest& interest)
58{
59 lp::Packet lpPacket(interest.wireEncode());
Junxiao Shi0de23a22015-12-03 20:07:02 +000060
Eric Newberry86d31872015-09-23 16:24:59 -070061 if (m_options.allowLocalFields) {
62 encodeLocalFields(interest, lpPacket);
63 }
Eric Newberry4c3e6b82015-11-10 16:48:42 -070064
Junxiao Shi0de23a22015-12-03 20:07:02 +000065 this->sendNetPacket(std::move(lpPacket));
Eric Newberrya98bf932015-09-21 00:58:47 -070066}
67
68void
69GenericLinkService::doSendData(const Data& data)
70{
71 lp::Packet lpPacket(data.wireEncode());
Junxiao Shi0de23a22015-12-03 20:07:02 +000072
Eric Newberry86d31872015-09-23 16:24:59 -070073 if (m_options.allowLocalFields) {
74 encodeLocalFields(data, lpPacket);
75 }
Eric Newberry4c3e6b82015-11-10 16:48:42 -070076
Junxiao Shi0de23a22015-12-03 20:07:02 +000077 this->sendNetPacket(std::move(lpPacket));
Eric Newberrya98bf932015-09-21 00:58:47 -070078}
79
80void
81GenericLinkService::doSendNack(const lp::Nack& nack)
82{
83 lp::Packet lpPacket(nack.getInterest().wireEncode());
84 lpPacket.add<lp::NackField>(nack.getHeader());
Junxiao Shi0de23a22015-12-03 20:07:02 +000085
Eric Newberry86d31872015-09-23 16:24:59 -070086 if (m_options.allowLocalFields) {
Junxiao Shi0de23a22015-12-03 20:07:02 +000087 encodeLocalFields(nack, lpPacket);
Eric Newberry86d31872015-09-23 16:24:59 -070088 }
Eric Newberry4c3e6b82015-11-10 16:48:42 -070089
Junxiao Shi0de23a22015-12-03 20:07:02 +000090 this->sendNetPacket(std::move(lpPacket));
Eric Newberrya98bf932015-09-21 00:58:47 -070091}
92
Junxiao Shi0de23a22015-12-03 20:07:02 +000093void
94GenericLinkService::encodeLocalFields(const ndn::TagHost& netPkt, lp::Packet& lpPacket)
Eric Newberry86d31872015-09-23 16:24:59 -070095{
Junxiao Shi0de23a22015-12-03 20:07:02 +000096 shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = netPkt.getTag<lp::IncomingFaceIdTag>();
97 if (incomingFaceIdTag != nullptr) {
98 lpPacket.add<lp::IncomingFaceIdField>(*incomingFaceIdTag);
Eric Newberry86d31872015-09-23 16:24:59 -070099 }
Eric Newberry86d31872015-09-23 16:24:59 -0700100}
101
Eric Newberrya98bf932015-09-21 00:58:47 -0700102void
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700103GenericLinkService::sendNetPacket(lp::Packet&& pkt)
104{
105 std::vector<lp::Packet> frags;
106 const ssize_t mtu = this->getTransport()->getMtu();
107 if (m_options.allowFragmentation && mtu != MTU_UNLIMITED) {
108 bool isOk = false;
109 std::tie(isOk, frags) = m_fragmenter.fragmentPacket(pkt, mtu);
110 if (!isOk) {
111 // fragmentation failed (warning is logged by LpFragmenter)
Junxiao Shi0de23a22015-12-03 20:07:02 +0000112 ++this->nFragmentationErrors;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700113 return;
114 }
115 }
116 else {
117 frags.push_back(pkt);
118 }
119
120 if (frags.size() > 1) {
121 // sequence is needed only if packet is fragmented
Junxiao Shi0de23a22015-12-03 20:07:02 +0000122 this->assignSequences(frags);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700123 }
124 else {
125 // even if indexed fragmentation is enabled, the fragmenter should not
126 // fragment the packet if it can fit in MTU
127 BOOST_ASSERT(frags.size() > 0);
128 BOOST_ASSERT(!frags.front().has<lp::FragIndexField>());
129 BOOST_ASSERT(!frags.front().has<lp::FragCountField>());
130 }
131
132 for (const lp::Packet& frag : frags) {
133 Transport::Packet tp(frag.wireEncode());
134 if (mtu != MTU_UNLIMITED && tp.packet.size() > static_cast<size_t>(mtu)) {
Junxiao Shi0de23a22015-12-03 20:07:02 +0000135 ++this->nOutOverMtu;
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700136 NFD_LOG_FACE_WARN("attempt to send packet over MTU limit");
137 continue;
138 }
Junxiao Shi0de23a22015-12-03 20:07:02 +0000139 this->sendPacket(std::move(tp));
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700140 }
141}
142
143void
144GenericLinkService::assignSequence(lp::Packet& pkt)
145{
146 pkt.set<lp::SequenceField>(++m_lastSeqNo);
147}
148
149void
150GenericLinkService::assignSequences(std::vector<lp::Packet>& pkts)
151{
152 std::for_each(pkts.begin(), pkts.end(), bind(&GenericLinkService::assignSequence, this, _1));
153}
154
155void
Eric Newberrya98bf932015-09-21 00:58:47 -0700156GenericLinkService::doReceivePacket(Transport::Packet&& packet)
157{
Eric Newberry86d31872015-09-23 16:24:59 -0700158 try {
Eric Newberrya1939ba2015-10-09 12:35:03 -0700159 lp::Packet pkt(packet.packet);
160
161 if (!pkt.has<lp::FragmentField>()) {
162 NFD_LOG_FACE_TRACE("received IDLE packet: DROP");
163 return;
164 }
165
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700166 if ((pkt.has<lp::FragIndexField>() || pkt.has<lp::FragCountField>()) &&
167 !m_options.allowReassembly) {
168 NFD_LOG_FACE_WARN("received fragment, but reassembly disabled: DROP");
Eric Newberrya1939ba2015-10-09 12:35:03 -0700169 return;
170 }
171
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700172 bool isReassembled = false;
173 Block netPkt;
174 lp::Packet firstPkt;
175 std::tie(isReassembled, netPkt, firstPkt) = m_reassembler.receiveFragment(packet.remoteEndpoint,
176 pkt);
177 if (isReassembled) {
178 this->decodeNetPacket(netPkt, firstPkt);
179 }
180 }
181 catch (const tlv::Error& e) {
182 ++this->nInLpInvalid;
183 NFD_LOG_FACE_WARN("packet parse error (" << e.what() << "): DROP");
184 }
185}
Eric Newberry86d31872015-09-23 16:24:59 -0700186
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700187void
188GenericLinkService::decodeNetPacket(const Block& netPkt, const lp::Packet& firstPkt)
189{
190 try {
Eric Newberry86d31872015-09-23 16:24:59 -0700191 switch (netPkt.type()) {
192 case tlv::Interest:
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700193 if (firstPkt.has<lp::NackField>()) {
194 this->decodeNack(netPkt, firstPkt);
Eric Newberry86d31872015-09-23 16:24:59 -0700195 }
196 else {
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700197 this->decodeInterest(netPkt, firstPkt);
Eric Newberry86d31872015-09-23 16:24:59 -0700198 }
199 break;
200 case tlv::Data:
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700201 this->decodeData(netPkt, firstPkt);
Eric Newberry86d31872015-09-23 16:24:59 -0700202 break;
203 default:
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700204 ++this->nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700205 NFD_LOG_FACE_WARN("unrecognized network-layer packet TLV-TYPE " << netPkt.type() << ": DROP");
206 return;
Eric Newberrya98bf932015-09-21 00:58:47 -0700207 }
208 }
Eric Newberry86d31872015-09-23 16:24:59 -0700209 catch (const tlv::Error& e) {
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700210 ++this->nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700211 NFD_LOG_FACE_WARN("packet parse error (" << e.what() << "): DROP");
212 }
213}
214
Eric Newberry86d31872015-09-23 16:24:59 -0700215void
216GenericLinkService::decodeInterest(const Block& netPkt, const lp::Packet& firstPkt)
217{
218 BOOST_ASSERT(netPkt.type() == tlv::Interest);
219 BOOST_ASSERT(!firstPkt.has<lp::NackField>());
220
221 // forwarding expects Interest to be created with make_shared
222 auto interest = make_shared<Interest>(netPkt);
223
224 if (firstPkt.has<lp::NextHopFaceIdField>()) {
225 if (m_options.allowLocalFields) {
Junxiao Shi0de23a22015-12-03 20:07:02 +0000226 interest->setTag(make_shared<lp::NextHopFaceIdTag>(firstPkt.get<lp::NextHopFaceIdField>()));
Eric Newberry86d31872015-09-23 16:24:59 -0700227 }
228 else {
229 NFD_LOG_FACE_WARN("received NextHopFaceId, but local fields disabled: DROP");
230 return;
231 }
232 }
233
234 if (firstPkt.has<lp::CachePolicyField>()) {
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700235 ++this->nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700236 NFD_LOG_FACE_WARN("received CachePolicy with Interest: DROP");
237 return;
238 }
239
240 if (firstPkt.has<lp::IncomingFaceIdField>()) {
241 NFD_LOG_FACE_WARN("received IncomingFaceId: IGNORE");
242 }
243
244 this->receiveInterest(*interest);
245}
246
247void
248GenericLinkService::decodeData(const Block& netPkt, const lp::Packet& firstPkt)
249{
250 BOOST_ASSERT(netPkt.type() == tlv::Data);
251
252 // forwarding expects Data to be created with make_shared
253 auto data = make_shared<Data>(netPkt);
254
255 if (firstPkt.has<lp::NackField>()) {
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700256 ++this->nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700257 NFD_LOG_FACE_WARN("received Nack with Data: DROP");
258 return;
259 }
260
261 if (firstPkt.has<lp::NextHopFaceIdField>()) {
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700262 ++this->nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700263 NFD_LOG_FACE_WARN("received NextHopFaceId with Data: DROP");
264 return;
265 }
266
267 if (firstPkt.has<lp::CachePolicyField>()) {
268 if (m_options.allowLocalFields) {
Junxiao Shi0de23a22015-12-03 20:07:02 +0000269 // In case of an invalid CachePolicyType, get<lp::CachePolicyField> will throw,
270 // so it's unnecessary to check here.
271 data->setTag(make_shared<lp::CachePolicyTag>(firstPkt.get<lp::CachePolicyField>()));
Eric Newberry86d31872015-09-23 16:24:59 -0700272 }
273 else {
274 NFD_LOG_FACE_WARN("received CachePolicy, but local fields disabled: IGNORE");
275 }
276 }
277
278 if (firstPkt.has<lp::IncomingFaceIdField>()) {
279 NFD_LOG_FACE_WARN("received IncomingFaceId: IGNORE");
280 }
281
282 this->receiveData(*data);
283}
284
285void
286GenericLinkService::decodeNack(const Block& netPkt, const lp::Packet& firstPkt)
287{
288 BOOST_ASSERT(netPkt.type() == tlv::Interest);
289 BOOST_ASSERT(firstPkt.has<lp::NackField>());
290
291 lp::Nack nack((Interest(netPkt)));
292 nack.setHeader(firstPkt.get<lp::NackField>());
293
294 if (firstPkt.has<lp::NextHopFaceIdField>()) {
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700295 ++this->nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700296 NFD_LOG_FACE_WARN("received NextHopFaceId with Nack: DROP");
297 return;
298 }
299
300 if (firstPkt.has<lp::CachePolicyField>()) {
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700301 ++this->nInNetInvalid;
Eric Newberry86d31872015-09-23 16:24:59 -0700302 NFD_LOG_FACE_WARN("received CachePolicy with Nack: DROP");
303 return;
304 }
305
306 if (firstPkt.has<lp::IncomingFaceIdField>()) {
307 NFD_LOG_FACE_WARN("received IncomingFaceId: IGNORE");
308 }
309
310 this->receiveNack(nack);
Eric Newberrya98bf932015-09-21 00:58:47 -0700311}
312
313} // namespace face
314} // namespace nfd