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