当前位置: 首页 > news >正文

tun驱动之open

tun驱动对应的设备文件是:/dev/net/tun,其详细信息如下:

crw-rw-rw- 1 root root 10, 200 2月  26 08:05 tun

主次设备号的定义如下:

#define MISC_MAJOR 10

#define TUN_MINOR 200

由于tun驱动属于misc设备驱动,因此用户调用open打开/dev/net/tun时,会调用到misc_open。

static int misc_open(struct inode *inode, struct file *file)
{int minor = iminor(inode);struct miscdevice *c;int err = -ENODEV;const struct file_operations *new_fops = NULL;mutex_lock(&misc_mtx);// 根据次设备号,查找miscdevice,获取其fops(对于tun设备,就是tun_fops)list_for_each_entry(c, &misc_list, list) {if (c->minor == minor) {new_fops = fops_get(c->fops);break;}}if (!new_fops) {mutex_unlock(&misc_mtx);request_module("char-major-%d-%d", MISC_MAJOR, minor);mutex_lock(&misc_mtx);list_for_each_entry(c, &misc_list, list) {if (c->minor == minor) {new_fops = fops_get(c->fops);break;}}if (!new_fops)goto fail;}/** Place the miscdevice in the file's* private_data so it can be used by the* file operations, including f_op->open below*/file->private_data = c;err = 0;replace_fops(file, new_fops); // 将file->f_ops设置为tun_fopsif (file->f_op->open)err = file->f_op->open(inode, file); // 调用tun_fops
fail:mutex_unlock(&misc_mtx);return err;
}

 代码中已经加了注释,最终调用到tun_fops中的open,即tun_chr_open。

static int tun_chr_open(struct inode *inode, struct file * file)

{

struct net *net = current->nsproxy->net_ns;

struct tun_file *tfile;

DBG1(KERN_INFO, "tunX: tun_chr_open\n");

// sk_alloc返回struct sock类型,tun_file第一个成员是struct sock类型,所以可以进行类型转换

tfile = (struct tun_file *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,

&tun_proto, 0);

if (!tfile)

return -ENOMEM;

RCU_INIT_POINTER(tfile->tun, NULL);

tfile->flags = 0;

tfile->ifindex = 0;

init_waitqueue_head(&tfile->wq.wait);

RCU_INIT_POINTER(tfile->socket.wq, &tfile->wq);

tfile->socket.file = file;

tfile->socket.ops = &tun_socket_ops;

sock_init_data(&tfile->socket, &tfile->sk);

tfile->sk.sk_write_space = tun_sock_write_space;

tfile->sk.sk_sndbuf = INT_MAX;

file->private_data = tfile;

INIT_LIST_HEAD(&tfile->next);

sock_set_flag(&tfile->sk, SOCK_ZEROCOPY);

memset(&tfile->tx_array, 0, sizeof(tfile->tx_array));

return 0;

}

 sk_alloc申请的内存的大小,是由tun_proto.obj_size来指定的。上面的代码中,比较重要的几个地方,已经加粗。下面是涉及到的两个结构。

struct tun_file {

struct sock sk;

struct socket socket;

struct socket_wq wq;

... ...

};

static struct proto tun_proto = {

.name = "tun",

.owner = THIS_MODULE,

.obj_size = sizeof(struct tun_file), // 指定动态申请的tun_file大小

};

最后做下总结:用户空间打开/dev/net/tun驱动文件时,最终是调用tun_chr_open来处理的。在tun_chr_open中申请struct tun_file结构,并进行了相关字段的初始化。

http://www.lryc.cn/news/21604.html

相关文章:

  • 计算机网络体系结构
  • 基础夯实,字节内部总结240道算法LeetCode刷题笔记,直呼太全
  • Three.js使用WebWorker进行八叉树碰撞检测
  • 【教程】Notion笔记多平台设置中文显示
  • [牛客Hot101]链表篇
  • Vue3 核心模块源码解析(上)
  • 【C进阶】指针的高级话题
  • 无源晶振匹配电容—计算方法
  • 【测试】自动化测试03(JUnit)
  • 《计算机视觉和图像处理简介 - 中英双语版》:神经网络中的激活函数 ReLU vs Sigmoid
  • (三十七)大白话SQL标准中对事务的4个隔离级别,都是如何规定的呢?
  • 全国计算机等级考试三级网络技术考试大纲(2022年版)
  • 服务器部署—若依【vue】如何部署到nginx里面?nginx刷新页面404怎么办?【完美解决建议收藏】
  • 算法练习(特辑)算法常用的数据结构、集合和方法总结
  • Apk转Aab(Android-App-Bundle)
  • 大学物理期末大题专题训练总结-热学大题
  • 有趣的Hack-A-Sat黑掉卫星挑战赛——卫星平台内存dump
  • OAK相机如何将yoloV8模型转换成blob格式?
  • Python解题 - CSDN周赛第32期 - 运输石油(三维背包)
  • JVM - G1垃圾收集器深入剖析
  • 角度制与弧度制的相互转换np.deg2radnp.rad2deg
  • 【SAP Abap】X-DOC:SAP ABAP 语法更新之一(Open SQL新增特性)
  • 【改进灰狼优化算法】改进收敛因子和比例权重的灰狼优化算法【期刊论文完美复现】(Matlab代码实现)
  • Linux C代码获取线程ID
  • 基本密码技术
  • 【力扣周赛#334】6369. 左右元素和的差值 + 6368. 找出字符串的可整除数组 + 6367. 求出最多标记下标
  • 行测-判断推理-图形推理-位置规律-平移
  • 数据库基础知识(一)
  • MyBatis 的工作原理解析
  • 终端软件架构说