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