src/devices/csma/csma-net-device.cc
changeset 1866 e7dbcc4df546
parent 1504 36ecc970ba96
child 1870 67b3d2dea3d5
equal deleted inserted replaced
1865:829dc1815014 1866:e7dbcc4df546
   217   m_backoff.m_ceiling = ceiling;
   217   m_backoff.m_ceiling = ceiling;
   218   m_backoff.m_maxRetries = maxRetries;
   218   m_backoff.m_maxRetries = maxRetries;
   219 }
   219 }
   220 
   220 
   221 void 
   221 void 
   222 CsmaNetDevice::AddHeader (Packet& p, Mac48Address dest,
   222 CsmaNetDevice::AddHeader (Ptr<Packet> p, Mac48Address dest,
   223                             uint16_t protocolNumber)
   223                             uint16_t protocolNumber)
   224 {
   224 {
   225   NS_LOG_FUNCTION;
   225   NS_LOG_FUNCTION;
   226   if (m_encapMode == RAW)
   226   if (m_encapMode == RAW)
   227     {
   227     {
   235 
   235 
   236   uint16_t lengthType = 0;
   236   uint16_t lengthType = 0;
   237   switch (m_encapMode) 
   237   switch (m_encapMode) 
   238     {
   238     {
   239     case ETHERNET_V1:
   239     case ETHERNET_V1:
   240       lengthType = p.GetSize() + header.GetSerializedSize() + trailer.GetSerializedSize();
   240       lengthType = p->GetSize() + header.GetSerializedSize() + trailer.GetSerializedSize();
   241       break;
   241       break;
   242     case IP_ARP:
   242     case IP_ARP:
   243       lengthType = protocolNumber;
   243       lengthType = protocolNumber;
   244       break;
   244       break;
   245     case LLC: {
   245     case LLC: {
   246       LlcSnapHeader llc;
   246       LlcSnapHeader llc;
   247       llc.SetType (protocolNumber);
   247       llc.SetType (protocolNumber);
   248       p.AddHeader (llc);
   248       p->AddHeader (llc);
   249     } break;
   249     } break;
   250     case RAW:
   250     case RAW:
   251       NS_ASSERT (false);
   251       NS_ASSERT (false);
   252       break;
   252       break;
   253     }
   253     }
   254   header.SetLengthType (lengthType);
   254   header.SetLengthType (lengthType);
   255   p.AddHeader(header);
   255   p->AddHeader(header);
   256   trailer.CalcFcs(p);
   256   trailer.CalcFcs(p);
   257   p.AddTrailer(trailer);
   257   p->AddTrailer(trailer);
   258 }
   258 }
   259 
   259 
   260 bool 
   260 bool 
   261 CsmaNetDevice::ProcessHeader (Packet& p, uint16_t & param)
   261 CsmaNetDevice::ProcessHeader (Ptr<Packet> p, uint16_t & param)
   262 {
   262 {
   263   NS_LOG_FUNCTION;
   263   NS_LOG_FUNCTION;
   264   if (m_encapMode == RAW)
   264   if (m_encapMode == RAW)
   265     {
   265     {
   266       return true;
   266       return true;
   267     }
   267     }
   268   EthernetHeader header (false);
   268   EthernetHeader header (false);
   269   EthernetTrailer trailer;
   269   EthernetTrailer trailer;
   270       
   270       
   271   p.RemoveTrailer(trailer);
   271   p->RemoveTrailer(trailer);
   272   trailer.CheckFcs(p);
   272   trailer.CheckFcs(p);
   273   p.RemoveHeader(header);
   273   p->RemoveHeader(header);
   274 
   274 
   275   if ((header.GetDestination() != GetBroadcast ()) &&
   275   if ((header.GetDestination() != GetBroadcast ()) &&
   276       (header.GetDestination() != GetAddress ()))
   276       (header.GetDestination() != GetAddress ()))
   277     {
   277     {
   278       return false;
   278       return false;
   284     case IP_ARP:
   284     case IP_ARP:
   285       param = header.GetLengthType();
   285       param = header.GetLengthType();
   286       break;
   286       break;
   287     case LLC: {
   287     case LLC: {
   288       LlcSnapHeader llc;
   288       LlcSnapHeader llc;
   289       p.RemoveHeader (llc);
   289       p->RemoveHeader (llc);
   290       param = llc.GetType ();
   290       param = llc.GetType ();
   291     } break;
   291     } break;
   292     case RAW:
   292     case RAW:
   293       NS_ASSERT (false);
   293       NS_ASSERT (false);
   294       break;
   294       break;
   309       return false;
   309       return false;
   310     }
   310     }
   311 }
   311 }
   312 
   312 
   313 bool
   313 bool
   314 CsmaNetDevice::SendTo (
   314 CsmaNetDevice::SendTo (Ptr<Packet> packet, 
   315   const Packet& packet, 
   315                        const Address& dest, 
   316   const Address& dest, 
   316                        uint16_t protocolNumber)
   317   uint16_t protocolNumber)
   317 {
   318 {
   318   NS_LOG_FUNCTION;
   319   NS_LOG_FUNCTION;
   319   NS_LOG_LOGIC ("p=" << packet);
   320   Packet p = packet;
   320   NS_LOG_LOGIC ("UID is " << packet->GetUid () << ")");
   321   NS_LOG_LOGIC ("p=" << &p);
       
   322   NS_LOG_LOGIC ("UID is " << p.GetUid () << ")");
       
   323 
   321 
   324   NS_ASSERT (IsLinkUp ());
   322   NS_ASSERT (IsLinkUp ());
   325 
   323 
   326   // Only transmit if send side of net device is enabled
   324   // Only transmit if send side of net device is enabled
   327   if (!IsSendEnabled())
   325   if (!IsSendEnabled())
   328     return false;
   326     return false;
   329 
   327 
   330   Mac48Address destination = Mac48Address::ConvertFrom (dest);
   328   Mac48Address destination = Mac48Address::ConvertFrom (dest);
   331   AddHeader(p, destination, protocolNumber);
   329   AddHeader(packet, destination, protocolNumber);
   332 
   330 
   333   // Place the packet to be sent on the send queue
   331   // Place the packet to be sent on the send queue
   334   if (m_queue->Enqueue(p) == false )
   332   if (m_queue->Enqueue(packet) == false )
   335     {
   333     {
   336       return false;
   334       return false;
   337     }
   335     }
   338   // If the device is idle, we need to start a transmission. Otherwise,
   336   // If the device is idle, we need to start a transmission. Otherwise,
   339   // the transmission will be started when the current packet finished
   337   // the transmission will be started when the current packet finished
   340   // transmission (see TransmitCompleteEvent)
   338   // transmission (see TransmitCompleteEvent)
   341   if (m_txMachineState == READY) 
   339   if (m_txMachineState == READY) 
   342     {
   340     {
   343       // Store the next packet to be transmitted
   341       // Store the next packet to be transmitted
   344       if (m_queue->Dequeue (m_currentPkt))
   342       m_currentPkt = m_queue->Dequeue ();
       
   343       if (m_currentPkt != 0)
   345         {
   344         {
   346           TransmitStart();
   345           TransmitStart();
   347         }
   346         }
   348     }
   347     }
   349   return true;
   348   return true;
   351 
   350 
   352 void
   351 void
   353 CsmaNetDevice::TransmitStart ()
   352 CsmaNetDevice::TransmitStart ()
   354 {
   353 {
   355   NS_LOG_FUNCTION;
   354   NS_LOG_FUNCTION;
   356   NS_LOG_LOGIC ("m_currentPkt=" << &m_currentPkt);
   355   NS_LOG_LOGIC ("m_currentPkt=" << m_currentPkt);
   357   NS_LOG_LOGIC ("UID is " << m_currentPkt.GetUid ());
   356   NS_LOG_LOGIC ("UID is " << m_currentPkt->GetUid ());
   358 //
   357 //
   359 // This function is called to start the process of transmitting a packet.
   358 // This function is called to start the process of transmitting a packet.
   360 // We need to tell the channel that we've started wiggling the wire and
   359 // We need to tell the channel that we've started wiggling the wire and
   361 // schedule an event that will be executed when it's time to tell the 
   360 // schedule an event that will be executed when it's time to tell the 
   362 // channel that we're done wiggling the wire.
   361 // channel that we're done wiggling the wire.
   391     } 
   390     } 
   392   else 
   391   else 
   393     {
   392     {
   394       // Channel is free, transmit packet
   393       // Channel is free, transmit packet
   395       m_txMachineState = BUSY;
   394       m_txMachineState = BUSY;
   396       Time tEvent = Seconds (m_bps.CalculateTxTime(m_currentPkt.GetSize()));
   395       Time tEvent = Seconds (m_bps.CalculateTxTime(m_currentPkt->GetSize()));
   397       
   396       
   398       NS_LOG_LOGIC ("Schedule TransmitCompleteEvent in " << 
   397       NS_LOG_LOGIC ("Schedule TransmitCompleteEvent in " << 
   399         tEvent.GetSeconds () << "sec");
   398         tEvent.GetSeconds () << "sec");
   400       
   399       
   401       Simulator::Schedule (tEvent, 
   400       Simulator::Schedule (tEvent, 
   418 
   417 
   419 void
   418 void
   420 CsmaNetDevice::TransmitAbort (void)
   419 CsmaNetDevice::TransmitAbort (void)
   421 {
   420 {
   422   NS_LOG_FUNCTION;
   421   NS_LOG_FUNCTION;
   423   NS_LOG_LOGIC ("Pkt UID is " << m_currentPkt.GetUid () << ")");
   422   NS_LOG_LOGIC ("Pkt UID is " << m_currentPkt->GetUid () << ")");
   424 
   423 
   425   // Try to transmit a new packet
   424   // Try to transmit a new packet
   426   bool found;
   425   m_currentPkt = m_queue->Dequeue ();
   427   found = m_queue->Dequeue (m_currentPkt);
   426   NS_ASSERT_MSG(m_currentPkt != 0, "IsEmpty false but no Packet on queue?");
   428   NS_ASSERT_MSG(found, "IsEmpty false but no Packet on queue?");
       
   429   m_backoff.ResetBackoffTime();
   427   m_backoff.ResetBackoffTime();
   430   m_txMachineState = READY;
   428   m_txMachineState = READY;
   431   TransmitStart ();
   429   TransmitStart ();
   432 }
   430 }
   433 
   431 
   444   NS_ASSERT_MSG(m_txMachineState == BUSY, "Must be BUSY if transmitting");
   442   NS_ASSERT_MSG(m_txMachineState == BUSY, "Must be BUSY if transmitting");
   445   // Channel should be transmitting
   443   // Channel should be transmitting
   446   NS_ASSERT(m_channel->GetState() == TRANSMITTING);
   444   NS_ASSERT(m_channel->GetState() == TRANSMITTING);
   447   m_txMachineState = GAP;
   445   m_txMachineState = GAP;
   448 
   446 
   449   NS_LOG_LOGIC ("Pkt UID is " << m_currentPkt.GetUid () << ")");
   447   NS_LOG_LOGIC ("Pkt UID is " << m_currentPkt->GetUid () << ")");
   450   m_channel->TransmitEnd (); 
   448   m_channel->TransmitEnd (); 
   451 
   449 
   452   NS_LOG_LOGIC ("Schedule TransmitReadyEvent in "
   450   NS_LOG_LOGIC ("Schedule TransmitReadyEvent in "
   453     << m_tInterframeGap.GetSeconds () << "sec");
   451     << m_tInterframeGap.GetSeconds () << "sec");
   454 
   452 
   474     {
   472     {
   475       return;
   473       return;
   476     }
   474     }
   477   else
   475   else
   478     {
   476     {
   479       bool found;
   477       m_currentPkt = m_queue->Dequeue ();
   480       found = m_queue->Dequeue (m_currentPkt);
   478       NS_ASSERT_MSG(m_currentPkt != 0, "IsEmpty false but no Packet on queue?");
   481       NS_ASSERT_MSG(found, "IsEmpty false but no Packet on queue?");
       
   482       TransmitStart ();
   479       TransmitStart ();
   483     }
   480     }
   484 }
   481 }
   485 
   482 
   486 Ptr<TraceResolver>
   483 Ptr<TraceResolver>
   489   NS_LOG_FUNCTION;
   486   NS_LOG_FUNCTION;
   490   Ptr<CompositeTraceResolver> resolver = Create<CompositeTraceResolver> ();
   487   Ptr<CompositeTraceResolver> resolver = Create<CompositeTraceResolver> ();
   491   resolver->AddComposite ("queue", m_queue);
   488   resolver->AddComposite ("queue", m_queue);
   492   resolver->AddSource ("rx",
   489   resolver->AddSource ("rx",
   493                        TraceDoc ("receive MAC packet",
   490                        TraceDoc ("receive MAC packet",
   494                                  "const Packet &", "packet received"),
   491                                  "Ptr<const Packet>", "packet received"),
   495                        m_rxTrace,
   492                        m_rxTrace,
   496                        CsmaTraceType (CsmaTraceType::RX));
   493                        CsmaTraceType (CsmaTraceType::RX));
   497   resolver->AddSource ("drop",
   494   resolver->AddSource ("drop",
   498                        TraceDoc ("drop MAC packet",
   495                        TraceDoc ("drop MAC packet",
   499                                  "const Packet &", "packet dropped"),
   496                                  "Ptr<const Packet>", "packet dropped"),
   500                        m_dropTrace,
   497                        m_dropTrace,
   501                        CsmaTraceType (CsmaTraceType::DROP));
   498                        CsmaTraceType (CsmaTraceType::DROP));
   502   resolver->SetParentResolver (NetDevice::GetTraceResolver ());
   499   resolver->SetParentResolver (NetDevice::GetTraceResolver ());
   503   return resolver;
   500   return resolver;
   504 }
   501 }
   530 
   527 
   531   m_queue = q;
   528   m_queue = q;
   532 }
   529 }
   533 
   530 
   534 void
   531 void
   535 CsmaNetDevice::Receive (const Packet& packet)
   532 CsmaNetDevice::Receive (Ptr<Packet> packet)
   536 {
   533 {
   537   NS_LOG_FUNCTION;
   534   NS_LOG_FUNCTION;
   538 
   535 
   539   EthernetHeader header (false);
   536   EthernetHeader header (false);
   540   EthernetTrailer trailer;
   537   EthernetTrailer trailer;
   541   Mac48Address broadcast;
   538   Mac48Address broadcast;
   542   Mac48Address multicast;
   539   Mac48Address multicast;
   543   Mac48Address destination;
   540   Mac48Address destination;
   544   Packet p = packet;
   541 
   545 
   542   NS_LOG_LOGIC ("UID is " << packet->GetUid());
   546   NS_LOG_LOGIC ("UID is " << p.GetUid());
       
   547 
   543 
   548   // Only receive if send side of net device is enabled
   544   // Only receive if send side of net device is enabled
   549   if (!IsReceiveEnabled())
   545   if (!IsReceiveEnabled())
   550     {
   546     {
   551       m_dropTrace (p);
   547       m_dropTrace (packet);
   552       return;
   548       return;
   553     }
   549     }
   554 
   550 
   555   if (m_encapMode == RAW)
   551   if (m_encapMode == RAW)
   556     {
   552     {
   557       ForwardUp (packet, 0, GetBroadcast ());
   553       ForwardUp (packet, 0, GetBroadcast ());
   558       m_dropTrace (p);
   554       m_dropTrace (packet);
   559       return;
   555       return;
   560     }
   556     }
   561   p.RemoveTrailer(trailer);
   557   packet->RemoveTrailer(trailer);
   562   trailer.CheckFcs(p);
   558   trailer.CheckFcs(packet);
   563   p.RemoveHeader(header);
   559   packet->RemoveHeader(header);
   564 
   560 
   565   NS_LOG_LOGIC ("Pkt destination is " << header.GetDestination ());
   561   NS_LOG_LOGIC ("Pkt destination is " << header.GetDestination ());
   566 //
   562 //
   567 // An IP host group address is mapped to an Ethernet multicast address
   563 // An IP host group address is mapped to an Ethernet multicast address
   568 // by placing the low-order 23-bits of the IP address into the low-order
   564 // by placing the low-order 23-bits of the IP address into the low-order
   587   if ((header.GetDestination () != broadcast) &&
   583   if ((header.GetDestination () != broadcast) &&
   588       (mcDest != multicast) &&
   584       (mcDest != multicast) &&
   589       (header.GetDestination () != destination))
   585       (header.GetDestination () != destination))
   590     {
   586     {
   591       NS_LOG_LOGIC ("Dropping pkt ");
   587       NS_LOG_LOGIC ("Dropping pkt ");
   592       m_dropTrace (p);
   588       m_dropTrace (packet);
   593       return;
   589       return;
   594     }
   590     }
   595 
   591 
   596   m_rxTrace (p);
   592   m_rxTrace (packet);
   597 //
   593 //
   598 // protocol must be initialized to avoid a compiler warning in the RAW
   594 // protocol must be initialized to avoid a compiler warning in the RAW
   599 // case that breaks the optimized build.
   595 // case that breaks the optimized build.
   600 //
   596 //
   601   uint16_t protocol = 0;
   597   uint16_t protocol = 0;
   606     case IP_ARP:
   602     case IP_ARP:
   607       protocol = header.GetLengthType();
   603       protocol = header.GetLengthType();
   608       break;
   604       break;
   609     case LLC: {
   605     case LLC: {
   610       LlcSnapHeader llc;
   606       LlcSnapHeader llc;
   611       p.RemoveHeader (llc);
   607       packet->RemoveHeader (llc);
   612       protocol = llc.GetType ();
   608       protocol = llc.GetType ();
   613     } break;
   609     } break;
   614     case RAW:
   610     case RAW:
   615       NS_ASSERT (false);
   611       NS_ASSERT (false);
   616       break;
   612       break;
   617     }
   613     }
   618   
   614   
   619   ForwardUp (p, protocol, header.GetSource ());
   615   ForwardUp (packet, protocol, header.GetSource ());
   620   return;
   616   return;
   621 }
   617 }
   622 
   618 
   623 Address
   619 Address
   624 CsmaNetDevice::MakeMulticastAddress(Ipv4Address multicastGroup) const
   620 CsmaNetDevice::MakeMulticastAddress(Ipv4Address multicastGroup) const