src/devices/emu/emu-net-device.cc
changeset 3847 784c45b27326
parent 3842 545ddf9398ed
child 3850 03ebd59f4d06
equal deleted inserted replaced
3846:265004d6dc15 3847:784c45b27326
   151   NS_LOG_FUNCTION_NOARGS ();
   151   NS_LOG_FUNCTION_NOARGS ();
   152 
   152 
   153   //
   153   //
   154   // Spin up the emu net device and start receiving packets.
   154   // Spin up the emu net device and start receiving packets.
   155   //
   155   //
   156   NS_ASSERT_MSG (m_sock == -1, "EmuNetDevice::StartDevice(): Device is already started");
   156   if (m_sock != -1)
       
   157     {
       
   158       NS_FATAL_ERROR ("EmuNetDevice::StartDevice(): Device is already started");
       
   159     }
   157 
   160 
   158   NS_LOG_LOGIC ("Creating socket");
   161   NS_LOG_LOGIC ("Creating socket");
   159   //
   162   //
   160   // Call out to a separate process running as suid root in order to get a raw 
   163   // Call out to a separate process running as suid root in order to get a raw 
   161   // socket.  We do this to avoid having the entire simulation running as root.
   164   // socket.  We do this to avoid having the entire simulation running as root.
   170   bzero (&ifr, sizeof(ifr));
   173   bzero (&ifr, sizeof(ifr));
   171   strncpy ((char *)ifr.ifr_name, m_deviceName.c_str (), IFNAMSIZ);
   174   strncpy ((char *)ifr.ifr_name, m_deviceName.c_str (), IFNAMSIZ);
   172 
   175 
   173   NS_LOG_LOGIC ("Getting interface index");
   176   NS_LOG_LOGIC ("Getting interface index");
   174   int32_t rc = ioctl (m_sock, SIOCGIFINDEX, &ifr);
   177   int32_t rc = ioctl (m_sock, SIOCGIFINDEX, &ifr);
   175   NS_ASSERT_MSG (rc != -1, "EmuNetDevice::StartDevice(): Can't get interface index");
   178   if (rc == -1)
       
   179     {
       
   180       NS_FATAL_ERROR ("EmuNetDevice::StartDevice(): Can't get interface index");
       
   181     }
   176 
   182 
   177   //
   183   //
   178   // Save the real interface index for later calls to sendto
   184   // Save the real interface index for later calls to sendto
   179   //
   185   //
   180   m_sll_ifindex = ifr.ifr_ifindex;
   186   m_sll_ifindex = ifr.ifr_ifindex;
   190   ll.sll_protocol = htons(ETH_P_ALL); 
   196   ll.sll_protocol = htons(ETH_P_ALL); 
   191 
   197 
   192   NS_LOG_LOGIC ("Binding socket to interface");
   198   NS_LOG_LOGIC ("Binding socket to interface");
   193 
   199 
   194   rc = bind (m_sock, (struct sockaddr *)&ll, sizeof (ll));
   200   rc = bind (m_sock, (struct sockaddr *)&ll, sizeof (ll));
   195   NS_ASSERT_MSG (rc != -1, "EmuNetDevice::StartDevice(): Can't bind to specified interface");
   201   if (rc == -1)
       
   202     {
       
   203       NS_FATAL_ERROR ("EmuNetDevice::StartDevice(): Can't bind to specified interface");
       
   204     }
   196 
   205 
   197   rc = ioctl(m_sock, SIOCGIFFLAGS, &ifr);
   206   rc = ioctl(m_sock, SIOCGIFFLAGS, &ifr);
   198   NS_ASSERT_MSG (rc != -1, "EmuNetDevice::StartDevice(): Can't get interface flags");
   207   if (rc == -1)
       
   208     {
       
   209       NS_FATAL_ERROR ("EmuNetDevice::StartDevice(): Can't get interface flags");
       
   210     }
   199   
   211   
   200   //
   212   //
   201   // This device only works if the underlying interface is up in promiscuous 
   213   // This device only works if the underlying interface is up in promiscuous 
   202   // mode.  We could have turned it on in the socket creator, but the situation
   214   // mode.  We could have turned it on in the socket creator, but the situation
   203   // is that we expect these devices to be used in conjunction with virtual 
   215   // is that we expect these devices to be used in conjunction with virtual 
   214     }
   226     }
   215 
   227 
   216   //
   228   //
   217   // Now spin up a read thread to read packets.
   229   // Now spin up a read thread to read packets.
   218   //
   230   //
   219   NS_ASSERT_MSG (m_readThread == 0, "EmuNetDevice::StartDevice(): Receive thread is already running");
   231   if (m_readThread != 0)
       
   232     {
       
   233       NS_FATAL_ERROR ("EmuNetDevice::StartDevice(): Receive thread is already running");
       
   234     }
   220 
   235 
   221   NS_LOG_LOGIC ("Spinning up read thread");
   236   NS_LOG_LOGIC ("Spinning up read thread");
   222 
   237 
   223   m_readThread = Create<SystemThread> (MakeCallback (&EmuNetDevice::ReadThread, this));
   238   m_readThread = Create<SystemThread> (MakeCallback (&EmuNetDevice::ReadThread, this));
   224   m_readThread->Start ();
   239   m_readThread->Start ();
   563 
   578 
   564   for (;;) 
   579   for (;;) 
   565     {
   580     {
   566       uint32_t bufferSize = 65536;
   581       uint32_t bufferSize = 65536;
   567       uint8_t *buf = (uint8_t *)malloc (bufferSize);
   582       uint8_t *buf = (uint8_t *)malloc (bufferSize);
   568       NS_ASSERT_MSG (buf, "EmuNetDevice::ReadThread(): malloc packet buffer failed");
   583       if (buf == 0)
       
   584         {
       
   585           NS_FATAL_ERROR ("EmuNetDevice::ReadThread(): malloc packet buffer failed");
       
   586         }
       
   587 
   569       NS_LOG_LOGIC ("Calling recvfrom");
   588       NS_LOG_LOGIC ("Calling recvfrom");
   570       len = recvfrom (m_sock, buf, bufferSize, 0, (struct sockaddr *)&addr, &addrSize);
   589       len = recvfrom (m_sock, buf, bufferSize, 0, (struct sockaddr *)&addr, &addrSize);
   571 
   590 
   572       if (len == -1)
   591       if (len == -1)
   573         {
   592         {
   634     bzero (&ifr, sizeof(ifr));
   653     bzero (&ifr, sizeof(ifr));
   635     strncpy ((char *)ifr.ifr_name, m_deviceName.c_str (), IFNAMSIZ);
   654     strncpy ((char *)ifr.ifr_name, m_deviceName.c_str (), IFNAMSIZ);
   636 
   655 
   637     NS_LOG_LOGIC ("Getting MAC address");
   656     NS_LOG_LOGIC ("Getting MAC address");
   638     int32_t rc = ioctl (m_sock, SIOCGIFHWADDR, &ifr);
   657     int32_t rc = ioctl (m_sock, SIOCGIFHWADDR, &ifr);
   639     NS_ASSERT_MSG (rc != -1, "EmuNetDevice::SendFrom(): Can't get MAC address");
   658     if (rc == -1)
       
   659       {
       
   660         NS_FATAL_ERROR ("EmuNetDevice::SendFrom(): Can't get MAC address");
       
   661       }
   640 
   662 
   641     std::ostringstream oss;
   663     std::ostringstream oss;
   642     oss << std::hex <<
   664     oss << std::hex <<
   643       (ifr.ifr_hwaddr.sa_data[0] & 0xff) << ":" <<
   665       (ifr.ifr_hwaddr.sa_data[0] & 0xff) << ":" <<
   644       (ifr.ifr_hwaddr.sa_data[1] & 0xff) << ":" <<
   666       (ifr.ifr_hwaddr.sa_data[1] & 0xff) << ":" <<
   692 
   714 
   693 void 
   715 void 
   694 EmuNetDevice::SetDataRate(DataRate bps)
   716 EmuNetDevice::SetDataRate(DataRate bps)
   695 {
   717 {
   696   NS_LOG_FUNCTION (this << bps);
   718   NS_LOG_FUNCTION (this << bps);
   697   NS_ASSERT_MSG (false, "EmuNetDevice::SetDataRate():  Unable."); 
   719   NS_FATAL_ERROR ("EmuNetDevice::SetDataRate():  Unable."); 
   698 }
   720 }
   699 
   721 
   700 void
   722 void
   701 EmuNetDevice::SetQueue (Ptr<Queue> q)
   723 EmuNetDevice::SetQueue (Ptr<Queue> q)
   702 {
   724 {
   746 }
   768 }
   747 
   769 
   748 Ptr<Channel> 
   770 Ptr<Channel> 
   749 EmuNetDevice::GetChannel (void) const
   771 EmuNetDevice::GetChannel (void) const
   750 {
   772 {
   751   NS_ASSERT_MSG (false, "EmuNetDevice::GetChannel():  Unable."); 
   773   NS_FATAL_ERROR ("EmuNetDevice::GetChannel():  Unable."); 
   752   return 0;
   774   return 0;
   753 }
   775 }
   754 
   776 
   755 void 
   777 void 
   756 EmuNetDevice::SetAddress (Mac48Address addr)
   778 EmuNetDevice::SetAddress (Mac48Address addr)
   767 }
   789 }
   768 
   790 
   769 bool 
   791 bool 
   770 EmuNetDevice::SetMtu (const uint16_t mtu)
   792 EmuNetDevice::SetMtu (const uint16_t mtu)
   771 {
   793 {
   772   NS_ASSERT_MSG (false, "EmuNetDevice::SetMtu():  Unable."); 
   794   NS_FATAL_ERROR ("EmuNetDevice::SetMtu():  Unable."); 
   773   return false;
   795   return false;
   774 }
   796 }
   775 
   797 
   776 uint16_t 
   798 uint16_t 
   777 EmuNetDevice::GetMtu (void) const
   799 EmuNetDevice::GetMtu (void) const
   780   bzero (&ifr, sizeof (ifr));
   802   bzero (&ifr, sizeof (ifr));
   781   strcpy(ifr.ifr_name, m_deviceName.c_str ());
   803   strcpy(ifr.ifr_name, m_deviceName.c_str ());
   782 
   804 
   783   int32_t fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
   805   int32_t fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
   784 
   806 
       
   807 
   785   int32_t rc = ioctl(fd, SIOCGIFMTU, &ifr);
   808   int32_t rc = ioctl(fd, SIOCGIFMTU, &ifr);
   786   NS_ASSERT_MSG (rc != -1, "EmuNetDevice::GetMtu(): Can't ioctl SIOCGIFMTU");
   809   if (rc == -1)
       
   810     {
       
   811       NS_FATAL_ERROR ("EmuNetDevice::GetMtu(): Can't ioctl SIOCGIFMTU");
       
   812     }
   787 
   813 
   788   close (fd);
   814   close (fd);
   789 
   815 
   790   return ifr.ifr_mtu;
   816   return ifr.ifr_mtu;
   791 }
   817 }
   844 }
   870 }
   845 
   871 
   846 void
   872 void
   847 EmuNetDevice::SetPromiscReceiveCallback (PromiscReceiveCallback cb)
   873 EmuNetDevice::SetPromiscReceiveCallback (PromiscReceiveCallback cb)
   848 {
   874 {
   849   NS_ASSERT_MSG (false, "EmuNetDevice::SetPromiscReceiveCallback(): Not implemented");
   875   NS_FATAL_ERROR ("EmuNetDevice::SetPromiscReceiveCallback(): Not implemented");
   850 }
   876 }
   851 
   877 
   852   bool 
   878   bool 
   853 EmuNetDevice::SupportsSendFrom () const
   879 EmuNetDevice::SupportsSendFrom () const
   854 {
   880 {