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