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 "../../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
&&
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
)
143 * ALWAYS called holding the lnet_res_lock, and can't lnet_res_unlock;
144 * lnet_match_blocked_msg() relies on this to avoid races
147 unsigned int mlength
;
148 lnet_me_t
*me
= md
->md_me
;
151 if (lnet_md_exhausted(md
))
152 return LNET_MATCHMD_NONE
| LNET_MATCHMD_EXHAUSTED
;
154 /* mismatched MD op */
155 if (!(md
->md_options
& info
->mi_opc
))
156 return LNET_MATCHMD_NONE
;
158 /* mismatched ME nid/pid? */
159 if (me
->me_match_id
.nid
!= LNET_NID_ANY
&&
160 me
->me_match_id
.nid
!= info
->mi_id
.nid
)
161 return LNET_MATCHMD_NONE
;
163 if (me
->me_match_id
.pid
!= LNET_PID_ANY
&&
164 me
->me_match_id
.pid
!= info
->mi_id
.pid
)
165 return LNET_MATCHMD_NONE
;
167 /* mismatched ME matchbits? */
168 if ((me
->me_match_bits
^ info
->mi_mbits
) & ~me
->me_ignore_bits
)
169 return LNET_MATCHMD_NONE
;
171 /* Hurrah! This _is_ a match; check it out... */
173 if (!(md
->md_options
& LNET_MD_MANAGE_REMOTE
))
174 offset
= md
->md_offset
;
176 offset
= info
->mi_roffset
;
178 if (md
->md_options
& LNET_MD_MAX_SIZE
) {
179 mlength
= md
->md_max_size
;
180 LASSERT(md
->md_offset
+ mlength
<= md
->md_length
);
182 mlength
= md
->md_length
- offset
;
185 if (info
->mi_rlength
<= mlength
) { /* fits in allowed space */
186 mlength
= info
->mi_rlength
;
187 } else if (!(md
->md_options
& LNET_MD_TRUNCATE
)) {
188 /* this packet _really_ is too big */
189 CERROR("Matching packet from %s, match %llu length %d too big: %d left, %d allowed\n",
190 libcfs_id2str(info
->mi_id
), info
->mi_mbits
,
191 info
->mi_rlength
, md
->md_length
- offset
, mlength
);
193 return LNET_MATCHMD_DROP
;
196 /* Commit to this ME/MD */
197 CDEBUG(D_NET
, "Incoming %s index %x from %s of 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
;
209 * Auto-unlink NOW, so the ME gets unlinked if required.
210 * We bumped md->md_refcount above so the MD just gets flagged
211 * for unlink when it is finalized.
213 if (md
->md_flags
& LNET_MD_FLAG_AUTO_UNLINK
)
216 return LNET_MATCHMD_OK
| LNET_MATCHMD_EXHAUSTED
;
219 static struct lnet_match_table
*
220 lnet_match2mt(struct lnet_portal
*ptl
, lnet_process_id_t id
, __u64 mbits
)
222 if (LNET_CPT_NUMBER
== 1)
223 return ptl
->ptl_mtables
[0]; /* the only one */
225 /* if it's a unique portal, return match-table hashed by NID */
226 return lnet_ptl_is_unique(ptl
) ?
227 ptl
->ptl_mtables
[lnet_cpt_of_nid(id
.nid
)] : NULL
;
230 struct lnet_match_table
*
231 lnet_mt_of_attach(unsigned int index
, lnet_process_id_t id
,
232 __u64 mbits
, __u64 ignore_bits
, lnet_ins_pos_t pos
)
234 struct lnet_portal
*ptl
;
235 struct lnet_match_table
*mtable
;
237 /* NB: called w/o lock */
238 LASSERT(index
< the_lnet
.ln_nportals
);
240 if (!lnet_ptl_match_type(index
, id
, mbits
, ignore_bits
))
243 ptl
= the_lnet
.ln_portals
[index
];
245 mtable
= lnet_match2mt(ptl
, id
, mbits
);
246 if (mtable
) /* unique portal or only one match-table */
249 /* it's a wildcard portal */
253 case LNET_INS_BEFORE
:
256 * posted by no affinity thread, always hash to specific
257 * match-table to avoid buffer stealing which is heavy
259 return ptl
->ptl_mtables
[ptl
->ptl_index
% LNET_CPT_NUMBER
];
261 /* posted by cpu-affinity thread */
262 return ptl
->ptl_mtables
[lnet_cpt_current()];
266 static struct lnet_match_table
*
267 lnet_mt_of_match(struct lnet_match_info
*info
, struct lnet_msg
*msg
)
269 struct lnet_match_table
*mtable
;
270 struct lnet_portal
*ptl
;
276 /* NB: called w/o lock */
277 LASSERT(info
->mi_portal
< the_lnet
.ln_nportals
);
278 ptl
= the_lnet
.ln_portals
[info
->mi_portal
];
280 LASSERT(lnet_ptl_is_wildcard(ptl
) || lnet_ptl_is_unique(ptl
));
282 mtable
= lnet_match2mt(ptl
, info
->mi_id
, info
->mi_mbits
);
286 /* it's a wildcard portal */
287 routed
= LNET_NIDNET(msg
->msg_hdr
.src_nid
) !=
288 LNET_NIDNET(msg
->msg_hdr
.dest_nid
);
290 if (portal_rotor
== LNET_PTL_ROTOR_OFF
||
291 (portal_rotor
!= LNET_PTL_ROTOR_ON
&& !routed
)) {
292 cpt
= lnet_cpt_current();
293 if (ptl
->ptl_mtables
[cpt
]->mt_enabled
)
294 return ptl
->ptl_mtables
[cpt
];
297 rotor
= ptl
->ptl_rotor
++; /* get round-robin factor */
298 if (portal_rotor
== LNET_PTL_ROTOR_HASH_RT
&& routed
)
299 cpt
= lnet_cpt_of_nid(msg
->msg_hdr
.src_nid
);
301 cpt
= rotor
% LNET_CPT_NUMBER
;
303 if (!ptl
->ptl_mtables
[cpt
]->mt_enabled
) {
304 /* is there any active entry for this portal? */
305 nmaps
= ptl
->ptl_mt_nmaps
;
306 /* map to an active mtable to avoid heavy "stealing" */
309 * NB: there is possibility that ptl_mt_maps is being
310 * changed because we are not under protection of
311 * lnet_ptl_lock, but it shouldn't hurt anything
313 cpt
= ptl
->ptl_mt_maps
[rotor
% nmaps
];
317 return ptl
->ptl_mtables
[cpt
];
321 lnet_mt_test_exhausted(struct lnet_match_table
*mtable
, int pos
)
326 if (!lnet_ptl_is_wildcard(the_lnet
.ln_portals
[mtable
->mt_portal
]))
329 if (pos
< 0) { /* check all bits */
330 for (i
= 0; i
< LNET_MT_EXHAUSTED_BMAP
; i
++) {
331 if (mtable
->mt_exhausted
[i
] != (__u64
)(-1))
337 LASSERT(pos
<= LNET_MT_HASH_IGNORE
);
338 /* mtable::mt_mhash[pos] is marked as exhausted or not */
339 bmap
= &mtable
->mt_exhausted
[pos
>> LNET_MT_BITS_U64
];
340 pos
&= (1 << LNET_MT_BITS_U64
) - 1;
342 return (*bmap
& (1ULL << pos
));
346 lnet_mt_set_exhausted(struct lnet_match_table
*mtable
, int pos
, int exhausted
)
350 LASSERT(lnet_ptl_is_wildcard(the_lnet
.ln_portals
[mtable
->mt_portal
]));
351 LASSERT(pos
<= LNET_MT_HASH_IGNORE
);
353 /* set mtable::mt_mhash[pos] as exhausted/non-exhausted */
354 bmap
= &mtable
->mt_exhausted
[pos
>> LNET_MT_BITS_U64
];
355 pos
&= (1 << LNET_MT_BITS_U64
) - 1;
358 *bmap
&= ~(1ULL << pos
);
360 *bmap
|= 1ULL << pos
;
364 lnet_mt_match_head(struct lnet_match_table
*mtable
,
365 lnet_process_id_t id
, __u64 mbits
)
367 struct lnet_portal
*ptl
= the_lnet
.ln_portals
[mtable
->mt_portal
];
369 if (lnet_ptl_is_wildcard(ptl
)) {
370 return &mtable
->mt_mhash
[mbits
& LNET_MT_HASH_MASK
];
372 unsigned long hash
= mbits
+ id
.nid
+ id
.pid
;
374 LASSERT(lnet_ptl_is_unique(ptl
));
375 hash
= hash_long(hash
, LNET_MT_HASH_BITS
);
376 return &mtable
->mt_mhash
[hash
];
381 lnet_mt_match_md(struct lnet_match_table
*mtable
,
382 struct lnet_match_info
*info
, struct lnet_msg
*msg
)
384 struct list_head
*head
;
390 /* any ME with ignore bits? */
391 if (!list_empty(&mtable
->mt_mhash
[LNET_MT_HASH_IGNORE
]))
392 head
= &mtable
->mt_mhash
[LNET_MT_HASH_IGNORE
];
394 head
= lnet_mt_match_head(mtable
, info
->mi_id
, info
->mi_mbits
);
396 /* NB: only wildcard portal needs to return LNET_MATCHMD_EXHAUSTED */
397 if (lnet_ptl_is_wildcard(the_lnet
.ln_portals
[mtable
->mt_portal
]))
398 exhausted
= LNET_MATCHMD_EXHAUSTED
;
400 list_for_each_entry_safe(me
, tmp
, head
, me_list
) {
401 /* ME attached but MD not attached yet */
405 LASSERT(me
== me
->me_md
->md_me
);
407 rc
= lnet_try_match_md(me
->me_md
, info
, msg
);
408 if (!(rc
& LNET_MATCHMD_EXHAUSTED
))
409 exhausted
= 0; /* mlist is not empty */
411 if (rc
& LNET_MATCHMD_FINISH
) {
413 * don't return EXHAUSTED bit because we don't know
414 * whether the mlist is empty or not
416 return rc
& ~LNET_MATCHMD_EXHAUSTED
;
420 if (exhausted
== LNET_MATCHMD_EXHAUSTED
) { /* @head is exhausted */
421 lnet_mt_set_exhausted(mtable
, head
- mtable
->mt_mhash
, 1);
422 if (!lnet_mt_test_exhausted(mtable
, -1))
426 if (!exhausted
&& head
== &mtable
->mt_mhash
[LNET_MT_HASH_IGNORE
]) {
427 head
= lnet_mt_match_head(mtable
, info
->mi_id
, info
->mi_mbits
);
428 goto again
; /* re-check MEs w/o ignore-bits */
431 if (info
->mi_opc
== LNET_MD_OP_GET
||
432 !lnet_ptl_is_lazy(the_lnet
.ln_portals
[info
->mi_portal
]))
433 return exhausted
| LNET_MATCHMD_DROP
;
435 return exhausted
| LNET_MATCHMD_NONE
;
439 lnet_ptl_match_early(struct lnet_portal
*ptl
, struct lnet_msg
*msg
)
444 * message arrived before any buffer posting on this portal,
445 * simply delay or drop this message
447 if (likely(lnet_ptl_is_wildcard(ptl
) || lnet_ptl_is_unique(ptl
)))
451 /* check it again with hold of lock */
452 if (lnet_ptl_is_wildcard(ptl
) || lnet_ptl_is_unique(ptl
)) {
453 lnet_ptl_unlock(ptl
);
457 if (lnet_ptl_is_lazy(ptl
)) {
458 if (msg
->msg_rx_ready_delay
) {
459 msg
->msg_rx_delayed
= 1;
460 list_add_tail(&msg
->msg_list
,
461 &ptl
->ptl_msg_delayed
);
463 rc
= LNET_MATCHMD_NONE
;
465 rc
= LNET_MATCHMD_DROP
;
468 lnet_ptl_unlock(ptl
);
473 lnet_ptl_match_delay(struct lnet_portal
*ptl
,
474 struct lnet_match_info
*info
, struct lnet_msg
*msg
)
476 int first
= ptl
->ptl_mt_maps
[0]; /* read w/o lock */
481 * steal buffer from other CPTs, and delay it if nothing to steal,
482 * this function is more expensive than a regular match, but we
483 * don't expect it can happen a lot
485 LASSERT(lnet_ptl_is_wildcard(ptl
));
487 for (i
= 0; i
< LNET_CPT_NUMBER
; i
++) {
488 struct lnet_match_table
*mtable
;
491 cpt
= (first
+ i
) % LNET_CPT_NUMBER
;
492 mtable
= ptl
->ptl_mtables
[cpt
];
493 if (i
&& i
!= LNET_CPT_NUMBER
- 1 && !mtable
->mt_enabled
)
499 if (!i
) { /* the first try, attach on stealing list */
500 list_add_tail(&msg
->msg_list
,
501 &ptl
->ptl_msg_stealing
);
504 if (!list_empty(&msg
->msg_list
)) { /* on stealing list */
505 rc
= lnet_mt_match_md(mtable
, info
, msg
);
507 if ((rc
& LNET_MATCHMD_EXHAUSTED
) &&
509 lnet_ptl_disable_mt(ptl
, cpt
);
511 if (rc
& LNET_MATCHMD_FINISH
)
512 list_del_init(&msg
->msg_list
);
516 * could be matched by lnet_ptl_attach_md()
517 * which is called by another thread
520 LNET_MATCHMD_DROP
: LNET_MATCHMD_OK
;
523 if (!list_empty(&msg
->msg_list
) && /* not matched yet */
524 (i
== LNET_CPT_NUMBER
- 1 || /* the last CPT */
525 !ptl
->ptl_mt_nmaps
|| /* no active CPT */
526 (ptl
->ptl_mt_nmaps
== 1 && /* the only active CPT */
527 ptl
->ptl_mt_maps
[0] == cpt
))) {
528 /* nothing to steal, delay or drop */
529 list_del_init(&msg
->msg_list
);
531 if (lnet_ptl_is_lazy(ptl
)) {
532 msg
->msg_rx_delayed
= 1;
533 list_add_tail(&msg
->msg_list
,
534 &ptl
->ptl_msg_delayed
);
535 rc
= LNET_MATCHMD_NONE
;
537 rc
= LNET_MATCHMD_DROP
;
541 lnet_ptl_unlock(ptl
);
542 lnet_res_unlock(cpt
);
544 if ((rc
& LNET_MATCHMD_FINISH
) || msg
->msg_rx_delayed
)
552 lnet_ptl_match_md(struct lnet_match_info
*info
, struct lnet_msg
*msg
)
554 struct lnet_match_table
*mtable
;
555 struct lnet_portal
*ptl
;
558 CDEBUG(D_NET
, "Request from %s of length %d into portal %d MB=%#llx\n",
559 libcfs_id2str(info
->mi_id
), info
->mi_rlength
, info
->mi_portal
,
562 if (info
->mi_portal
>= the_lnet
.ln_nportals
) {
563 CERROR("Invalid portal %d not in [0-%d]\n",
564 info
->mi_portal
, the_lnet
.ln_nportals
);
565 return LNET_MATCHMD_DROP
;
568 ptl
= the_lnet
.ln_portals
[info
->mi_portal
];
569 rc
= lnet_ptl_match_early(ptl
, msg
);
570 if (rc
) /* matched or delayed early message */
573 mtable
= lnet_mt_of_match(info
, msg
);
574 lnet_res_lock(mtable
->mt_cpt
);
576 if (the_lnet
.ln_shutdown
) {
577 rc
= LNET_MATCHMD_DROP
;
581 rc
= lnet_mt_match_md(mtable
, info
, msg
);
582 if ((rc
& LNET_MATCHMD_EXHAUSTED
) && mtable
->mt_enabled
) {
584 lnet_ptl_disable_mt(ptl
, mtable
->mt_cpt
);
585 lnet_ptl_unlock(ptl
);
588 if (rc
& LNET_MATCHMD_FINISH
) /* matched or dropping */
591 if (!msg
->msg_rx_ready_delay
)
594 LASSERT(lnet_ptl_is_lazy(ptl
));
595 LASSERT(!msg
->msg_rx_delayed
);
597 /* NB: we don't expect "delay" can happen a lot */
598 if (lnet_ptl_is_unique(ptl
) || LNET_CPT_NUMBER
== 1) {
601 msg
->msg_rx_delayed
= 1;
602 list_add_tail(&msg
->msg_list
, &ptl
->ptl_msg_delayed
);
604 lnet_ptl_unlock(ptl
);
605 lnet_res_unlock(mtable
->mt_cpt
);
608 lnet_res_unlock(mtable
->mt_cpt
);
609 rc
= lnet_ptl_match_delay(ptl
, info
, msg
);
612 if (msg
->msg_rx_delayed
) {
614 "Delaying %s from %s ptl %d MB %#llx off %d len %d\n",
615 info
->mi_opc
== LNET_MD_OP_PUT
? "PUT" : "GET",
616 libcfs_id2str(info
->mi_id
), info
->mi_portal
,
617 info
->mi_mbits
, info
->mi_roffset
, info
->mi_rlength
);
621 lnet_res_unlock(mtable
->mt_cpt
);
623 /* EXHAUSTED bit is only meaningful for internal functions */
624 return rc
& ~LNET_MATCHMD_EXHAUSTED
;
628 lnet_ptl_detach_md(lnet_me_t
*me
, lnet_libmd_t
*md
)
630 LASSERT(me
->me_md
== md
&& md
->md_me
== me
);
636 /* called with lnet_res_lock held */
638 lnet_ptl_attach_md(lnet_me_t
*me
, lnet_libmd_t
*md
,
639 struct list_head
*matches
, struct list_head
*drops
)
641 struct lnet_portal
*ptl
= the_lnet
.ln_portals
[me
->me_portal
];
642 struct lnet_match_table
*mtable
;
643 struct list_head
*head
;
649 LASSERT(!md
->md_refcount
); /* a brand new MD */
654 cpt
= lnet_cpt_of_cookie(md
->md_lh
.lh_cookie
);
655 mtable
= ptl
->ptl_mtables
[cpt
];
657 if (list_empty(&ptl
->ptl_msg_stealing
) &&
658 list_empty(&ptl
->ptl_msg_delayed
) &&
659 !lnet_mt_test_exhausted(mtable
, me
->me_pos
))
663 head
= &ptl
->ptl_msg_stealing
;
665 list_for_each_entry_safe(msg
, tmp
, head
, msg_list
) {
666 struct lnet_match_info info
;
670 LASSERT(msg
->msg_rx_delayed
|| head
== &ptl
->ptl_msg_stealing
);
673 info
.mi_id
.nid
= hdr
->src_nid
;
674 info
.mi_id
.pid
= hdr
->src_pid
;
675 info
.mi_opc
= LNET_MD_OP_PUT
;
676 info
.mi_portal
= hdr
->msg
.put
.ptl_index
;
677 info
.mi_rlength
= hdr
->payload_length
;
678 info
.mi_roffset
= hdr
->msg
.put
.offset
;
679 info
.mi_mbits
= hdr
->msg
.put
.match_bits
;
681 rc
= lnet_try_match_md(md
, &info
, msg
);
683 exhausted
= (rc
& LNET_MATCHMD_EXHAUSTED
);
684 if (rc
& LNET_MATCHMD_NONE
) {
690 /* Hurrah! This _is_ a match */
691 LASSERT(rc
& LNET_MATCHMD_FINISH
);
692 list_del_init(&msg
->msg_list
);
694 if (head
== &ptl
->ptl_msg_stealing
) {
697 /* stealing thread will handle the message */
701 if (rc
& LNET_MATCHMD_OK
) {
702 list_add_tail(&msg
->msg_list
, matches
);
704 CDEBUG(D_NET
, "Resuming delayed PUT from %s portal %d match %llu offset %d length %d.\n",
705 libcfs_id2str(info
.mi_id
),
706 info
.mi_portal
, info
.mi_mbits
,
707 info
.mi_roffset
, info
.mi_rlength
);
709 list_add_tail(&msg
->msg_list
, drops
);
716 if (!exhausted
&& head
== &ptl
->ptl_msg_stealing
) {
717 head
= &ptl
->ptl_msg_delayed
;
721 if (lnet_ptl_is_wildcard(ptl
) && !exhausted
) {
722 lnet_mt_set_exhausted(mtable
, me
->me_pos
, 0);
723 if (!mtable
->mt_enabled
)
724 lnet_ptl_enable_mt(ptl
, cpt
);
727 lnet_ptl_unlock(ptl
);
731 lnet_ptl_cleanup(struct lnet_portal
*ptl
)
733 struct lnet_match_table
*mtable
;
736 if (!ptl
->ptl_mtables
) /* uninitialized portal */
739 LASSERT(list_empty(&ptl
->ptl_msg_delayed
));
740 LASSERT(list_empty(&ptl
->ptl_msg_stealing
));
741 cfs_percpt_for_each(mtable
, i
, ptl
->ptl_mtables
) {
742 struct list_head
*mhash
;
746 if (!mtable
->mt_mhash
) /* uninitialized match-table */
749 mhash
= mtable
->mt_mhash
;
751 for (j
= 0; j
< LNET_MT_HASH_SIZE
+ 1; j
++) {
752 while (!list_empty(&mhash
[j
])) {
753 me
= list_entry(mhash
[j
].next
,
755 CERROR("Active ME %p on exit\n", me
);
756 list_del(&me
->me_list
);
760 /* the extra entry is for MEs with ignore bits */
761 LIBCFS_FREE(mhash
, sizeof(*mhash
) * (LNET_MT_HASH_SIZE
+ 1));
764 cfs_percpt_free(ptl
->ptl_mtables
);
765 ptl
->ptl_mtables
= NULL
;
769 lnet_ptl_setup(struct lnet_portal
*ptl
, int index
)
771 struct lnet_match_table
*mtable
;
772 struct list_head
*mhash
;
776 ptl
->ptl_mtables
= cfs_percpt_alloc(lnet_cpt_table(),
777 sizeof(struct lnet_match_table
));
778 if (!ptl
->ptl_mtables
) {
779 CERROR("Failed to create match table for portal %d\n", index
);
783 ptl
->ptl_index
= index
;
784 INIT_LIST_HEAD(&ptl
->ptl_msg_delayed
);
785 INIT_LIST_HEAD(&ptl
->ptl_msg_stealing
);
786 spin_lock_init(&ptl
->ptl_lock
);
787 cfs_percpt_for_each(mtable
, i
, ptl
->ptl_mtables
) {
788 /* the extra entry is for MEs with ignore bits */
789 LIBCFS_CPT_ALLOC(mhash
, lnet_cpt_table(), i
,
790 sizeof(*mhash
) * (LNET_MT_HASH_SIZE
+ 1));
792 CERROR("Failed to create match hash for portal %d\n",
797 memset(&mtable
->mt_exhausted
[0], -1,
798 sizeof(mtable
->mt_exhausted
[0]) *
799 LNET_MT_EXHAUSTED_BMAP
);
800 mtable
->mt_mhash
= mhash
;
801 for (j
= 0; j
< LNET_MT_HASH_SIZE
+ 1; j
++)
802 INIT_LIST_HEAD(&mhash
[j
]);
804 mtable
->mt_portal
= index
;
810 lnet_ptl_cleanup(ptl
);
815 lnet_portals_destroy(void)
819 if (!the_lnet
.ln_portals
)
822 for (i
= 0; i
< the_lnet
.ln_nportals
; i
++)
823 lnet_ptl_cleanup(the_lnet
.ln_portals
[i
]);
825 cfs_array_free(the_lnet
.ln_portals
);
826 the_lnet
.ln_portals
= NULL
;
830 lnet_portals_create(void)
835 size
= offsetof(struct lnet_portal
, ptl_mt_maps
[LNET_CPT_NUMBER
]);
837 the_lnet
.ln_nportals
= MAX_PORTALS
;
838 the_lnet
.ln_portals
= cfs_array_alloc(the_lnet
.ln_nportals
, size
);
839 if (!the_lnet
.ln_portals
) {
840 CERROR("Failed to allocate portals table\n");
844 for (i
= 0; i
< the_lnet
.ln_nportals
; i
++) {
845 if (lnet_ptl_setup(the_lnet
.ln_portals
[i
], i
)) {
846 lnet_portals_destroy();
855 * Turn on the lazy portal attribute. Use with caution!
857 * This portal attribute only affects incoming PUT requests to the portal,
858 * and is off by default. By default, if there's no matching MD for an
859 * incoming PUT request, it is simply dropped. With the lazy attribute on,
860 * such requests are queued indefinitely until either a matching MD is
861 * posted to the portal or the lazy attribute is turned off.
863 * It would prevent dropped requests, however it should be regarded as the
864 * last line of defense - i.e. users must keep a close watch on active
865 * buffers on a lazy portal and once it becomes too low post more buffers as
866 * soon as possible. This is because delayed requests usually have detrimental
867 * effects on underlying network connections. A few delayed requests often
868 * suffice to bring an underlying connection to a complete halt, due to flow
869 * control mechanisms.
871 * There's also a DOS attack risk. If users don't post match-all MDs on a
872 * lazy portal, a malicious peer can easily stop a service by sending some
873 * PUT requests with match bits that won't match any MD. A routed server is
874 * especially vulnerable since the connections to its neighbor routers are
875 * shared among all clients.
877 * \param portal Index of the portal to enable the lazy attribute on.
879 * \retval 0 On success.
880 * \retval -EINVAL If \a portal is not a valid index.
883 LNetSetLazyPortal(int portal
)
885 struct lnet_portal
*ptl
;
887 if (portal
< 0 || portal
>= the_lnet
.ln_nportals
)
890 CDEBUG(D_NET
, "Setting portal %d lazy\n", portal
);
891 ptl
= the_lnet
.ln_portals
[portal
];
893 lnet_res_lock(LNET_LOCK_EX
);
896 lnet_ptl_setopt(ptl
, LNET_PTL_LAZY
);
898 lnet_ptl_unlock(ptl
);
899 lnet_res_unlock(LNET_LOCK_EX
);
903 EXPORT_SYMBOL(LNetSetLazyPortal
);
906 * Turn off the lazy portal attribute. Delayed requests on the portal,
907 * if any, will be all dropped when this function returns.
909 * \param portal Index of the portal to disable the lazy attribute on.
911 * \retval 0 On success.
912 * \retval -EINVAL If \a portal is not a valid index.
915 LNetClearLazyPortal(int portal
)
917 struct lnet_portal
*ptl
;
920 if (portal
< 0 || portal
>= the_lnet
.ln_nportals
)
923 ptl
= the_lnet
.ln_portals
[portal
];
925 lnet_res_lock(LNET_LOCK_EX
);
928 if (!lnet_ptl_is_lazy(ptl
)) {
929 lnet_ptl_unlock(ptl
);
930 lnet_res_unlock(LNET_LOCK_EX
);
934 if (the_lnet
.ln_shutdown
)
935 CWARN("Active lazy portal %d on exit\n", portal
);
937 CDEBUG(D_NET
, "clearing portal %d lazy\n", portal
);
939 /* grab all the blocked messages atomically */
940 list_splice_init(&ptl
->ptl_msg_delayed
, &zombies
);
942 lnet_ptl_unsetopt(ptl
, LNET_PTL_LAZY
);
944 lnet_ptl_unlock(ptl
);
945 lnet_res_unlock(LNET_LOCK_EX
);
947 lnet_drop_delayed_msg_list(&zombies
, "Clearing lazy portal attr");
951 EXPORT_SYMBOL(LNetClearLazyPortal
);