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, 2017, Intel Corporation.
27 * This file is part of Lustre, http://www.lustre.org/
31 * portal & match routines
33 * Author: liang@whamcloud.com
36 #define DEBUG_SUBSYSTEM S_LNET
38 #include <lnet/lib-lnet.h>
40 /* NB: add /proc interfaces in upcoming patches */
41 int portal_rotor = LNET_PTL_ROTOR_HASH_RT;
42 module_param(portal_rotor, int, 0644);
43 MODULE_PARM_DESC(portal_rotor, "redirect PUTs to different cpu-partitions");
46 lnet_ptl_match_type(unsigned int index, struct lnet_processid *match_id,
47 __u64 mbits, __u64 ignore_bits)
49 struct lnet_portal *ptl = the_lnet.ln_portals[index];
52 unique = (ignore_bits == 0 &&
53 !LNET_NID_IS_ANY(&match_id->nid) &&
54 match_id->pid != LNET_PID_ANY);
56 LASSERT(!lnet_ptl_is_unique(ptl) || !lnet_ptl_is_wildcard(ptl));
58 /* prefer to check w/o any lock */
59 if (likely(lnet_ptl_is_unique(ptl) || lnet_ptl_is_wildcard(ptl)))
62 /* unset, new portal */
64 /* check again with lock */
65 if (unlikely(lnet_ptl_is_unique(ptl) || lnet_ptl_is_wildcard(ptl))) {
72 lnet_ptl_setopt(ptl, LNET_PTL_MATCH_UNIQUE);
74 lnet_ptl_setopt(ptl, LNET_PTL_MATCH_WILDCARD);
81 if ((lnet_ptl_is_unique(ptl) && !unique) ||
82 (lnet_ptl_is_wildcard(ptl) && unique))
88 lnet_ptl_enable_mt(struct lnet_portal *ptl, int cpt)
90 struct lnet_match_table *mtable = ptl->ptl_mtables[cpt];
93 /* with hold of both lnet_res_lock(cpt) and lnet_ptl_lock */
94 LASSERT(lnet_ptl_is_wildcard(ptl));
96 mtable->mt_enabled = 1;
98 ptl->ptl_mt_maps[ptl->ptl_mt_nmaps] = cpt;
99 for (i = ptl->ptl_mt_nmaps - 1; i >= 0; i--) {
100 LASSERT(ptl->ptl_mt_maps[i] != cpt);
101 if (ptl->ptl_mt_maps[i] < cpt)
105 ptl->ptl_mt_maps[i + 1] = ptl->ptl_mt_maps[i];
106 ptl->ptl_mt_maps[i] = cpt;
113 lnet_ptl_disable_mt(struct lnet_portal *ptl, int cpt)
115 struct lnet_match_table *mtable = ptl->ptl_mtables[cpt];
118 /* with hold of both lnet_res_lock(cpt) and lnet_ptl_lock */
119 LASSERT(lnet_ptl_is_wildcard(ptl));
121 if (LNET_CPT_NUMBER == 1)
122 return; /* never disable the only match-table */
124 mtable->mt_enabled = 0;
126 LASSERT(ptl->ptl_mt_nmaps > 0 &&
127 ptl->ptl_mt_nmaps <= LNET_CPT_NUMBER);
129 /* remove it from mt_maps */
131 for (i = 0; i < ptl->ptl_mt_nmaps; i++) {
132 if (ptl->ptl_mt_maps[i] >= cpt) /* overwrite it */
133 ptl->ptl_mt_maps[i] = ptl->ptl_mt_maps[i + 1];
138 lnet_try_match_md(struct lnet_libmd *md,
139 struct lnet_match_info *info, struct lnet_msg *msg)
141 /* ALWAYS called holding the lnet_res_lock, and can't lnet_res_unlock;
142 * lnet_match_blocked_msg() relies on this to avoid races */
144 unsigned int mlength;
145 struct lnet_me *me = md->md_me;
148 if (lnet_md_exhausted(md))
149 return LNET_MATCHMD_NONE | LNET_MATCHMD_EXHAUSTED;
151 /* mismatched MD op */
152 if ((md->md_options & info->mi_opc) == 0)
153 return LNET_MATCHMD_NONE;
155 /* mismatched ME nid/pid? */
156 if (!LNET_NID_IS_ANY(&me->me_match_id.nid) &&
157 !nid_same(&me->me_match_id.nid, &info->mi_id.nid))
158 return LNET_MATCHMD_NONE;
160 if (me->me_match_id.pid != LNET_PID_ANY &&
161 me->me_match_id.pid != info->mi_id.pid)
162 return LNET_MATCHMD_NONE;
164 /* mismatched ME matchbits? */
165 if (((me->me_match_bits ^ info->mi_mbits) & ~me->me_ignore_bits) != 0)
166 return LNET_MATCHMD_NONE;
168 /* Hurrah! This _is_ a match; check it out... */
170 if ((md->md_options & LNET_MD_MANAGE_REMOTE) == 0)
171 offset = md->md_offset;
173 offset = info->mi_roffset;
175 if ((md->md_options & LNET_MD_MAX_SIZE) != 0) {
176 mlength = md->md_max_size;
177 LASSERT(md->md_offset + mlength <= md->md_length);
179 mlength = md->md_length - offset;
182 if (info->mi_rlength <= mlength) { /* fits in allowed space */
183 mlength = info->mi_rlength;
184 } else if ((md->md_options & LNET_MD_TRUNCATE) == 0) {
185 /* this packet _really_ is too big */
186 CERROR("Matching packet from %s, match %llu"
187 " length %d too big: %d left, %d allowed\n",
188 libcfs_idstr(&info->mi_id), info->mi_mbits,
189 info->mi_rlength, md->md_length - offset, mlength);
191 return LNET_MATCHMD_DROP;
194 /* Commit to this ME/MD */
195 CDEBUG(D_NET, "Incoming %s index %x from %s of "
196 "length %d/%d into md %#llx [%d] + %d\n",
197 (info->mi_opc == LNET_MD_OP_PUT) ? "put" : "get",
198 info->mi_portal, libcfs_idstr(&info->mi_id), mlength,
199 info->mi_rlength, md->md_lh.lh_cookie, md->md_niov, offset);
201 lnet_msg_attach_md(msg, md, offset, mlength);
202 md->md_offset = offset + mlength;
204 if (!lnet_md_exhausted(md))
205 return LNET_MATCHMD_OK;
207 /* Auto-unlink NOW, so the ME gets unlinked if required.
208 * We bumped md->md_refcount above so the MD just gets flagged
209 * for unlink when it is finalized. */
210 if ((md->md_flags & LNET_MD_FLAG_AUTO_UNLINK) != 0)
213 return LNET_MATCHMD_OK | LNET_MATCHMD_EXHAUSTED;
216 static struct lnet_match_table *
217 lnet_match2mt(struct lnet_portal *ptl, struct lnet_processid *id, __u64 mbits)
219 if (LNET_CPT_NUMBER == 1)
220 return ptl->ptl_mtables[0]; /* the only one */
222 /* if it's a unique portal, return match-table hashed by NID */
223 return lnet_ptl_is_unique(ptl) ?
224 ptl->ptl_mtables[lnet_nid2cpt(&id->nid, NULL)] : NULL;
227 struct lnet_match_table *
228 lnet_mt_of_attach(unsigned int index, struct lnet_processid *id,
229 __u64 mbits, __u64 ignore_bits, enum lnet_ins_pos pos)
231 struct lnet_portal *ptl;
232 struct lnet_match_table *mtable;
234 /* NB: called w/o lock */
235 LASSERT(index < the_lnet.ln_nportals);
237 if (!lnet_ptl_match_type(index, id, mbits, ignore_bits))
240 ptl = the_lnet.ln_portals[index];
242 mtable = lnet_match2mt(ptl, id, mbits);
243 if (mtable != NULL) /* unique portal or only one match-table */
246 /* it's a wildcard portal */
250 case LNET_INS_BEFORE:
252 /* posted by no affinity thread, always hash to specific
253 * match-table to avoid buffer stealing which is heavy */
254 return ptl->ptl_mtables[ptl->ptl_index % LNET_CPT_NUMBER];
256 /* posted by cpu-affinity thread */
257 return ptl->ptl_mtables[lnet_cpt_current()];
261 static struct lnet_match_table *
262 lnet_mt_of_match(struct lnet_match_info *info, struct lnet_msg *msg)
264 struct lnet_match_table *mtable;
265 struct lnet_portal *ptl;
271 /* NB: called w/o lock */
272 LASSERT(info->mi_portal < the_lnet.ln_nportals);
273 ptl = the_lnet.ln_portals[info->mi_portal];
275 LASSERT(lnet_ptl_is_wildcard(ptl) || lnet_ptl_is_unique(ptl));
277 mtable = lnet_match2mt(ptl, &info->mi_id, info->mi_mbits);
281 /* it's a wildcard portal */
282 routed = LNET_NID_NET(&msg->msg_hdr.src_nid) !=
283 LNET_NID_NET(&msg->msg_hdr.dest_nid);
285 if (portal_rotor == LNET_PTL_ROTOR_OFF ||
286 (portal_rotor != LNET_PTL_ROTOR_ON && !routed)) {
287 cpt = lnet_cpt_current();
288 if (ptl->ptl_mtables[cpt]->mt_enabled)
289 return ptl->ptl_mtables[cpt];
292 rotor = ptl->ptl_rotor++; /* get round-robin factor */
293 if (portal_rotor == LNET_PTL_ROTOR_HASH_RT && routed)
296 cpt = rotor % LNET_CPT_NUMBER;
298 if (!ptl->ptl_mtables[cpt]->mt_enabled) {
299 /* is there any active entry for this portal? */
300 nmaps = ptl->ptl_mt_nmaps;
301 /* map to an active mtable to avoid heavy "stealing" */
303 /* NB: there is possibility that ptl_mt_maps is being
304 * changed because we are not under protection of
305 * lnet_ptl_lock, but it shouldn't hurt anything */
306 cpt = ptl->ptl_mt_maps[rotor % nmaps];
310 return ptl->ptl_mtables[cpt];
314 lnet_mt_test_exhausted(struct lnet_match_table *mtable, int pos)
319 if (!lnet_ptl_is_wildcard(the_lnet.ln_portals[mtable->mt_portal]))
322 if (pos < 0) { /* check all bits */
323 for (i = 0; i < LNET_MT_EXHAUSTED_BMAP; i++) {
324 if (mtable->mt_exhausted[i] != (__u64)(-1))
330 LASSERT(pos <= LNET_MT_HASH_IGNORE);
331 /* mtable::mt_mhash[pos] is marked as exhausted or not */
332 bmap = &mtable->mt_exhausted[pos >> LNET_MT_BITS_U64];
333 pos &= (1 << LNET_MT_BITS_U64) - 1;
335 return ((*bmap) & (1ULL << pos)) != 0;
339 lnet_mt_set_exhausted(struct lnet_match_table *mtable, int pos, int exhausted)
343 LASSERT(lnet_ptl_is_wildcard(the_lnet.ln_portals[mtable->mt_portal]));
344 LASSERT(pos <= LNET_MT_HASH_IGNORE);
346 /* set mtable::mt_mhash[pos] as exhausted/non-exhausted */
347 bmap = &mtable->mt_exhausted[pos >> LNET_MT_BITS_U64];
348 pos &= (1 << LNET_MT_BITS_U64) - 1;
351 *bmap &= ~(1ULL << pos);
353 *bmap |= 1ULL << pos;
357 lnet_mt_match_head(struct lnet_match_table *mtable,
358 struct lnet_processid *id, __u64 mbits)
360 struct lnet_portal *ptl = the_lnet.ln_portals[mtable->mt_portal];
362 if (lnet_ptl_is_wildcard(ptl)) {
363 return &mtable->mt_mhash[mbits & LNET_MT_HASH_MASK];
365 unsigned long hash = mbits + nidhash(&id->nid) + id->pid;
367 LASSERT(lnet_ptl_is_unique(ptl));
368 hash = hash_long(hash, LNET_MT_HASH_BITS);
369 return &mtable->mt_mhash[hash & LNET_MT_HASH_MASK];
374 lnet_mt_match_md(struct lnet_match_table *mtable,
375 struct lnet_match_info *info, struct lnet_msg *msg)
377 struct list_head *head;
383 /* any ME with ignore bits? */
384 if (!list_empty(&mtable->mt_mhash[LNET_MT_HASH_IGNORE]))
385 head = &mtable->mt_mhash[LNET_MT_HASH_IGNORE];
387 head = lnet_mt_match_head(mtable, &info->mi_id,
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,
421 goto again; /* re-check MEs w/o ignore-bits */
424 if (info->mi_opc == LNET_MD_OP_GET ||
425 !lnet_ptl_is_lazy(the_lnet.ln_portals[info->mi_portal]))
426 return LNET_MATCHMD_DROP | exhausted;
428 return LNET_MATCHMD_NONE | exhausted;
432 lnet_ptl_match_early(struct lnet_portal *ptl, struct lnet_msg *msg)
436 /* message arrived before any buffer posting on this portal,
437 * simply delay or drop this message */
438 if (likely(lnet_ptl_is_wildcard(ptl) || lnet_ptl_is_unique(ptl)))
442 /* check it again with hold of lock */
443 if (lnet_ptl_is_wildcard(ptl) || lnet_ptl_is_unique(ptl)) {
444 lnet_ptl_unlock(ptl);
448 if (lnet_ptl_is_lazy(ptl)) {
449 if (msg->msg_rx_ready_delay) {
450 msg->msg_rx_delayed = 1;
451 list_add_tail(&msg->msg_list,
452 &ptl->ptl_msg_delayed);
454 rc = LNET_MATCHMD_NONE;
456 rc = LNET_MATCHMD_DROP;
459 lnet_ptl_unlock(ptl);
464 lnet_ptl_match_delay(struct lnet_portal *ptl,
465 struct lnet_match_info *info, struct lnet_msg *msg)
467 int first = ptl->ptl_mt_maps[0]; /* read w/o lock */
472 * Steal buffer from other CPTs, and delay msg if nothing to
473 * steal. This function is more expensive than a regular
474 * match, but we don't expect it can happen a lot. The return
475 * code contains one of LNET_MATCHMD_OK, LNET_MATCHMD_DROP, or
478 LASSERT(lnet_ptl_is_wildcard(ptl));
480 for (i = 0; i < LNET_CPT_NUMBER; i++) {
481 struct lnet_match_table *mtable;
484 cpt = (first + i) % LNET_CPT_NUMBER;
485 mtable = ptl->ptl_mtables[cpt];
486 if (i != 0 && i != LNET_CPT_NUMBER - 1 && !mtable->mt_enabled)
493 /* The first try, add to stealing list. */
494 list_add_tail(&msg->msg_list,
495 &ptl->ptl_msg_stealing);
498 if (!list_empty(&msg->msg_list)) {
499 /* On stealing list. */
500 rc = lnet_mt_match_md(mtable, info, msg);
502 if ((rc & LNET_MATCHMD_EXHAUSTED) != 0 &&
504 lnet_ptl_disable_mt(ptl, cpt);
506 if ((rc & LNET_MATCHMD_FINISH) != 0) {
507 /* Match found, remove from stealing list. */
508 list_del_init(&msg->msg_list);
509 } else if (i == LNET_CPT_NUMBER - 1 || /* (1) */
510 ptl->ptl_mt_nmaps == 0 || /* (2) */
511 (ptl->ptl_mt_nmaps == 1 && /* (3) */
512 ptl->ptl_mt_maps[0] == cpt)) {
514 * No match found, and this is either
515 * (1) the last cpt to check, or
516 * (2) there is no active cpt, or
517 * (3) this is the only active cpt.
518 * There is nothing to steal: delay or
521 list_del_init(&msg->msg_list);
523 if (lnet_ptl_is_lazy(ptl)) {
524 msg->msg_rx_delayed = 1;
525 list_add_tail(&msg->msg_list,
526 &ptl->ptl_msg_delayed);
527 rc = LNET_MATCHMD_NONE;
529 rc = LNET_MATCHMD_DROP;
532 /* Do another iteration. */
537 * No longer on stealing list: another thread
538 * matched the message in lnet_ptl_attach_md().
539 * We are now expected to handle the message.
541 rc = msg->msg_md == NULL ?
542 LNET_MATCHMD_DROP : LNET_MATCHMD_OK;
545 lnet_ptl_unlock(ptl);
546 lnet_res_unlock(cpt);
549 * Note that test (1) above ensures that we always
550 * exit the loop through this break statement.
552 * LNET_MATCHMD_NONE means msg was added to the
553 * delayed queue, and we may no longer reference it
554 * after lnet_ptl_unlock() and lnet_res_unlock().
556 if (rc & (LNET_MATCHMD_FINISH | LNET_MATCHMD_NONE))
564 lnet_ptl_match_md(struct lnet_match_info *info, struct lnet_msg *msg)
566 struct lnet_match_table *mtable;
567 struct lnet_portal *ptl;
571 "Request from %s of length %d into portal %d MB=%#llx\n",
572 libcfs_idstr(&info->mi_id),
573 info->mi_rlength, info->mi_portal, info->mi_mbits);
575 if (info->mi_portal >= the_lnet.ln_nportals) {
576 CERROR("Invalid portal %d not in [0-%d]\n",
577 info->mi_portal, the_lnet.ln_nportals);
578 return LNET_MATCHMD_DROP;
581 ptl = the_lnet.ln_portals[info->mi_portal];
582 rc = lnet_ptl_match_early(ptl, msg);
583 if (rc != 0) /* matched or delayed early message */
586 mtable = lnet_mt_of_match(info, msg);
587 lnet_res_lock(mtable->mt_cpt);
589 if (the_lnet.ln_state != LNET_STATE_RUNNING) {
590 rc = LNET_MATCHMD_DROP;
594 rc = lnet_mt_match_md(mtable, info, msg);
595 if ((rc & LNET_MATCHMD_EXHAUSTED) != 0 && mtable->mt_enabled) {
597 lnet_ptl_disable_mt(ptl, mtable->mt_cpt);
598 lnet_ptl_unlock(ptl);
601 if ((rc & LNET_MATCHMD_FINISH) != 0) /* matched or dropping */
604 if (!msg->msg_rx_ready_delay)
607 LASSERT(lnet_ptl_is_lazy(ptl));
608 LASSERT(!msg->msg_rx_delayed);
610 /* NB: we don't expect "delay" can happen a lot */
611 if (lnet_ptl_is_unique(ptl) || LNET_CPT_NUMBER == 1) {
614 msg->msg_rx_delayed = 1;
615 list_add_tail(&msg->msg_list, &ptl->ptl_msg_delayed);
617 lnet_ptl_unlock(ptl);
618 lnet_res_unlock(mtable->mt_cpt);
619 rc = LNET_MATCHMD_NONE;
621 lnet_res_unlock(mtable->mt_cpt);
622 rc = lnet_ptl_match_delay(ptl, info, msg);
625 /* LNET_MATCHMD_NONE means msg was added to the delay queue */
626 if (rc & LNET_MATCHMD_NONE) {
628 "Delaying %s from %s ptl %d MB %#llx off %d len %d\n",
629 info->mi_opc == LNET_MD_OP_PUT ? "PUT" : "GET",
630 libcfs_idstr(&info->mi_id), info->mi_portal,
631 info->mi_mbits, info->mi_roffset, info->mi_rlength);
635 lnet_res_unlock(mtable->mt_cpt);
637 /* EXHAUSTED bit is only meaningful for internal functions */
638 return rc & ~LNET_MATCHMD_EXHAUSTED;
642 lnet_ptl_detach_md(struct lnet_me *me, struct lnet_libmd *md)
644 LASSERT(me->me_md == md && md->md_me == me);
650 /* called with lnet_res_lock held */
652 lnet_ptl_attach_md(struct lnet_me *me, struct lnet_libmd *md,
653 struct list_head *matches, struct list_head *drops)
655 struct lnet_portal *ptl = the_lnet.ln_portals[me->me_portal];
656 struct lnet_match_table *mtable;
657 struct list_head *head;
658 struct lnet_msg *tmp;
659 struct lnet_msg *msg;
663 LASSERT(md->md_refcount == 0); /* a brand new MD */
668 cpt = lnet_cpt_of_cookie(md->md_lh.lh_cookie);
669 mtable = ptl->ptl_mtables[cpt];
671 if (list_empty(&ptl->ptl_msg_stealing) &&
672 list_empty(&ptl->ptl_msg_delayed) &&
673 !lnet_mt_test_exhausted(mtable, me->me_pos))
677 head = &ptl->ptl_msg_stealing;
679 list_for_each_entry_safe(msg, tmp, head, msg_list) {
680 struct lnet_match_info info;
681 struct lnet_hdr *hdr;
684 LASSERT(msg->msg_rx_delayed || head == &ptl->ptl_msg_stealing);
687 /* Multi-Rail: Primary peer NID */
688 info.mi_id.nid = msg->msg_initiator;
689 info.mi_id.pid = hdr->src_pid;
690 info.mi_opc = LNET_MD_OP_PUT;
691 info.mi_portal = hdr->msg.put.ptl_index;
692 info.mi_rlength = hdr->payload_length;
693 info.mi_roffset = hdr->msg.put.offset;
694 info.mi_mbits = hdr->msg.put.match_bits;
696 rc = lnet_try_match_md(md, &info, msg);
698 exhausted = (rc & LNET_MATCHMD_EXHAUSTED) != 0;
699 if ((rc & LNET_MATCHMD_NONE) != 0) {
705 /* Hurrah! This _is_ a match */
706 LASSERT((rc & LNET_MATCHMD_FINISH) != 0);
707 list_del_init(&msg->msg_list);
709 if (head == &ptl->ptl_msg_stealing) {
712 /* stealing thread will handle the message */
716 if ((rc & LNET_MATCHMD_OK) != 0) {
717 list_add_tail(&msg->msg_list, matches);
719 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
720 "match %llu offset %d length %d.\n",
721 libcfs_idstr(&info.mi_id),
722 info.mi_portal, info.mi_mbits,
723 info.mi_roffset, info.mi_rlength);
725 list_add_tail(&msg->msg_list, drops);
732 if (!exhausted && head == &ptl->ptl_msg_stealing) {
733 head = &ptl->ptl_msg_delayed;
737 if (lnet_ptl_is_wildcard(ptl) && !exhausted) {
738 lnet_mt_set_exhausted(mtable, me->me_pos, 0);
739 if (!mtable->mt_enabled)
740 lnet_ptl_enable_mt(ptl, cpt);
743 lnet_ptl_unlock(ptl);
747 lnet_ptl_cleanup(struct lnet_portal *ptl)
749 struct lnet_match_table *mtable;
752 if (ptl->ptl_mtables == NULL) /* uninitialized portal */
755 LASSERT(list_empty(&ptl->ptl_msg_delayed));
756 LASSERT(list_empty(&ptl->ptl_msg_stealing));
757 cfs_percpt_for_each(mtable, i, ptl->ptl_mtables) {
758 struct list_head *mhash;
762 if (mtable->mt_mhash == NULL) /* uninitialized match-table */
765 mhash = mtable->mt_mhash;
767 for (j = 0; j < LNET_MT_HASH_SIZE + 1; j++) {
768 while (!list_empty(&mhash[j])) {
769 me = list_entry(mhash[j].next,
770 struct lnet_me, me_list);
771 CERROR("Active ME %p on exit\n", me);
772 list_del(&me->me_list);
774 "slab-freed 'me' at %p in cleanup.\n",
776 kmem_cache_free(lnet_mes_cachep, me);
779 /* the extra entry is for MEs with ignore bits */
780 CFS_FREE_PTR_ARRAY(mhash, LNET_MT_HASH_SIZE + 1);
783 cfs_percpt_free(ptl->ptl_mtables);
784 ptl->ptl_mtables = NULL;
788 lnet_ptl_setup(struct lnet_portal *ptl, int index)
790 struct lnet_match_table *mtable;
791 struct list_head *mhash;
795 ptl->ptl_mtables = cfs_percpt_alloc(lnet_cpt_table(),
796 sizeof(struct lnet_match_table));
797 if (ptl->ptl_mtables == NULL) {
798 CERROR("Failed to create match table for portal %d\n", index);
802 ptl->ptl_index = index;
803 INIT_LIST_HEAD(&ptl->ptl_msg_delayed);
804 INIT_LIST_HEAD(&ptl->ptl_msg_stealing);
805 spin_lock_init(&ptl->ptl_lock);
806 cfs_percpt_for_each(mtable, i, ptl->ptl_mtables) {
807 /* the extra entry is for MEs with ignore bits */
808 LIBCFS_CPT_ALLOC(mhash, lnet_cpt_table(), i,
809 sizeof(*mhash) * (LNET_MT_HASH_SIZE + 1));
811 CERROR("Failed to create match hash for portal %d\n",
816 memset(&mtable->mt_exhausted[0], -1,
817 sizeof(mtable->mt_exhausted[0]) *
818 LNET_MT_EXHAUSTED_BMAP);
819 mtable->mt_mhash = mhash;
820 for (j = 0; j < LNET_MT_HASH_SIZE + 1; j++)
821 INIT_LIST_HEAD(&mhash[j]);
823 mtable->mt_portal = index;
829 lnet_ptl_cleanup(ptl);
833 #define PORTAL_SIZE (offsetof(struct lnet_portal, ptl_mt_maps[LNET_CPT_NUMBER]))
835 lnet_portals_destroy(void)
839 if (the_lnet.ln_portals == NULL)
842 for (i = 0; i < the_lnet.ln_nportals; i++)
843 if (the_lnet.ln_portals[i]) {
844 lnet_ptl_cleanup(the_lnet.ln_portals[i]);
845 LIBCFS_FREE(the_lnet.ln_portals[i], PORTAL_SIZE);
848 CFS_FREE_PTR_ARRAY(the_lnet.ln_portals, the_lnet.ln_nportals);
849 the_lnet.ln_portals = NULL;
853 lnet_portals_create(void)
857 the_lnet.ln_nportals = MAX_PORTALS;
858 CFS_ALLOC_PTR_ARRAY(the_lnet.ln_portals, the_lnet.ln_nportals);
859 if (the_lnet.ln_portals == NULL) {
860 CERROR("Failed to allocate portals table\n");
864 for (i = 0; i < the_lnet.ln_nportals; i++) {
865 LIBCFS_ALLOC(the_lnet.ln_portals[i], PORTAL_SIZE);
866 if (!the_lnet.ln_portals[i] ||
867 lnet_ptl_setup(the_lnet.ln_portals[i], i)) {
868 lnet_portals_destroy();
877 * Turn on the lazy portal attribute. Use with caution!
879 * This portal attribute only affects incoming PUT requests to the portal,
880 * and is off by default. By default, if there's no matching MD for an
881 * incoming PUT request, it is simply dropped. With the lazy attribute on,
882 * such requests are queued indefinitely until either a matching MD is
883 * posted to the portal or the lazy attribute is turned off.
885 * It would prevent dropped requests, however it should be regarded as the
886 * last line of defense - i.e. users must keep a close watch on active
887 * buffers on a lazy portal and once it becomes too low post more buffers as
888 * soon as possible. This is because delayed requests usually have detrimental
889 * effects on underlying network connections. A few delayed requests often
890 * suffice to bring an underlying connection to a complete halt, due to flow
891 * control mechanisms.
893 * There's also a DOS attack risk. If users don't post match-all MDs on a
894 * lazy portal, a malicious peer can easily stop a service by sending some
895 * PUT requests with match bits that won't match any MD. A routed server is
896 * especially vulnerable since the connections to its neighbor routers are
897 * shared among all clients.
899 * \param portal Index of the portal to enable the lazy attribute on.
901 * \retval 0 On success.
902 * \retval -EINVAL If \a portal is not a valid index.
905 LNetSetLazyPortal(int portal)
907 struct lnet_portal *ptl;
909 if (portal < 0 || portal >= the_lnet.ln_nportals)
912 CDEBUG(D_NET, "Setting portal %d lazy\n", portal);
913 ptl = the_lnet.ln_portals[portal];
915 lnet_res_lock(LNET_LOCK_EX);
918 lnet_ptl_setopt(ptl, LNET_PTL_LAZY);
920 lnet_ptl_unlock(ptl);
921 lnet_res_unlock(LNET_LOCK_EX);
925 EXPORT_SYMBOL(LNetSetLazyPortal);
928 lnet_clear_lazy_portal(struct lnet_ni *ni, int portal, char *reason)
930 struct lnet_portal *ptl;
933 if (portal < 0 || portal >= the_lnet.ln_nportals)
936 ptl = the_lnet.ln_portals[portal];
938 lnet_res_lock(LNET_LOCK_EX);
941 if (!lnet_ptl_is_lazy(ptl)) {
942 lnet_ptl_unlock(ptl);
943 lnet_res_unlock(LNET_LOCK_EX);
948 struct lnet_msg *msg, *tmp;
950 /* grab all messages which are on the NI passed in */
951 list_for_each_entry_safe(msg, tmp, &ptl->ptl_msg_delayed,
953 if (msg->msg_txni == ni || msg->msg_rxni == ni)
954 list_move(&msg->msg_list, &zombies);
957 if (the_lnet.ln_state != LNET_STATE_RUNNING)
958 CWARN("Active lazy portal %d on exit\n", portal);
960 CDEBUG(D_NET, "clearing portal %d lazy\n", portal);
962 /* grab all the blocked messages atomically */
963 list_splice_init(&ptl->ptl_msg_delayed, &zombies);
965 lnet_ptl_unsetopt(ptl, LNET_PTL_LAZY);
968 lnet_ptl_unlock(ptl);
969 lnet_res_unlock(LNET_LOCK_EX);
971 lnet_drop_delayed_msg_list(&zombies, reason);
977 * Turn off the lazy portal attribute. Delayed requests on the portal,
978 * if any, will be all dropped when this function returns.
980 * \param portal Index of the portal to disable the lazy attribute on.
982 * \retval 0 On success.
983 * \retval -EINVAL If \a portal is not a valid index.
986 LNetClearLazyPortal(int portal)
988 return lnet_clear_lazy_portal(NULL, portal,
989 "Clearing lazy portal attr");
991 EXPORT_SYMBOL(LNetClearLazyPortal);