| 1 |
/* |
| 2 |
* Copyright (C) by Argonne National Laboratory |
| 3 |
* See COPYRIGHT in top-level directory |
| 4 |
*/ |
| 5 |
|
| 6 |
#ifndef SOCKSM_H_INCLUDED |
| 7 |
#define SOCKSM_H_INCLUDED |
| 8 |
|
| 9 |
#include <sys/poll.h> |
| 10 |
#include <stddef.h> |
| 11 |
|
| 12 |
enum SOCK_CONSTS { /* more type safe than #define's */ |
| 13 |
LISTENQLEN = 10, |
| 14 |
POLL_CONN_TIME = 2 |
| 15 |
}; |
| 16 |
|
| 17 |
typedef struct sockaddr_in SA_IN; |
| 18 |
typedef struct sockaddr SA; |
| 19 |
|
| 20 |
enum CONSTS { |
| 21 |
CONN_PLFD_TBL_INIT_SIZE = 20, |
| 22 |
CONN_PLFD_TBL_GROW_SIZE = 10, |
| 23 |
CONN_INVALID_FD = -1, |
| 24 |
CONN_INVALID_RANK = -1, |
| 25 |
NEGOMSG_DATA_LEN = 4, /* Length of data during negotiation message exchanges. */ |
| 26 |
SLEEP_INTERVAL = 1500, |
| 27 |
PROGSM_TIMES = 20 |
| 28 |
}; |
| 29 |
|
| 30 |
typedef enum { |
| 31 |
MPID_NEM_TCP_SOCK_ERROR_EOF, /* either a socket error or EOF received from peer */ |
| 32 |
MPID_NEM_TCP_SOCK_CONNECTED, |
| 33 |
MPID_NEM_TCP_SOCK_NOEVENT /* No poll event on socket */ |
| 34 |
} MPID_NEM_TCP_SOCK_STATUS_t; |
| 35 |
|
| 36 |
#define M_(x) x |
| 37 |
#define CONN_TYPE_ M_(TYPE_CONN), M_(TYPE_ACPT) |
| 38 |
|
| 39 |
/* Note : '_' denotes sub-states */ |
| 40 |
/* For example, CSO_DISCCONNECTING_DISCREQSENT, CSO_DISCONNECTING_DISCRSPRCVD are sub-states */ |
| 41 |
/* of CSO_DISCONNECTING */ |
| 42 |
/* LSO - Listening SOcket states */ |
| 43 |
#define LISTEN_STATE_ \ |
| 44 |
M_(LISTEN_STATE_CLOSED), \ |
| 45 |
M_(LISTEN_STATE_LISTENING) |
| 46 |
|
| 47 |
/* |
| 48 |
CONN_STATE - Connection states of socket |
| 49 |
TC = Type connected (state of a socket that was issued a connect on) |
| 50 |
TA = Type Accepted (state of a socket returned by accept) |
| 51 |
TS = Type Shared (state of either TC or TA) |
| 52 |
|
| 53 |
C - Connection sub-states |
| 54 |
D - Disconnection sub-states |
| 55 |
*/ |
| 56 |
|
| 57 |
#define CONN_STATE_ \ |
| 58 |
M_(CONN_STATE_UNDEFINED), \ |
| 59 |
M_(CONN_STATE_TS_CLOSED), \ |
| 60 |
M_(CONN_STATE_TC_C_CNTING), \ |
| 61 |
M_(CONN_STATE_TC_C_CNTD), \ |
| 62 |
M_(CONN_STATE_TC_C_RANKSENT), \ |
| 63 |
M_(CONN_STATE_TC_C_TMPVCSENT), \ |
| 64 |
M_(CONN_STATE_TA_C_CNTD), \ |
| 65 |
M_(CONN_STATE_TA_C_RANKRCVD), \ |
| 66 |
M_(CONN_STATE_TA_C_TMPVCRCVD), \ |
| 67 |
M_(CONN_STATE_TS_COMMRDY) |
| 68 |
|
| 69 |
/* REQ - Request, RSP - Response */ |
| 70 |
|
| 71 |
typedef enum CONN_TYPE { CONN_TYPE_, CONN_TYPE_SIZE } Conn_type_t; |
| 72 |
typedef enum MPID_nem_tcp_Listen_State { LISTEN_STATE_INVALID = -1, LISTEN_STATE_, LISTEN_STATE_SIZE |
| 73 |
} MPID_nem_tcp_Listen_State_t; |
| 74 |
|
| 75 |
typedef enum MPID_nem_tcp_Conn_State { CONN_STATE_INVALID = -1, CONN_STATE_, CONN_STATE_SIZE } |
| 76 |
MPID_nem_tcp_Conn_State_t; |
| 77 |
|
| 78 |
/* |
| 79 |
Note: event numbering starts from 1, as 0 is assumed to be the state of all-events cleared |
| 80 |
*/ |
| 81 |
typedef enum sockconn_event { EVENT_CONNECT = 1, EVENT_DISCONNECT } sockconn_event_t; |
| 82 |
|
| 83 |
#undef M_ |
| 84 |
#define M_(x) #x |
| 85 |
|
| 86 |
#if defined(SOCKSM_H_DEFGLOBALS_) |
| 87 |
const char *const CONN_TYPE_STR[CONN_TYPE_SIZE] = { CONN_TYPE_ }; |
| 88 |
const char *const LISTEN_STATE_STR[LISTEN_STATE_SIZE] = { LISTEN_STATE_ }; |
| 89 |
const char *const CONN_STATE_STR[CONN_STATE_SIZE] = { CONN_STATE_ }; |
| 90 |
#elif defined(SOCKSM_H_EXTERNS_) |
| 91 |
extern const char *const CONN_TYPE_STR[]; |
| 92 |
extern const char *const LISTEN_STATE_STR[]; |
| 93 |
extern const char *const CONN_STATE_STR[]; |
| 94 |
#endif |
| 95 |
#undef M_ |
| 96 |
|
| 97 |
#ifdef MPL_USE_DBG_LOGGING |
| 98 |
#define CONN_STATE_TO_STRING(_cstate) \ |
| 99 |
(((_cstate) >= CONN_STATE_TS_CLOSED && (_cstate) < CONN_STATE_SIZE) ? CONN_STATE_STR[_cstate] : "out_of_range") |
| 100 |
|
| 101 |
#define DBG_CHANGE_STATE(_sc, _cstate) do { \ |
| 102 |
const char *state_str = NULL; \ |
| 103 |
const char *old_state_str = NULL; \ |
| 104 |
if (MPL_DBG_SELECTED(MPIDI_NEM_TCP_DBG_DET,VERBOSE)) { \ |
| 105 |
if ((_sc)) { \ |
| 106 |
old_state_str = CONN_STATE_TO_STRING((_sc)->state.cstate); \ |
| 107 |
state_str = CONN_STATE_TO_STRING(_cstate); \ |
| 108 |
} \ |
| 109 |
MPL_DBG_OUT_FMT(MPIDI_NEM_TCP_DBG_DET, (MPL_DBG_FDEST, "CHANGE_STATE(_sc=%p, _cstate=%d (%s)) - old_state=%s sc->vc=%p", _sc, _cstate, state_str, old_state_str, (_sc)->vc)); \ |
| 110 |
} \ |
| 111 |
} while (0) |
| 112 |
#else |
| 113 |
#define CONN_STATE_TO_STRING(_cstate) "unavailable" |
| 114 |
#define DBG_CHANGE_STATE(_sc, _cstate) do { /*nothing*/ } while (0) |
| 115 |
#endif |
| 116 |
|
| 117 |
#define CHANGE_STATE(_sc, _cstate) do { \ |
| 118 |
DBG_CHANGE_STATE(_sc, _cstate); \ |
| 119 |
(_sc)->state.cstate = (_cstate); \ |
| 120 |
(_sc)->handler = sc_state_info[_cstate].sc_state_handler; \ |
| 121 |
MPID_nem_tcp_plfd_tbl[(_sc)->index].events = sc_state_info[_cstate].sc_state_plfd_events; \ |
| 122 |
} while (0) |
| 123 |
|
| 124 |
struct MPID_nem_new_tcp_sockconn; |
| 125 |
typedef struct MPID_nem_new_tcp_sockconn sockconn_t; |
| 126 |
|
| 127 |
/* FIXME: should plfd really be const const? Some handlers may need to change the plfd entry. */ |
| 128 |
typedef int (*handler_func_t) (struct pollfd * const plfd, sockconn_t * const conn); |
| 129 |
struct MPID_nem_new_tcp_sockconn { |
| 130 |
int fd; |
| 131 |
int index; |
| 132 |
|
| 133 |
/* Used to prevent the usage of uninitialized pg info. is_same_pg, pg_rank, |
| 134 |
* and pg_id are _ONLY VALID_ if this (pg_is_set) is true */ |
| 135 |
int pg_is_set; |
| 136 |
int is_same_pg; /* boolean, true if the process on the other end of this sc |
| 137 |
* is in the same PG as us (this process) */ |
| 138 |
int is_tmpvc; |
| 139 |
|
| 140 |
int pg_rank; /* rank and id cached here to avoid chasing pointers in vc and vc->pg */ |
| 141 |
char *pg_id; /* MUST be used only if is_same_pg == FALSE */ |
| 142 |
union { |
| 143 |
MPID_nem_tcp_Conn_State_t cstate; |
| 144 |
MPID_nem_tcp_Listen_State_t lstate; |
| 145 |
} state; |
| 146 |
MPIDI_VC_t *vc; |
| 147 |
/* Conn_type_t conn_type; Probably useful for debugging/analyzing purposes. */ |
| 148 |
handler_func_t handler; |
| 149 |
}; |
| 150 |
|
| 151 |
typedef enum MPIDI_nem_tcp_socksm_pkt_type { |
| 152 |
MPIDI_NEM_TCP_SOCKSM_PKT_ID_INFO, /* ID = rank + pg_id */ |
| 153 |
MPIDI_NEM_TCP_SOCKSM_PKT_ID_ACK, |
| 154 |
MPIDI_NEM_TCP_SOCKSM_PKT_ID_NAK, |
| 155 |
MPIDI_NEM_TCP_SOCKSM_PKT_TMPVC_INFO, |
| 156 |
MPIDI_NEM_TCP_SOCKSM_PKT_TMPVC_ACK, |
| 157 |
MPIDI_NEM_TCP_SOCKSM_PKT_TMPVC_NAK, |
| 158 |
MPIDI_NEM_TCP_SOCKSM_PKT_CLOSED |
| 159 |
} MPIDI_nem_tcp_socksm_pkt_type_t; |
| 160 |
|
| 161 |
typedef struct MPIDI_nem_tcp_header { |
| 162 |
MPIDI_nem_tcp_socksm_pkt_type_t pkt_type; |
| 163 |
intptr_t datalen; |
| 164 |
} MPIDI_nem_tcp_header_t; |
| 165 |
|
| 166 |
typedef struct MPIDI_nem_tcp_idinfo { |
| 167 |
int pg_rank; |
| 168 |
/* Commented intentionally */ |
| 169 |
/* char pg_id[pg_id_len+1]; Memory is dynamically allocated for pg_id_len+1 */ |
| 170 |
/* Also, this is optional. Sent only, if the recipient belongs to a different pg. */ |
| 171 |
/* As long as another variable length field needs to be sent across(if at all required */ |
| 172 |
/* in the future), datalen of header itself is enough to find the offset of pg_id */ |
| 173 |
/* in the packet to be sent. */ |
| 174 |
} MPIDI_nem_tcp_idinfo_t; |
| 175 |
|
| 176 |
/* FIXME: bc actually contains port_name info */ |
| 177 |
typedef struct MPIDI_nem_tcp_portinfo { |
| 178 |
int port_name_tag; |
| 179 |
} MPIDI_nem_tcp_portinfo_t; |
| 180 |
|
| 181 |
|
| 182 |
#define MPID_nem_tcp_vc_is_connected(vc_tcp) (vc_tcp->sc && vc_tcp->sc->state.cstate == CONN_STATE_TS_COMMRDY) |
| 183 |
#define MPID_nem_tcp_vc_send_paused(vc_tcp) (vc_tcp->send_paused) |
| 184 |
|
| 185 |
#endif /* SOCKSM_H_INCLUDED */ |