blob: ecb262a6ff28dae523edb4f565c32cc4c38553cf [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
42// TODO: remove as soon as L3Protocol is back
43const uint16_t L3Protocol::ETHERNET_FRAME_TYPE = 0x7777;
44const uint16_t L3Protocol::IP_STACK_PORT = 9695;
45
46NetDeviceLinkService::NetDeviceLinkService(Ptr<Node> node, const Ptr<NetDevice>& netDevice)
47 : m_node(node)
48 , m_netDevice(netDevice)
49{
50 NS_LOG_FUNCTION(this << netDevice);
51
52 NS_ASSERT_MSG(m_netDevice != 0, "NetDeviceFace needs to be assigned a valid NetDevice");
53
54 m_node->RegisterProtocolHandler(MakeCallback(&NetDeviceLinkService::receiveFromNetDevice, this),
55 L3Protocol::ETHERNET_FRAME_TYPE, m_netDevice,
56 true /*promiscuous mode*/);
57}
58
59NetDeviceLinkService::~NetDeviceLinkService()
60{
61 NS_LOG_FUNCTION_NOARGS();
62}
63
64Ptr<Node>
65NetDeviceLinkService::GetNode() const
66{
67 return m_node;
68}
69
70Ptr<NetDevice>
71NetDeviceLinkService::GetNetDevice() const
72{
73 return m_netDevice;
74}
75
76void
77NetDeviceLinkService::doSendInterest(const Interest& interest)
78{
79 NS_LOG_FUNCTION(this << &interest);
80
81 Ptr<Packet> packet = Convert::ToPacket(interest);
82 send(packet);
83}
84
85void
86NetDeviceLinkService::doSendData(const Data& data)
87{
88 NS_LOG_FUNCTION(this << &data);
89
90 Ptr<Packet> packet = Convert::ToPacket(data);
91 send(packet);
92}
93
94void
95NetDeviceLinkService::doSendNack(const lp::Nack& nack)
96{
97 NS_LOG_FUNCTION(this << &nack);
98
99 // TODO
100 // Ptr<Packet> packet = Convert::ToPacket(nack);
101 // send(packet);
102}
103
104// callback
105void
106NetDeviceLinkService::receiveFromNetDevice(Ptr<NetDevice> device, Ptr<const Packet> p, uint16_t protocol,
107 const Address& from, const Address& to,
108 NetDevice::PacketType packetType)
109{
110 NS_LOG_FUNCTION(device << p << protocol << from << to << packetType);
111
112 Ptr<Packet> packet = p->Copy();
113 try {
114 switch (Convert::getPacketType(p)) {
115 case ::ndn::tlv::Interest: {
116 shared_ptr<const Interest> i = Convert::FromPacket<Interest>(packet);
117 this->receiveInterest(*i);
118 break;
119 }
120 case ::ndn::tlv::Data: {
121 shared_ptr<const Data> d = Convert::FromPacket<Data>(packet);
122 this->receiveData(*d);
123 break;
124 }
125 // case ::ndn::tlv::Nack: {
126 // shared_ptr<const Nack> n = Convert::FromPacket<Nack>(packet);
127 // this->onReceiveNack(*n);
128 // }
129 default:
130 NS_LOG_ERROR("Unsupported TLV packet");
131 }
132 }
133 catch (const ::ndn::tlv::Error& e) {
134 NS_LOG_ERROR("Unrecognized TLV packet " << e.what());
135 }
136}
137
138void
139NetDeviceLinkService::send(Ptr<Packet> packet)
140{
141 NS_ASSERT_MSG(packet->GetSize() <= m_netDevice->GetMtu(),
142 "Packet size " << packet->GetSize() << " exceeds device MTU "
143 << m_netDevice->GetMtu());
144
145 FwHopCountTag tag;
146 packet->RemovePacketTag(tag);
147 tag.Increment();
148 packet->AddPacketTag(tag);
149
150 m_netDevice->Send(packet, m_netDevice->GetBroadcast(), L3Protocol::ETHERNET_FRAME_TYPE);
151}
152
153} // namespace face
154} // namespace nfd