blob: bc149d2894d79bb402a586b75ccc9e424c7eae60 [file] [log] [blame]
Davide Pesavento43ff2a92017-05-18 19:50:57 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi0d82d042017-07-07 06:15:27 +00002/*
Davide Pesavento43ff2a92017-05-18 19:50:57 -04003 * Copyright (c) 2014-2017, Regents of the University of California,
4 * 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 "ethernet-channel.hpp"
27#include "ethernet-protocol.hpp"
28#include "generic-link-service.hpp"
29#include "unicast-ethernet-transport.hpp"
30#include "core/global-io.hpp"
31
32#include <boost/range/adaptor/map.hpp>
33#include <pcap/pcap.h>
34
35namespace nfd {
36namespace face {
37
38NFD_LOG_INIT("EthernetChannel");
39
Junxiao Shi0d82d042017-07-07 06:15:27 +000040EthernetChannel::EthernetChannel(shared_ptr<const ndn::net::NetworkInterface> localEndpoint,
Davide Pesavento43ff2a92017-05-18 19:50:57 -040041 time::nanoseconds idleTimeout)
Junxiao Shi0d82d042017-07-07 06:15:27 +000042 : m_localEndpoint(std::move(localEndpoint))
Davide Pesavento43ff2a92017-05-18 19:50:57 -040043 , m_isListening(false)
44 , m_socket(getGlobalIoService())
Junxiao Shi0d82d042017-07-07 06:15:27 +000045 , m_pcap(m_localEndpoint->getName())
Davide Pesavento43ff2a92017-05-18 19:50:57 -040046 , m_idleFaceTimeout(idleTimeout)
47#ifdef _DEBUG
48 , m_nDropped(0)
49#endif
50{
Junxiao Shi0d82d042017-07-07 06:15:27 +000051 setUri(FaceUri::fromDev(m_localEndpoint->getName()));
Davide Pesavento43ff2a92017-05-18 19:50:57 -040052 NFD_LOG_CHAN_INFO("Creating channel");
53}
54
55void
56EthernetChannel::connect(const ethernet::Address& remoteEndpoint,
57 ndn::nfd::FacePersistency persistency,
Eric Newberry2642cd22017-07-13 21:34:53 -040058 bool wantLpReliability,
Davide Pesavento43ff2a92017-05-18 19:50:57 -040059 const FaceCreatedCallback& onFaceCreated,
60 const FaceCreationFailedCallback& onConnectFailed)
61{
62 shared_ptr<Face> face;
63 try {
Eric Newberry2642cd22017-07-13 21:34:53 -040064 face = createFace(remoteEndpoint, persistency, wantLpReliability).second;
Davide Pesavento43ff2a92017-05-18 19:50:57 -040065 }
66 catch (const boost::system::system_error& e) {
67 NFD_LOG_CHAN_DEBUG("Face creation for " << remoteEndpoint << " failed: " << e.what());
68 if (onConnectFailed)
69 onConnectFailed(504, std::string("Face creation failed: ") + e.what());
70 return;
71 }
72
73 // Need to invoke the callback regardless of whether or not we had already
74 // created the face so that control responses and such can be sent
75 onFaceCreated(face);
76}
77
78void
79EthernetChannel::listen(const FaceCreatedCallback& onFaceCreated,
80 const FaceCreationFailedCallback& onFaceCreationFailed)
81{
82 if (isListening()) {
83 NFD_LOG_CHAN_WARN("Already listening");
84 return;
85 }
86 m_isListening = true;
87
88 try {
89 m_pcap.activate(DLT_EN10MB);
90 m_socket.assign(m_pcap.getFd());
91 }
92 catch (const PcapHelper::Error& e) {
93 BOOST_THROW_EXCEPTION(Error(e.what()));
94 }
95 updateFilter();
96
97 asyncRead(onFaceCreated, onFaceCreationFailed);
98 NFD_LOG_CHAN_DEBUG("Started listening");
99}
100
101void
102EthernetChannel::asyncRead(const FaceCreatedCallback& onFaceCreated,
103 const FaceCreationFailedCallback& onReceiveFailed)
104{
105 m_socket.async_read_some(boost::asio::null_buffers(),
106 bind(&EthernetChannel::handleRead, this,
107 boost::asio::placeholders::error,
108 onFaceCreated, onReceiveFailed));
109}
110
111void
112EthernetChannel::handleRead(const boost::system::error_code& error,
113 const FaceCreatedCallback& onFaceCreated,
114 const FaceCreationFailedCallback& onReceiveFailed)
115{
116 if (error) {
117 if (error != boost::asio::error::operation_aborted) {
118 NFD_LOG_CHAN_DEBUG("Receive failed: " << error.message());
119 if (onReceiveFailed)
120 onReceiveFailed(500, "Receive failed: " + error.message());
121 }
122 return;
123 }
124
125 const uint8_t* pkt;
126 size_t len;
127 std::string err;
128 std::tie(pkt, len, err) = m_pcap.readNextPacket();
129
130 if (pkt == nullptr) {
131 NFD_LOG_CHAN_WARN("Read error: " << err);
132 }
133 else {
134 const ether_header* eh;
Junxiao Shi0d82d042017-07-07 06:15:27 +0000135 std::tie(eh, err) = ethernet::checkFrameHeader(pkt, len, m_localEndpoint->getEthernetAddress(),
136 m_localEndpoint->getEthernetAddress());
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400137 if (eh == nullptr) {
138 NFD_LOG_CHAN_DEBUG(err);
139 }
140 else {
141 ethernet::Address sender(eh->ether_shost);
142 pkt += ethernet::HDR_LEN;
143 len -= ethernet::HDR_LEN;
144 processIncomingPacket(pkt, len, sender, onFaceCreated, onReceiveFailed);
145 }
146 }
147
148#ifdef _DEBUG
149 size_t nDropped = m_pcap.getNDropped();
150 if (nDropped - m_nDropped > 0)
151 NFD_LOG_CHAN_DEBUG("Detected " << nDropped - m_nDropped << " dropped frame(s)");
152 m_nDropped = nDropped;
153#endif
154
155 asyncRead(onFaceCreated, onReceiveFailed);
156}
157
158void
159EthernetChannel::processIncomingPacket(const uint8_t* packet, size_t length,
160 const ethernet::Address& sender,
161 const FaceCreatedCallback& onFaceCreated,
162 const FaceCreationFailedCallback& onReceiveFailed)
163{
164 NFD_LOG_CHAN_TRACE("New peer " << sender);
165
166 bool isCreated = false;
167 shared_ptr<Face> face;
168 try {
Eric Newberry2642cd22017-07-13 21:34:53 -0400169 std::tie(isCreated, face) = createFace(sender, ndn::nfd::FACE_PERSISTENCY_ON_DEMAND, false);
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400170 }
171 catch (const EthernetTransport::Error& e) {
172 NFD_LOG_CHAN_DEBUG("Face creation for " << sender << " failed: " << e.what());
173 if (onReceiveFailed)
174 onReceiveFailed(504, std::string("Face creation failed: ") + e.what());
175 return;
176 }
177
178 if (isCreated)
179 onFaceCreated(face);
180 else
181 NFD_LOG_CHAN_DEBUG("Received frame for existing face");
182
183 // dispatch the packet to the face for processing
184 auto* transport = static_cast<UnicastEthernetTransport*>(face->getTransport());
185 transport->receivePayload(packet, length, sender);
186}
187
188std::pair<bool, shared_ptr<Face>>
189EthernetChannel::createFace(const ethernet::Address& remoteEndpoint,
Eric Newberry2642cd22017-07-13 21:34:53 -0400190 ndn::nfd::FacePersistency persistency,
191 bool wantLpReliability)
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400192{
193 auto it = m_channelFaces.find(remoteEndpoint);
194 if (it != m_channelFaces.end()) {
195 // we already have a face for this endpoint, so reuse it
196 NFD_LOG_CHAN_TRACE("Reusing existing face for " << remoteEndpoint);
197 return {false, it->second};
198 }
199
200 // else, create a new face
Eric Newberry2642cd22017-07-13 21:34:53 -0400201 GenericLinkService::Options options;
202 options.reliabilityOptions.isEnabled = wantLpReliability;
203 auto linkService = make_unique<GenericLinkService>(options);
Junxiao Shi0d82d042017-07-07 06:15:27 +0000204 auto transport = make_unique<UnicastEthernetTransport>(*m_localEndpoint, remoteEndpoint,
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400205 persistency, m_idleFaceTimeout);
206 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
207
208 m_channelFaces[remoteEndpoint] = face;
209 connectFaceClosedSignal(*face, [this, remoteEndpoint] {
210 m_channelFaces.erase(remoteEndpoint);
211 updateFilter();
212 });
213 updateFilter();
214
215 return {true, face};
216}
217
218void
219EthernetChannel::updateFilter()
220{
221 if (!isListening())
222 return;
223
224 std::string filter = "(ether proto " + to_string(ethernet::ETHERTYPE_NDN) +
Junxiao Shi0d82d042017-07-07 06:15:27 +0000225 ") && (ether dst " + m_localEndpoint->getEthernetAddress().toString() + ")";
Davide Pesavento43ff2a92017-05-18 19:50:57 -0400226 for (const auto& addr : m_channelFaces | boost::adaptors::map_keys) {
227 filter += " && (not ether src " + addr.toString() + ")";
228 }
229 // "not vlan" must appear last in the filter expression, or the
230 // rest of the filter won't work as intended, see pcap-filter(7)
231 filter += " && (not vlan)";
232
233 NFD_LOG_CHAN_TRACE("Updating filter: " << filter);
234 m_pcap.setPacketFilter(filter.data());
235}
236
237} // namespace face
238} // namespace nfd