From 931a4270bd037884639cb3f5fb4bd3c3e57d7e05 Mon Sep 17 00:00:00 2001 From: ftillier Date: Tue, 27 Sep 2005 00:48:36 +0000 Subject: [PATCH] [WSD] Make QP destruction asynchronous to speed up socket closure. Cleanup socket creation by consolitdating operations into create_socket_info. Signed-off-by: Fab Tillier (ftillier@silverstorm.com) git-svn-id: svn://openib.tc.cornell.edu/gen1@96 ad392aa1-c5ef-ae45-8dd8-e69d62a5ef86 --- trunk/ulp/wsd/user/ibsp_duplicate.c | 93 +++++++------------ trunk/ulp/wsd/user/ibsp_iblow.c | 22 ++--- trunk/ulp/wsd/user/ibspdll.c | 137 +++++++++------------------- trunk/ulp/wsd/user/ibspproto.h | 3 +- trunk/ulp/wsd/user/sockinfo.c | 124 ++++++++++++++++--------- 5 files changed, 166 insertions(+), 213 deletions(-) diff --git a/trunk/ulp/wsd/user/ibsp_duplicate.c b/trunk/ulp/wsd/user/ibsp_duplicate.c index e7108f5d..9258da44 100644 --- a/trunk/ulp/wsd/user/ibsp_duplicate.c +++ b/trunk/ulp/wsd/user/ibsp_duplicate.c @@ -79,19 +79,17 @@ setup_duplicate_socket( fname ); // name of map object if( h == NULL ) { - IBSP_ERROR( - ("CreateFileMapping failed with %d\n", GetLastError()) ); + IBSP_ERROR( ("CreateFileMapping failed with %d\n", GetLastError()) ); ret = WSAENETDOWN; - goto done; + goto err1; } /* Make sure this file already existed. */ if( GetLastError() != ERROR_ALREADY_EXISTS ) { - CloseHandle( h ); IBSP_ERROR( ("not mapping for socket duplicate info\n") ); ret = WSAENETDOWN; - goto done; + goto err1; } /* Get a pointer to the file-mapped shared memory. */ @@ -102,34 +100,9 @@ setup_duplicate_socket( 0 ); // default: map entire file if( dup_info == NULL ) { - IBSP_ERROR( - ("MapViewOfFile failed with %d\n", GetLastError()) ); + IBSP_ERROR( ("MapViewOfFile failed with %d\n", GetLastError()) ); ret = WSAENETDOWN; - goto done; - } - - /* Check if we already have a switch socket handle. We can have both cases - * if the two processes are calling IBSPDuplicate back and forth. */ - if( socket_info->switch_socket == INVALID_SOCKET ) - { - socket_info->switch_socket = - g_ibsp.up_call_table.lpWPUCreateSocketHandle(0, - (DWORD_PTR) socket_info, &ret ); - - if( socket_info->switch_socket == INVALID_SOCKET ) - { - IBSP_ERROR( - ("WPUCreateSocketHandle() failed: %d", ret) ); - goto done; - } - else - { - fzprint(("%s():%d:0x%x:0x%x: socket_info=0x%p switch_socket=0x%p \n", - __FUNCTION__, __LINE__, GetCurrentProcessId(), GetCurrentThreadId(), - socket_info, socket_info->switch_socket)); - - STAT_INC( wpusocket_num ); - } + goto err1; } socket_info->peer_addr = dup_info->peer_addr; @@ -143,32 +116,21 @@ setup_duplicate_socket( socket_info->port = get_port_from_ip_address( dup_info->local_addr.sin_addr ); if( socket_info->port == NULL ) { - IBSP_ERROR( - ("incoming destination IP address not local (%s)\n", + IBSP_ERROR( ("incoming destination IP address not local (%s)\n", inet_ntoa( dup_info->local_addr.sin_addr )) ); ret = WSAENETDOWN; - goto done; - } - - ret = ib_create_socket( socket_info ); - if( ret ) - { - IBSP_ERROR_EXIT( - ("ib_create socket failed with %d\n", ret) ); - ret = WSAENOBUFS; - goto done; + goto err1; } /* Get the GUID for the remote IP address. */ ret = query_guid_address( socket_info->port, - socket_info->peer_addr.sin_addr.S_un.S_addr, - &dest_port_guid ); + socket_info->peer_addr.sin_addr.S_un.S_addr, &dest_port_guid ); if( ret ) { IBSP_ERROR( ("query_guid_address failed for IP %08x\n", socket_info->peer_addr.sin_addr.s_addr) ); - ret = WSAEHOSTUNREACH; - goto done; + ret = WSAENETDOWN; + goto err1; } /* Get the path record */ @@ -177,39 +139,51 @@ setup_duplicate_socket( { IBSP_ERROR( ("query_pr failed for IP %08x\n", socket_info->peer_addr.sin_addr.s_addr) ); - ret = WSAEHOSTUNREACH; - goto done; + ret = WSAENETDOWN; + goto err1; } IBSP_CHANGE_SOCKET_STATE( socket_info, IBSP_DUPLICATING_NEW ); socket_info->h_event = CreateEvent( NULL, FALSE, FALSE, NULL ); + if( !socket_info->h_event ) + { + IBSP_ERROR( ("CreateEvent failed (%d)\n", GetLastError()) ); + goto err1; + } + + ret = ib_create_socket( socket_info ); + if( ret ) + { + IBSP_ERROR( ("ib_create socket failed with %d\n", ret) ); + goto err1; + } /* Connects the QP. */ ret = ib_connect( socket_info, &path_rec ); if( ret != WSAEWOULDBLOCK ) { IBSP_ERROR( ("ib_connect failed (%d)\n", ret) ); - goto done; + goto err2; } if( WaitForSingleObject( socket_info->h_event, INFINITE ) != WAIT_OBJECT_0 ) - { IBSP_ERROR( ("WaitForSingleObject failed\n") ); - } cl_spinlock_acquire( &socket_info->mutex ); if( socket_info->socket_state != IBSP_CONNECTED ) { cl_spinlock_release( &socket_info->mutex ); IBSP_ERROR( ("Failed to connect\n") ); - ret = WSAEHOSTUNREACH; - goto done; + ret = WSAENETDOWN; +err2: + ib_destroy_socket( socket_info ); + } + else + { + cl_spinlock_release( &socket_info->mutex ); } - cl_spinlock_release( &socket_info->mutex ); - - ret = 0; -done: +err1: if( h ) CloseHandle( h ); @@ -220,7 +194,6 @@ done: } IBSP_EXIT( IBSP_DBG_CONN ); - return ret; } diff --git a/trunk/ulp/wsd/user/ibsp_iblow.c b/trunk/ulp/wsd/user/ibsp_iblow.c index a01c3af2..1acd0638 100644 --- a/trunk/ulp/wsd/user/ibsp_iblow.c +++ b/trunk/ulp/wsd/user/ibsp_iblow.c @@ -873,23 +873,18 @@ ib_destroy_socket( if( socket_info->qp ) { - status = ib_destroy_qp( socket_info->qp, ib_sync_destroy ); - if( status ) + cl_atomic_inc( &socket_info->ref_cnt ); + status = ib_destroy_qp( socket_info->qp, deref_socket_info ); + if( status != IB_SUCCESS ) { - IBSP_ERROR( ("ib_destroy_qp failed (%d)\n", status) ); + IBSP_ERROR( ("ib_destroy_qp returned %s\n", + ib_get_err_str( status )) ); + deref_socket_info( socket_info ); } - else - { - IBSP_TRACE( IBSP_DBG_WQ, - ("%s():%d:0x%x:0x%x: ib_destroy_qp() finished\n", __FUNCTION__, - __LINE__, GetCurrentProcessId(), GetCurrentThreadId()) ); - - socket_info->qp = NULL; - STAT_DEC( qp_num ); + ib_release_cq_tinfo( socket_info->cq_tinfo ); - ib_release_cq_tinfo( socket_info->cq_tinfo ); - } + socket_info->qp = NULL; } IBSP_EXIT( IBSP_DBG_EP ); @@ -938,6 +933,7 @@ ib_create_socket( &socket_info->qp ); if( status ) { + ib_release_cq_tinfo( socket_info->cq_tinfo ); IBSP_ERROR_EXIT( ("ib_create_qp returned %s\n", ib_get_err_str( status )) ); return WSAENOBUFS; diff --git a/trunk/ulp/wsd/user/ibspdll.c b/trunk/ulp/wsd/user/ibspdll.c index 7933df09..e7bf41b8 100644 --- a/trunk/ulp/wsd/user/ibspdll.c +++ b/trunk/ulp/wsd/user/ibspdll.c @@ -242,7 +242,6 @@ IBSPAccept( struct ibsp_socket_info *socket_info = (struct ibsp_socket_info *)s; struct ibsp_socket_info *new_socket_info; int ret; - SOCKET new_socket = INVALID_SOCKET; WSABUF caller_id; WSABUF callee_id; struct listen_incoming *incoming; @@ -397,45 +396,24 @@ reject: } /* Create a new socket here */ - new_socket_info = create_socket_info(); - if( new_socket_info == NULL ) + new_socket_info = create_socket_info( lpErrno ); + if( !new_socket_info ) { cl_spinlock_release( &socket_info->mutex ); - IBSP_ERROR_EXIT( ("create_socket_info return NULL\n") ); - *lpErrno = WSAENOBUFS; - return INVALID_SOCKET; - } - - new_socket = g_ibsp.up_call_table.lpWPUCreateSocketHandle( - 0, (DWORD_PTR)new_socket_info, lpErrno ); - - if( new_socket == INVALID_SOCKET ) - { - cl_spinlock_release( &socket_info->mutex ); - - IBSP_ERROR_EXIT( - ("WPUCreateSocketHandle() failed: %d", *lpErrno) ); - deref_socket_info( new_socket_info ); + IBSP_ERROR_EXIT( ("create_socket_info failed (%d)\n", *lpErrno) ); return INVALID_SOCKET; } - STAT_INC( wpusocket_num ); - - fzprint(("%s():%d:0x%x:0x%x: new_socket_info=0x%p new_socket=0x%p \n", __FUNCTION__, - __LINE__, GetCurrentProcessId(), GetCurrentThreadId(), new_socket_info, - new_socket)); - /* Time to allocate our IB QP */ new_socket_info->port = port; - ret = ib_create_socket( new_socket_info ); - if( ret ) + *lpErrno = ib_create_socket( new_socket_info ); + if( *lpErrno ) { cl_spinlock_release( &socket_info->mutex ); + deref_socket_info( new_socket_info ); - IBSP_ERROR_EXIT( - ("ib_create socket failed with %d\n", ret) ); - *lpErrno = WSAENOBUFS; + IBSP_ERROR_EXIT( ("ib_create socket failed with %d\n", *lpErrno) ); return INVALID_SOCKET; } @@ -456,11 +434,11 @@ reject: memcpy( addr, &incoming->params.source, sizeof(struct sockaddr_in) ); *addrlen = sizeof(struct sockaddr_in); - IBSP_TRACE( IBSP_DBG_CONN, ("The socket address of connecting entity is\n") ); + IBSP_TRACE( IBSP_DBG_CONN, + ("The socket address of connecting entity is\n") ); DebugPrintSockAddr( IBSP_DBG_CONN, gdbg_lvl, &incoming->params.source ); new_socket_info->peer_addr = incoming->params.source; - new_socket_info->switch_socket = new_socket; new_socket_info->h_event = CreateEvent( NULL, FALSE, FALSE, NULL ); @@ -473,10 +451,6 @@ reject: { IBSP_CHANGE_SOCKET_STATE( new_socket_info, IBSP_CREATE ); cl_spinlock_release( &new_socket_info->mutex ); - /* Free the socket descriptor */ - fzprint(("%s():%d:0x%x:0x%x: socket=0x%p calling lpWPUCloseSocketHandle=0x%p\n", - __FUNCTION__, __LINE__, GetCurrentProcessId(), GetCurrentThreadId(), - socket_info, socket_info->switch_socket)); if( g_ibsp.up_call_table.lpWPUCloseSocketHandle( new_socket_info->switch_socket, &ret ) == SOCKET_ERROR ) @@ -536,8 +510,6 @@ reject: /* The accept failed (by a REJ for instance). */ /* Free the socket descriptor */ - fzprint(("%s():%d:0x%x:0x%x: socket=0x%p calling lpWPUCloseSocketHandle=0x%p\n", __FUNCTION__, __LINE__, GetCurrentProcessId(), GetCurrentThreadId(), socket_info, socket_info->switch_socket)); - if( g_ibsp.up_call_table.lpWPUCloseSocketHandle( new_socket_info->switch_socket, lpErrno ) == SOCKET_ERROR ) { @@ -558,8 +530,8 @@ reject: new_socket_info = (struct ibsp_socket_info *)INVALID_SOCKET; } - CL_TRACE_EXIT(IBSP_DBG_CONN, gdbg_lvl, - ("returns new SocketID (0x%x)\n", new_socket) ); + IBSP_TRACE_EXIT( IBSP_DBG_CONN, + ("returns new SocketID (0x%x)\n", new_socket_info) ); return (SOCKET) new_socket_info; } @@ -2095,86 +2067,69 @@ IBSPSocket( if( af != AF_INET ) { - IBSP_ERROR( + IBSP_ERROR_EXIT( ("bad family %d instead of %d\n", af, AF_INET) ); *lpErrno = WSAEAFNOSUPPORT; - goto error; + return INVALID_SOCKET; } if( type != SOCK_STREAM ) { - IBSP_ERROR( + IBSP_ERROR_EXIT( ("bad type %d instead of %d\n", type, SOCK_STREAM) ); *lpErrno = WSAEPROTOTYPE; - goto error; + return INVALID_SOCKET; } if( protocol != IPPROTO_TCP ) { - IBSP_ERROR( + IBSP_ERROR_EXIT( ("bad protocol %d instead of %d\n", protocol, IPPROTO_TCP) ); *lpErrno = WSAEPROTONOSUPPORT; - goto error; + return INVALID_SOCKET; } if( (dwFlags != WSA_FLAG_OVERLAPPED) ) { - IBSP_ERROR( + IBSP_ERROR_EXIT( ("dwFlags is not WSA_FLAG_OVERLAPPED (%x)\n", dwFlags) ); *lpErrno = WSAEINVAL; - goto error; + return INVALID_SOCKET; } - socket_info = create_socket_info(); + socket_info = create_socket_info( lpErrno ); if( socket_info == NULL ) { - IBSP_ERROR( ("create_socket_info return NULL\n") ); - *lpErrno = WSAENOBUFS; - goto error; + IBSP_ERROR_EXIT( ("create_socket_info return NULL\n") ); + return INVALID_SOCKET; } if( lpProtocolInfo->dwProviderReserved != 0 ) { /* This is a duplicate socket. */ - int ret; - - ret = setup_duplicate_socket( socket_info, lpProtocolInfo->dwProviderReserved ); - if( ret ) - { - IBSP_ERROR( - ("setup_duplicate_socket failed with %d\n",ret) ); - *lpErrno = ret; - goto error; - } - } - else - { - socket_info->switch_socket = - g_ibsp.up_call_table.lpWPUCreateSocketHandle( 0, - (DWORD_PTR) socket_info, - lpErrno ); - - if( socket_info->switch_socket != INVALID_SOCKET ) + *lpErrno = setup_duplicate_socket( + socket_info, lpProtocolInfo->dwProviderReserved ); + if( *lpErrno ) { - IBSP_TRACE1( IBSP_DBG_SI, ("socket_info=0x%p switch_socket=0x%p \n", - socket_info, socket_info->switch_socket) ); + int error; - STAT_INC( wpusocket_num ); - } - } + if( g_ibsp.up_call_table.lpWPUCloseSocketHandle( + socket_info->switch_socket, &error ) == SOCKET_ERROR ) + { + IBSP_ERROR( ("WPUCloseSocketHandle failed: %d\n", error) ); + } + else + { + STAT_DEC( wpusocket_num ); + } - if( socket_info->switch_socket == INVALID_SOCKET ) - { - IBSP_ERROR( - ("WPUCreateSocketHandle() failed: %d\n", *lpErrno) ); - /* lpErrno has just been set */ - goto error; - } + socket_info->switch_socket = INVALID_SOCKET; - /* Success */ - if( lpProtocolInfo->dwProviderReserved != 0 ) - { - CL_ASSERT( socket_info->socket_state == IBSP_CONNECTED ); + deref_socket_info( socket_info ); + IBSP_ERROR( + ("setup_duplicate_socket failed with %d\n", *lpErrno) ); + return INVALID_SOCKET; + } } else { @@ -2199,16 +2154,6 @@ IBSPSocket( ("returning socket handle %p\n", socket_info) ); return (SOCKET) socket_info; - -error: - if( socket_info ) - deref_socket_info( socket_info ); - - CL_ASSERT( *lpErrno != 0 ); - - IBSP_ERROR_EXIT( ("Returning error %d\n", *lpErrno) ); - - return INVALID_SOCKET; } diff --git a/trunk/ulp/wsd/user/ibspproto.h b/trunk/ulp/wsd/user/ibspproto.h index eb84bd02..a0ea72b4 100644 --- a/trunk/ulp/wsd/user/ibspproto.h +++ b/trunk/ulp/wsd/user/ibspproto.h @@ -31,7 +31,8 @@ /* protos from socketinfo.c */ struct ibsp_socket_info * -create_socket_info( void ); +create_socket_info( + OUT LPINT lpErrno ); void AL_API deref_socket_info( diff --git a/trunk/ulp/wsd/user/sockinfo.c b/trunk/ulp/wsd/user/sockinfo.c index 883a9bc6..b73f8c8b 100644 --- a/trunk/ulp/wsd/user/sockinfo.c +++ b/trunk/ulp/wsd/user/sockinfo.c @@ -32,6 +32,10 @@ #include "ibspdll.h" +static void +free_socket_info( + IN struct ibsp_socket_info *socket_info ); + /* * Function: create_socket_info * @@ -39,7 +43,8 @@ * Allocates a new socket info context structure and initializes some fields. */ struct ibsp_socket_info * -create_socket_info(void) +create_socket_info( + OUT LPINT lpErrno ) { struct ibsp_socket_info *socket_info; @@ -51,18 +56,28 @@ create_socket_info(void) { IBSP_TRACE_EXIT( IBSP_DBG_SI, ("HeapAlloc() failed: %d\n", GetLastError()) ); + *lpErrno = WSAENOBUFS; return NULL; } - cl_spinlock_init( &socket_info->mutex ); - + cl_spinlock_construct( &socket_info->mutex ); + cl_spinlock_construct( &socket_info->buf_mem_list.mutex ); + cl_spinlock_construct( &socket_info->send_lock ); + cl_spinlock_construct( &socket_info->recv_lock ); cl_qlist_init( &socket_info->buf_mem_list.list ); - cl_spinlock_init( &socket_info->buf_mem_list.mutex ); - cl_qlist_init( &socket_info->listen.list ); - cl_spinlock_init( &socket_info->send_lock ); - cl_spinlock_init( &socket_info->recv_lock ); + if( cl_spinlock_init( &socket_info->mutex ) != CL_SUCCESS ) + goto err; + + if( cl_spinlock_init( &socket_info->buf_mem_list.mutex ) != CL_SUCCESS ) + goto err; + + if( cl_spinlock_init( &socket_info->send_lock ) != CL_SUCCESS ) + goto err; + + if( cl_spinlock_init( &socket_info->recv_lock ) != CL_SUCCESS ) + goto err; #ifdef _DEBUG_ memset( socket_info->recv_wr, 0x38, sizeof(socket_info->send_wr) ); @@ -70,20 +85,76 @@ create_socket_info(void) memset( socket_info->recv_wr, 0x38, sizeof(socket_info->dup_wr) ); #endif - socket_info->switch_socket = INVALID_SOCKET; + socket_info->switch_socket = + g_ibsp.up_call_table.lpWPUCreateSocketHandle( + 0, (DWORD_PTR)socket_info, lpErrno ); + + if( socket_info->switch_socket == INVALID_SOCKET ) + { + IBSP_ERROR( + ("WPUCreateSocketHandle() failed: %d", *lpErrno) ); +err: + free_socket_info( socket_info ); + IBSP_EXIT( IBSP_DBG_SI ); + return NULL; + } + + STAT_INC( wpusocket_num ); + /* * Preset to 1, IBSPCloseSocket will decrement it, and switch socket * will be freed once it goes to zero. */ socket_info->ref_cnt = 1; - IBSP_TRACE( IBSP_DBG_SI, ("socket_info (%p)\n", socket_info) ); + IBSP_TRACE( IBSP_DBG_SI, ("socket_info (%p), switch socket (%p)\n", + socket_info, socket_info->switch_socket) ); IBSP_EXIT( IBSP_DBG_SI ); return socket_info; } +static void +free_socket_info( + IN struct ibsp_socket_info *p_socket ) +{ + int ret, error; + + if( p_socket->switch_socket != INVALID_SOCKET ) + { + /* ref_cnt hit zero - destroy the switch socket. */ + IBSP_TRACE1( IBSP_DBG_SI, + ("socket=0x%p calling lpWPUCloseSocketHandle=0x%p\n", + p_socket, p_socket->switch_socket) ); + + ret = g_ibsp.up_call_table.lpWPUCloseSocketHandle( + p_socket->switch_socket, &error ); + if( ret == SOCKET_ERROR ) + { + IBSP_ERROR( ("WPUCloseSocketHandle failed: %d\n", error) ); + } + else + { + STAT_DEC( wpusocket_num ); + } + + p_socket->switch_socket = INVALID_SOCKET; + } + + CL_ASSERT( !p_socket->qp ); + CL_ASSERT( !p_socket->conn_item.p_map ); + + cl_spinlock_destroy( &p_socket->buf_mem_list.mutex ); + cl_spinlock_destroy( &p_socket->mutex ); + + cl_spinlock_destroy( &p_socket->send_lock ); + cl_spinlock_destroy( &p_socket->recv_lock ); + + HeapFree( g_ibsp.heap, 0, p_socket ); +} + + /* * Function: deref_sock_info * @@ -95,44 +166,11 @@ void AL_API deref_socket_info( IN struct ibsp_socket_info *p_socket ) { - int ret, error; - IBSP_ENTER( IBSP_DBG_SI ); if( !cl_atomic_dec( &p_socket->ref_cnt ) ) { - if( p_socket->switch_socket != INVALID_SOCKET ) - { - /* ref_cnt hit zero - destroy the switch socket. */ - IBSP_TRACE1( IBSP_DBG_SI, - ("socket=0x%p calling lpWPUCloseSocketHandle=0x%p\n", - p_socket, p_socket->switch_socket) ); - - ret = g_ibsp.up_call_table.lpWPUCloseSocketHandle( - p_socket->switch_socket, &error ); - if( ret == SOCKET_ERROR ) - { - IBSP_ERROR( ("WPUCloseSocketHandle failed: %d\n", error) ); - } - else - { - STAT_DEC( wpusocket_num ); - } - - p_socket->switch_socket = INVALID_SOCKET; - } - - CL_ASSERT( !p_socket->qp ); - CL_ASSERT( !p_socket->conn_item.p_map ); - - cl_spinlock_destroy( &p_socket->buf_mem_list.mutex ); - cl_spinlock_destroy( &p_socket->mutex ); - - cl_spinlock_destroy( &p_socket->send_lock ); - cl_spinlock_destroy( &p_socket->recv_lock ); - - HeapFree( g_ibsp.heap, 0, p_socket ); - + free_socket_info( p_socket ); IBSP_TRACE( IBSP_DBG_SI, ("Freed socket_info (%p)\n", p_socket) ); } IBSP_EXIT( IBSP_DBG_SI ); -- 2.41.0