blob: 19266b5ca3eb7b823e87677815c6d12ec07ca2fb [file] [log] [blame]
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
Ilya Moiseenko956d0542012-01-02 15:26:40 -08003 * Copyright (c) 2011 University of California, Los Angeles
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -07004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 *
20 */
21
Alexander Afanasyev0c395372014-12-20 15:54:02 -080022#include "ndn-net-device-face.hpp"
23#include "ndn-l3-protocol.hpp"
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070024
Alexander Afanasyev82d5ffe2014-12-30 23:55:38 -080025#include "ndn-ns3.hpp"
26
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070027#include "ns3/net-device.h"
28#include "ns3/log.h"
29#include "ns3/packet.h"
30#include "ns3/node.h"
31#include "ns3/pointer.h"
32
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070033// #include "ns3/address.h"
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -070034#include "ns3/point-to-point-net-device.h"
35#include "ns3/channel.h"
36
Alexander Afanasyev82d5ffe2014-12-30 23:55:38 -080037#include "../utils/ndn-fw-hop-count-tag.hpp"
38
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080039NS_LOG_COMPONENT_DEFINE("ndn.NetDeviceFace");
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070040
41namespace ns3 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070042namespace ndn {
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070043
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080044NetDeviceFace::NetDeviceFace(Ptr<Node> node, const Ptr<NetDevice>& netDevice)
Alexander Afanasyev82d5ffe2014-12-30 23:55:38 -080045 : Face(FaceUri("netDeviceFace://"), FaceUri("netDeviceFace://"))
46 , m_node(node)
47 , m_netDevice(netDevice)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070048{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080049 NS_LOG_FUNCTION(this << netDevice);
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -070050
Alexander Afanasyev82d5ffe2014-12-30 23:55:38 -080051 setMetric(1); // default metric
Alexander Afanasyeva5abcd92012-04-17 13:34:43 -070052
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080053 NS_ASSERT_MSG(m_netDevice != 0, "NetDeviceFace needs to be assigned a valid NetDevice");
Alexander Afanasyev82d5ffe2014-12-30 23:55:38 -080054
55 m_node->RegisterProtocolHandler(MakeCallback(&NetDeviceFace::receiveFromNetDevice, this),
56 L3Protocol::ETHERNET_FRAME_TYPE, m_netDevice,
57 true /*promiscuous mode*/);
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070058}
59
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080060NetDeviceFace::~NetDeviceFace()
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070061{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080062 NS_LOG_FUNCTION_NOARGS();
Mickey Sweatt89046c12014-11-16 20:32:27 -080063 close();
Alexander Afanasyev82d5ffe2014-12-30 23:55:38 -080064}
65
66void
67NetDeviceFace::close()
68{
Mickey Sweatt89046c12014-11-16 20:32:27 -080069 m_node->UnregisterProtocolHandler(MakeCallback(&NetDeviceFace::receiveFromNetDevice, this));
70 this->fail("Close connection");
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070071}
72
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070073Ptr<NetDevice>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080074NetDeviceFace::GetNetDevice() const
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070075{
76 return m_netDevice;
77}
78
Alexander Afanasyev82d5ffe2014-12-30 23:55:38 -080079void
80NetDeviceFace::send(Ptr<Packet> packet)
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -070081{
Alexander Afanasyev82d5ffe2014-12-30 23:55:38 -080082 NS_ASSERT_MSG(packet->GetSize() <= m_netDevice->GetMtu(),
83 "Packet size " << packet->GetSize() << " exceeds device MTU "
84 << m_netDevice->GetMtu());
85
86 FwHopCountTag tag;
87 packet->RemovePacketTag(tag);
88 tag.Increment();
89 packet->AddPacketTag(tag);
90
91 m_netDevice->Send(packet, m_netDevice->GetBroadcast(), L3Protocol::ETHERNET_FRAME_TYPE);
92}
93
94void
95NetDeviceFace::sendInterest(const Interest& interest)
96{
97 NS_LOG_FUNCTION(this << &interest);
98
99 this->onSendInterest(interest);
100
101 Ptr<Packet> packet = Convert::ToPacket(interest);
102 send(packet);
103}
104
105void
106NetDeviceFace::sendData(const Data& data)
107{
108 NS_LOG_FUNCTION(this << &data);
109
110 this->onSendData(data);
111
112 Ptr<Packet> packet = Convert::ToPacket(data);
113 send(packet);
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700114}
115
116// callback
117void
Alexander Afanasyev82d5ffe2014-12-30 23:55:38 -0800118NetDeviceFace::receiveFromNetDevice(Ptr<NetDevice> device, Ptr<const Packet> p, uint16_t protocol,
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800119 const Address& from, const Address& to,
120 NetDevice::PacketType packetType)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700121{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800122 NS_LOG_FUNCTION(device << p << protocol << from << to << packetType);
Alexander Afanasyev82d5ffe2014-12-30 23:55:38 -0800123
124 Ptr<Packet> packet = p->Copy();
125 try {
126 uint32_t type = Convert::getPacketType(p);
127 if (type == ::ndn::tlv::Interest) {
128 shared_ptr<const Interest> i = Convert::FromPacket<Interest>(packet);
129 this->onReceiveInterest(*i);
130 }
131 else if (type == ::ndn::tlv::Data) {
132 shared_ptr<const Data> d = Convert::FromPacket<Data>(packet);
133 this->onReceiveData(*d);
134 }
135 else {
136 NS_LOG_ERROR("Unsupported TLV packet");
137 }
138 }
139 catch (::ndn::tlv::Error&) {
140 NS_LOG_ERROR("Unrecognized TLV packet");
141 }
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700142}
143
Alexander Afanasyev82d5ffe2014-12-30 23:55:38 -0800144} // namespace ndn
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700145} // namespace ns3