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

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