简体   繁体   English

UDP套接字缓冲区溢出检测

[英]UDP socket buffer overflow detection

I'm developing cross-platform tool that captures multiple udp streams with various bit-rate. 我正在开发跨平台工具,该工具可以捕获具有各种比特率的多个udp流。 boost::asio is used for networking. boost :: asio用于联网。 Is there any way to detect the situation when the udp buffer was full and data loss on socket could take place? 有什么方法可以检测udp缓冲区已满并且套接字上可能发生数据丢失的情况? The only way I can see now is reading /proc/%pid%/net/udp, but it's not aplicable for windows as you know :). 我现在看到的唯一方法是读取/ proc /%pid%/ net / udp,但正如您所知,它不适用于Windows :)。 Also I'd like to use boost features for it if possible. 另外,如果可能的话,我想使用增强功能。

If you need this capability, you have to code it into the protocol you are using. 如果需要此功能,则必须将其编码到正在使用的协议中。 UDP is incapable of doing this by itself. UDP本身无法执行此操作。 For example, you could put a sequence number in each datagram. 例如,您可以在每个数据报中放置一个序列号。 Missing datagrams would correspond to missing sequence numbers. 丢失的数据报将对应于丢失的序列号。

I've just hit the same issue (although for me Linux-specific), and despite the question being old might as well document my findings for others. 我遇到了同样的问题(尽管对我来说是Linux特有的),尽管这个问题比较老,也可能会为其他人记录我的发现。

As far as I know, there are no portable way to do this, and nothing directly supported by Boost. 据我所知,尚没有可移植的方法来执行此操作,Boost也没有直接支持的方法。

That said, there are some platform-specific ways of doing it. 也就是说,有一些特定于平台的方法。 In Linux, it can be done by setting the SO_RXQ_OVFL socket-option, and then getting the replies using recvmsg(). 在Linux中,可以通过设置SO_RXQ_OVFL套接字选项来完成,然后使用recvmsg()获取答复。 It's poorly documented though, but you may be helped by http://lists.openwall.net/netdev/2009/10/09/75 . 虽然它的文档很少,但是http://lists.openwall.net/netdev/2009/10/09/75可能会为您提供帮助。

One way to avoid it in the first place is to increase the receive-buffers (I assume you've investigated it already, but including it for completeness). 首先避免这种情况的一种方法是增加接收缓冲区(我想您已经研究过了,但是为了完整起见包括了它)。 The SO_RCVBUF options seems fairly well-supported cross-platform. SO_RCVBUF选项似乎是跨平台支持得很好的。 http://pubs.opengroup.org/onlinepubs/7908799/xns/setsockopt.html http://msdn.microsoft.com/en-us/library/windows/hardware/ff570832(v=vs.85).aspx OS:es puts an upper limit on this though, which an administrator might have to increase. http://pubs.opengroup.org/onlinepubs/7908799/xns/setsockopt.html http://msdn.microsoft.com/zh-cn/library/windows/hardware/ff570832(v=vs.85).aspx操作系统:es对此设置了上限,管理员可能必须提高该上限。 On Linux, IE it can be increased using /proc/sys/net/core/rmem_max. 在Linux上,可以使用/ proc / sys / net / core / rmem_max来增加IE。

Finally, one way for your application to assess it's "load", which with large input-buffers might serve for early detection of overloading, could be to introduce a timestamp before and after the async operations. 最后,让应用程序评估其“负载”的一种方法(可能具有大的输入缓冲区)可用于及早发现过载,它可以在异步操作之前和之后引入时间戳。 In pseudo_code (not boost::async-adapted): 在伪代码中(不通过boost :: async修改):

work_time = 0
idle_time = 0

b = clock.now()
while running:
  a = clock.now()
  work_time += a-b
  data = wait_for_input()
  b = clock.now()
  idle_time += b-a
  process(data)

Then every second or so, you can check and reset work_time / (work_time+idle_time) . 然后work_time / (work_time+idle_time) ,您可以检查并重置work_time / (work_time+idle_time) If it approaches 1, you know you're heading for trouble and can send out an alert or take other actions. 如果接近1,您就知道您将要遇到麻烦了,可以发出警报或采取其他措施。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM