Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-23 10:12:54

0001 /* -*- C -*-
0002  *
0003  * Copyright (c) 2020      Intel, Inc.  All rights reserved.
0004  * Copyright (c) 2021      Nanook Consulting.  All rights reserved.
0005  * $COPYRIGHT$
0006  *
0007  * Additional copyrights may follow
0008  *
0009  * $HEADER$
0010  *
0011  */
0012 #ifndef PMIX_PTL_BASE_HANDSHAKE_H_
0013 #define PMIX_PTL_BASE_HANDSHAKE_H_
0014 
0015 #include "src/include/pmix_config.h"
0016 
0017 #ifdef HAVE_STRING_H
0018 #    include <string.h>
0019 #endif
0020 
0021 /* The following macros are for use in the PTL connection
0022  * handler. For simplicity, they include variables that are
0023  * defined in the functions where they are used - thus, these
0024  * macros are NOT portable for use elsewhere
0025  *
0026  * A symmetric pair of macros are provided specifically to
0027  * make it easier to read and compare the two ends of the
0028  * handshake */
0029 
0030 BEGIN_C_DECLS
0031 
0032 /* define flag values that indicate the type of process attempting
0033  * to connect to a server:
0034  *  0 => simple client process
0035  *  1 => legacy tool - may or may not have an identifier
0036  *  2 => legacy launcher - may or may not have an identifier
0037  * ------------------------------------------
0038  *  3 => self-started tool process that needs an identifier
0039  *  4 => self-started tool process that was given an identifier by caller
0040  *  5 => tool that was started by a PMIx server - identifier specified by server
0041  *  6 => self-started launcher that needs an identifier
0042  *  7 => self-started launcher that was given an identifier by caller
0043  *  8 => launcher that was started by a PMIx server - identifier specified by server
0044  *  9 => singleton client - treated like a tool that has an identifier
0045  * ------------------------------------------
0046  * 10 => scheduler
0047  */
0048 typedef uint8_t pmix_rnd_flag_t;
0049 #define PMIX_SIMPLE_CLIENT      0
0050 #define PMIX_LEGACY_TOOL        1
0051 #define PMIX_LEGACY_LAUNCHER    2
0052 #define PMIX_TOOL_NEEDS_ID      3
0053 #define PMIX_TOOL_GIVEN_ID      4
0054 #define PMIX_TOOL_CLIENT        5
0055 #define PMIX_LAUNCHER_NEEDS_ID  6
0056 #define PMIX_LAUNCHER_GIVEN_ID  7
0057 #define PMIX_LAUNCHER_CLIENT    8
0058 #define PMIX_SINGLETON_CLIENT   9
0059 #define PMIX_SCHEDULER_WITH_ID 10
0060 
0061 /* The following macros are used in the ptl_base_connection_hdlr.c
0062  * file to parse the handshake message and extract its fields */
0063 
0064 #define PMIX_PTL_GET_STRING(n)                  \
0065     do {                                        \
0066         size_t _l;                              \
0067         PMIX_STRNLEN(_l, mg, cnt);              \
0068         if (_l < cnt) {                         \
0069             (n) = strdup(mg);                   \
0070             mg += strlen((n)) + 1;              \
0071             cnt -= strlen((n)) + 1;             \
0072         } else {                                \
0073             PMIX_ERROR_LOG(PMIX_ERR_BAD_PARAM); \
0074             goto error;                         \
0075         }                                       \
0076     } while (0)
0077 
0078 /* numerical value must be converted back to host
0079  * order, so we use an intermediate storage place
0080  * for that purpose */
0081 #define PMIX_PTL_GET_U32_NOERROR(r, n)          \
0082     do {                                        \
0083         uint32_t _u;                            \
0084         if (sizeof(uint32_t) <= cnt) {          \
0085             memcpy(&_u, mg, sizeof(uint32_t));  \
0086             (n) = ntohl(_u);                    \
0087             mg += sizeof(uint32_t);             \
0088             cnt -= sizeof(uint32_t);            \
0089             (r) = PMIX_SUCCESS;                 \
0090         } else {                                \
0091             (r) = PMIX_ERR_BAD_PARAM;           \
0092         }                                       \
0093     } while (0)
0094 
0095 #define PMIX_PTL_GET_U32(n)                     \
0096     do {                                        \
0097         uint32_t _u;                            \
0098         if (sizeof(uint32_t) <= cnt) {          \
0099             memcpy(&_u, mg, sizeof(uint32_t));  \
0100             (n) = ntohl(_u);                    \
0101             mg += sizeof(uint32_t);             \
0102             cnt -= sizeof(uint32_t);            \
0103         } else {                                \
0104             PMIX_ERROR_LOG(PMIX_ERR_BAD_PARAM); \
0105             goto error;                         \
0106         }                                       \
0107     } while (0)
0108 
0109 #define PMIX_PTL_GET_BLOB(b, l)                 \
0110     do {                                        \
0111         if (0 < (l)) {                          \
0112             (b) = (char *) malloc((l));         \
0113             if (NULL == (b)) {                  \
0114                 PMIX_ERROR_LOG(PMIX_ERR_NOMEM); \
0115                 goto error;                     \
0116             }                                   \
0117             memcpy((b), mg, (l));               \
0118             mg += (l);                          \
0119             cnt -= (l);                         \
0120         }                                       \
0121     } while (0)
0122 
0123 #define PMIX_PTL_GET_U8(n)                      \
0124     do {                                        \
0125         if (sizeof(uint8_t) <= cnt) {           \
0126             memcpy(&(n), mg, sizeof(uint8_t));  \
0127             mg += sizeof(uint8_t);              \
0128             cnt -= sizeof(uint8_t);             \
0129         } else {                                \
0130             PMIX_ERROR_LOG(PMIX_ERR_BAD_PARAM); \
0131             goto error;                         \
0132         }                                       \
0133     } while (0)
0134 
0135 #define PMIX_PTL_GET_PROCID(p)              \
0136     do {                                    \
0137         char *n;                            \
0138         uint32_t _r;                        \
0139         pmix_status_t _rc;                  \
0140         PMIX_PTL_GET_STRING(n);             \
0141         PMIX_PTL_GET_U32_NOERROR(_rc, _r);  \
0142         if (PMIX_SUCCESS != _rc) {          \
0143             PMIX_ERROR_LOG(_rc);            \
0144             free(n);                        \
0145             goto error;                     \
0146         }                                   \
0147         PMIX_LOAD_PROCID(&(p), n, _r);      \
0148         free(n);                            \
0149     } while (0)
0150 
0151 /* The following macros are for use in the ptl_base_fns.c
0152  * when constructing the handshake message. */
0153 #define PMIX_PTL_PUT_STRING(n)                 \
0154     do {                                       \
0155         memcpy(msg + csize, (n), strlen((n))); \
0156         csize += strlen((n)) + 1;              \
0157     } while (0)
0158 
0159 /* numerical value must be converted to network byte
0160  * order, so we use an intermediate storage place
0161  * for that purpose */
0162 #define PMIX_PTL_PUT_U32(n)                         \
0163     do {                                            \
0164         uint32_t _u;                                \
0165         _u = htonl((uint32_t)(n));                  \
0166         memcpy(msg + csize, &_u, sizeof(uint32_t)); \
0167         csize += sizeof(uint32_t);                  \
0168     } while (0)
0169 
0170 #define PMIX_PTL_PUT_BLOB(b, l)            \
0171     do {                                   \
0172         if (0 < (l)) {                     \
0173             memcpy(msg + csize, (b), (l)); \
0174             csize += (l);                  \
0175         }                                  \
0176     } while (0)
0177 
0178 #define PMIX_PTL_PUT_U8(n)                          \
0179     do {                                            \
0180         memcpy(msg + csize, &(n), sizeof(uint8_t)); \
0181         csize += sizeof(uint8_t);                   \
0182     } while (0)
0183 
0184 #define PMIX_PTL_PUT_PROCID(p)           \
0185     do {                                 \
0186         PMIX_PTL_PUT_STRING((p).nspace); \
0187         PMIX_PTL_PUT_U32((p).rank);      \
0188     } while (0)
0189 
0190 /* the following macros are for use in the ptl_base_fns.c
0191  * when sending/recving values during the handshake */
0192 #define PMIX_PTL_RECV_NSPACE(s, n)                                              \
0193     do {                                                                        \
0194         pmix_status_t r;                                                        \
0195         r = pmix_ptl_base_recv_blocking((s), (char *) (n), PMIX_MAX_NSLEN + 1); \
0196         (n)[PMIX_MAX_NSLEN] = '\0'; /* ensure NULL termination */               \
0197         if (PMIX_SUCCESS != r) {                                                \
0198             return r;                                                           \
0199         }                                                                       \
0200     } while (0)
0201 
0202 #define PMIX_PTL_RECV_U32(s, n)                                               \
0203     do {                                                                      \
0204         pmix_status_t r;                                                      \
0205         uint32_t _u;                                                          \
0206         r = pmix_ptl_base_recv_blocking((s), (char *) &_u, sizeof(uint32_t)); \
0207         if (PMIX_SUCCESS != r) {                                              \
0208             return r;                                                         \
0209         }                                                                     \
0210         (n) = htonl(_u);                                                      \
0211     } while (0)
0212 
0213 END_C_DECLS
0214 
0215 #endif