diff --git a/src/devices/emulated/emulated-net-device.cc b/src/devices/emulated/emulated-net-device.cc new file mode 100644 index 000000000..157308a7c --- /dev/null +++ b/src/devices/emulated/emulated-net-device.cc @@ -0,0 +1,642 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2008 University of Washington + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "emulated-net-device.h" + +#include "ns3/log.h" +#include "ns3/queue.h" +#include "ns3/simulator.h" +#include "ns3/realtime-simulator-impl.h" +#include "ns3/mac48-address.h" +#include "ns3/ethernet-header.h" +#include "ns3/ethernet-trailer.h" +#include "ns3/llc-snap-header.h" +#include "ns3/trace-source-accessor.h" +#include "ns3/pointer.h" +#include "ns3/channel.h" +#include "ns3/system-thread.h" +#include "ns3/string.h" +#include "ns3/boolean.h" + +#include +#include +#include +#include +#include +#include +#include + +NS_LOG_COMPONENT_DEFINE ("EmulatedNetDevice"); + +namespace ns3 { + +NS_OBJECT_ENSURE_REGISTERED (EmulatedNetDevice); + +TypeId +EmulatedNetDevice::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::EmulatedNetDevice") + .SetParent () + .AddConstructor () + .AddAttribute ("Address", + "The ns-3 MAC address of this (virtual) device.", + Mac48AddressValue (Mac48Address ("ff:ff:ff:ff:ff:ff")), + MakeMac48AddressAccessor (&EmulatedNetDevice::m_address), + MakeMac48AddressChecker ()) + .AddAttribute ("DeviceName", + "The name of the underlying real device (e.g. eth1).", + StringValue ("eth1"), + MakeStringAccessor (&EmulatedNetDevice::m_deviceName), + MakeStringChecker ()) + .AddAttribute ("Start", + "The simulation time at which to spin up the device thread.", + TimeValue (Seconds (0.)), + MakeTimeAccessor (&EmulatedNetDevice::m_tStart), + MakeTimeChecker ()) + .AddAttribute ("Stop", + "The simulation time at which to tear down the device thread.", + TimeValue (Seconds (0.)), + MakeTimeAccessor (&EmulatedNetDevice::m_tStop), + MakeTimeChecker ()) + .AddAttribute ("TxQueue", + "A queue to use as the transmit queue in the device.", + PointerValue (), + MakePointerAccessor (&EmulatedNetDevice::m_queue), + MakePointerChecker ()) + .AddTraceSource ("Rx", + "Trace source to fire on reception of a MAC packet.", + MakeTraceSourceAccessor (&EmulatedNetDevice::m_rxTrace)) + .AddTraceSource ("Drop", + "Trace source to fire on when a MAC packet is dropped.", + MakeTraceSourceAccessor (&EmulatedNetDevice::m_dropTrace)) + ; + return tid; +} + + +EmulatedNetDevice::EmulatedNetDevice () +: + m_startEvent (), + m_stopEvent (), + m_sock (-1), + m_readThread (0), + m_ifIndex (-1), + m_sll_ifindex (-1), + m_name ("Emulated NetDevice") +{ + NS_LOG_FUNCTION (this); + Start (m_tStart); +} + +EmulatedNetDevice::~EmulatedNetDevice () +{ +} + +void +EmulatedNetDevice::DoDispose() +{ + NS_LOG_FUNCTION_NOARGS (); + m_node = 0; + NetDevice::DoDispose (); +} + +void +EmulatedNetDevice::Start (Time tStart) +{ + NS_LOG_FUNCTION (tStart); + + // + // Cancel any pending start event and schedule a new one at some relative time in the future. + // + Simulator::Cancel (m_startEvent); + m_startEvent = Simulator::Schedule (tStart, &EmulatedNetDevice::StartDevice, this); +} + + void +EmulatedNetDevice::Stop (Time tStop) +{ + NS_LOG_FUNCTION (tStop); + // + // Cancel any pending stop event and schedule a new one at some relative time in the future. + // + Simulator::Cancel (m_stopEvent); + m_startEvent = Simulator::Schedule (tStop, &EmulatedNetDevice::StopDevice, this); +} + + void +EmulatedNetDevice::StartDevice (void) +{ + NS_LOG_FUNCTION_NOARGS (); + + // + // Spin up the emulated net device and start receiving packets. + // + NS_ASSERT_MSG (m_sock == -1, "EmulatedNetDevice::StartDevice(): Device is already started"); + + NS_LOG_LOGIC ("Creating socket"); + + m_sock = socket (PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); + // m_sock = socket (AF_INET, SOCK_PACKET, htons(ETH_P_ALL)); + NS_ASSERT_MSG (m_sock != -1, "EmulatedNetDevice::StartDevice(): Unable to open socket"); + + // + // Figure out which interface index corresponds to the device name in the corresponding attribute. + // + struct ifreq ifr; + bzero (&ifr, sizeof(ifr)); + strncpy ((char *)ifr.ifr_name, m_deviceName.c_str (), IFNAMSIZ); + + NS_LOG_LOGIC ("Getting interface index"); + int32_t rc = ioctl (m_sock, SIOCGIFINDEX, &ifr); + NS_ASSERT_MSG (rc != -1, "EmulatedNetDevice::StartDevice(): Can't get interface index"); + + // + // Save the real interface index for later calls to sendto + // + m_sll_ifindex = ifr.ifr_ifindex; + + // + // Bind the socket to the interface we just found. + // + struct sockaddr_ll ll; + bzero (&ll, sizeof(ll)); + + ll.sll_family = AF_PACKET; + ll.sll_ifindex = m_sll_ifindex; + ll.sll_protocol = htons(ETH_P_ALL); + + NS_LOG_LOGIC ("Binding socket to interface"); + + rc = bind (m_sock, (struct sockaddr *)&ll, sizeof (ll)); + NS_ASSERT_MSG (rc != -1, "EmulatedNetDevice::StartDevice(): Can't bind to specified interface"); + + // + // Now spin up a read thread to read packets. + // + NS_ASSERT_MSG (m_readThread == 0, "EmulatedNetDevice::StartDevice(): Receive thread is already running"); + + NS_LOG_LOGIC ("Spinning up read thread"); + + m_readThread = Create (MakeCallback (&EmulatedNetDevice::ReadThread, this)); + m_readThread->Start (); + + NotifyLinkUp (); +} + +void +EmulatedNetDevice::StopDevice (void) +{ + NS_LOG_FUNCTION_NOARGS (); + + close (m_sock); + m_sock = -1; + + NS_ASSERT_MSG (m_readThread != 0, "EmulatedNetDevice::StopDevice(): Receive thread is not running"); + + NS_LOG_LOGIC ("Joining read thread"); + m_readThread->Join (); + m_readThread = 0; +} + +void +EmulatedNetDevice::ForwardUp (uint8_t *buf, uint32_t len) +{ + NS_LOG_FUNCTION (buf << len); + + // + // Create a packet out of the buffer we received and free that buffer. + // + Ptr packet = Create (reinterpret_cast (buf), len); + free (buf); + buf = 0; + + // + // Trace sinks will expect complete packets, not packets without some of the + // headers. + // + Ptr originalPacket = packet->Copy (); + + // + // Checksum the packet + // + EthernetTrailer trailer; + packet->RemoveTrailer (trailer); + trailer.CheckFcs (packet); + + EthernetHeader header (false); + packet->RemoveHeader (header); + + NS_LOG_LOGIC ("Pkt source is " << header.GetSource ()); + NS_LOG_LOGIC ("Pkt destination is " << header.GetDestination ()); + + // + // An IP host group address is mapped to an Ethernet multicast address + // by placing the low-order 23-bits of the IP address into the low-order + // 23 bits of the Ethernet multicast address 01-00-5E-00-00-00 (hex). + // + // We are going to receive all packets destined to any multicast address, + // which means clearing the low-order 23 bits the header destination + // + Mac48Address mcDest; + uint8_t mcBuf[6]; + + header.GetDestination ().CopyTo (mcBuf); + mcBuf[3] &= 0x80; + mcBuf[4] = 0; + mcBuf[5] = 0; + mcDest.CopyFrom (mcBuf); + + Mac48Address multicast = Mac48Address::ConvertFrom (GetMulticast ()); + Mac48Address broadcast = Mac48Address::ConvertFrom (GetBroadcast ()); + Mac48Address destination = Mac48Address::ConvertFrom (GetAddress ()); + + LlcSnapHeader llc; + packet->RemoveHeader (llc); + uint16_t protocol = llc.GetType (); + + PacketType packetType; + + if (header.GetDestination () == broadcast) + { + NS_LOG_LOGIC ("Pkt destination is PACKET_BROADCAST"); + packetType = NS3_PACKET_BROADCAST; + } + else if (mcDest == multicast) + { + NS_LOG_LOGIC ("Pkt destination is PACKET_MULTICAST"); + packetType = NS3_PACKET_MULTICAST; + } + else if (header.GetDestination () == destination) + { + NS_LOG_LOGIC ("Pkt destination is PACKET_HOST"); + packetType = NS3_PACKET_HOST; + } + else + { + NS_LOG_LOGIC ("Pkt destination is PACKET_OTHERHOST"); + packetType = NS3_PACKET_OTHERHOST; + } + + if (!m_promiscRxCallback.IsNull ()) + { + NS_LOG_LOGIC ("calling m_promiscRxCallback"); + m_promiscRxCallback (this, packet->Copy (), protocol, header.GetSource (), header.GetDestination (), packetType); + } + + if (packetType != NS3_PACKET_OTHERHOST) + { + m_rxTrace (originalPacket); + NS_LOG_LOGIC ("calling m_rxCallback"); + m_rxCallback (this, packet, protocol, header.GetSource ()); + } +} + +void +EmulatedNetDevice::ReadThread (void) +{ + NS_LOG_FUNCTION_NOARGS (); + + // + // It's important to remember that we're in a completely different thread than the simulator is running in. We + // need to synchronize with that other thread to get the packet up into ns-3. What we will need to do is to schedule + // a method to forward up the packet using the multithreaded simulator we are most certainly running. However, I just + // said it -- we are talking about two threads here, so it is very, very dangerous to do any kind of reference couning + // on a shared object. Just don't do it. So what we're going to do is to allocate a buffer on the heap and pass that + // buffer into the ns-3 context thread where it will create the packet. + // + + int32_t len = -1; + struct sockaddr_ll addr; + socklen_t addrSize = sizeof (addr); + + for (;;) + { + uint32_t bufferSize = 65536; + uint8_t *buf = (uint8_t *)malloc (bufferSize); + NS_ASSERT_MSG (buf, "EmulatedNetDevice::ReadThread(): malloc packet buffer failed"); + NS_LOG_LOGIC ("Calling recvfrom"); + len = recvfrom (m_sock, buf, bufferSize, 0, (struct sockaddr *)&addr, &addrSize); + + if (len == -1) + { + free (buf); + buf = 0; + return; + } + + NS_LOG_INFO ("EmulatedNetDevice::ReadThread(): Received packet"); + NS_LOG_INFO ("EmulatedNetDevice::ReadThread(): Scheduling handler"); + DynamicCast (Simulator::GetImplementation ())->ScheduleRealtimeNow ( + MakeEvent (&EmulatedNetDevice::ForwardUp, this, buf, len)); + buf = 0; + } +} + +bool +EmulatedNetDevice::Send (Ptr packet, const Address &dest, uint16_t protocolNumber) +{ + NS_LOG_FUNCTION (packet << dest << protocolNumber); + // + // The immediate questions here are how are we going to encapsulate packets and what do we use as the MAC source and + // destination (hardware) addresses? + // + // If we return false from EmulatedNetDevice::NeedsArp, the ArpIpv4Interface will pass the broadcast address as the + // hardware (Ethernet) destination by default. If we return true from EmulatedNetDevice::NeedsArp, then the hardware + // destination is actually meaningful, but we'll have an ns-3 ARP running on this device. There can also be an ARP + // running on the underlying OS so we have to be very careful, both about multiple ARPs and also about TCP, UDP, etc. + // + // We are operating in promiscuous mode on the receive side (all ns-3 net devices are required to implement the + // promiscuous callback in a meaningful way), so we have an option regarding the hardware addresses. We don't actually have + // to use the real hardware addresses and IP addresses of the underlying system. We can completely use MAC-spoofing to + // fake out the OS by using the ns-3 assigned MAC address (and also the ns-3 assigned IP addresses). Ns-3 starts its + // MAC address allocation using the OUI (vendor-code) 00:00:00 which is unassigned to any organization and is a globally + // administered address, so there shouldn't be any collisions with real hardware. + // + // So what we do is we return true from EmulatedNetDevice::NeedsArp which tells ns-3 to use its own ARP. We spoof the + // MAC address of the device and use promiscuous mode to receive traffic destined to that address. + // + return SendFrom (packet, m_address, dest, protocolNumber); +} + +bool +EmulatedNetDevice::SendFrom (Ptr packet, const Address &src, const Address &dest, uint16_t protocolNumber) +{ + NS_LOG_FUNCTION (packet << src << dest << protocolNumber); + + if (IsLinkUp () == false) + { + NS_LOG_LOGIC ("Link is down, returning"); + return false; + } + + Mac48Address destination = Mac48Address::ConvertFrom (dest); + Mac48Address source = Mac48Address::ConvertFrom (src); + + NS_LOG_LOGIC ("Transmit packet with UID " << packet->GetUid ()); + NS_LOG_LOGIC ("Transmit packet from " << source); + NS_LOG_LOGIC ("Transmit packet to " << destination); + +#if 0 + { + struct ifreq ifr; + bzero (&ifr, sizeof(ifr)); + strncpy ((char *)ifr.ifr_name, m_deviceName.c_str (), IFNAMSIZ); + + NS_LOG_LOGIC ("Getting MAC address"); + int32_t rc = ioctl (m_sock, SIOCGIFHWADDR, &ifr); + NS_ASSERT_MSG (rc != -1, "EmulatedNetDevice::SendFrom(): Can't get MAC address"); + + std::ostringstream oss; + oss << std::hex << + (ifr.ifr_hwaddr.sa_data[0] & 0xff) << ":" << + (ifr.ifr_hwaddr.sa_data[1] & 0xff) << ":" << + (ifr.ifr_hwaddr.sa_data[2] & 0xff) << ":" << + (ifr.ifr_hwaddr.sa_data[3] & 0xff) << ":" << + (ifr.ifr_hwaddr.sa_data[4] & 0xff) << ":" << + (ifr.ifr_hwaddr.sa_data[5] & 0xff) << std::dec; + + NS_LOG_LOGIC ("Fixup source to HW MAC " << oss.str ()); + source = Mac48Address (oss.str ().c_str ()); + NS_LOG_LOGIC ("source now " << source); + } +#endif + + LlcSnapHeader llc; + llc.SetType (protocolNumber); + packet->AddHeader (llc); + + EthernetHeader header (false); + header.SetSource (source); + header.SetDestination (destination); + header.SetLengthType (packet->GetSize ()); + packet->AddHeader (header); + + EthernetTrailer trailer; + trailer.CalcFcs (packet); + packet->AddTrailer (trailer); + + // + // Enqueue and dequeue the packet to hit the tracing hooks. + // + m_queue->Enqueue (packet); + packet = m_queue->Dequeue (); + + struct sockaddr_ll ll; + bzero (&ll, sizeof (ll)); + + ll.sll_family = AF_PACKET; + ll.sll_ifindex = m_sll_ifindex; + ll.sll_protocol = htons(ETH_P_ALL); + + NS_LOG_LOGIC ("calling sendto"); + + int32_t rc; + rc = sendto (m_sock, packet->PeekData (), packet->GetSize (), 0, reinterpret_cast (&ll), sizeof (ll)); + + NS_LOG_LOGIC ("sendto returns " << rc); + + return rc == -1 ? false : true; +} + +void +EmulatedNetDevice::SetDataRate(DataRate bps) +{ + NS_LOG_FUNCTION (this << bps); + NS_ASSERT_MSG (false, "EmulatedNetDevice::SetDataRate(): Unable."); +} + +void +EmulatedNetDevice::SetQueue (Ptr q) +{ + NS_LOG_FUNCTION (this << q); + m_queue = q; +} + +Ptr +EmulatedNetDevice::GetQueue(void) const +{ + NS_LOG_FUNCTION_NOARGS (); + return m_queue; +} + +void +EmulatedNetDevice::NotifyLinkUp (void) +{ + m_linkUp = true; + if (!m_linkChangeCallback.IsNull ()) + { + m_linkChangeCallback (); + } +} + +void +EmulatedNetDevice::SetName(const std::string name) +{ + m_name = name; +} + +std::string +EmulatedNetDevice::GetName(void) const +{ + return m_name; +} + +void +EmulatedNetDevice::SetIfIndex(const uint32_t index) +{ + m_ifIndex = index; +} + +uint32_t +EmulatedNetDevice::GetIfIndex(void) const +{ + return m_ifIndex; +} + +Ptr +EmulatedNetDevice::GetChannel (void) const +{ + NS_ASSERT_MSG (false, "EmulatedNetDevice::GetChannel(): Unable."); + return 0; +} + +void +EmulatedNetDevice::SetAddress (Mac48Address addr) +{ + NS_LOG_FUNCTION (addr); + m_address = addr; +} + +Address +EmulatedNetDevice::GetAddress (void) const +{ + NS_LOG_FUNCTION_NOARGS (); + return m_address; +} + +bool +EmulatedNetDevice::SetMtu (const uint16_t mtu) +{ + NS_ASSERT_MSG (false, "EmulatedNetDevice::SetMtu(): Unable."); + return false; +} + +uint16_t +EmulatedNetDevice::GetMtu (void) const +{ + struct ifreq ifr; + bzero (&ifr, sizeof (ifr)); + strcpy(ifr.ifr_name, m_deviceName.c_str ()); + + int32_t fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); + + int32_t rc = ioctl(fd, SIOCGIFMTU, &ifr); + NS_ASSERT_MSG (rc != -1, "EmulatedNetDevice::GetMtu(): Can't ioctl SIOCGIFMTU"); + + close (fd); + + return ifr.ifr_mtu; +} + +bool +EmulatedNetDevice::IsLinkUp (void) const +{ + return m_linkUp; +} + +void +EmulatedNetDevice::SetLinkChangeCallback (Callback callback) +{ + m_linkChangeCallback = callback; +} + +bool +EmulatedNetDevice::IsBroadcast (void) const +{ + return true; +} + +Address +EmulatedNetDevice::GetBroadcast (void) const +{ + return Mac48Address ("ff:ff:ff:ff:ff:ff"); +} + +bool +EmulatedNetDevice::IsMulticast (void) const +{ + return false; +} + +Address +EmulatedNetDevice::GetMulticast (void) const +{ + return Mac48Address ("01:00:5e:00:00:00"); +} + +Address +EmulatedNetDevice::MakeMulticastAddress (Ipv4Address multicastGroup) const +{ + return Mac48Address ("01:00:5e:00:00:00"); +} + +bool +EmulatedNetDevice::IsPointToPoint (void) const +{ + return false; +} + +void +EmulatedNetDevice::SetPromiscReceiveCallback (PromiscReceiveCallback cb) +{ + NS_ASSERT_MSG (false, "EmulatedNetDevice::SetPromiscReceiveCallback(): Not implemented"); +} + + bool +EmulatedNetDevice::SupportsSendFrom () const +{ + NS_LOG_FUNCTION_NOARGS (); + return true; +} + + +Ptr +EmulatedNetDevice::GetNode (void) const +{ + return m_node; +} + +void +EmulatedNetDevice::SetNode (Ptr node) +{ + m_node = node; +} + +bool +EmulatedNetDevice::NeedsArp (void) const +{ + return true; +} + +void +EmulatedNetDevice::SetReceiveCallback (NetDevice::ReceiveCallback cb) +{ + m_rxCallback = cb; +} + +} // namespace ns3 diff --git a/src/devices/emulated/emulated-net-device.h b/src/devices/emulated/emulated-net-device.h new file mode 100644 index 000000000..ef5fb66b6 --- /dev/null +++ b/src/devices/emulated/emulated-net-device.h @@ -0,0 +1,321 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2008 University of Washington + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef EMULATED_NET_DEVICE_H +#define EMULATED_NET_DEVICE_H + +#include +#include "ns3/address.h" +#include "ns3/net-device.h" +#include "ns3/node.h" +#include "ns3/callback.h" +#include "ns3/packet.h" +#include "ns3/traced-callback.h" +#include "ns3/event-id.h" +#include "ns3/nstime.h" +#include "ns3/data-rate.h" +#include "ns3/ptr.h" +#include "ns3/mac48-address.h" +#include "ns3/system-thread.h" + +namespace ns3 { + +class Queue; + +/** + * \class EmulatedNetDevice + * \brief A Device for an Emulated Network Link. + */ +class EmulatedNetDevice : public NetDevice +{ +public: + static TypeId GetTypeId (void); + + /** + * Construct a EmulatedNetDevice + * + * This is the constructor for the EmulatedNetDevice. It takes as a + */ + EmulatedNetDevice (); + + /** + * Destroy a EmulatedNetDevice + * + * This is the destructor for the EmulatedNetDevice. + */ + virtual ~EmulatedNetDevice (); + + /** + * Set the Data Rate used for transmission of packets. + * + * @see Attach () + * @param bps the data rate at which this object operates + */ + void SetDataRate (DataRate bps); + + /** + * Set the inteframe gap used to separate packets. The interframe gap + * defines the minimum space required between packets sent by this device. + * + * @param t the interframe gap time + */ + void SetInterframeGap (Time t); + + /** + * Set a start time for the device. + * + * @param tStart the start time + */ + void Start (Time tStart); + + /** + * Set a stop time for the device. + * + * @param tStop the stop time + */ + void Stop (Time tStop); + + /** + * Attach a queue to the EmulatedNetDevice. + * + * The EmulatedNetDevice "owns" a queue that implements a queueing + * method such as DropTail or RED. + * + * @see Queue + * @see DropTailQueue + * @param queue Ptr to the new queue. + */ + void SetQueue (Ptr queue); + + /** + * Receive a packet. + * + * The EmulatedNetDevice receives packets from its socket reader + * and forwards them up the protocol stack. This is the public method + * used by the reader to indicate that a packet has arrived at the device. + * + * @param p Ptr to the received packet. + */ + void Receive (Ptr p); + + /** + * Assign a MAC address to this device. + * + * @see Mac48Address + * @param addr The new address. + */ + void SetAddress (Mac48Address addr); + +// +// Pure virtual methods inherited from NetDevice we must implement. +// + virtual void SetName(const std::string name); + virtual std::string GetName(void) const; + + virtual void SetIfIndex(const uint32_t index); + virtual uint32_t GetIfIndex(void) const; + + virtual Ptr GetChannel (void) const; + virtual Address GetAddress (void) const; + + virtual bool SetMtu (const uint16_t mtu); + virtual uint16_t GetMtu (void) const; + + virtual bool IsLinkUp (void) const; + + virtual void SetLinkChangeCallback (Callback callback); + + virtual bool IsBroadcast (void) const; + virtual Address GetBroadcast (void) const; + + virtual bool IsMulticast (void) const; + virtual Address GetMulticast (void) const; + virtual Address MakeMulticastAddress (Ipv4Address multicastGroup) const; + + virtual bool IsPointToPoint (void) const; + + virtual bool Send(Ptr packet, const Address &dest, uint16_t protocolNumber); + + virtual bool SendFrom(Ptr packet, const Address& source, const Address& dest, uint16_t protocolNumber); + + virtual Ptr GetNode (void) const; + virtual void SetNode (Ptr node); + + virtual bool NeedsArp (void) const; + + virtual void SetReceiveCallback (NetDevice::ReceiveCallback cb); + virtual void SetPromiscReceiveCallback (PromiscReceiveCallback cb); + + virtual bool SupportsSendFrom (void) const; + +private: + + virtual void DoDispose (void); + + /** + * Get a copy of the attached Queue. + * + * This method is provided for any derived class that may need to get + * direct access to the underlying queue. + * + * @returns Ptr to the queue. + */ + Ptr GetQueue(void) const; + + /** + * Spin up the device + */ + void StartDevice (void); + + /** + * Tear down the device + */ + void StopDevice (void); + + /** + * Loop to read and process packets + */ + void ReadThread (void); + + /** + * Method to handle received packets. Synchronized with simulator via ScheduleNow from ReadThread. + */ + void ForwardUp (uint8_t *buf, uint32_t len); + + /** + * Adds the necessary headers and trailers to a packet of data in order to + * respect the protocol implemented by the agent. + */ + void AddHeader(Ptr p, uint16_t protocolNumber); + + /** + * Removes, from a packet of data, all headers and trailers that + * relate to the protocol implemented by the agent + * \return Returns true if the packet should be forwarded up the + * protocol stack. + */ + bool ProcessHeader(Ptr p, uint16_t& param); + + /** + * Start Sending a Packet Down the Wire. + * + * @returns true if success, false on failure + */ + bool TransmitStart (Ptr p); + + void NotifyLinkUp (void); + + /** + * The Queue which this EmulatedNetDevice uses as a packet source. + * Management of this Queue has been delegated to the EmulatedNetDevice + * and it has the responsibility for deletion. + * @see class Queue + * @see class DropTailQueue + */ + Ptr m_queue; + + /** + * The trace source for the packet reception events that the device can + * fire. + * + * @see class CallBackTraceSource + */ + TracedCallback > m_rxTrace; + + /** + * The trace source for the packet drop events that the device can + * fire. + * + * @see class CallBackTraceSource + */ + TracedCallback > m_dropTrace; + + /** + * Time to start spinning up the device + */ + Time m_tStart; + + /** + * Time to start tearing down the device + */ + Time m_tStop; + + EventId m_startEvent; + EventId m_stopEvent; + + int32_t m_sock; + + Ptr m_readThread; + + /** + * The Node to which this device is attached. + */ + Ptr m_node; + + /** + * The MAC address which has been assigned to this device. + */ + Mac48Address m_address; + + /** + * The callback used to notify higher layers that a packet has been received. + */ + NetDevice::ReceiveCallback m_rxCallback; + + /** + * The callback used to notify higher layers that a packet has been received in promiscuous mode. + */ + NetDevice::PromiscReceiveCallback m_promiscRxCallback; + + /** + * The ns-3 interface index (in the sense of net device index) that has been assigned to this network device. + */ + uint32_t m_ifIndex; + + /** + * The Unix interface index that we got from the system and which corresponds to the interface (e.g., "eth1") + * we are using to talk to the network. Valid when m_sock is valid. + */ + int32_t m_sll_ifindex; + + /** + * The human readable name of this device. + */ + std::string m_name; + + /** + * Flag indicating whether or not the link is up. In this case, + * whether or not the device is connected to a channel. + */ + bool m_linkUp; + + /** + * Callback to fire if the link changes state (up or down). + */ + Callback m_linkChangeCallback; + + /** + * The unix/linux name of the underlying device (e.g., eth0) + */ + std::string m_deviceName; +}; + +} // namespace ns3 + +#endif // EMULATED_NET_DEVICE_H + diff --git a/src/devices/emulated/emulated.h b/src/devices/emulated/emulated.h new file mode 100644 index 000000000..8b2601bc3 --- /dev/null +++ b/src/devices/emulated/emulated.h @@ -0,0 +1,10 @@ +/** + * \ingroup devices + * \defgroup Emulated Emulated Net Device Model + * + * \section Emulated Net Device Model + * + * This is a description of the emulated network device model. + * + * It is very detailed and comprehensive and answers all possible questions. + */ diff --git a/src/devices/emulated/waf b/src/devices/emulated/waf new file mode 100755 index 000000000..4283ec141 --- /dev/null +++ b/src/devices/emulated/waf @@ -0,0 +1 @@ +exec "`dirname "$0"`"/../../../waf "$@" diff --git a/src/devices/emulated/wscript b/src/devices/emulated/wscript new file mode 100644 index 000000000..75d32c750 --- /dev/null +++ b/src/devices/emulated/wscript @@ -0,0 +1,14 @@ +## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*- + + +def build(bld): + module = bld.create_ns3_module('emulated', ['node']) + module.source = [ + 'emulated-net-device.cc', + ] + headers = bld.create_obj('ns3header') + headers.module = 'emulated' + headers.source = [ + 'emulated-net-device.h', + ] + diff --git a/src/devices/emutap/host-tap-net-device.cc b/src/devices/tap/host-tap-net-device.cc similarity index 100% rename from src/devices/emutap/host-tap-net-device.cc rename to src/devices/tap/host-tap-net-device.cc diff --git a/src/devices/emutap/host-tap-net-device.h b/src/devices/tap/host-tap-net-device.h similarity index 100% rename from src/devices/emutap/host-tap-net-device.h rename to src/devices/tap/host-tap-net-device.h diff --git a/src/devices/emutap/tap-channel.cc b/src/devices/tap/tap-channel.cc similarity index 100% rename from src/devices/emutap/tap-channel.cc rename to src/devices/tap/tap-channel.cc diff --git a/src/devices/emutap/tap-channel.h b/src/devices/tap/tap-channel.h similarity index 100% rename from src/devices/emutap/tap-channel.h rename to src/devices/tap/tap-channel.h diff --git a/src/devices/emutap/tap-manager-client.cc b/src/devices/tap/tap-manager-client.cc similarity index 100% rename from src/devices/emutap/tap-manager-client.cc rename to src/devices/tap/tap-manager-client.cc diff --git a/src/devices/emutap/tap-manager-client.h b/src/devices/tap/tap-manager-client.h similarity index 100% rename from src/devices/emutap/tap-manager-client.h rename to src/devices/tap/tap-manager-client.h diff --git a/src/devices/emutap/tap-manager.cc b/src/devices/tap/tap-manager.cc similarity index 88% rename from src/devices/emutap/tap-manager.cc rename to src/devices/tap/tap-manager.cc index a68ec9fc7..271d463ae 100644 --- a/src/devices/emutap/tap-manager.cc +++ b/src/devices/tap/tap-manager.cc @@ -1,3 +1,23 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2008 INRIA + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Mathieu Lacage + */ + #include #include #include @@ -21,6 +41,7 @@ std::cout << __FILE__ << ":" << __LINE__ << ": Unrecoverable Error: " << x; \ std::cout << " errno=" << strerror (errno) << std::endl; \ exit (err) + #ifdef ENABLE_LOG #define LOG(x) \ std::cout << x << std::endl; @@ -30,13 +51,13 @@ #define CHECK_ARG(el,var) \ - { \ - char start[] = "--" el "="; \ - if (strncmp (*argv, start, strlen (start)) == 0) \ - { \ - var = *argv + strlen (start); \ - LOG ("--" << el << "=" << var); \ - } \ + { \ + char start[] = "--" el "="; \ + if (strncmp (*argv, start, strlen (start)) == 0) \ + { \ + var = *argv + strlen (start); \ + LOG ("--" << el << "=" << var); \ + } \ } #define ASCII_DOT (0x2e) @@ -47,6 +68,7 @@ #define ASCII_Z (0x7a) #define ASCII_COLON (0x3a) #define ASCII_ZERO (0x30) + static char AsciiToLowCase (char c) { @@ -58,6 +80,7 @@ AsciiToLowCase (char c) return c; } } + static uint32_t AsciiToIpv4 (const char *address) { diff --git a/src/devices/emutap/tap-net-device.cc b/src/devices/tap/tap-net-device.cc similarity index 100% rename from src/devices/emutap/tap-net-device.cc rename to src/devices/tap/tap-net-device.cc diff --git a/src/devices/emutap/tap-net-device.h b/src/devices/tap/tap-net-device.h similarity index 100% rename from src/devices/emutap/tap-net-device.h rename to src/devices/tap/tap-net-device.h diff --git a/src/devices/emutap/wscript b/src/devices/tap/wscript similarity index 85% rename from src/devices/emutap/wscript rename to src/devices/tap/wscript index 11bdb3a3c..bde538c0a 100644 --- a/src/devices/emutap/wscript +++ b/src/devices/tap/wscript @@ -7,7 +7,7 @@ def build(bld): 'tap-manager.cc', ] - module = bld.create_ns3_module('emutap', ['node']) + module = bld.create_ns3_module('tap', ['node']) module.uselib = 'CAP' module.source = [ 'tap-net-device.cc', @@ -16,7 +16,7 @@ def build(bld): 'host-tap-net-device.cc', ] headers = bld.create_obj('ns3header') - headers.module = 'emutap' + headers.module = 'tap' headers.source = [ 'tap-net-device.h', 'host-tap-net-device.h', diff --git a/src/node/net-device.h b/src/node/net-device.h index 35c81f4f0..a3130b2d1 100644 --- a/src/node/net-device.h +++ b/src/node/net-device.h @@ -255,13 +255,23 @@ public: virtual bool NeedsArp (void) const = 0; - /** Packet types */ + /** + * Packet types are used as they are in Linux. GCC name resolution on + * typedef enum {} PacketType is broken for the foreseeable future, so + * if you need to use ns-3 PacketType in a driver that also uses the + * Linux packet types you're hosed unless we define a shadow type, + * which we do here. + */ enum PacketType { - PACKET_HOST = 1, /* To us */ - PACKET_BROADCAST, /* To all */ - PACKET_MULTICAST, /* To group */ - PACKET_OTHERHOST, /* To someone else */ + PACKET_HOST = 1, /**< Packet addressed oo us */ + NS3_PACKET_HOST = PACKET_HOST, + PACKET_BROADCAST, /**< Packet addressed to all */ + NS3_PACKET_BROADCAST = PACKET_BROADCAST, + PACKET_MULTICAST, /**< Packet addressed to multicast group */ + NS3_PACKET_MULTICAST = PACKET_MULTICAST, + PACKET_OTHERHOST, /**< Packet addressed to someone else */ + NS3_PACKET_OTHERHOST = PACKET_OTHERHOST, }; /** diff --git a/src/wscript b/src/wscript index 5cf64f53f..a2d4731e9 100644 --- a/src/wscript +++ b/src/wscript @@ -20,7 +20,8 @@ all_modules = ( 'devices/point-to-point', 'devices/csma', 'devices/bridge', - 'devices/emutap', + 'devices/tap', + 'devices/emulated', 'applications/onoff', 'applications/packet-sink', 'applications/udp-echo',