summaryrefslogtreecommitdiff
path: root/libmultipath/pgpolicies.c
blob: cfaf1c31c23db14f56fe3544314437d4b2deb4d2 (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
/*
 * Copyright (c) 2004, 2005 Christophe Varoqui
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "checkers.h"
#include "util.h"
#include "memory.h"
#include "vector.h"
#include "structs.h"
#include "pgpolicies.h"

extern int
get_pgpolicy_id (char * str)
{
	if (0 == strncmp(str, "failover", 8))
		return FAILOVER;
	if (0 == strncmp(str, "multibus", 8))
		return MULTIBUS;
	if (0 == strncmp(str, "group_by_serial", 15))
		return GROUP_BY_SERIAL;
	if (0 == strncmp(str, "group_by_prio", 13))
		return GROUP_BY_PRIO;
	if (0 == strncmp(str, "group_by_node_name", 18))
		return GROUP_BY_NODE_NAME;

	return -1;
}

extern int
get_pgpolicy_name (char * buff, int len, int id)
{
	char * s;

	switch (id) {
	case FAILOVER:
		s = "failover";
		break;
	case MULTIBUS:
		s = "multibus";
		break;
	case GROUP_BY_SERIAL:
		s = "group_by_serial";
		break;
	case GROUP_BY_PRIO:
		s = "group_by_prio";
		break;
	case GROUP_BY_NODE_NAME:
		s = "group_by_node_name";
		break;
	default:
		s = "undefined";
		break;
	}
	return snprintf(buff, POLICY_NAME_SIZE, "%s", s);
}

/*
 * One path group per unique tgt_node_name present in the path vector
 */
extern int
group_by_node_name (struct multipath * mp) {
	int i, j;
	int * bitmap;
	struct path * pp;
	struct pathgroup * pgp;
	struct path * pp2;

	if (!mp->pg)
		mp->pg = vector_alloc();

	if (!mp->pg)
		return 1;

	/* init the bitmap */
	bitmap = (int *)MALLOC(VECTOR_SIZE(mp->paths) * sizeof (int));

	if (!bitmap)
		goto out;

	for (i = 0; i < VECTOR_SIZE(mp->paths); i++) {

		if (bitmap[i])
			continue;

		pp = VECTOR_SLOT(mp->paths, i);

		/* here, we really got a new pg */
		pgp = alloc_pathgroup();

		if (!pgp)
			goto out1;

		if (store_pathgroup(mp->pg, pgp))
			goto out1;

		/* feed the first path */
		if (store_path(pgp->paths, pp))
			goto out1;

		bitmap[i] = 1;

		for (j = i + 1; j < VECTOR_SIZE(mp->paths); j++) {

			if (bitmap[j])
				continue;

			pp2 = VECTOR_SLOT(mp->paths, j);

			if (!strncmp(pp->tgt_node_name, pp2->tgt_node_name,
					NODE_NAME_SIZE)) {
				if (store_path(pgp->paths, pp2))
					goto out1;

				bitmap[j] = 1;
			}
		}
	}
	FREE(bitmap);
	free_pathvec(mp->paths, KEEP_PATHS);
	mp->paths = NULL;
	return 0;
out1:
	FREE(bitmap);
out:
	free_pgvec(mp->pg, KEEP_PATHS);
	mp->pg = NULL;
	return 1;
}

/*
 * One path group per unique serial number present in the path vector
 */
extern int
group_by_serial (struct multipath * mp) {
	int i, j;
	int * bitmap;
	struct path * pp;
	struct pathgroup * pgp;
	struct path * pp2;

	if (!mp->pg)
		mp->pg = vector_alloc();

	if (!mp->pg)
		return 1;

	/* init the bitmap */
	bitmap = (int *)MALLOC(VECTOR_SIZE(mp->paths) * sizeof (int));

	if (!bitmap)
		goto out;

	for (i = 0; i < VECTOR_SIZE(mp->paths); i++) {

		if (bitmap[i])
			continue;

		pp = VECTOR_SLOT(mp->paths, i);

		/* here, we really got a new pg */
		pgp = alloc_pathgroup();

		if (!pgp)
			goto out1;

		if (store_pathgroup(mp->pg, pgp))
			goto out1;

		/* feed the first path */
		if (store_path(pgp->paths, pp))
			goto out1;

		bitmap[i] = 1;

		for (j = i + 1; j < VECTOR_SIZE(mp->paths); j++) {

			if (bitmap[j])
				continue;

			pp2 = VECTOR_SLOT(mp->paths, j);

			if (0 == strcmp(pp->serial, pp2->serial)) {
				if (store_path(pgp->paths, pp2))
					goto out1;

				bitmap[j] = 1;
			}
		}
	}
	FREE(bitmap);
	free_pathvec(mp->paths, KEEP_PATHS);
	mp->paths = NULL;
	return 0;
out1:
	FREE(bitmap);
out:
	free_pgvec(mp->pg, KEEP_PATHS);
	mp->pg = NULL;
	return 1;
}

extern int
one_path_per_group (struct multipath * mp)
{
	int i;
	struct path * pp;
	struct pathgroup * pgp;

	if (!mp->pg)
		mp->pg = vector_alloc();

	if (!mp->pg)
		return 1;

	for (i = 0; i < VECTOR_SIZE(mp->paths); i++) {
		pp = VECTOR_SLOT(mp->paths, i);
		pgp = alloc_pathgroup();

		if (!pgp)
			goto out;

		if (store_pathgroup(mp->pg, pgp))
			goto out;

		if (store_path(pgp->paths, pp))
			goto out;
	}
	free_pathvec(mp->paths, KEEP_PATHS);
	mp->paths = NULL;
	return 0;
out:
	free_pgvec(mp->pg, KEEP_PATHS);
	mp->pg = NULL;
	return 1;
}

extern int
one_group (struct multipath * mp)	/* aka multibus */
{
	struct pathgroup * pgp;

	if (VECTOR_SIZE(mp->paths) < 0)
		return 0;

	if (!mp->pg)
		mp->pg = vector_alloc();

	if (!mp->pg)
		return 1;

	if (VECTOR_SIZE(mp->paths) > 0) {
		pgp = alloc_pathgroup();

		if (!pgp)
			goto out;

		vector_free(pgp->paths);
		pgp->paths = mp->paths;
		mp->paths = NULL;

		if (store_pathgroup(mp->pg, pgp))
			goto out;
	}

	return 0;
out:
	free_pgvec(mp->pg, KEEP_PATHS);
	mp->pg = NULL;
	return 1;
}

extern int
group_by_prio (struct multipath * mp)
{
	int i;
	unsigned int prio;
	struct path * pp;
	struct pathgroup * pgp;

	if (!mp->pg)
		mp->pg = vector_alloc();

	if (!mp->pg)
		return 1;

	while (VECTOR_SIZE(mp->paths) > 0) {
		pp = VECTOR_SLOT(mp->paths, 0);
		prio = pp->priority;

		/*
		 * Find the position to insert the new path group. All groups
		 * are ordered by the priority value (higher value first).
		 */
		vector_foreach_slot(mp->pg, pgp, i) {
			pp  = VECTOR_SLOT(pgp->paths, 0);

			if (prio > pp->priority)
				break;
		}

		/*
		 * Initialize the new path group.
		 */
		pgp = alloc_pathgroup();

		if (!pgp)
			goto out;

		if (store_path(pgp->paths, VECTOR_SLOT(mp->paths, 0)))
				goto out;

		vector_del_slot(mp->paths, 0);

		/*
		 * Store the new path group into the vector.
		 */
		if (i < VECTOR_SIZE(mp->pg)) {
			if (!vector_insert_slot(mp->pg, i, pgp))
				goto out;
		} else {
			if (store_pathgroup(mp->pg, pgp))
				goto out;
		}

		/*
		 * add the other paths with the same prio
		 */
		vector_foreach_slot(mp->paths, pp, i) {
			if (pp->priority == prio) {
				if (store_path(pgp->paths, pp))
					goto out;

				vector_del_slot(mp->paths, i);
				i--;
			}
		}
	}
	free_pathvec(mp->paths, KEEP_PATHS);
	mp->paths = NULL;
	return 0;
out:
	free_pgvec(mp->pg, KEEP_PATHS);
	mp->pg = NULL;
	return 1;

}