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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 * Modified by Emmanuelle Laprise to remove dependence on LLC headers
8 */
9#ifndef NET_DEVICE_H
10#define NET_DEVICE_H
11
12#include "address.h"
13#include "packet.h"
14
15#include "ns3/callback.h"
16#include "ns3/ipv4-address.h"
17#include "ns3/ipv6-address.h"
18#include "ns3/object.h"
19#include "ns3/ptr.h"
20
21#include <stdint.h>
22
23namespace ns3
24{
25
26class Node;
27class Channel;
28
29/**
30 * @ingroup network
31 * @defgroup netdevice Network Device
32 */
33
34/**
35 * @ingroup netdevice
36 *
37 * @brief Network layer to device interface
38 *
39 * This interface defines the API which the IP and ARP
40 * layers need to access to manage an instance of a network device
41 * layer. It currently does not support MAC-level
42 * multicast but this should not be too hard to add by adding
43 * extra methods to register MAC multicast addresses to
44 * filter out unwanted packets before handing them to the
45 * higher layers.
46 *
47 * In Linux, this interface is analogous to the interface
48 * just above dev_queue_xmit() (i.e., IP packet is fully
49 * constructed with destination MAC address already selected).
50 *
51 * If you want to write a new MAC layer, you need to subclass
52 * this base class and implement your own version of the
53 * pure virtual methods in this class.
54 *
55 * This class was designed to hide as many MAC-level details as
56 * possible from the perspective of layer 3 to allow a single layer 3
57 * to work with any kind of MAC layer. Specifically, this class
58 * encapsulates the specific format of MAC addresses used by a
59 * device such that the layer 3 does not need any modification
60 * to handle new address formats. This means obviously that the
61 * NetDevice class must know about the address format of all potential
62 * layer 3 protocols through its GetMulticast methods: the current
63 * API has been optimized to make it easy to add new MAC protocols,
64 * not to add new layer 3 protocols.
65 *
66 * Devices aiming to support flow control and dynamic queue limits must perform
67 * the following operations:
68 * - in the NotifyNewAggregate method
69 * + cache the pointer to the netdevice queue interface aggregated to the
70 * device
71 * + set the select queue callback through the netdevice queue interface,
72 * if the device is multi-queue
73 * - anytime before initialization
74 * + set the number of device transmission queues (and optionally create them)
75 * through the netdevice queue interface, if the device is multi-queue
76 * - when the device queues have been created, invoke
77 * NetDeviceQueueInterface::ConnectQueueTraces, which
78 * + connects the Enqueue traced callback of the device queues to the
79 * PacketEnqueued static method of the NetDeviceQueue class
80 * + connects the Dequeue and DropAfterDequeue traced callback of the device
81 * queues to the PacketDequeued static method of the NetDeviceQueue
82 * class
83 * + connects the DropBeforeEnqueue traced callback of the device queues to
84 * the PacketDiscarded static method of the NetDeviceQueue class
85 */
86class NetDevice : public Object
87{
88 public:
89 /**
90 * @brief Get the type ID.
91 * @return the object TypeId
92 */
93 static TypeId GetTypeId();
94 ~NetDevice() override;
95
96 /**
97 * @param index ifIndex of the device
98 */
99 virtual void SetIfIndex(const uint32_t index) = 0;
100 /**
101 * @return index ifIndex of the device
102 */
103 virtual uint32_t GetIfIndex() const = 0;
104
105 /**
106 * @return the channel this NetDevice is connected to. The value
107 * returned can be zero if the NetDevice is not yet connected
108 * to any channel or if the underlying NetDevice has no
109 * concept of a channel. i.e., callers _must_ check for zero
110 * and be ready to handle it.
111 */
112 virtual Ptr<Channel> GetChannel() const = 0;
113
114 /**
115 * Set the address of this interface
116 * @param address address to set
117 */
118 virtual void SetAddress(Address address) = 0;
119
120 /**
121 * @return the current Address of this interface.
122 */
123 virtual Address GetAddress() const = 0;
124
125 /**
126 * Get the current address used to communicate with a remote.
127 * This method can be overridden by devices that have multiple addresses and that need to select
128 * from among them based on the remote address provided. The default implementation of this
129 * method will call GetAddress().
130 *
131 * @param remoteAddr the address of the remote.
132 * @return the current Address of this interface.
133 */
134 virtual Address GetAddressFor(const Address& remoteAddr) const;
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:114
Callback template class.
Definition callback.h:428
Abstract Channel Base Class.
Definition channel.h:34
Ipv4 addresses are stored in host order in this class.
Describes an IPv6 address.
Network layer to device interface.
Definition net-device.h:87
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:21
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:27
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 Address GetAddressFor(const Address &remoteAddr) const
Get the current address used to communicate with a remote.
Definition net-device.cc:33
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 network Node.
Definition node.h:46
Object()
Caller graph was not generated because of its size.
Definition object.cc:93
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
a unique identifier for an interface.
Definition type-id.h:50
Every class exported by the ns3 library is enclosed in the ns3 namespace.