Whamcloud - gitweb
b=21571 stacksize and locking fixes for loadgen patch from umka
[fs/lustre-release.git] / lustre / utils / nidlist.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/utils/nidlist.c
37  *
38  * Author: Jim Garlick <garlick@llnl.gov>
39  */
40
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <ctype.h>
45 #include <netdb.h>
46 #include <netinet/in.h>
47 #include <sys/socket.h>
48
49 #include "nidlist.h"
50
51 struct nl_struct {
52         char **nids;
53         int len;
54         int count;
55 };
56 #define NL_CHUNK        64
57
58 extern char *prog;
59
60 static void nl_oom(void)
61 {
62         fprintf(stderr, "%s: out of memory\n", prog);
63         exit(1);
64 }
65
66 NIDList nl_create(void)
67 {
68         struct nl_struct *nl;
69
70         if (!(nl = malloc(sizeof(struct nl_struct))))
71                 nl_oom();
72         nl->len = NL_CHUNK;
73         if (!(nl->nids = malloc(nl->len * sizeof(char *))))
74                 nl_oom();
75         nl->count = 0;
76
77         return nl;
78 }
79
80 void nl_destroy(NIDList nl)
81 {
82         int i;
83
84         for (i = 0; i < nl->count; i++)
85                 free(nl->nids[i]);
86         free(nl->nids);
87         free(nl);
88 }
89
90 static void nl_grow(NIDList nl, int n)
91 {
92         nl->len += n;
93         if (!(nl->nids = realloc(nl->nids, nl->len * sizeof(char *))))
94                 nl_oom();
95 }
96
97 void nl_add(NIDList nl, char *nid)
98 {
99         char *cp;
100
101         if (!(cp = strdup(nid)))
102                 nl_oom();
103         if (nl->count == nl->len)
104                 nl_grow(nl, NL_CHUNK);
105         nl->nids[nl->count++] = cp;
106 }
107
108 int nl_count(NIDList nl)
109 {
110         return nl->count;
111 }
112
113 static char *nl_nid_addr(char *nid)
114 {
115         char *addr, *p;
116
117         if (!(addr = strdup(nid)))
118                 nl_oom();
119         if ((p = strchr(addr, '@')))
120                 *p = '\0';
121
122         return addr;
123 }
124
125 static int nl_nid_parse_addr(char *addr)
126 {
127         int o;
128
129         for (o = strlen(addr); o > 0; o--)
130                 if (!isdigit(addr[o - 1]))
131                         break;
132
133         return o;
134 }
135
136 static int nl_cmp_addr(char *nid1, char *nid2, int *cflagp)
137 {
138         char *p1 = nl_nid_addr(nid1);
139         char *p2 = nl_nid_addr(nid2);
140         int res, o1, o2, cflag = 0;
141
142         o1 = nl_nid_parse_addr(p1);
143         o2 = nl_nid_parse_addr(p2);
144
145         if (o1 == o2 && (res = strncmp(p1, p2, o1)) == 0) {
146                 res = strtoul(&p1[o1], NULL, 10) - strtoul(&p2[o2], NULL, 10);
147                 if (cflagp && strlen(&p1[o1]) > 0 && strlen(&p2[o2]) > 0)
148                         cflag = 1;
149         } else
150                 res = strcmp(p1, p2);
151         free(p1);
152         free(p2);
153         if (cflagp)
154                 *cflagp = cflag;
155         return res;
156 }
157
158 static int nl_cmp_lnet(char *nid1, char *nid2)
159 {
160         char *s1 = strchr(nid1, '@');
161         char *s2 = strchr(nid2, '@');
162
163         return strcmp(s1 ? s1 + 1 : "", s2 ? s2 + 1 : "");
164 }
165
166 static int nl_cmp(const void *p1, const void *p2)
167 {
168         int res;
169
170         if ((res = nl_cmp_lnet(*(char **)p1, *(char **)p2)) == 0)
171                 res = nl_cmp_addr(*(char **)p1, *(char **)p2, NULL);
172         return res;
173 }
174
175 void nl_sort(NIDList nl)
176 {
177         qsort(nl->nids, nl->count, sizeof(char *), nl_cmp);
178 }
179
180 void nl_uniq(NIDList nl)
181 {
182         int i, j;
183
184         for (i = 1; i < nl->count; i++) {
185                 if (!strcmp(nl->nids[i], nl->nids[i - 1])) {
186                         free(nl->nids[i]);
187                         for (j = i; j < nl->count - 1; j++)
188                                 nl->nids[j] = nl->nids[j + 1];
189                         nl->count--;
190                         i--;
191                 }
192         }
193 }
194
195 static char *nl_nid_lookup_ipaddr(char *nid)
196 {
197         struct addrinfo *ai, *aip;
198         char name[NI_MAXHOST] = "";
199         char *p, *addr, *lnet = NULL, *res = NULL;
200         int len, x;
201
202         addr = nl_nid_addr(nid);
203         if (sscanf(addr, "%d.%d.%d.%d", &x, &x, &x, &x) == 4) {
204                 if ((p = strchr(nid, '@')))
205                         lnet = p + 1;
206                 if (getaddrinfo(addr, NULL, NULL, &ai) == 0) {
207                         for (aip = ai; aip != NULL; aip = aip->ai_next) {
208                                 if (getnameinfo(aip->ai_addr, aip->ai_addrlen,
209                                     name, sizeof(name), NULL, 0,
210                                     NI_NAMEREQD | NI_NOFQDN) == 0) {
211                                         if ((p = strchr(name, '.')))
212                                                 *p = '\0';
213                                         len = strlen(name) + strlen(lnet) + 2;
214                                         if (!(res = malloc(len)))
215                                                 nl_oom();
216                                         snprintf(res, len, "%s@%s", name, lnet);
217                                         break;
218                                 }
219                         }
220                         freeaddrinfo(ai);
221                 }
222         }
223         free(addr);
224
225         return res;
226 }
227
228 void nl_lookup_ip(NIDList nl)
229 {
230         int i;
231         char *new;
232
233         for (i = 0; i < nl->count; i++) {
234                 if ((new = nl_nid_lookup_ipaddr(nl->nids[i]))) {
235                         free(nl->nids[i]);
236                         nl->nids[i] = new;
237                 }
238         }
239 }
240
241 char *nl_string(NIDList nl, char *sep)
242 {
243         int seplen = strlen(sep);
244         int i, len = 1;
245         char *s;
246
247         for (i = 0; i < nl->count; i++)
248                 len += strlen(nl->nids[i]) + seplen;
249         if (!(s = malloc(len)))
250                 nl_oom();
251         s[0] = '\0';
252         for (i = 0; i < nl->count; i++) {
253                 if (i > 0)
254                         strcat(s, sep);
255                 strcat(s, nl->nids[i]);
256         }
257         return s;
258 }
259
260 static void nl_strxcat(char *s, char **nids, int len)
261 {
262         int i, o, lastn = 0;
263         char *base, *p, *lnet = NULL, *savedn = NULL;
264
265         if ((p = strchr(nids[0], '@')))
266                 lnet = p + 1;
267         base = nl_nid_addr(nids[0]);
268         o = nl_nid_parse_addr(base);
269         base[o] = '\0';
270         for (i = 0; i < len; i++) {
271                 char *addr = nl_nid_addr(nids[i]);
272                 int n = strtoul(&addr[o], NULL, 10);
273
274                 if (i == 0)
275                         sprintf(s + strlen(s), "%s[%s", base, &addr[o]);
276                 else if (i < len) {
277                         if (n == lastn + 1) {
278                                 if (savedn)
279                                         free(savedn);
280                                 if (!(savedn = strdup(&addr[o])))
281                                         nl_oom();
282                         } else {
283                                 if (savedn) {
284                                         sprintf(s + strlen(s), "-%s", savedn);
285                                         free(savedn);
286                                         savedn = NULL;
287                                 }
288                                 sprintf(s + strlen(s), ",%s", &addr[o]);
289                         }
290                 }
291                 if (i == len - 1) {
292                         if (savedn) {
293                                 sprintf(s + strlen(s), "-%s", savedn);
294                                 free(savedn);
295                         }
296                         strcat(s, "]");
297                         if (lnet)
298                                 sprintf(s + strlen(s), "@%s", lnet);
299                 }
300                 free(addr);
301                 lastn = n;
302         }
303         free(base);
304 }
305
306 char *nl_xstring(NIDList nl, char *sep)
307 {
308         int seplen = strlen(sep);
309         int cflag, i, j, len = 1;
310         char *s;
311
312         for (i = 0; i < nl->count; i++)
313                 len += strlen(nl->nids[i]) + seplen;
314         if (!(s = malloc(len)))
315                 nl_oom();
316         s[0] = '\0';
317         for (i = 0; i < nl->count; i++) {
318                 if (i > 0)
319                         strcat(s, sep);
320                 for (j = i + 1; j < nl->count; j++) {
321                         if (nl_cmp_lnet(nl->nids[i], nl->nids[j]) != 0)
322                                 break;
323                         (void)nl_cmp_addr(nl->nids[i], nl->nids[j], &cflag);
324                         if (!cflag)
325                                 break;
326                 }
327                 if (j - i > 1)
328                         nl_strxcat(s, &nl->nids[i], j - i);
329                 else
330                         strcat(s, nl->nids[i]);
331                 i += j - i - 1;
332         }
333         return s;
334 }