9 /* #include <vstream.h>
11 /* VSTREAM *vstream_tweak_sock(stream)
14 /* VSTREAM *vstream_tweak_tcp(stream)
17 /* vstream_tweak_sock() does a best effort to boost your
18 /* network performance on the specified generic stream.
20 /* vstream_tweak_tcp() does a best effort to boost your
21 /* Internet performance on the specified TCP stream.
25 /* The stream being boosted.
27 /* Panics: interface violations.
31 /* The Secure Mailer license must be distributed with this software.
34 /* IBM T.J. Watson Research
36 /* Yorktown Heights, NY 10598, USA
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <netinet/tcp.h>
46 /* Utility library. */
51 /* Application-specific. */
54 #define SOCKADDR_STORAGE struct sockaddr_storage
56 #define SOCKADDR_STORAGE struct sockaddr
59 /* vstream_tweak_sock - boost your generic network performance */
61 int vstream_tweak_sock(VSTREAM
*fp
)
64 struct sockaddr
*sa
= (struct sockaddr
*) & ss
;
65 SOCKADDR_SIZE sa_length
= sizeof(ss
);
69 * If the caller doesn't know if this socket is AF_LOCAL, AF_INET, etc.,
70 * figure it out for them.
72 if ((ret
= getsockname(vstream_fileno(fp
), sa
, &sa_length
)) >= 0) {
73 switch (sa
->sa_family
) {
78 ret
= vstream_tweak_tcp(fp
);
85 /* vstream_tweak_tcp - boost your TCP performance */
87 int vstream_tweak_tcp(VSTREAM
*fp
)
89 const char *myname
= "vstream_tweak_tcp";
91 SOCKOPT_SIZE mss_len
= sizeof(mss
);
95 * Avoid Nagle delays when VSTREAM buffers are smaller than the MSS.
97 * Forcing TCP_NODELAY to be "always on" would hurt performance in the
98 * common case where VSTREAM buffers are larger than the MSS.
100 * Instead we ask the kernel what the current MSS is, and take appropriate
101 * action. Linux <= 2.2 getsockopt(TCP_MAXSEG) always returns zero (or
102 * whatever value was stored last with setsockopt()).
104 if ((err
= getsockopt(vstream_fileno(fp
), IPPROTO_TCP
, TCP_MAXSEG
,
105 (char *) &mss
, &mss_len
)) < 0) {
106 msg_warn("%s: getsockopt TCP_MAXSEG: %m", myname
);
110 msg_info("%s: TCP_MAXSEG %d", myname
, mss
);
113 * Fix for recent Postfix versions: increase the VSTREAM buffer size if
114 * the VSTREAM buffer is smaller than the MSS. Note: the MSS may change
115 * when the route changes and IP path MTU discovery is turned on, so we
116 * choose a somewhat larger buffer.
118 #ifdef VSTREAM_CTL_BUFSIZE
120 if (mss
< INT_MAX
/ 2)
123 VSTREAM_CTL_BUFSIZE
, (ssize_t
) mss
,
128 * Workaround for older Postfix versions: turn on TCP_NODELAY if the
129 * VSTREAM buffer size is smaller than the MSS.
132 if (mss
> VSTREAM_BUFSIZE
) {
135 if ((err
= setsockopt(vstream_fileno(fp
), IPPROTO_TCP
, TCP_NODELAY
,
136 (char *) &nodelay
, sizeof(nodelay
))) < 0)
137 msg_warn("%s: setsockopt TCP_NODELAY: %m", myname
);