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 <lustre_sec.h>
38 #include "ptlrpc_internal.h"
40 lnet_handle_eq_t ptlrpc_eq_h;
43 * Client's outgoing request callback
45 void request_out_callback(lnet_event_t *ev)
47 struct ptlrpc_cb_id *cbid = ev->md.user_ptr;
48 struct ptlrpc_request *req = cbid->cbid_arg;
51 LASSERT (ev->type == LNET_EVENT_SEND ||
52 ev->type == LNET_EVENT_UNLINK);
53 LASSERT (ev->unlinked);
55 DEBUG_REQ((ev->status == 0) ? D_NET : D_ERROR, req,
56 "type %d, status %d", ev->type, ev->status);
58 sptlrpc_request_out_callback(req);
60 if (ev->type == LNET_EVENT_UNLINK || ev->status != 0) {
62 /* Failed send: make it seem like the reply timed out, just
63 * like failing sends in client.c does currently... */
65 spin_lock(&req->rq_lock);
67 spin_unlock(&req->rq_lock);
69 ptlrpc_wake_client_req(req);
72 /* these balance the references in ptl_send_rpc() */
73 atomic_dec(&req->rq_import->imp_inflight);
74 ptlrpc_req_finished(req);
80 * Client's incoming reply callback
82 void reply_in_callback(lnet_event_t *ev)
84 struct ptlrpc_cb_id *cbid = ev->md.user_ptr;
85 struct ptlrpc_request *req = cbid->cbid_arg;
88 LASSERT (ev->type == LNET_EVENT_PUT ||
89 ev->type == LNET_EVENT_UNLINK);
90 LASSERT (ev->unlinked);
91 LASSERT (ev->md.start == req->rq_repbuf);
92 LASSERT (ev->offset == 0);
93 LASSERT (ev->mlength <= req->rq_repbuf_len);
95 DEBUG_REQ((ev->status == 0) ? D_NET : D_ERROR, req,
96 "type %d, status %d", ev->type, ev->status);
98 spin_lock(&req->rq_lock);
100 LASSERT (req->rq_receiving_reply);
101 req->rq_receiving_reply = 0;
103 if (ev->type == LNET_EVENT_PUT && ev->status == 0) {
105 req->rq_nob_received = ev->mlength;
108 /* NB don't unlock till after wakeup; req can disappear under us
109 * since we don't have our own ref */
110 ptlrpc_wake_client_req(req);
112 spin_unlock(&req->rq_lock);
117 * Client's bulk has been written/read
119 void client_bulk_callback (lnet_event_t *ev)
121 struct ptlrpc_cb_id *cbid = ev->md.user_ptr;
122 struct ptlrpc_bulk_desc *desc = cbid->cbid_arg;
125 LASSERT ((desc->bd_type == BULK_PUT_SINK &&
126 ev->type == LNET_EVENT_PUT) ||
127 (desc->bd_type == BULK_GET_SOURCE &&
128 ev->type == LNET_EVENT_GET) ||
129 ev->type == LNET_EVENT_UNLINK);
130 LASSERT (ev->unlinked);
132 CDEBUG((ev->status == 0) ? D_NET : D_ERROR,
133 "event type %d, status %d, desc %p\n",
134 ev->type, ev->status, desc);
136 spin_lock(&desc->bd_lock);
138 LASSERT(desc->bd_network_rw);
139 desc->bd_network_rw = 0;
141 if (ev->type != LNET_EVENT_UNLINK && ev->status == 0) {
142 desc->bd_success = 1;
143 desc->bd_nob_transferred = ev->mlength;
144 desc->bd_sender = ev->sender;
147 sptlrpc_enc_pool_put_pages(desc);
149 /* NB don't unlock till after wakeup; desc can disappear under us
151 ptlrpc_wake_client_req(desc->bd_req);
153 spin_unlock(&desc->bd_lock);
158 * Server's incoming request callback
160 void request_in_callback(lnet_event_t *ev)
162 struct ptlrpc_cb_id *cbid = ev->md.user_ptr;
163 struct ptlrpc_request_buffer_desc *rqbd = cbid->cbid_arg;
164 struct ptlrpc_service *service = rqbd->rqbd_service;
165 struct ptlrpc_request *req;
168 LASSERT (ev->type == LNET_EVENT_PUT ||
169 ev->type == LNET_EVENT_UNLINK);
170 LASSERT ((char *)ev->md.start >= rqbd->rqbd_buffer);
171 LASSERT ((char *)ev->md.start + ev->offset + ev->mlength <=
172 rqbd->rqbd_buffer + service->srv_buf_size);
174 CDEBUG((ev->status == 0) ? D_NET : D_ERROR,
175 "event type %d, status %d, service %s\n",
176 ev->type, ev->status, service->srv_name);
179 /* If this is the last request message to fit in the
180 * request buffer we can use the request object embedded in
181 * rqbd. Note that if we failed to allocate a request,
182 * we'd have to re-post the rqbd, which we can't do in this
184 req = &rqbd->rqbd_req;
185 memset(req, 0, sizeof (*req));
187 LASSERT (ev->type == LNET_EVENT_PUT);
188 if (ev->status != 0) {
189 /* We moaned above already... */
192 OBD_ALLOC_GFP(req, sizeof(*req), CFS_ALLOC_ATOMIC_TRY);
194 CERROR("Can't allocate incoming request descriptor: "
195 "Dropping %s RPC from %s\n",
197 libcfs_id2str(ev->initiator));
202 /* NB we ABSOLUTELY RELY on req being zeroed, so pointers are NULL,
203 * flags are reset and scalars are zero. We only set the message
204 * size to non-zero if this was a successful receive. */
205 req->rq_xid = ev->match_bits;
206 req->rq_reqbuf = ev->md.start + ev->offset;
207 if (ev->type == LNET_EVENT_PUT && ev->status == 0)
208 req->rq_reqdata_len = ev->mlength;
209 do_gettimeofday(&req->rq_arrival_time);
210 req->rq_peer = ev->initiator;
211 req->rq_self = ev->target.nid;
213 req->rq_phase = RQ_PHASE_NEW;
215 req->rq_uid = ev->uid;
218 CDEBUG(D_RPCTRACE, "peer: %s\n", libcfs_id2str(req->rq_peer));
220 spin_lock(&service->srv_lock);
222 req->rq_history_seq = service->srv_request_seq++;
223 list_add_tail(&req->rq_history_list, &service->srv_request_history);
226 service->srv_nrqbd_receiving--;
227 CDEBUG(D_INFO, "Buffer complete: %d buffers still posted\n",
228 service->srv_nrqbd_receiving);
230 /* Normally, don't complain about 0 buffers posted; LNET won't
231 * drop incoming reqs since we set the portal lazy */
232 if (test_req_buffer_pressure &&
233 ev->type != LNET_EVENT_UNLINK &&
234 service->srv_nrqbd_receiving == 0)
235 CWARN("All %s request buffers busy\n",
238 /* req takes over the network's ref on rqbd */
240 /* req takes a ref on rqbd */
241 rqbd->rqbd_refcount++;
244 list_add_tail(&req->rq_list, &service->srv_request_queue);
245 service->srv_n_queued_reqs++;
247 /* NB everything can disappear under us once the request
248 * has been queued and we unlock, so do the wake now... */
249 cfs_waitq_signal(&service->srv_waitq);
251 spin_unlock(&service->srv_lock);
256 * Server's outgoing reply callback
258 void reply_out_callback(lnet_event_t *ev)
260 struct ptlrpc_cb_id *cbid = ev->md.user_ptr;
261 struct ptlrpc_reply_state *rs = cbid->cbid_arg;
262 struct ptlrpc_service *svc = rs->rs_service;
265 LASSERT (ev->type == LNET_EVENT_SEND ||
266 ev->type == LNET_EVENT_ACK ||
267 ev->type == LNET_EVENT_UNLINK);
269 if (!rs->rs_difficult) {
270 /* 'Easy' replies have no further processing so I drop the
271 * net's ref on 'rs' */
272 LASSERT (ev->unlinked);
273 ptlrpc_rs_decref(rs);
274 atomic_dec (&svc->srv_outstanding_replies);
279 LASSERT (rs->rs_on_net);
282 /* Last network callback. The net's ref on 'rs' stays put
283 * until ptlrpc_server_handle_reply() is done with it */
284 spin_lock(&svc->srv_lock);
286 ptlrpc_schedule_difficult_reply (rs);
287 spin_unlock(&svc->srv_lock);
294 * Server's bulk completion callback
296 void server_bulk_callback (lnet_event_t *ev)
298 struct ptlrpc_cb_id *cbid = ev->md.user_ptr;
299 struct ptlrpc_bulk_desc *desc = cbid->cbid_arg;
302 LASSERT (ev->type == LNET_EVENT_SEND ||
303 ev->type == LNET_EVENT_UNLINK ||
304 (desc->bd_type == BULK_PUT_SOURCE &&
305 ev->type == LNET_EVENT_ACK) ||
306 (desc->bd_type == BULK_GET_SINK &&
307 ev->type == LNET_EVENT_REPLY));
309 CDEBUG((ev->status == 0) ? D_NET : D_ERROR,
310 "event type %d, status %d, desc %p\n",
311 ev->type, ev->status, desc);
313 spin_lock(&desc->bd_lock);
315 if ((ev->type == LNET_EVENT_ACK ||
316 ev->type == LNET_EVENT_REPLY) &&
318 /* We heard back from the peer, so even if we get this
319 * before the SENT event (oh yes we can), we know we
320 * read/wrote the peer buffer and how much... */
321 desc->bd_success = 1;
322 desc->bd_nob_transferred = ev->mlength;
323 desc->bd_sender = ev->sender;
327 /* This is the last callback no matter what... */
328 desc->bd_network_rw = 0;
329 cfs_waitq_signal(&desc->bd_waitq);
332 spin_unlock(&desc->bd_lock);
336 static void ptlrpc_master_callback(lnet_event_t *ev)
338 struct ptlrpc_cb_id *cbid = ev->md.user_ptr;
339 void (*callback)(lnet_event_t *ev) = cbid->cbid_fn;
341 /* Honestly, it's best to find out early. */
342 LASSERT (cbid->cbid_arg != LP_POISON);
343 LASSERT (callback == request_out_callback ||
344 callback == reply_in_callback ||
345 callback == client_bulk_callback ||
346 callback == request_in_callback ||
347 callback == reply_out_callback ||
348 callback == server_bulk_callback);
353 int ptlrpc_uuid_to_peer (struct obd_uuid *uuid,
354 lnet_process_id_t *peer, lnet_nid_t *self)
357 __u32 best_order = 0;
360 int portals_compatibility;
366 portals_compatibility = LNetCtl(IOC_LIBCFS_PORTALS_COMPATIBILITY, NULL);
368 peer->pid = LUSTRE_SRV_LNET_PID;
370 /* Choose the matching UUID that's closest */
371 while (lustre_uuid_to_peer(uuid->uuid, &dst_nid, count++) == 0) {
372 dist = LNetDist(dst_nid, &src_nid, &order);
376 if (dist == 0) { /* local! use loopback LND */
377 peer->nid = *self = LNET_MKNID(LNET_MKNET(LOLND, 0), 0);
384 (dist == best_dist && order < best_order)) {
388 if (portals_compatibility > 1) {
389 /* Strong portals compatibility: Zero the nid's
390 * NET, so if I'm reading new config logs, or
391 * getting configured by (new) lconf I can
392 * still talk to old servers. */
393 dst_nid = LNET_MKNID(0, LNET_NIDADDR(dst_nid));
394 src_nid = LNET_MKNID(0, LNET_NIDADDR(src_nid));
402 CDEBUG(D_NET,"%s->%s\n", uuid->uuid, libcfs_id2str(*peer));
404 CERROR("No NID found for %s\n", uuid->uuid);
408 void ptlrpc_ni_fini(void)
411 struct l_wait_info lwi;
415 /* Wait for the event queue to become idle since there may still be
416 * messages in flight with pending events (i.e. the fire-and-forget
417 * messages == client requests and "non-difficult" server
420 for (retries = 0;; retries++) {
421 rc = LNetEQFree(ptlrpc_eq_h);
432 CWARN("Event queue still busy\n");
435 cfs_waitq_init(&waitq);
436 lwi = LWI_TIMEOUT(cfs_time_seconds(2), NULL, NULL);
437 l_wait_event(waitq, 0, &lwi);
444 lnet_pid_t ptl_get_pid(void)
451 pid = LUSTRE_SRV_LNET_PID;
456 int ptlrpc_ni_init(void)
462 CDEBUG(D_NET, "My pid is: %x\n", pid);
464 /* We're not passing any limits yet... */
465 rc = LNetNIInit(pid);
467 CDEBUG (D_NET, "Can't init network interface: %d\n", rc);
471 /* CAVEAT EMPTOR: how we process portals events is _radically_
472 * different depending on... */
474 /* kernel portals calls our master callback when events are added to
475 * the event queue. In fact lustre never pulls events off this queue,
476 * so it's only sized for some debug history. */
477 rc = LNetEQAlloc(1024, ptlrpc_master_callback, &ptlrpc_eq_h);
479 /* liblustre calls the master callback when it removes events from the
480 * event queue. The event queue has to be big enough not to drop
482 rc = LNetEQAlloc(10240, LNET_EQ_HANDLER_NONE, &ptlrpc_eq_h);
487 CERROR ("Failed to allocate event queue: %d\n", rc);
494 CFS_LIST_HEAD(liblustre_wait_callbacks);
495 CFS_LIST_HEAD(liblustre_idle_callbacks);
496 void *liblustre_services_callback;
499 liblustre_register_waitidle_callback (struct list_head *callback_list,
501 int (*fn)(void *arg), void *arg)
503 struct liblustre_wait_callback *llwc;
505 OBD_ALLOC(llwc, sizeof(*llwc));
506 LASSERT (llwc != NULL);
508 llwc->llwc_name = name;
510 llwc->llwc_arg = arg;
511 list_add_tail(&llwc->llwc_list, callback_list);
517 liblustre_deregister_waitidle_callback (void *opaque)
519 struct liblustre_wait_callback *llwc = opaque;
521 list_del(&llwc->llwc_list);
522 OBD_FREE(llwc, sizeof(*llwc));
526 liblustre_register_wait_callback (const char *name,
527 int (*fn)(void *arg), void *arg)
529 return liblustre_register_waitidle_callback(&liblustre_wait_callbacks,
534 liblustre_deregister_wait_callback (void *opaque)
536 liblustre_deregister_waitidle_callback(opaque);
540 liblustre_register_idle_callback (const char *name,
541 int (*fn)(void *arg), void *arg)
543 return liblustre_register_waitidle_callback(&liblustre_idle_callbacks,
548 liblustre_deregister_idle_callback (void *opaque)
550 liblustre_deregister_waitidle_callback(opaque);
554 liblustre_check_events (int timeout)
561 rc = LNetEQPoll(&ptlrpc_eq_h, 1, timeout * 1000, &ev, &i);
565 LASSERT (rc == -EOVERFLOW || rc == 1);
567 /* liblustre: no asynch callback so we can't affort to miss any
569 if (rc == -EOVERFLOW) {
570 CERROR ("Dropped an event!!!\n");
574 ptlrpc_master_callback (&ev);
578 int liblustre_waiting = 0;
581 liblustre_wait_event (int timeout)
583 struct list_head *tmp;
584 struct liblustre_wait_callback *llwc;
585 int found_something = 0;
587 /* single threaded recursion check... */
588 liblustre_waiting = 1;
591 /* Deal with all pending events */
592 while (liblustre_check_events(0))
595 /* Give all registered callbacks a bite at the cherry */
596 list_for_each(tmp, &liblustre_wait_callbacks) {
597 llwc = list_entry(tmp, struct liblustre_wait_callback,
600 if (llwc->llwc_fn(llwc->llwc_arg))
604 if (found_something || timeout == 0)
607 /* Nothing so far, but I'm allowed to block... */
608 found_something = liblustre_check_events(timeout);
609 if (!found_something) /* still nothing */
610 break; /* I timed out */
613 liblustre_waiting = 0;
615 return found_something;
619 liblustre_wait_idle(void)
621 static int recursed = 0;
623 struct list_head *tmp;
624 struct liblustre_wait_callback *llwc;
631 liblustre_wait_event(0);
635 list_for_each(tmp, &liblustre_idle_callbacks) {
636 llwc = list_entry(tmp, struct liblustre_wait_callback,
639 if (!llwc->llwc_fn(llwc->llwc_arg)) {
650 #endif /* __KERNEL__ */
652 int ptlrpc_init_portals(void)
654 int rc = ptlrpc_ni_init();
657 CERROR("network initialisation failed\n");
661 liblustre_services_callback =
662 liblustre_register_wait_callback("liblustre_check_services",
663 &liblustre_check_services,
666 rc = ptlrpcd_addref();
670 CERROR("rpcd initialisation failed\n");
672 liblustre_deregister_wait_callback(liblustre_services_callback);
678 void ptlrpc_exit_portals(void)
681 liblustre_deregister_wait_callback(liblustre_services_callback);