a467e087f98da21444a191f60b1e2554b33c8091
[rsync.git/patches.git] / congestion.diff
1 From: Dave Taht <d@taht.net>
2
3 In the bufferbloat age, anything that can make for a kinder, gentler bulk
4 transfer protocol seems desirable.
5
6 This add support for user and server selectable congestion control algorithms.
7
8 For example:
9     --congestion-alg=lp # For the tcp-lp algorithm on the command line
10
11 And diffserv support:
12     --diffserv=8 for setting the CS1 bit
13
14 Also available in rsync daemon modules:
15
16 [mystuff]
17     congestion alg = westwood # for a wireless connection
18     diffserv = 8
19
20 This could be improved by being able to specify a list of congestion algorithms
21 to try, symbolic names for diffserv (CS1), a better name than 'congestion-alg',
22 and some error checking.
23
24 To use this patch, run these commands for a successful build:
25
26     patch -p1 <patches/congestion.diff
27     ./configure                         (optional if already run)
28     make
29
30 based-on: fe2ef556d9ef11e5dd549e19a06a7a924f7ddfa1
31 diff --git a/loadparm.c b/loadparm.c
32 --- a/loadparm.c
33 +++ b/loadparm.c
34 @@ -120,6 +120,7 @@ typedef struct {
35         char *auth_users;
36         char *charset;
37         char *comment;
38 +       char *congestion_alg;
39         char *dont_compress;
40         char *early_exec;
41         char *exclude;
42 @@ -149,6 +150,7 @@ typedef struct {
43         BOOL auth_users_EXP;
44         BOOL charset_EXP;
45         BOOL comment_EXP;
46 +       BOOL congestion_alg_EXP;
47         BOOL dont_compress_EXP;
48         BOOL early_exec_EXP;
49         BOOL exclude_EXP;
50 @@ -174,6 +176,7 @@ typedef struct {
51         BOOL temp_dir_EXP;
52         BOOL uid_EXP;
53  
54 +       int diffserv;
55         int max_connections;
56         int max_verbosity;
57         int syslog_facility;
58 @@ -238,6 +241,7 @@ static const all_vars Defaults = {
59   /* auth_users; */             NULL,
60   /* charset; */                NULL,
61   /* comment; */                NULL,
62 + /* congestion_alg; */                 NULL,
63   /* dont_compress; */          DEFAULT_DONT_COMPRESS,
64   /* early_exec; */             NULL,
65   /* exclude; */                NULL,
66 @@ -266,6 +270,7 @@ static const all_vars Defaults = {
67   /* auth_users_EXP; */         False,
68   /* charset_EXP; */            False,
69   /* comment_EXP; */            False,
70 + /* congestion_alg_EXP; */     False,
71   /* dont_compress_EXP; */      False,
72   /* early_exec_EXP; */         False,
73   /* exclude_EXP; */            False,
74 @@ -291,6 +296,7 @@ static const all_vars Defaults = {
75   /* temp_dir_EXP; */           False,
76   /* uid_EXP; */                        False,
77  
78 + /* diffserv; */               8,
79   /* max_connections; */                0,
80   /* max_verbosity; */          1,
81   /* syslog_facility; */                LOG_DAEMON,
82 @@ -409,6 +415,8 @@ static struct parm_struct parm_table[] =
83   {"auth users",        P_STRING, P_LOCAL, &Vars.l.auth_users,          NULL,0},
84   {"charset",           P_STRING, P_LOCAL, &Vars.l.charset,             NULL,0},
85   {"comment",           P_STRING, P_LOCAL, &Vars.l.comment,             NULL,0},
86 + {"congestion alg",    P_STRING, P_LOCAL, &Vars.l.congestion_alg,      NULL,0},
87 + {"diffserv",          P_INTEGER,P_LOCAL, &Vars.l.diffserv,            NULL,0},
88   {"dont compress",     P_STRING, P_LOCAL, &Vars.l.dont_compress,       NULL,0},
89   {"early exec",        P_STRING, P_LOCAL, &Vars.l.early_exec,          NULL,0},
90   {"exclude from",      P_STRING, P_LOCAL, &Vars.l.exclude_from,        NULL,0},
91 @@ -550,6 +558,7 @@ FN_GLOBAL_BOOL(lp_proxy_protocol, proxy_protocol)
92  FN_LOCAL_STRING(lp_auth_users, auth_users)
93  FN_LOCAL_STRING(lp_charset, charset)
94  FN_LOCAL_STRING(lp_comment, comment)
95 +FN_LOCAL_STRING(lp_congestion_alg, congestion_alg)
96  FN_LOCAL_STRING(lp_dont_compress, dont_compress)
97  FN_LOCAL_STRING(lp_early_exec, early_exec)
98  FN_LOCAL_STRING(lp_exclude, exclude)
99 @@ -575,6 +584,7 @@ FN_LOCAL_STRING(lp_syslog_tag, syslog_tag)
100  FN_LOCAL_STRING(lp_temp_dir, temp_dir)
101  FN_LOCAL_STRING(lp_uid, uid)
102  
103 +FN_LOCAL_INTEGER(lp_diffserv, diffserv)
104  FN_LOCAL_INTEGER(lp_max_connections, max_connections)
105  FN_LOCAL_INTEGER(lp_max_verbosity, max_verbosity)
106  FN_LOCAL_INTEGER(lp_syslog_facility, syslog_facility)
107 diff --git a/options.c b/options.c
108 --- a/options.c
109 +++ b/options.c
110 @@ -75,6 +75,8 @@ int delete_during = 0;
111  int delete_before = 0;
112  int delete_after = 0;
113  int delete_excluded = 0;
114 +int diffserv = 8;
115 +char *congestion_alg = NULL;
116  int remove_source_files = 0;
117  int one_file_system = 0;
118  int protocol_version = PROTOCOL_VERSION;
119 @@ -1016,6 +1018,8 @@ static struct poptOption long_options[] = {
120    {"outbuf",           0,  POPT_ARG_STRING, &outbuf_mode, 0, 0, 0 },
121    {"remote-option",   'M', POPT_ARG_STRING, 0, 'M', 0, 0 },
122    {"protocol",         0,  POPT_ARG_INT,    &protocol_version, 0, 0, 0 },
123 +  {"congestion-alg",   0,  POPT_ARG_STRING, &congestion_alg, 0, 0, 0 },
124 +  {"diffserv",         0,  POPT_ARG_INT,    &diffserv, 0, 0, 0 },
125    {"checksum-seed",    0,  POPT_ARG_INT,    &checksum_seed, 0, 0, 0 },
126    {"server",           0,  POPT_ARG_NONE,   0, OPT_SERVER, 0, 0 },
127    {"sender",           0,  POPT_ARG_NONE,   0, OPT_SENDER, 0, 0 },
128 diff --git a/rsync.1.md b/rsync.1.md
129 --- a/rsync.1.md
130 +++ b/rsync.1.md
131 @@ -441,6 +441,8 @@ detailed description below for a complete description.
132  --address=ADDRESS        bind address for outgoing socket to daemon
133  --port=PORT              specify double-colon alternate port number
134  --sockopts=OPTIONS       specify custom TCP options
135 +--diffserv=[0-63]        specify diffserv setting
136 +--congestion-alg=STRING  choose a congestion algo
137  --blocking-io            use blocking I/O for the remote shell
138  --outbuf=N|L|B           set out buffering to None, Line, or Block
139  --stats                  give some file-transfer stats
140 diff --git a/socket.c b/socket.c
141 --- a/socket.c
142 +++ b/socket.c
143 @@ -40,6 +40,8 @@ extern char *sockopts;
144  extern int default_af_hint;
145  extern int connect_timeout;
146  extern int pid_file_fd;
147 +extern int diffserv;
148 +extern char *congestion_alg;
149  
150  #ifdef HAVE_SIGACTION
151  static struct sigaction sigact;
152 @@ -166,6 +168,37 @@ static void contimeout_handler(UNUSED(int val))
153         connect_timeout = -1;
154  }
155  
156 +/* Set special socket options
157 + *
158 + * Diffserv is a value in the range of 0-63, and needs to be shifted left
159 + *          2 places AND treated differently for ipv4 and ipv6.
160 + * Setting TCP congestion control is rather Linux specific (at the moment)
161 + * and sends a varying length string to setsockopt rather than an integer
162 + * or character.
163 +*/
164 +
165 +void set_special_sockopts(int s)
166 +{
167 +#if defined(TCP_CONGESTION)
168 +       if (congestion_alg) {
169 +               if (setsockopt(s, SOL_TCP, TCP_CONGESTION, congestion_alg, strlen(congestion_alg)) == -1)
170 +                       rprintf(FINFO, "Couldn't set %s congestion algorithm\n", congestion_alg);
171 +       }
172 +#endif
173 +
174 +/* setting the diffserv/tos bits is different on ipv6 and
175 + *  ipv4, so we just hammer down on both and ignore the result
176 + *  And ipv6 demands an int for v. I didn't write the spec.
177 + */
178 +       if (diffserv) {
179 +               int v = (diffserv & 63) <<2;
180 +               setsockopt(s,IPPROTO_IP, IP_TOS, &v, sizeof(v));
181 +#if defined(IPV6_TCLASS)
182 +               setsockopt(s,IPPROTO_IPV6, IPV6_TCLASS, &v, sizeof(v));
183 +#endif
184 +       }
185 +}
186 +
187  /* Open a socket to a tcp remote host with the specified port.
188   *
189   * Based on code from Warren.  Proxy support by Stephen Rothwell.
190 @@ -272,6 +305,7 @@ int open_socket_out(char *host, int port, const char *bind_addr, int af_hint)
191                         alarm(connect_timeout);
192                 }
193  
194 +               set_special_sockopts(s);
195                 set_socket_options(s, sockopts);
196                 while (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
197                         if (connect_timeout < 0)
198 @@ -440,6 +474,7 @@ static int *open_socket_in(int type, int port, const char *bind_addr,
199                         continue;
200                 }
201  
202 +               set_special_sockopts(s);
203                 setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
204                            (char *)&one, sizeof one);
205                 if (sockopts)