1 /*
2  * Copyright (c) 2004-2009 Voltaire, Inc. All rights reserved.
3  * Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved.
4  * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
5  * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
6  *
7  * This software is available to you under a choice of one of two
8  * licenses.  You may choose to be licensed under the terms of the GNU
9  * General Public License (GPL) Version 2, available from the file
10  * COPYING in the main directory of this source tree, or the
11  * OpenIB.org BSD license below:
12  *
13  *     Redistribution and use in source and binary forms, with or
14  *     without modification, are permitted provided that the following
15  *     conditions are met:
16  *
17  *      - Redistributions of source code must retain the above
18  *        copyright notice, this list of conditions and the following
19  *        disclaimer.
20  *
21  *      - Redistributions in binary form must reproduce the above
22  *        copyright notice, this list of conditions and the following
23  *        disclaimer in the documentation and/or other materials
24  *        provided with the distribution.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33  * SOFTWARE.
34  *
35  */
36 
37 /*
38  * Abstract:
39  *    Implementation of osm_mftr_rcv_t.
40  *   This object represents the MulticastForwardingTable Receiver object.
41  *   This object is part of the opensm family of objects.
42  */
43 
44 #if HAVE_CONFIG_H
45 #  include <config.h>
46 #endif				/* HAVE_CONFIG_H */
47 
48 #include <string.h>
49 #include <iba/ib_types.h>
50 #include <complib/cl_debug.h>
51 #include <complib/cl_qlist.h>
52 #include <opensm/osm_file_ids.h>
53 #define FILE_ID OSM_FILE_SA_MFT_RECORD_C
54 #include <vendor/osm_vendor_api.h>
55 #include <opensm/osm_switch.h>
56 #include <opensm/osm_helper.h>
57 #include <opensm/osm_pkey.h>
58 #include <opensm/osm_sa.h>
59 
60 #define SA_MFTR_RESP_SIZE SA_ITEM_RESP_SIZE(mft_rec)
61 
62 typedef struct osm_mftr_search_ctxt {
63 	const ib_mft_record_t *p_rcvd_rec;
64 	ib_net64_t comp_mask;
65 	cl_qlist_t *p_list;
66 	osm_sa_t *sa;
67 	const osm_physp_t *p_req_physp;
68 } osm_mftr_search_ctxt_t;
69 
mftr_rcv_new_mftr(IN osm_sa_t * sa,IN osm_switch_t * p_sw,IN cl_qlist_t * p_list,IN ib_net16_t lid,IN uint16_t block,IN uint8_t position)70 static ib_api_status_t mftr_rcv_new_mftr(IN osm_sa_t * sa,
71 					 IN osm_switch_t * p_sw,
72 					 IN cl_qlist_t * p_list,
73 					 IN ib_net16_t lid, IN uint16_t block,
74 					 IN uint8_t position)
75 {
76 	osm_sa_item_t *p_rec_item;
77 	ib_api_status_t status = IB_SUCCESS;
78 	uint16_t position_block_num;
79 
80 	OSM_LOG_ENTER(sa->p_log);
81 
82 	p_rec_item = malloc(SA_MFTR_RESP_SIZE);
83 	if (p_rec_item == NULL) {
84 		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4A02: "
85 			"rec_item alloc failed\n");
86 		status = IB_INSUFFICIENT_RESOURCES;
87 		goto Exit;
88 	}
89 
90 	OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
91 		"New MulticastForwardingTable: sw 0x%016" PRIx64
92 		"\n\t\t\t\tblock %u position %u lid %u\n",
93 		cl_ntoh64(osm_node_get_node_guid(p_sw->p_node)),
94 		block, position, cl_ntoh16(lid));
95 
96 	position_block_num = ((uint16_t) position << 12) |
97 	    (block & IB_MCAST_BLOCK_ID_MASK_HO);
98 
99 	memset(p_rec_item, 0, SA_MFTR_RESP_SIZE);
100 
101 	p_rec_item->resp.mft_rec.lid = lid;
102 	p_rec_item->resp.mft_rec.position_block_num = cl_hton16(position_block_num);
103 
104 	/* copy the mft block */
105 	osm_switch_get_mft_block(p_sw, block, position, p_rec_item->resp.mft_rec.mft);
106 
107 	cl_qlist_insert_tail(p_list, &p_rec_item->list_item);
108 
109 Exit:
110 	OSM_LOG_EXIT(sa->p_log);
111 	return status;
112 }
113 
mftr_rcv_by_comp_mask(IN cl_map_item_t * p_map_item,IN void * cxt)114 static void mftr_rcv_by_comp_mask(IN cl_map_item_t * p_map_item, IN void *cxt)
115 {
116 	const osm_mftr_search_ctxt_t *p_ctxt = cxt;
117 	osm_switch_t *p_sw = (osm_switch_t *) p_map_item;
118 	const ib_mft_record_t *const p_rcvd_rec = p_ctxt->p_rcvd_rec;
119 	osm_sa_t *sa = p_ctxt->sa;
120 	ib_net64_t const comp_mask = p_ctxt->comp_mask;
121 	const osm_physp_t *const p_req_physp = p_ctxt->p_req_physp;
122 	osm_port_t *p_port;
123 	uint16_t min_lid_ho, max_lid_ho;
124 	uint16_t position_block_num_ho;
125 	uint16_t min_block, max_block, block;
126 	const osm_physp_t *p_physp;
127 	uint8_t min_position, max_position, position;
128 
129 	/* In switches, the port guid is the node guid. */
130 	p_port =
131 	    osm_get_port_by_guid(sa->p_subn, p_sw->p_node->node_info.port_guid);
132 	if (!p_port) {
133 		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4A05: "
134 			"Failed to find Port by Node Guid:0x%016" PRIx64
135 			"\n", cl_ntoh64(p_sw->p_node->node_info.node_guid));
136 		return;
137 	}
138 
139 	/* check that the requester physp and the current physp are under
140 	   the same partition. */
141 	p_physp = p_port->p_physp;
142 	if (!p_physp) {
143 		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4A06: "
144 			"Failed to find default physical Port by Node Guid:0x%016"
145 			PRIx64 "\n",
146 			cl_ntoh64(p_sw->p_node->node_info.node_guid));
147 		return;
148 	}
149 	if (!osm_physp_share_pkey(sa->p_log, p_req_physp, p_physp,
150 				  sa->p_subn->opt.allow_both_pkeys))
151 		return;
152 
153 	/* get the port 0 of the switch */
154 	osm_port_get_lid_range_ho(p_port, &min_lid_ho, &max_lid_ho);
155 
156 	/* compare the lids - if required */
157 	if (comp_mask & IB_MFTR_COMPMASK_LID) {
158 		OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
159 			"Comparing lid:%u to port lid range: %u .. %u\n",
160 			cl_ntoh16(p_rcvd_rec->lid), min_lid_ho, max_lid_ho);
161 		/* ok we are ready for range check */
162 		if (min_lid_ho > cl_ntoh16(p_rcvd_rec->lid) ||
163 		    max_lid_ho < cl_ntoh16(p_rcvd_rec->lid))
164 			return;
165 	}
166 
167 	if (!osm_switch_supports_mcast(p_sw))
168 		return;
169 
170 	/* Are there any blocks in use ? */
171 	if (osm_switch_get_mft_max_block_in_use(p_sw) == -1)
172 		return;
173 
174 	position_block_num_ho = cl_ntoh16(p_rcvd_rec->position_block_num);
175 
176 	/* now we need to decide which blocks to output */
177 	if (comp_mask & IB_MFTR_COMPMASK_BLOCK) {
178 		max_block = min_block =
179 		    position_block_num_ho & IB_MCAST_BLOCK_ID_MASK_HO;
180 		if (max_block > osm_switch_get_mft_max_block_in_use(p_sw))
181 			return;
182 	} else {
183 		/* use as many blocks as needed */
184 		min_block = 0;
185 		max_block = osm_switch_get_mft_max_block_in_use(p_sw);
186 	}
187 
188 	/* need to decide which positions to output */
189 	if (comp_mask & IB_MFTR_COMPMASK_POSITION) {
190 		min_position = max_position =
191 		    (position_block_num_ho & 0xF000) >> 12;
192 		if (max_position > osm_switch_get_mft_max_position(p_sw))
193 			return;
194 	} else {
195 		/* use as many positions as needed */
196 		min_position = 0;
197 		max_position = osm_switch_get_mft_max_position(p_sw);
198 	}
199 
200 	/* so we can add these one by one ... */
201 	for (block = min_block; block <= max_block; block++)
202 		for (position = min_position; position <= max_position;
203 		     position++)
204 			mftr_rcv_new_mftr(sa, p_sw, p_ctxt->p_list,
205 					  osm_port_get_base_lid(p_port), block,
206 					  position);
207 }
208 
osm_mftr_rcv_process(IN void * ctx,IN void * data)209 void osm_mftr_rcv_process(IN void *ctx, IN void *data)
210 {
211 	osm_sa_t *sa = ctx;
212 	osm_madw_t *p_madw = data;
213 	const ib_sa_mad_t *p_rcvd_mad;
214 	const ib_mft_record_t *p_rcvd_rec;
215 	cl_qlist_t rec_list;
216 	osm_mftr_search_ctxt_t context;
217 	osm_physp_t *p_req_physp;
218 
219 	CL_ASSERT(sa);
220 
221 	OSM_LOG_ENTER(sa->p_log);
222 
223 	CL_ASSERT(p_madw);
224 
225 	p_rcvd_mad = osm_madw_get_sa_mad_ptr(p_madw);
226 	p_rcvd_rec = (ib_mft_record_t *) ib_sa_mad_get_payload_ptr(p_rcvd_mad);
227 
228 	CL_ASSERT(p_rcvd_mad->attr_id == IB_MAD_ATTR_MFT_RECORD);
229 
230 	/* we only support SubnAdmGet and SubnAdmGetTable methods */
231 	if (p_rcvd_mad->method != IB_MAD_METHOD_GET &&
232 	    p_rcvd_mad->method != IB_MAD_METHOD_GETTABLE) {
233 		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4A08: "
234 			"Unsupported Method (%s) for MFTRecord request\n",
235 			ib_get_sa_method_str(p_rcvd_mad->method));
236 		osm_sa_send_error(sa, p_madw, IB_MAD_STATUS_UNSUP_METHOD_ATTR);
237 		goto Exit;
238 	}
239 
240 	cl_plock_acquire(sa->p_lock);
241 
242 	/* update the requester physical port */
243 	p_req_physp = osm_get_physp_by_mad_addr(sa->p_log, sa->p_subn,
244 						osm_madw_get_mad_addr_ptr
245 						(p_madw));
246 	if (p_req_physp == NULL) {
247 		cl_plock_release(sa->p_lock);
248 		OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 4A07: "
249 			"Cannot find requester physical port\n");
250 		goto Exit;
251 	}
252 	OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
253 		"Requester port GUID 0x%" PRIx64 "\n",
254 		cl_ntoh64(osm_physp_get_port_guid(p_req_physp)));
255 
256 	cl_qlist_init(&rec_list);
257 
258 	context.p_rcvd_rec = p_rcvd_rec;
259 	context.p_list = &rec_list;
260 	context.comp_mask = p_rcvd_mad->comp_mask;
261 	context.sa = sa;
262 	context.p_req_physp = p_req_physp;
263 
264 	/* Go over all switches */
265 	cl_qmap_apply_func(&sa->p_subn->sw_guid_tbl, mftr_rcv_by_comp_mask,
266 			   &context);
267 
268 	cl_plock_release(sa->p_lock);
269 
270 	osm_sa_respond(sa, p_madw, sizeof(ib_mft_record_t), &rec_list);
271 
272 Exit:
273 	OSM_LOG_EXIT(sa->p_log);
274 }
275