Whamcloud - gitweb
new files - restructure RPC further
[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
37 /*
38  *  Free the packet when it has gone out
39  */
40 static int sent_packet_callback(ptl_event_t *ev, void *data)
41 {
42         ENTRY;
43
44         if (ev->type == PTL_EVENT_SENT) {
45                 OBD_FREE(ev->mem_desc.start, ev->mem_desc.length);
46         } else { 
47                 // XXX make sure we understand all events, including ACK's
48                 CERROR("Unknown event %d\n", ev->type); 
49                 BUG();
50         }
51
52         EXIT;
53         return 1;
54 }
55
56 /*
57  * Wake up the thread waiting for the reply once it comes in.
58  */
59 static int rcvd_reply_callback(ptl_event_t *ev, void *data)
60 {
61         struct ptlrpc_request *rpc = ev->mem_desc.user_ptr;
62         ENTRY;
63
64         if (ev->type == PTL_EVENT_PUT) {
65                 rpc->rq_repbuf = ev->mem_desc.start + ev->offset;
66                 barrier();
67                 wake_up_interruptible(&rpc->rq_wait_for_rep);
68         } else { 
69                 // XXX make sure we understand all events, including ACK's
70                 CERROR("Unknown event %d\n", ev->type); 
71                 BUG();
72         }
73
74         EXIT;
75         return 1;
76 }
77
78 int server_request_callback(ptl_event_t *ev, void *data)
79 {
80         struct ptlrpc_service *service = data;
81         int rc;
82
83         if (ev->rlength != ev->mlength)
84                 CERROR("Warning: Possibly truncated rpc (%d/%d)\n",
85                        ev->mlength, ev->rlength);
86
87         /* The ME is unlinked when there is less than 1024 bytes free
88          * on its MD.  This ensures we are always able to handle the rpc, 
89          * although the 1024 value is a guess as to the size of a
90          * large rpc (the known safe margin should be determined).
91          *
92          * NOTE: The portals API by default unlinks all MD's associated
93          *       with an ME when it's unlinked.  For now, this behavior
94          *       has been commented out of the portals library so the
95          *       MD can be unlinked when its ref count drops to zero.
96          *       A new MD and ME will then be created that use the same
97          *       kmalloc()'ed memory and inserted at the ring tail.
98          */
99
100         service->srv_ref_count[service->srv_md_active]++;
101
102         if (ev->offset >= (service->srv_buf_size - 1024)) {
103                 CDEBUG(D_INODE, "Unlinking ME %d\n", service->srv_me_active);
104
105                 rc = PtlMEUnlink(service->srv_me_h[service->srv_me_active]);
106                 service->srv_me_h[service->srv_me_active] = 0;
107
108                 if (rc != PTL_OK) {
109                         CERROR("PtlMEUnlink failed - DROPPING soon: %d\n", rc);
110                         BUG();
111                         return rc;
112                 }
113
114                 service->srv_me_active = NEXT_INDEX(service->srv_me_active,
115                         service->srv_ring_length);
116
117                 if (service->srv_me_h[service->srv_me_active] == 0)
118                         CERROR("All %d ring ME's are unlinked!\n",
119                                service->srv_ring_length);
120         }
121
122         if (ev->type == PTL_EVENT_PUT) {
123                 wake_up(&service->srv_waitq);
124         } else {
125                 CERROR("Unexpected event type: %d\n", ev->type);
126         }
127
128         return 0;
129 }
130
131
132 static int bulk_source_callback(ptl_event_t *ev, void *data)
133 {
134         struct ptlrpc_request *rpc = ev->mem_desc.user_ptr;
135
136         ENTRY;
137
138         if (ev->type == PTL_EVENT_SENT) {
139                 CDEBUG(D_NET, "got SENT event\n");
140         } else if (ev->type == PTL_EVENT_ACK) {
141                 CDEBUG(D_NET, "got ACK event\n");
142                 wake_up_interruptible(&rpc->rq_wait_for_bulk);
143         } else {
144                 CERROR("Unexpected event type!\n");
145                 BUG();
146         }
147
148         EXIT;
149         return 1;
150 }
151
152 static int bulk_sink_callback(ptl_event_t *ev, void *data)
153 {
154         struct ptlrpc_request *rpc = ev->mem_desc.user_ptr;
155
156         ENTRY;
157
158         if (ev->type == PTL_EVENT_PUT) {
159                 if (rpc->rq_bulkbuf != ev->mem_desc.start + ev->offset)
160                         CERROR("bulkbuf != mem_desc -- why?\n");
161                 wake_up_interruptible(&rpc->rq_wait_for_bulk);
162         } else {
163                 CERROR("Unexpected event type!\n");
164                 BUG();
165         }
166
167         EXIT;
168         return 1;
169 }
170
171 int ptlrpc_init_portals(void)
172 {
173         int rc;
174         const ptl_handle_ni_t *nip;
175         ptl_handle_ni_t ni;
176
177         nip = inter_module_get_request(LUSTRE_NAL "_ni", LUSTRE_NAL);
178         if (nip == NULL) {
179                 CERROR("get_ni failed: is the NAL module loaded?\n");
180                 return -EIO;
181         }
182         ni = *nip;
183
184         rc = PtlEQAlloc(ni, 128, sent_packet_callback, NULL, &sent_pkt_eq);
185         if (rc != PTL_OK)
186                 CERROR("PtlEQAlloc failed: %d\n", rc);
187
188         rc = PtlEQAlloc(ni, 128, rcvd_reply_callback, NULL, &rcvd_rep_eq);
189         if (rc != PTL_OK)
190                 CERROR("PtlEQAlloc failed: %d\n", rc);
191
192         rc = PtlEQAlloc(ni, 128, bulk_source_callback, NULL, &bulk_source_eq);
193         if (rc != PTL_OK)
194                 CERROR("PtlEQAlloc failed: %d\n", rc);
195
196         rc = PtlEQAlloc(ni, 128, bulk_sink_callback, NULL, &bulk_sink_eq);
197         if (rc != PTL_OK)
198                 CERROR("PtlEQAlloc failed: %d\n", rc);
199
200         return rc;
201 }
202
203 void ptlrpc_exit_portals(void)
204 {
205         PtlEQFree(sent_pkt_eq);
206         PtlEQFree(rcvd_rep_eq);
207         PtlEQFree(bulk_source_eq);
208         PtlEQFree(bulk_sink_eq);
209
210         inter_module_put(LUSTRE_NAL "_ni");
211 }