Whamcloud - gitweb
- fixed some NTOH/HTON mixups
[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                 BUG();
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                 BUG();
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 rc;
83
84         if (ev->rlength != ev->mlength)
85                 CERROR("Warning: Possibly truncated rpc (%d/%d)\n",
86                        ev->mlength, ev->rlength);
87
88         /* The ME is unlinked when there is less than 1024 bytes free
89          * on its MD.  This ensures we are always able to handle the rpc, 
90          * although the 1024 value is a guess as to the size of a
91          * large rpc (the known safe margin should be determined).
92          *
93          * NOTE: The portals API by default unlinks all MD's associated
94          *       with an ME when it's unlinked.  For now, this behavior
95          *       has been commented out of the portals library so the
96          *       MD can be unlinked when its ref count drops to zero.
97          *       A new MD and ME will then be created that use the same
98          *       kmalloc()'ed memory and inserted at the ring tail.
99          */
100
101         service->srv_ref_count[service->srv_md_active]++;
102
103         CDEBUG(D_INODE, "event offset %d buf size %d\n", 
104                ev->offset, service->srv_buf_size);
105         if (ev->offset >= (service->srv_buf_size - 1024)) {
106                 CDEBUG(D_INODE, "Unlinking ME %d\n", service->srv_me_active);
107
108                 rc = PtlMEUnlink(service->srv_me_h[service->srv_me_active]);
109                 service->srv_me_h[service->srv_me_active] = 0;
110
111                 if (rc != PTL_OK) {
112                         CERROR("PtlMEUnlink failed - DROPPING soon: %d\n", rc);
113                         BUG();
114                         return rc;
115                 }
116
117                 service->srv_me_active = NEXT_INDEX(service->srv_me_active,
118                                                     service->srv_ring_length);
119
120                 if (service->srv_me_h[service->srv_me_active] == 0)
121                         CERROR("All %d ring ME's are unlinked!\n",
122                                service->srv_ring_length);
123         }
124
125         if (ev->type == PTL_EVENT_PUT) {
126                 wake_up(&service->srv_waitq);
127         } else {
128                 CERROR("Unexpected event type: %d\n", ev->type);
129         }
130
131         return 0;
132 }
133
134 static int bulk_source_callback(ptl_event_t *ev, void *data)
135 {
136         struct ptlrpc_bulk_desc *bulk = ev->mem_desc.user_ptr;
137
138         ENTRY;
139
140         if (ev->type == PTL_EVENT_SENT) {
141                 CDEBUG(D_NET, "got SENT event\n");
142         } else if (ev->type == PTL_EVENT_ACK) {
143                 CDEBUG(D_NET, "got ACK event\n");
144                 bulk->b_flags = PTL_BULK_SENT;
145                 wake_up_interruptible(&bulk->b_waitq);
146         } else {
147                 CERROR("Unexpected event type!\n");
148                 BUG();
149         }
150
151         EXIT;
152         return 1;
153 }
154
155 static int bulk_sink_callback(ptl_event_t *ev, void *data)
156 {
157         struct ptlrpc_bulk_desc *bulk = ev->mem_desc.user_ptr;
158
159         ENTRY;
160
161         if (ev->type == PTL_EVENT_PUT) {
162                 if (bulk->b_buf != ev->mem_desc.start + ev->offset)
163                         CERROR("bulkbuf != mem_desc -- why?\n");
164                 bulk->b_flags = PTL_BULK_RCVD;
165                 if (bulk->b_cb != NULL)
166                         bulk->b_cb(bulk, data);
167                 wake_up_interruptible(&bulk->b_waitq);
168         } else {
169                 CERROR("Unexpected event type!\n");
170                 BUG();
171         }
172
173         /* FIXME: This should happen unconditionally */
174         if (bulk->b_cb != NULL) {
175                 OBD_FREE(bulk, sizeof(*bulk));
176         }
177
178         EXIT;
179         return 1;
180 }
181
182 int ptlrpc_init_portals(void)
183 {
184         int rc;
185         ptl_handle_ni_t ni;
186
187         socknal_nip = inter_module_get_request("ksocknal_ni", "ksocknal");
188         qswnal_nip = inter_module_get_request("kqswnal_ni", "kqswnal");
189         if (socknal_nip == NULL && qswnal_nip == NULL) {
190                 CERROR("get_ni failed: is a NAL module loaded?\n");
191                 return -EIO;
192         }
193
194         /* Use the qswnal if it's there */
195         if (qswnal_nip != NULL)
196                 ni = *qswnal_nip;
197         else
198                 ni = *socknal_nip;
199
200         rc = PtlEQAlloc(ni, 128, sent_packet_callback, NULL, &sent_pkt_eq);
201         if (rc != PTL_OK)
202                 CERROR("PtlEQAlloc failed: %d\n", rc);
203
204         rc = PtlEQAlloc(ni, 128, rcvd_reply_callback, NULL, &rcvd_rep_eq);
205         if (rc != PTL_OK)
206                 CERROR("PtlEQAlloc failed: %d\n", rc);
207
208         rc = PtlEQAlloc(ni, 128, bulk_source_callback, NULL, &bulk_source_eq);
209         if (rc != PTL_OK)
210                 CERROR("PtlEQAlloc failed: %d\n", rc);
211
212         rc = PtlEQAlloc(ni, 128, bulk_sink_callback, NULL, &bulk_sink_eq);
213         if (rc != PTL_OK)
214                 CERROR("PtlEQAlloc failed: %d\n", rc);
215
216         return rc;
217 }
218
219 void ptlrpc_exit_portals(void)
220 {
221         PtlEQFree(sent_pkt_eq);
222         PtlEQFree(rcvd_rep_eq);
223         PtlEQFree(bulk_source_eq);
224         PtlEQFree(bulk_sink_eq);
225
226         if (qswnal_nip != NULL)
227                 inter_module_put("kqswnal_ni");
228         if (socknal_nip != NULL)
229                 inter_module_put("ksocknal_ni");
230 }