1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008 INRIA
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  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #ifndef SIMPLE_NET_DEVICE_H
21 #define SIMPLE_NET_DEVICE_H
22 
23 #include <stdint.h>
24 #include <string>
25 
26 #include "ns3/traced-callback.h"
27 #include "ns3/net-device.h"
28 #include "ns3/data-rate.h"
29 #include "ns3/event-id.h"
30 
31 #include "mac48-address.h"
32 
33 namespace ns3 {
34 
35 template <typename Item> class Queue;
36 class SimpleChannel;
37 class Node;
38 class ErrorModel;
39 
40 /**
41  * \ingroup netdevice
42  *
43  * This device assumes 48-bit mac addressing; there is also the possibility to
44  * add an ErrorModel if you want to force losses on the device.
45  *
46  * The device can be installed on a node through the SimpleNetDeviceHelper.
47  * In case of manual creation, the user is responsible for assigning an unique
48  * address to the device.
49  *
50  * By default the device is in Broadcast mode, with infinite bandwidth.
51  *
52  * \brief simple net device for simple things and testing
53  */
54 class SimpleNetDevice : public NetDevice
55 {
56 public:
57   /**
58    * \brief Get the type ID.
59    * \return the object TypeId
60    */
61   static TypeId GetTypeId (void);
62   SimpleNetDevice ();
63 
64   /**
65    * Receive a packet from a connected SimpleChannel.  The
66    * SimpleNetDevice receives packets from its connected channel
67    * and then forwards them by calling its rx callback method
68    *
69    * \param packet Packet received on the channel
70    * \param protocol protocol number
71    * \param to address packet should be sent to
72    * \param from address packet was sent from
73    */
74   void Receive (Ptr<Packet> packet, uint16_t protocol, Mac48Address to, Mac48Address from);
75 
76   /**
77    * Attach a channel to this net device.  This will be the
78    * channel the net device sends on
79    *
80    * \param channel channel to assign to this net device
81    *
82    */
83   void SetChannel (Ptr<SimpleChannel> channel);
84 
85   /**
86    * Attach a queue to the SimpleNetDevice.
87    *
88    * \param queue Ptr to the new queue.
89    */
90   void SetQueue (Ptr<Queue<Packet> > queue);
91 
92   /**
93    * Get a copy of the attached Queue.
94    *
95    * \returns Ptr to the queue.
96    */
97   Ptr<Queue<Packet> > GetQueue (void) const;
98 
99   /**
100    * Attach a receive ErrorModel to the SimpleNetDevice.
101    *
102    * The SimpleNetDevice may optionally include an ErrorModel in
103    * the packet receive chain.
104    *
105    * \see ErrorModel
106    * \param em Ptr to the ErrorModel.
107    */
108   void SetReceiveErrorModel (Ptr<ErrorModel> em);
109 
110   // inherited from NetDevice base class.
111   virtual void SetIfIndex (const uint32_t index);
112   virtual uint32_t GetIfIndex (void) const;
113   virtual Ptr<Channel> GetChannel (void) const;
114   virtual void SetAddress (Address address);
115   virtual Address GetAddress (void) const;
116   virtual bool SetMtu (const uint16_t mtu);
117   virtual uint16_t GetMtu (void) const;
118   virtual bool IsLinkUp (void) const;
119   virtual void AddLinkChangeCallback (Callback<void> callback);
120   virtual bool IsBroadcast (void) const;
121   virtual Address GetBroadcast (void) const;
122   virtual bool IsMulticast (void) const;
123   virtual Address GetMulticast (Ipv4Address multicastGroup) const;
124   virtual bool IsPointToPoint (void) const;
125   virtual bool IsBridge (void) const;
126   virtual bool Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber);
127   virtual bool SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber);
128   virtual Ptr<Node> GetNode (void) const;
129   virtual void SetNode (Ptr<Node> node);
130   virtual bool NeedsArp (void) const;
131   virtual void SetReceiveCallback (NetDevice::ReceiveCallback cb);
132 
133   virtual Address GetMulticast (Ipv6Address addr) const;
134 
135   virtual void SetPromiscReceiveCallback (PromiscReceiveCallback cb);
136   virtual bool SupportsSendFrom (void) const;
137 
138 protected:
139   virtual void DoDispose (void);
140 
141 private:
142   Ptr<SimpleChannel> m_channel; //!< the channel the device is connected to
143   NetDevice::ReceiveCallback m_rxCallback; //!< Receive callback
144   NetDevice::PromiscReceiveCallback m_promiscCallback; //!< Promiscuous receive callback
145   Ptr<Node> m_node; //!< Node this netDevice is associated to
146   uint16_t m_mtu;   //!< MTU
147   uint32_t m_ifIndex; //!< Interface index
148   Mac48Address m_address; //!< MAC address
149   Ptr<ErrorModel> m_receiveErrorModel; //!< Receive error model.
150 
151   /**
152    * The trace source fired when the phy layer drops a packet it has received
153    * due to the error model being active.  Although SimpleNetDevice doesn't
154    * really have a Phy model, we choose this trace source name for alignment
155    * with other trace sources.
156    *
157    * \see class CallBackTraceSource
158    */
159   TracedCallback<Ptr<const Packet> > m_phyRxDropTrace;
160 
161   /**
162    * The StartTransmission method is used internally to start the process
163    * of sending a packet out on the channel, by scheduling the
164    * FinishTransmission method at a time corresponding to the transmission
165    * delay of the packet.
166    */
167   void StartTransmission (void);
168 
169   /**
170    * The FinishTransmission method is used internally to finish the process
171    * of sending a packet out on the channel.
172    * \param packet The packet to send on the channel
173    */
174   void FinishTransmission (Ptr<Packet> packet);
175 
176   bool m_linkUp; //!< Flag indicating whether or not the link is up
177 
178   /**
179    * Flag indicating whether or not the NetDevice is a Point to Point model.
180    * Enabling this will disable Broadcast and Arp.
181    */
182   bool m_pointToPointMode;
183 
184   Ptr<Queue<Packet> > m_queue; //!< The Queue for outgoing packets.
185   DataRate m_bps; //!< The device nominal Data rate. Zero means infinite
186   EventId FinishTransmissionEvent; //!< the Tx Complete event
187 
188   /**
189    * List of callbacks to fire if the link changes state (up or down).
190    */
191   TracedCallback<> m_linkChangeCallbacks;
192 };
193 
194 } // namespace ns3
195 
196 #endif /* SIMPLE_NET_DEVICE_H */
197