2 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
4 * Copyright (c) 2012, 2017, Intel Corporation.
6 * Author: Zach Brown <zab@zabbo.net>
7 * Author: Peter J. Braam <braam@clusterfs.com>
8 * Author: Phil Schwan <phil@clusterfs.com>
9 * Author: Eric Barton <eric@bartonsoftware.com>
11 * This file is part of Lustre, https://wiki.whamcloud.com/
13 * Portals is free software; you can redistribute it and/or
14 * modify it under the terms of version 2 of the GNU General Public
15 * License as published by the Free Software Foundation.
17 * Portals is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with Portals; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 * pro_send_hello : send hello message
32 * pro_recv_hello : receive hello message
33 * pro_pack : pack message header
34 * pro_unpack : unpack message header
35 * pro_queue_tx_zcack() : Called holding BH lock: kss_lock
36 * return 1 if ACK is piggybacked, otherwise return 0
37 * pro_queue_tx_msg() : Called holding BH lock: kss_lock
38 * return the ACK that piggybacked by my message, or NULL
39 * pro_handle_zcreq() : handler of incoming ZC-REQ
40 * pro_handle_zcack() : handler of incoming ZC-ACK
41 * pro_match_tx() : Called holding glock
44 static struct ksock_tx *
45 ksocknal_queue_tx_msg_v1(struct ksock_conn *conn, struct ksock_tx *tx_msg)
47 /* V1.x, just enqueue it */
48 list_add_tail(&tx_msg->tx_list, &conn->ksnc_tx_queue);
53 ksocknal_next_tx_carrier(struct ksock_conn *conn)
55 struct ksock_tx *tx = conn->ksnc_tx_carrier;
57 /* Called holding BH lock: conn->ksnc_scheduler->kss_lock */
58 LASSERT(!list_empty(&conn->ksnc_tx_queue));
61 /* Next TX that can carry ZC-ACK or LNet message */
62 if (tx->tx_list.next == &conn->ksnc_tx_queue) {
63 /* no more packets queued */
64 conn->ksnc_tx_carrier = NULL;
66 conn->ksnc_tx_carrier = list_next_entry(tx, tx_list);
67 LASSERT(conn->ksnc_tx_carrier->tx_msg.ksm_type ==
73 ksocknal_queue_tx_zcack_v2(struct ksock_conn *conn,
74 struct ksock_tx *tx_ack, __u64 cookie)
76 struct ksock_tx *tx = conn->ksnc_tx_carrier;
78 LASSERT (tx_ack == NULL ||
79 tx_ack->tx_msg.ksm_type == KSOCK_MSG_NOOP);
82 * Enqueue or piggyback tx_ack / cookie
83 * . no tx can piggyback cookie of tx_ack (or cookie), just
84 * enqueue the tx_ack (if tx_ack != NUL) and return NULL.
85 * . There is tx can piggyback cookie of tx_ack (or cookie),
86 * piggyback the cookie and return the tx.
90 list_add_tail(&tx_ack->tx_list,
91 &conn->ksnc_tx_queue);
92 conn->ksnc_tx_carrier = tx_ack;
97 if (tx->tx_msg.ksm_type == KSOCK_MSG_NOOP) {
98 /* tx is noop zc-ack, can't piggyback zc-ack cookie */
100 list_add_tail(&tx_ack->tx_list,
101 &conn->ksnc_tx_queue);
105 LASSERT(tx->tx_msg.ksm_type == KSOCK_MSG_LNET);
106 LASSERT(tx->tx_msg.ksm_zc_cookies[1] == 0);
109 cookie = tx_ack->tx_msg.ksm_zc_cookies[1];
111 /* piggyback the zc-ack cookie */
112 tx->tx_msg.ksm_zc_cookies[1] = cookie;
113 /* move on to the next TX which can carry cookie */
114 ksocknal_next_tx_carrier(conn);
119 static struct ksock_tx *
120 ksocknal_queue_tx_msg_v2(struct ksock_conn *conn, struct ksock_tx *tx_msg)
122 struct ksock_tx *tx = conn->ksnc_tx_carrier;
126 * . If there is no NOOP on the connection, just enqueue
127 * tx_msg and return NULL
128 * . If there is NOOP on the connection, piggyback the cookie
129 * and replace the NOOP tx, and return the NOOP tx.
131 if (tx == NULL) { /* nothing on queue */
132 list_add_tail(&tx_msg->tx_list, &conn->ksnc_tx_queue);
133 conn->ksnc_tx_carrier = tx_msg;
137 if (tx->tx_msg.ksm_type == KSOCK_MSG_LNET) { /* nothing to carry */
138 list_add_tail(&tx_msg->tx_list, &conn->ksnc_tx_queue);
142 LASSERT (tx->tx_msg.ksm_type == KSOCK_MSG_NOOP);
144 /* There is a noop zc-ack can be piggybacked */
145 tx_msg->tx_msg.ksm_zc_cookies[1] = tx->tx_msg.ksm_zc_cookies[1];
146 ksocknal_next_tx_carrier(conn);
148 /* use new_tx to replace the noop zc-ack packet */
149 list_splice(&tx->tx_list, &tx_msg->tx_list);
155 ksocknal_queue_tx_zcack_v3(struct ksock_conn *conn,
156 struct ksock_tx *tx_ack, __u64 cookie)
160 if (conn->ksnc_type != SOCKLND_CONN_ACK)
161 return ksocknal_queue_tx_zcack_v2(conn, tx_ack, cookie);
163 /* non-blocking ZC-ACK (to router) */
164 LASSERT (tx_ack == NULL ||
165 tx_ack->tx_msg.ksm_type == KSOCK_MSG_NOOP);
167 if ((tx = conn->ksnc_tx_carrier) == NULL) {
168 if (tx_ack != NULL) {
169 list_add_tail(&tx_ack->tx_list,
170 &conn->ksnc_tx_queue);
171 conn->ksnc_tx_carrier = tx_ack;
176 /* conn->ksnc_tx_carrier != NULL */
179 cookie = tx_ack->tx_msg.ksm_zc_cookies[1];
181 if (cookie == SOCKNAL_KEEPALIVE_PING) /* ignore keepalive PING */
184 if (tx->tx_msg.ksm_zc_cookies[1] == SOCKNAL_KEEPALIVE_PING) {
185 /* replace the keepalive PING with a real ACK */
186 LASSERT (tx->tx_msg.ksm_zc_cookies[0] == 0);
187 tx->tx_msg.ksm_zc_cookies[1] = cookie;
191 if (cookie == tx->tx_msg.ksm_zc_cookies[0] ||
192 cookie == tx->tx_msg.ksm_zc_cookies[1]) {
193 CWARN("%s: duplicated ZC cookie: %llu\n",
194 libcfs_idstr(&conn->ksnc_peer->ksnp_id), cookie);
195 return 1; /* XXX return error in the future */
198 if (tx->tx_msg.ksm_zc_cookies[0] == 0) {
199 /* NOOP tx has only one ZC-ACK cookie, can carry at least one more */
200 if (tx->tx_msg.ksm_zc_cookies[1] > cookie) {
201 tx->tx_msg.ksm_zc_cookies[0] = tx->tx_msg.ksm_zc_cookies[1];
202 tx->tx_msg.ksm_zc_cookies[1] = cookie;
204 tx->tx_msg.ksm_zc_cookies[0] = cookie;
207 if (tx->tx_msg.ksm_zc_cookies[0] - tx->tx_msg.ksm_zc_cookies[1] > 2) {
208 /* not likely to carry more ACKs, skip it to simplify logic */
209 ksocknal_next_tx_carrier(conn);
215 /* takes two or more cookies already */
217 if (tx->tx_msg.ksm_zc_cookies[0] > tx->tx_msg.ksm_zc_cookies[1]) {
220 /* two separated cookies: (a+2, a) or (a+1, a) */
221 LASSERT (tx->tx_msg.ksm_zc_cookies[0] -
222 tx->tx_msg.ksm_zc_cookies[1] <= 2);
224 if (tx->tx_msg.ksm_zc_cookies[0] -
225 tx->tx_msg.ksm_zc_cookies[1] == 2) {
226 if (cookie == tx->tx_msg.ksm_zc_cookies[1] + 1)
228 } else if (cookie == tx->tx_msg.ksm_zc_cookies[1] - 1) {
229 tmp = tx->tx_msg.ksm_zc_cookies[1];
230 } else if (cookie == tx->tx_msg.ksm_zc_cookies[0] + 1) {
231 tmp = tx->tx_msg.ksm_zc_cookies[0];
235 /* range of cookies */
236 tx->tx_msg.ksm_zc_cookies[0] = tmp - 1;
237 tx->tx_msg.ksm_zc_cookies[1] = tmp + 1;
242 /* ksm_zc_cookies[0] < ksm_zc_cookies[1], it is a range
245 if (cookie >= tx->tx_msg.ksm_zc_cookies[0] &&
246 cookie <= tx->tx_msg.ksm_zc_cookies[1]) {
247 CWARN("%s: duplicated ZC cookie: %llu\n",
248 libcfs_idstr(&conn->ksnc_peer->ksnp_id),
250 return 1; /* XXX: return error in the future */
253 if (cookie == tx->tx_msg.ksm_zc_cookies[1] + 1) {
254 tx->tx_msg.ksm_zc_cookies[1] = cookie;
258 if (cookie == tx->tx_msg.ksm_zc_cookies[0] - 1) {
259 tx->tx_msg.ksm_zc_cookies[0] = cookie;
264 /* failed to piggyback ZC-ACK */
265 if (tx_ack != NULL) {
266 list_add_tail(&tx_ack->tx_list, &conn->ksnc_tx_queue);
267 /* the next tx can piggyback at least 1 ACK */
268 ksocknal_next_tx_carrier(conn);
275 ksocknal_match_tx(struct ksock_conn *conn, struct ksock_tx *tx, int nonblk)
279 #if SOCKNAL_VERSION_DEBUG
280 if (!*ksocknal_tunables.ksnd_typed_conns)
281 return SOCKNAL_MATCH_YES;
284 if (tx == NULL || tx->tx_lnetmsg == NULL) {
286 nob = sizeof(struct ksock_msg_hdr);
288 nob = tx->tx_lnetmsg->msg_len +
289 ((conn->ksnc_proto == &ksocknal_protocol_v1x) ?
290 0 : sizeof(struct ksock_msg_hdr)) +
291 sizeof(struct lnet_hdr_nid4);
294 /* default checking for typed connection */
295 switch (conn->ksnc_type) {
297 CERROR("ksnc_type bad: %u\n", conn->ksnc_type);
299 case SOCKLND_CONN_ANY:
300 return SOCKNAL_MATCH_YES;
302 case SOCKLND_CONN_BULK_IN:
303 return SOCKNAL_MATCH_MAY;
305 case SOCKLND_CONN_BULK_OUT:
306 if (nob < *ksocknal_tunables.ksnd_min_bulk)
307 return SOCKNAL_MATCH_MAY;
309 return SOCKNAL_MATCH_YES;
311 case SOCKLND_CONN_CONTROL:
312 if (nob >= *ksocknal_tunables.ksnd_min_bulk)
313 return SOCKNAL_MATCH_MAY;
315 return SOCKNAL_MATCH_YES;
320 ksocknal_match_tx_v3(struct ksock_conn *conn, struct ksock_tx *tx, int nonblk)
324 if (tx == NULL || tx->tx_lnetmsg == NULL)
325 nob = sizeof(struct ksock_msg_hdr);
327 nob = sizeof(struct ksock_msg_hdr) +
328 sizeof(struct lnet_hdr_nid4) +
329 tx->tx_lnetmsg->msg_len;
331 switch (conn->ksnc_type) {
333 CERROR("ksnc_type bad: %u\n", conn->ksnc_type);
335 case SOCKLND_CONN_ANY:
336 return SOCKNAL_MATCH_NO;
338 case SOCKLND_CONN_ACK:
340 return SOCKNAL_MATCH_YES;
341 else if (tx == NULL || tx->tx_lnetmsg == NULL)
342 return SOCKNAL_MATCH_MAY;
344 return SOCKNAL_MATCH_NO;
346 case SOCKLND_CONN_BULK_OUT:
348 return SOCKNAL_MATCH_NO;
349 else if (nob < *ksocknal_tunables.ksnd_min_bulk)
350 return SOCKNAL_MATCH_MAY;
352 return SOCKNAL_MATCH_YES;
354 case SOCKLND_CONN_CONTROL:
356 return SOCKNAL_MATCH_NO;
357 else if (nob >= *ksocknal_tunables.ksnd_min_bulk)
358 return SOCKNAL_MATCH_MAY;
360 return SOCKNAL_MATCH_YES;
365 ksocknal_match_tx_v4(struct ksock_conn *conn, struct ksock_tx *tx, int nonblk)
369 if (!tx || !tx->tx_lnetmsg)
370 nob = sizeof(struct ksock_msg_hdr);
372 nob = sizeof(struct ksock_msg_hdr) +
373 sizeof(struct lnet_hdr_nid16) +
374 tx->tx_lnetmsg->msg_len;
376 switch (conn->ksnc_type) {
378 CERROR("ksnc_type bad: %u\n", conn->ksnc_type);
380 case SOCKLND_CONN_ANY:
381 return SOCKNAL_MATCH_NO;
383 case SOCKLND_CONN_ACK:
385 return SOCKNAL_MATCH_YES;
386 else if (tx == NULL || tx->tx_lnetmsg == NULL)
387 return SOCKNAL_MATCH_MAY;
389 return SOCKNAL_MATCH_NO;
391 case SOCKLND_CONN_BULK_OUT:
393 return SOCKNAL_MATCH_NO;
394 else if (nob < *ksocknal_tunables.ksnd_min_bulk)
395 return SOCKNAL_MATCH_MAY;
397 return SOCKNAL_MATCH_YES;
399 case SOCKLND_CONN_CONTROL:
401 return SOCKNAL_MATCH_NO;
402 else if (nob >= *ksocknal_tunables.ksnd_min_bulk)
403 return SOCKNAL_MATCH_MAY;
405 return SOCKNAL_MATCH_YES;
409 /* (Sink) handle incoming ZC request from sender */
411 ksocknal_handle_zcreq(struct ksock_conn *c, __u64 cookie, int remote)
413 struct ksock_peer_ni *peer_ni = c->ksnc_peer;
414 struct ksock_conn *conn;
418 read_lock(&ksocknal_data.ksnd_global_lock);
420 conn = ksocknal_find_conn_locked(peer_ni, NULL, !!remote);
422 struct ksock_sched *sched = conn->ksnc_scheduler;
424 LASSERT(conn->ksnc_proto->pro_queue_tx_zcack != NULL);
426 spin_lock_bh(&sched->kss_lock);
428 rc = conn->ksnc_proto->pro_queue_tx_zcack(conn, NULL, cookie);
430 spin_unlock_bh(&sched->kss_lock);
432 if (rc) { /* piggybacked */
433 read_unlock(&ksocknal_data.ksnd_global_lock);
438 read_unlock(&ksocknal_data.ksnd_global_lock);
440 /* ACK connection is not ready, or can't piggyback the ACK */
441 tx = ksocknal_alloc_tx_noop(cookie, !!remote);
445 rc = ksocknal_launch_packet(peer_ni->ksnp_ni, tx, &peer_ni->ksnp_id);
449 ksocknal_free_tx(tx);
453 /* (Sender) handle ZC_ACK from sink */
455 ksocknal_handle_zcack(struct ksock_conn *conn, __u64 cookie1, __u64 cookie2)
457 struct ksock_peer_ni *peer_ni = conn->ksnc_peer;
459 struct ksock_tx *tmp;
466 count = (cookie1 > cookie2) ? 2 : (cookie2 - cookie1 + 1);
468 if (cookie2 == SOCKNAL_KEEPALIVE_PING &&
469 (conn->ksnc_proto == &ksocknal_protocol_v3x ||
470 conn->ksnc_proto == &ksocknal_protocol_v4x)) {
471 /* keepalive PING for V3.x, just ignore it */
472 return count == 1 ? 0 : -EPROTO;
475 spin_lock(&peer_ni->ksnp_lock);
477 list_for_each_entry_safe(tx, tmp, &peer_ni->ksnp_zc_req_list,
479 __u64 c = tx->tx_msg.ksm_zc_cookies[0];
481 if (c == cookie1 || c == cookie2 || (cookie1 < c && c < cookie2)) {
482 tx->tx_msg.ksm_zc_cookies[0] = 0;
483 list_move(&tx->tx_zc_list, &zlist);
490 spin_unlock(&peer_ni->ksnp_lock);
492 while ((tx = list_first_entry_or_null(&zlist, struct ksock_tx,
493 tx_zc_list)) != NULL) {
494 list_del(&tx->tx_zc_list);
495 ksocknal_tx_decref(tx);
498 return count == 0 ? 0 : -EPROTO;
502 ksocknal_send_hello_v1(struct ksock_conn *conn, struct ksock_hello_msg *hello)
504 struct socket *sock = conn->ksnc_sock;
505 struct _lnet_hdr_nid4 *hdr;
506 struct lnet_magicversion *hmv;
510 BUILD_BUG_ON(sizeof(struct lnet_magicversion) !=
511 offsetof(struct _lnet_hdr_nid4, src_nid));
513 LIBCFS_ALLOC(hdr, sizeof(*hdr));
515 CERROR("Can't allocate struct lnet_hdr_nid4\n");
519 hmv = (struct lnet_magicversion *)&hdr->dest_nid;
521 /* Re-organize V2.x message header to V1.x (struct lnet_hdr_nid4)
522 * header and send out
524 hmv->magic = cpu_to_le32 (LNET_PROTO_TCP_MAGIC);
525 hmv->version_major = cpu_to_le16 (KSOCK_PROTO_V1_MAJOR);
526 hmv->version_minor = cpu_to_le16 (KSOCK_PROTO_V1_MINOR);
528 if (the_lnet.ln_testprotocompat) {
529 /* single-shot proto check */
530 if (test_and_clear_bit(0, &the_lnet.ln_testprotocompat))
531 hmv->version_major++; /* just different! */
533 if (test_and_clear_bit(1, &the_lnet.ln_testprotocompat))
534 hmv->magic = LNET_PROTO_MAGIC;
537 hdr->src_nid = cpu_to_le64(lnet_nid_to_nid4(&hello->kshm_src_nid));
538 hdr->src_pid = cpu_to_le32 (hello->kshm_src_pid);
539 hdr->type = cpu_to_le32 (LNET_MSG_HELLO);
540 hdr->payload_length = cpu_to_le32 (hello->kshm_nips * sizeof(__u32));
541 hdr->msg.hello.type = cpu_to_le32 (hello->kshm_ctype);
542 hdr->msg.hello.incarnation = cpu_to_le64 (hello->kshm_src_incarnation);
544 rc = lnet_sock_write(sock, hdr, sizeof(*hdr), lnet_acceptor_timeout());
546 CNETERR("Error %d sending HELLO hdr to %pISp\n",
547 rc, &conn->ksnc_peeraddr);
551 if (hello->kshm_nips == 0)
554 for (i = 0; i < (int) hello->kshm_nips; i++)
555 hello->kshm_ips[i] = __cpu_to_le32 (hello->kshm_ips[i]);
557 rc = lnet_sock_write(sock, hello->kshm_ips,
558 hello->kshm_nips * sizeof(__u32),
559 lnet_acceptor_timeout());
561 CNETERR("Error %d sending HELLO payload (%d) to %pISp\n",
562 rc, hello->kshm_nips,
563 &conn->ksnc_peeraddr);
566 LIBCFS_FREE(hdr, sizeof(*hdr));
572 ksocknal_send_hello_v2(struct ksock_conn *conn, struct ksock_hello_msg *hello)
574 struct socket *sock = conn->ksnc_sock;
576 struct ksock_hello_msg_nid4 *hello4;
578 CFS_ALLOC_PTR(hello4);
580 CERROR("Can't allocate struct ksock_hello_msg_nid4\n");
584 hello->kshm_magic = LNET_PROTO_MAGIC;
585 hello->kshm_version = conn->ksnc_proto->pro_version;
587 hello4->kshm_magic = LNET_PROTO_MAGIC;
588 hello4->kshm_version = conn->ksnc_proto->pro_version;
589 hello4->kshm_src_nid = lnet_nid_to_nid4(&hello->kshm_src_nid);
590 hello4->kshm_dst_nid = lnet_nid_to_nid4(&hello->kshm_dst_nid);
591 hello4->kshm_src_pid = hello->kshm_src_pid;
592 hello4->kshm_dst_pid = hello->kshm_dst_pid;
593 hello4->kshm_src_incarnation = hello->kshm_src_incarnation;
594 hello4->kshm_dst_incarnation = hello->kshm_dst_incarnation;
595 hello4->kshm_ctype = hello->kshm_ctype;
596 hello4->kshm_nips = hello->kshm_nips;
598 if (the_lnet.ln_testprotocompat) {
599 /* single-shot proto check */
600 if (test_and_clear_bit(0, &the_lnet.ln_testprotocompat))
601 hello->kshm_version++; /* just different! */
603 hello4->kshm_magic = LNET_PROTO_MAGIC;
604 hello4->kshm_version = hello->kshm_version;
605 hello4->kshm_src_nid = lnet_nid_to_nid4(&hello->kshm_src_nid);
606 hello4->kshm_dst_nid = lnet_nid_to_nid4(&hello->kshm_dst_nid);
607 hello4->kshm_src_pid = hello->kshm_src_pid;
608 hello4->kshm_dst_pid = hello->kshm_dst_pid;
609 hello4->kshm_src_incarnation = hello->kshm_src_incarnation;
610 hello4->kshm_dst_incarnation = hello->kshm_dst_incarnation;
611 hello4->kshm_ctype = hello->kshm_ctype;
612 hello4->kshm_nips = hello->kshm_nips;
614 rc = lnet_sock_write(sock, hello4, sizeof(*hello4),
615 lnet_acceptor_timeout());
616 CFS_FREE_PTR(hello4);
618 CNETERR("Error %d sending HELLO hdr to %pISp\n",
619 rc, &conn->ksnc_peeraddr);
623 if (hello->kshm_nips == 0)
626 rc = lnet_sock_write(sock, hello->kshm_ips,
627 hello->kshm_nips * sizeof(__u32),
628 lnet_acceptor_timeout());
630 CNETERR("Error %d sending HELLO payload (%d) to %pISp\n", rc,
632 &conn->ksnc_peeraddr);
639 ksocknal_send_hello_v4(struct ksock_conn *conn, struct ksock_hello_msg *hello)
641 struct socket *sock = conn->ksnc_sock;
644 hello->kshm_magic = LNET_PROTO_MAGIC;
645 hello->kshm_version = conn->ksnc_proto->pro_version;
647 rc = lnet_sock_write(sock, hello, sizeof(*hello),
648 lnet_acceptor_timeout());
651 CNETERR("Error %d sending HELLO hdr to %pISp\n",
652 rc, &conn->ksnc_peeraddr);
657 ksocknal_recv_hello_v1(struct ksock_conn *conn, struct ksock_hello_msg *hello,
660 struct socket *sock = conn->ksnc_sock;
661 struct _lnet_hdr_nid4 *hdr;
667 CERROR("Can't allocate struct lnet_hdr_nid4\n");
671 rc = lnet_sock_read(sock, &hdr->src_nid,
672 sizeof(*hdr) - offsetof(struct _lnet_hdr_nid4,
676 CERROR("Error %d reading rest of HELLO hdr from %pIS\n",
677 rc, &conn->ksnc_peeraddr);
678 LASSERT(rc < 0 && rc != -EALREADY);
682 /* ...and check we got what we expected */
683 if (hdr->type != cpu_to_le32 (LNET_MSG_HELLO)) {
684 CERROR("Expecting a HELLO hdr, but got type %d from %pIS\n",
685 le32_to_cpu(hdr->type),
686 &conn->ksnc_peeraddr);
691 lnet_nid4_to_nid(le64_to_cpu(hdr->src_nid), &hello->kshm_src_nid);
692 hello->kshm_src_pid = le32_to_cpu(hdr->src_pid);
693 hello->kshm_src_incarnation = le64_to_cpu(hdr->msg.hello.incarnation);
694 hello->kshm_ctype = le32_to_cpu(hdr->msg.hello.type);
695 hello->kshm_nips = le32_to_cpu(hdr->payload_length) / sizeof(__u32);
697 if (hello->kshm_nips > LNET_INTERFACES_NUM) {
698 CERROR("Bad nips %d from ip %pIS\n",
699 hello->kshm_nips, &conn->ksnc_peeraddr);
704 if (hello->kshm_nips == 0)
707 rc = lnet_sock_read(sock, hello->kshm_ips,
708 hello->kshm_nips * sizeof(__u32), timeout);
710 CERROR("Error %d reading IPs from ip %pIS\n",
711 rc, &conn->ksnc_peeraddr);
712 LASSERT(rc < 0 && rc != -EALREADY);
716 for (i = 0; i < (int) hello->kshm_nips; i++) {
717 hello->kshm_ips[i] = __le32_to_cpu(hello->kshm_ips[i]);
719 if (hello->kshm_ips[i] == 0) {
720 CERROR("Zero IP[%d] from ip %pIS\n",
721 i, &conn->ksnc_peeraddr);
733 ksocknal_recv_hello_v2(struct ksock_conn *conn, struct ksock_hello_msg *hello,
736 struct socket *sock = conn->ksnc_sock;
737 struct ksock_hello_msg_nid4 *hello4 = (void *)hello;
741 if (hello->kshm_magic == LNET_PROTO_MAGIC)
746 rc = lnet_sock_read(sock, &hello4->kshm_src_nid,
747 offsetof(struct ksock_hello_msg_nid4, kshm_ips) -
748 offsetof(struct ksock_hello_msg_nid4, kshm_src_nid),
751 CERROR("Error %d reading HELLO from %pIS\n",
752 rc, &conn->ksnc_peeraddr);
753 LASSERT(rc < 0 && rc != -EALREADY);
757 if (conn->ksnc_flip) {
758 /* These must be copied in reverse order to avoid corruption. */
759 hello->kshm_nips = __swab32(hello4->kshm_nips);
760 hello->kshm_ctype = __swab32(hello4->kshm_ctype);
761 hello->kshm_dst_incarnation = __swab64(hello4->kshm_dst_incarnation);
762 hello->kshm_src_incarnation = __swab64(hello4->kshm_src_incarnation);
763 hello->kshm_dst_pid = __swab32(hello4->kshm_dst_pid);
764 hello->kshm_src_pid = __swab32(hello4->kshm_src_pid);
765 lnet_nid4_to_nid(hello4->kshm_dst_nid, &hello->kshm_dst_nid);
766 lnet_nid4_to_nid(hello4->kshm_src_nid, &hello->kshm_src_nid);
768 /* These must be copied in reverse order to avoid corruption. */
769 hello->kshm_nips = hello4->kshm_nips;
770 hello->kshm_ctype = hello4->kshm_ctype;
771 hello->kshm_dst_incarnation = hello4->kshm_dst_incarnation;
772 hello->kshm_src_incarnation = hello4->kshm_src_incarnation;
773 hello->kshm_dst_pid = hello4->kshm_dst_pid;
774 hello->kshm_src_pid = hello4->kshm_src_pid;
775 lnet_nid4_to_nid(hello4->kshm_dst_nid, &hello->kshm_dst_nid);
776 lnet_nid4_to_nid(hello4->kshm_src_nid, &hello->kshm_src_nid);
779 if (hello->kshm_nips > LNET_INTERFACES_NUM) {
780 CERROR("Bad nips %d from ip %pIS\n",
781 hello->kshm_nips, &conn->ksnc_peeraddr);
785 if (hello->kshm_nips == 0)
788 rc = lnet_sock_read(sock, hello->kshm_ips,
789 hello->kshm_nips * sizeof(__u32), timeout);
791 CERROR("Error %d reading IPs from ip %pIS\n",
792 rc, &conn->ksnc_peeraddr);
793 LASSERT(rc < 0 && rc != -EALREADY);
797 for (i = 0; i < (int) hello->kshm_nips; i++) {
799 __swab32s(&hello->kshm_ips[i]);
801 if (hello->kshm_ips[i] == 0) {
802 CERROR("Zero IP[%d] from ip %pIS\n",
803 i, &conn->ksnc_peeraddr);
812 ksocknal_recv_hello_v4(struct ksock_conn *conn, struct ksock_hello_msg *hello,
815 struct socket *sock = conn->ksnc_sock;
818 if (hello->kshm_magic == LNET_PROTO_MAGIC)
823 rc = lnet_sock_read(sock, &hello->kshm_src_nid,
825 offsetof(struct ksock_hello_msg, kshm_src_nid),
828 CERROR("Error %d reading HELLO from %pIS\n",
829 rc, &conn->ksnc_peeraddr);
830 LASSERT(rc < 0 && rc != -EALREADY);
834 if (conn->ksnc_flip) {
835 __swab32s(&hello->kshm_src_pid);
836 __swab32s(&hello->kshm_dst_pid);
837 __swab64s(&hello->kshm_src_incarnation);
838 __swab64s(&hello->kshm_dst_incarnation);
839 __swab32s(&hello->kshm_ctype);
846 ksocknal_pack_msg_v1(struct ksock_tx *tx)
848 /* V1.x has no KSOCK_MSG_NOOP */
849 LASSERT(tx->tx_msg.ksm_type != KSOCK_MSG_NOOP);
850 LASSERT(tx->tx_lnetmsg != NULL);
852 lnet_hdr_to_nid4(&tx->tx_lnetmsg->msg_hdr,
853 &tx->tx_msg.ksm_u.lnetmsg_nid4);
854 tx->tx_hdr.iov_base = (void *)&tx->tx_msg.ksm_u.lnetmsg_nid4;
855 tx->tx_hdr.iov_len = sizeof(struct lnet_hdr_nid4);
857 tx->tx_nob = tx->tx_lnetmsg->msg_len + sizeof(struct lnet_hdr_nid4);
858 tx->tx_resid = tx->tx_nob;
862 ksocknal_pack_msg_v2(struct ksock_tx *tx)
866 tx->tx_hdr.iov_base = (void *)&tx->tx_msg;
868 switch (tx->tx_msg.ksm_type) {
870 LASSERT(tx->tx_lnetmsg != NULL);
871 hdr_size = (sizeof(struct ksock_msg_hdr) +
872 sizeof(struct lnet_hdr_nid4));
874 lnet_hdr_to_nid4(&tx->tx_lnetmsg->msg_hdr,
875 &tx->tx_msg.ksm_u.lnetmsg_nid4);
876 tx->tx_hdr.iov_len = hdr_size;
877 tx->tx_resid = tx->tx_nob = hdr_size + tx->tx_lnetmsg->msg_len;
880 LASSERT(tx->tx_lnetmsg == NULL);
881 hdr_size = sizeof(struct ksock_msg_hdr);
883 tx->tx_hdr.iov_len = hdr_size;
884 tx->tx_resid = tx->tx_nob = hdr_size;
889 /* Don't checksum before start sending, because packet can be
890 * piggybacked with ACK
895 ksocknal_pack_msg_v4(struct ksock_tx *tx)
899 tx->tx_hdr.iov_base = (void *)&tx->tx_msg;
901 switch (tx->tx_msg.ksm_type) {
903 LASSERT(tx->tx_lnetmsg != NULL);
904 hdr_size = (sizeof(struct ksock_msg_hdr) +
905 sizeof(struct lnet_hdr_nid16));
907 lnet_hdr_to_nid16(&tx->tx_lnetmsg->msg_hdr,
908 &tx->tx_msg.ksm_u.lnetmsg_nid16);
909 tx->tx_hdr.iov_len = hdr_size;
910 tx->tx_resid = tx->tx_nob = hdr_size + tx->tx_lnetmsg->msg_len;
913 LASSERT(tx->tx_lnetmsg == NULL);
914 hdr_size = sizeof(struct ksock_msg_hdr);
916 tx->tx_hdr.iov_len = hdr_size;
917 tx->tx_resid = tx->tx_nob = hdr_size;
922 /* Don't checksum before start sending, because packet can be
923 * piggybacked with ACK
928 ksocknal_unpack_msg_v1(struct ksock_msg *msg, struct lnet_hdr *hdr)
931 msg->ksm_type = KSOCK_MSG_LNET;
932 msg->ksm_zc_cookies[0] = msg->ksm_zc_cookies[1] = 0;
933 lnet_hdr_from_nid4(hdr, &msg->ksm_u.lnetmsg_nid4);
937 ksocknal_unpack_msg_v2(struct ksock_msg *msg, struct lnet_hdr *hdr)
939 lnet_hdr_from_nid4(hdr, &msg->ksm_u.lnetmsg_nid4);
943 ksocknal_unpack_msg_v4(struct ksock_msg *msg, struct lnet_hdr *hdr)
945 lnet_hdr_from_nid16(hdr, &msg->ksm_u.lnetmsg_nid16);
948 const struct ksock_proto ksocknal_protocol_v1x =
950 .pro_version = KSOCK_PROTO_V1,
951 .pro_send_hello = ksocknal_send_hello_v1,
952 .pro_recv_hello = ksocknal_recv_hello_v1,
953 .pro_pack = ksocknal_pack_msg_v1,
954 .pro_unpack = ksocknal_unpack_msg_v1,
955 .pro_queue_tx_msg = ksocknal_queue_tx_msg_v1,
956 .pro_handle_zcreq = NULL,
957 .pro_handle_zcack = NULL,
958 .pro_queue_tx_zcack = NULL,
959 .pro_match_tx = ksocknal_match_tx
962 const struct ksock_proto ksocknal_protocol_v2x =
964 .pro_version = KSOCK_PROTO_V2,
965 .pro_send_hello = ksocknal_send_hello_v2,
966 .pro_recv_hello = ksocknal_recv_hello_v2,
967 .pro_pack = ksocknal_pack_msg_v2,
968 .pro_unpack = ksocknal_unpack_msg_v2,
969 .pro_queue_tx_msg = ksocknal_queue_tx_msg_v2,
970 .pro_queue_tx_zcack = ksocknal_queue_tx_zcack_v2,
971 .pro_handle_zcreq = ksocknal_handle_zcreq,
972 .pro_handle_zcack = ksocknal_handle_zcack,
973 .pro_match_tx = ksocknal_match_tx
976 const struct ksock_proto ksocknal_protocol_v3x =
978 .pro_version = KSOCK_PROTO_V3,
979 .pro_send_hello = ksocknal_send_hello_v2,
980 .pro_recv_hello = ksocknal_recv_hello_v2,
981 .pro_pack = ksocknal_pack_msg_v2,
982 .pro_unpack = ksocknal_unpack_msg_v2,
983 .pro_queue_tx_msg = ksocknal_queue_tx_msg_v2,
984 .pro_queue_tx_zcack = ksocknal_queue_tx_zcack_v3,
985 .pro_handle_zcreq = ksocknal_handle_zcreq,
986 .pro_handle_zcack = ksocknal_handle_zcack,
987 .pro_match_tx = ksocknal_match_tx_v3
990 const struct ksock_proto ksocknal_protocol_v4x = {
991 .pro_version = KSOCK_PROTO_V4,
992 .pro_send_hello = ksocknal_send_hello_v4,
993 .pro_recv_hello = ksocknal_recv_hello_v4,
994 .pro_pack = ksocknal_pack_msg_v4,
995 .pro_unpack = ksocknal_unpack_msg_v4,
996 .pro_queue_tx_msg = ksocknal_queue_tx_msg_v2,
997 .pro_queue_tx_zcack = ksocknal_queue_tx_zcack_v3,
998 .pro_handle_zcreq = ksocknal_handle_zcreq,
999 .pro_handle_zcack = ksocknal_handle_zcack,
1000 .pro_match_tx = ksocknal_match_tx_v4,