1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lnet/ulnds/socklnd/usocklnd.c
38 * Author: Maxim Patlasov <maxim@clusterfs.com>
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,
53 usock_data_t usock_data;
54 usock_tunables_t usock_tuns = {
61 .ut_peertxcredits = 8,
66 #define MAX_REASONABLE_TIMEOUT 36000 /* 10 hours */
67 #define MAX_REASONABLE_NPT 1000
70 usocklnd_validate_tunables()
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);
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);
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);
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);
99 if (usock_tuns.ut_txcredits <= 0) {
100 CERROR("USOCK_TXCREDITS: %d should be positive\n",
101 usock_tuns.ut_txcredits);
105 if (usock_tuns.ut_peertxcredits <= 0) {
106 CERROR("USOCK_PEERTXCREDITS: %d should be positive\n",
107 usock_tuns.ut_peertxcredits);
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);
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);
125 if (usock_tuns.ut_sockbufsiz < 0) {
126 CERROR("USOCK_SOCKBUFSIZ: %d should be 0 or positive\n",
127 usock_tuns.ut_sockbufsiz);
135 usocklnd_release_poll_states(int n)
139 for (i = 0; i < n; i++) {
140 usock_pollthread_t *pt = &usock_data.ud_pollthreads[i];
142 libcfs_sock_release(pt->upt_notifier[0]);
143 libcfs_sock_release(pt->upt_notifier[1]);
145 pthread_mutex_destroy(&pt->upt_pollrequests_lock);
146 cfs_fini_completion(&pt->upt_completion);
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);
158 usocklnd_update_tunables()
162 rc = lnet_parse_int_tunable(&usock_tuns.ut_timeout,
167 rc = lnet_parse_int_tunable(&usock_tuns.ut_poll_timeout,
168 "USOCK_POLL_TIMEOUT");
172 rc = lnet_parse_int_tunable(&usock_tuns.ut_npollthreads,
173 "USOCK_NPOLLTHREADS");
177 rc = lnet_parse_int_tunable(&usock_tuns.ut_fair_limit,
182 rc = lnet_parse_int_tunable(&usock_tuns.ut_min_bulk,
187 rc = lnet_parse_int_tunable(&usock_tuns.ut_txcredits,
192 rc = lnet_parse_int_tunable(&usock_tuns.ut_peertxcredits,
193 "USOCK_PEERTXCREDITS");
197 rc = lnet_parse_int_tunable(&usock_tuns.ut_socknagle,
202 rc = lnet_parse_int_tunable(&usock_tuns.ut_sockbufsiz,
207 if (usocklnd_validate_tunables())
210 if (usock_tuns.ut_npollthreads == 0) {
211 usock_tuns.ut_npollthreads = cfs_online_cpus();
213 if (usock_tuns.ut_npollthreads <= 0) {
214 CERROR("Cannot find out the number of online CPUs\n");
224 usocklnd_base_startup()
226 usock_pollthread_t *pt;
230 rc = usocklnd_update_tunables();
234 usock_data.ud_npollthreads = usock_tuns.ut_npollthreads;
236 LIBCFS_ALLOC (usock_data.ud_pollthreads,
237 usock_data.ud_npollthreads *
238 sizeof(usock_pollthread_t));
239 if (usock_data.ud_pollthreads == NULL)
242 /* Initialize poll thread state structures */
243 for (i = 0; i < usock_data.ud_npollthreads; i++) {
245 pt = &usock_data.ud_pollthreads[i];
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;
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;
259 LIBCFS_ALLOC (pt->upt_fd2idx,
260 sizeof(int) * UPT_START_SIZ);
261 if (pt->upt_fd2idx == NULL)
262 goto base_startup_failed_2;
264 memset(pt->upt_fd2idx, 0,
265 sizeof(int) * UPT_START_SIZ);
267 LIBCFS_ALLOC (pt->upt_skip,
268 sizeof(int) * UPT_START_SIZ);
269 if (pt->upt_skip == NULL)
270 goto base_startup_failed_3;
272 pt->upt_npollfd = pt->upt_nfd2idx = UPT_START_SIZ;
274 rc = libcfs_socketpair(pt->upt_notifier);
276 goto base_startup_failed_4;
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;
283 pt->upt_idx2conn[0] = NULL;
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);
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]);
296 pthread_rwlock_init(&usock_data.ud_peers_lock, NULL);
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]);
303 usocklnd_base_shutdown(i);
308 usock_data.ud_state = UD_STATE_INITIALIZED;
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:
322 usocklnd_release_poll_states(i);
323 LIBCFS_FREE (usock_data.ud_pollthreads,
324 usock_data.ud_npollthreads *
325 sizeof(usock_pollthread_t));
330 usocklnd_base_shutdown(int n)
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);
341 pthread_rwlock_destroy(&usock_data.ud_peers_lock);
343 usocklnd_release_poll_states(usock_data.ud_npollthreads);
345 LIBCFS_FREE (usock_data.ud_pollthreads,
346 usock_data.ud_npollthreads *
347 sizeof(usock_pollthread_t));
349 usock_data.ud_state = UD_STATE_INIT_NOTHING;
353 usocklnd_new_incarnation()
356 int rc = gettimeofday(&tv, NULL);
358 return (((__u64)tv.tv_sec) * 1000000) + tv.tv_usec;
362 usocklnd_assign_ni_nid(lnet_ni_t *ni)
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)' */
375 if (ni->ni_interfaces[0] == NULL) {
379 n = libcfs_ipif_enumerate(&names);
381 CERROR("Can't enumerate interfaces: %d\n", n);
385 for (i = 0; i < n; i++) {
387 if (!strcmp(names[i], "lo")) /* skip the loopback IF */
390 rc = libcfs_ipif_query(names[i], &up, &ipaddr);
392 CWARN("Can't get interface %s info: %d\n",
398 CWARN("Ignoring interface %s (down)\n",
403 break; /* one address is quite enough */
406 libcfs_ipif_free_enumeration(names, n);
409 CERROR("Can't find any usable interfaces\n");
413 CDEBUG(D_NET, "No explicit interfaces defined. "
414 "%u.%u.%u.%u used\n", HIPQUAD(ipaddr));
416 if (ni->ni_interfaces[1] != NULL) {
417 CERROR("only one explicit interface is allowed\n");
421 rc = libcfs_ipif_query(ni->ni_interfaces[0], &up, &ipaddr);
423 CERROR("Can't get interface %s info: %d\n",
424 ni->ni_interfaces[0], rc);
429 CERROR("Explicit interface defined: %s but is down\n",
430 ni->ni_interfaces[0]);
434 CDEBUG(D_NET, "Explicit interface defined: %s. "
435 "%u.%u.%u.%u used\n",
436 ni->ni_interfaces[0], HIPQUAD(ipaddr));
440 ni->ni_nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid), ipaddr);
446 usocklnd_startup(lnet_ni_t *ni)
451 if (usock_data.ud_state == UD_STATE_INIT_NOTHING) {
452 rc = usocklnd_base_startup();
457 LIBCFS_ALLOC(net, sizeof(*net));
459 goto startup_failed_0;
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);
468 if (!(the_lnet.ln_pid & LNET_PID_USERFLAG)) {
469 rc = usocklnd_assign_ni_nid(ni);
471 goto startup_failed_1;
474 LASSERT (ni->ni_lnd == &the_tcplnd);
476 ni->ni_maxtxcredits = usock_tuns.ut_txcredits;
477 ni->ni_peertxcredits = usock_tuns.ut_peertxcredits;
479 usock_data.ud_nets_count++;
483 pthread_mutex_destroy(&net->un_lock);
484 pthread_cond_destroy(&net->un_cond);
485 LIBCFS_FREE(net, sizeof(*net));
487 if (usock_data.ud_nets_count == 0)
488 usocklnd_base_shutdown(usock_data.ud_npollthreads);
494 usocklnd_shutdown(lnet_ni_t *ni)
496 usock_net_t *net = ni->ni_data;
498 net->un_shutdown = 1;
500 usocklnd_del_all_peers(ni);
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);
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));
513 usock_data.ud_nets_count--;
514 if (usock_data.ud_nets_count == 0)
515 usocklnd_base_shutdown(usock_data.ud_npollthreads);
519 usocklnd_del_all_peers(lnet_ni_t *ni)
521 struct list_head *ptmp;
522 struct list_head *pnxt;
526 pthread_rwlock_wrlock(&usock_data.ud_peers_lock);
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);
532 if (peer->up_ni != ni)
535 usocklnd_del_peer_and_conns(peer);
539 pthread_rwlock_unlock(&usock_data.ud_peers_lock);
541 /* wakeup all threads */
542 for (i = 0; i < usock_data.ud_npollthreads; i++)
543 usocklnd_wakeup_pollthread(i);
547 usocklnd_del_peer_and_conns(usock_peer_t *peer)
549 /* peer cannot disappear because it's still in hash list */
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);
556 /* peer hash list is still protected by the caller */
557 list_del(&peer->up_list);
559 usocklnd_peer_decref(peer); /* peer isn't in hash list anymore */
563 usocklnd_del_conns_locked(usock_peer_t *peer)
567 for (i=0; i < N_CONN_TYPES; i++) {
568 usock_conn_t *conn = peer->up_conns[i];
570 usocklnd_conn_kill(conn);