2 * Copyright (C) 2005-2010 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file udp_send.c Para_server's udp sender. */
13 #include <sys/socket.h>
14 #include <netinet/udp.h>
18 #include "server.cmdline.h"
28 #include "portable_io.h"
32 #include "close_on_fork.h"
33 #include "chunk_queue.h"
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.
40 #define UDP_MAX_UNREACHABLE_TIME 30
42 /** Describes one entry in the list of targets for the udp sender. */
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
;
52 static struct list_head targets
;
53 static int sender_status
;
55 static void udp_close_target(struct sender_client
*sc
)
63 static void udp_delete_target(struct sender_client
*sc
, const char *msg
)
65 struct udp_target
*ut
= sc
->private_data
;
67 PARA_NOTICE_LOG("deleting %s (%s) from list\n", sc
->name
, msg
);
70 del_close_on_fork_list(sc
->fd
);
71 vss_del_fec_client(ut
->fc
);
79 * Perform AF-independent multicast sender setup.
81 * \param sc The connected sender client.
83 * \return Zero if okay, negative on error.
85 static int mcast_sender_setup(struct sender_client
*sc
)
87 struct sockaddr_storage ss
;
88 socklen_t sslen
= sizeof(ss
);
89 int ttl
= conf
.udp_ttl_arg
, id
= 0;
92 if (conf
.udp_mcast_iface_given
) {
93 char *iface
= conf
.udp_mcast_iface_arg
;
95 id
= if_nametoindex(iface
);
97 PARA_WARNING_LOG("could not resolve interface '%s', "
98 "using default", iface
);
101 if (getpeername(sc
->fd
, (struct sockaddr
*)&ss
, &sslen
) < 0)
103 assert(ss
.ss_family
== AF_INET
|| ss
.ss_family
== AF_INET6
);
105 /* RFC 3493, 5.2: -1 means 'use kernel default' */
106 if (ttl
< 0 || ttl
> 255)
109 switch (ss
.ss_family
) {
111 if (!IN_MULTICAST(htonl(((struct sockaddr_in
*)&ss
)->sin_addr
.s_addr
)))
117 memset(&mn
, 0, sizeof(mn
));
119 if (setsockopt(sc
->fd
, IPPROTO_IP
, IP_MULTICAST_IF
, &mn
, sizeof(mn
)) < 0)
122 PARA_ERROR_LOG("No support for setting outgoing IPv4 mcast interface.");
126 * Enable receiving multicast messages generated on the local host
127 * At least on Linux, this is enabled by default.
129 if (setsockopt(sc
->fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
, &on
, sizeof(on
)) < 0)
132 /* Default: use local subnet (do not flood out into the WAN) */
135 if (setsockopt(sc
->fd
, IPPROTO_IP
, IP_MULTICAST_TTL
, &ttl
, sizeof(ttl
)) < 0)
139 if (!IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6
*)&ss
)->sin6_addr
))
142 setsockopt(sc
->fd
, IPPROTO_IPV6
, IPV6_MULTICAST_IF
, &id
, sizeof(id
)) < 0)
144 if (setsockopt(sc
->fd
, IPPROTO_IPV6
, IPV6_MULTICAST_LOOP
, &on
, sizeof(on
)) < 0)
146 if (setsockopt(sc
->fd
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
, &ttl
, sizeof(ttl
)) < 0)
151 return -ERRNO_TO_PARA_ERROR(errno
);
154 /** The maximal size of the per-target chunk queue. */
155 #define UDP_CQ_BYTES 40000
157 static void udp_init_session(struct sender_client
*sc
)
159 if (sc
->cq
== NULL
) {
160 sc
->cq
= cq_new(UDP_CQ_BYTES
);
161 PARA_NOTICE_LOG("sending to udp %s\n", sc
->name
);
165 static void udp_shutdown_targets(void)
167 struct sender_client
*sc
, *tmp
;
169 size_t len
= vss_get_fec_eof_packet(&buf
);
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
);
179 static int udp_resolve_target(const char *url
, struct sender_command_data
*scd
)
184 ret
= parse_fec_url(url
, scd
);
187 port
= scd
->port
> 0 ? scd
->port
: conf
.udp_default_port_arg
;
189 ret
= para_connect_simple(IPPROTO_UDP
, scd
->host
, port
);
193 result
= remote_name(ret
);
196 if (!parse_url(result
, scd
->host
, sizeof(scd
->host
), &scd
->port
))
197 return -E_ADDRESS_LOOKUP
;
201 static int udp_com_on(__a_unused
struct sender_command_data
*scd
)
203 sender_status
= SENDER_ON
;
207 static int udp_com_off(__a_unused
struct sender_command_data
*scd
)
209 udp_shutdown_targets();
210 sender_status
= SENDER_OFF
;
214 static struct sender_client
*udp_lookup_target(struct sender_command_data
*scd
)
216 struct sender_client
*sc
, *tmp
;
217 char host
[MAX_HOSTLEN
];
220 list_for_each_entry_safe(sc
, tmp
, &targets
, node
) {
221 parse_url(sc
->name
, host
, sizeof(host
), &port
);
223 /* Unspecified port means wildcard port match */
224 if (scd
->port
> 0 && scd
->port
!= port
)
226 if (strcmp(host
, scd
->host
))
233 static int udp_com_delete(struct sender_command_data
*scd
)
235 struct sender_client
*sc
= udp_lookup_target(scd
);
238 udp_delete_target(sc
, "com_delete");
241 PARA_NOTICE_LOG("not deleting non-existing target '%s'\n", scd
->host
);
242 return -E_TARGET_NOT_FOUND
;
245 /** Initialize UDP session and set maximum payload size. */
246 static int udp_init_fec(struct sender_client
*sc
)
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
);
256 /** Check and clear socket error if any. */
257 static int udp_check_socket_state(struct sender_client
*sc
)
259 struct udp_target
*ut
= sc
->private_data
;
261 socklen_t errlen
= sizeof(ret
);
263 if (getsockopt(sc
->fd
, SOL_SOCKET
, SO_ERROR
, &ret
, &errlen
) < 0) {
264 PARA_ERROR_LOG("SO_ERROR failed: %s\n", strerror(ret
));
266 } else if (ret
== 0) {
268 } else if (ret
== ECONNREFUSED
) {
269 time_t dist
= now
->tv_sec
- ut
->last_unreachable
;
271 if (dist
<= UDP_MAX_UNREACHABLE_TIME
) {
273 } else if (dist
> 2 * UDP_MAX_UNREACHABLE_TIME
) {
274 ut
->last_unreachable
= now
->tv_sec
;
278 * unreachable_time < dist <= 2 * unreachable_time
279 * No errors are allowed during this time window.
281 PARA_NOTICE_LOG("Evicting %s after %d seconds "
282 "of connection errors.\n",
283 sc
->name
, (int)dist
);
286 return -ERRNO_TO_PARA_ERROR(ret
);
289 static int udp_send_fec(struct sender_client
*sc
, char *buf
, size_t len
)
293 if (sender_status
== SENDER_OFF
)
295 if (len
== 0 && !cq_peek(sc
->cq
))
297 ret
= udp_check_socket_state(sc
);
300 ret
= send_queued_chunks(sc
->fd
, sc
->cq
);
303 if (!ret
) { /* still data left in the queue */
304 ret
= cq_force_enqueue(sc
->cq
, buf
, len
);
308 ret
= write_nonblock(sc
->fd
, buf
, len
);
309 if (ret
== -ERRNO_TO_PARA_ERROR(ECONNREFUSED
)) {
311 * Happens if meanwhile an ICMP Destination / Port Unreachable
312 * has arrived. Ignore, persistent errors will be caught above.
319 ret
= cq_force_enqueue(sc
->cq
, buf
+ ret
, len
- ret
);
324 udp_delete_target(sc
, para_strerror(-ret
));
328 static int udp_com_add(struct sender_command_data
*scd
)
331 struct udp_target
*ut
;
332 struct sender_client
*sc
= udp_lookup_target(scd
);
335 PARA_NOTICE_LOG("target %s already exists - not adding it\n",
337 return -E_TARGET_EXISTS
;
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
;
346 sc
->private_data
= ut
;
348 ret
= para_connect_simple(IPPROTO_UDP
, scd
->host
, scd
->port
);
353 ret
= mcast_sender_setup(sc
);
356 ret
= mark_fd_nonblocking(sc
->fd
);
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
);
368 PARA_NOTICE_LOG("failed to set up %s:%d (%s)- not adding it\n",
369 scd
->host
, scd
->port
, para_strerror(-ret
));
375 static char *udp_info(void)
377 struct sender_client
*sc
;
378 char *ret
, *tgts
= NULL
;
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
395 (sender_status
== SENDER_ON
)? "on" : "off",
396 stringify_port(conf
.udp_default_port_arg
, "udp"),
397 tgts
? tgts
: "(none)"
403 static void udp_init_target_list(void)
405 struct sender_command_data scd
;
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
]);
418 static char *udp_help(void)
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"
438 * The init function of para_server's udp sender.
440 * \param s Pointer to the udp sender struct.
442 * It initializes all function pointers of \a s and the list of udp targets.
444 void udp_send_init(struct sender
*s
)
446 INIT_LIST_HEAD(&targets
);
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");