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, 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 "../../include/linux/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 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);
191 return LNET_MATCHMD_DROP;
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);
200 lnet_msg_attach_md(msg, md, offset, mlength);
201 md->md_offset = offset + mlength;
203 if (!lnet_md_exhausted(md))
204 return LNET_MATCHMD_OK;
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)
212 return LNET_MATCHMD_OK | LNET_MATCHMD_EXHAUSTED;
215 static struct lnet_match_table *
216 lnet_match2mt(struct lnet_portal *ptl, lnet_process_id_t id, __u64 mbits)
218 if (LNET_CPT_NUMBER == 1)
219 return ptl->ptl_mtables[0]; /* the only one */
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;
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)
230 struct lnet_portal *ptl;
231 struct lnet_match_table *mtable;
233 /* NB: called w/o lock */
234 LASSERT(index < the_lnet.ln_nportals);
236 if (!lnet_ptl_match_type(index, id, mbits, ignore_bits))
239 ptl = the_lnet.ln_portals[index];
241 mtable = lnet_match2mt(ptl, id, mbits);
242 if (mtable != NULL) /* unique portal or only one match-table */
245 /* it's a wildcard portal */
249 case LNET_INS_BEFORE:
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];
255 /* posted by cpu-affinity thread */
256 return ptl->ptl_mtables[lnet_cpt_current()];
260 static struct lnet_match_table *
261 lnet_mt_of_match(struct lnet_match_info *info, struct lnet_msg *msg)
263 struct lnet_match_table *mtable;
264 struct lnet_portal *ptl;
270 /* NB: called w/o lock */
271 LASSERT(info->mi_portal < the_lnet.ln_nportals);
272 ptl = the_lnet.ln_portals[info->mi_portal];
274 LASSERT(lnet_ptl_is_wildcard(ptl) || lnet_ptl_is_unique(ptl));
276 mtable = lnet_match2mt(ptl, info->mi_id, info->mi_mbits);
280 /* it's a wildcard portal */
281 routed = LNET_NIDNET(msg->msg_hdr.src_nid) !=
282 LNET_NIDNET(msg->msg_hdr.dest_nid);
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];
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);
295 cpt = rotor % LNET_CPT_NUMBER;
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" */
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];
309 return ptl->ptl_mtables[cpt];
313 lnet_mt_test_exhausted(struct lnet_match_table *mtable, int pos)
318 if (!lnet_ptl_is_wildcard(the_lnet.ln_portals[mtable->mt_portal]))
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))
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;
334 return ((*bmap) & (1ULL << pos)) != 0;
338 lnet_mt_set_exhausted(struct lnet_match_table *mtable, int pos, int exhausted)
342 LASSERT(lnet_ptl_is_wildcard(the_lnet.ln_portals[mtable->mt_portal]));
343 LASSERT(pos <= LNET_MT_HASH_IGNORE);
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;
350 *bmap &= ~(1ULL << pos);
352 *bmap |= 1ULL << pos;
356 lnet_mt_match_head(struct lnet_match_table *mtable,
357 lnet_process_id_t id, __u64 mbits)
359 struct lnet_portal *ptl = the_lnet.ln_portals[mtable->mt_portal];
361 if (lnet_ptl_is_wildcard(ptl)) {
362 return &mtable->mt_mhash[mbits & LNET_MT_HASH_MASK];
364 unsigned long hash = mbits + id.nid + id.pid;
366 LASSERT(lnet_ptl_is_unique(ptl));
367 hash = hash_long(hash, LNET_MT_HASH_BITS);
368 return &mtable->mt_mhash[hash];
373 lnet_mt_match_md(struct lnet_match_table *mtable,
374 struct lnet_match_info *info, struct lnet_msg *msg)
376 struct list_head *head;
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];
386 head = lnet_mt_match_head(mtable, info->mi_id, info->mi_mbits);
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;
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)
397 LASSERT(me == me->me_md->md_me);
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 */
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;
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))
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 */
421 if (info->mi_opc == LNET_MD_OP_GET ||
422 !lnet_ptl_is_lazy(the_lnet.ln_portals[info->mi_portal]))
423 return exhausted | LNET_MATCHMD_DROP;
425 return exhausted | LNET_MATCHMD_NONE;
429 lnet_ptl_match_early(struct lnet_portal *ptl, struct lnet_msg *msg)
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)))
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);
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);
451 rc = LNET_MATCHMD_NONE;
453 rc = LNET_MATCHMD_DROP;
456 lnet_ptl_unlock(ptl);
461 lnet_ptl_match_delay(struct lnet_portal *ptl,
462 struct lnet_match_info *info, struct lnet_msg *msg)
464 int first = ptl->ptl_mt_maps[0]; /* read w/o lock */
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));
473 for (i = 0; i < LNET_CPT_NUMBER; i++) {
474 struct lnet_match_table *mtable;
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)
485 if (i == 0) { /* the first try, attach on stealing list */
486 list_add_tail(&msg->msg_list,
487 &ptl->ptl_msg_stealing);
490 if (!list_empty(&msg->msg_list)) { /* on stealing list */
491 rc = lnet_mt_match_md(mtable, info, msg);
493 if ((rc & LNET_MATCHMD_EXHAUSTED) != 0 &&
495 lnet_ptl_disable_mt(ptl, cpt);
497 if ((rc & LNET_MATCHMD_FINISH) != 0)
498 list_del_init(&msg->msg_list);
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;
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);
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;
521 rc = LNET_MATCHMD_DROP;
525 lnet_ptl_unlock(ptl);
526 lnet_res_unlock(cpt);
528 if ((rc & LNET_MATCHMD_FINISH) != 0 || msg->msg_rx_delayed)
536 lnet_ptl_match_md(struct lnet_match_info *info, struct lnet_msg *msg)
538 struct lnet_match_table *mtable;
539 struct lnet_portal *ptl;
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,
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;
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 */
557 mtable = lnet_mt_of_match(info, msg);
558 lnet_res_lock(mtable->mt_cpt);
560 if (the_lnet.ln_shutdown) {
561 rc = LNET_MATCHMD_DROP;
565 rc = lnet_mt_match_md(mtable, info, msg);
566 if ((rc & LNET_MATCHMD_EXHAUSTED) != 0 && mtable->mt_enabled) {
568 lnet_ptl_disable_mt(ptl, mtable->mt_cpt);
569 lnet_ptl_unlock(ptl);
572 if ((rc & LNET_MATCHMD_FINISH) != 0) /* matched or dropping */
575 if (!msg->msg_rx_ready_delay)
578 LASSERT(lnet_ptl_is_lazy(ptl));
579 LASSERT(!msg->msg_rx_delayed);
581 /* NB: we don't expect "delay" can happen a lot */
582 if (lnet_ptl_is_unique(ptl) || LNET_CPT_NUMBER == 1) {
585 msg->msg_rx_delayed = 1;
586 list_add_tail(&msg->msg_list, &ptl->ptl_msg_delayed);
588 lnet_ptl_unlock(ptl);
589 lnet_res_unlock(mtable->mt_cpt);
592 lnet_res_unlock(mtable->mt_cpt);
593 rc = lnet_ptl_match_delay(ptl, info, msg);
596 if (msg->msg_rx_delayed) {
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);
605 lnet_res_unlock(mtable->mt_cpt);
607 /* EXHAUSTED bit is only meaningful for internal functions */
608 return rc & ~LNET_MATCHMD_EXHAUSTED;
612 lnet_ptl_detach_md(lnet_me_t *me, lnet_libmd_t *md)
614 LASSERT(me->me_md == md && md->md_me == me);
620 /* called with lnet_res_lock held */
622 lnet_ptl_attach_md(lnet_me_t *me, lnet_libmd_t *md,
623 struct list_head *matches, struct list_head *drops)
625 struct lnet_portal *ptl = the_lnet.ln_portals[me->me_portal];
626 struct lnet_match_table *mtable;
627 struct list_head *head;
633 LASSERT(md->md_refcount == 0); /* a brand new MD */
638 cpt = lnet_cpt_of_cookie(md->md_lh.lh_cookie);
639 mtable = ptl->ptl_mtables[cpt];
641 if (list_empty(&ptl->ptl_msg_stealing) &&
642 list_empty(&ptl->ptl_msg_delayed) &&
643 !lnet_mt_test_exhausted(mtable, me->me_pos))
647 head = &ptl->ptl_msg_stealing;
649 list_for_each_entry_safe(msg, tmp, head, msg_list) {
650 struct lnet_match_info info;
654 LASSERT(msg->msg_rx_delayed || head == &ptl->ptl_msg_stealing);
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;
665 rc = lnet_try_match_md(md, &info, msg);
667 exhausted = (rc & LNET_MATCHMD_EXHAUSTED) != 0;
668 if ((rc & LNET_MATCHMD_NONE) != 0) {
674 /* Hurrah! This _is_ a match */
675 LASSERT((rc & LNET_MATCHMD_FINISH) != 0);
676 list_del_init(&msg->msg_list);
678 if (head == &ptl->ptl_msg_stealing) {
681 /* stealing thread will handle the message */
685 if ((rc & LNET_MATCHMD_OK) != 0) {
686 list_add_tail(&msg->msg_list, matches);
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);
693 list_add_tail(&msg->msg_list, drops);
700 if (!exhausted && head == &ptl->ptl_msg_stealing) {
701 head = &ptl->ptl_msg_delayed;
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);
711 lnet_ptl_unlock(ptl);
715 lnet_ptl_cleanup(struct lnet_portal *ptl)
717 struct lnet_match_table *mtable;
720 if (ptl->ptl_mtables == NULL) /* uninitialized portal */
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;
730 if (mtable->mt_mhash == NULL) /* uninitialized match-table */
733 mhash = mtable->mt_mhash;
735 for (j = 0; j < LNET_MT_HASH_SIZE + 1; j++) {
736 while (!list_empty(&mhash[j])) {
737 me = list_entry(mhash[j].next,
739 CERROR("Active ME %p on exit\n", me);
740 list_del(&me->me_list);
744 /* the extra entry is for MEs with ignore bits */
745 LIBCFS_FREE(mhash, sizeof(*mhash) * (LNET_MT_HASH_SIZE + 1));
748 cfs_percpt_free(ptl->ptl_mtables);
749 ptl->ptl_mtables = NULL;
753 lnet_ptl_setup(struct lnet_portal *ptl, int index)
755 struct lnet_match_table *mtable;
756 struct list_head *mhash;
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);
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));
776 CERROR("Failed to create match hash for portal %d\n",
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]);
788 mtable->mt_portal = index;
794 lnet_ptl_cleanup(ptl);
799 lnet_portals_destroy(void)
803 if (the_lnet.ln_portals == NULL)
806 for (i = 0; i < the_lnet.ln_nportals; i++)
807 lnet_ptl_cleanup(the_lnet.ln_portals[i]);
809 cfs_array_free(the_lnet.ln_portals);
810 the_lnet.ln_portals = NULL;
814 lnet_portals_create(void)
819 size = offsetof(struct lnet_portal, ptl_mt_maps[LNET_CPT_NUMBER]);
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");
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();
839 * Turn on the lazy portal attribute. Use with caution!
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.
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.
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.
861 * \param portal Index of the portal to enable the lazy attribute on.
863 * \retval 0 On success.
864 * \retval -EINVAL If \a portal is not a valid index.
867 LNetSetLazyPortal(int portal)
869 struct lnet_portal *ptl;
871 if (portal < 0 || portal >= the_lnet.ln_nportals)
874 CDEBUG(D_NET, "Setting portal %d lazy\n", portal);
875 ptl = the_lnet.ln_portals[portal];
877 lnet_res_lock(LNET_LOCK_EX);
880 lnet_ptl_setopt(ptl, LNET_PTL_LAZY);
882 lnet_ptl_unlock(ptl);
883 lnet_res_unlock(LNET_LOCK_EX);
887 EXPORT_SYMBOL(LNetSetLazyPortal);
890 * Turn off the lazy portal attribute. Delayed requests on the portal,
891 * if any, will be all dropped when this function returns.
893 * \param portal Index of the portal to disable the lazy attribute on.
895 * \retval 0 On success.
896 * \retval -EINVAL If \a portal is not a valid index.
899 LNetClearLazyPortal(int portal)
901 struct lnet_portal *ptl;
904 if (portal < 0 || portal >= the_lnet.ln_nportals)
907 ptl = the_lnet.ln_portals[portal];
909 lnet_res_lock(LNET_LOCK_EX);
912 if (!lnet_ptl_is_lazy(ptl)) {
913 lnet_ptl_unlock(ptl);
914 lnet_res_unlock(LNET_LOCK_EX);
918 if (the_lnet.ln_shutdown)
919 CWARN("Active lazy portal %d on exit\n", portal);
921 CDEBUG(D_NET, "clearing portal %d lazy\n", portal);
923 /* grab all the blocked messages atomically */
924 list_splice_init(&ptl->ptl_msg_delayed, &zombies);
926 lnet_ptl_unsetopt(ptl, LNET_PTL_LAZY);
928 lnet_ptl_unlock(ptl);
929 lnet_res_unlock(LNET_LOCK_EX);
931 lnet_drop_delayed_msg_list(&zombies, "Clearing lazy portal attr");
935 EXPORT_SYMBOL(LNetClearLazyPortal);