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