Whamcloud - gitweb
78e70ce10882ee38ce065a411e151d4122ee68d6
[fs/lustre-release.git] / lustre / utils / nidlist.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * lustre/utils/nidlist.c
35  *
36  * Author: Jim Garlick <garlick@llnl.gov>
37  */
38
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <ctype.h>
43 #include <netdb.h>
44 #include <netinet/in.h>
45 #include <sys/socket.h>
46
47 #include "nidlist.h"
48
49 struct nl_struct {
50         char **nids;
51         int len;
52         int count;
53 };
54 #define NL_CHUNK        64
55
56 extern char *prog;
57
58 static void nl_oom(void)
59 {
60         fprintf(stderr, "%s: out of memory\n", prog);
61         exit(1);
62 }
63
64 NIDList nl_create(void)
65 {
66         struct nl_struct *nl;
67
68         if (!(nl = malloc(sizeof(struct nl_struct))))
69                 nl_oom();
70         nl->len = NL_CHUNK;
71         if (!(nl->nids = malloc(nl->len * sizeof(char *))))
72                 nl_oom();
73         nl->count = 0;
74
75         return nl;
76 }
77
78 void nl_destroy(NIDList nl)
79 {
80         int i;
81
82         for (i = 0; i < nl->count; i++)
83                 free(nl->nids[i]);
84         free(nl->nids);
85         free(nl);
86 }
87
88 static void nl_grow(NIDList nl, int n)
89 {
90         nl->len += n;
91         if (!(nl->nids = realloc(nl->nids, nl->len * sizeof(char *))))
92                 nl_oom();
93 }
94
95 void nl_add(NIDList nl, char *nid)
96 {
97         char *cp;
98
99         if (!(cp = strdup(nid)))
100                 nl_oom();
101         if (nl->count == nl->len)
102                 nl_grow(nl, NL_CHUNK);
103         nl->nids[nl->count++] = cp;
104 }
105
106 int nl_count(NIDList nl)
107 {
108         return nl->count;
109 }
110
111 static char *nl_nid_addr(char *nid)
112 {
113         char *addr, *p;
114
115         if (!(addr = strdup(nid)))
116                 nl_oom();
117         if ((p = strchr(addr, '@')))
118                 *p = '\0';
119
120         return addr;
121 }
122
123 static int nl_nid_parse_addr(char *addr)
124 {
125         int o;
126
127         for (o = strlen(addr); o > 0; o--)
128                 if (!isdigit(addr[o - 1]))
129                         break;
130
131         return o;
132 }
133
134 static int nl_cmp_addr(char *nid1, char *nid2, int *cflagp)
135 {
136         char *p1 = nl_nid_addr(nid1);
137         char *p2 = nl_nid_addr(nid2);
138         int res, o1, o2, cflag = 0;
139
140         o1 = nl_nid_parse_addr(p1);
141         o2 = nl_nid_parse_addr(p2);
142
143         if (o1 == o2 && (res = strncmp(p1, p2, o1)) == 0) {
144                 res = strtoul(&p1[o1], NULL, 10) - strtoul(&p2[o2], NULL, 10);
145                 if (cflagp && strlen(&p1[o1]) > 0 && strlen(&p2[o2]) > 0)
146                         cflag = 1;
147         } else
148                 res = strcmp(p1, p2);
149         free(p1);
150         free(p2);
151         if (cflagp)
152                 *cflagp = cflag;
153         return res;
154 }
155
156 static int nl_cmp_lnet(char *nid1, char *nid2)
157 {
158         char *s1 = strchr(nid1, '@');
159         char *s2 = strchr(nid2, '@');
160
161         return strcmp(s1 ? s1 + 1 : "", s2 ? s2 + 1 : "");
162 }
163
164 static int nl_cmp(const void *p1, const void *p2)
165 {
166         int res;
167
168         if ((res = nl_cmp_lnet(*(char **)p1, *(char **)p2)) == 0)
169                 res = nl_cmp_addr(*(char **)p1, *(char **)p2, NULL);
170         return res;
171 }
172
173 void nl_sort(NIDList nl)
174 {
175         qsort(nl->nids, nl->count, sizeof(char *), nl_cmp);
176 }
177
178 void nl_uniq(NIDList nl)
179 {
180         int i, j;
181
182         for (i = 1; i < nl->count; i++) {
183                 if (!strcmp(nl->nids[i], nl->nids[i - 1])) {
184                         free(nl->nids[i]);
185                         for (j = i; j < nl->count - 1; j++)
186                                 nl->nids[j] = nl->nids[j + 1];
187                         nl->count--;
188                         i--;
189                 }
190         }
191 }
192
193 static char *nl_nid_lookup_ipaddr(char *nid)
194 {
195         struct addrinfo *ai, *aip;
196         char name[NI_MAXHOST] = "";
197         char *p, *addr, *lnet = NULL, *res = NULL;
198         int len, x;
199
200         addr = nl_nid_addr(nid);
201         if (sscanf(addr, "%d.%d.%d.%d", &x, &x, &x, &x) == 4) {
202                 if ((p = strchr(nid, '@')))
203                         lnet = p + 1;
204                 if (getaddrinfo(addr, NULL, NULL, &ai) == 0) {
205                         for (aip = ai; aip != NULL; aip = aip->ai_next) {
206                                 if (getnameinfo(aip->ai_addr, aip->ai_addrlen,
207                                     name, sizeof(name), NULL, 0,
208                                     NI_NAMEREQD | NI_NOFQDN) == 0) {
209                                         if ((p = strchr(name, '.')))
210                                                 *p = '\0';
211                                         len = strlen(name) + 2;
212                                         if (lnet)
213                                                 len += strlen(lnet);
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 }