Whamcloud - gitweb
LU-14945 lnet: don't use hops to determine the route state
[fs/lustre-release.git] / lnet / klnds / socklnd / socklnd_proto.c
1 /*
2  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
3  *
4  * Copyright (c) 2012, 2017, Intel Corporation.
5  *
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>
10  *
11  *   This file is part of Lustre, https://wiki.whamcloud.com/
12  *
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.
16  *
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.
21  *
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.
25  */
26
27 #include "socklnd.h"
28
29 /*
30  * Protocol entries :
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
42  */
43
44 static struct ksock_tx *
45 ksocknal_queue_tx_msg_v1(struct ksock_conn *conn, struct ksock_tx *tx_msg)
46 {
47         /* V1.x, just enqueue it */
48         list_add_tail(&tx_msg->tx_list, &conn->ksnc_tx_queue);
49         return NULL;
50 }
51
52 void
53 ksocknal_next_tx_carrier(struct ksock_conn *conn)
54 {
55         struct ksock_tx *tx = conn->ksnc_tx_carrier;
56
57         /* Called holding BH lock: conn->ksnc_scheduler->kss_lock */
58         LASSERT(!list_empty(&conn->ksnc_tx_queue));
59         LASSERT(tx != NULL);
60
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;
65         } else {
66                 conn->ksnc_tx_carrier = list_next_entry(tx, tx_list);
67                 LASSERT(conn->ksnc_tx_carrier->tx_msg.ksm_type ==
68                         tx->tx_msg.ksm_type);
69         }
70 }
71
72 static int
73 ksocknal_queue_tx_zcack_v2(struct ksock_conn *conn,
74                            struct ksock_tx *tx_ack, __u64 cookie)
75 {
76         struct ksock_tx *tx = conn->ksnc_tx_carrier;
77
78         LASSERT (tx_ack == NULL ||
79                  tx_ack->tx_msg.ksm_type == KSOCK_MSG_NOOP);
80
81         /*
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.
87          */
88         if (tx == NULL) {
89                 if (tx_ack != NULL) {
90                         list_add_tail(&tx_ack->tx_list,
91                                           &conn->ksnc_tx_queue);
92                         conn->ksnc_tx_carrier = tx_ack;
93                 }
94                 return 0;
95         }
96
97         if (tx->tx_msg.ksm_type == KSOCK_MSG_NOOP) {
98                 /* tx is noop zc-ack, can't piggyback zc-ack cookie */
99                 if (tx_ack != NULL)
100                         list_add_tail(&tx_ack->tx_list,
101                                           &conn->ksnc_tx_queue);
102                 return 0;
103         }
104
105         LASSERT(tx->tx_msg.ksm_type == KSOCK_MSG_LNET);
106         LASSERT(tx->tx_msg.ksm_zc_cookies[1] == 0);
107
108         if (tx_ack != NULL)
109                 cookie = tx_ack->tx_msg.ksm_zc_cookies[1];
110
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);
115
116         return 1;
117 }
118
119 static struct ksock_tx *
120 ksocknal_queue_tx_msg_v2(struct ksock_conn *conn, struct ksock_tx *tx_msg)
121 {
122         struct ksock_tx  *tx  = conn->ksnc_tx_carrier;
123
124         /*
125          * Enqueue tx_msg:
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.
130          */
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;
134                 return NULL;
135         }
136
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);
139                 return NULL;
140         }
141
142         LASSERT (tx->tx_msg.ksm_type == KSOCK_MSG_NOOP);
143
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);
147
148         /* use new_tx to replace the noop zc-ack packet */
149         list_splice(&tx->tx_list, &tx_msg->tx_list);
150
151         return tx;
152 }
153
154 static int
155 ksocknal_queue_tx_zcack_v3(struct ksock_conn *conn,
156                            struct ksock_tx *tx_ack, __u64 cookie)
157 {
158         struct ksock_tx *tx;
159
160         if (conn->ksnc_type != SOCKLND_CONN_ACK)
161                 return ksocknal_queue_tx_zcack_v2(conn, tx_ack, cookie);
162
163         /* non-blocking ZC-ACK (to router) */
164         LASSERT (tx_ack == NULL ||
165                  tx_ack->tx_msg.ksm_type == KSOCK_MSG_NOOP);
166
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;
172                 }
173                 return 0;
174         }
175
176         /* conn->ksnc_tx_carrier != NULL */
177
178         if (tx_ack != NULL)
179                 cookie = tx_ack->tx_msg.ksm_zc_cookies[1];
180
181         if (cookie == SOCKNAL_KEEPALIVE_PING) /* ignore keepalive PING */
182                 return 1;
183
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;
188                 return 1;
189         }
190
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 */
196         }
197
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;
203                 } else {
204                         tx->tx_msg.ksm_zc_cookies[0] = cookie;
205                 }
206
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);
210                 }
211
212                 return 1;
213         }
214
215         /* takes two or more cookies already */
216
217         if (tx->tx_msg.ksm_zc_cookies[0] > tx->tx_msg.ksm_zc_cookies[1]) {
218                 __u64   tmp = 0;
219
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);
223
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)
227                                 tmp = cookie;
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];
232                 }
233
234                 if (tmp != 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;
238                         return 1;
239                 }
240
241         } else {
242                 /* ksm_zc_cookies[0] < ksm_zc_cookies[1], it is a range
243                  * of cookies
244                  */
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),
249                               cookie);
250                         return 1; /* XXX: return error in the future */
251                 }
252
253                 if (cookie == tx->tx_msg.ksm_zc_cookies[1] + 1) {
254                         tx->tx_msg.ksm_zc_cookies[1] = cookie;
255                         return 1;
256                 }
257
258                 if (cookie == tx->tx_msg.ksm_zc_cookies[0] - 1) {
259                         tx->tx_msg.ksm_zc_cookies[0] = cookie;
260                         return 1;
261                 }
262         }
263
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);
269         }
270
271         return 0;
272 }
273
274 static int
275 ksocknal_match_tx(struct ksock_conn *conn, struct ksock_tx *tx, int nonblk)
276 {
277         int nob;
278
279 #if SOCKNAL_VERSION_DEBUG
280         if (!*ksocknal_tunables.ksnd_typed_conns)
281                 return SOCKNAL_MATCH_YES;
282 #endif
283
284         if (tx == NULL || tx->tx_lnetmsg == NULL) {
285                 /* noop packet */
286                 nob = offsetof(struct ksock_msg, ksm_u);
287         } else {
288                 nob = tx->tx_lnetmsg->msg_len +
289                       ((conn->ksnc_proto == &ksocknal_protocol_v1x) ?
290                        sizeof(struct lnet_hdr) : sizeof(struct ksock_msg));
291         }
292
293         /* default checking for typed connection */
294         switch (conn->ksnc_type) {
295         default:
296                 CERROR("ksnc_type bad: %u\n", conn->ksnc_type);
297                 LBUG();
298         case SOCKLND_CONN_ANY:
299                 return SOCKNAL_MATCH_YES;
300
301         case SOCKLND_CONN_BULK_IN:
302                 return SOCKNAL_MATCH_MAY;
303
304         case SOCKLND_CONN_BULK_OUT:
305                 if (nob < *ksocknal_tunables.ksnd_min_bulk)
306                         return SOCKNAL_MATCH_MAY;
307                 else
308                         return SOCKNAL_MATCH_YES;
309
310         case SOCKLND_CONN_CONTROL:
311                 if (nob >= *ksocknal_tunables.ksnd_min_bulk)
312                         return SOCKNAL_MATCH_MAY;
313                 else
314                         return SOCKNAL_MATCH_YES;
315         }
316 }
317
318 static int
319 ksocknal_match_tx_v3(struct ksock_conn *conn, struct ksock_tx *tx, int nonblk)
320 {
321         int nob;
322
323         if (tx == NULL || tx->tx_lnetmsg == NULL)
324                 nob = offsetof(struct ksock_msg, ksm_u);
325         else
326                 nob = tx->tx_lnetmsg->msg_len + sizeof(struct ksock_msg);
327
328         switch (conn->ksnc_type) {
329         default:
330                 CERROR("ksnc_type bad: %u\n", conn->ksnc_type);
331                 LBUG();
332         case SOCKLND_CONN_ANY:
333                 return SOCKNAL_MATCH_NO;
334
335         case SOCKLND_CONN_ACK:
336                 if (nonblk)
337                         return SOCKNAL_MATCH_YES;
338                 else if (tx == NULL || tx->tx_lnetmsg == NULL)
339                         return SOCKNAL_MATCH_MAY;
340                 else
341                         return SOCKNAL_MATCH_NO;
342
343         case SOCKLND_CONN_BULK_OUT:
344                 if (nonblk)
345                         return SOCKNAL_MATCH_NO;
346                 else if (nob < *ksocknal_tunables.ksnd_min_bulk)
347                         return SOCKNAL_MATCH_MAY;
348                 else
349                         return SOCKNAL_MATCH_YES;
350
351         case SOCKLND_CONN_CONTROL:
352                 if (nonblk)
353                         return SOCKNAL_MATCH_NO;
354                 else if (nob >= *ksocknal_tunables.ksnd_min_bulk)
355                         return SOCKNAL_MATCH_MAY;
356                 else
357                         return SOCKNAL_MATCH_YES;
358         }
359 }
360
361 /* (Sink) handle incoming ZC request from sender */
362 static int
363 ksocknal_handle_zcreq(struct ksock_conn *c, __u64 cookie, int remote)
364 {
365         struct ksock_peer_ni *peer_ni = c->ksnc_peer;
366         struct ksock_conn *conn;
367         struct ksock_tx *tx;
368         int rc;
369
370         read_lock(&ksocknal_data.ksnd_global_lock);
371
372         conn = ksocknal_find_conn_locked(peer_ni, NULL, !!remote);
373         if (conn != NULL) {
374                 struct ksock_sched *sched = conn->ksnc_scheduler;
375
376                 LASSERT(conn->ksnc_proto->pro_queue_tx_zcack != NULL);
377
378                 spin_lock_bh(&sched->kss_lock);
379
380                 rc = conn->ksnc_proto->pro_queue_tx_zcack(conn, NULL, cookie);
381
382                 spin_unlock_bh(&sched->kss_lock);
383
384                 if (rc) { /* piggybacked */
385                         read_unlock(&ksocknal_data.ksnd_global_lock);
386                         return 0;
387                 }
388         }
389
390         read_unlock(&ksocknal_data.ksnd_global_lock);
391
392         /* ACK connection is not ready, or can't piggyback the ACK */
393         tx = ksocknal_alloc_tx_noop(cookie, !!remote);
394         if (tx == NULL)
395                 return -ENOMEM;
396
397         rc = ksocknal_launch_packet(peer_ni->ksnp_ni, tx, &peer_ni->ksnp_id);
398         if (rc == 0)
399                 return 0;
400
401         ksocknal_free_tx(tx);
402         return rc;
403 }
404
405 /* (Sender) handle ZC_ACK from sink */
406 static int
407 ksocknal_handle_zcack(struct ksock_conn *conn, __u64 cookie1, __u64 cookie2)
408 {
409         struct ksock_peer_ni *peer_ni = conn->ksnc_peer;
410         struct ksock_tx *tx;
411         struct ksock_tx *tmp;
412         LIST_HEAD(zlist);
413         int count;
414
415         if (cookie1 == 0)
416                 cookie1 = cookie2;
417
418         count = (cookie1 > cookie2) ? 2 : (cookie2 - cookie1 + 1);
419
420         if (cookie2 == SOCKNAL_KEEPALIVE_PING &&
421             conn->ksnc_proto == &ksocknal_protocol_v3x) {
422                 /* keepalive PING for V3.x, just ignore it */
423                 return count == 1 ? 0 : -EPROTO;
424         }
425
426         spin_lock(&peer_ni->ksnp_lock);
427
428         list_for_each_entry_safe(tx, tmp, &peer_ni->ksnp_zc_req_list,
429                                  tx_zc_list) {
430                 __u64 c = tx->tx_msg.ksm_zc_cookies[0];
431
432                 if (c == cookie1 || c == cookie2 || (cookie1 < c && c < cookie2)) {
433                         tx->tx_msg.ksm_zc_cookies[0] = 0;
434                         list_move(&tx->tx_zc_list, &zlist);
435
436                         if (--count == 0)
437                                 break;
438                 }
439         }
440
441         spin_unlock(&peer_ni->ksnp_lock);
442
443         while ((tx = list_first_entry_or_null(&zlist, struct ksock_tx,
444                                               tx_zc_list)) != NULL) {
445                 list_del(&tx->tx_zc_list);
446                 ksocknal_tx_decref(tx);
447         }
448
449         return count == 0 ? 0 : -EPROTO;
450 }
451
452 static int
453 ksocknal_send_hello_v1(struct ksock_conn *conn, struct ksock_hello_msg *hello)
454 {
455         struct socket *sock = conn->ksnc_sock;
456         struct lnet_hdr *hdr;
457         struct lnet_magicversion *hmv;
458         int rc;
459         int i;
460
461         BUILD_BUG_ON(sizeof(struct lnet_magicversion) !=
462                      offsetof(struct lnet_hdr, src_nid));
463
464         LIBCFS_ALLOC(hdr, sizeof(*hdr));
465         if (hdr == NULL) {
466                 CERROR("Can't allocate struct lnet_hdr\n");
467                 return -ENOMEM;
468         }
469
470         hmv = (struct lnet_magicversion *)&hdr->dest_nid;
471
472         /* Re-organize V2.x message header to V1.x (struct lnet_hdr)
473          * header and send out */
474         hmv->magic         = cpu_to_le32 (LNET_PROTO_TCP_MAGIC);
475         hmv->version_major = cpu_to_le16 (KSOCK_PROTO_V1_MAJOR);
476         hmv->version_minor = cpu_to_le16 (KSOCK_PROTO_V1_MINOR);
477
478         if (the_lnet.ln_testprotocompat) {
479                 /* single-shot proto check */
480                 if (test_and_clear_bit(0, &the_lnet.ln_testprotocompat))
481                         hmv->version_major++;   /* just different! */
482
483                 if (test_and_clear_bit(1, &the_lnet.ln_testprotocompat))
484                         hmv->magic = LNET_PROTO_MAGIC;
485         }
486
487         hdr->src_nid        = cpu_to_le64 (hello->kshm_src_nid);
488         hdr->src_pid        = cpu_to_le32 (hello->kshm_src_pid);
489         hdr->type           = cpu_to_le32 (LNET_MSG_HELLO);
490         hdr->payload_length = cpu_to_le32 (hello->kshm_nips * sizeof(__u32));
491         hdr->msg.hello.type = cpu_to_le32 (hello->kshm_ctype);
492         hdr->msg.hello.incarnation = cpu_to_le64 (hello->kshm_src_incarnation);
493
494         rc = lnet_sock_write(sock, hdr, sizeof(*hdr), lnet_acceptor_timeout());
495         if (rc != 0) {
496                 CNETERR("Error %d sending HELLO hdr to %pISp\n",
497                         rc, &conn->ksnc_peeraddr);
498                 goto out;
499         }
500
501         if (hello->kshm_nips == 0)
502                 goto out;
503
504         for (i = 0; i < (int) hello->kshm_nips; i++) {
505                 hello->kshm_ips[i] = __cpu_to_le32 (hello->kshm_ips[i]);
506         }
507
508         rc = lnet_sock_write(sock, hello->kshm_ips,
509                              hello->kshm_nips * sizeof(__u32),
510                              lnet_acceptor_timeout());
511         if (rc != 0) {
512                 CNETERR("Error %d sending HELLO payload (%d) to %pISp\n",
513                         rc, hello->kshm_nips,
514                         &conn->ksnc_peeraddr);
515         }
516 out:
517         LIBCFS_FREE(hdr, sizeof(*hdr));
518
519         return rc;
520 }
521
522 static int
523 ksocknal_send_hello_v2(struct ksock_conn *conn, struct ksock_hello_msg *hello)
524 {
525         struct socket *sock = conn->ksnc_sock;
526         int rc;
527
528         hello->kshm_magic   = LNET_PROTO_MAGIC;
529         hello->kshm_version = conn->ksnc_proto->pro_version;
530
531         if (the_lnet.ln_testprotocompat) {
532                 /* single-shot proto check */
533                 if (test_and_clear_bit(0, &the_lnet.ln_testprotocompat))
534                         hello->kshm_version++;   /* just different! */
535         }
536
537         rc = lnet_sock_write(sock, hello, offsetof(struct ksock_hello_msg, kshm_ips),
538                                lnet_acceptor_timeout());
539
540         if (rc != 0) {
541                 CNETERR("Error %d sending HELLO hdr to %pISp\n",
542                         rc, &conn->ksnc_peeraddr);
543                 return rc;
544         }
545
546         if (hello->kshm_nips == 0)
547                 return 0;
548
549         rc = lnet_sock_write(sock, hello->kshm_ips,
550                              hello->kshm_nips * sizeof(__u32),
551                              lnet_acceptor_timeout());
552         if (rc != 0) {
553                 CNETERR("Error %d sending HELLO payload (%d) to %pISp\n", rc,
554                         hello->kshm_nips,
555                         &conn->ksnc_peeraddr);
556         }
557
558         return rc;
559 }
560
561 static int
562 ksocknal_recv_hello_v1(struct ksock_conn *conn, struct ksock_hello_msg *hello,
563                        int timeout)
564 {
565         struct socket *sock = conn->ksnc_sock;
566         struct lnet_hdr *hdr;
567         int rc;
568         int i;
569
570         LIBCFS_ALLOC(hdr, sizeof(*hdr));
571         if (hdr == NULL) {
572                 CERROR("Can't allocate struct lnet_hdr\n");
573                 return -ENOMEM;
574         }
575
576         rc = lnet_sock_read(sock, &hdr->src_nid,
577                               sizeof(*hdr) - offsetof(struct lnet_hdr, src_nid),
578                               timeout);
579         if (rc != 0) {
580                 CERROR("Error %d reading rest of HELLO hdr from %pIS\n",
581                        rc, &conn->ksnc_peeraddr);
582                 LASSERT(rc < 0 && rc != -EALREADY);
583                 goto out;
584         }
585
586         /* ...and check we got what we expected */
587         if (hdr->type != cpu_to_le32 (LNET_MSG_HELLO)) {
588                 CERROR("Expecting a HELLO hdr, but got type %d from %pIS\n",
589                        le32_to_cpu(hdr->type),
590                        &conn->ksnc_peeraddr);
591                 rc = -EPROTO;
592                 goto out;
593         }
594
595         hello->kshm_src_nid         = le64_to_cpu (hdr->src_nid);
596         hello->kshm_src_pid         = le32_to_cpu (hdr->src_pid);
597         hello->kshm_src_incarnation = le64_to_cpu (hdr->msg.hello.incarnation);
598         hello->kshm_ctype           = le32_to_cpu (hdr->msg.hello.type);
599         hello->kshm_nips            = le32_to_cpu (hdr->payload_length) /
600                                          sizeof (__u32);
601
602         if (hello->kshm_nips > LNET_INTERFACES_NUM) {
603                 CERROR("Bad nips %d from ip %pIS\n",
604                        hello->kshm_nips, &conn->ksnc_peeraddr);
605                 rc = -EPROTO;
606                 goto out;
607         }
608
609         if (hello->kshm_nips == 0)
610                 goto out;
611
612         rc = lnet_sock_read(sock, hello->kshm_ips,
613                               hello->kshm_nips * sizeof(__u32), timeout);
614         if (rc != 0) {
615                 CERROR("Error %d reading IPs from ip %pIS\n",
616                        rc, &conn->ksnc_peeraddr);
617                 LASSERT(rc < 0 && rc != -EALREADY);
618                 goto out;
619         }
620
621         for (i = 0; i < (int) hello->kshm_nips; i++) {
622                 hello->kshm_ips[i] = __le32_to_cpu(hello->kshm_ips[i]);
623
624                 if (hello->kshm_ips[i] == 0) {
625                         CERROR("Zero IP[%d] from ip %pIS\n",
626                                i, &conn->ksnc_peeraddr);
627                         rc = -EPROTO;
628                         break;
629                 }
630         }
631 out:
632         LIBCFS_FREE(hdr, sizeof(*hdr));
633
634         return rc;
635 }
636
637 static int
638 ksocknal_recv_hello_v2(struct ksock_conn *conn, struct ksock_hello_msg *hello,
639                        int timeout)
640 {
641         struct socket     *sock = conn->ksnc_sock;
642         int                rc;
643         int                i;
644
645         if (hello->kshm_magic == LNET_PROTO_MAGIC)
646                 conn->ksnc_flip = 0;
647         else
648                 conn->ksnc_flip = 1;
649
650         rc = lnet_sock_read(sock, &hello->kshm_src_nid,
651                               offsetof(struct ksock_hello_msg, kshm_ips) -
652                                        offsetof(struct ksock_hello_msg, kshm_src_nid),
653                               timeout);
654         if (rc != 0) {
655                 CERROR("Error %d reading HELLO from %pIS\n",
656                        rc, &conn->ksnc_peeraddr);
657                 LASSERT(rc < 0 && rc != -EALREADY);
658                 return rc;
659         }
660
661         if (conn->ksnc_flip) {
662                 __swab32s(&hello->kshm_src_pid);
663                 __swab64s(&hello->kshm_src_nid);
664                 __swab32s(&hello->kshm_dst_pid);
665                 __swab64s(&hello->kshm_dst_nid);
666                 __swab64s(&hello->kshm_src_incarnation);
667                 __swab64s(&hello->kshm_dst_incarnation);
668                 __swab32s(&hello->kshm_ctype);
669                 __swab32s(&hello->kshm_nips);
670         }
671
672         if (hello->kshm_nips > LNET_INTERFACES_NUM) {
673                 CERROR("Bad nips %d from ip %pIS\n",
674                        hello->kshm_nips, &conn->ksnc_peeraddr);
675                 return -EPROTO;
676         }
677
678         if (hello->kshm_nips == 0)
679                 return 0;
680
681         rc = lnet_sock_read(sock, hello->kshm_ips,
682                             hello->kshm_nips * sizeof(__u32), timeout);
683         if (rc != 0) {
684                 CERROR("Error %d reading IPs from ip %pIS\n",
685                        rc, &conn->ksnc_peeraddr);
686                 LASSERT(rc < 0 && rc != -EALREADY);
687                 return rc;
688         }
689
690         for (i = 0; i < (int) hello->kshm_nips; i++) {
691                 if (conn->ksnc_flip)
692                         __swab32s(&hello->kshm_ips[i]);
693
694                 if (hello->kshm_ips[i] == 0) {
695                         CERROR("Zero IP[%d] from ip %pIS\n",
696                                i, &conn->ksnc_peeraddr);
697                         return -EPROTO;
698                 }
699         }
700
701         return 0;
702 }
703
704 static void
705 ksocknal_pack_msg_v1(struct ksock_tx *tx)
706 {
707         /* V1.x has no KSOCK_MSG_NOOP */
708         LASSERT(tx->tx_msg.ksm_type != KSOCK_MSG_NOOP);
709         LASSERT(tx->tx_lnetmsg != NULL);
710
711         tx->tx_hdr.iov_base = (void *)&tx->tx_lnetmsg->msg_hdr;
712         tx->tx_hdr.iov_len  = sizeof(struct lnet_hdr);
713
714         tx->tx_nob = tx->tx_lnetmsg->msg_len + sizeof(struct lnet_hdr);
715         tx->tx_resid = tx->tx_nob;
716 }
717
718 static void
719 ksocknal_pack_msg_v2(struct ksock_tx *tx)
720 {
721         tx->tx_hdr.iov_base = (void *)&tx->tx_msg;
722
723         if (tx->tx_lnetmsg != NULL) {
724                 LASSERT(tx->tx_msg.ksm_type != KSOCK_MSG_NOOP);
725
726                 tx->tx_msg.ksm_u.lnetmsg.ksnm_hdr = tx->tx_lnetmsg->msg_hdr;
727                 tx->tx_hdr.iov_len = sizeof(struct ksock_msg);
728                 tx->tx_resid = tx->tx_nob = sizeof(struct ksock_msg) + tx->tx_lnetmsg->msg_len;
729         } else {
730                 LASSERT(tx->tx_msg.ksm_type == KSOCK_MSG_NOOP);
731
732                 tx->tx_hdr.iov_len = offsetof(struct ksock_msg,
733                                               ksm_u.lnetmsg.ksnm_hdr);
734                 tx->tx_resid = tx->tx_nob = offsetof(struct ksock_msg,  ksm_u.lnetmsg.ksnm_hdr);
735         }
736         /* Don't checksum before start sending, because packet can be piggybacked with ACK */
737 }
738
739 static void
740 ksocknal_unpack_msg_v1(struct ksock_msg *msg)
741 {
742         msg->ksm_csum           = 0;
743         msg->ksm_type           = KSOCK_MSG_LNET;
744         msg->ksm_zc_cookies[0]  = msg->ksm_zc_cookies[1]  = 0;
745 }
746
747 static void
748 ksocknal_unpack_msg_v2(struct ksock_msg *msg)
749 {
750         return;  /* Do nothing */
751 }
752
753 const struct ksock_proto ksocknal_protocol_v1x =
754 {
755         .pro_version            = KSOCK_PROTO_V1,
756         .pro_send_hello         = ksocknal_send_hello_v1,
757         .pro_recv_hello         = ksocknal_recv_hello_v1,
758         .pro_pack               = ksocknal_pack_msg_v1,
759         .pro_unpack             = ksocknal_unpack_msg_v1,
760         .pro_queue_tx_msg       = ksocknal_queue_tx_msg_v1,
761         .pro_handle_zcreq       = NULL,
762         .pro_handle_zcack       = NULL,
763         .pro_queue_tx_zcack     = NULL,
764         .pro_match_tx           = ksocknal_match_tx
765 };
766
767 const struct ksock_proto ksocknal_protocol_v2x =
768 {
769         .pro_version            = KSOCK_PROTO_V2,
770         .pro_send_hello         = ksocknal_send_hello_v2,
771         .pro_recv_hello         = ksocknal_recv_hello_v2,
772         .pro_pack               = ksocknal_pack_msg_v2,
773         .pro_unpack             = ksocknal_unpack_msg_v2,
774         .pro_queue_tx_msg       = ksocknal_queue_tx_msg_v2,
775         .pro_queue_tx_zcack     = ksocknal_queue_tx_zcack_v2,
776         .pro_handle_zcreq       = ksocknal_handle_zcreq,
777         .pro_handle_zcack       = ksocknal_handle_zcack,
778         .pro_match_tx           = ksocknal_match_tx
779 };
780
781 const struct ksock_proto ksocknal_protocol_v3x =
782 {
783         .pro_version            = KSOCK_PROTO_V3,
784         .pro_send_hello         = ksocknal_send_hello_v2,
785         .pro_recv_hello         = ksocknal_recv_hello_v2,
786         .pro_pack               = ksocknal_pack_msg_v2,
787         .pro_unpack             = ksocknal_unpack_msg_v2,
788         .pro_queue_tx_msg       = ksocknal_queue_tx_msg_v2,
789         .pro_queue_tx_zcack     = ksocknal_queue_tx_zcack_v3,
790         .pro_handle_zcreq       = ksocknal_handle_zcreq,
791         .pro_handle_zcack       = ksocknal_handle_zcack,
792         .pro_match_tx           = ksocknal_match_tx_v3
793 };
794