int main(void) { int i, num_uris = 1; char **search_uris = NULL; int ret, exit_status = NETLOC_SUCCESS; int num_all_networks = 0; netloc_network_t **all_networks = NULL; netloc_topology_t topology; netloc_dt_lookup_table_t nodes = NULL; netloc_dt_lookup_table_iterator_t hti = NULL; netloc_node_t *node = NULL; /* * Where to search for network topology information. * Information generated from a netloc reader. */ search_uris = (char**)malloc(sizeof(char*) * num_uris ); if( NULL == search_uris ) { return NETLOC_ERROR; } search_uris[0] = strdup("file://data/netloc"); /* * Find all of the networks in the specified serach URI locations */ ret = netloc_foreach_network((const char * const *) search_uris, num_uris, NULL, // Callback function (NULL = include all networks) NULL, // Callback function data &num_all_networks, &all_networks); if( NETLOC_SUCCESS != ret ) { fprintf(stderr, "Error: netloc_foreach_network returned an error (%d)\n", ret); exit_status = ret; goto cleanup; } /* * For each of those networks access the detailed topology */ for(i = 0; i < num_all_networks; ++i ) { // Pretty print the network for debugging purposes printf("\tIncluded Network: %s\n", netloc_pretty_print_network_t(all_networks[i]) ); /* * Attach to the network */ ret = netloc_attach(&topology, *(all_networks[i])); if( NETLOC_SUCCESS != ret ) { fprintf(stderr, "Error: netloc_attach returned an error (%d)\n", ret); return ret; } /* * Access all of the nodes in the topology */ ret = netloc_get_all_nodes(topology, &nodes); if( NETLOC_SUCCESS != ret ) { fprintf(stderr, "Error: get_all_nodes returned %d\n", ret); return ret; } // Display all of the nodes found hti = netloc_dt_lookup_table_iterator_t_construct( nodes ); while( !netloc_lookup_table_iterator_at_end(hti) ) { node = netloc_lookup_table_iterator_next_entry(hti); if( NULL == node ) { break; } if( NETLOC_NODE_TYPE_INVALID == node->node_type ) { fprintf(stderr, "Error: Returned unexpected node: %s\n", netloc_pretty_print_node_t(node)); return NETLOC_ERROR; } printf("Found: %s\n", netloc_pretty_print_node_t(node)); } /* Cleanup the lookup table objects */ if( NULL != hti ) { netloc_dt_lookup_table_iterator_t_destruct(hti); hti = NULL; } if( NULL != nodes ) { netloc_lookup_table_destroy(nodes); free(nodes); nodes = NULL; } /* * Detach from the network */ ret = netloc_detach(topology); if( NETLOC_SUCCESS != ret ) { fprintf(stderr, "Error: netloc_detach returned an error (%d)\n", ret); return ret; } } /* * Cleanup */ cleanup: if( NULL != hti ) { netloc_dt_lookup_table_iterator_t_destruct(hti); hti = NULL; } if( NULL != nodes ) { netloc_lookup_table_destroy(nodes); free(nodes); nodes = NULL; } if( NULL != all_networks ) { for(i = 0; i < num_all_networks; ++i ) { netloc_dt_network_t_destruct(all_networks[i]); all_networks[i] = NULL; } free(all_networks); all_networks = NULL; } if( NULL != search_uris ) { for(i = 0; i < num_uris; ++i) { free(search_uris[i]); search_uris[i] = NULL; } free(search_uris); search_uris = NULL; } return NETLOC_SUCCESS; }
int netloc_find_network(const char * network_topo_uri, netloc_network_t* network) { int ret, exit_status = NETLOC_SUCCESS; netloc_network_t **all_networks = NULL; int i, num_networks; int num_found = 0; netloc_network_t *last_found = NULL; /* * Find all of the network information at this URI */ num_networks = 0; ret = search_uri(network_topo_uri, NULL, NULL, &num_networks, &all_networks); if( NETLOC_SUCCESS != ret ) { fprintf(stderr, "Error: Failed to search the uri: %s\n", network_topo_uri); return ret; } /* * Compare the networks to see if they match */ num_found = 0; for(i = 0; i < num_networks; ++i) { if( NETLOC_NETWORK_TYPE_INVALID != network->network_type ) { if(all_networks[i]->network_type != network->network_type) { continue; } } if( NULL != network->subnet_id ) { if( 0 != strncmp(all_networks[i]->subnet_id, network->subnet_id, strlen(all_networks[i]->subnet_id)) ) { continue; } } ++num_found; last_found = all_networks[i]; } /* * Determine if we found too much or too little */ if( num_found == 0 ) { exit_status = NETLOC_ERROR_EMPTY; goto cleanup; } if( num_found > 1 ) { exit_status = NETLOC_ERROR_MULTIPLE; goto cleanup; } /* * If we found exactly one then copy the information into the handle */ netloc_dt_network_t_copy(last_found, network); /* * Cleanup */ cleanup: for(i = 0; i < num_networks; ++i) { if( NULL != all_networks[i] ) { netloc_dt_network_t_destruct(all_networks[i]); all_networks[i] = NULL; } } free(all_networks); return exit_status; }
int main(void) { char **search_uris = NULL; int num_uris = 1, ret; netloc_network_t *tmp_network = 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; // Specify where to search for network data search_uris = (char**)malloc(sizeof(char*) * num_uris ); search_uris[0] = strdup("file://data/netloc"); // Find a specific InfiniBand network tmp_network = netloc_dt_network_t_construct(); tmp_network->network_type = NETLOC_NETWORK_TYPE_INFINIBAND; tmp_network->subnet_id = strdup("fe80:0000:0000:0000"); // Search for the specific network ret = netloc_find_network(search_uris[0], tmp_network); if( NETLOC_SUCCESS != ret ) { fprintf(stderr, "Error: network not found!\n"); netloc_dt_network_t_destruct(tmp_network); return NETLOC_ERROR; } printf("\tFound Network: %s\n", netloc_pretty_print_network_t(tmp_network)); // Attach to the network ret = netloc_attach(&topology, *tmp_network); if( NETLOC_SUCCESS != ret ) { fprintf(stderr, "Error: netloc_attach returned an error (%d)\n", ret); return ret; } // Query the network topology // Access all of the nodes in the topology ret = netloc_get_all_nodes(topology, &nodes); if( NETLOC_SUCCESS != ret ) { fprintf(stderr, "Error: get_all_nodes returned %d\n", ret); return ret; } // Display all of the nodes found hti = netloc_dt_lookup_table_iterator_t_construct( nodes ); while( !netloc_lookup_table_iterator_at_end(hti) ) { // Access the data by key (could also access by entry in the example) 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 ) { fprintf(stderr, "Error: Returned unexpected node: %s\n", netloc_pretty_print_node_t(node)); return NETLOC_ERROR; } printf("Found: %s\n", netloc_pretty_print_node_t(node)); } /* Cleanup the lookup table objects */ if( NULL != hti ) { netloc_dt_lookup_table_iterator_t_destruct(hti); hti = NULL; } if( NULL != nodes ) { netloc_lookup_table_destroy(nodes); free(nodes); nodes = NULL; } // Detach from the network ret = netloc_detach(topology); if( NETLOC_SUCCESS != ret ) { fprintf(stderr, "Error: netloc_detach returned an error (%d)\n", ret); return ret; } /* * Cleanup */ netloc_dt_network_t_destruct(tmp_network); tmp_network = NULL; return NETLOC_SUCCESS; }
static int search_uri(const char * search_uri, int (*func)(const netloc_network_t *network, void *funcdata), void *funcdata, int *num_networks, netloc_network_t ***networks) { int ret, i; netloc_network_t *tmp_network = NULL; netloc_network_t **all_networks = NULL; int all_num_networks; char * uri_str = NULL; uri_type_t uri_type; char * filename = NULL; DIR *dirp = NULL; struct stat dstat; struct dirent *dir_entry = NULL; bool found; /* * Process the URI */ ret = support_extract_filename_from_uri(search_uri, &uri_type, &uri_str); if( NETLOC_SUCCESS != ret ) { fprintf(stderr, "Error: Malformed URI <%s>.\n", search_uri); return ret; } if( URI_FILE != uri_type ) { fprintf(stderr, "Error: Unsupported protocol in URI <%s>.\n", search_uri); return NETLOC_ERROR; } /* * Check to make sure we are looking at a directory */ ret = stat(uri_str, &dstat); if( 0 != ret ) { fprintf(stderr, "Error: Cannot stat the directory <%s>.\n", uri_str); return NETLOC_ERROR; } if( !(dstat.st_mode & S_IFDIR) ) { fprintf(stderr, "Error: The URI does not point to a directory <%s>.\n", uri_str); return NETLOC_ERROR_NOTDIR; } /* * Search the directory */ dirp = opendir(uri_str); if( NULL == dirp ) { fprintf(stderr, "Error: Cannot open the directory <%s>.\n", uri_str); return NETLOC_ERROR_NOENT; } all_num_networks = 0; while( NULL != (dir_entry = readdir(dirp)) ) { /* * Skip directories */ if( DT_DIR == dir_entry->d_type ) { continue; } /* * Skip if does not end in .ndat extension */ if( NULL == strstr(dir_entry->d_name, ".ndat") ) { continue; } /* * Extract the network metadata from the file */ asprintf(&filename, "%s%s", uri_str, dir_entry->d_name); tmp_network = extract_network_info(filename); if( NULL == tmp_network ) { fprintf(stderr, "Error: Failed to extract network data from the file %s\n", filename); continue; } /* * Determine file type: nodes or paths */ if( NULL != strstr(filename, "nodes.ndat") ) { tmp_network->node_uri = strdup(filename); } else if( NULL != strstr(filename, "phy-paths.ndat") ) { tmp_network->phy_path_uri = strdup(filename); } else if( NULL != strstr(filename, "log-paths.ndat") ) { tmp_network->path_uri = strdup(filename); } asprintf(&tmp_network->data_uri, "%s%s", URI_PREFIX_FILE, uri_str); /* * Have we seen this network before? */ found = false; for( i = 0; i < all_num_networks; ++i ) { if( NETLOC_CMP_SAME == netloc_dt_network_t_compare(all_networks[i], tmp_network) ) { found = true; break; } } /* * Append only the unique networks */ if( !found ) { all_num_networks += 1; all_networks = (netloc_network_t**)realloc(all_networks, sizeof(netloc_network_t*)*all_num_networks); if( NULL == all_networks ) { fprintf(stderr, "Error: Failed to allocate space for %d networks\n", all_num_networks); return NETLOC_ERROR; } all_networks[all_num_networks-1] = tmp_network; } else { // If not unique, then we may still need to copy over the filenames // Determine file type: nodes or paths if( NULL != strstr(filename, "nodes.ndat") ) { all_networks[i]->node_uri = strdup(filename); } else if( NULL != strstr(filename, "phy-paths.ndat") ) { all_networks[i]->phy_path_uri = strdup(filename); } else if( NULL != strstr(filename, "log-paths.ndat") ) { all_networks[i]->path_uri = strdup(filename); } netloc_dt_network_t_destruct(tmp_network); } if( NULL != filename ) { free(filename); filename = NULL; } } /* * From those unique networks, ask the user if they should be included * in the final vector returned */ for( i = 0; i < all_num_networks; ++i ) { ret = -1; /* * Call the user callback function to decide if we should * include this in the vector returned. */ if( NULL != func ) { ret = func(all_networks[i], funcdata); } if( 0 != ret ) { // Note: we could be extending an existing networks array, so make sure not to clear that memory. (*num_networks)++; (*networks) = (netloc_network_t**)realloc((*networks), sizeof(netloc_network_t*)*(*num_networks)); if( NULL == (*networks) ) { return NETLOC_ERROR; } (*networks)[(*num_networks)-1] = all_networks[i]; } else { netloc_dt_network_t_destruct(all_networks[i]); all_networks[i] = NULL; } } /* * Cleanup */ closedir(dirp); dirp = NULL; free(uri_str); if( NULL != all_networks ) { free(all_networks); all_networks = NULL; } return NETLOC_SUCCESS; }
int netloc_dt_data_collection_handle_t_destruct(netloc_data_collection_handle_t *handle) { struct netloc_dt_lookup_table_iterator *hti = NULL; netloc_node_t *cur_node = NULL; netloc_edge_t *cur_edge = NULL; if( NULL != handle->network ) { netloc_dt_network_t_destruct(handle->network); handle->network = NULL; } handle->is_open = false; handle->is_read_only = false; if( NULL != handle->unique_id_str ) { free(handle->unique_id_str); handle->unique_id_str = NULL; } if( NULL != handle->data_uri ) { free(handle->data_uri); handle->data_uri = NULL; } if( NULL != handle->filename_nodes ) { free(handle->filename_nodes); handle->filename_nodes = NULL; } if( NULL != handle->filename_physical_paths ) { free(handle->filename_physical_paths); handle->filename_physical_paths = NULL; } if( NULL != handle->filename_logical_paths ) { free(handle->filename_logical_paths); handle->filename_logical_paths = NULL; } if( NULL != handle->node_list ) { // Make sure to free all of the nodes pointed to in the lookup table 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; } netloc_dt_node_t_destruct(cur_node); } netloc_dt_lookup_table_iterator_t_destruct(hti); netloc_lookup_table_destroy(handle->node_list); free(handle->node_list); handle->node_list = NULL; } if( NULL != handle->edges ) { // Make sure to free all of the edges pointed to in the lookup table hti = netloc_dt_lookup_table_iterator_t_construct(handle->edges); while( !netloc_lookup_table_iterator_at_end(hti) ) { cur_edge = (netloc_edge_t*)netloc_lookup_table_iterator_next_entry(hti); if( NULL == cur_edge ) { break; } netloc_dt_edge_t_destruct(cur_edge); } netloc_dt_lookup_table_iterator_t_destruct(hti); netloc_lookup_table_destroy(handle->edges); free(handle->edges); handle->edges = NULL; } if( NULL != handle->node_data ) { json_decref(handle->node_data); handle->node_data = NULL; // Implied decref of handle->node_data_acc } if( NULL != handle->path_data ) { json_decref(handle->path_data); handle->path_data = NULL; // Implied decref of handle->path_data_acc } free( handle ); return NETLOC_SUCCESS; }
int main(void) { int ret, exit_status = NETLOC_SUCCESS; int i; netloc_topology_t topology; netloc_network_t *network = NULL; char *search_uri = NULL; netloc_dt_lookup_table_t *nodes = NULL; netloc_node_t *node = NULL; int num_edges; netloc_edge_t **edges = NULL; netloc_node_t *src_node = NULL; char * phy_id = NULL; char * tmp_str = NULL; phy_id = strdup("0002:c903:0002:a0eb"); /* * Setup a Network connection */ network = netloc_dt_network_t_construct(); network->network_type = NETLOC_NETWORK_TYPE_INFINIBAND; network->subnet_id = strdup("fe80:0000:0000:0000"); search_uri = strdup("file://data/"); ret = netloc_find_network(search_uri, network); if( NETLOC_SUCCESS != ret ) { fprintf(stderr, "Error: netloc_find_network returned an error (%d)\n", ret); return ret; } /* * Attach to the topology context */ printf("Test attach: "); fflush(NULL); ret = netloc_attach(&topology, *network); if( NETLOC_SUCCESS != ret ) { fprintf(stderr, "Error: netloc_attach returned an error (%d)\n", ret); return ret; } netloc_dt_network_t_destruct(network); network = NULL; printf("Success\n"); /* * Get all the verticies */ ret = netloc_get_all_nodes(topology, &nodes); if( NETLOC_SUCCESS != ret ) { fprintf(stderr, "Error: get_all_nodes returned %d\n", ret); exit_status = ret; goto cleanup; } /* * Get all of the edges */ src_node = netloc_get_node_by_physical_id(topology, phy_id); if( NULL == src_node ) { fprintf(stderr, "Error: Failed to in the node associated with the physical id %s\n", phy_id); return NETLOC_ERROR; } ret = netloc_get_all_edges(topology, src_node, &num_edges, &edges); if( NETLOC_SUCCESS != ret ) { tmp_str = netloc_pretty_print_node_t(node); fprintf(stderr, "Error: get_all_edges_by_id returned %d for node %s\n", ret, tmp_str); free(tmp_str); return ret; } /* * Display the data */ tmp_str = netloc_pretty_print_node_t(src_node); printf("Neighbors of %s\n", tmp_str); free(tmp_str); for(i = 0; i < num_edges; ++i ) { node = netloc_get_node_by_physical_id(topology, edges[i]->dest_node_id); printf("%s ( from port %3s to port %3s, type: %6s) has %3d directed edge(s)\n", node->physical_id, edges[i]->src_port_id, edges[i]->dest_port_id, (NETLOC_NODE_TYPE_SWITCH == node->node_type ? "switch" : "host"), node->num_edges); } /* Cleanup */ netloc_lookup_table_destroy(nodes); free(nodes); cleanup: /* * Cleanup */ ret = netloc_detach(topology); if( NETLOC_SUCCESS != ret ) { fprintf(stderr, "Error: netloc_detach returned an error (%d)\n", ret); return ret; } if( NULL != phy_id ) { free(phy_id); phy_id = NULL; } if( NULL != search_uri ) { free(search_uri); search_uri = NULL; } 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; }
int main(int argc, char ** argv) { int ret, exit_status = NETLOC_SUCCESS; int i; netloc_network_t *network = NULL; netloc_data_collection_handle_t *dc_handle = NULL; /* * Parse Args */ if( 0 != parse_args(argc, argv) ) { printf("Usage: %s %s|%s <controller> [%s|%s <subnet id>] [%s|%s <output directory>] [%s|%s <URL Address:Port>] [%s|%s <username>] [%s|%s <password>] [%s|%s]\n", argv[0], ARG_CONTROLLER, ARG_SHORT_CONTROLLER, ARG_SUBNET, ARG_SHORT_SUBNET, ARG_OUTDIR, ARG_SHORT_OUTDIR, ARG_ADDRESS, ARG_SHORT_ADDRESS, ARG_AUTH_USER, ARG_SHORT_AUTH_USER, ARG_AUTH_PASS, ARG_SHORT_AUTH_PASS, ARG_HELP, ARG_SHORT_HELP); printf(" Default %-10s = \"unknown\"\n", ARG_SUBNET); printf(" Default %-10s = \"127.0.0.1:8080\"\n", ARG_ADDRESS); printf(" Default %-10s = current working directory\n", ARG_OUTDIR); printf(" Valid Options for %s:\n", ARG_CONTROLLER ); // Note: Hide 'noop' since it is only meant for debugging, and not for normal use for(i = 1; i < num_valid_controllers; ++i) { printf("\t\t%s\n", valid_controllers[i] ); } return NETLOC_ERROR; } /* * Run the parser requested */ if( 0 != (ret = run_parser() ) ) { return ret; } /* * Setup network information */ network = netloc_dt_network_t_construct(); ret = extract_network_info_from_json_file(network, out_file_nodes); if( NETLOC_SUCCESS != ret ) { fprintf(stderr, "Error: Failed to extract network information from the file: %s\n", out_file_nodes); return ret; } dc_handle = netloc_dc_create(network, outdir); if( NULL == dc_handle ) { fprintf(stderr, "Error: Failed to create a new data file\n"); return ret; } netloc_dt_network_t_destruct(network); network = NULL; /* * Convert the temporary node file to the proper format */ if( 0 != (ret = convert_nodes_file(dc_handle)) ) { exit_status = ret; goto cleanup; } /* * Find all physical paths */ if( 0 != (ret = compute_physical_paths(dc_handle)) ) { exit_status = ret; goto cleanup; } cleanup: /* * Close the handle */ ret = netloc_dc_close(dc_handle); if( NETLOC_SUCCESS != ret ) { fprintf(stderr, "Error: Failed to close the data connection!\n"); return ret; } netloc_dt_data_collection_handle_t_destruct(dc_handle); dc_handle = NULL; /* * Validate the resulting .dat files */ if( 0 == exit_status ) { if( 0 != (ret = check_dat_files() ) ) { return ret; } } free(outdir); free(out_file_nodes); free(subnet); free(uri_address); free(auth_username); free(auth_password); return exit_status; }