root/net/tcp/dev.c

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

DEFINITIONS

This source file includes following definitions.
  1. min
  2. dev_add_pack
  3. dev_remove_pack
  4. get_dev
  5. dev_queue_xmit
  6. dev_rint
  7. dev_tint

   1 /* dev.c */
   2 /*
   3     Copyright (C) 1992  Ross Biro
   4 
   5     This program is free software; you can redistribute it and/or modify
   6     it under the terms of the GNU General Public License as published by
   7     the Free Software Foundation; either version 2, or (at your option)
   8     any later version.
   9 
  10     This program is distributed in the hope that it will be useful,
  11     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13     GNU General Public License for more details.
  14 
  15     You should have received a copy of the GNU General Public License
  16     along with this program; if not, write to the Free Software
  17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
  18 
  19     The Author may be reached as bir7@leland.stanford.edu or
  20     C/O Department of Mathematics; Stanford University; Stanford, CA 94305
  21 */
  22 
  23 #include <asm/segment.h>
  24 #include <asm/system.h>
  25 #include <linux/types.h>
  26 #include <linux/kernel.h>
  27 #include <linux/sched.h>
  28 #include <linux/string.h>
  29 #include <linux/mm.h>
  30 #include <linux/socket.h>
  31 #include <netinet/in.h>
  32 #include "dev.h"
  33 #include "eth.h"
  34 #include "timer.h"
  35 #include "ip.h"
  36 #include "tcp.h"
  37 #include "sock.h"
  38 #include <linux/errno.h>
  39 #include "arp.h"
  40 
  41 #undef DEV_DEBUG
  42 #ifdef DEV_DEBUG
  43 #define PRINTK printk
  44 #else
  45 #define PRINTK dummy_routine
  46 #endif
  47 
  48 
  49 static  unsigned long
  50 min(unsigned long a, unsigned long b)
     /* [previous][next][first][last][top][bottom][index][help] */
  51 {
  52    if (a < b) return (a);
  53    return (b);
  54 }
  55 
  56 void
  57 dev_add_pack (struct packet_type *pt)
     /* [previous][next][first][last][top][bottom][index][help] */
  58 {
  59    struct packet_type *p1;
  60    pt->next = ptype_base;
  61 
  62    /* see if we need to copy it. */
  63    for (p1 = ptype_base; p1 != NULL; p1 = p1->next)
  64      {
  65         if (p1->type == pt->type)
  66           {
  67              pt->copy = 1;
  68              break;
  69           }
  70      }
  71 
  72    ptype_base = pt;
  73    
  74 }
  75 
  76 void
  77 dev_remove_pack (struct packet_type *pt)
     /* [previous][next][first][last][top][bottom][index][help] */
  78 {
  79    struct packet_type *lpt, *pt1;
  80    if (pt == ptype_base)
  81      {
  82         ptype_base = pt->next;
  83         return;
  84      }
  85 
  86    lpt = NULL;
  87 
  88    for (pt1 = ptype_base; pt1->next != NULL; pt1=pt1->next)
  89      {
  90         if (pt1->next == pt )
  91           {
  92              cli();
  93              if (!pt->copy && lpt) 
  94                lpt->copy = 0;
  95              pt1->next = pt->next;
  96              sti();
  97              return;
  98           }
  99 
 100         if (pt1->next -> type == pt ->type)
 101           {
 102              lpt = pt1->next;
 103           }
 104      }
 105 }
 106 
 107 struct device *
 108 get_dev (char *name)
     /* [previous][next][first][last][top][bottom][index][help] */
 109 {
 110    struct device *dev;
 111    for (dev = dev_base; dev != NULL; dev=dev->next)
 112      {
 113         if (strcmp (dev->name, name) == 0) return (dev);
 114      }
 115    return (NULL);
 116 }
 117 
 118 void
 119 dev_queue_xmit (struct sk_buff *skb, struct device *dev, int pri)
     /* [previous][next][first][last][top][bottom][index][help] */
 120 {
 121   struct sk_buff *skb2;
 122   PRINTK ("eth_queue_xmit (skb=%X, dev=%X, pri = %d)\n", skb, dev, pri);
 123   skb->dev = dev;
 124 
 125   if (skb->next != NULL)
 126     {
 127 /*      printk ("retransmitted packet still on queue. \n");*/
 128        return;
 129     }
 130 
 131   if (pri < 0 || pri >= DEV_NUMBUFFS)
 132     {
 133        printk ("bad priority in dev_queue_xmit.\n");
 134        pri = 1;
 135     }
 136 
 137   if (dev->hard_start_xmit(skb, dev) == 0)
 138     {
 139        return;
 140     }
 141 
 142   /* used to say it is not currently on a send list. */
 143   skb->next = NULL;
 144 
 145 
 146   /* put skb into a bidirectional circular linked list. */
 147   PRINTK ("eth_queue dev->buffs[%d]=%X\n",pri, dev->buffs[pri]);
 148   /* interrupts should already be cleared by hard_start_xmit. */
 149   cli();
 150   if (dev->buffs[pri] == NULL)
 151     {
 152       dev->buffs[pri]=skb;
 153       skb->next = skb;
 154       skb->prev = skb;
 155     }
 156   else
 157     {
 158       skb2=dev->buffs[pri];
 159       skb->next = skb2;
 160       skb->prev = skb2->prev;
 161       skb->next->prev = skb;
 162       skb->prev->next = skb;
 163     }
 164   sti();
 165 
 166 }
 167 
 168 
 169 /* this routine now just gets the data out of the card and returns.
 170    it's return values now mean.
 171 
 172    1 <- exit even if you have more packets.
 173    0 <- call me again no matter what.
 174   -1 <- last packet not processed, try again. */
 175 
 176 int
 177 dev_rint(unsigned char *buff, unsigned long len, int flags,
     /* [previous][next][first][last][top][bottom][index][help] */
 178          struct device * dev)
 179 {
 180    struct sk_buff *skb=NULL;
 181    struct packet_type *ptype;
 182    unsigned short type;
 183    unsigned char flag =0;
 184    unsigned char *to;
 185    int amount;
 186 
 187    /* try to grab some memory. */
 188    if (len > 0 && buff != NULL)
 189      {
 190         skb = kmalloc (sizeof (*skb) + len, GFP_ATOMIC);
 191         if (skb != NULL)
 192           {
 193             skb->mem_len = sizeof (*skb) + len;
 194             skb->mem_addr = skb;
 195           }
 196      }
 197 
 198    /* firs we copy the packet into a buffer, and save it for later. */
 199    if (buff != NULL && skb != NULL)
 200      {
 201         if ( !(flags & IN_SKBUFF))
 202           {
 203              to = (unsigned char *)(skb+1);
 204              while (len > 0)
 205                {
 206                   amount = min (len, (unsigned long) dev->rmem_end -
 207                                 (unsigned long) buff);
 208                   memcpy (to, buff, amount);
 209                   len -= amount;
 210                   buff += amount;
 211                   to += amount;
 212                   if ((unsigned long)buff == dev->rmem_end)
 213                     buff = (unsigned char *)dev->rmem_start;
 214                }
 215           }
 216         else
 217           {
 218              kfree_s (skb->mem_addr, skb->mem_len);
 219              skb = (struct sk_buff *)buff;
 220           }
 221 
 222         skb->len = len;
 223         skb->dev = dev;
 224         skb->sk = NULL;
 225 
 226         /* now add it to the dev backlog. */
 227         cli();
 228         if (dev-> backlog == NULL)
 229           {
 230              skb->prev = skb;
 231              skb->next = skb;
 232              dev->backlog = skb;
 233           }
 234         else
 235           {
 236              skb ->prev = dev->backlog->prev;
 237              skb->next = dev->backlog;
 238              skb->next->prev = skb;
 239              skb->prev->next = skb;
 240           }
 241         sti();
 242         return (0);
 243      }
 244 
 245    if (skb != NULL) 
 246      kfree_s (skb->mem_addr, skb->mem_len);
 247 
 248    /* anything left to process? */
 249 
 250    if (dev->backlog == NULL)
 251      {
 252         if (buff == NULL)
 253           {
 254              sti();
 255              return (1);
 256           }
 257 
 258         if (skb != NULL)
 259           {
 260              sti();
 261              return (-1);
 262           }
 263 
 264         sti();
 265         printk ("dev_rint:Dropping packets due to lack of memory\n");
 266         return (1);
 267      }
 268 
 269    skb= dev->backlog;
 270    if (skb->next == skb)
 271      {
 272         dev->backlog = NULL;
 273      }
 274    else
 275      {
 276         dev->backlog = skb->next;
 277         skb->next->prev = skb->prev;
 278         skb->prev->next = skb->next;
 279      }
 280    sti();
 281 
 282    /* bump the pointer to the next structure. */
 283    skb->h.raw = (unsigned char *)(skb+1) + dev->hard_header_len;
 284    skb->len -= dev->hard_header_len;
 285 
 286    /* convert the type to an ethernet type. */
 287    type = dev->type_trans (skb, dev);
 288 
 289    /* if there get to be a lot of types we should changes this to
 290       a bunch of linked lists like we do for ip protocols. */
 291    for (ptype = ptype_base; ptype != NULL; ptype=ptype->next)
 292      {
 293         if (ptype->type == type)
 294           {
 295              struct sk_buff *skb2;
 296              /* copy the packet if we need to. */
 297              if (ptype->copy)
 298                {
 299                   skb2 = kmalloc (skb->mem_len, GFP_ATOMIC);
 300                   if (skb2 == NULL) continue;
 301                   memcpy (skb2, skb, skb->mem_len);
 302                   skb2->mem_addr = skb2;
 303                }
 304              else
 305                {
 306                   skb2 = skb;
 307                   flag = 1;
 308                }
 309                
 310              ptype->func (skb2, dev, ptype);
 311           }
 312      }
 313 
 314    if (!flag)
 315      {
 316         PRINTK ("discarding packet type = %X\n", type);
 317         free_skb (skb, FREE_READ);
 318      }
 319 
 320      if (buff == NULL)
 321        return (0);
 322      else
 323        return (-1);
 324 }
 325 
 326 /* This routine is called when an device interface is ready to
 327    transmit a packet.  Buffer points to where the packet should
 328    be put, and the routine returns the length of the packet.  A
 329    length of zero is interrpreted to mean the transmit buffers
 330    are empty, and the transmitter should be shut down. */
 331 
 332 unsigned long
 333 dev_tint(unsigned char *buff,  struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 334 {
 335   int i;
 336   int tmp;
 337   struct sk_buff *skb;
 338   for (i=0; i < DEV_NUMBUFFS; i++)
 339     {
 340       while (dev->buffs[i]!=NULL)
 341         {
 342           cli();
 343           skb=dev->buffs[i];
 344           if (skb->next == skb)
 345             {
 346               dev->buffs[i] = NULL;
 347             }
 348           else
 349             {
 350               dev->buffs[i]=skb->next;
 351               skb->prev->next = skb->next;
 352               skb->next->prev = skb->prev;
 353             }
 354           skb->next = NULL;
 355           skb->prev = NULL;
 356           sti();
 357           tmp = skb->len;
 358           if (!skb->arp)
 359             {
 360                if (dev->rebuild_header (skb+1, dev))
 361                  {
 362                     skb->dev = dev;
 363                     arp_queue (skb);
 364                     continue;
 365                  }
 366             }
 367              
 368           if (tmp <= dev->mtu)
 369             {
 370                if (dev->send_packet != NULL)
 371                  {
 372                     dev->send_packet(skb, dev);
 373                  }
 374                if (buff != NULL)
 375                  memcpy (buff, skb + 1, tmp);
 376 
 377                PRINTK (">>\n");
 378                print_eth ((struct enet_header *)(skb+1));
 379             }
 380           else
 381             {
 382                printk ("**** bug len bigger than mtu. \n");
 383             }
 384 
 385           if (skb->free)
 386             {
 387                   free_skb(skb, FREE_WRITE);
 388             }
 389 
 390           if (tmp != 0)
 391             return (tmp);
 392         }
 393     }
 394   PRINTK ("dev_tint returning 0 \n");
 395   return (0);
 396 }
 397 

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