Whamcloud - gitweb
a280d3b2543be1a03d3490bfe2dd3901ce66d3dd
[fs/lustre-release.git] / lnet / ulnds / socklnd / usocklnd.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
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                 cfs_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                 cfs_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 = cfs_create_thread(usocklnd_poll_thread,
301                                        &usock_data.ud_pollthreads[i]);
302                 if (rc) {
303                         usocklnd_base_shutdown(i);
304                         return rc;
305                 }
306         }
307
308         usock_data.ud_state = UD_STATE_INITIALIZED;
309
310         return 0;
311
312   base_startup_failed_4:
313         LIBCFS_FREE (pt->upt_skip, sizeof(int) * UPT_START_SIZ);
314   base_startup_failed_3:
315         LIBCFS_FREE (pt->upt_fd2idx, sizeof(int) * UPT_START_SIZ);
316   base_startup_failed_2:
317         LIBCFS_FREE (pt->upt_idx2conn, sizeof(usock_conn_t *) * UPT_START_SIZ);
318   base_startup_failed_1:
319         LIBCFS_FREE (pt->upt_pollfd, sizeof(struct pollfd) * UPT_START_SIZ);
320   base_startup_failed_0:
321         LASSERT(rc != 0);
322         usocklnd_release_poll_states(i);
323         LIBCFS_FREE (usock_data.ud_pollthreads,
324                      usock_data.ud_npollthreads *
325                      sizeof(usock_pollthread_t));
326         return rc;
327 }
328
329 void
330 usocklnd_base_shutdown(int n)
331 {
332         int i;
333
334         usock_data.ud_shutdown = 1;
335         for (i = 0; i < n; i++) {
336                 usock_pollthread_t *pt = &usock_data.ud_pollthreads[i];
337                 usocklnd_wakeup_pollthread(i);
338                 cfs_wait_for_completion(&pt->upt_completion);
339         }
340
341         pthread_rwlock_destroy(&usock_data.ud_peers_lock);
342
343         usocklnd_release_poll_states(usock_data.ud_npollthreads);
344
345         LIBCFS_FREE (usock_data.ud_pollthreads,
346                      usock_data.ud_npollthreads *
347                      sizeof(usock_pollthread_t));
348
349         usock_data.ud_state = UD_STATE_INIT_NOTHING;
350 }
351
352 __u64
353 usocklnd_new_incarnation()
354 {
355         struct timeval tv;
356         int            rc = gettimeofday(&tv, NULL);
357         LASSERT (rc == 0);
358         return (((__u64)tv.tv_sec) * 1000000) + tv.tv_usec;
359 }
360
361 static int
362 usocklnd_assign_ni_nid(lnet_ni_t *ni)
363 {
364         int   rc;
365         int   up;
366         __u32 ipaddr;
367
368         /* Find correct IP-address and update ni_nid with it.
369          * Two cases are supported:
370          * 1) no explicit interfaces are defined. NID will be assigned to
371          * first non-lo interface that is up;
372          * 2) exactly one explicit interface is defined. For example,
373          * LNET_NETWORKS='tcp(eth0)' */
374
375         if (ni->ni_interfaces[0] == NULL) {
376                 char **names;
377                 int    i, n;
378
379                 n = libcfs_ipif_enumerate(&names);
380                 if (n <= 0) {
381                         CERROR("Can't enumerate interfaces: %d\n", n);
382                         return -1;
383                 }
384
385                 for (i = 0; i < n; i++) {
386
387                         if (!strcmp(names[i], "lo")) /* skip the loopback IF */
388                                 continue;
389
390                         rc = libcfs_ipif_query(names[i], &up, &ipaddr);
391                         if (rc != 0) {
392                                 CWARN("Can't get interface %s info: %d\n",
393                                       names[i], rc);
394                                 continue;
395                         }
396
397                         if (!up) {
398                                 CWARN("Ignoring interface %s (down)\n",
399                                       names[i]);
400                             continue;
401                         }
402
403                         break;      /* one address is quite enough */
404                 }
405
406                 libcfs_ipif_free_enumeration(names, n);
407
408                 if (i >= n) {
409                         CERROR("Can't find any usable interfaces\n");
410                         return -1;
411                 }
412
413                 CDEBUG(D_NET, "No explicit interfaces defined. "
414                        "%u.%u.%u.%u used\n", HIPQUAD(ipaddr));
415         } else {
416                 if (ni->ni_interfaces[1] != NULL) {
417                         CERROR("only one explicit interface is allowed\n");
418                         return -1;
419                 }
420
421                 rc = libcfs_ipif_query(ni->ni_interfaces[0], &up, &ipaddr);
422                 if (rc != 0) {
423                         CERROR("Can't get interface %s info: %d\n",
424                                ni->ni_interfaces[0], rc);
425                         return -1;
426                 }
427
428                 if (!up) {
429                         CERROR("Explicit interface defined: %s but is down\n",
430                                ni->ni_interfaces[0]);
431                         return -1;
432                 }
433
434                 CDEBUG(D_NET, "Explicit interface defined: %s. "
435                        "%u.%u.%u.%u used\n",
436                        ni->ni_interfaces[0], HIPQUAD(ipaddr));
437
438         }
439
440         ni->ni_nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid), ipaddr);
441
442         return 0;
443 }
444
445 int
446 usocklnd_startup(lnet_ni_t *ni)
447 {
448         int          rc;
449         usock_net_t *net;
450
451         if (usock_data.ud_state == UD_STATE_INIT_NOTHING) {
452                 rc = usocklnd_base_startup();
453                 if (rc != 0)
454                         return rc;
455         }
456
457         LIBCFS_ALLOC(net, sizeof(*net));
458         if (net == NULL)
459                 goto startup_failed_0;
460
461         memset(net, 0, sizeof(*net));
462         net->un_incarnation = usocklnd_new_incarnation();
463         pthread_mutex_init(&net->un_lock, NULL);
464         pthread_cond_init(&net->un_cond, NULL);
465
466         ni->ni_data = net;
467
468         if (!(the_lnet.ln_pid & LNET_PID_USERFLAG)) {
469                 rc = usocklnd_assign_ni_nid(ni);
470                 if (rc != 0)
471                         goto startup_failed_1;
472         }
473
474         LASSERT (ni->ni_lnd == &the_tcplnd);
475
476         ni->ni_maxtxcredits = usock_tuns.ut_txcredits;
477         ni->ni_peertxcredits = usock_tuns.ut_peertxcredits;
478
479         usock_data.ud_nets_count++;
480         return 0;
481
482  startup_failed_1:
483         pthread_mutex_destroy(&net->un_lock);
484         pthread_cond_destroy(&net->un_cond);
485         LIBCFS_FREE(net, sizeof(*net));
486  startup_failed_0:
487         if (usock_data.ud_nets_count == 0)
488                 usocklnd_base_shutdown(usock_data.ud_npollthreads);
489
490         return -ENETDOWN;
491 }
492
493 void
494 usocklnd_shutdown(lnet_ni_t *ni)
495 {
496         usock_net_t *net = ni->ni_data;
497
498         net->un_shutdown = 1;
499
500         usocklnd_del_all_peers(ni);
501
502         /* Wait for all peer state to clean up */
503         pthread_mutex_lock(&net->un_lock);
504         while (net->un_peercount != 0)
505                 pthread_cond_wait(&net->un_cond, &net->un_lock);
506         pthread_mutex_unlock(&net->un_lock);
507
508         /* Release usock_net_t structure */
509         pthread_mutex_destroy(&net->un_lock);
510         pthread_cond_destroy(&net->un_cond);
511         LIBCFS_FREE(net, sizeof(*net));
512
513         usock_data.ud_nets_count--;
514         if (usock_data.ud_nets_count == 0)
515                 usocklnd_base_shutdown(usock_data.ud_npollthreads);
516 }
517
518 void
519 usocklnd_del_all_peers(lnet_ni_t *ni)
520 {
521         struct list_head  *ptmp;
522         struct list_head  *pnxt;
523         usock_peer_t      *peer;
524         int                i;
525
526         pthread_rwlock_wrlock(&usock_data.ud_peers_lock);
527
528         for (i = 0; i < UD_PEER_HASH_SIZE; i++) {
529                 list_for_each_safe (ptmp, pnxt, &usock_data.ud_peers[i]) {
530                         peer = list_entry (ptmp, usock_peer_t, up_list);
531
532                         if (peer->up_ni != ni)
533                                 continue;
534
535                         usocklnd_del_peer_and_conns(peer);
536                 }
537         }
538
539         pthread_rwlock_unlock(&usock_data.ud_peers_lock);
540
541         /* wakeup all threads */
542         for (i = 0; i < usock_data.ud_npollthreads; i++)
543                 usocklnd_wakeup_pollthread(i);
544 }
545
546 void
547 usocklnd_del_peer_and_conns(usock_peer_t *peer)
548 {
549         /* peer cannot disappear because it's still in hash list */
550
551         pthread_mutex_lock(&peer->up_lock);
552         /* content of conn[] array cannot change now */
553         usocklnd_del_conns_locked(peer);
554         pthread_mutex_unlock(&peer->up_lock);
555
556         /* peer hash list is still protected by the caller */
557         list_del(&peer->up_list);
558
559         usocklnd_peer_decref(peer); /* peer isn't in hash list anymore */
560 }
561
562 void
563 usocklnd_del_conns_locked(usock_peer_t *peer)
564 {
565         int i;
566
567         for (i=0; i < N_CONN_TYPES; i++) {
568                 usock_conn_t *conn = peer->up_conns[i];
569                 if (conn != NULL)
570                         usocklnd_conn_kill(conn);
571         }
572 }