Whamcloud - gitweb
LU-56 lnet: move "match" functions to lib-ptl.c
[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                   unsigned int *mlength_out, unsigned int *offset_out)
87 {
88         /* ALWAYS called holding the LNET_LOCK, and can't LNET_UNLOCK;
89          * lnet_match_blocked_msg() relies on this to avoid races */
90         unsigned int    offset;
91         unsigned int    mlength;
92         lnet_me_t       *me = md->md_me;
93
94         /* mismatched MD op */
95         if ((md->md_options & op_mask) == 0)
96                 return LNET_MATCHMD_NONE;
97
98         /* MD exhausted */
99         if (lnet_md_exhausted(md))
100                 return LNET_MATCHMD_NONE;
101
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;
106
107         if (me->me_match_id.pid != LNET_PID_ANY &&
108             me->me_match_id.pid != src.pid)
109                 return LNET_MATCHMD_NONE;
110
111         /* mismatched ME matchbits? */
112         if (((me->me_match_bits ^ match_bits) & ~me->me_ignore_bits) != 0)
113                 return LNET_MATCHMD_NONE;
114
115         /* Hurrah! This _is_ a match; check it out... */
116
117         if ((md->md_options & LNET_MD_MANAGE_REMOTE) == 0)
118                 offset = md->md_offset;
119         else
120                 offset = roffset;
121
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);
125         } else {
126                 mlength = md->md_length - offset;
127         }
128
129         if (rlength <= mlength) {        /* fits in allowed space */
130                 mlength = rlength;
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);
137
138                 return LNET_MATCHMD_DROP;
139         }
140
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);
147
148         lnet_commit_md(md, msg);
149         md->md_offset = offset + mlength;
150
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;
158
159         lnet_md_deconstruct(md, &msg->msg_ev.md);
160         lnet_md2handle(&msg->msg_ev.md_handle, md);
161
162         *offset_out = offset;
163         *mlength_out = mlength;
164
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)) {
170                 lnet_md_unlink(md);
171         }
172
173         return LNET_MATCHMD_OK;
174 }
175
176 int
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)
182 {
183         struct lnet_portal      *ptl = the_lnet.ln_portals[index];
184         cfs_list_t              *head;
185         lnet_me_t               *me;
186         lnet_me_t               *tmp;
187         lnet_libmd_t            *md;
188         int                     rc;
189
190         CDEBUG(D_NET, "Request from %s of length %d into portal %d "
191                "MB="LPX64"\n", libcfs_id2str(src), rlength, index, match_bits);
192
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;
197         }
198
199         head = lnet_ptl_me_head(index, src, match_bits);
200         if (head == NULL) /* nobody posted anything on this portal */
201                 goto out;
202
203         cfs_list_for_each_entry_safe(me, tmp, head, me_list) {
204                 md = me->me_md;
205
206                 /* ME attached but MD not attached yet */
207                 if (md == NULL)
208                         continue;
209
210                 LASSERT(me == md->md_me);
211
212                 rc = lnet_try_match_md(index, op_mask, src, rlength,
213                                        roffset, match_bits, md, msg,
214                                        mlength_out, offset_out);
215                 switch (rc) {
216                 default:
217                         LBUG();
218
219                 case LNET_MATCHMD_NONE:
220                         continue;
221
222                 case LNET_MATCHMD_OK:
223                         *md_out = md;
224                         return LNET_MATCHMD_OK;
225
226                 case LNET_MATCHMD_DROP:
227                         return LNET_MATCHMD_DROP;
228                 }
229                 /* not reached */
230         }
231
232  out:
233         if (op_mask == LNET_MD_OP_GET ||
234             !lnet_ptl_is_lazy(ptl))
235                 return LNET_MATCHMD_DROP;
236
237         return LNET_MATCHMD_NONE;
238 }
239
240 /* called with LNET_LOCK held */
241 void
242 lnet_match_blocked_msg(lnet_libmd_t *md)
243 {
244         CFS_LIST_HEAD           (drops);
245         CFS_LIST_HEAD           (matches);
246         cfs_list_t              *tmp;
247         cfs_list_t              *entry;
248         lnet_msg_t              *msg;
249         struct lnet_portal      *ptl;
250         lnet_me_t               *me  = md->md_me;
251
252         LASSERT(me->me_portal < (unsigned int)the_lnet.ln_nportals);
253
254         ptl = the_lnet.ln_portals[me->me_portal];
255         if (!lnet_ptl_is_lazy(ptl)) {
256                 LASSERT(cfs_list_empty(&ptl->ptl_msgq));
257                 return;
258         }
259
260         LASSERT(md->md_refcount == 0); /* a brand new MD */
261
262         cfs_list_for_each_safe(entry, tmp, &ptl->ptl_msgq) {
263                 int               rc;
264                 int               index;
265                 unsigned int      mlength;
266                 unsigned int      offset;
267                 lnet_hdr_t       *hdr;
268                 lnet_process_id_t src;
269
270                 msg = cfs_list_entry(entry, lnet_msg_t, msg_list);
271
272                 LASSERT(msg->msg_delayed);
273
274                 hdr   = &msg->msg_hdr;
275                 index = hdr->msg.put.ptl_index;
276
277                 src.nid = hdr->src_nid;
278                 src.pid = hdr->src_pid;
279
280                 rc = lnet_try_match_md(index, LNET_MD_OP_PUT, src,
281                                        hdr->payload_length,
282                                        hdr->msg.put.offset,
283                                        hdr->msg.put.match_bits,
284                                        md, msg, &mlength, &offset);
285
286                 if (rc == LNET_MATCHMD_NONE)
287                         continue;
288
289                 /* Hurrah! This _is_ a match */
290                 cfs_list_del(&msg->msg_list);
291                 ptl->ptl_msgq_version++;
292
293                 if (rc == LNET_MATCHMD_OK) {
294                         cfs_list_add_tail(&msg->msg_list, &matches);
295
296                         CDEBUG(D_NET, "Resuming delayed PUT from %s portal %d "
297                                "match "LPU64" offset %d length %d.\n",
298                                libcfs_id2str(src),
299                                hdr->msg.put.ptl_index,
300                                hdr->msg.put.match_bits,
301                                hdr->msg.put.offset,
302                                hdr->payload_length);
303                 } else {
304                         LASSERT(rc == LNET_MATCHMD_DROP);
305
306                         cfs_list_add_tail(&msg->msg_list, &drops);
307                 }
308
309                 if (lnet_md_exhausted(md))
310                         break;
311         }
312
313         LNET_UNLOCK();
314
315         lnet_drop_delayed_msg_list(&drops, "Bad match");
316         lnet_recv_delayed_msg_list(&matches);
317
318         LNET_LOCK();
319 }
320
321 void
322 lnet_ptl_cleanup(struct lnet_portal *ptl)
323 {
324         lnet_me_t               *me;
325         int                     j;
326
327         LASSERT(cfs_list_empty(&ptl->ptl_msgq));
328         LASSERT(cfs_list_empty(&ptl->ptl_mlist));
329
330         if (ptl->ptl_mhash == NULL) /* uninitialized portal */
331                 return;
332
333         /* cleanup ME */
334         while (!cfs_list_empty(&ptl->ptl_mlist)) {
335                 me = cfs_list_entry(ptl->ptl_mlist.next,
336                                     lnet_me_t, me_list);
337                 CERROR("Active wildcard ME %p on exit\n", me);
338                 cfs_list_del(&me->me_list);
339                 lnet_me_free(me);
340         }
341
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,
345                                        lnet_me_t, me_list);
346                         CERROR("Active unique ME %p on exit\n", me);
347                         cfs_list_del(&me->me_list);
348                         lnet_me_free(me);
349                 }
350         }
351
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 */
355 }
356
357 int
358 lnet_ptl_setup(struct lnet_portal *ptl, int index)
359 {
360         cfs_list_t              *mhash;
361         int                     i;
362
363         ptl->ptl_index = index;
364         CFS_INIT_LIST_HEAD(&ptl->ptl_msgq);
365         CFS_INIT_LIST_HEAD(&ptl->ptl_mlist);
366
367         LIBCFS_ALLOC(mhash, sizeof(*mhash) * LNET_PORTAL_HASH_SIZE);
368         if (mhash == NULL) {
369                 CERROR("Failed to create match table for portal %d\n", index);
370                 return -ENOMEM;
371         }
372
373         for (i = 0; i < LNET_PORTAL_HASH_SIZE; i++)
374                 CFS_INIT_LIST_HEAD(&mhash[i]);
375
376         ptl->ptl_mhash = mhash; /* initialized */
377
378         return 0;
379 }
380
381 void
382 lnet_portals_destroy(void)
383 {
384         int     i;
385
386         if (the_lnet.ln_portals == NULL)
387                 return;
388
389         for (i = 0; i < the_lnet.ln_nportals; i++)
390                 lnet_ptl_cleanup(the_lnet.ln_portals[i]);
391
392         cfs_array_free(the_lnet.ln_portals);
393         the_lnet.ln_portals = NULL;
394 }
395
396 int
397 lnet_portals_create(void)
398 {
399         int     size;
400         int     i;
401
402         size = sizeof(struct lnet_portal);
403
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");
408                 return -ENOMEM;
409         }
410
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();
414                         return -ENOMEM;
415                 }
416         }
417
418         return 0;
419 }
420
421 /**
422  * Turn on the lazy portal attribute. Use with caution!
423  *
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.
429  *
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.
437  *
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.
443  *
444  * \param portal Index of the portal to enable the lazy attribute on.
445  *
446  * \retval 0       On success.
447  * \retval -EINVAL If \a portal is not a valid index.
448  */
449 int
450 LNetSetLazyPortal(int portal)
451 {
452         struct lnet_portal *ptl;
453
454         if (portal < 0 || portal >= the_lnet.ln_nportals)
455                 return -EINVAL;
456
457         CDEBUG(D_NET, "Setting portal %d lazy\n", portal);
458         ptl = the_lnet.ln_portals[portal];
459
460         LNET_LOCK();
461         lnet_ptl_setopt(ptl, LNET_PTL_LAZY);
462         LNET_UNLOCK();
463
464         return 0;
465 }
466
467 /**
468  * Turn off the lazy portal attribute. Delayed requests on the portal,
469  * if any, will be all dropped when this function returns.
470  *
471  * \param portal Index of the portal to disable the lazy attribute on.
472  *
473  * \retval 0       On success.
474  * \retval -EINVAL If \a portal is not a valid index.
475  */
476 int
477 LNetClearLazyPortal(int portal)
478 {
479         struct lnet_portal      *ptl;
480         CFS_LIST_HEAD           (zombies);
481
482         if (portal < 0 || portal >= the_lnet.ln_nportals)
483                 return -EINVAL;
484
485         ptl = the_lnet.ln_portals[portal];
486
487         LNET_LOCK();
488
489         if (!lnet_ptl_is_lazy(ptl)) {
490                 LNET_UNLOCK();
491                 return 0;
492         }
493
494         if (the_lnet.ln_shutdown)
495                 CWARN("Active lazy portal %d on exit\n", portal);
496         else
497                 CDEBUG(D_NET, "clearing portal %d lazy\n", portal);
498
499         /* grab all the blocked messages atomically */
500         cfs_list_splice_init(&ptl->ptl_msgq, &zombies);
501
502         ptl->ptl_msgq_version++;
503         lnet_ptl_unsetopt(ptl, LNET_PTL_LAZY);
504
505         LNET_UNLOCK();
506
507         lnet_drop_delayed_msg_list(&zombies, "Clearing lazy portal attr");
508
509         return 0;
510 }