src/devices/l2-routing/l2-routing-main/l2-routing-net-device.cc
changeset 4793 4f6a6772628e
child 4809 c7c4e4f260a9
equal deleted inserted replaced
4244:7c98934dcccd 4793:4f6a6772628e
       
     1 /* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
       
     2 /*
       
     3  * Copyright (c) 2008,2009 IITP RAS
       
     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: Kirill Andreev <andreev@iitp.ru>
       
    19  */
       
    20 
       
    21 
       
    22 #include "ns3/node.h"
       
    23 #include "ns3/channel.h"
       
    24 #include "ns3/packet.h"
       
    25 #include "ns3/log.h"
       
    26 #include "ns3/boolean.h"
       
    27 #include "ns3/simulator.h"
       
    28 #include "ns3/l2-routing-net-device.h"
       
    29 
       
    30 NS_LOG_COMPONENT_DEFINE ("L2RoutingNetDevice");
       
    31 
       
    32 namespace ns3 {
       
    33 
       
    34 NS_OBJECT_ENSURE_REGISTERED (L2RoutingNetDevice);
       
    35 
       
    36 
       
    37 TypeId
       
    38 L2RoutingNetDevice::GetTypeId (void)
       
    39 {
       
    40 	static TypeId tid = TypeId ("ns3::L2RoutingNetDevice")
       
    41 		.SetParent<NetDevice> ()
       
    42 		.AddConstructor<L2RoutingNetDevice> ()
       
    43 		;
       
    44 	return tid;
       
    45 }
       
    46 
       
    47 
       
    48 L2RoutingNetDevice::L2RoutingNetDevice ()
       
    49 {
       
    50 	NS_LOG_FUNCTION_NOARGS ();
       
    51 	m_channel = CreateObject<BridgeChannel> ();
       
    52 }
       
    53 
       
    54 L2RoutingNetDevice::~L2RoutingNetDevice()
       
    55 {
       
    56 	NS_LOG_FUNCTION_NOARGS ();
       
    57 }
       
    58 
       
    59 	void
       
    60 L2RoutingNetDevice::DoDispose ()
       
    61 {
       
    62 	NS_LOG_FUNCTION_NOARGS ();
       
    63 	for (std::vector< Ptr<NetDevice> >::iterator iter = m_ports.begin (); iter != m_ports.end (); iter++)
       
    64 		*iter = 0;
       
    65 	m_ports.clear ();
       
    66 	m_node = 0;
       
    67 	NetDevice::DoDispose ();
       
    68 
       
    69 }
       
    70 
       
    71 	void
       
    72 L2RoutingNetDevice::ReceiveFromDevice (Ptr<NetDevice> incomingPort, Ptr<const Packet> packet, uint16_t protocol,
       
    73 		Address const &src, Address const &dst, PacketType packetType)
       
    74 {
       
    75 	NS_LOG_FUNCTION_NOARGS ();
       
    76 	NS_LOG_DEBUG ("UID is " << packet->GetUid ());
       
    77 	const Mac48Address src48 = Mac48Address::ConvertFrom (src);
       
    78 	const Mac48Address dst48 = Mac48Address::ConvertFrom (dst);
       
    79 	if (!m_promiscRxCallback.IsNull ())
       
    80 		m_promiscRxCallback (this, packet, protocol, src, dst, packetType);
       
    81 	switch (packetType)
       
    82 	{
       
    83 		case PACKET_HOST:
       
    84 			if (dst48 == m_address)
       
    85 				m_rxCallback (this, packet, protocol, src);
       
    86 			break;
       
    87 		case PACKET_BROADCAST:
       
    88 		case PACKET_MULTICAST:
       
    89 			m_rxCallback (this, packet, protocol, src);
       
    90 		case PACKET_OTHERHOST:
       
    91 			Forward (incomingPort, packet->Copy(), protocol, src48, dst48);
       
    92 			break;
       
    93 	}
       
    94 
       
    95 }
       
    96 
       
    97 void
       
    98 L2RoutingNetDevice::Forward (Ptr<NetDevice> inport, Ptr<Packet> packet,
       
    99 		uint16_t protocol, const Mac48Address src, const Mac48Address dst)
       
   100 {
       
   101 	//pass through routing protocol
       
   102 	m_requestRoute(inport->GetIfIndex(), src, dst, packet, protocol, m_myResponse);
       
   103 }
       
   104 
       
   105 uint32_t
       
   106 L2RoutingNetDevice::GetNPorts (void) const
       
   107 {
       
   108 	NS_LOG_FUNCTION_NOARGS ();
       
   109 	return m_ports.size ();
       
   110 }
       
   111 
       
   112 Ptr<NetDevice>
       
   113 L2RoutingNetDevice::GetPort (uint32_t n) const
       
   114 {
       
   115 	NS_ASSERT(m_ports.size () > n);
       
   116 	return m_ports[n];
       
   117 }
       
   118 
       
   119 void
       
   120 L2RoutingNetDevice::AddPort (Ptr<NetDevice> routingPort)
       
   121 {
       
   122 	NS_LOG_FUNCTION_NOARGS ();
       
   123 	NS_ASSERT (routingPort != this);
       
   124 	if (!Mac48Address::IsMatchingType (routingPort->GetAddress ()))
       
   125 		NS_FATAL_ERROR ("Device does not support eui 48 addresses: cannot be added to bridge.");
       
   126 	if (!routingPort->SupportsSendFrom ())
       
   127 		NS_FATAL_ERROR ("Device does not support SendFrom: cannot be added to bridge.");
       
   128 	m_address = Mac48Address::ConvertFrom (routingPort->GetAddress ());
       
   129 	NS_LOG_DEBUG ("RegisterProtocolHandler for " << routingPort->GetName ());
       
   130 	m_node->RegisterProtocolHandler (MakeCallback (&L2RoutingNetDevice::ReceiveFromDevice, this),
       
   131 			0, routingPort, true);
       
   132 	m_ports.push_back (routingPort);
       
   133 	m_channel->AddChannel (routingPort->GetChannel ());
       
   134 }
       
   135 
       
   136 	void
       
   137 L2RoutingNetDevice::SetName(const std::string name)
       
   138 {
       
   139 	NS_LOG_FUNCTION_NOARGS ();
       
   140 	m_name = name;
       
   141 }
       
   142 
       
   143 std::string
       
   144 L2RoutingNetDevice::GetName(void) const
       
   145 {
       
   146 	NS_LOG_FUNCTION_NOARGS ();
       
   147 	return m_name;
       
   148 }
       
   149 
       
   150 	void
       
   151 L2RoutingNetDevice::SetIfIndex(const uint32_t index)
       
   152 {
       
   153 	NS_LOG_FUNCTION_NOARGS ();
       
   154 	m_ifIndex = index;
       
   155 }
       
   156 
       
   157 uint32_t
       
   158 L2RoutingNetDevice::GetIfIndex(void) const
       
   159 {
       
   160 	NS_LOG_FUNCTION_NOARGS ();
       
   161 	return m_ifIndex;
       
   162 }
       
   163 
       
   164 Ptr<Channel>
       
   165 L2RoutingNetDevice::GetChannel (void) const
       
   166 {
       
   167 	NS_LOG_FUNCTION_NOARGS ();
       
   168 	return m_channel;
       
   169 }
       
   170 
       
   171 Address
       
   172 L2RoutingNetDevice::GetAddress (void) const
       
   173 {
       
   174 	NS_LOG_FUNCTION_NOARGS ();
       
   175 	return m_address;
       
   176 }
       
   177 
       
   178 	bool
       
   179 L2RoutingNetDevice::SetMtu (const uint16_t mtu)
       
   180 {
       
   181 	NS_LOG_FUNCTION_NOARGS ();
       
   182 	m_mtu = mtu;
       
   183 	return true;
       
   184 }
       
   185 
       
   186 uint16_t
       
   187 L2RoutingNetDevice::GetMtu (void) const
       
   188 {
       
   189 	NS_LOG_FUNCTION_NOARGS ();
       
   190 	return 1500;
       
   191 }
       
   192 
       
   193 
       
   194 bool
       
   195 L2RoutingNetDevice::IsLinkUp (void) const
       
   196 {
       
   197 	NS_LOG_FUNCTION_NOARGS ();
       
   198 	return true;
       
   199 }
       
   200 
       
   201 
       
   202 	void
       
   203 L2RoutingNetDevice::SetLinkChangeCallback (Callback<void> callback)
       
   204 {}
       
   205 
       
   206 bool
       
   207 L2RoutingNetDevice::IsBroadcast (void) const
       
   208 {
       
   209 	NS_LOG_FUNCTION_NOARGS ();
       
   210 	return true;
       
   211 }
       
   212 
       
   213 
       
   214 Address
       
   215 L2RoutingNetDevice::GetBroadcast (void) const
       
   216 {
       
   217 	NS_LOG_FUNCTION_NOARGS ();
       
   218 	return Mac48Address ("ff:ff:ff:ff:ff:ff");
       
   219 }
       
   220 
       
   221 bool
       
   222 L2RoutingNetDevice::IsMulticast (void) const
       
   223 {
       
   224 	NS_LOG_FUNCTION_NOARGS ();
       
   225 	return true;
       
   226 }
       
   227 
       
   228 Address
       
   229 L2RoutingNetDevice::GetMulticast (Ipv4Address multicastGroup) const
       
   230 {
       
   231 	NS_LOG_FUNCTION (this << multicastGroup);
       
   232 	Mac48Address multicast = Mac48Address::GetMulticast (multicastGroup);
       
   233 	return multicast;
       
   234 }
       
   235 
       
   236 
       
   237 bool
       
   238 L2RoutingNetDevice::IsPointToPoint (void) const
       
   239 {
       
   240 	NS_LOG_FUNCTION_NOARGS ();
       
   241 	return false;
       
   242 }
       
   243 
       
   244 bool
       
   245 L2RoutingNetDevice::IsBridge (void) const
       
   246 {
       
   247 	NS_LOG_FUNCTION_NOARGS ();
       
   248 	return false;
       
   249 }
       
   250 
       
   251 
       
   252 bool
       
   253 L2RoutingNetDevice::Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
       
   254 {
       
   255 
       
   256 	const Mac48Address dst48 = Mac48Address::ConvertFrom (dest);
       
   257 	return m_requestRoute(m_ifIndex, m_address, dst48, packet, protocolNumber, m_myResponse);
       
   258 }
       
   259 
       
   260 bool
       
   261 L2RoutingNetDevice::SendFrom (Ptr<Packet> packet, const Address& src, const Address& dest, uint16_t protocolNumber)
       
   262 {
       
   263 	const Mac48Address src48 = Mac48Address::ConvertFrom (src);
       
   264 	const Mac48Address dst48 = Mac48Address::ConvertFrom (dest);
       
   265 	return m_requestRoute(m_ifIndex, src48, dst48, packet, protocolNumber, m_myResponse);
       
   266 }
       
   267 
       
   268 
       
   269 Ptr<Node>
       
   270 L2RoutingNetDevice::GetNode (void) const
       
   271 {
       
   272 	NS_LOG_FUNCTION_NOARGS ();
       
   273 	return m_node;
       
   274 }
       
   275 
       
   276 
       
   277 	void
       
   278 L2RoutingNetDevice::SetNode (Ptr<Node> node)
       
   279 {
       
   280 	NS_LOG_FUNCTION_NOARGS ();
       
   281 	m_node = node;
       
   282 }
       
   283 
       
   284 
       
   285 bool
       
   286 L2RoutingNetDevice::NeedsArp (void) const
       
   287 {
       
   288 	NS_LOG_FUNCTION_NOARGS ();
       
   289 	return true;
       
   290 }
       
   291 
       
   292 
       
   293 	void
       
   294 L2RoutingNetDevice::SetReceiveCallback (NetDevice::ReceiveCallback cb)
       
   295 {
       
   296 	NS_LOG_FUNCTION_NOARGS ();
       
   297 	m_rxCallback = cb;
       
   298 }
       
   299 
       
   300 	void
       
   301 L2RoutingNetDevice::SetPromiscReceiveCallback (NetDevice::PromiscReceiveCallback cb)
       
   302 {
       
   303 	NS_LOG_FUNCTION_NOARGS ();
       
   304 	m_promiscRxCallback = cb;
       
   305 }
       
   306 
       
   307 bool
       
   308 L2RoutingNetDevice::SupportsSendFrom () const
       
   309 {
       
   310 	NS_LOG_FUNCTION_NOARGS ();
       
   311 	return true;
       
   312 }
       
   313 
       
   314 Address
       
   315 L2RoutingNetDevice::GetMulticast (Ipv6Address addr) const
       
   316 {
       
   317 	NS_LOG_FUNCTION (this << addr);
       
   318 	return Mac48Address::GetMulticast (addr);
       
   319 }
       
   320 //L2RouitingSpecific methods:
       
   321 
       
   322 bool
       
   323 L2RoutingNetDevice::AttachProtocol(Ptr<L2RoutingProtocol> protocol)
       
   324 {
       
   325 	m_requestRoute = MakeCallback(&L2RoutingProtocol::RequestRoute, protocol);
       
   326 	m_myResponse = MakeCallback(&L2RoutingNetDevice::ProtocolResponse, this);
       
   327 	protocol->SetIfIndex(m_ifIndex);
       
   328 	return protocol->AttachPorts(m_ports);
       
   329 }
       
   330 
       
   331 void
       
   332 L2RoutingNetDevice::ProtocolResponse(
       
   333 		bool success,
       
   334 		Ptr<Packet> packet,
       
   335 		Mac48Address src,
       
   336 		Mac48Address dst,
       
   337 		uint16_t protocol,
       
   338 		uint32_t outPort
       
   339 		)
       
   340 {
       
   341 	if(!success)
       
   342 	{
       
   343 		NS_LOG_UNCOND("Resolve failed");
       
   344 		//TODO: SendError callback
       
   345 		return;
       
   346 	}
       
   347 	//just do sendFrom!
       
   348 	if(outPort!=0xffffffff)
       
   349 		GetPort(outPort)->SendFrom(packet, src, dst, protocol);
       
   350 	else
       
   351 		for(std::vector<Ptr<NetDevice> >::iterator i = m_ports.begin(); i!= m_ports.end(); i++)
       
   352 			(*i) -> SendFrom(packet, src, dst, protocol);
       
   353 }
       
   354 
       
   355 } // namespace ns3