void * netloc_lookup_table_iterator_next_entry(struct netloc_dt_lookup_table_iterator* hti) { size_t i; for(i = hti->loc; i < netloc_lookup_table_size(hti->htp); ++i) { if( NULL != hti->htp->ht_entries[i] ) { hti->loc = i+1; return hti->htp->ht_entries[i]->value; } } hti->loc = netloc_lookup_table_size(hti->htp); hti->at_end = true; return NULL; }
unsigned long netloc_lookup_table_iterator_next_key_int(struct netloc_dt_lookup_table_iterator* hti) { size_t i; for(i = hti->loc; i < netloc_lookup_table_size(hti->htp); ++i) { if( NULL != hti->htp->ht_entries[i] ) { hti->loc = i+1; return hti->htp->ht_entries[i]->__key__; } } hti->loc = netloc_lookup_table_size(hti->htp); hti->at_end = true; return 0; }
int netloc_dt_lookup_table_t_copy(struct netloc_dt_lookup_table *from, struct netloc_dt_lookup_table *to) { size_t i; int dup = !(from->flags & NETLOC_LOOKUP_TABLE_FLAG_NO_STRDUP_KEY); if( NULL == from || NULL == to ) { return NETLOC_ERROR; } netloc_lookup_table_init(to, netloc_lookup_table_size(from), from->flags); for(i = 0; i < from->ht_size; ++i ) { if( NULL != from->ht_entries[i] ) { to->ht_entries[i] = netloc_copy_lookup_table_entry_t(from->ht_entries[i], dup); } } return NETLOC_SUCCESS; }
static int display_topo_screen(netloc_topology_t topology, netloc_network_t *network) { int ret, exit_status = NETLOC_SUCCESS; int i; netloc_dt_lookup_table_t hosts_nodes = NULL; netloc_dt_lookup_table_t switches_nodes = NULL; netloc_dt_lookup_table_iterator_t hti = NULL; const char * key = NULL; netloc_node_t *node = NULL; int num_edges; netloc_edge_t **edges = NULL; printf("Network: %s\n", netloc_pretty_print_network_t(network) ); printf(" Type : %s\n", netloc_decode_network_type_readable(network->network_type) ); printf(" Subnet : %s\n", network->subnet_id); /* * Get hosts and switches */ ret = netloc_get_all_host_nodes(topology, &hosts_nodes); if( NETLOC_SUCCESS != ret ) { exit_status = ret; goto cleanup; } printf(" Hosts : %d\n", netloc_lookup_table_size(hosts_nodes));; ret = netloc_get_all_switch_nodes(topology, &switches_nodes); if( NETLOC_SUCCESS != ret ) { exit_status = ret; goto cleanup; } printf(" Switches: %d\n", netloc_lookup_table_size(switches_nodes) ); printf("---------------------------------------------------\n"); if( full_output ) { /* * Print out a list of hosts and their connections */ printf("\n"); printf("Information by Host\n"); printf("---------------------\n"); hti = netloc_dt_lookup_table_iterator_t_construct( hosts_nodes ); while( !netloc_lookup_table_iterator_at_end(hti) ) { key = netloc_lookup_table_iterator_next_key(hti); if( NULL == key ) { break; } node = (netloc_node_t*)netloc_lookup_table_access(hosts_nodes, key); ret = netloc_get_all_edges(topology, node, &num_edges, &edges); if( NETLOC_SUCCESS != ret ) { exit_status = ret; goto cleanup; } for(i = 0; i < num_edges; ++i ) { display_netloc_edge_screen(edges[i]); } } /* * Print out a list of switches and their connections */ printf("\n"); printf("Information by Switch\n"); printf("---------------------\n"); hti = netloc_dt_lookup_table_iterator_t_construct( switches_nodes ); while( !netloc_lookup_table_iterator_at_end(hti) ) { key = netloc_lookup_table_iterator_next_key(hti); if( NULL == key ) { break; } node = (netloc_node_t*)netloc_lookup_table_access(switches_nodes, key); ret = netloc_get_all_edges(topology, node, &num_edges, &edges); if( NETLOC_SUCCESS != ret ) { exit_status = ret; goto cleanup; } for(i = 0; i < num_edges; ++i ) { display_netloc_edge_screen(edges[i]); } } printf("------------------------------------------------------------------------------\n"); } printf("\n"); cleanup: return exit_status; }
/************************************************************* * Support Functionality *************************************************************/ static int compute_shortest_path_dijkstra(netloc_data_collection_handle_t *handle, netloc_node_t *src_node, netloc_node_t *dest_node, int *num_edges, netloc_edge_t ***edges) { int exit_status = NETLOC_SUCCESS; int i; pq_queue_t *queue = NULL; int *distance = NULL; bool *not_seen = NULL; netloc_node_t *node_u = NULL; netloc_node_t *node_v = NULL; netloc_node_t **prev_node = NULL; netloc_edge_t **prev_edge = NULL; int alt; int idx_u, idx_v; int num_rev_edges; netloc_edge_t **rev_edges = NULL; struct netloc_dt_lookup_table_iterator *hti = NULL; netloc_node_t *cur_node = NULL; unsigned long key_int; // Just in case things go poorly below (*num_edges) = 0; (*edges) = NULL; /* * Allocate some data structures */ queue = pq_queue_t_construct(); if( NULL == queue ) { fprintf(stderr, "Error: Failed to allocate the queue\n"); exit_status = NETLOC_ERROR; goto cleanup; } distance = (int*)malloc(sizeof(int) * netloc_lookup_table_size(handle->node_list)); if( NULL == distance ) { fprintf(stderr, "Error: Failed to allocate the distance array\n"); exit_status = NETLOC_ERROR; goto cleanup; } not_seen = (bool*)malloc(sizeof(bool) * netloc_lookup_table_size(handle->node_list)); if( NULL == not_seen ) { fprintf(stderr, "Error: Failed to allocate the 'not_seen' array\n"); exit_status = NETLOC_ERROR; goto cleanup; } prev_node = (netloc_node_t**)malloc(sizeof(netloc_node_t*) * netloc_lookup_table_size(handle->node_list)); if( NULL == prev_node ) { fprintf(stderr, "Error: Failed to allocate the 'prev_node' array\n"); exit_status = NETLOC_ERROR; goto cleanup; } prev_edge = (netloc_edge_t**)malloc(sizeof(netloc_edge_t*) * netloc_lookup_table_size(handle->node_list)); if( NULL == prev_edge ) { fprintf(stderr, "Error: Failed to allocate the 'prev_edge' array\n"); exit_status = NETLOC_ERROR; goto cleanup; } /* * Initialize the data structures */ // Make sure to initialize the arrays for( i = 0; i < netloc_lookup_table_size(handle->node_list); ++i){ distance[i] = INT_MAX; not_seen[i] = true; prev_node[i] = NULL; prev_edge[i] = NULL; } i = 0; hti = netloc_dt_lookup_table_iterator_t_construct(handle->node_list); while( !netloc_lookup_table_iterator_at_end(hti) ) { cur_node = (netloc_node_t*)netloc_lookup_table_iterator_next_entry(hti); if( NULL == cur_node ) { break; } if( cur_node == src_node ) { pq_push(queue, 0, cur_node); distance[i] = 0; } else { pq_push(queue, INT_MAX, cur_node); distance[i] = INT_MAX; } not_seen[i] = true; prev_node[i] = NULL; prev_edge[i] = NULL; cur_node->__uid__ = i; ++i; } /* * Search */ while( !pq_is_empty(queue) ) { //pq_dump(queue); // Grab the next hop node_u = pq_pop(queue); // Mark as seen idx_u = -1; i = 0; idx_u = node_u->__uid__; not_seen[idx_u] = false; // For all the edges from this node for(i = 0; i < node_u->num_edges; ++i ) { node_v = NULL; idx_v = -1; // Lookup the "dest" node node_v = node_u->edges[i]->dest_node; idx_v = node_v->__uid__; // If the node has been seen, skip if( !not_seen[idx_v] ) { continue; } // Otherwise check to see if we found a shorter path // Future Work: Add a weight factor other than 1. // Maybe calculated based on speed/width alt = distance[idx_u] + 1; if( alt < distance[idx_v] ) { distance[idx_v] = alt; prev_node[idx_v] = node_u; prev_edge[idx_v] = node_u->edges[i]; // Adjust the priority queue as needed pq_reorder(queue, alt, node_v); } } } /* * Reconstruct the path by picking up the edges * The edges will be in reverse order (dest to source). */ num_rev_edges = 0; rev_edges = NULL; // Find last hop SUPPORT_CONVERT_ADDR_TO_INT(dest_node->physical_id, handle->network->network_type, key_int); node_u = netloc_lookup_table_access_with_int( handle->node_list, dest_node->physical_id, key_int); idx_u = node_u->__uid__; node_v = NULL; idx_v = -1; while( prev_node[idx_u] != NULL ) { // Find the linking edge if( node_u != dest_node) { for(i = 0; i < node_u->num_edges; ++i ) { if( node_v->physical_id_int == node_u->edges[i]->dest_node->physical_id_int ) { ++num_rev_edges; rev_edges = (netloc_edge_t**)realloc(rev_edges, sizeof(netloc_edge_t*) * num_rev_edges); if( NULL == rev_edges ) { fprintf(stderr, "Error: Failed to re-allocate the 'rev_edges' array with %d elements\n", num_rev_edges); exit_status = NETLOC_ERROR; goto cleanup; } rev_edges[num_rev_edges-1] = node_u->edges[i]; break; } } } node_v = node_u; idx_v = idx_u; // Find the next node SUPPORT_CONVERT_ADDR_TO_INT(prev_node[idx_u]->physical_id, handle->network->network_type, key_int); node_u = netloc_lookup_table_access_with_int( handle->node_list, prev_node[idx_u]->physical_id, key_int); idx_u = node_u->__uid__; } for(i = 0; i < src_node->num_edges; ++i ) { if( NULL == node_v ) { fprintf(stderr, "Error: This should never happen, but node_v is NULL at line %d in file %s\n", __LINE__, __FILE__); exit_status = NETLOC_ERROR; goto cleanup; } if( node_v->physical_id_int == src_node->edges[i]->dest_node->physical_id_int ) { ++num_rev_edges; rev_edges = (netloc_edge_t**)realloc(rev_edges, sizeof(netloc_edge_t*) * num_rev_edges); if( NULL == rev_edges ) { fprintf(stderr, "Error: Failed to re-allocate the 'rev_edges' array with %d elements\n", num_rev_edges); exit_status = NETLOC_ERROR; goto cleanup; } rev_edges[num_rev_edges-1] = node_u->edges[i]; break; } } /* * Copy the edges back in correct order */ (*num_edges) = num_rev_edges; (*edges) = (netloc_edge_t**)malloc(sizeof(netloc_edge_t*) * (*num_edges)); if( NULL == (*edges) ) { fprintf(stderr, "Error: Failed to allocate the edges array\n"); exit_status = NETLOC_ERROR; goto cleanup; } for( i = 0; i < num_rev_edges; ++i ) { (*edges)[i] = rev_edges[num_rev_edges-1-i]; //printf("DEBUG: \t Edge: %s\n", netloc_pretty_print_edge_t( (*edges)[i] ) ); } /* * Cleanup */ cleanup: if( NULL != queue ) { pq_queue_t_destruct(queue); queue = NULL; } if( NULL != rev_edges ) { free(rev_edges); rev_edges = NULL; } if( NULL != distance ) { free(distance); distance = NULL; } if( NULL != not_seen ) { free(not_seen); not_seen = NULL; } if( NULL != prev_node ) { free(prev_node); prev_node = NULL; } if( NULL != prev_edge ) { free(prev_edge); prev_edge = NULL; } netloc_dt_lookup_table_iterator_t_destruct(hti); return exit_status; }
static int check_dat_files() { int ret, exit_status = NETLOC_SUCCESS; char * spec = NULL; char * hosts_filename = NULL; char * switches_filename = NULL; char *search_uri = NULL; netloc_topology_t topology; netloc_dt_lookup_table_t nodes = NULL; netloc_dt_lookup_table_iterator_t hti = NULL; const char * key = NULL; netloc_node_t *node = NULL; int num_bad = 0; netloc_network_t *network = NULL; int total_num_edges = 0; printf("Status: Validating the output...\n"); network = netloc_dt_network_t_construct(); network->network_type = NETLOC_NETWORK_TYPE_ETHERNET; network->subnet_id = strdup(subnet); asprintf(&search_uri, "file://%s/", outdir); if( NETLOC_SUCCESS != (ret = netloc_find_network(search_uri, network)) ) { fprintf(stderr, "Error: Cannot find the network data. (%d)\n", ret); exit_status = ret; goto cleanup; } /* * Attach to Netloc */ if( NETLOC_SUCCESS != (ret = netloc_attach(&topology, *network)) ) { fprintf(stderr, "Error: Cannot attach the topology\n"); exit_status = ret; goto cleanup; } /* * Check the 'hosts' */ ret = netloc_get_all_host_nodes(topology, &nodes); if( NETLOC_SUCCESS != ret ) { fprintf(stderr, "Error: Cannot access the list of hosts!\n"); exit_status = ret; goto cleanup; } printf("\tNumber of hosts : %4d\n", netloc_lookup_table_size(nodes) ); hti = netloc_dt_lookup_table_iterator_t_construct( nodes ); while( !netloc_lookup_table_iterator_at_end(hti) ) { key = netloc_lookup_table_iterator_next_key(hti); if( NULL == key ) { break; } node = (netloc_node_t*)netloc_lookup_table_access(nodes, key); if( NETLOC_NODE_TYPE_INVALID == node->node_type ) { printf("Host Node: %s is invalid\n", netloc_pretty_print_node_t(node) ); num_bad++; } else { total_num_edges += node->num_edges; } } netloc_dt_lookup_table_iterator_t_destruct(hti); netloc_lookup_table_destroy(nodes); free(nodes); /* * Check 'switches' */ ret = netloc_get_all_switch_nodes(topology, &nodes); if( NETLOC_SUCCESS != ret ) { fprintf(stderr, "Error: Cannot access the list of switches!\n"); exit_status = ret; goto cleanup; } printf("\tNumber of switches: %4d\n", netloc_lookup_table_size(nodes) ); hti = netloc_dt_lookup_table_iterator_t_construct( nodes ); while( !netloc_lookup_table_iterator_at_end(hti) ) { key = netloc_lookup_table_iterator_next_key(hti); if( NULL == key ) { break; } node = (netloc_node_t*)netloc_lookup_table_access(nodes, key); if( NETLOC_NODE_TYPE_INVALID == node->node_type ) { printf("Switch Node: %s is invalid\n", netloc_pretty_print_node_t(node) ); num_bad++; } else { total_num_edges += node->num_edges; } } netloc_dt_lookup_table_iterator_t_destruct(hti); netloc_lookup_table_destroy(nodes); free(nodes); if( num_bad > 0 ) { fprintf(stderr, "Error: Found %2d malformed nodes in the .dat files\n", num_bad); exit_status = NETLOC_ERROR; } printf("\tNumber of edges : %4d\n", total_num_edges ); /* * Cleanup */ if( NETLOC_SUCCESS != (ret = netloc_detach(topology)) ) { fprintf(stderr, "Error: Failed to detach the topology\n"); exit_status = ret; goto cleanup; } cleanup: netloc_dt_network_t_destruct(network); free(search_uri); free(spec); free(hosts_filename); free(switches_filename); return exit_status; }