1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
32 * Copyright (c) 2003 Los Alamos National Laboratory (LANL)
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
40 * This file contains all gmnal send and receive functions
46 gmnal_notify_peer_down(gmnal_tx_t *tx)
50 then = cfs_time_current_sec() -
51 cfs_duration_sec(cfs_time_current() -
54 lnet_notify(tx->tx_gmni->gmni_ni, tx->tx_nid, 0, then);
58 gmnal_pack_msg(gmnal_ni_t *gmni, gmnal_msg_t *msg,
59 lnet_nid_t dstnid, int type)
61 /* CAVEAT EMPTOR! this only sets the common message fields. */
62 msg->gmm_magic = GMNAL_MSG_MAGIC;
63 msg->gmm_version = GMNAL_MSG_VERSION;
65 msg->gmm_srcnid = gmni->gmni_ni->ni_nid;
66 msg->gmm_dstnid = dstnid;
70 gmnal_unpack_msg(gmnal_ni_t *gmni, gmnal_rx_t *rx)
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;
78 /* rc = 0:SUCCESS -ve:failure +ve:version mismatch */
80 /* GM may not overflow our buffer */
81 LASSERT (rx->rx_recv_nob <= buffnob);
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);
90 if (msg->gmm_magic == GMNAL_MSG_MAGIC) {
92 } else if (msg->gmm_magic == __swab32(GMNAL_MSG_MAGIC)) {
94 } else if (msg->gmm_magic == LNET_PROTO_MAGIC ||
95 msg->gmm_magic == __swab32(LNET_PROTO_MAGIC)) {
98 CERROR("Bad magic from gmid %u: %08x\n",
99 rx->rx_recv_gmid, msg->gmm_magic);
103 if (msg->gmm_version !=
104 (flip ? __swab16(GMNAL_MSG_VERSION) : GMNAL_MSG_VERSION)) {
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);
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);
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));
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));
134 switch (msg->gmm_type) {
136 CERROR("Unknown message type from %u: %x\n",
137 rx->rx_recv_gmid, msg->gmm_type);
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]));
155 gmnal_get_tx(gmnal_ni_t *gmni)
157 gmnal_tx_t *tx = NULL;
159 spin_lock(&gmni->gmni_tx_lock);
161 if (gmni->gmni_shutdown ||
162 list_empty(&gmni->gmni_idle_txs)) {
163 spin_unlock(&gmni->gmni_tx_lock);
167 tx = list_entry(gmni->gmni_idle_txs.next, gmnal_tx_t, tx_list);
168 list_del(&tx->tx_list);
170 spin_unlock(&gmni->gmni_tx_lock);
172 LASSERT (tx->tx_lntmsg == NULL);
173 LASSERT (tx->tx_ltxb == NULL);
174 LASSERT (!tx->tx_credit);
180 gmnal_tx_done(gmnal_tx_t *tx, int rc)
182 gmnal_ni_t *gmni = tx->tx_gmni;
184 lnet_msg_t *lnetmsg = tx->tx_lntmsg;
186 tx->tx_lntmsg = NULL;
188 spin_lock(&gmni->gmni_tx_lock);
190 if (tx->tx_ltxb != NULL) {
192 list_add_tail(&tx->tx_ltxb->txb_list, &gmni->gmni_idle_ltxbs);
198 gmni->gmni_tx_credits++;
202 list_add_tail(&tx->tx_list, &gmni->gmni_idle_txs);
205 gmnal_check_txqueues_locked(gmni);
207 spin_unlock(&gmni->gmni_tx_lock);
209 /* Delay finalize until tx is free */
211 lnet_finalize(gmni->gmni_ni, lnetmsg, rc);
215 gmnal_drop_sends_callback(struct gm_port *gm_port, void *context,
218 gmnal_tx_t *tx = (gmnal_tx_t*)context;
220 LASSERT(!in_interrupt());
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));
226 gmnal_tx_done(tx, -EIO);
230 gmnal_tx_callback(gm_port_t *gm_port, void *context, gm_status_t status)
232 gmnal_tx_t *tx = (gmnal_tx_t*)context;
233 gmnal_ni_t *gmni = tx->tx_gmni;
235 LASSERT(!in_interrupt());
239 gmnal_tx_done(tx, 0);
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);
251 /* Some error; NB don't complete tx yet; we need its credit for
253 CDEBUG(D_NETERROR, "tx %p error %d(%s), nid %s\n",
254 tx, status, gmnal_gmstatus2str(status),
255 libcfs_nid2str(tx->tx_nid));
257 gmnal_notify_peer_down(tx);
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);
274 gmnal_check_txqueues_locked (gmnal_ni_t *gmni)
282 tx = list_empty(&gmni->gmni_buf_txq) ? NULL :
283 list_entry(gmni->gmni_buf_txq.next, gmnal_tx_t, tx_list);
286 (tx->tx_large_nob == 0 ||
287 !list_empty(&gmni->gmni_idle_ltxbs))) {
290 list_del(&tx->tx_list);
292 LASSERT (tx->tx_ltxb == NULL);
294 if (tx->tx_large_nob != 0) {
295 ltxb = list_entry(gmni->gmni_idle_ltxbs.next,
296 gmnal_txbuf_t, txb_list);
298 /* consume large buffer */
299 list_del(<xb->txb_list);
301 spin_unlock(&gmni->gmni_tx_lock);
303 /* Unlocking here allows sends to get re-ordered,
304 * but we want to allow other CPUs to progress... */
308 /* marshall message in tx_ltxb...
309 * 1. Copy what was marshalled so far (in tx_buf) */
310 memcpy(GMNAL_NETBUF_MSG(<xb->txb_buf),
311 GMNAL_NETBUF_MSG(&tx->tx_buf), tx->tx_msgnob);
313 /* 2. Copy the payload */
314 if (tx->tx_large_iskiov)
316 gmni->gmni_large_pages,
317 ltxb->txb_buf.nb_kiov,
320 tx->tx_large_frags.kiov,
325 gmni->gmni_large_pages,
326 ltxb->txb_buf.nb_kiov,
329 tx->tx_large_frags.iov,
333 tx->tx_msgnob += tx->tx_large_nob;
335 spin_lock(&gmni->gmni_tx_lock);
338 list_add_tail(&tx->tx_list, &gmni->gmni_cred_txq);
341 if (!list_empty(&gmni->gmni_cred_txq) &&
342 gmni->gmni_tx_credits != 0) {
344 tx = list_entry(gmni->gmni_cred_txq.next, gmnal_tx_t, tx_list);
346 /* consume tx and 1 credit */
347 list_del(&tx->tx_list);
348 gmni->gmni_tx_credits--;
350 spin_unlock(&gmni->gmni_tx_lock);
352 /* Unlocking here allows sends to get re-ordered, but we want
353 * to allow other CPUs to progress... */
355 LASSERT(!tx->tx_credit);
358 tx->tx_launchtime = cfs_time_current();
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;
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;
372 spin_lock(&gmni->gmni_gm_lock);
374 gm_send_to_peer_with_callback(gmni->gmni_port,
382 spin_unlock(&gmni->gmni_gm_lock);
383 spin_lock(&gmni->gmni_tx_lock);
388 gmnal_post_rx(gmnal_ni_t *gmni, gmnal_rx_t *rx)
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);
396 CDEBUG(D_NET, "posting rx %p buf %p\n", rx, buffer);
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);
405 gmnal_version_reply (gmnal_ni_t *gmni, gmnal_rx_t *rx)
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... */
412 gmnal_tx_t *tx = gmnal_get_tx(gmni);
415 CERROR("Can't allocate tx to send version info to %u\n",
420 LASSERT (tx->tx_lntmsg == NULL); /* no finalize */
422 tx->tx_nid = LNET_NID_ANY;
423 tx->tx_gmlid = rx->rx_recv_gmid;
425 msg = GMNAL_NETBUF_MSG(&tx->tx_buf);
426 msg->gmm_magic = GMNAL_MSG_MAGIC;
427 msg->gmm_version = GMNAL_MSG_VERSION;
429 /* just send magic + version */
430 tx->tx_msgnob = offsetof(gmnal_msg_t, gmm_type);
431 tx->tx_large_nob = 0;
433 spin_lock(&gmni->gmni_tx_lock);
435 list_add_tail(&tx->tx_list, &gmni->gmni_buf_txq);
436 gmnal_check_txqueues_locked(gmni);
438 spin_unlock(&gmni->gmni_tx_lock);
442 gmnal_rx_thread(void *arg)
444 gmnal_ni_t *gmni = arg;
445 gm_recv_event_t *rxevent = NULL;
446 gm_recv_t *recv = NULL;
450 cfs_daemonize("gmnal_rxd");
452 while (!gmni->gmni_shutdown) {
453 rc = down_interruptible(&gmni->gmni_rx_mutex);
454 LASSERT (rc == 0 || rc == -EINTR);
458 spin_lock(&gmni->gmni_gm_lock);
459 rxevent = gm_blocking_receive_no_spin(gmni->gmni_port);
460 spin_unlock(&gmni->gmni_gm_lock);
462 switch (GM_RECV_EVENT_TYPE(rxevent)) {
464 gm_unknown(gmni->gmni_port, rxevent);
465 up(&gmni->gmni_rx_mutex);
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:
475 case GM_HIGH_RECV_EVENT:
479 recv = &rxevent->recv;
480 rx = gm_hash_find(gmni->gmni_rx_hash,
481 gm_ntohp(recv->buffer));
482 LASSERT (rx != NULL);
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);
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);
496 memcpy(GMNAL_NETBUF_MSG(&rx->rx_buf),
497 gm_ntohp(recv->message), rx->rx_recv_nob);
501 up(&gmni->gmni_rx_mutex);
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);
507 /* We're connectionless: simply drop packets with
509 rc = gmnal_unpack_msg(gmni, rx);
512 gmnal_msg_t *msg = GMNAL_NETBUF_MSG(&rx->rx_buf);
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);
519 gmnal_version_reply(gmni, rx);
520 rc = -EPROTO; /* repost rx */
523 if (rc < 0) /* parse failure */
524 gmnal_post_rx(gmni, rx);
527 CDEBUG(D_NET, "exiting\n");
528 atomic_dec(&gmni->gmni_nthreads);
533 gmnal_stop_threads(gmnal_ni_t *gmni)
537 gmni->gmni_shutdown = 1;
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);
545 while (atomic_read(&gmni->gmni_nthreads) != 0) {
547 if ((count & (count - 1)) == 0)
548 CWARN("Waiting for %d threads to stop\n",
549 atomic_read(&gmni->gmni_nthreads));
555 gmnal_start_threads(gmnal_ni_t *gmni)
560 LASSERT (!gmni->gmni_shutdown);
561 LASSERT (atomic_read(&gmni->gmni_nthreads) == 0);
563 gm_initialize_alarm(&gmni->gmni_alarm);
565 for (i = 0; i < num_online_cpus(); i++) {
567 pid = kernel_thread(gmnal_rx_thread, (void*)gmni, 0);
569 CERROR("rx thread failed to start: %d\n", pid);
570 gmnal_stop_threads(gmni);
574 atomic_inc(&gmni->gmni_nthreads);