Whamcloud - gitweb
b=16098
[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   *  Copyright (c) 2003 Los Alamos National Laboratory (LANL)
5   *  Copyright (C) 2005 Cluster File Systems, Inc. All rights reserved.
6   *
7   *   This file is part of Lustre, http://www.lustre.org/
8   *
9   *   This file is free software; you can redistribute it and/or
10   *   modify it under the terms of version 2.1 of the GNU Lesser General
11   *   Public License as published by the Free Software Foundation.
12   *
13   *   Lustre is distributed in the hope that it will be useful,
14   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   *   GNU Lesser General Public License for more details.
17   *
18   *   You should have received a copy of the GNU Lesser General Public
19   *   License along with Portals; if not, write to the Free Software
20   *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21   */
22
23 #include <stdio.h>
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <netinet/tcp.h>
27 #include <netdb.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <fcntl.h>
31 #include <sys/ioctl.h>
32 #include <unistd.h>
33 #include <syslog.h>
34 #include <errno.h>
35
36 #include <lnet/api-support.h>
37 #include <lnet/lib-types.h>
38
39 #include <gm.h>
40
41 /*
42  *      portals always uses unit 0
43  *      Can this be configurable?
44  */
45 #define GM_UNIT 0
46
47 void
48 usage(char *prg, int h)
49 {
50         fprintf(stderr,
51                 "usage %s -h\n"
52                 "      %s [-l] [-n hostname] [-L] [hostnames]\n", prg);
53
54         if (h)
55                 printf("Print Myrinet Global network ids for specified hosts\n"
56                        "-l                    print local host's ID\n"
57                        "-n hostname           print given host's ID\n"
58                        "-L                    print Myringet local net ID too\n"
59                        "[hostnames]           print ids of given hosts (local if none)\n");
60 }
61
62 gm_status_t
63 print_gmid(char *name, int name_fieldlen, int show_local_id)
64 {
65         struct gm_port *gm_port;
66         int             gm_port_id;
67         gm_status_t     gm_status;
68         unsigned int    local_id;
69         unsigned int    global_id;
70
71         gm_status = gm_init();
72         if (gm_status != GM_SUCCESS) {
73                 fprintf(stderr, "gm_init: %s\n", gm_strerror(gm_status));
74                 return gm_status;
75         }
76
77         gm_port_id = 2;
78         gm_status = gm_open(&gm_port, GM_UNIT, gm_port_id, "gmnalnid",
79                             GM_API_VERSION);
80         if (gm_status != GM_SUCCESS) {
81                 int num_ports = gm_num_ports(gm_port);
82
83                 /* Couldn't open port 2, try 4 ... num_ports */
84                 for (gm_port_id = 4; gm_port_id < num_ports; gm_port_id++) {
85                         gm_status = gm_open(&gm_port, GM_UNIT, gm_port_id,
86                                             "gmnalnid", GM_API_VERSION);
87                         if (gm_status == GM_SUCCESS)
88                                 break;
89                 }
90
91                 if (gm_status != GM_SUCCESS) {
92                         fprintf(stderr, "gm_open: %s\n",gm_strerror(gm_status));
93                         goto out_0;
94                 }
95         }
96
97         if (name == NULL) {
98                 local_id = 1;
99                 name = "<local>";
100         } else {
101                 gm_status = gm_host_name_to_node_id_ex(gm_port, 1000000, name,
102                                                        &local_id);
103                 if (gm_status != GM_SUCCESS) {
104                         fprintf(stderr, "gm_host_name_to_node_id_ex(%s): %s\n",
105                                 name, gm_strerror(gm_status));
106                         goto out_1;
107                 }
108         }
109
110         gm_status = gm_node_id_to_global_id(gm_port, local_id, &global_id) ;
111         if (gm_status != GM_SUCCESS) {
112                 fprintf(stderr, "gm_node_id_to_global_id(%s:%d): %s\n",
113                         name, local_id, gm_strerror(gm_status));
114                 goto out_1;
115         }
116
117         if (name_fieldlen > 0)
118                 printf ("%*s ", name_fieldlen, name);
119
120         if (!show_local_id)
121                 printf("0x%x\n", global_id);
122         else
123                 printf("local 0x%x global 0x%x\n", local_id, global_id);
124
125  out_1:
126         gm_close(gm_port);
127  out_0:
128         gm_finalize();
129
130         return gm_status;
131 }
132
133 int
134 main (int argc, char **argv)
135 {
136         int                 c;
137         gm_status_t         gmrc;
138         int                 rc;
139         int                 max_namelen = 0;
140         int                 show_local_id = 0;
141
142         while ((c = getopt(argc, argv, "n:lLh")) != -1)
143                 switch(c) {
144                 case 'h':
145                         usage(argv[0], 1);
146                         return 0;
147
148                 case 'L':
149                         show_local_id = 1;
150                         break;
151
152                 case 'n':
153                         gmrc = print_gmid(optarg, 0, show_local_id);
154                         return (gmrc == GM_SUCCESS) ? 0 : 1;
155
156                 case 'l':
157                         gmrc = print_gmid(NULL, 0, show_local_id);
158                         return (gmrc == GM_SUCCESS) ? 0 : 1;
159
160                 default:
161                         usage(argv[0], 0);
162                         return 2;
163                 }
164
165         if (optind == argc) {
166                 gmrc = print_gmid(NULL, 0, show_local_id);
167                 return (gmrc == GM_SUCCESS) ? 0 : 1;
168         }
169
170         if (optind != argc - 1)
171                 for (c = optind; c < argc; c++)
172                         if (strlen(argv[c]) > max_namelen)
173                                 max_namelen = strlen(argv[c]);
174
175         rc = 0;
176
177         for (c = optind; c < argc; c++) {
178                 gmrc = print_gmid(argv[c], max_namelen, show_local_id);
179
180                 if (gmrc != GM_SUCCESS)
181                         rc = 1;
182         }
183
184         return rc;
185 }