| 1 | /* -*- Mode: C; c-basic-offset:4 ; -*- */ |
|---|
| 2 | /* |
|---|
| 3 | * (C) 2006 by Argonne National Laboratory. |
|---|
| 4 | * See COPYRIGHT in top-level directory. |
|---|
| 5 | */ |
|---|
| 6 | |
|---|
| 7 | #ifndef TCP_IMPL_H |
|---|
| 8 | #define TCP_IMPL_H |
|---|
| 9 | |
|---|
| 10 | #include "mpid_nem_impl.h" |
|---|
| 11 | #include <sys/types.h> |
|---|
| 12 | #include <sys/socket.h> |
|---|
| 13 | #include <netinet/in.h> |
|---|
| 14 | #include <errno.h> |
|---|
| 15 | #include <netinet/tcp.h> |
|---|
| 16 | #include <netdb.h> |
|---|
| 17 | #include "socksm.h" |
|---|
| 18 | |
|---|
| 19 | /* globals */ |
|---|
| 20 | extern struct pollfd *MPID_nem_tcp_plfd_tbl; |
|---|
| 21 | extern sockconn_t MPID_nem_tcp_g_lstn_sc; |
|---|
| 22 | extern struct pollfd MPID_nem_tcp_g_lstn_plfd; |
|---|
| 23 | |
|---|
| 24 | #define MPID_NEM_TCP_VC_STATE_DISCONNECTED 0 |
|---|
| 25 | #define MPID_NEM_TCP_VC_STATE_CONNECTED 1 |
|---|
| 26 | |
|---|
| 27 | /* The vc provides a generic buffer in which network modules can store |
|---|
| 28 | private fields This removes all dependencies from the VC struction |
|---|
| 29 | on the network module, facilitating dynamic module loading. */ |
|---|
| 30 | typedef struct |
|---|
| 31 | { |
|---|
| 32 | struct sockaddr_in sock_id; |
|---|
| 33 | struct MPID_nem_new_tcp_sockconn *sc; |
|---|
| 34 | struct |
|---|
| 35 | { |
|---|
| 36 | struct MPID_Request *head; |
|---|
| 37 | struct MPID_Request *tail; |
|---|
| 38 | } send_queue; |
|---|
| 39 | |
|---|
| 40 | /* this is a count of how many sc objects refer to this vc */ |
|---|
| 41 | int sc_ref_count; |
|---|
| 42 | } MPID_nem_tcp_vc_area; |
|---|
| 43 | |
|---|
| 44 | /* accessor macro to private fields in VC */ |
|---|
| 45 | #define VC_FIELD(vc, field) (((MPID_nem_tcp_vc_area *)((MPIDI_CH3I_VC *)(vc)->channel_private)->netmod_area.padding)->field) |
|---|
| 46 | |
|---|
| 47 | #define ASSIGN_SC_TO_VC(vc_, sc_) \ |
|---|
| 48 | do { \ |
|---|
| 49 | VC_FIELD((vc_), sc) = (sc_); \ |
|---|
| 50 | } while (0) |
|---|
| 51 | |
|---|
| 52 | /* functions */ |
|---|
| 53 | int MPID_nem_tcp_init (MPID_nem_queue_ptr_t proc_recv_queue, |
|---|
| 54 | MPID_nem_queue_ptr_t proc_free_queue, |
|---|
| 55 | MPID_nem_cell_ptr_t proc_elements, int num_proc_elements, |
|---|
| 56 | MPID_nem_cell_ptr_t module_elements, int num_module_elements, |
|---|
| 57 | MPID_nem_queue_ptr_t *module_free_queue, int ckpt_restart, |
|---|
| 58 | MPIDI_PG_t *pg_p, int pg_rank, |
|---|
| 59 | char **bc_val_p, int *val_max_sz_p); |
|---|
| 60 | int MPID_nem_tcp_finalize (void); |
|---|
| 61 | int MPID_nem_tcp_ckpt_shutdown (void); |
|---|
| 62 | int MPID_nem_tcp_send (MPIDI_VC_t *vc, MPID_nem_cell_ptr_t cell, int datalen); |
|---|
| 63 | int MPID_nem_tcp_get_business_card (int my_rank, char **bc_val_p, int *val_max_sz_p); |
|---|
| 64 | int MPID_nem_tcp_connect_to_root (const char *business_card, MPIDI_VC_t *new_vc); |
|---|
| 65 | int MPID_nem_tcp_vc_init (MPIDI_VC_t *vc); |
|---|
| 66 | int MPID_nem_tcp_vc_destroy(MPIDI_VC_t *vc); |
|---|
| 67 | int MPID_nem_tcp_vc_terminate (MPIDI_VC_t *vc); |
|---|
| 68 | |
|---|
| 69 | /* completion counter is atomically decremented when operation completes */ |
|---|
| 70 | int MPID_nem_tcp_get (void *target_p, void *source_p, int source_node, int len, int *completion_ctr); |
|---|
| 71 | int MPID_nem_tcp_put (void *target_p, int target_node, void *source_p, int len, int *completion_ctr); |
|---|
| 72 | |
|---|
| 73 | int MPID_nem_tcp_send_init(void); |
|---|
| 74 | int MPID_nem_tcp_connect(struct MPIDI_VC *const vc); |
|---|
| 75 | int MPID_nem_tcp_connpoll(int in_blocking_poll); |
|---|
| 76 | int MPID_nem_tcp_sm_init(void); |
|---|
| 77 | int MPID_nem_tcp_sm_finalize(void); |
|---|
| 78 | int MPID_nem_tcp_set_sockopts(int fd); |
|---|
| 79 | MPID_NEM_TCP_SOCK_STATUS_t MPID_nem_tcp_check_sock_status(const struct pollfd *const plfd); |
|---|
| 80 | int MPID_nem_tcp_send_finalize(void); |
|---|
| 81 | int MPID_nem_tcp_bind(int sockfd); |
|---|
| 82 | int MPID_nem_tcp_conn_est(MPIDI_VC_t *vc); |
|---|
| 83 | int MPID_nem_tcp_get_conninfo(struct MPIDI_VC *vc, struct sockaddr_in *addr, char **pg_id, int *pg_rank); |
|---|
| 84 | int MPID_nem_tcp_get_vc_from_conninfo(char *pg_id, int pg_rank, struct MPIDI_VC **vc); |
|---|
| 85 | int MPID_nem_tcp_is_sock_connected(int fd); |
|---|
| 86 | int MPID_nem_tcp_disconnect(struct MPIDI_VC *const vc); |
|---|
| 87 | int MPID_nem_tcp_cleanup (struct MPIDI_VC *const vc); |
|---|
| 88 | int MPID_nem_tcp_state_listening_handler(struct pollfd *const l_plfd, sockconn_t *const l_sc); |
|---|
| 89 | int MPID_nem_tcp_send_queued(MPIDI_VC_t *vc); |
|---|
| 90 | |
|---|
| 91 | int MPID_nem_tcp_iSendContig(MPIDI_VC_t *vc, MPID_Request *sreq, void *hdr, MPIDI_msg_sz_t hdr_sz, void *data, MPIDI_msg_sz_t data_sz); |
|---|
| 92 | int MPID_nem_tcp_iStartContigMsg(MPIDI_VC_t *vc, void *hdr, MPIDI_msg_sz_t hdr_sz, void *data, MPIDI_msg_sz_t data_sz, |
|---|
| 93 | MPID_Request **sreq_ptr); |
|---|
| 94 | int MPID_nem_tcp_SendNoncontig(MPIDI_VC_t *vc, MPID_Request *sreq, void *header, MPIDI_msg_sz_t hdr_sz); |
|---|
| 95 | int MPID_nem_tcp_get_addr_port_from_bc(const char *business_card, struct in_addr *addr, in_port_t *port); |
|---|
| 96 | |
|---|
| 97 | void MPID_nem_tcp_vc_dbg_print_sendq(FILE *stream, MPIDI_VC_t *vc); |
|---|
| 98 | |
|---|
| 99 | int MPID_nem_tcp_socksm_finalize(void); |
|---|
| 100 | int MPID_nem_tcp_socksm_init(void); |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | /* Macros */ |
|---|
| 104 | |
|---|
| 105 | /* system call wrapper -- This retries the syscall each time it is interrupted. |
|---|
| 106 | Example usage: instead of writing "ret = write(fd, buf, len);" |
|---|
| 107 | use: "CHECK_EINTR(ret, write(fd, buf, len)); |
|---|
| 108 | Caution: |
|---|
| 109 | (1) Some of the system calls have value-result parameters. Those system calls |
|---|
| 110 | should not be used within CHECK_EINTR macro or should be used with CARE. |
|---|
| 111 | For eg. accept, the last parameter (addrlen) is a value-result one. So, even if the |
|---|
| 112 | system call is interrupted, addrlen should be initialized to appropriate value before |
|---|
| 113 | calling it again. |
|---|
| 114 | |
|---|
| 115 | (2) connect should not be called within a loop. In case, the connect is interrupted after |
|---|
| 116 | the TCP handshake is initiated, calling connect again will only fail. So, select/poll |
|---|
| 117 | should be called to check the status of the socket. |
|---|
| 118 | I don't know what will happen, if a connect is interrupted even before the system call |
|---|
| 119 | tries to initiate TCP handshake. No book/manual doesn't seem to explain this scenario. |
|---|
| 120 | */ |
|---|
| 121 | #define CHECK_EINTR(var, func) do { \ |
|---|
| 122 | (var) = (func); \ |
|---|
| 123 | } while ((var) == -1 && errno == EINTR) |
|---|
| 124 | |
|---|
| 125 | /* Send queue macros */ |
|---|
| 126 | #define Q_EMPTY(q) GENERIC_Q_EMPTY (q) |
|---|
| 127 | #define Q_HEAD(q) GENERIC_Q_HEAD (q) |
|---|
| 128 | #define Q_ENQUEUE_EMPTY(qp, ep) GENERIC_Q_ENQUEUE_EMPTY (qp, ep, next) |
|---|
| 129 | #define Q_ENQUEUE(qp, ep) GENERIC_Q_ENQUEUE (qp, ep, next) |
|---|
| 130 | #define Q_ENQUEUE_EMPTY_MULTIPLE(qp, ep0, ep1) GENERIC_Q_ENQUEUE_EMPTY_MULTIPLE (qp, ep0, ep1, next) |
|---|
| 131 | #define Q_ENQUEUE_MULTIPLE(qp, ep0, ep1) GENERIC_Q_ENQUEUE_MULTIPLE (qp, ep0, ep1, next) |
|---|
| 132 | #define Q_DEQUEUE(qp, ep) GENERIC_Q_DEQUEUE (qp, ep, next) |
|---|
| 133 | #define Q_REMOVE_ELEMENTS(qp, ep0, ep1) GENERIC_Q_REMOVE_ELEMENTS (qp, ep0, ep1, next) |
|---|
| 134 | |
|---|
| 135 | /* VC list macros */ |
|---|
| 136 | #define VC_L_EMPTY(q) GENERIC_L_EMPTY (q) |
|---|
| 137 | #define VC_L_HEAD(q) GENERIC_L_HEAD (q) |
|---|
| 138 | #define SET_PLFD(ep) MPID_nem_tcp_plfd_tbl[VC_FIELD(ep, sc)->index].events |= POLLOUT |
|---|
| 139 | #define UNSET_PLFD(ep) MPID_nem_tcp_plfd_tbl[VC_FIELD(ep, sc)->index].events &= ~POLLOUT |
|---|
| 140 | |
|---|
| 141 | /* stack macros */ |
|---|
| 142 | #define S_EMPTY(s) GENERIC_S_EMPTY (s) |
|---|
| 143 | #define S_TOP(s) GENERIC_S_TOP (s) |
|---|
| 144 | #define S_PUSH(sp, ep) GENERIC_S_PUSH (sp, ep, next) |
|---|
| 145 | #define S_PUSH_MULTIPLE(sp, ep0, ep1) GENERIC_S_PUSH_MULTIPLE (sp, ep0, ep1, next) |
|---|
| 146 | #define S_POP(sp, ep) GENERIC_S_POP (sp, ep, next) |
|---|
| 147 | |
|---|
| 148 | /* interface utilities */ |
|---|
| 149 | /*S |
|---|
| 150 | MPIDU_Sock_ifaddr_t - Structure to hold an Internet address. |
|---|
| 151 | |
|---|
| 152 | + len - Length of the address. 4 for IPv4, 16 for IPv6. |
|---|
| 153 | - ifaddr - Address bytes (as bytes, not characters) |
|---|
| 154 | |
|---|
| 155 | S*/ |
|---|
| 156 | typedef struct MPIDU_Sock_ifaddr_t { |
|---|
| 157 | int len, type; |
|---|
| 158 | unsigned char ifaddr[16]; |
|---|
| 159 | } MPIDU_Sock_ifaddr_t; |
|---|
| 160 | int MPIDI_GetIPInterface( MPIDU_Sock_ifaddr_t *ifaddr, int *found ); |
|---|
| 161 | |
|---|
| 162 | #endif /* TCP_IMPL_H */ |
|---|