src/devices/emu/emu-net-device.h
author vincent@clarinet.u-strasbg.fr
Wed, 05 Nov 2008 14:49:21 -0800
changeset 3842 545ddf9398ed
parent 3830 8862b9be62bb
child 3843 ca2159d54d75
permissions -rw-r--r--
apply patch for bug 294 (GetMulticastAddr) + emu device update + rescan
     1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
     2 /*
     3  * Copyright (c) 2008 University of Washington
     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 
    19 #ifndef EMU_NET_DEVICE_H
    20 #define EMU_NET_DEVICE_H
    21 
    22 #include <string.h>
    23 #include "ns3/address.h"
    24 #include "ns3/net-device.h"
    25 #include "ns3/node.h"
    26 #include "ns3/callback.h"
    27 #include "ns3/packet.h"
    28 #include "ns3/traced-callback.h"
    29 #include "ns3/event-id.h"
    30 #include "ns3/nstime.h"
    31 #include "ns3/data-rate.h"
    32 #include "ns3/ptr.h"
    33 #include "ns3/mac48-address.h"
    34 #include "ns3/system-thread.h"
    35 
    36 namespace ns3 {
    37 
    38 class Queue;
    39 
    40 /**
    41  * \class EmuNetDevice
    42  * \brief A Device for an Emu Network Link.
    43  */
    44 class EmuNetDevice : public NetDevice 
    45 {
    46 public:
    47   static TypeId GetTypeId (void);
    48 
    49   /**
    50    * Construct a EmuNetDevice
    51    *
    52    * This is the constructor for the EmuNetDevice.  It takes as a
    53    */
    54   EmuNetDevice ();
    55 
    56   /**
    57    * Destroy a EmuNetDevice
    58    *
    59    * This is the destructor for the EmuNetDevice.
    60    */
    61   virtual ~EmuNetDevice ();
    62 
    63   /**
    64    * Set the Data Rate used for transmission of packets.  
    65    *
    66    * @see Attach ()
    67    * @param bps the data rate at which this object operates
    68    */
    69   void SetDataRate (DataRate bps);
    70 
    71   /**
    72    * Set the inteframe gap used to separate packets.  The interframe gap
    73    * defines the minimum space required between packets sent by this device.
    74    *
    75    * @param t the interframe gap time
    76    */
    77   void SetInterframeGap (Time t);
    78 
    79   /**
    80    * Set a start time for the device.
    81    *
    82    * @param tStart the start time
    83    */
    84   void Start (Time tStart);
    85 
    86   /**
    87    * Set a stop time for the device.
    88    *
    89    * @param tStop the stop time
    90    */
    91   void Stop (Time tStop);
    92 
    93   /**
    94    * Attach a queue to the EmuNetDevice.
    95    *
    96    * The EmuNetDevice "owns" a queue that implements a queueing 
    97    * method such as DropTail or RED.  
    98    *
    99    * @see Queue
   100    * @see DropTailQueue
   101    * @param queue Ptr to the new queue.
   102    */
   103   void SetQueue (Ptr<Queue> queue);
   104 
   105   /**
   106    * Receive a packet.
   107    *
   108    * The EmuNetDevice receives packets from its socket reader
   109    * and forwards them up the protocol stack.  This is the public method
   110    * used by the reader to indicate that a packet has arrived at the device.
   111    *
   112    * @param p Ptr to the received packet.
   113    */
   114   void Receive (Ptr<Packet> p);
   115 
   116   /**
   117    * Assign a MAC address to this device.
   118    *
   119    * @see Mac48Address
   120    * @param addr The new address.
   121    */
   122   void SetAddress (Mac48Address addr);
   123 
   124 //
   125 // Pure virtual methods inherited from NetDevice we must implement.
   126 //
   127   virtual void SetName(const std::string name);
   128   virtual std::string GetName(void) const;
   129 
   130   virtual void SetIfIndex(const uint32_t index);
   131   virtual uint32_t GetIfIndex(void) const;
   132 
   133   virtual Ptr<Channel> GetChannel (void) const;
   134   virtual Address GetAddress (void) const;
   135 
   136   virtual bool SetMtu (const uint16_t mtu);
   137   virtual uint16_t GetMtu (void) const;
   138 
   139   virtual bool IsLinkUp (void) const;
   140 
   141   virtual void SetLinkChangeCallback (Callback<void> callback);
   142 
   143   virtual bool IsBroadcast (void) const;
   144   virtual Address GetBroadcast (void) const;
   145 
   146   virtual bool IsMulticast (void) const;
   147 
   148   /**
   149    * \brief Make and return a MAC multicast address using the provided
   150    *        multicast group
   151    *
   152    * RFC 1112 says that an Ipv4 host group address is mapped to an Ethernet 
   153    * multicast address by placing the low-order 23-bits of the IP address into 
   154    * the low-order 23 bits of the Ethernet multicast address 
   155    * 01-00-5E-00-00-00 (hex).
   156    *
   157    * This method performs the multicast address creation function appropriate
   158    * to an EUI-48-based CSMA device.  This MAC address is encapsulated in an
   159    *  abstract Address to avoid dependencies on the exact address format.
   160    *
   161    * \param multicastGroup The IP address for the multicast group destination
   162    * of the packet.
   163    * \return The MAC multicast Address used to send packets to the provided
   164    * multicast group.
   165    *
   166    * \see Ipv4Address
   167    * \see Mac48Address
   168    * \see Address
   169    */
   170   virtual Address GetMulticast (Ipv4Address multicastGroup) const;
   171 
   172   /**
   173    * Is this a point to point link?
   174    * \returns false.
   175    */
   176   virtual bool IsPointToPoint (void) const;
   177 
   178   virtual bool Send(Ptr<Packet> packet, const Address &dest, uint16_t protocolNumber);
   179 
   180   virtual bool SendFrom(Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber);
   181 
   182   virtual Ptr<Node> GetNode (void) const;
   183   virtual void SetNode (Ptr<Node> node);
   184 
   185   virtual bool NeedsArp (void) const;
   186 
   187   virtual void SetReceiveCallback (NetDevice::ReceiveCallback cb);
   188   virtual void SetPromiscReceiveCallback (PromiscReceiveCallback cb);
   189 
   190   virtual bool SupportsSendFrom (void) const;
   191 
   192 private:
   193 
   194   virtual void DoDispose (void);
   195 
   196   /**
   197    * Call out to a separate process running as suid root in order to get a raw 
   198    * socket.  We do this to avoid having the entire simulation running as root.
   199    * If this method returns, we'll have a raw socket waiting for us in m_sock.
   200    */
   201   void CreateSocket (void);
   202 
   203   /**
   204    * Figure out where the raw socket creation process lives on the system.
   205    */
   206   std::string FindCreator (void);
   207 
   208   /**
   209    * Get a copy of the attached Queue.
   210    *
   211    * This method is provided for any derived class that may need to get
   212    * direct access to the underlying queue.
   213    *
   214    * @returns Ptr to the queue.
   215    */
   216   Ptr<Queue> GetQueue(void) const; 
   217 
   218   /**
   219    * Spin up the device
   220    */
   221   void StartDevice (void);
   222 
   223   /**
   224    * Tear down the device
   225    */
   226   void StopDevice (void);
   227 
   228   /**
   229    * Loop to read and process packets
   230    */
   231   void ReadThread (void);
   232 
   233   /**
   234    * Method to handle received packets.  Synchronized with simulator via ScheduleNow from ReadThread.
   235    */
   236   void ForwardUp (uint8_t *buf, uint32_t len);
   237 
   238   /**
   239    * Adds the necessary headers and trailers to a packet of data in order to
   240    * respect the protocol implemented by the agent.
   241    */
   242   void AddHeader(Ptr<Packet> p, uint16_t protocolNumber);
   243 
   244   /**
   245    * Removes, from a packet of data, all headers and trailers that
   246    * relate to the protocol implemented by the agent
   247    * \return Returns true if the packet should be forwarded up the
   248    * protocol stack.
   249    */
   250   bool ProcessHeader(Ptr<Packet> p, uint16_t& param);
   251 
   252   /**
   253    * Start Sending a Packet Down the Wire.
   254    *
   255    * @returns true if success, false on failure
   256    */
   257   bool TransmitStart (Ptr<Packet> p);
   258 
   259   void NotifyLinkUp (void);
   260 
   261   /**
   262    * The Queue which this EmuNetDevice uses as a packet source.
   263    * Management of this Queue has been delegated to the EmuNetDevice
   264    * and it has the responsibility for deletion.
   265    * @see class Queue
   266    * @see class DropTailQueue
   267    */
   268   Ptr<Queue> m_queue;
   269 
   270   /**
   271    * The trace source for the packet reception events that the device can
   272    * fire.
   273    *
   274    * @see class CallBackTraceSource
   275    */
   276   TracedCallback<Ptr<const Packet> > m_rxTrace;
   277 
   278   /**
   279    * The trace source for the packet drop events that the device can
   280    * fire.
   281    *
   282    * @see class CallBackTraceSource
   283    */
   284   TracedCallback<Ptr<const Packet> > m_dropTrace;
   285 
   286   /**
   287    * Time to start spinning up the device
   288    */
   289   Time m_tStart;
   290 
   291   /**
   292    * Time to start tearing down the device
   293    */
   294   Time m_tStop;
   295 
   296   EventId m_startEvent;
   297   EventId m_stopEvent;
   298 
   299   int32_t m_sock;
   300 
   301   Ptr<SystemThread> m_readThread;
   302 
   303   /**
   304    * The Node to which this device is attached.
   305    */
   306   Ptr<Node> m_node;
   307 
   308   /**
   309    * The MAC address which has been assigned to this device.
   310    */
   311   Mac48Address m_address;
   312 
   313   /**
   314    * The callback used to notify higher layers that a packet has been received.
   315    */
   316   NetDevice::ReceiveCallback m_rxCallback;
   317 
   318   /**
   319    * The callback used to notify higher layers that a packet has been received in promiscuous mode.
   320    */
   321   NetDevice::PromiscReceiveCallback m_promiscRxCallback;
   322 
   323   /**
   324    * The ns-3 interface index (in the sense of net device index) that has been assigned to this network device.
   325    */
   326   uint32_t m_ifIndex;
   327 
   328   /**
   329    * The Unix interface index that we got from the system and which corresponds to the interface (e.g., "eth1")
   330    * we are using to talk to the network.  Valid when m_sock is valid.
   331    */
   332   int32_t m_sll_ifindex;
   333 
   334   /**
   335    * The human readable name of this device.
   336    */
   337   std::string m_name;
   338 
   339   /**
   340    * Flag indicating whether or not the link is up.  In this case,
   341    * whether or not the device is connected to a channel.
   342    */
   343   bool m_linkUp;
   344 
   345   /**
   346    * Callback to fire if the link changes state (up or down).
   347    */
   348   Callback<void> m_linkChangeCallback;
   349 
   350   /**
   351    * The unix/linux name of the underlying device (e.g., eth0)
   352    */
   353   std::string m_deviceName;
   354 };
   355 
   356 } // namespace ns3
   357 
   358 #endif // EMU_NET_DEVICE_H
   359