4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
24 * Copyright (c) 2012, 2015, Intel Corporation.
27 * This file is part of Lustre, http://www.lustre.org/
28 * Lustre is a trademark of Sun Microsystems, Inc.
32 * portal & match routines
34 * Author: liang@whamcloud.com
37 #define DEBUG_SUBSYSTEM S_LNET
39 #include <lnet/lib-lnet.h>
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");
47 lnet_ptl_match_type(unsigned int index, lnet_process_id_t match_id,
48 __u64 mbits, __u64 ignore_bits)
50 struct lnet_portal *ptl = the_lnet.ln_portals[index];
53 unique = ignore_bits == 0 &&
54 match_id.nid != LNET_NID_ANY &&
55 match_id.pid != LNET_PID_ANY;
57 LASSERT(!lnet_ptl_is_unique(ptl) || !lnet_ptl_is_wildcard(ptl));
59 /* prefer to check w/o any lock */
60 if (likely(lnet_ptl_is_unique(ptl) || lnet_ptl_is_wildcard(ptl)))
63 /* unset, new portal */
65 /* check again with lock */
66 if (unlikely(lnet_ptl_is_unique(ptl) || lnet_ptl_is_wildcard(ptl))) {
73 lnet_ptl_setopt(ptl, LNET_PTL_MATCH_UNIQUE);
75 lnet_ptl_setopt(ptl, LNET_PTL_MATCH_WILDCARD);
82 if ((lnet_ptl_is_unique(ptl) && !unique) ||
83 (lnet_ptl_is_wildcard(ptl) && unique))
89 lnet_ptl_enable_mt(struct lnet_portal *ptl, int cpt)
91 struct lnet_match_table *mtable = ptl->ptl_mtables[cpt];
94 /* with hold of both lnet_res_lock(cpt) and lnet_ptl_lock */
95 LASSERT(lnet_ptl_is_wildcard(ptl));
97 mtable->mt_enabled = 1;
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)
106 ptl->ptl_mt_maps[i + 1] = ptl->ptl_mt_maps[i];
107 ptl->ptl_mt_maps[i] = cpt;
114 lnet_ptl_disable_mt(struct lnet_portal *ptl, int cpt)
116 struct lnet_match_table *mtable = ptl->ptl_mtables[cpt];
119 /* with hold of both lnet_res_lock(cpt) and lnet_ptl_lock */
120 LASSERT(lnet_ptl_is_wildcard(ptl));
122 if (LNET_CPT_NUMBER == 1)
123 return; /* never disable the only match-table */
125 mtable->mt_enabled = 0;
127 LASSERT(ptl->ptl_mt_nmaps > 0 &&
128 ptl->ptl_mt_nmaps <= LNET_CPT_NUMBER);
130 /* remove it from mt_maps */
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];
139 lnet_try_match_md(lnet_libmd_t *md,
140 struct lnet_match_info *info, struct lnet_msg *msg)
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 */
145 unsigned int mlength;
146 lnet_me_t *me = md->md_me;
149 if (lnet_md_exhausted(md))
150 return LNET_MATCHMD_NONE | LNET_MATCHMD_EXHAUSTED;
152 /* mismatched MD op */
153 if ((md->md_options & info->mi_opc) == 0)
154 return LNET_MATCHMD_NONE;
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;
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;
165 /* mismatched ME matchbits? */
166 if (((me->me_match_bits ^ info->mi_mbits) & ~me->me_ignore_bits) != 0)
167 return LNET_MATCHMD_NONE;
169 /* Hurrah! This _is_ a match; check it out... */
171 if ((md->md_options & LNET_MD_MANAGE_REMOTE) == 0)
172 offset = md->md_offset;
174 offset = info->mi_roffset;
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);
180 mlength = md->md_length - offset;
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"
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);
192 return LNET_MATCHMD_DROP;
195 /* Commit to this ME/MD */
196 CDEBUG(D_NET, "Incoming %s index %x from %s of "
197 "length %d/%d into md %#llx [%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);
202 lnet_msg_attach_md(msg, md, offset, mlength);
203 md->md_offset = offset + mlength;
205 if (!lnet_md_exhausted(md))
206 return LNET_MATCHMD_OK;
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)
214 return LNET_MATCHMD_OK | LNET_MATCHMD_EXHAUSTED;
217 static struct lnet_match_table *
218 lnet_match2mt(struct lnet_portal *ptl, lnet_process_id_t id, __u64 mbits)
220 if (LNET_CPT_NUMBER == 1)
221 return ptl->ptl_mtables[0]; /* the only one */
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)] : NULL;
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)
232 struct lnet_portal *ptl;
233 struct lnet_match_table *mtable;
235 /* NB: called w/o lock */
236 LASSERT(index < the_lnet.ln_nportals);
238 if (!lnet_ptl_match_type(index, id, mbits, ignore_bits))
241 ptl = the_lnet.ln_portals[index];
243 mtable = lnet_match2mt(ptl, id, mbits);
244 if (mtable != NULL) /* unique portal or only one match-table */
247 /* it's a wildcard portal */
251 case LNET_INS_BEFORE:
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];
257 /* posted by cpu-affinity thread */
258 return ptl->ptl_mtables[lnet_cpt_current()];
262 static struct lnet_match_table *
263 lnet_mt_of_match(struct lnet_match_info *info, struct lnet_msg *msg)
265 struct lnet_match_table *mtable;
266 struct lnet_portal *ptl;
272 /* NB: called w/o lock */
273 LASSERT(info->mi_portal < the_lnet.ln_nportals);
274 ptl = the_lnet.ln_portals[info->mi_portal];
276 LASSERT(lnet_ptl_is_wildcard(ptl) || lnet_ptl_is_unique(ptl));
278 mtable = lnet_match2mt(ptl, info->mi_id, info->mi_mbits);
282 /* it's a wildcard portal */
283 routed = LNET_NIDNET(msg->msg_hdr.src_nid) !=
284 LNET_NIDNET(msg->msg_hdr.dest_nid);
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];
293 rotor = ptl->ptl_rotor++; /* get round-robin factor */
294 if (portal_rotor == LNET_PTL_ROTOR_HASH_RT && routed)
297 cpt = rotor % LNET_CPT_NUMBER;
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" */
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];
311 return ptl->ptl_mtables[cpt];
315 lnet_mt_test_exhausted(struct lnet_match_table *mtable, int pos)
320 if (!lnet_ptl_is_wildcard(the_lnet.ln_portals[mtable->mt_portal]))
323 if (pos < 0) { /* check all bits */
324 for (i = 0; i < LNET_MT_EXHAUSTED_BMAP; i++) {
325 if (mtable->mt_exhausted[i] != (__u64)(-1))
331 LASSERT(pos <= LNET_MT_HASH_IGNORE);
332 /* mtable::mt_mhash[pos] is marked as exhausted or not */
333 bmap = &mtable->mt_exhausted[pos >> LNET_MT_BITS_U64];
334 pos &= (1 << LNET_MT_BITS_U64) - 1;
336 return ((*bmap) & (1ULL << pos)) != 0;
340 lnet_mt_set_exhausted(struct lnet_match_table *mtable, int pos, int exhausted)
344 LASSERT(lnet_ptl_is_wildcard(the_lnet.ln_portals[mtable->mt_portal]));
345 LASSERT(pos <= LNET_MT_HASH_IGNORE);
347 /* set mtable::mt_mhash[pos] as exhausted/non-exhausted */
348 bmap = &mtable->mt_exhausted[pos >> LNET_MT_BITS_U64];
349 pos &= (1 << LNET_MT_BITS_U64) - 1;
352 *bmap &= ~(1ULL << pos);
354 *bmap |= 1ULL << pos;
358 lnet_mt_match_head(struct lnet_match_table *mtable,
359 lnet_process_id_t id, __u64 mbits)
361 struct lnet_portal *ptl = the_lnet.ln_portals[mtable->mt_portal];
363 if (lnet_ptl_is_wildcard(ptl)) {
364 return &mtable->mt_mhash[mbits & LNET_MT_HASH_MASK];
366 unsigned long hash = mbits + id.nid + id.pid;
368 LASSERT(lnet_ptl_is_unique(ptl));
369 hash = hash_long(hash, LNET_MT_HASH_BITS);
370 return &mtable->mt_mhash[hash & LNET_MT_HASH_MASK];
375 lnet_mt_match_md(struct lnet_match_table *mtable,
376 struct lnet_match_info *info, struct lnet_msg *msg)
378 struct list_head *head;
384 /* any ME with ignore bits? */
385 if (!list_empty(&mtable->mt_mhash[LNET_MT_HASH_IGNORE]))
386 head = &mtable->mt_mhash[LNET_MT_HASH_IGNORE];
388 head = lnet_mt_match_head(mtable, info->mi_id, info->mi_mbits);
390 /* NB: only wildcard portal needs to return LNET_MATCHMD_EXHAUSTED */
391 if (lnet_ptl_is_wildcard(the_lnet.ln_portals[mtable->mt_portal]))
392 exhausted = LNET_MATCHMD_EXHAUSTED;
394 list_for_each_entry_safe(me, tmp, head, me_list) {
395 /* ME attached but MD not attached yet */
396 if (me->me_md == NULL)
399 LASSERT(me == me->me_md->md_me);
401 rc = lnet_try_match_md(me->me_md, info, msg);
402 if ((rc & LNET_MATCHMD_EXHAUSTED) == 0)
403 exhausted = 0; /* mlist is not empty */
405 if ((rc & LNET_MATCHMD_FINISH) != 0) {
406 /* don't return EXHAUSTED bit because we don't know
407 * whether the mlist is empty or not */
408 return rc & ~LNET_MATCHMD_EXHAUSTED;
412 if (exhausted == LNET_MATCHMD_EXHAUSTED) { /* @head is exhausted */
413 lnet_mt_set_exhausted(mtable, head - mtable->mt_mhash, 1);
414 if (!lnet_mt_test_exhausted(mtable, -1))
418 if (exhausted == 0 && head == &mtable->mt_mhash[LNET_MT_HASH_IGNORE]) {
419 head = lnet_mt_match_head(mtable, info->mi_id, info->mi_mbits);
420 goto again; /* re-check MEs w/o ignore-bits */
423 if (info->mi_opc == LNET_MD_OP_GET ||
424 !lnet_ptl_is_lazy(the_lnet.ln_portals[info->mi_portal]))
425 return LNET_MATCHMD_DROP | exhausted;
427 return LNET_MATCHMD_NONE | exhausted;
431 lnet_ptl_match_early(struct lnet_portal *ptl, struct lnet_msg *msg)
435 /* message arrived before any buffer posting on this portal,
436 * simply delay or drop this message */
437 if (likely(lnet_ptl_is_wildcard(ptl) || lnet_ptl_is_unique(ptl)))
441 /* check it again with hold of lock */
442 if (lnet_ptl_is_wildcard(ptl) || lnet_ptl_is_unique(ptl)) {
443 lnet_ptl_unlock(ptl);
447 if (lnet_ptl_is_lazy(ptl)) {
448 if (msg->msg_rx_ready_delay) {
449 msg->msg_rx_delayed = 1;
450 list_add_tail(&msg->msg_list,
451 &ptl->ptl_msg_delayed);
453 rc = LNET_MATCHMD_NONE;
455 rc = LNET_MATCHMD_DROP;
458 lnet_ptl_unlock(ptl);
463 lnet_ptl_match_delay(struct lnet_portal *ptl,
464 struct lnet_match_info *info, struct lnet_msg *msg)
466 int first = ptl->ptl_mt_maps[0]; /* read w/o lock */
471 * Steal buffer from other CPTs, and delay msg if nothing to
472 * steal. This function is more expensive than a regular
473 * match, but we don't expect it can happen a lot. The return
474 * code contains one of LNET_MATCHMD_OK, LNET_MATCHMD_DROP, or
477 LASSERT(lnet_ptl_is_wildcard(ptl));
479 for (i = 0; i < LNET_CPT_NUMBER; i++) {
480 struct lnet_match_table *mtable;
483 cpt = (first + i) % LNET_CPT_NUMBER;
484 mtable = ptl->ptl_mtables[cpt];
485 if (i != 0 && i != LNET_CPT_NUMBER - 1 && !mtable->mt_enabled)
492 /* The first try, add to stealing list. */
493 list_add_tail(&msg->msg_list,
494 &ptl->ptl_msg_stealing);
497 if (!list_empty(&msg->msg_list)) {
498 /* On stealing list. */
499 rc = lnet_mt_match_md(mtable, info, msg);
501 if ((rc & LNET_MATCHMD_EXHAUSTED) != 0 &&
503 lnet_ptl_disable_mt(ptl, cpt);
505 if ((rc & LNET_MATCHMD_FINISH) != 0) {
506 /* Match found, remove from stealing list. */
507 list_del_init(&msg->msg_list);
508 } else if (i == LNET_CPT_NUMBER - 1 || /* (1) */
509 ptl->ptl_mt_nmaps == 0 || /* (2) */
510 (ptl->ptl_mt_nmaps == 1 && /* (3) */
511 ptl->ptl_mt_maps[0] == cpt)) {
513 * No match found, and this is either
514 * (1) the last cpt to check, or
515 * (2) there is no active cpt, or
516 * (3) this is the only active cpt.
517 * There is nothing to steal: delay or
520 list_del_init(&msg->msg_list);
522 if (lnet_ptl_is_lazy(ptl)) {
523 msg->msg_rx_delayed = 1;
524 list_add_tail(&msg->msg_list,
525 &ptl->ptl_msg_delayed);
526 rc = LNET_MATCHMD_NONE;
528 rc = LNET_MATCHMD_DROP;
531 /* Do another iteration. */
536 * No longer on stealing list: another thread
537 * matched the message in lnet_ptl_attach_md().
538 * We are now expected to handle the message.
540 rc = msg->msg_md == NULL ?
541 LNET_MATCHMD_DROP : LNET_MATCHMD_OK;
544 lnet_ptl_unlock(ptl);
545 lnet_res_unlock(cpt);
548 * Note that test (1) above ensures that we always
549 * exit the loop through this break statement.
551 * LNET_MATCHMD_NONE means msg was added to the
552 * delayed queue, and we may no longer reference it
553 * after lnet_ptl_unlock() and lnet_res_unlock().
555 if (rc & (LNET_MATCHMD_FINISH | LNET_MATCHMD_NONE))
563 lnet_ptl_match_md(struct lnet_match_info *info, struct lnet_msg *msg)
565 struct lnet_match_table *mtable;
566 struct lnet_portal *ptl;
569 CDEBUG(D_NET, "Request from %s of length %d into portal %d "
570 "MB=%#llx\n", libcfs_id2str(info->mi_id),
571 info->mi_rlength, info->mi_portal, info->mi_mbits);
573 if (info->mi_portal >= the_lnet.ln_nportals) {
574 CERROR("Invalid portal %d not in [0-%d]\n",
575 info->mi_portal, the_lnet.ln_nportals);
576 return LNET_MATCHMD_DROP;
579 ptl = the_lnet.ln_portals[info->mi_portal];
580 rc = lnet_ptl_match_early(ptl, msg);
581 if (rc != 0) /* matched or delayed early message */
584 mtable = lnet_mt_of_match(info, msg);
585 lnet_res_lock(mtable->mt_cpt);
587 if (the_lnet.ln_shutdown) {
588 rc = LNET_MATCHMD_DROP;
592 rc = lnet_mt_match_md(mtable, info, msg);
593 if ((rc & LNET_MATCHMD_EXHAUSTED) != 0 && mtable->mt_enabled) {
595 lnet_ptl_disable_mt(ptl, mtable->mt_cpt);
596 lnet_ptl_unlock(ptl);
599 if ((rc & LNET_MATCHMD_FINISH) != 0) /* matched or dropping */
602 if (!msg->msg_rx_ready_delay)
605 LASSERT(lnet_ptl_is_lazy(ptl));
606 LASSERT(!msg->msg_rx_delayed);
608 /* NB: we don't expect "delay" can happen a lot */
609 if (lnet_ptl_is_unique(ptl) || LNET_CPT_NUMBER == 1) {
612 msg->msg_rx_delayed = 1;
613 list_add_tail(&msg->msg_list, &ptl->ptl_msg_delayed);
615 lnet_ptl_unlock(ptl);
616 lnet_res_unlock(mtable->mt_cpt);
617 rc = LNET_MATCHMD_NONE;
619 lnet_res_unlock(mtable->mt_cpt);
620 rc = lnet_ptl_match_delay(ptl, info, msg);
623 /* LNET_MATCHMD_NONE means msg was added to the delay queue */
624 if (rc & LNET_MATCHMD_NONE) {
626 "Delaying %s from %s ptl %d MB %#llx off %d len %d\n",
627 info->mi_opc == LNET_MD_OP_PUT ? "PUT" : "GET",
628 libcfs_id2str(info->mi_id), info->mi_portal,
629 info->mi_mbits, info->mi_roffset, info->mi_rlength);
633 lnet_res_unlock(mtable->mt_cpt);
635 /* EXHAUSTED bit is only meaningful for internal functions */
636 return rc & ~LNET_MATCHMD_EXHAUSTED;
640 lnet_ptl_detach_md(lnet_me_t *me, lnet_libmd_t *md)
642 LASSERT(me->me_md == md && md->md_me == me);
648 /* called with lnet_res_lock held */
650 lnet_ptl_attach_md(lnet_me_t *me, lnet_libmd_t *md,
651 struct list_head *matches, struct list_head *drops)
653 struct lnet_portal *ptl = the_lnet.ln_portals[me->me_portal];
654 struct lnet_match_table *mtable;
655 struct list_head *head;
661 LASSERT(md->md_refcount == 0); /* a brand new MD */
666 cpt = lnet_cpt_of_cookie(md->md_lh.lh_cookie);
667 mtable = ptl->ptl_mtables[cpt];
669 if (list_empty(&ptl->ptl_msg_stealing) &&
670 list_empty(&ptl->ptl_msg_delayed) &&
671 !lnet_mt_test_exhausted(mtable, me->me_pos))
675 head = &ptl->ptl_msg_stealing;
677 list_for_each_entry_safe(msg, tmp, head, msg_list) {
678 struct lnet_match_info info;
682 LASSERT(msg->msg_rx_delayed || head == &ptl->ptl_msg_stealing);
685 /* Multi-Rail: Primary peer NID */
686 info.mi_id.nid = msg->msg_initiator;
687 info.mi_id.pid = hdr->src_pid;
688 info.mi_opc = LNET_MD_OP_PUT;
689 info.mi_portal = hdr->msg.put.ptl_index;
690 info.mi_rlength = hdr->payload_length;
691 info.mi_roffset = hdr->msg.put.offset;
692 info.mi_mbits = hdr->msg.put.match_bits;
694 rc = lnet_try_match_md(md, &info, msg);
696 exhausted = (rc & LNET_MATCHMD_EXHAUSTED) != 0;
697 if ((rc & LNET_MATCHMD_NONE) != 0) {
703 /* Hurrah! This _is_ a match */
704 LASSERT((rc & LNET_MATCHMD_FINISH) != 0);
705 list_del_init(&msg->msg_list);
707 if (head == &ptl->ptl_msg_stealing) {
710 /* stealing thread will handle the message */
714 if ((rc & LNET_MATCHMD_OK) != 0) {
715 list_add_tail(&msg->msg_list, matches);
717 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
718 "match %llu offset %d length %d.\n",
719 libcfs_id2str(info.mi_id),
720 info.mi_portal, info.mi_mbits,
721 info.mi_roffset, info.mi_rlength);
723 list_add_tail(&msg->msg_list, drops);
730 if (!exhausted && head == &ptl->ptl_msg_stealing) {
731 head = &ptl->ptl_msg_delayed;
735 if (lnet_ptl_is_wildcard(ptl) && !exhausted) {
736 lnet_mt_set_exhausted(mtable, me->me_pos, 0);
737 if (!mtable->mt_enabled)
738 lnet_ptl_enable_mt(ptl, cpt);
741 lnet_ptl_unlock(ptl);
745 lnet_ptl_cleanup(struct lnet_portal *ptl)
747 struct lnet_match_table *mtable;
750 if (ptl->ptl_mtables == NULL) /* uninitialized portal */
753 LASSERT(list_empty(&ptl->ptl_msg_delayed));
754 LASSERT(list_empty(&ptl->ptl_msg_stealing));
755 cfs_percpt_for_each(mtable, i, ptl->ptl_mtables) {
756 struct list_head *mhash;
760 if (mtable->mt_mhash == NULL) /* uninitialized match-table */
763 mhash = mtable->mt_mhash;
765 for (j = 0; j < LNET_MT_HASH_SIZE + 1; j++) {
766 while (!list_empty(&mhash[j])) {
767 me = list_entry(mhash[j].next,
769 CERROR("Active ME %p on exit\n", me);
770 list_del(&me->me_list);
774 /* the extra entry is for MEs with ignore bits */
775 LIBCFS_FREE(mhash, sizeof(*mhash) * (LNET_MT_HASH_SIZE + 1));
778 cfs_percpt_free(ptl->ptl_mtables);
779 ptl->ptl_mtables = NULL;
783 lnet_ptl_setup(struct lnet_portal *ptl, int index)
785 struct lnet_match_table *mtable;
786 struct list_head *mhash;
790 ptl->ptl_mtables = cfs_percpt_alloc(lnet_cpt_table(),
791 sizeof(struct lnet_match_table));
792 if (ptl->ptl_mtables == NULL) {
793 CERROR("Failed to create match table for portal %d\n", index);
797 ptl->ptl_index = index;
798 INIT_LIST_HEAD(&ptl->ptl_msg_delayed);
799 INIT_LIST_HEAD(&ptl->ptl_msg_stealing);
800 spin_lock_init(&ptl->ptl_lock);
801 cfs_percpt_for_each(mtable, i, ptl->ptl_mtables) {
802 /* the extra entry is for MEs with ignore bits */
803 LIBCFS_CPT_ALLOC(mhash, lnet_cpt_table(), i,
804 sizeof(*mhash) * (LNET_MT_HASH_SIZE + 1));
806 CERROR("Failed to create match hash for portal %d\n",
811 memset(&mtable->mt_exhausted[0], -1,
812 sizeof(mtable->mt_exhausted[0]) *
813 LNET_MT_EXHAUSTED_BMAP);
814 mtable->mt_mhash = mhash;
815 for (j = 0; j < LNET_MT_HASH_SIZE + 1; j++)
816 INIT_LIST_HEAD(&mhash[j]);
818 mtable->mt_portal = index;
824 lnet_ptl_cleanup(ptl);
829 lnet_portals_destroy(void)
833 if (the_lnet.ln_portals == NULL)
836 for (i = 0; i < the_lnet.ln_nportals; i++)
837 lnet_ptl_cleanup(the_lnet.ln_portals[i]);
839 cfs_array_free(the_lnet.ln_portals);
840 the_lnet.ln_portals = NULL;
844 lnet_portals_create(void)
849 size = offsetof(struct lnet_portal, ptl_mt_maps[LNET_CPT_NUMBER]);
851 the_lnet.ln_nportals = MAX_PORTALS;
852 the_lnet.ln_portals = cfs_array_alloc(the_lnet.ln_nportals, size);
853 if (the_lnet.ln_portals == NULL) {
854 CERROR("Failed to allocate portals table\n");
858 for (i = 0; i < the_lnet.ln_nportals; i++) {
859 if (lnet_ptl_setup(the_lnet.ln_portals[i], i)) {
860 lnet_portals_destroy();
869 * Turn on the lazy portal attribute. Use with caution!
871 * This portal attribute only affects incoming PUT requests to the portal,
872 * and is off by default. By default, if there's no matching MD for an
873 * incoming PUT request, it is simply dropped. With the lazy attribute on,
874 * such requests are queued indefinitely until either a matching MD is
875 * posted to the portal or the lazy attribute is turned off.
877 * It would prevent dropped requests, however it should be regarded as the
878 * last line of defense - i.e. users must keep a close watch on active
879 * buffers on a lazy portal and once it becomes too low post more buffers as
880 * soon as possible. This is because delayed requests usually have detrimental
881 * effects on underlying network connections. A few delayed requests often
882 * suffice to bring an underlying connection to a complete halt, due to flow
883 * control mechanisms.
885 * There's also a DOS attack risk. If users don't post match-all MDs on a
886 * lazy portal, a malicious peer can easily stop a service by sending some
887 * PUT requests with match bits that won't match any MD. A routed server is
888 * especially vulnerable since the connections to its neighbor routers are
889 * shared among all clients.
891 * \param portal Index of the portal to enable the lazy attribute on.
893 * \retval 0 On success.
894 * \retval -EINVAL If \a portal is not a valid index.
897 LNetSetLazyPortal(int portal)
899 struct lnet_portal *ptl;
901 if (portal < 0 || portal >= the_lnet.ln_nportals)
904 CDEBUG(D_NET, "Setting portal %d lazy\n", portal);
905 ptl = the_lnet.ln_portals[portal];
907 lnet_res_lock(LNET_LOCK_EX);
910 lnet_ptl_setopt(ptl, LNET_PTL_LAZY);
912 lnet_ptl_unlock(ptl);
913 lnet_res_unlock(LNET_LOCK_EX);
917 EXPORT_SYMBOL(LNetSetLazyPortal);
920 lnet_clear_lazy_portal(struct lnet_ni *ni, int portal, char *reason)
922 struct lnet_portal *ptl;
923 struct list_head zombies = LIST_HEAD_INIT(zombies);
925 if (portal < 0 || portal >= the_lnet.ln_nportals)
928 ptl = the_lnet.ln_portals[portal];
930 lnet_res_lock(LNET_LOCK_EX);
933 if (!lnet_ptl_is_lazy(ptl)) {
934 lnet_ptl_unlock(ptl);
935 lnet_res_unlock(LNET_LOCK_EX);
940 struct lnet_msg *msg, *tmp;
942 /* grab all messages which are on the NI passed in */
943 list_for_each_entry_safe(msg, tmp, &ptl->ptl_msg_delayed,
945 if (msg->msg_txni == ni || msg->msg_rxni == ni)
946 list_move(&msg->msg_list, &zombies);
949 if (the_lnet.ln_shutdown)
950 CWARN("Active lazy portal %d on exit\n", portal);
952 CDEBUG(D_NET, "clearing portal %d lazy\n", portal);
954 /* grab all the blocked messages atomically */
955 list_splice_init(&ptl->ptl_msg_delayed, &zombies);
957 lnet_ptl_unsetopt(ptl, LNET_PTL_LAZY);
960 lnet_ptl_unlock(ptl);
961 lnet_res_unlock(LNET_LOCK_EX);
963 lnet_drop_delayed_msg_list(&zombies, reason);
969 * Turn off the lazy portal attribute. Delayed requests on the portal,
970 * if any, will be all dropped when this function returns.
972 * \param portal Index of the portal to disable the lazy attribute on.
974 * \retval 0 On success.
975 * \retval -EINVAL If \a portal is not a valid index.
978 LNetClearLazyPortal(int portal)
980 return lnet_clear_lazy_portal(NULL, portal,
981 "Clearing lazy portal attr");
983 EXPORT_SYMBOL(LNetClearLazyPortal);