Chinaunix首页 | 论坛 | 博客
  • 博客访问: 121624
  • 博文数量: 31
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 296
  • 用 户 组: 普通用户
  • 注册时间: 2015-01-10 21:57
文章分类

全部博文(31)

文章存档

2016年(4)

2015年(27)

我的朋友

分类: LINUX

2016-06-30 22:17:21

loopback.c

点击(此处)折叠或打开

  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. static int loopback_xmit(struct sk_buff *skb, struct net_device *dev)
  62. {
  63.     skb_orphan(skb);
  64.     skb->protocol = eth_type_trans(skb,dev);

  65.     bytes += skb->len;
  66.     packets++;

  67.     netif_rx(skb);

  68.     return 0;
  69. }

  70. static struct net_device_stats *loopback_get_stats(struct net_device *dev)
  71. {
  72.     struct net_device_stats *stats = &dev->stats;
  73.     
  74.     stats->rx_packets = packets;
  75.     stats->tx_packets = packets;
  76.     stats->rx_bytes = bytes;
  77.     stats->tx_bytes = bytes;
  78.     return stats;
  79. }

  80. static const struct net_device_ops loopback_ops = {
  81.     .ndo_start_xmit= loopback_xmit,
  82.     .ndo_get_stats = loopback_get_stats,
  83. };

  84. /*
  85.  * The loopback device is special. There is only one instance
  86.  * per network namespace.
  87.  */
  88. static void loopback_setup(struct net_device *dev)
  89. {

  90.     
  91.     dev->mtu        = (16 * 1024) + 20 + 20 + 12;//
  92.     dev->flags        = IFF_LOOPBACK;//
  93.     dev->priv_flags     &= ~IFF_XMIT_DST_RELEASE;
  94.     dev->header_ops        = &eth_header_ops;//
  95.     dev->netdev_ops        = &loopback_ops;//
  96. }

  97. /* Setup and register the loopback device. */
  98. static __net_init int loopback_net_init(struct net *net)
  99. {
  100.     struct net_device *dev;
  101.     int err;
  102.     err = -ENOMEM;
  103.     dev = alloc_netdev(0, "lo", loopback_setup);
  104.     if (!dev)
  105.         goto out;

  106.     err = register_netdev(dev);
  107.     if (err)
  108.         goto out_free_netdev;

  109.     net->loopback_dev = dev;
  110.     return 0;


  111. out_free_netdev:
  112.     free_netdev(dev);
  113. out:
  114.     if (net == &init_net)
  115.         panic("loopback: Failed to register netdevice: %d\n", err);
  116.     return err;
  117. }

  118. static __net_exit void loopback_net_exit(struct net *net)
  119. {
  120.     struct net_device *dev = net->loopback_dev;

  121.     unregister_netdev(dev);
  122. }

  123. /* Registered in net/core/dev.c */
  124. struct pernet_operations __net_initdata loopback_net_ops = {
  125.        .init = loopback_net_init,
  126.        .exit = loopback_net_exit,
  127. };

编译内核代码,并在开发板上运行,可通过ifconfig命令查看回环网卡信息,也可通过ping命令ping通,下图是不加那一行
ping不通的图,
阅读(2826) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~