Whamcloud - gitweb
LU-1425 build: make Lustre build ready for gcov
[fs/lustre-release.git] / lnet / lnet / lib-msg.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * lnet/lnet/lib-msg.c
35  *
36  * Message decoding, parsing and finalizing routines
37  */
38
39 #define DEBUG_SUBSYSTEM S_LNET
40
41 #include <lnet/lib-lnet.h>
42
43 void
44 lnet_build_unlink_event (lnet_libmd_t *md, lnet_event_t *ev)
45 {
46         ENTRY;
47
48         memset(ev, 0, sizeof(*ev));
49
50         ev->status   = 0;
51         ev->unlinked = 1;
52         ev->type     = LNET_EVENT_UNLINK;
53         lnet_md_deconstruct(md, &ev->md);
54         lnet_md2handle(&ev->md_handle, md);
55         EXIT;
56 }
57
58 /*
59  * Don't need any lock, must be called after lnet_commit_md
60  */
61 void
62 lnet_build_msg_event(lnet_msg_t *msg, lnet_event_kind_t ev_type)
63 {
64         lnet_hdr_t      *hdr = &msg->msg_hdr;
65         lnet_event_t    *ev  = &msg->msg_ev;
66
67         LASSERT(!msg->msg_routing);
68
69         ev->type = ev_type;
70
71         if (ev_type == LNET_EVENT_SEND) {
72                 /* event for active message */
73                 ev->target.nid    = le64_to_cpu(hdr->dest_nid);
74                 ev->target.pid    = le32_to_cpu(hdr->dest_pid);
75                 ev->initiator.nid = LNET_NID_ANY;
76                 ev->initiator.pid = the_lnet.ln_pid;
77                 ev->sender        = LNET_NID_ANY;
78
79         } else {
80                 /* event for passive message */
81                 ev->target.pid    = hdr->dest_pid;
82                 ev->target.nid    = hdr->dest_nid;
83                 ev->initiator.pid = hdr->src_pid;
84                 ev->initiator.nid = hdr->src_nid;
85                 ev->rlength       = hdr->payload_length;
86                 ev->sender        = msg->msg_from;
87                 ev->mlength       = msg->msg_wanted;
88                 ev->offset        = msg->msg_offset;
89         }
90
91         switch (ev_type) {
92         default:
93                 LBUG();
94
95         case LNET_EVENT_PUT: /* passive PUT */
96                 ev->pt_index   = hdr->msg.put.ptl_index;
97                 ev->match_bits = hdr->msg.put.match_bits;
98                 ev->hdr_data   = hdr->msg.put.hdr_data;
99                 return;
100
101         case LNET_EVENT_GET: /* passive GET */
102                 ev->pt_index   = hdr->msg.get.ptl_index;
103                 ev->match_bits = hdr->msg.get.match_bits;
104                 ev->hdr_data   = 0;
105                 return;
106
107         case LNET_EVENT_ACK: /* ACK */
108                 ev->match_bits = hdr->msg.ack.match_bits;
109                 ev->mlength    = hdr->msg.ack.mlength;
110                 return;
111
112         case LNET_EVENT_REPLY: /* REPLY */
113                 return;
114
115         case LNET_EVENT_SEND: /* active message */
116                 if (msg->msg_type == LNET_MSG_PUT) {
117                         ev->pt_index   = le32_to_cpu(hdr->msg.put.ptl_index);
118                         ev->match_bits = le64_to_cpu(hdr->msg.put.match_bits);
119                         ev->offset     = le32_to_cpu(hdr->msg.put.offset);
120                         ev->mlength    =
121                         ev->rlength    = le32_to_cpu(hdr->payload_length);
122                         ev->hdr_data   = le64_to_cpu(hdr->msg.put.hdr_data);
123
124                 } else {
125                         LASSERT(msg->msg_type == LNET_MSG_GET);
126                         ev->pt_index   = le32_to_cpu(hdr->msg.get.ptl_index);
127                         ev->match_bits = le64_to_cpu(hdr->msg.get.match_bits);
128                         ev->mlength    =
129                         ev->rlength    = le32_to_cpu(hdr->msg.get.sink_length);
130                         ev->offset     = le32_to_cpu(hdr->msg.get.src_offset);
131                         ev->hdr_data   = 0;
132                 }
133                 return;
134         }
135 }
136
137 void
138 lnet_msg_commit(lnet_msg_t *msg, int cpt)
139 {
140         struct lnet_msg_container *container = the_lnet.ln_msg_containers[cpt];
141         lnet_counters_t           *counters  = the_lnet.ln_counters[cpt];
142
143         /* routed message can be committed for both receiving and sending */
144         LASSERT(!msg->msg_tx_committed);
145
146         if (msg->msg_sending) {
147                 LASSERT(!msg->msg_receiving);
148
149                 msg->msg_tx_cpt = cpt;
150                 msg->msg_tx_committed = 1;
151                 if (msg->msg_rx_committed) { /* routed message REPLY */
152                         LASSERT(msg->msg_onactivelist);
153                         return;
154                 }
155         } else {
156                 LASSERT(!msg->msg_sending);
157                 msg->msg_rx_cpt = cpt;
158                 msg->msg_rx_committed = 1;
159         }
160
161         LASSERT(!msg->msg_onactivelist);
162         msg->msg_onactivelist = 1;
163         cfs_list_add(&msg->msg_activelist, &container->msc_active);
164
165         counters->msgs_alloc++;
166         if (counters->msgs_alloc > counters->msgs_max)
167                 counters->msgs_max = counters->msgs_alloc;
168 }
169
170 static void
171 lnet_msg_decommit_tx(lnet_msg_t *msg, int status)
172 {
173         lnet_counters_t *counters;
174         lnet_event_t    *ev = &msg->msg_ev;
175
176         LASSERT(msg->msg_tx_committed);
177         if (status != 0)
178                 goto out;
179
180         counters = the_lnet.ln_counters[msg->msg_tx_cpt];
181         switch (ev->type) {
182         default: /* routed message */
183                 LASSERT(msg->msg_routing);
184                 LASSERT(msg->msg_rx_committed);
185                 LASSERT(ev->type == 0);
186
187                 counters->route_length += msg->msg_len;
188                 counters->route_count++;
189                 goto out;
190
191         case LNET_EVENT_PUT:
192                 /* should have been decommitted */
193                 LASSERT(!msg->msg_rx_committed);
194                 /* overwritten while sending ACK */
195                 LASSERT(msg->msg_type == LNET_MSG_ACK);
196                 msg->msg_type = LNET_MSG_PUT; /* fix type */
197                 break;
198
199         case LNET_EVENT_SEND:
200                 LASSERT(!msg->msg_rx_committed);
201                 if (msg->msg_type == LNET_MSG_PUT)
202                         counters->send_length += msg->msg_len;
203                 break;
204
205         case LNET_EVENT_GET:
206                 LASSERT(msg->msg_rx_committed);
207                 /* overwritten while sending reply, we should never be
208                  * here for optimized GET */
209                 LASSERT(msg->msg_type == LNET_MSG_REPLY);
210                 msg->msg_type = LNET_MSG_GET; /* fix type */
211                 break;
212         }
213
214         counters->send_count++;
215  out:
216         lnet_return_tx_credits_locked(msg);
217         msg->msg_tx_committed = 0;
218 }
219
220 static void
221 lnet_msg_decommit_rx(lnet_msg_t *msg, int status)
222 {
223         lnet_counters_t *counters;
224         lnet_event_t    *ev = &msg->msg_ev;
225
226         LASSERT(!msg->msg_tx_committed); /* decommitted or never committed */
227         LASSERT(msg->msg_rx_committed);
228
229         if (status != 0)
230                 goto out;
231
232         counters = the_lnet.ln_counters[msg->msg_rx_cpt];
233         switch (ev->type) {
234         default:
235                 LASSERT(ev->type == 0);
236                 LASSERT(msg->msg_routing);
237                 goto out;
238
239         case LNET_EVENT_ACK:
240                 LASSERT(msg->msg_type == LNET_MSG_ACK);
241                 break;
242
243         case LNET_EVENT_GET:
244                 /* type is "REPLY" if it's an optimized GET on passive side,
245                  * because optimized GET will never be committed for sending,
246                  * so message type wouldn't be changed back to "GET" by
247                  * lnet_msg_decommit_tx(), see details in lnet_parse_get() */
248                 LASSERT(msg->msg_type == LNET_MSG_REPLY ||
249                         msg->msg_type == LNET_MSG_GET);
250                 counters->send_length += msg->msg_wanted;
251                 break;
252
253         case LNET_EVENT_PUT:
254                 LASSERT(msg->msg_type == LNET_MSG_PUT);
255                 break;
256
257         case LNET_EVENT_REPLY:
258                 /* type is "GET" if it's an optimized GET on active side,
259                  * see details in lnet_create_reply_msg() */
260                 LASSERT(msg->msg_type == LNET_MSG_GET ||
261                         msg->msg_type == LNET_MSG_REPLY);
262                 break;
263         }
264
265         counters->recv_count++;
266         if (ev->type == LNET_EVENT_PUT || ev->type == LNET_EVENT_REPLY)
267                 counters->recv_length += msg->msg_wanted;
268
269  out:
270         lnet_return_rx_credits_locked(msg);
271         msg->msg_rx_committed = 0;
272 }
273
274 void
275 lnet_msg_decommit(lnet_msg_t *msg, int cpt, int status)
276 {
277         int     cpt2 = cpt;
278
279         LASSERT(msg->msg_tx_committed || msg->msg_rx_committed);
280         LASSERT(msg->msg_onactivelist);
281
282         if (msg->msg_tx_committed) { /* always decommit for sending first */
283                 LASSERT(cpt == msg->msg_tx_cpt);
284                 lnet_msg_decommit_tx(msg, status);
285         }
286
287         if (msg->msg_rx_committed) {
288                 /* forwarding msg committed for both receiving and sending */
289                 if (cpt != msg->msg_rx_cpt) {
290                         lnet_net_unlock(cpt);
291                         cpt2 = msg->msg_rx_cpt;
292                         lnet_net_lock(cpt2);
293                 }
294                 lnet_msg_decommit_rx(msg, status);
295         }
296
297         cfs_list_del(&msg->msg_activelist);
298         msg->msg_onactivelist = 0;
299
300         the_lnet.ln_counters[cpt2]->msgs_alloc--;
301
302         if (cpt2 != cpt) {
303                 lnet_net_unlock(cpt2);
304                 lnet_net_lock(cpt);
305         }
306 }
307
308 void
309 lnet_msg_attach_md(lnet_msg_t *msg, lnet_libmd_t *md,
310                    unsigned int offset, unsigned int mlen)
311 {
312         /* NB: @offset and @len are only useful for receiving */
313         /* Here, we attach the MD on lnet_msg and mark it busy and
314          * decrementing its threshold. Come what may, the lnet_msg "owns"
315          * the MD until a call to lnet_msg_detach_md or lnet_finalize()
316          * signals completion. */
317         LASSERT(!msg->msg_routing);
318
319         msg->msg_md = md;
320         if (msg->msg_receiving) { /* commited for receiving */
321                 msg->msg_offset = offset;
322                 msg->msg_wanted = mlen;
323         }
324
325         md->md_refcount++;
326         if (md->md_threshold != LNET_MD_THRESH_INF) {
327                 LASSERT(md->md_threshold > 0);
328                 md->md_threshold--;
329         }
330
331         /* build umd in event */
332         lnet_md2handle(&msg->msg_ev.md_handle, md);
333         lnet_md_deconstruct(md, &msg->msg_ev.md);
334 }
335
336 void
337 lnet_msg_detach_md(lnet_msg_t *msg, int status)
338 {
339         lnet_libmd_t    *md = msg->msg_md;
340         int             unlink;
341
342         /* Now it's safe to drop my caller's ref */
343         md->md_refcount--;
344         LASSERT(md->md_refcount >= 0);
345
346         unlink = lnet_md_unlinkable(md);
347         if (md->md_eq != NULL) {
348                 msg->msg_ev.status   = status;
349                 msg->msg_ev.unlinked = unlink;
350                 lnet_eq_enqueue_event(md->md_eq, &msg->msg_ev);
351         }
352
353         if (unlink)
354                 lnet_md_unlink(md);
355
356         msg->msg_md = NULL;
357 }
358
359 static int
360 lnet_complete_msg_locked(lnet_msg_t *msg, int cpt)
361 {
362         lnet_handle_wire_t ack_wmd;
363         int                rc;
364         int                status = msg->msg_ev.status;
365
366         LASSERT (msg->msg_onactivelist);
367
368         if (status == 0 && msg->msg_ack) {
369                 /* Only send an ACK if the PUT completed successfully */
370
371                 lnet_msg_decommit(msg, cpt, 0);
372
373                 msg->msg_ack = 0;
374                 lnet_net_unlock(cpt);
375
376                 LASSERT(msg->msg_ev.type == LNET_EVENT_PUT);
377                 LASSERT(!msg->msg_routing);
378
379                 ack_wmd = msg->msg_hdr.msg.put.ack_wmd;
380
381                 lnet_prep_send(msg, LNET_MSG_ACK, msg->msg_ev.initiator, 0, 0);
382
383                 msg->msg_hdr.msg.ack.dst_wmd = ack_wmd;
384                 msg->msg_hdr.msg.ack.match_bits = msg->msg_ev.match_bits;
385                 msg->msg_hdr.msg.ack.mlength = cpu_to_le32(msg->msg_ev.mlength);
386
387                 /* NB: we probably want to use NID of msg::msg_from as 3rd
388                  * parameter (router NID) if it's routed message */
389                 rc = lnet_send(msg->msg_ev.target.nid, msg, LNET_NID_ANY);
390
391                 lnet_net_lock(cpt);
392                 /*
393                  * NB: message is committed for sending, we should return
394                  * on success because LND will finalize this message later.
395                  *
396                  * Also, there is possibility that message is commited for
397                  * sending and also failed before delivering to LND,
398                  * i.e: ENOMEM, in that case we can't fall through either
399                  * because CPT for sending can be different with CPT for
400                  * receiving, so we should return back to lnet_finalize()
401                  * to make sure we are locking the correct partition.
402                  */
403                 return rc;
404
405         } else if (status == 0 &&       /* OK so far */
406                    (msg->msg_routing && !msg->msg_sending)) {
407                 /* not forwarded */
408                 LASSERT(!msg->msg_receiving);   /* called back recv already */
409                 lnet_net_unlock(cpt);
410
411                 rc = lnet_send(LNET_NID_ANY, msg, LNET_NID_ANY);
412
413                 lnet_net_lock(cpt);
414                 /*
415                  * NB: message is committed for sending, we should return
416                  * on success because LND will finalize this message later.
417                  *
418                  * Also, there is possibility that message is commited for
419                  * sending and also failed before delivering to LND,
420                  * i.e: ENOMEM, in that case we can't fall through either:
421                  * - The rule is message must decommit for sending first if
422                  *   the it's committed for both sending and receiving
423                  * - CPT for sending can be different with CPT for receiving,
424                  *   so we should return back to lnet_finalize() to make
425                  *   sure we are locking the correct partition.
426                  */
427                 return rc;
428         }
429
430         lnet_msg_decommit(msg, cpt, status);
431         lnet_msg_free_locked(msg);
432         return 0;
433 }
434
435 void
436 lnet_finalize (lnet_ni_t *ni, lnet_msg_t *msg, int status)
437 {
438         struct lnet_msg_container       *container;
439         int                             my_slot;
440         int                             cpt;
441         int                             rc;
442         int                             i;
443
444         LASSERT (!cfs_in_interrupt ());
445
446         if (msg == NULL)
447                 return;
448 #if 0
449         CDEBUG(D_WARNING, "%s msg->%s Flags:%s%s%s%s%s%s%s%s%s%s%s txp %s rxp %s\n",
450                lnet_msgtyp2str(msg->msg_type), libcfs_id2str(msg->msg_target),
451                msg->msg_target_is_router ? "t" : "",
452                msg->msg_routing ? "X" : "",
453                msg->msg_ack ? "A" : "",
454                msg->msg_sending ? "S" : "",
455                msg->msg_receiving ? "R" : "",
456                msg->msg_delayed ? "d" : "",
457                msg->msg_txcredit ? "C" : "",
458                msg->msg_peertxcredit ? "c" : "",
459                msg->msg_rtrcredit ? "F" : "",
460                msg->msg_peerrtrcredit ? "f" : "",
461                msg->msg_onactivelist ? "!" : "",
462                msg->msg_txpeer == NULL ? "<none>" : libcfs_nid2str(msg->msg_txpeer->lp_nid),
463                msg->msg_rxpeer == NULL ? "<none>" : libcfs_nid2str(msg->msg_rxpeer->lp_nid));
464 #endif
465         msg->msg_ev.status = status;
466
467         if (msg->msg_md != NULL) {
468                 cpt = lnet_cpt_of_cookie(msg->msg_md->md_lh.lh_cookie);
469
470                 lnet_res_lock(cpt);
471                 lnet_msg_detach_md(msg, status);
472                 lnet_res_unlock(cpt);
473         }
474
475  again:
476         rc = 0;
477         if (!msg->msg_tx_committed && !msg->msg_rx_committed) {
478                 /* not commited to network yet */
479                 LASSERT(!msg->msg_onactivelist);
480                 lnet_msg_free(msg);
481                 return;
482         }
483
484         /*
485          * NB: routed message can be commited for both receiving and sending,
486          * we should finalize in LIFO order and keep counters correct.
487          * (finalize sending first then finalize receiving)
488          */
489         cpt = msg->msg_tx_committed ? msg->msg_tx_cpt : msg->msg_rx_cpt;
490         lnet_net_lock(cpt);
491
492         container = the_lnet.ln_msg_containers[cpt];
493         cfs_list_add_tail(&msg->msg_list, &container->msc_finalizing);
494
495         /* Recursion breaker.  Don't complete the message here if I am (or
496          * enough other threads are) already completing messages */
497
498 #ifdef __KERNEL__
499         my_slot = -1;
500         for (i = 0; i < container->msc_nfinalizers; i++) {
501                 if (container->msc_finalizers[i] == cfs_current())
502                         break;
503
504                 if (my_slot < 0 && container->msc_finalizers[i] == NULL)
505                         my_slot = i;
506         }
507
508         if (i < container->msc_nfinalizers || my_slot < 0) {
509                 lnet_net_unlock(cpt);
510                 return;
511         }
512
513         container->msc_finalizers[my_slot] = cfs_current();
514 #else
515         LASSERT(container->msc_nfinalizers == 1);
516         if (container->msc_finalizers[0] != NULL) {
517                 lnet_net_unlock(cpt);
518                 return;
519         }
520
521         my_slot = i = 0;
522         container->msc_finalizers[0] = (struct lnet_msg_container *)1;
523 #endif
524
525         while (!cfs_list_empty(&container->msc_finalizing)) {
526                 msg = cfs_list_entry(container->msc_finalizing.next,
527                                      lnet_msg_t, msg_list);
528
529                 cfs_list_del(&msg->msg_list);
530
531                 /* NB drops and regains the lnet lock if it actually does
532                  * anything, so my finalizing friends can chomp along too */
533                 rc = lnet_complete_msg_locked(msg, cpt);
534                 if (rc != 0)
535                         break;
536         }
537
538         container->msc_finalizers[my_slot] = NULL;
539         lnet_net_unlock(cpt);
540
541         if (rc != 0)
542                 goto again;
543 }
544 EXPORT_SYMBOL(lnet_finalize);
545
546 void
547 lnet_msg_container_cleanup(struct lnet_msg_container *container)
548 {
549         int     count = 0;
550
551         if (container->msc_init == 0)
552                 return;
553
554         while (!cfs_list_empty(&container->msc_active)) {
555                 lnet_msg_t *msg = cfs_list_entry(container->msc_active.next,
556                                                  lnet_msg_t, msg_activelist);
557
558                 LASSERT(msg->msg_onactivelist);
559                 msg->msg_onactivelist = 0;
560                 cfs_list_del(&msg->msg_activelist);
561                 lnet_msg_free(msg);
562                 count++;
563         }
564
565         if (count > 0)
566                 CERROR("%d active msg on exit\n", count);
567
568         if (container->msc_finalizers != NULL) {
569                 LIBCFS_FREE(container->msc_finalizers,
570                             container->msc_nfinalizers *
571                             sizeof(*container->msc_finalizers));
572                 container->msc_finalizers = NULL;
573         }
574 #ifdef LNET_USE_LIB_FREELIST
575         lnet_freelist_fini(&container->msc_freelist);
576 #endif
577         container->msc_init = 0;
578 }
579
580 int
581 lnet_msg_container_setup(struct lnet_msg_container *container, int cpt)
582 {
583         int     rc;
584
585         container->msc_init = 1;
586
587         CFS_INIT_LIST_HEAD(&container->msc_active);
588         CFS_INIT_LIST_HEAD(&container->msc_finalizing);
589
590 #ifdef LNET_USE_LIB_FREELIST
591         memset(&container->msc_freelist, 0, sizeof(lnet_freelist_t));
592
593         rc = lnet_freelist_init(&container->msc_freelist,
594                                 LNET_FL_MAX_MSGS, sizeof(lnet_msg_t));
595         if (rc != 0) {
596                 CERROR("Failed to init freelist for message container\n");
597                 lnet_msg_container_cleanup(container);
598                 return rc;
599         }
600 #else
601         rc = 0;
602 #endif
603         /* number of CPUs */
604         container->msc_nfinalizers = cfs_cpt_weight(lnet_cpt_table(), cpt);
605
606         LIBCFS_CPT_ALLOC(container->msc_finalizers, lnet_cpt_table(), cpt,
607                          container->msc_nfinalizers *
608                          sizeof(*container->msc_finalizers));
609
610         if (container->msc_finalizers == NULL) {
611                 CERROR("Failed to allocate message finalizers\n");
612                 lnet_msg_container_cleanup(container);
613                 return -ENOMEM;
614         }
615
616         return rc;
617 }
618
619 void
620 lnet_msg_containers_destroy(void)
621 {
622         struct lnet_msg_container *container;
623         int     i;
624
625         if (the_lnet.ln_msg_containers == NULL)
626                 return;
627
628         cfs_percpt_for_each(container, i, the_lnet.ln_msg_containers)
629                 lnet_msg_container_cleanup(container);
630
631         cfs_percpt_free(the_lnet.ln_msg_containers);
632         the_lnet.ln_msg_containers = NULL;
633 }
634
635 int
636 lnet_msg_containers_create(void)
637 {
638         struct lnet_msg_container *container;
639         int     rc;
640         int     i;
641
642         the_lnet.ln_msg_containers = cfs_percpt_alloc(lnet_cpt_table(),
643                                                       sizeof(*container));
644
645         if (the_lnet.ln_msg_containers == NULL) {
646                 CERROR("Failed to allocate cpu-partition data for network\n");
647                 return -ENOMEM;
648         }
649
650         cfs_percpt_for_each(container, i, the_lnet.ln_msg_containers) {
651                 rc = lnet_msg_container_setup(container, i);
652                 if (rc != 0) {
653                         lnet_msg_containers_destroy();
654                         return rc;
655                 }
656         }
657
658         return 0;
659 }