概述

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的更多相关文章

  1. 邻居子系统 之 邻居表的初始化neigh_table_init

    概述 邻居子系统支持多种实现,例如ARP,ND等,这些实现需要在其初始化的时候,调用neigh_table_init将邻居表项添加到全局邻居子系统数组中,并对实例中的字段(如hash,定时器等)进行相 ...

  2. 邻居子系统 之 邻居项查找neigh_lookup、___neigh_lookup_noref

    概述 邻居项查找是通过neigh_lookup相关函数来进行的: ___neigh_lookup_noref,该函数根据输出设备和主键值(IPv4为下一跳ip地址)在邻居项hash表中查找,找到则返回 ...

  3. 邻居子系统 之 状态定时器回调neigh_timer_handler

    概述 在分配邻居子系统之后,会设置定时器来处理那些需要定时器处理的状态,定时器回调函数为neigh_timer_handler:函数会根据状态机变换规则对状态进行切换,切换状态后,如果需要更新输出函数 ...

  4. 邻居子系统 之 更新neigh_update

    概述 neigh_update函数用来更新指定的邻居项,更新内容是硬件地址和状态,更新之后,会根据新状态设置其输出函数,CONNECTED状态则使用快速输出,否则使用慢速输出:如果是由原来的无效状态变 ...

  5. 邻居子系统输出 之 neigh_output、neigh_hh_output

    概述 ip层在构造好ip头,检查完分片之后,会调用邻居子系统的输出函数neigh_output进行输出,输出分为有二层头缓存和没有两种情况,有缓存时调用neigh_hh_output进行快速输出,没有 ...

  6. ubuntu 启动项创建器 选择不了CD镜像,IOS镜像的解决方法

    自己系统是ubuntu14.04 , 想使用 ubuntu自带的启动项创建器(usb-creator-gtk)做一个CDLinux的U盘启动项, 打开程序后发现U盘识别了, 在添加镜像的时候,发现怎么 ...

  7. 自学Zabbix4.2 web监控项创建+item详解

    自学Zabbix4.2 web监控项创建+item详解 1. web监控项创建 1.1  Scenario 选项卡 Name: 监控项的名称 Application: 放到哪个应用中 Authenti ...

  8. 《深入理解Linux网络技术内幕》阅读笔记 --- 邻居子系统

    1.封包从L3至L2的传送过程如下所示: 本地主机的路由子系统选择L3目的地址(下一个跃点). 根据路由表,如果下一个跃点在同一个网络中,邻居层就把目的L3地址解析为跃点的L2地址.这个关联会被放入缓 ...

  9. 邻居子系统1.5 neigh output

    1.5.1 当邻居项不处于NUD_CONNECTD状态时,不允许快速路径发送报文,函数neigh_resolve_output 用于慢而安全的输出,通常用初始化neigh_ops结构 来实例outpu ...

随机推荐

  1. Laravel使用whereHas进行过滤不符合条件的预加载with数据

    问题描述:目前有用户表,文章表,文章评论表,收藏表.我需要获我的收藏文章列表(可以被搜索,通过分类,文章标题等),通过收藏预加载with文章表,文章评论表,文章用户表 解决办法:通过whereHas限 ...

  2. vue入门:(class与style绑定)

    对象语法 数组语法 一.对象语法 1.1对象语法绑定HTML Class 语法:v-bind:class="{'className1':boolean1,'className2':boole ...

  3. 搭建vue.js 的npm脚手架

    1.在cmd中,找到nodeJs安装的路径下,运行 vue -V,查看当前vue版本,如下图所示,表明已经安装过了. 2.没有安装,进行安装.在cmd中,找到nodeJs安装的路径下,运命令行 npm ...

  4. mysql 中的 tinyint 字段

    只能存储  -128 ~ 127  之间的数字

  5. 第六章·Logstash深入-收集java日志

    1.通过Logstash收集java日志并输出到ES中 因为我们现在需要用Logstash收集tomcat日志,所以我们暂时将tomcat安装到Logstash所在机器,也就是db03:10.0.0. ...

  6. inputrc命令

    问题:搭建ubuntu系统后,输入命令的第一个字符+上页按键,发现不能找到历史命令,找了好久才发现是因为/etc/inputrc没有对键盘映射的上页键和下页键进行设置. 解决方法: 修改文件/etc/ ...

  7. /proc/filesystems各字段含义

    /proc/filesystems A text listing of the filesystems which were compiled into the kernel. Incidentall ...

  8. Galera Cluster 实现mysql的高可用 (Percona XtraDB Cluster)

    Galera Cluster 实现mysql的高可用 (Percona XtraDB Cluster) # 基础搭建 # 设备:三台主机 192.168.47.101 192.168.47.102 1 ...

  9. .NET 树型递归

    /// <summary> /// 获取全部水价标准模型 /// </summary> /// <returns></returns> public I ...

  10. Winform的高DPI问题

    现在的屏幕大部分都是高分屏,在这样的屏幕下开发winfrom软件就需要注意高DPI问题了 1.Form和UserControl的AutoScaleMode设置为Dpi 2.为项目添加应用程序清单文件( ...