sendmsg: no buffer space available

今天在将项目从虚拟机上移植到真实机器上面的时候,发现问题,总是不成功,最后判断是userspace的程序没有向kernel发送消息成功,因为无法触发kernel的行为,但是userspace显示正常。

这个问题好像两个月之前,netlink模块测试的时候遇到过这个问题,当时加上sleep就好了,同样复制这个方法,发现使用usleep(1)就解决问题了。

接下来分析问题的原因,问题锁定在sendmsg上面。

查看sendmsg的返回值,发现是-1,也就是说sendmsg失败了,看errno: ENOBUFS。

       ENOBUFS
             
The output queue for a network interface was full.  This generally
indicates that the interface has stopped sending, but may be caused  by 
transient  conges-
              tion.  (Normally, this does not occur in Linux.  Packets are just silently dropped when a device queue overflows.)

也就是说发送队列占满了,怎么会出现出现这样的问题呢?

追踪sendmsg函数:

sys_sendmsg()

......
    err = -ENOBUFS;

if (msg_sys.msg_controllen > INT_MAX)
        goto out_freeiov;
    ctl_len = msg_sys.msg_controllen;
    if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
        err = cmsghdr_from_user_compat_to_kern(&msg_sys, sock->sk, ctl, sizeof(ctl));
        if (err)
            goto out_freeiov;

......
out_freectl:
    if (ctl_buf != ctl)   
        sock_kfree_s(sock->sk, ctl_buf, ctl_len);
out_freeiov:
    if (iov != iovstack)
        sock_kfree_s(sock->sk, iov, iov_size);
out_put:
    fput_light(sock->file, fput_needed);
out:      
    return err;

发现在sys_sendmsg中会引用一些msg中的成员变量,一些变量很大导致返回ENOBUFS错误,最终导致no buffer space available。

看我们的userspace代码:

    struct msghdr msg;

msg.msg_name = (void *)&dest_addr;
    msg.msg_namelen = sizeof(dest_addr);
    msg.msg_iov = &iov;
    msg.msg_iovlen = 1;

OMG,竟然没有初始化,这样的话,msg.msg_controllen就有可能比较大,导致出现ENOBUFS错误。

解决方法就是初始化msg:

memset(&msg, 0 ,sizeof(msg));

注意:在调试内核相关程序的时候,一定要检查返回值,一定要进行初始化,不要想当然。有些编译器会帮你初始化,但是有些不会,程序员不能够依赖编译器来干活 :-)

时间: 2024-07-31 16:52:49

sendmsg: no buffer space available的相关文章

Windows 7下解决: java.net.SocketException: No buffer space available (maximum connections reached?)

查了一大堆网上的资料全都没用,Google得知,是Windows 7 的socket泄漏 : https://supportkb.riverbed.com/support/index?page=content&id=S23580&actp=LIST_RECENT 补丁下载地址:  http://support.microsoft.com/kb/2577795 记录一下. Windows 7下解决: java.net.SocketException: No buffer space avai

java.net.SocketException: No buffer space available

今天写了一个4线程并发发送10W条数据,但是在发送1W2左右时,突然开始报java.net.SocketException: No buffer space available异常,经过相关资料的查询,发现是Windows Server 2008 R2 或 Windows 7 的多处理器计算机上都存在的问题,具体可以参照window官方的解释;地址为:http://support.microsoft.com/kb/2577795 我本人的电脑是window7四核的处理器: java.net.So

socket-详细分析No buffer space available(转)

新年上班第一天,突然遇到一个socket连接No buffer space available的问题,导致接口大面积调用(webservice,httpclient)失败的问题,重启服务器后又恢复了正常. 问题详情 具体异常栈信息如下: Caused by: java.net.SocketException: No buffer space available (maximum connections reached?): connect at org.apache.axis.AxisFault

log buffer space事件(转)

看了这篇文章: Oracle常见的等待事件说明http://database.ctocio.com.cn/tips/38/6669538.shtml 对于Log Buffer Space-日志缓冲空间描述如下: 当你将日志缓冲(log buffer)产生重做日志的速度比LGWR 的写出速度快,或者是当日志切换(log switch)太慢时,就会发生这种等待.这个等待出现时,通常表明redo log buffer 过小,为解决这个问题,可以考虑增大日志文件的大小,或者增加日志缓冲器的大小. 另外一

error:No buffer space available (maximum connections reached

2015-02-02 17:49:09,035 ERROR basic.DBManager - Failded to establish the connection. com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driv

连接db2数据库出现No buffer space available (maximum connections reached?)

Caused by: javax.naming.NamingException: [jcc][t4][2043][11550][3.57.82] 异常 java.net.SocketException:打开端口 50,000 上服务器 localhost/127.0.0.1 的套接字时出错,消息为:No buffer space available (maximum connections reached?): connect. ERRORCODE=-4499, SQLSTATE=08001 数

no buffer space available、由于系统缓冲空间不足或队列已满,不能执行套接字上的操作

netstat -nat | find " " /c   查看连接数才2千多,怎么就报下面的错了 报以下错:(IE打开网站也打不开) no buffer space available(curl 报错). 由于系统缓冲空间不足或队列已满,不能执行套接字上的操作. 超出本地计算机网络适配器卡的名称限制 重启workstation服务.修改MaxUserPort值.修改TcpTimedWaitDelay值这些都不启作用 https://support.microsoft.com/zh-cn

LR报错 No buffer space available Try changing the registry value 端口号不够用了

报错:Action.c(6): Error -27796: Failed to connect to server "10.16.137.8:10035": [10055] No buffer space availableTry changing the registry value HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\tcpip\Parameters\TcpTimedWaitDelay to 30and HKEY

AIX weblogic 启动报There is not enough buffer space

AIX weblogic 启动报There is not enough buffer space for the requested socket operation 错误 <Sep 19, 2014 10:27:09 AM GMT+08:00> <Error> <Coherence> <BEA-000000> <Oracle Coherence 3.6.0.4 (member=n/a): Error while starting cluster: (