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,
86 unsigned int *mlength_out, unsigned int *offset_out)
88 /* ALWAYS called holding the LNET_LOCK, and can't LNET_UNLOCK;
89 * lnet_match_blocked_msg() relies on this to avoid races */
92 lnet_me_t *me = md->md_me;
94 /* mismatched MD op */
95 if ((md->md_options & op_mask) == 0)
96 return LNET_MATCHMD_NONE;
99 if (lnet_md_exhausted(md))
100 return LNET_MATCHMD_NONE;
102 /* mismatched ME nid/pid? */
103 if (me->me_match_id.nid != LNET_NID_ANY &&
104 me->me_match_id.nid != src.nid)
105 return LNET_MATCHMD_NONE;
107 if (me->me_match_id.pid != LNET_PID_ANY &&
108 me->me_match_id.pid != src.pid)
109 return LNET_MATCHMD_NONE;
111 /* mismatched ME matchbits? */
112 if (((me->me_match_bits ^ match_bits) & ~me->me_ignore_bits) != 0)
113 return LNET_MATCHMD_NONE;
115 /* Hurrah! This _is_ a match; check it out... */
117 if ((md->md_options & LNET_MD_MANAGE_REMOTE) == 0)
118 offset = md->md_offset;
122 if ((md->md_options & LNET_MD_MAX_SIZE) != 0) {
123 mlength = md->md_max_size;
124 LASSERT(md->md_offset + mlength <= md->md_length);
126 mlength = md->md_length - offset;
129 if (rlength <= mlength) { /* fits in allowed space */
131 } else if ((md->md_options & LNET_MD_TRUNCATE) == 0) {
132 /* this packet _really_ is too big */
133 CERROR("Matching packet from %s, match "LPU64
134 " length %d too big: %d left, %d allowed\n",
135 libcfs_id2str(src), match_bits, rlength,
136 md->md_length - offset, mlength);
138 return LNET_MATCHMD_DROP;
141 /* Commit to this ME/MD */
142 CDEBUG(D_NET, "Incoming %s index %x from %s of "
143 "length %d/%d into md "LPX64" [%d] + %d\n",
144 (op_mask == LNET_MD_OP_PUT) ? "put" : "get",
145 index, libcfs_id2str(src), mlength, rlength,
146 md->md_lh.lh_cookie, md->md_niov, offset);
148 lnet_commit_md(md, msg);
149 md->md_offset = offset + mlength;
151 /* NB Caller will set ev.type and ev.hdr_data */
152 msg->msg_ev.initiator = src;
153 msg->msg_ev.pt_index = index;
154 msg->msg_ev.match_bits = match_bits;
155 msg->msg_ev.rlength = rlength;
156 msg->msg_ev.mlength = mlength;
157 msg->msg_ev.offset = offset;
159 lnet_md_deconstruct(md, &msg->msg_ev.md);
160 lnet_md2handle(&msg->msg_ev.md_handle, md);
162 *offset_out = offset;
163 *mlength_out = mlength;
165 /* Auto-unlink NOW, so the ME gets unlinked if required.
166 * We bumped md->md_refcount above so the MD just gets flagged
167 * for unlink when it is finalized. */
168 if ((md->md_flags & LNET_MD_FLAG_AUTO_UNLINK) != 0 &&
169 lnet_md_exhausted(md)) {
173 return LNET_MATCHMD_OK;
177 lnet_match_md(int index, int op_mask, lnet_process_id_t src,
178 unsigned int rlength, unsigned int roffset,
179 __u64 match_bits, lnet_msg_t *msg,
180 unsigned int *mlength_out, unsigned int *offset_out,
181 lnet_libmd_t **md_out)
183 struct lnet_portal *ptl = the_lnet.ln_portals[index];
190 CDEBUG(D_NET, "Request from %s of length %d into portal %d "
191 "MB="LPX64"\n", libcfs_id2str(src), rlength, index, match_bits);
193 if (index < 0 || index >= the_lnet.ln_nportals) {
194 CERROR("Invalid portal %d not in [0-%d]\n",
195 index, the_lnet.ln_nportals);
196 return LNET_MATCHMD_DROP;
199 head = lnet_ptl_me_head(index, src, match_bits);
200 if (head == NULL) /* nobody posted anything on this portal */
203 cfs_list_for_each_entry_safe(me, tmp, head, me_list) {
206 /* ME attached but MD not attached yet */
210 LASSERT(me == md->md_me);
212 rc = lnet_try_match_md(index, op_mask, src, rlength,
213 roffset, match_bits, md, msg,
214 mlength_out, offset_out);
219 case LNET_MATCHMD_NONE:
222 case LNET_MATCHMD_OK:
224 return LNET_MATCHMD_OK;
226 case LNET_MATCHMD_DROP:
227 return LNET_MATCHMD_DROP;
233 if (op_mask == LNET_MD_OP_GET ||
234 !lnet_ptl_is_lazy(ptl))
235 return LNET_MATCHMD_DROP;
237 return LNET_MATCHMD_NONE;
240 /* called with LNET_LOCK held */
242 lnet_match_blocked_msg(lnet_libmd_t *md)
244 CFS_LIST_HEAD (drops);
245 CFS_LIST_HEAD (matches);
249 struct lnet_portal *ptl;
250 lnet_me_t *me = md->md_me;
252 LASSERT(me->me_portal < (unsigned int)the_lnet.ln_nportals);
254 ptl = the_lnet.ln_portals[me->me_portal];
255 if (!lnet_ptl_is_lazy(ptl)) {
256 LASSERT(cfs_list_empty(&ptl->ptl_msgq));
260 LASSERT(md->md_refcount == 0); /* a brand new MD */
262 cfs_list_for_each_safe(entry, tmp, &ptl->ptl_msgq) {
265 unsigned int mlength;
268 lnet_process_id_t src;
270 msg = cfs_list_entry(entry, lnet_msg_t, msg_list);
272 LASSERT(msg->msg_delayed);
275 index = hdr->msg.put.ptl_index;
277 src.nid = hdr->src_nid;
278 src.pid = hdr->src_pid;
280 rc = lnet_try_match_md(index, LNET_MD_OP_PUT, src,
283 hdr->msg.put.match_bits,
284 md, msg, &mlength, &offset);
286 if (rc == LNET_MATCHMD_NONE)
289 /* Hurrah! This _is_ a match */
290 cfs_list_del(&msg->msg_list);
291 ptl->ptl_msgq_version++;
293 if (rc == LNET_MATCHMD_OK) {
294 cfs_list_add_tail(&msg->msg_list, &matches);
296 CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
297 "match "LPU64" offset %d length %d.\n",
299 hdr->msg.put.ptl_index,
300 hdr->msg.put.match_bits,
302 hdr->payload_length);
304 LASSERT(rc == LNET_MATCHMD_DROP);
306 cfs_list_add_tail(&msg->msg_list, &drops);
309 if (lnet_md_exhausted(md))
315 lnet_drop_delayed_msg_list(&drops, "Bad match");
316 lnet_recv_delayed_msg_list(&matches);
322 lnet_ptl_cleanup(struct lnet_portal *ptl)
327 LASSERT(cfs_list_empty(&ptl->ptl_msgq));
328 LASSERT(cfs_list_empty(&ptl->ptl_mlist));
330 if (ptl->ptl_mhash == NULL) /* uninitialized portal */
334 while (!cfs_list_empty(&ptl->ptl_mlist)) {
335 me = cfs_list_entry(ptl->ptl_mlist.next,
337 CERROR("Active wildcard ME %p on exit\n", me);
338 cfs_list_del(&me->me_list);
342 for (j = 0; j < LNET_PORTAL_HASH_SIZE; j++) {
343 while (!cfs_list_empty(&ptl->ptl_mhash[j])) {
344 me = cfs_list_entry(ptl->ptl_mhash[j].next,
346 CERROR("Active unique ME %p on exit\n", me);
347 cfs_list_del(&me->me_list);
352 LIBCFS_FREE(ptl->ptl_mhash,
353 LNET_PORTAL_HASH_SIZE * sizeof(ptl->ptl_mhash[0]));
354 ptl->ptl_mhash = NULL; /* mark it as finalized */
358 lnet_ptl_setup(struct lnet_portal *ptl, int index)
363 ptl->ptl_index = index;
364 CFS_INIT_LIST_HEAD(&ptl->ptl_msgq);
365 CFS_INIT_LIST_HEAD(&ptl->ptl_mlist);
367 LIBCFS_ALLOC(mhash, sizeof(*mhash) * LNET_PORTAL_HASH_SIZE);
369 CERROR("Failed to create match table for portal %d\n", index);
373 for (i = 0; i < LNET_PORTAL_HASH_SIZE; i++)
374 CFS_INIT_LIST_HEAD(&mhash[i]);
376 ptl->ptl_mhash = mhash; /* initialized */
382 lnet_portals_destroy(void)
386 if (the_lnet.ln_portals == NULL)
389 for (i = 0; i < the_lnet.ln_nportals; i++)
390 lnet_ptl_cleanup(the_lnet.ln_portals[i]);
392 cfs_array_free(the_lnet.ln_portals);
393 the_lnet.ln_portals = NULL;
397 lnet_portals_create(void)
402 size = sizeof(struct lnet_portal);
404 the_lnet.ln_nportals = MAX_PORTALS;
405 the_lnet.ln_portals = cfs_array_alloc(the_lnet.ln_nportals, size);
406 if (the_lnet.ln_portals == NULL) {
407 CERROR("Failed to allocate portals table\n");
411 for (i = 0; i < the_lnet.ln_nportals; i++) {
412 if (lnet_ptl_setup(the_lnet.ln_portals[i], i)) {
413 lnet_portals_destroy();
422 * Turn on the lazy portal attribute. Use with caution!
424 * This portal attribute only affects incoming PUT requests to the portal,
425 * and is off by default. By default, if there's no matching MD for an
426 * incoming PUT request, it is simply dropped. With the lazy attribute on,
427 * such requests are queued indefinitely until either a matching MD is
428 * posted to the portal or the lazy attribute is turned off.
430 * It would prevent dropped requests, however it should be regarded as the
431 * last line of defense - i.e. users must keep a close watch on active
432 * buffers on a lazy portal and once it becomes too low post more buffers as
433 * soon as possible. This is because delayed requests usually have detrimental
434 * effects on underlying network connections. A few delayed requests often
435 * suffice to bring an underlying connection to a complete halt, due to flow
436 * control mechanisms.
438 * There's also a DOS attack risk. If users don't post match-all MDs on a
439 * lazy portal, a malicious peer can easily stop a service by sending some
440 * PUT requests with match bits that won't match any MD. A routed server is
441 * especially vulnerable since the connections to its neighbor routers are
442 * shared among all clients.
444 * \param portal Index of the portal to enable the lazy attribute on.
446 * \retval 0 On success.
447 * \retval -EINVAL If \a portal is not a valid index.
450 LNetSetLazyPortal(int portal)
452 struct lnet_portal *ptl;
454 if (portal < 0 || portal >= the_lnet.ln_nportals)
457 CDEBUG(D_NET, "Setting portal %d lazy\n", portal);
458 ptl = the_lnet.ln_portals[portal];
461 lnet_ptl_setopt(ptl, LNET_PTL_LAZY);
468 * Turn off the lazy portal attribute. Delayed requests on the portal,
469 * if any, will be all dropped when this function returns.
471 * \param portal Index of the portal to disable the lazy attribute on.
473 * \retval 0 On success.
474 * \retval -EINVAL If \a portal is not a valid index.
477 LNetClearLazyPortal(int portal)
479 struct lnet_portal *ptl;
480 CFS_LIST_HEAD (zombies);
482 if (portal < 0 || portal >= the_lnet.ln_nportals)
485 ptl = the_lnet.ln_portals[portal];
489 if (!lnet_ptl_is_lazy(ptl)) {
494 if (the_lnet.ln_shutdown)
495 CWARN("Active lazy portal %d on exit\n", portal);
497 CDEBUG(D_NET, "clearing portal %d lazy\n", portal);
499 /* grab all the blocked messages atomically */
500 cfs_list_splice_init(&ptl->ptl_msgq, &zombies);
502 ptl->ptl_msgq_version++;
503 lnet_ptl_unsetopt(ptl, LNET_PTL_LAZY);
507 lnet_drop_delayed_msg_list(&zombies, "Clearing lazy portal attr");