tun-device: Read from tun to buffer on stack to avoid over-allocation of packets
authorMartin Willi <martin@revosec.ch>
Fri, 21 Nov 2014 10:23:08 +0000 (11:23 +0100)
committerMartin Willi <martin@revosec.ch>
Fri, 21 Nov 2014 11:02:07 +0000 (12:02 +0100)
Instead of allocating MTU-sized buffers for each packet, read to a stack buffer
and copy to an allocation of the actual packet size. While it requires an
additional copy on non-Apple platforms, this should make allocation more
efficient for small packets.

src/libstrongswan/networking/tun_device.c

index 035b846..81d2156 100644 (file)
@@ -346,29 +346,27 @@ METHOD(tun_device_t, write_packet, bool,
 METHOD(tun_device_t, read_packet, bool,
        private_tun_device_t *this, chunk_t *packet)
 {
+       chunk_t data;
        ssize_t len;
        bool old;
 
-       *packet = chunk_alloc(get_mtu(this));
+       data = chunk_alloca(get_mtu(this));
 
-       thread_cleanup_push(free, packet->ptr);
        old = thread_cancelability(TRUE);
-       len = read(this->tunfd, packet->ptr, packet->len);
+       len = read(this->tunfd, data.ptr, data.len);
        thread_cancelability(old);
-       thread_cleanup_pop(FALSE);
        if (len < 0)
        {
                DBG1(DBG_LIB, "reading from TUN device %s failed: %s", this->if_name,
                         strerror(errno));
-               free(packet->ptr);
                return FALSE;
        }
-       packet->len = len;
+       data.len = len;
 #ifdef __APPLE__
        /* UTUN's prepend packets with a 32-bit protocol number */
-       packet->len -= sizeof(u_int32_t);
-       memmove(packet->ptr, packet->ptr + sizeof(u_int32_t), packet->len);
+       data = chunk_skip(data, sizeof(u_int32_t));
 #endif
+       *packet = chunk_clone(data);
        return TRUE;
 }