]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/ofed/management/opensm/opensm/osm_db_pack.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / ofed / management / opensm / opensm / osm_db_pack.c
1 /*
2  * Copyright (c) 2004-2007 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.
5  *
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:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
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.
24  *
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
32  * SOFTWARE.
33  *
34  */
35
36 #if HAVE_CONFIG_H
37 #  include <config.h>
38 #endif                          /* HAVE_CONFIG_H */
39
40 #include <stdlib.h>
41 #include <complib/cl_debug.h>
42 #include <opensm/osm_db_pack.h>
43
44 static inline void __osm_pack_guid(uint64_t guid, char *p_guid_str)
45 {
46         sprintf(p_guid_str, "0x%016" PRIx64, guid);
47 }
48
49 static inline uint64_t __osm_unpack_guid(char *p_guid_str)
50 {
51         return strtoull(p_guid_str, NULL, 0);
52 }
53
54 static inline void
55 __osm_pack_lids(uint16_t min_lid, uint16_t max_lid, char *p_lid_str)
56 {
57         sprintf(p_lid_str, "0x%04x 0x%04x", min_lid, max_lid);
58 }
59
60 static inline int
61 __osm_unpack_lids(IN char *p_lid_str,
62                   OUT uint16_t * p_min_lid, OUT uint16_t * p_max_lid)
63 {
64         unsigned long tmp;
65         char *p_next;
66         char *p_num;
67         char lids_str[24];
68
69         strncpy(lids_str, p_lid_str, 23);
70         lids_str[23] = '\0';
71         p_num = strtok_r(lids_str, " \t", &p_next);
72         if (!p_num)
73                 return 1;
74         tmp = strtoul(p_num, NULL, 0);
75         CL_ASSERT(tmp < 0x10000);
76         *p_min_lid = (uint16_t) tmp;
77
78         p_num = strtok_r(NULL, " \t", &p_next);
79         if (!p_num)
80                 return 1;
81         tmp = strtoul(p_num, NULL, 0);
82         CL_ASSERT(tmp < 0x10000);
83         *p_max_lid = (uint16_t) tmp;
84
85         return 0;
86 }
87
88 int
89 osm_db_guid2lid_guids(IN osm_db_domain_t * const p_g2l,
90                       OUT cl_qlist_t * p_guid_list)
91 {
92         char *p_key;
93         cl_list_t keys;
94         osm_db_guid_elem_t *p_guid_elem;
95
96         cl_list_construct(&keys);
97         cl_list_init(&keys, 10);
98
99         if (osm_db_keys(p_g2l, &keys))
100                 return 1;
101
102         while ((p_key = cl_list_remove_head(&keys)) != NULL) {
103                 p_guid_elem =
104                     (osm_db_guid_elem_t *) malloc(sizeof(osm_db_guid_elem_t));
105                 CL_ASSERT(p_guid_elem != NULL);
106
107                 p_guid_elem->guid = __osm_unpack_guid(p_key);
108                 cl_qlist_insert_head(p_guid_list, &p_guid_elem->item);
109         }
110
111         cl_list_destroy(&keys);
112         return 0;
113 }
114
115 int
116 osm_db_guid2lid_get(IN osm_db_domain_t * const p_g2l,
117                     IN uint64_t guid,
118                     OUT uint16_t * p_min_lid, OUT uint16_t * p_max_lid)
119 {
120         char guid_str[20];
121         char *p_lid_str;
122         uint16_t min_lid, max_lid;
123
124         __osm_pack_guid(guid, guid_str);
125         p_lid_str = osm_db_lookup(p_g2l, guid_str);
126         if (!p_lid_str)
127                 return 1;
128         if (__osm_unpack_lids(p_lid_str, &min_lid, &max_lid))
129                 return 1;
130
131         if (p_min_lid)
132                 *p_min_lid = min_lid;
133         if (p_max_lid)
134                 *p_max_lid = max_lid;
135
136         return 0;
137 }
138
139 int
140 osm_db_guid2lid_set(IN osm_db_domain_t * const p_g2l,
141                     IN uint64_t guid, IN uint16_t min_lid, IN uint16_t max_lid)
142 {
143         char guid_str[20];
144         char lid_str[16];
145
146         __osm_pack_guid(guid, guid_str);
147         __osm_pack_lids(min_lid, max_lid, lid_str);
148
149         return (osm_db_update(p_g2l, guid_str, lid_str));
150 }
151
152 int osm_db_guid2lid_delete(IN osm_db_domain_t * const p_g2l, IN uint64_t guid)
153 {
154         char guid_str[20];
155         __osm_pack_guid(guid, guid_str);
156         return (osm_db_delete(p_g2l, guid_str));
157 }