新闻详情

新闻详情

首页 / 资讯中心 / 详情

【2016-01-21】LinuxLoopBack简单笔记

发布时间:2026/9/16 9:43:58来源:尧图网络
【2016-01-21】LinuxLoopBack简单笔记
[历史归档]本文原发布于 cstriker1407.info 个人博客内容为历史存档仅供参考。发布时间2016-01-21 标题LinuxLoopBack简单笔记分类操作系统 / linux / kernel 标签linux·kernel·loopbackLinuxLoopBack简单笔记在Linux网络设备中有一个网络回环设备代码非常简单这里笔记下:/* * INET An implementation of the TCP/IP protocol suite for the LINUX * operating system. INET is implemented using the BSD Socket * interface as the means of communication with the user level. * * Pseudo-driver for the loopback interface. * * Version: (#)loopback.c 1.0.4b 08/16/93 * * Authors: Ross Biro, bir7leland.Stanford.Edu * Fred N. van Kempen, waltjeuWalt.NL.Mugnet.ORG * Donald Becker, beckerscyld.com * * Alan Cox : Fixed oddments for NET3.014 * Alan Cox : Rejig for NET3.029 snap #3 * Alan Cox : Fixed NET3.029 bugs and sped up * Larry McVoy : Tiny tweak to double performance * Alan Cox : Backed out LMVs tweak - the linux mm * cant take it... * Michael Griffith: Dont bother computing the checksums * on packets received on the loopback * interface. * Alexey Kuznetsov: Potential hang under some extreme * cases removed. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version * 2 of the License, or (at your option) any later version. */#includelinux/kernel.h#includelinux/jiffies.h#includelinux/module.h#includelinux/interrupt.h#includelinux/fs.h#includelinux/types.h#includelinux/string.h#includelinux/socket.h#includelinux/errno.h#includelinux/fcntl.h#includelinux/in.h#includelinux/init.h#includeasm/system.h#includeasm/uaccess.h#includeasm/io.h#includelinux/inet.h#includelinux/netdevice.h#includelinux/etherdevice.h#includelinux/skbuff.h#includelinux/ethtool.h#includenet/sock.h#includenet/checksum.h#includelinux/if_ether.h/* For the statistics structure. */#includelinux/if_arp.h/* For ARPHRD_ETHER */#includelinux/ip.h#includelinux/tcp.h#includelinux/percpu.hstaticDEFINE_PER_CPU(structnet_device_stats,loopback_stats);#defineLOOPBACK_OVERHEAD(128MAX_HEADER1616)/* KISS: just allocate small chunks and copy bits. * * So, in fact, this is documentation, explaining what we expect * of largesending device modulo TCP checksum, which is ignored for loopback. *//* 如果包很大就拆分成多个skb 调用收包接口 */staticvoidemulate_large_send_offload(structsk_buff*skb){structiphdr*iphskb-nh.iph;structtcphdr*th(structtcphdr*)(skb-nh.raw(iph-ihl*4));unsignedintdoffset(iph-ihlth-doff)*4;unsignedintmtuskb_shinfo(skb)-tso_sizedoffset;unsignedintoffset0;u32 seqntohl(th-seq);u16 idntohs(iph-id);while(offsetdoffsetskb-len){unsignedintfrag_sizemin(mtu,skb-len-offset)-doffset;structsk_buff*nskballoc_skb(mtu32,GFP_ATOMIC);/* 新的skb */if(!nskb)break;skb_reserve(nskb,32);nskb-mac.rawnskb-data-14;nskb-nh.rawnskb-data;iphnskb-nh.iph;memcpy(nskb-data,skb-nh.raw,doffset);if(skb_copy_bits(skb,doffsetoffset,nskb-datadoffset,frag_size))/* 拷贝新的skb 内容 */BUG();skb_put(nskb,doffsetfrag_size);nskb-ip_summedCHECKSUM_UNNECESSARY;nskb-devskb-dev;nskb-priorityskb-priority;nskb-protocolskb-protocol;nskb-dstdst_clone(skb-dst);memcpy(nskb-cb,skb-cb,sizeof(skb-cb));nskb-pkt_typeskb-pkt_type;th(structtcphdr*)(nskb-nh.rawiph-ihl*4);iph-tot_lenhtons(frag_sizedoffset);iph-idhtons(id);iph-check0;iph-checkip_fast_csum((unsignedchar*)iph,iph-ihl);th-seqhtonl(seq);if(offsetdoffsetfrag_sizeskb-len)th-finth-psh0;netif_rx(nskb);/* 上送 */offsetfrag_size;seqfrag_size;id;}dev_kfree_skb(skb);}/* * The higher levels take care of making this non-reentrant (its * called with bhs disabled). *//* 发送回调函数 */staticintloopback_xmit(structsk_buff*skb,structnet_device*dev){structnet_device_stats*lb_stats;skb_orphan(skb);skb-protocoleth_type_trans(skb,dev);skb-devdev;#ifndefLOOPBACK_MUST_CHECKSUMskb-ip_summedCHECKSUM_UNNECESSARY;#endifif(skb_shinfo(skb)-tso_size){BUG_ON(skb-protocol!htons(ETH_P_IP));BUG_ON(skb-nh.iph-protocol!IPPROTO_TCP);emulate_large_send_offload(skb);return0;}dev-last_rxjiffies;lb_statsper_cpu(loopback_stats,get_cpu());lb_stats-rx_bytesskb-len;lb_stats-tx_bytesskb-len;lb_stats-rx_packets;lb_stats-tx_packets;put_cpu();/* 当拿到要发送的包之后直接调用收包即可。 */netif_rx(skb);return(0);}staticstructnet_device_stats*get_stats(structnet_device*dev){structnet_device_stats*statsdev-priv;inti;if(!stats){returnNULL;}memset(stats,0,sizeof(structnet_device_stats));for(i0;iNR_CPUS;i){structnet_device_stats*lb_stats;if(!cpu_possible(i))continue;lb_statsper_cpu(loopback_stats,i);stats-rx_byteslb_stats-rx_bytes;stats-tx_byteslb_stats-tx_bytes;stats-rx_packetslb_stats-rx_packets;stats-tx_packetslb_stats-tx_packets;}returnstats;}u32loopback_get_link(structnet_device*dev){return1;}staticstructethtool_opsloopback_ethtool_ops{.get_linkloopback_get_link,.get_tsoethtool_op_get_tso,.set_tsoethtool_op_set_tso,};/* 由于loopback是在本机内循环因此非常简单只需要重写发送函数即可。 */structnet_deviceloopback_dev{.namelo,.mtu(16*1024)202012,.hard_start_xmitloopback_xmit,/* 发送回调函数 */.hard_headereth_header,.hard_header_cacheeth_header_cache,.header_cache_updateeth_header_cache_update,.hard_header_lenETH_HLEN,/* 14 */.addr_lenETH_ALEN,/* 6 */.tx_queue_len0,.typeARPHRD_LOOPBACK,/* 0x0001*/.rebuild_headereth_rebuild_header,.flagsIFF_LOOPBACK,.featuresNETIF_F_SG|NETIF_F_FRAGLIST|NETIF_F_NO_CSUM|NETIF_F_HIGHDMA|NETIF_F_LLTX,.ethtool_opsloopback_ethtool_ops,};/* loopback 模块初始化 *//* Setup and register the of the LOOPBACK device. */int__initloopback_init(void){structnet_device_stats*stats;/* loopback模块非常简单只需要额外申请内存来统计收发包个数即可。 *//* Can survive without statistics */statskmalloc(sizeof(structnet_device_stats),GFP_KERNEL);if(stats){memset(stats,0,sizeof(structnet_device_stats));loopback_dev.privstats;loopback_dev.get_statsget_stats;}/* 注册网络设备 */returnregister_netdev(loopback_dev);};EXPORT_SYMBOL(loopback_dev);
网站建设高端定制企业官网
RELATED

相关资讯

更多精彩内容,欢迎继续阅读

较早相关资讯

最新相关资讯

Meta内部AI路线之争:学术严谨vs商业创新 2026/9/16 10:26:26

Meta内部AI路线之争:学术严谨vs商业创新

1. 项目概述:Meta内部动荡与AI领域的分歧最近Meta首席AI科学家Yann LeCun对Scale AI创始人Alexandr Wang的评论引发了业内广泛讨论。作为一名在AI领域工作多年的从业者,我想从技术角度解析这场争论背后的深层原因,以及它反映出的AI发展方向之…

阅读更多 →
AI时代机器人测试范式迁移:仿真驱动的场景覆盖率工程 2026/9/16 10:26:26

AI时代机器人测试范式迁移:仿真驱动的场景覆盖率工程

1. 为什么“机器人测试”在AI时代突然成了高频词?——从工业现场到具身智能实验室的真实断层你有没有注意过,最近半年,朋友圈里做自动化产线的工程师、高校搞机器人方向的博士生、甚至做智能硬件创业的CEO,都在聊“机器人测试”。…

阅读更多 →
npm核心机制与依赖管理常见报错排查指南 2026/9/16 10:26:26

npm核心机制与依赖管理常见报错排查指南

做前端和 Node.js 开发的人,没有谁没敲过npm install。但我发现一个很有意思的现象:很多同学能熟练跑命令,却说不清 npm 到底是怎么工作的。装包装到一半卡住、报错报得看不懂、换个环境就装不上依赖,这些问题本质上都源于对 npm …

阅读更多 →
Flutter与OpenHarmony开发21点游戏实战 2026/9/16 10:26:26

Flutter与OpenHarmony开发21点游戏实战

1. 项目背景与核心价值在跨平台开发领域,Flutter与OpenHarmony的结合正在开辟新的技术路径。这个21点游戏项目完美展示了如何利用Flutter框架在OpenHarmony系统上构建完整的游戏应用。不同于简单的UI演示,该项目实现了包括牌组管理、胜负判定、状态流转在…

阅读更多 →
AI Agent核心技术解析:从架构设计到行业落地 2026/9/16 10:26:26

AI Agent核心技术解析:从架构设计到行业落地

1. AI Agent的本质与定义AI Agent(人工智能代理)本质上是一个具备自主决策能力的智能系统。不同于传统程序只能执行预设指令,AI Agent更像是一个"数字员工",能够感知环境、分析信息并主动采取行动。这种技术突破让机器从…

阅读更多 →
PCIe L1.2低功耗本质:链路控制权移交与状态跃迁鲁棒性 2026/9/16 10:23:25

PCIe L1.2低功耗本质:链路控制权移交与状态跃迁鲁棒性

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

阅读更多 →

今日资讯

本周资讯

本月资讯

看完文章仍有疑问?

联系尧图顾问,获取一对一建站咨询

立即免费咨询 📞 400-888-8888
📞