Whamcloud - gitweb
- change the prefix of bulk descriptors to bd_ and bulk pages to bp_
[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;
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_service *service = ev->mem_desc.user_ptr;
109
110         LASSERT ((ev->mem_desc.options & PTL_MD_IOV) == 0); /* requests always contiguous */
111         
112         if (ev->rlength != ev->mlength)
113                 CERROR("Warning: Possibly truncated rpc (%d/%d)\n",
114                        ev->mlength, ev->rlength);
115
116         if (ev->type == PTL_EVENT_PUT)
117                 wake_up(&service->srv_waitq);
118         else
119                 CERROR("Unexpected event type: %d\n", ev->type);
120
121         return 0;
122 }
123
124 static int bulk_source_callback(ptl_event_t *ev)
125 {
126         struct ptlrpc_bulk_desc *desc = ev->mem_desc.user_ptr;
127         struct ptlrpc_bulk_page *bulk;
128         struct list_head        *tmp;
129         struct list_head        *next;
130         ENTRY;
131
132         /* 1 fragment for each page always */
133         LASSERT (ev->mem_desc.niov == desc->bd_page_count);
134         
135         if (ev->type == PTL_EVENT_SENT) {
136                 CDEBUG(D_NET, "got SENT event\n");
137         } else if (ev->type == PTL_EVENT_ACK) {
138                 CDEBUG(D_NET, "got ACK event\n");
139                 
140                 list_for_each_safe(tmp, next, &desc->bd_page_list) {
141                         bulk = list_entry(tmp, struct ptlrpc_bulk_page, bp_link);
142                         
143                         if (bulk->bp_cb != NULL)
144                                 bulk->bp_cb(bulk);
145                 }
146                 desc->bd_flags |= PTL_BULK_FL_SENT;
147                 wake_up(&desc->bd_waitq);
148                 if (desc->bd_cb != NULL)
149                         desc->bd_cb(desc, desc->bd_cb_data);
150         } else {
151                 CERROR("Unexpected event type!\n");
152                 LBUG();
153         }
154
155         RETURN(1);
156 }
157
158 static int bulk_sink_callback(ptl_event_t *ev)
159 {
160         struct ptlrpc_bulk_desc *desc = ev->mem_desc.user_ptr;
161         struct ptlrpc_bulk_page *bulk;
162         struct list_head        *tmp;
163         struct list_head        *next;
164         ptl_size_t               total = 0;
165         ENTRY;
166
167         if (ev->type == PTL_EVENT_PUT) {
168                 /* put with zero offset */
169                 LASSERT (ev->offset == 0);
170                 /* used iovs */
171                 LASSERT ((ev->mem_desc.options & PTL_MD_IOV) != 0);
172                 /* 1 fragment for each page always */
173                 LASSERT (ev->mem_desc.niov == desc->bd_page_count);
174
175                 list_for_each_safe (tmp, next, &desc->bd_page_list) {
176                         bulk = list_entry(tmp, struct ptlrpc_bulk_page, bp_link);
177
178                         total += bulk->bp_buflen;
179                         
180                         if (bulk->bp_cb != NULL)
181                                 bulk->bp_cb(bulk);
182                 }
183
184                 LASSERT (ev->mem_desc.length == total);
185                 
186                 desc->bd_flags |= PTL_BULK_FL_RCVD;
187                 wake_up(&desc->bd_waitq);
188                 if (desc->bd_cb != NULL)
189                         desc->bd_cb(desc, desc->bd_cb_data);
190         } else {
191                 CERROR("Unexpected event type!\n");
192                 LBUG();
193         }
194
195         RETURN(1);
196 }
197
198 int ptlrpc_init_portals(void)
199 {
200         int rc;
201         ptl_handle_ni_t ni;
202
203         socknal_nip = inter_module_get_request("ksocknal_ni", "ksocknal");
204         qswnal_nip = inter_module_get_request("kqswnal_ni", "kqswnal");
205         if (socknal_nip == NULL && qswnal_nip == NULL) {
206                 CERROR("get_ni failed: is a NAL module loaded?\n");
207                 return -EIO;
208         }
209
210         /* Use the qswnal if it's there */
211         if (qswnal_nip != NULL)
212                 ni = *qswnal_nip;
213         else
214                 ni = *socknal_nip;
215
216         rc = PtlEQAlloc(ni, 1024, request_out_callback, &request_out_eq);
217         if (rc != PTL_OK)
218                 CERROR("PtlEQAlloc failed: %d\n", rc);
219
220         rc = PtlEQAlloc(ni, 1024, reply_out_callback, &reply_out_eq);
221         if (rc != PTL_OK)
222                 CERROR("PtlEQAlloc failed: %d\n", rc);
223
224         rc = PtlEQAlloc(ni, 1024, reply_in_callback, &reply_in_eq);
225         if (rc != PTL_OK)
226                 CERROR("PtlEQAlloc failed: %d\n", rc);
227
228         rc = PtlEQAlloc(ni, 1024, bulk_source_callback, &bulk_source_eq);
229         if (rc != PTL_OK)
230                 CERROR("PtlEQAlloc failed: %d\n", rc);
231
232         rc = PtlEQAlloc(ni, 1024, bulk_sink_callback, &bulk_sink_eq);
233         if (rc != PTL_OK)
234                 CERROR("PtlEQAlloc failed: %d\n", rc);
235
236         return rc;
237 }
238
239 void ptlrpc_exit_portals(void)
240 {
241         PtlEQFree(request_out_eq);
242         PtlEQFree(reply_out_eq);
243         PtlEQFree(reply_in_eq);
244         PtlEQFree(bulk_source_eq);
245         PtlEQFree(bulk_sink_eq);
246
247         if (qswnal_nip != NULL)
248                 inter_module_put("kqswnal_ni");
249         if (socknal_nip != NULL)
250                 inter_module_put("ksocknal_ni");
251 }