alfred: implement TCP support for server-to-server
As announced in A.L.F.R.E.D. issue #202 (https://www.open-mesh.org/issues/202),
I gave implementing server-to-server communication via TCP a spin.
The result is this - rather large - patch. I'd be glad for comments on the implementation. Also, I might split this into more commits, but I think I need ideas on how to properly separate the various parts - they mostly make sense to me as a unit only.
A rough overview:
alfred.h:
* struct tcp_connection is new, stores socket fd, pointer to data buffer, a counter for data already read from socket (TCP is a stream and we can't rely on self-contained datagrams), and list management
* struct interface is extended by a socket fd for the TCP socket and a list of "tcp_connection"s
* struct globals is extended by a "requestproto" field that stores desired operation mode.
main.c:
* TCP operation is for now triggered by a command line flag "-t".
netsock.c:
* for each interface, a TCP socket, bound/listening, is created and teared down along with the UDP sockets
* a list of tcp_connection sockets is kept, managed and cleared alongside
* the sockets are incorporated into the fd_set management for select()
recv.c:
* process_alfred_request() gets another argument for a socket fd. It just gets handed over to push_data() for sending the reply over the same socket the request came from.
* recv_alfred_stream() is new and the analog function to recv_alfred_packet(). It manages the data buffer and will act on a received packet if and when it is completely received.
ALFRED_REQUEST, ALFRED_PUSH_DATA and ALFRED_STATUS_TXEND packets are handled for now.
send.c:
* connect_tcp() function for setting up a new connection to a (TCP) server
* push_data() gets a new argument for a socket fd (c.f. process_alfred_request() in recv.c). If said argument is >=0, the function will operate on the socket rather than recvmsg() a datagram.
* sync_data() will act on requestproto configuration in globals struct and opt to make the sync via TCP if configured to do so
* send_alfred_stream() will use connect_tcp() to create a new connection, then send a packet analog to send_alfred_packet() and will then register the socket to the list of "tcp_connection"s for the interface it is currently operating upon. This way, the socket can receive a reply and will be handled as long as it is not closed on the other end.
server.c:
* check_if_socket() will tear down the TCP server and client sockets along with the UDP sockets
unix_sock.c:
* depending on the globals' requestproto setting, a REQUEST will be send via TCP rather than UDP.
Testing has been only in an artificial virtual setup for now. I have some hopes to get "mission clearance" for giving it a spin in our local "Freifunk" setup. Even then TCP will most certainly only be used on a handful of gateways since on the majority of nodes, reading values from A.L.F.R.E.D. is not really needed (they are mostly push-only).
I'd be glad to hear some feedback.
-hwh
Hans-Werner Hilse (1):
alfred: implement TCP support for server-to-server communication
alfred.h | 24 +++++++++- main.c | 8 +++- netsock.c | 148 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- recv.c | 88 ++++++++++++++++++++++++++++++++++-- send.c | 119 +++++++++++++++++++++++++++++++++++++++++++----- server.c | 11 +++++ unix_sock.c | 8 ++++ 7 files changed, 389 insertions(+), 17 deletions(-)
Signature · 1 line
2.7.3
RFC 1/1 alfred: implement TCP support for server-to-server communication
This implements support for using TCP instead of UDP for server-to-server communication for slave-to-master requests and for master-to-master synchronization.
Both these scenarios potentially exchange large chunks of data. Alfred distributes this data over multiple UDP packets. However, it - sensibly - uses 64kByte as the maximum UDP packet size. This makes sense since the data stored might well exceed the small sizes of typical network layer packages (like 1280 or 1500 Bytes).
Large UDP packets will then get fragmented by the Kernel on the IPv6 layer. For busy links and for exchange over very heterogenous network architectures, this lead to the loss of fragments (some or all of them). That makes proper communication a matter of luck for some users.
In all these cases, reliable communication is preferred. Instead of implementing our own handling for this upon UDP, using TCP will provide everything what's needed.
Implementation overview:
The interface struct is extended by a list of TCP sockets. Sockets in this list will get cleaned up when the interface goes away. The sockets in this list will be monitored for incoming data (or for them to get closed on the other side). Incoming data is handled more or less the same way as incoming UDP data is, with the added speciality that packets are accumulated since it is not arriving in self-contained datagrams like when using UDP. The packet data is stored along the socket information. When a full packet is received, it is handled just like a packet received as a UDP datagram.
A REQUEST is answered on the same socket as it was received on, since TCP allows bidirectional communication.
--- alfred.h | 24 +++++++++- main.c | 8 +++- netsock.c | 148 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- recv.c | 88 ++++++++++++++++++++++++++++++++++-- send.c | 119 +++++++++++++++++++++++++++++++++++++++++++----- server.c | 11 +++++ unix_sock.c | 8 ++++ 7 files changed, 389 insertions(+), 17 deletions(-)
alfred.h +23 -1
@@ -89,6 +89,11 @@ enum opmode { OPMODE_MASTER, }; +enum requestproto { +REQPROTO_UDP , +REQPROTO_TCP +}; +enum clientmode {CLIENT_NONE ,CLIENT_REQUEST_DATA ,
@@ -97,6 +102,15 @@ enum clientmode { CLIENT_CHANGE_INTERFACE, }; +struct tcp_connection { +int netsock; +struct in6_addr address; +struct alfred_tlv *packet; +uint16_t read; + +struct list_head list; +}; +struct interface {struct ether_addr hwaddr;struct in6_addr address;
@@ -104,6 +118,9 @@ struct interface {char *interface;int netsock;int netsock_mcast; +int netsock_tcp; + +struct list_head tcp_connections;struct hashtable_t *server_hash;
@@ -117,6 +134,7 @@ struct globals {struct server *best_server;/* NULL if we are a server ourselves */ const char *mesh_iface;enum opmode opmode; +enum requestproto requestproto;enum clientmode clientmode;int clientmode_arg;int clientmode_version;
@@ -155,6 +173,8 @@ int alfred_client_change_interface(struct globals *globals);/* recv.c */ int recv_alfred_packet (struct globals *globals,struct interface *interface,int recv_sock); +int recv_alfred_stream (struct globals *globals, +struct tcp_connection *tcp_connection);struct transaction_head *transaction_add (struct globals *globals,struct ether_addr mac,uint16_t id); struct transaction_head *
@@ -165,12 +185,14 @@ struct transaction_head *transaction_clean(struct globals *globals,/* send.c */ int push_data (struct globals *globals,struct interface *interface,struct in6_addr *destination,enum data_source max_source_level, -int type_filter,uint16_t tx_id); +int type_filter,uint16_t tx_id,int socket);int announce_master (struct globals *globals);int push_local_data (struct globals *globals);int sync_data (struct globals *globals);ssize_t send_alfred_packet (struct interface *interface,const struct in6_addr *dest,void *buf,int length); +ssize_t send_alfred_stream (struct interface *interface, +const struct in6_addr *dest,void *buf,int length);/* unix_sock.c */ int unix_sock_read (struct globals *globals);int unix_sock_open_daemon (struct globals *globals);
main.c +7 -1
@@ -58,6 +58,7 @@ static void alfred_usage(void)printf (" -m, --master start up the daemon in master mode, which\n" );printf (" accepts data from slaves and syncs it with\n" );printf (" other masters\n" ); +printf (" -t, --tcp use TCP protocol for server-to-server communication\n" );printf ("\n" );printf (" -u, --unix-path [path] path to unix socket used for client-server\n" );printf (" communication (default: \"" ALFRED_SOCK_PATH_DEFAULT "\")\n" );
@@ -149,6 +150,7 @@ static struct globals *alfred_init(int argc, char *argv[]) {"request", required_argument, NULL, 'r'}, {"interface", required_argument, NULL, 'i'}, {"master", no_argument, NULL, 'm'}, + {"tcp", no_argument, NULL, 't'}, {"help", no_argument, NULL, 'h'}, {"req-version", required_argument, NULL, 'V'}, {"modeswitch", required_argument, NULL, 'M'},
@@ -170,6 +172,7 @@ static struct globals *alfred_init(int argc, char *argv[])INIT_LIST_HEAD (&globals->interfaces); globals->change_interface =NULL ; globals->opmode =OPMODE_SLAVE ; + globals->requestproto =REQPROTO_UDP ; globals->clientmode =CLIENT_NONE ; globals->best_server =NULL ; globals->clientmode_version =0 ;
@@ -182,7 +185,7 @@ static struct globals *alfred_init(int argc, char *argv[])time_random_seed (); -while ((opt =getopt_long (argc, argv,"ms:r:hi:b:vV:M:I:u:dc:" , long_options, +while ((opt =getopt_long (argc, argv,"mts:r:hi:b:vV:M:I:u:dc:" , long_options, &opt_ind)) !=-1 ) {switch (opt) {case 'r' :
@@ -207,6 +210,9 @@ static struct globals *alfred_init(int argc, char *argv[])case 'm' : globals->opmode =OPMODE_MASTER ;break ; +case 't' : + globals->requestproto =REQPROTO_TCP ; +break ;case 'i' :netsock_set_interfaces (globals, optarg);break ;
netsock.c +147 -1
251 lines not on this page · raw message
recv.c +85 -3
109 lines not on this page · raw message
send.c +108 -11
175 lines not on this page · raw message
server.c +11 -0
@@ -222,6 +222,7 @@ static void check_if_socket(struct interface *interface) {int sock;struct ifreq ifr; +struct tcp_connection *tcp_connection, *tc;if (interface->netsock <0 )return ;
@@ -261,10 +262,20 @@ static void check_if_socket(struct interface *interface)return ; close: +list_for_each_entry_safe (tcp_connection, tc, + &interface->tcp_connections, list) { +shutdown (tcp_connection->netsock,SHUT_RDWR ); +close (tcp_connection->netsock); +list_del (&tcp_connection->list); +free (tcp_connection->packet); +free (tcp_connection); + }close (interface->netsock);close (interface->netsock_mcast); +close (interface->netsock_tcp); interface->netsock =-1 ; interface->netsock_mcast =-1 ; + interface->netsock_tcp =-1 ;close (sock); }
unix_sock.c +8 -0
@@ -251,6 +251,14 @@ static int unix_sock_req_data(struct globals *globals, head->client_socket = client_sock; head->requested_type = request->requested_type; +if (globals->requestproto ==REQPROTO_TCP ) { +if (!send_alfred_stream (interface, + &globals->best_server->address, + request,sizeof (*request))) +return 0 ; + } + +/* default and fallback case: UDP */ send_alfred_packet (interface, &globals->best_server->address, request,sizeof (*request));
Signature · 1 line
2.7.3
Re: [B.A.T.M.A.N.] [RFC] alfred: implement TCP support for server-to-server communication
Quoted 14 lines
send.c +14
-0+int connect_tcp (struct interface *interface,const struct in6_addr *dest) +{ +struct sockaddr_in6 dest_addr; +int sock; + +memset (&dest_addr,0 ,sizeof (dest_addr)); + dest_addr.sin6_family =AF_INET6 ; + dest_addr.sin6_port =htons (ALFRED_PORT ); + dest_addr.sin6_scope_id = interface->scope_id; +memcpy (&dest_addr.sin6_addr, dest,sizeof (*dest)); + + sock =socket (PF_INET6 ,SOCK_STREAM ,IPPROTO_TCP ); +if (sock <0 ) +return -1 ;
send.c line 59
Quoted 10 lines
send.c +10
-0+ +if (connect (sock, (struct sockaddr *)&dest_addr, +sizeof (struct sockaddr_in6 )) <0 ) { +close (sock); +return -1 ; + } + +return sock; +} +
Wouldn't this hang for a while and make the alfred server "unresponsive" when the remote is not reachable at this moment?.
Kind regards,
Sven
Re: [B.A.T.M.A.N.] [RFC] alfred: implement TCP support for server-to-server communication
Hi,
Am 2016-03-21 10:13, schrieb Sven Eckelmann:
Quoted 9 lines
recv.c int recv_alfred_packet(struct globals *globals, struct interface *interface, · line 467
Quoted 6 lines
+int
connect_tcp(struct interface *interface, const struct in6_addr *dest)recv.c +3
-0+{ +} +Wouldn't this hang for a while and make the alfred server "unresponsive" when the remote is not reachable at this moment?.
Yes, it most definitely would, you're right.
If the route I'm following is generally right, I think I'll work on this part (*that* I can make into a different commit). The connect() can be made non-blocking, and I would add the socket and the request to send to a list (possibly using the transaction list that already exists right now).
-hwh
Re: [B.A.T.M.A.N.] [RFC] alfred: implement TCP support for server-to-server communication
Hello Hans-Werner,
Quoted 14 lines · On Monday 21 March 2016 10:38:55 Hans-Werner Hilse wrote:
Hi,
Am 2016-03-21 10:13, schrieb Sven Eckelmann:
Quoted 10 lines
recv.c int recv_alfred_packet(struct globals *globals, struct interface *interface, · line 467
Quoted 6 lines
+int
connect_tcp(struct interface *interface, const struct in6_addr *dest)recv.c +3
-0+{ +} +Wouldn't this hang for a while and make the alfred server "unresponsive"
when the remote is not reachable at this moment?.
Quoted 7 lines
Yes, it most definitely would, you're right.
If the route I'm following is generally right, I think I'll work on this part (*that* I can make into a different commit). The
connect()can be made non-blocking, and I would add the socket and the request to send to a list (possibly using the transaction list that already exists right now).
I think you are on the right track - in the ticket, we discussed whether we do TCP or just decrease the fragment size, but doing TCP is probably the more reliable option to do.
I agree that we should definitely use nonblocking operation.
I was also thinking that we might want to use TCP in general when the packet size reaches a certain threshold (e.g. >3kB), and always use UDP for small packets. Then we would also not need the commandline option (unless you think we should leave a way to force a mode). Any thoughts on that?
I don't think you have to split your patch further, at least I don't see a good way right now. I didn't do a deep review yet, but can do in the next iteration with the nonblocking stuff implemented. :)
Thanks a lot for taking care of this!
Simon
Re: [B.A.T.M.A.N.] [RFC] alfred: implement TCP support for server-to-server communication
Quoted 4 lines
I was also thinking that we might want to use TCP in general when the packet size reaches a certain threshold (e.g. >3kB), and always use UDP for small packets. Then we would also not need the commandline option (unless you think we should leave a way to force a mode). Any thoughts on that?
Hi Simon
Either/or behaviour like this can cause hard to understand issues. e.g. i opened a hole in the firewall to let Alfred traffic though, but it suddenly stops when the network gets big, because it swapped to TCP, and i don't have a hole of that. Similar for QoS filters, i prioritise UDP Alfred packets, but when it swaps to TCP they are not prioritised and get discarded during congestion...
Andrew
RFC v2 1/1 alfred: implement TCP support for server-to-server communication
In reply to Hans-Werner HilseThis implements support for using TCP instead of UDP for server-to-server communication for slave-to-master requests and for master-to-master synchronization.
Both these scenarios potentially exchange large chunks of data. Alfred distributes this data over multiple UDP packets. However, it - sensibly - uses 64kByte as the maximum UDP packet size. This makes sense since the data stored might well exceed the small sizes of typical network layer packages (like 1280 or 1500 Bytes).
Large UDP packets will then get fragmented by the Kernel on the IPv6 layer. For busy links and for exchange over very heterogenous network architectures, this lead to the loss of fragments (some or all of them). That makes proper communication a matter of luck for some users.
In all these cases, reliable communication is preferred. Instead of implementing our own handling for this upon UDP, using TCP will provide everything what's needed.
Implementation overview:
The interface struct is extended by a list of TCP sockets. Sockets in this list will get cleaned up when the interface goes away. The sockets in this list will be monitored for incoming data (or for them to get closed on the other side). Incoming data is handled more or less the same way as incoming UDP data is, with the added speciality that packets are accumulated since it is not arriving in self-contained datagrams like when using UDP. The packet data is stored along the socket information. When a full packet is received, it is handled just like a packet received as a UDP datagram.
A REQUEST is answered on the same socket as it was received on, since TCP allows bidirectional communication.
---
Changes in v2:
- non-blocking sending over TCP sockets
alfred.h | 44 ++++++++++- main.c | 8 +- netsock.c | 239 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- recv.c | 90 ++++++++++++++++++++++- send.c | 204 +++++++++++++++++++++++++++++++++++++++++++++++---- server.c | 30 +++++++- unix_sock.c | 14 ++++ 7 files changed, 607 insertions(+), 22 deletions(-)
alfred.h +43 -1
@@ -89,6 +89,11 @@ enum opmode { OPMODE_MASTER, }; +enum requestproto { +REQPROTO_UDP , +REQPROTO_TCP +}; +enum clientmode {CLIENT_NONE ,CLIENT_REQUEST_DATA ,
@@ -97,6 +102,27 @@ enum clientmode { CLIENT_CHANGE_INTERFACE, }; +enum tcp_close { +CLOSE_WHEN_READ , +CLOSE_WHEN_WRITTEN , +CLOSED_FOR_READING , +}; + +struct tcp_connection { +int netsock; +struct in6_addr address; + +struct alfred_tlv *packet; +uint16_t read; + +uint8_t *send_packet; +uint32_t send_length; +uint32_t written; + +enum tcp_close close; +struct list_head list; +}; +struct interface {struct ether_addr hwaddr;struct in6_addr address;
@@ -104,6 +130,9 @@ struct interface {char *interface;int netsock;int netsock_mcast; +int netsock_tcp; + +struct list_head tcp_connections;struct hashtable_t *server_hash;
@@ -117,6 +146,7 @@ struct globals {struct server *best_server;/* NULL if we are a server ourselves */ const char *mesh_iface;enum opmode opmode; +enum requestproto requestproto;enum clientmode clientmode;int clientmode_arg;int clientmode_version;
@@ -139,6 +169,7 @@ struct globals {#define debugFree (ptr, num) free(ptr)#define MAX_PAYLOAD ((1 << 16) - 1 - sizeof(struct udphdr)) +#define MAX_UDP_ANSWER 1280 - 40 - sizeof(struct udphdr)extern const struct in6_addr in6addr_localmcast;
@@ -155,6 +186,9 @@ int alfred_client_change_interface(struct globals *globals);/* recv.c */ int recv_alfred_packet (struct globals *globals,struct interface *interface,int recv_sock); +int recv_alfred_stream (struct globals *globals, +struct interface *interface, +struct tcp_connection *tcp_connection);struct transaction_head *transaction_add (struct globals *globals,struct ether_addr mac,uint16_t id); struct transaction_head *
@@ -165,12 +199,17 @@ struct transaction_head *transaction_clean(struct globals *globals,/* send.c */ int push_data (struct globals *globals,struct interface *interface,struct in6_addr *destination,enum data_source max_source_level, -int type_filter,uint16_t tx_id); +int type_filter,uint16_t tx_id, +struct tcp_connection *tcp_connection);int announce_master (struct globals *globals);int push_local_data (struct globals *globals);int sync_data (struct globals *globals);ssize_t send_alfred_packet (struct interface *interface,const struct in6_addr *dest,void *buf,int length); +ssize_t open_alfred_stream (struct interface *interface, +const struct in6_addr *dest,void *buf,int length, +enum tcp_close close_mode); +ssize_t send_alfred_stream (struct tcp_connection *tcp_client);/* unix_sock.c */ int unix_sock_read (struct globals *globals);int unix_sock_open_daemon (struct globals *globals);
@@ -187,8 +226,11 @@ int netsock_set_interfaces(struct globals *globals, char *interfaces);struct interface *netsock_first_interface (struct globals *globals);void netsock_reopen (struct globals *globals);int netsock_prepare_select (struct globals *globals,fd_set *fds,int maxsock); +int netsock_prepare_write_select (struct globals *globals,fd_set *fds, +int maxsock);void netsock_check_error (struct globals *globals,fd_set *errfds);int netsock_receive_packet (struct globals *globals,fd_set *fds); +int netsock_send (struct globals *globals,fd_set *fds);int netsock_own_address (const struct globals *globals,const struct in6_addr *address);/* util.c */
main.c +7 -1
@@ -58,6 +58,7 @@ static void alfred_usage(void)printf (" -m, --master start up the daemon in master mode, which\n" );printf (" accepts data from slaves and syncs it with\n" );printf (" other masters\n" ); +printf (" -t, --tcp use TCP protocol for server-to-server communication\n" );printf ("\n" );printf (" -u, --unix-path [path] path to unix socket used for client-server\n" );printf (" communication (default: \"" ALFRED_SOCK_PATH_DEFAULT "\")\n" );
@@ -149,6 +150,7 @@ static struct globals *alfred_init(int argc, char *argv[]) {"request", required_argument, NULL, 'r'}, {"interface", required_argument, NULL, 'i'}, {"master", no_argument, NULL, 'm'}, + {"tcp", no_argument, NULL, 't'}, {"help", no_argument, NULL, 'h'}, {"req-version", required_argument, NULL, 'V'}, {"modeswitch", required_argument, NULL, 'M'},
@@ -170,6 +172,7 @@ static struct globals *alfred_init(int argc, char *argv[])INIT_LIST_HEAD (&globals->interfaces); globals->change_interface =NULL ; globals->opmode =OPMODE_SLAVE ; + globals->requestproto =REQPROTO_UDP ; globals->clientmode =CLIENT_NONE ; globals->best_server =NULL ; globals->clientmode_version =0 ;
@@ -182,7 +185,7 @@ static struct globals *alfred_init(int argc, char *argv[])time_random_seed (); -while ((opt =getopt_long (argc, argv,"ms:r:hi:b:vV:M:I:u:dc:" , long_options, +while ((opt =getopt_long (argc, argv,"mts:r:hi:b:vV:M:I:u:dc:" , long_options, &opt_ind)) !=-1 ) {switch (opt) {case 'r' :
@@ -207,6 +210,9 @@ static struct globals *alfred_init(int argc, char *argv[])case 'm' : globals->opmode =OPMODE_MASTER ;break ; +case 't' : + globals->requestproto =REQPROTO_TCP ; +break ;case 'i' :netsock_set_interfaces (globals, optarg);break ;
netsock.c +238 -1
343 lines not on this page · raw message
recv.c +87 -3
111 lines not on this page · raw message
send.c +190 -14
263 lines not on this page · raw message
server.c +28 -2
@@ -222,6 +222,7 @@ static void check_if_socket(struct interface *interface) {int sock;struct ifreq ifr; +struct tcp_connection *tcp_connection, *tc;if (interface->netsock <0 )return ;
@@ -261,10 +262,23 @@ static void check_if_socket(struct interface *interface)return ; close: +list_for_each_entry_safe (tcp_connection, tc, + &interface->tcp_connections, list) { +shutdown (tcp_connection->netsock,SHUT_RDWR ); +close (tcp_connection->netsock); +list_del (&tcp_connection->list); +if (tcp_connection->packet) +free (tcp_connection->packet); +if (tcp_connection->send_packet) +free (tcp_connection->send_packet); +free (tcp_connection); + }close (interface->netsock);close (interface->netsock_mcast); +close (interface->netsock_tcp); interface->netsock =-1 ; interface->netsock_mcast =-1 ; + interface->netsock_tcp =-1 ;close (sock); }
@@ -338,7 +352,7 @@ int alfred_server(struct globals *globals) {int maxsock, ret, recvs;struct timespec last_check, now, tv; -fd_set fds, errfds; +fd_set fds, wrfds, errfds;int num_socks;if (create_hashes (globals))
@@ -381,14 +395,23 @@ int alfred_server(struct globals *globals)netsock_reopen (globals);FD_ZERO (&fds); +FD_ZERO (&wrfds);FD_ZERO (&errfds);FD_SET (globals->unix_sock, &fds); maxsock = globals->unix_sock; +/* testing write sockets might clean up socket list, +* so this has to be done before proceeding to the +* list of sockets tested for reading +*/ + maxsock =netsock_prepare_write_select (globals, &wrfds, + maxsock); + maxsock =netsock_prepare_write_select (globals, &errfds, + maxsock); maxsock =netsock_prepare_select (globals, &fds, maxsock); maxsock =netsock_prepare_select (globals, &errfds, maxsock); - ret =pselect (maxsock +1 , &fds,NULL , &errfds, &tv,NULL ); + ret =pselect (maxsock +1 , &fds, &wrfds, &errfds, &tv,NULL );if (ret ==-1 ) {perror ("main loop select failed ..." );
@@ -404,6 +427,9 @@ int alfred_server(struct globals *globals)if (recvs >0 )continue ; } + +if (netsock_send (globals, &wrfds) >0 ) +continue ; }clock_gettime (CLOCK_MONOTONIC , &last_check);
unix_sock.c +14 -0
@@ -222,6 +222,7 @@ static int unix_sock_req_data(struct globals *globals, {int len;uint16_t id; +uint8_t *buf;struct transaction_head *head =NULL ;struct interface *interface;
@@ -251,6 +252,19 @@ static int unix_sock_req_data(struct globals *globals, head->client_socket = client_sock; head->requested_type = request->requested_type; +if (globals->requestproto ==REQPROTO_TCP ) { + buf =malloc (sizeof (*request)); +if (buf) { +memcpy (buf, request,sizeof (*request)); +if (!open_alfred_stream (interface, + &globals->best_server->address, + buf,sizeof (*request), +CLOSE_WHEN_READ )) +return 0 ; + } + } + +/* default and fallback case: UDP */ send_alfred_packet (interface, &globals->best_server->address, request,sizeof (*request));
Signature · 1 line
2.7.3
Re: [B.A.T.M.A.N.] [RFC v2] alfred: implement TCP support for server-to-server communication
Hi,
Am 2016-03-27 20:26, schrieb Hans-Werner Hilse:
Quoted 3 lines
[...]
Changes in v2:
- non-blocking sending over TCP sockets
This uses non-blocking IO also for sending via TCP.
TCP is chosen when the message size exceeds MAX_UDP_ANSWER (from alfred.h), which is for now conservatively chosen to fit in bad-case MTU settings - or if the data request came via TCP (as the same connection is then used for the reply).
I've run this for a few hours in a test setup with 3x alfred as master and 30 slaves, with some errors, dups and latency thrown in for good measure (yay for VDE, virtual distributed ethernet).
The current implementation has a downside: non-blocking TCP sending for now assembles the full data that is to be sent in a memory buffer, which will then get sent bit by bit (depending on buffers, network and the remote side). This is a consequence of the lack of a good way to guarantee a consistent state of the data store over the time it takes until the full message stream is completely transmitted to the remote end (the data store might get modified between beginning and finishing transmission).
An alternative approach - and a major redesign of data storage - would be some kind of log-based approach. I'm not so sure if that really is warranted for, and in any case, that's quite a different undertaking.
The flag "-t" is still present and will toggle whether alfred will try to request using TCP at all. This will allow to use a TCP-enabled alfred in an environment that is not yet fully TCP enabled (this matters only for the master servers AFAICS).
-hwh
Re: [B.A.T.M.A.N.] [RFC v2] alfred: implement TCP support for server-to-server communication
i installed your patch .. here some feedback/problems a had
with OpenWRT and gluon i succeeded ... actually testet it on a tplink841n-v9 runs with or without -m (master) for a while (30 min++) /usr/sbin/alfred -i br-client -b bat0 -m
[my big wonder is , why do these router always only output their own entry for alfred -r 158 , while there are master .. on debian based master this is filled up to 290]
on debian 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt20-1+deb8u3 (2016-01-17) x86_64 GNU/Linux
i got some build errors .. and than after a short while in alfred master mode ... segmentation fault
(1 or 2 minutes, sometimes only seconds)
this after master announcements and already collecting some 90+ alfred information.
####### patch stuff
####### wget https://patchwork.open-mesh.org/patch/15944/raw/ -O tcp.patch patch < tcp.patch
patching file alfred.h
Hunk #1 succeeded at 90 (offset 1 line).
Hunk #2 succeeded at 103 (offset 1 line).
Hunk #3 succeeded at 131 (offset 1 line).
Hunk #4 succeeded at 147 (offset 1 line).
Hunk #5 succeeded at 170 (offset 1 line).
Hunk #6 succeeded at 187 (offset 1 line).
Hunk #7 succeeded at 200 (offset 1 line).
Hunk #8 succeeded at 227 (offset 1 line).
patching file main.c
patching file netsock.c
patching file recv.c
patching file send.c
patching file server.c
patching file unix_sock.c
Hunk #1 succeeded at 229 (offset 7 lines).
Hunk #2 succeeded at 259 (offset 7 lines).
####### build errors
make
CC main.o
CC server.o
CC client.o
CC netsock.o
CC send.o
CC recv.orecv.c: In function 'recv_alfred_packet':
recv.c:436:48: warning: passing argument 5 of 'process_alfred_request' makes pointer from integer without a cast
(struct alfred_request_v0 *)packet, -1);
^
recv.c:302:12: note: expected 'struct tcp_connection *' but argument is
of type 'int'
static int process_alfred_request(struct globals *globals,
^
CC hash.o
CC unix_sock.o
CC util.o
CC debugfs.o
CC batadv_query.o
LD alfredmake -C vis all
make[1]: Entering directory '/home/freifunk/alfred/vis'
CC vis.o
CC debugfs.o
LD batadv-vis
make[1]: Leaving directory '/home/freifunk/alfred/vis' make -C gpsd all
make[1]: Entering directory '/home/freifunk/alfred/gpsd'
CC alfred-gpsd.o
LD alfred-gpsd
make[1]: Leaving directory '/home/freifunk/alfred/gpsd'
###### var/log/kern.log
Apr 22 20:01:06 fffr-spielwiese kernel: [4398895.515177] alfred[31733]: segfault at 2f ip 0000000000406034 sp 00007fff9dc39580 error 6 in alfred[400000+d000] Apr 22 20:02:05 fffr-spielwiese kernel: [4398954.569665] alfred[32657]:
segfault at 2f ip 0000000000406034 sp 00007ffc45b97d50 error 6 in alfred[400000+d000]
Quoted 36 lines · Hans-Werner Hilse
Hi,
Am 2016-03-27 20:26, schrieb Hans-Werner Hilse:
Quoted 3 lines
[...]
Changes in v2:
- non-blocking sending over TCP socketsThis uses non-blocking IO also for sending via TCP.
TCP is chosen when the message size exceedsMAX_UDP_ANSWER(fromalfred.h), which is for now conservatively chosen to fit in bad-case MTU settings - or if the data request came via TCP (as the same connection is then used for the reply).I've run this for a few hours in a test setup with 3x alfred as master and 30 slaves, with some errors, dups and latency thrown in for good measure (yay for VDE, virtual distributed ethernet).
The current implementation has a downside: non-blocking TCP sending for now assembles the full data that is to be sent in a memory buffer, which will then get sent bit by bit (depending on buffers, network and the remote side). This is a consequence of the lack of a good way to guarantee a consistent state of the data store over the time it takes until the full message stream is completely transmitted to the remote end (the data store might get modified between beginning and finishing transmission).
An alternative approach - and a major redesign of data storage - would be some kind of log-based approach. I'm not so sure if that really is warranted for, and in any case, that's quite a different undertaking.
The flag "-t" is still present and will toggle whether alfred will try to request using TCP at all. This will allow to use a TCP-enabled alfred in an environment that is not yet fully TCP enabled (this matters only for the master servers AFAICS).
-hwh
Signature · 1 line
make the world nicer, please use PGP encryption
Re: [B.A.T.M.A.N.] [RFC v2] alfred: implement TCP support for server-to-server communication
Hi,
first: many thanks for testing.
Am 2016-04-22 20:37, schrieb jens:
Quoted 12 lines
####### patch stuff
####### wget https://patchwork.open-mesh.org/patch/15944/raw/ -O tcp.patch patch < tcp.patch
patching filealfred.h
Hunk #1 succeeded at 90 (offset 1 line).
Hunk #2 succeeded at 103 (offset 1 line).
Hunk #3 succeeded at 131 (offset 1 line).
Hunk #4 succeeded at 147 (offset 1 line).
Hunk #5 succeeded at 170 (offset 1 line).
Hunk #6 succeeded at 187 (offset 1 line).
Hunk #7 succeeded at 200 (offset 1 line).
Hunk #8 succeeded at 227 (offset 1 line).
Quoted 8 lines
patching file
main.c
patching filenetsock.c
patching filerecv.c
patching filesend.c
patching fileserver.c
patching fileunix_sock.c
Hunk #1 succeeded at 229 (offset 7 lines).
Hunk #2 succeeded at 259 (offset 7 lines).
Is this patch run against git master's HEAD?
Even then, offsets might occur, there were some minor changes since I've sent the patch.
Quoted 11 lines
####### build errors
make
CC main.o
CC server.o
CC client.o
CC netsock.o
CC send.o
CC recv.orecv.c: In function 'recv_alfred_packet':
recv.c:436:48: warning: passing argument 5 of 'process_alfred_request' makes pointer from integer without a cast
Quoted 6 lines
(struct alfred_request_v0 *)packet, -1);
^
recv.c:302:12: note: expected 'struct tcp_connection *' but argument is
of type 'int'
static int process_alfred_request(struct globals *globals,
^Oh dear. I'm not sure how my gcc managed to stay quiet at this. Also, I'm astonished it didn't blew to pieces during my tests.
The "-1" is a leftover from an earlier implementation, where I still thought that I hadn't that much TCP connection state to carry around - so I passed socket fds. I no longer do that, and switched to a struct holding the state. In this case, the "-1" should be NULL. It seems to be the only oversight of this kind as far as a I can see.
I will update the patch shortly.
Quoted 7 lines
###### var/log/kern.log
Apr 22 20:01:06 fffr-spielwiese kernel: [4398895.515177] alfred[31733]: segfault at 2f ip 0000000000406034 sp 00007fff9dc39580 error 6 in alfred[400000+d000] Apr 22 20:02:05 fffr-spielwiese kernel: [4398954.569665] alfred[32657]:segfault at 2f ip 0000000000406034 sp
00007ffc45b97d50error 6 in alfred[400000+d000]
In the light of a dereference of a "-1" pointer, that doesn't come as a surprise.
-hwh