summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/io/ib/clients/rdsv3/rdsv3_sc.c
blob: 8510746b9eed2d3a57f4952a24f54f2ea5cb3371 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
 */
#include <sys/types.h>
#include <sys/sunddi.h>
#include <sys/dlpi.h>
#include <sys/ib/clients/rdsv3/rdsv3_sc.h>
#include <sys/ib/clients/rdsv3/rdsv3_debug.h>

/*
 * RDS Path MAP
 *
 * N - Node record, P - Path record
 *
 * rds_path_map -
 *              |
 *              v
 *      	---------	---------	---------
 *     		|   N   |------>|  N    |------>|   N   |------> NULL
 * NULL <-------|       |<------|       |<------|       |
 *     		---------       ---------       ---------
 *               |               |               |
 *               |               |               |
 *               v               v               v
 *		--------        ---------       ---------
 *		|  P   |        |  P    |       |  P    |
 *		--------        ---------       ---------
 *		|  ^            |   ^           |   ^
 *		|  |            |   |           |   |
 *		v  |            v   |           v   |
 *		--------	--------	---------
 *		|   P  |	|  P   |	|  P    |
 *		--------	--------	---------
 *		  o		   o		   o
 *		  o		   o		   o
 *		  o		   o		   o
 */

typedef struct rds_path_record_s {
	ipaddr_t			libd_ip;
	ipaddr_t			ribd_ip;
	struct rds_path_record_s	*up;
	struct rds_path_record_s	*downp;
	char				lifname[MAXNAMELEN];
	char				rifname[MAXNAMELEN];
} rds_path_record_t;

typedef struct rds_node_record_s {
	struct rds_node_record_s	*nextp;
	ipaddr_t			lnode_ip;	/* local ip */
	ipaddr_t			rnode_ip;	/* remote ip */
	struct rds_path_record_s	*downp;
	struct rds_node_record_s	*prevp;
} rds_node_record_t;

static char			sc_device_name[MAXNAMELEN] = "NotInitialized";
static kmutex_t			rdsv3_pathmap_lock;
static rds_node_record_t	*rdsv3_pathmap = NULL;

#define	RDS_VALIDATE_PATH(p)						\
	if ((p->local.iftype != DL_IB) || (p->remote.iftype != DL_IB))	\
		return

#define	isalpha(ch)	(((ch) >= 'a' && (ch) <= 'z') || \
			((ch) >= 'A' && (ch) <= 'Z'))

/*
 * Called by SC to register the Sun Cluster device name
 */
void
rdsv3_clif_name(char *name)
{
	int	i;

	ASSERT(name != NULL);

	mutex_enter(&rdsv3_pathmap_lock);

	/* extract the device name from the interface name */
	i = strlen(name) - 1;
	while ((i >= 0) && (!isalpha(name[i]))) i--;
	if (i >= 0) {
		(void) strncpy(sc_device_name, name, i + 1);
		sc_device_name[i + 1] = '\0';
	}

	mutex_exit(&rdsv3_pathmap_lock);
}

/*
 * Called by SC on discovering a new path
 */
void
rdsv3_path_up(rds_path_t *path)
{
	rds_node_record_t	*p;
	rds_path_record_t	*p1;

	ASSERT(path != NULL);

	/* ignore if the end points are not of type DL_IB */
	RDS_VALIDATE_PATH(path);

	mutex_enter(&rdsv3_pathmap_lock);

	p = rdsv3_pathmap;
	while ((p) && ((p->lnode_ip != path->local.node_ipaddr) ||
	    (p->rnode_ip != path->remote.node_ipaddr))) {
		p = p->nextp;
	}

	if (p == NULL) {
		p = (rds_node_record_t *)kmem_alloc(sizeof (rds_node_record_t),
		    KM_SLEEP);
		p1 = (rds_path_record_t *)kmem_alloc(
		    sizeof (rds_path_record_t), KM_SLEEP);

		p->nextp = NULL;
		p->lnode_ip = path->local.node_ipaddr;
		p->rnode_ip = path->remote.node_ipaddr;
		p->downp = p1;
		p->prevp = NULL;

		p1->libd_ip = path->local.ipaddr;
		p1->ribd_ip = path->remote.ipaddr;
		p1->up = NULL;
		p1->downp = NULL;
		(void) strcpy(p1->lifname, path->local.ifname);
		(void) strcpy(p1->rifname, path->remote.ifname);

		if (rdsv3_pathmap == NULL) {
			rdsv3_pathmap = p;
		} else {
			/* insert this node at the head */
			rdsv3_pathmap->prevp = p;
			p->nextp = rdsv3_pathmap;
			rdsv3_pathmap = p;
		}
	} else {
		/* we found a match */
		p1 = (rds_path_record_t *)kmem_alloc(
		    sizeof (rds_path_record_t), KM_SLEEP);

		p1->libd_ip = path->local.ipaddr;
		p1->ribd_ip = path->remote.ipaddr;
		p1->downp = p->downp;
		p->downp->up = p1;
		p1->up = NULL;
		p->downp = p1;
		(void) strcpy(p1->lifname, path->local.ifname);
		(void) strcpy(p1->rifname, path->remote.ifname);
	}

	mutex_exit(&rdsv3_pathmap_lock);
}

/*
 * Called by SC to delete a path
 */
void
rdsv3_path_down(rds_path_t *path)
{
	rds_node_record_t	*p;
	rds_path_record_t	*p1, *p1up, *p1downp;

	ASSERT(path != NULL);

	/* ignore if the end points are not of type DL_IB */
	RDS_VALIDATE_PATH(path);

	mutex_enter(&rdsv3_pathmap_lock);

	p = rdsv3_pathmap;
	while ((p) && ((p->lnode_ip != path->local.node_ipaddr) ||
	    (p->rnode_ip != path->remote.node_ipaddr))) {
		p = p->nextp;
	}

	if (p == NULL) {
		/* no match */
		RDSV3_DPRINTF2("rdsv3_path_down", "Node record not found "
		    "(0x%x <-> 0x%x)", path->local.node_ipaddr,
		    path->remote.node_ipaddr);
		mutex_exit(&rdsv3_pathmap_lock);
		return;
	}

	p1 = p->downp;
	while ((p1) && ((p1->libd_ip != path->local.ipaddr) ||
	    (p1->ribd_ip != path->remote.ipaddr))) {
		p1 = p1->downp;
	}

	if (p1 == NULL) {
		/* no match */
		RDSV3_DPRINTF2("rdsv3_path_down", "Path record not found "
		    "(0x%x <-> 0x%x)", path->local.ipaddr, path->remote.ipaddr);
		mutex_exit(&rdsv3_pathmap_lock);
		return;
	}

	/* we found the record, remove it */
	p1up = p1->up;
	p1downp = p1->downp;

	if (p1up) {
		p1up->downp = p1downp;
	} else {
		/* this is the first path record */
		p->downp = p1downp;
	}

	if (p1downp) {
		p1downp->up = p1up;
	}

	kmem_free(p1, sizeof (rds_path_record_t));

	/* remove the node record if there are no path records */
	if (p->downp == NULL) {
		if (p->prevp) {
			p->prevp->nextp = p->nextp;
		} else {
			/* this is the first node record */
			ASSERT(p == rdsv3_pathmap);
			rdsv3_pathmap = p->nextp;
		}

		if (p->nextp) {
			p->nextp->prevp = p->prevp;
		}

		kmem_free(p, sizeof (rds_node_record_t));
	}

	mutex_exit(&rdsv3_pathmap_lock);
}

int
rdsv3_sc_path_lookup(ipaddr_t *localip, ipaddr_t *remip)
{
	rds_node_record_t	*p;
	rds_path_record_t	*p1, *p1downp;

	mutex_enter(&rdsv3_pathmap_lock);

	p = rdsv3_pathmap;
	while ((p) && ((p->lnode_ip != *localip) || (p->rnode_ip != *remip))) {
		p = p->nextp;
	}

	if (p == NULL) {
		/* no match */
		RDSV3_DPRINTF2("rdsv3_sc_path_lookup", "Node record not found "
		    "(0x%x <-> 0x%x)", *localip, *remip);
		mutex_exit(&rdsv3_pathmap_lock);
		return (0);
	}

	/* found a path */
	p1 = p->downp;
	*localip = p1->libd_ip;
	*remip = p1->ribd_ip;

	/*
	 * But next time, we want to use a different path record so move this
	 * path record to the end.
	 */
	p1downp = p1->downp;
	if (p1downp != NULL) {
		p->downp = p1downp;
		p1downp->up = NULL;

		/* walk down to the last path record */
		while (p1downp->downp != NULL) {
			p1downp = p1downp->downp;
		}

		/* Attach the first path record to the end */
		p1downp->downp = p1;
		p1->up = p1downp;
		p1->downp = NULL;
	}

	mutex_exit(&rdsv3_pathmap_lock);

	return (1);
}

boolean_t
rdsv3_if_lookup_by_name(char *devname)
{
	mutex_enter(&rdsv3_pathmap_lock);

	/*
	 * Sun Cluster always names its interconnect virtual network interface
	 * as clprivnetx, so  return TRUE if there is atleast one node record
	 * and the interface name is clprivnet something.
	 */
	if (strcmp(devname, sc_device_name) == 0) {
		/* clprivnet address */
		mutex_exit(&rdsv3_pathmap_lock);
		return (B_TRUE);
	}

	mutex_exit(&rdsv3_pathmap_lock);
	return (B_FALSE);
}

boolean_t
rdsv3_if_lookup_by_addr(ipaddr_t addr)
{
	rds_node_record_t	*p;
	rds_path_record_t	*p1;

	mutex_enter(&rdsv3_pathmap_lock);

	p = rdsv3_pathmap;
	while ((p) && (p->lnode_ip != addr)) {
		p1 = p->downp;
		while ((p1) && (p1->libd_ip != addr)) {
			p1 = p1->downp;
		}

		/* we found a match */
		if (p1 != NULL)
			break;

		/* go to the next node record */
		p = p->nextp;
	}

	mutex_exit(&rdsv3_pathmap_lock);
	if (p == NULL) {
		/* no match */
		RDSV3_DPRINTF2("rds_if_lookup_by_addr",
		    "Addr: 0x%x not found", addr);
		return (B_FALSE);
	}

	/* Found a matching node record */
	return (B_TRUE);
}

/*
 * If SC is configured then addr would be a clprivnet address. Find the
 * node record and return the first IB address. If the node record is not
 * found, then return addr as-is.
 */
ipaddr_t
rdsv3_scaddr_to_ibaddr(ipaddr_t addr)
{
	rds_node_record_t	*p;
	rds_path_record_t	*p1;
	ipaddr_t		ret = addr;

	mutex_enter(&rdsv3_pathmap_lock);

	p = rdsv3_pathmap;
	while ((p) && (p->lnode_ip != addr)) {
		/* go to the next node record */
		p = p->nextp;
	}

	if (p != NULL) {
		p1 = p->downp;
		ret = p1->libd_ip;
		RDSV3_DPRINTF3("rds_scaddr_to_ibaddr",
		    "Addr: 0x%x found: 0x%x", addr, p1->libd_ip);
	}
	mutex_exit(&rdsv3_pathmap_lock);

	/* Found a matching node record */
	return (ret);
}