00001 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ 00002 /* 00003 * Copyright (c) 2008 University of Washington 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License version 2 as 00007 * published by the Free Software Foundation; 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 */ 00018 00019 #ifndef EMU_NET_DEVICE_H 00020 #define EMU_NET_DEVICE_H 00021 00022 #include <string.h> 00023 #include "ns3/address.h" 00024 #include "ns3/net-device.h" 00025 #include "ns3/node.h" 00026 #include "ns3/callback.h" 00027 #include "ns3/packet.h" 00028 #include "ns3/traced-callback.h" 00029 #include "ns3/event-id.h" 00030 #include "ns3/nstime.h" 00031 #include "ns3/data-rate.h" 00032 #include "ns3/ptr.h" 00033 #include "ns3/mac48-address.h" 00034 #include "ns3/system-thread.h" 00035 00036 namespace ns3 { 00037 00038 class Queue; 00039 00040 /** 00041 * \class EmuNetDevice 00042 * \brief A Device for an Emu Network Link. 00043 */ 00044 class EmuNetDevice : public NetDevice 00045 { 00046 public: 00047 static TypeId GetTypeId (void); 00048 00049 /** 00050 * Construct a EmuNetDevice 00051 * 00052 * This is the constructor for the EmuNetDevice. It takes as a 00053 */ 00054 EmuNetDevice (); 00055 00056 /** 00057 * Destroy a EmuNetDevice 00058 * 00059 * This is the destructor for the EmuNetDevice. 00060 */ 00061 virtual ~EmuNetDevice (); 00062 00063 /** 00064 * Set the Data Rate used for transmission of packets. 00065 * 00066 * @see Attach () 00067 * @param bps the data rate at which this object operates 00068 */ 00069 void SetDataRate (DataRate bps); 00070 00071 /** 00072 * Set a start time for the device. 00073 * 00074 * @param tStart the start time 00075 */ 00076 void Start (Time tStart); 00077 00078 /** 00079 * Set a stop time for the device. 00080 * 00081 * @param tStop the stop time 00082 */ 00083 void Stop (Time tStop); 00084 00085 /** 00086 * Attach a queue to the EmuNetDevice. 00087 * 00088 * The EmuNetDevice "owns" a queue that implements a queueing 00089 * method such as DropTail or RED. 00090 * 00091 * @see Queue 00092 * @see DropTailQueue 00093 * @param queue Ptr to the new queue. 00094 */ 00095 void SetQueue (Ptr<Queue> queue); 00096 00097 /** 00098 * Assign a MAC address to this device. 00099 * 00100 * @see Mac48Address 00101 * @param addr The new address. 00102 */ 00103 void SetAddress (Mac48Address addr); 00104 00105 // 00106 // Pure virtual methods inherited from NetDevice we must implement. 00107 // 00108 virtual void SetName(const std::string name); 00109 virtual std::string GetName(void) const; 00110 00111 virtual void SetIfIndex(const uint32_t index); 00112 virtual uint32_t GetIfIndex(void) const; 00113 00114 virtual Ptr<Channel> GetChannel (void) const; 00115 virtual Address GetAddress (void) const; 00116 00117 virtual bool SetMtu (const uint16_t mtu); 00118 virtual uint16_t GetMtu (void) const; 00119 00120 virtual bool IsLinkUp (void) const; 00121 00122 virtual void SetLinkChangeCallback (Callback<void> callback); 00123 00124 virtual bool IsBroadcast (void) const; 00125 virtual Address GetBroadcast (void) const; 00126 00127 virtual bool IsMulticast (void) const; 00128 00129 /** 00130 * \brief Make and return a MAC multicast address using the provided 00131 * multicast group 00132 * 00133 * RFC 1112 says that an Ipv4 host group address is mapped to an Ethernet 00134 * multicast address by placing the low-order 23-bits of the IP address into 00135 * the low-order 23 bits of the Ethernet multicast address 00136 * 01-00-5E-00-00-00 (hex). 00137 * 00138 * This method performs the multicast address creation function appropriate 00139 * to an EUI-48-based CSMA device. This MAC address is encapsulated in an 00140 * abstract Address to avoid dependencies on the exact address format. 00141 * 00142 * \param multicastGroup The IP address for the multicast group destination 00143 * of the packet. 00144 * \return The MAC multicast Address used to send packets to the provided 00145 * multicast group. 00146 * 00147 * \see Ipv4Address 00148 * \see Mac48Address 00149 * \see Address 00150 */ 00151 virtual Address GetMulticast (Ipv4Address multicastGroup) const; 00152 00153 /** 00154 * \brief Get the MAC multicast address corresponding 00155 * to the IPv6 address provided. 00156 * \param addr IPv6 address 00157 * \return the MAC multicast address 00158 * \warning Calling this method is invalid if IsMulticast returns not true. 00159 */ 00160 virtual Address GetMulticast (Ipv6Address addr) const; 00161 00162 /** 00163 * Is this a point to point link? 00164 * \returns false. 00165 */ 00166 virtual bool IsPointToPoint (void) const; 00167 00168 /** 00169 * Is this a bridge? 00170 * \returns false. 00171 */ 00172 virtual bool IsBridge (void) const; 00173 00174 virtual bool Send(Ptr<Packet> packet, const Address &dest, uint16_t protocolNumber); 00175 00176 virtual bool SendFrom(Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber); 00177 00178 virtual Ptr<Node> GetNode (void) const; 00179 virtual void SetNode (Ptr<Node> node); 00180 00181 virtual bool NeedsArp (void) const; 00182 00183 virtual void SetReceiveCallback (NetDevice::ReceiveCallback cb); 00184 virtual void SetPromiscReceiveCallback (PromiscReceiveCallback cb); 00185 00186 virtual bool SupportsSendFrom (void) const; 00187 00188 private: 00189 00190 virtual void DoDispose (void); 00191 00192 /** 00193 * Call out to a separate process running as suid root in order to get a raw 00194 * socket. We do this to avoid having the entire simulation running as root. 00195 * If this method returns, we'll have a raw socket waiting for us in m_sock. 00196 */ 00197 void CreateSocket (void); 00198 00199 /** 00200 * Figure out where the raw socket creation process lives on the system. 00201 */ 00202 std::string FindCreator (void); 00203 00204 /** 00205 * Get a copy of the attached Queue. 00206 * 00207 * This method is provided for any derived class that may need to get 00208 * direct access to the underlying queue. 00209 * 00210 * @returns Ptr to the queue. 00211 */ 00212 Ptr<Queue> GetQueue(void) const; 00213 00214 /** 00215 * Spin up the device 00216 */ 00217 void StartDevice (void); 00218 00219 /** 00220 * Tear down the device 00221 */ 00222 void StopDevice (void); 00223 00224 /** 00225 * Loop to read and process packets 00226 */ 00227 void ReadThread (void); 00228 00229 /** 00230 * Method to handle received packets. Synchronized with simulator via ScheduleNow from ReadThread. 00231 */ 00232 void ForwardUp (uint8_t *buf, uint32_t len); 00233 00234 /** 00235 * Adds the necessary headers and trailers to a packet of data in order to 00236 * respect the protocol implemented by the agent. 00237 */ 00238 void AddHeader(Ptr<Packet> p, uint16_t protocolNumber); 00239 00240 /** 00241 * Removes, from a packet of data, all headers and trailers that 00242 * relate to the protocol implemented by the agent 00243 * \return Returns true if the packet should be forwarded up the 00244 * protocol stack. 00245 */ 00246 bool ProcessHeader(Ptr<Packet> p, uint16_t& param); 00247 00248 /** 00249 * Start Sending a Packet Down the Wire. 00250 * 00251 * @returns true if success, false on failure 00252 */ 00253 bool TransmitStart (Ptr<Packet> p); 00254 00255 void NotifyLinkUp (void); 00256 00257 /** 00258 * The Queue which this EmuNetDevice uses as a packet source. 00259 * Management of this Queue has been delegated to the EmuNetDevice 00260 * and it has the responsibility for deletion. 00261 * @see class Queue 00262 * @see class DropTailQueue 00263 */ 00264 Ptr<Queue> m_queue; 00265 00266 /** 00267 * The trace source for the packet reception events that the device can 00268 * fire. 00269 * 00270 * @see class CallBackTraceSource 00271 */ 00272 TracedCallback<Ptr<const Packet> > m_rxTrace; 00273 00274 /** 00275 * The trace source for the packet drop events that the device can 00276 * fire. 00277 * 00278 * @see class CallBackTraceSource 00279 */ 00280 TracedCallback<Ptr<const Packet> > m_dropTrace; 00281 00282 /** 00283 * Time to start spinning up the device 00284 */ 00285 Time m_tStart; 00286 00287 /** 00288 * Time to start tearing down the device 00289 */ 00290 Time m_tStop; 00291 00292 EventId m_startEvent; 00293 EventId m_stopEvent; 00294 00295 int32_t m_sock; 00296 00297 Ptr<SystemThread> m_readThread; 00298 00299 /** 00300 * The Node to which this device is attached. 00301 */ 00302 Ptr<Node> m_node; 00303 00304 /** 00305 * The MAC address which has been assigned to this device. 00306 */ 00307 Mac48Address m_address; 00308 00309 /** 00310 * The callback used to notify higher layers that a packet has been received. 00311 */ 00312 NetDevice::ReceiveCallback m_rxCallback; 00313 00314 /** 00315 * The callback used to notify higher layers that a packet has been received in promiscuous mode. 00316 */ 00317 NetDevice::PromiscReceiveCallback m_promiscRxCallback; 00318 00319 /** 00320 * The ns-3 interface index (in the sense of net device index) that has been assigned to this network device. 00321 */ 00322 uint32_t m_ifIndex; 00323 00324 /** 00325 * The Unix interface index that we got from the system and which corresponds to the interface (e.g., "eth1") 00326 * we are using to talk to the network. Valid when m_sock is valid. 00327 */ 00328 int32_t m_sll_ifindex; 00329 00330 /** 00331 * The human readable name of this device. 00332 */ 00333 std::string m_name; 00334 00335 /** 00336 * Flag indicating whether or not the link is up. In this case, 00337 * whether or not the device is connected to a channel. 00338 */ 00339 bool m_linkUp; 00340 00341 /** 00342 * Callback to fire if the link changes state (up or down). 00343 */ 00344 Callback<void> m_linkChangeCallback; 00345 00346 /** 00347 * The unix/linux name of the underlying device (e.g., eth0) 00348 */ 00349 std::string m_deviceName; 00350 }; 00351 00352 } // namespace ns3 00353 00354 #endif // EMU_NET_DEVICE_H 00355