Whamcloud - gitweb
land b_md onto HEAD. the highlights:
[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                 void (*event_handler)(struct ptlrpc_bulk_desc *);
174
175                 list_for_each_safe(tmp, next, &desc->bd_page_list) {
176                         bulk = list_entry(tmp, struct ptlrpc_bulk_page,
177                                           bp_link);
178
179                         if (bulk->bp_cb != NULL)
180                                 bulk->bp_cb(bulk);
181                 }
182
183                 /* We need to make a note of whether there's an event handler
184                  * before we call wake_up, because if there is no event handler,
185                  * 'desc' might be freed before we're scheduled again. */
186                 event_handler = desc->bd_ptl_ev_hdlr;
187
188                 desc->bd_flags |= PTL_BULK_FL_SENT;
189                 wake_up(&desc->bd_waitq);
190                 if (event_handler) {
191                         LASSERT(desc->bd_ptl_ev_hdlr == event_handler);
192                         event_handler(desc);
193                 }
194         }
195
196         RETURN(0);
197 }
198
199 static int bulk_sink_callback(ptl_event_t *ev)
200 {
201         struct ptlrpc_bulk_desc *desc = ev->mem_desc.user_ptr;
202         struct ptlrpc_bulk_page *bulk;
203         struct list_head        *tmp;
204         struct list_head        *next;
205         ptl_size_t               total = 0;
206         void                   (*event_handler)(struct ptlrpc_bulk_desc *);
207         ENTRY;
208
209         LASSERT(ev->type == PTL_EVENT_PUT);
210
211         /* put with zero offset */
212         LASSERT(ev->offset == 0);
213         /* used iovs */
214         LASSERT((ev->mem_desc.options & PTL_MD_IOV) != 0);
215         /* 1 fragment for each page always */
216         LASSERT(ev->mem_desc.niov == desc->bd_page_count);
217
218         list_for_each_safe (tmp, next, &desc->bd_page_list) {
219                 bulk = list_entry(tmp, struct ptlrpc_bulk_page, bp_link);
220
221                 total += bulk->bp_buflen;
222
223                 if (bulk->bp_cb != NULL)
224                         bulk->bp_cb(bulk);
225         }
226
227         LASSERT(ev->mem_desc.length == total);
228
229         /* We need to make a note of whether there's an event handler
230          * before we call wake_up, because if there is no event
231          * handler, 'desc' might be freed before we're scheduled again. */
232         event_handler = desc->bd_ptl_ev_hdlr;
233
234         desc->bd_flags |= PTL_BULK_FL_RCVD;
235         wake_up(&desc->bd_waitq);
236         if (event_handler) {
237                 LASSERT(desc->bd_ptl_ev_hdlr == event_handler);
238                 event_handler(desc);
239         }
240
241         RETURN(1);
242 }
243
244 int ptlrpc_init_portals(void)
245 {
246         int rc;
247         ptl_handle_ni_t ni;
248
249         /* Use the qswnal if it's there */
250         if ((qswnal_nip = inter_module_get("kqswnal_ni")) != NULL)
251                 ni = *qswnal_nip;
252         else if ((gmnal_nip = inter_module_get("kgmnal_ni")) != NULL)
253                 ni = *gmnal_nip;
254         else if ((socknal_nip = inter_module_get("ksocknal_ni")) != NULL)
255                 ni = *socknal_nip;
256         else if ((toenal_nip = inter_module_get("ktoenal_ni")) != NULL)
257                 ni = *toenal_nip;
258         else {
259                 CERROR("get_ni failed: is a NAL module loaded?\n");
260                 return -EIO;
261         }
262
263         rc = PtlEQAlloc(ni, 1024, request_out_callback, &request_out_eq);
264         if (rc != PTL_OK)
265                 CERROR("PtlEQAlloc failed: %d\n", rc);
266
267         rc = PtlEQAlloc(ni, 1024, reply_out_callback, &reply_out_eq);
268         if (rc != PTL_OK)
269                 CERROR("PtlEQAlloc failed: %d\n", rc);
270
271         rc = PtlEQAlloc(ni, 1024, reply_in_callback, &reply_in_eq);
272         if (rc != PTL_OK)
273                 CERROR("PtlEQAlloc failed: %d\n", rc);
274
275         rc = PtlEQAlloc(ni, 1024, bulk_source_callback, &bulk_source_eq);
276         if (rc != PTL_OK)
277                 CERROR("PtlEQAlloc failed: %d\n", rc);
278
279         rc = PtlEQAlloc(ni, 1024, bulk_sink_callback, &bulk_sink_eq);
280         if (rc != PTL_OK)
281                 CERROR("PtlEQAlloc failed: %d\n", rc);
282
283         return rc;
284 }
285
286 void ptlrpc_exit_portals(void)
287 {
288         PtlEQFree(request_out_eq);
289         PtlEQFree(reply_out_eq);
290         PtlEQFree(reply_in_eq);
291         PtlEQFree(bulk_source_eq);
292         PtlEQFree(bulk_sink_eq);
293
294         if (qswnal_nip != NULL)
295                 inter_module_put("kqswnal_ni");
296         if (socknal_nip != NULL)
297                 inter_module_put("ksocknal_ni");
298         if (gmnal_nip != NULL)
299                 inter_module_put("kgmnal_ni");
300         if (toenal_nip != NULL)
301                 inter_module_put("ktoenal_ni");
302 }