Whamcloud - gitweb
c4dbe2554bfab4e588ccb15f25fd2ead8124313e
[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 <portals/api-support.h>
36 #include <portals/lib-types.h>
37
38 #include <gm.h>
39
40 #define GMNAL_IOC_GET_GNID 1
41 /*
42  *      portals always uses unit 0
43  *      Can this be configurable?
44  */
45 #define GM_UNIT 0
46
47 /*
48  * prototypes
49  */
50 unsigned u_getgmnid(char *name, int get_local_id);
51 void usage(char *prg, int h);
52
53 int main(int argc, char **argv)
54 {
55     unsigned int        nid = 0;
56     char                *name = NULL;
57     int                 c;
58     int                 get_local_id = 0;
59
60
61
62     while ((c = getopt(argc, argv, "n:lh")) != -1) {
63         switch(c) {
64             case('n'):
65                 if (get_local_id) {
66                     usage(argv[0], 0);
67                     exit(-1);
68                 }
69             name = optarg;      
70             break;
71             case('h'):
72                 usage(argv[0], 1);
73                 exit(-1);
74             break;
75             case('l'):
76                 if (name) {
77                     usage(argv[0], 0);
78                     exit(-1);
79                 }
80                 get_local_id = 1;
81             break;
82             default:
83                 usage(argv[0], 0);
84                 exit(-1);
85             }
86     }
87
88     if (!name && !get_local_id) {
89         usage(argv[0], 0);
90         exit(-1);
91     }
92
93     nid = u_getgmnid(name, get_local_id);
94     printf("%u\n", nid);
95     exit(0);
96 }
97
98 unsigned
99 u_getgmnid(char *name, int get_local_id)
100 {
101     struct gm_port      *gm_port;
102     int         gm_port_id = 2;
103     gm_status_t     gm_status = GM_SUCCESS;
104
105     /*
106      *  gm global or local ids are never 0
107      */
108     unsigned    global_nid = 0, local_nid = 0;
109
110     gm_status = gm_init();
111     if (gm_status != GM_SUCCESS) {
112         fprintf(stderr, "gm_init :: %s\n", gm_strerror(gm_status));
113         return(0);
114     }
115         
116     gm_status = gm_open(&gm_port, GM_UNIT, gm_port_id,  
117                             "gmnalnid", GM_API_VERSION);
118
119     if (gm_status != GM_SUCCESS) {
120         /*
121          *      Couldn't open port 2 
122          *      try 4 5 6 7 
123          */
124         
125         for (gm_port_id=4; gm_port_id<8; gm_port_id++) {
126             gm_status = gm_open(&gm_port, 
127                                 GM_UNIT, 
128                                 gm_port_id,  
129                                 "gmnalnid", 
130                                 GM_API_VERSION);
131             if (gm_status == GM_SUCCESS) {
132                 break;
133             }
134         fprintf(stderr, "gm_open :: %s\n", 
135         gm_strerror(gm_status));
136         gm_finalize();
137         return(0);
138         }
139     }
140
141     if (get_local_id) {
142         local_nid = 1;
143     } else {
144         gm_status = gm_host_name_to_node_id_ex(gm_port, 1000000, name, 
145                                                &local_nid);
146         if (gm_status != GM_SUCCESS) {
147             fprintf(stderr, "gm_host_name_to_node_id_ex :: %s\n", 
148             gm_strerror(gm_status));
149             gm_close(gm_port);
150             gm_finalize();
151             return(0);
152         }
153     }
154
155     gm_status = gm_node_id_to_global_id(gm_port, local_nid, &global_nid) ;
156     if (gm_status != GM_SUCCESS) {
157         fprintf(stderr, "gm_node_id_to_global_id :: %s\n", 
158         gm_strerror(gm_status));
159         gm_close(gm_port);
160         gm_finalize();
161         return(0);
162     }
163     gm_close(gm_port);
164     gm_finalize();
165     return(global_nid);
166 }
167
168 void 
169 usage(char *prg, int h)
170 {
171
172     fprintf(stderr, "usage %s -n hostname | -l | -h\n", prg);
173     if (h) {
174         printf("\nGet Myrinet Global network ids for specified host\n");
175         printf("-l gets network id for local host\n");
176     }
177     return;
178 }