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