Whamcloud - gitweb
- added connection structure which encompasses some (but perhaps not enough yet)
[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 EXPORT_SYMTAB
24
25 #include <linux/module.h>
26
27 #define DEBUG_SUBSYSTEM S_RPC
28
29 #include <linux/lustre_net.h>
30
31 ptl_handle_eq_t request_out_eq, 
32                 reply_in_eq, 
33                 reply_out_eq,
34                 bulk_source_eq, 
35                 bulk_sink_eq;
36 static const ptl_handle_ni_t *socknal_nip = NULL, *qswnal_nip = NULL;
37
38 /*
39  *  Free the packet when it has gone out
40  */
41 static int request_out_callback(ptl_event_t *ev, void *data)
42 {
43         struct ptlrpc_request *req = ev->mem_desc.user_ptr;
44         struct ptlrpc_client *cl = req->rq_client; 
45         
46         ENTRY;
47
48         if (ev->type == PTL_EVENT_SENT) {
49                 list_del(&req->rq_list);
50                 list_add(&req->rq_list, &cl->cli_sent_head);
51         } else { 
52                 // XXX make sure we understand all events, including ACK's
53                 CERROR("Unknown event %d\n", ev->type); 
54                 LBUG();
55         }
56
57         RETURN(1);
58 }
59
60
61 /*
62  *  Free the packet when it has gone out
63  */
64 static int reply_out_callback(ptl_event_t *ev, void *data)
65 {
66         ENTRY;
67
68         if (ev->type == PTL_EVENT_SENT) {
69                 OBD_FREE(ev->mem_desc.start, ev->mem_desc.length);
70         } else { 
71                 // XXX make sure we understand all events, including ACK's
72                 CERROR("Unknown event %d\n", ev->type); 
73                 LBUG();
74         }
75
76         RETURN(1);
77 }
78
79 /*
80  * Wake up the thread waiting for the reply once it comes in.
81  */
82 static int reply_in_callback(ptl_event_t *ev, void *data)
83 {
84         struct ptlrpc_request *rpc = ev->mem_desc.user_ptr;
85         ENTRY;
86
87         if (ev->type == PTL_EVENT_PUT) {
88                 rpc->rq_repmsg = ev->mem_desc.start + ev->offset;
89                 barrier();
90                 wake_up_interruptible(&rpc->rq_wait_for_rep);
91         } else { 
92                 // XXX make sure we understand all events, including ACK's
93                 CERROR("Unknown event %d\n", ev->type); 
94                 LBUG();
95         }
96
97         RETURN(1);
98 }
99
100 int request_in_callback(ptl_event_t *ev, void *data)
101 {
102         struct ptlrpc_service *service = data;
103         int index;
104
105         if (ev->rlength != ev->mlength)
106                 CERROR("Warning: Possibly truncated rpc (%d/%d)\n",
107                        ev->mlength, ev->rlength);
108
109         spin_lock(&service->srv_lock); 
110         for (index = 0; index < service->srv_ring_length; index++)
111                 if ( service->srv_buf[index] == ev->mem_desc.start) 
112                         break;
113
114         if (index == service->srv_ring_length)
115                 LBUG();
116
117         service->srv_ref_count[index]++;
118
119         if (ptl_is_valid_handle(&ev->unlinked_me)) {
120                 int idx;
121
122                 for (idx = 0; idx < service->srv_ring_length; idx++)
123                         if (service->srv_me_h[idx].handle_idx ==
124                             ev->unlinked_me.handle_idx)
125                                 break;
126                 if (idx == service->srv_ring_length)
127                         LBUG();
128
129                 CDEBUG(D_NET, "unlinked %d\n", idx);
130                 ptl_set_inv_handle(&(service->srv_me_h[idx]));
131
132                 if (service->srv_ref_count[idx] == 0)
133                         ptlrpc_link_svc_me(service, idx); 
134         }
135
136         spin_unlock(&service->srv_lock); 
137         if (ev->type == PTL_EVENT_PUT)
138                 wake_up(&service->srv_waitq);
139         else
140                 CERROR("Unexpected event type: %d\n", ev->type);
141
142         return 0;
143 }
144
145 static int bulk_source_callback(ptl_event_t *ev, void *data)
146 {
147         struct ptlrpc_bulk_desc *bulk = ev->mem_desc.user_ptr;
148
149         ENTRY;
150
151         if (ev->type == PTL_EVENT_SENT) {
152                 CDEBUG(D_NET, "got SENT event\n");
153         } else if (ev->type == PTL_EVENT_ACK) {
154                 CDEBUG(D_NET, "got ACK event\n");
155                 bulk->b_flags = PTL_BULK_SENT;
156                 wake_up_interruptible(&bulk->b_waitq);
157         } else {
158                 CERROR("Unexpected event type!\n");
159                 LBUG();
160         }
161
162         EXIT;
163         return 1;
164 }
165
166 static int bulk_sink_callback(ptl_event_t *ev, void *data)
167 {
168         struct ptlrpc_bulk_desc *bulk = ev->mem_desc.user_ptr;
169
170         ENTRY;
171
172         if (ev->type == PTL_EVENT_PUT) {
173                 if (bulk->b_buf != ev->mem_desc.start + ev->offset)
174                         CERROR("bulkbuf != mem_desc -- why?\n");
175                 bulk->b_flags = PTL_BULK_RCVD;
176                 if (bulk->b_cb != NULL)
177                         bulk->b_cb(bulk, data);
178                 wake_up_interruptible(&bulk->b_waitq);
179         } else {
180                 CERROR("Unexpected event type!\n");
181                 LBUG();
182         }
183
184         /* FIXME: This should happen unconditionally */
185         if (bulk->b_cb != NULL)
186                 OBD_FREE(bulk, sizeof(*bulk));
187
188         EXIT;
189         return 1;
190 }
191
192 int ptlrpc_init_portals(void)
193 {
194         int rc;
195         ptl_handle_ni_t ni;
196
197         socknal_nip = inter_module_get_request("ksocknal_ni", "ksocknal");
198         qswnal_nip = inter_module_get_request("kqswnal_ni", "kqswnal");
199         if (socknal_nip == NULL && qswnal_nip == NULL) {
200                 CERROR("get_ni failed: is a NAL module loaded?\n");
201                 return -EIO;
202         }
203
204         /* Use the qswnal if it's there */
205         if (qswnal_nip != NULL)
206                 ni = *qswnal_nip;
207         else
208                 ni = *socknal_nip;
209
210         rc = PtlEQAlloc(ni, 128, request_out_callback, NULL, &request_out_eq);
211         if (rc != PTL_OK)
212                 CERROR("PtlEQAlloc failed: %d\n", rc);
213
214         rc = PtlEQAlloc(ni, 128, reply_out_callback, NULL, &reply_out_eq);
215         if (rc != PTL_OK)
216                 CERROR("PtlEQAlloc failed: %d\n", rc);
217
218         rc = PtlEQAlloc(ni, 128, reply_in_callback, NULL, &reply_in_eq);
219         if (rc != PTL_OK)
220                 CERROR("PtlEQAlloc failed: %d\n", rc);
221
222         rc = PtlEQAlloc(ni, 128, bulk_source_callback, NULL, &bulk_source_eq);
223         if (rc != PTL_OK)
224                 CERROR("PtlEQAlloc failed: %d\n", rc);
225
226         rc = PtlEQAlloc(ni, 128, bulk_sink_callback, NULL, &bulk_sink_eq);
227         if (rc != PTL_OK)
228                 CERROR("PtlEQAlloc failed: %d\n", rc);
229
230         return rc;
231 }
232
233 void ptlrpc_exit_portals(void)
234 {
235         PtlEQFree(request_out_eq);
236         PtlEQFree(reply_out_eq);
237         PtlEQFree(reply_in_eq);
238         PtlEQFree(bulk_source_eq);
239         PtlEQFree(bulk_sink_eq);
240
241         if (qswnal_nip != NULL)
242                 inter_module_put("kqswnal_ni");
243         if (socknal_nip != NULL)
244                 inter_module_put("ksocknal_ni");
245 }