Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Ilya Moiseenko | 956d054 | 2012-01-02 15:26:40 -0800 | [diff] [blame] | 3 | * Copyright (c) 2011 University of California, Los Angeles |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 4 | * |
| 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 Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 22 | #include "ndn-net-device-face.hpp" |
| 23 | #include "ndn-l3-protocol.hpp" |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 24 | |
Alexander Afanasyev | 82d5ffe | 2014-12-30 23:55:38 -0800 | [diff] [blame] | 25 | #include "ndn-ns3.hpp" |
| 26 | |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 27 | #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 Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 33 | // #include "ns3/address.h" |
Alexander Afanasyev | 7f3e49e | 2012-04-30 00:17:07 -0700 | [diff] [blame] | 34 | #include "ns3/point-to-point-net-device.h" |
| 35 | #include "ns3/channel.h" |
| 36 | |
Alexander Afanasyev | 82d5ffe | 2014-12-30 23:55:38 -0800 | [diff] [blame] | 37 | #include "../utils/ndn-fw-hop-count-tag.hpp" |
| 38 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 39 | NS_LOG_COMPONENT_DEFINE("ndn.NetDeviceFace"); |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 40 | |
| 41 | namespace ns3 { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 42 | namespace ndn { |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 43 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 44 | NetDeviceFace::NetDeviceFace(Ptr<Node> node, const Ptr<NetDevice>& netDevice) |
Alexander Afanasyev | 82d5ffe | 2014-12-30 23:55:38 -0800 | [diff] [blame] | 45 | : Face(FaceUri("netDeviceFace://"), FaceUri("netDeviceFace://")) |
| 46 | , m_node(node) |
| 47 | , m_netDevice(netDevice) |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 48 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 49 | NS_LOG_FUNCTION(this << netDevice); |
Alexander Afanasyev | 0ab833e | 2011-08-18 15:49:13 -0700 | [diff] [blame] | 50 | |
Alexander Afanasyev | 82d5ffe | 2014-12-30 23:55:38 -0800 | [diff] [blame] | 51 | setMetric(1); // default metric |
Alexander Afanasyev | a5abcd9 | 2012-04-17 13:34:43 -0700 | [diff] [blame] | 52 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 53 | NS_ASSERT_MSG(m_netDevice != 0, "NetDeviceFace needs to be assigned a valid NetDevice"); |
Alexander Afanasyev | 82d5ffe | 2014-12-30 23:55:38 -0800 | [diff] [blame] | 54 | |
| 55 | m_node->RegisterProtocolHandler(MakeCallback(&NetDeviceFace::receiveFromNetDevice, this), |
| 56 | L3Protocol::ETHERNET_FRAME_TYPE, m_netDevice, |
| 57 | true /*promiscuous mode*/); |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 60 | NetDeviceFace::~NetDeviceFace() |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 61 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 62 | NS_LOG_FUNCTION_NOARGS(); |
Alexander Afanasyev | 82d5ffe | 2014-12-30 23:55:38 -0800 | [diff] [blame] | 63 | |
| 64 | m_node->UnregisterProtocolHandler(MakeCallback(&NetDeviceFace::receiveFromNetDevice, this)); |
| 65 | } |
| 66 | |
| 67 | void |
| 68 | NetDeviceFace::close() |
| 69 | { |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 72 | Ptr<NetDevice> |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 73 | NetDeviceFace::GetNetDevice() const |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 74 | { |
| 75 | return m_netDevice; |
| 76 | } |
| 77 | |
Alexander Afanasyev | 82d5ffe | 2014-12-30 23:55:38 -0800 | [diff] [blame] | 78 | void |
| 79 | NetDeviceFace::send(Ptr<Packet> packet) |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 80 | { |
Alexander Afanasyev | 82d5ffe | 2014-12-30 23:55:38 -0800 | [diff] [blame] | 81 | NS_ASSERT_MSG(packet->GetSize() <= m_netDevice->GetMtu(), |
| 82 | "Packet size " << packet->GetSize() << " exceeds device MTU " |
| 83 | << m_netDevice->GetMtu()); |
| 84 | |
| 85 | FwHopCountTag tag; |
| 86 | packet->RemovePacketTag(tag); |
| 87 | tag.Increment(); |
| 88 | packet->AddPacketTag(tag); |
| 89 | |
| 90 | m_netDevice->Send(packet, m_netDevice->GetBroadcast(), L3Protocol::ETHERNET_FRAME_TYPE); |
| 91 | } |
| 92 | |
| 93 | void |
| 94 | NetDeviceFace::sendInterest(const Interest& interest) |
| 95 | { |
| 96 | NS_LOG_FUNCTION(this << &interest); |
| 97 | |
| 98 | this->onSendInterest(interest); |
| 99 | |
| 100 | Ptr<Packet> packet = Convert::ToPacket(interest); |
| 101 | send(packet); |
| 102 | } |
| 103 | |
| 104 | void |
| 105 | NetDeviceFace::sendData(const Data& data) |
| 106 | { |
| 107 | NS_LOG_FUNCTION(this << &data); |
| 108 | |
| 109 | this->onSendData(data); |
| 110 | |
| 111 | Ptr<Packet> packet = Convert::ToPacket(data); |
| 112 | send(packet); |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | // callback |
| 116 | void |
Alexander Afanasyev | 82d5ffe | 2014-12-30 23:55:38 -0800 | [diff] [blame] | 117 | NetDeviceFace::receiveFromNetDevice(Ptr<NetDevice> device, Ptr<const Packet> p, uint16_t protocol, |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 118 | const Address& from, const Address& to, |
| 119 | NetDevice::PacketType packetType) |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 120 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 121 | NS_LOG_FUNCTION(device << p << protocol << from << to << packetType); |
Alexander Afanasyev | 82d5ffe | 2014-12-30 23:55:38 -0800 | [diff] [blame] | 122 | |
| 123 | Ptr<Packet> packet = p->Copy(); |
| 124 | try { |
| 125 | uint32_t type = Convert::getPacketType(p); |
| 126 | if (type == ::ndn::tlv::Interest) { |
| 127 | shared_ptr<const Interest> i = Convert::FromPacket<Interest>(packet); |
| 128 | this->onReceiveInterest(*i); |
| 129 | } |
| 130 | else if (type == ::ndn::tlv::Data) { |
| 131 | shared_ptr<const Data> d = Convert::FromPacket<Data>(packet); |
| 132 | this->onReceiveData(*d); |
| 133 | } |
| 134 | else { |
| 135 | NS_LOG_ERROR("Unsupported TLV packet"); |
| 136 | } |
| 137 | } |
| 138 | catch (::ndn::tlv::Error&) { |
| 139 | NS_LOG_ERROR("Unrecognized TLV packet"); |
| 140 | } |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Alexander Afanasyev | 82d5ffe | 2014-12-30 23:55:38 -0800 | [diff] [blame] | 143 | } // namespace ndn |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 144 | } // namespace ns3 |