Whamcloud - gitweb
- simplified ptlrpc_service by using ME auto-unlinking
[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/config.h>
26 #include <linux/module.h>
27 #include <linux/kernel.h>
28
29 #define DEBUG_SUBSYSTEM S_RPC
30
31 #include <linux/obd_support.h>
32 #include <linux/obd_class.h>
33 #include <linux/lustre_net.h>
34
35 ptl_handle_eq_t sent_pkt_eq, rcvd_rep_eq, bulk_source_eq, 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 sent_packet_callback(ptl_event_t *ev, void *data)
42 {
43         ENTRY;
44
45         if (ev->type == PTL_EVENT_SENT) {
46                 OBD_FREE(ev->mem_desc.start, ev->mem_desc.length);
47         } else { 
48                 // XXX make sure we understand all events, including ACK's
49                 CERROR("Unknown event %d\n", ev->type); 
50                 LBUG();
51         }
52
53         EXIT;
54         return 1;
55 }
56
57 /*
58  * Wake up the thread waiting for the reply once it comes in.
59  */
60 static int rcvd_reply_callback(ptl_event_t *ev, void *data)
61 {
62         struct ptlrpc_request *rpc = ev->mem_desc.user_ptr;
63         ENTRY;
64
65         if (ev->type == PTL_EVENT_PUT) {
66                 rpc->rq_repbuf = ev->mem_desc.start + ev->offset;
67                 barrier();
68                 wake_up_interruptible(&rpc->rq_wait_for_rep);
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         EXIT;
76         return 1;
77 }
78
79 int server_request_callback(ptl_event_t *ev, void *data)
80 {
81         struct ptlrpc_service *service = data;
82         int index;
83
84         if (ev->rlength != ev->mlength)
85                 CERROR("Warning: Possibly truncated rpc (%d/%d)\n",
86                        ev->mlength, ev->rlength);
87
88         spin_lock(&service->srv_lock); 
89         for (index = 0; index < service->srv_ring_length; index++)
90                 if ( service->srv_buf[index] == ev->mem_desc.start) 
91                         break;
92
93         if (index == service->srv_ring_length)
94                 LBUG();
95
96         service->srv_ref_count[index]++;
97
98         if (ev->unlinked_me != -1) {
99                 int idx;
100
101                 for (idx = 0; idx < service->srv_ring_length; idx++)
102                         if (service->srv_me_h[idx] == ev->unlinked_me)
103                                 break;
104                 if (idx == service->srv_ring_length)
105                         LBUG();
106
107                 CDEBUG(D_NET, "unlinked %d\n", idx); 
108                 service->srv_me_h[idx] = 0; 
109
110                 if (service->srv_ref_count[idx] == 0)
111                         ptlrpc_link_svc_me(service, idx); 
112         }
113
114         spin_unlock(&service->srv_lock); 
115         if (ev->type == PTL_EVENT_PUT)
116                 wake_up(&service->srv_waitq);
117         else
118                 CERROR("Unexpected event type: %d\n", ev->type);
119
120         return 0;
121 }
122
123 static int bulk_source_callback(ptl_event_t *ev, void *data)
124 {
125         struct ptlrpc_bulk_desc *bulk = ev->mem_desc.user_ptr;
126
127         ENTRY;
128
129         if (ev->type == PTL_EVENT_SENT) {
130                 CDEBUG(D_NET, "got SENT event\n");
131         } else if (ev->type == PTL_EVENT_ACK) {
132                 CDEBUG(D_NET, "got ACK event\n");
133                 bulk->b_flags = PTL_BULK_SENT;
134                 wake_up_interruptible(&bulk->b_waitq);
135         } else {
136                 CERROR("Unexpected event type!\n");
137                 LBUG();
138         }
139
140         EXIT;
141         return 1;
142 }
143
144 static int bulk_sink_callback(ptl_event_t *ev, void *data)
145 {
146         struct ptlrpc_bulk_desc *bulk = ev->mem_desc.user_ptr;
147
148         ENTRY;
149
150         if (ev->type == PTL_EVENT_PUT) {
151                 if (bulk->b_buf != ev->mem_desc.start + ev->offset)
152                         CERROR("bulkbuf != mem_desc -- why?\n");
153                 bulk->b_flags = PTL_BULK_RCVD;
154                 if (bulk->b_cb != NULL)
155                         bulk->b_cb(bulk, data);
156                 wake_up_interruptible(&bulk->b_waitq);
157         } else {
158                 CERROR("Unexpected event type!\n");
159                 LBUG();
160         }
161
162         /* FIXME: This should happen unconditionally */
163         if (bulk->b_cb != NULL)
164                 OBD_FREE(bulk, sizeof(*bulk));
165
166         EXIT;
167         return 1;
168 }
169
170 int ptlrpc_init_portals(void)
171 {
172         int rc;
173         ptl_handle_ni_t ni;
174
175         socknal_nip = inter_module_get_request("ksocknal_ni", "ksocknal");
176         qswnal_nip = inter_module_get_request("kqswnal_ni", "kqswnal");
177         if (socknal_nip == NULL && qswnal_nip == NULL) {
178                 CERROR("get_ni failed: is a NAL module loaded?\n");
179                 return -EIO;
180         }
181
182         /* Use the qswnal if it's there */
183         if (qswnal_nip != NULL)
184                 ni = *qswnal_nip;
185         else
186                 ni = *socknal_nip;
187
188         rc = PtlEQAlloc(ni, 128, sent_packet_callback, NULL, &sent_pkt_eq);
189         if (rc != PTL_OK)
190                 CERROR("PtlEQAlloc failed: %d\n", rc);
191
192         rc = PtlEQAlloc(ni, 128, rcvd_reply_callback, NULL, &rcvd_rep_eq);
193         if (rc != PTL_OK)
194                 CERROR("PtlEQAlloc failed: %d\n", rc);
195
196         rc = PtlEQAlloc(ni, 128, bulk_source_callback, NULL, &bulk_source_eq);
197         if (rc != PTL_OK)
198                 CERROR("PtlEQAlloc failed: %d\n", rc);
199
200         rc = PtlEQAlloc(ni, 128, bulk_sink_callback, NULL, &bulk_sink_eq);
201         if (rc != PTL_OK)
202                 CERROR("PtlEQAlloc failed: %d\n", rc);
203
204         return rc;
205 }
206
207 void ptlrpc_exit_portals(void)
208 {
209         PtlEQFree(sent_pkt_eq);
210         PtlEQFree(rcvd_rep_eq);
211         PtlEQFree(bulk_source_eq);
212         PtlEQFree(bulk_sink_eq);
213
214         if (qswnal_nip != NULL)
215                 inter_module_put("kqswnal_ni");
216         if (socknal_nip != NULL)
217                 inter_module_put("ksocknal_ni");
218 }