Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lnet / klnds / socklnd / socklnd_proto.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
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 Portals, http://www.sf.net/projects/sandiaportals/
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 ksock_tx_t *
45 ksocknal_queue_tx_msg_v1(ksock_conn_t *conn, ksock_tx_t *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(ksock_conn_t *conn)
54 {
55         ksock_tx_t     *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_entry(tx->tx_list.next, ksock_tx_t, tx_list);
67                 LASSERT (conn->ksnc_tx_carrier->tx_msg.ksm_type == tx->tx_msg.ksm_type);
68         }
69 }
70
71 static int
72 ksocknal_queue_tx_zcack_v2(ksock_conn_t *conn,
73                            ksock_tx_t *tx_ack, __u64 cookie)
74 {
75         ksock_tx_t *tx = conn->ksnc_tx_carrier;
76
77         LASSERT (tx_ack == NULL ||
78                  tx_ack->tx_msg.ksm_type == KSOCK_MSG_NOOP);
79
80         /*
81          * Enqueue or piggyback tx_ack / cookie
82          * . no tx can piggyback cookie of tx_ack (or cookie), just
83          *   enqueue the tx_ack (if tx_ack != NUL) and return NULL.
84          * . There is tx can piggyback cookie of tx_ack (or cookie),
85          *   piggyback the cookie and return the tx.
86          */
87         if (tx == NULL) {
88                 if (tx_ack != NULL) {
89                         list_add_tail(&tx_ack->tx_list, &conn->ksnc_tx_queue);
90                         conn->ksnc_tx_carrier = tx_ack;
91                 }
92                 return 0;
93         }
94
95         if (tx->tx_msg.ksm_type == KSOCK_MSG_NOOP) {
96                 /* tx is noop zc-ack, can't piggyback zc-ack cookie */
97                 if (tx_ack != NULL)
98                         list_add_tail(&tx_ack->tx_list, &conn->ksnc_tx_queue);
99                 return 0;
100         }
101
102         LASSERT(tx->tx_msg.ksm_type == KSOCK_MSG_LNET);
103         LASSERT(tx->tx_msg.ksm_zc_cookies[1] == 0);
104
105         if (tx_ack != NULL)
106                 cookie = tx_ack->tx_msg.ksm_zc_cookies[1];
107
108         /* piggyback the zc-ack cookie */
109         tx->tx_msg.ksm_zc_cookies[1] = cookie;
110         /* move on to the next TX which can carry cookie */
111         ksocknal_next_tx_carrier(conn);
112
113         return 1;
114 }
115
116 static ksock_tx_t *
117 ksocknal_queue_tx_msg_v2(ksock_conn_t *conn, ksock_tx_t *tx_msg)
118 {
119         ksock_tx_t  *tx  = conn->ksnc_tx_carrier;
120
121         /*
122          * Enqueue tx_msg:
123          * . If there is no NOOP on the connection, just enqueue
124          *   tx_msg and return NULL
125          * . If there is NOOP on the connection, piggyback the cookie
126          *   and replace the NOOP tx, and return the NOOP tx.
127          */
128         if (tx == NULL) { /* nothing on queue */
129                 list_add_tail(&tx_msg->tx_list, &conn->ksnc_tx_queue);
130                 conn->ksnc_tx_carrier = tx_msg;
131                 return NULL;
132         }
133
134         if (tx->tx_msg.ksm_type == KSOCK_MSG_LNET) { /* nothing to carry */
135                 list_add_tail(&tx_msg->tx_list, &conn->ksnc_tx_queue);
136                 return NULL;
137         }
138
139         LASSERT (tx->tx_msg.ksm_type == KSOCK_MSG_NOOP);
140
141         /* There is a noop zc-ack can be piggybacked */
142         tx_msg->tx_msg.ksm_zc_cookies[1] = tx->tx_msg.ksm_zc_cookies[1];
143         ksocknal_next_tx_carrier(conn);
144
145         /* use new_tx to replace the noop zc-ack packet */
146         list_add(&tx_msg->tx_list, &tx->tx_list);
147         list_del(&tx->tx_list);
148
149         return tx;
150 }
151
152 static int
153 ksocknal_queue_tx_zcack_v3(ksock_conn_t *conn,
154                            ksock_tx_t *tx_ack, __u64 cookie)
155 {
156         ksock_tx_t *tx;
157
158         if (conn->ksnc_type != SOCKLND_CONN_ACK)
159                 return ksocknal_queue_tx_zcack_v2(conn, tx_ack, cookie);
160
161         /* non-blocking ZC-ACK (to router) */
162         LASSERT (tx_ack == NULL ||
163                  tx_ack->tx_msg.ksm_type == KSOCK_MSG_NOOP);
164
165         if ((tx = conn->ksnc_tx_carrier) == NULL) {
166                 if (tx_ack != NULL) {
167                         list_add_tail(&tx_ack->tx_list, &conn->ksnc_tx_queue);
168                         conn->ksnc_tx_carrier = tx_ack;
169                 }
170                 return 0;
171         }
172
173         /* conn->ksnc_tx_carrier != NULL */
174
175         if (tx_ack != NULL)
176                 cookie = tx_ack->tx_msg.ksm_zc_cookies[1];
177
178         if (cookie == SOCKNAL_KEEPALIVE_PING) /* ignore keepalive PING */
179                 return 1;
180
181         if (tx->tx_msg.ksm_zc_cookies[1] == SOCKNAL_KEEPALIVE_PING) {
182                 /* replace the keepalive PING with a real ACK */
183                 LASSERT (tx->tx_msg.ksm_zc_cookies[0] == 0);
184                 tx->tx_msg.ksm_zc_cookies[1] = cookie;
185                 return 1;
186         }
187
188         if (cookie == tx->tx_msg.ksm_zc_cookies[0] ||
189             cookie == tx->tx_msg.ksm_zc_cookies[1]) {
190                 CWARN("%s: duplicated ZC cookie: "LPU64"\n",
191                       libcfs_id2str(conn->ksnc_peer->ksnp_id), cookie);
192                 return 1; /* XXX return error in the future */
193         }
194
195         if (tx->tx_msg.ksm_zc_cookies[0] == 0) {
196                 /* NOOP tx has only one ZC-ACK cookie, can carry at least one more */
197                 if (tx->tx_msg.ksm_zc_cookies[1] > cookie) {
198                         tx->tx_msg.ksm_zc_cookies[0] = tx->tx_msg.ksm_zc_cookies[1];
199                         tx->tx_msg.ksm_zc_cookies[1] = cookie;
200                 } else {
201                         tx->tx_msg.ksm_zc_cookies[0] = cookie;
202                 }
203
204                 if (tx->tx_msg.ksm_zc_cookies[0] - tx->tx_msg.ksm_zc_cookies[1] > 2) {
205                         /* not likely to carry more ACKs, skip it to simplify logic */
206                         ksocknal_next_tx_carrier(conn);
207                 }
208
209                 return 1;
210         }
211
212         /* takes two or more cookies already */
213
214         if (tx->tx_msg.ksm_zc_cookies[0] > tx->tx_msg.ksm_zc_cookies[1]) {
215                 __u64   tmp = 0;
216
217                 /* two seperated cookies: (a+2, a) or (a+1, a) */
218                 LASSERT (tx->tx_msg.ksm_zc_cookies[0] -
219                          tx->tx_msg.ksm_zc_cookies[1] <= 2);
220
221                 if (tx->tx_msg.ksm_zc_cookies[0] -
222                     tx->tx_msg.ksm_zc_cookies[1] == 2) {
223                         if (cookie == tx->tx_msg.ksm_zc_cookies[1] + 1)
224                                 tmp = cookie;
225                 } else if (cookie == tx->tx_msg.ksm_zc_cookies[1] - 1) {
226                         tmp = tx->tx_msg.ksm_zc_cookies[1];
227                 } else if (cookie == tx->tx_msg.ksm_zc_cookies[0] + 1) {
228                         tmp = tx->tx_msg.ksm_zc_cookies[0];
229                 }
230
231                 if (tmp != 0) {
232                         /* range of cookies */
233                         tx->tx_msg.ksm_zc_cookies[0] = tmp - 1;
234                         tx->tx_msg.ksm_zc_cookies[1] = tmp + 1;
235                         return 1;
236                 }
237
238         } else {
239                 /* ksm_zc_cookies[0] < ksm_zc_cookies[1], it is range of cookies */
240                 if (cookie >= tx->tx_msg.ksm_zc_cookies[0] &&
241                     cookie <= tx->tx_msg.ksm_zc_cookies[1]) {
242                         CWARN("%s: duplicated ZC cookie: "LPU64"\n",
243                               libcfs_id2str(conn->ksnc_peer->ksnp_id), cookie);
244                         return 1; /* XXX: return error in the future */
245                 }
246
247                 if (cookie == tx->tx_msg.ksm_zc_cookies[1] + 1) {
248                         tx->tx_msg.ksm_zc_cookies[1] = cookie;
249                         return 1;
250                 }
251
252                 if (cookie == tx->tx_msg.ksm_zc_cookies[0] - 1) {
253                         tx->tx_msg.ksm_zc_cookies[0] = cookie;
254                         return 1;
255                 }
256         }
257
258         /* failed to piggyback ZC-ACK */
259         if (tx_ack != NULL) {
260                 list_add_tail(&tx_ack->tx_list, &conn->ksnc_tx_queue);
261                 /* the next tx can piggyback at least 1 ACK */
262                 ksocknal_next_tx_carrier(conn);
263         }
264
265         return 0;
266 }
267
268 static int
269 ksocknal_match_tx(ksock_conn_t *conn, ksock_tx_t *tx, int nonblk)
270 {
271         int nob;
272
273 #if SOCKNAL_VERSION_DEBUG
274         if (!*ksocknal_tunables.ksnd_typed_conns)
275                 return SOCKNAL_MATCH_YES;
276 #endif
277
278         if (tx == NULL || tx->tx_lnetmsg == NULL) {
279                 /* noop packet */
280                 nob = offsetof(ksock_msg_t, ksm_u);
281         } else {
282                 nob = tx->tx_lnetmsg->msg_len +
283                       ((conn->ksnc_proto == &ksocknal_protocol_v1x) ?
284                        sizeof(lnet_hdr_t) : sizeof(ksock_msg_t));
285         }
286
287         /* default checking for typed connection */
288         switch (conn->ksnc_type) {
289         default:
290                 CERROR("ksnc_type bad: %u\n", conn->ksnc_type);
291                 LBUG();
292         case SOCKLND_CONN_ANY:
293                 return SOCKNAL_MATCH_YES;
294
295         case SOCKLND_CONN_BULK_IN:
296                 return SOCKNAL_MATCH_MAY;
297
298         case SOCKLND_CONN_BULK_OUT:
299                 if (nob < *ksocknal_tunables.ksnd_min_bulk)
300                         return SOCKNAL_MATCH_MAY;
301                 else
302                         return SOCKNAL_MATCH_YES;
303
304         case SOCKLND_CONN_CONTROL:
305                 if (nob >= *ksocknal_tunables.ksnd_min_bulk)
306                         return SOCKNAL_MATCH_MAY;
307                 else
308                         return SOCKNAL_MATCH_YES;
309         }
310 }
311
312 static int
313 ksocknal_match_tx_v3(ksock_conn_t *conn, ksock_tx_t *tx, int nonblk)
314 {
315         int nob;
316
317         if (tx == NULL || tx->tx_lnetmsg == NULL)
318                 nob = offsetof(ksock_msg_t, ksm_u);
319         else
320                 nob = tx->tx_lnetmsg->msg_len + sizeof(ksock_msg_t);
321
322         switch (conn->ksnc_type) {
323         default:
324                 CERROR("ksnc_type bad: %u\n", conn->ksnc_type);
325                 LBUG();
326         case SOCKLND_CONN_ANY:
327                 return SOCKNAL_MATCH_NO;
328
329         case SOCKLND_CONN_ACK:
330                 if (nonblk)
331                         return SOCKNAL_MATCH_YES;
332                 else if (tx == NULL || tx->tx_lnetmsg == NULL)
333                         return SOCKNAL_MATCH_MAY;
334                 else
335                         return SOCKNAL_MATCH_NO;
336
337         case SOCKLND_CONN_BULK_OUT:
338                 if (nonblk)
339                         return SOCKNAL_MATCH_NO;
340                 else if (nob < *ksocknal_tunables.ksnd_min_bulk)
341                         return SOCKNAL_MATCH_MAY;
342                 else
343                         return SOCKNAL_MATCH_YES;
344
345         case SOCKLND_CONN_CONTROL:
346                 if (nonblk)
347                         return SOCKNAL_MATCH_NO;
348                 else if (nob >= *ksocknal_tunables.ksnd_min_bulk)
349                         return SOCKNAL_MATCH_MAY;
350                 else
351                         return SOCKNAL_MATCH_YES;
352         }
353 }
354
355 /* (Sink) handle incoming ZC request from sender */
356 static int
357 ksocknal_handle_zcreq(ksock_conn_t *c, __u64 cookie, int remote)
358 {
359         ksock_peer_t   *peer = c->ksnc_peer;
360         ksock_conn_t   *conn;
361         ksock_tx_t     *tx;
362         int             rc;
363
364         cfs_read_lock (&ksocknal_data.ksnd_global_lock);
365
366         conn = ksocknal_find_conn_locked(peer, NULL, !!remote);
367         if (conn != NULL) {
368                 ksock_sched_t *sched = conn->ksnc_scheduler;
369
370                 LASSERT (conn->ksnc_proto->pro_queue_tx_zcack != NULL);
371
372                 cfs_spin_lock_bh (&sched->kss_lock);
373
374                 rc = conn->ksnc_proto->pro_queue_tx_zcack(conn, NULL, cookie);
375
376                 cfs_spin_unlock_bh (&sched->kss_lock);
377
378                 if (rc) { /* piggybacked */
379                         read_unlock (&ksocknal_data.ksnd_global_lock);
380                         return 0;
381                 }
382         }
383
384         cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
385
386         /* ACK connection is not ready, or can't piggyback the ACK */
387         tx = ksocknal_alloc_tx_noop(cookie, !!remote);
388         if (tx == NULL)
389                 return -ENOMEM;
390
391         if ((rc = ksocknal_launch_packet(peer->ksnp_ni, tx, peer->ksnp_id)) == 0)
392                 return 0;
393
394         ksocknal_free_tx(tx);
395         return rc;
396 }
397
398 /* (Sender) handle ZC_ACK from sink */
399 static int
400 ksocknal_handle_zcack(ksock_conn_t *conn, __u64 cookie1, __u64 cookie2)
401 {
402         ksock_peer_t      *peer = conn->ksnc_peer;
403         ksock_tx_t        *tx;
404         ksock_tx_t        *tmp;
405         CFS_LIST_HEAD     (zlist);
406         int                count;
407
408         if (cookie1 == 0)
409                 cookie1 = cookie2;
410
411         count = (cookie1 > cookie2) ? 2 : (cookie2 - cookie1 + 1);
412
413         if (cookie2 == SOCKNAL_KEEPALIVE_PING &&
414             conn->ksnc_proto == &ksocknal_protocol_v3x) {
415                 /* keepalive PING for V3.x, just ignore it */
416                 return count == 1 ? 0 : -EPROTO;
417         }
418
419         cfs_spin_lock(&peer->ksnp_lock);
420
421         list_for_each_entry_safe(tx, tmp,
422                                  &peer->ksnp_zc_req_list, tx_zc_list) {
423                 __u64 c = tx->tx_msg.ksm_zc_cookies[0];
424
425                 if (c == cookie1 || c == cookie2 || (cookie1 < c && c < cookie2)) {
426                         tx->tx_msg.ksm_zc_cookies[0] = 0;
427                         list_del(&tx->tx_zc_list);
428                         list_add(&tx->tx_zc_list, &zlist);
429
430                         if (--count == 0)
431                                 break;
432                 }
433         }
434
435         cfs_spin_unlock(&peer->ksnp_lock);
436
437         while (!list_empty(&zlist)) {
438                 tx = list_entry(zlist.next, ksock_tx_t, tx_zc_list);
439                 list_del(&tx->tx_zc_list);
440                 ksocknal_tx_decref(tx);
441         }
442
443         return count == 0 ? 0 : -EPROTO;
444 }
445
446 static int
447 ksocknal_send_hello_v1 (ksock_conn_t *conn, ksock_hello_msg_t *hello)
448 {
449         cfs_socket_t        *sock = conn->ksnc_sock;
450         lnet_hdr_t          *hdr;
451         lnet_magicversion_t *hmv;
452         int                  rc;
453         int                  i;
454
455         CLASSERT(sizeof(lnet_magicversion_t) == offsetof(lnet_hdr_t, src_nid));
456
457         LIBCFS_ALLOC(hdr, sizeof(*hdr));
458         if (hdr == NULL) {
459                 CERROR("Can't allocate lnet_hdr_t\n");
460                 return -ENOMEM;
461         }
462
463         hmv = (lnet_magicversion_t *)&hdr->dest_nid;
464
465         /* Re-organize V2.x message header to V1.x (lnet_hdr_t)
466          * header and send out */
467         hmv->magic         = cpu_to_le32 (LNET_PROTO_TCP_MAGIC);
468         hmv->version_major = cpu_to_le16 (KSOCK_PROTO_V1_MAJOR);
469         hmv->version_minor = cpu_to_le16 (KSOCK_PROTO_V1_MINOR);
470
471         if (the_lnet.ln_testprotocompat != 0) {
472                 /* single-shot proto check */
473                 LNET_LOCK();
474                 if ((the_lnet.ln_testprotocompat & 1) != 0) {
475                         hmv->version_major++;   /* just different! */
476                         the_lnet.ln_testprotocompat &= ~1;
477                 }
478                 if ((the_lnet.ln_testprotocompat & 2) != 0) {
479                         hmv->magic = LNET_PROTO_MAGIC;
480                         the_lnet.ln_testprotocompat &= ~2;
481                 }
482                 LNET_UNLOCK();
483         }
484
485         hdr->src_nid        = cpu_to_le64 (hello->kshm_src_nid);
486         hdr->src_pid        = cpu_to_le32 (hello->kshm_src_pid);
487         hdr->type           = cpu_to_le32 (LNET_MSG_HELLO);
488         hdr->payload_length = cpu_to_le32 (hello->kshm_nips * sizeof(__u32));
489         hdr->msg.hello.type = cpu_to_le32 (hello->kshm_ctype);
490         hdr->msg.hello.incarnation = cpu_to_le64 (hello->kshm_src_incarnation);
491
492         rc = libcfs_sock_write(sock, hdr, sizeof(*hdr), lnet_acceptor_timeout());
493
494         if (rc != 0) {
495                 CDEBUG (D_NETERROR, "Error %d sending HELLO hdr to %u.%u.%u.%u/%d\n",
496                         rc, HIPQUAD(conn->ksnc_ipaddr), conn->ksnc_port);
497                 goto out;
498         }
499
500         if (hello->kshm_nips == 0)
501                 goto out;
502
503         for (i = 0; i < (int) hello->kshm_nips; i++) {
504                 hello->kshm_ips[i] = __cpu_to_le32 (hello->kshm_ips[i]);
505         }
506
507         rc = libcfs_sock_write(sock, hello->kshm_ips,
508                                hello->kshm_nips * sizeof(__u32),
509                                lnet_acceptor_timeout());
510         if (rc != 0) {
511                 CDEBUG (D_NETERROR, "Error %d sending HELLO payload (%d)"
512                         " to %u.%u.%u.%u/%d\n", rc, hello->kshm_nips,
513                         HIPQUAD(conn->ksnc_ipaddr), conn->ksnc_port);
514         }
515 out:
516         LIBCFS_FREE(hdr, sizeof(*hdr));
517
518         return rc;
519 }
520
521 static int
522 ksocknal_send_hello_v2 (ksock_conn_t *conn, ksock_hello_msg_t *hello)
523 {
524         cfs_socket_t   *sock = conn->ksnc_sock;
525         int             rc;
526
527         hello->kshm_magic   = LNET_PROTO_MAGIC;
528         hello->kshm_version = conn->ksnc_proto->pro_version;
529
530         if (the_lnet.ln_testprotocompat != 0) {
531                 /* single-shot proto check */
532                 LNET_LOCK();
533                 if ((the_lnet.ln_testprotocompat & 1) != 0) {
534                         hello->kshm_version++;   /* just different! */
535                         the_lnet.ln_testprotocompat &= ~1;
536                 }
537                 LNET_UNLOCK();
538         }
539
540         rc = libcfs_sock_write(sock, hello, offsetof(ksock_hello_msg_t, kshm_ips),
541                                lnet_acceptor_timeout());
542
543         if (rc != 0) {
544                 CDEBUG (D_NETERROR, "Error %d sending HELLO hdr to %u.%u.%u.%u/%d\n",
545                         rc, HIPQUAD(conn->ksnc_ipaddr), conn->ksnc_port);
546                 return rc;
547         }
548
549         if (hello->kshm_nips == 0)
550                 return 0;
551
552         rc = libcfs_sock_write(sock, hello->kshm_ips,
553                                hello->kshm_nips * sizeof(__u32),
554                                lnet_acceptor_timeout());
555         if (rc != 0) {
556                 CDEBUG (D_NETERROR, "Error %d sending HELLO payload (%d)"
557                         " to %u.%u.%u.%u/%d\n", rc, hello->kshm_nips,
558                         HIPQUAD(conn->ksnc_ipaddr), conn->ksnc_port);
559         }
560
561         return rc;
562 }
563
564 static int
565 ksocknal_recv_hello_v1(ksock_conn_t *conn, ksock_hello_msg_t *hello,int timeout)
566 {
567         cfs_socket_t        *sock = conn->ksnc_sock;
568         lnet_hdr_t          *hdr;
569         int                  rc;
570         int                  i;
571
572         LIBCFS_ALLOC(hdr, sizeof(*hdr));
573         if (hdr == NULL) {
574                 CERROR("Can't allocate lnet_hdr_t\n");
575                 return -ENOMEM;
576         }
577
578         rc = libcfs_sock_read(sock, &hdr->src_nid,
579                               sizeof (*hdr) - offsetof (lnet_hdr_t, src_nid),
580                               timeout);
581         if (rc != 0) {
582                 CERROR ("Error %d reading rest of HELLO hdr from %u.%u.%u.%u\n",
583                         rc, HIPQUAD(conn->ksnc_ipaddr));
584                 LASSERT (rc < 0 && rc != -EALREADY);
585                 goto out;
586         }
587
588         /* ...and check we got what we expected */
589         if (hdr->type != cpu_to_le32 (LNET_MSG_HELLO)) {
590                 CERROR ("Expecting a HELLO hdr,"
591                         " but got type %d from %u.%u.%u.%u\n",
592                         le32_to_cpu (hdr->type),
593                         HIPQUAD(conn->ksnc_ipaddr));
594                 rc = -EPROTO;
595                 goto out;
596         }
597
598         hello->kshm_src_nid         = le64_to_cpu (hdr->src_nid);
599         hello->kshm_src_pid         = le32_to_cpu (hdr->src_pid);
600         hello->kshm_src_incarnation = le64_to_cpu (hdr->msg.hello.incarnation);
601         hello->kshm_ctype           = le32_to_cpu (hdr->msg.hello.type);
602         hello->kshm_nips            = le32_to_cpu (hdr->payload_length) /
603                                          sizeof (__u32);
604
605         if (hello->kshm_nips > LNET_MAX_INTERFACES) {
606                 CERROR("Bad nips %d from ip %u.%u.%u.%u\n",
607                        hello->kshm_nips, HIPQUAD(conn->ksnc_ipaddr));
608                 rc = -EPROTO;
609                 goto out;
610         }
611
612         if (hello->kshm_nips == 0)
613                 goto out;
614
615         rc = libcfs_sock_read(sock, hello->kshm_ips,
616                               hello->kshm_nips * sizeof(__u32), timeout);
617         if (rc != 0) {
618                 CERROR ("Error %d reading IPs from ip %u.%u.%u.%u\n",
619                         rc, HIPQUAD(conn->ksnc_ipaddr));
620                 LASSERT (rc < 0 && rc != -EALREADY);
621                 goto out;
622         }
623
624         for (i = 0; i < (int) hello->kshm_nips; i++) {
625                 hello->kshm_ips[i] = __le32_to_cpu(hello->kshm_ips[i]);
626
627                 if (hello->kshm_ips[i] == 0) {
628                         CERROR("Zero IP[%d] from ip %u.%u.%u.%u\n",
629                                i, HIPQUAD(conn->ksnc_ipaddr));
630                         rc = -EPROTO;
631                         break;
632                 }
633         }
634 out:
635         LIBCFS_FREE(hdr, sizeof(*hdr));
636
637         return rc;
638 }
639
640 static int
641 ksocknal_recv_hello_v2 (ksock_conn_t *conn, ksock_hello_msg_t *hello, int timeout)
642 {
643         cfs_socket_t      *sock = conn->ksnc_sock;
644         int                rc;
645         int                i;
646
647         if (hello->kshm_magic == LNET_PROTO_MAGIC)
648                 conn->ksnc_flip = 0;
649         else
650                 conn->ksnc_flip = 1;
651
652         rc = libcfs_sock_read(sock, &hello->kshm_src_nid,
653                               offsetof(ksock_hello_msg_t, kshm_ips) -
654                                        offsetof(ksock_hello_msg_t, kshm_src_nid),
655                               timeout);
656         if (rc != 0) {
657                 CERROR ("Error %d reading HELLO from %u.%u.%u.%u\n",
658                         rc, HIPQUAD(conn->ksnc_ipaddr));
659                 LASSERT (rc < 0 && rc != -EALREADY);
660                 return rc;
661         }
662
663         if (conn->ksnc_flip) {
664                 __swab32s(&hello->kshm_src_pid);
665                 __swab64s(&hello->kshm_src_nid);
666                 __swab32s(&hello->kshm_dst_pid);
667                 __swab64s(&hello->kshm_dst_nid);
668                 __swab64s(&hello->kshm_src_incarnation);
669                 __swab64s(&hello->kshm_dst_incarnation);
670                 __swab32s(&hello->kshm_ctype);
671                 __swab32s(&hello->kshm_nips);
672         }
673
674         if (hello->kshm_nips > LNET_MAX_INTERFACES) {
675                 CERROR("Bad nips %d from ip %u.%u.%u.%u\n",
676                        hello->kshm_nips, HIPQUAD(conn->ksnc_ipaddr));
677                 return -EPROTO;
678         }
679
680         if (hello->kshm_nips == 0)
681                 return 0;
682
683         rc = libcfs_sock_read(sock, hello->kshm_ips,
684                               hello->kshm_nips * sizeof(__u32), timeout);
685         if (rc != 0) {
686                 CERROR ("Error %d reading IPs from ip %u.%u.%u.%u\n",
687                         rc, HIPQUAD(conn->ksnc_ipaddr));
688                 LASSERT (rc < 0 && rc != -EALREADY);
689                 return rc;
690         }
691
692         for (i = 0; i < (int) hello->kshm_nips; i++) {
693                 if (conn->ksnc_flip)
694                         __swab32s(&hello->kshm_ips[i]);
695
696                 if (hello->kshm_ips[i] == 0) {
697                         CERROR("Zero IP[%d] from ip %u.%u.%u.%u\n",
698                                i, HIPQUAD(conn->ksnc_ipaddr));
699                         return -EPROTO;
700                 }
701         }
702
703         return 0;
704 }
705
706 static void
707 ksocknal_pack_msg_v1(ksock_tx_t *tx)
708 {
709         /* V1.x has no KSOCK_MSG_NOOP */
710         LASSERT(tx->tx_msg.ksm_type != KSOCK_MSG_NOOP);
711         LASSERT(tx->tx_lnetmsg != NULL);
712
713         tx->tx_iov[0].iov_base = (void *)&tx->tx_lnetmsg->msg_hdr;
714         tx->tx_iov[0].iov_len  = sizeof(lnet_hdr_t);
715
716         tx->tx_resid = tx->tx_nob = tx->tx_lnetmsg->msg_len + sizeof(lnet_hdr_t);
717 }
718
719 static void
720 ksocknal_pack_msg_v2(ksock_tx_t *tx)
721 {
722         tx->tx_iov[0].iov_base = (void *)&tx->tx_msg;
723
724         if (tx->tx_lnetmsg != NULL) {
725                 LASSERT(tx->tx_msg.ksm_type != KSOCK_MSG_NOOP);
726
727                 tx->tx_msg.ksm_u.lnetmsg.ksnm_hdr = tx->tx_lnetmsg->msg_hdr;
728                 tx->tx_iov[0].iov_len = sizeof(ksock_msg_t);
729                 tx->tx_resid = tx->tx_nob = sizeof(ksock_msg_t) + tx->tx_lnetmsg->msg_len;
730         } else {
731                 LASSERT(tx->tx_msg.ksm_type == KSOCK_MSG_NOOP);
732
733                 tx->tx_iov[0].iov_len = offsetof(ksock_msg_t, ksm_u.lnetmsg.ksnm_hdr);
734                 tx->tx_resid = tx->tx_nob = offsetof(ksock_msg_t,  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(ksock_msg_t *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(ksock_msg_t *msg)
749 {
750         return;  /* Do nothing */
751 }
752
753 ksock_proto_t  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 ksock_proto_t  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 ksock_proto_t  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