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 a start time for the device.
74 * @param tStart the start time
76 void Start (Time tStart);
79 * Set a stop time for the device.
81 * @param tStop the stop time
83 void Stop (Time tStop);
86 * Attach a queue to the EmuNetDevice.
88 * The EmuNetDevice "owns" a queue that implements a queueing
89 * method such as DropTail or RED.
93 * @param queue Ptr to the new queue.
95 void SetQueue (Ptr<Queue> queue);
98 * Assign a MAC address to this device.
101 * @param addr The new address.
103 void SetAddress (Mac48Address addr);
106 // Pure virtual methods inherited from NetDevice we must implement.
108 virtual void SetName(const std::string name);
109 virtual std::string GetName(void) const;
111 virtual void SetIfIndex(const uint32_t index);
112 virtual uint32_t GetIfIndex(void) const;
114 virtual Ptr<Channel> GetChannel (void) const;
115 virtual Address GetAddress (void) const;
117 virtual bool SetMtu (const uint16_t mtu);
118 virtual uint16_t GetMtu (void) const;
120 virtual bool IsLinkUp (void) const;
122 virtual void SetLinkChangeCallback (Callback<void> callback);
124 virtual bool IsBroadcast (void) const;
125 virtual Address GetBroadcast (void) const;
127 virtual bool IsMulticast (void) const;
130 * \brief Make and return a MAC multicast address using the provided
133 * RFC 1112 says that an Ipv4 host group address is mapped to an Ethernet
134 * multicast address by placing the low-order 23-bits of the IP address into
135 * the low-order 23 bits of the Ethernet multicast address
136 * 01-00-5E-00-00-00 (hex).
138 * This method performs the multicast address creation function appropriate
139 * to an EUI-48-based CSMA device. This MAC address is encapsulated in an
140 * abstract Address to avoid dependencies on the exact address format.
142 * \param multicastGroup The IP address for the multicast group destination
144 * \return The MAC multicast Address used to send packets to the provided
151 virtual Address GetMulticast (Ipv4Address multicastGroup) const;
154 * \brief Get the MAC multicast address corresponding
155 * to the IPv6 address provided.
156 * \param addr IPv6 address
157 * \return the MAC multicast address
158 * \warning Calling this method is invalid if IsMulticast returns not true.
160 virtual Address GetMulticast (Ipv6Address addr) const;
163 * Is this a point to point link?
166 virtual bool IsPointToPoint (void) const;
168 virtual bool Send(Ptr<Packet> packet, const Address &dest, uint16_t protocolNumber);
170 virtual bool SendFrom(Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber);
172 virtual Ptr<Node> GetNode (void) const;
173 virtual void SetNode (Ptr<Node> node);
175 virtual bool NeedsArp (void) const;
177 virtual void SetReceiveCallback (NetDevice::ReceiveCallback cb);
178 virtual void SetPromiscReceiveCallback (PromiscReceiveCallback cb);
180 virtual bool SupportsSendFrom (void) const;
184 virtual void DoDispose (void);
187 * Call out to a separate process running as suid root in order to get a raw
188 * socket. We do this to avoid having the entire simulation running as root.
189 * If this method returns, we'll have a raw socket waiting for us in m_sock.
191 void CreateSocket (void);
194 * Figure out where the raw socket creation process lives on the system.
196 std::string FindCreator (void);
199 * Get a copy of the attached Queue.
201 * This method is provided for any derived class that may need to get
202 * direct access to the underlying queue.
204 * @returns Ptr to the queue.
206 Ptr<Queue> GetQueue(void) const;
211 void StartDevice (void);
214 * Tear down the device
216 void StopDevice (void);
219 * Loop to read and process packets
221 void ReadThread (void);
224 * Method to handle received packets. Synchronized with simulator via ScheduleNow from ReadThread.
226 void ForwardUp (uint8_t *buf, uint32_t len);
229 * Adds the necessary headers and trailers to a packet of data in order to
230 * respect the protocol implemented by the agent.
232 void AddHeader(Ptr<Packet> p, uint16_t protocolNumber);
235 * Removes, from a packet of data, all headers and trailers that
236 * relate to the protocol implemented by the agent
237 * \return Returns true if the packet should be forwarded up the
240 bool ProcessHeader(Ptr<Packet> p, uint16_t& param);
243 * Start Sending a Packet Down the Wire.
245 * @returns true if success, false on failure
247 bool TransmitStart (Ptr<Packet> p);
249 void NotifyLinkUp (void);
252 * The Queue which this EmuNetDevice uses as a packet source.
253 * Management of this Queue has been delegated to the EmuNetDevice
254 * and it has the responsibility for deletion.
256 * @see class DropTailQueue
261 * The trace source for the packet reception events that the device can
264 * @see class CallBackTraceSource
266 TracedCallback<Ptr<const Packet> > m_rxTrace;
269 * The trace source for the packet drop events that the device can
272 * @see class CallBackTraceSource
274 TracedCallback<Ptr<const Packet> > m_dropTrace;
277 * Time to start spinning up the device
282 * Time to start tearing down the device
286 EventId m_startEvent;
291 Ptr<SystemThread> m_readThread;
294 * The Node to which this device is attached.
299 * The MAC address which has been assigned to this device.
301 Mac48Address m_address;
304 * The callback used to notify higher layers that a packet has been received.
306 NetDevice::ReceiveCallback m_rxCallback;
309 * The callback used to notify higher layers that a packet has been received in promiscuous mode.
311 NetDevice::PromiscReceiveCallback m_promiscRxCallback;
314 * The ns-3 interface index (in the sense of net device index) that has been assigned to this network device.
319 * The Unix interface index that we got from the system and which corresponds to the interface (e.g., "eth1")
320 * we are using to talk to the network. Valid when m_sock is valid.
322 int32_t m_sll_ifindex;
325 * The human readable name of this device.
330 * Flag indicating whether or not the link is up. In this case,
331 * whether or not the device is connected to a channel.
336 * Callback to fire if the link changes state (up or down).
338 Callback<void> m_linkChangeCallback;
341 * The unix/linux name of the underlying device (e.g., eth0)
343 std::string m_deviceName;
348 #endif // EMU_NET_DEVICE_H