⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch3u_connect_sock.c

📁 fortran并行计算包
💻 C
📖 第 1 页 / 共 4 页
字号:
/* -*- Mode: C; c-basic-offset:4 ; -*- *//* *  (C) 2001 by Argonne National Laboratory. *      See COPYRIGHT in top-level directory. */#include "mpidi_ch3_impl.h"#include "pmi.h"#include "mpidu_sock.h"#include "ch3usock.h"/* Private packet types used only within this file *//* Note that these must be smaller than the PktGeneric type and    their MPIDI_CH3_Pkt_type_t values are arbitrary (but must be   consistent) *//* FIXME - We need a little security here to avoid having a random port scan    crash the process.  Perhaps a "secret" value for each process could be    published in the key-val space and subsequently sent in the open pkt. */typedef struct{    MPIDI_CH3_Pkt_type_t type;    int pg_id_len;    int pg_rank;}MPIDI_CH3I_Pkt_sc_open_req_t;typedef struct{    MPIDI_CH3_Pkt_type_t type;    int ack;}MPIDI_CH3I_Pkt_sc_open_resp_t;typedef struct{    MPIDI_CH3_Pkt_type_t type;    int port_name_tag;}MPIDI_CH3I_Pkt_sc_conn_accept_t;#ifdef HAVE_NETDB_H#include <netdb.h>#endif#ifdef HAVE_SYS_SOCKET_H/* Include this for AF_INET */#include <sys/socket.h>#endif#ifdef HAVE_ARPA_INET_H/* Include this for inet_pton prototype */#include <arpa/inet.h>#endif/* FIXME: Describe what these routines do *//* FIXME: Clean up use of private packets (open/accept) *//* Partial description:    This file contains the routines that are used to create socket connections,   including the routines used to encode/decode the description of a connection   into/out of the "business card".   ToDo: change the "host description" to an "interface address" so that   socket connections are targeted at particularly interfaces, not   compute nodes, and that the address is in ready-to-use IP address format,    and does not require a gethostbyname lookup.  - Partially done *//* * Manage the connection information that is exported to other processes *  */#define MPIDI_CH3I_HOST_DESCRIPTION_KEY  "description"#define MPIDI_CH3I_PORT_KEY              "port"#define MPIDI_CH3I_IFNAME_KEY            "ifname"/* * Routines for establishing a listener socket on the socket set that * is used for all communication.  These should be called from the * channel init and finalize routines. */static int MPIDI_CH3I_listener_port = 0;static MPIDI_CH3I_Connection_t * MPIDI_CH3I_listener_conn = NULL;/* Required for (socket version) upcall to Connect_to_root (see FIXME) */extern MPIDU_Sock_set_t MPIDI_CH3I_sock_set;int MPIDU_CH3I_SetupListener( MPIDU_Sock_set_t sock_set ){    int mpi_errno = MPI_SUCCESS;    MPIDU_Sock_t sock;    mpi_errno = MPIDI_CH3I_Connection_alloc(&MPIDI_CH3I_listener_conn);    if (mpi_errno != MPI_SUCCESS) {	return mpi_errno;    }    MPIU_DBG_MSG(CH3_CONNECT,TYPICAL,		 "Setting listener connect state to CONN_STATE_LISTENING");    MPIDI_CH3I_listener_conn->sock	  = NULL;    MPIDI_CH3I_listener_conn->vc	  = NULL;    MPIDI_CH3I_listener_conn->state	  = CONN_STATE_LISTENING;    MPIDI_CH3I_listener_conn->send_active = NULL;    MPIDI_CH3I_listener_conn->recv_active = NULL;        mpi_errno = MPIDU_Sock_listen(sock_set, MPIDI_CH3I_listener_conn,				  &MPIDI_CH3I_listener_port, &sock);    if (mpi_errno) return mpi_errno;    MPIU_DBG_MSG_D(CH3_CONNECT,VERBOSE,"Listener port %d",		   MPIDI_CH3I_listener_port );    MPIDI_CH3I_listener_conn->sock = sock;    return mpi_errno;}int MPIDU_CH3I_ShutdownListener( void ){    int mpi_errno;    MPID_Progress_state progress_state;    MPIU_DBG_MSG(CH3_DISCONNECT,TYPICAL,"Closing listener sock (Post_close)");    mpi_errno = MPIDU_Sock_post_close(MPIDI_CH3I_listener_conn->sock);    if (mpi_errno != MPI_SUCCESS) {	return mpi_errno;    }        MPID_Progress_start(&progress_state);    while(MPIDI_CH3I_listener_conn != NULL)    {	mpi_errno = MPID_Progress_wait(&progress_state);	    }    MPID_Progress_end(&progress_state);    return mpi_errno;}/* Allocates a connection and the pg_id field for a connection only.   Does not initialize any connection fields other than pg_id.   Called by routines that create connections, used in this   file and in ch3_progress*.c in various channels.*/#undef FUNCNAME#define FUNCNAME MPIDI_CH3I_Connection_alloc#undef FCNAME#define FCNAME MPIDI_QUOTE(FUNCNAME)int MPIDI_CH3I_Connection_alloc(MPIDI_CH3I_Connection_t ** connp){    int mpi_errno = MPI_SUCCESS;    MPIDI_CH3I_Connection_t * conn = NULL;    int id_sz;    MPIU_CHKPMEM_DECL(2);    MPIDI_STATE_DECL(MPID_STATE_CONNECTION_ALLOC);    MPIDI_FUNC_ENTER(MPID_STATE_CONNECTION_ALLOC);    MPIU_CHKPMEM_MALLOC(conn,MPIDI_CH3I_Connection_t*,			sizeof(MPIDI_CH3I_Connection_t),mpi_errno,"conn");    /* FIXME: This size is unchanging, so get it only once (at most);        we might prefer for connections to simply point at the single process       group to which the remote process belong */    mpi_errno = PMI_Get_id_length_max(&id_sz);    if (mpi_errno != PMI_SUCCESS) {	MPIU_ERR_SETANDJUMP1(mpi_errno,MPI_ERR_OTHER, 			     "**pmi_get_id_length_max",			     "**pmi_get_id_length_max %d", mpi_errno);    }    MPIU_CHKPMEM_MALLOC(conn->pg_id,char*,id_sz + 1,mpi_errno,"conn->pg_id");    conn->pg_id[0] = 0;           /* Be careful about pg_id in case a later 				     error */    *connp = conn;  fn_exit:    MPIDI_FUNC_EXIT(MPID_STATE_CONNECTION_ALLOC);    return mpi_errno;  fn_fail:    MPIU_CHKPMEM_REAP();    goto fn_exit;}/* FIXME: Why does the name include "to_root"?  *//* FIXME: Describe the algorithm for the connection logic */#undef FUNCNAME#define FUNCNAME  MPIDI_CH3I_Connect_to_root_sock#undef FCNAME#define FCNAME MPIDI_QUOTE(FUNCNAME)int MPIDI_CH3I_Connect_to_root_sock(const char * port_name, 				    MPIDI_VC_t ** new_vc){    int mpi_errno = MPI_SUCCESS;    MPIDI_VC_t * vc;    MPIDI_CH3I_VC *vcch;    MPIU_CHKPMEM_DECL(1);    char host_description[MAX_HOST_DESCRIPTION_LEN];    int port, port_name_tag;    MPIDU_Sock_ifaddr_t ifaddr;    int hasIfaddr = 0;    MPIDI_CH3I_Connection_t * conn;    MPIDI_STATE_DECL(MPID_STATE_MPIDI_CH3I_CONNECT_TO_ROOT_SOCK);    MPIDI_FUNC_ENTER(MPID_STATE_MPIDI_CH3I_CONNECT_TO_ROOT_SOCK);    /* First, create a new vc (we may use this to pass to a generic       connection routine) */    MPIU_CHKPMEM_MALLOC(vc,MPIDI_VC_t *,sizeof(MPIDI_VC_t),mpi_errno,"vc");    /* FIXME - where does this vc get freed? */    *new_vc = vc;    /* FIXME: There may need to be an additional routine here, to ensure that the       channel is initialized for this pair of process groups (this process       and the remote process to which the vc will connect). */    MPIDI_VC_Init(vc, NULL, 0);    MPIU_DBG_MSG_S(CH3_CONNECT,VERBOSE,"Connect to root with portstring %s",		   port_name );    mpi_errno = MPIDU_Sock_get_conninfo_from_bc( port_name, host_description,						 sizeof(host_description),						 &port, &ifaddr, &hasIfaddr );    if (mpi_errno) {	MPIU_ERR_POP(mpi_errno);    }    mpi_errno = MPIDI_GetTagFromPort(port_name, &port_name_tag);    if (mpi_errno != MPIU_STR_SUCCESS) {	MPIU_ERR_SETANDJUMP(mpi_errno,MPI_ERR_OTHER, "**argstr_port_name_tag");    }    MPIU_DBG_MSG_D(CH3_CONNECT,VERBOSE,"port tag %d",port_name_tag);    mpi_errno = MPIDI_CH3I_Connection_alloc(&conn);    if (mpi_errno != MPI_SUCCESS) {	MPIU_ERR_POP(mpi_errno);    }    /* conn->pg_id is not used for this conection */    /* FIXME: To avoid this global (MPIDI_CH3I_sock_set) which is        used only in ch3_progress.c and ch3_progress_connect.c in the channels,       this should be a call into the channel, asking it to setup the       socket for a connection and return the connection.  That will       keep the socket set out of the general ch3 code, even if this       is the socket utility functions. */    MPIU_DBG_MSG_FMT(CH3_CONNECT,VERBOSE,(MPIU_DBG_FDEST,	  "posting connect to host %s, port %d", host_description, port ));    mpi_errno = MPIDU_Sock_post_connect(MPIDI_CH3I_sock_set, conn, 					host_description, port, &conn->sock);    if (mpi_errno == MPI_SUCCESS)    {	MPIDI_CH3I_Pkt_sc_conn_accept_t *acceptpkt = 	    (MPIDI_CH3I_Pkt_sc_conn_accept_t *)&conn->pkt.type;    	vcch = (MPIDI_CH3I_VC *)vc->channel_private;	vcch->sock = conn->sock;        vcch->conn = conn;        vcch->state = MPIDI_CH3I_VC_STATE_CONNECTING;        conn->vc = vc;	MPIU_DBG_CONNSTATECHANGE(vc,conn,CONN_STATE_CONNECT_ACCEPT);        conn->state = CONN_STATE_CONNECT_ACCEPT;        conn->send_active = NULL;        conn->recv_active = NULL;        /* place the port name tag in the pkt that will eventually be sent to 	   the other side */        acceptpkt->port_name_tag = port_name_tag;    }    /* --BEGIN ERROR HANDLING-- */    else    {	if (MPIR_ERR_GET_CLASS(mpi_errno) == MPIDU_SOCK_ERR_BAD_HOST)        {             mpi_errno = MPIR_Err_create_code(		MPI_SUCCESS, MPIR_ERR_RECOVERABLE, FCNAME, __LINE__, MPI_ERR_OTHER, "**ch3|sock|badhost",		"**ch3|sock|badhost %s %d %s", conn->pg_id, conn->vc->pg_rank, port_name);        }        else if (MPIR_ERR_GET_CLASS(mpi_errno) == MPIDU_SOCK_ERR_CONN_FAILED)        {             mpi_errno = MPIR_Err_create_code(		MPI_SUCCESS, MPIR_ERR_RECOVERABLE, FCNAME, __LINE__, MPI_ERR_OTHER, "**ch3|sock|connrefused",		"**ch3|sock|connrefused %s %d %s", conn->pg_id, conn->vc->pg_rank, port_name);        }        else        {	    MPIU_ERR_POP(mpi_errno);	}    	vcch = (MPIDI_CH3I_VC *)vc->channel_private;        vcch->state = MPIDI_CH3I_VC_STATE_FAILED;        MPIU_Free(conn);        goto fn_fail;    }    /* --END ERROR HANDLING-- */ fn_exit:    MPIDI_FUNC_EXIT(MPID_STATE_MPIDI_CH3I_CONNECT_TO_ROOT_SOCK);    return mpi_errno; fn_fail:    MPIU_CHKPMEM_REAP();    goto fn_exit;}/* ------------------------------------------------------------------------- *//* Business card management.  These routines insert or extract connection   information when using sockets from the business card *//* ------------------------------------------------------------------------- *//* FIXME: These are small routines; we may want to bring them together    into a more specific post-connection-for-sock *//* The host_description should be of length MAX_HOST_DESCRIPTION_LEN */#undef FUNCNAME#define FUNCNAME MPIDU_Sock_get_conninfo_from_bc#undef FCNAME#define FCNAME MPIDI_QUOTE(FUNCNAME)int MPIDU_Sock_get_conninfo_from_bc( const char *bc, 				     char *host_description, int maxlen,				     int *port, MPIDU_Sock_ifaddr_t *ifaddr, 				     int *hasIfaddr ){    int mpi_errno = MPI_SUCCESS;    int str_errno;#if !defined(HAVE_WINDOWS_H) && defined(HAVE_INET_PTON)    char ifname[256];#endif    MPIDI_STATE_DECL(MPID_STATE_MPIDU_SOCK_GET_CONNINFO_FROM_BC);    MPIDI_FUNC_ENTER(MPID_STATE_MPIDU_SOCK_GET_CONNINFO_FROM_BC);    str_errno = MPIU_Str_get_string_arg(bc, MPIDI_CH3I_HOST_DESCRIPTION_KEY, 				 host_description, maxlen);    if (str_errno != MPIU_STR_SUCCESS) {	/* --BEGIN ERROR HANDLING */	if (str_errno == MPIU_STR_FAIL) {	    MPIU_ERR_SETANDJUMP(mpi_errno,MPI_ERR_OTHER,"**argstr_missinghost");	}	else {	    /* MPIU_STR_TRUNCATED or MPIU_STR_NONEM */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -