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