Whamcloud - gitweb
Minor bugfix to allow multiple networked operations:
[fs/lustre-release.git] / lustre / ptlrpc / rpc.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2002 Cluster File Systems, 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
23 #define EXPORT_SYMTAB
24
25 #include <linux/config.h>
26 #include <linux/module.h>
27 #include <linux/kernel.h>
28
29 #include <linux/obd_support.h>
30 #include <linux/lustre_net.h>
31
32 static ptl_handle_eq_t req_eq;
33
34 static int request_callback(ptl_event_t *ev, void *data)
35 {
36         struct ptlrpc_request *rpc = ev->mem_desc.user_ptr;
37
38         ENTRY;
39
40         if (ev->type == PTL_EVENT_SENT) {
41                 kfree(ev->mem_desc.start);
42         } else if (ev->type == PTL_EVENT_PUT) {
43                 rpc->rq_repbuf = ev->mem_desc.start + ev->offset;
44                 wake_up_interruptible(&rpc->rq_wait_for_rep);
45         }
46
47         EXIT;
48         return 1;
49 }
50
51 static int incoming_callback(ptl_event_t *ev, void *data)
52 {
53         struct ptlrpc_service *service = data;
54
55         ENTRY;
56
57         if (ev->type == PTL_EVENT_PUT) {
58                 wake_up(service->srv_wait_queue);
59         } else {
60                 printk("Unexpected event type: %d\n", ev->type);
61         }
62
63         EXIT;
64         return 0;
65 }
66
67 int ptl_send_buf(struct ptlrpc_request *request, struct lustre_peer *peer,
68                  int portal, int is_request)
69 {
70         int rc;
71         ptl_process_id_t remote_id;
72         ptl_handle_md_t md_h;
73
74         if (is_request) {
75                 request->rq_req_md.start = request->rq_reqbuf;
76                 request->rq_req_md.length = request->rq_reqlen;
77         } else {
78                 request->rq_req_md.start = request->rq_repbuf;
79                 request->rq_req_md.length = request->rq_replen;
80         }
81         request->rq_req_md.threshold = PTL_MD_THRESH_INF;
82         request->rq_req_md.options = PTL_MD_OP_PUT;
83         request->rq_req_md.user_ptr = request;
84         request->rq_req_md.eventq = req_eq;
85
86         rc = PtlMDBind(peer->peer_ni, request->rq_req_md, &md_h);
87         if (rc != 0) {
88                 printk(__FUNCTION__ ": PtlMDBind failed: %d\n", rc);
89                 return rc;
90         }
91
92         remote_id.addr_kind = PTL_ADDR_NID;
93         remote_id.nid = peer->peer_nid;
94         remote_id.pid = 0;
95
96         rc = PtlPut(md_h, PTL_NOACK_REQ, remote_id, portal, 0, request->rq_xid,
97                     0, 0);
98         if (rc != PTL_OK) {
99                 printk(__FUNCTION__ ": PtlPut failed: %d\n", rc);
100                 /* FIXME: tear down md */
101         }
102
103         return rc;
104 }
105
106 int ptl_send_rpc(struct ptlrpc_request *request, struct lustre_peer *peer)
107 {
108         ptl_handle_me_t me_h;
109         ptl_process_id_t local_id;
110         int rc;
111
112         ENTRY;
113
114         request->rq_repbuf = kmalloc(request->rq_replen, GFP_KERNEL); 
115         if (!request->rq_repbuf) { 
116                 EXIT;
117                 return -ENOMEM;
118         }
119
120         local_id.addr_kind = PTL_ADDR_GID;
121         local_id.gid = PTL_ID_ANY;
122         local_id.rid = PTL_ID_ANY;
123
124         rc = PtlMEAttach(peer->peer_ni, request->rq_reply_portal, local_id,
125                          request->rq_xid, 0, PTL_UNLINK, &me_h);
126         if (rc != PTL_OK) {
127                 EXIT;
128                 /* FIXME: tear down EQ, free reqbuf */
129                 return rc;
130         }
131
132         request->rq_reply_md.start = request->rq_repbuf;
133         request->rq_reply_md.length = request->rq_replen;
134         request->rq_reply_md.threshold = 1;
135         request->rq_reply_md.options = PTL_MD_OP_PUT;
136         request->rq_reply_md.user_ptr = request;
137         request->rq_reply_md.eventq = req_eq;
138
139         rc = PtlMDAttach(me_h, request->rq_reply_md, PTL_UNLINK,
140                          &request->rq_reply_md_h);
141         if (rc != PTL_OK) {
142                 EXIT;
143                 return rc;
144         }
145
146         return ptl_send_buf(request, peer, request->rq_req_portal, 1);
147 }
148
149 int rpc_register_service(struct ptlrpc_service *service, char *uuid)
150 {
151         struct lustre_peer peer;
152         int rc;
153
154         rc = kportal_uuid_to_peer(uuid, &peer);
155         if (rc != 0) {
156                 printk("Invalid uuid \"%s\"\n", uuid);
157                 return -EINVAL;
158         }
159
160         service->srv_buf = kmalloc(service->srv_buf_size, GFP_KERNEL);
161         if (service->srv_buf == NULL) {
162                 printk(__FUNCTION__ ": no memory\n");
163                 return -ENOMEM;
164         }
165
166         service->srv_id.addr_kind = PTL_ADDR_GID;
167         service->srv_id.gid = PTL_ID_ANY;
168         service->srv_id.rid = PTL_ID_ANY;
169
170         rc = PtlMEAttach(peer.peer_ni, service->srv_portal, service->srv_id,
171                          0, ~0, PTL_RETAIN, &service->srv_me);
172         if (rc != PTL_OK) {
173                 printk("PtlMEAttach failed: %d\n", rc);
174                 return rc;
175         }
176
177         rc = PtlEQAlloc(peer.peer_ni, 128, incoming_callback, service,
178                         &service->srv_eq);
179         if (rc != PTL_OK) {
180                 printk("PtlEQAlloc failed: %d\n", rc);
181                 return rc;
182         }
183
184         /* FIXME: Build an auto-unlinking MD and build a ring. */
185         /* FIXME: Make sure that these are reachable by DMA on well-known
186          * addresses. */
187         service->srv_md.start           = service->srv_buf;
188         service->srv_md.length          = service->srv_buf_size;
189         service->srv_md.threshold       = PTL_MD_THRESH_INF;
190         service->srv_md.options         = PTL_MD_OP_PUT;
191         service->srv_md.user_ptr        = service;
192         service->srv_md.eventq          = service->srv_eq;
193
194         rc = PtlMDAttach(service->srv_me, service->srv_md,
195                          PTL_RETAIN, &service->srv_md_h);
196         if (rc != PTL_OK) {
197                 printk("PtlMDAttach failed: %d\n", rc);
198                 /* FIXME: wow, we need to clean up. */
199                 return rc;
200         }
201
202         return 0;
203 }
204
205 static int req_init_portals(void)
206 {
207         int rc;
208         const ptl_handle_ni_t *nip;
209         ptl_handle_ni_t ni;
210
211         nip = inter_module_get_request(LUSTRE_NAL "_ni", LUSTRE_NAL);
212         if (nip == NULL) {
213                 printk("get_ni failed: is the NAL module loaded?\n");
214                 return -EIO;
215         }
216         ni = *nip;
217
218         rc = PtlEQAlloc(ni, 128, request_callback, NULL, &req_eq);
219         if (rc != PTL_OK)
220                 printk("PtlEQAlloc failed: %d\n", rc);
221
222         return rc;
223 }
224
225 static int __init req_init(void)
226 {
227         return req_init_portals();
228 }
229
230 static void __exit req_exit(void)
231 {
232         PtlEQFree(req_eq);
233
234         inter_module_put(LUSTRE_NAL "_ni");
235
236         return;
237 }
238
239 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
240 MODULE_DESCRIPTION("Lustre Request Processor v1.0");
241 MODULE_LICENSE("GPL"); 
242
243 module_init(req_init);
244 module_exit(req_exit);