2 * Copyright (c) 2004-2009 Voltaire, Inc. All rights reserved.
3 * Copyright (c) 2002-2006 Mellanox Technologies LTD. All rights reserved.
4 * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38 #endif /* HAVE_CONFIG_H */
41 #include <complib/cl_debug.h>
42 #include <opensm/osm_file_ids.h>
43 #define FILE_ID OSM_FILE_DB_PACK_C
44 #include <opensm/osm_db_pack.h>
46 static inline void pack_guid(uint64_t guid, char *p_guid_str)
48 sprintf(p_guid_str, "0x%016" PRIx64, guid);
51 static inline uint64_t unpack_guid(char *p_guid_str)
53 return strtoull(p_guid_str, NULL, 0);
56 static inline void pack_lids(uint16_t min_lid, uint16_t max_lid, char *lid_str)
58 sprintf(lid_str, "0x%04x 0x%04x", min_lid, max_lid);
61 static inline int unpack_lids(IN char *p_lid_str, OUT uint16_t * p_min_lid,
62 OUT uint16_t * p_max_lid)
69 strncpy(lids_str, p_lid_str, 23);
71 p_num = strtok_r(lids_str, " \t", &p_next);
74 tmp = strtoul(p_num, NULL, 0);
78 *p_min_lid = (uint16_t) tmp;
80 p_num = strtok_r(NULL, " \t", &p_next);
83 tmp = strtoul(p_num, NULL, 0);
87 *p_max_lid = (uint16_t) tmp;
92 static inline void pack_mkey(uint64_t mkey, char *p_mkey_str)
94 sprintf(p_mkey_str, "0x%016" PRIx64, mkey);
97 static inline uint64_t unpack_mkey(char *p_mkey_str)
99 return strtoull(p_mkey_str, NULL, 0);
102 static inline void pack_neighbor(uint64_t guid, uint8_t portnum, char *p_str)
104 sprintf(p_str, "0x%016" PRIx64 ":%u", guid, portnum);
107 static inline int unpack_neighbor(char *p_str, uint64_t *guid,
111 char *p_num, *p_next;
112 unsigned long tmp_port;
114 strncpy(tmp_str, p_str, 23);
116 p_num = strtok_r(tmp_str, ":", &p_next);
120 *guid = strtoull(p_num, NULL, 0);
122 p_num = strtok_r(NULL, ":", &p_next);
126 tmp_port = strtoul(p_num, NULL, 0);
127 CL_ASSERT(tmp_port < 0x100);
128 *portnum = (uint8_t) tmp_port;
134 int osm_db_guid2lid_guids(IN osm_db_domain_t * p_g2l,
135 OUT cl_qlist_t * p_guid_list)
139 osm_db_guid_elem_t *p_guid_elem;
141 cl_list_construct(&keys);
142 cl_list_init(&keys, 10);
144 if (osm_db_keys(p_g2l, &keys))
147 while ((p_key = cl_list_remove_head(&keys)) != NULL) {
149 (osm_db_guid_elem_t *) malloc(sizeof(osm_db_guid_elem_t));
150 CL_ASSERT(p_guid_elem != NULL);
152 p_guid_elem->guid = unpack_guid(p_key);
153 cl_qlist_insert_head(p_guid_list, &p_guid_elem->item);
156 cl_list_destroy(&keys);
160 int osm_db_guid2lid_get(IN osm_db_domain_t * p_g2l, IN uint64_t guid,
161 OUT uint16_t * p_min_lid, OUT uint16_t * p_max_lid)
165 uint16_t min_lid, max_lid;
167 pack_guid(guid, guid_str);
168 p_lid_str = osm_db_lookup(p_g2l, guid_str);
171 if (unpack_lids(p_lid_str, &min_lid, &max_lid))
175 *p_min_lid = min_lid;
177 *p_max_lid = max_lid;
182 int osm_db_guid2lid_set(IN osm_db_domain_t * p_g2l, IN uint64_t guid,
183 IN uint16_t min_lid, IN uint16_t max_lid)
188 pack_guid(guid, guid_str);
189 pack_lids(min_lid, max_lid, lid_str);
191 return osm_db_update(p_g2l, guid_str, lid_str);
194 int osm_db_guid2lid_delete(IN osm_db_domain_t * p_g2l, IN uint64_t guid)
197 pack_guid(guid, guid_str);
198 return osm_db_delete(p_g2l, guid_str);
201 int osm_db_guid2mkey_guids(IN osm_db_domain_t * p_g2m,
202 OUT cl_qlist_t * p_guid_list)
206 osm_db_guid_elem_t *p_guid_elem;
208 cl_list_construct(&keys);
209 cl_list_init(&keys, 10);
211 if (osm_db_keys(p_g2m, &keys))
214 while ((p_key = cl_list_remove_head(&keys)) != NULL) {
216 (osm_db_guid_elem_t *) malloc(sizeof(osm_db_guid_elem_t));
217 CL_ASSERT(p_guid_elem != NULL);
219 p_guid_elem->guid = unpack_guid(p_key);
220 cl_qlist_insert_head(p_guid_list, &p_guid_elem->item);
223 cl_list_destroy(&keys);
227 int osm_db_guid2mkey_get(IN osm_db_domain_t * p_g2m, IN uint64_t guid,
228 OUT uint64_t * p_mkey)
233 pack_guid(guid, guid_str);
234 p_mkey_str = osm_db_lookup(p_g2m, guid_str);
239 *p_mkey = unpack_mkey(p_mkey_str);
244 int osm_db_guid2mkey_set(IN osm_db_domain_t * p_g2m, IN uint64_t guid,
250 pack_guid(guid, guid_str);
251 pack_mkey(mkey, mkey_str);
253 return osm_db_update(p_g2m, guid_str, mkey_str);
256 int osm_db_guid2mkey_delete(IN osm_db_domain_t * p_g2m, IN uint64_t guid)
259 pack_guid(guid, guid_str);
260 return osm_db_delete(p_g2m, guid_str);
263 int osm_db_neighbor_guids(IN osm_db_domain_t * p_neighbor,
264 OUT cl_qlist_t * p_neighbor_list)
268 osm_db_neighbor_elem_t *p_neighbor_elem;
270 cl_list_construct(&keys);
271 cl_list_init(&keys, 10);
273 if (osm_db_keys(p_neighbor, &keys))
276 while ((p_key = cl_list_remove_head(&keys)) != NULL) {
278 (osm_db_neighbor_elem_t *) malloc(sizeof(osm_db_neighbor_elem_t));
279 CL_ASSERT(p_neighbor_elem != NULL);
281 unpack_neighbor(p_key, &p_neighbor_elem->guid,
282 &p_neighbor_elem->portnum);
283 cl_qlist_insert_head(p_neighbor_list, &p_neighbor_elem->item);
286 cl_list_destroy(&keys);
290 int osm_db_neighbor_get(IN osm_db_domain_t * p_neighbor, IN uint64_t guid1,
291 IN uint8_t portnum1, OUT uint64_t * p_guid2,
292 OUT uint8_t * p_portnum2)
294 char neighbor_str[24];
297 uint8_t temp_portnum;
299 pack_neighbor(guid1, portnum1, neighbor_str);
300 p_other_str = osm_db_lookup(p_neighbor, neighbor_str);
303 if (unpack_neighbor(p_other_str, &temp_guid, &temp_portnum))
307 *p_guid2 = temp_guid;
309 *p_portnum2 = temp_portnum;
314 int osm_db_neighbor_set(IN osm_db_domain_t * p_neighbor, IN uint64_t guid1,
315 IN uint8_t portnum1, IN uint64_t guid2,
318 char n1_str[24], n2_str[24];
320 pack_neighbor(guid1, portnum1, n1_str);
321 pack_neighbor(guid2, portnum2, n2_str);
323 return osm_db_update(p_neighbor, n1_str, n2_str);
326 int osm_db_neighbor_delete(IN osm_db_domain_t * p_neighbor, IN uint64_t guid,
331 pack_neighbor(guid, portnum, n_str);
332 return osm_db_delete(p_neighbor, n_str);