Whamcloud - gitweb
LU-14945 lnet: don't use hops to determine the route state
[fs/lustre-release.git] / lnet / lnet / lib-ptl.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, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 021110-1307, USA
20  *
21  * GPL HEADER END
22  */
23 /*
24  * Copyright (c) 2012, 2017, Intel Corporation.
25  */
26 /*
27  * This file is part of Lustre, http://www.lustre.org/
28  *
29  * lnet/lnet/lib-ptl.c
30  *
31  * portal & match routines
32  *
33  * Author: liang@whamcloud.com
34  */
35
36 #define DEBUG_SUBSYSTEM S_LNET
37
38 #include <lnet/lib-lnet.h>
39
40 /* NB: add /proc interfaces in upcoming patches */
41 int portal_rotor = LNET_PTL_ROTOR_HASH_RT;
42 module_param(portal_rotor, int, 0644);
43 MODULE_PARM_DESC(portal_rotor, "redirect PUTs to different cpu-partitions");
44
45 static int
46 lnet_ptl_match_type(unsigned int index, struct lnet_process_id match_id,
47                     __u64 mbits, __u64 ignore_bits)
48 {
49         struct lnet_portal      *ptl = the_lnet.ln_portals[index];
50         int                     unique;
51
52         unique = ignore_bits == 0 &&
53                  match_id.nid != LNET_NID_ANY &&
54                  match_id.pid != LNET_PID_ANY;
55
56         LASSERT(!lnet_ptl_is_unique(ptl) || !lnet_ptl_is_wildcard(ptl));
57
58         /* prefer to check w/o any lock */
59         if (likely(lnet_ptl_is_unique(ptl) || lnet_ptl_is_wildcard(ptl)))
60                 goto match;
61
62         /* unset, new portal */
63         lnet_ptl_lock(ptl);
64         /* check again with lock */
65         if (unlikely(lnet_ptl_is_unique(ptl) || lnet_ptl_is_wildcard(ptl))) {
66                 lnet_ptl_unlock(ptl);
67                 goto match;
68         }
69
70         /* still not set */
71         if (unique)
72                 lnet_ptl_setopt(ptl, LNET_PTL_MATCH_UNIQUE);
73         else
74                 lnet_ptl_setopt(ptl, LNET_PTL_MATCH_WILDCARD);
75
76         lnet_ptl_unlock(ptl);
77
78         return 1;
79
80  match:
81         if ((lnet_ptl_is_unique(ptl) && !unique) ||
82             (lnet_ptl_is_wildcard(ptl) && unique))
83                 return 0;
84         return 1;
85 }
86
87 static void
88 lnet_ptl_enable_mt(struct lnet_portal *ptl, int cpt)
89 {
90         struct lnet_match_table *mtable = ptl->ptl_mtables[cpt];
91         int                     i;
92
93         /* with hold of both lnet_res_lock(cpt) and lnet_ptl_lock */
94         LASSERT(lnet_ptl_is_wildcard(ptl));
95
96         mtable->mt_enabled = 1;
97
98         ptl->ptl_mt_maps[ptl->ptl_mt_nmaps] = cpt;
99         for (i = ptl->ptl_mt_nmaps - 1; i >= 0; i--) {
100                 LASSERT(ptl->ptl_mt_maps[i] != cpt);
101                 if (ptl->ptl_mt_maps[i] < cpt)
102                         break;
103
104                 /* swap to order */
105                 ptl->ptl_mt_maps[i + 1] = ptl->ptl_mt_maps[i];
106                 ptl->ptl_mt_maps[i] = cpt;
107         }
108
109         ptl->ptl_mt_nmaps++;
110 }
111
112 static void
113 lnet_ptl_disable_mt(struct lnet_portal *ptl, int cpt)
114 {
115         struct lnet_match_table *mtable = ptl->ptl_mtables[cpt];
116         int                     i;
117
118         /* with hold of both lnet_res_lock(cpt) and lnet_ptl_lock */
119         LASSERT(lnet_ptl_is_wildcard(ptl));
120
121         if (LNET_CPT_NUMBER == 1)
122                 return; /* never disable the only match-table */
123
124         mtable->mt_enabled = 0;
125
126         LASSERT(ptl->ptl_mt_nmaps > 0 &&
127                 ptl->ptl_mt_nmaps <= LNET_CPT_NUMBER);
128
129         /* remove it from mt_maps */
130         ptl->ptl_mt_nmaps--;
131         for (i = 0; i < ptl->ptl_mt_nmaps; i++) {
132                 if (ptl->ptl_mt_maps[i] >= cpt) /* overwrite it */
133                         ptl->ptl_mt_maps[i] = ptl->ptl_mt_maps[i + 1];
134         }
135 }
136
137 static int
138 lnet_try_match_md(struct lnet_libmd *md,
139                   struct lnet_match_info *info, struct lnet_msg *msg)
140 {
141         /* ALWAYS called holding the lnet_res_lock, and can't lnet_res_unlock;
142          * lnet_match_blocked_msg() relies on this to avoid races */
143         unsigned int    offset;
144         unsigned int    mlength;
145         struct lnet_me  *me = md->md_me;
146
147         /* MD exhausted */
148         if (lnet_md_exhausted(md))
149                 return LNET_MATCHMD_NONE | LNET_MATCHMD_EXHAUSTED;
150
151         /* mismatched MD op */
152         if ((md->md_options & info->mi_opc) == 0)
153                 return LNET_MATCHMD_NONE;
154
155         /* mismatched ME nid/pid? */
156         if (me->me_match_id.nid != LNET_NID_ANY &&
157             me->me_match_id.nid != info->mi_id.nid)
158                 return LNET_MATCHMD_NONE;
159
160         if (me->me_match_id.pid != LNET_PID_ANY &&
161             me->me_match_id.pid != info->mi_id.pid)
162                 return LNET_MATCHMD_NONE;
163
164         /* mismatched ME matchbits? */
165         if (((me->me_match_bits ^ info->mi_mbits) & ~me->me_ignore_bits) != 0)
166                 return LNET_MATCHMD_NONE;
167
168         /* Hurrah! This _is_ a match; check it out... */
169
170         if ((md->md_options & LNET_MD_MANAGE_REMOTE) == 0)
171                 offset = md->md_offset;
172         else
173                 offset = info->mi_roffset;
174
175         if ((md->md_options & LNET_MD_MAX_SIZE) != 0) {
176                 mlength = md->md_max_size;
177                 LASSERT(md->md_offset + mlength <= md->md_length);
178         } else {
179                 mlength = md->md_length - offset;
180         }
181
182         if (info->mi_rlength <= mlength) {      /* fits in allowed space */
183                 mlength = info->mi_rlength;
184         } else if ((md->md_options & LNET_MD_TRUNCATE) == 0) {
185                 /* this packet _really_ is too big */
186                 CERROR("Matching packet from %s, match %llu"
187                        " length %d too big: %d left, %d allowed\n",
188                        libcfs_id2str(info->mi_id), info->mi_mbits,
189                        info->mi_rlength, md->md_length - offset, mlength);
190
191                 return LNET_MATCHMD_DROP;
192         }
193
194         /* Commit to this ME/MD */
195         CDEBUG(D_NET, "Incoming %s index %x from %s of "
196                "length %d/%d into md %#llx [%d] + %d\n",
197                (info->mi_opc == LNET_MD_OP_PUT) ? "put" : "get",
198                info->mi_portal, libcfs_id2str(info->mi_id), mlength,
199                info->mi_rlength, md->md_lh.lh_cookie, md->md_niov, offset);
200
201         lnet_msg_attach_md(msg, md, offset, mlength);
202         md->md_offset = offset + mlength;
203
204         if (!lnet_md_exhausted(md))
205                 return LNET_MATCHMD_OK;
206
207         /* Auto-unlink NOW, so the ME gets unlinked if required.
208          * We bumped md->md_refcount above so the MD just gets flagged
209          * for unlink when it is finalized. */
210         if ((md->md_flags & LNET_MD_FLAG_AUTO_UNLINK) != 0)
211                 lnet_md_unlink(md);
212
213         return LNET_MATCHMD_OK | LNET_MATCHMD_EXHAUSTED;
214 }
215
216 static struct lnet_match_table *
217 lnet_match2mt(struct lnet_portal *ptl, struct lnet_process_id id, __u64 mbits)
218 {
219         if (LNET_CPT_NUMBER == 1)
220                 return ptl->ptl_mtables[0]; /* the only one */
221
222         /* if it's a unique portal, return match-table hashed by NID */
223         return lnet_ptl_is_unique(ptl) ?
224                ptl->ptl_mtables[lnet_cpt_of_nid(id.nid, NULL)] : NULL;
225 }
226
227 struct lnet_match_table *
228 lnet_mt_of_attach(unsigned int index, struct lnet_process_id id,
229                   __u64 mbits, __u64 ignore_bits, enum lnet_ins_pos pos)
230 {
231         struct lnet_portal      *ptl;
232         struct lnet_match_table *mtable;
233
234         /* NB: called w/o lock */
235         LASSERT(index < the_lnet.ln_nportals);
236
237         if (!lnet_ptl_match_type(index, id, mbits, ignore_bits))
238                 return NULL;
239
240         ptl = the_lnet.ln_portals[index];
241
242         mtable = lnet_match2mt(ptl, id, mbits);
243         if (mtable != NULL) /* unique portal or only one match-table */
244                 return mtable;
245
246         /* it's a wildcard portal */
247         switch (pos) {
248         default:
249                 return NULL;
250         case LNET_INS_BEFORE:
251         case LNET_INS_AFTER:
252                 /* posted by no affinity thread, always hash to specific
253                  * match-table to avoid buffer stealing which is heavy */
254                 return ptl->ptl_mtables[ptl->ptl_index % LNET_CPT_NUMBER];
255         case LNET_INS_LOCAL:
256                 /* posted by cpu-affinity thread */
257                 return ptl->ptl_mtables[lnet_cpt_current()];
258         }
259 }
260
261 static struct lnet_match_table *
262 lnet_mt_of_match(struct lnet_match_info *info, struct lnet_msg *msg)
263 {
264         struct lnet_match_table *mtable;
265         struct lnet_portal      *ptl;
266         unsigned int            nmaps;
267         unsigned int            rotor;
268         unsigned int            cpt;
269         bool                    routed;
270
271         /* NB: called w/o lock */
272         LASSERT(info->mi_portal < the_lnet.ln_nportals);
273         ptl = the_lnet.ln_portals[info->mi_portal];
274
275         LASSERT(lnet_ptl_is_wildcard(ptl) || lnet_ptl_is_unique(ptl));
276
277         mtable = lnet_match2mt(ptl, info->mi_id, info->mi_mbits);
278         if (mtable != NULL)
279                 return mtable;
280
281         /* it's a wildcard portal */
282         routed = LNET_NIDNET(msg->msg_hdr.src_nid) !=
283                  LNET_NIDNET(msg->msg_hdr.dest_nid);
284
285         if (portal_rotor == LNET_PTL_ROTOR_OFF ||
286             (portal_rotor != LNET_PTL_ROTOR_ON && !routed)) {
287                 cpt = lnet_cpt_current();
288                 if (ptl->ptl_mtables[cpt]->mt_enabled)
289                         return ptl->ptl_mtables[cpt];
290         }
291
292         rotor = ptl->ptl_rotor++; /* get round-robin factor */
293         if (portal_rotor == LNET_PTL_ROTOR_HASH_RT && routed)
294                 cpt = info->mi_cpt;
295         else
296                 cpt = rotor % LNET_CPT_NUMBER;
297
298         if (!ptl->ptl_mtables[cpt]->mt_enabled) {
299                 /* is there any active entry for this portal? */
300                 nmaps = ptl->ptl_mt_nmaps;
301                 /* map to an active mtable to avoid heavy "stealing" */
302                 if (nmaps != 0) {
303                         /* NB: there is possibility that ptl_mt_maps is being
304                          * changed because we are not under protection of
305                          * lnet_ptl_lock, but it shouldn't hurt anything */
306                         cpt = ptl->ptl_mt_maps[rotor % nmaps];
307                 }
308         }
309
310         return ptl->ptl_mtables[cpt];
311 }
312
313 static int
314 lnet_mt_test_exhausted(struct lnet_match_table *mtable, int pos)
315 {
316         __u64   *bmap;
317         int     i;
318
319         if (!lnet_ptl_is_wildcard(the_lnet.ln_portals[mtable->mt_portal]))
320                 return 0;
321
322         if (pos < 0) { /* check all bits */
323                 for (i = 0; i < LNET_MT_EXHAUSTED_BMAP; i++) {
324                         if (mtable->mt_exhausted[i] != (__u64)(-1))
325                                 return 0;
326                 }
327                 return 1;
328         }
329
330         LASSERT(pos <= LNET_MT_HASH_IGNORE);
331         /* mtable::mt_mhash[pos] is marked as exhausted or not */
332         bmap = &mtable->mt_exhausted[pos >> LNET_MT_BITS_U64];
333         pos &= (1 << LNET_MT_BITS_U64) - 1;
334
335         return ((*bmap) & (1ULL << pos)) != 0;
336 }
337
338 static void
339 lnet_mt_set_exhausted(struct lnet_match_table *mtable, int pos, int exhausted)
340 {
341         __u64   *bmap;
342
343         LASSERT(lnet_ptl_is_wildcard(the_lnet.ln_portals[mtable->mt_portal]));
344         LASSERT(pos <= LNET_MT_HASH_IGNORE);
345
346         /* set mtable::mt_mhash[pos] as exhausted/non-exhausted */
347         bmap = &mtable->mt_exhausted[pos >> LNET_MT_BITS_U64];
348         pos &= (1 << LNET_MT_BITS_U64) - 1;
349
350         if (!exhausted)
351                 *bmap &= ~(1ULL << pos);
352         else
353                 *bmap |= 1ULL << pos;
354 }
355
356 struct list_head *
357 lnet_mt_match_head(struct lnet_match_table *mtable,
358                    struct lnet_process_id id, __u64 mbits)
359 {
360         struct lnet_portal *ptl = the_lnet.ln_portals[mtable->mt_portal];
361
362         if (lnet_ptl_is_wildcard(ptl)) {
363                 return &mtable->mt_mhash[mbits & LNET_MT_HASH_MASK];
364         } else {
365                 unsigned long hash = mbits + id.nid + id.pid;
366
367                 LASSERT(lnet_ptl_is_unique(ptl));
368                 hash = hash_long(hash, LNET_MT_HASH_BITS);
369                 return &mtable->mt_mhash[hash & LNET_MT_HASH_MASK];
370         }
371 }
372
373 int
374 lnet_mt_match_md(struct lnet_match_table *mtable,
375                  struct lnet_match_info *info, struct lnet_msg *msg)
376 {
377         struct list_head        *head;
378         struct lnet_me          *me;
379         struct lnet_me          *tmp;
380         int                     exhausted = 0;
381         int                     rc;
382
383         /* any ME with ignore bits? */
384         if (!list_empty(&mtable->mt_mhash[LNET_MT_HASH_IGNORE]))
385                 head = &mtable->mt_mhash[LNET_MT_HASH_IGNORE];
386         else
387                 head = lnet_mt_match_head(mtable, info->mi_id, info->mi_mbits);
388  again:
389         /* NB: only wildcard portal needs to return LNET_MATCHMD_EXHAUSTED */
390         if (lnet_ptl_is_wildcard(the_lnet.ln_portals[mtable->mt_portal]))
391                 exhausted = LNET_MATCHMD_EXHAUSTED;
392
393         list_for_each_entry_safe(me, tmp, head, me_list) {
394                 /* ME attached but MD not attached yet */
395                 if (me->me_md == NULL)
396                         continue;
397
398                 LASSERT(me == me->me_md->md_me);
399
400                 rc = lnet_try_match_md(me->me_md, info, msg);
401                 if ((rc & LNET_MATCHMD_EXHAUSTED) == 0)
402                         exhausted = 0; /* mlist is not empty */
403
404                 if ((rc & LNET_MATCHMD_FINISH) != 0) {
405                         /* don't return EXHAUSTED bit because we don't know
406                          * whether the mlist is empty or not */
407                         return rc & ~LNET_MATCHMD_EXHAUSTED;
408                 }
409         }
410
411         if (exhausted == LNET_MATCHMD_EXHAUSTED) { /* @head is exhausted */
412                 lnet_mt_set_exhausted(mtable, head - mtable->mt_mhash, 1);
413                 if (!lnet_mt_test_exhausted(mtable, -1))
414                         exhausted = 0;
415         }
416
417         if (exhausted == 0 && head == &mtable->mt_mhash[LNET_MT_HASH_IGNORE]) {
418                 head = lnet_mt_match_head(mtable, info->mi_id, info->mi_mbits);
419                 goto again; /* re-check MEs w/o ignore-bits */
420         }
421
422         if (info->mi_opc == LNET_MD_OP_GET ||
423             !lnet_ptl_is_lazy(the_lnet.ln_portals[info->mi_portal]))
424                 return LNET_MATCHMD_DROP | exhausted;
425
426         return LNET_MATCHMD_NONE | exhausted;
427 }
428
429 static int
430 lnet_ptl_match_early(struct lnet_portal *ptl, struct lnet_msg *msg)
431 {
432         int     rc;
433
434         /* message arrived before any buffer posting on this portal,
435          * simply delay or drop this message */
436         if (likely(lnet_ptl_is_wildcard(ptl) || lnet_ptl_is_unique(ptl)))
437                 return 0;
438
439         lnet_ptl_lock(ptl);
440         /* check it again with hold of lock */
441         if (lnet_ptl_is_wildcard(ptl) || lnet_ptl_is_unique(ptl)) {
442                 lnet_ptl_unlock(ptl);
443                 return 0;
444         }
445
446         if (lnet_ptl_is_lazy(ptl)) {
447                 if (msg->msg_rx_ready_delay) {
448                         msg->msg_rx_delayed = 1;
449                         list_add_tail(&msg->msg_list,
450                                       &ptl->ptl_msg_delayed);
451                 }
452                 rc = LNET_MATCHMD_NONE;
453         } else {
454                 rc = LNET_MATCHMD_DROP;
455         }
456
457         lnet_ptl_unlock(ptl);
458         return rc;
459 }
460
461 static int
462 lnet_ptl_match_delay(struct lnet_portal *ptl,
463                      struct lnet_match_info *info, struct lnet_msg *msg)
464 {
465         int     first = ptl->ptl_mt_maps[0]; /* read w/o lock */
466         int     rc = 0;
467         int     i;
468
469         /*
470          * Steal buffer from other CPTs, and delay msg if nothing to
471          * steal.  This function is more expensive than a regular
472          * match, but we don't expect it can happen a lot. The return
473          * code contains one of LNET_MATCHMD_OK, LNET_MATCHMD_DROP, or
474          * LNET_MATCHMD_NONE.
475          */
476         LASSERT(lnet_ptl_is_wildcard(ptl));
477
478         for (i = 0; i < LNET_CPT_NUMBER; i++) {
479                 struct lnet_match_table *mtable;
480                 int                     cpt;
481
482                 cpt = (first + i) % LNET_CPT_NUMBER;
483                 mtable = ptl->ptl_mtables[cpt];
484                 if (i != 0 && i != LNET_CPT_NUMBER - 1 && !mtable->mt_enabled)
485                         continue;
486
487                 lnet_res_lock(cpt);
488                 lnet_ptl_lock(ptl);
489
490                 if (i == 0) {
491                         /* The first try, add to stealing list. */
492                         list_add_tail(&msg->msg_list,
493                                       &ptl->ptl_msg_stealing);
494                 }
495
496                 if (!list_empty(&msg->msg_list)) {
497                         /* On stealing list. */
498                         rc = lnet_mt_match_md(mtable, info, msg);
499
500                         if ((rc & LNET_MATCHMD_EXHAUSTED) != 0 &&
501                             mtable->mt_enabled)
502                                 lnet_ptl_disable_mt(ptl, cpt);
503
504                         if ((rc & LNET_MATCHMD_FINISH) != 0) {
505                                 /* Match found, remove from stealing list. */
506                                 list_del_init(&msg->msg_list);
507                         } else if (i == LNET_CPT_NUMBER - 1 || /* (1) */
508                                    ptl->ptl_mt_nmaps == 0 ||   /* (2) */
509                                    (ptl->ptl_mt_nmaps == 1 &&  /* (3) */
510                                     ptl->ptl_mt_maps[0] == cpt)) {
511                                 /*
512                                  * No match found, and this is either
513                                  * (1) the last cpt to check, or
514                                  * (2) there is no active cpt, or
515                                  * (3) this is the only active cpt.
516                                  * There is nothing to steal: delay or
517                                  * drop the message.
518                                  */
519                                 list_del_init(&msg->msg_list);
520
521                                 if (lnet_ptl_is_lazy(ptl)) {
522                                         msg->msg_rx_delayed = 1;
523                                         list_add_tail(&msg->msg_list,
524                                                       &ptl->ptl_msg_delayed);
525                                         rc = LNET_MATCHMD_NONE;
526                                 } else {
527                                         rc = LNET_MATCHMD_DROP;
528                                 }
529                         } else {
530                                 /* Do another iteration. */
531                                 rc = 0;
532                         }
533                 } else {
534                         /*
535                          * No longer on stealing list: another thread
536                          * matched the message in lnet_ptl_attach_md().
537                          * We are now expected to handle the message.
538                          */
539                         rc = msg->msg_md == NULL ?
540                                 LNET_MATCHMD_DROP : LNET_MATCHMD_OK;
541                 }
542
543                 lnet_ptl_unlock(ptl);
544                 lnet_res_unlock(cpt);
545
546                 /*
547                  * Note that test (1) above ensures that we always
548                  * exit the loop through this break statement.
549                  *
550                  * LNET_MATCHMD_NONE means msg was added to the
551                  * delayed queue, and we may no longer reference it
552                  * after lnet_ptl_unlock() and lnet_res_unlock().
553                  */
554                 if (rc & (LNET_MATCHMD_FINISH | LNET_MATCHMD_NONE))
555                         break;
556         }
557
558         return rc;
559 }
560
561 int
562 lnet_ptl_match_md(struct lnet_match_info *info, struct lnet_msg *msg)
563 {
564         struct lnet_match_table *mtable;
565         struct lnet_portal      *ptl;
566         int                     rc;
567
568         CDEBUG(D_NET, "Request from %s of length %d into portal %d "
569                "MB=%#llx\n", libcfs_id2str(info->mi_id),
570                info->mi_rlength, info->mi_portal, info->mi_mbits);
571
572         if (info->mi_portal >= the_lnet.ln_nportals) {
573                 CERROR("Invalid portal %d not in [0-%d]\n",
574                        info->mi_portal, the_lnet.ln_nportals);
575                 return LNET_MATCHMD_DROP;
576         }
577
578         ptl = the_lnet.ln_portals[info->mi_portal];
579         rc = lnet_ptl_match_early(ptl, msg);
580         if (rc != 0) /* matched or delayed early message */
581                 return rc;
582
583         mtable = lnet_mt_of_match(info, msg);
584         lnet_res_lock(mtable->mt_cpt);
585
586         if (the_lnet.ln_state != LNET_STATE_RUNNING) {
587                 rc = LNET_MATCHMD_DROP;
588                 goto out1;
589         }
590
591         rc = lnet_mt_match_md(mtable, info, msg);
592         if ((rc & LNET_MATCHMD_EXHAUSTED) != 0 && mtable->mt_enabled) {
593                 lnet_ptl_lock(ptl);
594                 lnet_ptl_disable_mt(ptl, mtable->mt_cpt);
595                 lnet_ptl_unlock(ptl);
596         }
597
598         if ((rc & LNET_MATCHMD_FINISH) != 0)    /* matched or dropping */
599                 goto out1;
600
601         if (!msg->msg_rx_ready_delay)
602                 goto out1;
603
604         LASSERT(lnet_ptl_is_lazy(ptl));
605         LASSERT(!msg->msg_rx_delayed);
606
607         /* NB: we don't expect "delay" can happen a lot */
608         if (lnet_ptl_is_unique(ptl) || LNET_CPT_NUMBER == 1) {
609                 lnet_ptl_lock(ptl);
610
611                 msg->msg_rx_delayed = 1;
612                 list_add_tail(&msg->msg_list, &ptl->ptl_msg_delayed);
613
614                 lnet_ptl_unlock(ptl);
615                 lnet_res_unlock(mtable->mt_cpt);
616                 rc = LNET_MATCHMD_NONE;
617         } else  {
618                 lnet_res_unlock(mtable->mt_cpt);
619                 rc = lnet_ptl_match_delay(ptl, info, msg);
620         }
621
622         /* LNET_MATCHMD_NONE means msg was added to the delay queue */
623         if (rc & LNET_MATCHMD_NONE) {
624                 CDEBUG(D_NET,
625                        "Delaying %s from %s ptl %d MB %#llx off %d len %d\n",
626                        info->mi_opc == LNET_MD_OP_PUT ? "PUT" : "GET",
627                        libcfs_id2str(info->mi_id), info->mi_portal,
628                        info->mi_mbits, info->mi_roffset, info->mi_rlength);
629         }
630         goto out0;
631  out1:
632         lnet_res_unlock(mtable->mt_cpt);
633  out0:
634         /* EXHAUSTED bit is only meaningful for internal functions */
635         return rc & ~LNET_MATCHMD_EXHAUSTED;
636 }
637
638 void
639 lnet_ptl_detach_md(struct lnet_me *me, struct lnet_libmd *md)
640 {
641         LASSERT(me->me_md == md && md->md_me == me);
642
643         me->me_md = NULL;
644         md->md_me = NULL;
645 }
646
647 /* called with lnet_res_lock held */
648 void
649 lnet_ptl_attach_md(struct lnet_me *me, struct lnet_libmd *md,
650                    struct list_head *matches, struct list_head *drops)
651 {
652         struct lnet_portal *ptl = the_lnet.ln_portals[me->me_portal];
653         struct lnet_match_table *mtable;
654         struct list_head *head;
655         struct lnet_msg *tmp;
656         struct lnet_msg *msg;
657         int exhausted = 0;
658         int cpt;
659
660         LASSERT(md->md_refcount == 0); /* a brand new MD */
661
662         me->me_md = md;
663         md->md_me = me;
664
665         cpt = lnet_cpt_of_cookie(md->md_lh.lh_cookie);
666         mtable = ptl->ptl_mtables[cpt];
667
668         if (list_empty(&ptl->ptl_msg_stealing) &&
669             list_empty(&ptl->ptl_msg_delayed) &&
670             !lnet_mt_test_exhausted(mtable, me->me_pos))
671                 return;
672
673         lnet_ptl_lock(ptl);
674         head = &ptl->ptl_msg_stealing;
675  again:
676         list_for_each_entry_safe(msg, tmp, head, msg_list) {
677                 struct lnet_match_info  info;
678                 struct lnet_hdr         *hdr;
679                 int                     rc;
680
681                 LASSERT(msg->msg_rx_delayed || head == &ptl->ptl_msg_stealing);
682
683                 hdr   = &msg->msg_hdr;
684                 /* Multi-Rail: Primary peer NID */
685                 info.mi_id.nid  = msg->msg_initiator;
686                 info.mi_id.pid  = hdr->src_pid;
687                 info.mi_opc     = LNET_MD_OP_PUT;
688                 info.mi_portal  = hdr->msg.put.ptl_index;
689                 info.mi_rlength = hdr->payload_length;
690                 info.mi_roffset = hdr->msg.put.offset;
691                 info.mi_mbits   = hdr->msg.put.match_bits;
692
693                 rc = lnet_try_match_md(md, &info, msg);
694
695                 exhausted = (rc & LNET_MATCHMD_EXHAUSTED) != 0;
696                 if ((rc & LNET_MATCHMD_NONE) != 0) {
697                         if (exhausted)
698                                 break;
699                         continue;
700                 }
701
702                 /* Hurrah! This _is_ a match */
703                 LASSERT((rc & LNET_MATCHMD_FINISH) != 0);
704                 list_del_init(&msg->msg_list);
705
706                 if (head == &ptl->ptl_msg_stealing) {
707                         if (exhausted)
708                                 break;
709                         /* stealing thread will handle the message */
710                         continue;
711                 }
712
713                 if ((rc & LNET_MATCHMD_OK) != 0) {
714                         list_add_tail(&msg->msg_list, matches);
715
716                         CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
717                                "match %llu offset %d length %d.\n",
718                                libcfs_id2str(info.mi_id),
719                                info.mi_portal, info.mi_mbits,
720                                info.mi_roffset, info.mi_rlength);
721                 } else {
722                         list_add_tail(&msg->msg_list, drops);
723                 }
724
725                 if (exhausted)
726                         break;
727         }
728
729         if (!exhausted && head == &ptl->ptl_msg_stealing) {
730                 head = &ptl->ptl_msg_delayed;
731                 goto again;
732         }
733
734         if (lnet_ptl_is_wildcard(ptl) && !exhausted) {
735                 lnet_mt_set_exhausted(mtable, me->me_pos, 0);
736                 if (!mtable->mt_enabled)
737                         lnet_ptl_enable_mt(ptl, cpt);
738         }
739
740         lnet_ptl_unlock(ptl);
741 }
742
743 static void
744 lnet_ptl_cleanup(struct lnet_portal *ptl)
745 {
746         struct lnet_match_table *mtable;
747         int                     i;
748
749         if (ptl->ptl_mtables == NULL) /* uninitialized portal */
750                 return;
751
752         LASSERT(list_empty(&ptl->ptl_msg_delayed));
753         LASSERT(list_empty(&ptl->ptl_msg_stealing));
754         cfs_percpt_for_each(mtable, i, ptl->ptl_mtables) {
755                 struct list_head *mhash;
756                 struct lnet_me   *me;
757                 int               j;
758
759                 if (mtable->mt_mhash == NULL) /* uninitialized match-table */
760                         continue;
761
762                 mhash = mtable->mt_mhash;
763                 /* cleanup ME */
764                 for (j = 0; j < LNET_MT_HASH_SIZE + 1; j++) {
765                         while (!list_empty(&mhash[j])) {
766                                 me = list_entry(mhash[j].next,
767                                                 struct lnet_me, me_list);
768                                 CERROR("Active ME %p on exit\n", me);
769                                 list_del(&me->me_list);
770                                 CDEBUG(D_MALLOC,
771                                        "slab-freed 'me' at %p in cleanup.\n",
772                                        me);
773                                 kmem_cache_free(lnet_mes_cachep, me);
774                         }
775                 }
776                 /* the extra entry is for MEs with ignore bits */
777                 CFS_FREE_PTR_ARRAY(mhash, LNET_MT_HASH_SIZE + 1);
778         }
779
780         cfs_percpt_free(ptl->ptl_mtables);
781         ptl->ptl_mtables = NULL;
782 }
783
784 static int
785 lnet_ptl_setup(struct lnet_portal *ptl, int index)
786 {
787         struct lnet_match_table *mtable;
788         struct list_head        *mhash;
789         int                     i;
790         int                     j;
791
792         ptl->ptl_mtables = cfs_percpt_alloc(lnet_cpt_table(),
793                                             sizeof(struct lnet_match_table));
794         if (ptl->ptl_mtables == NULL) {
795                 CERROR("Failed to create match table for portal %d\n", index);
796                 return -ENOMEM;
797         }
798
799         ptl->ptl_index = index;
800         INIT_LIST_HEAD(&ptl->ptl_msg_delayed);
801         INIT_LIST_HEAD(&ptl->ptl_msg_stealing);
802         spin_lock_init(&ptl->ptl_lock);
803         cfs_percpt_for_each(mtable, i, ptl->ptl_mtables) {
804                 /* the extra entry is for MEs with ignore bits */
805                 LIBCFS_CPT_ALLOC(mhash, lnet_cpt_table(), i,
806                                  sizeof(*mhash) * (LNET_MT_HASH_SIZE + 1));
807                 if (mhash == NULL) {
808                         CERROR("Failed to create match hash for portal %d\n",
809                                index);
810                         goto failed;
811                 }
812
813                 memset(&mtable->mt_exhausted[0], -1,
814                        sizeof(mtable->mt_exhausted[0]) *
815                        LNET_MT_EXHAUSTED_BMAP);
816                 mtable->mt_mhash = mhash;
817                 for (j = 0; j < LNET_MT_HASH_SIZE + 1; j++)
818                         INIT_LIST_HEAD(&mhash[j]);
819
820                 mtable->mt_portal = index;
821                 mtable->mt_cpt = i;
822         }
823
824         return 0;
825  failed:
826         lnet_ptl_cleanup(ptl);
827         return -ENOMEM;
828 }
829
830 #define PORTAL_SIZE (offsetof(struct lnet_portal, ptl_mt_maps[LNET_CPT_NUMBER]))
831 void
832 lnet_portals_destroy(void)
833 {
834         int     i;
835
836         if (the_lnet.ln_portals == NULL)
837                 return;
838
839         for (i = 0; i < the_lnet.ln_nportals; i++)
840                 if (the_lnet.ln_portals[i]) {
841                         lnet_ptl_cleanup(the_lnet.ln_portals[i]);
842                         LIBCFS_FREE(the_lnet.ln_portals[i], PORTAL_SIZE);
843                 }
844
845         CFS_FREE_PTR_ARRAY(the_lnet.ln_portals, the_lnet.ln_nportals);
846         the_lnet.ln_portals = NULL;
847 }
848
849 int
850 lnet_portals_create(void)
851 {
852         int     i;
853
854         the_lnet.ln_nportals = MAX_PORTALS;
855         CFS_ALLOC_PTR_ARRAY(the_lnet.ln_portals, the_lnet.ln_nportals);
856         if (the_lnet.ln_portals == NULL) {
857                 CERROR("Failed to allocate portals table\n");
858                 return -ENOMEM;
859         }
860
861         for (i = 0; i < the_lnet.ln_nportals; i++) {
862                 LIBCFS_ALLOC(the_lnet.ln_portals[i], PORTAL_SIZE);
863                 if (!the_lnet.ln_portals[i] ||
864                     lnet_ptl_setup(the_lnet.ln_portals[i], i)) {
865                         lnet_portals_destroy();
866                         return -ENOMEM;
867                 }
868         }
869
870         return 0;
871 }
872
873 /**
874  * Turn on the lazy portal attribute. Use with caution!
875  *
876  * This portal attribute only affects incoming PUT requests to the portal,
877  * and is off by default. By default, if there's no matching MD for an
878  * incoming PUT request, it is simply dropped. With the lazy attribute on,
879  * such requests are queued indefinitely until either a matching MD is
880  * posted to the portal or the lazy attribute is turned off.
881  *
882  * It would prevent dropped requests, however it should be regarded as the
883  * last line of defense - i.e. users must keep a close watch on active
884  * buffers on a lazy portal and once it becomes too low post more buffers as
885  * soon as possible. This is because delayed requests usually have detrimental
886  * effects on underlying network connections. A few delayed requests often
887  * suffice to bring an underlying connection to a complete halt, due to flow
888  * control mechanisms.
889  *
890  * There's also a DOS attack risk. If users don't post match-all MDs on a
891  * lazy portal, a malicious peer can easily stop a service by sending some
892  * PUT requests with match bits that won't match any MD. A routed server is
893  * especially vulnerable since the connections to its neighbor routers are
894  * shared among all clients.
895  *
896  * \param portal Index of the portal to enable the lazy attribute on.
897  *
898  * \retval 0       On success.
899  * \retval -EINVAL If \a portal is not a valid index.
900  */
901 int
902 LNetSetLazyPortal(int portal)
903 {
904         struct lnet_portal *ptl;
905
906         if (portal < 0 || portal >= the_lnet.ln_nportals)
907                 return -EINVAL;
908
909         CDEBUG(D_NET, "Setting portal %d lazy\n", portal);
910         ptl = the_lnet.ln_portals[portal];
911
912         lnet_res_lock(LNET_LOCK_EX);
913         lnet_ptl_lock(ptl);
914
915         lnet_ptl_setopt(ptl, LNET_PTL_LAZY);
916
917         lnet_ptl_unlock(ptl);
918         lnet_res_unlock(LNET_LOCK_EX);
919
920         return 0;
921 }
922 EXPORT_SYMBOL(LNetSetLazyPortal);
923
924 int
925 lnet_clear_lazy_portal(struct lnet_ni *ni, int portal, char *reason)
926 {
927         struct lnet_portal      *ptl;
928         LIST_HEAD(zombies);
929
930         if (portal < 0 || portal >= the_lnet.ln_nportals)
931                 return -EINVAL;
932
933         ptl = the_lnet.ln_portals[portal];
934
935         lnet_res_lock(LNET_LOCK_EX);
936         lnet_ptl_lock(ptl);
937
938         if (!lnet_ptl_is_lazy(ptl)) {
939                 lnet_ptl_unlock(ptl);
940                 lnet_res_unlock(LNET_LOCK_EX);
941                 return 0;
942         }
943
944         if (ni != NULL) {
945                 struct lnet_msg *msg, *tmp;
946
947                 /* grab all messages which are on the NI passed in */
948                 list_for_each_entry_safe(msg, tmp, &ptl->ptl_msg_delayed,
949                                          msg_list) {
950                         if (msg->msg_txni == ni || msg->msg_rxni == ni)
951                                 list_move(&msg->msg_list, &zombies);
952                 }
953         } else {
954                 if (the_lnet.ln_state != LNET_STATE_RUNNING)
955                         CWARN("Active lazy portal %d on exit\n", portal);
956                 else
957                         CDEBUG(D_NET, "clearing portal %d lazy\n", portal);
958
959                 /* grab all the blocked messages atomically */
960                 list_splice_init(&ptl->ptl_msg_delayed, &zombies);
961
962                 lnet_ptl_unsetopt(ptl, LNET_PTL_LAZY);
963         }
964
965         lnet_ptl_unlock(ptl);
966         lnet_res_unlock(LNET_LOCK_EX);
967
968         lnet_drop_delayed_msg_list(&zombies, reason);
969
970         return 0;
971 }
972
973 /**
974  * Turn off the lazy portal attribute. Delayed requests on the portal,
975  * if any, will be all dropped when this function returns.
976  *
977  * \param portal Index of the portal to disable the lazy attribute on.
978  *
979  * \retval 0       On success.
980  * \retval -EINVAL If \a portal is not a valid index.
981  */
982 int
983 LNetClearLazyPortal(int portal)
984 {
985         return lnet_clear_lazy_portal(NULL, portal,
986                                       "Clearing lazy portal attr");
987 }
988 EXPORT_SYMBOL(LNetClearLazyPortal);