尧图网络科技YAOTU DIGITAL 获取报价
获取报价
首页 / 资讯中心 / 文章详情

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

发布时间:2026/9/16 9:44:06

资讯中心
01
ARTICLE

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

【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);
02
RELATED NEWS

相关资讯

更多网站建设与数字化升级内容

03
WHY YAOTU

想打造同款高转化官网?

懂行业、懂生意,从建站到增长一站式陪跑

场景化定制

不做模板站,围绕你的业务场景量身设计,小众不撞款。

营销型架构

以转化目标组织内容与路径,让官网真正带来询盘。

全周期服务

设计、开发、运营、运维一体,上线只是开始。

免费获取你的建站方案

留下需求,专属顾问 24 小时内为你输出方案建议。