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