A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
net-device.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2005,2006 INRIA
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 * Modified by Emmanuelle Laprise to remove dependence on LLC headers
19 */
20#ifndef NET_DEVICE_H
21#define NET_DEVICE_H
22
23#include "address.h"
24#include "packet.h"
25
26#include "ns3/callback.h"
27#include "ns3/ipv4-address.h"
28#include "ns3/ipv6-address.h"
29#include "ns3/object.h"
30#include "ns3/ptr.h"
31
32#include <stdint.h>
33
34namespace ns3
35{
36
37class Node;
38class Channel;
39
40/**
41 * \ingroup network
42 * \defgroup netdevice Network Device
43 */
44
45/**
46 * \ingroup netdevice
47 *
48 * \brief Network layer to device interface
49 *
50 * This interface defines the API which the IP and ARP
51 * layers need to access to manage an instance of a network device
52 * layer. It currently does not support MAC-level
53 * multicast but this should not be too hard to add by adding
54 * extra methods to register MAC multicast addresses to
55 * filter out unwanted packets before handing them to the
56 * higher layers.
57 *
58 * In Linux, this interface is analogous to the interface
59 * just above dev_queue_xmit() (i.e., IP packet is fully
60 * constructed with destination MAC address already selected).
61 *
62 * If you want to write a new MAC layer, you need to subclass
63 * this base class and implement your own version of the
64 * pure virtual methods in this class.
65 *
66 * This class was designed to hide as many MAC-level details as
67 * possible from the perspective of layer 3 to allow a single layer 3
68 * to work with any kind of MAC layer. Specifically, this class
69 * encapsulates the specific format of MAC addresses used by a
70 * device such that the layer 3 does not need any modification
71 * to handle new address formats. This means obviously that the
72 * NetDevice class must know about the address format of all potential
73 * layer 3 protocols through its GetMulticast methods: the current
74 * API has been optimized to make it easy to add new MAC protocols,
75 * not to add new layer 3 protocols.
76 *
77 * Devices aiming to support flow control and dynamic queue limits must perform
78 * the following operations:
79 * - in the NotifyNewAggregate method
80 * + cache the pointer to the netdevice queue interface aggregated to the
81 * device
82 * + set the select queue callback through the netdevice queue interface,
83 * if the device is multi-queue
84 * - anytime before initialization
85 * + set the number of device transmission queues (and optionally create them)
86 * through the netdevice queue interface, if the device is multi-queue
87 * - when the device queues have been created, invoke
88 * NetDeviceQueueInterface::ConnectQueueTraces, which
89 * + connects the Enqueue traced callback of the device queues to the
90 * PacketEnqueued static method of the NetDeviceQueue class
91 * + connects the Dequeue and DropAfterDequeue traced callback of the device
92 * queues to the PacketDequeued static method of the NetDeviceQueue
93 * class
94 * + connects the DropBeforeEnqueue traced callback of the device queues to
95 * the PacketDiscarded static method of the NetDeviceQueue class
96 */
97class NetDevice : public Object
98{
99 public:
100 /**
101 * \brief Get the type ID.
102 * \return the object TypeId
103 */
104 static TypeId GetTypeId();
105 ~NetDevice() override;
106
107 /**
108 * \param index ifIndex of the device
109 */
110 virtual void SetIfIndex(const uint32_t index) = 0;
111 /**
112 * \return index ifIndex of the device
113 */
114 virtual uint32_t GetIfIndex() const = 0;
115
116 /**
117 * \return the channel this NetDevice is connected to. The value
118 * returned can be zero if the NetDevice is not yet connected
119 * to any channel or if the underlying NetDevice has no
120 * concept of a channel. i.e., callers _must_ check for zero
121 * and be ready to handle it.
122 */
123 virtual Ptr<Channel> GetChannel() const = 0;
124
125 /**
126 * Set the address of this interface
127 * \param address address to set
128 */
129 virtual void SetAddress(Address address) = 0;
130
131 /**
132 * \return the current Address of this interface.
133 */
134 virtual Address GetAddress() const = 0;
135
136 /**
137 * \param mtu MTU value, in bytes, to set for the device
138 * \return whether the MTU value was within legal bounds
139 *
140 * Override for default MTU defined on a per-type basis.
141 */
142 virtual bool SetMtu(const uint16_t mtu) = 0;
143 /**
144 * \return the link-level MTU in bytes for this interface.
145 *
146 * This value is typically used by the IP layer to perform
147 * IP fragmentation when needed.
148 */
149 virtual uint16_t GetMtu() const = 0;
150 /**
151 * \return true if link is up; false otherwise
152 */
153 virtual bool IsLinkUp() const = 0;
154 /**
155 * TracedCallback signature for link changed event.
156 */
157 typedef void (*LinkChangeTracedCallback)();
158 /**
159 * \param callback the callback to invoke
160 *
161 * Add a callback invoked whenever the link
162 * status changes to UP. This callback is typically used
163 * by the IP/ARP layer to flush the ARP cache and by IPv6 stack
164 * to flush NDISC cache whenever the link goes up.
165 */
166 virtual void AddLinkChangeCallback(Callback<void> callback) = 0;
167 /**
168 * \return true if this interface supports a broadcast address,
169 * false otherwise.
170 */
171 virtual bool IsBroadcast() const = 0;
172 /**
173 * \return the broadcast address supported by
174 * this netdevice.
175 *
176 * Calling this method is invalid if IsBroadcast returns
177 * not true.
178 */
179 virtual Address GetBroadcast() const = 0;
180
181 /**
182 * \return value of m_isMulticast flag
183 */
184 virtual bool IsMulticast() const = 0;
185
186 /**
187 * \brief Make and return a MAC multicast address using the provided
188 * multicast group
189 *
190 * \RFC{1112} says that an Ipv4 host group address is mapped to an Ethernet
191 * multicast address by placing the low-order 23-bits of the IP address into
192 * the low-order 23 bits of the Ethernet multicast address
193 * 01-00-5E-00-00-00 (hex). Similar RFCs exist for Ipv6 and Eui64 mappings.
194 * This method performs the multicast address creation function appropriate
195 * to the underlying MAC address of the device. This MAC address is
196 * encapsulated in an abstract Address to avoid dependencies on the exact
197 * MAC address format.
198 *
199 * In the case of net devices that do not support
200 * multicast, clients are expected to test NetDevice::IsMulticast and avoid
201 * attempting to map multicast packets. Subclasses of NetDevice that do
202 * support multicasting are expected to override this method and provide an
203 * implementation appropriate to the particular device.
204 *
205 * \param multicastGroup The IP address for the multicast group destination
206 * of the packet.
207 * \return The MAC multicast Address used to send packets to the provided
208 * multicast group.
209 *
210 * \warning Calling this method is invalid if IsMulticast returns not true.
211 * \see IsMulticast()
212 */
213 virtual Address GetMulticast(Ipv4Address multicastGroup) const = 0;
214
215 /**
216 * \brief Get the MAC multicast address corresponding
217 * to the IPv6 address provided.
218 * \param addr IPv6 address
219 * \return the MAC multicast address
220 * \warning Calling this method is invalid if IsMulticast returns not true.
221 */
222 virtual Address GetMulticast(Ipv6Address addr) const = 0;
223
224 /**
225 * \brief Return true if the net device is acting as a bridge.
226 *
227 * \return value of m_isBridge flag
228 */
229 virtual bool IsBridge() const = 0;
230
231 /**
232 * \brief Return true if the net device is on a point-to-point link.
233 *
234 * \return value of m_isPointToPoint flag
235 */
236 virtual bool IsPointToPoint() const = 0;
237 /**
238 * \param packet packet sent from above down to Network Device
239 * \param dest mac address of the destination (already resolved)
240 * \param protocolNumber identifies the type of payload contained in
241 * this packet. Used to call the right L3Protocol when the packet
242 * is received.
243 *
244 * Called from higher layer to send packet into Network Device
245 * to the specified destination Address
246 *
247 * \return whether the Send operation succeeded
248 */
249 virtual bool Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber) = 0;
250 /**
251 * \param packet packet sent from above down to Network Device
252 * \param source source mac address (so called "MAC spoofing")
253 * \param dest mac address of the destination (already resolved)
254 * \param protocolNumber identifies the type of payload contained in
255 * this packet. Used to call the right L3Protocol when the packet
256 * is received.
257 *
258 * Called from higher layer to send packet into Network Device
259 * with the specified source and destination Addresses.
260 *
261 * \return whether the Send operation succeeded
262 */
263 virtual bool SendFrom(Ptr<Packet> packet,
264 const Address& source,
265 const Address& dest,
266 uint16_t protocolNumber) = 0;
267 /**
268 * \returns the node base class which contains this network
269 * interface.
270 *
271 * When a subclass needs to get access to the underlying node
272 * base class to print the nodeid for example, it can invoke
273 * this method.
274 */
275 virtual Ptr<Node> GetNode() const = 0;
276
277 /**
278 * \param node the node associated to this netdevice.
279 *
280 * This method is called from ns3::Node::AddDevice.
281 */
282 virtual void SetNode(Ptr<Node> node) = 0;
283
284 /**
285 * \returns true if ARP is needed, false otherwise.
286 *
287 * Called by higher-layers to check if this NetDevice requires
288 * ARP to be used.
289 */
290 virtual bool NeedsArp() const = 0;
291
292 /**
293 * Packet types are used as they are in Linux. GCC name resolution on
294 * typedef enum {} PacketType is broken for the foreseeable future, so
295 * if you need to use ns-3 PacketType in a driver that also uses the
296 * Linux packet types you're hosed unless we define a shadow type,
297 * which we do here.
298 */
300 {
301 PACKET_HOST = 1, //!< Packet addressed to us
303 PACKET_BROADCAST, //!< Packet addressed to all
305 PACKET_MULTICAST, //!< Packet addressed to multicast group
307 PACKET_OTHERHOST, //!< Packet addressed to someone else
309 };
310
311 /**
312 * \param device a pointer to the net device which is calling this callback
313 * \param packet the packet received
314 * \param protocol the 16 bit protocol number associated with this packet.
315 * This protocol number is expected to be the same protocol number
316 * given to the Send method by the user on the sender side.
317 * \param sender the address of the sender
318 * \returns true if the callback could handle the packet successfully, false
319 * otherwise.
320 */
323
324 /**
325 * \param cb callback to invoke whenever a packet has been received and must
326 * be forwarded to the higher layers.
327 *
328 * Set the callback to be used to notify higher layers when a packet has been
329 * received.
330 */
332
333 /**
334 * \param device a pointer to the net device which is calling this callback
335 * \param packet the packet received
336 * \param protocol the 16 bit protocol number associated with this packet.
337 * This protocol number is expected to be the same protocol number
338 * given to the Send method by the user on the sender side.
339 * \param sender the address of the sender
340 * \param receiver the address of the receiver
341 * \param packetType type of packet received (broadcast/multicast/unicast/otherhost)
342 * \returns true if the callback could handle the packet successfully, false
343 * otherwise.
344 */
345 typedef Callback<bool,
348 uint16_t,
349 const Address&,
350 const Address&,
353
354 /**
355 * \param cb callback to invoke whenever a packet has been received in promiscuous mode and must
356 * be forwarded to the higher layers.
357 *
358 * Enables netdevice promiscuous mode and sets the callback that
359 * will handle promiscuous mode packets. Note, promiscuous mode
360 * packets means _all_ packets, including those packets that can be
361 * sensed by the netdevice but which are intended to be received by
362 * other hosts.
363 */
365
366 /**
367 * \return true if this interface supports a bridging mode, false otherwise.
368 */
369 virtual bool SupportsSendFrom() const = 0;
370};
371
372} // namespace ns3
373
374#endif /* NET_DEVICE_H */
a polymophic address class
Definition: address.h:101
Callback template class.
Definition: callback.h:438
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
Describes an IPv6 address.
Definition: ipv6-address.h:49
Network layer to device interface.
Definition: net-device.h:98
virtual bool SupportsSendFrom() const =0
Callback< bool, Ptr< NetDevice >, Ptr< const Packet >, uint16_t, const Address &, const Address &, PacketType > PromiscReceiveCallback
Definition: net-device.h:352
virtual uint32_t GetIfIndex() const =0
virtual bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)=0
static TypeId GetTypeId()
Get the type ID.
Definition: net-device.cc:32
virtual bool SetMtu(const uint16_t mtu)=0
virtual void SetIfIndex(const uint32_t index)=0
virtual uint16_t GetMtu() const =0
virtual void SetPromiscReceiveCallback(PromiscReceiveCallback cb)=0
virtual void SetNode(Ptr< Node > node)=0
virtual Address GetMulticast(Ipv6Address addr) const =0
Get the MAC multicast address corresponding to the IPv6 address provided.
void(* LinkChangeTracedCallback)()
TracedCallback signature for link changed event.
Definition: net-device.h:157
virtual bool IsMulticast() const =0
virtual Address GetBroadcast() const =0
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)=0
virtual bool NeedsArp() const =0
virtual Address GetAddress() const =0
virtual bool IsLinkUp() const =0
~NetDevice() override
Definition: net-device.cc:38
virtual bool IsBridge() const =0
Return true if the net device is acting as a bridge.
virtual Address GetMulticast(Ipv4Address multicastGroup) const =0
Make and return a MAC multicast address using the provided multicast group.
virtual bool IsBroadcast() const =0
virtual void AddLinkChangeCallback(Callback< void > callback)=0
virtual void SetAddress(Address address)=0
Set the address of this interface.
virtual Ptr< Channel > GetChannel() const =0
virtual void SetReceiveCallback(ReceiveCallback cb)=0
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:300
@ PACKET_HOST
Packet addressed to us.
Definition: net-device.h:301
@ PACKET_OTHERHOST
Packet addressed to someone else.
Definition: net-device.h:307
@ PACKET_BROADCAST
Packet addressed to all.
Definition: net-device.h:303
@ PACKET_MULTICAST
Packet addressed to multicast group.
Definition: net-device.h:305
Callback< bool, Ptr< NetDevice >, Ptr< const Packet >, uint16_t, const Address & > ReceiveCallback
Definition: net-device.h:322
virtual Ptr< Node > GetNode() const =0
virtual bool IsPointToPoint() const =0
Return true if the net device is on a point-to-point link.
A base class which provides memory management and object aggregation.
Definition: object.h:89
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
a unique identifier for an interface.
Definition: type-id.h:59
Every class exported by the ns3 library is enclosed in the ns3 namespace.