1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
23 #define DEBUG_SUBSYSTEM S_RPC
25 #include <linux/module.h>
26 #include <linux/obd_support.h>
27 #include <linux/lustre_net.h>
29 ptl_handle_eq_t request_out_eq, reply_in_eq, reply_out_eq, bulk_source_eq,
31 static const ptl_handle_ni_t *socknal_nip = NULL, *toenal_nip = NULL,
32 *qswnal_nip = NULL, *gmnal_nip = NULL;
35 * Free the packet when it has gone out
37 static int request_out_callback(ptl_event_t *ev)
39 struct ptlrpc_request *req = ev->mem_desc.user_ptr;
42 /* requests always contiguous */
43 LASSERT((ev->mem_desc.options & PTL_MD_IOV) == 0);
45 if (ev->type != PTL_EVENT_SENT) {
46 // XXX make sure we understand all events, including ACK's
47 CERROR("Unknown event %d\n", ev->type);
51 /* this balances the atomic_inc in ptl_send_rpc */
52 ptlrpc_req_finished(req);
58 * Free the packet when it has gone out
60 static int reply_out_callback(ptl_event_t *ev)
64 /* replies always contiguous */
65 LASSERT((ev->mem_desc.options & PTL_MD_IOV) == 0);
67 if (ev->type == PTL_EVENT_SENT) {
68 OBD_FREE(ev->mem_desc.start, ev->mem_desc.length);
70 // XXX make sure we understand all events, including ACK's
71 CERROR("Unknown event %d\n", ev->type);
79 * Wake up the thread waiting for the reply once it comes in.
81 static int reply_in_callback(ptl_event_t *ev)
83 struct ptlrpc_request *req = ev->mem_desc.user_ptr;
86 /* replies always contiguous */
87 LASSERT((ev->mem_desc.options & PTL_MD_IOV) == 0);
89 if (req->rq_xid == 0x5a5a5a5a5a5a5a5a) {
90 CERROR("Reply received for freed request! Probably a missing "
95 if (req->rq_xid != ev->match_bits) {
96 CERROR("Reply packet for wrong request\n");
100 if (ev->type == PTL_EVENT_PUT) {
101 req->rq_repmsg = ev->mem_desc.start + ev->offset;
103 wake_up(&req->rq_wait_for_rep);
105 // XXX make sure we understand all events, including ACK's
106 CERROR("Unknown event %d\n", ev->type);
113 int request_in_callback(ptl_event_t *ev)
115 struct ptlrpc_request_buffer_desc *rqbd = ev->mem_desc.user_ptr;
116 struct ptlrpc_service *service = rqbd->rqbd_service;
118 /* requests always contiguous */
119 LASSERT((ev->mem_desc.options & PTL_MD_IOV) == 0);
120 /* we only enable puts */
121 LASSERT(ev->type == PTL_EVENT_PUT);
122 LASSERT(atomic_read(&service->srv_nrqbds_receiving) > 0);
123 LASSERT(atomic_read(&rqbd->rqbd_refcount) > 0);
125 if (ev->rlength != ev->mlength)
126 CERROR("Warning: Possibly truncated rpc (%d/%d)\n",
127 ev->mlength, ev->rlength);
129 if (ptl_is_valid_handle(&ev->unlinked_me)) {
130 /* This is the last request to be received into this
131 * request buffer. We don't bump the refcount, since the
132 * thread servicing this event is effectively taking over
133 * portals' reference.
135 #warning ev->unlinked_me.nal_idx is not set properly in a callback
136 LASSERT(ev->unlinked_me.handle_idx==rqbd->rqbd_me_h.handle_idx);
138 /* we're off the air */
139 /* we'll probably start dropping packets in portals soon */
140 if (atomic_dec_and_test(&service->srv_nrqbds_receiving))
141 CERROR("All request buffers busy\n");
143 /* +1 ref for service thread */
144 atomic_inc(&rqbd->rqbd_refcount);
147 wake_up(&service->srv_waitq);
152 static int bulk_source_callback(ptl_event_t *ev)
154 struct ptlrpc_bulk_desc *desc = ev->mem_desc.user_ptr;
155 struct ptlrpc_bulk_page *bulk;
156 struct list_head *tmp;
157 struct list_head *next;
160 CDEBUG(D_NET, "got %s event %d\n",
161 (ev->type == PTL_EVENT_SENT) ? "SENT" :
162 (ev->type == PTL_EVENT_ACK) ? "ACK" : "UNEXPECTED", ev->type);
164 LASSERT(ev->type == PTL_EVENT_SENT || ev->type == PTL_EVENT_ACK);
166 LASSERT(atomic_read(&desc->bd_source_callback_count) > 0 &&
167 atomic_read(&desc->bd_source_callback_count) <= 2);
169 /* 1 fragment for each page always */
170 LASSERT(ev->mem_desc.niov == desc->bd_page_count);
172 if (atomic_dec_and_test(&desc->bd_source_callback_count)) {
173 void (*event_handler)(struct ptlrpc_bulk_desc *);
175 list_for_each_safe(tmp, next, &desc->bd_page_list) {
176 bulk = list_entry(tmp, struct ptlrpc_bulk_page,
179 if (bulk->bp_cb != NULL)
183 /* We need to make a note of whether there's an event handler
184 * before we call wake_up, because if there is no event handler,
185 * 'desc' might be freed before we're scheduled again. */
186 event_handler = desc->bd_ptl_ev_hdlr;
188 desc->bd_flags |= PTL_BULK_FL_SENT;
189 wake_up(&desc->bd_waitq);
191 LASSERT(desc->bd_ptl_ev_hdlr == event_handler);
199 static int bulk_sink_callback(ptl_event_t *ev)
201 struct ptlrpc_bulk_desc *desc = ev->mem_desc.user_ptr;
202 struct ptlrpc_bulk_page *bulk;
203 struct list_head *tmp;
204 struct list_head *next;
205 ptl_size_t total = 0;
206 void (*event_handler)(struct ptlrpc_bulk_desc *);
209 LASSERT(ev->type == PTL_EVENT_PUT);
211 /* put with zero offset */
212 LASSERT(ev->offset == 0);
214 LASSERT((ev->mem_desc.options & PTL_MD_IOV) != 0);
215 /* 1 fragment for each page always */
216 LASSERT(ev->mem_desc.niov == desc->bd_page_count);
218 list_for_each_safe (tmp, next, &desc->bd_page_list) {
219 bulk = list_entry(tmp, struct ptlrpc_bulk_page, bp_link);
221 total += bulk->bp_buflen;
223 if (bulk->bp_cb != NULL)
227 LASSERT(ev->mem_desc.length == total);
229 /* We need to make a note of whether there's an event handler
230 * before we call wake_up, because if there is no event
231 * handler, 'desc' might be freed before we're scheduled again. */
232 event_handler = desc->bd_ptl_ev_hdlr;
234 desc->bd_flags |= PTL_BULK_FL_RCVD;
235 wake_up(&desc->bd_waitq);
237 LASSERT(desc->bd_ptl_ev_hdlr == event_handler);
244 int ptlrpc_init_portals(void)
249 /* Use the qswnal if it's there */
250 if ((qswnal_nip = inter_module_get("kqswnal_ni")) != NULL)
252 else if ((gmnal_nip = inter_module_get("kgmnal_ni")) != NULL)
254 else if ((socknal_nip = inter_module_get("ksocknal_ni")) != NULL)
256 else if ((toenal_nip = inter_module_get("ktoenal_ni")) != NULL)
259 CERROR("get_ni failed: is a NAL module loaded?\n");
263 rc = PtlEQAlloc(ni, 1024, request_out_callback, &request_out_eq);
265 CERROR("PtlEQAlloc failed: %d\n", rc);
267 rc = PtlEQAlloc(ni, 1024, reply_out_callback, &reply_out_eq);
269 CERROR("PtlEQAlloc failed: %d\n", rc);
271 rc = PtlEQAlloc(ni, 1024, reply_in_callback, &reply_in_eq);
273 CERROR("PtlEQAlloc failed: %d\n", rc);
275 rc = PtlEQAlloc(ni, 1024, bulk_source_callback, &bulk_source_eq);
277 CERROR("PtlEQAlloc failed: %d\n", rc);
279 rc = PtlEQAlloc(ni, 1024, bulk_sink_callback, &bulk_sink_eq);
281 CERROR("PtlEQAlloc failed: %d\n", rc);
286 void ptlrpc_exit_portals(void)
288 PtlEQFree(request_out_eq);
289 PtlEQFree(reply_out_eq);
290 PtlEQFree(reply_in_eq);
291 PtlEQFree(bulk_source_eq);
292 PtlEQFree(bulk_sink_eq);
294 if (qswnal_nip != NULL)
295 inter_module_put("kqswnal_ni");
296 if (socknal_nip != NULL)
297 inter_module_put("ksocknal_ni");
298 if (gmnal_nip != NULL)
299 inter_module_put("kgmnal_ni");
300 if (toenal_nip != NULL)
301 inter_module_put("ktoenal_ni");