Merge branch 't/dccp_fec'
[paraslash.git] / udp_send.c
1 /*
2 * Copyright (C) 2005-2010 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file udp_send.c Para_server's udp sender. */
8
9
10 #include <regex.h>
11 #include <sys/time.h>
12 #include <dirent.h>
13 #include <sys/socket.h>
14 #include <netinet/udp.h>
15 #include <net/if.h>
16 #include <osl.h>
17
18 #include "server.cmdline.h"
19 #include "para.h"
20 #include "error.h"
21 #include "string.h"
22 #include "afh.h"
23 #include "afs.h"
24 #include "server.h"
25 #include "list.h"
26 #include "send.h"
27 #include "vss.h"
28 #include "portable_io.h"
29 #include "net.h"
30 #include "fd.h"
31 #include "sched.h"
32 #include "close_on_fork.h"
33 #include "chunk_queue.h"
34
35 /**
36 * Time window during which ICMP Destination/Port Unreachable messages are
37 * ignored, covering transient receiver problems such as restarting the
38 * client, rebooting, reconfiguration, or handover.
39 */
40 #define UDP_MAX_UNREACHABLE_TIME 30
41
42 /** Describes one entry in the list of targets for the udp sender. */
43 struct udp_target {
44 /** The hostname (DNS name or IPv4/v6 address string). */
45 char host[MAX_HOSTLEN];
46 /** The UDP port. */
47 int port;
48 /** Track time (seconds) of last ICMP Port Unreachable error */
49 time_t last_unreachable;
50 /** Common sender client data */
51 struct sender_client *sc;
52 /** The opaque structure returned by vss_add_fec_client(). */
53 struct fec_client *fc;
54 /** The FEC parameters for this target. */
55 struct fec_client_parms fcp;
56 };
57
58 static struct list_head targets;
59 static int sender_status;
60
61 static void udp_close_target(struct sender_client *sc)
62 {
63 if (sc->cq != NULL) {
64 del_close_on_fork_list(sc->fd);
65 cq_destroy(sc->cq);
66 free(sc->name);
67 sc->name = NULL;
68 sc->cq = NULL;
69
70 }
71 }
72
73 static void udp_delete_target(struct udp_target *ut, const char *msg)
74 {
75
76 PARA_NOTICE_LOG("deleting %s#%d (%s) from list\n", ut->host,
77 ut->port, msg);
78 udp_close_target(ut->sc);
79 vss_del_fec_client(ut->fc);
80 list_del(&ut->sc->node);
81 free(ut->sc);
82 free(ut);
83 }
84
85 /**
86 * Perform AF-independent multicast sender setup.
87 *
88 * \param sc The connected sender client.
89 *
90 * \return Zero if okay, negative on error.
91 */
92 static int mcast_sender_setup(struct sender_client *sc)
93 {
94 struct sockaddr_storage ss;
95 socklen_t sslen = sizeof(ss);
96 int ttl = conf.udp_ttl_arg, id = 0;
97 const int on = 1;
98
99 if (conf.udp_mcast_iface_given) {
100 char *iface = conf.udp_mcast_iface_arg;
101
102 id = if_nametoindex(iface);
103 if (id == 0)
104 PARA_WARNING_LOG("could not resolve interface '%s', "
105 "using default", iface);
106 }
107
108 if (getpeername(sc->fd, (struct sockaddr *)&ss, &sslen) < 0)
109 goto err;
110
111 /* RFC 3493, 5.2: -1 means 'use kernel default' */
112 if (ttl < 0 || ttl > 255)
113 ttl = -1;
114
115 switch (ss.ss_family) {
116 case AF_INET:
117 if (!IN_MULTICAST(htonl(((struct sockaddr_in *)&ss)->sin_addr.s_addr)))
118 return 0;
119 if (id != 0) {
120 #ifdef HAVE_IP_MREQN
121 struct ip_mreqn mn;
122
123 memset(&mn, 0, sizeof(mn));
124 mn.imr_ifindex = id;
125 if (setsockopt(sc->fd, IPPROTO_IP, IP_MULTICAST_IF, &mn, sizeof(mn)) < 0)
126 goto err;
127 #else
128 PARA_ERROR_LOG("No support for setting outgoing IPv4 mcast interface.");
129 #endif
130 }
131 /*
132 * Enable receiving multicast messages generated on the local host
133 * At least on Linux, this is enabled by default.
134 */
135 if (setsockopt(sc->fd, IPPROTO_IP, IP_MULTICAST_LOOP, &on, sizeof(on)) < 0)
136 break;
137
138 /* Default: use local subnet (do not flood out into the WAN) */
139 if (ttl == -1)
140 ttl = 1;
141 if (setsockopt(sc->fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0)
142 break;
143 return 0;
144 case AF_INET6:
145 if (!IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)&ss)->sin6_addr))
146 return 0;
147 if (id != 0 &&
148 setsockopt(sc->fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &id, sizeof(id)) < 0)
149 break;
150 if (setsockopt(sc->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &on, sizeof(on)) < 0)
151 break;
152 if (setsockopt(sc->fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0)
153 break;
154 return 0;
155 default:
156 PARA_ERROR_LOG("address family %d not supported", ss.ss_family);
157 return -E_ADDRESS_LOOKUP;
158 }
159 err:
160 return -ERRNO_TO_PARA_ERROR(errno);
161 }
162
163 /** The maximal size of the per-target chunk queue. */
164 #define UDP_CQ_BYTES 40000
165
166 static void udp_init_session(struct sender_client *sc)
167 {
168 if (sc->cq == NULL) {
169 add_close_on_fork_list(sc->fd);
170 sc->cq = cq_new(UDP_CQ_BYTES);
171 sc->name = para_strdup(remote_name(sc->fd));
172 PARA_NOTICE_LOG("sending to udp %s\n", sc->name);
173 }
174 }
175
176 static void udp_shutdown_targets(void)
177 {
178 struct sender_client *sc, *tmp;
179 const char *buf;
180 size_t len = vss_get_fec_eof_packet(&buf);
181
182 list_for_each_entry_safe(sc, tmp, &targets, node)
183 if (sc->cq != NULL) {
184 /* ignore return value, closing the target anyway. */
185 (void)write(sc->fd, buf, len);
186 udp_close_target(sc);
187 }
188 }
189
190 static int udp_com_on(__a_unused struct sender_command_data *scd)
191 {
192 sender_status = SENDER_ON;
193 return 1;
194 }
195
196 static int udp_com_off(__a_unused struct sender_command_data *scd)
197 {
198 udp_shutdown_targets();
199 sender_status = SENDER_OFF;
200 return 1;
201 }
202
203 static int udp_com_delete(struct sender_command_data *scd)
204 {
205 struct sender_client *sc, *tmp;
206
207 list_for_each_entry_safe(sc, tmp, &targets, node) {
208 struct udp_target *ut = sc->private_data;
209
210 /* Unspecified port means wildcard port match */
211 if (scd->port > 0 && scd->port != ut->port)
212 continue;
213 if (strcmp(ut->host, scd->host))
214 continue;
215 udp_delete_target(ut, "com_delete");
216 }
217 return 1;
218 }
219
220 /** Initialize UDP session and set maximum payload size. */
221 static int udp_init_fec(struct sender_client *sc)
222 {
223 int mps;
224
225 udp_init_session(sc);
226 mps = generic_max_transport_msg_size(sc->fd) - sizeof(struct udphdr);
227 PARA_INFO_LOG("current MPS = %d bytes\n", mps);
228 return mps;
229 }
230
231 /** Check and clear socket error if any. */
232 static int udp_check_socket_state(struct udp_target *ut)
233 {
234 int ret;
235 socklen_t errlen = sizeof(ret);
236
237 if (getsockopt(ut->sc->fd, SOL_SOCKET, SO_ERROR, &ret, &errlen) < 0) {
238 PARA_ERROR_LOG("SO_ERROR failed: %s\n", strerror(ret));
239 return 0;
240 } else if (ret == 0) {
241 return 0;
242 } else if (ret == ECONNREFUSED) {
243 time_t dist = now->tv_sec - ut->last_unreachable;
244
245 if (dist <= UDP_MAX_UNREACHABLE_TIME) {
246 return 0;
247 } else if (dist > 2 * UDP_MAX_UNREACHABLE_TIME) {
248 ut->last_unreachable = now->tv_sec;
249 return 0;
250 } else {
251 /*
252 * unreachable_time < dist <= 2 * unreachable_time
253 * No errors are allowed during this time window.
254 */
255 PARA_NOTICE_LOG("Evicting %s#%d after %d seconds "
256 "of connection errors.\n",
257 ut->host, ut->port, (int)dist);
258 }
259 }
260 return -ERRNO_TO_PARA_ERROR(ret);
261 }
262
263 static int udp_send_fec(struct sender_client *sc, char *buf, size_t len)
264 {
265 struct udp_target *ut = sc->private_data;
266 int ret;
267
268 if (sender_status == SENDER_OFF)
269 return 0;
270 if (len == 0 && !cq_peek(ut->sc->cq))
271 return 0;
272 ret = udp_check_socket_state(ut);
273 if (ret < 0)
274 goto fail;
275 ret = send_queued_chunks(sc->fd, sc->cq);
276 if (ret < 0)
277 goto fail;
278 if (!ret) { /* still data left in the queue */
279 ret = cq_force_enqueue(sc->cq, buf, len);
280 assert(ret >= 0);
281 return 0;
282 }
283 ret = write_nonblock(sc->fd, buf, len);
284 if (ret == -ERRNO_TO_PARA_ERROR(ECONNREFUSED)) {
285 /*
286 * Happens if meanwhile an ICMP Destination / Port Unreachable
287 * has arrived. Ignore, persistent errors will be caught above.
288 */
289 ret = 0;
290 }
291 if (ret < 0)
292 goto fail;
293 if (ret != len) {
294 ret = cq_force_enqueue(sc->cq, buf + ret, len - ret);
295 assert(ret >= 0);
296 }
297 return 1;
298 fail:
299 udp_delete_target(ut, para_strerror(-ret));
300 return ret;
301 }
302
303 static void udp_add_target(struct sender_command_data *scd)
304 {
305 int ret, port = scd->port > 0 ? scd->port : conf.udp_default_port_arg;
306 struct udp_target *ut = para_calloc(sizeof(*ut));
307
308 strncpy(ut->host, scd->host, sizeof(ut->host));
309 ut->port = scd->port > 0 ? scd->port : conf.udp_default_port_arg;
310
311 ut->fcp.slices_per_group = scd->slices_per_group;
312 ut->fcp.data_slices_per_group = scd->data_slices_per_group;
313 ut->fcp.max_slice_bytes = scd->max_slice_bytes;
314 ut->fcp.init_fec = udp_init_fec;
315 ut->fcp.send_fec = udp_send_fec;
316
317 ut->sc = para_calloc(sizeof(*ut->sc));
318 ut->sc->private_data = ut;
319 ut->sc->fd = -1;
320 ret = para_connect_simple(IPPROTO_UDP, scd->host, port);
321 if (ret < 0)
322 goto err;
323 ut->sc->fd = ret;
324
325 ret = mcast_sender_setup(ut->sc);
326 if (ret < 0)
327 goto err;
328 ret = mark_fd_nonblocking(ut->sc->fd);
329 if (ret < 0)
330 goto err;
331 PARA_INFO_LOG("adding to target list (%s#%d)\n", ut->host, ut->port);
332 ut->fc = vss_add_fec_client(ut->sc, &ut->fcp);
333 para_list_add(&ut->sc->node, &targets);
334 return;
335 err:
336 if (ut->sc->fd >= 0)
337 close(ut->sc->fd);
338 PARA_NOTICE_LOG("failed to set up %s#%d (%s)- not adding it\n",
339 scd->host, port, para_strerror(-ret));
340 free(ut->sc);
341 free(ut);
342 }
343
344 static int udp_com_add(struct sender_command_data *scd)
345 {
346 udp_add_target(scd);
347 return 1;
348 }
349
350 static char *udp_info(void)
351 {
352 struct sender_client *sc;
353 char *ret, *tgts = NULL;
354
355 list_for_each_entry(sc, &targets, node) {
356 struct udp_target *ut = sc->private_data;
357 bool is_v6 = strchr(ut->host, ':') != NULL;
358 char *tmp = make_message("%s%s%s%s:%d/%u:%u:%u ", tgts ? : "",
359 is_v6 ? "[" : "", ut->host,
360 is_v6 ? "]" : "", ut->port,
361 ut->fcp.max_slice_bytes,
362 ut->fcp.data_slices_per_group,
363 ut->fcp.slices_per_group
364 );
365 free(tgts);
366 tgts = tmp;
367 }
368 ret = make_message(
369 "udp sender:\n"
370 "\tstatus: %s\n"
371 "\tport: %s\n"
372 "\ttargets: %s\n",
373 (sender_status == SENDER_ON)? "on" : "off",
374 stringify_port(conf.udp_default_port_arg, "udp"),
375 tgts? tgts : "(none)"
376 );
377 free(tgts);
378 return ret;
379 }
380
381 static void udp_init_target_list(void)
382 {
383 struct sender_command_data scd;
384 int i;
385
386 INIT_LIST_HEAD(&targets);
387 for (i = 0; i < conf.udp_target_given; i++) {
388 if (parse_fec_url(conf.udp_target_arg[i], &scd) < 0) {
389 PARA_CRIT_LOG("syntax error for udp target option "
390 "#%d, ignoring\n", i);
391 continue;
392 }
393 udp_add_target(&scd);
394 }
395 }
396
397 static char *udp_help(void)
398 {
399 return make_message(
400 "usage: {on|off}\n"
401 "usage: {add|delete} host[:port][/packet_size:k:n]\n"
402 "examples: add 224.0.1.38:1500 (IPv4 multicast)\n"
403 " add 224.0.1.38:1500/1400:14:16\n"
404 " (likewise, using 1400 byte packets, with 14\n"
405 " data slices per 16 slice FEC group)\n"
406 " add 10.10.1.42 (using default port)\n"
407 " add [FF05::42]:1500 (IPv6 multicast)\n"
408 " add [::1] (IPv6 localhost/default port)\n"
409 " delete myhost.net (host with port wildcard)\n"
410 " delete [badc0de::1] (IPv6 with port wildcard)\n"
411 );
412 }
413
414 /**
415 * The init function of para_server's udp sender.
416 *
417 * \param s Pointer to the udp sender struct.
418 *
419 * It initializes all function pointers of \a s and the list of udp targets.
420 */
421 void udp_send_init(struct sender *s)
422 {
423 INIT_LIST_HEAD(&targets);
424 s->info = udp_info;
425 s->help = udp_help;
426 s->send = NULL;
427 s->pre_select = NULL;
428 s->post_select = NULL;
429 s->shutdown_clients = udp_shutdown_targets;
430 s->client_cmds[SENDER_ON] = udp_com_on;
431 s->client_cmds[SENDER_OFF] = udp_com_off;
432 s->client_cmds[SENDER_DENY] = NULL;
433 s->client_cmds[SENDER_ALLOW] = NULL;
434 s->client_cmds[SENDER_ADD] = udp_com_add;
435 s->client_cmds[SENDER_DELETE] = udp_com_delete;
436 sender_status = SENDER_OFF;
437 udp_init_target_list();
438 if (!conf.udp_no_autostart_given)
439 sender_status = SENDER_ON;
440 PARA_DEBUG_LOG("udp sender init complete\n");
441 }