11、控制eth网卡名字顺序
一、网卡名字设置
分析一下__dev_alloc_name
0、获取一块物理页
1、先扫描eth
网卡
2、设置位
3、取出未被占用的位
C
static int __dev_alloc_name(struct net *net, const char *name, char *buf)
{
int i = 0;
const char *p;
const int max_netdevices = 8*PAGE_SIZE;
unsigned long *inuse;
struct net_device *d;
if (!dev_valid_name(name))
return -EINVAL;
p = strchr(name, '%');
if (p) {
/*
* Verify the string as this thing may have come from
* the user. There must be either one "%d" and no other "%"
* characters.
*/
if (p[1] != 'd' || strchr(p + 2, '%'))
return -EINVAL;
//在中断、软中断、spinlock等原子上下文里面,申请内存,应该使用GFP_ATOMIC标记
/* Use one page as a bit array of possible slots */
inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
if (!inuse)
return -ENOMEM;
for_each_netdev(net, d) {
if (!sscanf(d->name, name, &i))
continue;
if (i < 0 || i >= max_netdevices)
continue;
/* avoid cases where sscanf is not exact inverse of printf */
snprintf(buf, IFNAMSIZ, name, i);
if (!strncmp(buf, d->name, IFNAMSIZ))
set_bit(i, inuse);
}
i = find_first_zero_bit(inuse, max_netdevices);
+if(eth_flag){
+ eth_flag = 0;
+ i = 1;
+}
+printk("eth is number %d\n", i);
free_page((unsigned long) inuse);
}
snprintf(buf, IFNAMSIZ, name, i);
if (!__dev_get_by_name(net, buf))
return i;
/* It is possible to run out of possible slots
* when the name is long and there isn't enough space left
* for the digits, or if all bits are used.
*/
return -ENFILE;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
1.1、get_zeroed_page
get_zeroed_page( )
函数获取一个物理页,它保证该页不属于高端内存,并将该页的内容清零。
get_zeroed_page
文件包含:
C
#include<linux/gfp.h>
1
get_zeroed_page
函数定义: 位于:mm/page_alloc.c
C
unsigned long get_zeroed_page(gfp_t gfp_mask)
1
输入参数说明:
gft_mask
:是分配标志,它提供了多种分配内存的方式,指示内核如何分配和在哪分配所需的内存,其常见取值及说明参考alloc_pages( )
函数的分析。返回参数说明: 返回值是一个无符号长整型,它表示所分配的页的起始地址,该地址是一个逻辑地址。
1.2、Bitmap 查找操作
Bitmap
同样提供了一套用于查找特定 bit
的函数接口,以便在 bitmap
中找到指定 的 bit
,函数接口及实践入口如下:
bitmap_find_next_zero_area_off
:bitmap
中找到整块零区域find_first_bit
:bitmap
中找到第一个置位的位置find_first_zero_bit
:bitmap
中找到第一个清零的位置find_last_bit
:bitmap
中找到最后一个置位的位置
1.3、set_bit
C
set_bit(int n, unsigned long *addr) //将addr位图中的第n位设置为1
1