src/devices/emu/emu-net-device.h
author vincent@clarinet.u-strasbg.fr
Fri Nov 07 11:36:15 2008 -0800 (2008-11-07)
changeset 3852 9cf7ad0cac85
parent 3843 ca2159d54d75
child 3936 e525995ce5dc
permissions -rw-r--r--
Initial IPv6 capability
craigdo@3827
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
craigdo@3827
     2
/*
craigdo@3827
     3
 * Copyright (c) 2008 University of Washington
craigdo@3827
     4
 *
craigdo@3827
     5
 * This program is free software; you can redistribute it and/or modify
craigdo@3827
     6
 * it under the terms of the GNU General Public License version 2 as
craigdo@3827
     7
 * published by the Free Software Foundation;
craigdo@3827
     8
 *
craigdo@3827
     9
 * This program is distributed in the hope that it will be useful,
craigdo@3827
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
craigdo@3827
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
craigdo@3827
    12
 * GNU General Public License for more details.
craigdo@3827
    13
 *
craigdo@3827
    14
 * You should have received a copy of the GNU General Public License
craigdo@3827
    15
 * along with this program; if not, write to the Free Software
craigdo@3827
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
craigdo@3827
    17
 */
craigdo@3827
    18
craigdo@3830
    19
#ifndef EMU_NET_DEVICE_H
craigdo@3830
    20
#define EMU_NET_DEVICE_H
craigdo@3827
    21
craigdo@3827
    22
#include <string.h>
craigdo@3827
    23
#include "ns3/address.h"
craigdo@3827
    24
#include "ns3/net-device.h"
craigdo@3827
    25
#include "ns3/node.h"
craigdo@3827
    26
#include "ns3/callback.h"
craigdo@3827
    27
#include "ns3/packet.h"
craigdo@3827
    28
#include "ns3/traced-callback.h"
craigdo@3827
    29
#include "ns3/event-id.h"
craigdo@3827
    30
#include "ns3/nstime.h"
craigdo@3827
    31
#include "ns3/data-rate.h"
craigdo@3827
    32
#include "ns3/ptr.h"
craigdo@3827
    33
#include "ns3/mac48-address.h"
craigdo@3827
    34
#include "ns3/system-thread.h"
craigdo@3827
    35
craigdo@3827
    36
namespace ns3 {
craigdo@3827
    37
craigdo@3827
    38
class Queue;
craigdo@3827
    39
craigdo@3827
    40
/**
craigdo@3830
    41
 * \class EmuNetDevice
craigdo@3830
    42
 * \brief A Device for an Emu Network Link.
craigdo@3827
    43
 */
craigdo@3830
    44
class EmuNetDevice : public NetDevice 
craigdo@3827
    45
{
craigdo@3827
    46
public:
craigdo@3827
    47
  static TypeId GetTypeId (void);
craigdo@3827
    48
craigdo@3827
    49
  /**
craigdo@3830
    50
   * Construct a EmuNetDevice
craigdo@3827
    51
   *
craigdo@3830
    52
   * This is the constructor for the EmuNetDevice.  It takes as a
craigdo@3827
    53
   */
craigdo@3830
    54
  EmuNetDevice ();
craigdo@3827
    55
craigdo@3827
    56
  /**
craigdo@3830
    57
   * Destroy a EmuNetDevice
craigdo@3827
    58
   *
craigdo@3830
    59
   * This is the destructor for the EmuNetDevice.
craigdo@3827
    60
   */
craigdo@3830
    61
  virtual ~EmuNetDevice ();
craigdo@3827
    62
craigdo@3827
    63
  /**
craigdo@3827
    64
   * Set the Data Rate used for transmission of packets.  
craigdo@3827
    65
   *
craigdo@3827
    66
   * @see Attach ()
craigdo@3827
    67
   * @param bps the data rate at which this object operates
craigdo@3827
    68
   */
craigdo@3827
    69
  void SetDataRate (DataRate bps);
craigdo@3827
    70
craigdo@3827
    71
  /**
craigdo@3827
    72
   * Set a start time for the device.
craigdo@3827
    73
   *
craigdo@3827
    74
   * @param tStart the start time
craigdo@3827
    75
   */
craigdo@3827
    76
  void Start (Time tStart);
craigdo@3827
    77
craigdo@3827
    78
  /**
craigdo@3827
    79
   * Set a stop time for the device.
craigdo@3827
    80
   *
craigdo@3827
    81
   * @param tStop the stop time
craigdo@3827
    82
   */
craigdo@3827
    83
  void Stop (Time tStop);
craigdo@3827
    84
craigdo@3827
    85
  /**
craigdo@3830
    86
   * Attach a queue to the EmuNetDevice.
craigdo@3827
    87
   *
craigdo@3830
    88
   * The EmuNetDevice "owns" a queue that implements a queueing 
craigdo@3827
    89
   * method such as DropTail or RED.  
craigdo@3827
    90
   *
craigdo@3827
    91
   * @see Queue
craigdo@3827
    92
   * @see DropTailQueue
craigdo@3827
    93
   * @param queue Ptr to the new queue.
craigdo@3827
    94
   */
craigdo@3827
    95
  void SetQueue (Ptr<Queue> queue);
craigdo@3827
    96
craigdo@3827
    97
  /**
craigdo@3827
    98
   * Assign a MAC address to this device.
craigdo@3827
    99
   *
craigdo@3827
   100
   * @see Mac48Address
craigdo@3827
   101
   * @param addr The new address.
craigdo@3827
   102
   */
craigdo@3827
   103
  void SetAddress (Mac48Address addr);
craigdo@3827
   104
craigdo@3827
   105
//
craigdo@3827
   106
// Pure virtual methods inherited from NetDevice we must implement.
craigdo@3827
   107
//
craigdo@3827
   108
  virtual void SetName(const std::string name);
craigdo@3827
   109
  virtual std::string GetName(void) const;
craigdo@3827
   110
craigdo@3827
   111
  virtual void SetIfIndex(const uint32_t index);
craigdo@3827
   112
  virtual uint32_t GetIfIndex(void) const;
craigdo@3827
   113
craigdo@3827
   114
  virtual Ptr<Channel> GetChannel (void) const;
craigdo@3827
   115
  virtual Address GetAddress (void) const;
craigdo@3827
   116
craigdo@3827
   117
  virtual bool SetMtu (const uint16_t mtu);
craigdo@3827
   118
  virtual uint16_t GetMtu (void) const;
craigdo@3827
   119
craigdo@3827
   120
  virtual bool IsLinkUp (void) const;
craigdo@3827
   121
craigdo@3827
   122
  virtual void SetLinkChangeCallback (Callback<void> callback);
craigdo@3827
   123
craigdo@3827
   124
  virtual bool IsBroadcast (void) const;
craigdo@3827
   125
  virtual Address GetBroadcast (void) const;
craigdo@3827
   126
craigdo@3827
   127
  virtual bool IsMulticast (void) const;
craigdo@3827
   128
vincent@3842
   129
  /**
vincent@3842
   130
   * \brief Make and return a MAC multicast address using the provided
vincent@3842
   131
   *        multicast group
vincent@3842
   132
   *
vincent@3842
   133
   * RFC 1112 says that an Ipv4 host group address is mapped to an Ethernet 
vincent@3842
   134
   * multicast address by placing the low-order 23-bits of the IP address into 
vincent@3842
   135
   * the low-order 23 bits of the Ethernet multicast address 
vincent@3842
   136
   * 01-00-5E-00-00-00 (hex).
vincent@3842
   137
   *
vincent@3842
   138
   * This method performs the multicast address creation function appropriate
vincent@3842
   139
   * to an EUI-48-based CSMA device.  This MAC address is encapsulated in an
vincent@3842
   140
   *  abstract Address to avoid dependencies on the exact address format.
vincent@3842
   141
   *
vincent@3842
   142
   * \param multicastGroup The IP address for the multicast group destination
vincent@3842
   143
   * of the packet.
vincent@3842
   144
   * \return The MAC multicast Address used to send packets to the provided
vincent@3842
   145
   * multicast group.
vincent@3842
   146
   *
vincent@3842
   147
   * \see Ipv4Address
vincent@3842
   148
   * \see Mac48Address
vincent@3842
   149
   * \see Address
vincent@3842
   150
   */
vincent@3842
   151
  virtual Address GetMulticast (Ipv4Address multicastGroup) const;
vincent@3842
   152
vincent@3842
   153
  /**
vincent@3852
   154
   * \brief Get the MAC multicast address corresponding
vincent@3852
   155
   * to the IPv6 address provided.
vincent@3852
   156
   * \param addr IPv6 address
vincent@3852
   157
   * \return the MAC multicast address
vincent@3852
   158
   * \warning Calling this method is invalid if IsMulticast returns not true.
vincent@3852
   159
   */
vincent@3852
   160
  virtual Address GetMulticast (Ipv6Address addr) const;
vincent@3852
   161
vincent@3852
   162
  /**
vincent@3842
   163
   * Is this a point to point link?
vincent@3842
   164
   * \returns false.
vincent@3842
   165
   */
craigdo@3827
   166
  virtual bool IsPointToPoint (void) const;
craigdo@3827
   167
craigdo@3827
   168
  virtual bool Send(Ptr<Packet> packet, const Address &dest, uint16_t protocolNumber);
craigdo@3827
   169
craigdo@3827
   170
  virtual bool SendFrom(Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber);
craigdo@3827
   171
craigdo@3827
   172
  virtual Ptr<Node> GetNode (void) const;
craigdo@3827
   173
  virtual void SetNode (Ptr<Node> node);
craigdo@3827
   174
craigdo@3827
   175
  virtual bool NeedsArp (void) const;
craigdo@3827
   176
craigdo@3827
   177
  virtual void SetReceiveCallback (NetDevice::ReceiveCallback cb);
craigdo@3827
   178
  virtual void SetPromiscReceiveCallback (PromiscReceiveCallback cb);
craigdo@3827
   179
craigdo@3827
   180
  virtual bool SupportsSendFrom (void) const;
craigdo@3827
   181
craigdo@3827
   182
private:
craigdo@3827
   183
craigdo@3827
   184
  virtual void DoDispose (void);
craigdo@3827
   185
craigdo@3827
   186
  /**
craigdo@3830
   187
   * Call out to a separate process running as suid root in order to get a raw 
craigdo@3830
   188
   * socket.  We do this to avoid having the entire simulation running as root.
craigdo@3830
   189
   * If this method returns, we'll have a raw socket waiting for us in m_sock.
craigdo@3830
   190
   */
craigdo@3830
   191
  void CreateSocket (void);
craigdo@3830
   192
craigdo@3830
   193
  /**
craigdo@3830
   194
   * Figure out where the raw socket creation process lives on the system.
craigdo@3830
   195
   */
craigdo@3830
   196
  std::string FindCreator (void);
craigdo@3830
   197
craigdo@3830
   198
  /**
craigdo@3827
   199
   * Get a copy of the attached Queue.
craigdo@3827
   200
   *
craigdo@3827
   201
   * This method is provided for any derived class that may need to get
craigdo@3827
   202
   * direct access to the underlying queue.
craigdo@3827
   203
   *
craigdo@3827
   204
   * @returns Ptr to the queue.
craigdo@3827
   205
   */
craigdo@3827
   206
  Ptr<Queue> GetQueue(void) const; 
craigdo@3827
   207
craigdo@3827
   208
  /**
craigdo@3827
   209
   * Spin up the device
craigdo@3827
   210
   */
craigdo@3827
   211
  void StartDevice (void);
craigdo@3827
   212
craigdo@3827
   213
  /**
craigdo@3827
   214
   * Tear down the device
craigdo@3827
   215
   */
craigdo@3827
   216
  void StopDevice (void);
craigdo@3827
   217
craigdo@3827
   218
  /**
craigdo@3827
   219
   * Loop to read and process packets
craigdo@3827
   220
   */
craigdo@3827
   221
  void ReadThread (void);
craigdo@3827
   222
craigdo@3827
   223
  /**
craigdo@3827
   224
   * Method to handle received packets.  Synchronized with simulator via ScheduleNow from ReadThread.
craigdo@3827
   225
   */
craigdo@3827
   226
  void ForwardUp (uint8_t *buf, uint32_t len);
craigdo@3827
   227
craigdo@3827
   228
  /**
craigdo@3827
   229
   * Adds the necessary headers and trailers to a packet of data in order to
craigdo@3827
   230
   * respect the protocol implemented by the agent.
craigdo@3827
   231
   */
craigdo@3827
   232
  void AddHeader(Ptr<Packet> p, uint16_t protocolNumber);
craigdo@3827
   233
craigdo@3827
   234
  /**
craigdo@3827
   235
   * Removes, from a packet of data, all headers and trailers that
craigdo@3827
   236
   * relate to the protocol implemented by the agent
craigdo@3827
   237
   * \return Returns true if the packet should be forwarded up the
craigdo@3827
   238
   * protocol stack.
craigdo@3827
   239
   */
craigdo@3827
   240
  bool ProcessHeader(Ptr<Packet> p, uint16_t& param);
craigdo@3827
   241
craigdo@3827
   242
  /**
craigdo@3827
   243
   * Start Sending a Packet Down the Wire.
craigdo@3827
   244
   *
craigdo@3827
   245
   * @returns true if success, false on failure
craigdo@3827
   246
   */
craigdo@3827
   247
  bool TransmitStart (Ptr<Packet> p);
craigdo@3827
   248
craigdo@3827
   249
  void NotifyLinkUp (void);
craigdo@3827
   250
craigdo@3827
   251
  /**
craigdo@3830
   252
   * The Queue which this EmuNetDevice uses as a packet source.
craigdo@3830
   253
   * Management of this Queue has been delegated to the EmuNetDevice
craigdo@3827
   254
   * and it has the responsibility for deletion.
craigdo@3827
   255
   * @see class Queue
craigdo@3827
   256
   * @see class DropTailQueue
craigdo@3827
   257
   */
craigdo@3827
   258
  Ptr<Queue> m_queue;
craigdo@3827
   259
craigdo@3827
   260
  /**
craigdo@3827
   261
   * The trace source for the packet reception events that the device can
craigdo@3827
   262
   * fire.
craigdo@3827
   263
   *
craigdo@3827
   264
   * @see class CallBackTraceSource
craigdo@3827
   265
   */
craigdo@3827
   266
  TracedCallback<Ptr<const Packet> > m_rxTrace;
craigdo@3827
   267
craigdo@3827
   268
  /**
craigdo@3827
   269
   * The trace source for the packet drop events that the device can
craigdo@3827
   270
   * fire.
craigdo@3827
   271
   *
craigdo@3827
   272
   * @see class CallBackTraceSource
craigdo@3827
   273
   */
craigdo@3827
   274
  TracedCallback<Ptr<const Packet> > m_dropTrace;
craigdo@3827
   275
craigdo@3827
   276
  /**
craigdo@3827
   277
   * Time to start spinning up the device
craigdo@3827
   278
   */
craigdo@3827
   279
  Time m_tStart;
craigdo@3827
   280
craigdo@3827
   281
  /**
craigdo@3827
   282
   * Time to start tearing down the device
craigdo@3827
   283
   */
craigdo@3827
   284
  Time m_tStop;
craigdo@3827
   285
craigdo@3827
   286
  EventId m_startEvent;
craigdo@3827
   287
  EventId m_stopEvent;
craigdo@3827
   288
craigdo@3827
   289
  int32_t m_sock;
craigdo@3827
   290
craigdo@3827
   291
  Ptr<SystemThread> m_readThread;
craigdo@3827
   292
craigdo@3827
   293
  /**
craigdo@3827
   294
   * The Node to which this device is attached.
craigdo@3827
   295
   */
craigdo@3827
   296
  Ptr<Node> m_node;
craigdo@3827
   297
craigdo@3827
   298
  /**
craigdo@3827
   299
   * The MAC address which has been assigned to this device.
craigdo@3827
   300
   */
craigdo@3827
   301
  Mac48Address m_address;
craigdo@3827
   302
craigdo@3827
   303
  /**
craigdo@3827
   304
   * The callback used to notify higher layers that a packet has been received.
craigdo@3827
   305
   */
craigdo@3827
   306
  NetDevice::ReceiveCallback m_rxCallback;
craigdo@3827
   307
craigdo@3827
   308
  /**
craigdo@3827
   309
   * The callback used to notify higher layers that a packet has been received in promiscuous mode.
craigdo@3827
   310
   */
craigdo@3827
   311
  NetDevice::PromiscReceiveCallback m_promiscRxCallback;
craigdo@3827
   312
craigdo@3827
   313
  /**
craigdo@3827
   314
   * The ns-3 interface index (in the sense of net device index) that has been assigned to this network device.
craigdo@3827
   315
   */
craigdo@3827
   316
  uint32_t m_ifIndex;
craigdo@3827
   317
craigdo@3827
   318
  /**
craigdo@3827
   319
   * The Unix interface index that we got from the system and which corresponds to the interface (e.g., "eth1")
craigdo@3827
   320
   * we are using to talk to the network.  Valid when m_sock is valid.
craigdo@3827
   321
   */
craigdo@3827
   322
  int32_t m_sll_ifindex;
craigdo@3827
   323
craigdo@3827
   324
  /**
craigdo@3827
   325
   * The human readable name of this device.
craigdo@3827
   326
   */
craigdo@3827
   327
  std::string m_name;
craigdo@3827
   328
craigdo@3827
   329
  /**
craigdo@3827
   330
   * Flag indicating whether or not the link is up.  In this case,
craigdo@3827
   331
   * whether or not the device is connected to a channel.
craigdo@3827
   332
   */
craigdo@3827
   333
  bool m_linkUp;
craigdo@3827
   334
craigdo@3827
   335
  /**
craigdo@3827
   336
   * Callback to fire if the link changes state (up or down).
craigdo@3827
   337
   */
craigdo@3827
   338
  Callback<void> m_linkChangeCallback;
craigdo@3827
   339
craigdo@3827
   340
  /**
craigdo@3827
   341
   * The unix/linux name of the underlying device (e.g., eth0)
craigdo@3827
   342
   */
craigdo@3827
   343
  std::string m_deviceName;
craigdo@3827
   344
};
craigdo@3827
   345
craigdo@3827
   346
} // namespace ns3
craigdo@3827
   347
craigdo@3830
   348
#endif // EMU_NET_DEVICE_H
craigdo@3827
   349