4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, Intel Corporation.
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 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 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 = PTR_ERR(kthread_run(usocklnd_poll_thread,
301 &usock_data.ud_pollthreads[i],
303 if (IS_ERR_VALUE(rc)) {
304 usocklnd_base_shutdown(i);
309 usock_data.ud_state = UD_STATE_INITIALIZED;
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:
323 usocklnd_release_poll_states(i);
324 LIBCFS_FREE (usock_data.ud_pollthreads,
325 usock_data.ud_npollthreads *
326 sizeof(usock_pollthread_t));
331 usocklnd_base_shutdown(int n)
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);
342 pthread_rwlock_destroy(&usock_data.ud_peers_lock);
344 usocklnd_release_poll_states(usock_data.ud_npollthreads);
346 LIBCFS_FREE (usock_data.ud_pollthreads,
347 usock_data.ud_npollthreads *
348 sizeof(usock_pollthread_t));
350 usock_data.ud_state = UD_STATE_INIT_NOTHING;
354 usocklnd_new_incarnation()
357 int rc = gettimeofday(&tv, NULL);
359 return (((__u64)tv.tv_sec) * 1000000) + tv.tv_usec;
363 usocklnd_assign_ni_nid(lnet_ni_t *ni)
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)' */
376 if (ni->ni_interfaces[0] == NULL) {
380 n = libcfs_ipif_enumerate(&names);
382 CERROR("Can't enumerate interfaces: %d\n", n);
386 for (i = 0; i < n; i++) {
388 if (!strcmp(names[i], "lo")) /* skip the loopback IF */
391 rc = libcfs_ipif_query(names[i], &up, &ipaddr);
393 CWARN("Can't get interface %s info: %d\n",
399 CWARN("Ignoring interface %s (down)\n",
404 break; /* one address is quite enough */
407 libcfs_ipif_free_enumeration(names, n);
410 CERROR("Can't find any usable interfaces\n");
414 CDEBUG(D_NET, "No explicit interfaces defined. "
415 "%u.%u.%u.%u used\n", HIPQUAD(ipaddr));
417 if (ni->ni_interfaces[1] != NULL) {
418 CERROR("only one explicit interface is allowed\n");
422 rc = libcfs_ipif_query(ni->ni_interfaces[0], &up, &ipaddr);
424 CERROR("Can't get interface %s info: %d\n",
425 ni->ni_interfaces[0], rc);
430 CERROR("Explicit interface defined: %s but is down\n",
431 ni->ni_interfaces[0]);
435 CDEBUG(D_NET, "Explicit interface defined: %s. "
436 "%u.%u.%u.%u used\n",
437 ni->ni_interfaces[0], HIPQUAD(ipaddr));
441 ni->ni_nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid), ipaddr);
447 usocklnd_startup(lnet_ni_t *ni)
452 if (usock_data.ud_state == UD_STATE_INIT_NOTHING) {
453 rc = usocklnd_base_startup();
458 LIBCFS_ALLOC(net, sizeof(*net));
460 goto startup_failed_0;
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);
469 rc = usocklnd_assign_ni_nid(ni);
471 goto startup_failed_1;
473 LASSERT (ni->ni_lnd == &the_tcplnd);
475 ni->ni_maxtxcredits = usock_tuns.ut_txcredits;
476 ni->ni_peertxcredits = usock_tuns.ut_peertxcredits;
478 usock_data.ud_nets_count++;
482 pthread_mutex_destroy(&net->un_lock);
483 pthread_cond_destroy(&net->un_cond);
484 LIBCFS_FREE(net, sizeof(*net));
486 if (usock_data.ud_nets_count == 0)
487 usocklnd_base_shutdown(usock_data.ud_npollthreads);
493 usocklnd_shutdown(lnet_ni_t *ni)
495 usock_net_t *net = ni->ni_data;
497 net->un_shutdown = 1;
499 usocklnd_del_all_peers(ni);
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);
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));
512 usock_data.ud_nets_count--;
513 if (usock_data.ud_nets_count == 0)
514 usocklnd_base_shutdown(usock_data.ud_npollthreads);
518 usocklnd_del_all_peers(lnet_ni_t *ni)
525 pthread_rwlock_wrlock(&usock_data.ud_peers_lock);
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);
531 if (peer->up_ni != ni)
534 usocklnd_del_peer_and_conns(peer);
538 pthread_rwlock_unlock(&usock_data.ud_peers_lock);
540 /* wakeup all threads */
541 for (i = 0; i < usock_data.ud_npollthreads; i++)
542 usocklnd_wakeup_pollthread(i);
546 usocklnd_del_peer_and_conns(usock_peer_t *peer)
548 /* peer cannot disappear because it's still in hash list */
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);
555 /* peer hash list is still protected by the caller */
556 cfs_list_del(&peer->up_list);
558 usocklnd_peer_decref(peer); /* peer isn't in hash list anymore */
562 usocklnd_del_conns_locked(usock_peer_t *peer)
566 for (i=0; i < N_CONN_TYPES; i++) {
567 usock_conn_t *conn = peer->up_conns[i];
569 usocklnd_conn_kill(conn);