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) 2011, 2012, Whamcloud, Inc.
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>
42 lnet_ptl_type_match(struct lnet_portal *ptl, lnet_process_id_t match_id,
43 __u64 mbits, __u64 ignore_bits)
47 unique = ignore_bits == 0 &&
48 match_id.nid != LNET_NID_ANY &&
49 match_id.pid != LNET_PID_ANY;
51 LASSERT(!lnet_ptl_is_unique(ptl) || !lnet_ptl_is_wildcard(ptl));
53 /* prefer to check w/o any lock */
54 if (likely(lnet_ptl_is_unique(ptl) || lnet_ptl_is_wildcard(ptl)))
57 /* unset, new portal */
59 /* check again with lock */
60 if (unlikely(lnet_ptl_is_unique(ptl) || lnet_ptl_is_wildcard(ptl))) {
67 lnet_ptl_setopt(ptl, LNET_PTL_MATCH_UNIQUE);
69 lnet_ptl_setopt(ptl, LNET_PTL_MATCH_WILDCARD);
76 if ((lnet_ptl_is_unique(ptl) && !unique) ||
77 (lnet_ptl_is_wildcard(ptl) && unique))
83 lnet_try_match_md(int index, int op_mask, lnet_process_id_t src,
84 unsigned int rlength, unsigned int roffset,
85 __u64 match_bits, lnet_libmd_t *md, lnet_msg_t *msg)
87 /* ALWAYS called holding the LNET_LOCK, and can't LNET_UNLOCK;
88 * lnet_match_blocked_msg() relies on this to avoid races */
91 lnet_me_t *me = md->md_me;
93 /* mismatched MD op */
94 if ((md->md_options & op_mask) == 0)
95 return LNET_MATCHMD_NONE;
98 if (lnet_md_exhausted(md))
99 return LNET_MATCHMD_NONE;
101 /* mismatched ME nid/pid? */
102 if (me->me_match_id.nid != LNET_NID_ANY &&
103 me->me_match_id.nid != src.nid)
104 return LNET_MATCHMD_NONE;
106 if (me->me_match_id.pid != LNET_PID_ANY &&
107 me->me_match_id.pid != src.pid)
108 return LNET_MATCHMD_NONE;
110 /* mismatched ME matchbits? */
111 if (((me->me_match_bits ^ match_bits) & ~me->me_ignore_bits) != 0)
112 return LNET_MATCHMD_NONE;
114 /* Hurrah! This _is_ a match; check it out... */
116 if ((md->md_options & LNET_MD_MANAGE_REMOTE) == 0)
117 offset = md->md_offset;
121 if ((md->md_options & LNET_MD_MAX_SIZE) != 0) {
122 mlength = md->md_max_size;
123 LASSERT(md->md_offset + mlength <= md->md_length);
125 mlength = md->md_length - offset;
128 if (rlength <= mlength) { /* fits in allowed space */
130 } else if ((md->md_options & LNET_MD_TRUNCATE) == 0) {
131 /* this packet _really_ is too big */
132 CERROR("Matching packet from %s, match "LPU64
133 " length %d too big: %d left, %d allowed\n",
134 libcfs_id2str(src), match_bits, rlength,
135 md->md_length - offset, mlength);
137 return LNET_MATCHMD_DROP;
140 /* Commit to this ME/MD */
141 CDEBUG(D_NET, "Incoming %s index %x from %s of "
142 "length %d/%d into md "LPX64" [%d] + %d\n",
143 (op_mask == LNET_MD_OP_PUT) ? "put" : "get",
144 index, libcfs_id2str(src), mlength, rlength,
145 md->md_lh.lh_cookie, md->md_niov, offset);
147 lnet_msg_attach_md(msg, md, offset, mlength);
148 md->md_offset = offset + mlength;
150 /* Auto-unlink NOW, so the ME gets unlinked if required.
151 * We bumped md->md_refcount above so the MD just gets flagged
152 * for unlink when it is finalized. */
153 if ((md->md_flags & LNET_MD_FLAG_AUTO_UNLINK) != 0 &&
154 lnet_md_exhausted(md)) {
158 return LNET_MATCHMD_OK;
162 lnet_match_md(int index, int op_mask, lnet_process_id_t src,
163 unsigned int rlength, unsigned int roffset,
164 __u64 match_bits, lnet_msg_t *msg)
166 struct lnet_portal *ptl = the_lnet.ln_portals[index];
173 CDEBUG(D_NET, "Request from %s of length %d into portal %d "
174 "MB="LPX64"\n", libcfs_id2str(src), rlength, index, match_bits);
176 if (index < 0 || index >= the_lnet.ln_nportals) {
177 CERROR("Invalid portal %d not in [0-%d]\n",
178 index, the_lnet.ln_nportals);
179 return LNET_MATCHMD_DROP;
182 head = lnet_ptl_me_head(index, src, match_bits);
183 if (head == NULL) /* nobody posted anything on this portal */
186 cfs_list_for_each_entry_safe(me, tmp, head, me_list) {
189 /* ME attached but MD not attached yet */
193 LASSERT(me == md->md_me);
195 rc = lnet_try_match_md(index, op_mask, src, rlength,
196 roffset, match_bits, md, msg);
201 case LNET_MATCHMD_NONE:
204 case LNET_MATCHMD_OK:
205 return LNET_MATCHMD_OK;
207 case LNET_MATCHMD_DROP:
208 return LNET_MATCHMD_DROP;
214 if (op_mask == LNET_MD_OP_GET ||
215 !lnet_ptl_is_lazy(ptl))
216 return LNET_MATCHMD_DROP;
218 return LNET_MATCHMD_NONE;
221 /* called with LNET_LOCK held */
223 lnet_match_blocked_msg(lnet_libmd_t *md)
225 CFS_LIST_HEAD (drops);
226 CFS_LIST_HEAD (matches);
230 struct lnet_portal *ptl;
231 lnet_me_t *me = md->md_me;
233 LASSERT(me->me_portal < (unsigned int)the_lnet.ln_nportals);
235 ptl = the_lnet.ln_portals[me->me_portal];
236 if (!lnet_ptl_is_lazy(ptl)) {
237 LASSERT(cfs_list_empty(&ptl->ptl_msgq));
241 LASSERT(md->md_refcount == 0); /* a brand new MD */
243 cfs_list_for_each_safe(entry, tmp, &ptl->ptl_msgq) {
247 lnet_process_id_t src;
249 msg = cfs_list_entry(entry, lnet_msg_t, msg_list);
251 LASSERT(msg->msg_delayed);
254 index = hdr->msg.put.ptl_index;
256 src.nid = hdr->src_nid;
257 src.pid = hdr->src_pid;
259 rc = lnet_try_match_md(index, LNET_MD_OP_PUT, src,
262 hdr->msg.put.match_bits, md, msg);
264 if (rc == LNET_MATCHMD_NONE)
267 /* Hurrah! This _is_ a match */
268 cfs_list_del(&msg->msg_list);
269 ptl->ptl_msgq_version++;
271 if (rc == LNET_MATCHMD_OK) {
272 cfs_list_add_tail(&msg->msg_list, &matches);
274 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
275 "match "LPU64" offset %d length %d.\n",
277 hdr->msg.put.ptl_index,
278 hdr->msg.put.match_bits,
280 hdr->payload_length);
282 LASSERT(rc == LNET_MATCHMD_DROP);
284 cfs_list_add_tail(&msg->msg_list, &drops);
287 if (lnet_md_exhausted(md))
293 lnet_drop_delayed_msg_list(&drops, "Bad match");
294 lnet_recv_delayed_msg_list(&matches);
300 lnet_ptl_cleanup(struct lnet_portal *ptl)
305 LASSERT(cfs_list_empty(&ptl->ptl_msgq));
306 LASSERT(cfs_list_empty(&ptl->ptl_mlist));
308 if (ptl->ptl_mhash == NULL) /* uninitialized portal */
312 while (!cfs_list_empty(&ptl->ptl_mlist)) {
313 me = cfs_list_entry(ptl->ptl_mlist.next,
315 CERROR("Active wildcard ME %p on exit\n", me);
316 cfs_list_del(&me->me_list);
320 for (j = 0; j < LNET_PORTAL_HASH_SIZE; j++) {
321 while (!cfs_list_empty(&ptl->ptl_mhash[j])) {
322 me = cfs_list_entry(ptl->ptl_mhash[j].next,
324 CERROR("Active unique ME %p on exit\n", me);
325 cfs_list_del(&me->me_list);
330 LIBCFS_FREE(ptl->ptl_mhash,
331 LNET_PORTAL_HASH_SIZE * sizeof(ptl->ptl_mhash[0]));
332 ptl->ptl_mhash = NULL; /* mark it as finalized */
336 lnet_ptl_setup(struct lnet_portal *ptl, int index)
341 ptl->ptl_index = index;
342 CFS_INIT_LIST_HEAD(&ptl->ptl_msgq);
343 CFS_INIT_LIST_HEAD(&ptl->ptl_mlist);
345 LIBCFS_ALLOC(mhash, sizeof(*mhash) * LNET_PORTAL_HASH_SIZE);
347 CERROR("Failed to create match table for portal %d\n", index);
351 for (i = 0; i < LNET_PORTAL_HASH_SIZE; i++)
352 CFS_INIT_LIST_HEAD(&mhash[i]);
354 ptl->ptl_mhash = mhash; /* initialized */
360 lnet_portals_destroy(void)
364 if (the_lnet.ln_portals == NULL)
367 for (i = 0; i < the_lnet.ln_nportals; i++)
368 lnet_ptl_cleanup(the_lnet.ln_portals[i]);
370 cfs_array_free(the_lnet.ln_portals);
371 the_lnet.ln_portals = NULL;
375 lnet_portals_create(void)
380 size = sizeof(struct lnet_portal);
382 the_lnet.ln_nportals = MAX_PORTALS;
383 the_lnet.ln_portals = cfs_array_alloc(the_lnet.ln_nportals, size);
384 if (the_lnet.ln_portals == NULL) {
385 CERROR("Failed to allocate portals table\n");
389 for (i = 0; i < the_lnet.ln_nportals; i++) {
390 if (lnet_ptl_setup(the_lnet.ln_portals[i], i)) {
391 lnet_portals_destroy();
400 * Turn on the lazy portal attribute. Use with caution!
402 * This portal attribute only affects incoming PUT requests to the portal,
403 * and is off by default. By default, if there's no matching MD for an
404 * incoming PUT request, it is simply dropped. With the lazy attribute on,
405 * such requests are queued indefinitely until either a matching MD is
406 * posted to the portal or the lazy attribute is turned off.
408 * It would prevent dropped requests, however it should be regarded as the
409 * last line of defense - i.e. users must keep a close watch on active
410 * buffers on a lazy portal and once it becomes too low post more buffers as
411 * soon as possible. This is because delayed requests usually have detrimental
412 * effects on underlying network connections. A few delayed requests often
413 * suffice to bring an underlying connection to a complete halt, due to flow
414 * control mechanisms.
416 * There's also a DOS attack risk. If users don't post match-all MDs on a
417 * lazy portal, a malicious peer can easily stop a service by sending some
418 * PUT requests with match bits that won't match any MD. A routed server is
419 * especially vulnerable since the connections to its neighbor routers are
420 * shared among all clients.
422 * \param portal Index of the portal to enable the lazy attribute on.
424 * \retval 0 On success.
425 * \retval -EINVAL If \a portal is not a valid index.
428 LNetSetLazyPortal(int portal)
430 struct lnet_portal *ptl;
432 if (portal < 0 || portal >= the_lnet.ln_nportals)
435 CDEBUG(D_NET, "Setting portal %d lazy\n", portal);
436 ptl = the_lnet.ln_portals[portal];
439 lnet_ptl_setopt(ptl, LNET_PTL_LAZY);
446 * Turn off the lazy portal attribute. Delayed requests on the portal,
447 * if any, will be all dropped when this function returns.
449 * \param portal Index of the portal to disable the lazy attribute on.
451 * \retval 0 On success.
452 * \retval -EINVAL If \a portal is not a valid index.
455 LNetClearLazyPortal(int portal)
457 struct lnet_portal *ptl;
458 CFS_LIST_HEAD (zombies);
460 if (portal < 0 || portal >= the_lnet.ln_nportals)
463 ptl = the_lnet.ln_portals[portal];
467 if (!lnet_ptl_is_lazy(ptl)) {
472 if (the_lnet.ln_shutdown)
473 CWARN("Active lazy portal %d on exit\n", portal);
475 CDEBUG(D_NET, "clearing portal %d lazy\n", portal);
477 /* grab all the blocked messages atomically */
478 cfs_list_splice_init(&ptl->ptl_msgq, &zombies);
480 ptl->ptl_msgq_version++;
481 lnet_ptl_unsetopt(ptl, LNET_PTL_LAZY);
485 lnet_drop_delayed_msg_list(&zombies, "Clearing lazy portal attr");