Whamcloud - gitweb
. fixed ia64 format warnings
[fs/lustre-release.git] / lnet / ulnds / socklnd / tcplnd.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  *  Copyright (c) 2003 Cluster File Systems, Inc.
6  *
7  *   This file is part of Lustre, http://www.lustre.org.
8  *
9  *   Lustre is free software; you can redistribute it and/or
10  *   modify it under the terms of version 2 of the GNU General Public
11  *   License as published by the Free Software Foundation.
12  *
13  *   Lustre is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with Lustre; if not, write to the Free Software
20  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 /* tcpnal.c:
24    This file implements the TCP-based nal by providing glue
25    between the connection service and the generic NAL implementation */
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <stdarg.h>
30 #include <unistd.h>
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <pqtimer.h>
35 #include <dispatch.h>
36 #include <bridge.h>
37 #include <ipmap.h>
38 #include <connection.h>
39 #include <pthread.h>
40 #include <errno.h>
41 #ifndef __CYGWIN__
42 #include <syscall.h>
43 #endif
44
45 /* Function:  tcpnal_send
46  * Arguments: nal:     pointer to my nal control block
47  *            private: unused
48  *            cookie:  passed back to the portals library
49  *            hdr:     pointer to the portals header
50  *            nid:     destination node
51  *            pid:     destination process
52  *            data:    body of the message
53  *            len:     length of the body
54  * Returns: zero on success
55  *
56  * sends a packet to the peer, after insuring that a connection exists
57  */
58 int tcpnal_send(nal_cb_t *n,
59                 void *private,
60                 lib_msg_t *cookie,
61                 ptl_hdr_t *hdr,
62                 int type,
63                 ptl_nid_t nid,
64                 ptl_pid_t pid,
65                 unsigned int niov,
66                 struct iovec *iov,
67                 size_t len)
68 {
69     connection c;
70     bridge b=(bridge)n->nal_data;
71     struct iovec tiov[257];
72     static pthread_mutex_t send_lock = PTHREAD_MUTEX_INITIALIZER;
73     int   rc;
74     int   total;
75     int i;
76
77     if (!(c=force_tcp_connection((manager)b->lower,
78                                  PNAL_IP(nid,b),
79                                  PNAL_PORT(nid,pid)))) 
80         return(1);
81
82 #if 0
83     /* TODO: these results should be checked. furthermore, provision
84        must be made for the SIGPIPE which is delivered when
85        writing on a tcp socket which has closed underneath
86        the application. there is a linux flag in the sendmsg
87        call which turns off the signally behaviour, but its
88        nonstandard */
89     syscall(SYS_write, c->fd,hdr,sizeof(ptl_hdr_t));
90     LASSERT (niov <= 1);
91     if (len) syscall(SYS_write, c->fd,iov[0].iov_base,len);
92 #else
93     LASSERT (niov <= 256);
94
95     tiov[0].iov_base = hdr;
96     tiov[0].iov_len = sizeof(ptl_hdr_t);
97
98     if (niov > 0)
99             memcpy(&tiov[1], iov, niov * sizeof(struct iovec));
100     pthread_mutex_lock(&send_lock);
101 #if 1
102     for (i = total = 0; i <= niov; i++)
103             total += tiov[i].iov_len;
104     
105     rc = syscall(SYS_writev, c->fd, tiov, niov+1);
106     if (rc != total) {
107             fprintf (stderr, "BAD SEND rc %d != %d, errno %d\n",
108                      rc, total, errno);
109             abort();
110     }
111 #else
112     for (i = total = 0; i <= niov; i++) {
113             rc = send(c->fd, tiov[i].iov_base, tiov[i].iov_len, 0);
114             
115             if (rc != tiov[i].iov_len) {
116                     fprintf (stderr, "BAD SEND rc %d != %d, errno %d\n",
117                              rc, tiov[i].iov_len, errno);
118                     abort();
119             }
120             total != rc;
121     }
122 #endif
123 #if 0
124     fprintf (stderr, "sent %s total %d in %d frags\n", 
125              hdr->type == PTL_MSG_ACK ? "ACK" :
126              hdr->type == PTL_MSG_PUT ? "PUT" :
127              hdr->type == PTL_MSG_GET ? "GET" :
128              hdr->type == PTL_MSG_REPLY ? "REPLY" :
129              hdr->type == PTL_MSG_HELLO ? "HELLO" : "UNKNOWN",
130              total, niov + 1);
131 #endif
132     pthread_mutex_unlock(&send_lock);
133 #endif
134     lib_finalize(n, private, cookie);
135         
136     return(0);
137 }
138
139
140 /* Function:  tcpnal_recv
141  * Arguments: nal_cb_t *nal:     pointer to my nal control block
142  *            void *private:     connection pointer passed through
143  *                               lib_parse()
144  *            lib_msg_t *cookie: passed back to portals library
145  *            user_ptr data:     pointer to the destination buffer
146  *            size_t mlen:       length of the body
147  *            size_t rlen:       length of data in the network
148  * Returns: zero on success
149  *
150  * blocking read of the requested data. must drain out the
151  * difference of mainpulated and requested lengths from the network
152  */
153 int tcpnal_recv(nal_cb_t *n,
154                 void *private,
155                 lib_msg_t *cookie,
156                 unsigned int niov,
157                 struct iovec *iov,
158                 size_t mlen,
159                 size_t rlen)
160
161 {
162     int i;
163
164     if (!niov)
165             goto finalize;
166
167     LASSERT(mlen);
168     LASSERT(rlen);
169     LASSERT(rlen >= mlen);
170
171     /* FIXME
172      * 1. Is this effecient enough? change to use readv() directly?
173      * 2. need check return from read_connection()
174      * - MeiJia
175      */
176     for (i = 0; i < niov; i++)
177         read_connection(private, iov[i].iov_base, iov[i].iov_len);
178
179 finalize:
180     lib_finalize(n, private, cookie);
181
182     if (mlen!=rlen){
183         char *trash=malloc(rlen-mlen);
184         
185         /*TODO: check error status*/
186         read_connection(private,trash,rlen-mlen);
187         free(trash);
188     }
189
190     return(rlen);
191 }
192
193
194 /* Function:  from_connection: 
195  * Arguments: c: the connection to read from 
196  * Returns: whether or not to continue reading from this connection,
197  *          expressed as a 1 to continue, and a 0 to not
198  *
199  *  from_connection() is called from the select loop when i/o is 
200  *  available. It attempts to read the portals header and 
201  *  pass it to the generic library for processing.
202  */
203 static int from_connection(void *a, void *d)
204 {
205     connection c = d;
206     bridge b = a;
207     ptl_hdr_t hdr;
208
209     if (read_connection(c, (unsigned char *)&hdr, sizeof(hdr))){
210         lib_parse(b->nal_cb, &hdr, c);
211         return(1);
212     }
213     return(0);
214 }
215
216
217 static void tcpnal_shutdown(bridge b)
218 {
219     shutdown_connections(b->lower);
220 }
221
222 /* Function:  PTL_IFACE_TCP
223  * Arguments: pid_request: desired port number to bind to
224  *            desired: passed NAL limits structure
225  *            actual: returned NAL limits structure
226  * Returns: a nal structure on success, or null on failure
227  */
228 int tcpnal_init(bridge b)
229 {
230     manager m;
231         
232     b->nal_cb->cb_send=tcpnal_send;
233     b->nal_cb->cb_recv=tcpnal_recv;
234     b->shutdown=tcpnal_shutdown;
235     
236     if (!(m=init_connections(PNAL_PORT(b->nal_cb->ni.nid,
237                                        b->nal_cb->ni.pid),
238                              from_connection,b))){
239         /* TODO: this needs to shut down the
240            newly created junk */
241         return(PTL_NAL_FAILED);
242     }
243     /* XXX cfs hack */
244     b->nal_cb->ni.pid=0;
245     b->lower=m;
246     return(PTL_OK);
247 }