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(); |
Mickey Sweatt | 89046c1 | 2014-11-16 20:32:27 -0800 | [diff] [blame^] | 63 | close(); |
Alexander Afanasyev | 82d5ffe | 2014-12-30 23:55:38 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void |
| 67 | NetDeviceFace::close() |
| 68 | { |
Mickey Sweatt | 89046c1 | 2014-11-16 20:32:27 -0800 | [diff] [blame^] | 69 | m_node->UnregisterProtocolHandler(MakeCallback(&NetDeviceFace::receiveFromNetDevice, this)); |
| 70 | this->fail("Close connection"); |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 73 | Ptr<NetDevice> |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 74 | NetDeviceFace::GetNetDevice() const |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 75 | { |
| 76 | return m_netDevice; |
| 77 | } |
| 78 | |
Alexander Afanasyev | 82d5ffe | 2014-12-30 23:55:38 -0800 | [diff] [blame] | 79 | void |
| 80 | NetDeviceFace::send(Ptr<Packet> packet) |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 81 | { |
Alexander Afanasyev | 82d5ffe | 2014-12-30 23:55:38 -0800 | [diff] [blame] | 82 | 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 | |
| 94 | void |
| 95 | NetDeviceFace::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 | |
| 105 | void |
| 106 | NetDeviceFace::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 Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | // callback |
| 117 | void |
Alexander Afanasyev | 82d5ffe | 2014-12-30 23:55:38 -0800 | [diff] [blame] | 118 | NetDeviceFace::receiveFromNetDevice(Ptr<NetDevice> device, Ptr<const Packet> p, uint16_t protocol, |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 119 | const Address& from, const Address& to, |
| 120 | NetDevice::PacketType packetType) |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 121 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 122 | NS_LOG_FUNCTION(device << p << protocol << from << to << packetType); |
Alexander Afanasyev | 82d5ffe | 2014-12-30 23:55:38 -0800 | [diff] [blame] | 123 | |
| 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 Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Alexander Afanasyev | 82d5ffe | 2014-12-30 23:55:38 -0800 | [diff] [blame] | 144 | } // namespace ndn |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 145 | } // namespace ns3 |