Whamcloud - gitweb
2957365d8760f14f26b30bb9ae1dccbf962be331
[fs/lustre-release.git] / lnet / klnds / gmlnd / gmlnd_comm.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2003 Los Alamos National Laboratory (LANL)
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  */
38
39 /*
40  *      This file contains all gmnal send and receive functions
41  */
42
43 #include "gmlnd.h"
44
45 void
46 gmnal_notify_peer_down(gmnal_tx_t *tx)
47 {
48         time_t             then;
49
50         then = cfs_time_current_sec() -
51                 cfs_duration_sec(cfs_time_current() -
52                                  tx->tx_launchtime);
53
54         lnet_notify(tx->tx_gmni->gmni_ni, tx->tx_nid, 0, then);
55 }
56
57 void
58 gmnal_pack_msg(gmnal_ni_t *gmni, gmnal_msg_t *msg,
59                lnet_nid_t dstnid, int type)
60 {
61         /* CAVEAT EMPTOR! this only sets the common message fields. */
62         msg->gmm_magic    = GMNAL_MSG_MAGIC;
63         msg->gmm_version  = GMNAL_MSG_VERSION;
64         msg->gmm_type     = type;
65         msg->gmm_srcnid   = gmni->gmni_ni->ni_nid;
66         msg->gmm_dstnid   = dstnid;
67 }
68
69 int
70 gmnal_unpack_msg(gmnal_ni_t *gmni, gmnal_rx_t *rx)
71 {
72         gmnal_msg_t *msg = GMNAL_NETBUF_MSG(&rx->rx_buf);
73         const int    hdr_size = offsetof(gmnal_msg_t, gmm_u);
74         int          buffnob = rx->rx_islarge ? gmni->gmni_large_msgsize :
75                                                 gmni->gmni_small_msgsize;
76         int          flip;
77
78         /* rc = 0:SUCCESS -ve:failure +ve:version mismatch */
79
80         /* GM may not overflow our buffer */
81         LASSERT (rx->rx_recv_nob <= buffnob);
82
83         /* 6 bytes are enough to have received magic + version */
84         if (rx->rx_recv_nob < 6) {
85                 CERROR("Short message from gmid %u: %d\n",
86                        rx->rx_recv_gmid, rx->rx_recv_nob);
87                 return -EPROTO;
88         }
89
90         if (msg->gmm_magic == GMNAL_MSG_MAGIC) {
91                 flip = 0;
92         } else if (msg->gmm_magic == __swab32(GMNAL_MSG_MAGIC)) {
93                 flip = 1;
94         } else if (msg->gmm_magic == LNET_PROTO_MAGIC ||
95                    msg->gmm_magic == __swab32(LNET_PROTO_MAGIC)) {
96                 return EPROTO;
97         } else {
98                 CERROR("Bad magic from gmid %u: %08x\n",
99                        rx->rx_recv_gmid, msg->gmm_magic);
100                 return -EPROTO;
101         }
102
103         if (msg->gmm_version !=
104             (flip ? __swab16(GMNAL_MSG_VERSION) : GMNAL_MSG_VERSION)) {
105                 return EPROTO;
106         }
107
108         if (rx->rx_recv_nob < hdr_size) {
109                 CERROR("Short message from %u: %d\n",
110                        rx->rx_recv_gmid, rx->rx_recv_nob);
111                 return -EPROTO;
112         }
113
114         if (flip) {
115                 /* leave magic unflipped as a clue to peer endianness */
116                 __swab16s(&msg->gmm_version);
117                 __swab16s(&msg->gmm_type);
118                 __swab64s(&msg->gmm_srcnid);
119                 __swab64s(&msg->gmm_dstnid);
120         }
121
122         if (msg->gmm_srcnid == LNET_NID_ANY) {
123                 CERROR("Bad src nid from %u: %s\n",
124                        rx->rx_recv_gmid, libcfs_nid2str(msg->gmm_srcnid));
125                 return -EPROTO;
126         }
127
128         if (gmni->gmni_ni->ni_nid != msg->gmm_dstnid) {
129                 CERROR("Bad dst nid from %u: %s\n",
130                        rx->rx_recv_gmid, libcfs_nid2str(msg->gmm_dstnid));
131                 return -EPROTO;
132         }
133
134         switch (msg->gmm_type) {
135         default:
136                 CERROR("Unknown message type from %u: %x\n",
137                        rx->rx_recv_gmid, msg->gmm_type);
138                 return -EPROTO;
139
140         case GMNAL_MSG_IMMEDIATE:
141                 if (rx->rx_recv_nob < offsetof(gmnal_msg_t, gmm_u.immediate.gmim_payload[0])) {
142                         CERROR("Short IMMEDIATE from %u: %d("LPSZ")\n",
143                                rx->rx_recv_gmid, rx->rx_recv_nob,
144                                offsetof(gmnal_msg_t, gmm_u.immediate.gmim_payload[0]));
145                         return -EPROTO;
146                 }
147                 break;
148         }
149         return 0;
150 }
151
152 gmnal_tx_t *
153 gmnal_get_tx(gmnal_ni_t *gmni)
154 {
155         gmnal_tx_t *tx = NULL;
156
157         spin_lock(&gmni->gmni_tx_lock);
158
159         if (gmni->gmni_shutdown ||
160             list_empty(&gmni->gmni_idle_txs)) {
161                 spin_unlock(&gmni->gmni_tx_lock);
162                 return NULL;
163         }
164
165         tx = list_entry(gmni->gmni_idle_txs.next, gmnal_tx_t, tx_list);
166         list_del(&tx->tx_list);
167
168         spin_unlock(&gmni->gmni_tx_lock);
169
170         LASSERT (tx->tx_lntmsg == NULL);
171         LASSERT (tx->tx_ltxb == NULL);
172         LASSERT (!tx->tx_credit);
173
174         return tx;
175 }
176
177 void
178 gmnal_tx_done(gmnal_tx_t *tx, int rc)
179 {
180         gmnal_ni_t *gmni = tx->tx_gmni;
181         int         wake_sched = 0;
182         lnet_msg_t *lnetmsg = tx->tx_lntmsg;
183
184         tx->tx_lntmsg = NULL;
185
186         spin_lock(&gmni->gmni_tx_lock);
187
188         if (tx->tx_ltxb != NULL) {
189                 wake_sched = 1;
190                 list_add_tail(&tx->tx_ltxb->txb_list, &gmni->gmni_idle_ltxbs);
191                 tx->tx_ltxb = NULL;
192         }
193
194         if (tx->tx_credit) {
195                 wake_sched = 1;
196                 gmni->gmni_tx_credits++;
197                 tx->tx_credit = 0;
198         }
199
200         list_add_tail(&tx->tx_list, &gmni->gmni_idle_txs);
201
202         if (wake_sched)
203                 gmnal_check_txqueues_locked(gmni);
204
205         spin_unlock(&gmni->gmni_tx_lock);
206
207         /* Delay finalize until tx is free */
208         if (lnetmsg != NULL)
209                 lnet_finalize(gmni->gmni_ni, lnetmsg, rc);
210 }
211
212 void
213 gmnal_drop_sends_callback(struct gm_port *gm_port, void *context,
214                           gm_status_t status)
215 {
216         gmnal_tx_t *tx = (gmnal_tx_t*)context;
217
218         LASSERT(!in_interrupt());
219
220         CDEBUG(D_NET, "status for tx [%p] is [%d][%s], nid %s\n",
221                tx, status, gmnal_gmstatus2str(status),
222                libcfs_nid2str(tx->tx_nid));
223
224         gmnal_tx_done(tx, -EIO);
225 }
226
227 void
228 gmnal_tx_callback(gm_port_t *gm_port, void *context, gm_status_t status)
229 {
230         gmnal_tx_t *tx = (gmnal_tx_t*)context;
231         gmnal_ni_t *gmni = tx->tx_gmni;
232
233         LASSERT(!in_interrupt());
234
235         switch(status) {
236         case GM_SUCCESS:
237                 gmnal_tx_done(tx, 0);
238                 return;
239
240         case GM_SEND_DROPPED:
241                 CDEBUG(D_NETERROR, "Dropped tx %p to %s\n",
242                        tx, libcfs_nid2str(tx->tx_nid));
243                 /* Another tx failed and called gm_drop_sends() which made this
244                  * one complete immediately */
245                 gmnal_tx_done(tx, -EIO);
246                 return;
247
248         default:
249                 /* Some error; NB don't complete tx yet; we need its credit for
250                  * gm_drop_sends() */
251                 CDEBUG(D_NETERROR, "tx %p error %d(%s), nid %s\n",
252                        tx, status, gmnal_gmstatus2str(status),
253                        libcfs_nid2str(tx->tx_nid));
254
255                 gmnal_notify_peer_down(tx);
256
257                 spin_lock(&gmni->gmni_gm_lock);
258                 gm_drop_sends(gmni->gmni_port,
259                               tx->tx_ltxb != NULL ?
260                               GMNAL_LARGE_PRIORITY : GMNAL_SMALL_PRIORITY,
261                               tx->tx_gmlid, *gmnal_tunables.gm_port,
262                               gmnal_drop_sends_callback, tx);
263                 spin_unlock(&gmni->gmni_gm_lock);
264                 return;
265         }
266
267         /* not reached */
268         LBUG();
269 }
270
271 void
272 gmnal_check_txqueues_locked (gmnal_ni_t *gmni)
273 {
274         gmnal_tx_t    *tx;
275         gmnal_txbuf_t *ltxb;
276         int            gmsize;
277         int            pri;
278         void          *netaddr;
279
280         tx = list_empty(&gmni->gmni_buf_txq) ? NULL :
281              list_entry(gmni->gmni_buf_txq.next, gmnal_tx_t, tx_list);
282
283         if (tx != NULL &&
284             (tx->tx_large_nob == 0 ||
285              !list_empty(&gmni->gmni_idle_ltxbs))) {
286
287                 /* consume tx */
288                 list_del(&tx->tx_list);
289
290                 LASSERT (tx->tx_ltxb == NULL);
291
292                 if (tx->tx_large_nob != 0) {
293                         ltxb = list_entry(gmni->gmni_idle_ltxbs.next,
294                                           gmnal_txbuf_t, txb_list);
295
296                         /* consume large buffer */
297                         list_del(&ltxb->txb_list);
298
299                         spin_unlock(&gmni->gmni_tx_lock);
300
301                         /* Unlocking here allows sends to get re-ordered,
302                          * but we want to allow other CPUs to progress... */
303
304                         tx->tx_ltxb = ltxb;
305
306                         /* marshall message in tx_ltxb...
307                          * 1. Copy what was marshalled so far (in tx_buf) */
308                         memcpy(GMNAL_NETBUF_MSG(&ltxb->txb_buf),
309                                GMNAL_NETBUF_MSG(&tx->tx_buf), tx->tx_msgnob);
310
311                         /* 2. Copy the payload */
312                         if (tx->tx_large_iskiov)
313                                 lnet_copy_kiov2kiov(
314                                         gmni->gmni_large_pages,
315                                         ltxb->txb_buf.nb_kiov,
316                                         tx->tx_msgnob,
317                                         tx->tx_large_niov,
318                                         tx->tx_large_frags.kiov,
319                                         tx->tx_large_offset,
320                                         tx->tx_large_nob);
321                         else
322                                 lnet_copy_iov2kiov(
323                                         gmni->gmni_large_pages,
324                                         ltxb->txb_buf.nb_kiov,
325                                         tx->tx_msgnob,
326                                         tx->tx_large_niov,
327                                         tx->tx_large_frags.iov,
328                                         tx->tx_large_offset,
329                                         tx->tx_large_nob);
330
331                         tx->tx_msgnob += tx->tx_large_nob;
332
333                         spin_lock(&gmni->gmni_tx_lock);
334                 }
335
336                 list_add_tail(&tx->tx_list, &gmni->gmni_cred_txq);
337         }
338
339         if (!list_empty(&gmni->gmni_cred_txq) &&
340             gmni->gmni_tx_credits != 0) {
341
342                 tx = list_entry(gmni->gmni_cred_txq.next, gmnal_tx_t, tx_list);
343
344                 /* consume tx and 1 credit */
345                 list_del(&tx->tx_list);
346                 gmni->gmni_tx_credits--;
347
348                 spin_unlock(&gmni->gmni_tx_lock);
349
350                 /* Unlocking here allows sends to get re-ordered, but we want
351                  * to allow other CPUs to progress... */
352
353                 LASSERT(!tx->tx_credit);
354                 tx->tx_credit = 1;
355
356                 tx->tx_launchtime = cfs_time_current();
357
358                 if (tx->tx_msgnob <= gmni->gmni_small_msgsize) {
359                         LASSERT (tx->tx_ltxb == NULL);
360                         netaddr = GMNAL_NETBUF_LOCAL_NETADDR(&tx->tx_buf);
361                         gmsize = gmni->gmni_small_gmsize;
362                         pri = GMNAL_SMALL_PRIORITY;
363                 } else {
364                         LASSERT (tx->tx_ltxb != NULL);
365                         netaddr = GMNAL_NETBUF_LOCAL_NETADDR(&tx->tx_ltxb->txb_buf);
366                         gmsize = gmni->gmni_large_gmsize;
367                         pri = GMNAL_LARGE_PRIORITY;
368                 }
369
370                 spin_lock(&gmni->gmni_gm_lock);
371
372                 gm_send_to_peer_with_callback(gmni->gmni_port,
373                                               netaddr, gmsize,
374                                               tx->tx_msgnob,
375                                               pri,
376                                               tx->tx_gmlid,
377                                               gmnal_tx_callback,
378                                               (void*)tx);
379
380                 spin_unlock(&gmni->gmni_gm_lock);
381                 spin_lock(&gmni->gmni_tx_lock);
382         }
383 }
384
385 void
386 gmnal_post_rx(gmnal_ni_t *gmni, gmnal_rx_t *rx)
387 {
388         int   gmsize = rx->rx_islarge ? gmni->gmni_large_gmsize :
389                                         gmni->gmni_small_gmsize;
390         int   pri    = rx->rx_islarge ? GMNAL_LARGE_PRIORITY :
391                                         GMNAL_SMALL_PRIORITY;
392         void *buffer = GMNAL_NETBUF_LOCAL_NETADDR(&rx->rx_buf);
393
394         CDEBUG(D_NET, "posting rx %p buf %p\n", rx, buffer);
395
396         spin_lock(&gmni->gmni_gm_lock);
397         gm_provide_receive_buffer_with_tag(gmni->gmni_port,
398                                            buffer, gmsize, pri, 0);
399         spin_unlock(&gmni->gmni_gm_lock);
400 }
401
402 void
403 gmnal_version_reply (gmnal_ni_t *gmni, gmnal_rx_t *rx)
404 {
405         /* Future protocol version compatibility support!
406          * The next gmlnd-specific protocol rev will first send a message to
407          * check version; I reply with a stub message containing my current
408          * magic+version... */
409         gmnal_msg_t *msg;
410         gmnal_tx_t  *tx = gmnal_get_tx(gmni);
411
412         if (tx == NULL) {
413                 CERROR("Can't allocate tx to send version info to %u\n",
414                        rx->rx_recv_gmid);
415                 return;
416         }
417
418         LASSERT (tx->tx_lntmsg == NULL);        /* no finalize */
419
420         tx->tx_nid = LNET_NID_ANY;
421         tx->tx_gmlid = rx->rx_recv_gmid;
422
423         msg = GMNAL_NETBUF_MSG(&tx->tx_buf);
424         msg->gmm_magic   = GMNAL_MSG_MAGIC;
425         msg->gmm_version = GMNAL_MSG_VERSION;
426
427         /* just send magic + version */
428         tx->tx_msgnob = offsetof(gmnal_msg_t, gmm_type);
429         tx->tx_large_nob = 0;
430
431         spin_lock(&gmni->gmni_tx_lock);
432
433         list_add_tail(&tx->tx_list, &gmni->gmni_buf_txq);
434         gmnal_check_txqueues_locked(gmni);
435
436         spin_unlock(&gmni->gmni_tx_lock);
437 }
438
439 int
440 gmnal_rx_thread(void *arg)
441 {
442         gmnal_ni_t      *gmni = arg;
443         gm_recv_event_t *rxevent = NULL;
444         gm_recv_t       *recv = NULL;
445         gmnal_rx_t      *rx;
446         int              rc;
447
448         cfs_daemonize("gmnal_rxd");
449
450         while (!gmni->gmni_shutdown) {
451                 rc = down_interruptible(&gmni->gmni_rx_mutex);
452                 LASSERT (rc == 0 || rc == -EINTR);
453                 if (rc != 0)
454                         continue;
455
456                 spin_lock(&gmni->gmni_gm_lock);
457                 rxevent = gm_blocking_receive_no_spin(gmni->gmni_port);
458                 spin_unlock(&gmni->gmni_gm_lock);
459
460                 switch (GM_RECV_EVENT_TYPE(rxevent)) {
461                 default:
462                         gm_unknown(gmni->gmni_port, rxevent);
463                         up(&gmni->gmni_rx_mutex);
464                         continue;
465
466                 case GM_FAST_RECV_EVENT:
467                 case GM_FAST_PEER_RECV_EVENT:
468                 case GM_PEER_RECV_EVENT:
469                 case GM_FAST_HIGH_RECV_EVENT:
470                 case GM_FAST_HIGH_PEER_RECV_EVENT:
471                 case GM_HIGH_PEER_RECV_EVENT:
472                 case GM_RECV_EVENT:
473                 case GM_HIGH_RECV_EVENT:
474                         break;
475                 }
476
477                 recv = &rxevent->recv;
478                 rx = gm_hash_find(gmni->gmni_rx_hash,
479                                   gm_ntohp(recv->buffer));
480                 LASSERT (rx != NULL);
481
482                 rx->rx_recv_nob  = gm_ntoh_u32(recv->length);
483                 rx->rx_recv_gmid = gm_ntoh_u16(recv->sender_node_id);
484                 rx->rx_recv_port = gm_ntoh_u8(recv->sender_port_id);
485                 rx->rx_recv_type = gm_ntoh_u8(recv->type);
486
487                 switch (GM_RECV_EVENT_TYPE(rxevent)) {
488                 case GM_FAST_RECV_EVENT:
489                 case GM_FAST_PEER_RECV_EVENT:
490                 case GM_FAST_HIGH_RECV_EVENT:
491                 case GM_FAST_HIGH_PEER_RECV_EVENT:
492                         LASSERT (rx->rx_recv_nob <= PAGE_SIZE);
493
494                         memcpy(GMNAL_NETBUF_MSG(&rx->rx_buf),
495                                gm_ntohp(recv->message), rx->rx_recv_nob);
496                         break;
497                 }
498
499                 up(&gmni->gmni_rx_mutex);
500
501                 CDEBUG (D_NET, "rx %p: buf %p(%p) nob %d\n", rx,
502                         GMNAL_NETBUF_LOCAL_NETADDR(&rx->rx_buf),
503                         gm_ntohp(recv->buffer), rx->rx_recv_nob);
504
505                 /* We're connectionless: simply drop packets with
506                  * errors */
507                 rc = gmnal_unpack_msg(gmni, rx);
508
509                 if (rc == 0) {
510                         gmnal_msg_t *msg = GMNAL_NETBUF_MSG(&rx->rx_buf);
511
512                         LASSERT (msg->gmm_type == GMNAL_MSG_IMMEDIATE);
513                         rc = lnet_parse(gmni->gmni_ni,
514                                         &msg->gmm_u.immediate.gmim_hdr,
515                                         msg->gmm_srcnid, rx, 0);
516                 } else if (rc > 0) {
517                         gmnal_version_reply(gmni, rx);
518                         rc = -EPROTO;           /* repost rx */
519                 }
520
521                 if (rc < 0)                     /* parse failure */
522                         gmnal_post_rx(gmni, rx);
523         }
524
525         CDEBUG(D_NET, "exiting\n");
526         atomic_dec(&gmni->gmni_nthreads);
527         return 0;
528 }
529
530 void
531 gmnal_stop_threads(gmnal_ni_t *gmni)
532 {
533         int count = 2;
534
535         gmni->gmni_shutdown = 1;
536         mb();
537
538         /* wake rxthread owning gmni_rx_mutex with an alarm. */
539         spin_lock(&gmni->gmni_gm_lock);
540         gm_set_alarm(gmni->gmni_port, &gmni->gmni_alarm, 0, NULL, NULL);
541         spin_unlock(&gmni->gmni_gm_lock);
542
543         while (atomic_read(&gmni->gmni_nthreads) != 0) {
544                 count++;
545                 if ((count & (count - 1)) == 0)
546                         CWARN("Waiting for %d threads to stop\n",
547                               atomic_read(&gmni->gmni_nthreads));
548                 gmnal_yield(1);
549         }
550 }
551
552 int
553 gmnal_start_threads(gmnal_ni_t *gmni)
554 {
555         int     i;
556         int     pid;
557
558         LASSERT (!gmni->gmni_shutdown);
559         LASSERT (atomic_read(&gmni->gmni_nthreads) == 0);
560
561         gm_initialize_alarm(&gmni->gmni_alarm);
562
563         for (i = 0; i < num_online_cpus(); i++) {
564
565                 pid = kernel_thread(gmnal_rx_thread, (void*)gmni, 0);
566                 if (pid < 0) {
567                         CERROR("rx thread failed to start: %d\n", pid);
568                         gmnal_stop_threads(gmni);
569                         return pid;
570                 }
571
572                 atomic_inc(&gmni->gmni_nthreads);
573         }
574
575         return 0;
576 }