ns3_AODV 源碼閱讀1:RouteOutput和RouteInput是什麼

運行腳本aodv.cc 能夠獲得如下log輸出node

RouteOutput:socket

主動發包時調用的(它在找到路由的時候返回路由地址)ide

而這裏沒有找到路由,因此返回了loopback,也就是本機地址,因而數據包又發回了本機。函數

RouteInput:oop

收到包轉發時調用的,在這裏調用loopback發給本身後ui

node0就從發包着變成了收包者,而後再進行轉發this

 

以及調用DeferredRouteOutput(它又調用SendRequest)發送RREQspa

 

 

 

這樣複用了RouteInput,從而簡化了RouteOutput的操做,只進行路由查詢,查詢不到的狀況下和轉發函數合併爲RouteInput,不須要在主動發起時考慮如何發出去了(廣播啥的)指針

將發包和收包時的路由請求過程合併code

 

 
 
Ptr<Ipv4Route>
RoutingProtocol::RouteOutput (Ptr<Packet> p, const Ipv4Header &header,
                              Ptr<NetDevice> oif, Socket::SocketErrno &sockerr)
{
  NS_LOG_FUNCTION (this << header << (oif ? oif->GetIfIndex () : 0));
  if (!p)
    {
      NS_LOG_DEBUG("Packet is == 0");
      return LoopbackRoute (header, oif); // later
    }
  if (m_socketAddresses.empty ())
    {
      sockerr = Socket::ERROR_NOROUTETOHOST;
      NS_LOG_LOGIC ("No aodv interfaces");
      Ptr<Ipv4Route> route;
      return route;
    }
  sockerr = Socket::ERROR_NOTERROR;
  Ptr<Ipv4Route> route;
  Ipv4Address dst = header.GetDestination ();
  RoutingTableEntry rt;
  if (m_routingTable.LookupValidRoute (dst, rt))
    {//找到路由則返回路由
      route = rt.GetRoute ();
      NS_ASSERT (route != 0);
      NS_LOG_DEBUG ("Exist route to " << route->GetDestination () << " from interface " << route->GetSource ());
      if (oif != 0 && route->GetOutputDevice () != oif)
        {
          NS_LOG_DEBUG ("Output device doesn't match. Dropped.");
          sockerr = Socket::ERROR_NOROUTETOHOST;
          return Ptr<Ipv4Route> ();
        }
      UpdateRouteLifeTime (dst, m_activeRouteTimeout);
      UpdateRouteLifeTime (route->GetGateway (), m_activeRouteTimeout);
      return route;
    }

  // 沒有找到有效的路由,在這種狀況下咱們返回loopback。Valid route not found, in this case we return loopback. 
  //實際路徑請求將被推遲,直到包將徹底造成, Actual route request will be deferred until packet will be fully formed, 
  // 路由到loopback,從loopback接收並傳遞到RouteInput(見下文)routed to loopback, received from loopback and passed to RouteInput (see below)
  uint32_t iif = (oif ? m_ipv4->GetInterfaceForDevice (oif) : -1);
  DeferredRouteOutputTag tag (iif);
  NS_LOG_DEBUG ("Valid Route not found");
  if (!p->PeekPacketTag (tag))
    {
      p->AddPacketTag (tag);
    }
  return LoopbackRoute (header, oif);
}
 
 
bool
RoutingProtocol::RouteInput (Ptr<const Packet> p, const Ipv4Header &header,
                             Ptr<const NetDevice> idev, UnicastForwardCallback ucb,
                             MulticastForwardCallback mcb, LocalDeliverCallback lcb, ErrorCallback ecb)
{//CALLBACK,回調函數就是一個經過函數指針調用的函數。
    //(雖然這裏是類)
    //若是你把函數的指針(地址)做爲參數傳遞給另外一個函數,
    //當這個指針被用爲調用它所指向的函數時,咱們就說這是回調函數。
    //回調函數不是由該函數的實現方直接調用,而是在特定的事件或條件發生時由另外的一方調用的,用於對該事件或條件進行響應。
  NS_LOG_FUNCTION (this << p->GetUid () << header.GetDestination () << idev->GetAddress ());
  if (m_socketAddresses.empty ())
    {
      NS_LOG_LOGIC ("No aodv interfaces");//沒有aodv接口
      return false;
    }
  NS_ASSERT (m_ipv4 != 0);
  NS_ASSERT (p != 0);
  // 檢查輸入設備是否支持IP Check if input device supports IP
  NS_ASSERT (m_ipv4->GetInterfaceForDevice (idev) >= 0);
  int32_t iif = m_ipv4->GetInterfaceForDevice (idev);

  Ipv4Address dst = header.GetDestination ();
  Ipv4Address origin = header.GetSource ();

  // 延遲的路由請求Deferred route request
  if (idev == m_lo)
    {
      DeferredRouteOutputTag tag;
      if (p->PeekPacketTag (tag))
        {
          DeferredRouteOutput (p, header, ucb, ecb);
          //當一個節點沒法找到一個可用的到某個節點的路由時,它就會廣播一條 RREQ 消息。
          return true;
        }
    }

  // 重複本身的數據包 Duplicate of own packet
  if (IsMyOwnAddress (origin))
    return true;

  // AODV不是多播路由協議 AODV is not a multicast routing protocol
  if (dst.IsMulticast ())
    {
      return false; 
    }

  // 廣播本地交付/轉發 Broadcast local delivery/forwarding
  for (std::map<Ptr<Socket>, Ipv4InterfaceAddress>::const_iterator j =
         m_socketAddresses.begin (); j != m_socketAddresses.end (); ++j)
    {
      Ipv4InterfaceAddress iface = j->second;
      if (m_ipv4->GetInterfaceForAddress (iface.GetLocal ()) == iif)
        if (dst == iface.GetBroadcast () || dst.IsBroadcast ())
          {
            if (m_dpd.IsDuplicate (p, header))
              {//重複數據包p from Source,Drop
                NS_LOG_DEBUG ("Duplicated packet " << p->GetUid () << " from " << origin << ". Drop.");
                return true;
              }
            UpdateRouteLifeTime (origin, m_activeRouteTimeout);
            Ptr<Packet> packet = p->Copy ();
            if (lcb.IsNull () == false)
              {//廣播本地交付to  iface.GetLocal 
                NS_LOG_LOGIC ("Broadcast local delivery to " << iface.GetLocal ());
                lcb (p, header, iif);
                // 經過追加處理 Fall through to additional processing
              }
            else
              {//因爲null callback ,沒法在本地傳遞數據包 
                NS_LOG_ERROR ("Unable to deliver packet locally due to null callback " << p->GetUid () << " from " << origin);
                ecb (p, header, Socket::ERROR_NOROUTETOHOST);
              }
            if (!m_enableBroadcast)
              {
                return true;
              }
            if (header.GetProtocol () == UdpL4Protocol::PROT_NUMBER)
              {
                UdpHeader udpHeader;
                p->PeekHeader (udpHeader);
                if (udpHeader.GetDestinationPort () == AODV_PORT)
                  {
                    // 廣播發送的AODV數據包已經被管理 AODV packets sent in broadcast are already managed
                    return true;
                  }
              }
            if (header.GetTtl () > 1)
              {//轉發廣播。 TTL=header.GetTtl ()
                NS_LOG_LOGIC ("Forward broadcast. TTL " << (uint16_t) header.GetTtl ());
                RoutingTableEntry toBroadcast;
                if (m_routingTable.LookupRoute (dst, toBroadcast))
                  {
                    Ptr<Ipv4Route> route = toBroadcast.GetRoute ();
                    ucb (route, packet, header);
                  }
                else
                  {//沒有路由轉發廣播。 丟棄數據包
                    NS_LOG_DEBUG ("No route to forward broadcast. Drop packet " << p->GetUid ());
                  }
              }
            else
              {//TTL超出。 丟棄數據包
                NS_LOG_DEBUG ("TTL exceeded. Drop packet " << p->GetUid ());
              }
            return true;
          }
    }

  // 單播本地交付Unicast local delivery
  if (m_ipv4->IsDestinationAddress (dst, iif))
    {
      UpdateRouteLifeTime (origin, m_activeRouteTimeout);
      RoutingTableEntry toOrigin;
      if (m_routingTable.LookupValidRoute (origin, toOrigin))
        {
          UpdateRouteLifeTime (toOrigin.GetNextHop (), m_activeRouteTimeout);
          m_nb.Update (toOrigin.GetNextHop (), m_activeRouteTimeout);
        }
      if (lcb.IsNull () == false)
        {//單播本地交付到dst
          NS_LOG_LOGIC ("Unicast local delivery to " << dst);
          lcb (p, header, iif);
        }
      else
        {//因爲null回調,沒法在本地交付數據包
          NS_LOG_ERROR ("Unable to deliver packet locally due to null callback " << p->GetUid () << " from " << origin);
          ecb (p, header, Socket::ERROR_NOROUTETOHOST);
        }
      return true;
    }

  // 檢查輸入設備是否支持IP轉發Check if input device supports IP forwarding
  if (m_ipv4->IsForwarding (iif) == false)
    {
      NS_LOG_LOGIC ("Forwarding disabled for this interface");
      ecb (p, header, Socket::ERROR_NOROUTETOHOST);
      return true;
    }

  // 轉發Forwarding
  return Forwarding (p, header, ucb, ecb);
}
相關文章
相關標籤/搜索