swrap: Add enviornment variable to specify mtu size
authorAndreas Schneider <asn@samba.org>
Tue, 11 Aug 2015 10:11:16 +0000 (12:11 +0200)
committerAndreas Schneider <asn@samba.org>
Tue, 11 Aug 2015 11:58:45 +0000 (13:58 +0200)
Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
doc/socket_wrapper.1
doc/socket_wrapper.1.txt
src/socket_wrapper.c

index 4e0dd01a02d969e54622caea92ded35764e9e43e..c3cf835e237daba024540aeb4108b84aa77d93ab 100644 (file)
@@ -2,12 +2,12 @@
 .\"     Title: socket_wrapper
 .\"    Author: [FIXME: author] [see http://docbook.sf.net/el/author]
 .\" Generator: DocBook XSL Stylesheets v1.78.1 <http://docbook.sf.net/>
-.\"      Date: 2014-07-09
+.\"      Date: 2015-08-11
 .\"    Manual: \ \&
 .\"    Source: \ \&
 .\"  Language: English
 .\"
-.TH "SOCKET_WRAPPER" "1" "2014\-07\-09" "\ \&" "\ \&"
+.TH "SOCKET_WRAPPER" "1" "2015\-08\-11" "\ \&" "\ \&"
 .\" -----------------------------------------------------------------
 .\" * Define some portability stuff
 .\" -----------------------------------------------------------------
@@ -85,6 +85,13 @@ Additionally, the default interface to be used by an application is defined with
 When debugging, it is often interesting to investigate the network traffic between the client and server within your application\&. If you define SOCKET_WRAPPER_PCAP_FILE=/path/to/file\&.pcap, socket_wrapper will dump all your network traffic to the specified file\&. After the test has been finished you\(cqre able to open the file for example with Wireshark\&.
 .RE
 .PP
+\fBSOCKET_WRAPPER_MTU\fR
+.RS 4
+With this variable you can change the MTU size\&. However we do not recomment to do that as the default size of 1500 byte is best for formatting PCAP files\&.
+.RE
+.sp
+The minimum value you can set is 512 and the maximum 32768\&.
+.PP
 \fBSOCKET_WRAPPER_DEBUGLEVEL\fR
 .RS 4
 If you need to see what is going on in socket_wrapper itself or try to find a bug, you can enable logging support in socket_wrapper if you built it with debug symbols\&.
index 2ab330eb17941fa3c1325d28c2096b0758d7308c..f4e82a8fe9a1d03b29dcaa7bf09f04230e1faa96 100644 (file)
@@ -1,6 +1,6 @@
 socket_wrapper(1)
 =================
-:revdate: 2014-07-09
+:revdate: 2015-08-11
 
 NAME
 ----
@@ -52,6 +52,13 @@ SOCKET_WRAPPER_PCAP_FILE=/path/to/file.pcap, socket_wrapper will dump all your
 network traffic to the specified file. After the test has been finished you're
 able to open the file for example with Wireshark.
 
+*SOCKET_WRAPPER_MTU*::
+
+With this variable you can change the MTU size. However we do not recomment to
+do that as the default size of 1500 byte is best for formatting PCAP files.
+
+The minimum value you can set is 512 and the maximum 32768.
+
 *SOCKET_WRAPPER_DEBUGLEVEL*::
 
 If you need to see what is going on in socket_wrapper itself or try to find a
index 18622af27a0c3cf26dfc471c65531bc6b85dbf9e..01ab8d52d38d23ad703bc634fe531efc34a8f6f7 100644 (file)
@@ -203,11 +203,12 @@ enum swrap_dbglvl_e {
 #define SOCKET_TYPE_CHAR_UDP_V6                'Y'
 
 /*
- * Cut down to 1500 byte packets for stream sockets,
- * which makes it easier to format PCAP capture files
- * (as the caller will simply continue from here)
+ * Set the packet MTU to 1500 bytes for stream sockets to make it it easier to
+ * format PCAP capture files (as the caller will simply continue from here).
  */
-#define SOCKET_MAX_PACKET 1500
+#define SOCKET_WRAPPER_MTU_DEFAULT 1500
+#define SOCKET_WRAPPER_MTU_MIN     512
+#define SOCKET_WRAPPER_MTU_MAX     32768
 
 #define SOCKET_MAX_SOCKETS 1024
 
@@ -912,6 +913,38 @@ static const char *socket_wrapper_dir(void)
        return s;
 }
 
+static unsigned int socket_wrapper_mtu(void)
+{
+       static unsigned int max_mtu = 0;
+       unsigned int tmp;
+       const char *s;
+       char *endp;
+
+       if (max_mtu != 0) {
+               return max_mtu;
+       }
+
+       max_mtu = SOCKET_WRAPPER_MTU_DEFAULT;
+
+       s = getenv("SOCKET_WRAPPER_MTU");
+       if (s == NULL) {
+               goto done;
+       }
+
+       tmp = strtol(s, &endp, 10);
+       if (s == endp) {
+               goto done;
+       }
+
+       if (tmp < SOCKET_WRAPPER_MTU_MIN || tmp > SOCKET_WRAPPER_MTU_MAX) {
+               goto done;
+       }
+       max_mtu = tmp;
+
+done:
+       return max_mtu;
+}
+
 bool socket_wrapper_enabled(void)
 {
        const char *s = socket_wrapper_dir();
@@ -3743,7 +3776,9 @@ static ssize_t swrap_sendmsg_before(int fd,
        }
 
        switch (si->type) {
-       case SOCK_STREAM:
+       case SOCK_STREAM: {
+               unsigned long mtu;
+
                if (!si->connected) {
                        errno = ENOTCONN;
                        return -1;
@@ -3753,22 +3788,23 @@ static ssize_t swrap_sendmsg_before(int fd,
                        break;
                }
 
+               mtu = socket_wrapper_mtu();
                for (i = 0; i < (size_t)msg->msg_iovlen; i++) {
                        size_t nlen;
                        nlen = len + msg->msg_iov[i].iov_len;
-                       if (nlen > SOCKET_MAX_PACKET) {
+                       if (nlen > mtu) {
                                break;
                        }
                }
                msg->msg_iovlen = i;
                if (msg->msg_iovlen == 0) {
                        *tmp_iov = msg->msg_iov[0];
-                       tmp_iov->iov_len = MIN(tmp_iov->iov_len, SOCKET_MAX_PACKET);
+                       tmp_iov->iov_len = MIN(tmp_iov->iov_len, mtu);
                        msg->msg_iov = tmp_iov;
                        msg->msg_iovlen = 1;
                }
                break;
-
+       }
        case SOCK_DGRAM:
                if (si->connected) {
                        if (msg->msg_name) {
@@ -3958,7 +3994,8 @@ static int swrap_recvmsg_before(int fd,
        (void)fd; /* unused */
 
        switch (si->type) {
-       case SOCK_STREAM:
+       case SOCK_STREAM: {
+               unsigned int mtu;
                if (!si->connected) {
                        errno = ENOTCONN;
                        return -1;
@@ -3968,22 +4005,23 @@ static int swrap_recvmsg_before(int fd,
                        break;
                }
 
+               mtu = socket_wrapper_mtu();
                for (i = 0; i < (size_t)msg->msg_iovlen; i++) {
                        size_t nlen;
                        nlen = len + msg->msg_iov[i].iov_len;
-                       if (nlen > SOCKET_MAX_PACKET) {
+                       if (nlen > mtu) {
                                break;
                        }
                }
                msg->msg_iovlen = i;
                if (msg->msg_iovlen == 0) {
                        *tmp_iov = msg->msg_iov[0];
-                       tmp_iov->iov_len = MIN(tmp_iov->iov_len, SOCKET_MAX_PACKET);
+                       tmp_iov->iov_len = MIN(tmp_iov->iov_len, mtu);
                        msg->msg_iov = tmp_iov;
                        msg->msg_iovlen = 1;
                }
                break;
-
+       }
        case SOCK_DGRAM:
                if (msg->msg_name == NULL) {
                        errno = EINVAL;