Whamcloud - gitweb
b=20339
[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 <
142                     offsetof(gmnal_msg_t, gmm_u.immediate.gmim_payload[0])) {
143                         CERROR("Short IMMEDIATE from %u: %d(%lu)\n",
144                                rx->rx_recv_gmid, rx->rx_recv_nob,
145                                (long)offsetof(gmnal_msg_t,
146                                               gmm_u.immediate.gmim_payload[0]));
147                         return -EPROTO;
148                 }
149                 break;
150         }
151         return 0;
152 }
153
154 gmnal_tx_t *
155 gmnal_get_tx(gmnal_ni_t *gmni)
156 {
157         gmnal_tx_t *tx = NULL;
158
159         spin_lock(&gmni->gmni_tx_lock);
160
161         if (gmni->gmni_shutdown ||
162             list_empty(&gmni->gmni_idle_txs)) {
163                 spin_unlock(&gmni->gmni_tx_lock);
164                 return NULL;
165         }
166
167         tx = list_entry(gmni->gmni_idle_txs.next, gmnal_tx_t, tx_list);
168         list_del(&tx->tx_list);
169
170         spin_unlock(&gmni->gmni_tx_lock);
171
172         LASSERT (tx->tx_lntmsg == NULL);
173         LASSERT (tx->tx_ltxb == NULL);
174         LASSERT (!tx->tx_credit);
175
176         return tx;
177 }
178
179 void
180 gmnal_tx_done(gmnal_tx_t *tx, int rc)
181 {
182         gmnal_ni_t *gmni = tx->tx_gmni;
183         int         wake_sched = 0;
184         lnet_msg_t *lnetmsg = tx->tx_lntmsg;
185
186         tx->tx_lntmsg = NULL;
187
188         spin_lock(&gmni->gmni_tx_lock);
189
190         if (tx->tx_ltxb != NULL) {
191                 wake_sched = 1;
192                 list_add_tail(&tx->tx_ltxb->txb_list, &gmni->gmni_idle_ltxbs);
193                 tx->tx_ltxb = NULL;
194         }
195
196         if (tx->tx_credit) {
197                 wake_sched = 1;
198                 gmni->gmni_tx_credits++;
199                 tx->tx_credit = 0;
200         }
201
202         list_add_tail(&tx->tx_list, &gmni->gmni_idle_txs);
203
204         if (wake_sched)
205                 gmnal_check_txqueues_locked(gmni);
206
207         spin_unlock(&gmni->gmni_tx_lock);
208
209         /* Delay finalize until tx is free */
210         if (lnetmsg != NULL)
211                 lnet_finalize(gmni->gmni_ni, lnetmsg, rc);
212 }
213
214 void
215 gmnal_drop_sends_callback(struct gm_port *gm_port, void *context,
216                           gm_status_t status)
217 {
218         gmnal_tx_t *tx = (gmnal_tx_t*)context;
219
220         LASSERT(!in_interrupt());
221
222         CDEBUG(D_NET, "status for tx [%p] is [%d][%s], nid %s\n",
223                tx, status, gmnal_gmstatus2str(status),
224                libcfs_nid2str(tx->tx_nid));
225
226         gmnal_tx_done(tx, -EIO);
227 }
228
229 void
230 gmnal_tx_callback(gm_port_t *gm_port, void *context, gm_status_t status)
231 {
232         gmnal_tx_t *tx = (gmnal_tx_t*)context;
233         gmnal_ni_t *gmni = tx->tx_gmni;
234
235         LASSERT(!in_interrupt());
236
237         switch(status) {
238         case GM_SUCCESS:
239                 gmnal_tx_done(tx, 0);
240                 return;
241
242         case GM_SEND_DROPPED:
243                 CDEBUG(D_NETERROR, "Dropped tx %p to %s\n",
244                        tx, libcfs_nid2str(tx->tx_nid));
245                 /* Another tx failed and called gm_drop_sends() which made this
246                  * one complete immediately */
247                 gmnal_tx_done(tx, -EIO);
248                 return;
249
250         default:
251                 /* Some error; NB don't complete tx yet; we need its credit for
252                  * gm_drop_sends() */
253                 CDEBUG(D_NETERROR, "tx %p error %d(%s), nid %s\n",
254                        tx, status, gmnal_gmstatus2str(status),
255                        libcfs_nid2str(tx->tx_nid));
256
257                 gmnal_notify_peer_down(tx);
258
259                 spin_lock(&gmni->gmni_gm_lock);
260                 gm_drop_sends(gmni->gmni_port,
261                               tx->tx_ltxb != NULL ?
262                               GMNAL_LARGE_PRIORITY : GMNAL_SMALL_PRIORITY,
263                               tx->tx_gmlid, *gmnal_tunables.gm_port,
264                               gmnal_drop_sends_callback, tx);
265                 spin_unlock(&gmni->gmni_gm_lock);
266                 return;
267         }
268
269         /* not reached */
270         LBUG();
271 }
272
273 void
274 gmnal_check_txqueues_locked (gmnal_ni_t *gmni)
275 {
276         gmnal_tx_t    *tx;
277         gmnal_txbuf_t *ltxb;
278         int            gmsize;
279         int            pri;
280         void          *netaddr;
281
282         tx = list_empty(&gmni->gmni_buf_txq) ? NULL :
283              list_entry(gmni->gmni_buf_txq.next, gmnal_tx_t, tx_list);
284
285         if (tx != NULL &&
286             (tx->tx_large_nob == 0 ||
287              !list_empty(&gmni->gmni_idle_ltxbs))) {
288
289                 /* consume tx */
290                 list_del(&tx->tx_list);
291
292                 LASSERT (tx->tx_ltxb == NULL);
293
294                 if (tx->tx_large_nob != 0) {
295                         ltxb = list_entry(gmni->gmni_idle_ltxbs.next,
296                                           gmnal_txbuf_t, txb_list);
297
298                         /* consume large buffer */
299                         list_del(&ltxb->txb_list);
300
301                         spin_unlock(&gmni->gmni_tx_lock);
302
303                         /* Unlocking here allows sends to get re-ordered,
304                          * but we want to allow other CPUs to progress... */
305
306                         tx->tx_ltxb = ltxb;
307
308                         /* marshall message in tx_ltxb...
309                          * 1. Copy what was marshalled so far (in tx_buf) */
310                         memcpy(GMNAL_NETBUF_MSG(&ltxb->txb_buf),
311                                GMNAL_NETBUF_MSG(&tx->tx_buf), tx->tx_msgnob);
312
313                         /* 2. Copy the payload */
314                         if (tx->tx_large_iskiov)
315                                 lnet_copy_kiov2kiov(
316                                         gmni->gmni_large_pages,
317                                         ltxb->txb_buf.nb_kiov,
318                                         tx->tx_msgnob,
319                                         tx->tx_large_niov,
320                                         tx->tx_large_frags.kiov,
321                                         tx->tx_large_offset,
322                                         tx->tx_large_nob);
323                         else
324                                 lnet_copy_iov2kiov(
325                                         gmni->gmni_large_pages,
326                                         ltxb->txb_buf.nb_kiov,
327                                         tx->tx_msgnob,
328                                         tx->tx_large_niov,
329                                         tx->tx_large_frags.iov,
330                                         tx->tx_large_offset,
331                                         tx->tx_large_nob);
332
333                         tx->tx_msgnob += tx->tx_large_nob;
334
335                         spin_lock(&gmni->gmni_tx_lock);
336                 }
337
338                 list_add_tail(&tx->tx_list, &gmni->gmni_cred_txq);
339         }
340
341         if (!list_empty(&gmni->gmni_cred_txq) &&
342             gmni->gmni_tx_credits != 0) {
343
344                 tx = list_entry(gmni->gmni_cred_txq.next, gmnal_tx_t, tx_list);
345
346                 /* consume tx and 1 credit */
347                 list_del(&tx->tx_list);
348                 gmni->gmni_tx_credits--;
349
350                 spin_unlock(&gmni->gmni_tx_lock);
351
352                 /* Unlocking here allows sends to get re-ordered, but we want
353                  * to allow other CPUs to progress... */
354
355                 LASSERT(!tx->tx_credit);
356                 tx->tx_credit = 1;
357
358                 tx->tx_launchtime = cfs_time_current();
359
360                 if (tx->tx_msgnob <= gmni->gmni_small_msgsize) {
361                         LASSERT (tx->tx_ltxb == NULL);
362                         netaddr = GMNAL_NETBUF_LOCAL_NETADDR(&tx->tx_buf);
363                         gmsize = gmni->gmni_small_gmsize;
364                         pri = GMNAL_SMALL_PRIORITY;
365                 } else {
366                         LASSERT (tx->tx_ltxb != NULL);
367                         netaddr = GMNAL_NETBUF_LOCAL_NETADDR(&tx->tx_ltxb->txb_buf);
368                         gmsize = gmni->gmni_large_gmsize;
369                         pri = GMNAL_LARGE_PRIORITY;
370                 }
371
372                 spin_lock(&gmni->gmni_gm_lock);
373
374                 gm_send_to_peer_with_callback(gmni->gmni_port,
375                                               netaddr, gmsize,
376                                               tx->tx_msgnob,
377                                               pri,
378                                               tx->tx_gmlid,
379                                               gmnal_tx_callback,
380                                               (void*)tx);
381
382                 spin_unlock(&gmni->gmni_gm_lock);
383                 spin_lock(&gmni->gmni_tx_lock);
384         }
385 }
386
387 void
388 gmnal_post_rx(gmnal_ni_t *gmni, gmnal_rx_t *rx)
389 {
390         int   gmsize = rx->rx_islarge ? gmni->gmni_large_gmsize :
391                                         gmni->gmni_small_gmsize;
392         int   pri    = rx->rx_islarge ? GMNAL_LARGE_PRIORITY :
393                                         GMNAL_SMALL_PRIORITY;
394         void *buffer = GMNAL_NETBUF_LOCAL_NETADDR(&rx->rx_buf);
395
396         CDEBUG(D_NET, "posting rx %p buf %p\n", rx, buffer);
397
398         spin_lock(&gmni->gmni_gm_lock);
399         gm_provide_receive_buffer_with_tag(gmni->gmni_port,
400                                            buffer, gmsize, pri, 0);
401         spin_unlock(&gmni->gmni_gm_lock);
402 }
403
404 void
405 gmnal_version_reply (gmnal_ni_t *gmni, gmnal_rx_t *rx)
406 {
407         /* Future protocol version compatibility support!
408          * The next gmlnd-specific protocol rev will first send a message to
409          * check version; I reply with a stub message containing my current
410          * magic+version... */
411         gmnal_msg_t *msg;
412         gmnal_tx_t  *tx = gmnal_get_tx(gmni);
413
414         if (tx == NULL) {
415                 CERROR("Can't allocate tx to send version info to %u\n",
416                        rx->rx_recv_gmid);
417                 return;
418         }
419
420         LASSERT (tx->tx_lntmsg == NULL);        /* no finalize */
421
422         tx->tx_nid = LNET_NID_ANY;
423         tx->tx_gmlid = rx->rx_recv_gmid;
424
425         msg = GMNAL_NETBUF_MSG(&tx->tx_buf);
426         msg->gmm_magic   = GMNAL_MSG_MAGIC;
427         msg->gmm_version = GMNAL_MSG_VERSION;
428
429         /* just send magic + version */
430         tx->tx_msgnob = offsetof(gmnal_msg_t, gmm_type);
431         tx->tx_large_nob = 0;
432
433         spin_lock(&gmni->gmni_tx_lock);
434
435         list_add_tail(&tx->tx_list, &gmni->gmni_buf_txq);
436         gmnal_check_txqueues_locked(gmni);
437
438         spin_unlock(&gmni->gmni_tx_lock);
439 }
440
441 int
442 gmnal_rx_thread(void *arg)
443 {
444         gmnal_ni_t      *gmni = arg;
445         gm_recv_event_t *rxevent = NULL;
446         gm_recv_t       *recv = NULL;
447         gmnal_rx_t      *rx;
448         int              rc;
449
450         cfs_daemonize("gmnal_rxd");
451
452         while (!gmni->gmni_shutdown) {
453                 rc = down_interruptible(&gmni->gmni_rx_mutex);
454                 LASSERT (rc == 0 || rc == -EINTR);
455                 if (rc != 0)
456                         continue;
457
458                 spin_lock(&gmni->gmni_gm_lock);
459                 rxevent = gm_blocking_receive_no_spin(gmni->gmni_port);
460                 spin_unlock(&gmni->gmni_gm_lock);
461
462                 switch (GM_RECV_EVENT_TYPE(rxevent)) {
463                 default:
464                         gm_unknown(gmni->gmni_port, rxevent);
465                         up(&gmni->gmni_rx_mutex);
466                         continue;
467
468                 case GM_FAST_RECV_EVENT:
469                 case GM_FAST_PEER_RECV_EVENT:
470                 case GM_PEER_RECV_EVENT:
471                 case GM_FAST_HIGH_RECV_EVENT:
472                 case GM_FAST_HIGH_PEER_RECV_EVENT:
473                 case GM_HIGH_PEER_RECV_EVENT:
474                 case GM_RECV_EVENT:
475                 case GM_HIGH_RECV_EVENT:
476                         break;
477                 }
478
479                 recv = &rxevent->recv;
480                 rx = gm_hash_find(gmni->gmni_rx_hash,
481                                   gm_ntohp(recv->buffer));
482                 LASSERT (rx != NULL);
483
484                 rx->rx_recv_nob  = gm_ntoh_u32(recv->length);
485                 rx->rx_recv_gmid = gm_ntoh_u16(recv->sender_node_id);
486                 rx->rx_recv_port = gm_ntoh_u8(recv->sender_port_id);
487                 rx->rx_recv_type = gm_ntoh_u8(recv->type);
488
489                 switch (GM_RECV_EVENT_TYPE(rxevent)) {
490                 case GM_FAST_RECV_EVENT:
491                 case GM_FAST_PEER_RECV_EVENT:
492                 case GM_FAST_HIGH_RECV_EVENT:
493                 case GM_FAST_HIGH_PEER_RECV_EVENT:
494                         LASSERT (rx->rx_recv_nob <= PAGE_SIZE);
495
496                         memcpy(GMNAL_NETBUF_MSG(&rx->rx_buf),
497                                gm_ntohp(recv->message), rx->rx_recv_nob);
498                         break;
499                 }
500
501                 up(&gmni->gmni_rx_mutex);
502
503                 CDEBUG (D_NET, "rx %p: buf %p(%p) nob %d\n", rx,
504                         GMNAL_NETBUF_LOCAL_NETADDR(&rx->rx_buf),
505                         gm_ntohp(recv->buffer), rx->rx_recv_nob);
506
507                 /* We're connectionless: simply drop packets with
508                  * errors */
509                 rc = gmnal_unpack_msg(gmni, rx);
510
511                 if (rc == 0) {
512                         gmnal_msg_t *msg = GMNAL_NETBUF_MSG(&rx->rx_buf);
513
514                         LASSERT (msg->gmm_type == GMNAL_MSG_IMMEDIATE);
515                         rc = lnet_parse(gmni->gmni_ni,
516                                         &msg->gmm_u.immediate.gmim_hdr,
517                                         msg->gmm_srcnid, rx, 0);
518                 } else if (rc > 0) {
519                         gmnal_version_reply(gmni, rx);
520                         rc = -EPROTO;           /* repost rx */
521                 }
522
523                 if (rc < 0)                     /* parse failure */
524                         gmnal_post_rx(gmni, rx);
525         }
526
527         CDEBUG(D_NET, "exiting\n");
528         atomic_dec(&gmni->gmni_nthreads);
529         return 0;
530 }
531
532 void
533 gmnal_stop_threads(gmnal_ni_t *gmni)
534 {
535         int count = 2;
536
537         gmni->gmni_shutdown = 1;
538         mb();
539
540         /* wake rxthread owning gmni_rx_mutex with an alarm. */
541         spin_lock(&gmni->gmni_gm_lock);
542         gm_set_alarm(gmni->gmni_port, &gmni->gmni_alarm, 0, NULL, NULL);
543         spin_unlock(&gmni->gmni_gm_lock);
544
545         while (atomic_read(&gmni->gmni_nthreads) != 0) {
546                 count++;
547                 if ((count & (count - 1)) == 0)
548                         CWARN("Waiting for %d threads to stop\n",
549                               atomic_read(&gmni->gmni_nthreads));
550                 gmnal_yield(1);
551         }
552 }
553
554 int
555 gmnal_start_threads(gmnal_ni_t *gmni)
556 {
557         int     i;
558         int     pid;
559
560         LASSERT (!gmni->gmni_shutdown);
561         LASSERT (atomic_read(&gmni->gmni_nthreads) == 0);
562
563         gm_initialize_alarm(&gmni->gmni_alarm);
564
565         for (i = 0; i < num_online_cpus(); i++) {
566
567                 pid = kernel_thread(gmnal_rx_thread, (void*)gmni, 0);
568                 if (pid < 0) {
569                         CERROR("rx thread failed to start: %d\n", pid);
570                         gmnal_stop_threads(gmni);
571                         return pid;
572                 }
573
574                 atomic_inc(&gmni->gmni_nthreads);
575         }
576
577         return 0;
578 }