1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
3 * Copyright (c) 2005,2006 INRIA
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
18 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19 * Modified by Emmanuelle Laprise to remove dependance on LLC headers
26 #include "ns3/callback.h"
27 #include "ns3/object.h"
30 #include "ipv4-address.h"
31 #include "ipv6-address.h"
41 * \defgroup netdevice NetDevice
46 * \brief Network layer to device interface
48 * This interface defines the API which the IP and ARP
49 * layers need to access to manage an instance of a network device
50 * layer. It currently does not support MAC-level
51 * multicast but this should not be too hard to add by adding
52 * extra methods to register MAC multicast addresses to
53 * filter out unwanted packets before handing them to the
56 * In Linux, this interface is analogous to the interface
57 * just above dev_queue_xmit() (i.e., IP packet is fully
58 * constructed with destination MAC address already selected).
60 * If you want to write a new MAC layer, you need to subclass
61 * this base class and implement your own version of the
62 * NetDevice::SendTo method.
64 class NetDevice : public Object
67 static TypeId GetTypeId (void);
71 * \param name name of the device (e.g. "eth0")
73 virtual void SetName(const std::string name) = 0;
75 * \return name name of the device (e.g. "eth0")
77 virtual std::string GetName(void) const = 0;
79 * \param index ifIndex of the device
81 virtual void SetIfIndex(const uint32_t index) = 0;
83 * \return index ifIndex of the device
85 virtual uint32_t GetIfIndex(void) const = 0;
89 * \return the channel this NetDevice is connected to. The value
90 * returned can be zero if the NetDevice is not yet connected
93 virtual Ptr<Channel> GetChannel (void) const = 0;
96 * \return the current Address of this interface.
98 virtual Address GetAddress (void) const = 0;
100 * \param mtu MTU value, in bytes, to set for the device
101 * \return whether the MTU value was within legal bounds
103 * Override for default MTU defined on a per-type basis.
105 virtual bool SetMtu (const uint16_t mtu) = 0;
107 * \return the link-level MTU in bytes for this interface.
109 * This value is typically used by the IP layer to perform
110 * IP fragmentation when needed.
112 virtual uint16_t GetMtu (void) const = 0;
114 * \return true if link is up; false otherwise
116 virtual bool IsLinkUp (void) const = 0;
118 * \param callback the callback to invoke
120 * Register a callback invoked whenever the link
121 * status changes to UP. This callback is typically used
122 * by the IP/ARP layer to flush the ARP cache
123 * whenever the link goes up.
125 virtual void SetLinkChangeCallback (Callback<void> callback) = 0;
127 * \return true if this interface supports a broadcast address,
130 virtual bool IsBroadcast (void) const = 0;
132 * \return the broadcast address supported by
135 * Calling this method is invalid if IsBroadcast returns
138 virtual Address GetBroadcast (void) const = 0;
141 * \return value of m_isMulticast flag
143 virtual bool IsMulticast (void) const = 0;
146 * \brief Make and return a MAC multicast address using the provided
149 * RFC 1112 says that an Ipv4 host group address is mapped to an Ethernet
150 * multicast address by placing the low-order 23-bits of the IP address into
151 * the low-order 23 bits of the Ethernet multicast address
152 * 01-00-5E-00-00-00 (hex). Similar RFCs exist for Ipv6 and Eui64 mappings.
153 * This method performs the multicast address creation function appropriate
154 * to the underlying MAC address of the device. This MAC address is
155 * encapsulated in an abstract Address to avoid dependencies on the exact
156 * MAC address format.
158 * A default imlementation of GetMulticast is provided, but this
159 * method simply NS_ASSERTS. In the case of net devices that do not support
160 * multicast, clients are expected to test NetDevice::IsMulticast and avoid
161 * attempting to map multicast packets. Subclasses of NetDevice that do
162 * support multicasting are expected to override this method and provide an
163 * implementation appropriate to the particular device.
165 * \param multicastGroup The IP address for the multicast group destination
167 * \return The MAC multicast Address used to send packets to the provided
170 * \warning Calling this method is invalid if IsMulticast returns not true.
173 * \see NetDevice::IsMulticast
175 virtual Address GetMulticast (Ipv4Address multicastGroup) const = 0;
178 * \brief Get the MAC multicast address corresponding
179 * to the IPv6 address provided.
180 * \param addr IPv6 address
181 * \return the MAC multicast address
182 * \warning Calling this method is invalid if IsMulticast returns not true.
184 virtual Address GetMulticast (Ipv6Address addr) const = 0;
187 * \return value of m_isPointToPoint flag
189 virtual bool IsPointToPoint (void) const = 0;
191 * \param packet packet sent from above down to Network Device
192 * \param dest mac address of the destination (already resolved)
193 * \param protocolNumber identifies the type of payload contained in
194 * this packet. Used to call the right L3Protocol when the packet
197 * Called from higher layer to send packet into Network Device
198 * to the specified destination Address
200 * \return whether the Send operation succeeded
202 virtual bool Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber) = 0;
204 * \param packet packet sent from above down to Network Device
205 * \param source source mac address (so called "MAC spoofing")
206 * \param dest mac address of the destination (already resolved)
207 * \param protocolNumber identifies the type of payload contained in
208 * this packet. Used to call the right L3Protocol when the packet
211 * Called from higher layer to send packet into Network Device
212 * with the specified source and destination Addresses.
214 * \return whether the Send operation succeeded
216 virtual bool SendFrom(Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber) = 0;
218 * \returns the node base class which contains this network
221 * When a subclass needs to get access to the underlying node
222 * base class to print the nodeid for example, it can invoke
225 virtual Ptr<Node> GetNode (void) const = 0;
228 * \param node the node associated to this netdevice.
230 * This method is called from ns3::Node::AddDevice.
232 virtual void SetNode (Ptr<Node> node) = 0;
235 * \returns true if ARP is needed, false otherwise.
237 * Called by higher-layers to check if this NetDevice requires
240 virtual bool NeedsArp (void) const = 0;
244 * Packet types are used as they are in Linux. GCC name resolution on
245 * typedef enum {} PacketType is broken for the foreseeable future, so
246 * if you need to use ns-3 PacketType in a driver that also uses the
247 * Linux packet types you're hosed unless we define a shadow type,
252 PACKET_HOST = 1, /**< Packet addressed oo us */
253 NS3_PACKET_HOST = PACKET_HOST,
254 PACKET_BROADCAST, /**< Packet addressed to all */
255 NS3_PACKET_BROADCAST = PACKET_BROADCAST,
256 PACKET_MULTICAST, /**< Packet addressed to multicast group */
257 NS3_PACKET_MULTICAST = PACKET_MULTICAST,
258 PACKET_OTHERHOST, /**< Packet addressed to someone else */
259 NS3_PACKET_OTHERHOST = PACKET_OTHERHOST,
263 * \param device a pointer to the net device which is calling this callback
264 * \param packet the packet received
265 * \param protocol the 16 bit protocol number associated with this packet.
266 * This protocol number is expected to be the same protocol number
267 * given to the Send method by the user on the sender side.
268 * \param sender the address of the sender
269 * \returns true if the callback could handle the packet successfully, false
272 typedef Callback<bool,Ptr<NetDevice>,Ptr<const Packet>,uint16_t,const Address &> ReceiveCallback;
275 * \param cb callback to invoke whenever a packet has been received and must
276 * be forwarded to the higher layers.
279 virtual void SetReceiveCallback (ReceiveCallback cb) = 0;
283 * \param device a pointer to the net device which is calling this callback
284 * \param packet the packet received
285 * \param protocol the 16 bit protocol number associated with this packet.
286 * This protocol number is expected to be the same protocol number
287 * given to the Send method by the user on the sender side.
288 * \param sender the address of the sender
289 * \param receiver the address of the receiver
290 * \param packetType type of packet received (broadcast/multicast/unicast/otherhost)
291 * \returns true if the callback could handle the packet successfully, false
294 typedef Callback< bool, Ptr<NetDevice>, Ptr<const Packet>, uint16_t,
295 const Address &, const Address &, enum PacketType > PromiscReceiveCallback;
298 * \param cb callback to invoke whenever a packet has been received in promiscuous mode and must
299 * be forwarded to the higher layers.
301 * Enables netdevice promiscuous mode and sets the callback that
302 * will handle promiscuous mode packets. Note, promiscuous mode
303 * packets means _all_ packets, including those packets that can be
304 * sensed by the netdevice but which are intended to be received by
307 virtual void SetPromiscReceiveCallback (PromiscReceiveCallback cb) = 0;
310 * \return true if this interface supports a bridging mode, false otherwise.
312 virtual bool SupportsSendFrom (void) const = 0;
318 #endif /* NET_DEVICE_H */