blob: 09a03a0cec1fddfb6545b38459c09e01b37bba21 [file] [log] [blame]
Xuxiang Tian91018732016-08-17 16:24:47 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2011-2015 Regents of the University of California.
4 *
5 * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
6 * contributors.
7 *
8 * ndnSIM is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 **/
19
20#include "ndn-net-device-link-service.hpp"
21#include "ndn-l3-protocol.hpp"
22
23#include "ndn-ns3.hpp"
24
25#include "ns3/net-device.h"
26#include "ns3/log.h"
27#include "ns3/packet.h"
28#include "ns3/node.h"
29#include "ns3/pointer.h"
30
31// #include "ns3/address.h"
32#include "ns3/point-to-point-net-device.h"
33#include "ns3/channel.h"
34
35#include "../utils/ndn-fw-hop-count-tag.hpp"
36
37NS_LOG_COMPONENT_DEFINE("ndn.NetDeviceLinkService");
38
39namespace ns3 {
40namespace ndn {
41
Xuxiang Tian91018732016-08-17 16:24:47 -070042NetDeviceLinkService::NetDeviceLinkService(Ptr<Node> node, const Ptr<NetDevice>& netDevice)
43 : m_node(node)
44 , m_netDevice(netDevice)
45{
46 NS_LOG_FUNCTION(this << netDevice);
47
48 NS_ASSERT_MSG(m_netDevice != 0, "NetDeviceFace needs to be assigned a valid NetDevice");
49
50 m_node->RegisterProtocolHandler(MakeCallback(&NetDeviceLinkService::receiveFromNetDevice, this),
51 L3Protocol::ETHERNET_FRAME_TYPE, m_netDevice,
52 true /*promiscuous mode*/);
53}
54
55NetDeviceLinkService::~NetDeviceLinkService()
56{
57 NS_LOG_FUNCTION_NOARGS();
58}
59
60Ptr<Node>
61NetDeviceLinkService::GetNode() const
62{
63 return m_node;
64}
65
66Ptr<NetDevice>
67NetDeviceLinkService::GetNetDevice() const
68{
69 return m_netDevice;
70}
71
72void
73NetDeviceLinkService::doSendInterest(const Interest& interest)
74{
75 NS_LOG_FUNCTION(this << &interest);
76
77 Ptr<Packet> packet = Convert::ToPacket(interest);
78 send(packet);
79}
80
81void
82NetDeviceLinkService::doSendData(const Data& data)
83{
84 NS_LOG_FUNCTION(this << &data);
85
86 Ptr<Packet> packet = Convert::ToPacket(data);
87 send(packet);
88}
89
90void
91NetDeviceLinkService::doSendNack(const lp::Nack& nack)
92{
93 NS_LOG_FUNCTION(this << &nack);
94
95 // TODO
96 // Ptr<Packet> packet = Convert::ToPacket(nack);
97 // send(packet);
98}
99
100// callback
101void
102NetDeviceLinkService::receiveFromNetDevice(Ptr<NetDevice> device, Ptr<const Packet> p, uint16_t protocol,
103 const Address& from, const Address& to,
104 NetDevice::PacketType packetType)
105{
106 NS_LOG_FUNCTION(device << p << protocol << from << to << packetType);
107
108 Ptr<Packet> packet = p->Copy();
109 try {
110 switch (Convert::getPacketType(p)) {
111 case ::ndn::tlv::Interest: {
112 shared_ptr<const Interest> i = Convert::FromPacket<Interest>(packet);
113 this->receiveInterest(*i);
114 break;
115 }
116 case ::ndn::tlv::Data: {
117 shared_ptr<const Data> d = Convert::FromPacket<Data>(packet);
118 this->receiveData(*d);
119 break;
120 }
121 // case ::ndn::tlv::Nack: {
122 // shared_ptr<const Nack> n = Convert::FromPacket<Nack>(packet);
123 // this->onReceiveNack(*n);
124 // }
125 default:
126 NS_LOG_ERROR("Unsupported TLV packet");
127 }
128 }
129 catch (const ::ndn::tlv::Error& e) {
130 NS_LOG_ERROR("Unrecognized TLV packet " << e.what());
131 }
132}
133
134void
135NetDeviceLinkService::send(Ptr<Packet> packet)
136{
137 NS_ASSERT_MSG(packet->GetSize() <= m_netDevice->GetMtu(),
138 "Packet size " << packet->GetSize() << " exceeds device MTU "
139 << m_netDevice->GetMtu());
140
141 FwHopCountTag tag;
142 packet->RemovePacketTag(tag);
143 tag.Increment();
144 packet->AddPacketTag(tag);
145
146 m_netDevice->Send(packet, m_netDevice->GetBroadcast(), L3Protocol::ETHERNET_FRAME_TYPE);
147}
148
149} // namespace face
150} // namespace nfd