Whamcloud - gitweb
LU-2850 kernel: 3.8 upstream kills daemonize()
[fs/lustre-release.git] / lnet / ulnds / socklnd / usocklnd.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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lnet/ulnds/socklnd/usocklnd.c
37  *
38  * Author: Maxim Patlasov <maxim@clusterfs.com>
39  */
40
41 #include "usocklnd.h"
42 #include <sys/time.h>
43
44 lnd_t the_tcplnd = {
45         .lnd_type      = SOCKLND,
46         .lnd_startup   = usocklnd_startup,
47         .lnd_shutdown  = usocklnd_shutdown,
48         .lnd_send      = usocklnd_send,
49         .lnd_recv      = usocklnd_recv,
50         .lnd_accept    = usocklnd_accept,
51 };
52
53 usock_data_t usock_data;
54 usock_tunables_t usock_tuns = {
55         .ut_timeout         = 50,
56         .ut_poll_timeout    = 1,
57         .ut_fair_limit      = 1,
58         .ut_npollthreads    = 0,
59         .ut_min_bulk        = 1<<10,
60         .ut_txcredits       = 256,
61         .ut_peertxcredits   = 8,
62         .ut_socknagle       = 0,
63         .ut_sockbufsiz      = 0,
64 };
65
66 #define MAX_REASONABLE_TIMEOUT 36000 /* 10 hours */
67 #define MAX_REASONABLE_NPT 1000
68
69 int
70 usocklnd_validate_tunables()
71 {
72         if (usock_tuns.ut_timeout <= 0 ||
73             usock_tuns.ut_timeout > MAX_REASONABLE_TIMEOUT) {
74                 CERROR("USOCK_TIMEOUT: %d is out of reasonable limits\n",
75                        usock_tuns.ut_timeout);
76                 return -1;
77         }
78
79         if (usock_tuns.ut_poll_timeout <= 0 ||
80             usock_tuns.ut_poll_timeout > MAX_REASONABLE_TIMEOUT) {
81                 CERROR("USOCK_POLL_TIMEOUT: %d is out of reasonable limits\n",
82                        usock_tuns.ut_poll_timeout);
83                 return -1;
84         }
85
86         if (usock_tuns.ut_fair_limit <= 0) {
87                 CERROR("Invalid USOCK_FAIR_LIMIT: %d (should be >0)\n",
88                        usock_tuns.ut_fair_limit);
89                 return -1;
90         }
91
92         if (usock_tuns.ut_npollthreads < 0 ||
93             usock_tuns.ut_npollthreads > MAX_REASONABLE_NPT) {
94                 CERROR("USOCK_NPOLLTHREADS: %d is out of reasonable limits\n",
95                        usock_tuns.ut_npollthreads);
96                 return -1;
97         }
98
99         if (usock_tuns.ut_txcredits <= 0) {
100                 CERROR("USOCK_TXCREDITS: %d should be positive\n",
101                        usock_tuns.ut_txcredits);
102                 return -1;
103         }
104
105         if (usock_tuns.ut_peertxcredits <= 0) {
106                 CERROR("USOCK_PEERTXCREDITS: %d should be positive\n",
107                        usock_tuns.ut_peertxcredits);
108                 return -1;
109         }
110
111         if (usock_tuns.ut_peertxcredits > usock_tuns.ut_txcredits) {
112                 CERROR("USOCK_PEERTXCREDITS: %d should not be greater"
113                        " than USOCK_TXCREDITS: %d\n",
114                        usock_tuns.ut_peertxcredits, usock_tuns.ut_txcredits);
115                 return -1;
116         }
117
118         if (usock_tuns.ut_socknagle != 0 &&
119             usock_tuns.ut_socknagle != 1) {
120                 CERROR("USOCK_SOCKNAGLE: %d should be 0 or 1\n",
121                        usock_tuns.ut_socknagle);
122                 return -1;
123         }
124
125         if (usock_tuns.ut_sockbufsiz < 0) {
126                 CERROR("USOCK_SOCKBUFSIZ: %d should be 0 or positive\n",
127                        usock_tuns.ut_sockbufsiz);
128                 return -1;
129         }
130
131         return 0;
132 }
133
134 void
135 usocklnd_release_poll_states(int n)
136 {
137         int i;
138
139         for (i = 0; i < n; i++) {
140                 usock_pollthread_t *pt = &usock_data.ud_pollthreads[i];
141
142                 libcfs_sock_release(pt->upt_notifier[0]);
143                 libcfs_sock_release(pt->upt_notifier[1]);
144
145                 pthread_mutex_destroy(&pt->upt_pollrequests_lock);
146                 fini_completion(&pt->upt_completion);
147
148                 LIBCFS_FREE (pt->upt_pollfd,
149                              sizeof(struct pollfd) * pt->upt_npollfd);
150                 LIBCFS_FREE (pt->upt_idx2conn,
151                               sizeof(usock_conn_t *) * pt->upt_npollfd);
152                 LIBCFS_FREE (pt->upt_fd2idx,
153                               sizeof(int) * pt->upt_nfd2idx);
154         }
155 }
156
157 int
158 usocklnd_update_tunables()
159 {
160         int rc;
161
162         rc = lnet_parse_int_tunable(&usock_tuns.ut_timeout,
163                                       "USOCK_TIMEOUT");
164         if (rc)
165                 return rc;
166
167         rc = lnet_parse_int_tunable(&usock_tuns.ut_poll_timeout,
168                                       "USOCK_POLL_TIMEOUT");
169         if (rc)
170                 return rc;
171
172         rc = lnet_parse_int_tunable(&usock_tuns.ut_npollthreads,
173                                       "USOCK_NPOLLTHREADS");
174         if (rc)
175                 return rc;
176
177         rc = lnet_parse_int_tunable(&usock_tuns.ut_fair_limit,
178                                       "USOCK_FAIR_LIMIT");
179         if (rc)
180                 return rc;
181
182         rc = lnet_parse_int_tunable(&usock_tuns.ut_min_bulk,
183                                       "USOCK_MIN_BULK");
184         if (rc)
185                 return rc;
186
187         rc = lnet_parse_int_tunable(&usock_tuns.ut_txcredits,
188                                       "USOCK_TXCREDITS");
189         if (rc)
190                 return rc;
191
192         rc = lnet_parse_int_tunable(&usock_tuns.ut_peertxcredits,
193                                       "USOCK_PEERTXCREDITS");
194         if (rc)
195                 return rc;
196
197         rc = lnet_parse_int_tunable(&usock_tuns.ut_socknagle,
198                                       "USOCK_SOCKNAGLE");
199         if (rc)
200                 return rc;
201
202         rc = lnet_parse_int_tunable(&usock_tuns.ut_sockbufsiz,
203                                       "USOCK_SOCKBUFSIZ");
204         if (rc)
205                 return rc;
206
207         if (usocklnd_validate_tunables())
208                 return -EINVAL;
209
210         if (usock_tuns.ut_npollthreads == 0) {
211                 usock_tuns.ut_npollthreads = cfs_online_cpus();
212
213                 if (usock_tuns.ut_npollthreads <= 0) {
214                         CERROR("Cannot find out the number of online CPUs\n");
215                         return -EINVAL;
216                 }
217         }
218
219         return 0;
220 }
221
222
223 int
224 usocklnd_base_startup()
225 {
226         usock_pollthread_t *pt;
227         int                 i;
228         int                 rc;
229
230         rc = usocklnd_update_tunables();
231         if (rc)
232                 return rc;
233
234         usock_data.ud_npollthreads = usock_tuns.ut_npollthreads;
235
236         LIBCFS_ALLOC (usock_data.ud_pollthreads,
237                       usock_data.ud_npollthreads *
238                       sizeof(usock_pollthread_t));
239         if (usock_data.ud_pollthreads == NULL)
240                 return -ENOMEM;
241
242         /* Initialize poll thread state structures */
243         for (i = 0; i < usock_data.ud_npollthreads; i++) {
244
245                 pt = &usock_data.ud_pollthreads[i];
246
247                 rc = -ENOMEM;
248
249                 LIBCFS_ALLOC (pt->upt_pollfd,
250                               sizeof(struct pollfd) * UPT_START_SIZ);
251                 if (pt->upt_pollfd == NULL)
252                         goto base_startup_failed_0;
253
254                 LIBCFS_ALLOC (pt->upt_idx2conn,
255                               sizeof(usock_conn_t *) * UPT_START_SIZ);
256                 if (pt->upt_idx2conn == NULL)
257                         goto base_startup_failed_1;
258
259                 LIBCFS_ALLOC (pt->upt_fd2idx,
260                               sizeof(int) * UPT_START_SIZ);
261                 if (pt->upt_fd2idx == NULL)
262                         goto base_startup_failed_2;
263
264                 memset(pt->upt_fd2idx, 0,
265                        sizeof(int) * UPT_START_SIZ);
266
267                 LIBCFS_ALLOC (pt->upt_skip,
268                               sizeof(int) * UPT_START_SIZ);
269                 if (pt->upt_skip == NULL)
270                         goto base_startup_failed_3;
271
272                 pt->upt_npollfd = pt->upt_nfd2idx = UPT_START_SIZ;
273
274                 rc = libcfs_socketpair(pt->upt_notifier);
275                 if (rc != 0)
276                         goto base_startup_failed_4;
277
278                 pt->upt_pollfd[0].fd = LIBCFS_SOCK2FD(pt->upt_notifier[1]);
279                 pt->upt_pollfd[0].events = POLLIN;
280                 pt->upt_pollfd[0].revents = 0;
281
282                 pt->upt_nfds = 1;
283                 pt->upt_idx2conn[0] = NULL;
284
285                 pt->upt_errno = 0;
286                 CFS_INIT_LIST_HEAD (&pt->upt_pollrequests);
287                 CFS_INIT_LIST_HEAD (&pt->upt_stale_list);
288                 pthread_mutex_init(&pt->upt_pollrequests_lock, NULL);
289                 init_completion(&pt->upt_completion);
290         }
291
292         /* Initialize peer hash list */
293         for (i = 0; i < UD_PEER_HASH_SIZE; i++)
294                 CFS_INIT_LIST_HEAD(&usock_data.ud_peers[i]);
295
296         pthread_rwlock_init(&usock_data.ud_peers_lock, NULL);
297
298         /* Spawn poll threads */
299         for (i = 0; i < usock_data.ud_npollthreads; i++) {
300                 rc = PTR_ERR(kthread_run(usocklnd_poll_thread,
301                                              &usock_data.ud_pollthreads[i],
302                                              ""));
303                 if (IS_ERR_VALUE(rc)) {
304                         usocklnd_base_shutdown(i);
305                         return rc;
306                 }
307         }
308
309         usock_data.ud_state = UD_STATE_INITIALIZED;
310
311         return 0;
312
313   base_startup_failed_4:
314         LIBCFS_FREE (pt->upt_skip, sizeof(int) * UPT_START_SIZ);
315   base_startup_failed_3:
316         LIBCFS_FREE (pt->upt_fd2idx, sizeof(int) * UPT_START_SIZ);
317   base_startup_failed_2:
318         LIBCFS_FREE (pt->upt_idx2conn, sizeof(usock_conn_t *) * UPT_START_SIZ);
319   base_startup_failed_1:
320         LIBCFS_FREE (pt->upt_pollfd, sizeof(struct pollfd) * UPT_START_SIZ);
321   base_startup_failed_0:
322         LASSERT(rc != 0);
323         usocklnd_release_poll_states(i);
324         LIBCFS_FREE (usock_data.ud_pollthreads,
325                      usock_data.ud_npollthreads *
326                      sizeof(usock_pollthread_t));
327         return rc;
328 }
329
330 void
331 usocklnd_base_shutdown(int n)
332 {
333         int i;
334
335         usock_data.ud_shutdown = 1;
336         for (i = 0; i < n; i++) {
337                 usock_pollthread_t *pt = &usock_data.ud_pollthreads[i];
338                 usocklnd_wakeup_pollthread(i);
339                 wait_for_completion(&pt->upt_completion);
340         }
341
342         pthread_rwlock_destroy(&usock_data.ud_peers_lock);
343
344         usocklnd_release_poll_states(usock_data.ud_npollthreads);
345
346         LIBCFS_FREE (usock_data.ud_pollthreads,
347                      usock_data.ud_npollthreads *
348                      sizeof(usock_pollthread_t));
349
350         usock_data.ud_state = UD_STATE_INIT_NOTHING;
351 }
352
353 __u64
354 usocklnd_new_incarnation()
355 {
356         struct timeval tv;
357         int            rc = gettimeofday(&tv, NULL);
358         LASSERT (rc == 0);
359         return (((__u64)tv.tv_sec) * 1000000) + tv.tv_usec;
360 }
361
362 static int
363 usocklnd_assign_ni_nid(lnet_ni_t *ni)
364 {
365         int   rc;
366         int   up;
367         __u32 ipaddr;
368
369         /* Find correct IP-address and update ni_nid with it.
370          * Two cases are supported:
371          * 1) no explicit interfaces are defined. NID will be assigned to
372          * first non-lo interface that is up;
373          * 2) exactly one explicit interface is defined. For example,
374          * LNET_NETWORKS='tcp(eth0)' */
375
376         if (ni->ni_interfaces[0] == NULL) {
377                 char **names;
378                 int    i, n;
379
380                 n = libcfs_ipif_enumerate(&names);
381                 if (n <= 0) {
382                         CERROR("Can't enumerate interfaces: %d\n", n);
383                         return -1;
384                 }
385
386                 for (i = 0; i < n; i++) {
387
388                         if (!strcmp(names[i], "lo")) /* skip the loopback IF */
389                                 continue;
390
391                         rc = libcfs_ipif_query(names[i], &up, &ipaddr);
392                         if (rc != 0) {
393                                 CWARN("Can't get interface %s info: %d\n",
394                                       names[i], rc);
395                                 continue;
396                         }
397
398                         if (!up) {
399                                 CWARN("Ignoring interface %s (down)\n",
400                                       names[i]);
401                             continue;
402                         }
403
404                         break;      /* one address is quite enough */
405                 }
406
407                 libcfs_ipif_free_enumeration(names, n);
408
409                 if (i >= n) {
410                         CERROR("Can't find any usable interfaces\n");
411                         return -1;
412                 }
413
414                 CDEBUG(D_NET, "No explicit interfaces defined. "
415                        "%u.%u.%u.%u used\n", HIPQUAD(ipaddr));
416         } else {
417                 if (ni->ni_interfaces[1] != NULL) {
418                         CERROR("only one explicit interface is allowed\n");
419                         return -1;
420                 }
421
422                 rc = libcfs_ipif_query(ni->ni_interfaces[0], &up, &ipaddr);
423                 if (rc != 0) {
424                         CERROR("Can't get interface %s info: %d\n",
425                                ni->ni_interfaces[0], rc);
426                         return -1;
427                 }
428
429                 if (!up) {
430                         CERROR("Explicit interface defined: %s but is down\n",
431                                ni->ni_interfaces[0]);
432                         return -1;
433                 }
434
435                 CDEBUG(D_NET, "Explicit interface defined: %s. "
436                        "%u.%u.%u.%u used\n",
437                        ni->ni_interfaces[0], HIPQUAD(ipaddr));
438
439         }
440
441         ni->ni_nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid), ipaddr);
442
443         return 0;
444 }
445
446 int
447 usocklnd_startup(lnet_ni_t *ni)
448 {
449         int          rc;
450         usock_net_t *net;
451
452         if (usock_data.ud_state == UD_STATE_INIT_NOTHING) {
453                 rc = usocklnd_base_startup();
454                 if (rc != 0)
455                         return rc;
456         }
457
458         LIBCFS_ALLOC(net, sizeof(*net));
459         if (net == NULL)
460                 goto startup_failed_0;
461
462         memset(net, 0, sizeof(*net));
463         net->un_incarnation = usocklnd_new_incarnation();
464         pthread_mutex_init(&net->un_lock, NULL);
465         pthread_cond_init(&net->un_cond, NULL);
466
467         ni->ni_data = net;
468
469         rc = usocklnd_assign_ni_nid(ni);
470         if (rc != 0)
471                 goto startup_failed_1;
472
473         LASSERT (ni->ni_lnd == &the_tcplnd);
474
475         ni->ni_maxtxcredits = usock_tuns.ut_txcredits;
476         ni->ni_peertxcredits = usock_tuns.ut_peertxcredits;
477
478         usock_data.ud_nets_count++;
479         return 0;
480
481  startup_failed_1:
482         pthread_mutex_destroy(&net->un_lock);
483         pthread_cond_destroy(&net->un_cond);
484         LIBCFS_FREE(net, sizeof(*net));
485  startup_failed_0:
486         if (usock_data.ud_nets_count == 0)
487                 usocklnd_base_shutdown(usock_data.ud_npollthreads);
488
489         return -ENETDOWN;
490 }
491
492 void
493 usocklnd_shutdown(lnet_ni_t *ni)
494 {
495         usock_net_t *net = ni->ni_data;
496
497         net->un_shutdown = 1;
498
499         usocklnd_del_all_peers(ni);
500
501         /* Wait for all peer state to clean up */
502         pthread_mutex_lock(&net->un_lock);
503         while (net->un_peercount != 0)
504                 pthread_cond_wait(&net->un_cond, &net->un_lock);
505         pthread_mutex_unlock(&net->un_lock);
506
507         /* Release usock_net_t structure */
508         pthread_mutex_destroy(&net->un_lock);
509         pthread_cond_destroy(&net->un_cond);
510         LIBCFS_FREE(net, sizeof(*net));
511
512         usock_data.ud_nets_count--;
513         if (usock_data.ud_nets_count == 0)
514                 usocklnd_base_shutdown(usock_data.ud_npollthreads);
515 }
516
517 void
518 usocklnd_del_all_peers(lnet_ni_t *ni)
519 {
520         cfs_list_t        *ptmp;
521         cfs_list_t        *pnxt;
522         usock_peer_t      *peer;
523         int                i;
524
525         pthread_rwlock_wrlock(&usock_data.ud_peers_lock);
526
527         for (i = 0; i < UD_PEER_HASH_SIZE; i++) {
528                 cfs_list_for_each_safe (ptmp, pnxt, &usock_data.ud_peers[i]) {
529                         peer = cfs_list_entry (ptmp, usock_peer_t, up_list);
530
531                         if (peer->up_ni != ni)
532                                 continue;
533
534                         usocklnd_del_peer_and_conns(peer);
535                 }
536         }
537
538         pthread_rwlock_unlock(&usock_data.ud_peers_lock);
539
540         /* wakeup all threads */
541         for (i = 0; i < usock_data.ud_npollthreads; i++)
542                 usocklnd_wakeup_pollthread(i);
543 }
544
545 void
546 usocklnd_del_peer_and_conns(usock_peer_t *peer)
547 {
548         /* peer cannot disappear because it's still in hash list */
549
550         pthread_mutex_lock(&peer->up_lock);
551         /* content of conn[] array cannot change now */
552         usocklnd_del_conns_locked(peer);
553         pthread_mutex_unlock(&peer->up_lock);
554
555         /* peer hash list is still protected by the caller */
556         cfs_list_del(&peer->up_list);
557
558         usocklnd_peer_decref(peer); /* peer isn't in hash list anymore */
559 }
560
561 void
562 usocklnd_del_conns_locked(usock_peer_t *peer)
563 {
564         int i;
565
566         for (i=0; i < N_CONN_TYPES; i++) {
567                 usock_conn_t *conn = peer->up_conns[i];
568                 if (conn != NULL)
569                         usocklnd_conn_kill(conn);
570         }
571 }