Whamcloud - gitweb
file jbd-stats-2.6.9.patch was initially added on branch b1_4.
[fs/lustre-release.git] / lnet / ulnds / socklnd / address.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 2002 Cray Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   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 General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 /* address.c:
23  * this file provides functions to aquire the IP address of the node
24  * and translate them into a NID/PID pair which supports a static
25  * mapping of virtual nodes into the port range of an IP socket.
26 */
27
28 #define DEBUG_SUBSYSTEM S_NAL
29
30 #include <stdlib.h>
31 #include <netdb.h>
32 #include <unistd.h>
33 #include <stdio.h>
34 #include <portals/p30.h>
35 #include <bridge.h>
36 #include <ipmap.h>
37
38
39 /* Function:  get_node_id
40  * Returns: a 32 bit id for this node, actually a big-endian IP address
41  *
42  * get_node_id() determines the host name and uses the resolver to
43  *  find out its ip address. This is fairly fragile and inflexible, but
44  *  explicitly asking about interfaces and their addresses is very
45  *  complicated and nonportable.
46  */
47 static unsigned int get_node_id(void)
48 {
49     char buffer[255];
50     unsigned int x;
51     struct hostent *he;
52     char * host_envp;
53
54     if (!(host_envp = getenv("PTL_HOSTID")))
55         {
56             gethostname(buffer,sizeof(buffer));
57             he=gethostbyname(buffer);
58             if (he)
59                     x=*(unsigned int *)he->h_addr_list[0];
60             else
61                     x = 0;
62             return(ntohl(x));
63         }
64     else
65         {
66             if (host_envp[1] != 'x')
67                 {
68                     int a, b, c, d;
69                     sscanf(host_envp, "%d.%d.%d.%d", &a, &b, &c, &d);
70                     return ((a<<24) | (b<<16) | (c<<8) | d);
71                 }
72             else
73                 {
74                     long long hostid = strtoll(host_envp, 0, 0);
75                     return((unsigned int) hostid);
76                 }
77         }
78 }
79
80
81 /* Function:  set_address
82  * Arugments: t: a procnal structure to populate with the request
83  *
84  * set_address performs the bit manipulations to set the nid, pid, and
85  *    iptop8 fields of the procnal structures.
86  *
87  * TODO: fix pidrequest to try to do dynamic binding if PTL_ID_ANY
88  */
89
90 #ifdef DIRECT_IP_MODE
91 void set_address(bridge t,ptl_pid_t pidrequest)
92 {
93     int port;
94     if (pidrequest==(unsigned short)PTL_PID_ANY) port = 0;
95     else port=pidrequest;
96     t->lib_nal->libnal_ni.ni_pid.nid=get_node_id();
97     t->lib_nal->libnal_ni.ni_pid.pid=port;
98 }
99 #else
100
101 void set_address(bridge t,ptl_pid_t pidrequest)
102 {
103     int virtnode, in_addr, port;
104     ptl_pid_t pid;
105
106     /* get and remember my node id*/
107     if (!getenv("PTL_VIRTNODE"))
108         virtnode = 0;
109     else
110         {
111             int maxvnode = PNAL_VNODE_MASK - (PNAL_BASE_PORT
112                                               >> PNAL_VNODE_SHIFT);
113             virtnode = atoi(getenv("PTL_VIRTNODE"));
114             if (virtnode > maxvnode)
115                 {
116                     fprintf(stderr, "PTL_VIRTNODE of %d is too large - max %d\n",
117                             virtnode, maxvnode);
118                     return;
119                 }
120         }
121
122     in_addr = get_node_id();
123
124     t->iptop8 = in_addr >> PNAL_HOSTID_SHIFT;/* for making new connections */
125     t->lib_nal->libnal_ni.ni_pid.nid = ((in_addr & PNAL_HOSTID_MASK)
126                                         << PNAL_VNODE_SHIFT)
127                                        + virtnode;
128     pid=pidrequest;
129     /* TODO: Support of pid PTL_ID_ANY with virtual nodes needs more work. */
130 #ifdef notyet
131     if (pid==(unsigned short)PTL_PID_ANY) port = 0;
132 #endif
133     if (pid==(unsigned short)PTL_PID_ANY)
134         {
135             fprintf(stderr, "portal pid PTL_ID_ANY is not currently supported\n");
136             return;
137         }
138     else if (pid > PNAL_PID_MASK)
139         {
140             fprintf(stderr, "portal pid of %d is too large - max %d\n",
141                     pid, PNAL_PID_MASK);
142             return;
143         }
144     else port = ((virtnode << PNAL_VNODE_SHIFT) + pid) + PNAL_BASE_PORT;
145     t->lib_nal->libnal_ni.ni_pid.pid=pid;
146 }
147 #endif