Whamcloud - gitweb
b90c3d270faa7a98235776b8207bd80cfc9e02f9
[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 = sizeof(struct ksock_msg_hdr);
287         } else {
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);
292         }
293
294         /* default checking for typed connection */
295         switch (conn->ksnc_type) {
296         default:
297                 CERROR("ksnc_type bad: %u\n", conn->ksnc_type);
298                 LBUG();
299         case SOCKLND_CONN_ANY:
300                 return SOCKNAL_MATCH_YES;
301
302         case SOCKLND_CONN_BULK_IN:
303                 return SOCKNAL_MATCH_MAY;
304
305         case SOCKLND_CONN_BULK_OUT:
306                 if (nob < *ksocknal_tunables.ksnd_min_bulk)
307                         return SOCKNAL_MATCH_MAY;
308                 else
309                         return SOCKNAL_MATCH_YES;
310
311         case SOCKLND_CONN_CONTROL:
312                 if (nob >= *ksocknal_tunables.ksnd_min_bulk)
313                         return SOCKNAL_MATCH_MAY;
314                 else
315                         return SOCKNAL_MATCH_YES;
316         }
317 }
318
319 static int
320 ksocknal_match_tx_v3(struct ksock_conn *conn, struct ksock_tx *tx, int nonblk)
321 {
322         int nob;
323
324         if (tx == NULL || tx->tx_lnetmsg == NULL)
325                 nob = sizeof(struct ksock_msg_hdr);
326         else
327                 nob = sizeof(struct ksock_msg_hdr) +
328                         sizeof(struct lnet_hdr_nid4) +
329                         tx->tx_lnetmsg->msg_len;
330
331         switch (conn->ksnc_type) {
332         default:
333                 CERROR("ksnc_type bad: %u\n", conn->ksnc_type);
334                 LBUG();
335         case SOCKLND_CONN_ANY:
336                 return SOCKNAL_MATCH_NO;
337
338         case SOCKLND_CONN_ACK:
339                 if (nonblk)
340                         return SOCKNAL_MATCH_YES;
341                 else if (tx == NULL || tx->tx_lnetmsg == NULL)
342                         return SOCKNAL_MATCH_MAY;
343                 else
344                         return SOCKNAL_MATCH_NO;
345
346         case SOCKLND_CONN_BULK_OUT:
347                 if (nonblk)
348                         return SOCKNAL_MATCH_NO;
349                 else if (nob < *ksocknal_tunables.ksnd_min_bulk)
350                         return SOCKNAL_MATCH_MAY;
351                 else
352                         return SOCKNAL_MATCH_YES;
353
354         case SOCKLND_CONN_CONTROL:
355                 if (nonblk)
356                         return SOCKNAL_MATCH_NO;
357                 else if (nob >= *ksocknal_tunables.ksnd_min_bulk)
358                         return SOCKNAL_MATCH_MAY;
359                 else
360                         return SOCKNAL_MATCH_YES;
361         }
362 }
363
364 static int
365 ksocknal_match_tx_v4(struct ksock_conn *conn, struct ksock_tx *tx, int nonblk)
366 {
367         int nob;
368
369         if (!tx || !tx->tx_lnetmsg)
370                 nob = sizeof(struct ksock_msg_hdr);
371         else
372                 nob = sizeof(struct ksock_msg_hdr) +
373                         sizeof(struct lnet_hdr_nid16) +
374                         tx->tx_lnetmsg->msg_len;
375
376         switch (conn->ksnc_type) {
377         default:
378                 CERROR("ksnc_type bad: %u\n", conn->ksnc_type);
379                 LBUG();
380         case SOCKLND_CONN_ANY:
381                 return SOCKNAL_MATCH_NO;
382
383         case SOCKLND_CONN_ACK:
384                 if (nonblk)
385                         return SOCKNAL_MATCH_YES;
386                 else if (tx == NULL || tx->tx_lnetmsg == NULL)
387                         return SOCKNAL_MATCH_MAY;
388                 else
389                         return SOCKNAL_MATCH_NO;
390
391         case SOCKLND_CONN_BULK_OUT:
392                 if (nonblk)
393                         return SOCKNAL_MATCH_NO;
394                 else if (nob < *ksocknal_tunables.ksnd_min_bulk)
395                         return SOCKNAL_MATCH_MAY;
396                 else
397                         return SOCKNAL_MATCH_YES;
398
399         case SOCKLND_CONN_CONTROL:
400                 if (nonblk)
401                         return SOCKNAL_MATCH_NO;
402                 else if (nob >= *ksocknal_tunables.ksnd_min_bulk)
403                         return SOCKNAL_MATCH_MAY;
404                 else
405                         return SOCKNAL_MATCH_YES;
406         }
407 }
408
409 /* (Sink) handle incoming ZC request from sender */
410 static int
411 ksocknal_handle_zcreq(struct ksock_conn *c, __u64 cookie, int remote)
412 {
413         struct ksock_peer_ni *peer_ni = c->ksnc_peer;
414         struct ksock_conn *conn;
415         struct ksock_tx *tx;
416         int rc;
417
418         read_lock(&ksocknal_data.ksnd_global_lock);
419
420         conn = ksocknal_find_conn_locked(peer_ni, NULL, !!remote);
421         if (conn != NULL) {
422                 struct ksock_sched *sched = conn->ksnc_scheduler;
423
424                 LASSERT(conn->ksnc_proto->pro_queue_tx_zcack != NULL);
425
426                 spin_lock_bh(&sched->kss_lock);
427
428                 rc = conn->ksnc_proto->pro_queue_tx_zcack(conn, NULL, cookie);
429
430                 spin_unlock_bh(&sched->kss_lock);
431
432                 if (rc) { /* piggybacked */
433                         read_unlock(&ksocknal_data.ksnd_global_lock);
434                         return 0;
435                 }
436         }
437
438         read_unlock(&ksocknal_data.ksnd_global_lock);
439
440         /* ACK connection is not ready, or can't piggyback the ACK */
441         tx = ksocknal_alloc_tx_noop(cookie, !!remote);
442         if (tx == NULL)
443                 return -ENOMEM;
444
445         rc = ksocknal_launch_packet(peer_ni->ksnp_ni, tx, &peer_ni->ksnp_id);
446         if (rc == 0)
447                 return 0;
448
449         ksocknal_free_tx(tx);
450         return rc;
451 }
452
453 /* (Sender) handle ZC_ACK from sink */
454 static int
455 ksocknal_handle_zcack(struct ksock_conn *conn, __u64 cookie1, __u64 cookie2)
456 {
457         struct ksock_peer_ni *peer_ni = conn->ksnc_peer;
458         struct ksock_tx *tx;
459         struct ksock_tx *tmp;
460         LIST_HEAD(zlist);
461         int count;
462
463         if (cookie1 == 0)
464                 cookie1 = cookie2;
465
466         count = (cookie1 > cookie2) ? 2 : (cookie2 - cookie1 + 1);
467
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;
473         }
474
475         spin_lock(&peer_ni->ksnp_lock);
476
477         list_for_each_entry_safe(tx, tmp, &peer_ni->ksnp_zc_req_list,
478                                  tx_zc_list) {
479                 __u64 c = tx->tx_msg.ksm_zc_cookies[0];
480
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);
484
485                         if (--count == 0)
486                                 break;
487                 }
488         }
489
490         spin_unlock(&peer_ni->ksnp_lock);
491
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);
496         }
497
498         return count == 0 ? 0 : -EPROTO;
499 }
500
501 static int
502 ksocknal_send_hello_v1(struct ksock_conn *conn, struct ksock_hello_msg *hello)
503 {
504         struct socket *sock = conn->ksnc_sock;
505         struct _lnet_hdr_nid4 *hdr;
506         struct lnet_magicversion *hmv;
507         int rc;
508         int i;
509
510         BUILD_BUG_ON(sizeof(struct lnet_magicversion) !=
511                      offsetof(struct _lnet_hdr_nid4, src_nid));
512
513         LIBCFS_ALLOC(hdr, sizeof(*hdr));
514         if (hdr == NULL) {
515                 CERROR("Can't allocate struct lnet_hdr_nid4\n");
516                 return -ENOMEM;
517         }
518
519         hmv = (struct lnet_magicversion *)&hdr->dest_nid;
520
521         /* Re-organize V2.x message header to V1.x (struct lnet_hdr_nid4)
522          * header and send out
523          */
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);
527
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! */
532
533                 if (test_and_clear_bit(1, &the_lnet.ln_testprotocompat))
534                         hmv->magic = LNET_PROTO_MAGIC;
535         }
536
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);
543
544         rc = lnet_sock_write(sock, hdr, sizeof(*hdr), lnet_acceptor_timeout());
545         if (rc != 0) {
546                 CNETERR("Error %d sending HELLO hdr to %pIScp\n",
547                         rc, &conn->ksnc_peeraddr);
548                 goto out;
549         }
550
551         if (hello->kshm_nips == 0)
552                 goto out;
553
554         for (i = 0; i < (int) hello->kshm_nips; i++)
555                 hello->kshm_ips[i] = __cpu_to_le32 (hello->kshm_ips[i]);
556
557         rc = lnet_sock_write(sock, hello->kshm_ips,
558                              hello->kshm_nips * sizeof(__u32),
559                              lnet_acceptor_timeout());
560         if (rc != 0) {
561                 CNETERR("Error %d sending HELLO payload (%d) to %pIScp\n",
562                         rc, hello->kshm_nips,
563                         &conn->ksnc_peeraddr);
564         }
565 out:
566         LIBCFS_FREE(hdr, sizeof(*hdr));
567
568         return rc;
569 }
570
571 static int
572 ksocknal_send_hello_v2(struct ksock_conn *conn, struct ksock_hello_msg *hello)
573 {
574         struct socket *sock = conn->ksnc_sock;
575         int rc;
576         struct ksock_hello_msg_nid4 *hello4;
577
578         CFS_ALLOC_PTR(hello4);
579         if (!hello4) {
580                 CERROR("Can't allocate struct ksock_hello_msg_nid4\n");
581                 return -ENOMEM;
582         }
583
584         hello->kshm_magic = LNET_PROTO_MAGIC;
585         hello->kshm_version = conn->ksnc_proto->pro_version;
586
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;
597
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! */
602         }
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;
613
614         rc = lnet_sock_write(sock, hello4, sizeof(*hello4),
615                              lnet_acceptor_timeout());
616         CFS_FREE_PTR(hello4);
617         if (rc) {
618                 CNETERR("Error %d sending HELLO hdr to %pIScp\n",
619                         rc, &conn->ksnc_peeraddr);
620                 return rc;
621         }
622
623         if (hello->kshm_nips == 0)
624                 return 0;
625
626         rc = lnet_sock_write(sock, hello->kshm_ips,
627                              hello->kshm_nips * sizeof(__u32),
628                              lnet_acceptor_timeout());
629         if (rc != 0) {
630                 CNETERR("Error %d sending HELLO payload (%d) to %pIScp\n", rc,
631                         hello->kshm_nips,
632                         &conn->ksnc_peeraddr);
633         }
634
635         return rc;
636 }
637
638 static int
639 ksocknal_send_hello_v4(struct ksock_conn *conn, struct ksock_hello_msg *hello)
640 {
641         struct socket *sock = conn->ksnc_sock;
642         int rc;
643
644         hello->kshm_magic   = LNET_PROTO_MAGIC;
645         hello->kshm_version = conn->ksnc_proto->pro_version;
646
647         rc = lnet_sock_write(sock, hello, sizeof(*hello),
648                              lnet_acceptor_timeout());
649
650         if (rc != 0)
651                 CNETERR("Error %d sending HELLO hdr to %pIScp\n",
652                         rc, &conn->ksnc_peeraddr);
653         return rc;
654 }
655
656 static int
657 ksocknal_recv_hello_v1(struct ksock_conn *conn, struct ksock_hello_msg *hello,
658                        int timeout)
659 {
660         struct socket *sock = conn->ksnc_sock;
661         struct _lnet_hdr_nid4 *hdr;
662         int rc;
663         int i;
664
665         CFS_ALLOC_PTR(hdr);
666         if (!hdr) {
667                 CERROR("Can't allocate struct lnet_hdr_nid4\n");
668                 return -ENOMEM;
669         }
670
671         rc = lnet_sock_read(sock, &hdr->src_nid,
672                             sizeof(*hdr) - offsetof(struct _lnet_hdr_nid4,
673                                                     src_nid),
674                             timeout);
675         if (rc != 0) {
676                 CERROR("Error %d reading rest of HELLO hdr from %pISc\n",
677                        rc, &conn->ksnc_peeraddr);
678                 LASSERT(rc < 0 && rc != -EALREADY);
679                 goto out;
680         }
681
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 %pISc\n",
685                        le32_to_cpu(hdr->type),
686                        &conn->ksnc_peeraddr);
687                 rc = -EPROTO;
688                 goto out;
689         }
690
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);
696
697         if (hello->kshm_nips > LNET_INTERFACES_NUM) {
698                 CERROR("Bad nips %d from ip %pISc\n",
699                        hello->kshm_nips, &conn->ksnc_peeraddr);
700                 rc = -EPROTO;
701                 goto out;
702         }
703
704         if (hello->kshm_nips == 0)
705                 goto out;
706
707         rc = lnet_sock_read(sock, hello->kshm_ips,
708                             hello->kshm_nips * sizeof(__u32), timeout);
709         if (rc != 0) {
710                 CERROR("Error %d reading IPs from ip %pISc\n",
711                        rc, &conn->ksnc_peeraddr);
712                 LASSERT(rc < 0 && rc != -EALREADY);
713                 goto out;
714         }
715
716         for (i = 0; i < (int) hello->kshm_nips; i++) {
717                 hello->kshm_ips[i] = __le32_to_cpu(hello->kshm_ips[i]);
718
719                 if (hello->kshm_ips[i] == 0) {
720                         CERROR("Zero IP[%d] from ip %pISc\n",
721                                i, &conn->ksnc_peeraddr);
722                         rc = -EPROTO;
723                         break;
724                 }
725         }
726 out:
727         CFS_FREE_PTR(hdr);
728
729         return rc;
730 }
731
732 static int
733 ksocknal_recv_hello_v2(struct ksock_conn *conn, struct ksock_hello_msg *hello,
734                        int timeout)
735 {
736         struct socket *sock = conn->ksnc_sock;
737         struct ksock_hello_msg_nid4 *hello4 = (void *)hello;
738         int rc;
739         int i;
740
741         if (hello->kshm_magic == LNET_PROTO_MAGIC)
742                 conn->ksnc_flip = 0;
743         else
744                 conn->ksnc_flip = 1;
745
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),
749                             timeout);
750         if (rc != 0) {
751                 CERROR("Error %d reading HELLO from %pISc\n",
752                        rc, &conn->ksnc_peeraddr);
753                 LASSERT(rc < 0 && rc != -EALREADY);
754                 return rc;
755         }
756
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);
767         } else {
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);
777         }
778
779         if (hello->kshm_nips > LNET_INTERFACES_NUM) {
780                 CERROR("Bad nips %d from ip %pISc\n",
781                        hello->kshm_nips, &conn->ksnc_peeraddr);
782                 return -EPROTO;
783         }
784
785         if (hello->kshm_nips == 0)
786                 return 0;
787
788         rc = lnet_sock_read(sock, hello->kshm_ips,
789                             hello->kshm_nips * sizeof(__u32), timeout);
790         if (rc != 0) {
791                 CERROR("Error %d reading IPs from ip %pISc\n",
792                        rc, &conn->ksnc_peeraddr);
793                 LASSERT(rc < 0 && rc != -EALREADY);
794                 return rc;
795         }
796
797         for (i = 0; i < (int) hello->kshm_nips; i++) {
798                 if (conn->ksnc_flip)
799                         __swab32s(&hello->kshm_ips[i]);
800
801                 if (hello->kshm_ips[i] == 0) {
802                         CERROR("Zero IP[%d] from ip %pISc\n",
803                                i, &conn->ksnc_peeraddr);
804                         return -EPROTO;
805                 }
806         }
807
808         return 0;
809 }
810
811 static int
812 ksocknal_recv_hello_v4(struct ksock_conn *conn, struct ksock_hello_msg *hello,
813                        int timeout)
814 {
815         struct socket *sock = conn->ksnc_sock;
816         int rc;
817
818         if (hello->kshm_magic == LNET_PROTO_MAGIC)
819                 conn->ksnc_flip = 0;
820         else
821                 conn->ksnc_flip = 1;
822
823         rc = lnet_sock_read(sock, &hello->kshm_src_nid,
824                             sizeof(*hello) -
825                             offsetof(struct ksock_hello_msg, kshm_src_nid),
826                             timeout);
827         if (rc) {
828                 CERROR("Error %d reading HELLO from %pISc\n",
829                        rc, &conn->ksnc_peeraddr);
830                 LASSERT(rc < 0 && rc != -EALREADY);
831                 return rc;
832         }
833
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);
840         }
841
842         return 0;
843 }
844
845 static void
846 ksocknal_pack_msg_v1(struct ksock_tx *tx)
847 {
848         /* V1.x has no KSOCK_MSG_NOOP */
849         LASSERT(tx->tx_msg.ksm_type != KSOCK_MSG_NOOP);
850         LASSERT(tx->tx_lnetmsg != NULL);
851
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);
856
857         tx->tx_nob = tx->tx_lnetmsg->msg_len + sizeof(struct lnet_hdr_nid4);
858         tx->tx_resid = tx->tx_nob;
859 }
860
861 static void
862 ksocknal_pack_msg_v2(struct ksock_tx *tx)
863 {
864         int hdr_size;
865
866         tx->tx_hdr.iov_base = (void *)&tx->tx_msg;
867
868         switch (tx->tx_msg.ksm_type) {
869         case KSOCK_MSG_LNET:
870                 LASSERT(tx->tx_lnetmsg != NULL);
871                 hdr_size = (sizeof(struct ksock_msg_hdr) +
872                                 sizeof(struct lnet_hdr_nid4));
873
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;
878                 break;
879         case KSOCK_MSG_NOOP:
880                 LASSERT(tx->tx_lnetmsg == NULL);
881                 hdr_size = sizeof(struct ksock_msg_hdr);
882
883                 tx->tx_hdr.iov_len = hdr_size;
884                 tx->tx_resid = tx->tx_nob = hdr_size;
885                 break;
886         default:
887                 LASSERT(0);
888         }
889         /* Don't checksum before start sending, because packet can be
890          * piggybacked with ACK
891          */
892 }
893
894 static void
895 ksocknal_pack_msg_v4(struct ksock_tx *tx)
896 {
897         int hdr_size;
898
899         tx->tx_hdr.iov_base = (void *)&tx->tx_msg;
900
901         switch (tx->tx_msg.ksm_type) {
902         case KSOCK_MSG_LNET:
903                 LASSERT(tx->tx_lnetmsg != NULL);
904                 hdr_size = (sizeof(struct ksock_msg_hdr) +
905                                 sizeof(struct lnet_hdr_nid16));
906
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;
911                 break;
912         case KSOCK_MSG_NOOP:
913                 LASSERT(tx->tx_lnetmsg == NULL);
914                 hdr_size = sizeof(struct ksock_msg_hdr);
915
916                 tx->tx_hdr.iov_len = hdr_size;
917                 tx->tx_resid = tx->tx_nob = hdr_size;
918                 break;
919         default:
920                 LASSERT(0);
921         }
922         /* Don't checksum before start sending, because packet can be
923          * piggybacked with ACK
924          */
925 }
926
927 static void
928 ksocknal_unpack_msg_v1(struct ksock_msg *msg, struct lnet_hdr *hdr)
929 {
930         msg->ksm_csum           = 0;
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);
934 }
935
936 static void
937 ksocknal_unpack_msg_v2(struct ksock_msg *msg, struct lnet_hdr *hdr)
938 {
939         lnet_hdr_from_nid4(hdr, &msg->ksm_u.lnetmsg_nid4);
940 }
941
942 static void
943 ksocknal_unpack_msg_v4(struct ksock_msg *msg, struct lnet_hdr *hdr)
944 {
945         lnet_hdr_from_nid16(hdr, &msg->ksm_u.lnetmsg_nid16);
946 }
947
948 const struct ksock_proto ksocknal_protocol_v1x =
949 {
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
960 };
961
962 const struct ksock_proto ksocknal_protocol_v2x =
963 {
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
974 };
975
976 const struct ksock_proto ksocknal_protocol_v3x =
977 {
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
988 };
989
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,
1001 };