GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / staging / lustre / lnet / klnds / socklnd / socklnd_lib.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2012, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #include "socklnd.h"
34
35 int
36 ksocknal_lib_get_conn_addrs(struct ksock_conn *conn)
37 {
38         int rc = lnet_sock_getaddr(conn->ksnc_sock, 1, &conn->ksnc_ipaddr,
39                                    &conn->ksnc_port);
40
41         /* Didn't need the {get,put}connsock dance to deref ksnc_sock... */
42         LASSERT(!conn->ksnc_closing);
43
44         if (rc) {
45                 CERROR("Error %d getting sock peer IP\n", rc);
46                 return rc;
47         }
48
49         rc = lnet_sock_getaddr(conn->ksnc_sock, 0, &conn->ksnc_myipaddr, NULL);
50         if (rc) {
51                 CERROR("Error %d getting sock local IP\n", rc);
52                 return rc;
53         }
54
55         return 0;
56 }
57
58 int
59 ksocknal_lib_zc_capable(struct ksock_conn *conn)
60 {
61         int caps = conn->ksnc_sock->sk->sk_route_caps;
62
63         if (conn->ksnc_proto == &ksocknal_protocol_v1x)
64                 return 0;
65
66         /*
67          * ZC if the socket supports scatter/gather and doesn't need software
68          * checksums
69          */
70         return ((caps & NETIF_F_SG) && (caps & NETIF_F_CSUM_MASK));
71 }
72
73 int
74 ksocknal_lib_send_iov(struct ksock_conn *conn, struct ksock_tx *tx)
75 {
76         struct msghdr msg = {.msg_flags = MSG_DONTWAIT};
77         struct socket *sock = conn->ksnc_sock;
78         int nob, i;
79
80         if (*ksocknal_tunables.ksnd_enable_csum && /* checksum enabled */
81             conn->ksnc_proto == &ksocknal_protocol_v2x && /* V2.x connection  */
82             tx->tx_nob == tx->tx_resid           && /* frist sending    */
83             !tx->tx_msg.ksm_csum)                    /* not checksummed  */
84                 ksocknal_lib_csum_tx(tx);
85
86         for (nob = i = 0; i < tx->tx_niov; i++)
87                 nob += tx->tx_iov[i].iov_len;
88
89         if (!list_empty(&conn->ksnc_tx_queue) ||
90             nob < tx->tx_resid)
91                 msg.msg_flags |= MSG_MORE;
92
93         iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC,
94                       tx->tx_iov, tx->tx_niov, nob);
95         return sock_sendmsg(sock, &msg);
96 }
97
98 int
99 ksocknal_lib_send_kiov(struct ksock_conn *conn, struct ksock_tx *tx)
100 {
101         struct socket *sock = conn->ksnc_sock;
102         struct bio_vec *kiov = tx->tx_kiov;
103         int rc;
104         int nob;
105
106         /* Not NOOP message */
107         LASSERT(tx->tx_lnetmsg);
108
109         if (tx->tx_msg.ksm_zc_cookies[0]) {
110                 /* Zero copy is enabled */
111                 struct sock *sk = sock->sk;
112                 struct page *page = kiov->bv_page;
113                 int offset = kiov->bv_offset;
114                 int fragsize = kiov->bv_len;
115                 int msgflg = MSG_DONTWAIT;
116
117                 CDEBUG(D_NET, "page %p + offset %x for %d\n",
118                        page, offset, kiov->bv_len);
119
120                 if (!list_empty(&conn->ksnc_tx_queue) ||
121                     fragsize < tx->tx_resid)
122                         msgflg |= MSG_MORE;
123
124                 if (sk->sk_prot->sendpage) {
125                         rc = sk->sk_prot->sendpage(sk, page,
126                                                    offset, fragsize, msgflg);
127                 } else {
128                         rc = tcp_sendpage(sk, page, offset, fragsize, msgflg);
129                 }
130         } else {
131                 struct msghdr msg = {.msg_flags = MSG_DONTWAIT};
132                 int i;
133
134                 for (nob = i = 0; i < tx->tx_nkiov; i++)
135                         nob += kiov[i].bv_len;
136
137                 if (!list_empty(&conn->ksnc_tx_queue) ||
138                     nob < tx->tx_resid)
139                         msg.msg_flags |= MSG_MORE;
140
141                 iov_iter_bvec(&msg.msg_iter, WRITE | ITER_BVEC,
142                               kiov, tx->tx_nkiov, nob);
143                 rc = sock_sendmsg(sock, &msg);
144         }
145         return rc;
146 }
147
148 void
149 ksocknal_lib_eager_ack(struct ksock_conn *conn)
150 {
151         int opt = 1;
152         struct socket *sock = conn->ksnc_sock;
153
154         /*
155          * Remind the socket to ACK eagerly.  If I don't, the socket might
156          * think I'm about to send something it could piggy-back the ACK
157          * on, introducing delay in completing zero-copy sends in my
158          * peer.
159          */
160         kernel_setsockopt(sock, SOL_TCP, TCP_QUICKACK, (char *)&opt,
161                           sizeof(opt));
162 }
163
164 int
165 ksocknal_lib_recv_iov(struct ksock_conn *conn)
166 {
167         unsigned int niov = conn->ksnc_rx_niov;
168         struct kvec *iov = conn->ksnc_rx_iov;
169         struct msghdr msg = {
170                 .msg_flags = 0
171         };
172         int nob;
173         int i;
174         int rc;
175         int fragnob;
176         int sum;
177         __u32 saved_csum;
178
179         LASSERT(niov > 0);
180
181         for (nob = i = 0; i < niov; i++)
182                 nob += iov[i].iov_len;
183
184         LASSERT(nob <= conn->ksnc_rx_nob_wanted);
185
186         iov_iter_kvec(&msg.msg_iter, READ | ITER_KVEC, iov, niov, nob);
187         rc = sock_recvmsg(conn->ksnc_sock, &msg, MSG_DONTWAIT);
188
189         saved_csum = 0;
190         if (conn->ksnc_proto == &ksocknal_protocol_v2x) {
191                 saved_csum = conn->ksnc_msg.ksm_csum;
192                 conn->ksnc_msg.ksm_csum = 0;
193         }
194
195         if (saved_csum) {
196                 /* accumulate checksum */
197                 for (i = 0, sum = rc; sum > 0; i++, sum -= fragnob) {
198                         LASSERT(i < niov);
199
200                         fragnob = iov[i].iov_len;
201                         if (fragnob > sum)
202                                 fragnob = sum;
203
204                         conn->ksnc_rx_csum = crc32_le(conn->ksnc_rx_csum,
205                                                       iov[i].iov_base,
206                                                       fragnob);
207                 }
208                 conn->ksnc_msg.ksm_csum = saved_csum;
209         }
210
211         return rc;
212 }
213
214 int
215 ksocknal_lib_recv_kiov(struct ksock_conn *conn)
216 {
217         unsigned int niov = conn->ksnc_rx_nkiov;
218         struct bio_vec *kiov = conn->ksnc_rx_kiov;
219         struct msghdr msg = {
220                 .msg_flags = 0
221         };
222         int nob;
223         int i;
224         int rc;
225         void *base;
226         int sum;
227         int fragnob;
228
229         for (nob = i = 0; i < niov; i++)
230                 nob += kiov[i].bv_len;
231
232         LASSERT(nob <= conn->ksnc_rx_nob_wanted);
233
234         iov_iter_bvec(&msg.msg_iter, READ | ITER_BVEC, kiov, niov, nob);
235         rc = sock_recvmsg(conn->ksnc_sock, &msg, MSG_DONTWAIT);
236
237         if (conn->ksnc_msg.ksm_csum) {
238                 for (i = 0, sum = rc; sum > 0; i++, sum -= fragnob) {
239                         LASSERT(i < niov);
240
241                         base = kmap(kiov[i].bv_page) + kiov[i].bv_offset;
242                         fragnob = kiov[i].bv_len;
243                         if (fragnob > sum)
244                                 fragnob = sum;
245
246                         conn->ksnc_rx_csum = crc32_le(conn->ksnc_rx_csum,
247                                                       base, fragnob);
248
249                         kunmap(kiov[i].bv_page);
250                 }
251         }
252         return rc;
253 }
254
255 void
256 ksocknal_lib_csum_tx(struct ksock_tx *tx)
257 {
258         int i;
259         __u32 csum;
260         void *base;
261
262         LASSERT(tx->tx_iov[0].iov_base == &tx->tx_msg);
263         LASSERT(tx->tx_conn);
264         LASSERT(tx->tx_conn->ksnc_proto == &ksocknal_protocol_v2x);
265
266         tx->tx_msg.ksm_csum = 0;
267
268         csum = crc32_le(~0, tx->tx_iov[0].iov_base,
269                         tx->tx_iov[0].iov_len);
270
271         if (tx->tx_kiov) {
272                 for (i = 0; i < tx->tx_nkiov; i++) {
273                         base = kmap(tx->tx_kiov[i].bv_page) +
274                                tx->tx_kiov[i].bv_offset;
275
276                         csum = crc32_le(csum, base, tx->tx_kiov[i].bv_len);
277
278                         kunmap(tx->tx_kiov[i].bv_page);
279                 }
280         } else {
281                 for (i = 1; i < tx->tx_niov; i++)
282                         csum = crc32_le(csum, tx->tx_iov[i].iov_base,
283                                         tx->tx_iov[i].iov_len);
284         }
285
286         if (*ksocknal_tunables.ksnd_inject_csum_error) {
287                 csum++;
288                 *ksocknal_tunables.ksnd_inject_csum_error = 0;
289         }
290
291         tx->tx_msg.ksm_csum = csum;
292 }
293
294 int
295 ksocknal_lib_get_conn_tunables(struct ksock_conn *conn, int *txmem,
296                                int *rxmem, int *nagle)
297 {
298         struct socket *sock = conn->ksnc_sock;
299         int len;
300         int rc;
301
302         rc = ksocknal_connsock_addref(conn);
303         if (rc) {
304                 LASSERT(conn->ksnc_closing);
305                 *txmem = *rxmem = *nagle = 0;
306                 return -ESHUTDOWN;
307         }
308
309         rc = lnet_sock_getbuf(sock, txmem, rxmem);
310         if (!rc) {
311                 len = sizeof(*nagle);
312                 rc = kernel_getsockopt(sock, SOL_TCP, TCP_NODELAY,
313                                        (char *)nagle, &len);
314         }
315
316         ksocknal_connsock_decref(conn);
317
318         if (!rc)
319                 *nagle = !*nagle;
320         else
321                 *txmem = *rxmem = *nagle = 0;
322
323         return rc;
324 }
325
326 int
327 ksocknal_lib_setup_sock(struct socket *sock)
328 {
329         int rc;
330         int option;
331         int keep_idle;
332         int keep_intvl;
333         int keep_count;
334         int do_keepalive;
335         struct linger linger;
336
337         sock->sk->sk_allocation = GFP_NOFS;
338
339         /*
340          * Ensure this socket aborts active sends immediately when we close
341          * it.
342          */
343         linger.l_onoff = 0;
344         linger.l_linger = 0;
345
346         rc = kernel_setsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&linger,
347                                sizeof(linger));
348         if (rc) {
349                 CERROR("Can't set SO_LINGER: %d\n", rc);
350                 return rc;
351         }
352
353         option = -1;
354         rc = kernel_setsockopt(sock, SOL_TCP, TCP_LINGER2, (char *)&option,
355                                sizeof(option));
356         if (rc) {
357                 CERROR("Can't set SO_LINGER2: %d\n", rc);
358                 return rc;
359         }
360
361         if (!*ksocknal_tunables.ksnd_nagle) {
362                 option = 1;
363
364                 rc = kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY,
365                                        (char *)&option, sizeof(option));
366                 if (rc) {
367                         CERROR("Can't disable nagle: %d\n", rc);
368                         return rc;
369                 }
370         }
371
372         rc = lnet_sock_setbuf(sock, *ksocknal_tunables.ksnd_tx_buffer_size,
373                               *ksocknal_tunables.ksnd_rx_buffer_size);
374         if (rc) {
375                 CERROR("Can't set buffer tx %d, rx %d buffers: %d\n",
376                        *ksocknal_tunables.ksnd_tx_buffer_size,
377                        *ksocknal_tunables.ksnd_rx_buffer_size, rc);
378                 return rc;
379         }
380
381 /* TCP_BACKOFF_* sockopt tunables unsupported in stock kernels */
382
383         /* snapshot tunables */
384         keep_idle  = *ksocknal_tunables.ksnd_keepalive_idle;
385         keep_count = *ksocknal_tunables.ksnd_keepalive_count;
386         keep_intvl = *ksocknal_tunables.ksnd_keepalive_intvl;
387
388         do_keepalive = (keep_idle > 0 && keep_count > 0 && keep_intvl > 0);
389
390         option = (do_keepalive ? 1 : 0);
391         rc = kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char *)&option,
392                                sizeof(option));
393         if (rc) {
394                 CERROR("Can't set SO_KEEPALIVE: %d\n", rc);
395                 return rc;
396         }
397
398         if (!do_keepalive)
399                 return 0;
400
401         rc = kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE, (char *)&keep_idle,
402                                sizeof(keep_idle));
403         if (rc) {
404                 CERROR("Can't set TCP_KEEPIDLE: %d\n", rc);
405                 return rc;
406         }
407
408         rc = kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
409                                (char *)&keep_intvl, sizeof(keep_intvl));
410         if (rc) {
411                 CERROR("Can't set TCP_KEEPINTVL: %d\n", rc);
412                 return rc;
413         }
414
415         rc = kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT, (char *)&keep_count,
416                                sizeof(keep_count));
417         if (rc) {
418                 CERROR("Can't set TCP_KEEPCNT: %d\n", rc);
419                 return rc;
420         }
421
422         return 0;
423 }
424
425 void
426 ksocknal_lib_push_conn(struct ksock_conn *conn)
427 {
428         struct sock *sk;
429         struct tcp_sock *tp;
430         int nonagle;
431         int val = 1;
432         int rc;
433
434         rc = ksocknal_connsock_addref(conn);
435         if (rc)                     /* being shut down */
436                 return;
437
438         sk = conn->ksnc_sock->sk;
439         tp = tcp_sk(sk);
440
441         lock_sock(sk);
442         nonagle = tp->nonagle;
443         tp->nonagle = 1;
444         release_sock(sk);
445
446         rc = kernel_setsockopt(conn->ksnc_sock, SOL_TCP, TCP_NODELAY,
447                                (char *)&val, sizeof(val));
448         LASSERT(!rc);
449
450         lock_sock(sk);
451         tp->nonagle = nonagle;
452         release_sock(sk);
453
454         ksocknal_connsock_decref(conn);
455 }
456
457 /*
458  * socket call back in Linux
459  */
460 static void
461 ksocknal_data_ready(struct sock *sk)
462 {
463         struct ksock_conn *conn;
464
465         /* interleave correctly with closing sockets... */
466         LASSERT(!in_irq());
467         read_lock(&ksocknal_data.ksnd_global_lock);
468
469         conn = sk->sk_user_data;
470         if (!conn) {         /* raced with ksocknal_terminate_conn */
471                 LASSERT(sk->sk_data_ready != &ksocknal_data_ready);
472                 sk->sk_data_ready(sk);
473         } else {
474                 ksocknal_read_callback(conn);
475         }
476
477         read_unlock(&ksocknal_data.ksnd_global_lock);
478 }
479
480 static void
481 ksocknal_write_space(struct sock *sk)
482 {
483         struct ksock_conn *conn;
484         int wspace;
485         int min_wpace;
486
487         /* interleave correctly with closing sockets... */
488         LASSERT(!in_irq());
489         read_lock(&ksocknal_data.ksnd_global_lock);
490
491         conn = sk->sk_user_data;
492         wspace = sk_stream_wspace(sk);
493         min_wpace = sk_stream_min_wspace(sk);
494
495         CDEBUG(D_NET, "sk %p wspace %d low water %d conn %p%s%s%s\n",
496                sk, wspace, min_wpace, conn,
497                !conn ? "" : (conn->ksnc_tx_ready ?
498                                       " ready" : " blocked"),
499                !conn ? "" : (conn->ksnc_tx_scheduled ?
500                                       " scheduled" : " idle"),
501                !conn ? "" : (list_empty(&conn->ksnc_tx_queue) ?
502                                       " empty" : " queued"));
503
504         if (!conn) {         /* raced with ksocknal_terminate_conn */
505                 LASSERT(sk->sk_write_space != &ksocknal_write_space);
506                 sk->sk_write_space(sk);
507
508                 read_unlock(&ksocknal_data.ksnd_global_lock);
509                 return;
510         }
511
512         if (wspace >= min_wpace) {            /* got enough space */
513                 ksocknal_write_callback(conn);
514
515                 /*
516                  * Clear SOCK_NOSPACE _after_ ksocknal_write_callback so the
517                  * ENOMEM check in ksocknal_transmit is race-free (think about
518                  * it).
519                  */
520                 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
521         }
522
523         read_unlock(&ksocknal_data.ksnd_global_lock);
524 }
525
526 void
527 ksocknal_lib_save_callback(struct socket *sock, struct ksock_conn *conn)
528 {
529         conn->ksnc_saved_data_ready = sock->sk->sk_data_ready;
530         conn->ksnc_saved_write_space = sock->sk->sk_write_space;
531 }
532
533 void
534 ksocknal_lib_set_callback(struct socket *sock,  struct ksock_conn *conn)
535 {
536         sock->sk->sk_user_data = conn;
537         sock->sk->sk_data_ready = ksocknal_data_ready;
538         sock->sk->sk_write_space = ksocknal_write_space;
539 }
540
541 void
542 ksocknal_lib_reset_callback(struct socket *sock, struct ksock_conn *conn)
543 {
544         /*
545          * Remove conn's network callbacks.
546          * NB I _have_ to restore the callback, rather than storing a noop,
547          * since the socket could survive past this module being unloaded!!
548          */
549         sock->sk->sk_data_ready = conn->ksnc_saved_data_ready;
550         sock->sk->sk_write_space = conn->ksnc_saved_write_space;
551
552         /*
553          * A callback could be in progress already; they hold a read lock
554          * on ksnd_global_lock (to serialise with me) and NOOP if
555          * sk_user_data is NULL.
556          */
557         sock->sk->sk_user_data = NULL;
558 }
559
560 int
561 ksocknal_lib_memory_pressure(struct ksock_conn *conn)
562 {
563         int rc = 0;
564         struct ksock_sched *sched;
565
566         sched = conn->ksnc_scheduler;
567         spin_lock_bh(&sched->kss_lock);
568
569         if (!test_bit(SOCK_NOSPACE, &conn->ksnc_sock->flags) &&
570             !conn->ksnc_tx_ready) {
571                 /*
572                  * SOCK_NOSPACE is set when the socket fills
573                  * and cleared in the write_space callback
574                  * (which also sets ksnc_tx_ready).  If
575                  * SOCK_NOSPACE and ksnc_tx_ready are BOTH
576                  * zero, I didn't fill the socket and
577                  * write_space won't reschedule me, so I
578                  * return -ENOMEM to get my caller to retry
579                  * after a timeout
580                  */
581                 rc = -ENOMEM;
582         }
583
584         spin_unlock_bh(&sched->kss_lock);
585
586         return rc;
587 }