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