gpt4 book ai didi

c - 尝试从 tun 接口(interface)获取超过 8 个队列文件描述符时 ioctl 失败

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:23:24 27 4
gpt4 key购买 nike

在我听说内核 3.8 linux 添加了多队列功能以使用标志 IFF_MULTI_QUEUE 调整 tap 设备后,我将内核升级到 3.10 并将其 header 放在/usr/src然后我改变了我的c代码,让一个线程在每次需要时打开一个新的队列文件描述符。但是线程只能打开 8 个队列(使用早于 3.8 的内核,它根本无法打开队列),之后我从 ioctl 中得到这个“参数太长”错误

ioctl(fd, TUNSETIFF, (void *)&ifr)

然后我写了另一个程序来测试在我的 ubuntu 12.10 linux box 中可以打开多少队列 fd

uname -r
3.10.0-031000-generic

一个更简单的程序中的内核版本。

//essential
#include <net/if.h>
#include <linux/if_tun.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <arpa/inet.h>
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/time.h>
#include <errno.h>
#include <stdarg.h>

#include <netdb.h>

#define IFF_MULTI_QUEUE 0x0100
int tun_alloc_mq(char *dev, int queues, int *fds)
{
struct ifreq ifr;
int fd, err=-1, i;
char *clonedev = "/dev/net/tun";

if (!dev){
printf("dev");
return -1;
}

memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_MULTI_QUEUE;
strcpy(ifr.ifr_name, dev);
int error=0;

for (i = 0; i < queues; i++) {

printf("loop %d\n",i);

if( (fd = open(clonedev , O_RDWR)) < 0 ) {
perror("Opening /dev/net/tun");
error=1;
break;
}

if(ioctl(fd, TUNSETIFF, (void *)&ifr) < 0 ) {
printf("first error\n");
error=1;
close(fd);
break;
}
}
if(error==1)
return -1;
return 0;
}
int main(int argc, char *argv[])
{

int* fdsx;
if(tun_alloc_mq("testmqtun0",20,fdsx)<0)
{
perror("tun");
exit(1);
}
return 0;
}

事实证明这也限制为 8 个。这是输出:

loop 0
loop 1
loop 2
loop 3
loop 4
loop 5
loop 6
loop 7
loop 8
first error
tun: Argument list too long

我在另一个 linux 机器上测试了它,它有相同的输出。那么从内核中的 tun 设备打开超过 8 个队列是否有限制?如果是这样,如何解决?请帮我解决这个问题

最佳答案

如果你想从一个tun接口(interface)获取超过8个队列的文件描述符,你可以将你的linux内核升级到4.0+,它支持256个队列。

关于c - 尝试从 tun 接口(interface)获取超过 8 个队列文件描述符时 ioctl 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17504076/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com