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