1219820Sjeff/* 2219820Sjeff * Copyright (c) 2004-2007 Voltaire, Inc. All rights reserved. 3219820Sjeff * Copyright (c) 2002-2006 Mellanox Technologies LTD. All rights reserved. 4219820Sjeff * Copyright (c) 1996-2003 Intel Corporation. All rights reserved. 5219820Sjeff * 6219820Sjeff * This software is available to you under a choice of one of two 7219820Sjeff * licenses. You may choose to be licensed under the terms of the GNU 8219820Sjeff * General Public License (GPL) Version 2, available from the file 9219820Sjeff * COPYING in the main directory of this source tree, or the 10219820Sjeff * OpenIB.org BSD license below: 11219820Sjeff * 12219820Sjeff * Redistribution and use in source and binary forms, with or 13219820Sjeff * without modification, are permitted provided that the following 14219820Sjeff * conditions are met: 15219820Sjeff * 16219820Sjeff * - Redistributions of source code must retain the above 17219820Sjeff * copyright notice, this list of conditions and the following 18219820Sjeff * disclaimer. 19219820Sjeff * 20219820Sjeff * - Redistributions in binary form must reproduce the above 21219820Sjeff * copyright notice, this list of conditions and the following 22219820Sjeff * disclaimer in the documentation and/or other materials 23219820Sjeff * provided with the distribution. 24219820Sjeff * 25219820Sjeff * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 26219820Sjeff * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27219820Sjeff * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 28219820Sjeff * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 29219820Sjeff * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 30219820Sjeff * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 31219820Sjeff * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32219820Sjeff * SOFTWARE. 33219820Sjeff * 34219820Sjeff */ 35219820Sjeff 36219820Sjeff#if HAVE_CONFIG_H 37219820Sjeff# include <config.h> 38219820Sjeff#endif /* HAVE_CONFIG_H */ 39219820Sjeff 40219820Sjeff#include <stdlib.h> 41219820Sjeff#include <complib/cl_debug.h> 42219820Sjeff#include <opensm/osm_db_pack.h> 43219820Sjeff 44219820Sjeffstatic inline void __osm_pack_guid(uint64_t guid, char *p_guid_str) 45219820Sjeff{ 46219820Sjeff sprintf(p_guid_str, "0x%016" PRIx64, guid); 47219820Sjeff} 48219820Sjeff 49219820Sjeffstatic inline uint64_t __osm_unpack_guid(char *p_guid_str) 50219820Sjeff{ 51219820Sjeff return strtoull(p_guid_str, NULL, 0); 52219820Sjeff} 53219820Sjeff 54219820Sjeffstatic inline void 55219820Sjeff__osm_pack_lids(uint16_t min_lid, uint16_t max_lid, char *p_lid_str) 56219820Sjeff{ 57219820Sjeff sprintf(p_lid_str, "0x%04x 0x%04x", min_lid, max_lid); 58219820Sjeff} 59219820Sjeff 60219820Sjeffstatic inline int 61219820Sjeff__osm_unpack_lids(IN char *p_lid_str, 62219820Sjeff OUT uint16_t * p_min_lid, OUT uint16_t * p_max_lid) 63219820Sjeff{ 64219820Sjeff unsigned long tmp; 65219820Sjeff char *p_next; 66219820Sjeff char *p_num; 67219820Sjeff char lids_str[24]; 68219820Sjeff 69219820Sjeff strncpy(lids_str, p_lid_str, 23); 70219820Sjeff lids_str[23] = '\0'; 71219820Sjeff p_num = strtok_r(lids_str, " \t", &p_next); 72219820Sjeff if (!p_num) 73219820Sjeff return 1; 74219820Sjeff tmp = strtoul(p_num, NULL, 0); 75219820Sjeff CL_ASSERT(tmp < 0x10000); 76219820Sjeff *p_min_lid = (uint16_t) tmp; 77219820Sjeff 78219820Sjeff p_num = strtok_r(NULL, " \t", &p_next); 79219820Sjeff if (!p_num) 80219820Sjeff return 1; 81219820Sjeff tmp = strtoul(p_num, NULL, 0); 82219820Sjeff CL_ASSERT(tmp < 0x10000); 83219820Sjeff *p_max_lid = (uint16_t) tmp; 84219820Sjeff 85219820Sjeff return 0; 86219820Sjeff} 87219820Sjeff 88219820Sjeffint 89219820Sjeffosm_db_guid2lid_guids(IN osm_db_domain_t * const p_g2l, 90219820Sjeff OUT cl_qlist_t * p_guid_list) 91219820Sjeff{ 92219820Sjeff char *p_key; 93219820Sjeff cl_list_t keys; 94219820Sjeff osm_db_guid_elem_t *p_guid_elem; 95219820Sjeff 96219820Sjeff cl_list_construct(&keys); 97219820Sjeff cl_list_init(&keys, 10); 98219820Sjeff 99219820Sjeff if (osm_db_keys(p_g2l, &keys)) 100219820Sjeff return 1; 101219820Sjeff 102219820Sjeff while ((p_key = cl_list_remove_head(&keys)) != NULL) { 103219820Sjeff p_guid_elem = 104219820Sjeff (osm_db_guid_elem_t *) malloc(sizeof(osm_db_guid_elem_t)); 105219820Sjeff CL_ASSERT(p_guid_elem != NULL); 106219820Sjeff 107219820Sjeff p_guid_elem->guid = __osm_unpack_guid(p_key); 108219820Sjeff cl_qlist_insert_head(p_guid_list, &p_guid_elem->item); 109219820Sjeff } 110219820Sjeff 111219820Sjeff cl_list_destroy(&keys); 112219820Sjeff return 0; 113219820Sjeff} 114219820Sjeff 115219820Sjeffint 116219820Sjeffosm_db_guid2lid_get(IN osm_db_domain_t * const p_g2l, 117219820Sjeff IN uint64_t guid, 118219820Sjeff OUT uint16_t * p_min_lid, OUT uint16_t * p_max_lid) 119219820Sjeff{ 120219820Sjeff char guid_str[20]; 121219820Sjeff char *p_lid_str; 122219820Sjeff uint16_t min_lid, max_lid; 123219820Sjeff 124219820Sjeff __osm_pack_guid(guid, guid_str); 125219820Sjeff p_lid_str = osm_db_lookup(p_g2l, guid_str); 126219820Sjeff if (!p_lid_str) 127219820Sjeff return 1; 128219820Sjeff if (__osm_unpack_lids(p_lid_str, &min_lid, &max_lid)) 129219820Sjeff return 1; 130219820Sjeff 131219820Sjeff if (p_min_lid) 132219820Sjeff *p_min_lid = min_lid; 133219820Sjeff if (p_max_lid) 134219820Sjeff *p_max_lid = max_lid; 135219820Sjeff 136219820Sjeff return 0; 137219820Sjeff} 138219820Sjeff 139219820Sjeffint 140219820Sjeffosm_db_guid2lid_set(IN osm_db_domain_t * const p_g2l, 141219820Sjeff IN uint64_t guid, IN uint16_t min_lid, IN uint16_t max_lid) 142219820Sjeff{ 143219820Sjeff char guid_str[20]; 144219820Sjeff char lid_str[16]; 145219820Sjeff 146219820Sjeff __osm_pack_guid(guid, guid_str); 147219820Sjeff __osm_pack_lids(min_lid, max_lid, lid_str); 148219820Sjeff 149219820Sjeff return (osm_db_update(p_g2l, guid_str, lid_str)); 150219820Sjeff} 151219820Sjeff 152219820Sjeffint osm_db_guid2lid_delete(IN osm_db_domain_t * const p_g2l, IN uint64_t guid) 153219820Sjeff{ 154219820Sjeff char guid_str[20]; 155219820Sjeff __osm_pack_guid(guid, guid_str); 156219820Sjeff return (osm_db_delete(p_g2l, guid_str)); 157219820Sjeff} 158