1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
3 * Copyright (c) 2008 University of Washington
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;
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.
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
19 #ifndef EMU_NET_DEVICE_H
20 #define EMU_NET_DEVICE_H
23 #include "ns3/address.h"
24 #include "ns3/net-device.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"
33 #include "ns3/mac48-address.h"
34 #include "ns3/system-thread.h"
42 * \brief A Device for an Emu Network Link.
44 class EmuNetDevice : public NetDevice
47 static TypeId GetTypeId (void);
50 * Construct a EmuNetDevice
52 * This is the constructor for the EmuNetDevice. It takes as a
57 * Destroy a EmuNetDevice
59 * This is the destructor for the EmuNetDevice.
61 virtual ~EmuNetDevice ();
64 * Set the Data Rate used for transmission of packets.
67 * @param bps the data rate at which this object operates
69 void SetDataRate (DataRate bps);
72 * Set the inteframe gap used to separate packets. The interframe gap
73 * defines the minimum space required between packets sent by this device.
75 * @param t the interframe gap time
77 void SetInterframeGap (Time t);
80 * Set a start time for the device.
82 * @param tStart the start time
84 void Start (Time tStart);
87 * Set a stop time for the device.
89 * @param tStop the stop time
91 void Stop (Time tStop);
94 * Attach a queue to the EmuNetDevice.
96 * The EmuNetDevice "owns" a queue that implements a queueing
97 * method such as DropTail or RED.
101 * @param queue Ptr to the new queue.
103 void SetQueue (Ptr<Queue> queue);
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.
112 * @param p Ptr to the received packet.
114 void Receive (Ptr<Packet> p);
117 * Assign a MAC address to this device.
120 * @param addr The new address.
122 void SetAddress (Mac48Address addr);
125 // Pure virtual methods inherited from NetDevice we must implement.
127 virtual void SetName(const std::string name);
128 virtual std::string GetName(void) const;
130 virtual void SetIfIndex(const uint32_t index);
131 virtual uint32_t GetIfIndex(void) const;
133 virtual Ptr<Channel> GetChannel (void) const;
134 virtual Address GetAddress (void) const;
136 virtual bool SetMtu (const uint16_t mtu);
137 virtual uint16_t GetMtu (void) const;
139 virtual bool IsLinkUp (void) const;
141 virtual void SetLinkChangeCallback (Callback<void> callback);
143 virtual bool IsBroadcast (void) const;
144 virtual Address GetBroadcast (void) const;
146 virtual bool IsMulticast (void) const;
149 * \brief Make and return a MAC multicast address using the provided
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).
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.
161 * \param multicastGroup The IP address for the multicast group destination
163 * \return The MAC multicast Address used to send packets to the provided
170 virtual Address GetMulticast (Ipv4Address multicastGroup) const;
173 * Is this a point to point link?
176 virtual bool IsPointToPoint (void) const;
178 virtual bool Send(Ptr<Packet> packet, const Address &dest, uint16_t protocolNumber);
180 virtual bool SendFrom(Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber);
182 virtual Ptr<Node> GetNode (void) const;
183 virtual void SetNode (Ptr<Node> node);
185 virtual bool NeedsArp (void) const;
187 virtual void SetReceiveCallback (NetDevice::ReceiveCallback cb);
188 virtual void SetPromiscReceiveCallback (PromiscReceiveCallback cb);
190 virtual bool SupportsSendFrom (void) const;
194 virtual void DoDispose (void);
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.
201 void CreateSocket (void);
204 * Figure out where the raw socket creation process lives on the system.
206 std::string FindCreator (void);
209 * Get a copy of the attached Queue.
211 * This method is provided for any derived class that may need to get
212 * direct access to the underlying queue.
214 * @returns Ptr to the queue.
216 Ptr<Queue> GetQueue(void) const;
221 void StartDevice (void);
224 * Tear down the device
226 void StopDevice (void);
229 * Loop to read and process packets
231 void ReadThread (void);
234 * Method to handle received packets. Synchronized with simulator via ScheduleNow from ReadThread.
236 void ForwardUp (uint8_t *buf, uint32_t len);
239 * Adds the necessary headers and trailers to a packet of data in order to
240 * respect the protocol implemented by the agent.
242 void AddHeader(Ptr<Packet> p, uint16_t protocolNumber);
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
250 bool ProcessHeader(Ptr<Packet> p, uint16_t& param);
253 * Start Sending a Packet Down the Wire.
255 * @returns true if success, false on failure
257 bool TransmitStart (Ptr<Packet> p);
259 void NotifyLinkUp (void);
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.
266 * @see class DropTailQueue
271 * The trace source for the packet reception events that the device can
274 * @see class CallBackTraceSource
276 TracedCallback<Ptr<const Packet> > m_rxTrace;
279 * The trace source for the packet drop events that the device can
282 * @see class CallBackTraceSource
284 TracedCallback<Ptr<const Packet> > m_dropTrace;
287 * Time to start spinning up the device
292 * Time to start tearing down the device
296 EventId m_startEvent;
301 Ptr<SystemThread> m_readThread;
304 * The Node to which this device is attached.
309 * The MAC address which has been assigned to this device.
311 Mac48Address m_address;
314 * The callback used to notify higher layers that a packet has been received.
316 NetDevice::ReceiveCallback m_rxCallback;
319 * The callback used to notify higher layers that a packet has been received in promiscuous mode.
321 NetDevice::PromiscReceiveCallback m_promiscRxCallback;
324 * The ns-3 interface index (in the sense of net device index) that has been assigned to this network device.
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.
332 int32_t m_sll_ifindex;
335 * The human readable name of this device.
340 * Flag indicating whether or not the link is up. In this case,
341 * whether or not the device is connected to a channel.
346 * Callback to fire if the link changes state (up or down).
348 Callback<void> m_linkChangeCallback;
351 * The unix/linux name of the underlying device (e.g., eth0)
353 std::string m_deviceName;
358 #endif // EMU_NET_DEVICE_H