]> granicus.if.org Git - esp-idf/commitdiff
lwip: Refactor support for L2 pbuf free notification into each driver
authorAngus Gratton <angus@espressif.com>
Fri, 24 Feb 2017 03:45:17 +0000 (14:45 +1100)
committerAngus Gratton <angus@espressif.com>
Fri, 24 Feb 2017 03:45:17 +0000 (14:45 +1100)
Makes it easier to handle different drivers enabled at compile/link time.

components/lwip/core/pbuf.c
components/lwip/include/lwip/lwip/netif.h
components/lwip/include/lwip/lwip/pbuf.h
components/lwip/port/netif/ethernetif.c
components/lwip/port/netif/wlanif.c

index b954817492b1f13db60414e19e9784fb653e452f..84dcc3103657aeefecbddccb8eff29f90ec8d91c 100755 (executable)
 
 #include <string.h>
 
-#if ESP_LWIP
-#include "esp_wifi_internal.h"
-#include "esp_eth.h"
-#endif
-
 #define SIZEOF_STRUCT_PBUF        LWIP_MEM_ALIGN_SIZE(sizeof(struct pbuf))
 /* Since the pool is created in memp, PBUF_POOL_BUFSIZE will be automatically
    aligned there. Therefore, PBUF_POOL_BUFSIZE_ALIGNED can be used here. */
@@ -350,10 +345,10 @@ pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
   p->ref = 1;
   /* set flags */
   p->flags = 0;
-  
+
 #if ESP_LWIP
-  p->user_buf = NULL;
-  p->user_flag = PBUF_USER_FLAG_OWNER_NULL; 
+  p->l2_owner = NULL;
+  p->l2_buf = NULL;
 #endif
 
   LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc(length=%"U16_F") == %p\n", length, (void *)p));
@@ -720,14 +715,13 @@ pbuf_free(struct pbuf *p)
           memp_free(MEMP_PBUF_POOL, p);
         /* is this a ROM or RAM referencing pbuf? */
         } else if (type == PBUF_ROM || type == PBUF_REF) {
-        
+
 #if ESP_LWIP
-          if (type == PBUF_REF && p->user_flag == PBUF_USER_FLAG_OWNER_WIFI ) {
-               esp_wifi_internal_free_rx_buffer(p->user_buf);
-          }
-          if (type == PBUF_REF && p->user_flag == PBUF_USER_FLAG_OWNER_ETH ) {  
-               esp_eth_free_rx_buf(p->user_buf);
-          }
+            if (p->l2_owner != NULL
+                && p->l2_buf != NULL
+                && p->l2_owner->l2_buffer_free_notify != NULL) {
+                p->l2_owner->l2_buffer_free_notify(p->l2_buf);
+            }
 #endif
             memp_free(MEMP_PBUF, p);
             /* type == PBUF_RAM */
index 34e6d4489784c347652149ea170d51b6aac0da5f..13cbb798e06b6c9d468d4c9382290ff73de1f3ae 100755 (executable)
@@ -330,6 +330,10 @@ struct netif {
   u16_t loop_cnt_current;
 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
 #endif /* ENABLE_LOOPBACK */
+
+#if ESP_LWIP
+  void (*l2_buffer_free_notify)(void *user_buf); /* Allows LWIP to notify driver when a L2-supplied pbuf can be freed */
+#endif
 };
 
 #if LWIP_CHECKSUM_CTRL_PER_NETIF
index d84aabaf81430cf27f88576ff522ef93c6db100f..42146f6fc46e130117a49a576b1c337fc5d8dabc 100755 (executable)
@@ -105,12 +105,6 @@ typedef enum {
 /** indicates this pbuf includes a TCP FIN flag */
 #define PBUF_FLAG_TCP_FIN   0x20U
 
-#if ESP_LWIP
-#define PBUF_USER_FLAG_OWNER_NULL   0
-#define PBUF_USER_FLAG_OWNER_WIFI   1
-#define PBUF_USER_FLAG_OWNER_ETH    2
-#endif
-
 struct pbuf {
   /** next pbuf in singly linked pbuf chain */
   struct pbuf *next;
@@ -142,10 +136,10 @@ struct pbuf {
    * the stack itself, or pbuf->next pointers from a chain.
    */
   u16_t ref;
-  
+
 #if ESP_LWIP
-  void  *user_buf;
-  u8_t user_flag;
+  struct netif *l2_owner;
+  void *l2_buf;
 #endif
 };
 
index 90a5b241b03ad803481fbbe36b75d2d010aced7d..1930c5e4b27a3215dbd179da2f8ac25a17a86f12 100644 (file)
@@ -82,7 +82,10 @@ ethernet_low_level_init(struct netif *netif)
   netif->flags |= NETIF_FLAG_IGMP;
 #endif
 #endif
-  /* Do whatever else is needed to initialize interface. */
+
+#ifndef CONFIG_EMAC_L2_TO_L3_RX_BUF_MODE
+  netif->l2_buffer_free_notify = esp_eth_free_rx_buf;
+#endif
 }
 
 /**
@@ -152,11 +155,12 @@ ethernetif_input(struct netif *netif, void *buffer, uint16_t len)
 
   if(buffer== NULL || netif == NULL)
     goto _exit;
-#if CONFIG_EMAC_L2_TO_L3_RX_BUF_MODE
+#ifdef CONFIG_EMAC_L2_TO_L3_RX_BUF_MODE
   p = pbuf_alloc(PBUF_RAW, len, PBUF_RAM);
   if (p == NULL) {
     return;
   }
+  p->l2_owner = NULL;
   memcpy(p->payload, buffer, len);
 
 /* full packet send to tcpip_thread to process */
@@ -171,13 +175,13 @@ if (netif->input(p, netif) != ERR_OK) {
     return;
   }
   p->payload = buffer;
-  p->user_flag = PBUF_USER_FLAG_OWNER_ETH;
-  p->user_buf = buffer;
+  p->l2_owner = netif;
+  p->l2_buf = buffer;
 
   /* full packet send to tcpip_thread to process */
 if (netif->input(p, netif) != ERR_OK) {
   LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
-  p->user_flag = PBUF_USER_FLAG_OWNER_NULL;
+  p->l2_owner = NULL;
   pbuf_free(p);
 }
 #endif
index f9def49218235d293d79f49df7c4a00d2bbc6ccf..9f454230ab2049736c23625e74f8ef78e1c3f86e 100644 (file)
@@ -84,7 +84,9 @@ low_level_init(struct netif *netif)
 #endif
 #endif
 
-  /* Do whatever else is needed to initialize interface. */
+#if !ESP_L2_TO_L3_COPY
+  netif->l2_buffer_free_notify = esp_wifi_internal_free_rx_buffer;
+#endif
 }
 
 /**
@@ -119,6 +121,7 @@ low_level_output(struct netif *netif, struct pbuf *p)
     LWIP_DEBUGF(PBUF_DEBUG, ("low_level_output: pbuf is a list, application may has bug"));
     q = pbuf_alloc(PBUF_RAW_TX, p->tot_len, PBUF_RAM);
     if (q != NULL) {
+      q->l2_owner = NULL;
       pbuf_copy(q, p);
     } else {
       return ERR_MEM;
@@ -154,6 +157,7 @@ wlanif_input(struct netif *netif, void *buffer, u16_t len, void* eb)
     esp_wifi_internal_free_rx_buffer(eb);
     return;
   }
+  p->l2_owner = NULL;
   memcpy(p->payload, buffer, len);
   esp_wifi_internal_free_rx_buffer(eb);
 #else
@@ -163,8 +167,8 @@ wlanif_input(struct netif *netif, void *buffer, u16_t len, void* eb)
     return;
   }
   p->payload = buffer;
-  p->user_buf = eb;
-  p->user_flag = PBUF_USER_FLAG_OWNER_WIFI;
+  p->l2_owner = netif;
+  p->l2_buf = eb;
 #endif
 
   /* full packet send to tcpip_thread to process */