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