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
the Free Software Foundation ; either version 2 of the License , or
( 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
along with this program ; if not , write to the Free Software
Foundation , Inc . , 675 Mass Ave , Cambridge , MA 0213 9 , USA .
2006-11-28 03:51:33 +03:00
*/
# include "includes.h"
2007-01-23 03:38:45 +03:00
# include "lib/tdb/include/tdb.h"
2006-11-28 03:51:33 +03:00
# include "lib/events/events.h"
# include "system/network.h"
# include "system/filesys.h"
2007-01-23 03:38:45 +03:00
# include "../include/ctdb_private.h"
2006-11-28 03:51:33 +03:00
# include "ctdb_tcp.h"
2007-04-26 17:28:13 +04:00
/*
initialise tcp portion of a ctdb node
*/
static int ctdb_tcp_add_node ( struct ctdb_node * node )
{
2007-06-02 02:41:19 +04:00
struct ctdb_tcp * ctcp = talloc_get_type ( node - > ctdb - > private_data ,
struct ctdb_tcp ) ;
2007-04-26 17:28:13 +04:00
struct ctdb_tcp_node * tnode ;
2007-06-02 02:41:19 +04:00
tnode = talloc_zero ( ctcp , struct ctdb_tcp_node ) ;
2007-04-26 17:28:13 +04:00
CTDB_NO_MEMORY ( node - > ctdb , tnode ) ;
tnode - > fd = - 1 ;
node - > private_data = tnode ;
2007-06-02 02:41:19 +04:00
tnode - > queue = ctdb_queue_setup ( node - > ctdb , ctcp , tnode - > fd , CTDB_TCP_ALIGNMENT ,
2007-04-26 17:28:13 +04:00
ctdb_tcp_tnode_cb , node ) ;
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
{
2006-11-28 09:56:10 +03:00
int i ;
2006-11-28 03:51:33 +03:00
2007-05-30 08:46:14 +04:00
/* listen on our own address */
if ( ctdb_tcp_listen ( ctdb ) ! = 0 ) return - 1 ;
2007-04-26 17:28:13 +04:00
for ( i = 0 ; i < ctdb - > num_nodes ; i + + ) {
if ( ctdb_tcp_add_node ( ctdb - > nodes [ i ] ) ! = 0 ) {
DEBUG ( 0 , ( " methods->add_node failed at %d \n " , i ) ) ;
return - 1 ;
}
}
2007-05-30 07:26:50 +04:00
return 0 ;
}
/*
start the protocol going
*/
static int ctdb_tcp_start ( struct ctdb_context * ctdb )
{
int i ;
2006-11-28 03:51:33 +03:00
/* startup connections to the other servers - will happen on
next event loop */
2006-11-28 09:56:10 +03:00
for ( i = 0 ; i < ctdb - > num_nodes ; i + + ) {
struct ctdb_node * node = * ( ctdb - > nodes + i ) ;
2007-06-02 02:41:19 +04:00
struct ctdb_tcp_node * tnode = talloc_get_type (
node - > private_data , struct ctdb_tcp_node ) ;
2006-12-01 07:45:24 +03:00
if ( ! ( ctdb - > flags & CTDB_FLAG_SELF_CONNECT ) & &
ctdb_same_address ( & ctdb - > address , & node - > address ) ) continue ;
2007-06-02 02:41:19 +04:00
event_add_timed ( ctdb - > ev , tnode , timeval_zero ( ) ,
2006-11-28 03:51:33 +03:00
ctdb_tcp_node_connect , node ) ;
}
return 0 ;
}
2007-06-02 02:41:19 +04:00
/*
shutdown the transport
*/
static void ctdb_tcp_shutdown ( struct ctdb_context * ctdb )
{
struct ctdb_tcp * ctcp = talloc_get_type ( ctdb - > private_data ,
struct ctdb_tcp ) ;
talloc_free ( ctcp ) ;
ctdb - > private_data = NULL ;
}
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 ,
2007-06-02 02:41:19 +04:00
. allocate_pkt = ctdb_tcp_allocate_pkt ,
. shutdown = ctdb_tcp_shutdown ,
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 ;
2007-04-11 22:12:15 +04:00
ctdb - > private_data = ctcp ;
2006-11-28 03:51:33 +03:00
ctdb - > methods = & ctdb_tcp_methods ;
return 0 ;
}