Modules/IPStack - Cleanup, remove now extra calls to DestroyBuffer
authorJohn Hodge <[email protected]>
Wed, 25 Dec 2013 00:41:00 +0000 (08:41 +0800)
committerJohn Hodge <[email protected]>
Wed, 25 Dec 2013 00:41:00 +0000 (08:41 +0800)
- _DestroyBuffer is now called by low-level SendPacket after dispatch

KernelLand/Modules/IPStack/arp.c
KernelLand/Modules/IPStack/buffer.c
KernelLand/Modules/IPStack/icmp.c
KernelLand/Modules/IPStack/link.c
KernelLand/Modules/IPStack/udp.c

index d1d50cd..c81f4c2 100644 (file)
@@ -206,9 +206,6 @@ tMacAddr ARP_Resolve4(tInterface *Interface, tIPv4 Address)
 
        // Send Request
        Link_SendPacket(Interface->Adapter, 0x0806, req.DestMac, buffer);
-
-       // Clean up
-       IPStack_Buffer_DestroyBuffer(buffer);
        
        // Wait for a reply
        Time_ScheduleTimer(NULL, Interface->TimeoutDelay);
@@ -367,12 +364,12 @@ void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffe
                switch( req4->SWSize )
                {
                case 4:
-                       Log_Debug("ARP", "ARP Request IPv4 Address %i.%i.%i.%i from %i.%i.%i.%i",
+                       Log_Debug("ARP", "ARP Request IPv4 Address %i.%i.%i.%i from %i.%i.%i.%i"
+                               " (%02x:%02x:%02x:%02x:%02x:%02x)",
                                req4->DestIP.B[0], req4->DestIP.B[1], req4->DestIP.B[2],
                                req4->DestIP.B[3],
                                req4->SourceIP.B[0], req4->SourceIP.B[1],
-                               req4->SourceIP.B[2], req4->SourceIP.B[3]);
-                       Log_Debug("ARP", " from MAC %02x:%02x:%02x:%02x:%02x:%02x",
+                               req4->SourceIP.B[2], req4->SourceIP.B[3],
                                req4->SourceMac.B[0], req4->SourceMac.B[1],
                                req4->SourceMac.B[2], req4->SourceMac.B[3],
                                req4->SourceMac.B[4], req4->SourceMac.B[5]);
@@ -393,9 +390,10 @@ void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffe
                                
                                // Assumes only a header and footer at link layer
                                tIPStackBuffer  *buffer = IPStack_Buffer_CreateBuffer(3);
-                               IPStack_Buffer_AppendSubBuffer(buffer, sizeof(struct sArpRequest4), 0, req4, NULL, NULL);
+                               IPStack_Buffer_AppendSubBuffer(buffer,
+                                       sizeof(struct sArpRequest4), 0, req4,
+                                       NULL, NULL);
                                Link_SendPacket(Adapter, 0x0806, req4->DestMac, buffer);
-                               IPStack_Buffer_DestroyBuffer(buffer);
                        }
                        break;
                #if ARPv6
index ac27976..3fe8b89 100644 (file)
@@ -5,6 +5,7 @@
  * buffer.c
  * - Scatter-gather handling
  */
+#define DEBUG  0
 #include "ipstack.h"
 #include "include/buffer.h"
 
@@ -23,7 +24,6 @@ struct sIPStackBuffer
                size_t  PostLength;
                tIPStackBufferCb        Cb;
                void    *CbArg;
-               // TODO: Callbacks?
        } SubBuffers[];
 };
 
@@ -63,6 +63,7 @@ void IPStack_Buffer_ClearBuffer(tIPStackBuffer *Buffer)
 
 void IPStack_Buffer_DestroyBuffer(tIPStackBuffer *Buffer)
 {
+       LOG("Called with %p by %p", Buffer, __builtin_return_address(0));
        ASSERT(Buffer);
        IPStack_Buffer_ClearBuffer(Buffer);
        Buffer->MaxSubBufffers = 0;
index 66f0f96..fa9927e 100644 (file)
@@ -87,7 +87,6 @@ void ICMP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buff
                tIPStackBuffer  *buffer = IPStack_Buffer_CreateBuffer(1 + IPV4_BUFFERS);
                IPStack_Buffer_AppendSubBuffer(buffer, Length, 0, hdr, NULL, NULL);
                IPv4_SendPacket(Interface, *(tIPv4*)Address, 1, ntohs(hdr->Sequence), buffer);
-               IPStack_Buffer_DestroyBuffer(buffer);
                break;
        default:
                break;
@@ -128,7 +127,6 @@ int ICMP_Ping(tInterface *Interface, tIPv4 Addr)
        tIPStackBuffer  *buffer = IPStack_Buffer_CreateBuffer(1 + IPV4_BUFFERS);
        IPStack_Buffer_AppendSubBuffer(buffer, sizeof(buf), 0, buf, NULL, NULL);
        IPv4_SendPacket(Interface, Addr, 1, i, buffer);
-       IPStack_Buffer_DestroyBuffer(buffer);
        
        end = ts + Interface->TimeoutDelay;
        while( !gICMP_PingSlots[i].bArrived && now() < end)     Threads_Yield();
index f0c6367..06f774a 100644 (file)
 #define VALIDATE_CHECKSUM      0
 #define        MAX_PACKET_SIZE 2048
 
+// === TYPES ===
+typedef struct {
+       Uint16  Type;
+       tPacketCallback Callback;
+} tLink_PktType;
+
+
 // === PROTOTYPES ===
 void   Link_RegisterType(Uint16 Type, tPacketCallback Callback);
 void   Link_SendPacket(tAdapter *Adapter, Uint16 Type, tMacAddr To, tIPStackBuffer *Buffer);
@@ -28,10 +35,7 @@ Uint32       Link_CalculatePartialCRC(Uint32 CRC, const void *Data, int Length);
 // === GLOBALS ===
  int   giRegisteredTypes = 0;
  int   giRegisteredTypeSpace = 0;
-struct {
-       Uint16  Type;
-       tPacketCallback Callback;
-}      *gaRegisteredTypes;
+tLink_PktType  *gaRegisteredTypes;
  int   gbLink_CRCTableGenerated = 0;
 Uint32 gaiLink_CRCTable[256];
 
@@ -44,41 +48,45 @@ Uint32      gaiLink_CRCTable[256];
  */
 void Link_RegisterType(Uint16 Type, tPacketCallback Callback)
 {
-        int    i;
-       void    *tmp;
+       tLink_PktType *typeslot = NULL;
        
-       for( i = giRegisteredTypes; i -- ; )
+       for( int i = 0; i < giRegisteredTypes; i ++)
        {
                if(gaRegisteredTypes[i].Type == Type) {
                        Log_Warning("Net Link", "Attempt to register 0x%x twice", Type);
                        return ;
                }
                // Ooh! Free slot!
-               if(gaRegisteredTypes[i].Callback == NULL)       break;
+               if(gaRegisteredTypes[i].Callback == NULL)
+               {
+                       typeslot = &gaRegisteredTypes[i];
+                       break;
+               }
        }
        
-       if(i == -1)
+       if(typeslot == NULL)
        {
-               giRegisteredTypeSpace += 5;
-               tmp = realloc(gaRegisteredTypes, giRegisteredTypeSpace*sizeof(*gaRegisteredTypes));
-               if(!tmp) {
-                       Log_Warning("Net Link",
-                               "Out of heap space! (Attempted to allocate %i)",
-                               giRegisteredTypeSpace*sizeof(*gaRegisteredTypes)
-                               );
-                       return ;
+               if( giRegisteredTypes == giRegisteredTypeSpace )
+               {
+                       giRegisteredTypeSpace += 5;
+                       void *tmp = realloc(gaRegisteredTypes, giRegisteredTypeSpace*sizeof(tLink_PktType));
+                       if(!tmp) {
+                               Log_Warning("Net Link",
+                                       "Out of heap space! (Attempted to allocate %i)",
+                                       giRegisteredTypeSpace*sizeof(tLink_PktType)
+                                       );
+                               return ;
+                       }
+                       gaRegisteredTypes = tmp;
                }
-               gaRegisteredTypes = tmp;
-               i = giRegisteredTypes;
-               giRegisteredTypes ++;
+               typeslot = &gaRegisteredTypes[giRegisteredTypes++];
        }
        
-       gaRegisteredTypes[i].Callback = Callback;
-       gaRegisteredTypes[i].Type = Type;
+       typeslot->Callback = Callback;
+       typeslot->Type = Type;
 }
 
 /**
- * \fn void Link_SendPacket(tAdapter *Adapter, Uint16 Type, tMacAddr To, int Length, void *Buffer)
  * \brief Formats and sends a packet on the specified interface
  */
 void Link_SendPacket(tAdapter *Adapter, Uint16 Type, tMacAddr To, tIPStackBuffer *Buffer)
@@ -152,31 +160,28 @@ int Link_HandlePacket(tAdapter *Adapter, tIPStackBuffer *Buffer)
        // TODO: Check checksum
        #endif
        
+       Uint16  type = ntohs(hdr->Type);
        // Check if there is a registered callback for this packet type
-        int    i;
-       for( i = giRegisteredTypes; i--; )
+       for( int i = giRegisteredTypes; i--; )
        {
-               if(gaRegisteredTypes[i].Type == ntohs(hdr->Type))       break;
+               if(gaRegisteredTypes[i].Type == type)
+               {
+                       // Call the callback
+                       gaRegisteredTypes[i].Callback(
+                               Adapter,
+                               hdr->Src,
+                               len - sizeof(tEthernetHeader),
+                               hdr->Data
+                               );
+                       free(data);
+                       return 0;
+               }
        }
        // No? Ignore it
-       if( i == -1 ) {
-               Log_Log("Net Link", "Unregistered type 0x%x", ntohs(hdr->Type));
-               
-               free(data);     
-               return 1;
-       }
-       
-       // Call the callback
-       gaRegisteredTypes[i].Callback(
-               Adapter,
-               hdr->Src,
-               len - sizeof(tEthernetHeader),
-               hdr->Data
-               );
-       
-       free(data);
+       Log_Log("Net Link", "Unregistered type 0x%04x", type);
        
-       return 0;
+       free(data);     
+       return 1;
 }
 
 // From http://www.cl.cam.ac.uk/research/srg/bluebook/21/crc/node6.html
index 6aba50e..ddc79dd 100644 (file)
@@ -162,7 +162,6 @@ void UDP_SendPacketTo(tUDPChannel *Channel, int AddrType, const void *Address, U
                IPStack_Buffer_AppendSubBuffer(buffer, sizeof(hdr), 0, &hdr, NULL, NULL);
                // TODO: What if Channel->Interface is NULL here?
                IPv4_SendPacket(Channel->Interface, *(tIPv4*)Address, IP4PROT_UDP, 0, buffer);
-               IPStack_Buffer_DestroyBuffer(buffer);
                break;
        }
 }

UCC git Repository :: git.ucc.asn.au