Chinaunix首页 | 论坛 | 博客
  • 博客访问: 476514
  • 博文数量: 223
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2145
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-01 10:23
个人简介

该坚持的时候坚持,该妥协的时候妥协,该放弃的时候放弃

文章分类

全部博文(223)

文章存档

2017年(56)

2016年(118)

2015年(3)

2014年(46)

我的朋友

分类: 嵌入式

2016-11-23 21:08:26

一、loopback
协议栈需要发送数据时,将数据发送给网卡,网卡通过介质发送。而loopback回环网卡,是自发自收的。

二、代码分析

  1. /*
  2.  * INET        An implementation of the TCP/IP protocol suite for the LINUX
  3.  *        operating system. INET is implemented using the BSD Socket
  4.  *        interface as the means of communication with the user level.
  5.  *
  6.  *        Pseudo-driver for the loopback interface.
  7.  *
  8.  * Version:    @(#)loopback.c    1.0.4b    08/16/93
  9.  *
  10.  * Authors:    Ross Biro
  11.  *        Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12.  *        Donald Becker, <becker@scyld.com>
  13.  *
  14.  *        Alan Cox    :    Fixed oddments for NET3.014
  15.  *        Alan Cox    :    Rejig for NET3.029 snap #3
  16.  *        Alan Cox    :     Fixed NET3.029 bugs and sped up
  17.  *        Larry McVoy    :    Tiny tweak to double performance
  18.  *        Alan Cox    :    Backed out LMV's tweak - the linux mm
  19.  *                    can't take it...
  20.  * Michael Griffith: Don't bother computing the checksums
  21.  * on packets received on the loopback
  22.  * interface.
  23.  *        Alexey Kuznetsov:    Potential hang under some extreme
  24.  *                    cases removed.
  25.  *
  26.  *        This program is free software; you can redistribute it and/or
  27.  *        modify it under the terms of the GNU General Public License
  28.  *        as published by the Free Software Foundation; either version
  29.  *        2 of the License, or (at your option) any later version.
  30.  */
  31. #include <linux/kernel.h>
  32. #include <linux/jiffies.h>
  33. #include <linux/module.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/fs.h>
  36. #include <linux/types.h>
  37. #include <linux/string.h>
  38. #include <linux/socket.h>
  39. #include <linux/errno.h>
  40. #include <linux/fcntl.h>
  41. #include <linux/in.h>
  42. #include <linux/init.h>

  43. #include <asm/system.h>
  44. #include <asm/uaccess.h>
  45. #include <asm/io.h>

  46. #include <linux/inet.h>
  47. #include <linux/netdevice.h>
  48. #include <linux/etherdevice.h>
  49. #include <linux/skbuff.h>
  50. #include <linux/ethtool.h>
  51. #include <net/sock.h>
  52. #include <net/checksum.h>
  53. #include <linux/if_ether.h>    /* For the statistics structure. */
  54. #include <linux/if_arp.h>    /* For ARPHRD_ETHER */
  55. #include <linux/ip.h>
  56. #include <linux/tcp.h>
  57. #include <linux/percpu.h>
  58. #include <net/net_namespace.h>

  59. unsigned long bytes = 0;
  60. unsigned long packets = 0;
  61. struct net_device *dev;                                                                            //创建net_device结构

  62. int loopback_xmit(struct sk_buff *skb, struct net_device *dev)                                     //发送函数
  63. {
  64.     skb->protocol = eth_type_trans(skb, dev);                                            //获取网络包协议

  65.     bytes += skb->len;                                                                  //获取数据包的长度
  66.     packets++;                                                                          //包的量
  67.     
  68.     netif_rx(skb);                                                                      //发送skb信息

  69.     return 0;
  70. }

  71. struct net_device_stats *loopback_get_stats(struct net_device *dev)                     //获取状态
  72. {
  73.     struct net_device_stats *stats = &dev->stats;
  74.     
  75.     stats->rx_bytes = bytes;
  76.     stats->tx_bytes = bytes;
  77.     stats->rx_packets = packets;
  78.     stats->tx_packets = packets;

  79.     return &stats;
  80. }

  81. struct net_device_ops loopback_ops =                                                     //net_device_ops是回环网卡的函数结构指针 
  82. {
  83.     .ndo_start_xmit = loopback_xmit,
  84.     .ndo_get_stats = loopback_get_stats,
  85. };

  86. void loopback_setup(struct net_device *dev)
  87. {
  88.     dev->mtu = (16*1024)+20+20+12;                                                       //16kB+tcp+ip+net,设置最大的空间
  89.     dev->flags = IFF_LOOPBACK;                                                           //表明是loopback函数
  90.     dev->header_ops = &eth_header_ops;                                                   //使用默认的header_ops
  91.     dev->netdev_ops = &loopback_ops;                                                     //使用自定义的netdev_ops
  92. }

  93. /* Setup and register the loopback device. */
  94. static __net_init int loopback_net_init(struct net *net)
  95. {
  96.     int err = -ENOMEM;

  97.     //1.
  98.     dev = alloc_netdev(0, "lo", loopback_setup);                                       //给dev分配空间,0是自定义内容大小,lo是显示的名字,loopback_setup是初始化函数
  99.     
  100.     //4.
  101.     err = register_netdev(dev);                                                        //注册dev
  102.     
  103.     net->loopback_dev = dev;

  104.     return 0;
  105. }

  106. static __net_exit void loopback_net_exit(struct net *net)
  107. {

  108.     unregister_netdev(dev);                                                             //注销dev结构
  109. }

  110. /* Registered in net/core/dev.c */
  111. struct pernet_operations __net_initdata loopback_net_ops = {
  112.        .init = loopback_net_init,
  113.        .exit = loopback_net_exit,
  114. };


阅读(643) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~