packetdrill: tun device open: SUCCESS

用packetdirll学习Linux内核网络协议栈,脚本写完后遂在本地测试,遇到了这样的问题,无论运行脚本多少次,终端只显示tun device open:SUCCESS。

对于这个问题,先晒解决方案。
在终端命令上禁用packetdrill在本地模式上创建的虚拟网卡tun0。
ip link set tun0 down

引发问题的原因,是packetdrill中创建的虚拟网卡tun0没有释放,从而在local mode下,packetdrill确实打开了net0,但触发了netdevice.c的loop条件,并以die_perror("open tun device");的形式给出测试不成功的警告。

pcketdrill的netdevice.c

	/* Open the tun device, which "clones" it for our purposes. */
	int tun_fd;
#ifdef linux
	int nb = 0;

loop:
	if (++nb > 10)
		die_perror("open tun device");
#endif
	tun_fd = open(TUN_PATH, O_RDWR);
	if (tun_fd < 0)
		die_perror("open tun device");

	netdev->tun_fd = tun_fd;

#ifdef linux
	/* Create the device. Since we do not specify a device name, the
	 * kernel will try to allocate the "next" device of the specified
	 * type. This device will disappear when we are done.
	 */
	struct ifreq ifr;
	memset(&ifr, 0, sizeof(ifr));
	ifr.ifr_flags = IFF_TUN | IFF_NO_PI | IFF_VNET_HDR;
	int status = ioctl(netdev->tun_fd, TUNSETIFF, (void *)&ifr);
	if (status < 0)
		die_perror("TUNSETIFF");

	/* Our tests rely on using tun0.
	 * We might change this in the future, by passing a variable filled
	 * with tunnel name. In the mean time, wait a bit that tun0 gets free.
	 */
	if (strcmp(ifr.ifr_name, "tun0")) {
		close(tun_fd);
		usleep(100000);
		goto loop;
	}
	netdev->name = strdup(ifr.ifr_name);
#endif

问题在于虚拟网卡tun0的存在,if (strcmp(ifr.ifr_name, "tun0"))总成立,变量nb 的值不断累加。当变量的值不断上升,loop控制块的条件if(++nd>10)会成立。测试开始后,即总在这报错退出。

希望自己能在使用packetdrill时,给社区多提供一些packetdrill的日常使用手册,毕竟packetdrill方面的东西太少了,处处碰壁着实蓝瘦。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值