Whamcloud - gitweb
. request buffering parameters as checked out on MCR
[fs/lustre-release.git] / lustre / ptlrpc / events.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 DEBUG_SUBSYSTEM S_RPC
24
25 #include <linux/module.h>
26 #include <linux/obd_support.h>
27 #include <linux/lustre_net.h>
28
29 ptl_handle_eq_t request_out_eq, reply_in_eq, reply_out_eq, bulk_source_eq,
30         bulk_sink_eq;
31 static const ptl_handle_ni_t *socknal_nip = NULL, *qswnal_nip = NULL, *gmnal_nip = NULL;
32
33 /*
34  *  Free the packet when it has gone out
35  */
36 static int request_out_callback(ptl_event_t *ev)
37 {
38         struct ptlrpc_request *req = ev->mem_desc.user_ptr;
39         ENTRY;
40
41         LASSERT ((ev->mem_desc.options & PTL_MD_IOV) == 0); /* requests always contiguous */
42
43         if (ev->type != PTL_EVENT_SENT) {
44                 // XXX make sure we understand all events, including ACK's
45                 CERROR("Unknown event %d\n", ev->type);
46                 LBUG();
47         }
48
49         ptlrpc_req_finished(req);
50         RETURN(1);
51 }
52
53
54 /*
55  *  Free the packet when it has gone out
56  */
57 static int reply_out_callback(ptl_event_t *ev)
58 {
59         ENTRY;
60
61         LASSERT ((ev->mem_desc.options & PTL_MD_IOV) == 0); /* replies always contiguous */
62
63         if (ev->type == PTL_EVENT_SENT) {
64                 OBD_FREE(ev->mem_desc.start, ev->mem_desc.length);
65         } else {
66                 // XXX make sure we understand all events, including ACK's
67                 CERROR("Unknown event %d\n", ev->type);
68                 LBUG();
69         }
70
71         RETURN(1);
72 }
73
74 /*
75  * Wake up the thread waiting for the reply once it comes in.
76  */
77 static int reply_in_callback(ptl_event_t *ev)
78 {
79         struct ptlrpc_request *req = ev->mem_desc.user_ptr;
80         ENTRY;
81
82         LASSERT ((ev->mem_desc.options & PTL_MD_IOV) == 0); /* replies always contiguous */
83
84         if (req->rq_xid == 0x5a5a5a5a5a5a5a5a) {
85                 CERROR("Reply received for freed request!  Probably a missing "
86                        "ptlrpc_abort()\n");
87                 LBUG();
88         }
89
90         if (req->rq_xid != ev->match_bits) {
91                 CERROR("Reply packet for wrong request\n");
92                 LBUG();
93         }
94
95         if (ev->type == PTL_EVENT_PUT) {
96                 req->rq_repmsg = ev->mem_desc.start + ev->offset;
97                 barrier();
98                 wake_up(&req->rq_wait_for_rep);
99         } else {
100                 // XXX make sure we understand all events, including ACK's
101                 CERROR("Unknown event %d\n", ev->type);
102                 LBUG();
103         }
104
105         RETURN(1);
106 }
107
108 int request_in_callback(ptl_event_t *ev)
109 {
110         struct ptlrpc_request_buffer_desc *rqbd = ev->mem_desc.user_ptr;
111         struct ptlrpc_service *service = rqbd->rqbd_service;
112
113         LASSERT ((ev->mem_desc.options & PTL_MD_IOV) == 0); /* requests always contiguous */
114         LASSERT (ev->type == PTL_EVENT_PUT);    /* we only enable puts */
115         LASSERT (atomic_read (&service->srv_nrqbds_receiving) > 0);
116         LASSERT (atomic_read (&rqbd->rqbd_refcount) > 0);
117         
118         if (ev->rlength != ev->mlength)
119                 CERROR("Warning: Possibly truncated rpc (%d/%d)\n",
120                        ev->mlength, ev->rlength);
121
122         if (ptl_is_valid_handle (&ev->unlinked_me))
123         {
124                 /* This is the last request to be received into this
125                  * request buffer.  We don't bump the refcount, since the
126                  * thread servicing this event is effectively taking over
127                  * portals' reference.
128                  */
129 #warning ev->unlinked_me.nal_idx is not set properly in a callback
130                 LASSERT (ev->unlinked_me.handle_idx == rqbd->rqbd_me_h.handle_idx);
131
132                 if (atomic_dec_and_test (&service->srv_nrqbds_receiving)) /* we're off-air */
133                 {
134                         CERROR ("All request buffers busy\n");
135                         /* we'll probably start dropping packets in portals soon */
136                 }
137         }
138         else
139                 atomic_inc (&rqbd->rqbd_refcount); /* +1 ref for service thread */
140
141         wake_up(&service->srv_waitq);
142
143         return 0;
144 }
145
146 static int bulk_source_callback(ptl_event_t *ev)
147 {
148         struct ptlrpc_bulk_desc *desc = ev->mem_desc.user_ptr;
149         struct ptlrpc_bulk_page *bulk;
150         struct list_head        *tmp;
151         struct list_head        *next;
152         ENTRY;
153
154         CDEBUG(D_NET, "got %s event %d\n",
155                (ev->type == PTL_EVENT_SENT) ? "SENT" :
156                (ev->type == PTL_EVENT_ACK)  ? "ACK"  : "UNEXPECTED", ev->type);
157
158         LASSERT (ev->type == PTL_EVENT_SENT || ev->type == PTL_EVENT_ACK);
159
160         LASSERT (atomic_read (&desc->bd_source_callback_count) > 0 &&
161                  atomic_read (&desc->bd_source_callback_count) <= 2);
162
163         /* 1 fragment for each page always */
164         LASSERT (ev->mem_desc.niov == desc->bd_page_count);
165
166         if (atomic_dec_and_test (&desc->bd_source_callback_count)) {
167                 list_for_each_safe(tmp, next, &desc->bd_page_list) {
168                         bulk = list_entry(tmp, struct ptlrpc_bulk_page,
169                                           bp_link);
170
171                         if (bulk->bp_cb != NULL)
172                                 bulk->bp_cb(bulk);
173                 }
174                 desc->bd_flags |= PTL_BULK_FL_SENT;
175                 wake_up(&desc->bd_waitq);
176                 if (desc->bd_cb != NULL)
177                         desc->bd_cb(desc, desc->bd_cb_data);
178         }
179
180         RETURN(0);
181 }
182
183 static int bulk_sink_callback(ptl_event_t *ev)
184 {
185         struct ptlrpc_bulk_desc *desc = ev->mem_desc.user_ptr;
186         struct ptlrpc_bulk_page *bulk;
187         struct list_head        *tmp;
188         struct list_head        *next;
189         ptl_size_t               total = 0;
190         ENTRY;
191
192         if (ev->type == PTL_EVENT_PUT) {
193                 /* put with zero offset */
194                 LASSERT (ev->offset == 0);
195                 /* used iovs */
196                 LASSERT ((ev->mem_desc.options & PTL_MD_IOV) != 0);
197                 /* 1 fragment for each page always */
198                 LASSERT (ev->mem_desc.niov == desc->bd_page_count);
199
200                 list_for_each_safe (tmp, next, &desc->bd_page_list) {
201                         bulk = list_entry(tmp, struct ptlrpc_bulk_page,
202                                           bp_link);
203
204                         total += bulk->bp_buflen;
205
206                         if (bulk->bp_cb != NULL)
207                                 bulk->bp_cb(bulk);
208                 }
209
210                 LASSERT (ev->mem_desc.length == total);
211
212                 desc->bd_flags |= PTL_BULK_FL_RCVD;
213                 wake_up(&desc->bd_waitq);
214                 if (desc->bd_cb != NULL)
215                         desc->bd_cb(desc, desc->bd_cb_data);
216         } else {
217                 CERROR("Unexpected event type!\n");
218                 LBUG();
219         }
220
221         RETURN(1);
222 }
223
224 int ptlrpc_init_portals(void)
225 {
226         int rc;
227         ptl_handle_ni_t ni;
228
229         socknal_nip = inter_module_get_request("ksocknal_ni", "ksocknal");
230         qswnal_nip = inter_module_get_request("kqswnal_ni", "kqswnal");
231         gmnal_nip = inter_module_get_request("kgmnal_ni", "kgmnal");
232
233         /* Use the qswnal if it's there */
234         if (qswnal_nip != NULL)
235                 ni = *qswnal_nip;
236         else if (gmnal_nip != NULL)
237                 ni = *gmnal_nip;
238         else if (socknal_nip != NULL)
239                 ni = *socknal_nip;
240         else {
241                 CERROR("get_ni failed: is a NAL module loaded?\n");
242                 return -EIO;
243         }
244
245         rc = PtlEQAlloc(ni, 1024, request_out_callback, &request_out_eq);
246         if (rc != PTL_OK)
247                 CERROR("PtlEQAlloc failed: %d\n", rc);
248
249         rc = PtlEQAlloc(ni, 1024, reply_out_callback, &reply_out_eq);
250         if (rc != PTL_OK)
251                 CERROR("PtlEQAlloc failed: %d\n", rc);
252
253         rc = PtlEQAlloc(ni, 1024, reply_in_callback, &reply_in_eq);
254         if (rc != PTL_OK)
255                 CERROR("PtlEQAlloc failed: %d\n", rc);
256
257         rc = PtlEQAlloc(ni, 1024, bulk_source_callback, &bulk_source_eq);
258         if (rc != PTL_OK)
259                 CERROR("PtlEQAlloc failed: %d\n", rc);
260
261         rc = PtlEQAlloc(ni, 1024, bulk_sink_callback, &bulk_sink_eq);
262         if (rc != PTL_OK)
263                 CERROR("PtlEQAlloc failed: %d\n", rc);
264
265         return rc;
266 }
267
268 void ptlrpc_exit_portals(void)
269 {
270         PtlEQFree(request_out_eq);
271         PtlEQFree(reply_out_eq);
272         PtlEQFree(reply_in_eq);
273         PtlEQFree(bulk_source_eq);
274         PtlEQFree(bulk_sink_eq);
275
276         if (qswnal_nip != NULL)
277                 inter_module_put("kqswnal_ni");
278         if (socknal_nip != NULL)
279                 inter_module_put("ksocknal_ni");
280         if (gmnal_nip != NULL)
281                 inter_module_put("kgmnal_ni");
282 }