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) 2006 Myricom, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
38 * lnet/klnds/mxlnd/mxlnd.c
40 * Author: Eric Barton <eric@bartonsoftware.com>
41 * Author: Scott Atchley <atchley at myri.com>
46 mx_endpoint_addr_t MX_EPA_NULL; /* use to determine if an endpoint is NULL */
49 mxlnd_endpoint_addr_null(mx_endpoint_addr_t epa)
51 /* if memcmp() == 0, it is NULL */
52 return !(memcmp(&epa, &MX_EPA_NULL, sizeof(epa)));
56 mxlnd_ctxstate_to_str(int mxc_state)
60 return "MXLND_CTX_INIT";
62 return "MXLND_CTX_IDLE";
64 return "MXLND_CTX_PREP";
65 case MXLND_CTX_PENDING:
66 return "MXLND_CTX_PENDING";
67 case MXLND_CTX_COMPLETED:
68 return "MXLND_CTX_COMPLETED";
69 case MXLND_CTX_CANCELED:
70 return "MXLND_CTX_CANCELED";
77 mxlnd_connstatus_to_str(int mxk_status)
80 case MXLND_CONN_READY:
81 return "MXLND_CONN_READY";
83 return "MXLND_CONN_INIT";
85 return "MXLND_CONN_WAIT";
86 case MXLND_CONN_DISCONNECT:
87 return "MXLND_CONN_DISCONNECT";
89 return "MXLND_CONN_FAIL";
96 mxlnd_msgtype_to_str(int type) {
99 return "MXLND_MSG_EAGER";
100 case MXLND_MSG_CONN_REQ:
101 return "MXLND_MSG_CONN_REQ";
102 case MXLND_MSG_CONN_ACK:
103 return "MXLND_MSG_CONN_ACK";
105 return "MXLND_MSG_BYE";
107 return "MXLND_MSG_NOOP";
108 case MXLND_MSG_PUT_REQ:
109 return "MXLND_MSG_PUT_REQ";
110 case MXLND_MSG_PUT_ACK:
111 return "MXLND_MSG_PUT_ACK";
112 case MXLND_MSG_PUT_DATA:
113 return "MXLND_MSG_PUT_DATA";
114 case MXLND_MSG_GET_REQ:
115 return "MXLND_MSG_GET_REQ";
116 case MXLND_MSG_GET_DATA:
117 return "MXLND_MSG_GET_DATA";
124 mxlnd_lnetmsg_to_str(int type)
128 return "LNET_MSG_ACK";
130 return "LNET_MSG_PUT";
132 return "LNET_MSG_GET";
134 return "LNET_MSG_REPLY";
136 return "LNET_MSG_HELLO";
144 mxlnd_create_match(kmx_ctx_t *ctx, u8 error)
146 u64 type = (u64) ctx->mxc_msg_type;
147 u64 err = (u64) error;
150 mxlnd_valid_msg_type(ctx->mxc_msg_type);
151 LASSERT(ctx->mxc_cookie >> MXLND_ERROR_OFFSET == 0);
152 match = (type << MXLND_MSG_OFFSET) | (err << MXLND_ERROR_OFFSET) | ctx->mxc_cookie;
157 mxlnd_parse_match(u64 match, u8 *msg_type, u8 *error, u64 *cookie)
159 *msg_type = (u8) MXLND_MSG_TYPE(match);
160 *error = (u8) MXLND_ERROR_VAL(match);
161 *cookie = match & MXLND_MAX_COOKIE;
162 mxlnd_valid_msg_type(*msg_type);
167 mxlnd_get_idle_rx(kmx_conn_t *conn)
169 cfs_list_t *rxs = NULL;
170 kmx_ctx_t *rx = NULL;
172 LASSERT(conn != NULL);
174 rxs = &conn->mxk_rx_idle;
176 cfs_spin_lock(&conn->mxk_lock);
178 if (cfs_list_empty (rxs)) {
179 cfs_spin_unlock(&conn->mxk_lock);
183 rx = cfs_list_entry (rxs->next, kmx_ctx_t, mxc_list);
184 cfs_list_del_init(&rx->mxc_list);
185 cfs_spin_unlock(&conn->mxk_lock);
188 if (rx->mxc_get != rx->mxc_put) {
189 CDEBUG(D_NETERROR, "*** RX get (%llu) != put (%llu) ***\n", rx->mxc_get, rx->mxc_put);
190 CDEBUG(D_NETERROR, "*** incarnation= %lld ***\n", rx->mxc_incarnation);
191 CDEBUG(D_NETERROR, "*** deadline= %ld ***\n", rx->mxc_deadline);
192 CDEBUG(D_NETERROR, "*** state= %s ***\n", mxlnd_ctxstate_to_str(rx->mxc_state));
193 CDEBUG(D_NETERROR, "*** listed?= %d ***\n", !cfs_list_empty(&rx->mxc_list));
194 CDEBUG(D_NETERROR, "*** nid= 0x%llx ***\n", rx->mxc_nid);
195 CDEBUG(D_NETERROR, "*** peer= 0x%p ***\n", rx->mxc_peer);
196 CDEBUG(D_NETERROR, "*** msg_type= %s ***\n", mxlnd_msgtype_to_str(rx->mxc_msg_type));
197 CDEBUG(D_NETERROR, "*** cookie= 0x%llx ***\n", rx->mxc_cookie);
198 CDEBUG(D_NETERROR, "*** nob= %d ***\n", rx->mxc_nob);
201 LASSERT (rx->mxc_get == rx->mxc_put);
205 LASSERT (rx->mxc_state == MXLND_CTX_IDLE);
206 rx->mxc_state = MXLND_CTX_PREP;
207 rx->mxc_deadline = jiffies + MXLND_COMM_TIMEOUT;
213 mxlnd_put_idle_rx(kmx_ctx_t *rx)
215 kmx_conn_t *conn = rx->mxc_conn;
216 cfs_list_t *rxs = &conn->mxk_rx_idle;
218 LASSERT(rx->mxc_type == MXLND_REQ_RX);
223 LASSERT(rx->mxc_get == rx->mxc_put);
225 cfs_spin_lock(&conn->mxk_lock);
226 cfs_list_add(&rx->mxc_list, rxs);
227 cfs_spin_unlock(&conn->mxk_lock);
232 mxlnd_get_idle_tx(void)
234 cfs_list_t *tmp = &kmxlnd_data.kmx_tx_idle;
235 kmx_ctx_t *tx = NULL;
237 cfs_spin_lock(&kmxlnd_data.kmx_tx_idle_lock);
239 if (cfs_list_empty (&kmxlnd_data.kmx_tx_idle)) {
240 CDEBUG(D_NETERROR, "%d txs in use\n", kmxlnd_data.kmx_tx_used);
241 cfs_spin_unlock(&kmxlnd_data.kmx_tx_idle_lock);
245 tmp = &kmxlnd_data.kmx_tx_idle;
246 tx = cfs_list_entry (tmp->next, kmx_ctx_t, mxc_list);
247 cfs_list_del_init(&tx->mxc_list);
249 /* Allocate a new completion cookie. It might not be needed,
250 * but we've got a lock right now and we're unlikely to
252 tx->mxc_cookie = kmxlnd_data.kmx_tx_next_cookie++;
253 if (kmxlnd_data.kmx_tx_next_cookie > MXLND_MAX_COOKIE) {
254 kmxlnd_data.kmx_tx_next_cookie = 1;
256 kmxlnd_data.kmx_tx_used++;
257 cfs_spin_unlock(&kmxlnd_data.kmx_tx_idle_lock);
259 LASSERT (tx->mxc_get == tx->mxc_put);
263 LASSERT (tx->mxc_state == MXLND_CTX_IDLE);
264 LASSERT (tx->mxc_lntmsg[0] == NULL);
265 LASSERT (tx->mxc_lntmsg[1] == NULL);
267 tx->mxc_state = MXLND_CTX_PREP;
268 tx->mxc_deadline = jiffies + MXLND_COMM_TIMEOUT;
274 mxlnd_conn_disconnect(kmx_conn_t *conn, int mx_dis, int send_bye);
277 mxlnd_put_idle_tx(kmx_ctx_t *tx)
280 lnet_msg_t *lntmsg[2];
282 LASSERT(tx->mxc_type == MXLND_REQ_TX);
284 if (tx->mxc_status.code != MX_STATUS_SUCCESS || tx->mxc_errno != 0) {
285 kmx_conn_t *conn = tx->mxc_conn;
288 if (tx->mxc_errno != 0) result = tx->mxc_errno;
289 /* FIXME should we set mx_dis? */
290 mxlnd_conn_disconnect(conn, 0, 1);
293 lntmsg[0] = tx->mxc_lntmsg[0];
294 lntmsg[1] = tx->mxc_lntmsg[1];
299 LASSERT(tx->mxc_get == tx->mxc_put);
301 cfs_spin_lock(&kmxlnd_data.kmx_tx_idle_lock);
302 cfs_list_add_tail(&tx->mxc_list, &kmxlnd_data.kmx_tx_idle);
303 kmxlnd_data.kmx_tx_used--;
304 cfs_spin_unlock(&kmxlnd_data.kmx_tx_idle_lock);
306 if (lntmsg[0] != NULL) lnet_finalize(kmxlnd_data.kmx_ni, lntmsg[0], result);
307 if (lntmsg[1] != NULL) lnet_finalize(kmxlnd_data.kmx_ni, lntmsg[1], result);
313 mxlnd_connparams_free(kmx_connparams_t *cp)
315 LASSERT(cfs_list_empty(&cp->mxr_list));
316 MXLND_FREE(cp, sizeof(*cp));
321 mxlnd_connparams_alloc(kmx_connparams_t **cp, void *context,
322 mx_endpoint_addr_t epa, u64 match, u32 length,
323 kmx_conn_t *conn, kmx_peer_t *peer, void *data)
325 kmx_connparams_t *c = NULL;
327 MXLND_ALLOC(c, sizeof(*c));
328 if (!c) return -ENOMEM;
330 CFS_INIT_LIST_HEAD(&c->mxr_list);
331 c->mxr_context = context;
333 c->mxr_match = match;
337 c->mxr_msg = *((kmx_msg_t *) data);
344 mxlnd_set_conn_status(kmx_conn_t *conn, int status)
346 conn->mxk_status = status;
351 * mxlnd_conn_free_locked - free the conn
352 * @conn - a kmx_conn pointer
354 * The calling function should remove the conn from the conns list first
355 * then destroy it. Caller should have write-locked kmx_global_lock.
358 mxlnd_conn_free_locked(kmx_conn_t *conn)
360 int valid = !mxlnd_endpoint_addr_null(conn->mxk_epa);
361 kmx_peer_t *peer = conn->mxk_peer;
363 CDEBUG(D_NET, "freeing conn 0x%p *****\n", conn);
364 LASSERT (cfs_list_empty (&conn->mxk_tx_credit_queue) &&
365 cfs_list_empty (&conn->mxk_tx_free_queue) &&
366 cfs_list_empty (&conn->mxk_pending));
367 if (!cfs_list_empty(&conn->mxk_list)) {
368 cfs_list_del_init(&conn->mxk_list);
369 if (peer->mxp_conn == conn) {
370 peer->mxp_conn = NULL;
372 kmx_conn_t *temp = NULL;
374 mx_get_endpoint_addr_context(conn->mxk_epa,
377 mx_set_endpoint_addr_context(conn->mxk_epa,
381 /* unlink from global list and drop its ref */
382 cfs_list_del_init(&peer->mxp_list);
383 mxlnd_peer_decref(peer);
386 mxlnd_peer_decref(peer); /* drop conn's ref to peer */
387 if (conn->mxk_rx_pages) {
388 LASSERT (conn->mxk_rxs != NULL);
389 mxlnd_free_pages(conn->mxk_rx_pages);
393 kmx_ctx_t *rx = NULL;
395 for (i = 0; i < MXLND_RX_MSGS(); i++) {
396 rx = &conn->mxk_rxs[i];
397 if (rx->mxc_seg_list != NULL) {
398 LASSERT(rx->mxc_nseg > 0);
399 MXLND_FREE(rx->mxc_seg_list,
401 sizeof(*rx->mxc_seg_list));
404 MXLND_FREE(conn->mxk_rxs, MXLND_RX_MSGS() * sizeof(kmx_ctx_t));
407 MXLND_FREE(conn, sizeof (*conn));
413 mxlnd_conn_cancel_pending_rxs(kmx_conn_t *conn)
417 kmx_ctx_t *ctx = NULL;
418 kmx_ctx_t *next = NULL;
419 mx_return_t mxret = MX_SUCCESS;
424 cfs_spin_lock(&conn->mxk_lock);
425 cfs_list_for_each_entry_safe(ctx, next, &conn->mxk_pending,
427 cfs_list_del_init(&ctx->mxc_list);
428 if (ctx->mxc_type == MXLND_REQ_RX) {
430 mxret = mx_cancel(kmxlnd_data.kmx_endpt,
433 if (mxret != MX_SUCCESS) {
434 CDEBUG(D_NETERROR, "mx_cancel() returned %s (%d)\n", mx_strerror(mxret), mxret);
437 ctx->mxc_errno = -ECONNABORTED;
438 ctx->mxc_state = MXLND_CTX_CANCELED;
439 cfs_spin_unlock(&conn->mxk_lock);
440 cfs_spin_lock(&kmxlnd_data.kmx_conn_lock);
441 /* we may be holding the global lock,
442 * move to orphan list so that it can free it */
443 cfs_list_add_tail(&ctx->mxc_list,
444 &kmxlnd_data.kmx_orphan_msgs);
446 cfs_spin_unlock(&kmxlnd_data.kmx_conn_lock);
447 cfs_spin_lock(&conn->mxk_lock);
452 cfs_spin_unlock(&conn->mxk_lock);
460 mxlnd_cancel_queued_txs(kmx_conn_t *conn)
463 cfs_list_t *tmp = NULL;
465 cfs_spin_lock(&conn->mxk_lock);
466 while (!cfs_list_empty(&conn->mxk_tx_free_queue) ||
467 !cfs_list_empty(&conn->mxk_tx_credit_queue)) {
469 kmx_ctx_t *tx = NULL;
471 if (!cfs_list_empty(&conn->mxk_tx_free_queue)) {
472 tmp = &conn->mxk_tx_free_queue;
474 tmp = &conn->mxk_tx_credit_queue;
477 tx = cfs_list_entry(tmp->next, kmx_ctx_t, mxc_list);
478 cfs_list_del_init(&tx->mxc_list);
479 cfs_spin_unlock(&conn->mxk_lock);
480 tx->mxc_errno = -ECONNABORTED;
481 tx->mxc_state = MXLND_CTX_CANCELED;
482 /* move to orphan list and then abort */
483 cfs_spin_lock(&kmxlnd_data.kmx_conn_lock);
484 cfs_list_add_tail(&tx->mxc_list, &kmxlnd_data.kmx_orphan_msgs);
485 cfs_spin_unlock(&kmxlnd_data.kmx_conn_lock);
487 cfs_spin_lock(&conn->mxk_lock);
489 cfs_spin_unlock(&conn->mxk_lock);
495 mxlnd_send_message(mx_endpoint_addr_t epa, u8 msg_type, int error, u64 cookie)
497 u64 match = (((u64) msg_type) << MXLND_MSG_OFFSET) |
498 (((u64) error) << MXLND_ERROR_OFFSET) | cookie;
500 mx_kisend(kmxlnd_data.kmx_endpt, NULL, 0, MX_PIN_PHYSICAL,
501 epa, match, NULL, NULL);
506 * mxlnd_conn_disconnect - shutdown a connection
507 * @conn - a kmx_conn pointer
508 * @mx_dis - call mx_disconnect()
509 * @send_bye - send peer a BYE msg
511 * This function sets the status to DISCONNECT, completes queued
512 * txs with failure, calls mx_disconnect, which will complete
513 * pending txs and matched rxs with failure.
516 mxlnd_conn_disconnect(kmx_conn_t *conn, int mx_dis, int send_bye)
518 mx_endpoint_addr_t epa = conn->mxk_epa;
519 int valid = !mxlnd_endpoint_addr_null(epa);
522 cfs_spin_lock(&conn->mxk_lock);
523 if (conn->mxk_status == MXLND_CONN_DISCONNECT) {
524 cfs_spin_unlock(&conn->mxk_lock);
527 mxlnd_set_conn_status(conn, MXLND_CONN_DISCONNECT);
528 conn->mxk_timeout = 0;
529 cfs_spin_unlock(&conn->mxk_lock);
531 count = mxlnd_cancel_queued_txs(conn);
532 count += mxlnd_conn_cancel_pending_rxs(conn);
535 cfs_up(&kmxlnd_data.kmx_conn_sem); /* let connd call kmxlnd_abort_msgs() */
537 if (send_bye && valid &&
538 conn->mxk_peer->mxp_nid != kmxlnd_data.kmx_ni->ni_nid) {
539 /* send a BYE to the peer */
540 CDEBUG(D_NET, "%s: sending a BYE msg to %s\n", __func__,
541 libcfs_nid2str(conn->mxk_peer->mxp_nid));
542 mxlnd_send_message(epa, MXLND_MSG_BYE, 0, 0);
543 /* wait to allow the peer to ack our message */
544 mxlnd_sleep(msecs_to_jiffies(20));
547 if (cfs_atomic_read(&kmxlnd_data.kmx_shutdown) != 1) {
548 unsigned long last_msg = 0;
550 /* notify LNET that we are giving up on this peer */
551 if (cfs_time_after(conn->mxk_last_rx, conn->mxk_last_tx))
552 last_msg = conn->mxk_last_rx;
554 last_msg = conn->mxk_last_tx;
556 lnet_notify(kmxlnd_data.kmx_ni, conn->mxk_peer->mxp_nid, 0, last_msg);
558 if (mx_dis && valid &&
559 (memcmp(&epa, &kmxlnd_data.kmx_epa, sizeof(epa) != 0)))
560 mx_disconnect(kmxlnd_data.kmx_endpt, epa);
562 mxlnd_conn_decref(conn); /* drop the owning peer's reference */
568 * mxlnd_conn_alloc - allocate and initialize a new conn struct
569 * @connp - address of a kmx_conn pointer
570 * @peer - owning kmx_peer
572 * Returns 0 on success and -ENOMEM on failure
575 mxlnd_conn_alloc_locked(kmx_conn_t **connp, kmx_peer_t *peer)
582 kmx_conn_t *conn = NULL;
583 kmx_pages_t *pages = NULL;
584 struct page *page = NULL;
585 kmx_ctx_t *rx = NULL;
587 LASSERT(peer != NULL);
589 MXLND_ALLOC(conn, sizeof (*conn));
591 CDEBUG(D_NETERROR, "Cannot allocate conn\n");
594 CDEBUG(D_NET, "allocated conn 0x%p for peer 0x%p\n", conn, peer);
596 memset(conn, 0, sizeof(*conn));
598 ret = mxlnd_alloc_pages(&pages, MXLND_RX_MSG_PAGES());
600 CERROR("Can't allocate rx pages\n");
601 MXLND_FREE(conn, sizeof(*conn));
604 conn->mxk_rx_pages = pages;
606 MXLND_ALLOC(conn->mxk_rxs, MXLND_RX_MSGS() * sizeof(kmx_ctx_t));
607 if (conn->mxk_rxs == NULL) {
608 CERROR("Can't allocate %d rx descriptors\n", MXLND_RX_MSGS());
609 mxlnd_free_pages(pages);
610 MXLND_FREE(conn, sizeof(*conn));
614 memset(conn->mxk_rxs, 0, MXLND_RX_MSGS() * sizeof(kmx_ctx_t));
616 conn->mxk_peer = peer;
617 CFS_INIT_LIST_HEAD(&conn->mxk_list);
618 CFS_INIT_LIST_HEAD(&conn->mxk_zombie);
619 cfs_atomic_set(&conn->mxk_refcount, 2); /* ref for owning peer
620 and one for the caller */
621 if (peer->mxp_nid == kmxlnd_data.kmx_ni->ni_nid) {
625 /* this is localhost, set the epa and status as up */
626 mxlnd_set_conn_status(conn, MXLND_CONN_READY);
627 conn->mxk_epa = kmxlnd_data.kmx_epa;
628 mx_set_endpoint_addr_context(conn->mxk_epa, (void *) conn);
629 peer->mxp_reconnect_time = 0;
630 mx_decompose_endpoint_addr(kmxlnd_data.kmx_epa, &nic_id, &ep_id);
631 peer->mxp_nic_id = nic_id;
632 peer->mxp_ep_id = ep_id;
633 conn->mxk_incarnation = kmxlnd_data.kmx_incarnation;
634 conn->mxk_timeout = 0;
636 /* conn->mxk_incarnation = 0 - will be set by peer */
637 /* conn->mxk_sid = 0 - will be set by peer */
638 mxlnd_set_conn_status(conn, MXLND_CONN_INIT);
639 /* mxk_epa - to be set after mx_iconnect() */
641 cfs_spin_lock_init(&conn->mxk_lock);
642 /* conn->mxk_timeout = 0 */
643 /* conn->mxk_last_tx = 0 */
644 /* conn->mxk_last_rx = 0 */
645 CFS_INIT_LIST_HEAD(&conn->mxk_rx_idle);
647 conn->mxk_credits = *kmxlnd_tunables.kmx_peercredits;
648 /* mxk_outstanding = 0 */
650 CFS_INIT_LIST_HEAD(&conn->mxk_tx_credit_queue);
651 CFS_INIT_LIST_HEAD(&conn->mxk_tx_free_queue);
652 /* conn->mxk_ntx_msgs = 0 */
653 /* conn->mxk_ntx_data = 0 */
654 /* conn->mxk_ntx_posted = 0 */
655 /* conn->mxk_data_posted = 0 */
656 CFS_INIT_LIST_HEAD(&conn->mxk_pending);
658 for (i = 0; i < MXLND_RX_MSGS(); i++) {
660 rx = &conn->mxk_rxs[i];
661 rx->mxc_type = MXLND_REQ_RX;
662 CFS_INIT_LIST_HEAD(&rx->mxc_list);
664 /* map mxc_msg to page */
665 page = pages->mxg_pages[ipage];
666 addr = page_address(page);
667 LASSERT(addr != NULL);
668 rx->mxc_msg = (kmx_msg_t *)(addr + offset);
669 rx->mxc_seg.segment_ptr = MX_PA_TO_U64(virt_to_phys(rx->mxc_msg));
673 rx->mxc_nid = peer->mxp_nid;
677 offset += MXLND_MSG_SIZE;
678 LASSERT (offset <= PAGE_SIZE);
680 if (offset == PAGE_SIZE) {
683 LASSERT (ipage <= MXLND_TX_MSG_PAGES());
686 cfs_list_add_tail(&rx->mxc_list, &conn->mxk_rx_idle);
691 mxlnd_peer_addref(peer); /* add a ref for this conn */
693 /* add to front of peer's conns list */
694 cfs_list_add(&conn->mxk_list, &peer->mxp_conns);
695 peer->mxp_conn = conn;
700 mxlnd_conn_alloc(kmx_conn_t **connp, kmx_peer_t *peer)
703 cfs_rwlock_t *g_lock = &kmxlnd_data.kmx_global_lock;
705 cfs_write_lock(g_lock);
706 ret = mxlnd_conn_alloc_locked(connp, peer);
707 cfs_write_unlock(g_lock);
712 mxlnd_q_pending_ctx(kmx_ctx_t *ctx)
715 kmx_conn_t *conn = ctx->mxc_conn;
717 ctx->mxc_state = MXLND_CTX_PENDING;
719 cfs_spin_lock(&conn->mxk_lock);
720 if (conn->mxk_status >= MXLND_CONN_INIT) {
721 cfs_list_add_tail(&ctx->mxc_list, &conn->mxk_pending);
722 if (conn->mxk_timeout == 0 || ctx->mxc_deadline < conn->mxk_timeout) {
723 conn->mxk_timeout = ctx->mxc_deadline;
726 ctx->mxc_state = MXLND_CTX_COMPLETED;
729 cfs_spin_unlock(&conn->mxk_lock);
735 mxlnd_deq_pending_ctx(kmx_ctx_t *ctx)
737 LASSERT(ctx->mxc_state == MXLND_CTX_PENDING ||
738 ctx->mxc_state == MXLND_CTX_COMPLETED);
739 if (ctx->mxc_state != MXLND_CTX_PENDING &&
740 ctx->mxc_state != MXLND_CTX_COMPLETED) {
741 CDEBUG(D_NETERROR, "deq ctx->mxc_state = %s\n",
742 mxlnd_ctxstate_to_str(ctx->mxc_state));
744 ctx->mxc_state = MXLND_CTX_COMPLETED;
745 if (!cfs_list_empty(&ctx->mxc_list)) {
746 kmx_conn_t *conn = ctx->mxc_conn;
747 kmx_ctx_t *next = NULL;
749 LASSERT(conn != NULL);
750 cfs_spin_lock(&conn->mxk_lock);
751 cfs_list_del_init(&ctx->mxc_list);
752 conn->mxk_timeout = 0;
753 if (!cfs_list_empty(&conn->mxk_pending)) {
754 next = cfs_list_entry(conn->mxk_pending.next,
755 kmx_ctx_t, mxc_list);
756 conn->mxk_timeout = next->mxc_deadline;
758 cfs_spin_unlock(&conn->mxk_lock);
764 * mxlnd_peer_free - free the peer
765 * @peer - a kmx_peer pointer
767 * The calling function should decrement the rxs, drain the tx queues and
768 * remove the peer from the peers list first then destroy it.
771 mxlnd_peer_free(kmx_peer_t *peer)
773 CDEBUG(D_NET, "freeing peer 0x%p %s\n", peer, libcfs_nid2str(peer->mxp_nid));
775 LASSERT (cfs_atomic_read(&peer->mxp_refcount) == 0);
777 if (!cfs_list_empty(&peer->mxp_list)) {
778 /* assume we are locked */
779 cfs_list_del_init(&peer->mxp_list);
782 MXLND_FREE(peer, sizeof (*peer));
783 cfs_atomic_dec(&kmxlnd_data.kmx_npeers);
788 mxlnd_lookup_mac(u32 ip, u64 *tmp_id)
790 int ret = -EHOSTUNREACH;
791 unsigned char *haddr = NULL;
792 struct net_device *dev = NULL;
793 struct neighbour *n = NULL;
794 __be32 dst_ip = htonl(ip);
796 dev = dev_get_by_name(*kmxlnd_tunables.kmx_default_ipif);
800 haddr = (unsigned char *) tmp_id + 2; /* MAC is only 6 bytes */
802 n = neigh_lookup(&arp_tbl, &dst_ip, dev);
805 if (n->nud_state & NUD_VALID) {
806 memcpy(haddr, n->ha, dev->addr_len);
818 /* We only want the MAC address of the peer's Myricom NIC. We
819 * require that each node has the IPoMX interface (myriN) up.
820 * We will not pass any traffic over IPoMX, but it allows us
821 * to get the MAC address. */
823 mxlnd_ip2nic_id(u32 ip, u64 *nic_id, int tries)
829 cfs_socket_t *sock = NULL;
832 CDEBUG(D_NET, "try %d of %d tries\n", try, tries);
833 ret = mxlnd_lookup_mac(ip, &tmp_id);
837 /* not found, try to connect (force an arp) */
838 ret = libcfs_sock_connect(&sock, &fatal, 0, 0, ip, 987);
839 if (ret == -ECONNREFUSED) {
840 /* peer is there, get the MAC address */
841 mxlnd_lookup_mac(ip, &tmp_id);
845 } else if (ret == -EHOSTUNREACH && try < tries) {
846 /* add a little backoff */
847 CDEBUG(D_NET, "sleeping for %d jiffies\n",
849 mxlnd_sleep(CFS_HZ/4);
852 } while (try++ < tries);
853 CDEBUG(D_NET, "done trying. ret = %d\n", ret);
857 #ifdef __LITTLE_ENDIAN
858 *nic_id = ___arch__swab64(tmp_id);
866 * mxlnd_peer_alloc - allocate and initialize a new peer struct
867 * @peerp - address of a kmx_peer pointer
868 * @nid - LNET node id
870 * Returns 0 on success and -ENOMEM on failure
873 mxlnd_peer_alloc(kmx_peer_t **peerp, lnet_nid_t nid, u32 board, u32 ep_id, u64 nic_id)
876 u32 ip = LNET_NIDADDR(nid);
877 kmx_peer_t *peer = NULL;
879 LASSERT (nid != LNET_NID_ANY && nid != 0LL);
881 MXLND_ALLOC(peer, sizeof (*peer));
883 CDEBUG(D_NETERROR, "Cannot allocate peer for NID 0x%llx\n",
887 CDEBUG(D_NET, "allocated peer 0x%p for NID 0x%llx\n", peer, nid);
889 memset(peer, 0, sizeof(*peer));
891 CFS_INIT_LIST_HEAD(&peer->mxp_list);
893 /* peer->mxp_ni unused - may be used for multi-rail */
894 cfs_atomic_set(&peer->mxp_refcount, 1); /* ref for kmx_peers list */
896 peer->mxp_board = board;
897 peer->mxp_ep_id = ep_id;
898 peer->mxp_nic_id = nic_id;
900 CFS_INIT_LIST_HEAD(&peer->mxp_conns);
901 ret = mxlnd_conn_alloc(&peer->mxp_conn, peer); /* adds 2nd conn ref here... */
903 mxlnd_peer_decref(peer);
906 CFS_INIT_LIST_HEAD(&peer->mxp_tx_queue);
908 if (peer->mxp_nic_id != 0ULL)
909 nic_id = peer->mxp_nic_id;
911 if (nic_id == 0ULL) {
912 ret = mxlnd_ip2nic_id(ip, &nic_id, 1);
914 peer->mxp_nic_id = nic_id;
915 mx_nic_id_to_board_number(nic_id, &peer->mxp_board);
919 peer->mxp_nic_id = nic_id; /* may be 0ULL if ip2nic_id() failed */
921 /* peer->mxp_reconnect_time = 0 */
922 /* peer->mxp_incompatible = 0 */
928 static inline kmx_peer_t *
929 mxlnd_find_peer_by_nid_locked(lnet_nid_t nid)
933 kmx_peer_t *peer = NULL;
935 hash = mxlnd_nid_to_hash(nid);
937 cfs_list_for_each_entry(peer, &kmxlnd_data.kmx_peers[hash], mxp_list) {
938 if (peer->mxp_nid == nid) {
940 mxlnd_peer_addref(peer);
944 return (found ? peer : NULL);
948 mxlnd_find_peer_by_nid(lnet_nid_t nid, int create)
952 kmx_peer_t *peer = NULL;
953 kmx_peer_t *old = NULL;
954 cfs_rwlock_t *g_lock = &kmxlnd_data.kmx_global_lock;
956 cfs_read_lock(g_lock);
957 peer = mxlnd_find_peer_by_nid_locked(nid); /* adds peer ref */
959 if ((peer && peer->mxp_conn) || /* found peer with conn or */
960 (!peer && !create)) { /* did not find peer and do not create one */
961 cfs_read_unlock(g_lock);
965 cfs_read_unlock(g_lock);
967 /* if peer but _not_ conn */
968 if (peer && !peer->mxp_conn) {
970 cfs_write_lock(g_lock);
971 if (!peer->mxp_conn) { /* check again */
972 /* create the conn */
973 ret = mxlnd_conn_alloc_locked(&peer->mxp_conn, peer);
975 /* we tried, return the peer only.
976 * the caller needs to see if the conn exists */
977 CDEBUG(D_NETERROR, "%s: %s could not alloc conn\n",
978 __func__, libcfs_nid2str(peer->mxp_nid));
980 /* drop extra conn ref */
981 mxlnd_conn_decref(peer->mxp_conn);
984 cfs_write_unlock(g_lock);
989 /* peer not found and we need to create one */
990 hash = mxlnd_nid_to_hash(nid);
992 /* create peer (and conn) */
993 /* adds conn ref for peer and one for this function */
994 ret = mxlnd_peer_alloc(&peer, nid, *kmxlnd_tunables.kmx_board,
995 *kmxlnd_tunables.kmx_ep_id, 0ULL);
996 if (ret != 0) /* no memory, peer is NULL */
999 cfs_write_lock(g_lock);
1002 old = mxlnd_find_peer_by_nid_locked(nid);
1004 /* someone already created one */
1005 mxlnd_conn_decref(peer->mxp_conn); /* drop ref taken above.. */
1006 mxlnd_conn_decref(peer->mxp_conn); /* drop peer's ref */
1007 mxlnd_peer_decref(peer);
1010 /* no other peer, use this one */
1011 cfs_list_add_tail(&peer->mxp_list,
1012 &kmxlnd_data.kmx_peers[hash]);
1013 cfs_atomic_inc(&kmxlnd_data.kmx_npeers);
1014 mxlnd_peer_addref(peer);
1015 mxlnd_conn_decref(peer->mxp_conn); /* drop ref from peer_alloc */
1018 cfs_write_unlock(g_lock);
1024 mxlnd_tx_requires_credit(kmx_ctx_t *tx)
1026 return (tx->mxc_msg_type == MXLND_MSG_EAGER ||
1027 tx->mxc_msg_type == MXLND_MSG_GET_REQ ||
1028 tx->mxc_msg_type == MXLND_MSG_PUT_REQ ||
1029 tx->mxc_msg_type == MXLND_MSG_NOOP);
1033 * mxlnd_init_msg - set type and number of bytes
1034 * @msg - msg pointer
1035 * @type - of message
1036 * @body_nob - bytes in msg body
1039 mxlnd_init_msg(kmx_msg_t *msg, u8 type, int body_nob)
1041 msg->mxm_type = type;
1042 msg->mxm_nob = offsetof(kmx_msg_t, mxm_u) + body_nob;
1046 mxlnd_init_tx_msg (kmx_ctx_t *tx, u8 type, int body_nob, lnet_nid_t nid)
1048 int nob = offsetof (kmx_msg_t, mxm_u) + body_nob;
1049 kmx_msg_t *msg = NULL;
1051 LASSERT (tx != NULL);
1052 LASSERT (nob <= MXLND_MSG_SIZE);
1055 /* tx->mxc_peer should have already been set if we know it */
1056 tx->mxc_msg_type = type;
1058 /* tx->mxc_seg.segment_ptr is already pointing to mxc_page */
1059 tx->mxc_seg.segment_length = nob;
1060 tx->mxc_pin_type = MX_PIN_PHYSICAL;
1063 msg->mxm_type = type;
1070 mxlnd_cksum (void *ptr, int nob)
1076 sum = ((sum << 1) | (sum >> 31)) + *c++;
1078 /* ensure I don't return 0 (== no checksum) */
1079 return (sum == 0) ? 1 : sum;
1083 * mxlnd_pack_msg_locked - complete msg info
1087 mxlnd_pack_msg_locked(kmx_ctx_t *tx)
1089 kmx_msg_t *msg = tx->mxc_msg;
1091 /* type and nob should already be set in init_msg() */
1092 msg->mxm_magic = MXLND_MSG_MAGIC;
1093 msg->mxm_version = MXLND_MSG_VERSION;
1095 /* don't use mxlnd_tx_requires_credit() since we want PUT_ACK to
1096 * return credits as well */
1097 if (tx->mxc_msg_type != MXLND_MSG_CONN_REQ &&
1098 tx->mxc_msg_type != MXLND_MSG_CONN_ACK) {
1099 msg->mxm_credits = tx->mxc_conn->mxk_outstanding;
1100 tx->mxc_conn->mxk_outstanding = 0;
1102 msg->mxm_credits = 0;
1106 msg->mxm_srcnid = kmxlnd_data.kmx_ni->ni_nid;
1107 msg->mxm_srcstamp = kmxlnd_data.kmx_incarnation;
1108 msg->mxm_dstnid = tx->mxc_nid;
1109 /* if it is a new peer, the dststamp will be 0 */
1110 msg->mxm_dststamp = tx->mxc_conn->mxk_incarnation;
1112 if (*kmxlnd_tunables.kmx_cksum) {
1113 msg->mxm_cksum = mxlnd_cksum(msg, msg->mxm_nob);
1118 mxlnd_unpack_msg(kmx_msg_t *msg, int nob)
1120 const int hdr_size = offsetof(kmx_msg_t, mxm_u);
1121 __u32 msg_cksum = 0;
1125 /* 6 bytes are enough to have received magic + version */
1127 CDEBUG(D_NETERROR, "not enough bytes for magic + hdr: %d\n", nob);
1131 if (msg->mxm_magic == MXLND_MSG_MAGIC) {
1133 } else if (msg->mxm_magic == __swab32(MXLND_MSG_MAGIC)) {
1136 CDEBUG(D_NETERROR, "Bad magic: %08x\n", msg->mxm_magic);
1140 if (msg->mxm_version !=
1141 (flip ? __swab16(MXLND_MSG_VERSION) : MXLND_MSG_VERSION)) {
1142 CDEBUG(D_NETERROR, "Bad version: %d\n", msg->mxm_version);
1146 if (nob < hdr_size) {
1147 CDEBUG(D_NETERROR, "not enough for a header: %d\n", nob);
1151 msg_nob = flip ? __swab32(msg->mxm_nob) : msg->mxm_nob;
1152 if (msg_nob > nob) {
1153 CDEBUG(D_NETERROR, "Short message: got %d, wanted %d\n", nob, msg_nob);
1157 /* checksum must be computed with mxm_cksum zero and BEFORE anything
1159 msg_cksum = flip ? __swab32(msg->mxm_cksum) : msg->mxm_cksum;
1161 if (msg_cksum != 0 && msg_cksum != mxlnd_cksum(msg, msg_nob)) {
1162 CDEBUG(D_NETERROR, "Bad checksum\n");
1165 msg->mxm_cksum = msg_cksum;
1168 /* leave magic unflipped as a clue to peer endianness */
1169 __swab16s(&msg->mxm_version);
1170 CLASSERT (sizeof(msg->mxm_type) == 1);
1171 CLASSERT (sizeof(msg->mxm_credits) == 1);
1172 msg->mxm_nob = msg_nob;
1173 __swab64s(&msg->mxm_srcnid);
1174 __swab64s(&msg->mxm_srcstamp);
1175 __swab64s(&msg->mxm_dstnid);
1176 __swab64s(&msg->mxm_dststamp);
1179 if (msg->mxm_srcnid == LNET_NID_ANY) {
1180 CDEBUG(D_NETERROR, "Bad src nid: %s\n", libcfs_nid2str(msg->mxm_srcnid));
1184 switch (msg->mxm_type) {
1186 CDEBUG(D_NETERROR, "Unknown message type %x\n", msg->mxm_type);
1189 case MXLND_MSG_NOOP:
1192 case MXLND_MSG_EAGER:
1193 if (msg_nob < offsetof(kmx_msg_t, mxm_u.eager.mxem_payload[0])) {
1194 CDEBUG(D_NETERROR, "Short EAGER: %d(%d)\n", msg_nob,
1195 (int)offsetof(kmx_msg_t, mxm_u.eager.mxem_payload[0]));
1200 case MXLND_MSG_PUT_REQ:
1201 if (msg_nob < hdr_size + sizeof(msg->mxm_u.put_req)) {
1202 CDEBUG(D_NETERROR, "Short PUT_REQ: %d(%d)\n", msg_nob,
1203 (int)(hdr_size + sizeof(msg->mxm_u.put_req)));
1207 __swab64s(&msg->mxm_u.put_req.mxprm_cookie);
1210 case MXLND_MSG_PUT_ACK:
1211 if (msg_nob < hdr_size + sizeof(msg->mxm_u.put_ack)) {
1212 CDEBUG(D_NETERROR, "Short PUT_ACK: %d(%d)\n", msg_nob,
1213 (int)(hdr_size + sizeof(msg->mxm_u.put_ack)));
1217 __swab64s(&msg->mxm_u.put_ack.mxpam_src_cookie);
1218 __swab64s(&msg->mxm_u.put_ack.mxpam_dst_cookie);
1222 case MXLND_MSG_GET_REQ:
1223 if (msg_nob < hdr_size + sizeof(msg->mxm_u.get_req)) {
1224 CDEBUG(D_NETERROR, "Short GET_REQ: %d(%d)\n", msg_nob,
1225 (int)(hdr_size + sizeof(msg->mxm_u.get_req)));
1229 __swab64s(&msg->mxm_u.get_req.mxgrm_cookie);
1233 case MXLND_MSG_CONN_REQ:
1234 case MXLND_MSG_CONN_ACK:
1235 if (msg_nob < hdr_size + sizeof(msg->mxm_u.conn_req)) {
1236 CDEBUG(D_NETERROR, "Short connreq/ack: %d(%d)\n", msg_nob,
1237 (int)(hdr_size + sizeof(msg->mxm_u.conn_req)));
1241 __swab32s(&msg->mxm_u.conn_req.mxcrm_queue_depth);
1242 __swab32s(&msg->mxm_u.conn_req.mxcrm_eager_size);
1252 * @lntmsg - the LNET msg that this is continuing. If EAGER, then NULL.
1256 * @length - length of incoming message
1257 * @pending - add to kmx_pending (0 is NO and 1 is YES)
1259 * The caller gets the rx and sets nid, peer and conn if known.
1261 * Returns 0 on success and -1 on failure
1264 mxlnd_recv_msg(lnet_msg_t *lntmsg, kmx_ctx_t *rx, u8 msg_type, u64 cookie, u32 length)
1267 mx_return_t mxret = MX_SUCCESS;
1268 uint64_t mask = ~(MXLND_ERROR_MASK);
1270 rx->mxc_msg_type = msg_type;
1271 rx->mxc_lntmsg[0] = lntmsg; /* may be NULL if EAGER */
1272 rx->mxc_cookie = cookie;
1273 /* rx->mxc_match may already be set */
1274 /* rx->mxc_seg.segment_ptr is already set */
1275 rx->mxc_seg.segment_length = length;
1276 ret = mxlnd_q_pending_ctx(rx);
1278 /* the caller is responsible for calling conn_decref() if needed */
1281 mxret = mx_kirecv(kmxlnd_data.kmx_endpt, &rx->mxc_seg, 1, MX_PIN_PHYSICAL,
1282 cookie, mask, (void *) rx, &rx->mxc_mxreq);
1283 if (mxret != MX_SUCCESS) {
1284 mxlnd_deq_pending_ctx(rx);
1285 CDEBUG(D_NETERROR, "mx_kirecv() failed with %s (%d)\n",
1286 mx_strerror(mxret), (int) mxret);
1294 * mxlnd_unexpected_recv - this is the callback function that will handle
1295 * unexpected receives
1296 * @context - NULL, ignore
1297 * @source - the peer's mx_endpoint_addr_t
1298 * @match_value - the msg's bits, should be MXLND_MSG_EAGER
1299 * @length - length of incoming message
1300 * @data_if_available - used for CONN_[REQ|ACK]
1302 * If it is an eager-sized msg, we will call recv_msg() with the actual
1303 * length. If it is a large message, we will call recv_msg() with a
1304 * length of 0 bytes to drop it because we should never have a large,
1305 * unexpected message.
1307 * NOTE - The MX library blocks until this function completes. Make it as fast as
1308 * possible. DO NOT allocate memory which can block!
1310 * If we cannot get a rx or the conn is closed, drop the message on the floor
1311 * (i.e. recv 0 bytes and ignore).
1313 mx_unexp_handler_action_t
1314 mxlnd_unexpected_recv(void *context, mx_endpoint_addr_t source,
1315 uint64_t match_value, uint32_t length, void *data_if_available)
1318 kmx_ctx_t *rx = NULL;
1323 kmx_conn_t *conn = NULL;
1324 kmx_peer_t *peer = NULL;
1329 /* TODO this will change to the net struct */
1330 if (context != NULL) {
1331 CDEBUG(D_NETERROR, "non-NULL context\n");
1335 CDEBUG(D_NET, "bits=0x%llx length=%d\n", match_value, length);
1338 mx_decompose_endpoint_addr2(source, &nic_id, &ep_id, &sid);
1339 mxlnd_parse_match(match_value, &msg_type, &error, &cookie);
1340 cfs_read_lock(&kmxlnd_data.kmx_global_lock);
1341 mx_get_endpoint_addr_context(source, (void **) &conn);
1343 mxlnd_conn_addref(conn); /* add ref for this function */
1344 peer = conn->mxk_peer;
1346 cfs_read_unlock(&kmxlnd_data.kmx_global_lock);
1348 if (msg_type == MXLND_MSG_BYE) {
1350 CDEBUG(D_NET, "peer %s sent BYE msg\n",
1351 libcfs_nid2str(peer->mxp_nid));
1352 mxlnd_conn_disconnect(conn, 1, 0);
1353 mxlnd_conn_decref(conn); /* drop ref taken above */
1355 return MX_RECV_FINISHED;
1358 if (msg_type == MXLND_MSG_CONN_REQ) {
1359 kmx_connparams_t *cp = NULL;
1360 const int expected = offsetof(kmx_msg_t, mxm_u) +
1361 sizeof(kmx_connreq_msg_t);
1363 if (conn) mxlnd_conn_decref(conn); /* drop ref taken above */
1364 if (unlikely(length != expected || !data_if_available)) {
1365 CDEBUG(D_NETERROR, "received invalid CONN_REQ from %llx "
1366 "length=%d (expected %d)\n", nic_id, length, expected);
1367 mxlnd_send_message(source, MXLND_MSG_CONN_ACK, EPROTO, 0);
1368 return MX_RECV_FINISHED;
1371 ret = mxlnd_connparams_alloc(&cp, context, source, match_value, length,
1372 conn, peer, data_if_available);
1373 if (unlikely(ret != 0)) {
1374 CDEBUG(D_NETERROR, "unable to alloc CONN_REQ from %llx:%d\n",
1376 mxlnd_send_message(source, MXLND_MSG_CONN_ACK, ENOMEM, 0);
1377 return MX_RECV_FINISHED;
1379 cfs_spin_lock(&kmxlnd_data.kmx_conn_lock);
1380 cfs_list_add_tail(&cp->mxr_list, &kmxlnd_data.kmx_conn_reqs);
1381 cfs_spin_unlock(&kmxlnd_data.kmx_conn_lock);
1382 cfs_up(&kmxlnd_data.kmx_conn_sem);
1383 return MX_RECV_FINISHED;
1385 if (msg_type == MXLND_MSG_CONN_ACK) {
1386 kmx_connparams_t *cp = NULL;
1387 const int expected = offsetof(kmx_msg_t, mxm_u) +
1388 sizeof(kmx_connreq_msg_t);
1391 if (unlikely(error != 0)) {
1392 CDEBUG(D_NETERROR, "received CONN_ACK from %s "
1394 libcfs_nid2str(peer->mxp_nid), (int) error);
1395 mxlnd_conn_disconnect(conn, 1, 0);
1396 } else if (unlikely(length != expected || !data_if_available)) {
1397 CDEBUG(D_NETERROR, "received %s CONN_ACK from %s "
1398 "length=%d (expected %d)\n",
1399 data_if_available ? "short" : "missing",
1400 libcfs_nid2str(peer->mxp_nid), length, expected);
1401 mxlnd_conn_disconnect(conn, 1, 1);
1403 /* peer is ready for messages */
1404 ret = mxlnd_connparams_alloc(&cp, context, source, match_value, length,
1405 conn, peer, data_if_available);
1406 if (unlikely(ret != 0)) {
1407 CDEBUG(D_NETERROR, "unable to alloc kmx_connparams_t"
1408 " from %llx:%d\n", nic_id, ep_id);
1409 mxlnd_conn_disconnect(conn, 1, 1);
1411 cfs_spin_lock(&kmxlnd_data.kmx_conn_lock);
1412 cfs_list_add_tail(&cp->mxr_list,
1413 &kmxlnd_data.kmx_conn_reqs);
1414 cfs_spin_unlock(&kmxlnd_data.kmx_conn_lock);
1415 cfs_up(&kmxlnd_data.kmx_conn_sem);
1418 mxlnd_conn_decref(conn); /* drop ref taken above */
1420 return MX_RECV_FINISHED;
1423 /* Handle unexpected messages (PUT_REQ and GET_REQ) */
1425 LASSERT(peer != NULL && conn != NULL);
1427 rx = mxlnd_get_idle_rx(conn);
1429 if (length <= MXLND_MSG_SIZE) {
1430 ret = mxlnd_recv_msg(NULL, rx, msg_type, match_value, length);
1432 CDEBUG(D_NETERROR, "unexpected large receive with "
1433 "match_value=0x%llx length=%d\n",
1434 match_value, length);
1435 ret = mxlnd_recv_msg(NULL, rx, msg_type, match_value, 0);
1439 /* hold conn ref until rx completes */
1440 rx->mxc_conn = conn;
1441 rx->mxc_peer = peer;
1442 rx->mxc_nid = peer->mxp_nid;
1444 CDEBUG(D_NETERROR, "could not post receive\n");
1445 mxlnd_put_idle_rx(rx);
1449 /* Encountered error, drop incoming message on the floor */
1450 /* We could use MX_RECV_FINISHED but posting the receive of 0 bytes
1451 * uses the standard code path and acks the sender normally */
1453 if (rx == NULL || ret != 0) {
1454 mxlnd_conn_decref(conn); /* drop ref taken above */
1456 CDEBUG(D_NETERROR, "no idle rxs available - dropping rx"
1457 " 0x%llx from %s\n", match_value,
1458 libcfs_nid2str(peer->mxp_nid));
1461 CDEBUG(D_NETERROR, "disconnected peer - dropping rx\n");
1463 seg.segment_ptr = 0ULL;
1464 seg.segment_length = 0;
1465 mx_kirecv(kmxlnd_data.kmx_endpt, &seg, 1, MX_PIN_PHYSICAL,
1466 match_value, ~0ULL, NULL, NULL);
1469 return MX_RECV_CONTINUE;
1474 mxlnd_get_peer_info(int index, lnet_nid_t *nidp, int *count)
1478 kmx_peer_t *peer = NULL;
1480 cfs_read_lock(&kmxlnd_data.kmx_global_lock);
1481 for (i = 0; i < MXLND_HASH_SIZE; i++) {
1482 cfs_list_for_each_entry(peer, &kmxlnd_data.kmx_peers[i],
1485 *nidp = peer->mxp_nid;
1486 *count = cfs_atomic_read(&peer->mxp_refcount);
1492 cfs_read_unlock(&kmxlnd_data.kmx_global_lock);
1498 mxlnd_del_peer_locked(kmx_peer_t *peer)
1500 if (peer->mxp_conn) {
1501 mxlnd_conn_disconnect(peer->mxp_conn, 1, 1);
1503 cfs_list_del_init(&peer->mxp_list); /* remove from the global list */
1504 mxlnd_peer_decref(peer); /* drop global list ref */
1510 mxlnd_del_peer(lnet_nid_t nid)
1514 kmx_peer_t *peer = NULL;
1515 kmx_peer_t *next = NULL;
1517 if (nid != LNET_NID_ANY) {
1518 peer = mxlnd_find_peer_by_nid(nid, 0); /* adds peer ref */
1520 cfs_write_lock(&kmxlnd_data.kmx_global_lock);
1521 if (nid != LNET_NID_ANY) {
1525 mxlnd_peer_decref(peer); /* and drops it */
1526 mxlnd_del_peer_locked(peer);
1528 } else { /* LNET_NID_ANY */
1529 for (i = 0; i < MXLND_HASH_SIZE; i++) {
1530 cfs_list_for_each_entry_safe(peer, next,
1531 &kmxlnd_data.kmx_peers[i],
1533 mxlnd_del_peer_locked(peer);
1537 cfs_write_unlock(&kmxlnd_data.kmx_global_lock);
1543 mxlnd_get_conn_by_idx(int index)
1546 kmx_peer_t *peer = NULL;
1547 kmx_conn_t *conn = NULL;
1549 cfs_read_lock(&kmxlnd_data.kmx_global_lock);
1550 for (i = 0; i < MXLND_HASH_SIZE; i++) {
1551 cfs_list_for_each_entry(peer, &kmxlnd_data.kmx_peers[i],
1553 cfs_list_for_each_entry(conn, &peer->mxp_conns,
1559 mxlnd_conn_addref(conn); /* add ref here, dec in ctl() */
1560 cfs_read_unlock(&kmxlnd_data.kmx_global_lock);
1565 cfs_read_unlock(&kmxlnd_data.kmx_global_lock);
1571 mxlnd_close_matching_conns_locked(kmx_peer_t *peer)
1573 kmx_conn_t *conn = NULL;
1574 kmx_conn_t *next = NULL;
1576 cfs_list_for_each_entry_safe(conn, next, &peer->mxp_conns, mxk_list)
1577 mxlnd_conn_disconnect(conn, 0, 1);
1583 mxlnd_close_matching_conns(lnet_nid_t nid)
1587 kmx_peer_t *peer = NULL;
1589 cfs_write_lock(&kmxlnd_data.kmx_global_lock);
1590 if (nid != LNET_NID_ANY) {
1591 peer = mxlnd_find_peer_by_nid_locked(nid); /* adds peer ref */
1595 mxlnd_close_matching_conns_locked(peer);
1596 mxlnd_peer_decref(peer); /* and drops it here */
1598 } else { /* LNET_NID_ANY */
1599 for (i = 0; i < MXLND_HASH_SIZE; i++) {
1600 cfs_list_for_each_entry(peer, &kmxlnd_data.kmx_peers[i], mxp_list)
1601 mxlnd_close_matching_conns_locked(peer);
1604 cfs_write_unlock(&kmxlnd_data.kmx_global_lock);
1610 * mxlnd_ctl - modify MXLND parameters
1611 * @ni - LNET interface handle
1612 * @cmd - command to change
1613 * @arg - the ioctl data
1616 mxlnd_ctl(lnet_ni_t *ni, unsigned int cmd, void *arg)
1618 struct libcfs_ioctl_data *data = arg;
1621 LASSERT (ni == kmxlnd_data.kmx_ni);
1624 case IOC_LIBCFS_GET_PEER: {
1628 ret = mxlnd_get_peer_info(data->ioc_count, &nid, &count);
1629 data->ioc_nid = nid;
1630 data->ioc_count = count;
1633 case IOC_LIBCFS_DEL_PEER: {
1634 ret = mxlnd_del_peer(data->ioc_nid);
1637 case IOC_LIBCFS_GET_CONN: {
1638 kmx_conn_t *conn = NULL;
1640 conn = mxlnd_get_conn_by_idx(data->ioc_count);
1645 data->ioc_nid = conn->mxk_peer->mxp_nid;
1646 mxlnd_conn_decref(conn); /* dec ref taken in get_conn_by_idx() */
1650 case IOC_LIBCFS_CLOSE_CONNECTION: {
1651 ret = mxlnd_close_matching_conns(data->ioc_nid);
1655 CDEBUG(D_NETERROR, "unknown ctl(%d)\n", cmd);
1663 * mxlnd_peer_queue_tx_locked - add the tx to the peer's tx queue
1666 * Add the tx to the peer's msg or data queue. The caller has locked the peer.
1669 mxlnd_peer_queue_tx_locked(kmx_ctx_t *tx)
1671 u8 msg_type = tx->mxc_msg_type;
1672 kmx_conn_t *conn = tx->mxc_conn;
1674 LASSERT (msg_type != 0);
1675 LASSERT (tx->mxc_nid != 0);
1676 LASSERT (tx->mxc_peer != NULL);
1677 LASSERT (tx->mxc_conn != NULL);
1679 tx->mxc_incarnation = conn->mxk_incarnation;
1681 if (msg_type != MXLND_MSG_PUT_DATA &&
1682 msg_type != MXLND_MSG_GET_DATA) {
1684 if (mxlnd_tx_requires_credit(tx)) {
1685 cfs_list_add_tail(&tx->mxc_list,
1686 &conn->mxk_tx_credit_queue);
1687 conn->mxk_ntx_msgs++;
1688 } else if (msg_type == MXLND_MSG_CONN_REQ ||
1689 msg_type == MXLND_MSG_CONN_ACK) {
1690 /* put conn msgs at the front of the queue */
1691 cfs_list_add(&tx->mxc_list, &conn->mxk_tx_free_queue);
1693 /* PUT_ACK, PUT_NAK */
1694 cfs_list_add_tail(&tx->mxc_list,
1695 &conn->mxk_tx_free_queue);
1696 conn->mxk_ntx_msgs++;
1700 cfs_list_add_tail(&tx->mxc_list, &conn->mxk_tx_free_queue);
1701 conn->mxk_ntx_data++;
1708 * mxlnd_peer_queue_tx - add the tx to the global tx queue
1711 * Add the tx to the peer's msg or data queue
1714 mxlnd_peer_queue_tx(kmx_ctx_t *tx)
1716 LASSERT(tx->mxc_peer != NULL);
1717 LASSERT(tx->mxc_conn != NULL);
1718 cfs_spin_lock(&tx->mxc_conn->mxk_lock);
1719 mxlnd_peer_queue_tx_locked(tx);
1720 cfs_spin_unlock(&tx->mxc_conn->mxk_lock);
1726 * mxlnd_queue_tx - add the tx to the global tx queue
1729 * Add the tx to the global queue and up the tx_queue_sem
1732 mxlnd_queue_tx(kmx_ctx_t *tx)
1734 kmx_peer_t *peer = tx->mxc_peer;
1735 LASSERT (tx->mxc_nid != 0);
1738 if (peer->mxp_incompatible &&
1739 tx->mxc_msg_type != MXLND_MSG_CONN_ACK) {
1740 /* let this fail now */
1741 tx->mxc_errno = -ECONNABORTED;
1742 mxlnd_conn_decref(peer->mxp_conn);
1743 mxlnd_put_idle_tx(tx);
1746 if (tx->mxc_conn == NULL) {
1748 kmx_conn_t *conn = NULL;
1750 ret = mxlnd_conn_alloc(&conn, peer); /* adds 2nd ref for tx... */
1752 tx->mxc_errno = ret;
1753 mxlnd_put_idle_tx(tx);
1756 tx->mxc_conn = conn;
1757 mxlnd_peer_decref(peer); /* and takes it from peer */
1759 LASSERT(tx->mxc_conn != NULL);
1760 mxlnd_peer_queue_tx(tx);
1761 mxlnd_check_sends(peer);
1763 cfs_spin_lock(&kmxlnd_data.kmx_tx_queue_lock);
1764 cfs_list_add_tail(&tx->mxc_list, &kmxlnd_data.kmx_tx_queue);
1765 cfs_spin_unlock(&kmxlnd_data.kmx_tx_queue_lock);
1766 cfs_up(&kmxlnd_data.kmx_tx_queue_sem);
1773 mxlnd_setup_iov(kmx_ctx_t *ctx, u32 niov, struct iovec *iov, u32 offset, u32 nob)
1780 int first_iov_offset = 0;
1781 int first_found = 0;
1783 int last_iov_length = 0;
1784 mx_ksegment_t *seg = NULL;
1786 if (niov == 0) return 0;
1787 LASSERT(iov != NULL);
1789 for (i = 0; i < niov; i++) {
1790 sum = old_sum + (u32) iov[i].iov_len;
1791 if (!first_found && (sum > offset)) {
1793 first_iov_offset = offset - old_sum;
1795 sum = (u32) iov[i].iov_len - first_iov_offset;
1800 last_iov_length = (u32) iov[i].iov_len - (sum - nob);
1801 if (first_iov == last_iov) last_iov_length -= first_iov_offset;
1806 LASSERT(first_iov >= 0 && last_iov >= first_iov);
1807 nseg = last_iov - first_iov + 1;
1810 MXLND_ALLOC(seg, nseg * sizeof(*seg));
1812 CDEBUG(D_NETERROR, "MXLND_ALLOC() failed\n");
1815 memset(seg, 0, nseg * sizeof(*seg));
1816 ctx->mxc_nseg = nseg;
1818 for (i = 0; i < nseg; i++) {
1819 seg[i].segment_ptr = MX_PA_TO_U64(virt_to_phys(iov[first_iov + i].iov_base));
1820 seg[i].segment_length = (u32) iov[first_iov + i].iov_len;
1822 seg[i].segment_ptr += (u64) first_iov_offset;
1823 seg[i].segment_length -= (u32) first_iov_offset;
1825 if (i == (nseg - 1)) {
1826 seg[i].segment_length = (u32) last_iov_length;
1828 sum += seg[i].segment_length;
1830 ctx->mxc_seg_list = seg;
1831 ctx->mxc_pin_type = MX_PIN_PHYSICAL;
1832 #ifdef MX_PIN_FULLPAGES
1833 ctx->mxc_pin_type |= MX_PIN_FULLPAGES;
1835 LASSERT(nob == sum);
1840 mxlnd_setup_kiov(kmx_ctx_t *ctx, u32 niov, lnet_kiov_t *kiov, u32 offset, u32 nob)
1846 int first_kiov = -1;
1847 int first_kiov_offset = 0;
1848 int first_found = 0;
1850 int last_kiov_length = 0;
1851 mx_ksegment_t *seg = NULL;
1853 if (niov == 0) return 0;
1854 LASSERT(kiov != NULL);
1856 for (i = 0; i < niov; i++) {
1857 sum = old_sum + kiov[i].kiov_len;
1858 if (i == 0) sum -= kiov[i].kiov_offset;
1859 if (!first_found && (sum > offset)) {
1861 first_kiov_offset = offset - old_sum;
1862 if (i == 0) first_kiov_offset = kiov[i].kiov_offset;
1864 sum = kiov[i].kiov_len - first_kiov_offset;
1869 last_kiov_length = kiov[i].kiov_len - (sum - nob);
1870 if (first_kiov == last_kiov) last_kiov_length -= first_kiov_offset;
1875 LASSERT(first_kiov >= 0 && last_kiov >= first_kiov);
1876 nseg = last_kiov - first_kiov + 1;
1879 MXLND_ALLOC(seg, nseg * sizeof(*seg));
1881 CDEBUG(D_NETERROR, "MXLND_ALLOC() failed\n");
1884 memset(seg, 0, niov * sizeof(*seg));
1885 ctx->mxc_nseg = niov;
1887 for (i = 0; i < niov; i++) {
1888 seg[i].segment_ptr = lnet_page2phys(kiov[first_kiov + i].kiov_page);
1889 seg[i].segment_length = kiov[first_kiov + i].kiov_len;
1891 seg[i].segment_ptr += (u64) first_kiov_offset;
1892 /* we have to add back the original kiov_offset */
1893 seg[i].segment_length -= first_kiov_offset +
1894 kiov[first_kiov].kiov_offset;
1896 if (i == (nseg - 1)) {
1897 seg[i].segment_length = last_kiov_length;
1899 sum += seg[i].segment_length;
1901 ctx->mxc_seg_list = seg;
1902 ctx->mxc_pin_type = MX_PIN_PHYSICAL;
1903 #ifdef MX_PIN_FULLPAGES
1904 ctx->mxc_pin_type |= MX_PIN_FULLPAGES;
1906 LASSERT(nob == sum);
1911 mxlnd_send_nak(kmx_ctx_t *tx, lnet_nid_t nid, int type, int status, __u64 cookie)
1913 LASSERT(type == MXLND_MSG_PUT_ACK);
1914 mxlnd_init_tx_msg(tx, type, sizeof(kmx_putack_msg_t), tx->mxc_nid);
1915 tx->mxc_cookie = cookie;
1916 tx->mxc_msg->mxm_u.put_ack.mxpam_src_cookie = cookie;
1917 tx->mxc_msg->mxm_u.put_ack.mxpam_dst_cookie = ((u64) status << MXLND_ERROR_OFFSET); /* error code */
1918 tx->mxc_match = mxlnd_create_match(tx, status);
1925 * mxlnd_send_data - get tx, map [k]iov, queue tx
1932 * This setups the DATA send for PUT or GET.
1934 * On success, it queues the tx, on failure it calls lnet_finalize()
1937 mxlnd_send_data(lnet_ni_t *ni, lnet_msg_t *lntmsg, kmx_peer_t *peer, u8 msg_type, u64 cookie)
1940 lnet_process_id_t target = lntmsg->msg_target;
1941 unsigned int niov = lntmsg->msg_niov;
1942 struct iovec *iov = lntmsg->msg_iov;
1943 lnet_kiov_t *kiov = lntmsg->msg_kiov;
1944 unsigned int offset = lntmsg->msg_offset;
1945 unsigned int nob = lntmsg->msg_len;
1946 kmx_ctx_t *tx = NULL;
1948 LASSERT(lntmsg != NULL);
1949 LASSERT(peer != NULL);
1950 LASSERT(msg_type == MXLND_MSG_PUT_DATA || msg_type == MXLND_MSG_GET_DATA);
1951 LASSERT((cookie>>MXLND_ERROR_OFFSET) == 0);
1953 tx = mxlnd_get_idle_tx();
1955 CDEBUG(D_NETERROR, "Can't allocate %s tx for %s\n",
1956 msg_type == MXLND_MSG_PUT_DATA ? "PUT_DATA" : "GET_DATA",
1957 libcfs_nid2str(target.nid));
1960 tx->mxc_nid = target.nid;
1961 /* NOTE called when we have a ref on the conn, get one for this tx */
1962 mxlnd_conn_addref(peer->mxp_conn);
1963 tx->mxc_peer = peer;
1964 tx->mxc_conn = peer->mxp_conn;
1965 tx->mxc_msg_type = msg_type;
1966 tx->mxc_lntmsg[0] = lntmsg;
1967 tx->mxc_cookie = cookie;
1968 tx->mxc_match = mxlnd_create_match(tx, 0);
1970 /* This setups up the mx_ksegment_t to send the DATA payload */
1972 /* do not setup the segments */
1973 CDEBUG(D_NETERROR, "nob = 0; why didn't we use an EAGER reply "
1974 "to %s?\n", libcfs_nid2str(target.nid));
1976 } else if (kiov == NULL) {
1977 ret = mxlnd_setup_iov(tx, niov, iov, offset, nob);
1979 ret = mxlnd_setup_kiov(tx, niov, kiov, offset, nob);
1982 CDEBUG(D_NETERROR, "Can't setup send DATA for %s\n",
1983 libcfs_nid2str(target.nid));
1984 tx->mxc_errno = -EIO;
1991 mxlnd_conn_decref(peer->mxp_conn);
1992 mxlnd_put_idle_tx(tx);
1996 CDEBUG(D_NETERROR, "no tx avail\n");
1997 lnet_finalize(ni, lntmsg, -EIO);
2002 * mxlnd_recv_data - map [k]iov, post rx
2009 * This setups the DATA receive for PUT or GET.
2011 * On success, it returns 0, on failure it returns -1
2014 mxlnd_recv_data(lnet_ni_t *ni, lnet_msg_t *lntmsg, kmx_ctx_t *rx, u8 msg_type, u64 cookie)
2017 lnet_process_id_t target = lntmsg->msg_target;
2018 unsigned int niov = lntmsg->msg_niov;
2019 struct iovec *iov = lntmsg->msg_iov;
2020 lnet_kiov_t *kiov = lntmsg->msg_kiov;
2021 unsigned int offset = lntmsg->msg_offset;
2022 unsigned int nob = lntmsg->msg_len;
2023 mx_return_t mxret = MX_SUCCESS;
2024 u64 mask = ~(MXLND_ERROR_MASK);
2026 /* above assumes MXLND_MSG_PUT_DATA */
2027 if (msg_type == MXLND_MSG_GET_DATA) {
2028 niov = lntmsg->msg_md->md_niov;
2029 iov = lntmsg->msg_md->md_iov.iov;
2030 kiov = lntmsg->msg_md->md_iov.kiov;
2032 nob = lntmsg->msg_md->md_length;
2035 LASSERT(lntmsg != NULL);
2036 LASSERT(rx != NULL);
2037 LASSERT(msg_type == MXLND_MSG_PUT_DATA || msg_type == MXLND_MSG_GET_DATA);
2038 LASSERT((cookie>>MXLND_ERROR_OFFSET) == 0); /* ensure top 12 bits are 0 */
2040 rx->mxc_msg_type = msg_type;
2041 rx->mxc_state = MXLND_CTX_PENDING;
2042 rx->mxc_nid = target.nid;
2043 /* if posting a GET_DATA, we may not yet know the peer */
2044 if (rx->mxc_peer != NULL) {
2045 rx->mxc_conn = rx->mxc_peer->mxp_conn;
2047 rx->mxc_lntmsg[0] = lntmsg;
2048 rx->mxc_cookie = cookie;
2049 rx->mxc_match = mxlnd_create_match(rx, 0);
2050 /* This setups up the mx_ksegment_t to receive the DATA payload */
2052 ret = mxlnd_setup_iov(rx, niov, iov, offset, nob);
2054 ret = mxlnd_setup_kiov(rx, niov, kiov, offset, nob);
2056 if (msg_type == MXLND_MSG_GET_DATA) {
2057 rx->mxc_lntmsg[1] = lnet_create_reply_msg(kmxlnd_data.kmx_ni, lntmsg);
2058 if (rx->mxc_lntmsg[1] == NULL) {
2059 CDEBUG(D_NETERROR, "Can't create reply for GET -> %s\n",
2060 libcfs_nid2str(target.nid));
2065 CDEBUG(D_NETERROR, "Can't setup %s rx for %s\n",
2066 msg_type == MXLND_MSG_PUT_DATA ? "PUT_DATA" : "GET_DATA",
2067 libcfs_nid2str(target.nid));
2070 ret = mxlnd_q_pending_ctx(rx);
2074 CDEBUG(D_NET, "receiving %s 0x%llx\n", mxlnd_msgtype_to_str(msg_type), rx->mxc_cookie);
2075 mxret = mx_kirecv(kmxlnd_data.kmx_endpt,
2076 rx->mxc_seg_list, rx->mxc_nseg,
2077 rx->mxc_pin_type, rx->mxc_match,
2080 if (mxret != MX_SUCCESS) {
2081 if (rx->mxc_conn != NULL) {
2082 mxlnd_deq_pending_ctx(rx);
2084 CDEBUG(D_NETERROR, "mx_kirecv() failed with %d for %s\n",
2085 (int) mxret, libcfs_nid2str(target.nid));
2093 * mxlnd_send - the LND required send function
2098 * This must not block. Since we may not have a peer struct for the receiver,
2099 * it will append send messages on a global tx list. We will then up the
2100 * tx_queued's semaphore to notify it of the new send.
2103 mxlnd_send(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg)
2106 int type = lntmsg->msg_type;
2107 lnet_hdr_t *hdr = &lntmsg->msg_hdr;
2108 lnet_process_id_t target = lntmsg->msg_target;
2109 lnet_nid_t nid = target.nid;
2110 int target_is_router = lntmsg->msg_target_is_router;
2111 int routing = lntmsg->msg_routing;
2112 unsigned int payload_niov = lntmsg->msg_niov;
2113 struct iovec *payload_iov = lntmsg->msg_iov;
2114 lnet_kiov_t *payload_kiov = lntmsg->msg_kiov;
2115 unsigned int payload_offset = lntmsg->msg_offset;
2116 unsigned int payload_nob = lntmsg->msg_len;
2117 kmx_ctx_t *tx = NULL;
2118 kmx_msg_t *txmsg = NULL;
2119 kmx_ctx_t *rx = (kmx_ctx_t *) private; /* for REPLY */
2120 kmx_ctx_t *rx_data = NULL;
2121 kmx_conn_t *conn = NULL;
2123 uint32_t length = 0;
2124 kmx_peer_t *peer = NULL;
2125 cfs_rwlock_t *g_lock = &kmxlnd_data.kmx_global_lock;
2127 CDEBUG(D_NET, "sending %d bytes in %d frags to %s\n",
2128 payload_nob, payload_niov, libcfs_id2str(target));
2130 LASSERT (payload_nob == 0 || payload_niov > 0);
2131 LASSERT (payload_niov <= LNET_MAX_IOV);
2132 /* payload is either all vaddrs or all pages */
2133 LASSERT (!(payload_kiov != NULL && payload_iov != NULL));
2135 /* private is used on LNET_GET_REPLY only, NULL for all other cases */
2137 /* NOTE we may not know the peer if it is the very first PUT_REQ or GET_REQ
2138 * to a new peer, so create one if not found */
2139 peer = mxlnd_find_peer_by_nid(nid, 1); /* adds peer ref */
2140 if (peer == NULL || peer->mxp_conn == NULL) {
2141 /* we could not find it nor could we create one or
2142 * one exists but we cannot create a conn,
2143 * fail this message */
2145 /* found peer without conn, drop ref taken above */
2146 LASSERT(peer->mxp_conn == NULL);
2147 mxlnd_peer_decref(peer);
2152 /* we have a peer with a conn */
2154 if (unlikely(peer->mxp_incompatible)) {
2155 mxlnd_peer_decref(peer); /* drop ref taken above */
2157 cfs_read_lock(g_lock);
2158 conn = peer->mxp_conn;
2159 if (conn && conn->mxk_status != MXLND_CONN_DISCONNECT) {
2160 mxlnd_conn_addref(conn);
2164 cfs_read_unlock(g_lock);
2165 mxlnd_peer_decref(peer); /* drop peer ref taken above */
2170 LASSERT(peer && conn);
2172 CDEBUG(D_NET, "%s: peer 0x%llx is 0x%p\n", __func__, nid, peer);
2176 LASSERT (payload_nob == 0);
2179 case LNET_MSG_REPLY:
2181 /* Is the payload small enough not to need DATA? */
2182 nob = offsetof(kmx_msg_t, mxm_u.eager.mxem_payload[payload_nob]);
2183 if (nob <= MXLND_MSG_SIZE)
2184 break; /* send EAGER */
2186 tx = mxlnd_get_idle_tx();
2187 if (unlikely(tx == NULL)) {
2188 CDEBUG(D_NETERROR, "Can't allocate %s tx for %s\n",
2189 type == LNET_MSG_PUT ? "PUT" : "REPLY",
2190 libcfs_nid2str(nid));
2191 if (conn) mxlnd_conn_decref(conn);
2195 tx->mxc_peer = peer;
2196 tx->mxc_conn = conn;
2197 /* we added a conn ref above */
2198 mxlnd_init_tx_msg (tx, MXLND_MSG_PUT_REQ, sizeof(kmx_putreq_msg_t), nid);
2199 txmsg = tx->mxc_msg;
2200 txmsg->mxm_u.put_req.mxprm_hdr = *hdr;
2201 txmsg->mxm_u.put_req.mxprm_cookie = tx->mxc_cookie;
2202 tx->mxc_match = mxlnd_create_match(tx, 0);
2204 /* we must post a receive _before_ sending the request.
2205 * we need to determine how much to receive, it will be either
2206 * a put_ack or a put_nak. The put_ack is larger, so use it. */
2208 rx = mxlnd_get_idle_rx(conn);
2209 if (unlikely(rx == NULL)) {
2210 CDEBUG(D_NETERROR, "Can't allocate rx for PUT_ACK for %s\n",
2211 libcfs_nid2str(nid));
2212 mxlnd_put_idle_tx(tx);
2213 if (conn) mxlnd_conn_decref(conn); /* for the ref taken above */
2217 rx->mxc_peer = peer;
2218 mxlnd_conn_addref(conn); /* for this rx */
2219 rx->mxc_conn = conn;
2220 rx->mxc_msg_type = MXLND_MSG_PUT_ACK;
2221 rx->mxc_cookie = tx->mxc_cookie;
2222 rx->mxc_match = mxlnd_create_match(rx, 0);
2224 length = offsetof(kmx_msg_t, mxm_u) + sizeof(kmx_putack_msg_t);
2225 ret = mxlnd_recv_msg(lntmsg, rx, MXLND_MSG_PUT_ACK, rx->mxc_match, length);
2226 if (unlikely(ret != 0)) {
2227 CDEBUG(D_NETERROR, "recv_msg() failed for PUT_ACK for %s\n",
2228 libcfs_nid2str(nid));
2229 rx->mxc_lntmsg[0] = NULL;
2230 mxlnd_put_idle_rx(rx);
2231 mxlnd_put_idle_tx(tx);
2232 mxlnd_conn_decref(conn); /* for the rx... */
2233 mxlnd_conn_decref(conn); /* and for the tx */
2234 return -EHOSTUNREACH;
2241 if (routing || target_is_router)
2242 break; /* send EAGER */
2244 /* is the REPLY message too small for DATA? */
2245 nob = offsetof(kmx_msg_t, mxm_u.eager.mxem_payload[lntmsg->msg_md->md_length]);
2246 if (nob <= MXLND_MSG_SIZE)
2247 break; /* send EAGER */
2249 /* get tx (we need the cookie) , post rx for incoming DATA,
2250 * then post GET_REQ tx */
2251 tx = mxlnd_get_idle_tx();
2252 if (unlikely(tx == NULL)) {
2253 CDEBUG(D_NETERROR, "Can't allocate GET tx for %s\n",
2254 libcfs_nid2str(nid));
2255 mxlnd_conn_decref(conn); /* for the ref taken above */
2258 rx_data = mxlnd_get_idle_rx(conn);
2259 if (unlikely(rx_data == NULL)) {
2260 CDEBUG(D_NETERROR, "Can't allocate DATA rx for %s\n",
2261 libcfs_nid2str(nid));
2262 mxlnd_put_idle_tx(tx);
2263 mxlnd_conn_decref(conn); /* for the ref taken above */
2266 rx_data->mxc_peer = peer;
2267 /* NOTE no need to lock peer before adding conn ref since we took
2268 * a conn ref for the tx (it cannot be freed between there and here ) */
2269 mxlnd_conn_addref(conn); /* for the rx_data */
2270 rx_data->mxc_conn = conn;
2272 ret = mxlnd_recv_data(ni, lntmsg, rx_data, MXLND_MSG_GET_DATA, tx->mxc_cookie);
2273 if (unlikely(ret != 0)) {
2274 CDEBUG(D_NETERROR, "Can't setup GET sink for %s\n",
2275 libcfs_nid2str(nid));
2276 mxlnd_put_idle_rx(rx_data);
2277 mxlnd_put_idle_tx(tx);
2278 mxlnd_conn_decref(conn); /* for the rx_data... */
2279 mxlnd_conn_decref(conn); /* and for the tx */
2283 tx->mxc_peer = peer;
2284 tx->mxc_conn = conn;
2285 /* conn ref taken above */
2286 mxlnd_init_tx_msg(tx, MXLND_MSG_GET_REQ, sizeof(kmx_getreq_msg_t), nid);
2287 txmsg = tx->mxc_msg;
2288 txmsg->mxm_u.get_req.mxgrm_hdr = *hdr;
2289 txmsg->mxm_u.get_req.mxgrm_cookie = tx->mxc_cookie;
2290 tx->mxc_match = mxlnd_create_match(tx, 0);
2297 mxlnd_conn_decref(conn); /* drop ref taken above */
2303 LASSERT (offsetof(kmx_msg_t, mxm_u.eager.mxem_payload[payload_nob])
2306 tx = mxlnd_get_idle_tx();
2307 if (unlikely(tx == NULL)) {
2308 CDEBUG(D_NETERROR, "Can't send %s to %s: tx descs exhausted\n",
2309 mxlnd_lnetmsg_to_str(type), libcfs_nid2str(nid));
2310 mxlnd_conn_decref(conn); /* drop ref taken above */
2314 tx->mxc_peer = peer;
2315 tx->mxc_conn = conn;
2316 /* conn ref taken above */
2317 nob = offsetof(kmx_eager_msg_t, mxem_payload[payload_nob]);
2318 mxlnd_init_tx_msg (tx, MXLND_MSG_EAGER, nob, nid);
2319 tx->mxc_match = mxlnd_create_match(tx, 0);
2321 txmsg = tx->mxc_msg;
2322 txmsg->mxm_u.eager.mxem_hdr = *hdr;
2324 if (payload_kiov != NULL)
2325 lnet_copy_kiov2flat(MXLND_MSG_SIZE, txmsg,
2326 offsetof(kmx_msg_t, mxm_u.eager.mxem_payload),
2327 payload_niov, payload_kiov, payload_offset, payload_nob);
2329 lnet_copy_iov2flat(MXLND_MSG_SIZE, txmsg,
2330 offsetof(kmx_msg_t, mxm_u.eager.mxem_payload),
2331 payload_niov, payload_iov, payload_offset, payload_nob);
2333 tx->mxc_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
2339 * mxlnd_recv - the LND required recv function
2350 * This must not block.
2353 mxlnd_recv (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg, int delayed,
2354 unsigned int niov, struct iovec *iov, lnet_kiov_t *kiov,
2355 unsigned int offset, unsigned int mlen, unsigned int rlen)
2360 kmx_ctx_t *rx = private;
2361 kmx_msg_t *rxmsg = rx->mxc_msg;
2362 lnet_nid_t nid = rx->mxc_nid;
2363 kmx_ctx_t *tx = NULL;
2364 kmx_msg_t *txmsg = NULL;
2365 kmx_peer_t *peer = rx->mxc_peer;
2366 kmx_conn_t *conn = peer->mxp_conn;
2368 int msg_type = rxmsg->mxm_type;
2373 LASSERT (mlen <= rlen);
2374 /* Either all pages or all vaddrs */
2375 LASSERT (!(kiov != NULL && iov != NULL));
2376 LASSERT (peer && conn);
2378 /* conn_addref(conn) already taken for the primary rx */
2381 case MXLND_MSG_EAGER:
2382 nob = offsetof(kmx_msg_t, mxm_u.eager.mxem_payload[rlen]);
2383 len = rx->mxc_status.xfer_length;
2384 if (unlikely(nob > len)) {
2385 CDEBUG(D_NETERROR, "Eager message from %s too big: %d(%d)\n",
2386 libcfs_nid2str(nid), nob, len);
2392 lnet_copy_flat2kiov(niov, kiov, offset,
2393 MXLND_MSG_SIZE, rxmsg,
2394 offsetof(kmx_msg_t, mxm_u.eager.mxem_payload),
2397 lnet_copy_flat2iov(niov, iov, offset,
2398 MXLND_MSG_SIZE, rxmsg,
2399 offsetof(kmx_msg_t, mxm_u.eager.mxem_payload),
2405 case MXLND_MSG_PUT_REQ:
2406 /* we are going to reuse the rx, store the needed info */
2407 cookie = rxmsg->mxm_u.put_req.mxprm_cookie;
2409 /* get tx, post rx, send PUT_ACK */
2411 tx = mxlnd_get_idle_tx();
2412 if (unlikely(tx == NULL)) {
2413 CDEBUG(D_NETERROR, "Can't allocate tx for %s\n", libcfs_nid2str(nid));
2414 /* Not replying will break the connection */
2418 if (unlikely(mlen == 0)) {
2420 tx->mxc_peer = peer;
2421 tx->mxc_conn = conn;
2422 mxlnd_send_nak(tx, nid, MXLND_MSG_PUT_ACK, 0, cookie);
2427 mxlnd_init_tx_msg(tx, MXLND_MSG_PUT_ACK, sizeof(kmx_putack_msg_t), nid);
2428 tx->mxc_peer = peer;
2429 tx->mxc_conn = conn;
2430 /* no need to lock peer first since we already have a ref */
2431 mxlnd_conn_addref(conn); /* for the tx */
2432 txmsg = tx->mxc_msg;
2433 txmsg->mxm_u.put_ack.mxpam_src_cookie = cookie;
2434 txmsg->mxm_u.put_ack.mxpam_dst_cookie = tx->mxc_cookie;
2435 tx->mxc_cookie = cookie;
2436 tx->mxc_match = mxlnd_create_match(tx, 0);
2438 /* we must post a receive _before_ sending the PUT_ACK */
2440 rx->mxc_state = MXLND_CTX_PREP;
2441 rx->mxc_peer = peer;
2442 rx->mxc_conn = conn;
2443 /* do not take another ref for this rx, it is already taken */
2444 rx->mxc_nid = peer->mxp_nid;
2445 ret = mxlnd_recv_data(ni, lntmsg, rx, MXLND_MSG_PUT_DATA,
2446 txmsg->mxm_u.put_ack.mxpam_dst_cookie);
2448 if (unlikely(ret != 0)) {
2449 /* Notify peer that it's over */
2450 CDEBUG(D_NETERROR, "Can't setup PUT_DATA rx for %s: %d\n",
2451 libcfs_nid2str(nid), ret);
2453 tx->mxc_state = MXLND_CTX_PREP;
2454 tx->mxc_peer = peer;
2455 tx->mxc_conn = conn;
2456 /* finalize = 0, let the PUT_ACK tx finalize this */
2457 tx->mxc_lntmsg[0] = rx->mxc_lntmsg[0];
2458 tx->mxc_lntmsg[1] = rx->mxc_lntmsg[1];
2459 /* conn ref already taken above */
2460 mxlnd_send_nak(tx, nid, MXLND_MSG_PUT_ACK, ret, cookie);
2466 /* do not return a credit until after PUT_DATA returns */
2470 case MXLND_MSG_GET_REQ:
2471 cookie = rxmsg->mxm_u.get_req.mxgrm_cookie;
2473 if (likely(lntmsg != NULL)) {
2474 mxlnd_send_data(ni, lntmsg, rx->mxc_peer, MXLND_MSG_GET_DATA,
2477 /* GET didn't match anything */
2478 /* The initiator has a rx mapped to [k]iov. We cannot send a nak.
2479 * We have to embed the error code in the match bits.
2480 * Send the error in bits 52-59 and the cookie in bits 0-51 */
2481 tx = mxlnd_get_idle_tx();
2482 if (unlikely(tx == NULL)) {
2483 CDEBUG(D_NETERROR, "Can't get tx for GET NAK for %s\n",
2484 libcfs_nid2str(nid));
2485 /* we can't get a tx, notify the peer that the GET failed */
2486 mxlnd_send_message(conn->mxk_epa, MXLND_MSG_GET_DATA,
2491 tx->mxc_msg_type = MXLND_MSG_GET_DATA;
2492 tx->mxc_state = MXLND_CTX_PENDING;
2494 tx->mxc_peer = peer;
2495 tx->mxc_conn = conn;
2496 /* no need to lock peer first since we already have a ref */
2497 mxlnd_conn_addref(conn); /* for this tx */
2498 tx->mxc_cookie = cookie;
2499 tx->mxc_match = mxlnd_create_match(tx, ENODATA);
2500 tx->mxc_pin_type = MX_PIN_PHYSICAL;
2503 /* finalize lntmsg after tx completes */
2511 /* we received a message, increment peer's outstanding credits */
2513 cfs_spin_lock(&conn->mxk_lock);
2514 conn->mxk_outstanding++;
2515 cfs_spin_unlock(&conn->mxk_lock);
2517 /* we are done with the rx */
2518 mxlnd_put_idle_rx(rx);
2519 mxlnd_conn_decref(conn);
2522 if (finalize == 1) lnet_finalize(kmxlnd_data.kmx_ni, lntmsg, 0);
2524 /* we received a credit, see if we can use it to send a msg */
2525 if (credit) mxlnd_check_sends(peer);
2531 mxlnd_sleep(unsigned long timeout)
2533 cfs_set_current_state(CFS_TASK_INTERRUPTIBLE);
2534 cfs_schedule_timeout(timeout);
2539 * mxlnd_tx_queued - the generic send queue thread
2540 * @arg - thread id (as a void *)
2542 * This thread moves send messages from the global tx_queue to the owning
2543 * peer's tx_[msg|data]_queue. If the peer does not exist, it creates one and adds
2544 * it to the global peer list.
2547 mxlnd_tx_queued(void *arg)
2549 long id = (long) arg;
2552 kmx_ctx_t *tx = NULL;
2553 kmx_peer_t *peer = NULL;
2554 cfs_list_t *queue = &kmxlnd_data.kmx_tx_queue;
2555 cfs_spinlock_t *tx_q_lock = &kmxlnd_data.kmx_tx_queue_lock;
2556 cfs_rwlock_t *g_lock = &kmxlnd_data.kmx_global_lock;
2558 cfs_daemonize("mxlnd_tx_queued");
2560 while (!(cfs_atomic_read(&kmxlnd_data.kmx_shutdown))) {
2561 ret = down_interruptible(&kmxlnd_data.kmx_tx_queue_sem);
2562 if (cfs_atomic_read(&kmxlnd_data.kmx_shutdown))
2564 if (ret != 0) // Should we check for -EINTR?
2566 cfs_spin_lock(tx_q_lock);
2567 if (cfs_list_empty (&kmxlnd_data.kmx_tx_queue)) {
2568 cfs_spin_unlock(tx_q_lock);
2571 tx = cfs_list_entry (queue->next, kmx_ctx_t, mxc_list);
2572 cfs_list_del_init(&tx->mxc_list);
2573 cfs_spin_unlock(tx_q_lock);
2576 peer = mxlnd_find_peer_by_nid(tx->mxc_nid, 0); /* adds peer ref */
2578 tx->mxc_peer = peer;
2579 cfs_write_lock(g_lock);
2580 if (peer->mxp_conn == NULL) {
2581 ret = mxlnd_conn_alloc_locked(&peer->mxp_conn, peer);
2583 /* out of memory, give up and fail tx */
2584 tx->mxc_errno = -ENOMEM;
2585 mxlnd_peer_decref(peer);
2586 cfs_write_unlock(g_lock);
2587 mxlnd_put_idle_tx(tx);
2591 tx->mxc_conn = peer->mxp_conn;
2592 mxlnd_conn_addref(tx->mxc_conn); /* for this tx */
2593 mxlnd_peer_decref(peer); /* drop peer ref taken above */
2594 cfs_write_unlock(g_lock);
2600 kmx_peer_t *peer = NULL;
2601 kmx_peer_t *old = NULL;
2603 hash = mxlnd_nid_to_hash(tx->mxc_nid);
2605 LASSERT(tx->mxc_msg_type != MXLND_MSG_PUT_DATA &&
2606 tx->mxc_msg_type != MXLND_MSG_GET_DATA);
2608 /* adds conn ref for this function */
2609 ret = mxlnd_peer_alloc(&peer, tx->mxc_nid,
2610 *kmxlnd_tunables.kmx_board,
2611 *kmxlnd_tunables.kmx_ep_id, 0ULL);
2613 /* finalize message */
2614 tx->mxc_errno = ret;
2615 mxlnd_put_idle_tx(tx);
2618 tx->mxc_peer = peer;
2619 tx->mxc_conn = peer->mxp_conn;
2620 /* this tx will keep the conn ref taken in peer_alloc() */
2622 /* add peer to global peer list, but look to see
2623 * if someone already created it after we released
2625 cfs_write_lock(g_lock);
2626 old = mxlnd_find_peer_by_nid_locked(peer->mxp_nid);
2628 /* we have a peer ref on old */
2629 if (old->mxp_conn) {
2633 /* drop our ref taken above... */
2634 mxlnd_peer_decref(old);
2636 mxlnd_del_peer_locked(old);
2641 cfs_list_add_tail(&peer->mxp_list,
2642 &kmxlnd_data.kmx_peers[hash]);
2643 cfs_atomic_inc(&kmxlnd_data.kmx_npeers);
2646 tx->mxc_conn = old->mxp_conn;
2647 LASSERT(old->mxp_conn != NULL);
2648 mxlnd_conn_addref(old->mxp_conn);
2649 mxlnd_conn_decref(peer->mxp_conn); /* drop ref taken above.. */
2650 mxlnd_conn_decref(peer->mxp_conn); /* drop peer's ref */
2651 mxlnd_peer_decref(peer);
2653 cfs_write_unlock(g_lock);
2658 mxlnd_thread_stop(id);
2662 /* When calling this, we must not have the peer lock. */
2664 mxlnd_iconnect(kmx_peer_t *peer, u8 msg_type)
2666 mx_return_t mxret = MX_SUCCESS;
2667 mx_request_t request;
2668 kmx_conn_t *conn = peer->mxp_conn;
2669 u64 match = ((u64) msg_type) << MXLND_MSG_OFFSET;
2671 /* NOTE we are holding a conn ref every time we call this function,
2672 * we do not need to lock the peer before taking another ref */
2673 mxlnd_conn_addref(conn); /* hold until CONN_REQ or CONN_ACK completes */
2675 LASSERT(msg_type == MXLND_MSG_ICON_REQ || msg_type == MXLND_MSG_ICON_ACK);
2677 if (peer->mxp_reconnect_time == 0) {
2678 peer->mxp_reconnect_time = jiffies;
2681 if (peer->mxp_nic_id == 0ULL) {
2684 ret = mxlnd_ip2nic_id(LNET_NIDADDR(peer->mxp_nid),
2685 &peer->mxp_nic_id, MXLND_LOOKUP_COUNT);
2687 mx_nic_id_to_board_number(peer->mxp_nic_id, &peer->mxp_board);
2689 if (peer->mxp_nic_id == 0ULL && conn->mxk_status == MXLND_CONN_WAIT) {
2690 /* not mapped yet, return */
2691 cfs_spin_lock(&conn->mxk_lock);
2692 mxlnd_set_conn_status(conn, MXLND_CONN_INIT);
2693 cfs_spin_unlock(&conn->mxk_lock);
2697 if (cfs_time_after(jiffies,
2698 peer->mxp_reconnect_time + MXLND_CONNECT_TIMEOUT) &&
2699 conn->mxk_status != MXLND_CONN_DISCONNECT) {
2700 /* give up and notify LNET */
2701 CDEBUG(D_NET, "timeout trying to connect to %s\n",
2702 libcfs_nid2str(peer->mxp_nid));
2703 mxlnd_conn_disconnect(conn, 0, 0);
2704 mxlnd_conn_decref(conn);
2708 mxret = mx_iconnect(kmxlnd_data.kmx_endpt, peer->mxp_nic_id,
2709 peer->mxp_ep_id, MXLND_MSG_MAGIC, match,
2710 (void *) peer, &request);
2711 if (unlikely(mxret != MX_SUCCESS)) {
2712 cfs_spin_lock(&conn->mxk_lock);
2713 mxlnd_set_conn_status(conn, MXLND_CONN_FAIL);
2714 cfs_spin_unlock(&conn->mxk_lock);
2715 CDEBUG(D_NETERROR, "mx_iconnect() failed with %s (%d) to %s\n",
2716 mx_strerror(mxret), mxret, libcfs_nid2str(peer->mxp_nid));
2717 mxlnd_conn_decref(conn);
2719 mx_set_request_timeout(kmxlnd_data.kmx_endpt, request,
2720 MXLND_CONNECT_TIMEOUT/CFS_HZ*1000);
2724 #define MXLND_STATS 0
2727 mxlnd_check_sends(kmx_peer_t *peer)
2731 mx_return_t mxret = MX_SUCCESS;
2732 kmx_ctx_t *tx = NULL;
2733 kmx_conn_t *conn = NULL;
2740 static unsigned long last = 0;
2743 if (unlikely(peer == NULL)) {
2744 LASSERT(peer != NULL);
2747 cfs_write_lock(&kmxlnd_data.kmx_global_lock);
2748 conn = peer->mxp_conn;
2749 /* NOTE take a ref for the duration of this function since it is called
2750 * when there might not be any queued txs for this peer */
2752 if (conn->mxk_status == MXLND_CONN_DISCONNECT) {
2753 cfs_write_unlock(&kmxlnd_data.kmx_global_lock);
2756 mxlnd_conn_addref(conn); /* for duration of this function */
2758 cfs_write_unlock(&kmxlnd_data.kmx_global_lock);
2760 /* do not add another ref for this tx */
2763 /* we do not have any conns */
2764 CDEBUG(D_NETERROR, "peer %s has no conn\n", libcfs_nid2str(peer->mxp_nid));
2769 if (cfs_time_after(jiffies, last)) {
2770 last = jiffies + CFS_HZ;
2771 CDEBUG(D_NET, "status= %s credits= %d outstanding= %d ntx_msgs= %d "
2772 "ntx_posted= %d ntx_data= %d data_posted= %d\n",
2773 mxlnd_connstatus_to_str(conn->mxk_status), conn->mxk_credits,
2774 conn->mxk_outstanding, conn->mxk_ntx_msgs, conn->mxk_ntx_posted,
2775 conn->mxk_ntx_data, conn->mxk_data_posted);
2779 cfs_spin_lock(&conn->mxk_lock);
2780 ntx_posted = conn->mxk_ntx_posted;
2781 credits = conn->mxk_credits;
2783 LASSERT(ntx_posted <= *kmxlnd_tunables.kmx_peercredits);
2784 LASSERT(ntx_posted >= 0);
2786 LASSERT(credits <= *kmxlnd_tunables.kmx_peercredits);
2787 LASSERT(credits >= 0);
2789 /* check number of queued msgs, ignore data */
2790 if (conn->mxk_outstanding >= MXLND_CREDIT_HIGHWATER()) {
2791 /* check if any txs queued that could return credits... */
2792 if (cfs_list_empty(&conn->mxk_tx_credit_queue) ||
2793 conn->mxk_ntx_msgs == 0) {
2794 /* if not, send a NOOP */
2795 tx = mxlnd_get_idle_tx();
2796 if (likely(tx != NULL)) {
2797 tx->mxc_peer = peer;
2798 tx->mxc_conn = peer->mxp_conn;
2799 mxlnd_conn_addref(conn); /* for this tx */
2800 mxlnd_init_tx_msg (tx, MXLND_MSG_NOOP, 0, peer->mxp_nid);
2801 tx->mxc_match = mxlnd_create_match(tx, 0);
2802 mxlnd_peer_queue_tx_locked(tx);
2809 /* if the peer is not ready, try to connect */
2810 if (unlikely(conn->mxk_status == MXLND_CONN_INIT ||
2811 conn->mxk_status == MXLND_CONN_FAIL)) {
2812 CDEBUG(D_NET, "status=%s\n", mxlnd_connstatus_to_str(conn->mxk_status));
2813 mxlnd_set_conn_status(conn, MXLND_CONN_WAIT);
2814 cfs_spin_unlock(&conn->mxk_lock);
2815 mxlnd_iconnect(peer, (u8) MXLND_MSG_ICON_REQ);
2819 while (!cfs_list_empty(&conn->mxk_tx_free_queue) ||
2820 !cfs_list_empty(&conn->mxk_tx_credit_queue)) {
2821 /* We have something to send. If we have a queued tx that does not
2822 * require a credit (free), choose it since its completion will
2823 * return a credit (here or at the peer), complete a DATA or
2824 * CONN_REQ or CONN_ACK. */
2825 cfs_list_t *tmp_tx = NULL;
2826 if (!cfs_list_empty(&conn->mxk_tx_free_queue)) {
2827 tmp_tx = &conn->mxk_tx_free_queue;
2829 tmp_tx = &conn->mxk_tx_credit_queue;
2831 tx = cfs_list_entry(tmp_tx->next, kmx_ctx_t, mxc_list);
2833 msg_type = tx->mxc_msg_type;
2835 /* don't try to send a rx */
2836 LASSERT(tx->mxc_type == MXLND_REQ_TX);
2838 /* ensure that it is a valid msg type */
2839 LASSERT(msg_type == MXLND_MSG_CONN_REQ ||
2840 msg_type == MXLND_MSG_CONN_ACK ||
2841 msg_type == MXLND_MSG_NOOP ||
2842 msg_type == MXLND_MSG_EAGER ||
2843 msg_type == MXLND_MSG_PUT_REQ ||
2844 msg_type == MXLND_MSG_PUT_ACK ||
2845 msg_type == MXLND_MSG_PUT_DATA ||
2846 msg_type == MXLND_MSG_GET_REQ ||
2847 msg_type == MXLND_MSG_GET_DATA);
2848 LASSERT(tx->mxc_peer == peer);
2849 LASSERT(tx->mxc_nid == peer->mxp_nid);
2851 credit = mxlnd_tx_requires_credit(tx);
2854 if (conn->mxk_ntx_posted == *kmxlnd_tunables.kmx_peercredits) {
2855 CDEBUG(D_NET, "%s: posted enough\n",
2856 libcfs_nid2str(peer->mxp_nid));
2860 if (conn->mxk_credits == 0) {
2861 CDEBUG(D_NET, "%s: no credits\n",
2862 libcfs_nid2str(peer->mxp_nid));
2866 if (conn->mxk_credits == 1 && /* last credit reserved for */
2867 conn->mxk_outstanding == 0) { /* giving back credits */
2868 CDEBUG(D_NET, "%s: not using last credit\n",
2869 libcfs_nid2str(peer->mxp_nid));
2874 if (unlikely(conn->mxk_status != MXLND_CONN_READY)) {
2875 if ( ! (msg_type == MXLND_MSG_CONN_REQ ||
2876 msg_type == MXLND_MSG_CONN_ACK)) {
2877 CDEBUG(D_NET, "peer status is %s for tx 0x%llx (%s)\n",
2878 mxlnd_connstatus_to_str(conn->mxk_status),
2880 mxlnd_msgtype_to_str(tx->mxc_msg_type));
2881 if (conn->mxk_status == MXLND_CONN_DISCONNECT ||
2882 cfs_time_aftereq(jiffies, tx->mxc_deadline)) {
2883 cfs_list_del_init(&tx->mxc_list);
2884 tx->mxc_errno = -ECONNABORTED;
2885 cfs_spin_unlock(&conn->mxk_lock);
2886 mxlnd_put_idle_tx(tx);
2887 mxlnd_conn_decref(conn);
2894 cfs_list_del_init(&tx->mxc_list);
2896 /* handle credits, etc now while we have the lock to avoid races */
2898 conn->mxk_credits--;
2899 conn->mxk_ntx_posted++;
2901 if (msg_type != MXLND_MSG_PUT_DATA &&
2902 msg_type != MXLND_MSG_GET_DATA) {
2903 if (msg_type != MXLND_MSG_CONN_REQ &&
2904 msg_type != MXLND_MSG_CONN_ACK) {
2905 conn->mxk_ntx_msgs--;
2908 if (tx->mxc_incarnation == 0 &&
2909 conn->mxk_incarnation != 0) {
2910 tx->mxc_incarnation = conn->mxk_incarnation;
2913 /* if this is a NOOP and (1) mxp_conn->mxk_outstanding < CREDIT_HIGHWATER
2914 * or (2) there is a non-DATA msg that can return credits in the
2915 * queue, then drop this duplicate NOOP */
2916 if (unlikely(msg_type == MXLND_MSG_NOOP)) {
2917 if ((conn->mxk_outstanding < MXLND_CREDIT_HIGHWATER()) ||
2918 (conn->mxk_ntx_msgs >= 1)) {
2919 conn->mxk_credits++;
2920 conn->mxk_ntx_posted--;
2921 cfs_spin_unlock(&conn->mxk_lock);
2922 /* redundant NOOP */
2923 mxlnd_put_idle_tx(tx);
2924 mxlnd_conn_decref(conn);
2925 CDEBUG(D_NET, "%s: redundant noop\n",
2926 libcfs_nid2str(peer->mxp_nid));
2933 if (likely((msg_type != MXLND_MSG_PUT_DATA) &&
2934 (msg_type != MXLND_MSG_GET_DATA))) {
2935 mxlnd_pack_msg_locked(tx);
2940 status = conn->mxk_status;
2941 cfs_spin_unlock(&conn->mxk_lock);
2943 if (likely((status == MXLND_CONN_READY) ||
2944 (msg_type == MXLND_MSG_CONN_REQ) ||
2945 (msg_type == MXLND_MSG_CONN_ACK))) {
2947 if (msg_type != MXLND_MSG_CONN_REQ &&
2948 msg_type != MXLND_MSG_CONN_ACK) {
2949 /* add to the pending list */
2950 ret = mxlnd_q_pending_ctx(tx);
2953 tx->mxc_state = MXLND_CTX_PENDING;
2957 if (likely(msg_type != MXLND_MSG_PUT_DATA &&
2958 msg_type != MXLND_MSG_GET_DATA)) {
2959 /* send a msg style tx */
2960 LASSERT(tx->mxc_nseg == 1);
2961 LASSERT(tx->mxc_pin_type == MX_PIN_PHYSICAL);
2962 CDEBUG(D_NET, "sending %s 0x%llx\n",
2963 mxlnd_msgtype_to_str(msg_type),
2965 mxret = mx_kisend(kmxlnd_data.kmx_endpt,
2974 /* send a DATA tx */
2975 cfs_spin_lock(&conn->mxk_lock);
2976 conn->mxk_ntx_data--;
2977 conn->mxk_data_posted++;
2978 cfs_spin_unlock(&conn->mxk_lock);
2979 CDEBUG(D_NET, "sending %s 0x%llx\n",
2980 mxlnd_msgtype_to_str(msg_type),
2982 mxret = mx_kisend(kmxlnd_data.kmx_endpt,
2993 mxret = MX_CONNECTION_FAILED;
2995 if (likely(mxret == MX_SUCCESS)) {
2998 CDEBUG(D_NETERROR, "mx_kisend() failed with %s (%d) "
2999 "sending to %s\n", mx_strerror(mxret), (int) mxret,
3000 libcfs_nid2str(peer->mxp_nid));
3001 /* NOTE mx_kisend() only fails if there are not enough
3002 * resources. Do not change the connection status. */
3003 if (mxret == MX_NO_RESOURCES) {
3004 tx->mxc_errno = -ENOMEM;
3006 tx->mxc_errno = -ECONNABORTED;
3009 cfs_spin_lock(&conn->mxk_lock);
3010 conn->mxk_ntx_posted--;
3011 conn->mxk_credits++;
3012 cfs_spin_unlock(&conn->mxk_lock);
3013 } else if (msg_type == MXLND_MSG_PUT_DATA ||
3014 msg_type == MXLND_MSG_GET_DATA) {
3015 cfs_spin_lock(&conn->mxk_lock);
3016 conn->mxk_data_posted--;
3017 cfs_spin_unlock(&conn->mxk_lock);
3019 if (msg_type != MXLND_MSG_PUT_DATA &&
3020 msg_type != MXLND_MSG_GET_DATA &&
3021 msg_type != MXLND_MSG_CONN_REQ &&
3022 msg_type != MXLND_MSG_CONN_ACK) {
3023 cfs_spin_lock(&conn->mxk_lock);
3024 conn->mxk_outstanding += tx->mxc_msg->mxm_credits;
3025 cfs_spin_unlock(&conn->mxk_lock);
3027 if (msg_type != MXLND_MSG_CONN_REQ &&
3028 msg_type != MXLND_MSG_CONN_ACK) {
3029 /* remove from the pending list */
3030 mxlnd_deq_pending_ctx(tx);
3032 mxlnd_put_idle_tx(tx);
3033 mxlnd_conn_decref(conn);
3036 cfs_spin_lock(&conn->mxk_lock);
3039 cfs_spin_unlock(&conn->mxk_lock);
3041 mxlnd_conn_decref(conn); /* drop ref taken at start of function */
3047 * mxlnd_handle_tx_completion - a tx completed, progress or complete the msg
3048 * @ctx - the tx descriptor
3050 * Determine which type of send request it was and start the next step, if needed,
3051 * or, if done, signal completion to LNET. After we are done, put back on the
3055 mxlnd_handle_tx_completion(kmx_ctx_t *tx)
3057 int code = tx->mxc_status.code;
3058 int failed = (code != MX_STATUS_SUCCESS || tx->mxc_errno != 0);
3059 kmx_msg_t *msg = tx->mxc_msg;
3060 kmx_peer_t *peer = tx->mxc_peer;
3061 kmx_conn_t *conn = tx->mxc_conn;
3062 u8 type = tx->mxc_msg_type;
3063 int credit = mxlnd_tx_requires_credit(tx);
3064 u64 cookie = tx->mxc_cookie;
3066 CDEBUG(D_NET, "entering %s (0x%llx):\n",
3067 mxlnd_msgtype_to_str(tx->mxc_msg_type), cookie);
3069 LASSERT (peer != NULL);
3070 LASSERT (conn != NULL);
3072 if (type != MXLND_MSG_PUT_DATA && type != MXLND_MSG_GET_DATA) {
3073 LASSERT (type == msg->mxm_type);
3077 if (tx->mxc_errno == 0) tx->mxc_errno = -EIO;
3079 cfs_spin_lock(&conn->mxk_lock);
3080 conn->mxk_last_tx = cfs_time_current(); /* jiffies */
3081 cfs_spin_unlock(&conn->mxk_lock);
3086 case MXLND_MSG_GET_DATA:
3087 cfs_spin_lock(&conn->mxk_lock);
3088 if (conn->mxk_incarnation == tx->mxc_incarnation) {
3089 conn->mxk_outstanding++;
3090 conn->mxk_data_posted--;
3092 cfs_spin_unlock(&conn->mxk_lock);
3095 case MXLND_MSG_PUT_DATA:
3096 cfs_spin_lock(&conn->mxk_lock);
3097 if (conn->mxk_incarnation == tx->mxc_incarnation) {
3098 conn->mxk_data_posted--;
3100 cfs_spin_unlock(&conn->mxk_lock);
3103 case MXLND_MSG_NOOP:
3104 case MXLND_MSG_PUT_REQ:
3105 case MXLND_MSG_PUT_ACK:
3106 case MXLND_MSG_GET_REQ:
3107 case MXLND_MSG_EAGER:
3110 case MXLND_MSG_CONN_ACK:
3111 if (peer->mxp_incompatible) {
3112 /* we sent our params, now close this conn */
3113 mxlnd_conn_disconnect(conn, 0, 1);
3115 case MXLND_MSG_CONN_REQ:
3117 CDEBUG(D_NETERROR, "%s failed with %s (%d) (errno = %d)"
3119 type == MXLND_MSG_CONN_REQ ? "CONN_REQ" : "CONN_ACK",
3120 mx_strstatus(code), code, tx->mxc_errno,
3121 libcfs_nid2str(tx->mxc_nid));
3122 if (!peer->mxp_incompatible) {
3123 cfs_spin_lock(&conn->mxk_lock);
3124 if (code == MX_STATUS_BAD_SESSION)
3125 mxlnd_set_conn_status(conn, MXLND_CONN_INIT);
3127 mxlnd_set_conn_status(conn, MXLND_CONN_FAIL);
3128 cfs_spin_unlock(&conn->mxk_lock);
3134 CDEBUG(D_NETERROR, "Unknown msg type of %d\n", type);
3139 cfs_spin_lock(&conn->mxk_lock);
3140 if (conn->mxk_incarnation == tx->mxc_incarnation) {
3141 conn->mxk_ntx_posted--;
3143 cfs_spin_unlock(&conn->mxk_lock);
3146 mxlnd_put_idle_tx(tx);
3147 mxlnd_conn_decref(conn);
3149 mxlnd_check_sends(peer);
3151 CDEBUG(D_NET, "leaving\n");
3155 /* Handle completion of MSG or DATA rx.
3156 * CONN_REQ and CONN_ACK are handled elsewhere. */
3158 mxlnd_handle_rx_completion(kmx_ctx_t *rx)
3163 u32 nob = rx->mxc_status.xfer_length;
3164 u64 bits = rx->mxc_status.match_info;
3165 kmx_msg_t *msg = rx->mxc_msg;
3166 kmx_peer_t *peer = rx->mxc_peer;
3167 kmx_conn_t *conn = rx->mxc_conn;
3168 u8 type = rx->mxc_msg_type;
3170 lnet_msg_t *lntmsg[2];
3175 /* NOTE We may only know the peer's nid if it is a PUT_REQ, GET_REQ,
3176 * failed GET reply */
3178 /* NOTE peer may still be NULL if it is a new peer and
3179 * conn may be NULL if this is a re-connect */
3180 if (likely(peer != NULL && conn != NULL)) {
3181 /* we have a reference on the conn */
3183 } else if (peer != NULL && conn == NULL) {
3184 /* we have a reference on the peer */
3186 } else if (peer == NULL && conn != NULL) {
3188 CERROR("rx 0x%llx from %s has conn but no peer\n",
3189 bits, libcfs_nid2str(rx->mxc_nid));
3191 } /* else peer and conn == NULL */
3193 if (conn == NULL && peer != NULL) {
3194 cfs_write_lock(&kmxlnd_data.kmx_global_lock);
3195 conn = peer->mxp_conn;
3197 mxlnd_conn_addref(conn); /* conn takes ref... */
3198 mxlnd_peer_decref(peer); /* from peer */
3202 cfs_write_unlock(&kmxlnd_data.kmx_global_lock);
3203 rx->mxc_conn = conn;
3207 CDEBUG(D_NET, "receiving msg bits=0x%llx nob=%d peer=0x%p\n", bits, nob, peer);
3213 if (rx->mxc_status.code != MX_STATUS_SUCCESS &&
3214 rx->mxc_status.code != MX_STATUS_TRUNCATED) {
3215 CDEBUG(D_NETERROR, "rx from %s failed with %s (%d)\n",
3216 libcfs_nid2str(rx->mxc_nid),
3217 mx_strstatus(rx->mxc_status.code),
3218 rx->mxc_status.code);
3224 /* this may be a failed GET reply */
3225 if (type == MXLND_MSG_GET_DATA) {
3226 /* get the error (52-59) bits from the match bits */
3227 ret = (u32) MXLND_ERROR_VAL(rx->mxc_status.match_info);
3228 lntmsg[0] = rx->mxc_lntmsg[0];
3232 /* we had a rx complete with 0 bytes (no hdr, nothing) */
3233 CDEBUG(D_NETERROR, "rx from %s returned with 0 bytes\n",
3234 libcfs_nid2str(rx->mxc_nid));
3239 /* NOTE PUT_DATA and GET_DATA do not have mxc_msg, do not call unpack() */
3240 if (type == MXLND_MSG_PUT_DATA) {
3242 lntmsg[0] = rx->mxc_lntmsg[0];
3244 } else if (type == MXLND_MSG_GET_DATA) {
3246 lntmsg[0] = rx->mxc_lntmsg[0];
3247 lntmsg[1] = rx->mxc_lntmsg[1];
3251 ret = mxlnd_unpack_msg(msg, nob);
3253 CDEBUG(D_NETERROR, "Error %d unpacking rx from %s\n",
3254 ret, libcfs_nid2str(rx->mxc_nid));
3258 type = msg->mxm_type;
3260 if (rx->mxc_nid != msg->mxm_srcnid ||
3261 kmxlnd_data.kmx_ni->ni_nid != msg->mxm_dstnid) {
3262 CDEBUG(D_NETERROR, "rx with mismatched NID (type %s) (my nid is "
3263 "0x%llx and rx msg dst is 0x%llx)\n",
3264 mxlnd_msgtype_to_str(type), kmxlnd_data.kmx_ni->ni_nid,
3269 if ((conn != NULL && msg->mxm_srcstamp != conn->mxk_incarnation) ||
3270 msg->mxm_dststamp != kmxlnd_data.kmx_incarnation) {
3271 CDEBUG(D_NETERROR, "Stale rx from %s with type %s "
3272 "(mxm_srcstamp (%lld) != mxk_incarnation (%lld) "
3273 "|| mxm_dststamp (%lld) != kmx_incarnation (%lld))\n",
3274 libcfs_nid2str(rx->mxc_nid), mxlnd_msgtype_to_str(type),
3275 msg->mxm_srcstamp, conn->mxk_incarnation,
3276 msg->mxm_dststamp, kmxlnd_data.kmx_incarnation);
3281 CDEBUG(D_NET, "Received %s with %d credits\n",
3282 mxlnd_msgtype_to_str(type), msg->mxm_credits);
3284 LASSERT(peer != NULL && conn != NULL);
3285 if (msg->mxm_credits != 0) {
3286 cfs_spin_lock(&conn->mxk_lock);
3287 if (msg->mxm_srcstamp == conn->mxk_incarnation) {
3288 if ((conn->mxk_credits + msg->mxm_credits) >
3289 *kmxlnd_tunables.kmx_peercredits) {
3290 CDEBUG(D_NETERROR, "mxk_credits %d mxm_credits %d\n",
3291 conn->mxk_credits, msg->mxm_credits);
3293 conn->mxk_credits += msg->mxm_credits;
3294 LASSERT(conn->mxk_credits >= 0);
3295 LASSERT(conn->mxk_credits <= *kmxlnd_tunables.kmx_peercredits);
3297 cfs_spin_unlock(&conn->mxk_lock);
3300 CDEBUG(D_NET, "switch %s for rx (0x%llx)\n", mxlnd_msgtype_to_str(type), seq);
3302 case MXLND_MSG_NOOP:
3305 case MXLND_MSG_EAGER:
3306 ret = lnet_parse(kmxlnd_data.kmx_ni, &msg->mxm_u.eager.mxem_hdr,
3307 msg->mxm_srcnid, rx, 0);
3311 case MXLND_MSG_PUT_REQ:
3312 ret = lnet_parse(kmxlnd_data.kmx_ni, &msg->mxm_u.put_req.mxprm_hdr,
3313 msg->mxm_srcnid, rx, 1);
3317 case MXLND_MSG_PUT_ACK: {
3318 u64 cookie = (u64) msg->mxm_u.put_ack.mxpam_dst_cookie;
3319 if (cookie > MXLND_MAX_COOKIE) {
3320 CDEBUG(D_NETERROR, "NAK for msg_type %d from %s\n", rx->mxc_msg_type,
3321 libcfs_nid2str(rx->mxc_nid));
3322 result = -((u32) MXLND_ERROR_VAL(cookie));
3323 lntmsg[0] = rx->mxc_lntmsg[0];
3325 mxlnd_send_data(kmxlnd_data.kmx_ni, rx->mxc_lntmsg[0],
3326 rx->mxc_peer, MXLND_MSG_PUT_DATA,
3327 rx->mxc_msg->mxm_u.put_ack.mxpam_dst_cookie);
3332 case MXLND_MSG_GET_REQ:
3333 ret = lnet_parse(kmxlnd_data.kmx_ni, &msg->mxm_u.get_req.mxgrm_hdr,
3334 msg->mxm_srcnid, rx, 1);
3339 CDEBUG(D_NETERROR, "Bad MXLND message type %x from %s\n", msg->mxm_type,
3340 libcfs_nid2str(rx->mxc_nid));
3346 CDEBUG(D_NET, "setting PEER_CONN_FAILED\n");
3347 cfs_spin_lock(&conn->mxk_lock);
3348 mxlnd_set_conn_status(conn, MXLND_CONN_FAIL);
3349 cfs_spin_unlock(&conn->mxk_lock);
3354 cfs_spin_lock(&conn->mxk_lock);
3355 conn->mxk_last_rx = cfs_time_current(); /* jiffies */
3356 cfs_spin_unlock(&conn->mxk_lock);
3360 /* lnet_parse() failed, etc., repost now */
3361 mxlnd_put_idle_rx(rx);
3362 if (conn != NULL && credit == 1) {
3363 if (type == MXLND_MSG_PUT_DATA ||
3364 type == MXLND_MSG_EAGER ||
3365 type == MXLND_MSG_PUT_REQ ||
3366 type == MXLND_MSG_NOOP) {
3367 cfs_spin_lock(&conn->mxk_lock);
3368 conn->mxk_outstanding++;
3369 cfs_spin_unlock(&conn->mxk_lock);
3372 if (conn_ref) mxlnd_conn_decref(conn);
3373 LASSERT(peer_ref == 0);
3376 if (type == MXLND_MSG_PUT_DATA || type == MXLND_MSG_GET_DATA) {
3377 CDEBUG(D_NET, "leaving for rx (0x%llx)\n", bits);
3379 CDEBUG(D_NET, "leaving for rx (0x%llx)\n", seq);
3382 if (lntmsg[0] != NULL) lnet_finalize(kmxlnd_data.kmx_ni, lntmsg[0], result);
3383 if (lntmsg[1] != NULL) lnet_finalize(kmxlnd_data.kmx_ni, lntmsg[1], result);
3385 if (conn != NULL && credit == 1) mxlnd_check_sends(peer);
3391 mxlnd_handle_connect_msg(kmx_peer_t *peer, u8 msg_type, mx_status_t status)
3393 kmx_ctx_t *tx = NULL;
3394 kmx_msg_t *txmsg = NULL;
3395 kmx_conn_t *conn = peer->mxp_conn;
3399 u8 type = (msg_type == MXLND_MSG_ICON_REQ ?
3400 MXLND_MSG_CONN_REQ : MXLND_MSG_CONN_ACK);
3402 /* a conn ref was taken when calling mx_iconnect(),
3403 * hold it until CONN_REQ or CONN_ACK completes */
3405 CDEBUG(D_NET, "entering\n");
3406 if (status.code != MX_STATUS_SUCCESS) {
3407 int send_bye = (msg_type == MXLND_MSG_ICON_REQ ? 0 : 1);
3409 CDEBUG(D_NETERROR, "mx_iconnect() failed for %s with %s (%d) "
3410 "to %s mxp_nid = 0x%llx mxp_nic_id = 0x%0llx mxp_ep_id = %d\n",
3411 mxlnd_msgtype_to_str(msg_type),
3412 mx_strstatus(status.code), status.code,
3413 libcfs_nid2str(peer->mxp_nid),
3417 cfs_spin_lock(&conn->mxk_lock);
3418 mxlnd_set_conn_status(conn, MXLND_CONN_FAIL);
3419 cfs_spin_unlock(&conn->mxk_lock);
3421 if (cfs_time_after(jiffies, peer->mxp_reconnect_time +
3422 MXLND_CONNECT_TIMEOUT)) {
3423 CDEBUG(D_NETERROR, "timeout, calling conn_disconnect()\n");
3424 mxlnd_conn_disconnect(conn, 0, send_bye);
3427 mxlnd_conn_decref(conn);
3430 mx_decompose_endpoint_addr2(status.source, &nic_id, &ep_id, &sid);
3431 cfs_write_lock(&kmxlnd_data.kmx_global_lock);
3432 cfs_spin_lock(&conn->mxk_lock);
3433 conn->mxk_epa = status.source;
3434 mx_set_endpoint_addr_context(conn->mxk_epa, (void *) conn);
3435 if (msg_type == MXLND_MSG_ICON_ACK && likely(!peer->mxp_incompatible)) {
3436 mxlnd_set_conn_status(conn, MXLND_CONN_READY);
3438 cfs_spin_unlock(&conn->mxk_lock);
3439 cfs_write_unlock(&kmxlnd_data.kmx_global_lock);
3441 /* mx_iconnect() succeeded, reset delay to 0 */
3442 cfs_write_lock(&kmxlnd_data.kmx_global_lock);
3443 peer->mxp_reconnect_time = 0;
3444 peer->mxp_conn->mxk_sid = sid;
3445 cfs_write_unlock(&kmxlnd_data.kmx_global_lock);
3447 /* marshal CONN_REQ or CONN_ACK msg */
3448 /* we are still using the conn ref from iconnect() - do not take another */
3449 tx = mxlnd_get_idle_tx();
3451 CDEBUG(D_NETERROR, "Can't obtain %s tx for %s\n",
3452 mxlnd_msgtype_to_str(type),
3453 libcfs_nid2str(peer->mxp_nid));
3454 cfs_spin_lock(&conn->mxk_lock);
3455 mxlnd_set_conn_status(conn, MXLND_CONN_FAIL);
3456 cfs_spin_unlock(&conn->mxk_lock);
3457 mxlnd_conn_decref(conn);
3461 tx->mxc_peer = peer;
3462 tx->mxc_conn = conn;
3463 tx->mxc_deadline = jiffies + MXLND_CONNECT_TIMEOUT;
3464 CDEBUG(D_NET, "sending %s\n", mxlnd_msgtype_to_str(type));
3465 mxlnd_init_tx_msg (tx, type, sizeof(kmx_connreq_msg_t), peer->mxp_nid);
3466 txmsg = tx->mxc_msg;
3467 txmsg->mxm_u.conn_req.mxcrm_queue_depth = *kmxlnd_tunables.kmx_peercredits;
3468 txmsg->mxm_u.conn_req.mxcrm_eager_size = MXLND_MSG_SIZE;
3469 tx->mxc_match = mxlnd_create_match(tx, 0);
3476 * mxlnd_request_waitd - the MX request completion thread(s)
3477 * @arg - thread id (as a void *)
3479 * This thread waits for a MX completion and then completes the request.
3480 * We will create one thread per CPU.
3483 mxlnd_request_waitd(void *arg)
3485 long id = (long) arg;
3488 mx_return_t mxret = MX_SUCCESS;
3490 kmx_ctx_t *ctx = NULL;
3491 enum kmx_req_state req_type = MXLND_REQ_TX;
3492 kmx_peer_t *peer = NULL;
3493 kmx_conn_t *conn = NULL;
3498 memset(name, 0, sizeof(name));
3499 snprintf(name, sizeof(name), "mxlnd_request_waitd_%02ld", id);
3500 cfs_daemonize(name);
3502 memset(&status, 0, sizeof(status));
3504 CDEBUG(D_NET, "%s starting\n", name);
3506 while (!(cfs_atomic_read(&kmxlnd_data.kmx_shutdown))) {
3512 if (id == 0 && count++ < *kmxlnd_tunables.kmx_polling) {
3513 mxret = mx_test_any(kmxlnd_data.kmx_endpt, 0ULL, 0ULL,
3517 mxret = mx_wait_any(kmxlnd_data.kmx_endpt, MXLND_WAIT_TIMEOUT,
3518 0ULL, 0ULL, &status, &result);
3521 mxret = mx_wait_any(kmxlnd_data.kmx_endpt, MXLND_WAIT_TIMEOUT,
3522 0ULL, 0ULL, &status, &result);
3524 if (unlikely(cfs_atomic_read(&kmxlnd_data.kmx_shutdown)))
3528 /* nothing completed... */
3532 CDEBUG(D_NET, "wait_any() returned with %s (%d) with "
3533 "match_info 0x%llx and length %d\n",
3534 mx_strstatus(status.code), status.code,
3535 (u64) status.match_info, status.msg_length);
3537 if (status.code != MX_STATUS_SUCCESS) {
3538 CDEBUG(D_NETERROR, "wait_any() failed with %s (%d) with "
3539 "match_info 0x%llx and length %d\n",
3540 mx_strstatus(status.code), status.code,
3541 (u64) status.match_info, status.msg_length);
3544 msg_type = MXLND_MSG_TYPE(status.match_info);
3546 /* This may be a mx_iconnect() request completing,
3547 * check the bit mask for CONN_REQ and CONN_ACK */
3548 if (msg_type == MXLND_MSG_ICON_REQ ||
3549 msg_type == MXLND_MSG_ICON_ACK) {
3550 peer = (kmx_peer_t*) status.context;
3551 mxlnd_handle_connect_msg(peer, msg_type, status);
3555 /* This must be a tx or rx */
3557 /* NOTE: if this is a RX from the unexpected callback, it may
3558 * have very little info. If we dropped it in unexpected_recv(),
3559 * it will not have a context. If so, ignore it. */
3560 ctx = (kmx_ctx_t *) status.context;
3563 req_type = ctx->mxc_type;
3564 conn = ctx->mxc_conn; /* this may be NULL */
3565 mxlnd_deq_pending_ctx(ctx);
3567 /* copy status to ctx->mxc_status */
3568 ctx->mxc_status = status;
3572 mxlnd_handle_tx_completion(ctx);
3575 mxlnd_handle_rx_completion(ctx);
3578 CDEBUG(D_NETERROR, "Unknown ctx type %d\n", req_type);
3583 /* conn is always set except for the first CONN_REQ rx
3584 * from a new peer */
3585 if (status.code != MX_STATUS_SUCCESS && conn != NULL) {
3586 mxlnd_conn_disconnect(conn, 1, 1);
3589 CDEBUG(D_NET, "waitd() completed task\n");
3591 CDEBUG(D_NET, "%s stopping\n", name);
3592 mxlnd_thread_stop(id);
3598 mxlnd_check_timeouts(unsigned long now)
3602 unsigned long next = 0; /* jiffies */
3603 kmx_peer_t *peer = NULL;
3604 kmx_conn_t *conn = NULL;
3605 cfs_rwlock_t *g_lock = &kmxlnd_data.kmx_global_lock;
3607 cfs_read_lock(g_lock);
3608 for (i = 0; i < MXLND_HASH_SIZE; i++) {
3609 cfs_list_for_each_entry(peer, &kmxlnd_data.kmx_peers[i],
3612 if (unlikely(cfs_atomic_read(&kmxlnd_data.kmx_shutdown))) {
3613 cfs_read_unlock(g_lock);
3617 conn = peer->mxp_conn;
3619 mxlnd_conn_addref(conn);
3624 cfs_spin_lock(&conn->mxk_lock);
3626 /* if nothing pending (timeout == 0) or
3627 * if conn is already disconnected,
3629 if (conn->mxk_timeout == 0 ||
3630 conn->mxk_status == MXLND_CONN_DISCONNECT) {
3631 cfs_spin_unlock(&conn->mxk_lock);
3632 mxlnd_conn_decref(conn);
3636 /* we want to find the timeout that will occur first.
3637 * if it is in the future, we will sleep until then.
3638 * if it is in the past, then we will sleep one
3639 * second and repeat the process. */
3641 (cfs_time_before(conn->mxk_timeout, next))) {
3642 next = conn->mxk_timeout;
3647 if (cfs_time_aftereq(now, conn->mxk_timeout)) {
3650 cfs_spin_unlock(&conn->mxk_lock);
3653 mxlnd_conn_disconnect(conn, 1, 1);
3655 mxlnd_conn_decref(conn);
3658 cfs_read_unlock(g_lock);
3659 if (next == 0) next = now + MXLND_COMM_TIMEOUT;
3665 mxlnd_passive_connect(kmx_connparams_t *cp)
3668 int incompatible = 0;
3673 kmx_msg_t *msg = &cp->mxr_msg;
3674 kmx_peer_t *peer = cp->mxr_peer;
3675 kmx_conn_t *conn = NULL;
3676 cfs_rwlock_t *g_lock = &kmxlnd_data.kmx_global_lock;
3678 mx_decompose_endpoint_addr2(cp->mxr_epa, &nic_id, &ep_id, &sid);
3680 ret = mxlnd_unpack_msg(msg, cp->mxr_nob);
3683 CDEBUG(D_NETERROR, "Error %d unpacking CONN_REQ from %s\n",
3684 ret, libcfs_nid2str(peer->mxp_nid));
3686 CDEBUG(D_NETERROR, "Error %d unpacking CONN_REQ from "
3687 "unknown host with nic_id 0x%llx\n", ret, nic_id);
3691 if (kmxlnd_data.kmx_ni->ni_nid != msg->mxm_dstnid) {
3692 CDEBUG(D_NETERROR, "Can't accept %s: bad dst nid %s\n",
3693 libcfs_nid2str(msg->mxm_srcnid),
3694 libcfs_nid2str(msg->mxm_dstnid));
3697 if (msg->mxm_u.conn_req.mxcrm_queue_depth != *kmxlnd_tunables.kmx_peercredits) {
3698 CDEBUG(D_NETERROR, "Can't accept %s: incompatible queue depth "
3700 libcfs_nid2str(msg->mxm_srcnid),
3701 msg->mxm_u.conn_req.mxcrm_queue_depth,
3702 *kmxlnd_tunables.kmx_peercredits);
3705 if (msg->mxm_u.conn_req.mxcrm_eager_size != MXLND_MSG_SIZE) {
3706 CDEBUG(D_NETERROR, "Can't accept %s: incompatible EAGER size "
3708 libcfs_nid2str(msg->mxm_srcnid),
3709 msg->mxm_u.conn_req.mxcrm_eager_size,
3710 (int) MXLND_MSG_SIZE);
3715 peer = mxlnd_find_peer_by_nid(msg->mxm_srcnid, 0); /* adds peer ref */
3719 kmx_peer_t *existing_peer = NULL;
3721 hash = mxlnd_nid_to_hash(msg->mxm_srcnid);
3723 mx_nic_id_to_board_number(nic_id, &board);
3725 /* adds conn ref for peer and one for this function */
3726 ret = mxlnd_peer_alloc(&peer, msg->mxm_srcnid,
3727 board, ep_id, 0ULL);
3731 peer->mxp_conn->mxk_sid = sid;
3732 LASSERT(peer->mxp_ep_id == ep_id);
3733 cfs_write_lock(g_lock);
3734 existing_peer = mxlnd_find_peer_by_nid_locked(msg->mxm_srcnid);
3735 if (existing_peer) {
3736 mxlnd_conn_decref(peer->mxp_conn);
3737 mxlnd_peer_decref(peer);
3738 peer = existing_peer;
3739 mxlnd_conn_addref(peer->mxp_conn);
3740 conn = peer->mxp_conn;
3742 cfs_list_add_tail(&peer->mxp_list,
3743 &kmxlnd_data.kmx_peers[hash]);
3744 cfs_atomic_inc(&kmxlnd_data.kmx_npeers);
3746 cfs_write_unlock(g_lock);
3748 ret = mxlnd_conn_alloc(&conn, peer); /* adds 2nd ref */
3749 cfs_write_lock(g_lock);
3750 mxlnd_peer_decref(peer); /* drop ref taken above */
3751 cfs_write_unlock(g_lock);
3753 CDEBUG(D_NETERROR, "Cannot allocate mxp_conn\n");
3757 conn_ref = 1; /* peer/conn_alloc() added ref for this function */
3758 conn = peer->mxp_conn;
3759 } else { /* unexpected handler found peer */
3760 kmx_conn_t *old_conn = peer->mxp_conn;
3762 if (sid != peer->mxp_conn->mxk_sid) {
3763 /* do not call mx_disconnect() or send a BYE */
3764 mxlnd_conn_disconnect(old_conn, 0, 0);
3766 /* This allocs a conn, points peer->mxp_conn to this one.
3767 * The old conn is still on the peer->mxp_conns list.
3768 * As the pending requests complete, they will call
3769 * conn_decref() which will eventually free it. */
3770 ret = mxlnd_conn_alloc(&conn, peer);
3772 CDEBUG(D_NETERROR, "Cannot allocate peer->mxp_conn\n");
3775 /* conn_alloc() adds one ref for the peer and one
3776 * for this function */
3779 peer->mxp_conn->mxk_sid = sid;
3782 conn = peer->mxp_conn;
3785 cfs_write_lock(g_lock);
3786 peer->mxp_incompatible = incompatible;
3787 cfs_write_unlock(g_lock);
3788 cfs_spin_lock(&conn->mxk_lock);
3789 conn->mxk_incarnation = msg->mxm_srcstamp;
3790 mxlnd_set_conn_status(conn, MXLND_CONN_WAIT);
3791 cfs_spin_unlock(&conn->mxk_lock);
3793 /* handle_conn_ack() will create the CONN_ACK msg */
3794 mxlnd_iconnect(peer, (u8) MXLND_MSG_ICON_ACK);
3797 if (conn_ref) mxlnd_conn_decref(conn);
3799 mxlnd_connparams_free(cp);
3804 mxlnd_check_conn_ack(kmx_connparams_t *cp)
3807 int incompatible = 0;
3811 kmx_msg_t *msg = &cp->mxr_msg;
3812 kmx_peer_t *peer = cp->mxr_peer;
3813 kmx_conn_t *conn = cp->mxr_conn;
3815 mx_decompose_endpoint_addr2(cp->mxr_epa, &nic_id, &ep_id, &sid);
3817 ret = mxlnd_unpack_msg(msg, cp->mxr_nob);
3820 CDEBUG(D_NETERROR, "Error %d unpacking CONN_ACK from %s\n",
3821 ret, libcfs_nid2str(peer->mxp_nid));
3823 CDEBUG(D_NETERROR, "Error %d unpacking CONN_ACK from "
3824 "unknown host with nic_id 0x%llx\n", ret, nic_id);
3830 if (kmxlnd_data.kmx_ni->ni_nid != msg->mxm_dstnid) {
3831 CDEBUG(D_NETERROR, "Can't accept CONN_ACK from %s: "
3832 "bad dst nid %s\n", libcfs_nid2str(msg->mxm_srcnid),
3833 libcfs_nid2str(msg->mxm_dstnid));
3837 if (msg->mxm_u.conn_req.mxcrm_queue_depth != *kmxlnd_tunables.kmx_peercredits) {
3838 CDEBUG(D_NETERROR, "Can't accept CONN_ACK from %s: "
3839 "incompatible queue depth %d (%d wanted)\n",
3840 libcfs_nid2str(msg->mxm_srcnid),
3841 msg->mxm_u.conn_req.mxcrm_queue_depth,
3842 *kmxlnd_tunables.kmx_peercredits);
3847 if (msg->mxm_u.conn_req.mxcrm_eager_size != MXLND_MSG_SIZE) {
3848 CDEBUG(D_NETERROR, "Can't accept CONN_ACK from %s: "
3849 "incompatible EAGER size %d (%d wanted)\n",
3850 libcfs_nid2str(msg->mxm_srcnid),
3851 msg->mxm_u.conn_req.mxcrm_eager_size,
3852 (int) MXLND_MSG_SIZE);
3857 cfs_write_lock(&kmxlnd_data.kmx_global_lock);
3858 peer->mxp_incompatible = incompatible;
3859 cfs_write_unlock(&kmxlnd_data.kmx_global_lock);
3860 cfs_spin_lock(&conn->mxk_lock);
3861 conn->mxk_credits = *kmxlnd_tunables.kmx_peercredits;
3862 conn->mxk_outstanding = 0;
3863 conn->mxk_incarnation = msg->mxm_srcstamp;
3864 conn->mxk_timeout = 0;
3865 if (!incompatible) {
3866 CDEBUG(D_NET, "setting peer %s CONN_READY\n",
3867 libcfs_nid2str(msg->mxm_srcnid));
3868 mxlnd_set_conn_status(conn, MXLND_CONN_READY);
3870 cfs_spin_unlock(&conn->mxk_lock);
3873 mxlnd_check_sends(peer);
3877 cfs_spin_lock(&conn->mxk_lock);
3878 mxlnd_set_conn_status(conn, MXLND_CONN_FAIL);
3879 cfs_spin_unlock(&conn->mxk_lock);
3882 if (incompatible) mxlnd_conn_disconnect(conn, 0, 0);
3884 mxlnd_connparams_free(cp);
3889 mxlnd_abort_msgs(void)
3892 cfs_list_t *orphans = &kmxlnd_data.kmx_orphan_msgs;
3893 cfs_spinlock_t *g_conn_lock = &kmxlnd_data.kmx_conn_lock;
3896 cfs_spin_lock(g_conn_lock);
3897 while (!cfs_list_empty(orphans)) {
3898 kmx_ctx_t *ctx = NULL;
3899 kmx_conn_t *conn = NULL;
3901 ctx = cfs_list_entry(orphans->next, kmx_ctx_t, mxc_list);
3902 cfs_list_del_init(&ctx->mxc_list);
3903 cfs_spin_unlock(g_conn_lock);
3905 ctx->mxc_errno = -ECONNABORTED;
3906 conn = ctx->mxc_conn;
3907 CDEBUG(D_NET, "aborting %s %s %s\n",
3908 mxlnd_msgtype_to_str(ctx->mxc_msg_type),
3909 ctx->mxc_type == MXLND_REQ_TX ? "(TX) to" : "(RX) from",
3910 libcfs_nid2str(ctx->mxc_nid));
3911 if (ctx->mxc_type == MXLND_REQ_TX) {
3912 mxlnd_put_idle_tx(ctx); /* do not hold any locks */
3913 if (conn) mxlnd_conn_decref(conn); /* for this tx */
3915 ctx->mxc_state = MXLND_CTX_CANCELED;
3916 mxlnd_handle_rx_completion(ctx);
3920 cfs_spin_lock(g_conn_lock);
3922 cfs_spin_unlock(g_conn_lock);
3928 mxlnd_free_conn_zombies(void)
3931 cfs_list_t *zombies = &kmxlnd_data.kmx_conn_zombies;
3932 cfs_spinlock_t *g_conn_lock = &kmxlnd_data.kmx_conn_lock;
3933 cfs_rwlock_t *g_lock = &kmxlnd_data.kmx_global_lock;
3935 /* cleanup any zombies */
3936 cfs_spin_lock(g_conn_lock);
3937 while (!cfs_list_empty(zombies)) {
3938 kmx_conn_t *conn = NULL;
3940 conn = cfs_list_entry(zombies->next, kmx_conn_t, mxk_zombie);
3941 cfs_list_del_init(&conn->mxk_zombie);
3942 cfs_spin_unlock(g_conn_lock);
3944 cfs_write_lock(g_lock);
3945 mxlnd_conn_free_locked(conn);
3946 cfs_write_unlock(g_lock);
3949 cfs_spin_lock(g_conn_lock);
3951 cfs_spin_unlock(g_conn_lock);
3952 CDEBUG(D_NET, "%s: freed %d zombies\n", __func__, count);
3957 * mxlnd_connd - handles incoming connection requests
3958 * @arg - thread id (as a void *)
3960 * This thread handles incoming connection requests
3963 mxlnd_connd(void *arg)
3965 long id = (long) arg;
3967 cfs_daemonize("mxlnd_connd");
3969 CDEBUG(D_NET, "connd starting\n");
3971 while (!(cfs_atomic_read(&kmxlnd_data.kmx_shutdown))) {
3973 kmx_connparams_t *cp = NULL;
3974 cfs_spinlock_t *g_conn_lock = &kmxlnd_data.kmx_conn_lock;
3975 cfs_list_t *conn_reqs = &kmxlnd_data.kmx_conn_reqs;
3977 ret = down_interruptible(&kmxlnd_data.kmx_conn_sem);
3979 if (cfs_atomic_read(&kmxlnd_data.kmx_shutdown))
3985 ret = mxlnd_abort_msgs();
3986 ret += mxlnd_free_conn_zombies();
3988 cfs_spin_lock(g_conn_lock);
3989 if (cfs_list_empty(conn_reqs)) {
3991 CDEBUG(D_NETERROR, "connd woke up but did not "
3992 "find a kmx_connparams_t or zombie conn\n");
3993 cfs_spin_unlock(g_conn_lock);
3996 cp = cfs_list_entry(conn_reqs->next, kmx_connparams_t,
3998 cfs_list_del_init(&cp->mxr_list);
3999 cfs_spin_unlock(g_conn_lock);
4001 switch (MXLND_MSG_TYPE(cp->mxr_match)) {
4002 case MXLND_MSG_CONN_REQ:
4003 /* We have a connection request. Handle it. */
4004 mxlnd_passive_connect(cp);
4006 case MXLND_MSG_CONN_ACK:
4007 /* The peer is ready for messages */
4008 mxlnd_check_conn_ack(cp);
4013 mxlnd_free_conn_zombies();
4015 CDEBUG(D_NET, "connd stopping\n");
4016 mxlnd_thread_stop(id);
4021 * mxlnd_timeoutd - enforces timeouts on messages
4022 * @arg - thread id (as a void *)
4024 * This thread queries each peer for its earliest timeout. If a peer has timed out,
4025 * it calls mxlnd_conn_disconnect().
4027 * After checking for timeouts, try progressing sends (call check_sends()).
4030 mxlnd_timeoutd(void *arg)
4033 long id = (long) arg;
4034 unsigned long now = 0;
4035 unsigned long next = 0;
4036 unsigned long delay = CFS_HZ;
4037 kmx_peer_t *peer = NULL;
4038 kmx_peer_t *temp = NULL;
4039 kmx_conn_t *conn = NULL;
4040 cfs_rwlock_t *g_lock = &kmxlnd_data.kmx_global_lock;
4042 cfs_daemonize("mxlnd_timeoutd");
4044 CDEBUG(D_NET, "timeoutd starting\n");
4046 while (!(cfs_atomic_read(&kmxlnd_data.kmx_shutdown))) {
4049 /* if the next timeout has not arrived, go back to sleep */
4050 if (cfs_time_after(now, next)) {
4051 next = mxlnd_check_timeouts(now);
4054 /* try to progress peers' txs */
4055 cfs_write_lock(g_lock);
4056 for (i = 0; i < MXLND_HASH_SIZE; i++) {
4057 cfs_list_t *peers = &kmxlnd_data.kmx_peers[i];
4059 /* NOTE we are safe against the removal of peer, but
4060 * not against the removal of temp */
4061 cfs_list_for_each_entry_safe(peer, temp, peers,
4063 if (cfs_atomic_read(&kmxlnd_data.kmx_shutdown))
4065 mxlnd_peer_addref(peer); /* add ref... */
4066 conn = peer->mxp_conn;
4067 if (conn && conn->mxk_status != MXLND_CONN_DISCONNECT) {
4068 mxlnd_conn_addref(conn); /* take ref... */
4070 CDEBUG(D_NET, "ignoring %s\n",
4071 libcfs_nid2str(peer->mxp_nid));
4072 mxlnd_peer_decref(peer); /* ...to here */
4076 if ((conn->mxk_status == MXLND_CONN_READY ||
4077 conn->mxk_status == MXLND_CONN_FAIL) &&
4081 cfs_write_unlock(g_lock);
4082 mxlnd_check_sends(peer);
4083 cfs_write_lock(g_lock);
4085 mxlnd_conn_decref(conn); /* until here */
4086 mxlnd_peer_decref(peer); /* ...to here */
4089 cfs_write_unlock(g_lock);
4093 CDEBUG(D_NET, "timeoutd stopping\n");
4094 mxlnd_thread_stop(id);