diff --git a/src/internet-node/tcp-socket.cc b/src/internet-node/tcp-socket.cc index ea3eb9058..44b78290a 100644 --- a/src/internet-node/tcp-socket.cc +++ b/src/internet-node/tcp-socket.cc @@ -435,6 +435,14 @@ TcpSocket::SendTo (const Address &address, Ptr p) } } +// XXX Raj to make this functional +uint32_t +TcpSocket::GetTxAvailable (void) const +{ + // No finite send buffer is modelled + return 0xffffffff; +} + int TcpSocket::Listen (uint32_t q) { @@ -472,6 +480,33 @@ TcpSocket::GetRxAvailable (void) const return m_rxAvailable; } +// XXX Raj to finish +void +TcpSocket::SetSndBuf (uint32_t size) +{ + +} + +// XXX Raj to finish +uint32_t +TcpSocket::GetSndBuf (void) +{ + return 0; +} + +// XXX Raj to finish +void +TcpSocket::SetRcvBuf (uint32_t size) +{ +} + +// XXX Raj to finish +uint32_t +TcpSocket::GetRcvBuf (void) +{ + return 0; +} + void TcpSocket::ForwardUp (Ptr packet, Ipv4Address ipv4, uint16_t port) { diff --git a/src/internet-node/tcp-socket.h b/src/internet-node/tcp-socket.h index c7600e3f5..ce8948448 100644 --- a/src/internet-node/tcp-socket.h +++ b/src/internet-node/tcp-socket.h @@ -68,11 +68,17 @@ public: virtual int Send (Ptr p); virtual int Send (const uint8_t* buf, uint32_t size); virtual int SendTo(const Address &address, Ptr p); + virtual uint32_t GetTxAvailable (void) const; virtual int Listen(uint32_t queueLimit); virtual Ptr Recv (uint32_t maxSize, uint32_t flags); virtual uint32_t GetRxAvailable (void) const; + virtual void SetSndBuf (uint32_t size); + virtual uint32_t GetSndBuf (void); + virtual void SetRcvBuf (uint32_t size); + virtual uint32_t GetRcvBuf (void); + private: friend class Tcp; // invoked by Tcp class diff --git a/src/internet-node/udp-socket.cc b/src/internet-node/udp-socket.cc index 5960a929b..b705033a7 100644 --- a/src/internet-node/udp-socket.cc +++ b/src/internet-node/udp-socket.cc @@ -28,11 +28,25 @@ #include "ipv4-end-point.h" #include "ipv4-l4-demux.h" #include "ns3/ipv4.h" +#include "ns3/udp.h" +#include "ns3/trace-source-accessor.h" NS_LOG_COMPONENT_DEFINE ("UdpSocket"); namespace ns3 { +TypeId +UdpSocket::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::UdpSocket") + .SetParent () + .AddConstructor () + .AddTraceSource ("Drop", "Drop UDP packet due to receive buffer overflow", + MakeTraceSourceAccessor (&UdpSocket::m_dropTrace)) + ; + return tid; +} + UdpSocket::UdpSocket () : m_endPoint (0), m_node (0), @@ -41,7 +55,8 @@ UdpSocket::UdpSocket () m_shutdownSend (false), m_shutdownRecv (false), m_connected (false), - m_rxAvailable (0) + m_rxAvailable (0), + m_udp_rmem (0) { NS_LOG_FUNCTION_NOARGS (); } @@ -73,6 +88,9 @@ void UdpSocket::SetNode (Ptr node) { m_node = node; + Ptr u = node->GetObject (); + m_udp_rmem =u->GetDefaultRxBuffer (); + } void UdpSocket::SetUdp (Ptr udp) @@ -316,8 +334,15 @@ UdpSocket::DoSendTo (Ptr p, Ipv4Address dest, uint16_t port) return 0; } +uint32_t +UdpSocket::GetTxAvailable (void) const +{ + // No finite send buffer is modelled + return 0xffffffff; +} + int -UdpSocket::SendTo(const Address &address, Ptr p) +UdpSocket::SendTo (const Address &address, Ptr p) { NS_LOG_FUNCTION (this << address << p); InetSocketAddress transport = InetSocketAddress::ConvertFrom (address); @@ -363,15 +388,59 @@ UdpSocket::ForwardUp (Ptr packet, Ipv4Address ipv4, uint16_t port) { return; } - Address address = InetSocketAddress (ipv4, port); - SocketRxAddressTag tag; - tag.SetAddress (address); - packet->AddTag (tag); - m_deliveryQueue.push (packet); - m_rxAvailable += packet->GetSize (); - NotifyDataRecv (); + if ((m_rxAvailable + packet->GetSize ()) <= m_udp_rmem) + { + Address address = InetSocketAddress (ipv4, port); + SocketRxAddressTag tag; + tag.SetAddress (address); + packet->AddTag (tag); + m_deliveryQueue.push (packet); + m_rxAvailable += packet->GetSize (); + NotifyDataRecv (); + } + else + { + // In general, this case should not occur unless the + // receiving application reads data from this socket slowly + // in comparison to the arrival rate + // + // drop and trace packet + NS_LOG_WARN ("No receive buffer space available. Drop."); + m_dropTrace (packet); + } } +void +UdpSocket::SetSndBuf (uint32_t size) +{ + // return EINVAL since we are not modelling a finite send buffer + // Enforcing buffer size should be added if we ever start to model + // non-zero processing delay in the UDP/IP stack + m_errno = ERROR_INVAL; +} + +uint32_t +UdpSocket::GetSndBuf (void) +{ + m_errno = ERROR_NOTERROR; + return 0xffffffff; +} + +void +UdpSocket::SetRcvBuf (uint32_t size) +{ + m_errno = ERROR_NOTERROR; + m_udp_rmem = size; +} + +uint32_t +UdpSocket::GetRcvBuf (void) +{ + m_errno = ERROR_NOTERROR; + return m_udp_rmem; +} + + } //namespace ns3 diff --git a/src/internet-node/udp-socket.h b/src/internet-node/udp-socket.h index e5a425ddc..74fc2e26b 100644 --- a/src/internet-node/udp-socket.h +++ b/src/internet-node/udp-socket.h @@ -23,6 +23,7 @@ #include #include #include "ns3/callback.h" +#include "ns3/traced-callback.h" #include "ns3/socket.h" #include "ns3/ptr.h" #include "ns3/ipv4-address.h" @@ -37,6 +38,7 @@ class UdpL4Protocol; class UdpSocket : public Socket { public: + static TypeId GetTypeId (void); /** * Create an unbound udp socket. */ @@ -56,10 +58,16 @@ public: virtual int Connect(const Address &address); virtual int Send (Ptr p); virtual int SendTo(const Address &address,Ptr p); + virtual uint32_t GetTxAvailable (void) const; virtual Ptr Recv (uint32_t maxSize, uint32_t flags); virtual uint32_t GetRxAvailable (void) const; + virtual void SetSndBuf (uint32_t size); + virtual uint32_t GetSndBuf (void); + virtual void SetRcvBuf (uint32_t size); + virtual uint32_t GetRcvBuf (void); + private: friend class Udp; // invoked by Udp class @@ -77,6 +85,8 @@ private: uint16_t m_defaultPort; Callback,uint32_t,const Address &> m_dummyRxCallback; Callback,uint8_t const*,uint32_t,const Address &> m_rxCallback; + TracedCallback > m_dropTrace; + enum SocketErrno m_errno; bool m_shutdownSend; bool m_shutdownRecv; @@ -84,6 +94,8 @@ private: std::queue > m_deliveryQueue; uint32_t m_rxAvailable; + + uint32_t m_udp_rmem; }; }//namespace ns3 diff --git a/src/node/packet-socket.cc b/src/node/packet-socket.cc index 9600666e2..7accac512 100644 --- a/src/node/packet-socket.cc +++ b/src/node/packet-socket.cc @@ -214,6 +214,13 @@ PacketSocket::Send (Ptr p) return SendTo (m_destAddr, p); } +uint32_t +PacketSocket::GetTxAvailable (void) const +{ + // No finite send buffer is modelled + return 0xffffffff; +} + int PacketSocket::SendTo(const Address &address, Ptr p) { @@ -338,4 +345,23 @@ PacketSocket::GetRxAvailable (void) const return m_rxAvailable; } +void +PacketSocket::SetSndBuf (uint32_t size) +{ +} +uint32_t +PacketSocket::GetSndBuf (void) +{ +return 0; +} +void +PacketSocket::SetRcvBuf (uint32_t size) +{ +} +uint32_t +PacketSocket::GetRcvBuf (void) +{ +return 0; +} + }//namespace ns3 diff --git a/src/node/packet-socket.h b/src/node/packet-socket.h index a2a60403f..f9997c679 100644 --- a/src/node/packet-socket.h +++ b/src/node/packet-socket.h @@ -86,11 +86,17 @@ public: virtual int ShutdownRecv (void); virtual int Connect(const Address &address); virtual int Send (Ptr p); + virtual uint32_t GetTxAvailable (void) const; + virtual int SendTo(const Address &address,Ptr p); virtual Ptr Recv (uint32_t maxSize, uint32_t flags); virtual uint32_t GetRxAvailable (void) const; + virtual void SetSndBuf (uint32_t size); + virtual uint32_t GetSndBuf (void); + virtual void SetRcvBuf (uint32_t size); + virtual uint32_t GetRcvBuf (void); private: void ForwardUp (Ptr device, Ptr packet, diff --git a/src/node/socket.h b/src/node/socket.h index e9a11b51a..921070f77 100644 --- a/src/node/socket.h +++ b/src/node/socket.h @@ -220,6 +220,12 @@ public: */ virtual int Send (Ptr p) = 0; + /** + * returns the number of bytes which can be sent in a single call + * to Send. + */ + virtual uint32_t GetTxAvailable (void) const = 0; + /** * \brief Send data (or dummy data) to the remote host * \param buf A pointer to a raw byte buffer of some data to send. If this @@ -280,6 +286,35 @@ public: */ virtual uint32_t GetRxAvailable (void) const = 0; + /** + * \brief ns-3 version of setsockopt (SO_SNDBUF) + * + * The error code value can be checked by calling GetErrno () + */ + virtual void SetSndBuf (uint32_t size) = 0; + /** + * \brief ns-3 version of getsockopt (SO_SNDBUF) + * + * The error code value can be checked by calling GetErrno () + * + * \returns The size in bytes of the send buffer + */ + virtual uint32_t GetSndBuf (void) = 0; + /** + * \brief ns-3 version of setsockopt (SO_RCVBUF) + * + * The error code value can be checked by calling GetErrno () + */ + virtual void SetRcvBuf (uint32_t size) = 0; + /** + * \brief ns-3 version of getsockopt (SO_RCVBUF) + * + * The error code value can be checked by calling GetErrno () + * + * \returns The size in bytes of the receive buffer + */ + virtual uint32_t GetRcvBuf (void) = 0; + protected: void NotifyCloseCompleted (void); void NotifyConnectionSucceeded (void); diff --git a/src/node/udp.cc b/src/node/udp.cc index 989dd95f8..28046152d 100644 --- a/src/node/udp.cc +++ b/src/node/udp.cc @@ -18,6 +18,7 @@ * Author: Mathieu Lacage */ #include "udp.h" +#include "ns3/uinteger.h" namespace ns3 { @@ -26,11 +27,23 @@ NS_OBJECT_ENSURE_REGISTERED (Udp); TypeId Udp::GetTypeId (void) { static TypeId tid = TypeId ("ns3::Udp") - .SetParent (); + .SetParent () + .AddAttribute ("DefaultRxBufferSize", + "Default UDP maximum receive buffer size (bytes)", + UintegerValue (0xffffffffl), + MakeUintegerAccessor (&Udp::m_defaultRxBuffer), + MakeUintegerChecker ()) + ; return tid; } Udp::Udp () {} +uint32_t +Udp::GetDefaultRxBuffer (void) const +{ + return m_defaultRxBuffer; +} + } // namespace ns3 diff --git a/src/node/udp.h b/src/node/udp.h index a539eb94a..addcb583d 100644 --- a/src/node/udp.h +++ b/src/node/udp.h @@ -53,6 +53,10 @@ public: * implementations.. */ virtual Ptr CreateSocket (void) = 0; + + uint32_t GetDefaultRxBuffer (void) const; +private: + uint32_t m_defaultRxBuffer; }; } // namespace ns3