| 1 |
/* |
| 2 |
* Copyright (C) by Argonne National Laboratory |
| 3 |
* See COPYRIGHT in top-level directory |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "tcp_impl.h" |
| 7 |
#ifdef HAVE_SYS_TYPES_H |
| 8 |
#include <sys/types.h> |
| 9 |
#endif |
| 10 |
#ifdef HAVE_SYS_SOCKET_H |
| 11 |
#include <sys/socket.h> |
| 12 |
#endif |
| 13 |
#ifdef HAVE_ARPA_INET_H |
| 14 |
#include <arpa/inet.h> |
| 15 |
#endif |
| 16 |
#ifdef HAVE_NETDB_H |
| 17 |
#include <netdb.h> |
| 18 |
#endif |
| 19 |
#ifdef HAVE_SIGNAL_H |
| 20 |
#include <signal.h> |
| 21 |
#endif |
| 22 |
|
| 23 |
/* |
| 24 |
=== BEGIN_MPI_T_CVAR_INFO_BLOCK === |
| 25 |
|
| 26 |
cvars: |
| 27 |
- name : MPIR_CVAR_CH3_INTERFACE_HOSTNAME |
| 28 |
category : CH3 |
| 29 |
alt-env : MPIR_CVAR_INTERFACE_HOSTNAME |
| 30 |
type : string |
| 31 |
default : NULL |
| 32 |
class : none |
| 33 |
verbosity : MPI_T_VERBOSITY_USER_BASIC |
| 34 |
scope : MPI_T_SCOPE_LOCAL |
| 35 |
description : >- |
| 36 |
If non-NULL, this cvar specifies the IP address that |
| 37 |
other processes should use when connecting to this process. |
| 38 |
This cvar is mutually exclusive with the |
| 39 |
MPIR_CVAR_CH3_NETWORK_IFACE cvar and it is an error to set them |
| 40 |
both. |
| 41 |
|
| 42 |
- name : MPIR_CVAR_CH3_PORT_RANGE |
| 43 |
category : CH3 |
| 44 |
alt-env : MPIR_CVAR_PORTRANGE, MPIR_CVAR_PORT_RANGE |
| 45 |
type : range |
| 46 |
default : "0:0" |
| 47 |
class : none |
| 48 |
verbosity : MPI_T_VERBOSITY_USER_BASIC |
| 49 |
scope : MPI_T_SCOPE_ALL_EQ |
| 50 |
description : >- |
| 51 |
The MPIR_CVAR_CH3_PORT_RANGE environment variable allows you to |
| 52 |
specify the range of TCP ports to be used by the process |
| 53 |
manager and the MPICH library. The format of this variable is |
| 54 |
<low>:<high>. To specify any available port, use 0:0. |
| 55 |
|
| 56 |
- name : MPIR_CVAR_NEMESIS_TCP_NETWORK_IFACE |
| 57 |
category : NEMESIS |
| 58 |
alt-env : MPIR_CVAR_NETWORK_IFACE |
| 59 |
type : string |
| 60 |
default : NULL |
| 61 |
class : none |
| 62 |
verbosity : MPI_T_VERBOSITY_USER_BASIC |
| 63 |
scope : MPI_T_SCOPE_ALL_EQ |
| 64 |
description : >- |
| 65 |
If non-NULL, this cvar specifies which pseudo-ethernet |
| 66 |
interface the tcp netmod should use (e.g., "eth1", "ib0"). |
| 67 |
Note, this is a Linux-specific cvar. |
| 68 |
This cvar is mutually exclusive with the |
| 69 |
MPIR_CVAR_CH3_INTERFACE_HOSTNAME cvar and it is an error to set |
| 70 |
them both. |
| 71 |
|
| 72 |
- name : MPIR_CVAR_NEMESIS_TCP_HOST_LOOKUP_RETRIES |
| 73 |
category : NEMESIS |
| 74 |
type : int |
| 75 |
default : 10 |
| 76 |
class : none |
| 77 |
verbosity : MPI_T_VERBOSITY_USER_BASIC |
| 78 |
scope : MPI_T_SCOPE_ALL_EQ |
| 79 |
description : >- |
| 80 |
This cvar controls the number of times to retry the |
| 81 |
gethostbyname() function before giving up. |
| 82 |
|
| 83 |
=== END_MPI_T_CVAR_INFO_BLOCK === |
| 84 |
*/ |
| 85 |
|
| 86 |
#define DBG_IFNAME 0 |
| 87 |
|
| 88 |
#ifdef ENABLE_CHECKPOINTING |
| 89 |
static int ckpt_restart(void); |
| 90 |
#endif |
| 91 |
|
| 92 |
MPID_nem_netmod_funcs_t MPIDI_nem_tcp_funcs = { |
| 93 |
MPID_nem_tcp_init, |
| 94 |
MPID_nem_tcp_finalize, |
| 95 |
#ifdef ENABLE_CHECKPOINTING |
| 96 |
NULL, /* ckpt_precheck */ |
| 97 |
ckpt_restart, |
| 98 |
NULL, /* ckpt_continue */ |
| 99 |
#endif |
| 100 |
MPID_nem_tcp_connpoll, |
| 101 |
MPID_nem_tcp_get_business_card, |
| 102 |
MPID_nem_tcp_connect_to_root, |
| 103 |
MPID_nem_tcp_vc_init, |
| 104 |
MPID_nem_tcp_vc_destroy, |
| 105 |
MPID_nem_tcp_vc_terminate, |
| 106 |
NULL, /* anysource iprobe */ |
| 107 |
NULL, /* anysource_improbe */ |
| 108 |
MPID_nem_tcp_get_ordering |
| 109 |
}; |
| 110 |
|
| 111 |
/* in case there are no packet types defined (e.g., they're ifdef'ed out) make sure the array is not zero length */ |
| 112 |
static MPIDI_CH3_PktHandler_Fcn *pkt_handlers[MPIDI_NEM_TCP_PKT_NUM_TYPES ? |
| 113 |
MPIDI_NEM_TCP_PKT_NUM_TYPES : 1]; |
| 114 |
|
| 115 |
MPL_dbg_class MPIDI_NEM_TCP_DBG_DET; |
| 116 |
|
| 117 |
int MPID_nem_tcp_listen(int sockfd); |
| 118 |
static int set_up_listener(void) |
| 119 |
{ |
| 120 |
int mpi_errno = MPI_SUCCESS; |
| 121 |
#ifdef HAVE_ERROR_CHECKING |
| 122 |
char strerrbuf[MPIR_STRERROR_BUF_SIZE]; |
| 123 |
#endif |
| 124 |
|
| 125 |
MPIR_FUNC_ENTER; |
| 126 |
|
| 127 |
MPID_nem_tcp_g_lstn_plfd.fd = MPID_nem_tcp_g_lstn_sc.fd = |
| 128 |
socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 129 |
MPIR_ERR_CHKANDJUMP2(MPID_nem_tcp_g_lstn_sc.fd == -1, mpi_errno, MPI_ERR_OTHER, "**sock_create", |
| 130 |
"**sock_create %s %d", MPIR_Strerror(errno, strerrbuf, MPIR_STRERROR_BUF_SIZE), |
| 131 |
errno); |
| 132 |
|
| 133 |
mpi_errno = MPID_nem_tcp_set_sockopts(MPID_nem_tcp_g_lstn_sc.fd); |
| 134 |
MPIR_ERR_CHECK(mpi_errno); |
| 135 |
|
| 136 |
MPID_nem_tcp_g_lstn_plfd.events = POLLIN; |
| 137 |
mpi_errno = MPID_nem_tcp_listen(MPID_nem_tcp_g_lstn_sc.fd); |
| 138 |
MPIR_ERR_CHKANDJUMP2(mpi_errno, mpi_errno, MPI_ERR_OTHER, "**listen", "**listen %s %d", |
| 139 |
MPIR_Strerror(errno, strerrbuf, MPIR_STRERROR_BUF_SIZE), errno); |
| 140 |
MPID_nem_tcp_g_lstn_sc.state.lstate = LISTEN_STATE_LISTENING; |
| 141 |
MPID_nem_tcp_g_lstn_sc.handler = MPID_nem_tcp_state_listening_handler; |
| 142 |
|
| 143 |
fn_exit: |
| 144 |
MPIR_FUNC_EXIT; |
| 145 |
return mpi_errno; |
| 146 |
fn_fail: |
| 147 |
|
| 148 |
goto fn_exit; |
| 149 |
} |
| 150 |
|
| 151 |
int MPID_nem_tcp_init(MPIDI_PG_t * pg_p, int pg_rank, char **bc_val_p, int *val_max_sz_p) |
| 152 |
{ |
| 153 |
int mpi_errno = MPI_SUCCESS; |
| 154 |
#ifdef HAVE_ERROR_CHECKING |
| 155 |
char strerrbuf[MPIR_STRERROR_BUF_SIZE]; |
| 156 |
#endif |
| 157 |
|
| 158 |
MPIR_FUNC_ENTER; |
| 159 |
|
| 160 |
MPID_nem_net_module_vc_dbg_print_sendq = MPID_nem_tcp_vc_dbg_print_sendq; |
| 161 |
|
| 162 |
/* first make sure that our private fields in the vc fit into the area provided */ |
| 163 |
MPIR_Assert(sizeof(MPID_nem_tcp_vc_area) <= MPIDI_NEM_VC_NETMOD_AREA_LEN); |
| 164 |
|
| 165 |
#if defined (MPL_USE_DBG_LOGGING) |
| 166 |
MPIDI_NEM_TCP_DBG_DET = MPL_dbg_class_alloc("MPIDI_NEM_TCP_DBG_DET", "nem_sock_det"); |
| 167 |
#endif /* MPL_USE_DBG_LOGGING */ |
| 168 |
|
| 169 |
/* set up listener socket */ |
| 170 |
mpi_errno = set_up_listener(); |
| 171 |
MPIR_ERR_CHECK(mpi_errno); |
| 172 |
|
| 173 |
/* create business card */ |
| 174 |
mpi_errno = MPID_nem_tcp_get_business_card(pg_rank, bc_val_p, val_max_sz_p); |
| 175 |
MPIR_ERR_CHECK(mpi_errno); |
| 176 |
|
| 177 |
mpi_errno = MPID_nem_tcp_sm_init(); |
| 178 |
MPIR_ERR_CHECK(mpi_errno); |
| 179 |
mpi_errno = MPID_nem_tcp_send_init(); |
| 180 |
MPIR_ERR_CHECK(mpi_errno); |
| 181 |
|
| 182 |
#ifdef HAVE_SIGNAL |
| 183 |
{ |
| 184 |
/* In order to be able to handle socket errors on our own, we need |
| 185 |
* to ignore SIGPIPE. This may cause problems for programs that |
| 186 |
* intend to handle SIGPIPE or count on being killed, but I expect |
| 187 |
* such programs are very rare, and I'm not sure what the best |
| 188 |
* solution would be anyway. */ |
| 189 |
/* Linux and BSD typedef to sighandler_t and sig_t respectively, which |
| 190 |
* means we can't use either. Declare directly instead. */ |
| 191 |
void (*ret) (int); |
| 192 |
|
| 193 |
ret = signal(SIGPIPE, SIG_IGN); |
| 194 |
MPIR_ERR_CHKANDJUMP1(ret == SIG_ERR, mpi_errno, MPI_ERR_OTHER, "**signal", "**signal %s", |
| 195 |
MPIR_Strerror(errno, strerrbuf, MPIR_STRERROR_BUF_SIZE)); |
| 196 |
if (ret != SIG_DFL && ret != SIG_IGN) { |
| 197 |
/* The app has set its own signal handler. Replace the previous handler. */ |
| 198 |
ret = signal(SIGPIPE, ret); |
| 199 |
MPIR_ERR_CHKANDJUMP1(ret == SIG_ERR, mpi_errno, MPI_ERR_OTHER, "**signal", |
| 200 |
"**signal %s", MPIR_Strerror(errno, strerrbuf, MPIR_STRERROR_BUF_SIZE)); |
| 201 |
} |
| 202 |
} |
| 203 |
#endif |
| 204 |
|
| 205 |
fn_exit: |
| 206 |
MPIR_FUNC_EXIT; |
| 207 |
/* fprintf(stdout, __func__ " Exit\n"); fflush(stdout); */ |
| 208 |
return mpi_errno; |
| 209 |
fn_fail: |
| 210 |
/* fprintf(stdout, "failure. mpi_errno = %d\n", mpi_errno); */ |
| 211 |
goto fn_exit; |
| 212 |
} |
| 213 |
|
| 214 |
#ifdef ENABLE_CHECKPOINTING |
| 215 |
|
| 216 |
static int ckpt_restart(void) |
| 217 |
{ |
| 218 |
int mpi_errno = MPI_SUCCESS; |
| 219 |
char *publish_bc_orig = NULL; |
| 220 |
char *bc_val = NULL; |
| 221 |
int val_max_sz; |
| 222 |
int i; |
| 223 |
|
| 224 |
MPIR_FUNC_ENTER; |
| 225 |
|
| 226 |
/* First, clean up. We didn't shut anything down before the |
| 227 |
* checkpoint, so we need to go close and free any resources */ |
| 228 |
mpi_errno = MPID_nem_tcp_ckpt_cleanup(); |
| 229 |
MPIR_ERR_CHECK(mpi_errno); |
| 230 |
mpi_errno = MPID_nem_tcp_send_finalize(); |
| 231 |
MPIR_ERR_CHECK(mpi_errno); |
| 232 |
mpi_errno = MPID_nem_tcp_sm_finalize(); |
| 233 |
MPIR_ERR_CHECK(mpi_errno); |
| 234 |
|
| 235 |
/* Initialize the new business card */ |
| 236 |
mpi_errno = MPIDI_CH3I_BCInit(&bc_val, &val_max_sz); |
| 237 |
MPIR_ERR_CHECK(mpi_errno); |
| 238 |
publish_bc_orig = bc_val; |
| 239 |
|
| 240 |
/* Now we can restart */ |
| 241 |
mpi_errno = |
| 242 |
MPID_nem_tcp_init(MPIDI_Process.my_pg, MPIDI_Process.my_pg_rank, &bc_val, &val_max_sz); |
| 243 |
MPIR_ERR_CHECK(mpi_errno); |
| 244 |
|
| 245 |
/* publish business card */ |
| 246 |
mpi_errno = MPIDI_PG_SetConnInfo(MPIDI_Process.my_pg_rank, (const char *) publish_bc_orig); |
| 247 |
MPIR_ERR_CHECK(mpi_errno); |
| 248 |
MPL_free(publish_bc_orig); |
| 249 |
|
| 250 |
for (i = 0; i < MPIDI_Process.my_pg->size; ++i) { |
| 251 |
MPIDI_VC_t *vc; |
| 252 |
MPIDI_CH3I_VC *vc_ch; |
| 253 |
if (i == MPIDI_Process.my_pg_rank) |
| 254 |
continue; |
| 255 |
MPIDI_PG_Get_vc(MPIDI_Process.my_pg, i, &vc); |
| 256 |
vc_ch = &vc->ch; |
| 257 |
if (!vc_ch->is_local) { |
| 258 |
mpi_errno = vc_ch->ckpt_restart_vc(vc); |
| 259 |
MPIR_ERR_CHECK(mpi_errno); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
fn_exit: |
| 265 |
MPIR_FUNC_EXIT; |
| 266 |
return mpi_errno; |
| 267 |
fn_fail: |
| 268 |
|
| 269 |
goto fn_exit; |
| 270 |
} |
| 271 |
#endif |
| 272 |
|
| 273 |
/* |
| 274 |
* Get a description of the network interface to use for socket communication |
| 275 |
* |
| 276 |
* Here are the steps. This order of checks is used to provide the |
| 277 |
* user control over the choice of interface and to avoid, where possible, |
| 278 |
* the use of non-scalable services, such as centeralized name servers. |
| 279 |
* |
| 280 |
* MPICH_INTERFACE_HOSTNAME |
| 281 |
* MPICH_INTERFACE_HOSTNAME_R%d |
| 282 |
* a single (non-localhost) available IP address, if possible |
| 283 |
* gethostbyname(gethostname()) |
| 284 |
* |
| 285 |
* We return the following items: |
| 286 |
* |
| 287 |
* ifname - hostname of the interface. This may or may not be the same |
| 288 |
* as the name returned by gethostname (in Unix) |
| 289 |
* ifaddr - This structure includes the interface IP address (as bytes), |
| 290 |
* and the type (e.g., AF_INET or AF_INET6). Only |
| 291 |
* ipv4 (AF_INET) is used so far. |
| 292 |
*/ |
| 293 |
|
| 294 |
static int GetSockInterfaceAddr(int myRank, char *ifname, int maxIfname, MPL_sockaddr_t * p_addr) |
| 295 |
{ |
| 296 |
const char *ifname_string; |
| 297 |
int mpi_errno = MPI_SUCCESS; |
| 298 |
int ifaddrFound = 0; |
| 299 |
|
| 300 |
MPIR_Assert(maxIfname); |
| 301 |
ifname[0] = '\0'; |
| 302 |
|
| 303 |
MPIR_ERR_CHKANDJUMP(MPIR_CVAR_CH3_INTERFACE_HOSTNAME && |
| 304 |
MPIR_CVAR_NEMESIS_TCP_NETWORK_IFACE, mpi_errno, MPI_ERR_OTHER, |
| 305 |
"**ifname_and_hostname"); |
| 306 |
|
| 307 |
/* Check if user specified ethernet interface name, e.g., ib0, eth1 */ |
| 308 |
if (MPIR_CVAR_NEMESIS_TCP_NETWORK_IFACE) { |
| 309 |
char s[100]; |
| 310 |
int len; |
| 311 |
int ret = MPL_get_sockaddr_iface(MPIR_CVAR_NEMESIS_TCP_NETWORK_IFACE, p_addr); |
| 312 |
MPIR_ERR_CHKANDJUMP1(ret != 0, mpi_errno, MPI_ERR_OTHER, "**iface_notfound", |
| 313 |
"**iface_notfound %s", MPIR_CVAR_NEMESIS_TCP_NETWORK_IFACE); |
| 314 |
|
| 315 |
MPL_sockaddr_to_str(p_addr, s, 100); |
| 316 |
MPL_DBG_MSG_FMT(MPIDI_CH3_DBG_CONNECT, VERBOSE, (MPL_DBG_FDEST, "ifaddrFound: %s", s)); |
| 317 |
|
| 318 |
/* In this case, ifname is only used for debugging purposes */ |
| 319 |
mpi_errno = MPID_Get_processor_name(ifname, maxIfname, &len); |
| 320 |
MPIR_ERR_CHECK(mpi_errno); |
| 321 |
goto fn_exit; |
| 322 |
} |
| 323 |
|
| 324 |
/* Check for a host name supplied through an environment variable */ |
| 325 |
ifname_string = MPIR_CVAR_CH3_INTERFACE_HOSTNAME; |
| 326 |
if (!ifname_string) { |
| 327 |
ifname_string = MPIR_pmi_hostname(); |
| 328 |
} |
| 329 |
if (!ifname_string) { |
| 330 |
/* See if there is a per-process name for the interfaces (e.g., |
| 331 |
* the process manager only delivers the same values for the |
| 332 |
* environment to each process. There's no way to do this with |
| 333 |
* the param interface, so we need to use getenv() here. */ |
| 334 |
char namebuf[1024]; |
| 335 |
snprintf(namebuf, sizeof(namebuf), "MPICH_INTERFACE_HOSTNAME_R%d", myRank); |
| 336 |
ifname_string = getenv(namebuf); |
| 337 |
|
| 338 |
if (DBG_IFNAME && ifname_string) { |
| 339 |
fprintf(stdout, "Found interface name %s from %s\n", ifname_string, namebuf); |
| 340 |
fflush(stdout); |
| 341 |
} |
| 342 |
} else if (DBG_IFNAME) { |
| 343 |
fprintf(stdout, "Found interface name %s from MPICH_INTERFACE_HOSTNAME\n", ifname_string); |
| 344 |
fflush(stdout); |
| 345 |
} |
| 346 |
|
| 347 |
if (!ifname_string) { |
| 348 |
int len; |
| 349 |
|
| 350 |
/* User did not specify a hostname. Look it up. */ |
| 351 |
mpi_errno = MPID_Get_processor_name(ifname, maxIfname, &len); |
| 352 |
MPIR_ERR_CHECK(mpi_errno); |
| 353 |
|
| 354 |
ifname_string = ifname; |
| 355 |
|
| 356 |
/* If we didn't find a specific name, then try to get an IP address |
| 357 |
* directly from the available interfaces, if that is supported on |
| 358 |
* this platform. Otherwise, we'll drop into the next step that uses |
| 359 |
* the ifname */ |
| 360 |
int ret = MPL_get_sockaddr_iface(NULL, p_addr); |
| 361 |
MPIR_ERR_CHKANDJUMP1(ret != 0, mpi_errno, MPI_ERR_OTHER, "**iface_notfound", |
| 362 |
"**iface_notfound %s", NULL); |
| 363 |
ifaddrFound = 1; |
| 364 |
} else { |
| 365 |
/* Copy this name into the output name */ |
| 366 |
MPL_strncpy(ifname, ifname_string, maxIfname); |
| 367 |
} |
| 368 |
|
| 369 |
/* If we don't have an IP address, try to get it from the name */ |
| 370 |
if (!ifaddrFound) { |
| 371 |
int ret = MPL_get_sockaddr(ifname_string, p_addr); |
| 372 |
MPIR_ERR_CHKANDJUMP2(ret != 0, mpi_errno, MPI_ERR_OTHER, "**gethostbyname", |
| 373 |
"**gethostbyname %s %d", ifname_string, h_errno); |
| 374 |
} |
| 375 |
|
| 376 |
fn_exit: |
| 377 |
return mpi_errno; |
| 378 |
fn_fail: |
| 379 |
goto fn_exit; |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
int MPID_nem_tcp_get_business_card(int my_rank, char **bc_val_p, int *val_max_sz_p) |
| 384 |
{ |
| 385 |
int mpi_errno = MPI_SUCCESS; |
| 386 |
int str_errno = MPL_SUCCESS; |
| 387 |
MPL_sockaddr_t addr; |
| 388 |
char ifname[MAX_HOST_DESCRIPTION_LEN]; |
| 389 |
int ret; |
| 390 |
MPL_sockaddr_t sock_id; |
| 391 |
socklen_t len; |
| 392 |
#ifdef HAVE_ERROR_CHECKING |
| 393 |
char strerrbuf[MPIR_STRERROR_BUF_SIZE]; |
| 394 |
#endif |
| 395 |
|
| 396 |
MPIR_FUNC_ENTER; |
| 397 |
|
| 398 |
mpi_errno = GetSockInterfaceAddr(my_rank, ifname, sizeof(ifname), &addr); |
| 399 |
if (mpi_errno) |
| 400 |
MPIR_ERR_POP(mpi_errno); |
| 401 |
|
| 402 |
|
| 403 |
str_errno = |
| 404 |
MPL_str_add_string_arg(bc_val_p, val_max_sz_p, MPIDI_CH3I_HOST_DESCRIPTION_KEY, ifname); |
| 405 |
if (str_errno) { |
| 406 |
MPIR_ERR_CHKANDJUMP(str_errno == MPL_ERR_STR_NOMEM, mpi_errno, MPI_ERR_OTHER, "**buscard_len"); |
| 407 |
MPIR_ERR_SETANDJUMP(mpi_errno, MPI_ERR_OTHER, "**buscard"); |
| 408 |
} |
| 409 |
|
| 410 |
len = sizeof(sock_id); |
| 411 |
ret = getsockname(MPID_nem_tcp_g_lstn_sc.fd, (struct sockaddr *) &sock_id, &len); |
| 412 |
MPIR_ERR_CHKANDJUMP1(ret == -1, mpi_errno, MPI_ERR_OTHER, "**getsockname", "**getsockname %s", |
| 413 |
MPIR_Strerror(errno, strerrbuf, MPIR_STRERROR_BUF_SIZE)); |
| 414 |
|
| 415 |
str_errno = |
| 416 |
MPL_str_add_int_arg(bc_val_p, val_max_sz_p, MPIDI_CH3I_PORT_KEY, |
| 417 |
MPL_sockaddr_port(&sock_id)); |
| 418 |
if (str_errno) { |
| 419 |
MPIR_ERR_CHKANDJUMP(str_errno == MPL_ERR_STR_NOMEM, mpi_errno, MPI_ERR_OTHER, "**buscard_len"); |
| 420 |
MPIR_ERR_SETANDJUMP(mpi_errno, MPI_ERR_OTHER, "**buscard"); |
| 421 |
} |
| 422 |
|
| 423 |
if (addr.ss_family == AF_INET) { |
| 424 |
MPL_sockaddr_to_str(&addr, ifname, MAX_HOST_DESCRIPTION_LEN); |
| 425 |
MPL_DBG_MSG_S(MPIDI_CH3_DBG_CONNECT, VERBOSE, "ifname = %s", ifname); |
| 426 |
str_errno = MPL_str_add_string_arg(bc_val_p, val_max_sz_p, MPIDI_CH3I_IFNAME_KEY, ifname); |
| 427 |
if (str_errno) { |
| 428 |
MPIR_ERR_CHKANDJUMP(str_errno == MPL_ERR_STR_NOMEM, mpi_errno, MPI_ERR_OTHER, |
| 429 |
"**buscard_len"); |
| 430 |
MPIR_ERR_SETANDJUMP(mpi_errno, MPI_ERR_OTHER, "**buscard"); |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
/* printf("MPID_nem_tcp_get_business_card. port=%d\n", sock_id.sin_port); */ |
| 436 |
|
| 437 |
fn_exit: |
| 438 |
/* fprintf(stdout, "MPID_nem_tcp_get_business_card Exit, mpi_errno=%d\n", mpi_errno); fflush(stdout); */ |
| 439 |
MPIR_FUNC_EXIT; |
| 440 |
return mpi_errno; |
| 441 |
fn_fail: |
| 442 |
goto fn_exit; |
| 443 |
} |
| 444 |
|
| 445 |
int MPID_nem_tcp_connect_to_root(const char *business_card, MPIDI_VC_t * new_vc) |
| 446 |
{ |
| 447 |
int mpi_errno = MPI_SUCCESS; |
| 448 |
struct in_addr addr; |
| 449 |
MPID_nem_tcp_vc_area *vc_tcp = VC_TCP(new_vc); |
| 450 |
|
| 451 |
MPIR_FUNC_ENTER; |
| 452 |
|
| 453 |
/* vc is already allocated before reaching this point */ |
| 454 |
|
| 455 |
mpi_errno = MPID_nem_tcp_get_addr_port_from_bc(business_card, &addr, &vc_tcp->sock_id.sin_port); |
| 456 |
vc_tcp->sock_id.sin_addr.s_addr = addr.s_addr; |
| 457 |
MPIR_ERR_CHECK(mpi_errno); |
| 458 |
|
| 459 |
mpi_errno = MPIDI_GetTagFromPort(business_card, &new_vc->port_name_tag); |
| 460 |
MPIR_ERR_CHECK(mpi_errno); |
| 461 |
mpi_errno = MPID_nem_tcp_connect(new_vc); |
| 462 |
MPIR_ERR_CHECK(mpi_errno); |
| 463 |
|
| 464 |
fn_exit: |
| 465 |
MPIR_FUNC_EXIT; |
| 466 |
return mpi_errno; |
| 467 |
|
| 468 |
fn_fail: |
| 469 |
goto fn_exit; |
| 470 |
} |
| 471 |
|
| 472 |
int MPID_nem_tcp_vc_init(MPIDI_VC_t * vc) |
| 473 |
{ |
| 474 |
int mpi_errno = MPI_SUCCESS; |
| 475 |
MPIDI_CH3I_VC *vc_ch = &vc->ch; |
| 476 |
MPID_nem_tcp_vc_area *vc_tcp = VC_TCP(vc); |
| 477 |
|
| 478 |
MPIR_FUNC_ENTER; |
| 479 |
|
| 480 |
vc_tcp->state = MPID_NEM_TCP_VC_STATE_DISCONNECTED; |
| 481 |
|
| 482 |
vc->sendNoncontig_fn = MPID_nem_tcp_SendNoncontig; |
| 483 |
vc_ch->iStartContigMsg = MPID_nem_tcp_iStartContigMsg; |
| 484 |
vc_ch->iSendContig = MPID_nem_tcp_iSendContig; |
| 485 |
vc_ch->iSendIov = MPID_nem_tcp_iSendIov; |
| 486 |
#ifdef ENABLE_CHECKPOINTING |
| 487 |
vc_ch->ckpt_pause_send_vc = MPID_nem_tcp_ckpt_pause_send_vc; |
| 488 |
vc_ch->ckpt_continue_vc = MPID_nem_tcp_ckpt_continue_vc; |
| 489 |
vc_ch->ckpt_restart_vc = MPID_nem_tcp_ckpt_restart_vc; |
| 490 |
|
| 491 |
pkt_handlers[MPIDI_NEM_TCP_PKT_UNPAUSE] = MPID_nem_tcp_pkt_unpause_handler; |
| 492 |
#endif |
| 493 |
|
| 494 |
vc_ch->pkt_handler = pkt_handlers; |
| 495 |
vc_ch->num_pkt_handlers = MPIDI_NEM_TCP_PKT_NUM_TYPES; |
| 496 |
|
| 497 |
memset(&vc_tcp->sock_id, 0, sizeof(vc_tcp->sock_id)); |
| 498 |
vc_tcp->sock_id.sin_family = AF_INET; |
| 499 |
|
| 500 |
vc_ch->next = NULL; |
| 501 |
vc_ch->prev = NULL; |
| 502 |
|
| 503 |
ASSIGN_SC_TO_VC(vc_tcp, NULL); |
| 504 |
vc_tcp->send_queue.head = vc_tcp->send_queue.tail = NULL; |
| 505 |
|
| 506 |
vc_tcp->send_paused = FALSE; |
| 507 |
vc_tcp->paused_send_queue.head = vc_tcp->paused_send_queue.tail = NULL; |
| 508 |
|
| 509 |
vc_tcp->sc_ref_count = 0; |
| 510 |
|
| 511 |
vc_tcp->connect_retry_count = 0; |
| 512 |
|
| 513 |
MPIR_FUNC_EXIT; |
| 514 |
return mpi_errno; |
| 515 |
} |
| 516 |
|
| 517 |
int MPID_nem_tcp_vc_destroy(MPIDI_VC_t * vc) |
| 518 |
{ |
| 519 |
int mpi_errno = MPI_SUCCESS; |
| 520 |
|
| 521 |
/* currently do nothing */ |
| 522 |
|
| 523 |
return mpi_errno; |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
/* |
| 528 |
FIXME: this is the same function as in socksm.c |
| 529 |
This should be removed and use only one function eventually. |
| 530 |
*/ |
| 531 |
|
| 532 |
int MPID_nem_tcp_get_addr_port_from_bc(const char *business_card, struct in_addr *addr, |
| 533 |
in_port_t * port) |
| 534 |
{ |
| 535 |
int mpi_errno = MPI_SUCCESS; |
| 536 |
int ret; |
| 537 |
int port_int; |
| 538 |
/*char desc_str[256]; */ |
| 539 |
char ifname[256]; |
| 540 |
|
| 541 |
MPIR_FUNC_ENTER; |
| 542 |
|
| 543 |
/* fprintf(stdout, __func__ " Enter\n"); fflush(stdout); */ |
| 544 |
/* desc_str is only used for debugging |
| 545 |
* ret = MPL_str_get_string_arg (business_card, MPIDI_CH3I_HOST_DESCRIPTION_KEY, desc_str, sizeof(desc_str)); |
| 546 |
* MPIR_ERR_CHKANDJUMP (ret != MPL_SUCCESS, mpi_errno, MPI_ERR_OTHER, "**argstr_missinghost"); |
| 547 |
*/ |
| 548 |
|
| 549 |
/* sizeof(in_port_t) != sizeof(int) on most platforms, so we need to use |
| 550 |
* port_int as the arg to MPL_str_get_int_arg. */ |
| 551 |
ret = MPL_str_get_int_arg(business_card, MPIDI_CH3I_PORT_KEY, &port_int); |
| 552 |
/* MPL_ERR_STR_FAIL is not a valid MPI error code so we store the result in ret |
| 553 |
* instead of mpi_errno. */ |
| 554 |
MPIR_ERR_CHKANDJUMP(ret != MPL_SUCCESS, mpi_errno, MPI_ERR_OTHER, "**argstr_missingport"); |
| 555 |
MPIR_Assert((port_int >> (8 * sizeof(*port))) == 0); /* ensure port_int isn't too large for *port */ |
| 556 |
*port = htons((in_port_t) port_int); |
| 557 |
|
| 558 |
ret = MPL_str_get_string_arg(business_card, MPIDI_CH3I_IFNAME_KEY, ifname, sizeof(ifname)); |
| 559 |
MPIR_ERR_CHKANDJUMP(ret != MPL_SUCCESS, mpi_errno, MPI_ERR_OTHER, "**argstr_missingifname"); |
| 560 |
|
| 561 |
ret = inet_pton(AF_INET, (const char *) ifname, addr); |
| 562 |
MPIR_ERR_CHKANDJUMP(ret == 0, mpi_errno, MPI_ERR_OTHER, "**ifnameinvalid"); |
| 563 |
MPIR_ERR_CHKANDJUMP(ret < 0, mpi_errno, MPI_ERR_OTHER, "**afinetinvalid"); |
| 564 |
|
| 565 |
fn_exit: |
| 566 |
/* fprintf(stdout, __func__ " Exit\n"); fflush(stdout); */ |
| 567 |
MPIR_FUNC_EXIT; |
| 568 |
return mpi_errno; |
| 569 |
fn_fail: |
| 570 |
/* fprintf(stdout, "failure. mpi_errno = %d\n", mpi_errno); */ |
| 571 |
MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE, |
| 572 |
(MPL_DBG_FDEST, "failure. mpi_errno = %d", mpi_errno)); |
| 573 |
goto fn_exit; |
| 574 |
} |
| 575 |
|
| 576 |
/* MPID_nem_tcp_listen -- if MPICH_PORT_RANGE is set, this |
| 577 |
binds the socket to an available port number in the range. |
| 578 |
Otherwise, it binds it to any addr and any port */ |
| 579 |
int MPID_nem_tcp_listen(int sockfd) |
| 580 |
{ |
| 581 |
int mpi_errno = MPI_SUCCESS; |
| 582 |
int ret; |
| 583 |
unsigned short port; |
| 584 |
#ifdef HAVE_ERROR_CHECKING |
| 585 |
char strerrbuf[MPIR_STRERROR_BUF_SIZE]; |
| 586 |
#endif |
| 587 |
|
| 588 |
MPIR_FUNC_ENTER; |
| 589 |
|
| 590 |
MPIR_ERR_CHKANDJUMP(MPIR_CVAR_CH3_PORT_RANGE.low < 0 || |
| 591 |
MPIR_CVAR_CH3_PORT_RANGE.low > MPIR_CVAR_CH3_PORT_RANGE.high, mpi_errno, |
| 592 |
MPI_ERR_OTHER, "**badportrange"); |
| 593 |
|
| 594 |
/* default MPICH_PORT_RANGE is {0,0} so bind will use any available port */ |
| 595 |
ret = 0; |
| 596 |
if (MPIR_CVAR_CH3_PORT_RANGE.low == 0) { |
| 597 |
ret = MPL_listen_anyport(sockfd, &port); |
| 598 |
} else { |
| 599 |
ret = |
| 600 |
MPL_listen_portrange(sockfd, &port, MPIR_CVAR_CH3_PORT_RANGE.low, |
| 601 |
MPIR_CVAR_CH3_PORT_RANGE.high); |
| 602 |
} |
| 603 |
if (ret == -2) { |
| 604 |
/* check if an available port was found */ |
| 605 |
MPIR_ERR_CHKANDJUMP3(1, mpi_errno, MPI_ERR_OTHER, "**sock|poll|bind", |
| 606 |
"**sock|poll|bind %d %d %s", port - 1, errno, |
| 607 |
MPIR_Strerror(errno, strerrbuf, MPIR_STRERROR_BUF_SIZE)); |
| 608 |
} else if (ret) { |
| 609 |
/* check for real error */ |
| 610 |
MPIR_ERR_CHKANDJUMP3(errno != EADDRINUSE && |
| 611 |
errno != EADDRNOTAVAIL, mpi_errno, MPI_ERR_OTHER, "**sock|poll|bind", |
| 612 |
"**sock|poll|bind %d %d %s", port, errno, |
| 613 |
MPIR_Strerror(errno, strerrbuf, MPIR_STRERROR_BUF_SIZE)); |
| 614 |
} |
| 615 |
|
| 616 |
fn_exit: |
| 617 |
/* if (ret == 0) */ |
| 618 |
/* fprintf(stdout, "sockfd=%d port=%d bound\n", sockfd, port); */ |
| 619 |
/* fprintf(stdout, __func__ " Exit\n"); fflush(stdout); */ |
| 620 |
MPIR_FUNC_EXIT; |
| 621 |
return mpi_errno; |
| 622 |
fn_fail: |
| 623 |
/* fprintf(stdout, "failure. mpi_errno = %d\n", mpi_errno); */ |
| 624 |
MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE, |
| 625 |
(MPL_DBG_FDEST, "failure. mpi_errno = %d", mpi_errno)); |
| 626 |
goto fn_exit; |
| 627 |
} |
| 628 |
|
| 629 |
int MPID_nem_tcp_vc_terminate(MPIDI_VC_t * vc) |
| 630 |
{ |
| 631 |
int mpi_errno = MPI_SUCCESS; |
| 632 |
int req_errno = MPI_SUCCESS; |
| 633 |
|
| 634 |
MPIR_FUNC_ENTER; |
| 635 |
|
| 636 |
if (vc->state != MPIDI_VC_STATE_CLOSED) { |
| 637 |
/* VC is terminated as a result of a fault. Complete |
| 638 |
* outstanding sends with an error and terminate |
| 639 |
* connection immediately. */ |
| 640 |
MPIR_ERR_SET1(req_errno, MPIX_ERR_PROC_FAILED, "**comm_fail", "**comm_fail %d", |
| 641 |
vc->pg_rank); |
| 642 |
mpi_errno = MPID_nem_tcp_error_out_send_queue(vc, req_errno); |
| 643 |
MPIR_ERR_CHECK(mpi_errno); |
| 644 |
mpi_errno = MPID_nem_tcp_vc_terminated(vc); |
| 645 |
MPIR_ERR_CHECK(mpi_errno); |
| 646 |
} else { |
| 647 |
MPID_nem_tcp_vc_area *vc_tcp = VC_TCP(vc); |
| 648 |
/* VC is terminated as a result of the close protocol. |
| 649 |
* Wait for sends to complete, then terminate. */ |
| 650 |
|
| 651 |
if (MPIDI_CH3I_Sendq_empty(vc_tcp->send_queue)) { |
| 652 |
/* The sendq is empty, so we can immediately terminate |
| 653 |
* the connection. */ |
| 654 |
mpi_errno = MPID_nem_tcp_vc_terminated(vc); |
| 655 |
MPIR_ERR_CHECK(mpi_errno); |
| 656 |
} |
| 657 |
/* else: just return. We'll call vc_terminated() from the |
| 658 |
* commrdy_handler once the sendq is empty. */ |
| 659 |
} |
| 660 |
|
| 661 |
fn_exit: |
| 662 |
MPIR_FUNC_EXIT; |
| 663 |
return mpi_errno; |
| 664 |
fn_fail: |
| 665 |
goto fn_exit; |
| 666 |
} |
| 667 |
|
| 668 |
|
| 669 |
int MPID_nem_tcp_vc_terminated(MPIDI_VC_t * vc) |
| 670 |
{ |
| 671 |
/* This is called when the VC is to be terminated once all queued |
| 672 |
* sends have been sent. */ |
| 673 |
int mpi_errno = MPI_SUCCESS; |
| 674 |
MPIR_FUNC_ENTER; |
| 675 |
|
| 676 |
mpi_errno = MPID_nem_tcp_cleanup(vc); |
| 677 |
MPIR_ERR_CHECK(mpi_errno); |
| 678 |
|
| 679 |
mpi_errno = MPIDI_CH3U_Handle_connection(vc, MPIDI_VC_EVENT_TERMINATED); |
| 680 |
MPIR_ERR_CHECK(mpi_errno); |
| 681 |
|
| 682 |
fn_exit: |
| 683 |
MPIR_FUNC_EXIT; |
| 684 |
return mpi_errno; |
| 685 |
fn_fail: |
| 686 |
MPL_DBG_MSG_FMT(MPIDI_NEM_TCP_DBG_DET, VERBOSE, |
| 687 |
(MPL_DBG_FDEST, "failure. mpi_errno = %d", mpi_errno)); |
| 688 |
goto fn_exit; |
| 689 |
} |
| 690 |
|
| 691 |
int MPID_nem_tcp_get_ordering(int *ordering) |
| 692 |
{ |
| 693 |
(*ordering) = 1; |
| 694 |
return MPI_SUCCESS; |
| 695 |
} |