Exemplo n.º 1
0
/*
 * Check the status of the connection. If the connection failed, will retry
 * later. Otherwise, send this processes identifier to the endpoint on the 
 * newly connected socket.
 */
static void mca_btl_tcp_endpoint_complete_connect(mca_btl_base_endpoint_t* btl_endpoint)
{
    int so_error = 0;
    opal_socklen_t so_length = sizeof(so_error);
    struct sockaddr_storage endpoint_addr;

    mca_btl_tcp_proc_tosocks(btl_endpoint->endpoint_addr, &endpoint_addr);

    /* unregister from receiving event notifications */
    opal_event_del(&btl_endpoint->endpoint_send_event);

    /* check connect completion status */
    if(getsockopt(btl_endpoint->endpoint_sd, SOL_SOCKET, SO_ERROR, (char *)&so_error, &so_length) < 0) {
        BTL_ERROR(("getsockopt() to %s failed: %s (%d)", 
                   opal_net_get_hostname((struct sockaddr*) &endpoint_addr),
                   strerror(opal_socket_errno), opal_socket_errno));
        mca_btl_tcp_endpoint_close(btl_endpoint);
        return;
    }
    if(so_error == EINPROGRESS || so_error == EWOULDBLOCK) {
        opal_event_add(&btl_endpoint->endpoint_send_event, 0);
        return;
    }
    if(so_error != 0) {
        BTL_ERROR(("connect() to %s failed: %s (%d)", 
                   opal_net_get_hostname((struct sockaddr*) &endpoint_addr),
                   strerror(so_error), so_error));
        mca_btl_tcp_endpoint_close(btl_endpoint);
        return;
    }

    if(mca_btl_tcp_endpoint_send_connect_ack(btl_endpoint) == OMPI_SUCCESS) {
        btl_endpoint->endpoint_state = MCA_BTL_TCP_CONNECT_ACK;
        opal_event_add(&btl_endpoint->endpoint_recv_event, 0);
    } else {
        mca_btl_tcp_endpoint_close(btl_endpoint);
    }
}
Exemplo n.º 2
0
/*
 * Note that this routine must be called with the lock on the process
 * already held.  Insert a btl instance into the proc array and assign
 * it an address.
 */
int mca_btl_tcp_proc_insert( mca_btl_tcp_proc_t* btl_proc,
                             mca_btl_base_endpoint_t* btl_endpoint )
{
    struct sockaddr_storage endpoint_addr_ss;
    const char *proc_hostname;
    unsigned int perm_size;
    int rc, *a = NULL;
    size_t i, j;
    mca_btl_tcp_interface_t** peer_interfaces;
    mca_btl_tcp_proc_data_t _proc_data, *proc_data=&_proc_data;
    size_t max_peer_interfaces;
    memset(proc_data, 0, sizeof(mca_btl_tcp_proc_data_t));

    if (NULL == (proc_hostname = opal_get_proc_hostname(btl_proc->proc_opal))) {
        return OPAL_ERR_UNREACH;
    }

#ifndef WORDS_BIGENDIAN
    /* if we are little endian and our peer is not so lucky, then we
       need to put all information sent to him in big endian (aka
       Network Byte Order) and expect all information received to
       be in NBO.  Since big endian machines always send and receive
       in NBO, we don't care so much about that case. */
    if (btl_proc->proc_opal->proc_arch & OPAL_ARCH_ISBIGENDIAN) {
        btl_endpoint->endpoint_nbo = true;
    }
#endif

    /* insert into endpoint array */
    btl_endpoint->endpoint_proc = btl_proc;
    btl_proc->proc_endpoints[btl_proc->proc_endpoint_count++] = btl_endpoint;

    /* sanity checks */
    if( NULL == mca_btl_tcp_retrieve_local_interfaces(proc_data) )
        return OPAL_ERR_OUT_OF_RESOURCE;
    if( 0 == proc_data->num_local_interfaces ) {
        return OPAL_ERR_UNREACH;
    }

    max_peer_interfaces = proc_data->max_local_interfaces;
    peer_interfaces = (mca_btl_tcp_interface_t**)calloc( max_peer_interfaces, sizeof(mca_btl_tcp_interface_t*) );
    assert(NULL != peer_interfaces);
    proc_data->num_peer_interfaces = 0;
    memset(proc_data->peer_kindex_to_index, -1, sizeof(int)*MAX_KERNEL_INTERFACE_INDEX);

    /*
     * identify all kernel interfaces and the associated addresses of
     * the peer
     */

    for( i = 0; i < btl_proc->proc_addr_count; i++ ) {

        int index;

        mca_btl_tcp_addr_t* endpoint_addr = btl_proc->proc_addrs + i;

        mca_btl_tcp_proc_tosocks (endpoint_addr, &endpoint_addr_ss);

        index = proc_data->peer_kindex_to_index[endpoint_addr->addr_ifkindex];

        if(-1 == index) {
            index = proc_data->num_peer_interfaces++;
            proc_data->peer_kindex_to_index[endpoint_addr->addr_ifkindex] = index;
            if( proc_data->num_peer_interfaces == max_peer_interfaces ) {
                max_peer_interfaces <<= 1;
                peer_interfaces = (mca_btl_tcp_interface_t**)realloc( peer_interfaces,
                                                                      max_peer_interfaces * sizeof(mca_btl_tcp_interface_t*) );
                if( NULL == peer_interfaces )
                    return OPAL_ERR_OUT_OF_RESOURCE;
            }
            peer_interfaces[index] = (mca_btl_tcp_interface_t *) malloc(sizeof(mca_btl_tcp_interface_t));
            mca_btl_tcp_initialise_interface(peer_interfaces[index],
                                             endpoint_addr->addr_ifkindex, index);
        }

        /*
         * in case one of the peer addresses is already in use,
         * mark the complete peer interface as 'not available'
         */
        if(endpoint_addr->addr_inuse) {
            peer_interfaces[index]->inuse = 1;
        }

        switch(endpoint_addr_ss.ss_family) {
        case AF_INET:
            peer_interfaces[index]->ipv4_address = (struct sockaddr_storage*) malloc(sizeof(endpoint_addr_ss));
            peer_interfaces[index]->ipv4_endpoint_addr = endpoint_addr;
            memcpy(peer_interfaces[index]->ipv4_address,
                   &endpoint_addr_ss, sizeof(endpoint_addr_ss));
            break;
        case AF_INET6:
            peer_interfaces[index]->ipv6_address = (struct sockaddr_storage*) malloc(sizeof(endpoint_addr_ss));
            peer_interfaces[index]->ipv6_endpoint_addr = endpoint_addr;
            memcpy(peer_interfaces[index]->ipv6_address,
                   &endpoint_addr_ss, sizeof(endpoint_addr_ss));
            break;
        default:
            opal_output(0, "unknown address family for tcp: %d\n",
                        endpoint_addr_ss.ss_family);
            /*
             * return OPAL_UNREACH or some error, as this is not
             * good
             */
        }
    }

    /*
     * assign weights to each possible pair of interfaces
     */

    perm_size = proc_data->num_local_interfaces;
    if(proc_data->num_peer_interfaces > perm_size) {
        perm_size = proc_data->num_peer_interfaces;
    }

    proc_data->weights = (enum mca_btl_tcp_connection_quality**) malloc(perm_size
                                                             * sizeof(enum mca_btl_tcp_connection_quality*));
    assert(NULL != proc_data->weights);

    proc_data->best_addr = (mca_btl_tcp_addr_t ***) malloc(perm_size
                                                * sizeof(mca_btl_tcp_addr_t **));
    assert(NULL != proc_data->best_addr);
    for(i = 0; i < perm_size; ++i) {
        proc_data->weights[i] = (enum mca_btl_tcp_connection_quality*) calloc(perm_size,
                                                                   sizeof(enum mca_btl_tcp_connection_quality));
        assert(NULL != proc_data->weights[i]);

        proc_data->best_addr[i] = (mca_btl_tcp_addr_t **) calloc(perm_size,
                                                      sizeof(mca_btl_tcp_addr_t *));
        assert(NULL != proc_data->best_addr[i]);
    }


    for( i = 0; i < proc_data->num_local_interfaces; ++i ) {
        mca_btl_tcp_interface_t* local_interface = proc_data->local_interfaces[i];
        for( j = 0; j < proc_data->num_peer_interfaces; ++j ) {

            /*  initially, assume no connection is possible */
            proc_data->weights[i][j] = CQ_NO_CONNECTION;

            /* check state of ipv4 address pair */
            if(NULL != proc_data->local_interfaces[i]->ipv4_address &&
               NULL != peer_interfaces[j]->ipv4_address) {

                if(opal_net_addr_isipv4public((struct sockaddr*) local_interface->ipv4_address) &&
                   opal_net_addr_isipv4public((struct sockaddr*) peer_interfaces[j]->ipv4_address)) {
                    if(opal_net_samenetwork((struct sockaddr*) local_interface->ipv4_address,
                                            (struct sockaddr*) peer_interfaces[j]->ipv4_address,
                                            local_interface->ipv4_netmask)) {
                        proc_data->weights[i][j] = CQ_PUBLIC_SAME_NETWORK;
                    } else {
                        proc_data->weights[i][j] = CQ_PUBLIC_DIFFERENT_NETWORK;
                    }
                    proc_data->best_addr[i][j] = peer_interfaces[j]->ipv4_endpoint_addr;
                    continue;
                }
                if(opal_net_samenetwork((struct sockaddr*) local_interface->ipv4_address,
                                        (struct sockaddr*) peer_interfaces[j]->ipv4_address,
                                        local_interface->ipv4_netmask)) {
                    proc_data->weights[i][j] = CQ_PRIVATE_SAME_NETWORK;
                } else {
                    proc_data->weights[i][j] = CQ_PRIVATE_DIFFERENT_NETWORK;
                }
                proc_data->best_addr[i][j] = peer_interfaces[j]->ipv4_endpoint_addr;
                continue;
            }

            /* check state of ipv6 address pair - ipv6 is always public,
             * since link-local addresses are skipped in opal_ifinit()
             */
            if(NULL != local_interface->ipv6_address &&
               NULL != peer_interfaces[j]->ipv6_address) {

                if(opal_net_samenetwork((struct sockaddr*) local_interface->ipv6_address,
                                         (struct sockaddr*) peer_interfaces[j]->ipv6_address,
                                         local_interface->ipv6_netmask)) {
                    proc_data->weights[i][j] = CQ_PUBLIC_SAME_NETWORK;
                } else {
                    proc_data->weights[i][j] = CQ_PUBLIC_DIFFERENT_NETWORK;
                }
                proc_data->best_addr[i][j] = peer_interfaces[j]->ipv6_endpoint_addr;
                continue;
            }

        } /* for each peer interface */
    } /* for each local interface */

    /*
     * determine the size of the set to permute (max number of
     * interfaces
     */

    proc_data->best_assignment = (unsigned int *) malloc (perm_size * sizeof(int));

    a = (int *) malloc(perm_size * sizeof(int));
    if (NULL == a) {
        return OPAL_ERR_OUT_OF_RESOURCE;
    }

    /* Can only find the best set of connections when the number of
     * interfaces is not too big.  When it gets larger, we fall back
     * to a simpler and faster (and not as optimal) algorithm.
     * See ticket https://svn.open-mpi.org/trac/ompi/ticket/2031
     * for more details about this issue.  */
    if (perm_size <= MAX_PERMUTATION_INTERFACES) {
        memset(a, 0, perm_size * sizeof(int));
        proc_data->max_assignment_cardinality = -1;
        proc_data->max_assignment_weight = -1;
        visit(proc_data, 0, -1, perm_size, a);

        rc = OPAL_ERR_UNREACH;
        for(i = 0; i < perm_size; ++i) {
            unsigned int best = proc_data->best_assignment[i];
            if(best > proc_data->num_peer_interfaces
               || proc_data->weights[i][best] == CQ_NO_CONNECTION
               || peer_interfaces[best]->inuse
               || NULL == peer_interfaces[best]) {
                continue;
            }
            peer_interfaces[best]->inuse++;
            btl_endpoint->endpoint_addr = proc_data->best_addr[i][best];
            btl_endpoint->endpoint_addr->addr_inuse++;
            rc = OPAL_SUCCESS;
            break;
        }
    } else {
        enum mca_btl_tcp_connection_quality max;
        int i_max = 0, j_max = 0;
        /* Find the best connection that is not in use.  Save away
         * the indices of the best location. */
        max = CQ_NO_CONNECTION;
        for(i=0; i<proc_data->num_local_interfaces; ++i) {
            for(j=0; j<proc_data->num_peer_interfaces; ++j) {
                if (!peer_interfaces[j]->inuse) {
                    if (proc_data->weights[i][j] > max) {
                        max = proc_data->weights[i][j];
                        i_max = i;
                        j_max = j;
                    }
                }
            }
        }
        /* Now see if there is a some type of connection available. */
        rc = OPAL_ERR_UNREACH;
        if (CQ_NO_CONNECTION != max) {
            peer_interfaces[j_max]->inuse++;
            btl_endpoint->endpoint_addr = proc_data->best_addr[i_max][j_max];
            btl_endpoint->endpoint_addr->addr_inuse++;
            rc = OPAL_SUCCESS;
        }
    }

    for(i = 0; i < perm_size; ++i) {
        free(proc_data->weights[i]);
        free(proc_data->best_addr[i]);
    }

    for(i = 0; i < proc_data->num_peer_interfaces; ++i) {
        if(NULL != peer_interfaces[i]->ipv4_address) {
            free(peer_interfaces[i]->ipv4_address);
        }
        if(NULL != peer_interfaces[i]->ipv6_address) {
            free(peer_interfaces[i]->ipv6_address);
        }
        free(peer_interfaces[i]);
    }
    free(peer_interfaces);

    for(i = 0; i < proc_data->num_local_interfaces; ++i) {
        if(NULL != proc_data->local_interfaces[i]->ipv4_address) {
            free(proc_data->local_interfaces[i]->ipv4_address);
        }
        if(NULL != proc_data->local_interfaces[i]->ipv6_address) {
            free(proc_data->local_interfaces[i]->ipv6_address);
        }
        free(proc_data->local_interfaces[i]);
    }
    free(proc_data->local_interfaces);
    proc_data->max_local_interfaces = 0;

    free(proc_data->weights);
    free(proc_data->best_addr);
    free(proc_data->best_assignment);
    free(a);

    return rc;
}
Exemplo n.º 3
0
/*
 *  Start a connection to the endpoint. This will likely not complete,
 *  as the socket is set to non-blocking, so register for event
 *  notification of connect completion. On connection we send
 *  our globally unique process identifier to the endpoint and wait for
 *  the endpoints response.
 */
static int mca_btl_tcp_endpoint_start_connect(mca_btl_base_endpoint_t* btl_endpoint)
{
    int rc,flags;
    struct sockaddr_storage endpoint_addr;
    /* By default consider a IPv4 connection */
    uint16_t af_family = AF_INET;
    opal_socklen_t addrlen = sizeof(struct sockaddr_in);
    
#if OPAL_WANT_IPV6
    if (AF_INET6 == btl_endpoint->endpoint_addr->addr_family) {
        af_family = AF_INET6;
        addrlen = sizeof (struct sockaddr_in6);
    }
#endif
    
    btl_endpoint->endpoint_sd = socket(af_family, SOCK_STREAM, 0);
    if (btl_endpoint->endpoint_sd < 0) {
        btl_endpoint->endpoint_retries++;
        return OMPI_ERR_UNREACH;
    }

    /* setup socket buffer sizes */
    mca_btl_tcp_set_socket_options(btl_endpoint->endpoint_sd);

    /* setup event callbacks */
    mca_btl_tcp_endpoint_event_init(btl_endpoint);

    /* setup the socket as non-blocking */
    if((flags = fcntl(btl_endpoint->endpoint_sd, F_GETFL, 0)) < 0) {
        BTL_ERROR(("fcntl(F_GETFL) failed: %s (%d)", 
                   strerror(opal_socket_errno), opal_socket_errno));
    } else {
        flags |= O_NONBLOCK;
        if(fcntl(btl_endpoint->endpoint_sd, F_SETFL, flags) < 0)
            BTL_ERROR(("fcntl(F_SETFL) failed: %s (%d)", 
                       strerror(opal_socket_errno), opal_socket_errno));
    }

    /* start the connect - will likely fail with EINPROGRESS */
    mca_btl_tcp_proc_tosocks(btl_endpoint->endpoint_addr, &endpoint_addr);

    opal_output_verbose(20, mca_btl_base_output, 
                        "btl: tcp: attempting to connect() to %s address %s on port %d",
                        ORTE_NAME_PRINT(&btl_endpoint->endpoint_proc->proc_ompi->proc_name),
                        opal_net_get_hostname((struct sockaddr*) &endpoint_addr),
                        ntohs(btl_endpoint->endpoint_addr->addr_port));

    if(connect(btl_endpoint->endpoint_sd, (struct sockaddr*)&endpoint_addr, addrlen) < 0) {
        /* non-blocking so wait for completion */
        if(opal_socket_errno == EINPROGRESS || opal_socket_errno == EWOULDBLOCK) {
            btl_endpoint->endpoint_state = MCA_BTL_TCP_CONNECTING;
            opal_event_add(&btl_endpoint->endpoint_send_event, 0);
            return OMPI_SUCCESS;
        }
        {
            char *address;
            address = opal_net_get_hostname((struct sockaddr*) &endpoint_addr);
            BTL_PEER_ERROR( btl_endpoint->endpoint_proc->proc_ompi,
                          ( "Unable to connect to the peer %s on port %d: %s\n",
                            address,
                           btl_endpoint->endpoint_addr->addr_port, strerror(opal_socket_errno) ) );
        }
        mca_btl_tcp_endpoint_close(btl_endpoint);
        btl_endpoint->endpoint_retries++;
        return OMPI_ERR_UNREACH;
    }

    /* send our globally unique process identifier to the endpoint */
    if((rc = mca_btl_tcp_endpoint_send_connect_ack(btl_endpoint)) == OMPI_SUCCESS) {
        btl_endpoint->endpoint_state = MCA_BTL_TCP_CONNECT_ACK;
        opal_event_add(&btl_endpoint->endpoint_recv_event, 0);
    } else {
        mca_btl_tcp_endpoint_close(btl_endpoint);
    }
    return rc;
}