Whamcloud - gitweb
b=21816 allow querying quota stats even when some OSTs are down
[fs/lustre-release.git] / lnet / utils / gmlndnid.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 (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2003 Los Alamos National Laboratory (LANL)
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  */
38
39 #include <stdio.h>
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <netinet/tcp.h>
43 #include <netdb.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <fcntl.h>
47 #include <sys/ioctl.h>
48 #include <unistd.h>
49 #include <syslog.h>
50 #include <errno.h>
51
52 #include <lnet/api-support.h>
53 #include <lnet/lib-types.h>
54
55 #include <gm.h>
56
57 /*
58  *      portals always uses unit 0
59  *      Can this be configurable?
60  */
61 #define GM_UNIT 0
62
63 void
64 usage(char *prg, int h)
65 {
66         fprintf(stderr,
67                 "usage %s -h\n"
68                 "      %s [-l] [-n hostname] [-L] [hostnames]\n", prg);
69
70         if (h)
71                 printf("Print Myrinet Global network ids for specified hosts\n"
72                        "-l                    print local host's ID\n"
73                        "-n hostname           print given host's ID\n"
74                        "-L                    print Myringet local net ID too\n"
75                        "[hostnames]           print ids of given hosts (local if none)\n");
76 }
77
78 gm_status_t
79 print_gmid(char *name, int name_fieldlen, int show_local_id)
80 {
81         struct gm_port *gm_port;
82         int             gm_port_id;
83         gm_status_t     gm_status;
84         unsigned int    local_id;
85         unsigned int    global_id;
86
87         gm_status = gm_init();
88         if (gm_status != GM_SUCCESS) {
89                 fprintf(stderr, "gm_init: %s\n", gm_strerror(gm_status));
90                 return gm_status;
91         }
92
93         gm_port_id = 2;
94         gm_status = gm_open(&gm_port, GM_UNIT, gm_port_id, "gmnalnid",
95                             GM_API_VERSION);
96         if (gm_status != GM_SUCCESS) {
97                 int num_ports = gm_num_ports(gm_port);
98
99                 /* Couldn't open port 2, try 4 ... num_ports */
100                 for (gm_port_id = 4; gm_port_id < num_ports; gm_port_id++) {
101                         gm_status = gm_open(&gm_port, GM_UNIT, gm_port_id,
102                                             "gmnalnid", GM_API_VERSION);
103                         if (gm_status == GM_SUCCESS)
104                                 break;
105                 }
106
107                 if (gm_status != GM_SUCCESS) {
108                         fprintf(stderr, "gm_open: %s\n",gm_strerror(gm_status));
109                         goto out_0;
110                 }
111         }
112
113         if (name == NULL) {
114                 local_id = 1;
115                 name = "<local>";
116         } else {
117                 gm_status = gm_host_name_to_node_id_ex(gm_port, 1000000, name,
118                                                        &local_id);
119                 if (gm_status != GM_SUCCESS) {
120                         fprintf(stderr, "gm_host_name_to_node_id_ex(%s): %s\n",
121                                 name, gm_strerror(gm_status));
122                         goto out_1;
123                 }
124         }
125
126         gm_status = gm_node_id_to_global_id(gm_port, local_id, &global_id) ;
127         if (gm_status != GM_SUCCESS) {
128                 fprintf(stderr, "gm_node_id_to_global_id(%s:%d): %s\n",
129                         name, local_id, gm_strerror(gm_status));
130                 goto out_1;
131         }
132
133         if (name_fieldlen > 0)
134                 printf ("%*s ", name_fieldlen, name);
135
136         if (!show_local_id)
137                 printf("0x%x\n", global_id);
138         else
139                 printf("local 0x%x global 0x%x\n", local_id, global_id);
140
141  out_1:
142         gm_close(gm_port);
143  out_0:
144         gm_finalize();
145
146         return gm_status;
147 }
148
149 int
150 main (int argc, char **argv)
151 {
152         int                 c;
153         gm_status_t         gmrc;
154         int                 rc;
155         int                 max_namelen = 0;
156         int                 show_local_id = 0;
157
158         while ((c = getopt(argc, argv, "n:lLh")) != -1)
159                 switch(c) {
160                 case 'h':
161                         usage(argv[0], 1);
162                         return 0;
163
164                 case 'L':
165                         show_local_id = 1;
166                         break;
167
168                 case 'n':
169                         gmrc = print_gmid(optarg, 0, show_local_id);
170                         return (gmrc == GM_SUCCESS) ? 0 : 1;
171
172                 case 'l':
173                         gmrc = print_gmid(NULL, 0, show_local_id);
174                         return (gmrc == GM_SUCCESS) ? 0 : 1;
175
176                 default:
177                         usage(argv[0], 0);
178                         return 2;
179                 }
180
181         if (optind == argc) {
182                 gmrc = print_gmid(NULL, 0, show_local_id);
183                 return (gmrc == GM_SUCCESS) ? 0 : 1;
184         }
185
186         if (optind != argc - 1)
187                 for (c = optind; c < argc; c++)
188                         if (strlen(argv[c]) > max_namelen)
189                                 max_namelen = strlen(argv[c]);
190
191         rc = 0;
192
193         for (c = optind; c < argc; c++) {
194                 gmrc = print_gmid(argv[c], max_namelen, show_local_id);
195
196                 if (gmrc != GM_SUCCESS)
197                         rc = 1;
198         }
199
200         return rc;
201 }