Whamcloud - gitweb
LU-1347 lnet: makes EXPORT_SYMBOL follows function body
[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    = LNET_PTL_ROTOR_HASH_RT;
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 static struct lnet_match_table *
263 lnet_mt_of_match(struct lnet_match_info *info, struct lnet_msg *msg)
264 {
265         struct lnet_match_table *mtable;
266         struct lnet_portal      *ptl;
267         int                     nmaps;
268         int                     rotor;
269         int                     routed;
270         int                     cpt;
271
272         /* NB: called w/o lock */
273         LASSERT(info->mi_portal < the_lnet.ln_nportals);
274         ptl = the_lnet.ln_portals[info->mi_portal];
275
276         LASSERT(lnet_ptl_is_wildcard(ptl) || lnet_ptl_is_unique(ptl));
277
278         mtable = lnet_match2mt(ptl, info->mi_id, info->mi_mbits);
279         if (mtable != NULL)
280                 return mtable;
281
282         /* it's a wildcard portal */
283         routed = LNET_NIDNET(msg->msg_hdr.src_nid) !=
284                  LNET_NIDNET(msg->msg_hdr.dest_nid);
285
286         if (portal_rotor == LNET_PTL_ROTOR_OFF ||
287             (portal_rotor != LNET_PTL_ROTOR_ON && !routed)) {
288                 cpt = lnet_cpt_current();
289                 if (ptl->ptl_mtables[cpt]->mt_enabled)
290                         return ptl->ptl_mtables[cpt];
291         }
292
293         rotor = ptl->ptl_rotor++; /* get round-robin factor */
294         if (portal_rotor == LNET_PTL_ROTOR_HASH_RT && routed)
295                 cpt = lnet_cpt_of_nid(msg->msg_hdr.src_nid);
296         else
297                 cpt = rotor % LNET_CPT_NUMBER;
298
299         if (!ptl->ptl_mtables[cpt]->mt_enabled) {
300                 /* is there any active entry for this portal? */
301                 nmaps = ptl->ptl_mt_nmaps;
302                 /* map to an active mtable to avoid heavy "stealing" */
303                 if (nmaps != 0) {
304                         /* NB: there is possibility that ptl_mt_maps is being
305                          * changed because we are not under protection of
306                          * lnet_ptl_lock, but it shouldn't hurt anything */
307                         cpt = ptl->ptl_mt_maps[rotor % nmaps];
308                 }
309         }
310
311         return ptl->ptl_mtables[cpt];
312 }
313
314 cfs_list_t *
315 lnet_mt_match_head(struct lnet_match_table *mtable,
316                    lnet_process_id_t id, __u64 mbits)
317 {
318         struct lnet_portal *ptl = the_lnet.ln_portals[mtable->mt_portal];
319
320         if (lnet_ptl_is_wildcard(ptl)) {
321                 return &mtable->mt_mlist;
322
323         } else {
324                 unsigned long hash = mbits + id.nid + id.pid;
325
326                 LASSERT(lnet_ptl_is_unique(ptl));
327                 hash = cfs_hash_long(hash, LNET_MT_HASH_BITS);
328                 return &mtable->mt_mhash[hash];
329         }
330 }
331
332 int
333 lnet_mt_match_md(struct lnet_match_table *mtable,
334                  struct lnet_match_info *info, struct lnet_msg *msg)
335 {
336         cfs_list_t              *head;
337         lnet_me_t               *me;
338         lnet_me_t               *tmp;
339         int                     exhausted = 0;
340         int                     rc;
341
342         /* NB: only wildcard portal can return LNET_MATCHMD_EXHAUSTED */
343         if (lnet_ptl_is_wildcard(the_lnet.ln_portals[mtable->mt_portal]))
344                 exhausted = LNET_MATCHMD_EXHAUSTED;
345
346         head = lnet_mt_match_head(mtable, info->mi_id, info->mi_mbits);
347
348         cfs_list_for_each_entry_safe(me, tmp, head, me_list) {
349                 /* ME attached but MD not attached yet */
350                 if (me->me_md == NULL)
351                         continue;
352
353                 LASSERT(me == me->me_md->md_me);
354
355                 rc = lnet_try_match_md(me->me_md, info, msg);
356                 if ((rc & LNET_MATCHMD_EXHAUSTED) == 0)
357                         exhausted = 0; /* mlist is not empty */
358
359                 if ((rc & LNET_MATCHMD_FINISH) != 0) {
360                         /* don't return EXHAUSTED bit because we don't know
361                          * whether the mlist is empty or not */
362                         return rc & ~LNET_MATCHMD_EXHAUSTED;
363                 }
364         }
365
366         if (info->mi_opc == LNET_MD_OP_GET ||
367             !lnet_ptl_is_lazy(the_lnet.ln_portals[info->mi_portal]))
368                 return LNET_MATCHMD_DROP | exhausted;
369
370         return LNET_MATCHMD_NONE | exhausted;
371 }
372
373 static int
374 lnet_ptl_match_early(struct lnet_portal *ptl, struct lnet_msg *msg)
375 {
376         int     rc;
377
378         /* message arrived before any buffer posting on this portal,
379          * simply delay or drop this message */
380         if (likely(lnet_ptl_is_wildcard(ptl) || lnet_ptl_is_unique(ptl)))
381                 return 0;
382
383         lnet_ptl_lock(ptl);
384         /* check it again with hold of lock */
385         if (lnet_ptl_is_wildcard(ptl) || lnet_ptl_is_unique(ptl)) {
386                 lnet_ptl_unlock(ptl);
387                 return 0;
388         }
389
390         if (lnet_ptl_is_lazy(ptl)) {
391                 if (msg->msg_rx_ready_delay) {
392                         msg->msg_rx_delayed = 1;
393                         cfs_list_add_tail(&msg->msg_list,
394                                           &ptl->ptl_msg_delayed);
395                 }
396                 rc = LNET_MATCHMD_NONE;
397         } else {
398                 rc = LNET_MATCHMD_DROP;
399         }
400
401         lnet_ptl_unlock(ptl);
402         return rc;
403 }
404
405 static int
406 lnet_ptl_match_delay(struct lnet_portal *ptl,
407                      struct lnet_match_info *info, struct lnet_msg *msg)
408 {
409         int     first = ptl->ptl_mt_maps[0]; /* read w/o lock */
410         int     rc = 0;
411         int     i;
412
413         /* steal buffer from other CPTs, and delay it if nothing to steal,
414          * this function is more expensive than a regular match, but we
415          * don't expect it can happen a lot */
416         LASSERT(lnet_ptl_is_wildcard(ptl));
417
418         for (i = 0; i < LNET_CPT_NUMBER; i++) {
419                 struct lnet_match_table *mtable;
420                 int                     cpt;
421
422                 cpt = (first + i) % LNET_CPT_NUMBER;
423                 mtable = ptl->ptl_mtables[cpt];
424                 if (i != 0 && i != LNET_CPT_NUMBER - 1 && !mtable->mt_enabled)
425                         continue;
426
427                 lnet_res_lock(cpt);
428                 lnet_ptl_lock(ptl);
429
430                 if (i == 0) { /* the first try, attach on stealing list */
431                         cfs_list_add_tail(&msg->msg_list,
432                                           &ptl->ptl_msg_stealing);
433                 }
434
435                 if (!cfs_list_empty(&msg->msg_list)) { /* on stealing list */
436                         rc = lnet_mt_match_md(mtable, info, msg);
437
438                         if ((rc & LNET_MATCHMD_EXHAUSTED) != 0 &&
439                             mtable->mt_enabled)
440                                 lnet_ptl_disable_mt(ptl, cpt);
441
442                         if ((rc & LNET_MATCHMD_FINISH) != 0)
443                                 cfs_list_del_init(&msg->msg_list);
444
445                 } else {
446                         /* could be matched by lnet_ptl_attach_md()
447                          * which is called by another thread */
448                         rc = msg->msg_md == NULL ?
449                              LNET_MATCHMD_DROP : LNET_MATCHMD_OK;
450                 }
451
452                 if (!cfs_list_empty(&msg->msg_list) && /* not matched yet */
453                     (i == LNET_CPT_NUMBER - 1 || /* the last CPT */
454                      ptl->ptl_mt_nmaps == 0 ||   /* no active CPT */
455                      (ptl->ptl_mt_nmaps == 1 &&  /* the only active CPT */
456                       ptl->ptl_mt_maps[0] == cpt))) {
457                         /* nothing to steal, delay or drop */
458                         cfs_list_del_init(&msg->msg_list);
459
460                         if (lnet_ptl_is_lazy(ptl)) {
461                                 msg->msg_rx_delayed = 1;
462                                 cfs_list_add_tail(&msg->msg_list,
463                                                   &ptl->ptl_msg_delayed);
464                                 rc = LNET_MATCHMD_NONE;
465                         } else {
466                                 rc = LNET_MATCHMD_DROP;
467                         }
468                 }
469
470                 lnet_ptl_unlock(ptl);
471                 lnet_res_unlock(cpt);
472
473                 if ((rc & LNET_MATCHMD_FINISH) != 0 || msg->msg_rx_delayed)
474                         break;
475         }
476
477         return rc;
478 }
479
480 int
481 lnet_ptl_match_md(struct lnet_match_info *info, struct lnet_msg *msg)
482 {
483         struct lnet_match_table *mtable;
484         struct lnet_portal      *ptl;
485         int                     rc;
486
487         CDEBUG(D_NET, "Request from %s of length %d into portal %d "
488                "MB="LPX64"\n", libcfs_id2str(info->mi_id),
489                info->mi_rlength, info->mi_portal, info->mi_mbits);
490
491         if (info->mi_portal >= the_lnet.ln_nportals) {
492                 CERROR("Invalid portal %d not in [0-%d]\n",
493                        info->mi_portal, the_lnet.ln_nportals);
494                 return LNET_MATCHMD_DROP;
495         }
496
497         ptl = the_lnet.ln_portals[info->mi_portal];
498         rc = lnet_ptl_match_early(ptl, msg);
499         if (rc != 0) /* matched or delayed early message */
500                 return rc;
501
502         mtable = lnet_mt_of_match(info, msg);
503         lnet_res_lock(mtable->mt_cpt);
504
505         if (the_lnet.ln_shutdown) {
506                 rc = LNET_MATCHMD_DROP;
507                 goto out1;
508         }
509
510         rc = lnet_mt_match_md(mtable, info, msg);
511         if ((rc & LNET_MATCHMD_EXHAUSTED) != 0 && mtable->mt_enabled) {
512                 lnet_ptl_lock(ptl);
513                 lnet_ptl_disable_mt(ptl, mtable->mt_cpt);
514                 lnet_ptl_unlock(ptl);
515         }
516
517         if ((rc & LNET_MATCHMD_FINISH) != 0)    /* matched or dropping */
518                 goto out1;
519
520         if (!msg->msg_rx_ready_delay)
521                 goto out1;
522
523         LASSERT(lnet_ptl_is_lazy(ptl));
524         LASSERT(!msg->msg_rx_delayed);
525
526         /* NB: we don't expect "delay" can happen a lot */
527         if (lnet_ptl_is_unique(ptl) || LNET_CPT_NUMBER == 1) {
528                 lnet_ptl_lock(ptl);
529
530                 msg->msg_rx_delayed = 1;
531                 cfs_list_add_tail(&msg->msg_list, &ptl->ptl_msg_delayed);
532
533                 lnet_ptl_unlock(ptl);
534                 lnet_res_unlock(mtable->mt_cpt);
535
536         } else  {
537                 lnet_res_unlock(mtable->mt_cpt);
538                 rc = lnet_ptl_match_delay(ptl, info, msg);
539         }
540
541         if (msg->msg_rx_delayed) {
542                 CDEBUG(D_NET,
543                        "Delaying %s from %s ptl %d MB "LPX64" off %d len %d\n",
544                        info->mi_opc == LNET_MD_OP_PUT ? "PUT" : "GET",
545                        libcfs_id2str(info->mi_id), info->mi_portal,
546                        info->mi_mbits, info->mi_roffset, info->mi_rlength);
547         }
548         goto out0;
549  out1:
550         lnet_res_unlock(mtable->mt_cpt);
551  out0:
552         /* EXHAUSTED bit is only meaningful for internal functions */
553         return rc & ~LNET_MATCHMD_EXHAUSTED;
554 }
555
556 void
557 lnet_ptl_detach_md(lnet_me_t *me, lnet_libmd_t *md)
558 {
559         LASSERT(me->me_md == md && md->md_me == me);
560
561         me->me_md = NULL;
562         md->md_me = NULL;
563 }
564
565 /* called with lnet_res_lock held */
566 void
567 lnet_ptl_attach_md(lnet_me_t *me, lnet_libmd_t *md,
568                    cfs_list_t *matches, cfs_list_t *drops)
569 {
570         struct lnet_portal      *ptl = the_lnet.ln_portals[me->me_portal];
571         struct lnet_match_table *mtable;
572         cfs_list_t              *head;
573         lnet_msg_t              *tmp;
574         lnet_msg_t              *msg;
575         int                     exhausted = 0;
576         int                     cpt;
577
578         LASSERT(md->md_refcount == 0); /* a brand new MD */
579
580         me->me_md = md;
581         md->md_me = me;
582
583         cpt = lnet_cpt_of_cookie(md->md_lh.lh_cookie);
584         mtable = ptl->ptl_mtables[cpt];
585
586         if (cfs_list_empty(&ptl->ptl_msg_stealing) &&
587             cfs_list_empty(&ptl->ptl_msg_delayed) &&
588             mtable->mt_enabled)
589                 return;
590
591         lnet_ptl_lock(ptl);
592         head = &ptl->ptl_msg_stealing;
593  again:
594         cfs_list_for_each_entry_safe(msg, tmp, head, msg_list) {
595                 struct lnet_match_info  info;
596                 lnet_hdr_t              *hdr;
597                 int                     rc;
598
599                 LASSERT(msg->msg_rx_delayed || head == &ptl->ptl_msg_stealing);
600
601                 hdr   = &msg->msg_hdr;
602                 info.mi_id.nid  = hdr->src_nid;
603                 info.mi_id.pid  = hdr->src_pid;
604                 info.mi_opc     = LNET_MD_OP_PUT;
605                 info.mi_portal  = hdr->msg.put.ptl_index;
606                 info.mi_rlength = hdr->payload_length;
607                 info.mi_roffset = hdr->msg.put.offset;
608                 info.mi_mbits   = hdr->msg.put.match_bits;
609
610                 rc = lnet_try_match_md(md, &info, msg);
611
612                 exhausted = (rc & LNET_MATCHMD_EXHAUSTED) != 0;
613                 if ((rc & LNET_MATCHMD_NONE) != 0) {
614                         if (exhausted)
615                                 break;
616                         continue;
617                 }
618
619                 /* Hurrah! This _is_ a match */
620                 LASSERT((rc & LNET_MATCHMD_FINISH) != 0);
621                 cfs_list_del_init(&msg->msg_list);
622
623                 if (head == &ptl->ptl_msg_stealing) {
624                         if (exhausted)
625                                 break;
626                         /* stealing thread will handle the message */
627                         continue;
628                 }
629
630                 if ((rc & LNET_MATCHMD_OK) != 0) {
631                         cfs_list_add_tail(&msg->msg_list, matches);
632
633                         CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
634                                "match "LPU64" offset %d length %d.\n",
635                                libcfs_id2str(info.mi_id),
636                                info.mi_portal, info.mi_mbits,
637                                info.mi_roffset, info.mi_rlength);
638                 } else {
639                         cfs_list_add_tail(&msg->msg_list, drops);
640                 }
641
642                 if (exhausted)
643                         break;
644         }
645
646         if (!exhausted && head == &ptl->ptl_msg_stealing) {
647                 head = &ptl->ptl_msg_delayed;
648                 goto again;
649         }
650
651         if (lnet_ptl_is_wildcard(ptl) && !exhausted && !mtable->mt_enabled)
652                 lnet_ptl_enable_mt(ptl, cpt);
653
654         lnet_ptl_unlock(ptl);
655 }
656
657 void
658 lnet_ptl_cleanup(struct lnet_portal *ptl)
659 {
660         struct lnet_match_table *mtable;
661         int                     i;
662
663         if (ptl->ptl_mtables == NULL) /* uninitialized portal */
664                 return;
665
666         LASSERT(cfs_list_empty(&ptl->ptl_msg_delayed));
667         LASSERT(cfs_list_empty(&ptl->ptl_msg_stealing));
668 #ifndef __KERNEL__
669 # ifdef HAVE_LIBPTHREAD
670         pthread_mutex_destroy(&ptl->ptl_lock);
671 # endif
672 #endif
673         cfs_percpt_for_each(mtable, i, ptl->ptl_mtables) {
674                 cfs_list_t      *mhash;
675                 lnet_me_t       *me;
676                 int             j;
677
678                 if (mtable->mt_mhash == NULL) /* uninitialized match-table */
679                         continue;
680
681                 mhash = mtable->mt_mhash;
682                 /* cleanup ME */
683                 while (!cfs_list_empty(&mtable->mt_mlist)) {
684                         me = cfs_list_entry(mtable->mt_mlist.next,
685                                             lnet_me_t, me_list);
686                         CERROR("Active wildcard ME %p on exit\n", me);
687                         cfs_list_del(&me->me_list);
688                         lnet_me_free(me);
689                 }
690
691                 for (j = 0; j < LNET_MT_HASH_SIZE; j++) {
692                         while (!cfs_list_empty(&mhash[j])) {
693                                 me = cfs_list_entry(mhash[j].next,
694                                                     lnet_me_t, me_list);
695                                 CERROR("Active unique ME %p on exit\n", me);
696                                 cfs_list_del(&me->me_list);
697                                 lnet_me_free(me);
698                         }
699                 }
700
701                 LIBCFS_FREE(mhash, sizeof(*mhash) * LNET_MT_HASH_SIZE);
702         }
703
704         cfs_percpt_free(ptl->ptl_mtables);
705         ptl->ptl_mtables = NULL;
706 }
707
708 int
709 lnet_ptl_setup(struct lnet_portal *ptl, int index)
710 {
711         struct lnet_match_table *mtable;
712         cfs_list_t              *mhash;
713         int                     i;
714         int                     j;
715
716         ptl->ptl_mtables = cfs_percpt_alloc(lnet_cpt_table(),
717                                             sizeof(struct lnet_match_table));
718         if (ptl->ptl_mtables == NULL) {
719                 CERROR("Failed to create match table for portal %d\n", index);
720                 return -ENOMEM;
721         }
722
723         ptl->ptl_index = index;
724         CFS_INIT_LIST_HEAD(&ptl->ptl_msg_delayed);
725         CFS_INIT_LIST_HEAD(&ptl->ptl_msg_stealing);
726 #ifdef __KERNEL__
727         cfs_spin_lock_init(&ptl->ptl_lock);
728 #else
729 # ifdef HAVE_LIBPTHREAD
730         pthread_mutex_init(&ptl->ptl_lock, NULL);
731 # endif
732 #endif
733         cfs_percpt_for_each(mtable, i, ptl->ptl_mtables) {
734                 LIBCFS_CPT_ALLOC(mhash, lnet_cpt_table(), i,
735                                  sizeof(*mhash) * LNET_MT_HASH_SIZE);
736                 if (mhash == NULL) {
737                         CERROR("Failed to create match hash for portal %d\n",
738                                index);
739                         goto failed;
740                 }
741
742                 mtable->mt_mhash = mhash;
743                 for (j = 0; j < LNET_MT_HASH_SIZE; j++)
744                         CFS_INIT_LIST_HEAD(&mhash[j]);
745
746                 CFS_INIT_LIST_HEAD(&mtable->mt_mlist);
747                 mtable->mt_portal = index;
748                 mtable->mt_cpt = i;
749         }
750
751         return 0;
752  failed:
753         lnet_ptl_cleanup(ptl);
754         return -ENOMEM;
755 }
756
757 void
758 lnet_portals_destroy(void)
759 {
760         int     i;
761
762         if (the_lnet.ln_portals == NULL)
763                 return;
764
765         for (i = 0; i < the_lnet.ln_nportals; i++)
766                 lnet_ptl_cleanup(the_lnet.ln_portals[i]);
767
768         cfs_array_free(the_lnet.ln_portals);
769         the_lnet.ln_portals = NULL;
770 }
771
772 int
773 lnet_portals_create(void)
774 {
775         int     size;
776         int     i;
777
778         size = offsetof(struct lnet_portal, ptl_mt_maps[LNET_CPT_NUMBER]);
779
780         the_lnet.ln_nportals = MAX_PORTALS;
781         the_lnet.ln_portals = cfs_array_alloc(the_lnet.ln_nportals, size);
782         if (the_lnet.ln_portals == NULL) {
783                 CERROR("Failed to allocate portals table\n");
784                 return -ENOMEM;
785         }
786
787         for (i = 0; i < the_lnet.ln_nportals; i++) {
788                 if (lnet_ptl_setup(the_lnet.ln_portals[i], i)) {
789                         lnet_portals_destroy();
790                         return -ENOMEM;
791                 }
792         }
793
794         return 0;
795 }
796
797 /**
798  * Turn on the lazy portal attribute. Use with caution!
799  *
800  * This portal attribute only affects incoming PUT requests to the portal,
801  * and is off by default. By default, if there's no matching MD for an
802  * incoming PUT request, it is simply dropped. With the lazy attribute on,
803  * such requests are queued indefinitely until either a matching MD is
804  * posted to the portal or the lazy attribute is turned off.
805  *
806  * It would prevent dropped requests, however it should be regarded as the
807  * last line of defense - i.e. users must keep a close watch on active
808  * buffers on a lazy portal and once it becomes too low post more buffers as
809  * soon as possible. This is because delayed requests usually have detrimental
810  * effects on underlying network connections. A few delayed requests often
811  * suffice to bring an underlying connection to a complete halt, due to flow
812  * control mechanisms.
813  *
814  * There's also a DOS attack risk. If users don't post match-all MDs on a
815  * lazy portal, a malicious peer can easily stop a service by sending some
816  * PUT requests with match bits that won't match any MD. A routed server is
817  * especially vulnerable since the connections to its neighbor routers are
818  * shared among all clients.
819  *
820  * \param portal Index of the portal to enable the lazy attribute on.
821  *
822  * \retval 0       On success.
823  * \retval -EINVAL If \a portal is not a valid index.
824  */
825 int
826 LNetSetLazyPortal(int portal)
827 {
828         struct lnet_portal *ptl;
829
830         if (portal < 0 || portal >= the_lnet.ln_nportals)
831                 return -EINVAL;
832
833         CDEBUG(D_NET, "Setting portal %d lazy\n", portal);
834         ptl = the_lnet.ln_portals[portal];
835
836         lnet_res_lock(LNET_LOCK_EX);
837         lnet_ptl_lock(ptl);
838
839         lnet_ptl_setopt(ptl, LNET_PTL_LAZY);
840
841         lnet_ptl_unlock(ptl);
842         lnet_res_unlock(LNET_LOCK_EX);
843
844         return 0;
845 }
846 EXPORT_SYMBOL(LNetSetLazyPortal);
847
848 /**
849  * Turn off the lazy portal attribute. Delayed requests on the portal,
850  * if any, will be all dropped when this function returns.
851  *
852  * \param portal Index of the portal to disable the lazy attribute on.
853  *
854  * \retval 0       On success.
855  * \retval -EINVAL If \a portal is not a valid index.
856  */
857 int
858 LNetClearLazyPortal(int portal)
859 {
860         struct lnet_portal      *ptl;
861         CFS_LIST_HEAD           (zombies);
862
863         if (portal < 0 || portal >= the_lnet.ln_nportals)
864                 return -EINVAL;
865
866         ptl = the_lnet.ln_portals[portal];
867
868         lnet_res_lock(LNET_LOCK_EX);
869         lnet_ptl_lock(ptl);
870
871         if (!lnet_ptl_is_lazy(ptl)) {
872                 lnet_ptl_unlock(ptl);
873                 lnet_res_unlock(LNET_LOCK_EX);
874                 return 0;
875         }
876
877         if (the_lnet.ln_shutdown)
878                 CWARN("Active lazy portal %d on exit\n", portal);
879         else
880                 CDEBUG(D_NET, "clearing portal %d lazy\n", portal);
881
882         /* grab all the blocked messages atomically */
883         cfs_list_splice_init(&ptl->ptl_msg_delayed, &zombies);
884
885         lnet_ptl_unsetopt(ptl, LNET_PTL_LAZY);
886
887         lnet_ptl_unlock(ptl);
888         lnet_res_unlock(LNET_LOCK_EX);
889
890         lnet_drop_delayed_msg_list(&zombies, "Clearing lazy portal attr");
891
892         return 0;
893 }
894 EXPORT_SYMBOL(LNetClearLazyPortal);