Whamcloud - gitweb
61419d32da394aed865ca781c58a9b6d217112df
[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, 2003 Cluster File Systems, Inc.
5  *
6  *   This file is part of the Lustre file system, http://www.lustre.org
7  *   Lustre is a trademark of Cluster File Systems, Inc.
8  *
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.
13  *
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.
18  *
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.
23  *
24  */
25
26 #define DEBUG_SUBSYSTEM S_RPC
27
28 #ifndef __KERNEL__
29 # include <liblustre.h>
30 #else
31 # ifdef __mips64__
32 #  include <linux/kernel.h>
33 # endif
34 #endif
35 #include <obd_class.h>
36 #include <lustre_net.h>
37 #include "ptlrpc_internal.h"
38
39 lnet_handle_eq_t   ptlrpc_eq_h;
40
41 /*  
42  *  Client's outgoing request callback
43  */
44 void request_out_callback(lnet_event_t *ev)
45 {
46         struct ptlrpc_cb_id   *cbid = ev->md.user_ptr;
47         struct ptlrpc_request *req = cbid->cbid_arg;
48         ENTRY;
49
50         LASSERT (ev->type == LNET_EVENT_SEND ||
51                  ev->type == LNET_EVENT_UNLINK);
52         LASSERT (ev->unlinked);
53
54         DEBUG_REQ((ev->status == 0) ? D_NET : D_ERROR, req,
55                   "type %d, status %d", ev->type, ev->status);
56
57         if (ev->type == LNET_EVENT_UNLINK || ev->status != 0) {
58
59                 /* Failed send: make it seem like the reply timed out, just
60                  * like failing sends in client.c does currently...  */
61
62                 spin_lock(&req->rq_lock);
63                 req->rq_net_err = 1;
64                 spin_unlock(&req->rq_lock);
65
66                 ptlrpc_wake_client_req(req);
67         }
68
69         /* these balance the references in ptl_send_rpc() */
70         atomic_dec(&req->rq_import->imp_inflight);
71         ptlrpc_req_finished(req);
72
73         EXIT;
74 }
75
76 /*
77  * Client's incoming reply callback
78  */
79 void reply_in_callback(lnet_event_t *ev)
80 {
81         struct ptlrpc_cb_id   *cbid = ev->md.user_ptr;
82         struct ptlrpc_request *req = cbid->cbid_arg;
83         ENTRY;
84
85         LASSERT (ev->type == LNET_EVENT_PUT ||
86                  ev->type == LNET_EVENT_UNLINK);
87         LASSERT (ev->unlinked);
88         LASSERT (ev->md.start == req->rq_repmsg);
89         LASSERT (ev->offset == 0);
90         LASSERT (ev->mlength <= req->rq_replen);
91         
92         DEBUG_REQ((ev->status == 0) ? D_NET : D_ERROR, req,
93                   "type %d, status %d", ev->type, ev->status);
94
95         spin_lock(&req->rq_lock);
96
97         LASSERT (req->rq_receiving_reply);
98         req->rq_receiving_reply = 0;
99
100         if (ev->type == LNET_EVENT_PUT && ev->status == 0) {
101                 req->rq_replied = 1;
102                 req->rq_nob_received = ev->mlength;
103         }
104
105         /* NB don't unlock till after wakeup; req can disappear under us
106          * since we don't have our own ref */
107         ptlrpc_wake_client_req(req);
108
109         spin_unlock(&req->rq_lock);
110         EXIT;
111 }
112
113 /* 
114  * Client's bulk has been written/read
115  */
116 void client_bulk_callback (lnet_event_t *ev)
117 {
118         struct ptlrpc_cb_id     *cbid = ev->md.user_ptr;
119         struct ptlrpc_bulk_desc *desc = cbid->cbid_arg;
120         ENTRY;
121
122         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_CLIENT_BULK_CB))
123                 ev->status = -EIO;
124
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);
131
132         CDEBUG((ev->status == 0) ? D_NET : D_ERROR,
133                "event type %d, status %d, desc %p\n", 
134                ev->type, ev->status, desc);
135
136         spin_lock(&desc->bd_lock);
137
138         LASSERT(desc->bd_network_rw);
139         desc->bd_network_rw = 0;
140
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;
145         }
146
147         /* NB don't unlock till after wakeup; desc can disappear under us
148          * otherwise */
149         ptlrpc_wake_client_req(desc->bd_req);
150
151         spin_unlock(&desc->bd_lock);
152         EXIT;
153 }
154
155 /* 
156  * Server's incoming request callback
157  */
158 void request_in_callback(lnet_event_t *ev)
159 {
160         struct ptlrpc_cb_id               *cbid = ev->md.user_ptr;
161         struct ptlrpc_request_buffer_desc *rqbd = cbid->cbid_arg;
162         struct ptlrpc_service             *service = rqbd->rqbd_service;
163         struct ptlrpc_request             *req;
164         ENTRY;
165
166         LASSERT (ev->type == LNET_EVENT_PUT ||
167                  ev->type == LNET_EVENT_UNLINK);
168         LASSERT ((char *)ev->md.start >= rqbd->rqbd_buffer);
169         LASSERT ((char *)ev->md.start + ev->offset + ev->mlength <=
170                  rqbd->rqbd_buffer + service->srv_buf_size);
171
172         CDEBUG((ev->status == 0) ? D_NET : D_ERROR,
173                "event type %d, status %d, service %s\n", 
174                ev->type, ev->status, service->srv_name);
175
176         if (ev->unlinked) {
177                 /* If this is the last request message to fit in the
178                  * request buffer we can use the request object embedded in
179                  * rqbd.  Note that if we failed to allocate a request,
180                  * we'd have to re-post the rqbd, which we can't do in this
181                  * context. */
182                 req = &rqbd->rqbd_req;
183                 memset(req, 0, sizeof (*req));
184         } else {
185                 LASSERT (ev->type == LNET_EVENT_PUT);
186                 if (ev->status != 0) {
187                         /* We moaned above already... */
188                         return;
189                 }
190                 OBD_ALLOC_GFP(req, sizeof(*req), CFS_ALLOC_ATOMIC_TRY);
191                 if (req == NULL) {
192                         CERROR("Can't allocate incoming request descriptor: "
193                                "Dropping %s RPC from %s\n",
194                                service->srv_name, 
195                                libcfs_id2str(ev->initiator));
196                         return;
197                 }
198         }
199
200         /* NB we ABSOLUTELY RELY on req being zeroed, so pointers are NULL,
201          * flags are reset and scalars are zero.  We only set the message
202          * size to non-zero if this was a successful receive. */
203         req->rq_xid = ev->match_bits;
204         req->rq_reqmsg = ev->md.start + ev->offset;
205         if (ev->type == LNET_EVENT_PUT && ev->status == 0)
206                 req->rq_reqlen = ev->mlength;
207         do_gettimeofday(&req->rq_arrival_time);
208         req->rq_peer = ev->initiator;
209         req->rq_self = ev->target.nid;
210         req->rq_rqbd = rqbd;
211         req->rq_phase = RQ_PHASE_NEW;
212 #ifdef CRAY_XT3
213         req->rq_uid = ev->uid;
214 #endif
215
216         spin_lock(&service->srv_lock);
217
218         req->rq_history_seq = service->srv_request_seq++;
219         list_add_tail(&req->rq_history_list, &service->srv_request_history);
220
221         if (ev->unlinked) {
222                 service->srv_nrqbd_receiving--;
223                 CDEBUG(D_RPCTRACE,"Buffer complete: %d buffers still posted\n",
224                        service->srv_nrqbd_receiving);
225
226                 /* Normally, don't complain about 0 buffers posted; LNET won't
227                  * drop incoming reqs since we set the portal lazy */
228                 if (test_req_buffer_pressure &&
229                     ev->type != LNET_EVENT_UNLINK &&
230                     service->srv_nrqbd_receiving == 0)
231                         CWARN("All %s request buffers busy\n",
232                               service->srv_name);
233
234                 /* req takes over the network's ref on rqbd */
235         } else {
236                 /* req takes a ref on rqbd */
237                 rqbd->rqbd_refcount++;
238         }
239
240         list_add_tail(&req->rq_list, &service->srv_request_queue);
241         service->srv_n_queued_reqs++;
242
243         /* NB everything can disappear under us once the request
244          * has been queued and we unlock, so do the wake now... */
245         cfs_waitq_signal(&service->srv_waitq);
246
247         spin_unlock(&service->srv_lock);
248         EXIT;
249 }
250
251 /*
252  *  Server's outgoing reply callback
253  */
254 void reply_out_callback(lnet_event_t *ev)
255 {
256         struct ptlrpc_cb_id       *cbid = ev->md.user_ptr;
257         struct ptlrpc_reply_state *rs = cbid->cbid_arg;
258         struct ptlrpc_service     *svc = rs->rs_service;
259         ENTRY;
260
261         LASSERT (ev->type == LNET_EVENT_SEND ||
262                  ev->type == LNET_EVENT_ACK ||
263                  ev->type == LNET_EVENT_UNLINK);
264
265         if (!rs->rs_difficult) {
266                 /* 'Easy' replies have no further processing so I drop the
267                  * net's ref on 'rs' */
268                 LASSERT (ev->unlinked);
269                 ptlrpc_rs_decref(rs);
270                 atomic_dec (&svc->srv_outstanding_replies);
271                 EXIT;
272                 return;
273         }
274
275         LASSERT (rs->rs_on_net);
276
277         if (ev->unlinked) {
278                 /* Last network callback.  The net's ref on 'rs' stays put
279                  * until ptlrpc_server_handle_reply() is done with it */
280                 spin_lock(&svc->srv_lock);
281                 rs->rs_on_net = 0;
282                 ptlrpc_schedule_difficult_reply (rs);
283                 spin_unlock(&svc->srv_lock);
284         }
285
286         EXIT;
287 }
288
289 /*
290  * Server's bulk completion callback
291  */
292 void server_bulk_callback (lnet_event_t *ev)
293 {
294         struct ptlrpc_cb_id     *cbid = ev->md.user_ptr;
295         struct ptlrpc_bulk_desc *desc = cbid->cbid_arg;
296         ENTRY;
297
298         LASSERT (ev->type == LNET_EVENT_SEND ||
299                  ev->type == LNET_EVENT_UNLINK ||
300                  (desc->bd_type == BULK_PUT_SOURCE &&
301                   ev->type == LNET_EVENT_ACK) ||
302                  (desc->bd_type == BULK_GET_SINK &&
303                   ev->type == LNET_EVENT_REPLY));
304
305         CDEBUG((ev->status == 0) ? D_NET : D_ERROR,
306                "event type %d, status %d, desc %p\n", 
307                ev->type, ev->status, desc);
308
309         spin_lock(&desc->bd_lock);
310         
311         if ((ev->type == LNET_EVENT_ACK ||
312              ev->type == LNET_EVENT_REPLY) &&
313             ev->status == 0) {
314                 /* We heard back from the peer, so even if we get this
315                  * before the SENT event (oh yes we can), we know we
316                  * read/wrote the peer buffer and how much... */
317                 desc->bd_success = 1;
318                 desc->bd_nob_transferred = ev->mlength;
319                 desc->bd_sender = ev->sender;
320         }
321
322         if (ev->unlinked) {
323                 /* This is the last callback no matter what... */
324                 desc->bd_network_rw = 0;
325                 cfs_waitq_signal(&desc->bd_waitq);
326         }
327
328         spin_unlock(&desc->bd_lock);
329         EXIT;
330 }
331
332 static void ptlrpc_master_callback(lnet_event_t *ev)
333 {
334         struct ptlrpc_cb_id *cbid = ev->md.user_ptr;
335         void (*callback)(lnet_event_t *ev) = cbid->cbid_fn;
336
337         /* Honestly, it's best to find out early. */
338         LASSERT (cbid->cbid_arg != LP_POISON);
339         LASSERT (callback == request_out_callback ||
340                  callback == reply_in_callback ||
341                  callback == client_bulk_callback ||
342                  callback == request_in_callback ||
343                  callback == reply_out_callback ||
344                  callback == server_bulk_callback);
345         
346         callback (ev);
347 }
348
349 int ptlrpc_uuid_to_peer (struct obd_uuid *uuid, 
350                          lnet_process_id_t *peer, lnet_nid_t *self)
351 {
352         int               best_dist = 0;
353         int               best_order = 0;
354         int               count = 0;
355         int               rc = -ENOENT;
356         int               portals_compatibility;
357         int               dist;
358         int               order;
359         lnet_nid_t        dst_nid;
360         lnet_nid_t        src_nid;
361
362         portals_compatibility = LNetCtl(IOC_LIBCFS_PORTALS_COMPATIBILITY, NULL);
363
364         peer->pid = LUSTRE_SRV_LNET_PID;
365
366         /* Choose the matching UUID that's closest */
367         while (lustre_uuid_to_peer(uuid->uuid, &dst_nid, count++) == 0) {
368                 dist = LNetDist(dst_nid, &src_nid, &order);
369                 if (dist < 0)
370                         continue;
371
372                 if (dist == 0) {                /* local! use loopback LND */
373                         peer->nid = *self = LNET_MKNID(LNET_MKNET(LOLND, 0), 0);
374                         rc = 0;
375                         break;
376                 }
377                 
378                 LASSERT (order >= 0);
379                 if (rc < 0 ||
380                     dist < best_dist ||
381                     (dist == best_dist && order < best_order)) {
382                         best_dist = dist;
383                         best_order = order;
384
385                         if (portals_compatibility > 1) {
386                                 /* Strong portals compatibility: Zero the nid's
387                                  * NET, so if I'm reading new config logs, or
388                                  * getting configured by (new) lconf I can
389                                  * still talk to old servers. */
390                                 dst_nid = LNET_MKNID(0, LNET_NIDADDR(dst_nid));
391                                 src_nid = LNET_MKNID(0, LNET_NIDADDR(src_nid));
392                         }
393                         peer->nid = dst_nid;
394                         *self = src_nid;
395                         rc = 0;
396                 }
397         }
398
399         CDEBUG(D_NET,"%s->%s\n", uuid->uuid, libcfs_id2str(*peer));
400         if (rc != 0) 
401                 CERROR("No NID found for %s\n", uuid->uuid);
402         return rc;
403 }
404
405 void ptlrpc_ni_fini(void)
406 {
407         cfs_waitq_t         waitq;
408         struct l_wait_info  lwi;
409         int                 rc;
410         int                 retries;
411         
412         /* Wait for the event queue to become idle since there may still be
413          * messages in flight with pending events (i.e. the fire-and-forget
414          * messages == client requests and "non-difficult" server
415          * replies */
416
417         for (retries = 0;; retries++) {
418                 rc = LNetEQFree(ptlrpc_eq_h);
419                 switch (rc) {
420                 default:
421                         LBUG();
422
423                 case 0:
424                         LNetNIFini();
425                         return;
426                         
427                 case -EBUSY:
428                         if (retries != 0)
429                                 CWARN("Event queue still busy\n");
430                         
431                         /* Wait for a bit */
432                         cfs_waitq_init(&waitq);
433                         lwi = LWI_TIMEOUT(cfs_time_seconds(2), NULL, NULL);
434                         l_wait_event(waitq, 0, &lwi);
435                         break;
436                 }
437         }
438         /* notreached */
439 }
440
441 lnet_pid_t ptl_get_pid(void)
442 {
443         lnet_pid_t        pid;
444
445 #ifndef  __KERNEL__
446         pid = getpid();
447 #else
448         pid = LUSTRE_SRV_LNET_PID;
449 #endif
450         return pid;
451 }
452         
453 int ptlrpc_ni_init(void)
454 {
455         int              rc;
456         lnet_pid_t       pid;
457
458         pid = ptl_get_pid();
459         CDEBUG(D_NET, "My pid is: %x\n", pid);
460
461         /* We're not passing any limits yet... */
462         rc = LNetNIInit(pid);
463         if (rc < 0) {
464                 CDEBUG (D_NET, "Can't init network interface: %d\n", rc);
465                 return (-ENOENT);
466         }
467
468         /* CAVEAT EMPTOR: how we process portals events is _radically_
469          * different depending on... */
470 #ifdef __KERNEL__
471         /* kernel portals calls our master callback when events are added to
472          * the event queue.  In fact lustre never pulls events off this queue,
473          * so it's only sized for some debug history. */
474         rc = LNetEQAlloc(1024, ptlrpc_master_callback, &ptlrpc_eq_h);
475 #else
476         /* liblustre calls the master callback when it removes events from the
477          * event queue.  The event queue has to be big enough not to drop
478          * anything */
479         rc = LNetEQAlloc(10240, LNET_EQ_HANDLER_NONE, &ptlrpc_eq_h);
480 #endif
481         if (rc == 0)
482                 return 0;
483
484         CERROR ("Failed to allocate event queue: %d\n", rc);
485         LNetNIFini();
486
487         return (-ENOMEM);
488 }
489
490 #ifndef __KERNEL__
491 CFS_LIST_HEAD(liblustre_wait_callbacks);
492 CFS_LIST_HEAD(liblustre_idle_callbacks);
493 void *liblustre_services_callback;
494
495 void *
496 liblustre_register_waitidle_callback (struct list_head *callback_list,
497                                       const char *name,
498                                       int (*fn)(void *arg), void *arg)
499 {
500         struct liblustre_wait_callback *llwc;
501         
502         OBD_ALLOC(llwc, sizeof(*llwc));
503         LASSERT (llwc != NULL);
504         
505         llwc->llwc_name = name;
506         llwc->llwc_fn = fn;
507         llwc->llwc_arg = arg;
508         list_add_tail(&llwc->llwc_list, callback_list);
509         
510         return (llwc);
511 }
512
513 void
514 liblustre_deregister_waitidle_callback (void *opaque)
515 {
516         struct liblustre_wait_callback *llwc = opaque;
517         
518         list_del(&llwc->llwc_list);
519         OBD_FREE(llwc, sizeof(*llwc));
520 }
521
522 void *
523 liblustre_register_wait_callback (const char *name,
524                                   int (*fn)(void *arg), void *arg)
525 {
526         return liblustre_register_waitidle_callback(&liblustre_wait_callbacks,
527                                                     name, fn, arg);
528 }
529
530 void
531 liblustre_deregister_wait_callback (void *opaque)
532 {
533         liblustre_deregister_waitidle_callback(opaque);
534 }
535
536 void *
537 liblustre_register_idle_callback (const char *name,
538                                   int (*fn)(void *arg), void *arg)
539 {
540         return liblustre_register_waitidle_callback(&liblustre_idle_callbacks,
541                                                     name, fn, arg);
542 }
543
544 void
545 liblustre_deregister_idle_callback (void *opaque)
546 {
547         liblustre_deregister_waitidle_callback(opaque);
548 }
549
550 int
551 liblustre_check_events (int timeout)
552 {
553         lnet_event_t ev;
554         int         rc;
555         int         i;
556         ENTRY;
557
558         rc = LNetEQPoll(&ptlrpc_eq_h, 1, timeout * 1000, &ev, &i);
559         if (rc == 0)
560                 RETURN(0);
561         
562         LASSERT (rc == -EOVERFLOW || rc == 1);
563         
564         /* liblustre: no asynch callback so we can't affort to miss any
565          * events... */
566         if (rc == -EOVERFLOW) {
567                 CERROR ("Dropped an event!!!\n");
568                 abort();
569         }
570         
571         ptlrpc_master_callback (&ev);
572         RETURN(1);
573 }
574
575 int liblustre_waiting = 0;
576
577 int
578 liblustre_wait_event (int timeout)
579 {
580         struct list_head               *tmp;
581         struct liblustre_wait_callback *llwc;
582         int                             found_something = 0;
583
584         /* single threaded recursion check... */
585         liblustre_waiting = 1;
586
587         for (;;) {
588                 /* Deal with all pending events */
589                 while (liblustre_check_events(0))
590                         found_something = 1;
591
592                 /* Give all registered callbacks a bite at the cherry */
593                 list_for_each(tmp, &liblustre_wait_callbacks) {
594                         llwc = list_entry(tmp, struct liblustre_wait_callback, 
595                                           llwc_list);
596                 
597                         if (llwc->llwc_fn(llwc->llwc_arg))
598                                 found_something = 1;
599                 }
600
601                 if (found_something || timeout == 0)
602                         break;
603
604                 /* Nothing so far, but I'm allowed to block... */
605                 found_something = liblustre_check_events(timeout);
606                 if (!found_something)           /* still nothing */
607                         break;                  /* I timed out */
608         }
609
610         liblustre_waiting = 0;
611
612         return found_something;
613 }
614
615 void
616 liblustre_wait_idle(void)
617 {
618         static int recursed = 0;
619         
620         struct list_head               *tmp;
621         struct liblustre_wait_callback *llwc;
622         int                             idle = 0;
623
624         LASSERT(!recursed);
625         recursed = 1;
626         
627         do {
628                 liblustre_wait_event(0);
629
630                 idle = 1;
631
632                 list_for_each(tmp, &liblustre_idle_callbacks) {
633                         llwc = list_entry(tmp, struct liblustre_wait_callback,
634                                           llwc_list);
635                         
636                         if (!llwc->llwc_fn(llwc->llwc_arg)) {
637                                 idle = 0;
638                                 break;
639                         }
640                 }
641                         
642         } while (!idle);
643
644         recursed = 0;
645 }
646
647 #endif /* __KERNEL__ */
648
649 int ptlrpc_init_portals(void)
650 {
651         int   rc = ptlrpc_ni_init();
652
653         if (rc != 0) {
654                 CERROR("network initialisation failed\n");
655                 return -EIO;
656         }
657 #ifndef __KERNEL__
658         liblustre_services_callback = 
659                 liblustre_register_wait_callback("liblustre_check_services",
660                                                  &liblustre_check_services, NULL);
661 #endif
662         rc = ptlrpcd_addref();
663         if (rc == 0)
664                 return 0;
665         
666         CERROR("rpcd initialisation failed\n");
667 #ifndef __KERNEL__
668         liblustre_deregister_wait_callback(liblustre_services_callback);
669 #endif
670         ptlrpc_ni_fini();
671         return rc;
672 }
673
674 void ptlrpc_exit_portals(void)
675 {
676 #ifndef __KERNEL__
677         liblustre_deregister_wait_callback(liblustre_services_callback);
678 #endif
679         ptlrpcd_decref();
680         ptlrpc_ni_fini();
681 }