邻居子系统 之 邻居项创建__neigh_create
概述
IP层输出数据包会根据路由的下一跳查询邻居项,如果不存在则会调用__neigh_create创建邻居项,然后调用邻居项的output函数进行输出;
__neigh_create完成邻居项的创建,进行初始化之后,加入到邻居项hash表,然后返回,其中,如果hash表中有与新建邻居项相同的项会复用该项,新建项会被释放;
neigh_alloc完成邻居项的分配,分配成功后会设定定时器来检查和更新邻居项的状态;
源码分析
struct neighbour *__neigh_create(struct neigh_table *tbl, const void *pkey,
struct net_device *dev, bool want_ref)
{
u32 hash_val;
int key_len = tbl->key_len;
int error;
/* 分配邻居项n */
struct neighbour *n1, *rc, *n = neigh_alloc(tbl, dev);
struct neigh_hash_table *nht; if (!n) {
rc = ERR_PTR(-ENOBUFS);
goto out;
} /* 拷贝主键值,ipv4为下一跳ip地址 */
memcpy(n->primary_key, pkey, key_len);
/* 设置输出设备 */
n->dev = dev;
dev_hold(dev); /* Protocol specific setup. */
/* 执行邻居表的邻居项初始化函数,ARP为arp_constructor */
if (tbl->constructor && (error = tbl->constructor(n)) < ) {
rc = ERR_PTR(error);
goto out_neigh_release;
} /* 执行设备的邻居项初始化 */
if (dev->netdev_ops->ndo_neigh_construct) {
error = dev->netdev_ops->ndo_neigh_construct(dev, n);
if (error < ) {
rc = ERR_PTR(error);
goto out_neigh_release;
}
} /* Device specific setup. */
/* 老式设备的邻居项初始化 */
if (n->parms->neigh_setup &&
(error = n->parms->neigh_setup(n)) < ) {
rc = ERR_PTR(error);
goto out_neigh_release;
} /* 设置邻居项的确认时间 */
n->confirmed = jiffies - (NEIGH_VAR(n->parms, BASE_REACHABLE_TIME) << ); write_lock_bh(&tbl->lock); /* 获取hash */
nht = rcu_dereference_protected(tbl->nht,
lockdep_is_held(&tbl->lock)); /* hash扩容 */
if (atomic_read(&tbl->entries) > ( << nht->hash_shift))
nht = neigh_hash_grow(tbl, nht->hash_shift + ); /* 计算hash值 */
hash_val = tbl->hash(pkey, dev, nht->hash_rnd) >> ( - nht->hash_shift); /* 邻居项的配置参数正在被删除,不能继续初始化 */
if (n->parms->dead) {
rc = ERR_PTR(-EINVAL);
goto out_tbl_unlock;
} /* 遍历hash */
for (n1 = rcu_dereference_protected(nht->hash_buckets[hash_val],
lockdep_is_held(&tbl->lock));
n1 != NULL;
n1 = rcu_dereference_protected(n1->next,
lockdep_is_held(&tbl->lock))) {
/* 找到相同的邻居项,则使用之 */
if (dev == n1->dev && !memcmp(n1->primary_key, pkey, key_len)) {
if (want_ref)
neigh_hold(n1);
rc = n1;
/* 解锁,释放新的邻居项 */
goto out_tbl_unlock;
}
} /* 不存在,则添加新邻居项到hash */
n->dead = ;
if (want_ref)
neigh_hold(n);
rcu_assign_pointer(n->next,
rcu_dereference_protected(nht->hash_buckets[hash_val],
lockdep_is_held(&tbl->lock)));
rcu_assign_pointer(nht->hash_buckets[hash_val], n);
write_unlock_bh(&tbl->lock);
neigh_dbg(, "neigh %p is created\n", n); /* 返回新的邻居项 */
rc = n;
out:
return rc;
out_tbl_unlock:
write_unlock_bh(&tbl->lock);
out_neigh_release:
neigh_release(n);
goto out;
}
static struct neighbour *neigh_alloc(struct neigh_table *tbl, struct net_device *dev)
{
struct neighbour *n = NULL;
unsigned long now = jiffies;
int entries; /* 增加并返回邻居项数量 */
entries = atomic_inc_return(&tbl->entries) - ; /* 超过限额,则进行回收 */
if (entries >= tbl->gc_thresh3 ||
(entries >= tbl->gc_thresh2 &&
time_after(now, tbl->last_flush + * HZ))) {
if (!neigh_forced_gc(tbl) &&
entries >= tbl->gc_thresh3) {
net_info_ratelimited("%s: neighbor table overflow!\n",
tbl->id);
NEIGH_CACHE_STAT_INC(tbl, table_fulls);
goto out_entries;
}
} /* 分配邻居项 */
n = kzalloc(tbl->entry_size + dev->neigh_priv_len, GFP_ATOMIC);
if (!n)
goto out_entries; /* 成员初始化 */
__skb_queue_head_init(&n->arp_queue);
rwlock_init(&n->lock);
seqlock_init(&n->ha_lock);
n->updated = n->used = now;
n->nud_state = NUD_NONE;
n->output = neigh_blackhole;
seqlock_init(&n->hh.hh_lock);
n->parms = neigh_parms_clone(&tbl->parms); /* 设置定时器,检查和调整邻居项状态 */
setup_timer(&n->timer, neigh_timer_handler, (unsigned long)n); NEIGH_CACHE_STAT_INC(tbl, allocs); /* 关联邻居表 */
n->tbl = tbl;
atomic_set(&n->refcnt, );
n->dead = ;
out:
return n; out_entries:
atomic_dec(&tbl->entries);
goto out;
}
邻居子系统 之 邻居项创建__neigh_create的更多相关文章
- 邻居子系统 之 邻居表的初始化neigh_table_init
概述 邻居子系统支持多种实现,例如ARP,ND等,这些实现需要在其初始化的时候,调用neigh_table_init将邻居表项添加到全局邻居子系统数组中,并对实例中的字段(如hash,定时器等)进行相 ...
- 邻居子系统 之 邻居项查找neigh_lookup、___neigh_lookup_noref
概述 邻居项查找是通过neigh_lookup相关函数来进行的: ___neigh_lookup_noref,该函数根据输出设备和主键值(IPv4为下一跳ip地址)在邻居项hash表中查找,找到则返回 ...
- 邻居子系统 之 状态定时器回调neigh_timer_handler
概述 在分配邻居子系统之后,会设置定时器来处理那些需要定时器处理的状态,定时器回调函数为neigh_timer_handler:函数会根据状态机变换规则对状态进行切换,切换状态后,如果需要更新输出函数 ...
- 邻居子系统 之 更新neigh_update
概述 neigh_update函数用来更新指定的邻居项,更新内容是硬件地址和状态,更新之后,会根据新状态设置其输出函数,CONNECTED状态则使用快速输出,否则使用慢速输出:如果是由原来的无效状态变 ...
- 邻居子系统输出 之 neigh_output、neigh_hh_output
概述 ip层在构造好ip头,检查完分片之后,会调用邻居子系统的输出函数neigh_output进行输出,输出分为有二层头缓存和没有两种情况,有缓存时调用neigh_hh_output进行快速输出,没有 ...
- ubuntu 启动项创建器 选择不了CD镜像,IOS镜像的解决方法
自己系统是ubuntu14.04 , 想使用 ubuntu自带的启动项创建器(usb-creator-gtk)做一个CDLinux的U盘启动项, 打开程序后发现U盘识别了, 在添加镜像的时候,发现怎么 ...
- 自学Zabbix4.2 web监控项创建+item详解
自学Zabbix4.2 web监控项创建+item详解 1. web监控项创建 1.1 Scenario 选项卡 Name: 监控项的名称 Application: 放到哪个应用中 Authenti ...
- 《深入理解Linux网络技术内幕》阅读笔记 --- 邻居子系统
1.封包从L3至L2的传送过程如下所示: 本地主机的路由子系统选择L3目的地址(下一个跃点). 根据路由表,如果下一个跃点在同一个网络中,邻居层就把目的L3地址解析为跃点的L2地址.这个关联会被放入缓 ...
- 邻居子系统1.5 neigh output
1.5.1 当邻居项不处于NUD_CONNECTD状态时,不允许快速路径发送报文,函数neigh_resolve_output 用于慢而安全的输出,通常用初始化neigh_ops结构 来实例outpu ...
随机推荐
- C# 斐波那契数列 第n项数字/前n项的和
static void Main(string[] args) { int a = Convert.ToInt32(Console.ReadLine()); //求第n位数字是多少 Console.W ...
- 转载--Java中的PO、DO、DTO、 VO的概念
Java中的PO.DO.DTO. VO的概念 写的很清晰,学习了.
- HttpClient 源码阅读
在项目中常用的HttpClient,与我们非常的亲密,为了能处理遇到的Http问题,我们应该去了解里面的机制和实现. 官方文档:http://hc.apache.org/httpcomponents- ...
- 第十一章· MHA高可用及读写分离
一.MHA简介 1.1.作者简介 松信嘉範: MySQL/Linux专家 2001年索尼公司入职 2001年开始使用oracle 2004年开始使用MySQL 2006年9月-2010年8月MySQL ...
- 适配器 1、ArrayAdapter 2.SimpleAdapter
1.ArrayAdapter(数组适配器):用于绑定格式单一的数据.数据源:可以是集合或数组 public class MainActivity extends AppCompatActivity { ...
- Jmeter (二) 参数化
一.数据 用户 参数化 1.添加 用户参数 添加——>前置处理器 ——>用户参数 2.设置 目标参数 3.变量代替 ${name} 4.线程组 设置循环次数,查看结果数中查看结果 thre ...
- springboot配置对jsp页面的解析支持
pom.xml文件配置依赖信息 <!--引入Spring Boot内嵌的Tomcat对JSP的解析包,不加解析不了jsp页面--> <dependency> <group ...
- endpoint
你把机器关机一次,估计被你只写满不读,限速死锁了,因为目前没有心跳控制
- 在ASP.NET MVC项目中使用极验验证(geetest)
时间 2016-03-02 18:22:37 smallerpig 原文 http://www.smallerpig.com/979.html 主题 ASP.NET MVC geetest开发体 ...
- LabVIEW中的波形图表(Chart)与波形图(Graph)
在百度上随便找的一个简单的例子: 具体链接:https://jingyan.baidu.com/article/5552ef47deb996518ffbc983.html 波形图表最先出现数据,等待所 ...