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