Whamcloud - gitweb
- ph_subsys can be 7 symbols in HEAD
[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                 int num_ports = gm_num_ports(gm_port);
75
76                 /* Couldn't open port 2, try 4 ... num_ports */
77                 for (gm_port_id = 4; gm_port_id < num_ports; gm_port_id++) {
78                         gm_status = gm_open(&gm_port, GM_UNIT, gm_port_id,
79                                             "gmnalnid", GM_API_VERSION);
80                         if (gm_status == GM_SUCCESS)
81                                 break;
82                 }
83
84                 if (gm_status != GM_SUCCESS) {
85                         fprintf(stderr, "gm_open: %s\n",gm_strerror(gm_status));
86                         gm_finalize();
87                         return(0);
88                 }
89         }
90
91         if (get_local_id) {
92                 local_nid = 1;
93         } else {
94                 gm_status = gm_host_name_to_node_id_ex(gm_port, 1000000, name,
95                                                        &local_nid);
96                 if (gm_status != GM_SUCCESS) {
97                         fprintf(stderr, "gm_host_name_to_node_id_ex: %s\n",
98                                 gm_strerror(gm_status));
99                         gm_close(gm_port);
100                         gm_finalize();
101                         return(0);
102                 }
103         }
104
105         gm_status = gm_node_id_to_global_id(gm_port, local_nid, &global_nid) ;
106         if (gm_status != GM_SUCCESS) {
107                 fprintf(stderr, "gm_node_id_to_global_id: %s\n",
108                         gm_strerror(gm_status));
109                 gm_close(gm_port);
110                 gm_finalize();
111                 return(0);
112         }
113         gm_close(gm_port);
114         gm_finalize();
115         return(global_nid);
116 }
117
118 int main(int argc, char **argv)
119 {
120         unsigned int        nid = 0;
121         char               *name = NULL;
122         int                 c;
123         int                 get_local_id = 0;
124
125         while ((c = getopt(argc, argv, "n:lh")) != -1) {
126                 switch(c) {
127                 case('n'):
128                         if (get_local_id) {
129                                 usage(argv[0], 0);
130                                 exit(-1);
131                         }
132                         name = optarg;
133                         break;
134                 case('h'):
135                         usage(argv[0], 1);
136                         exit(-1);
137                         break;
138                 case('l'):
139                         if (name) {
140                                 usage(argv[0], 0);
141                                 exit(-1);
142                         }
143                         get_local_id = 1;
144                         break;
145                 default:
146                         usage(argv[0], 0);
147                         exit(-1);
148                 }
149         }
150
151         if (!name && !get_local_id) {
152                 usage(argv[0], 0);
153                 exit(-1);
154         }
155
156         nid = u_getgmnid(name, get_local_id);
157         printf("%u\n", nid);
158         exit(0);
159 }