1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2002, 2003 Cluster File Systems, Inc.
6 * This file is part of the Lustre file system, http://www.lustre.org
7 * Lustre is a trademark of Cluster File Systems, Inc.
9 * You may have signed or agreed to another license before downloading
10 * this software. If so, you are bound by the terms and conditions
11 * of that agreement, and the following does not apply to you. See the
12 * LICENSE file included with this distribution for more information.
14 * If you did not agree to a different license, then this copy of Lustre
15 * is open source software; you can redistribute it and/or modify it
16 * under the terms of version 2 of the GNU General Public License as
17 * published by the Free Software Foundation.
19 * In either case, Lustre is distributed in the hope that it will be
20 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * license text for more details.
26 #define DEBUG_SUBSYSTEM S_RPC
29 # include <liblustre.h>
32 # include <linux/kernel.h>
35 #include <obd_class.h>
36 #include <lustre_net.h>
37 #include "ptlrpc_internal.h"
39 lnet_handle_eq_t ptlrpc_eq_h;
42 * Client's outgoing request callback
44 void request_out_callback(lnet_event_t *ev)
46 struct ptlrpc_cb_id *cbid = ev->md.user_ptr;
47 struct ptlrpc_request *req = cbid->cbid_arg;
50 LASSERT (ev->type == LNET_EVENT_SEND ||
51 ev->type == LNET_EVENT_UNLINK);
52 LASSERT (ev->unlinked);
54 DEBUG_REQ((ev->status == 0) ? D_NET : D_ERROR, req,
55 "type %d, status %d", ev->type, ev->status);
57 if (ev->type == LNET_EVENT_UNLINK || ev->status != 0) {
59 /* Failed send: make it seem like the reply timed out, just
60 * like failing sends in client.c does currently... */
62 spin_lock(&req->rq_lock);
64 spin_unlock(&req->rq_lock);
66 ptlrpc_wake_client_req(req);
69 /* these balance the references in ptl_send_rpc() */
70 atomic_dec(&req->rq_import->imp_inflight);
71 ptlrpc_req_finished(req);
77 * Client's incoming reply callback
79 void reply_in_callback(lnet_event_t *ev)
81 struct ptlrpc_cb_id *cbid = ev->md.user_ptr;
82 struct ptlrpc_request *req = cbid->cbid_arg;
85 DEBUG_REQ((ev->status == 0) ? D_NET : D_ERROR, req,
86 "type %d, status %d", ev->type, ev->status);
88 LASSERT(ev->type == LNET_EVENT_PUT || ev->type == LNET_EVENT_UNLINK);
89 LASSERT(ev->md.start == req->rq_repbuf);
90 LASSERT(ev->mlength <= req->rq_replen);
92 spin_lock(&req->rq_lock);
94 req->rq_receiving_reply = 0;
99 if (ev->type == LNET_EVENT_UNLINK) {
100 req->rq_must_unlink = 0;
101 DEBUG_REQ(D_RPCTRACE, req, "unlink");
105 /* We've set LNET_MD_MANAGE_REMOTE for all outgoing requests
106 for adaptive timeouts' early reply. */
107 LASSERT(ev->md.options & LNET_MD_MANAGE_REMOTE);
109 if ((ev->offset == 0) &&
110 lustre_msg_get_flags(req->rq_reqmsg) & MSG_AT_SUPPORT) {
112 DEBUG_REQ(D_ADAPTTO, req,
113 "Early reply received: mlen=%u offset=%d replen=%d "
114 "replied=%d", ev->mlength, ev->offset, req->rq_replen,
117 LASSERT(ev->mlength == lustre_msg_early_size());
118 req->rq_early_count++; /* number received, client side */
120 /* If we already got the real reply, ignore the early
121 reply, but signal so we can unlink */
124 req->rq_nob_received = ev->mlength;
125 /* repmsg points to early reply */
126 req->rq_repmsg = req->rq_repbuf;
127 /* And we're still receiving */
128 req->rq_receiving_reply = 1;
132 req->rq_nob_received = ev->mlength;
133 /* repmsg points to real reply */
134 req->rq_repmsg = (struct lustre_msg *)((char *)req->rq_repbuf +
136 /* LNetMDUnlink can't be called under the LNET_LOCK,
137 so we must unlink in ptlrpc_unregister_reply */
138 DEBUG_REQ(D_INFO, req,
139 "reply in flags=%x mlen=%u offset=%d replen=%d",
140 lustre_msg_get_flags(req->rq_reqmsg),
141 ev->mlength, ev->offset, req->rq_replen);
144 req->rq_import->imp_last_reply_time = cfs_time_current_sec();
147 /* NB don't unlock till after wakeup; req can disappear under us
148 * since we don't have our own ref */
149 ptlrpc_wake_client_req(req);
150 spin_unlock(&req->rq_lock);
155 * Client's bulk has been written/read
157 void client_bulk_callback (lnet_event_t *ev)
159 struct ptlrpc_cb_id *cbid = ev->md.user_ptr;
160 struct ptlrpc_bulk_desc *desc = cbid->cbid_arg;
163 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_CLIENT_BULK_CB))
166 LASSERT ((desc->bd_type == BULK_PUT_SINK &&
167 ev->type == LNET_EVENT_PUT) ||
168 (desc->bd_type == BULK_GET_SOURCE &&
169 ev->type == LNET_EVENT_GET) ||
170 ev->type == LNET_EVENT_UNLINK);
171 LASSERT (ev->unlinked);
173 CDEBUG((ev->status == 0) ? D_NET : D_ERROR,
174 "event type %d, status %d, desc %p\n",
175 ev->type, ev->status, desc);
177 spin_lock(&desc->bd_lock);
179 LASSERT(desc->bd_network_rw);
180 desc->bd_network_rw = 0;
182 if (ev->type != LNET_EVENT_UNLINK && ev->status == 0) {
183 desc->bd_success = 1;
184 desc->bd_nob_transferred = ev->mlength;
185 desc->bd_sender = ev->sender;
188 /* NB don't unlock till after wakeup; desc can disappear under us
190 ptlrpc_wake_client_req(desc->bd_req);
192 spin_unlock(&desc->bd_lock);
197 * Server's incoming request callback
199 void request_in_callback(lnet_event_t *ev)
201 struct ptlrpc_cb_id *cbid = ev->md.user_ptr;
202 struct ptlrpc_request_buffer_desc *rqbd = cbid->cbid_arg;
203 struct ptlrpc_service *service = rqbd->rqbd_service;
204 struct ptlrpc_request *req;
207 LASSERT (ev->type == LNET_EVENT_PUT ||
208 ev->type == LNET_EVENT_UNLINK);
209 LASSERT ((char *)ev->md.start >= rqbd->rqbd_buffer);
210 LASSERT ((char *)ev->md.start + ev->offset + ev->mlength <=
211 rqbd->rqbd_buffer + service->srv_buf_size);
213 CDEBUG((ev->status == 0) ? D_NET : D_ERROR,
214 "event type %d, status %d, service %s\n",
215 ev->type, ev->status, service->srv_name);
218 /* If this is the last request message to fit in the
219 * request buffer we can use the request object embedded in
220 * rqbd. Note that if we failed to allocate a request,
221 * we'd have to re-post the rqbd, which we can't do in this
223 req = &rqbd->rqbd_req;
224 memset(req, 0, sizeof (*req));
226 LASSERT (ev->type == LNET_EVENT_PUT);
227 if (ev->status != 0) {
228 /* We moaned above already... */
231 OBD_ALLOC_GFP(req, sizeof(*req), CFS_ALLOC_ATOMIC_TRY);
233 CERROR("Can't allocate incoming request descriptor: "
234 "Dropping %s RPC from %s\n",
236 libcfs_id2str(ev->initiator));
241 /* NB we ABSOLUTELY RELY on req being zeroed, so pointers are NULL,
242 * flags are reset and scalars are zero. We only set the message
243 * size to non-zero if this was a successful receive. */
244 req->rq_xid = ev->match_bits;
245 req->rq_reqmsg = ev->md.start + ev->offset;
246 if (ev->type == LNET_EVENT_PUT && ev->status == 0)
247 req->rq_reqlen = ev->mlength;
248 do_gettimeofday(&req->rq_arrival_time);
249 req->rq_peer = ev->initiator;
250 req->rq_self = ev->target.nid;
252 req->rq_phase = RQ_PHASE_NEW;
254 req->rq_uid = ev->uid;
256 spin_lock_init(&req->rq_lock);
257 sema_init(&req->rq_rs_sem, 1);
258 CFS_INIT_LIST_HEAD(&req->rq_timed_list);
259 atomic_set(&req->rq_refcount, 1);
260 if (ev->type == LNET_EVENT_PUT)
261 DEBUG_REQ(D_RPCTRACE, req, "incoming req");
263 spin_lock(&service->srv_lock);
265 req->rq_history_seq = service->srv_request_seq++;
266 list_add_tail(&req->rq_history_list, &service->srv_request_history);
269 service->srv_nrqbd_receiving--;
270 CDEBUG(D_INFO, "Buffer complete: %d buffers still posted\n",
271 service->srv_nrqbd_receiving);
273 /* Normally, don't complain about 0 buffers posted; LNET won't
274 * drop incoming reqs since we set the portal lazy */
275 if (test_req_buffer_pressure &&
276 ev->type != LNET_EVENT_UNLINK &&
277 service->srv_nrqbd_receiving == 0)
278 CWARN("All %s request buffers busy\n",
281 /* req takes over the network's ref on rqbd */
283 /* req takes a ref on rqbd */
284 rqbd->rqbd_refcount++;
287 list_add_tail(&req->rq_list, &service->srv_req_in_queue);
288 service->srv_n_queued_reqs++;
290 /* NB everything can disappear under us once the request
291 * has been queued and we unlock, so do the wake now... */
292 cfs_waitq_signal(&service->srv_waitq);
294 spin_unlock(&service->srv_lock);
299 * Server's outgoing reply callback
301 void reply_out_callback(lnet_event_t *ev)
303 struct ptlrpc_cb_id *cbid = ev->md.user_ptr;
304 struct ptlrpc_reply_state *rs = cbid->cbid_arg;
305 struct ptlrpc_service *svc = rs->rs_service;
308 LASSERT (ev->type == LNET_EVENT_SEND ||
309 ev->type == LNET_EVENT_ACK ||
310 ev->type == LNET_EVENT_UNLINK);
312 if (!rs->rs_difficult) {
313 /* 'Easy' replies have no further processing so I drop the
314 * net's ref on 'rs' */
315 LASSERT (ev->unlinked);
316 ptlrpc_rs_decref(rs);
317 atomic_dec (&svc->srv_outstanding_replies);
322 LASSERT (rs->rs_on_net);
325 /* Last network callback. The net's ref on 'rs' stays put
326 * until ptlrpc_server_handle_reply() is done with it */
327 spin_lock(&svc->srv_lock);
329 ptlrpc_schedule_difficult_reply (rs);
330 spin_unlock(&svc->srv_lock);
337 * Server's bulk completion callback
339 void server_bulk_callback (lnet_event_t *ev)
341 struct ptlrpc_cb_id *cbid = ev->md.user_ptr;
342 struct ptlrpc_bulk_desc *desc = cbid->cbid_arg;
345 LASSERT (ev->type == LNET_EVENT_SEND ||
346 ev->type == LNET_EVENT_UNLINK ||
347 (desc->bd_type == BULK_PUT_SOURCE &&
348 ev->type == LNET_EVENT_ACK) ||
349 (desc->bd_type == BULK_GET_SINK &&
350 ev->type == LNET_EVENT_REPLY));
352 CDEBUG((ev->status == 0) ? D_NET : D_ERROR,
353 "event type %d, status %d, desc %p\n",
354 ev->type, ev->status, desc);
356 spin_lock(&desc->bd_lock);
358 if ((ev->type == LNET_EVENT_ACK ||
359 ev->type == LNET_EVENT_REPLY) &&
361 /* We heard back from the peer, so even if we get this
362 * before the SENT event (oh yes we can), we know we
363 * read/wrote the peer buffer and how much... */
364 desc->bd_success = 1;
365 desc->bd_nob_transferred = ev->mlength;
366 desc->bd_sender = ev->sender;
370 /* This is the last callback no matter what... */
371 desc->bd_network_rw = 0;
372 cfs_waitq_signal(&desc->bd_waitq);
375 spin_unlock(&desc->bd_lock);
379 static void ptlrpc_master_callback(lnet_event_t *ev)
381 struct ptlrpc_cb_id *cbid = ev->md.user_ptr;
382 void (*callback)(lnet_event_t *ev) = cbid->cbid_fn;
384 /* Honestly, it's best to find out early. */
385 LASSERT (cbid->cbid_arg != LP_POISON);
386 LASSERT (callback == request_out_callback ||
387 callback == reply_in_callback ||
388 callback == client_bulk_callback ||
389 callback == request_in_callback ||
390 callback == reply_out_callback ||
391 callback == server_bulk_callback);
396 int ptlrpc_uuid_to_peer (struct obd_uuid *uuid,
397 lnet_process_id_t *peer, lnet_nid_t *self)
403 int portals_compatibility;
409 portals_compatibility = LNetCtl(IOC_LIBCFS_PORTALS_COMPATIBILITY, NULL);
411 peer->pid = LUSTRE_SRV_LNET_PID;
413 /* Choose the matching UUID that's closest */
414 while (lustre_uuid_to_peer(uuid->uuid, &dst_nid, count++) == 0) {
415 dist = LNetDist(dst_nid, &src_nid, &order);
419 if (dist == 0) { /* local! use loopback LND */
420 peer->nid = *self = LNET_MKNID(LNET_MKNET(LOLND, 0), 0);
425 LASSERT (order >= 0);
428 (dist == best_dist && order < best_order)) {
432 if (portals_compatibility > 1) {
433 /* Strong portals compatibility: Zero the nid's
434 * NET, so if I'm reading new config logs, or
435 * getting configured by (new) lconf I can
436 * still talk to old servers. */
437 dst_nid = LNET_MKNID(0, LNET_NIDADDR(dst_nid));
438 src_nid = LNET_MKNID(0, LNET_NIDADDR(src_nid));
446 CDEBUG(D_NET,"%s->%s\n", uuid->uuid, libcfs_id2str(*peer));
448 CERROR("No NID found for %s\n", uuid->uuid);
452 void ptlrpc_ni_fini(void)
455 struct l_wait_info lwi;
459 /* Wait for the event queue to become idle since there may still be
460 * messages in flight with pending events (i.e. the fire-and-forget
461 * messages == client requests and "non-difficult" server
464 for (retries = 0;; retries++) {
465 rc = LNetEQFree(ptlrpc_eq_h);
476 CWARN("Event queue still busy\n");
479 cfs_waitq_init(&waitq);
480 lwi = LWI_TIMEOUT(cfs_time_seconds(2), NULL, NULL);
481 l_wait_event(waitq, 0, &lwi);
488 lnet_pid_t ptl_get_pid(void)
495 pid = LUSTRE_SRV_LNET_PID;
500 int ptlrpc_ni_init(void)
506 CDEBUG(D_NET, "My pid is: %x\n", pid);
508 /* We're not passing any limits yet... */
509 rc = LNetNIInit(pid);
511 CDEBUG (D_NET, "Can't init network interface: %d\n", rc);
515 /* CAVEAT EMPTOR: how we process portals events is _radically_
516 * different depending on... */
518 /* kernel portals calls our master callback when events are added to
519 * the event queue. In fact lustre never pulls events off this queue,
520 * so it's only sized for some debug history. */
521 rc = LNetEQAlloc(1024, ptlrpc_master_callback, &ptlrpc_eq_h);
523 /* liblustre calls the master callback when it removes events from the
524 * event queue. The event queue has to be big enough not to drop
526 rc = LNetEQAlloc(10240, LNET_EQ_HANDLER_NONE, &ptlrpc_eq_h);
531 CERROR ("Failed to allocate event queue: %d\n", rc);
538 CFS_LIST_HEAD(liblustre_wait_callbacks);
539 CFS_LIST_HEAD(liblustre_idle_callbacks);
540 void *liblustre_services_callback;
543 liblustre_register_waitidle_callback (struct list_head *callback_list,
545 int (*fn)(void *arg), void *arg)
547 struct liblustre_wait_callback *llwc;
549 OBD_ALLOC(llwc, sizeof(*llwc));
550 LASSERT (llwc != NULL);
552 llwc->llwc_name = name;
554 llwc->llwc_arg = arg;
555 list_add_tail(&llwc->llwc_list, callback_list);
561 liblustre_deregister_waitidle_callback (void *opaque)
563 struct liblustre_wait_callback *llwc = opaque;
565 list_del(&llwc->llwc_list);
566 OBD_FREE(llwc, sizeof(*llwc));
570 liblustre_register_wait_callback (const char *name,
571 int (*fn)(void *arg), void *arg)
573 return liblustre_register_waitidle_callback(&liblustre_wait_callbacks,
578 liblustre_deregister_wait_callback (void *opaque)
580 liblustre_deregister_waitidle_callback(opaque);
584 liblustre_register_idle_callback (const char *name,
585 int (*fn)(void *arg), void *arg)
587 return liblustre_register_waitidle_callback(&liblustre_idle_callbacks,
592 liblustre_deregister_idle_callback (void *opaque)
594 liblustre_deregister_waitidle_callback(opaque);
598 liblustre_check_events (int timeout)
605 rc = LNetEQPoll(&ptlrpc_eq_h, 1, timeout * 1000, &ev, &i);
609 LASSERT (rc == -EOVERFLOW || rc == 1);
611 /* liblustre: no asynch callback so we can't affort to miss any
613 if (rc == -EOVERFLOW) {
614 CERROR ("Dropped an event!!!\n");
618 ptlrpc_master_callback (&ev);
622 int liblustre_waiting = 0;
625 liblustre_wait_event (int timeout)
627 struct list_head *tmp;
628 struct liblustre_wait_callback *llwc;
629 int found_something = 0;
631 /* single threaded recursion check... */
632 liblustre_waiting = 1;
635 /* Deal with all pending events */
636 while (liblustre_check_events(0))
639 /* Give all registered callbacks a bite at the cherry */
640 list_for_each(tmp, &liblustre_wait_callbacks) {
641 llwc = list_entry(tmp, struct liblustre_wait_callback,
644 if (llwc->llwc_fn(llwc->llwc_arg))
648 if (found_something || timeout == 0)
651 /* Nothing so far, but I'm allowed to block... */
652 found_something = liblustre_check_events(timeout);
653 if (!found_something) /* still nothing */
654 break; /* I timed out */
657 liblustre_waiting = 0;
659 return found_something;
663 liblustre_wait_idle(void)
665 static int recursed = 0;
667 struct list_head *tmp;
668 struct liblustre_wait_callback *llwc;
675 liblustre_wait_event(0);
679 list_for_each(tmp, &liblustre_idle_callbacks) {
680 llwc = list_entry(tmp, struct liblustre_wait_callback,
683 if (!llwc->llwc_fn(llwc->llwc_arg)) {
694 #endif /* __KERNEL__ */
696 int ptlrpc_init_portals(void)
698 int rc = ptlrpc_ni_init();
701 CERROR("network initialisation failed\n");
705 liblustre_services_callback =
706 liblustre_register_wait_callback("liblustre_check_services",
707 &liblustre_check_services, NULL);
709 rc = ptlrpcd_addref();
713 CERROR("rpcd initialisation failed\n");
715 liblustre_deregister_wait_callback(liblustre_services_callback);
721 void ptlrpc_exit_portals(void)
724 liblustre_deregister_wait_callback(liblustre_services_callback);