8479b533286523a48f5eb092eae05e69e49a5f12
[cascardo/linux.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 socket *sock = conn->ksnc_sock;
77         int nob;
78         int rc;
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         /*
87          * NB we can't trust socket ops to either consume our iovs
88          * or leave them alone.
89          */
90         {
91 #if SOCKNAL_SINGLE_FRAG_TX
92                 struct kvec scratch;
93                 struct kvec *scratchiov = &scratch;
94                 unsigned int niov = 1;
95 #else
96                 struct kvec *scratchiov = conn->ksnc_scheduler->kss_scratch_iov;
97                 unsigned int niov = tx->tx_niov;
98 #endif
99                 struct msghdr msg = {.msg_flags = MSG_DONTWAIT};
100                 int i;
101
102                 for (nob = i = 0; i < niov; i++) {
103                         scratchiov[i] = tx->tx_iov[i];
104                         nob += scratchiov[i].iov_len;
105                 }
106
107                 if (!list_empty(&conn->ksnc_tx_queue) ||
108                     nob < tx->tx_resid)
109                         msg.msg_flags |= MSG_MORE;
110
111                 rc = kernel_sendmsg(sock, &msg, scratchiov, niov, nob);
112         }
113         return rc;
114 }
115
116 int
117 ksocknal_lib_send_kiov(struct ksock_conn *conn, struct ksock_tx *tx)
118 {
119         struct socket *sock = conn->ksnc_sock;
120         lnet_kiov_t *kiov = tx->tx_kiov;
121         int rc;
122         int nob;
123
124         /* Not NOOP message */
125         LASSERT(tx->tx_lnetmsg);
126
127         /*
128          * NB we can't trust socket ops to either consume our iovs
129          * or leave them alone.
130          */
131         if (tx->tx_msg.ksm_zc_cookies[0]) {
132                 /* Zero copy is enabled */
133                 struct sock *sk = sock->sk;
134                 struct page *page = kiov->kiov_page;
135                 int offset = kiov->kiov_offset;
136                 int fragsize = kiov->kiov_len;
137                 int msgflg = MSG_DONTWAIT;
138
139                 CDEBUG(D_NET, "page %p + offset %x for %d\n",
140                        page, offset, kiov->kiov_len);
141
142                 if (!list_empty(&conn->ksnc_tx_queue) ||
143                     fragsize < tx->tx_resid)
144                         msgflg |= MSG_MORE;
145
146                 if (sk->sk_prot->sendpage) {
147                         rc = sk->sk_prot->sendpage(sk, page,
148                                                    offset, fragsize, msgflg);
149                 } else {
150                         rc = tcp_sendpage(sk, page, offset, fragsize, msgflg);
151                 }
152         } else {
153 #if SOCKNAL_SINGLE_FRAG_TX || !SOCKNAL_RISK_KMAP_DEADLOCK
154                 struct kvec scratch;
155                 struct kvec *scratchiov = &scratch;
156                 unsigned int niov = 1;
157 #else
158 #ifdef CONFIG_HIGHMEM
159 #warning "XXX risk of kmap deadlock on multiple frags..."
160 #endif
161                 struct kvec *scratchiov = conn->ksnc_scheduler->kss_scratch_iov;
162                 unsigned int niov = tx->tx_nkiov;
163 #endif
164                 struct msghdr msg = {.msg_flags = MSG_DONTWAIT};
165                 int i;
166
167                 for (nob = i = 0; i < niov; i++) {
168                         scratchiov[i].iov_base = kmap(kiov[i].kiov_page) +
169                                                  kiov[i].kiov_offset;
170                         nob += scratchiov[i].iov_len = kiov[i].kiov_len;
171                 }
172
173                 if (!list_empty(&conn->ksnc_tx_queue) ||
174                     nob < tx->tx_resid)
175                         msg.msg_flags |= MSG_MORE;
176
177                 rc = kernel_sendmsg(sock, &msg, (struct kvec *)scratchiov, niov, nob);
178
179                 for (i = 0; i < niov; i++)
180                         kunmap(kiov[i].kiov_page);
181         }
182         return rc;
183 }
184
185 void
186 ksocknal_lib_eager_ack(struct ksock_conn *conn)
187 {
188         int opt = 1;
189         struct socket *sock = conn->ksnc_sock;
190
191         /*
192          * Remind the socket to ACK eagerly.  If I don't, the socket might
193          * think I'm about to send something it could piggy-back the ACK
194          * on, introducing delay in completing zero-copy sends in my
195          * peer.
196          */
197         kernel_setsockopt(sock, SOL_TCP, TCP_QUICKACK, (char *)&opt,
198                           sizeof(opt));
199 }
200
201 int
202 ksocknal_lib_recv_iov(struct ksock_conn *conn)
203 {
204 #if SOCKNAL_SINGLE_FRAG_RX
205         struct kvec scratch;
206         struct kvec *scratchiov = &scratch;
207         unsigned int niov = 1;
208 #else
209         struct kvec *scratchiov = conn->ksnc_scheduler->kss_scratch_iov;
210         unsigned int niov = conn->ksnc_rx_niov;
211 #endif
212         struct kvec *iov = conn->ksnc_rx_iov;
213         struct msghdr msg = {
214                 .msg_flags = 0
215         };
216         int nob;
217         int i;
218         int rc;
219         int fragnob;
220         int sum;
221         __u32 saved_csum;
222
223         /*
224          * NB we can't trust socket ops to either consume our iovs
225          * or leave them alone.
226          */
227         LASSERT(niov > 0);
228
229         for (nob = i = 0; i < niov; i++) {
230                 scratchiov[i] = iov[i];
231                 nob += scratchiov[i].iov_len;
232         }
233         LASSERT(nob <= conn->ksnc_rx_nob_wanted);
234
235         rc = kernel_recvmsg(conn->ksnc_sock, &msg, scratchiov, niov, nob,
236                             MSG_DONTWAIT);
237
238         saved_csum = 0;
239         if (conn->ksnc_proto == &ksocknal_protocol_v2x) {
240                 saved_csum = conn->ksnc_msg.ksm_csum;
241                 conn->ksnc_msg.ksm_csum = 0;
242         }
243
244         if (saved_csum) {
245                 /* accumulate checksum */
246                 for (i = 0, sum = rc; sum > 0; i++, sum -= fragnob) {
247                         LASSERT(i < niov);
248
249                         fragnob = iov[i].iov_len;
250                         if (fragnob > sum)
251                                 fragnob = sum;
252
253                         conn->ksnc_rx_csum = ksocknal_csum(conn->ksnc_rx_csum,
254                                                            iov[i].iov_base, fragnob);
255                 }
256                 conn->ksnc_msg.ksm_csum = saved_csum;
257         }
258
259         return rc;
260 }
261
262 int
263 ksocknal_lib_recv_kiov(struct ksock_conn *conn)
264 {
265         struct bio_vec *bv = conn->ksnc_scheduler->kss_scratch_bvec;
266         unsigned int niov = conn->ksnc_rx_nkiov;
267         lnet_kiov_t   *kiov = conn->ksnc_rx_kiov;
268         struct msghdr msg = {
269                 .msg_flags = 0
270         };
271         int nob;
272         int i;
273         int rc;
274         void *base;
275         int sum;
276         int fragnob;
277         int n;
278
279         for (nob = i = 0; i < niov; i++) {
280                 nob += bv[i].bv_len = kiov[i].kiov_len;
281                 bv[i].bv_page = kiov[i].kiov_page;
282                 bv[i].bv_offset = kiov[i].kiov_offset;
283         }
284         n = niov;
285
286         LASSERT(nob <= conn->ksnc_rx_nob_wanted);
287
288         iov_iter_bvec(&msg.msg_iter, READ | ITER_BVEC, bv, n, nob);
289         rc = sock_recvmsg(conn->ksnc_sock, &msg, MSG_DONTWAIT);
290
291         if (conn->ksnc_msg.ksm_csum) {
292                 for (i = 0, sum = rc; sum > 0; i++, sum -= fragnob) {
293                         LASSERT(i < niov);
294
295                         base = kmap(kiov[i].kiov_page) + kiov[i].kiov_offset;
296                         fragnob = kiov[i].kiov_len;
297                         if (fragnob > sum)
298                                 fragnob = sum;
299
300                         conn->ksnc_rx_csum = ksocknal_csum(conn->ksnc_rx_csum,
301                                                            base, fragnob);
302
303                         kunmap(kiov[i].kiov_page);
304                 }
305         }
306         return rc;
307 }
308
309 void
310 ksocknal_lib_csum_tx(struct ksock_tx *tx)
311 {
312         int i;
313         __u32 csum;
314         void *base;
315
316         LASSERT(tx->tx_iov[0].iov_base == &tx->tx_msg);
317         LASSERT(tx->tx_conn);
318         LASSERT(tx->tx_conn->ksnc_proto == &ksocknal_protocol_v2x);
319
320         tx->tx_msg.ksm_csum = 0;
321
322         csum = ksocknal_csum(~0, tx->tx_iov[0].iov_base,
323                              tx->tx_iov[0].iov_len);
324
325         if (tx->tx_kiov) {
326                 for (i = 0; i < tx->tx_nkiov; i++) {
327                         base = kmap(tx->tx_kiov[i].kiov_page) +
328                                tx->tx_kiov[i].kiov_offset;
329
330                         csum = ksocknal_csum(csum, base, tx->tx_kiov[i].kiov_len);
331
332                         kunmap(tx->tx_kiov[i].kiov_page);
333                 }
334         } else {
335                 for (i = 1; i < tx->tx_niov; i++)
336                         csum = ksocknal_csum(csum, tx->tx_iov[i].iov_base,
337                                              tx->tx_iov[i].iov_len);
338         }
339
340         if (*ksocknal_tunables.ksnd_inject_csum_error) {
341                 csum++;
342                 *ksocknal_tunables.ksnd_inject_csum_error = 0;
343         }
344
345         tx->tx_msg.ksm_csum = csum;
346 }
347
348 int
349 ksocknal_lib_get_conn_tunables(struct ksock_conn *conn, int *txmem, int *rxmem, int *nagle)
350 {
351         struct socket *sock = conn->ksnc_sock;
352         int len;
353         int rc;
354
355         rc = ksocknal_connsock_addref(conn);
356         if (rc) {
357                 LASSERT(conn->ksnc_closing);
358                 *txmem = *rxmem = *nagle = 0;
359                 return -ESHUTDOWN;
360         }
361
362         rc = lnet_sock_getbuf(sock, txmem, rxmem);
363         if (!rc) {
364                 len = sizeof(*nagle);
365                 rc = kernel_getsockopt(sock, SOL_TCP, TCP_NODELAY,
366                                        (char *)nagle, &len);
367         }
368
369         ksocknal_connsock_decref(conn);
370
371         if (!rc)
372                 *nagle = !*nagle;
373         else
374                 *txmem = *rxmem = *nagle = 0;
375
376         return rc;
377 }
378
379 int
380 ksocknal_lib_setup_sock(struct socket *sock)
381 {
382         int rc;
383         int option;
384         int keep_idle;
385         int keep_intvl;
386         int keep_count;
387         int do_keepalive;
388         struct linger linger;
389
390         sock->sk->sk_allocation = GFP_NOFS;
391
392         /*
393          * Ensure this socket aborts active sends immediately when we close
394          * it.
395          */
396         linger.l_onoff = 0;
397         linger.l_linger = 0;
398
399         rc = kernel_setsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&linger,
400                                sizeof(linger));
401         if (rc) {
402                 CERROR("Can't set SO_LINGER: %d\n", rc);
403                 return rc;
404         }
405
406         option = -1;
407         rc = kernel_setsockopt(sock, SOL_TCP, TCP_LINGER2, (char *)&option,
408                                sizeof(option));
409         if (rc) {
410                 CERROR("Can't set SO_LINGER2: %d\n", rc);
411                 return rc;
412         }
413
414         if (!*ksocknal_tunables.ksnd_nagle) {
415                 option = 1;
416
417                 rc = kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY,
418                                        (char *)&option, sizeof(option));
419                 if (rc) {
420                         CERROR("Can't disable nagle: %d\n", rc);
421                         return rc;
422                 }
423         }
424
425         rc = lnet_sock_setbuf(sock, *ksocknal_tunables.ksnd_tx_buffer_size,
426                               *ksocknal_tunables.ksnd_rx_buffer_size);
427         if (rc) {
428                 CERROR("Can't set buffer tx %d, rx %d buffers: %d\n",
429                        *ksocknal_tunables.ksnd_tx_buffer_size,
430                        *ksocknal_tunables.ksnd_rx_buffer_size, rc);
431                 return rc;
432         }
433
434 /* TCP_BACKOFF_* sockopt tunables unsupported in stock kernels */
435
436         /* snapshot tunables */
437         keep_idle  = *ksocknal_tunables.ksnd_keepalive_idle;
438         keep_count = *ksocknal_tunables.ksnd_keepalive_count;
439         keep_intvl = *ksocknal_tunables.ksnd_keepalive_intvl;
440
441         do_keepalive = (keep_idle > 0 && keep_count > 0 && keep_intvl > 0);
442
443         option = (do_keepalive ? 1 : 0);
444         rc = kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char *)&option,
445                                sizeof(option));
446         if (rc) {
447                 CERROR("Can't set SO_KEEPALIVE: %d\n", rc);
448                 return rc;
449         }
450
451         if (!do_keepalive)
452                 return 0;
453
454         rc = kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE, (char *)&keep_idle,
455                                sizeof(keep_idle));
456         if (rc) {
457                 CERROR("Can't set TCP_KEEPIDLE: %d\n", rc);
458                 return rc;
459         }
460
461         rc = kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
462                                (char *)&keep_intvl, sizeof(keep_intvl));
463         if (rc) {
464                 CERROR("Can't set TCP_KEEPINTVL: %d\n", rc);
465                 return rc;
466         }
467
468         rc = kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT, (char *)&keep_count,
469                                sizeof(keep_count));
470         if (rc) {
471                 CERROR("Can't set TCP_KEEPCNT: %d\n", rc);
472                 return rc;
473         }
474
475         return 0;
476 }
477
478 void
479 ksocknal_lib_push_conn(struct ksock_conn *conn)
480 {
481         struct sock *sk;
482         struct tcp_sock *tp;
483         int nonagle;
484         int val = 1;
485         int rc;
486
487         rc = ksocknal_connsock_addref(conn);
488         if (rc)                     /* being shut down */
489                 return;
490
491         sk = conn->ksnc_sock->sk;
492         tp = tcp_sk(sk);
493
494         lock_sock(sk);
495         nonagle = tp->nonagle;
496         tp->nonagle = 1;
497         release_sock(sk);
498
499         rc = kernel_setsockopt(conn->ksnc_sock, SOL_TCP, TCP_NODELAY,
500                                (char *)&val, sizeof(val));
501         LASSERT(!rc);
502
503         lock_sock(sk);
504         tp->nonagle = nonagle;
505         release_sock(sk);
506
507         ksocknal_connsock_decref(conn);
508 }
509
510 /*
511  * socket call back in Linux
512  */
513 static void
514 ksocknal_data_ready(struct sock *sk)
515 {
516         struct ksock_conn *conn;
517
518         /* interleave correctly with closing sockets... */
519         LASSERT(!in_irq());
520         read_lock(&ksocknal_data.ksnd_global_lock);
521
522         conn = sk->sk_user_data;
523         if (!conn) {         /* raced with ksocknal_terminate_conn */
524                 LASSERT(sk->sk_data_ready != &ksocknal_data_ready);
525                 sk->sk_data_ready(sk);
526         } else {
527                 ksocknal_read_callback(conn);
528         }
529
530         read_unlock(&ksocknal_data.ksnd_global_lock);
531 }
532
533 static void
534 ksocknal_write_space(struct sock *sk)
535 {
536         struct ksock_conn *conn;
537         int wspace;
538         int min_wpace;
539
540         /* interleave correctly with closing sockets... */
541         LASSERT(!in_irq());
542         read_lock(&ksocknal_data.ksnd_global_lock);
543
544         conn = sk->sk_user_data;
545         wspace = sk_stream_wspace(sk);
546         min_wpace = sk_stream_min_wspace(sk);
547
548         CDEBUG(D_NET, "sk %p wspace %d low water %d conn %p%s%s%s\n",
549                sk, wspace, min_wpace, conn,
550                !conn ? "" : (conn->ksnc_tx_ready ?
551                                       " ready" : " blocked"),
552                !conn ? "" : (conn->ksnc_tx_scheduled ?
553                                       " scheduled" : " idle"),
554                !conn ? "" : (list_empty(&conn->ksnc_tx_queue) ?
555                                       " empty" : " queued"));
556
557         if (!conn) {         /* raced with ksocknal_terminate_conn */
558                 LASSERT(sk->sk_write_space != &ksocknal_write_space);
559                 sk->sk_write_space(sk);
560
561                 read_unlock(&ksocknal_data.ksnd_global_lock);
562                 return;
563         }
564
565         if (wspace >= min_wpace) {            /* got enough space */
566                 ksocknal_write_callback(conn);
567
568                 /*
569                  * Clear SOCK_NOSPACE _after_ ksocknal_write_callback so the
570                  * ENOMEM check in ksocknal_transmit is race-free (think about
571                  * it).
572                  */
573                 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
574         }
575
576         read_unlock(&ksocknal_data.ksnd_global_lock);
577 }
578
579 void
580 ksocknal_lib_save_callback(struct socket *sock, struct ksock_conn *conn)
581 {
582         conn->ksnc_saved_data_ready = sock->sk->sk_data_ready;
583         conn->ksnc_saved_write_space = sock->sk->sk_write_space;
584 }
585
586 void
587 ksocknal_lib_set_callback(struct socket *sock,  struct ksock_conn *conn)
588 {
589         sock->sk->sk_user_data = conn;
590         sock->sk->sk_data_ready = ksocknal_data_ready;
591         sock->sk->sk_write_space = ksocknal_write_space;
592 }
593
594 void
595 ksocknal_lib_reset_callback(struct socket *sock, struct ksock_conn *conn)
596 {
597         /*
598          * Remove conn's network callbacks.
599          * NB I _have_ to restore the callback, rather than storing a noop,
600          * since the socket could survive past this module being unloaded!!
601          */
602         sock->sk->sk_data_ready = conn->ksnc_saved_data_ready;
603         sock->sk->sk_write_space = conn->ksnc_saved_write_space;
604
605         /*
606          * A callback could be in progress already; they hold a read lock
607          * on ksnd_global_lock (to serialise with me) and NOOP if
608          * sk_user_data is NULL.
609          */
610         sock->sk->sk_user_data = NULL;
611 }
612
613 int
614 ksocknal_lib_memory_pressure(struct ksock_conn *conn)
615 {
616         int rc = 0;
617         struct ksock_sched *sched;
618
619         sched = conn->ksnc_scheduler;
620         spin_lock_bh(&sched->kss_lock);
621
622         if (!test_bit(SOCK_NOSPACE, &conn->ksnc_sock->flags) &&
623             !conn->ksnc_tx_ready) {
624                 /*
625                  * SOCK_NOSPACE is set when the socket fills
626                  * and cleared in the write_space callback
627                  * (which also sets ksnc_tx_ready).  If
628                  * SOCK_NOSPACE and ksnc_tx_ready are BOTH
629                  * zero, I didn't fill the socket and
630                  * write_space won't reschedule me, so I
631                  * return -ENOMEM to get my caller to retry
632                  * after a timeout
633                  */
634                 rc = -ENOMEM;
635         }
636
637         spin_unlock_bh(&sched->kss_lock);
638
639         return rc;
640 }