Whamcloud - gitweb
114d02e74a90a96f27086a47473a040357364e2f
[fs/lustre-release.git] / lnet / lnet / lib-ptl.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
20  *
21  * GPL HEADER END
22  */
23 /*
24  * Copyright (c) 2011, 2012, Whamcloud, Inc.
25  */
26 /*
27  * This file is part of Lustre, http://www.lustre.org/
28  * Lustre is a trademark of Sun Microsystems, Inc.
29  *
30  * lnet/lnet/lib-ptl.c
31  *
32  * portal & match routines
33  *
34  * Author: liang@whamcloud.com
35  */
36
37 #define DEBUG_SUBSYSTEM S_LNET
38
39 #include <lnet/lib-lnet.h>
40
41 int
42 lnet_ptl_type_match(struct lnet_portal *ptl, lnet_process_id_t match_id,
43                     __u64 mbits, __u64 ignore_bits)
44 {
45         int unique;
46
47         unique = ignore_bits == 0 &&
48                  match_id.nid != LNET_NID_ANY &&
49                  match_id.pid != LNET_PID_ANY;
50
51         LASSERT(!lnet_ptl_is_unique(ptl) || !lnet_ptl_is_wildcard(ptl));
52
53         /* prefer to check w/o any lock */
54         if (likely(lnet_ptl_is_unique(ptl) || lnet_ptl_is_wildcard(ptl)))
55                 goto match;
56
57         /* unset, new portal */
58         LNET_LOCK();
59         /* check again with lock */
60         if (unlikely(lnet_ptl_is_unique(ptl) || lnet_ptl_is_wildcard(ptl))) {
61                 LNET_UNLOCK();
62                 goto match;
63         }
64
65         /* still not set */
66         if (unique)
67                 lnet_ptl_setopt(ptl, LNET_PTL_MATCH_UNIQUE);
68         else
69                 lnet_ptl_setopt(ptl, LNET_PTL_MATCH_WILDCARD);
70
71         LNET_UNLOCK();
72
73         return 1;
74
75  match:
76         if ((lnet_ptl_is_unique(ptl) && !unique) ||
77             (lnet_ptl_is_wildcard(ptl) && unique))
78                 return 0;
79         return 1;
80 }
81
82 static int
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 {
87         /* ALWAYS called holding the LNET_LOCK, and can't LNET_UNLOCK;
88          * lnet_match_blocked_msg() relies on this to avoid races */
89         unsigned int    offset;
90         unsigned int    mlength;
91         lnet_me_t       *me = md->md_me;
92
93         /* mismatched MD op */
94         if ((md->md_options & op_mask) == 0)
95                 return LNET_MATCHMD_NONE;
96
97         /* MD exhausted */
98         if (lnet_md_exhausted(md))
99                 return LNET_MATCHMD_NONE;
100
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;
105
106         if (me->me_match_id.pid != LNET_PID_ANY &&
107             me->me_match_id.pid != src.pid)
108                 return LNET_MATCHMD_NONE;
109
110         /* mismatched ME matchbits? */
111         if (((me->me_match_bits ^ match_bits) & ~me->me_ignore_bits) != 0)
112                 return LNET_MATCHMD_NONE;
113
114         /* Hurrah! This _is_ a match; check it out... */
115
116         if ((md->md_options & LNET_MD_MANAGE_REMOTE) == 0)
117                 offset = md->md_offset;
118         else
119                 offset = roffset;
120
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);
124         } else {
125                 mlength = md->md_length - offset;
126         }
127
128         if (rlength <= mlength) {        /* fits in allowed space */
129                 mlength = rlength;
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);
136
137                 return LNET_MATCHMD_DROP;
138         }
139
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);
146
147         lnet_commit_md(md, msg, offset, mlength);
148         md->md_offset = offset + mlength;
149
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)) {
155                 lnet_md_unlink(md);
156         }
157
158         return LNET_MATCHMD_OK;
159 }
160
161 int
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)
165 {
166         struct lnet_portal      *ptl = the_lnet.ln_portals[index];
167         cfs_list_t              *head;
168         lnet_me_t               *me;
169         lnet_me_t               *tmp;
170         lnet_libmd_t            *md;
171         int                     rc;
172
173         CDEBUG(D_NET, "Request from %s of length %d into portal %d "
174                "MB="LPX64"\n", libcfs_id2str(src), rlength, index, match_bits);
175
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;
180         }
181
182         head = lnet_ptl_me_head(index, src, match_bits);
183         if (head == NULL) /* nobody posted anything on this portal */
184                 goto out;
185
186         cfs_list_for_each_entry_safe(me, tmp, head, me_list) {
187                 md = me->me_md;
188
189                 /* ME attached but MD not attached yet */
190                 if (md == NULL)
191                         continue;
192
193                 LASSERT(me == md->md_me);
194
195                 rc = lnet_try_match_md(index, op_mask, src, rlength,
196                                        roffset, match_bits, md, msg);
197                 switch (rc) {
198                 default:
199                         LBUG();
200
201                 case LNET_MATCHMD_NONE:
202                         continue;
203
204                 case LNET_MATCHMD_OK:
205                         return LNET_MATCHMD_OK;
206
207                 case LNET_MATCHMD_DROP:
208                         return LNET_MATCHMD_DROP;
209                 }
210                 /* not reached */
211         }
212
213  out:
214         if (op_mask == LNET_MD_OP_GET ||
215             !lnet_ptl_is_lazy(ptl))
216                 return LNET_MATCHMD_DROP;
217
218         return LNET_MATCHMD_NONE;
219 }
220
221 /* called with LNET_LOCK held */
222 void
223 lnet_match_blocked_msg(lnet_libmd_t *md)
224 {
225         CFS_LIST_HEAD           (drops);
226         CFS_LIST_HEAD           (matches);
227         cfs_list_t              *tmp;
228         cfs_list_t              *entry;
229         lnet_msg_t              *msg;
230         struct lnet_portal      *ptl;
231         lnet_me_t               *me  = md->md_me;
232
233         LASSERT(me->me_portal < (unsigned int)the_lnet.ln_nportals);
234
235         ptl = the_lnet.ln_portals[me->me_portal];
236         if (!lnet_ptl_is_lazy(ptl)) {
237                 LASSERT(cfs_list_empty(&ptl->ptl_msgq));
238                 return;
239         }
240
241         LASSERT(md->md_refcount == 0); /* a brand new MD */
242
243         cfs_list_for_each_safe(entry, tmp, &ptl->ptl_msgq) {
244                 int               rc;
245                 int               index;
246                 lnet_hdr_t       *hdr;
247                 lnet_process_id_t src;
248
249                 msg = cfs_list_entry(entry, lnet_msg_t, msg_list);
250
251                 LASSERT(msg->msg_delayed);
252
253                 hdr   = &msg->msg_hdr;
254                 index = hdr->msg.put.ptl_index;
255
256                 src.nid = hdr->src_nid;
257                 src.pid = hdr->src_pid;
258
259                 rc = lnet_try_match_md(index, LNET_MD_OP_PUT, src,
260                                        hdr->payload_length,
261                                        hdr->msg.put.offset,
262                                        hdr->msg.put.match_bits, md, msg);
263
264                 if (rc == LNET_MATCHMD_NONE)
265                         continue;
266
267                 /* Hurrah! This _is_ a match */
268                 cfs_list_del(&msg->msg_list);
269                 ptl->ptl_msgq_version++;
270
271                 if (rc == LNET_MATCHMD_OK) {
272                         cfs_list_add_tail(&msg->msg_list, &matches);
273
274                         CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
275                                "match "LPU64" offset %d length %d.\n",
276                                libcfs_id2str(src),
277                                hdr->msg.put.ptl_index,
278                                hdr->msg.put.match_bits,
279                                hdr->msg.put.offset,
280                                hdr->payload_length);
281                 } else {
282                         LASSERT(rc == LNET_MATCHMD_DROP);
283
284                         cfs_list_add_tail(&msg->msg_list, &drops);
285                 }
286
287                 if (lnet_md_exhausted(md))
288                         break;
289         }
290
291         LNET_UNLOCK();
292
293         lnet_drop_delayed_msg_list(&drops, "Bad match");
294         lnet_recv_delayed_msg_list(&matches);
295
296         LNET_LOCK();
297 }
298
299 void
300 lnet_ptl_cleanup(struct lnet_portal *ptl)
301 {
302         lnet_me_t               *me;
303         int                     j;
304
305         LASSERT(cfs_list_empty(&ptl->ptl_msgq));
306         LASSERT(cfs_list_empty(&ptl->ptl_mlist));
307
308         if (ptl->ptl_mhash == NULL) /* uninitialized portal */
309                 return;
310
311         /* cleanup ME */
312         while (!cfs_list_empty(&ptl->ptl_mlist)) {
313                 me = cfs_list_entry(ptl->ptl_mlist.next,
314                                     lnet_me_t, me_list);
315                 CERROR("Active wildcard ME %p on exit\n", me);
316                 cfs_list_del(&me->me_list);
317                 lnet_me_free(me);
318         }
319
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,
323                                        lnet_me_t, me_list);
324                         CERROR("Active unique ME %p on exit\n", me);
325                         cfs_list_del(&me->me_list);
326                         lnet_me_free(me);
327                 }
328         }
329
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 */
333 }
334
335 int
336 lnet_ptl_setup(struct lnet_portal *ptl, int index)
337 {
338         cfs_list_t              *mhash;
339         int                     i;
340
341         ptl->ptl_index = index;
342         CFS_INIT_LIST_HEAD(&ptl->ptl_msgq);
343         CFS_INIT_LIST_HEAD(&ptl->ptl_mlist);
344
345         LIBCFS_ALLOC(mhash, sizeof(*mhash) * LNET_PORTAL_HASH_SIZE);
346         if (mhash == NULL) {
347                 CERROR("Failed to create match table for portal %d\n", index);
348                 return -ENOMEM;
349         }
350
351         for (i = 0; i < LNET_PORTAL_HASH_SIZE; i++)
352                 CFS_INIT_LIST_HEAD(&mhash[i]);
353
354         ptl->ptl_mhash = mhash; /* initialized */
355
356         return 0;
357 }
358
359 void
360 lnet_portals_destroy(void)
361 {
362         int     i;
363
364         if (the_lnet.ln_portals == NULL)
365                 return;
366
367         for (i = 0; i < the_lnet.ln_nportals; i++)
368                 lnet_ptl_cleanup(the_lnet.ln_portals[i]);
369
370         cfs_array_free(the_lnet.ln_portals);
371         the_lnet.ln_portals = NULL;
372 }
373
374 int
375 lnet_portals_create(void)
376 {
377         int     size;
378         int     i;
379
380         size = sizeof(struct lnet_portal);
381
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");
386                 return -ENOMEM;
387         }
388
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();
392                         return -ENOMEM;
393                 }
394         }
395
396         return 0;
397 }
398
399 /**
400  * Turn on the lazy portal attribute. Use with caution!
401  *
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.
407  *
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.
415  *
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.
421  *
422  * \param portal Index of the portal to enable the lazy attribute on.
423  *
424  * \retval 0       On success.
425  * \retval -EINVAL If \a portal is not a valid index.
426  */
427 int
428 LNetSetLazyPortal(int portal)
429 {
430         struct lnet_portal *ptl;
431
432         if (portal < 0 || portal >= the_lnet.ln_nportals)
433                 return -EINVAL;
434
435         CDEBUG(D_NET, "Setting portal %d lazy\n", portal);
436         ptl = the_lnet.ln_portals[portal];
437
438         LNET_LOCK();
439         lnet_ptl_setopt(ptl, LNET_PTL_LAZY);
440         LNET_UNLOCK();
441
442         return 0;
443 }
444
445 /**
446  * Turn off the lazy portal attribute. Delayed requests on the portal,
447  * if any, will be all dropped when this function returns.
448  *
449  * \param portal Index of the portal to disable the lazy attribute on.
450  *
451  * \retval 0       On success.
452  * \retval -EINVAL If \a portal is not a valid index.
453  */
454 int
455 LNetClearLazyPortal(int portal)
456 {
457         struct lnet_portal      *ptl;
458         CFS_LIST_HEAD           (zombies);
459
460         if (portal < 0 || portal >= the_lnet.ln_nportals)
461                 return -EINVAL;
462
463         ptl = the_lnet.ln_portals[portal];
464
465         LNET_LOCK();
466
467         if (!lnet_ptl_is_lazy(ptl)) {
468                 LNET_UNLOCK();
469                 return 0;
470         }
471
472         if (the_lnet.ln_shutdown)
473                 CWARN("Active lazy portal %d on exit\n", portal);
474         else
475                 CDEBUG(D_NET, "clearing portal %d lazy\n", portal);
476
477         /* grab all the blocked messages atomically */
478         cfs_list_splice_init(&ptl->ptl_msgq, &zombies);
479
480         ptl->ptl_msgq_version++;
481         lnet_ptl_unsetopt(ptl, LNET_PTL_LAZY);
482
483         LNET_UNLOCK();
484
485         lnet_drop_delayed_msg_list(&zombies, "Clearing lazy portal attr");
486
487         return 0;
488 }