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