]> www.infradead.org Git - users/dwmw2/openconnect.git/commitdiff
test: reuse packets instead of free/malloc
authorDavid Woodhouse <dwmw2@infradead.org>
Tue, 9 Apr 2019 15:44:22 +0000 (18:44 +0300)
committerDavid Woodhouse <dwmw2@infradead.org>
Mon, 15 Apr 2019 17:12:24 +0000 (18:12 +0100)
Some MTU-related issues to work out here... this gives me about 1%
additional performance.

dtls.c
esp.c
mainloop.c
openconnect-internal.h

diff --git a/dtls.c b/dtls.c
index 2f262934f0a3abaf16510d9cf1972c4d8e180052..3148f502b44ab90d8c6047f17cce92a7d167428e 100644 (file)
--- a/dtls.c
+++ b/dtls.c
@@ -276,7 +276,7 @@ int dtls_mainloop(struct openconnect_info *vpninfo, int *timeout, int readable)
                unsigned char *buf;
 
                if (!vpninfo->dtls_pkt) {
-                       vpninfo->dtls_pkt = malloc(sizeof(struct pkt) + len);
+                       vpninfo->dtls_pkt = pkt_alloc(vpninfo, sizeof(struct pkt) + len);
                        if (!vpninfo->dtls_pkt) {
                                vpn_progress(vpninfo, PRG_ERR, _("Allocation failed\n"));
                                break;
@@ -502,7 +502,7 @@ int dtls_mainloop(struct openconnect_info *vpninfo, int *timeout, int readable)
                vpn_progress(vpninfo, PRG_TRACE,
                             _("Sent DTLS packet of %d bytes; DTLS send returned %d\n"),
                             this->len, ret);
-               free(this);
+               pkt_free(vpninfo, this);
        }
 
        return work_done;
diff --git a/esp.c b/esp.c
index f77bcc97e5fc3aa43165aaf5171bfac5c16f25f1..14f34a77e2adff2c81a58723abeef897f65c73df 100644 (file)
--- a/esp.c
+++ b/esp.c
@@ -124,7 +124,7 @@ int esp_mainloop(struct openconnect_info *vpninfo, int *timeout, int readable)
                struct pkt *pkt;
 
                if (!vpninfo->dtls_pkt) {
-                       vpninfo->dtls_pkt = malloc(sizeof(struct pkt) + len);
+                       vpninfo->dtls_pkt = pkt_alloc(vpninfo, sizeof(struct pkt) + len);
                        if (!vpninfo->dtls_pkt) {
                                vpn_progress(vpninfo, PRG_ERR, _("Allocation failed\n"));
                                break;
@@ -277,7 +277,7 @@ int esp_mainloop(struct openconnect_info *vpninfo, int *timeout, int readable)
                                vpn_progress(vpninfo, PRG_ERR,
                                             _("Failed to encrypt ESP packet: %d\n"),
                                             len);
-                               free(this);
+                               pkt_free(vpninfo, this);
                                work_done = 1;
                                continue;
                        }
@@ -311,7 +311,7 @@ int esp_mainloop(struct openconnect_info *vpninfo, int *timeout, int readable)
                        unmonitor_write_fd(vpninfo, dtls);
                        vpninfo->deflate_pkt = NULL;
                }
-               free(this);
+               pkt_free(vpninfo, this);
                work_done = 1;
        }
 
index b21ff70c779e6d3cfbd2535b2a6d295c2470ab2c..c44bed8dfd2eabf251498b3b899bf4e7cc20fe9f 100644 (file)
@@ -64,7 +64,7 @@ int tun_mainloop(struct openconnect_info *vpninfo, int *timeout, int readable)
                        int len = vpninfo->ip_info.mtu;
 
                        if (!out_pkt) {
-                               out_pkt = malloc(sizeof(struct pkt) + len + vpninfo->pkt_trailer);
+                               out_pkt = pkt_alloc(vpninfo, sizeof(struct pkt) + len + vpninfo->pkt_trailer);
                                if (!out_pkt) {
                                        vpn_progress(vpninfo, PRG_ERR, _("Allocation failed\n"));
                                        break;
@@ -104,7 +104,7 @@ int tun_mainloop(struct openconnect_info *vpninfo, int *timeout, int readable)
                vpninfo->stats.rx_pkts++;
                vpninfo->stats.rx_bytes += this->len;
 
-               free(this);
+               pkt_free(vpninfo, this);
        }
        /* Work is not done if we just got rid of packets off the queue */
        return work_done;
@@ -196,7 +196,6 @@ int openconnect_mainloop(struct openconnect_info *vpninfo,
                struct timeval tv;
                fd_set rfds, wfds, efds;
 #endif
-
                /* If tun is not up, loop more often to detect
                 * a DTLS timeout (due to a firewall block) as soon. */
                if (tun_is_up(vpninfo))
index cbf0de083905cb8bcd20700dae43872edd01f902..d998f5d834b283cab29dbc5e54840c6e8b6de11a 100644 (file)
@@ -536,6 +536,7 @@ struct openconnect_info {
        struct pkt *cstp_pkt;
        struct pkt *dtls_pkt;
        struct pkt *tun_pkt;
+       struct pkt *pkt_pool;
        int pkt_trailer; /* How many bytes after payload for encryption (ESP HMAC) */
 
        z_stream inflate_strm;
@@ -813,6 +814,23 @@ OPENCONNECT_CMD_SOCKET dumb_socketpair(OPENCONNECT_CMD_SOCKET socks[2], int make
 
 /****************************************************************************/
 
+
+
+static inline struct pkt *pkt_alloc(struct openconnect_info *vpninfo, int len)
+{
+       struct pkt *ret = vpninfo->pkt_pool;
+       if (ret)
+               vpninfo->pkt_pool = ret->next;
+       else
+               ret = malloc(len);
+       return ret;
+}
+static inline void pkt_free(struct openconnect_info *vpninfo, struct pkt *pkt)
+{
+       pkt->next = vpninfo->pkt_pool;
+       vpninfo->pkt_pool = pkt;
+}
+
 /* iconv.c */
 #ifdef HAVE_ICONV
 char *openconnect_utf8_to_legacy(struct openconnect_info *vpninfo, const char *utf8);