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