Whamcloud - gitweb
Land b_head_libcfs onto HEAD (20080805_1715)
[fs/lustre-release.git] / lnet / utils / gmlndnid.c
1 /*
2  * -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
3  * vim:expandtab:shiftwidth=8:tabstop=8:
4  *
5  * GPL HEADER START
6  *
7  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 only,
11  * as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License version 2 for more details (a copy is included
17  * in the LICENSE file that accompanied this code).
18  *
19  * You should have received a copy of the GNU General Public License
20  * version 2 along with this program; If not, see [sun.com URL with a
21  * copy of GPLv2].
22  *
23  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
24  * CA 95054 USA or visit www.sun.com if you need additional information or
25  * have any questions.
26  *
27  * GPL HEADER END
28  */
29 /*
30  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
31  * Use is subject to license terms.
32  *
33  * Copyright (c) 2003 Los Alamos National Laboratory (LANL)
34  */
35 /*
36  * This file is part of Lustre, http://www.lustre.org/
37  * Lustre is a trademark of Sun Microsystems, Inc.
38  */
39
40 #include <stdio.h>
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <netinet/tcp.h>
44 #include <netdb.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <fcntl.h>
48 #include <sys/ioctl.h>
49 #include <unistd.h>
50 #include <syslog.h>
51 #include <errno.h>
52
53 #include <lnet/api-support.h>
54 #include <lnet/lib-types.h>
55
56 #include <gm.h>
57
58 /*
59  *      portals always uses unit 0
60  *      Can this be configurable?
61  */
62 #define GM_UNIT 0
63
64 void
65 usage(char *prg, int h)
66 {
67         fprintf(stderr,
68                 "usage %s -h\n"
69                 "      %s [-l] [-n hostname] [-L] [hostnames]\n", prg);
70
71         if (h)
72                 printf("Print Myrinet Global network ids for specified hosts\n"
73                        "-l                    print local host's ID\n"
74                        "-n hostname           print given host's ID\n"
75                        "-L                    print Myringet local net ID too\n"
76                        "[hostnames]           print ids of given hosts (local if none)\n");
77 }
78
79 gm_status_t
80 print_gmid(char *name, int name_fieldlen, int show_local_id)
81 {
82         struct gm_port *gm_port;
83         int             gm_port_id;
84         gm_status_t     gm_status;
85         unsigned int    local_id;
86         unsigned int    global_id;
87
88         gm_status = gm_init();
89         if (gm_status != GM_SUCCESS) {
90                 fprintf(stderr, "gm_init: %s\n", gm_strerror(gm_status));
91                 return gm_status;
92         }
93
94         gm_port_id = 2;
95         gm_status = gm_open(&gm_port, GM_UNIT, gm_port_id, "gmnalnid",
96                             GM_API_VERSION);
97         if (gm_status != GM_SUCCESS) {
98                 int num_ports = gm_num_ports(gm_port);
99
100                 /* Couldn't open port 2, try 4 ... num_ports */
101                 for (gm_port_id = 4; gm_port_id < num_ports; gm_port_id++) {
102                         gm_status = gm_open(&gm_port, GM_UNIT, gm_port_id,
103                                             "gmnalnid", GM_API_VERSION);
104                         if (gm_status == GM_SUCCESS)
105                                 break;
106                 }
107
108                 if (gm_status != GM_SUCCESS) {
109                         fprintf(stderr, "gm_open: %s\n",gm_strerror(gm_status));
110                         goto out_0;
111                 }
112         }
113
114         if (name == NULL) {
115                 local_id = 1;
116                 name = "<local>";
117         } else {
118                 gm_status = gm_host_name_to_node_id_ex(gm_port, 1000000, name,
119                                                        &local_id);
120                 if (gm_status != GM_SUCCESS) {
121                         fprintf(stderr, "gm_host_name_to_node_id_ex(%s): %s\n",
122                                 name, gm_strerror(gm_status));
123                         goto out_1;
124                 }
125         }
126
127         gm_status = gm_node_id_to_global_id(gm_port, local_id, &global_id) ;
128         if (gm_status != GM_SUCCESS) {
129                 fprintf(stderr, "gm_node_id_to_global_id(%s:%d): %s\n",
130                         name, local_id, gm_strerror(gm_status));
131                 goto out_1;
132         }
133
134         if (name_fieldlen > 0)
135                 printf ("%*s ", name_fieldlen, name);
136
137         if (!show_local_id)
138                 printf("0x%x\n", global_id);
139         else
140                 printf("local 0x%x global 0x%x\n", local_id, global_id);
141
142  out_1:
143         gm_close(gm_port);
144  out_0:
145         gm_finalize();
146
147         return gm_status;
148 }
149
150 int
151 main (int argc, char **argv)
152 {
153         int                 c;
154         gm_status_t         gmrc;
155         int                 rc;
156         int                 max_namelen = 0;
157         int                 show_local_id = 0;
158
159         while ((c = getopt(argc, argv, "n:lLh")) != -1)
160                 switch(c) {
161                 case 'h':
162                         usage(argv[0], 1);
163                         return 0;
164
165                 case 'L':
166                         show_local_id = 1;
167                         break;
168
169                 case 'n':
170                         gmrc = print_gmid(optarg, 0, show_local_id);
171                         return (gmrc == GM_SUCCESS) ? 0 : 1;
172
173                 case 'l':
174                         gmrc = print_gmid(NULL, 0, show_local_id);
175                         return (gmrc == GM_SUCCESS) ? 0 : 1;
176
177                 default:
178                         usage(argv[0], 0);
179                         return 2;
180                 }
181
182         if (optind == argc) {
183                 gmrc = print_gmid(NULL, 0, show_local_id);
184                 return (gmrc == GM_SUCCESS) ? 0 : 1;
185         }
186
187         if (optind != argc - 1)
188                 for (c = optind; c < argc; c++)
189                         if (strlen(argv[c]) > max_namelen)
190                                 max_namelen = strlen(argv[c]);
191
192         rc = 0;
193
194         for (c = optind; c < argc; c++) {
195                 gmrc = print_gmid(argv[c], max_namelen, show_local_id);
196
197                 if (gmrc != GM_SUCCESS)
198                         rc = 1;
199         }
200
201         return rc;
202 }