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.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lnet/ulnds/socklnd/conn.c
38 * Author: Maxim Patlasov <maxim@clusterfs.com>
43 /* Return 1 if the conn is timed out, 0 else */
45 usocklnd_conn_timed_out(usock_conn_t *conn, cfs_time_t current_time)
47 if (conn->uc_tx_flag && /* sending is in progress */
48 cfs_time_aftereq(current_time, conn->uc_tx_deadline))
51 if (conn->uc_rx_flag && /* receiving is in progress */
52 cfs_time_aftereq(current_time, conn->uc_rx_deadline))
59 usocklnd_conn_kill(usock_conn_t *conn)
61 pthread_mutex_lock(&conn->uc_lock);
62 if (conn->uc_state != UC_DEAD)
63 usocklnd_conn_kill_locked(conn);
64 pthread_mutex_unlock(&conn->uc_lock);
67 /* Mark the conn as DEAD and schedule its deletion */
69 usocklnd_conn_kill_locked(usock_conn_t *conn)
71 conn->uc_rx_flag = conn->uc_tx_flag = 0;
72 conn->uc_state = UC_DEAD;
73 usocklnd_add_killrequest(conn);
77 usocklnd_conn_allocate()
80 usock_pollrequest_t *pr;
82 LIBCFS_ALLOC (pr, sizeof(*pr));
86 LIBCFS_ALLOC (conn, sizeof(*conn));
88 LIBCFS_FREE (pr, sizeof(*pr));
91 memset(conn, 0, sizeof(*conn));
94 LIBCFS_ALLOC (conn->uc_rx_hello,
95 offsetof(ksock_hello_msg_t,
96 kshm_ips[LNET_MAX_INTERFACES]));
97 if (conn->uc_rx_hello == NULL) {
98 LIBCFS_FREE (pr, sizeof(*pr));
99 LIBCFS_FREE (conn, sizeof(*conn));
107 usocklnd_conn_free(usock_conn_t *conn)
109 usock_pollrequest_t *pr = conn->uc_preq;
112 LIBCFS_FREE (pr, sizeof(*pr));
114 if (conn->uc_rx_hello != NULL)
115 LIBCFS_FREE (conn->uc_rx_hello,
116 offsetof(ksock_hello_msg_t,
117 kshm_ips[LNET_MAX_INTERFACES]));
119 LIBCFS_FREE (conn, sizeof(*conn));
123 usocklnd_tear_peer_conn(usock_conn_t *conn)
125 usock_peer_t *peer = conn->uc_peer;
126 int idx = usocklnd_type2idx(conn->uc_type);
128 lnet_process_id_t id;
130 int killall_flag = 0;
132 if (peer == NULL) /* nothing to tear */
135 pthread_mutex_lock(&peer->up_lock);
136 pthread_mutex_lock(&conn->uc_lock);
139 id = peer->up_peerid;
141 if (peer->up_conns[idx] == conn) {
142 if (conn->uc_rx_state == UC_RX_LNET_PAYLOAD) {
143 /* change state not to finalize twice */
144 conn->uc_rx_state = UC_RX_KSM_HEADER;
145 lnet_finalize(peer->up_ni, conn->uc_rx_lnetmsg, -EIO);
148 usocklnd_destroy_txlist(peer->up_ni,
151 peer->up_conns[idx] = NULL;
152 conn->uc_peer = NULL;
155 if(conn->uc_errored && !peer->up_errored)
156 peer->up_errored = killall_flag = 1;
159 pthread_mutex_unlock(&conn->uc_lock);
162 usocklnd_del_conns_locked(peer);
164 pthread_mutex_unlock(&peer->up_lock);
169 usocklnd_conn_decref(conn);
170 usocklnd_peer_decref(peer);
172 usocklnd_check_peer_stale(ni, id);
175 /* Remove peer from hash list if all up_conns[i] is NULL &&
176 * hash table is the only consumer of the peer */
178 usocklnd_check_peer_stale(lnet_ni_t *ni, lnet_process_id_t id)
182 pthread_rwlock_wrlock(&usock_data.ud_peers_lock);
183 peer = usocklnd_find_peer_locked(ni, id);
186 pthread_rwlock_unlock(&usock_data.ud_peers_lock);
190 if (cfs_atomic_read(&peer->up_refcount) == 2) {
192 for (i = 0; i < N_CONN_TYPES; i++)
193 LASSERT (peer->up_conns[i] == NULL);
195 list_del(&peer->up_list);
197 if (peer->up_errored &&
198 (peer->up_peerid.pid & LNET_PID_USERFLAG) == 0)
199 lnet_notify (peer->up_ni, peer->up_peerid.nid, 0,
200 cfs_time_seconds(peer->up_last_alive));
202 usocklnd_peer_decref(peer);
205 usocklnd_peer_decref(peer);
206 pthread_rwlock_unlock(&usock_data.ud_peers_lock);
209 /* Returns 0 on success, <0 else */
211 usocklnd_create_passive_conn(lnet_ni_t *ni,
212 cfs_socket_t *sock, usock_conn_t **connp)
219 rc = libcfs_sock_getaddr(sock, 1, &peer_ip, &peer_port);
223 LASSERT (peer_port >= 0); /* uc_peer_port is u16 */
225 rc = usocklnd_set_sock_options(sock);
229 conn = usocklnd_conn_allocate();
233 usocklnd_rx_hellomagic_state_transition(conn);
235 conn->uc_sock = sock;
236 conn->uc_peer_ip = peer_ip;
237 conn->uc_peer_port = peer_port;
238 conn->uc_state = UC_RECEIVING_HELLO;
239 conn->uc_pt_idx = usocklnd_ip2pt_idx(peer_ip);
241 CFS_INIT_LIST_HEAD (&conn->uc_tx_list);
242 CFS_INIT_LIST_HEAD (&conn->uc_zcack_list);
243 pthread_mutex_init(&conn->uc_lock, NULL);
244 cfs_atomic_set(&conn->uc_refcount, 1); /* 1 ref for me */
250 /* Returns 0 on success, <0 else */
252 usocklnd_create_active_conn(usock_peer_t *peer, int type,
253 usock_conn_t **connp)
258 __u32 dst_ip = LNET_NIDADDR(peer->up_peerid.nid);
259 __u16 dst_port = lnet_acceptor_port();
261 conn = usocklnd_conn_allocate();
265 conn->uc_tx_hello = usocklnd_create_cr_hello_tx(peer->up_ni, type,
266 peer->up_peerid.nid);
267 if (conn->uc_tx_hello == NULL) {
268 usocklnd_conn_free(conn);
272 if (the_lnet.ln_pid & LNET_PID_USERFLAG)
273 rc = usocklnd_connect_cli_mode(&sock, dst_ip, dst_port);
275 rc = usocklnd_connect_srv_mode(&sock, dst_ip, dst_port);
278 usocklnd_destroy_tx(NULL, conn->uc_tx_hello);
279 usocklnd_conn_free(conn);
283 conn->uc_tx_deadline = cfs_time_shift(usock_tuns.ut_timeout);
284 conn->uc_tx_flag = 1;
286 conn->uc_sock = sock;
287 conn->uc_peer_ip = dst_ip;
288 conn->uc_peer_port = dst_port;
289 conn->uc_type = type;
290 conn->uc_activeflag = 1;
291 conn->uc_state = UC_CONNECTING;
292 conn->uc_pt_idx = usocklnd_ip2pt_idx(dst_ip);
294 conn->uc_peerid = peer->up_peerid;
295 conn->uc_peer = peer;
297 usocklnd_peer_addref(peer);
298 CFS_INIT_LIST_HEAD (&conn->uc_tx_list);
299 CFS_INIT_LIST_HEAD (&conn->uc_zcack_list);
300 pthread_mutex_init(&conn->uc_lock, NULL);
301 cfs_atomic_set(&conn->uc_refcount, 1); /* 1 ref for me */
307 /* Returns 0 on success, <0 else */
309 usocklnd_connect_srv_mode(cfs_socket_t **sockp, __u32 dst_ip, __u16 dst_port)
316 for (port = LNET_ACCEPTOR_MAX_RESERVED_PORT;
317 port >= LNET_ACCEPTOR_MIN_RESERVED_PORT;
319 /* Iterate through reserved ports. */
320 rc = libcfs_sock_create(&sock, &fatal, 0, port);
327 rc = usocklnd_set_sock_options(sock);
329 libcfs_sock_release(sock);
333 rc = libcfs_sock_connect(sock, dst_ip, dst_port);
339 if (rc != -EADDRINUSE && rc != -EADDRNOTAVAIL) {
340 libcfs_sock_release(sock);
344 libcfs_sock_release(sock);
347 CERROR("Can't bind to any reserved port\n");
351 /* Returns 0 on success, <0 else */
353 usocklnd_connect_cli_mode(cfs_socket_t **sockp, __u32 dst_ip, __u16 dst_port)
359 rc = libcfs_sock_create(&sock, &fatal, 0, 0);
363 rc = usocklnd_set_sock_options(sock);
365 libcfs_sock_release(sock);
369 rc = libcfs_sock_connect(sock, dst_ip, dst_port);
371 libcfs_sock_release(sock);
380 usocklnd_set_sock_options(cfs_socket_t *sock)
384 rc = libcfs_sock_set_nagle(sock, usock_tuns.ut_socknagle);
388 if (usock_tuns.ut_sockbufsiz) {
389 rc = libcfs_sock_set_bufsiz(sock, usock_tuns.ut_sockbufsiz);
394 return libcfs_fcntl_nonblock(sock);
398 usocklnd_create_noop_tx(__u64 cookie)
402 LIBCFS_ALLOC (tx, sizeof(usock_tx_t));
406 tx->tx_size = sizeof(usock_tx_t);
407 tx->tx_lnetmsg = NULL;
409 socklnd_init_msg(&tx->tx_msg, KSOCK_MSG_NOOP);
410 tx->tx_msg.ksm_zc_cookies[1] = cookie;
412 tx->tx_iova[0].iov_base = (void *)&tx->tx_msg;
413 tx->tx_iova[0].iov_len = tx->tx_resid = tx->tx_nob =
414 offsetof(ksock_msg_t, ksm_u.lnetmsg.ksnm_hdr);
415 tx->tx_iov = tx->tx_iova;
422 usocklnd_create_tx(lnet_msg_t *lntmsg)
425 unsigned int payload_niov = lntmsg->msg_niov;
426 struct iovec *payload_iov = lntmsg->msg_iov;
427 unsigned int payload_offset = lntmsg->msg_offset;
428 unsigned int payload_nob = lntmsg->msg_len;
429 int size = offsetof(usock_tx_t,
430 tx_iova[1 + payload_niov]);
432 LIBCFS_ALLOC (tx, size);
437 tx->tx_lnetmsg = lntmsg;
439 tx->tx_resid = tx->tx_nob = sizeof(ksock_msg_t) + payload_nob;
441 socklnd_init_msg(&tx->tx_msg, KSOCK_MSG_LNET);
442 tx->tx_msg.ksm_u.lnetmsg.ksnm_hdr = lntmsg->msg_hdr;
443 tx->tx_iova[0].iov_base = (void *)&tx->tx_msg;
444 tx->tx_iova[0].iov_len = sizeof(ksock_msg_t);
445 tx->tx_iov = tx->tx_iova;
448 lnet_extract_iov(payload_niov, &tx->tx_iov[1],
449 payload_niov, payload_iov,
450 payload_offset, payload_nob);
456 usocklnd_init_hello_msg(ksock_hello_msg_t *hello,
457 lnet_ni_t *ni, int type, lnet_nid_t peer_nid)
459 usock_net_t *net = (usock_net_t *)ni->ni_data;
461 hello->kshm_magic = LNET_PROTO_MAGIC;
462 hello->kshm_version = KSOCK_PROTO_V2;
463 hello->kshm_nips = 0;
464 hello->kshm_ctype = type;
466 hello->kshm_dst_incarnation = 0; /* not used */
467 hello->kshm_src_incarnation = net->un_incarnation;
469 hello->kshm_src_pid = the_lnet.ln_pid;
470 hello->kshm_src_nid = ni->ni_nid;
471 hello->kshm_dst_nid = peer_nid;
472 hello->kshm_dst_pid = 0; /* not used */
476 usocklnd_create_hello_tx(lnet_ni_t *ni,
477 int type, lnet_nid_t peer_nid)
481 ksock_hello_msg_t *hello;
483 size = sizeof(usock_tx_t) + offsetof(ksock_hello_msg_t, kshm_ips);
484 LIBCFS_ALLOC (tx, size);
489 tx->tx_lnetmsg = NULL;
491 hello = (ksock_hello_msg_t *)&tx->tx_iova[1];
492 usocklnd_init_hello_msg(hello, ni, type, peer_nid);
494 tx->tx_iova[0].iov_base = (void *)hello;
495 tx->tx_iova[0].iov_len = tx->tx_resid = tx->tx_nob =
496 offsetof(ksock_hello_msg_t, kshm_ips);
497 tx->tx_iov = tx->tx_iova;
504 usocklnd_create_cr_hello_tx(lnet_ni_t *ni,
505 int type, lnet_nid_t peer_nid)
509 lnet_acceptor_connreq_t *cr;
510 ksock_hello_msg_t *hello;
512 size = sizeof(usock_tx_t) +
513 sizeof(lnet_acceptor_connreq_t) +
514 offsetof(ksock_hello_msg_t, kshm_ips);
515 LIBCFS_ALLOC (tx, size);
520 tx->tx_lnetmsg = NULL;
522 cr = (lnet_acceptor_connreq_t *)&tx->tx_iova[1];
523 memset(cr, 0, sizeof(*cr));
524 cr->acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
525 cr->acr_version = LNET_PROTO_ACCEPTOR_VERSION;
526 cr->acr_nid = peer_nid;
528 hello = (ksock_hello_msg_t *)((char *)cr + sizeof(*cr));
529 usocklnd_init_hello_msg(hello, ni, type, peer_nid);
531 tx->tx_iova[0].iov_base = (void *)cr;
532 tx->tx_iova[0].iov_len = tx->tx_resid = tx->tx_nob =
533 sizeof(lnet_acceptor_connreq_t) +
534 offsetof(ksock_hello_msg_t, kshm_ips);
535 tx->tx_iov = tx->tx_iova;
542 usocklnd_destroy_tx(lnet_ni_t *ni, usock_tx_t *tx)
544 lnet_msg_t *lnetmsg = tx->tx_lnetmsg;
545 int rc = (tx->tx_resid == 0) ? 0 : -EIO;
547 LASSERT (ni != NULL || lnetmsg == NULL);
549 LIBCFS_FREE (tx, tx->tx_size);
551 if (lnetmsg != NULL) /* NOOP and hello go without lnetmsg */
552 lnet_finalize(ni, lnetmsg, rc);
556 usocklnd_destroy_txlist(lnet_ni_t *ni, struct list_head *txlist)
560 while (!list_empty(txlist)) {
561 tx = list_entry(txlist->next, usock_tx_t, tx_list);
562 list_del(&tx->tx_list);
564 usocklnd_destroy_tx(ni, tx);
569 usocklnd_destroy_zcack_list(struct list_head *zcack_list)
571 usock_zc_ack_t *zcack;
573 while (!list_empty(zcack_list)) {
574 zcack = list_entry(zcack_list->next, usock_zc_ack_t, zc_list);
575 list_del(&zcack->zc_list);
577 LIBCFS_FREE (zcack, sizeof(*zcack));
582 usocklnd_destroy_peer(usock_peer_t *peer)
584 usock_net_t *net = peer->up_ni->ni_data;
587 for (i = 0; i < N_CONN_TYPES; i++)
588 LASSERT (peer->up_conns[i] == NULL);
590 LIBCFS_FREE (peer, sizeof (*peer));
592 pthread_mutex_lock(&net->un_lock);
593 if(--net->un_peercount == 0)
594 pthread_cond_signal(&net->un_cond);
595 pthread_mutex_unlock(&net->un_lock);
599 usocklnd_destroy_conn(usock_conn_t *conn)
601 LASSERT (conn->uc_peer == NULL || conn->uc_ni == NULL);
603 if (conn->uc_rx_state == UC_RX_LNET_PAYLOAD) {
604 LASSERT (conn->uc_peer != NULL);
605 lnet_finalize(conn->uc_peer->up_ni, conn->uc_rx_lnetmsg, -EIO);
608 if (!list_empty(&conn->uc_tx_list)) {
609 LASSERT (conn->uc_peer != NULL);
610 usocklnd_destroy_txlist(conn->uc_peer->up_ni, &conn->uc_tx_list);
613 usocklnd_destroy_zcack_list(&conn->uc_zcack_list);
615 if (conn->uc_peer != NULL)
616 usocklnd_peer_decref(conn->uc_peer);
618 if (conn->uc_ni != NULL)
619 lnet_ni_decref(conn->uc_ni);
621 if (conn->uc_tx_hello)
622 usocklnd_destroy_tx(NULL, conn->uc_tx_hello);
624 usocklnd_conn_free(conn);
628 usocklnd_get_conn_type(lnet_msg_t *lntmsg)
632 if (the_lnet.ln_pid & LNET_PID_USERFLAG)
633 return SOCKLND_CONN_ANY;
635 nob = sizeof(ksock_msg_t) + lntmsg->msg_len;
637 if (nob >= usock_tuns.ut_min_bulk)
638 return SOCKLND_CONN_BULK_OUT;
640 return SOCKLND_CONN_CONTROL;
643 int usocklnd_type2idx(int type)
646 case SOCKLND_CONN_ANY:
647 case SOCKLND_CONN_CONTROL:
649 case SOCKLND_CONN_BULK_IN:
651 case SOCKLND_CONN_BULK_OUT:
659 usocklnd_find_peer_locked(lnet_ni_t *ni, lnet_process_id_t id)
661 struct list_head *peer_list = usocklnd_nid2peerlist(id.nid);
662 struct list_head *tmp;
665 list_for_each (tmp, peer_list) {
667 peer = list_entry (tmp, usock_peer_t, up_list);
669 if (peer->up_ni != ni)
672 if (peer->up_peerid.nid != id.nid ||
673 peer->up_peerid.pid != id.pid)
676 usocklnd_peer_addref(peer);
683 usocklnd_create_peer(lnet_ni_t *ni, lnet_process_id_t id,
684 usock_peer_t **peerp)
686 usock_net_t *net = ni->ni_data;
690 LIBCFS_ALLOC (peer, sizeof (*peer));
694 for (i = 0; i < N_CONN_TYPES; i++)
695 peer->up_conns[i] = NULL;
697 peer->up_peerid = id;
699 peer->up_incrn_is_set = 0;
700 peer->up_errored = 0;
701 peer->up_last_alive = 0;
702 cfs_atomic_set (&peer->up_refcount, 1); /* 1 ref for caller */
703 pthread_mutex_init(&peer->up_lock, NULL);
705 pthread_mutex_lock(&net->un_lock);
707 pthread_mutex_unlock(&net->un_lock);
713 /* Safely create new peer if needed. Save result in *peerp.
714 * Returns 0 on success, <0 else */
716 usocklnd_find_or_create_peer(lnet_ni_t *ni, lnet_process_id_t id,
717 usock_peer_t **peerp)
722 usock_net_t *net = ni->ni_data;
724 pthread_rwlock_rdlock(&usock_data.ud_peers_lock);
725 peer = usocklnd_find_peer_locked(ni, id);
726 pthread_rwlock_unlock(&usock_data.ud_peers_lock);
729 goto find_or_create_peer_done;
731 rc = usocklnd_create_peer(ni, id, &peer);
735 pthread_rwlock_wrlock(&usock_data.ud_peers_lock);
736 peer2 = usocklnd_find_peer_locked(ni, id);
738 if (net->un_shutdown) {
739 pthread_rwlock_unlock(&usock_data.ud_peers_lock);
740 usocklnd_peer_decref(peer); /* should destroy peer */
741 CERROR("Can't create peer: network shutdown\n");
745 /* peer table will take 1 of my refs on peer */
746 usocklnd_peer_addref(peer);
747 list_add_tail (&peer->up_list,
748 usocklnd_nid2peerlist(id.nid));
750 usocklnd_peer_decref(peer); /* should destroy peer */
753 pthread_rwlock_unlock(&usock_data.ud_peers_lock);
755 find_or_create_peer_done:
760 /* NB: both peer and conn locks are held */
762 usocklnd_enqueue_zcack(usock_conn_t *conn, usock_zc_ack_t *zc_ack)
764 if (conn->uc_state == UC_READY &&
765 list_empty(&conn->uc_tx_list) &&
766 list_empty(&conn->uc_zcack_list) &&
768 int rc = usocklnd_add_pollrequest(conn, POLL_TX_SET_REQUEST,
774 list_add_tail(&zc_ack->zc_list, &conn->uc_zcack_list);
778 /* NB: both peer and conn locks are held
779 * NB: if sending isn't in progress. the caller *MUST* send tx
780 * immediately after we'll return */
782 usocklnd_enqueue_tx(usock_conn_t *conn, usock_tx_t *tx,
783 int *send_immediately)
785 if (conn->uc_state == UC_READY &&
786 list_empty(&conn->uc_tx_list) &&
787 list_empty(&conn->uc_zcack_list) &&
789 conn->uc_sending = 1;
790 *send_immediately = 1;
794 *send_immediately = 0;
795 list_add_tail(&tx->tx_list, &conn->uc_tx_list);
798 /* Safely create new conn if needed. Save result in *connp.
799 * Returns 0 on success, <0 else */
801 usocklnd_find_or_create_conn(usock_peer_t *peer, int type,
802 usock_conn_t **connp,
803 usock_tx_t *tx, usock_zc_ack_t *zc_ack,
804 int *send_immediately)
809 lnet_pid_t userflag = peer->up_peerid.pid & LNET_PID_USERFLAG;
812 type = SOCKLND_CONN_ANY;
814 idx = usocklnd_type2idx(type);
816 pthread_mutex_lock(&peer->up_lock);
817 if (peer->up_conns[idx] != NULL) {
818 conn = peer->up_conns[idx];
819 LASSERT(conn->uc_type == type);
822 CERROR("Refusing to create a connection to "
823 "userspace process %s\n",
824 libcfs_id2str(peer->up_peerid));
826 goto find_or_create_conn_failed;
829 rc = usocklnd_create_active_conn(peer, type, &conn);
831 peer->up_errored = 1;
832 usocklnd_del_conns_locked(peer);
833 goto find_or_create_conn_failed;
836 /* peer takes 1 of conn refcount */
837 usocklnd_link_conn_to_peer(conn, peer, idx);
839 rc = usocklnd_add_pollrequest(conn, POLL_ADD_REQUEST, POLLOUT);
841 peer->up_conns[idx] = NULL;
842 usocklnd_conn_decref(conn); /* should destroy conn */
843 goto find_or_create_conn_failed;
845 usocklnd_wakeup_pollthread(conn->uc_pt_idx);
848 pthread_mutex_lock(&conn->uc_lock);
849 LASSERT(conn->uc_peer == peer);
851 LASSERT(tx == NULL || zc_ack == NULL);
853 usocklnd_enqueue_tx(conn, tx, send_immediately);
855 rc = usocklnd_enqueue_zcack(conn, zc_ack);
857 usocklnd_conn_kill_locked(conn);
858 pthread_mutex_unlock(&conn->uc_lock);
859 goto find_or_create_conn_failed;
862 pthread_mutex_unlock(&conn->uc_lock);
864 usocklnd_conn_addref(conn);
865 pthread_mutex_unlock(&peer->up_lock);
870 find_or_create_conn_failed:
871 pthread_mutex_unlock(&peer->up_lock);
876 usocklnd_link_conn_to_peer(usock_conn_t *conn, usock_peer_t *peer, int idx)
878 peer->up_conns[idx] = conn;
879 peer->up_errored = 0; /* this new fresh conn will try
880 * revitalize even stale errored peer */
884 usocklnd_invert_type(int type)
888 case SOCKLND_CONN_ANY:
889 case SOCKLND_CONN_CONTROL:
891 case SOCKLND_CONN_BULK_IN:
892 return SOCKLND_CONN_BULK_OUT;
893 case SOCKLND_CONN_BULK_OUT:
894 return SOCKLND_CONN_BULK_IN;
896 return SOCKLND_CONN_NONE;
901 usocklnd_conn_new_state(usock_conn_t *conn, int new_state)
903 pthread_mutex_lock(&conn->uc_lock);
904 if (conn->uc_state != UC_DEAD)
905 conn->uc_state = new_state;
906 pthread_mutex_unlock(&conn->uc_lock);
909 /* NB: peer is locked by caller */
911 usocklnd_cleanup_stale_conns(usock_peer_t *peer, __u64 incrn,
912 usock_conn_t *skip_conn)
916 if (!peer->up_incrn_is_set) {
917 peer->up_incarnation = incrn;
918 peer->up_incrn_is_set = 1;
922 if (peer->up_incarnation == incrn)
925 peer->up_incarnation = incrn;
927 for (i = 0; i < N_CONN_TYPES; i++) {
928 usock_conn_t *conn = peer->up_conns[i];
930 if (conn == NULL || conn == skip_conn)
933 pthread_mutex_lock(&conn->uc_lock);
934 LASSERT (conn->uc_peer == peer);
935 conn->uc_peer = NULL;
936 peer->up_conns[i] = NULL;
937 if (conn->uc_state != UC_DEAD)
938 usocklnd_conn_kill_locked(conn);
939 pthread_mutex_unlock(&conn->uc_lock);
941 usocklnd_conn_decref(conn);
942 usocklnd_peer_decref(peer);
946 /* RX state transition to UC_RX_HELLO_MAGIC: update RX part to receive
947 * MAGIC part of hello and set uc_rx_state
950 usocklnd_rx_hellomagic_state_transition(usock_conn_t *conn)
952 LASSERT(conn->uc_rx_hello != NULL);
954 conn->uc_rx_niov = 1;
955 conn->uc_rx_iov = conn->uc_rx_iova;
956 conn->uc_rx_iov[0].iov_base = &conn->uc_rx_hello->kshm_magic;
957 conn->uc_rx_iov[0].iov_len =
958 conn->uc_rx_nob_wanted =
959 conn->uc_rx_nob_left =
960 sizeof(conn->uc_rx_hello->kshm_magic);
962 conn->uc_rx_state = UC_RX_HELLO_MAGIC;
964 conn->uc_rx_flag = 1; /* waiting for incoming hello */
965 conn->uc_rx_deadline = cfs_time_shift(usock_tuns.ut_timeout);
968 /* RX state transition to UC_RX_HELLO_VERSION: update RX part to receive
969 * VERSION part of hello and set uc_rx_state
972 usocklnd_rx_helloversion_state_transition(usock_conn_t *conn)
974 LASSERT(conn->uc_rx_hello != NULL);
976 conn->uc_rx_niov = 1;
977 conn->uc_rx_iov = conn->uc_rx_iova;
978 conn->uc_rx_iov[0].iov_base = &conn->uc_rx_hello->kshm_version;
979 conn->uc_rx_iov[0].iov_len =
980 conn->uc_rx_nob_wanted =
981 conn->uc_rx_nob_left =
982 sizeof(conn->uc_rx_hello->kshm_version);
984 conn->uc_rx_state = UC_RX_HELLO_VERSION;
987 /* RX state transition to UC_RX_HELLO_BODY: update RX part to receive
988 * the rest of hello and set uc_rx_state
991 usocklnd_rx_hellobody_state_transition(usock_conn_t *conn)
993 LASSERT(conn->uc_rx_hello != NULL);
995 conn->uc_rx_niov = 1;
996 conn->uc_rx_iov = conn->uc_rx_iova;
997 conn->uc_rx_iov[0].iov_base = &conn->uc_rx_hello->kshm_src_nid;
998 conn->uc_rx_iov[0].iov_len =
999 conn->uc_rx_nob_wanted =
1000 conn->uc_rx_nob_left =
1001 offsetof(ksock_hello_msg_t, kshm_ips) -
1002 offsetof(ksock_hello_msg_t, kshm_src_nid);
1004 conn->uc_rx_state = UC_RX_HELLO_BODY;
1007 /* RX state transition to UC_RX_HELLO_IPS: update RX part to receive
1008 * array of IPs and set uc_rx_state
1011 usocklnd_rx_helloIPs_state_transition(usock_conn_t *conn)
1013 LASSERT(conn->uc_rx_hello != NULL);
1015 conn->uc_rx_niov = 1;
1016 conn->uc_rx_iov = conn->uc_rx_iova;
1017 conn->uc_rx_iov[0].iov_base = &conn->uc_rx_hello->kshm_ips;
1018 conn->uc_rx_iov[0].iov_len =
1019 conn->uc_rx_nob_wanted =
1020 conn->uc_rx_nob_left =
1021 conn->uc_rx_hello->kshm_nips *
1022 sizeof(conn->uc_rx_hello->kshm_ips[0]);
1024 conn->uc_rx_state = UC_RX_HELLO_IPS;
1027 /* RX state transition to UC_RX_LNET_HEADER: update RX part to receive
1028 * LNET header and set uc_rx_state
1031 usocklnd_rx_lnethdr_state_transition(usock_conn_t *conn)
1033 conn->uc_rx_niov = 1;
1034 conn->uc_rx_iov = conn->uc_rx_iova;
1035 conn->uc_rx_iov[0].iov_base = &conn->uc_rx_msg.ksm_u.lnetmsg;
1036 conn->uc_rx_iov[0].iov_len =
1037 conn->uc_rx_nob_wanted =
1038 conn->uc_rx_nob_left =
1039 sizeof(ksock_lnet_msg_t);
1041 conn->uc_rx_state = UC_RX_LNET_HEADER;
1042 conn->uc_rx_flag = 1;
1045 /* RX state transition to UC_RX_KSM_HEADER: update RX part to receive
1046 * KSM header and set uc_rx_state
1049 usocklnd_rx_ksmhdr_state_transition(usock_conn_t *conn)
1051 conn->uc_rx_niov = 1;
1052 conn->uc_rx_iov = conn->uc_rx_iova;
1053 conn->uc_rx_iov[0].iov_base = &conn->uc_rx_msg;
1054 conn->uc_rx_iov[0].iov_len =
1055 conn->uc_rx_nob_wanted =
1056 conn->uc_rx_nob_left =
1057 offsetof(ksock_msg_t, ksm_u);
1059 conn->uc_rx_state = UC_RX_KSM_HEADER;
1060 conn->uc_rx_flag = 0;
1063 /* RX state transition to UC_RX_SKIPPING: update RX part for
1064 * skipping and set uc_rx_state
1067 usocklnd_rx_skipping_state_transition(usock_conn_t *conn)
1069 static char skip_buffer[4096];
1072 unsigned int niov = 0;
1074 int nob_to_skip = conn->uc_rx_nob_left;
1076 LASSERT(nob_to_skip != 0);
1078 conn->uc_rx_iov = conn->uc_rx_iova;
1080 /* Set up to skip as much as possible now. If there's more left
1081 * (ran out of iov entries) we'll get called again */
1084 nob = MIN (nob_to_skip, sizeof(skip_buffer));
1086 conn->uc_rx_iov[niov].iov_base = skip_buffer;
1087 conn->uc_rx_iov[niov].iov_len = nob;
1092 } while (nob_to_skip != 0 && /* mustn't overflow conn's rx iov */
1093 niov < sizeof(conn->uc_rx_iova) / sizeof (struct iovec));
1095 conn->uc_rx_niov = niov;
1096 conn->uc_rx_nob_wanted = skipped;
1098 conn->uc_rx_state = UC_RX_SKIPPING;