1 /*
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.
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 /*
37  * Abstract:
38  *    Implementation of service record functions.
39  */
40 
41 #if HAVE_CONFIG_H
42 #  include <config.h>
43 #endif				/* HAVE_CONFIG_H */
44 
45 #include <stdlib.h>
46 #include <complib/cl_debug.h>
47 #include <complib/cl_timer.h>
48 #include <opensm/osm_file_ids.h>
49 #define FILE_ID OSM_FILE_SERVICE_C
50 #include <opensm/osm_service.h>
51 #include <opensm/osm_opensm.h>
52 
53 void osm_svcr_delete(IN osm_svcr_t * p_svcr)
54 {
55 	free(p_svcr);
56 }
57 
58 void osm_svcr_init(IN osm_svcr_t * p_svcr,
59 		   IN const ib_service_record_t * p_svc_rec)
60 {
61 	CL_ASSERT(p_svcr);
62 
63 	p_svcr->modified_time = cl_get_time_stamp_sec();
64 
65 	/* We track the time left for this service in
66 	   an external field to avoid extra cl_ntoh/hton
67 	   required for working with the MAD field */
68 	p_svcr->lease_period = cl_ntoh32(p_svc_rec->service_lease);
69 	p_svcr->service_record = *p_svc_rec;
70 }
71 
72 osm_svcr_t *osm_svcr_new(IN const ib_service_record_t * p_svc_rec)
73 {
74 	osm_svcr_t *p_svcr;
75 
76 	CL_ASSERT(p_svc_rec);
77 
78 	p_svcr = (osm_svcr_t *) malloc(sizeof(*p_svcr));
79 	if (p_svcr) {
80 		memset(p_svcr, 0, sizeof(*p_svcr));
81 		osm_svcr_init(p_svcr, p_svc_rec);
82 	}
83 
84 	return p_svcr;
85 }
86 
87 static cl_status_t match_rid_of_svc_rec(IN const cl_list_item_t * p_list_item,
88 					IN void *context)
89 {
90 	ib_service_record_t *p_svc_rec = (ib_service_record_t *) context;
91 	osm_svcr_t *p_svcr = (osm_svcr_t *) p_list_item;
92 
93 	if (memcmp(&p_svcr->service_record, p_svc_rec,
94 		   sizeof(p_svc_rec->service_id) +
95 		   sizeof(p_svc_rec->service_gid) +
96 		   sizeof(p_svc_rec->service_pkey)))
97 		return CL_NOT_FOUND;
98 	else
99 		return CL_SUCCESS;
100 }
101 
102 osm_svcr_t *osm_svcr_get_by_rid(IN osm_subn_t const *p_subn,
103 				IN osm_log_t * p_log,
104 				IN ib_service_record_t * p_svc_rec)
105 {
106 	cl_list_item_t *p_list_item;
107 
108 	OSM_LOG_ENTER(p_log);
109 
110 	p_list_item = cl_qlist_find_from_head(&p_subn->sa_sr_list,
111 					      match_rid_of_svc_rec, p_svc_rec);
112 	if (p_list_item == cl_qlist_end(&p_subn->sa_sr_list))
113 		p_list_item = NULL;
114 
115 	OSM_LOG_EXIT(p_log);
116 	return (osm_svcr_t *) p_list_item;
117 }
118 
119 void osm_svcr_insert_to_db(IN osm_subn_t * p_subn, IN osm_log_t * p_log,
120 			   IN osm_svcr_t * p_svcr)
121 {
122 	OSM_LOG_ENTER(p_log);
123 
124 	OSM_LOG(p_log, OSM_LOG_DEBUG,
125 		"Inserting new Service Record into Database\n");
126 
127 	cl_qlist_insert_head(&p_subn->sa_sr_list, &p_svcr->list_item);
128 	p_subn->p_osm->sa.dirty = TRUE;
129 
130 	OSM_LOG_EXIT(p_log);
131 }
132 
133 void osm_svcr_remove_from_db(IN osm_subn_t * p_subn, IN osm_log_t * p_log,
134 			     IN osm_svcr_t * p_svcr)
135 {
136 	OSM_LOG_ENTER(p_log);
137 
138 	OSM_LOG(p_log, OSM_LOG_DEBUG,
139 		"Removing Service Record Name:%s ID:0x%016" PRIx64
140 		" from Database\n", p_svcr->service_record.service_name,
141 		cl_ntoh64(p_svcr->service_record.service_id));
142 
143 	cl_qlist_remove_item(&p_subn->sa_sr_list, &p_svcr->list_item);
144 	p_subn->p_osm->sa.dirty = TRUE;
145 
146 	OSM_LOG_EXIT(p_log);
147 }
148