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