root/drivers/net/loopback.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. loopback_xmit
  2. get_stats
  3. loopback_open
  4. loopback_init

   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, <bir7@leland.Stanford.Edu>
  11  *              Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12  *              Donald Becker, <becker@cesdis.gsfc.nasa.gov>
  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  *
  18  *              This program is free software; you can redistribute it and/or
  19  *              modify it under the terms of the GNU General Public License
  20  *              as published by the Free Software Foundation; either version
  21  *              2 of the License, or (at your option) any later version.
  22  */
  23 #include <linux/config.h>
  24 #include <linux/kernel.h>
  25 #include <linux/sched.h>
  26 #include <linux/interrupt.h>
  27 #include <linux/fs.h>
  28 #include <linux/types.h>
  29 #include <linux/string.h>
  30 #include <linux/socket.h>
  31 #include <linux/errno.h>
  32 #include <linux/fcntl.h>
  33 #include <linux/in.h>
  34 #include <linux/if_ether.h>     /* For the statistics structure. */
  35 #include <linux/if_arp.h>       /* For ARPHRD_ETHER */
  36 
  37 #include <asm/system.h>
  38 #include <asm/segment.h>
  39 #include <asm/io.h>
  40 
  41 #include <linux/inet.h>
  42 #include <linux/netdevice.h>
  43 #include <linux/etherdevice.h>
  44 #include <linux/skbuff.h>
  45 #include <net/sock.h>
  46 
  47 
  48 static int loopback_xmit(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
  49 {
  50         struct enet_statistics *stats = (struct enet_statistics *)dev->priv;
  51         unsigned long flags;
  52         int unlock=1;
  53   
  54         if (skb == NULL || dev == NULL) 
  55                 return(0);
  56 
  57         save_flags(flags);
  58         cli();
  59         if (dev->tbusy != 0) 
  60         {
  61                 restore_flags(flags);
  62                 stats->tx_errors++;
  63                 return(1);
  64         }
  65         dev->tbusy = 1;
  66         restore_flags(flags);
  67   
  68         /*
  69          *      Optimise so buffers with skb->free=1 are not copied but
  70          *      instead are lobbed from tx queue to rx queue 
  71          */
  72 
  73         if(skb->free==0)
  74         {
  75                 struct sk_buff *skb2=skb;
  76                 skb=skb_clone(skb, GFP_ATOMIC);         /* Clone the buffer */
  77                 if(skb==NULL)
  78                         return 1;
  79                 dev_kfree_skb(skb2, FREE_READ);
  80                 unlock=0;
  81         }
  82         else if(skb->sk)
  83         {
  84                 /*
  85                  *      Packet sent but looped back around. Cease to charge
  86                  *      the socket for the frame.
  87                  */
  88                 save_flags(flags);
  89                 cli();
  90                 skb->sk->wmem_alloc-=skb->truesize;
  91                 skb->sk->write_space(skb->sk);
  92                 restore_flags(flags);
  93         }
  94 
  95         skb->protocol=eth_type_trans(skb,dev);
  96         skb->dev=dev;
  97         save_flags(flags);
  98         cli();
  99         netif_rx(skb);
 100         if(unlock)
 101                 skb_device_unlock(skb);
 102         restore_flags(flags);
 103   
 104         stats->tx_packets++;
 105         stats->rx_packets++;
 106 
 107         dev->tbusy = 0;
 108 
 109         return(0);
 110 }
 111 
 112 static struct enet_statistics *get_stats(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 113 {
 114         return (struct enet_statistics *)dev->priv;
 115 }
 116 
 117 static int loopback_open(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 118 {
 119         dev->flags|=IFF_LOOPBACK;
 120         return 0;
 121 }
 122 
 123 /* Initialize the rest of the LOOPBACK device. */
 124 int loopback_init(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 125 {
 126         int i;
 127 
 128         dev->mtu                = 2000;                 /* MTU                  */
 129         dev->tbusy              = 0;
 130         dev->hard_start_xmit    = loopback_xmit;
 131         dev->open               = NULL;
 132         dev->hard_header        = eth_header;
 133         dev->hard_header_len    = ETH_HLEN;             /* 14                   */
 134         dev->addr_len           = ETH_ALEN;             /* 6                    */
 135         dev->type               = ARPHRD_ETHER;         /* 0x0001               */
 136         dev->rebuild_header     = eth_rebuild_header;
 137         dev->open               = loopback_open;
 138         dev->flags              = IFF_LOOPBACK|IFF_BROADCAST;
 139         dev->family             = AF_INET;
 140 #ifdef CONFIG_INET    
 141         dev->pa_addr            = in_aton("127.0.0.1");
 142         dev->pa_brdaddr = in_aton("127.255.255.255");
 143         dev->pa_mask            = in_aton("255.0.0.0");
 144         dev->pa_alen            = 4;
 145 #endif  
 146         dev->priv = kmalloc(sizeof(struct enet_statistics), GFP_KERNEL);
 147         if (dev->priv == NULL)
 148                         return -ENOMEM;
 149         memset(dev->priv, 0, sizeof(struct enet_statistics));
 150         dev->get_stats = get_stats;
 151 
 152         /*
 153          *      Fill in the generic fields of the device structure. 
 154          */
 155    
 156         for (i = 0; i < DEV_NUMBUFFS; i++)
 157                 skb_queue_head_init(&dev->buffs[i]);
 158   
 159         return(0);
 160 };

/* [previous][next][first][last][top][bottom][index][help] */