Whamcloud - gitweb
3b04d32acf449171e4eb74f9818cb520581a3cd0
[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 != NULL)
213                                                 len += strlen(lnet);
214                                         if (!(res = malloc(len)))
215                                                 nl_oom();
216                                         if (lnet != NULL)
217                                                 snprintf(res, len, "%s@%s",
218                                                          name, lnet);
219                                         else
220                                                 snprintf(res, len, "%s", name);
221                                         break;
222                                 }
223                         }
224                         freeaddrinfo(ai);
225                 }
226         }
227         free(addr);
228
229         return res;
230 }
231
232 void nl_lookup_ip(NIDList nl)
233 {
234         int i;
235         char *new;
236
237         for (i = 0; i < nl->count; i++) {
238                 if ((new = nl_nid_lookup_ipaddr(nl->nids[i]))) {
239                         free(nl->nids[i]);
240                         nl->nids[i] = new;
241                 }
242         }
243 }
244
245 char *nl_string(NIDList nl, char *sep)
246 {
247         int seplen = strlen(sep);
248         int i, len = 1;
249         char *s;
250
251         for (i = 0; i < nl->count; i++)
252                 len += strlen(nl->nids[i]) + seplen;
253         if (!(s = malloc(len)))
254                 nl_oom();
255         s[0] = '\0';
256         for (i = 0; i < nl->count; i++) {
257                 if (i > 0)
258                         strcat(s, sep);
259                 strcat(s, nl->nids[i]);
260         }
261         return s;
262 }
263
264 static void nl_strxcat(char *s, char **nids, int len)
265 {
266         int i, o, lastn = 0;
267         char *base, *p, *lnet = NULL, *savedn = NULL;
268
269         if ((p = strchr(nids[0], '@')))
270                 lnet = p + 1;
271         base = nl_nid_addr(nids[0]);
272         o = nl_nid_parse_addr(base);
273         base[o] = '\0';
274         for (i = 0; i < len; i++) {
275                 char *addr = nl_nid_addr(nids[i]);
276                 int n = strtoul(&addr[o], NULL, 10);
277
278                 if (i == 0)
279                         sprintf(s + strlen(s), "%s[%s", base, &addr[o]);
280                 else if (i < len) {
281                         if (n == lastn + 1) {
282                                 if (savedn)
283                                         free(savedn);
284                                 if (!(savedn = strdup(&addr[o])))
285                                         nl_oom();
286                         } else {
287                                 if (savedn) {
288                                         sprintf(s + strlen(s), "-%s", savedn);
289                                         free(savedn);
290                                         savedn = NULL;
291                                 }
292                                 sprintf(s + strlen(s), ",%s", &addr[o]);
293                         }
294                 }
295                 if (i == len - 1) {
296                         if (savedn) {
297                                 sprintf(s + strlen(s), "-%s", savedn);
298                                 free(savedn);
299                         }
300                         strcat(s, "]");
301                         if (lnet)
302                                 sprintf(s + strlen(s), "@%s", lnet);
303                 }
304                 free(addr);
305                 lastn = n;
306         }
307         free(base);
308 }
309
310 char *nl_xstring(NIDList nl, char *sep)
311 {
312         int seplen = strlen(sep);
313         int cflag, i, j, len = 1;
314         char *s;
315
316         for (i = 0; i < nl->count; i++)
317                 len += strlen(nl->nids[i]) + seplen;
318         if (!(s = malloc(len)))
319                 nl_oom();
320         s[0] = '\0';
321         for (i = 0; i < nl->count; i++) {
322                 if (i > 0)
323                         strcat(s, sep);
324                 for (j = i + 1; j < nl->count; j++) {
325                         if (nl_cmp_lnet(nl->nids[i], nl->nids[j]) != 0)
326                                 break;
327                         (void)nl_cmp_addr(nl->nids[i], nl->nids[j], &cflag);
328                         if (!cflag)
329                                 break;
330                 }
331                 if (j - i > 1)
332                         nl_strxcat(s, &nl->nids[i], j - i);
333                 else
334                         strcat(s, nl->nids[i]);
335                 i += j - i - 1;
336         }
337         return s;
338 }