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