2006-11-28 03:51:33 +03:00
/*
ctdb over TCP
Copyright ( C ) Andrew Tridgell 2006
2007-05-31 07:50:53 +04:00
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
2007-07-10 09:29:31 +04:00
the Free Software Foundation ; either version 3 of the License , or
2007-05-31 07:50:53 +04:00
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
2006-11-28 03:51:33 +03:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
2007-05-31 07:50:53 +04:00
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
2007-07-10 09:29:31 +04:00
along with this program ; if not , see < http : //www.gnu.org/licenses/>.
2006-11-28 03:51:33 +03:00
*/
2015-10-26 08:50:46 +03:00
# include "replace.h"
2006-11-28 03:51:33 +03:00
# include "system/network.h"
# include "system/filesys.h"
2015-10-26 08:50:46 +03:00
# include <talloc.h>
# include <tevent.h>
# include "lib/util/time.h"
# include "lib/util/debug.h"
# include "ctdb_private.h"
2015-10-23 06:17:34 +03:00
# include "common/common.h"
2015-11-11 07:22:52 +03:00
# include "common/logging.h"
2015-10-23 06:17:34 +03:00
2006-11-28 03:51:33 +03:00
# include "ctdb_tcp.h"
2008-12-02 05:26:30 +03:00
static int tnode_destructor ( struct ctdb_tcp_node * tnode )
{
2009-03-24 05:45:11 +03:00
// struct ctdb_node *node = talloc_find_parent_bytype(tnode, struct ctdb_node);
2008-12-02 05:26:30 +03:00
2019-08-09 08:06:34 +03:00
if ( tnode - > out_fd ! = - 1 ) {
close ( tnode - > out_fd ) ;
tnode - > out_fd = - 1 ;
2008-12-02 05:26:30 +03:00
}
return 0 ;
}
2007-04-26 17:28:13 +04:00
/*
2019-08-09 08:06:34 +03:00
initialise tcp portion of a ctdb node
2007-04-26 17:28:13 +04:00
*/
static int ctdb_tcp_add_node ( struct ctdb_node * node )
{
struct ctdb_tcp_node * tnode ;
2008-10-07 11:12:54 +04:00
tnode = talloc_zero ( node , struct ctdb_tcp_node ) ;
2007-04-26 17:28:13 +04:00
CTDB_NO_MEMORY ( node - > ctdb , tnode ) ;
2019-08-09 08:06:34 +03:00
tnode - > out_fd = - 1 ;
2019-08-09 08:29:36 +03:00
tnode - > ctdb = node - > ctdb ;
2019-11-12 04:04:22 +03:00
node - > transport_data = tnode ;
2008-12-02 05:26:30 +03:00
talloc_set_destructor ( tnode , tnode_destructor ) ;
2007-04-26 17:28:13 +04:00
return 0 ;
}
2006-11-28 03:51:33 +03:00
/*
2007-05-30 07:26:50 +04:00
initialise transport structures
2006-11-28 03:51:33 +03:00
*/
2007-05-30 07:26:50 +04:00
static int ctdb_tcp_initialise ( struct ctdb_context * ctdb )
2006-11-28 03:51:33 +03:00
{
2019-06-21 22:29:53 +03:00
unsigned int i ;
2006-11-28 03:51:33 +03:00
2007-05-30 08:46:14 +04:00
/* listen on our own address */
2008-02-19 06:44:48 +03:00
if ( ctdb_tcp_listen ( ctdb ) ! = 0 ) {
DEBUG ( DEBUG_CRIT , ( __location__ " Failed to start listening on the CTDB socket \n " ) ) ;
exit ( 1 ) ;
}
2007-05-30 08:46:14 +04:00
2009-06-01 08:18:34 +04:00
for ( i = 0 ; i < ctdb - > num_nodes ; i + + ) {
if ( ctdb - > nodes [ i ] - > flags & NODE_FLAGS_DELETED ) {
continue ;
}
2007-04-26 17:28:13 +04:00
if ( ctdb_tcp_add_node ( ctdb - > nodes [ i ] ) ! = 0 ) {
2008-02-04 12:07:15 +03:00
DEBUG ( DEBUG_CRIT , ( " methods->add_node failed at %d \n " , i ) ) ;
2007-04-26 17:28:13 +04:00
return - 1 ;
}
}
2007-05-30 07:26:50 +04:00
return 0 ;
}
/*
start the protocol going
*/
2008-12-02 05:26:30 +03:00
static int ctdb_tcp_connect_node ( struct ctdb_node * node )
2007-05-30 07:26:50 +04:00
{
2008-12-02 05:26:30 +03:00
struct ctdb_context * ctdb = node - > ctdb ;
struct ctdb_tcp_node * tnode = talloc_get_type (
2019-11-12 04:04:22 +03:00
node - > transport_data , struct ctdb_tcp_node ) ;
2007-05-30 07:26:50 +04:00
2008-12-02 05:26:30 +03:00
/* startup connection to the other server - will happen on
2006-11-28 03:51:33 +03:00
next event loop */
2015-02-20 03:47:23 +03:00
if ( ! ctdb_same_address ( ctdb - > address , & node - > address ) ) {
2015-10-26 08:50:09 +03:00
tnode - > connect_te = tevent_add_timer ( ctdb - > ev , tnode ,
timeval_zero ( ) ,
ctdb_tcp_node_connect ,
node ) ;
2006-11-28 03:51:33 +03:00
}
return 0 ;
}
2007-10-19 02:58:30 +04:00
/*
shutdown and try to restart a connection to a node after it has been
disconnected
*/
static void ctdb_tcp_restart ( struct ctdb_node * node )
{
struct ctdb_tcp_node * tnode = talloc_get_type (
2019-11-12 04:04:22 +03:00
node - > transport_data , struct ctdb_tcp_node ) ;
2007-10-19 02:58:30 +04:00
2008-02-04 12:07:15 +03:00
DEBUG ( DEBUG_NOTICE , ( " Tearing down connection to dead node :%d \n " , node - > pnn ) ) ;
2020-02-29 13:54:51 +03:00
ctdb_tcp_stop_incoming ( node ) ;
2020-02-28 13:36:00 +03:00
ctdb_tcp_stop_outgoing ( node ) ;
2007-10-19 02:58:30 +04:00
2015-10-26 08:50:09 +03:00
tnode - > connect_te = tevent_add_timer ( node - > ctdb - > ev , tnode ,
timeval_zero ( ) ,
ctdb_tcp_node_connect , node ) ;
2007-10-19 02:58:30 +04:00
}
2006-11-28 03:51:33 +03:00
2007-06-02 02:41:19 +04:00
/*
shutdown the transport
*/
static void ctdb_tcp_shutdown ( struct ctdb_context * ctdb )
{
2019-11-07 17:26:01 +03:00
uint32_t i ;
2019-11-12 04:14:18 +03:00
TALLOC_FREE ( ctdb - > transport_data ) ;
2019-11-07 17:26:01 +03:00
for ( i = 0 ; i < ctdb - > num_nodes ; i + + ) {
2019-11-12 04:04:22 +03:00
TALLOC_FREE ( ctdb - > nodes [ i ] - > transport_data ) ;
2019-11-07 17:26:01 +03:00
}
2007-06-02 02:41:19 +04:00
}
2008-12-02 05:26:30 +03:00
/*
start the transport
*/
static int ctdb_tcp_start ( struct ctdb_context * ctdb )
{
2019-06-21 22:29:53 +03:00
unsigned int i ;
2008-12-02 05:26:30 +03:00
2009-06-01 08:18:34 +04:00
for ( i = 0 ; i < ctdb - > num_nodes ; i + + ) {
if ( ctdb - > nodes [ i ] - > flags & NODE_FLAGS_DELETED ) {
continue ;
}
2008-12-02 05:26:30 +03:00
ctdb_tcp_connect_node ( ctdb - > nodes [ i ] ) ;
}
return 0 ;
}
2007-06-02 02:41:19 +04:00
2006-12-19 04:03:10 +03:00
/*
transport packet allocator - allows transport to control memory for packets
*/
2007-04-19 04:37:44 +04:00
static void * ctdb_tcp_allocate_pkt ( TALLOC_CTX * mem_ctx , size_t size )
2006-12-19 04:03:10 +03:00
{
/* tcp transport needs to round to 8 byte alignment to ensure
that we can use a length header and 64 bit elements in
structures */
2006-12-19 04:07:07 +03:00
size = ( size + ( CTDB_TCP_ALIGNMENT - 1 ) ) & ~ ( CTDB_TCP_ALIGNMENT - 1 ) ;
2007-04-19 04:37:44 +04:00
return talloc_size ( mem_ctx , size ) ;
2006-12-19 04:03:10 +03:00
}
2006-11-28 03:51:33 +03:00
static const struct ctdb_methods ctdb_tcp_methods = {
2007-05-30 07:26:50 +04:00
. initialise = ctdb_tcp_initialise ,
2007-04-26 17:28:13 +04:00
. start = ctdb_tcp_start ,
. queue_pkt = ctdb_tcp_queue_pkt ,
. add_node = ctdb_tcp_add_node ,
2008-12-02 05:26:30 +03:00
. connect_node = ctdb_tcp_connect_node ,
2007-06-02 02:41:19 +04:00
. allocate_pkt = ctdb_tcp_allocate_pkt ,
. shutdown = ctdb_tcp_shutdown ,
2007-10-19 02:58:30 +04:00
. restart = ctdb_tcp_restart ,
2006-11-28 03:51:33 +03:00
} ;
2008-02-19 06:44:48 +03:00
static int tcp_ctcp_destructor ( struct ctdb_tcp * ctcp )
{
2019-11-12 04:12:46 +03:00
ctcp - > ctdb - > transport_data = NULL ;
2008-02-19 06:44:48 +03:00
ctcp - > ctdb - > methods = NULL ;
return 0 ;
}
2006-11-28 03:51:33 +03:00
/*
initialise tcp portion of ctdb
*/
int ctdb_tcp_init ( struct ctdb_context * ctdb )
{
struct ctdb_tcp * ctcp ;
ctcp = talloc_zero ( ctdb , struct ctdb_tcp ) ;
CTDB_NO_MEMORY ( ctdb , ctcp ) ;
ctcp - > listen_fd = - 1 ;
2008-02-19 06:44:48 +03:00
ctcp - > ctdb = ctdb ;
2019-11-12 04:12:46 +03:00
ctdb - > transport_data = ctcp ;
2006-11-28 03:51:33 +03:00
ctdb - > methods = & ctdb_tcp_methods ;
2008-02-19 06:44:48 +03:00
talloc_set_destructor ( ctcp , tcp_ctcp_destructor ) ;
2006-11-28 03:51:33 +03:00
return 0 ;
}