Whamcloud - gitweb
land b1_4_bgl on HEAD (20050404_1913)
[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 void
48 usage(char *prg, int h)
49 {
50         fprintf(stderr, "usage %s -n hostname | -l | -h\n", prg);
51         if (h) {
52                 printf("\nGet Myrinet Global network ids for specified host\n"
53                        "-l gets network id for local host\n");
54         }
55 }
56
57 unsigned
58 u_getgmnid(char *name, int get_local_id)
59 {
60         struct gm_port *gm_port;
61         int             gm_port_id = 2;
62         gm_status_t     gm_status = GM_SUCCESS;
63         unsigned        global_nid = 0, local_nid = 0; /* gm ids never 0 */
64
65         gm_status = gm_init();
66         if (gm_status != GM_SUCCESS) {
67                 fprintf(stderr, "gm_init: %s\n", gm_strerror(gm_status));
68                 return(0);
69         }
70
71         gm_status = gm_open(&gm_port, GM_UNIT, gm_port_id, "gmnalnid",
72                             GM_API_VERSION);
73         if (gm_status != GM_SUCCESS) {
74                 /* Couldn't open port 2, try 4 5 6 7 */
75                 for (gm_port_id = 4; gm_port_id < 8; gm_port_id++) {
76                         gm_status = gm_open(&gm_port, GM_UNIT, gm_port_id,
77                                             "gmnalnid", GM_API_VERSION);
78                         if (gm_status == GM_SUCCESS)
79                                 break;
80
81                         fprintf(stderr, "gm_open: %s\n",gm_strerror(gm_status));
82                         gm_finalize();
83                         return(0);
84                 }
85         }
86
87         if (get_local_id) {
88                 local_nid = 1;
89         } else {
90                 gm_status = gm_host_name_to_node_id_ex(gm_port, 1000000, name,
91                                                        &local_nid);
92                 if (gm_status != GM_SUCCESS) {
93                         fprintf(stderr, "gm_host_name_to_node_id_ex: %s\n",
94                                 gm_strerror(gm_status));
95                         gm_close(gm_port);
96                         gm_finalize();
97                         return(0);
98                 }
99         }
100
101         gm_status = gm_node_id_to_global_id(gm_port, local_nid, &global_nid) ;
102         if (gm_status != GM_SUCCESS) {
103                 fprintf(stderr, "gm_node_id_to_global_id: %s\n",
104                         gm_strerror(gm_status));
105                 gm_close(gm_port);
106                 gm_finalize();
107                 return(0);
108         }
109         gm_close(gm_port);
110         gm_finalize();
111         return(global_nid);
112 }
113
114 int main(int argc, char **argv)
115 {
116         unsigned int        nid = 0;
117         char               *name = NULL;
118         int                 c;
119         int                 get_local_id = 0;
120
121         while ((c = getopt(argc, argv, "n:lh")) != -1) {
122                 switch(c) {
123                 case('n'):
124                         if (get_local_id) {
125                                 usage(argv[0], 0);
126                                 exit(-1);
127                         }
128                         name = optarg;
129                         break;
130                 case('h'):
131                         usage(argv[0], 1);
132                         exit(-1);
133                         break;
134                 case('l'):
135                         if (name) {
136                                 usage(argv[0], 0);
137                                 exit(-1);
138                         }
139                         get_local_id = 1;
140                         break;
141                 default:
142                         usage(argv[0], 0);
143                         exit(-1);
144                 }
145         }
146
147         if (!name && !get_local_id) {
148                 usage(argv[0], 0);
149                 exit(-1);
150         }
151
152         nid = u_getgmnid(name, get_local_id);
153         printf("%u\n", nid);
154         exit(0);
155 }