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/klnds/socklnd/socklnd.c
38 * Author: Zach Brown <zab@zabbo.net>
39 * Author: Peter J. Braam <braam@clusterfs.com>
40 * Author: Phil Schwan <phil@clusterfs.com>
41 * Author: Eric Barton <eric@bartonsoftware.com>
47 ksock_nal_data_t ksocknal_data;
50 ksocknal_ip2iface(lnet_ni_t *ni, __u32 ip)
52 ksock_net_t *net = ni->ni_data;
54 ksock_interface_t *iface;
56 for (i = 0; i < net->ksnn_ninterfaces; i++) {
57 LASSERT(i < LNET_MAX_INTERFACES);
58 iface = &net->ksnn_interfaces[i];
60 if (iface->ksni_ipaddr == ip)
68 ksocknal_create_route (__u32 ipaddr, int port)
72 LIBCFS_ALLOC (route, sizeof (*route));
76 cfs_atomic_set (&route->ksnr_refcount, 1);
77 route->ksnr_peer = NULL;
78 route->ksnr_retry_interval = 0; /* OK to connect at any time */
79 route->ksnr_ipaddr = ipaddr;
80 route->ksnr_port = port;
81 route->ksnr_scheduled = 0;
82 route->ksnr_connecting = 0;
83 route->ksnr_connected = 0;
84 route->ksnr_deleted = 0;
85 route->ksnr_conn_count = 0;
86 route->ksnr_share_count = 0;
92 ksocknal_destroy_route (ksock_route_t *route)
94 LASSERT (cfs_atomic_read(&route->ksnr_refcount) == 0);
96 if (route->ksnr_peer != NULL)
97 ksocknal_peer_decref(route->ksnr_peer);
99 LIBCFS_FREE (route, sizeof (*route));
103 ksocknal_create_peer (ksock_peer_t **peerp, lnet_ni_t *ni, lnet_process_id_t id)
105 ksock_net_t *net = ni->ni_data;
108 LASSERT (id.nid != LNET_NID_ANY);
109 LASSERT (id.pid != LNET_PID_ANY);
110 LASSERT (!cfs_in_interrupt());
112 LIBCFS_ALLOC (peer, sizeof (*peer));
116 memset (peer, 0, sizeof (*peer)); /* NULL pointers/clear flags etc */
120 cfs_atomic_set (&peer->ksnp_refcount, 1); /* 1 ref for caller */
121 peer->ksnp_closing = 0;
122 peer->ksnp_accepting = 0;
123 peer->ksnp_proto = NULL;
124 peer->ksnp_last_alive = 0;
125 peer->ksnp_zc_next_cookie = SOCKNAL_KEEPALIVE_PING + 1;
127 CFS_INIT_LIST_HEAD (&peer->ksnp_conns);
128 CFS_INIT_LIST_HEAD (&peer->ksnp_routes);
129 CFS_INIT_LIST_HEAD (&peer->ksnp_tx_queue);
130 CFS_INIT_LIST_HEAD (&peer->ksnp_zc_req_list);
131 cfs_spin_lock_init(&peer->ksnp_lock);
133 cfs_spin_lock_bh (&net->ksnn_lock);
135 if (net->ksnn_shutdown) {
136 cfs_spin_unlock_bh (&net->ksnn_lock);
138 LIBCFS_FREE(peer, sizeof(*peer));
139 CERROR("Can't create peer: network shutdown\n");
145 cfs_spin_unlock_bh (&net->ksnn_lock);
152 ksocknal_destroy_peer (ksock_peer_t *peer)
154 ksock_net_t *net = peer->ksnp_ni->ni_data;
156 CDEBUG (D_NET, "peer %s %p deleted\n",
157 libcfs_id2str(peer->ksnp_id), peer);
159 LASSERT (cfs_atomic_read (&peer->ksnp_refcount) == 0);
160 LASSERT (peer->ksnp_accepting == 0);
161 LASSERT (cfs_list_empty (&peer->ksnp_conns));
162 LASSERT (cfs_list_empty (&peer->ksnp_routes));
163 LASSERT (cfs_list_empty (&peer->ksnp_tx_queue));
164 LASSERT (cfs_list_empty (&peer->ksnp_zc_req_list));
166 LIBCFS_FREE (peer, sizeof (*peer));
168 /* NB a peer's connections and routes keep a reference on their peer
169 * until they are destroyed, so we can be assured that _all_ state to
170 * do with this peer has been cleaned up when its refcount drops to
172 cfs_spin_lock_bh (&net->ksnn_lock);
174 cfs_spin_unlock_bh (&net->ksnn_lock);
178 ksocknal_find_peer_locked (lnet_ni_t *ni, lnet_process_id_t id)
180 cfs_list_t *peer_list = ksocknal_nid2peerlist(id.nid);
184 cfs_list_for_each (tmp, peer_list) {
186 peer = cfs_list_entry (tmp, ksock_peer_t, ksnp_list);
188 LASSERT (!peer->ksnp_closing);
190 if (peer->ksnp_ni != ni)
193 if (peer->ksnp_id.nid != id.nid ||
194 peer->ksnp_id.pid != id.pid)
197 CDEBUG(D_NET, "got peer [%p] -> %s (%d)\n",
198 peer, libcfs_id2str(id),
199 cfs_atomic_read(&peer->ksnp_refcount));
206 ksocknal_find_peer (lnet_ni_t *ni, lnet_process_id_t id)
210 cfs_read_lock (&ksocknal_data.ksnd_global_lock);
211 peer = ksocknal_find_peer_locked (ni, id);
212 if (peer != NULL) /* +1 ref for caller? */
213 ksocknal_peer_addref(peer);
214 cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
220 ksocknal_unlink_peer_locked (ksock_peer_t *peer)
224 ksock_interface_t *iface;
226 for (i = 0; i < peer->ksnp_n_passive_ips; i++) {
227 LASSERT (i < LNET_MAX_INTERFACES);
228 ip = peer->ksnp_passive_ips[i];
230 iface = ksocknal_ip2iface(peer->ksnp_ni, ip);
231 /* All IPs in peer->ksnp_passive_ips[] come from the
232 * interface list, therefore the call must succeed. */
233 LASSERT (iface != NULL);
235 CDEBUG(D_NET, "peer=%p iface=%p ksni_nroutes=%d\n",
236 peer, iface, iface->ksni_nroutes);
237 iface->ksni_npeers--;
240 LASSERT (cfs_list_empty(&peer->ksnp_conns));
241 LASSERT (cfs_list_empty(&peer->ksnp_routes));
242 LASSERT (!peer->ksnp_closing);
243 peer->ksnp_closing = 1;
244 cfs_list_del (&peer->ksnp_list);
245 /* lose peerlist's ref */
246 ksocknal_peer_decref(peer);
250 ksocknal_get_peer_info (lnet_ni_t *ni, int index,
251 lnet_process_id_t *id, __u32 *myip, __u32 *peer_ip,
252 int *port, int *conn_count, int *share_count)
256 ksock_route_t *route;
262 cfs_read_lock (&ksocknal_data.ksnd_global_lock);
264 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
266 cfs_list_for_each (ptmp, &ksocknal_data.ksnd_peers[i]) {
267 peer = cfs_list_entry (ptmp, ksock_peer_t, ksnp_list);
269 if (peer->ksnp_ni != ni)
272 if (peer->ksnp_n_passive_ips == 0 &&
273 cfs_list_empty(&peer->ksnp_routes)) {
287 for (j = 0; j < peer->ksnp_n_passive_ips; j++) {
292 *myip = peer->ksnp_passive_ips[j];
301 cfs_list_for_each (rtmp, &peer->ksnp_routes) {
305 route = cfs_list_entry(rtmp, ksock_route_t,
309 *myip = route->ksnr_myipaddr;
310 *peer_ip = route->ksnr_ipaddr;
311 *port = route->ksnr_port;
312 *conn_count = route->ksnr_conn_count;
313 *share_count = route->ksnr_share_count;
320 cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
325 ksocknal_associate_route_conn_locked(ksock_route_t *route, ksock_conn_t *conn)
327 ksock_peer_t *peer = route->ksnr_peer;
328 int type = conn->ksnc_type;
329 ksock_interface_t *iface;
331 conn->ksnc_route = route;
332 ksocknal_route_addref(route);
334 if (route->ksnr_myipaddr != conn->ksnc_myipaddr) {
335 if (route->ksnr_myipaddr == 0) {
336 /* route wasn't bound locally yet (the initial route) */
337 CDEBUG(D_NET, "Binding %s %u.%u.%u.%u to %u.%u.%u.%u\n",
338 libcfs_id2str(peer->ksnp_id),
339 HIPQUAD(route->ksnr_ipaddr),
340 HIPQUAD(conn->ksnc_myipaddr));
342 CDEBUG(D_NET, "Rebinding %s %u.%u.%u.%u from "
343 "%u.%u.%u.%u to %u.%u.%u.%u\n",
344 libcfs_id2str(peer->ksnp_id),
345 HIPQUAD(route->ksnr_ipaddr),
346 HIPQUAD(route->ksnr_myipaddr),
347 HIPQUAD(conn->ksnc_myipaddr));
349 iface = ksocknal_ip2iface(route->ksnr_peer->ksnp_ni,
350 route->ksnr_myipaddr);
352 iface->ksni_nroutes--;
354 route->ksnr_myipaddr = conn->ksnc_myipaddr;
355 iface = ksocknal_ip2iface(route->ksnr_peer->ksnp_ni,
356 route->ksnr_myipaddr);
358 iface->ksni_nroutes++;
361 route->ksnr_connected |= (1<<type);
362 route->ksnr_conn_count++;
364 /* Successful connection => further attempts can
365 * proceed immediately */
366 route->ksnr_retry_interval = 0;
370 ksocknal_add_route_locked (ksock_peer_t *peer, ksock_route_t *route)
374 ksock_route_t *route2;
376 LASSERT (!peer->ksnp_closing);
377 LASSERT (route->ksnr_peer == NULL);
378 LASSERT (!route->ksnr_scheduled);
379 LASSERT (!route->ksnr_connecting);
380 LASSERT (route->ksnr_connected == 0);
382 /* LASSERT(unique) */
383 cfs_list_for_each(tmp, &peer->ksnp_routes) {
384 route2 = cfs_list_entry(tmp, ksock_route_t, ksnr_list);
386 if (route2->ksnr_ipaddr == route->ksnr_ipaddr) {
387 CERROR ("Duplicate route %s %u.%u.%u.%u\n",
388 libcfs_id2str(peer->ksnp_id),
389 HIPQUAD(route->ksnr_ipaddr));
394 route->ksnr_peer = peer;
395 ksocknal_peer_addref(peer);
396 /* peer's routelist takes over my ref on 'route' */
397 cfs_list_add_tail(&route->ksnr_list, &peer->ksnp_routes);
399 cfs_list_for_each(tmp, &peer->ksnp_conns) {
400 conn = cfs_list_entry(tmp, ksock_conn_t, ksnc_list);
402 if (conn->ksnc_ipaddr != route->ksnr_ipaddr)
405 ksocknal_associate_route_conn_locked(route, conn);
406 /* keep going (typed routes) */
411 ksocknal_del_route_locked (ksock_route_t *route)
413 ksock_peer_t *peer = route->ksnr_peer;
414 ksock_interface_t *iface;
419 LASSERT (!route->ksnr_deleted);
421 /* Close associated conns */
422 cfs_list_for_each_safe (ctmp, cnxt, &peer->ksnp_conns) {
423 conn = cfs_list_entry(ctmp, ksock_conn_t, ksnc_list);
425 if (conn->ksnc_route != route)
428 ksocknal_close_conn_locked (conn, 0);
431 if (route->ksnr_myipaddr != 0) {
432 iface = ksocknal_ip2iface(route->ksnr_peer->ksnp_ni,
433 route->ksnr_myipaddr);
435 iface->ksni_nroutes--;
438 route->ksnr_deleted = 1;
439 cfs_list_del (&route->ksnr_list);
440 ksocknal_route_decref(route); /* drop peer's ref */
442 if (cfs_list_empty (&peer->ksnp_routes) &&
443 cfs_list_empty (&peer->ksnp_conns)) {
444 /* I've just removed the last route to a peer with no active
446 ksocknal_unlink_peer_locked (peer);
451 ksocknal_add_peer (lnet_ni_t *ni, lnet_process_id_t id, __u32 ipaddr, int port)
456 ksock_route_t *route;
457 ksock_route_t *route2;
460 if (id.nid == LNET_NID_ANY ||
461 id.pid == LNET_PID_ANY)
464 /* Have a brand new peer ready... */
465 rc = ksocknal_create_peer(&peer, ni, id);
469 route = ksocknal_create_route (ipaddr, port);
471 ksocknal_peer_decref(peer);
475 cfs_write_lock_bh (&ksocknal_data.ksnd_global_lock);
477 /* always called with a ref on ni, so shutdown can't have started */
478 LASSERT (((ksock_net_t *) ni->ni_data)->ksnn_shutdown == 0);
480 peer2 = ksocknal_find_peer_locked (ni, id);
482 ksocknal_peer_decref(peer);
485 /* peer table takes my ref on peer */
486 cfs_list_add_tail (&peer->ksnp_list,
487 ksocknal_nid2peerlist (id.nid));
491 cfs_list_for_each (tmp, &peer->ksnp_routes) {
492 route2 = cfs_list_entry(tmp, ksock_route_t, ksnr_list);
494 if (route2->ksnr_ipaddr == ipaddr)
499 if (route2 == NULL) {
500 ksocknal_add_route_locked(peer, route);
501 route->ksnr_share_count++;
503 ksocknal_route_decref(route);
504 route2->ksnr_share_count++;
507 cfs_write_unlock_bh (&ksocknal_data.ksnd_global_lock);
513 ksocknal_del_peer_locked (ksock_peer_t *peer, __u32 ip)
516 ksock_route_t *route;
521 LASSERT (!peer->ksnp_closing);
523 /* Extra ref prevents peer disappearing until I'm done with it */
524 ksocknal_peer_addref(peer);
526 cfs_list_for_each_safe (tmp, nxt, &peer->ksnp_routes) {
527 route = cfs_list_entry(tmp, ksock_route_t, ksnr_list);
530 if (!(ip == 0 || route->ksnr_ipaddr == ip))
533 route->ksnr_share_count = 0;
534 /* This deletes associated conns too */
535 ksocknal_del_route_locked (route);
539 cfs_list_for_each_safe (tmp, nxt, &peer->ksnp_routes) {
540 route = cfs_list_entry(tmp, ksock_route_t, ksnr_list);
541 nshared += route->ksnr_share_count;
545 /* remove everything else if there are no explicit entries
548 cfs_list_for_each_safe (tmp, nxt, &peer->ksnp_routes) {
549 route = cfs_list_entry(tmp, ksock_route_t, ksnr_list);
551 /* we should only be removing auto-entries */
552 LASSERT(route->ksnr_share_count == 0);
553 ksocknal_del_route_locked (route);
556 cfs_list_for_each_safe (tmp, nxt, &peer->ksnp_conns) {
557 conn = cfs_list_entry(tmp, ksock_conn_t, ksnc_list);
559 ksocknal_close_conn_locked(conn, 0);
563 ksocknal_peer_decref(peer);
564 /* NB peer unlinks itself when last conn/route is removed */
568 ksocknal_del_peer (lnet_ni_t *ni, lnet_process_id_t id, __u32 ip)
570 CFS_LIST_HEAD (zombies);
579 cfs_write_lock_bh (&ksocknal_data.ksnd_global_lock);
581 if (id.nid != LNET_NID_ANY)
582 lo = hi = (int)(ksocknal_nid2peerlist(id.nid) - ksocknal_data.ksnd_peers);
585 hi = ksocknal_data.ksnd_peer_hash_size - 1;
588 for (i = lo; i <= hi; i++) {
589 cfs_list_for_each_safe (ptmp, pnxt,
590 &ksocknal_data.ksnd_peers[i]) {
591 peer = cfs_list_entry (ptmp, ksock_peer_t, ksnp_list);
593 if (peer->ksnp_ni != ni)
596 if (!((id.nid == LNET_NID_ANY || peer->ksnp_id.nid == id.nid) &&
597 (id.pid == LNET_PID_ANY || peer->ksnp_id.pid == id.pid)))
600 ksocknal_peer_addref(peer); /* a ref for me... */
602 ksocknal_del_peer_locked (peer, ip);
604 if (peer->ksnp_closing &&
605 !cfs_list_empty(&peer->ksnp_tx_queue)) {
606 LASSERT (cfs_list_empty(&peer->ksnp_conns));
607 LASSERT (cfs_list_empty(&peer->ksnp_routes));
609 cfs_list_splice_init(&peer->ksnp_tx_queue,
613 ksocknal_peer_decref(peer); /* ...till here */
615 rc = 0; /* matched! */
619 cfs_write_unlock_bh (&ksocknal_data.ksnd_global_lock);
621 ksocknal_txlist_done(ni, &zombies, 1);
627 ksocknal_get_conn_by_idx (lnet_ni_t *ni, int index)
635 cfs_read_lock (&ksocknal_data.ksnd_global_lock);
637 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
638 cfs_list_for_each (ptmp, &ksocknal_data.ksnd_peers[i]) {
639 peer = cfs_list_entry (ptmp, ksock_peer_t, ksnp_list);
641 LASSERT (!peer->ksnp_closing);
643 if (peer->ksnp_ni != ni)
646 cfs_list_for_each (ctmp, &peer->ksnp_conns) {
650 conn = cfs_list_entry (ctmp, ksock_conn_t,
652 ksocknal_conn_addref(conn);
653 cfs_read_unlock (&ksocknal_data. \
660 cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
665 ksocknal_choose_scheduler_locked (unsigned int irq)
667 ksock_sched_t *sched;
668 ksock_irqinfo_t *info;
671 LASSERT (irq < CFS_NR_IRQS);
672 info = &ksocknal_data.ksnd_irqinfo[irq];
674 if (irq != 0 && /* hardware NIC */
675 info->ksni_valid) { /* already set up */
676 return (&ksocknal_data.ksnd_schedulers[info->ksni_sched]);
679 /* software NIC (irq == 0) || not associated with a scheduler yet.
680 * Choose the CPU with the fewest connections... */
681 sched = &ksocknal_data.ksnd_schedulers[0];
682 for (i = 1; i < ksocknal_data.ksnd_nschedulers; i++)
683 if (sched->kss_nconns >
684 ksocknal_data.ksnd_schedulers[i].kss_nconns)
685 sched = &ksocknal_data.ksnd_schedulers[i];
687 if (irq != 0) { /* Hardware NIC */
688 info->ksni_valid = 1;
689 info->ksni_sched = (unsigned int)(sched - ksocknal_data.ksnd_schedulers);
692 LASSERT (info->ksni_sched == (unsigned int)(sched - ksocknal_data.ksnd_schedulers));
699 ksocknal_local_ipvec (lnet_ni_t *ni, __u32 *ipaddrs)
701 ksock_net_t *net = ni->ni_data;
705 cfs_read_lock (&ksocknal_data.ksnd_global_lock);
707 nip = net->ksnn_ninterfaces;
708 LASSERT (nip <= LNET_MAX_INTERFACES);
710 /* Only offer interfaces for additional connections if I have
713 cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
717 for (i = 0; i < nip; i++) {
718 ipaddrs[i] = net->ksnn_interfaces[i].ksni_ipaddr;
719 LASSERT (ipaddrs[i] != 0);
722 cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
727 ksocknal_match_peerip (ksock_interface_t *iface, __u32 *ips, int nips)
729 int best_netmatch = 0;
736 for (i = 0; i < nips; i++) {
740 this_xor = (ips[i] ^ iface->ksni_ipaddr);
741 this_netmatch = ((this_xor & iface->ksni_netmask) == 0) ? 1 : 0;
744 best_netmatch < this_netmatch ||
745 (best_netmatch == this_netmatch &&
746 best_xor > this_xor)))
750 best_netmatch = this_netmatch;
759 ksocknal_select_ips(ksock_peer_t *peer, __u32 *peerips, int n_peerips)
761 cfs_rwlock_t *global_lock = &ksocknal_data.ksnd_global_lock;
762 ksock_net_t *net = peer->ksnp_ni->ni_data;
763 ksock_interface_t *iface;
764 ksock_interface_t *best_iface;
775 /* CAVEAT EMPTOR: We do all our interface matching with an
776 * exclusive hold of global lock at IRQ priority. We're only
777 * expecting to be dealing with small numbers of interfaces, so the
778 * O(n**3)-ness shouldn't matter */
780 /* Also note that I'm not going to return more than n_peerips
781 * interfaces, even if I have more myself */
783 cfs_write_lock_bh (global_lock);
785 LASSERT (n_peerips <= LNET_MAX_INTERFACES);
786 LASSERT (net->ksnn_ninterfaces <= LNET_MAX_INTERFACES);
788 /* Only match interfaces for additional connections
789 * if I have > 1 interface */
790 n_ips = (net->ksnn_ninterfaces < 2) ? 0 :
791 MIN(n_peerips, net->ksnn_ninterfaces);
793 for (i = 0; peer->ksnp_n_passive_ips < n_ips; i++) {
794 /* ^ yes really... */
796 /* If we have any new interfaces, first tick off all the
797 * peer IPs that match old interfaces, then choose new
798 * interfaces to match the remaining peer IPS.
799 * We don't forget interfaces we've stopped using; we might
800 * start using them again... */
802 if (i < peer->ksnp_n_passive_ips) {
804 ip = peer->ksnp_passive_ips[i];
805 best_iface = ksocknal_ip2iface(peer->ksnp_ni, ip);
807 /* peer passive ips are kept up to date */
808 LASSERT(best_iface != NULL);
810 /* choose a new interface */
811 LASSERT (i == peer->ksnp_n_passive_ips);
817 for (j = 0; j < net->ksnn_ninterfaces; j++) {
818 iface = &net->ksnn_interfaces[j];
819 ip = iface->ksni_ipaddr;
821 for (k = 0; k < peer->ksnp_n_passive_ips; k++)
822 if (peer->ksnp_passive_ips[k] == ip)
825 if (k < peer->ksnp_n_passive_ips) /* using it already */
828 k = ksocknal_match_peerip(iface, peerips, n_peerips);
829 xor = (ip ^ peerips[k]);
830 this_netmatch = ((xor & iface->ksni_netmask) == 0) ? 1 : 0;
832 if (!(best_iface == NULL ||
833 best_netmatch < this_netmatch ||
834 (best_netmatch == this_netmatch &&
835 best_npeers > iface->ksni_npeers)))
839 best_netmatch = this_netmatch;
840 best_npeers = iface->ksni_npeers;
843 best_iface->ksni_npeers++;
844 ip = best_iface->ksni_ipaddr;
845 peer->ksnp_passive_ips[i] = ip;
846 peer->ksnp_n_passive_ips = i+1;
849 LASSERT (best_iface != NULL);
851 /* mark the best matching peer IP used */
852 j = ksocknal_match_peerip(best_iface, peerips, n_peerips);
856 /* Overwrite input peer IP addresses */
857 memcpy(peerips, peer->ksnp_passive_ips, n_ips * sizeof(*peerips));
859 cfs_write_unlock_bh (global_lock);
865 ksocknal_create_routes(ksock_peer_t *peer, int port,
866 __u32 *peer_ipaddrs, int npeer_ipaddrs)
868 ksock_route_t *newroute = NULL;
869 cfs_rwlock_t *global_lock = &ksocknal_data.ksnd_global_lock;
870 lnet_ni_t *ni = peer->ksnp_ni;
871 ksock_net_t *net = ni->ni_data;
873 ksock_route_t *route;
874 ksock_interface_t *iface;
875 ksock_interface_t *best_iface;
882 /* CAVEAT EMPTOR: We do all our interface matching with an
883 * exclusive hold of global lock at IRQ priority. We're only
884 * expecting to be dealing with small numbers of interfaces, so the
885 * O(n**3)-ness here shouldn't matter */
887 cfs_write_lock_bh (global_lock);
889 if (net->ksnn_ninterfaces < 2) {
890 /* Only create additional connections
891 * if I have > 1 interface */
892 cfs_write_unlock_bh (global_lock);
896 LASSERT (npeer_ipaddrs <= LNET_MAX_INTERFACES);
898 for (i = 0; i < npeer_ipaddrs; i++) {
899 if (newroute != NULL) {
900 newroute->ksnr_ipaddr = peer_ipaddrs[i];
902 cfs_write_unlock_bh (global_lock);
904 newroute = ksocknal_create_route(peer_ipaddrs[i], port);
905 if (newroute == NULL)
908 cfs_write_lock_bh (global_lock);
911 if (peer->ksnp_closing) {
912 /* peer got closed under me */
916 /* Already got a route? */
918 cfs_list_for_each(rtmp, &peer->ksnp_routes) {
919 route = cfs_list_entry(rtmp, ksock_route_t, ksnr_list);
921 if (route->ksnr_ipaddr == newroute->ksnr_ipaddr)
933 LASSERT (net->ksnn_ninterfaces <= LNET_MAX_INTERFACES);
935 /* Select interface to connect from */
936 for (j = 0; j < net->ksnn_ninterfaces; j++) {
937 iface = &net->ksnn_interfaces[j];
939 /* Using this interface already? */
940 cfs_list_for_each(rtmp, &peer->ksnp_routes) {
941 route = cfs_list_entry(rtmp, ksock_route_t,
944 if (route->ksnr_myipaddr == iface->ksni_ipaddr)
952 this_netmatch = (((iface->ksni_ipaddr ^
953 newroute->ksnr_ipaddr) &
954 iface->ksni_netmask) == 0) ? 1 : 0;
956 if (!(best_iface == NULL ||
957 best_netmatch < this_netmatch ||
958 (best_netmatch == this_netmatch &&
959 best_nroutes > iface->ksni_nroutes)))
963 best_netmatch = this_netmatch;
964 best_nroutes = iface->ksni_nroutes;
967 if (best_iface == NULL)
970 newroute->ksnr_myipaddr = best_iface->ksni_ipaddr;
971 best_iface->ksni_nroutes++;
973 ksocknal_add_route_locked(peer, newroute);
977 cfs_write_unlock_bh (global_lock);
978 if (newroute != NULL)
979 ksocknal_route_decref(newroute);
983 ksocknal_accept (lnet_ni_t *ni, cfs_socket_t *sock)
990 rc = libcfs_sock_getaddr(sock, 1, &peer_ip, &peer_port);
991 LASSERT (rc == 0); /* we succeeded before */
993 LIBCFS_ALLOC(cr, sizeof(*cr));
995 LCONSOLE_ERROR_MSG(0x12f, "Dropping connection request from "
996 "%u.%u.%u.%u: memory exhausted\n",
1003 cr->ksncr_sock = sock;
1005 cfs_spin_lock_bh (&ksocknal_data.ksnd_connd_lock);
1007 cfs_list_add_tail(&cr->ksncr_list, &ksocknal_data.ksnd_connd_connreqs);
1008 cfs_waitq_signal(&ksocknal_data.ksnd_connd_waitq);
1010 cfs_spin_unlock_bh (&ksocknal_data.ksnd_connd_lock);
1015 ksocknal_connecting (ksock_peer_t *peer, __u32 ipaddr)
1017 ksock_route_t *route;
1019 cfs_list_for_each_entry_typed (route, &peer->ksnp_routes,
1020 ksock_route_t, ksnr_list) {
1022 if (route->ksnr_ipaddr == ipaddr)
1023 return route->ksnr_connecting;
1029 ksocknal_create_conn (lnet_ni_t *ni, ksock_route_t *route,
1030 cfs_socket_t *sock, int type)
1032 cfs_rwlock_t *global_lock = &ksocknal_data.ksnd_global_lock;
1033 CFS_LIST_HEAD (zombies);
1034 lnet_process_id_t peerid;
1038 ksock_conn_t *conn2;
1039 ksock_peer_t *peer = NULL;
1040 ksock_peer_t *peer2;
1041 ksock_sched_t *sched;
1042 ksock_hello_msg_t *hello;
1050 active = (route != NULL);
1052 LASSERT (active == (type != SOCKLND_CONN_NONE));
1054 irq = ksocknal_lib_sock_irq (sock);
1056 LIBCFS_ALLOC(conn, sizeof(*conn));
1062 memset (conn, 0, sizeof (*conn));
1064 conn->ksnc_peer = NULL;
1065 conn->ksnc_route = NULL;
1066 conn->ksnc_sock = sock;
1067 /* 2 ref, 1 for conn, another extra ref prevents socket
1068 * being closed before establishment of connection */
1069 cfs_atomic_set (&conn->ksnc_sock_refcount, 2);
1070 conn->ksnc_type = type;
1071 ksocknal_lib_save_callback(sock, conn);
1072 cfs_atomic_set (&conn->ksnc_conn_refcount, 1); /* 1 ref for me */
1074 conn->ksnc_rx_ready = 0;
1075 conn->ksnc_rx_scheduled = 0;
1077 CFS_INIT_LIST_HEAD (&conn->ksnc_tx_queue);
1078 conn->ksnc_tx_ready = 0;
1079 conn->ksnc_tx_scheduled = 0;
1080 conn->ksnc_tx_carrier = NULL;
1081 cfs_atomic_set (&conn->ksnc_tx_nob, 0);
1083 LIBCFS_ALLOC(hello, offsetof(ksock_hello_msg_t,
1084 kshm_ips[LNET_MAX_INTERFACES]));
1085 if (hello == NULL) {
1090 /* stash conn's local and remote addrs */
1091 rc = ksocknal_lib_get_conn_addrs (conn);
1095 /* Find out/confirm peer's NID and connection type and get the
1096 * vector of interfaces she's willing to let me connect to.
1097 * Passive connections use the listener timeout since the peer sends
1101 peer = route->ksnr_peer;
1102 LASSERT(ni == peer->ksnp_ni);
1104 /* Active connection sends HELLO eagerly */
1105 hello->kshm_nips = ksocknal_local_ipvec(ni, hello->kshm_ips);
1106 peerid = peer->ksnp_id;
1108 cfs_write_lock_bh(global_lock);
1109 conn->ksnc_proto = peer->ksnp_proto;
1110 cfs_write_unlock_bh(global_lock);
1112 if (conn->ksnc_proto == NULL) {
1113 conn->ksnc_proto = &ksocknal_protocol_v3x;
1114 #if SOCKNAL_VERSION_DEBUG
1115 if (*ksocknal_tunables.ksnd_protocol == 2)
1116 conn->ksnc_proto = &ksocknal_protocol_v2x;
1117 else if (*ksocknal_tunables.ksnd_protocol == 1)
1118 conn->ksnc_proto = &ksocknal_protocol_v1x;
1122 rc = ksocknal_send_hello (ni, conn, peerid.nid, hello);
1126 peerid.nid = LNET_NID_ANY;
1127 peerid.pid = LNET_PID_ANY;
1129 /* Passive, get protocol from peer */
1130 conn->ksnc_proto = NULL;
1133 rc = ksocknal_recv_hello (ni, conn, hello, &peerid, &incarnation);
1137 LASSERT (rc == 0 || active);
1138 LASSERT (conn->ksnc_proto != NULL);
1139 LASSERT (peerid.nid != LNET_NID_ANY);
1142 ksocknal_peer_addref(peer);
1143 cfs_write_lock_bh (global_lock);
1145 rc = ksocknal_create_peer(&peer, ni, peerid);
1149 cfs_write_lock_bh (global_lock);
1151 /* called with a ref on ni, so shutdown can't have started */
1152 LASSERT (((ksock_net_t *) ni->ni_data)->ksnn_shutdown == 0);
1154 peer2 = ksocknal_find_peer_locked(ni, peerid);
1155 if (peer2 == NULL) {
1156 /* NB this puts an "empty" peer in the peer
1157 * table (which takes my ref) */
1158 cfs_list_add_tail(&peer->ksnp_list,
1159 ksocknal_nid2peerlist(peerid.nid));
1161 ksocknal_peer_decref(peer);
1166 ksocknal_peer_addref(peer);
1167 peer->ksnp_accepting++;
1169 /* Am I already connecting to this guy? Resolve in
1170 * favour of higher NID... */
1171 if (peerid.nid < ni->ni_nid &&
1172 ksocknal_connecting(peer, conn->ksnc_ipaddr)) {
1174 warn = "connection race resolution";
1179 if (peer->ksnp_closing ||
1180 (active && route->ksnr_deleted)) {
1181 /* peer/route got closed under me */
1183 warn = "peer/route removed";
1187 if (peer->ksnp_proto == NULL) {
1188 /* Never connected before.
1189 * NB recv_hello may have returned EPROTO to signal my peer
1190 * wants a different protocol than the one I asked for.
1192 LASSERT (cfs_list_empty(&peer->ksnp_conns));
1194 peer->ksnp_proto = conn->ksnc_proto;
1195 peer->ksnp_incarnation = incarnation;
1198 if (peer->ksnp_proto != conn->ksnc_proto ||
1199 peer->ksnp_incarnation != incarnation) {
1200 /* Peer rebooted or I've got the wrong protocol version */
1201 ksocknal_close_peer_conns_locked(peer, 0, 0);
1203 peer->ksnp_proto = NULL;
1205 warn = peer->ksnp_incarnation != incarnation ?
1207 "wrong proto version";
1217 warn = "lost conn race";
1220 warn = "retry with different protocol version";
1224 /* Refuse to duplicate an existing connection, unless this is a
1225 * loopback connection */
1226 if (conn->ksnc_ipaddr != conn->ksnc_myipaddr) {
1227 cfs_list_for_each(tmp, &peer->ksnp_conns) {
1228 conn2 = cfs_list_entry(tmp, ksock_conn_t, ksnc_list);
1230 if (conn2->ksnc_ipaddr != conn->ksnc_ipaddr ||
1231 conn2->ksnc_myipaddr != conn->ksnc_myipaddr ||
1232 conn2->ksnc_type != conn->ksnc_type)
1235 /* Reply on a passive connection attempt so the peer
1236 * realises we're connected. */
1246 /* If the connection created by this route didn't bind to the IP
1247 * address the route connected to, the connection/route matching
1248 * code below probably isn't going to work. */
1250 route->ksnr_ipaddr != conn->ksnc_ipaddr) {
1251 CERROR("Route %s %u.%u.%u.%u connected to %u.%u.%u.%u\n",
1252 libcfs_id2str(peer->ksnp_id),
1253 HIPQUAD(route->ksnr_ipaddr),
1254 HIPQUAD(conn->ksnc_ipaddr));
1257 /* Search for a route corresponding to the new connection and
1258 * create an association. This allows incoming connections created
1259 * by routes in my peer to match my own route entries so I don't
1260 * continually create duplicate routes. */
1261 cfs_list_for_each (tmp, &peer->ksnp_routes) {
1262 route = cfs_list_entry(tmp, ksock_route_t, ksnr_list);
1264 if (route->ksnr_ipaddr != conn->ksnc_ipaddr)
1267 ksocknal_associate_route_conn_locked(route, conn);
1271 conn->ksnc_peer = peer; /* conn takes my ref on peer */
1272 peer->ksnp_last_alive = cfs_time_current();
1273 peer->ksnp_send_keepalive = 0;
1274 peer->ksnp_error = 0;
1276 sched = ksocknal_choose_scheduler_locked (irq);
1277 sched->kss_nconns++;
1278 conn->ksnc_scheduler = sched;
1280 conn->ksnc_tx_last_post = cfs_time_current();
1281 /* Set the deadline for the outgoing HELLO to drain */
1282 conn->ksnc_tx_bufnob = libcfs_sock_wmem_queued(sock);
1283 conn->ksnc_tx_deadline = cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
1284 cfs_mb(); /* order with adding to peer's conn list */
1286 cfs_list_add (&conn->ksnc_list, &peer->ksnp_conns);
1287 ksocknal_conn_addref(conn);
1289 ksocknal_new_packet(conn, 0);
1291 conn->ksnc_zc_capable = ksocknal_lib_zc_capable(conn);
1293 /* Take packets blocking for this connection. */
1294 cfs_list_for_each_entry_safe(tx, txtmp, &peer->ksnp_tx_queue, tx_list) {
1295 if (conn->ksnc_proto->pro_match_tx(conn, tx, tx->tx_nonblk) == SOCKNAL_MATCH_NO)
1298 cfs_list_del (&tx->tx_list);
1299 ksocknal_queue_tx_locked (tx, conn);
1302 cfs_write_unlock_bh (global_lock);
1304 /* We've now got a new connection. Any errors from here on are just
1305 * like "normal" comms errors and we close the connection normally.
1306 * NB (a) we still have to send the reply HELLO for passive
1308 * (b) normal I/O on the conn is blocked until I setup and call the
1312 ksocknal_lib_bind_irq (irq);
1314 CDEBUG(D_NET, "New conn %s p %d.x %u.%u.%u.%u -> %u.%u.%u.%u/%d"
1315 " incarnation:"LPD64" sched[%d]/%d\n",
1316 libcfs_id2str(peerid), conn->ksnc_proto->pro_version,
1317 HIPQUAD(conn->ksnc_myipaddr), HIPQUAD(conn->ksnc_ipaddr),
1318 conn->ksnc_port, incarnation,
1319 (int)(conn->ksnc_scheduler - ksocknal_data.ksnd_schedulers), irq);
1322 /* additional routes after interface exchange? */
1323 ksocknal_create_routes(peer, conn->ksnc_port,
1324 hello->kshm_ips, hello->kshm_nips);
1326 hello->kshm_nips = ksocknal_select_ips(peer, hello->kshm_ips,
1328 rc = ksocknal_send_hello(ni, conn, peerid.nid, hello);
1331 LIBCFS_FREE(hello, offsetof(ksock_hello_msg_t,
1332 kshm_ips[LNET_MAX_INTERFACES]));
1334 /* setup the socket AFTER I've received hello (it disables
1335 * SO_LINGER). I might call back to the acceptor who may want
1336 * to send a protocol version response and then close the
1337 * socket; this ensures the socket only tears down after the
1338 * response has been sent. */
1340 rc = ksocknal_lib_setup_sock(sock);
1342 cfs_write_lock_bh(global_lock);
1344 /* NB my callbacks block while I hold ksnd_global_lock */
1345 ksocknal_lib_set_callback(sock, conn);
1348 peer->ksnp_accepting--;
1350 cfs_write_unlock_bh(global_lock);
1353 cfs_write_lock_bh(global_lock);
1354 if (!conn->ksnc_closing) {
1355 /* could be closed by another thread */
1356 ksocknal_close_conn_locked(conn, rc);
1358 cfs_write_unlock_bh(global_lock);
1359 } else if (ksocknal_connsock_addref(conn) == 0) {
1360 /* Allow I/O to proceed. */
1361 ksocknal_read_callback(conn);
1362 ksocknal_write_callback(conn);
1363 ksocknal_connsock_decref(conn);
1366 ksocknal_connsock_decref(conn);
1367 ksocknal_conn_decref(conn);
1371 if (!peer->ksnp_closing &&
1372 cfs_list_empty (&peer->ksnp_conns) &&
1373 cfs_list_empty (&peer->ksnp_routes)) {
1374 cfs_list_add(&zombies, &peer->ksnp_tx_queue);
1375 cfs_list_del_init(&peer->ksnp_tx_queue);
1376 ksocknal_unlink_peer_locked(peer);
1379 cfs_write_unlock_bh (global_lock);
1383 CERROR("Not creating conn %s type %d: %s\n",
1384 libcfs_id2str(peerid), conn->ksnc_type, warn);
1386 CDEBUG(D_NET, "Not creating conn %s type %d: %s\n",
1387 libcfs_id2str(peerid), conn->ksnc_type, warn);
1392 /* Request retry by replying with CONN_NONE
1393 * ksnc_proto has been set already */
1394 conn->ksnc_type = SOCKLND_CONN_NONE;
1395 hello->kshm_nips = 0;
1396 ksocknal_send_hello(ni, conn, peerid.nid, hello);
1399 cfs_write_lock_bh(global_lock);
1400 peer->ksnp_accepting--;
1401 cfs_write_unlock_bh(global_lock);
1404 ksocknal_txlist_done(ni, &zombies, 1);
1405 ksocknal_peer_decref(peer);
1409 LIBCFS_FREE(hello, offsetof(ksock_hello_msg_t,
1410 kshm_ips[LNET_MAX_INTERFACES]));
1412 LIBCFS_FREE (conn, sizeof(*conn));
1415 libcfs_sock_release(sock);
1420 ksocknal_close_conn_locked (ksock_conn_t *conn, int error)
1422 /* This just does the immmediate housekeeping, and queues the
1423 * connection for the reaper to terminate.
1424 * Caller holds ksnd_global_lock exclusively in irq context */
1425 ksock_peer_t *peer = conn->ksnc_peer;
1426 ksock_route_t *route;
1427 ksock_conn_t *conn2;
1430 LASSERT (peer->ksnp_error == 0);
1431 LASSERT (!conn->ksnc_closing);
1432 conn->ksnc_closing = 1;
1434 /* ksnd_deathrow_conns takes over peer's ref */
1435 cfs_list_del (&conn->ksnc_list);
1437 route = conn->ksnc_route;
1438 if (route != NULL) {
1439 /* dissociate conn from route... */
1440 LASSERT (!route->ksnr_deleted);
1441 LASSERT ((route->ksnr_connected & (1 << conn->ksnc_type)) != 0);
1444 cfs_list_for_each(tmp, &peer->ksnp_conns) {
1445 conn2 = cfs_list_entry(tmp, ksock_conn_t, ksnc_list);
1447 if (conn2->ksnc_route == route &&
1448 conn2->ksnc_type == conn->ksnc_type)
1454 route->ksnr_connected &= ~(1 << conn->ksnc_type);
1456 conn->ksnc_route = NULL;
1458 #if 0 /* irrelevent with only eager routes */
1459 /* make route least favourite */
1460 cfs_list_del (&route->ksnr_list);
1461 cfs_list_add_tail (&route->ksnr_list, &peer->ksnp_routes);
1463 ksocknal_route_decref(route); /* drop conn's ref on route */
1466 if (cfs_list_empty (&peer->ksnp_conns)) {
1467 /* No more connections to this peer */
1469 if (!cfs_list_empty(&peer->ksnp_tx_queue)) {
1472 LASSERT (conn->ksnc_proto == &ksocknal_protocol_v3x);
1474 /* throw them to the last connection...,
1475 * these TXs will be send to /dev/null by scheduler */
1476 cfs_list_for_each_entry(tx, &peer->ksnp_tx_queue,
1478 ksocknal_tx_prep(conn, tx);
1480 cfs_spin_lock_bh(&conn->ksnc_scheduler->kss_lock);
1481 cfs_list_splice_init(&peer->ksnp_tx_queue,
1482 &conn->ksnc_tx_queue);
1483 cfs_spin_unlock_bh(&conn->ksnc_scheduler->kss_lock);
1486 peer->ksnp_proto = NULL; /* renegotiate protocol version */
1487 peer->ksnp_error = error; /* stash last conn close reason */
1489 if (cfs_list_empty (&peer->ksnp_routes)) {
1490 /* I've just closed last conn belonging to a
1491 * peer with no routes to it */
1492 ksocknal_unlink_peer_locked (peer);
1496 cfs_spin_lock_bh (&ksocknal_data.ksnd_reaper_lock);
1498 cfs_list_add_tail (&conn->ksnc_list,
1499 &ksocknal_data.ksnd_deathrow_conns);
1500 cfs_waitq_signal (&ksocknal_data.ksnd_reaper_waitq);
1502 cfs_spin_unlock_bh (&ksocknal_data.ksnd_reaper_lock);
1506 ksocknal_peer_failed (ksock_peer_t *peer)
1509 cfs_time_t last_alive = 0;
1511 /* There has been a connection failure or comms error; but I'll only
1512 * tell LNET I think the peer is dead if it's to another kernel and
1513 * there are no connections or connection attempts in existance. */
1515 cfs_read_lock (&ksocknal_data.ksnd_global_lock);
1517 if ((peer->ksnp_id.pid & LNET_PID_USERFLAG) == 0 &&
1518 cfs_list_empty(&peer->ksnp_conns) &&
1519 peer->ksnp_accepting == 0 &&
1520 ksocknal_find_connecting_route_locked(peer) == NULL) {
1522 last_alive = peer->ksnp_last_alive;
1525 cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
1528 lnet_notify (peer->ksnp_ni, peer->ksnp_id.nid, 0,
1533 ksocknal_finalize_zcreq(ksock_conn_t *conn)
1535 ksock_peer_t *peer = conn->ksnc_peer;
1538 CFS_LIST_HEAD (zlist);
1540 /* NB safe to finalize TXs because closing of socket will
1541 * abort all buffered data */
1542 LASSERT (conn->ksnc_sock == NULL);
1544 cfs_spin_lock(&peer->ksnp_lock);
1546 cfs_list_for_each_entry_safe_typed(tx, tmp, &peer->ksnp_zc_req_list,
1547 ksock_tx_t, tx_zc_list) {
1548 if (tx->tx_conn != conn)
1551 LASSERT (tx->tx_msg.ksm_zc_cookies[0] != 0);
1553 tx->tx_msg.ksm_zc_cookies[0] = 0;
1554 if (tx->tx_resid == 0)
1555 tx->tx_resid = -1; /* mark it as not-acked */
1556 cfs_list_del(&tx->tx_zc_list);
1557 cfs_list_add(&tx->tx_zc_list, &zlist);
1560 cfs_spin_unlock(&peer->ksnp_lock);
1562 while (!cfs_list_empty(&zlist)) {
1563 tx = cfs_list_entry(zlist.next, ksock_tx_t, tx_zc_list);
1565 cfs_list_del(&tx->tx_zc_list);
1566 ksocknal_tx_decref(tx);
1571 ksocknal_terminate_conn (ksock_conn_t *conn)
1573 /* This gets called by the reaper (guaranteed thread context) to
1574 * disengage the socket from its callbacks and close it.
1575 * ksnc_refcount will eventually hit zero, and then the reaper will
1577 ksock_peer_t *peer = conn->ksnc_peer;
1578 ksock_sched_t *sched = conn->ksnc_scheduler;
1581 LASSERT(conn->ksnc_closing);
1583 /* wake up the scheduler to "send" all remaining packets to /dev/null */
1584 cfs_spin_lock_bh (&sched->kss_lock);
1586 /* a closing conn is always ready to tx */
1587 conn->ksnc_tx_ready = 1;
1589 if (!conn->ksnc_tx_scheduled &&
1590 !cfs_list_empty(&conn->ksnc_tx_queue)){
1591 cfs_list_add_tail (&conn->ksnc_tx_list,
1592 &sched->kss_tx_conns);
1593 conn->ksnc_tx_scheduled = 1;
1594 /* extra ref for scheduler */
1595 ksocknal_conn_addref(conn);
1597 cfs_waitq_signal (&sched->kss_waitq);
1600 cfs_spin_unlock_bh (&sched->kss_lock);
1602 /* serialise with callbacks */
1603 cfs_write_lock_bh (&ksocknal_data.ksnd_global_lock);
1605 ksocknal_lib_reset_callback(conn->ksnc_sock, conn);
1607 /* OK, so this conn may not be completely disengaged from its
1608 * scheduler yet, but it _has_ committed to terminate... */
1609 conn->ksnc_scheduler->kss_nconns--;
1611 if (peer->ksnp_error != 0) {
1612 /* peer's last conn closed in error */
1613 LASSERT (cfs_list_empty (&peer->ksnp_conns));
1615 peer->ksnp_error = 0; /* avoid multiple notifications */
1618 cfs_write_unlock_bh (&ksocknal_data.ksnd_global_lock);
1621 ksocknal_peer_failed(peer);
1623 /* The socket is closed on the final put; either here, or in
1624 * ksocknal_{send,recv}msg(). Since we set up the linger2 option
1625 * when the connection was established, this will close the socket
1626 * immediately, aborting anything buffered in it. Any hung
1627 * zero-copy transmits will therefore complete in finite time. */
1628 ksocknal_connsock_decref(conn);
1632 ksocknal_queue_zombie_conn (ksock_conn_t *conn)
1634 /* Queue the conn for the reaper to destroy */
1636 LASSERT (cfs_atomic_read(&conn->ksnc_conn_refcount) == 0);
1637 cfs_spin_lock_bh (&ksocknal_data.ksnd_reaper_lock);
1639 cfs_list_add_tail(&conn->ksnc_list, &ksocknal_data.ksnd_zombie_conns);
1640 cfs_waitq_signal(&ksocknal_data.ksnd_reaper_waitq);
1642 cfs_spin_unlock_bh (&ksocknal_data.ksnd_reaper_lock);
1646 ksocknal_destroy_conn (ksock_conn_t *conn)
1648 cfs_time_t last_rcv;
1650 /* Final coup-de-grace of the reaper */
1651 CDEBUG (D_NET, "connection %p\n", conn);
1653 LASSERT (cfs_atomic_read (&conn->ksnc_conn_refcount) == 0);
1654 LASSERT (cfs_atomic_read (&conn->ksnc_sock_refcount) == 0);
1655 LASSERT (conn->ksnc_sock == NULL);
1656 LASSERT (conn->ksnc_route == NULL);
1657 LASSERT (!conn->ksnc_tx_scheduled);
1658 LASSERT (!conn->ksnc_rx_scheduled);
1659 LASSERT (cfs_list_empty(&conn->ksnc_tx_queue));
1661 /* complete current receive if any */
1662 switch (conn->ksnc_rx_state) {
1663 case SOCKNAL_RX_LNET_PAYLOAD:
1664 last_rcv = conn->ksnc_rx_deadline -
1665 cfs_time_seconds(*ksocknal_tunables.ksnd_timeout);
1666 CERROR("Completing partial receive from %s[%d]"
1667 ", ip %d.%d.%d.%d:%d, with error, wanted: %d, left: %d, "
1668 "last alive is %ld secs ago\n",
1669 libcfs_id2str(conn->ksnc_peer->ksnp_id), conn->ksnc_type,
1670 HIPQUAD(conn->ksnc_ipaddr), conn->ksnc_port,
1671 conn->ksnc_rx_nob_wanted, conn->ksnc_rx_nob_left,
1672 cfs_duration_sec(cfs_time_sub(cfs_time_current(),
1674 lnet_finalize (conn->ksnc_peer->ksnp_ni,
1675 conn->ksnc_cookie, -EIO);
1677 case SOCKNAL_RX_LNET_HEADER:
1678 if (conn->ksnc_rx_started)
1679 CERROR("Incomplete receive of lnet header from %s"
1680 ", ip %d.%d.%d.%d:%d, with error, protocol: %d.x.\n",
1681 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1682 HIPQUAD(conn->ksnc_ipaddr), conn->ksnc_port,
1683 conn->ksnc_proto->pro_version);
1685 case SOCKNAL_RX_KSM_HEADER:
1686 if (conn->ksnc_rx_started)
1687 CERROR("Incomplete receive of ksock message from %s"
1688 ", ip %d.%d.%d.%d:%d, with error, protocol: %d.x.\n",
1689 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1690 HIPQUAD(conn->ksnc_ipaddr), conn->ksnc_port,
1691 conn->ksnc_proto->pro_version);
1693 case SOCKNAL_RX_SLOP:
1694 if (conn->ksnc_rx_started)
1695 CERROR("Incomplete receive of slops from %s"
1696 ", ip %d.%d.%d.%d:%d, with error\n",
1697 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1698 HIPQUAD(conn->ksnc_ipaddr), conn->ksnc_port);
1705 ksocknal_peer_decref(conn->ksnc_peer);
1707 LIBCFS_FREE (conn, sizeof (*conn));
1711 ksocknal_close_peer_conns_locked (ksock_peer_t *peer, __u32 ipaddr, int why)
1718 cfs_list_for_each_safe (ctmp, cnxt, &peer->ksnp_conns) {
1719 conn = cfs_list_entry (ctmp, ksock_conn_t, ksnc_list);
1722 conn->ksnc_ipaddr == ipaddr) {
1724 ksocknal_close_conn_locked (conn, why);
1732 ksocknal_close_conn_and_siblings (ksock_conn_t *conn, int why)
1734 ksock_peer_t *peer = conn->ksnc_peer;
1735 __u32 ipaddr = conn->ksnc_ipaddr;
1738 cfs_write_lock_bh (&ksocknal_data.ksnd_global_lock);
1740 count = ksocknal_close_peer_conns_locked (peer, ipaddr, why);
1742 cfs_write_unlock_bh (&ksocknal_data.ksnd_global_lock);
1748 ksocknal_close_matching_conns (lnet_process_id_t id, __u32 ipaddr)
1758 cfs_write_lock_bh (&ksocknal_data.ksnd_global_lock);
1760 if (id.nid != LNET_NID_ANY)
1761 lo = hi = (int)(ksocknal_nid2peerlist(id.nid) - ksocknal_data.ksnd_peers);
1764 hi = ksocknal_data.ksnd_peer_hash_size - 1;
1767 for (i = lo; i <= hi; i++) {
1768 cfs_list_for_each_safe (ptmp, pnxt,
1769 &ksocknal_data.ksnd_peers[i]) {
1771 peer = cfs_list_entry (ptmp, ksock_peer_t, ksnp_list);
1773 if (!((id.nid == LNET_NID_ANY || id.nid == peer->ksnp_id.nid) &&
1774 (id.pid == LNET_PID_ANY || id.pid == peer->ksnp_id.pid)))
1777 count += ksocknal_close_peer_conns_locked (peer, ipaddr, 0);
1781 cfs_write_unlock_bh (&ksocknal_data.ksnd_global_lock);
1783 /* wildcards always succeed */
1784 if (id.nid == LNET_NID_ANY || id.pid == LNET_PID_ANY || ipaddr == 0)
1787 return (count == 0 ? -ENOENT : 0);
1791 ksocknal_notify (lnet_ni_t *ni, lnet_nid_t gw_nid, int alive)
1793 /* The router is telling me she's been notified of a change in
1794 * gateway state.... */
1795 lnet_process_id_t id = {0};
1798 id.pid = LNET_PID_ANY;
1800 CDEBUG (D_NET, "gw %s %s\n", libcfs_nid2str(gw_nid),
1801 alive ? "up" : "down");
1804 /* If the gateway crashed, close all open connections... */
1805 ksocknal_close_matching_conns (id, 0);
1809 /* ...otherwise do nothing. We can only establish new connections
1810 * if we have autroutes, and these connect on demand. */
1814 ksocknal_query (lnet_ni_t *ni, lnet_nid_t nid, cfs_time_t *when)
1817 cfs_time_t last_alive = 0;
1818 cfs_time_t now = cfs_time_current();
1819 ksock_peer_t *peer = NULL;
1820 cfs_rwlock_t *glock = &ksocknal_data.ksnd_global_lock;
1821 lnet_process_id_t id = {.nid = nid, .pid = LUSTRE_SRV_LNET_PID};
1823 cfs_read_lock(glock);
1825 peer = ksocknal_find_peer_locked(ni, id);
1831 cfs_list_for_each (tmp, &peer->ksnp_conns) {
1832 conn = cfs_list_entry(tmp, ksock_conn_t, ksnc_list);
1833 bufnob = libcfs_sock_wmem_queued(conn->ksnc_sock);
1835 if (bufnob < conn->ksnc_tx_bufnob) {
1836 /* something got ACKed */
1837 conn->ksnc_tx_deadline =
1838 cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
1839 peer->ksnp_last_alive = now;
1840 conn->ksnc_tx_bufnob = bufnob;
1844 last_alive = peer->ksnp_last_alive;
1845 if (ksocknal_find_connectable_route_locked(peer) == NULL)
1849 cfs_read_unlock(glock);
1851 if (last_alive != 0)
1854 CDEBUG(D_NET, "Peer %s %p, alive %ld secs ago, connect %d\n",
1855 libcfs_nid2str(nid), peer,
1856 last_alive ? cfs_duration_sec(now - last_alive) : -1,
1862 ksocknal_add_peer(ni, id, LNET_NIDADDR(nid), lnet_acceptor_port());
1864 cfs_write_lock_bh(glock);
1866 peer = ksocknal_find_peer_locked(ni, id);
1868 ksocknal_launch_all_connections_locked(peer);
1870 cfs_write_unlock_bh(glock);
1875 ksocknal_push_peer (ksock_peer_t *peer)
1882 for (index = 0; ; index++) {
1883 cfs_read_lock (&ksocknal_data.ksnd_global_lock);
1888 cfs_list_for_each (tmp, &peer->ksnp_conns) {
1890 conn = cfs_list_entry (tmp, ksock_conn_t,
1892 ksocknal_conn_addref(conn);
1897 cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
1902 ksocknal_lib_push_conn (conn);
1903 ksocknal_conn_decref(conn);
1908 ksocknal_push (lnet_ni_t *ni, lnet_process_id_t id)
1917 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
1918 for (j = 0; ; j++) {
1919 cfs_read_lock (&ksocknal_data.ksnd_global_lock);
1924 cfs_list_for_each (tmp, &ksocknal_data.ksnd_peers[i]) {
1925 peer = cfs_list_entry(tmp, ksock_peer_t,
1928 if (!((id.nid == LNET_NID_ANY ||
1929 id.nid == peer->ksnp_id.nid) &&
1930 (id.pid == LNET_PID_ANY ||
1931 id.pid == peer->ksnp_id.pid))) {
1937 ksocknal_peer_addref(peer);
1942 cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
1946 ksocknal_push_peer (peer);
1947 ksocknal_peer_decref(peer);
1957 ksocknal_add_interface(lnet_ni_t *ni, __u32 ipaddress, __u32 netmask)
1959 ksock_net_t *net = ni->ni_data;
1960 ksock_interface_t *iface;
1967 ksock_route_t *route;
1969 if (ipaddress == 0 ||
1973 cfs_write_lock_bh (&ksocknal_data.ksnd_global_lock);
1975 iface = ksocknal_ip2iface(ni, ipaddress);
1976 if (iface != NULL) {
1977 /* silently ignore dups */
1979 } else if (net->ksnn_ninterfaces == LNET_MAX_INTERFACES) {
1982 iface = &net->ksnn_interfaces[net->ksnn_ninterfaces++];
1984 iface->ksni_ipaddr = ipaddress;
1985 iface->ksni_netmask = netmask;
1986 iface->ksni_nroutes = 0;
1987 iface->ksni_npeers = 0;
1989 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
1990 cfs_list_for_each(ptmp, &ksocknal_data.ksnd_peers[i]) {
1991 peer = cfs_list_entry(ptmp, ksock_peer_t,
1994 for (j = 0; j < peer->ksnp_n_passive_ips; j++)
1995 if (peer->ksnp_passive_ips[j] == ipaddress)
1996 iface->ksni_npeers++;
1998 cfs_list_for_each(rtmp, &peer->ksnp_routes) {
1999 route = cfs_list_entry(rtmp,
2003 if (route->ksnr_myipaddr == ipaddress)
2004 iface->ksni_nroutes++;
2010 /* NB only new connections will pay attention to the new interface! */
2013 cfs_write_unlock_bh (&ksocknal_data.ksnd_global_lock);
2019 ksocknal_peer_del_interface_locked(ksock_peer_t *peer, __u32 ipaddr)
2023 ksock_route_t *route;
2028 for (i = 0; i < peer->ksnp_n_passive_ips; i++)
2029 if (peer->ksnp_passive_ips[i] == ipaddr) {
2030 for (j = i+1; j < peer->ksnp_n_passive_ips; j++)
2031 peer->ksnp_passive_ips[j-1] =
2032 peer->ksnp_passive_ips[j];
2033 peer->ksnp_n_passive_ips--;
2037 cfs_list_for_each_safe(tmp, nxt, &peer->ksnp_routes) {
2038 route = cfs_list_entry (tmp, ksock_route_t, ksnr_list);
2040 if (route->ksnr_myipaddr != ipaddr)
2043 if (route->ksnr_share_count != 0) {
2044 /* Manually created; keep, but unbind */
2045 route->ksnr_myipaddr = 0;
2047 ksocknal_del_route_locked(route);
2051 cfs_list_for_each_safe(tmp, nxt, &peer->ksnp_conns) {
2052 conn = cfs_list_entry(tmp, ksock_conn_t, ksnc_list);
2054 if (conn->ksnc_myipaddr == ipaddr)
2055 ksocknal_close_conn_locked (conn, 0);
2060 ksocknal_del_interface(lnet_ni_t *ni, __u32 ipaddress)
2062 ksock_net_t *net = ni->ni_data;
2071 cfs_write_lock_bh (&ksocknal_data.ksnd_global_lock);
2073 for (i = 0; i < net->ksnn_ninterfaces; i++) {
2074 this_ip = net->ksnn_interfaces[i].ksni_ipaddr;
2076 if (!(ipaddress == 0 ||
2077 ipaddress == this_ip))
2082 for (j = i+1; j < net->ksnn_ninterfaces; j++)
2083 net->ksnn_interfaces[j-1] =
2084 net->ksnn_interfaces[j];
2086 net->ksnn_ninterfaces--;
2088 for (j = 0; j < ksocknal_data.ksnd_peer_hash_size; j++) {
2089 cfs_list_for_each_safe(tmp, nxt,
2090 &ksocknal_data.ksnd_peers[j]) {
2091 peer = cfs_list_entry(tmp, ksock_peer_t,
2094 if (peer->ksnp_ni != ni)
2097 ksocknal_peer_del_interface_locked(peer, this_ip);
2102 cfs_write_unlock_bh (&ksocknal_data.ksnd_global_lock);
2108 ksocknal_ctl(lnet_ni_t *ni, unsigned int cmd, void *arg)
2110 lnet_process_id_t id = {0};
2111 struct libcfs_ioctl_data *data = arg;
2115 case IOC_LIBCFS_GET_INTERFACE: {
2116 ksock_net_t *net = ni->ni_data;
2117 ksock_interface_t *iface;
2119 cfs_read_lock (&ksocknal_data.ksnd_global_lock);
2121 if (data->ioc_count >= (__u32)net->ksnn_ninterfaces) {
2125 iface = &net->ksnn_interfaces[data->ioc_count];
2127 data->ioc_u32[0] = iface->ksni_ipaddr;
2128 data->ioc_u32[1] = iface->ksni_netmask;
2129 data->ioc_u32[2] = iface->ksni_npeers;
2130 data->ioc_u32[3] = iface->ksni_nroutes;
2133 cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
2137 case IOC_LIBCFS_ADD_INTERFACE:
2138 return ksocknal_add_interface(ni,
2139 data->ioc_u32[0], /* IP address */
2140 data->ioc_u32[1]); /* net mask */
2142 case IOC_LIBCFS_DEL_INTERFACE:
2143 return ksocknal_del_interface(ni,
2144 data->ioc_u32[0]); /* IP address */
2146 case IOC_LIBCFS_GET_PEER: {
2151 int share_count = 0;
2153 rc = ksocknal_get_peer_info(ni, data->ioc_count,
2154 &id, &myip, &ip, &port,
2155 &conn_count, &share_count);
2159 data->ioc_nid = id.nid;
2160 data->ioc_count = share_count;
2161 data->ioc_u32[0] = ip;
2162 data->ioc_u32[1] = port;
2163 data->ioc_u32[2] = myip;
2164 data->ioc_u32[3] = conn_count;
2165 data->ioc_u32[4] = id.pid;
2169 case IOC_LIBCFS_ADD_PEER:
2170 id.nid = data->ioc_nid;
2171 id.pid = LUSTRE_SRV_LNET_PID;
2172 return ksocknal_add_peer (ni, id,
2173 data->ioc_u32[0], /* IP */
2174 data->ioc_u32[1]); /* port */
2176 case IOC_LIBCFS_DEL_PEER:
2177 id.nid = data->ioc_nid;
2178 id.pid = LNET_PID_ANY;
2179 return ksocknal_del_peer (ni, id,
2180 data->ioc_u32[0]); /* IP */
2182 case IOC_LIBCFS_GET_CONN: {
2186 ksock_conn_t *conn = ksocknal_get_conn_by_idx (ni, data->ioc_count);
2191 ksocknal_lib_get_conn_tunables(conn, &txmem, &rxmem, &nagle);
2193 data->ioc_count = txmem;
2194 data->ioc_nid = conn->ksnc_peer->ksnp_id.nid;
2195 data->ioc_flags = nagle;
2196 data->ioc_u32[0] = conn->ksnc_ipaddr;
2197 data->ioc_u32[1] = conn->ksnc_port;
2198 data->ioc_u32[2] = conn->ksnc_myipaddr;
2199 data->ioc_u32[3] = conn->ksnc_type;
2200 data->ioc_u32[4] = (__u32)(conn->ksnc_scheduler -
2201 ksocknal_data.ksnd_schedulers);
2202 data->ioc_u32[5] = rxmem;
2203 data->ioc_u32[6] = conn->ksnc_peer->ksnp_id.pid;
2204 ksocknal_conn_decref(conn);
2208 case IOC_LIBCFS_CLOSE_CONNECTION:
2209 id.nid = data->ioc_nid;
2210 id.pid = LNET_PID_ANY;
2211 return ksocknal_close_matching_conns (id,
2214 case IOC_LIBCFS_REGISTER_MYNID:
2215 /* Ignore if this is a noop */
2216 if (data->ioc_nid == ni->ni_nid)
2219 CERROR("obsolete IOC_LIBCFS_REGISTER_MYNID: %s(%s)\n",
2220 libcfs_nid2str(data->ioc_nid),
2221 libcfs_nid2str(ni->ni_nid));
2224 case IOC_LIBCFS_PUSH_CONNECTION:
2225 id.nid = data->ioc_nid;
2226 id.pid = LNET_PID_ANY;
2227 return ksocknal_push(ni, id);
2236 ksocknal_free_buffers (void)
2238 LASSERT (cfs_atomic_read(&ksocknal_data.ksnd_nactive_txs) == 0);
2240 if (ksocknal_data.ksnd_schedulers != NULL)
2241 LIBCFS_FREE (ksocknal_data.ksnd_schedulers,
2242 sizeof (ksock_sched_t) * ksocknal_data.ksnd_nschedulers);
2244 LIBCFS_FREE (ksocknal_data.ksnd_peers,
2245 sizeof (cfs_list_t) *
2246 ksocknal_data.ksnd_peer_hash_size);
2248 cfs_spin_lock(&ksocknal_data.ksnd_tx_lock);
2250 if (!cfs_list_empty(&ksocknal_data.ksnd_idle_noop_txs)) {
2254 cfs_list_add(&zlist, &ksocknal_data.ksnd_idle_noop_txs);
2255 cfs_list_del_init(&ksocknal_data.ksnd_idle_noop_txs);
2256 cfs_spin_unlock(&ksocknal_data.ksnd_tx_lock);
2258 while(!cfs_list_empty(&zlist)) {
2259 tx = cfs_list_entry(zlist.next, ksock_tx_t, tx_list);
2260 cfs_list_del(&tx->tx_list);
2261 LIBCFS_FREE(tx, tx->tx_desc_size);
2264 cfs_spin_unlock(&ksocknal_data.ksnd_tx_lock);
2269 ksocknal_base_shutdown (void)
2271 ksock_sched_t *sched;
2274 CDEBUG(D_MALLOC, "before NAL cleanup: kmem %d\n",
2275 cfs_atomic_read (&libcfs_kmemory));
2276 LASSERT (ksocknal_data.ksnd_nnets == 0);
2278 switch (ksocknal_data.ksnd_init) {
2282 case SOCKNAL_INIT_ALL:
2283 case SOCKNAL_INIT_DATA:
2284 LASSERT (ksocknal_data.ksnd_peers != NULL);
2285 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
2286 LASSERT (cfs_list_empty (&ksocknal_data.ksnd_peers[i]));
2288 LASSERT (cfs_list_empty (&ksocknal_data.ksnd_enomem_conns));
2289 LASSERT (cfs_list_empty (&ksocknal_data.ksnd_zombie_conns));
2290 LASSERT (cfs_list_empty (&ksocknal_data.ksnd_connd_connreqs));
2291 LASSERT (cfs_list_empty (&ksocknal_data.ksnd_connd_routes));
2293 if (ksocknal_data.ksnd_schedulers != NULL)
2294 for (i = 0; i < ksocknal_data.ksnd_nschedulers; i++) {
2295 ksock_sched_t *kss =
2296 &ksocknal_data.ksnd_schedulers[i];
2298 LASSERT (cfs_list_empty (&kss->kss_tx_conns));
2299 LASSERT (cfs_list_empty (&kss->kss_rx_conns));
2300 LASSERT (cfs_list_empty (&kss-> \
2301 kss_zombie_noop_txs));
2302 LASSERT (kss->kss_nconns == 0);
2305 /* flag threads to terminate; wake and wait for them to die */
2306 ksocknal_data.ksnd_shuttingdown = 1;
2307 cfs_waitq_broadcast (&ksocknal_data.ksnd_connd_waitq);
2308 cfs_waitq_broadcast (&ksocknal_data.ksnd_reaper_waitq);
2310 if (ksocknal_data.ksnd_schedulers != NULL)
2311 for (i = 0; i < ksocknal_data.ksnd_nschedulers; i++) {
2312 sched = &ksocknal_data.ksnd_schedulers[i];
2313 cfs_waitq_broadcast(&sched->kss_waitq);
2317 cfs_read_lock (&ksocknal_data.ksnd_global_lock);
2318 while (ksocknal_data.ksnd_nthreads != 0) {
2320 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET, /* power of 2? */
2321 "waiting for %d threads to terminate\n",
2322 ksocknal_data.ksnd_nthreads);
2323 cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
2324 cfs_pause(cfs_time_seconds(1));
2325 cfs_read_lock (&ksocknal_data.ksnd_global_lock);
2327 cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
2329 ksocknal_free_buffers();
2331 ksocknal_data.ksnd_init = SOCKNAL_INIT_NOTHING;
2335 CDEBUG(D_MALLOC, "after NAL cleanup: kmem %d\n",
2336 cfs_atomic_read (&libcfs_kmemory));
2338 PORTAL_MODULE_UNUSE;
2342 ksocknal_new_incarnation (void)
2346 /* The incarnation number is the time this module loaded and it
2347 * identifies this particular instance of the socknal. Hopefully
2348 * we won't be able to reboot more frequently than 1MHz for the
2349 * forseeable future :) */
2351 cfs_gettimeofday(&tv);
2353 return (((__u64)tv.tv_sec) * 1000000) + tv.tv_usec;
2357 ksocknal_base_startup (void)
2362 LASSERT (ksocknal_data.ksnd_init == SOCKNAL_INIT_NOTHING);
2363 LASSERT (ksocknal_data.ksnd_nnets == 0);
2365 memset (&ksocknal_data, 0, sizeof (ksocknal_data)); /* zero pointers */
2367 ksocknal_data.ksnd_peer_hash_size = SOCKNAL_PEER_HASH_SIZE;
2368 LIBCFS_ALLOC (ksocknal_data.ksnd_peers,
2369 sizeof (cfs_list_t) *
2370 ksocknal_data.ksnd_peer_hash_size);
2371 if (ksocknal_data.ksnd_peers == NULL)
2374 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++)
2375 CFS_INIT_LIST_HEAD(&ksocknal_data.ksnd_peers[i]);
2377 cfs_rwlock_init(&ksocknal_data.ksnd_global_lock);
2379 cfs_spin_lock_init (&ksocknal_data.ksnd_reaper_lock);
2380 CFS_INIT_LIST_HEAD (&ksocknal_data.ksnd_enomem_conns);
2381 CFS_INIT_LIST_HEAD (&ksocknal_data.ksnd_zombie_conns);
2382 CFS_INIT_LIST_HEAD (&ksocknal_data.ksnd_deathrow_conns);
2383 cfs_waitq_init(&ksocknal_data.ksnd_reaper_waitq);
2385 cfs_spin_lock_init (&ksocknal_data.ksnd_connd_lock);
2386 CFS_INIT_LIST_HEAD (&ksocknal_data.ksnd_connd_connreqs);
2387 CFS_INIT_LIST_HEAD (&ksocknal_data.ksnd_connd_routes);
2388 cfs_waitq_init(&ksocknal_data.ksnd_connd_waitq);
2390 cfs_spin_lock_init (&ksocknal_data.ksnd_tx_lock);
2391 CFS_INIT_LIST_HEAD (&ksocknal_data.ksnd_idle_noop_txs);
2393 /* NB memset above zeros whole of ksocknal_data, including
2394 * ksocknal_data.ksnd_irqinfo[all].ksni_valid */
2396 /* flag lists/ptrs/locks initialised */
2397 ksocknal_data.ksnd_init = SOCKNAL_INIT_DATA;
2400 ksocknal_data.ksnd_nschedulers = ksocknal_nsched();
2401 LIBCFS_ALLOC(ksocknal_data.ksnd_schedulers,
2402 sizeof(ksock_sched_t) * ksocknal_data.ksnd_nschedulers);
2403 if (ksocknal_data.ksnd_schedulers == NULL)
2406 for (i = 0; i < ksocknal_data.ksnd_nschedulers; i++) {
2407 ksock_sched_t *kss = &ksocknal_data.ksnd_schedulers[i];
2409 cfs_spin_lock_init (&kss->kss_lock);
2410 CFS_INIT_LIST_HEAD (&kss->kss_rx_conns);
2411 CFS_INIT_LIST_HEAD (&kss->kss_tx_conns);
2412 CFS_INIT_LIST_HEAD (&kss->kss_zombie_noop_txs);
2413 cfs_waitq_init (&kss->kss_waitq);
2416 for (i = 0; i < ksocknal_data.ksnd_nschedulers; i++) {
2417 rc = ksocknal_thread_start (ksocknal_scheduler,
2418 &ksocknal_data.ksnd_schedulers[i]);
2420 CERROR("Can't spawn socknal scheduler[%d]: %d\n",
2426 /* must have at least 2 connds to remain responsive to accepts while
2428 if (*ksocknal_tunables.ksnd_nconnds < 2)
2429 *ksocknal_tunables.ksnd_nconnds = 2;
2431 for (i = 0; i < *ksocknal_tunables.ksnd_nconnds; i++) {
2432 rc = ksocknal_thread_start (ksocknal_connd,
2433 (void *)((ulong_ptr_t)i));
2435 CERROR("Can't spawn socknal connd: %d\n", rc);
2440 rc = ksocknal_thread_start (ksocknal_reaper, NULL);
2442 CERROR ("Can't spawn socknal reaper: %d\n", rc);
2446 /* flag everything initialised */
2447 ksocknal_data.ksnd_init = SOCKNAL_INIT_ALL;
2452 ksocknal_base_shutdown();
2457 ksocknal_debug_peerhash (lnet_ni_t *ni)
2459 ksock_peer_t *peer = NULL;
2463 cfs_read_lock (&ksocknal_data.ksnd_global_lock);
2465 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
2466 cfs_list_for_each (tmp, &ksocknal_data.ksnd_peers[i]) {
2467 peer = cfs_list_entry (tmp, ksock_peer_t, ksnp_list);
2469 if (peer->ksnp_ni == ni) break;
2476 ksock_route_t *route;
2479 CWARN ("Active peer on shutdown: %s, ref %d, scnt %d, "
2480 "closing %d, accepting %d, err %d, zcookie "LPU64", "
2481 "txq %d, zc_req %d\n", libcfs_id2str(peer->ksnp_id),
2482 cfs_atomic_read(&peer->ksnp_refcount),
2483 peer->ksnp_sharecount, peer->ksnp_closing,
2484 peer->ksnp_accepting, peer->ksnp_error,
2485 peer->ksnp_zc_next_cookie,
2486 !cfs_list_empty(&peer->ksnp_tx_queue),
2487 !cfs_list_empty(&peer->ksnp_zc_req_list));
2489 cfs_list_for_each (tmp, &peer->ksnp_routes) {
2490 route = cfs_list_entry(tmp, ksock_route_t, ksnr_list);
2491 CWARN ("Route: ref %d, schd %d, conn %d, cnted %d, "
2492 "del %d\n", cfs_atomic_read(&route->ksnr_refcount),
2493 route->ksnr_scheduled, route->ksnr_connecting,
2494 route->ksnr_connected, route->ksnr_deleted);
2497 cfs_list_for_each (tmp, &peer->ksnp_conns) {
2498 conn = cfs_list_entry(tmp, ksock_conn_t, ksnc_list);
2499 CWARN ("Conn: ref %d, sref %d, t %d, c %d\n",
2500 cfs_atomic_read(&conn->ksnc_conn_refcount),
2501 cfs_atomic_read(&conn->ksnc_sock_refcount),
2502 conn->ksnc_type, conn->ksnc_closing);
2506 cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
2511 ksocknal_shutdown (lnet_ni_t *ni)
2513 ksock_net_t *net = ni->ni_data;
2515 lnet_process_id_t anyid = {0};
2517 anyid.nid = LNET_NID_ANY;
2518 anyid.pid = LNET_PID_ANY;
2520 LASSERT(ksocknal_data.ksnd_init == SOCKNAL_INIT_ALL);
2521 LASSERT(ksocknal_data.ksnd_nnets > 0);
2523 cfs_spin_lock_bh (&net->ksnn_lock);
2524 net->ksnn_shutdown = 1; /* prevent new peers */
2525 cfs_spin_unlock_bh (&net->ksnn_lock);
2527 /* Delete all peers */
2528 ksocknal_del_peer(ni, anyid, 0);
2530 /* Wait for all peer state to clean up */
2532 cfs_spin_lock_bh (&net->ksnn_lock);
2533 while (net->ksnn_npeers != 0) {
2534 cfs_spin_unlock_bh (&net->ksnn_lock);
2537 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET, /* power of 2? */
2538 "waiting for %d peers to disconnect\n",
2540 cfs_pause(cfs_time_seconds(1));
2542 ksocknal_debug_peerhash(ni);
2544 cfs_spin_lock_bh (&net->ksnn_lock);
2546 cfs_spin_unlock_bh (&net->ksnn_lock);
2548 for (i = 0; i < net->ksnn_ninterfaces; i++) {
2549 LASSERT (net->ksnn_interfaces[i].ksni_npeers == 0);
2550 LASSERT (net->ksnn_interfaces[i].ksni_nroutes == 0);
2553 LIBCFS_FREE(net, sizeof(*net));
2555 ksocknal_data.ksnd_nnets--;
2556 if (ksocknal_data.ksnd_nnets == 0)
2557 ksocknal_base_shutdown();
2561 ksocknal_enumerate_interfaces(ksock_net_t *net)
2569 n = libcfs_ipif_enumerate(&names);
2571 CERROR("Can't enumerate interfaces: %d\n", n);
2575 for (i = j = 0; i < n; i++) {
2580 if (!strcmp(names[i], "lo")) /* skip the loopback IF */
2583 rc = libcfs_ipif_query(names[i], &up, &ip, &mask);
2585 CWARN("Can't get interface %s info: %d\n",
2591 CWARN("Ignoring interface %s (down)\n",
2596 if (j == LNET_MAX_INTERFACES) {
2597 CWARN("Ignoring interface %s (too many interfaces)\n",
2602 net->ksnn_interfaces[j].ksni_ipaddr = ip;
2603 net->ksnn_interfaces[j].ksni_netmask = mask;
2607 libcfs_ipif_free_enumeration(names, n);
2610 CERROR("Can't find any usable interfaces\n");
2616 ksocknal_startup (lnet_ni_t *ni)
2622 LASSERT (ni->ni_lnd == &the_ksocklnd);
2624 if (ksocknal_data.ksnd_init == SOCKNAL_INIT_NOTHING) {
2625 rc = ksocknal_base_startup();
2630 LIBCFS_ALLOC(net, sizeof(*net));
2634 memset(net, 0, sizeof(*net));
2635 cfs_spin_lock_init(&net->ksnn_lock);
2636 net->ksnn_incarnation = ksocknal_new_incarnation();
2638 ni->ni_peertimeout = *ksocknal_tunables.ksnd_peertimeout;
2639 ni->ni_maxtxcredits = *ksocknal_tunables.ksnd_credits;
2640 ni->ni_peertxcredits = *ksocknal_tunables.ksnd_peertxcredits;
2641 ni->ni_peerrtrcredits = *ksocknal_tunables.ksnd_peerrtrcredits;
2643 if (ni->ni_interfaces[0] == NULL) {
2644 rc = ksocknal_enumerate_interfaces(net);
2648 net->ksnn_ninterfaces = 1;
2650 for (i = 0; i < LNET_MAX_INTERFACES; i++) {
2653 if (ni->ni_interfaces[i] == NULL)
2656 rc = libcfs_ipif_query(
2657 ni->ni_interfaces[i], &up,
2658 &net->ksnn_interfaces[i].ksni_ipaddr,
2659 &net->ksnn_interfaces[i].ksni_netmask);
2662 CERROR("Can't get interface %s info: %d\n",
2663 ni->ni_interfaces[i], rc);
2668 CERROR("Interface %s is down\n",
2669 ni->ni_interfaces[i]);
2673 net->ksnn_ninterfaces = i;
2676 ni->ni_nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid),
2677 net->ksnn_interfaces[0].ksni_ipaddr);
2679 ksocknal_data.ksnd_nnets++;
2684 LIBCFS_FREE(net, sizeof(*net));
2686 if (ksocknal_data.ksnd_nnets == 0)
2687 ksocknal_base_shutdown();
2694 ksocknal_module_fini (void)
2696 lnet_unregister_lnd(&the_ksocklnd);
2697 ksocknal_tunables_fini();
2701 ksocknal_module_init (void)
2705 /* check ksnr_connected/connecting field large enough */
2706 CLASSERT (SOCKLND_CONN_NTYPES <= 4);
2707 CLASSERT (SOCKLND_CONN_ACK == SOCKLND_CONN_BULK_IN);
2709 /* initialize the_ksocklnd */
2710 the_ksocklnd.lnd_type = SOCKLND;
2711 the_ksocklnd.lnd_startup = ksocknal_startup;
2712 the_ksocklnd.lnd_shutdown = ksocknal_shutdown;
2713 the_ksocklnd.lnd_ctl = ksocknal_ctl;
2714 the_ksocklnd.lnd_send = ksocknal_send;
2715 the_ksocklnd.lnd_recv = ksocknal_recv;
2716 the_ksocklnd.lnd_notify = ksocknal_notify;
2717 the_ksocklnd.lnd_query = ksocknal_query;
2718 the_ksocklnd.lnd_accept = ksocknal_accept;
2720 rc = ksocknal_tunables_init();
2724 lnet_register_lnd(&the_ksocklnd);
2729 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2730 MODULE_DESCRIPTION("Kernel TCP Socket LND v3.0.0");
2731 MODULE_LICENSE("GPL");
2733 cfs_module(ksocknal, "3.0.0", ksocknal_module_init, ksocknal_module_fini);