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) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2015, Intel Corporation.
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>
46 static lnd_t the_ksocklnd;
47 ksock_nal_data_t ksocknal_data;
49 static ksock_interface_t *
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)
67 static ksock_route_t *
68 ksocknal_create_route (__u32 ipaddr, int port)
72 LIBCFS_ALLOC (route, sizeof (*route));
76 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 (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 int cpt = lnet_cpt_of_nid(id.nid);
106 ksock_net_t *net = ni->ni_data;
109 LASSERT(id.nid != LNET_NID_ANY);
110 LASSERT(id.pid != LNET_PID_ANY);
111 LASSERT(!in_interrupt());
113 LIBCFS_CPT_ALLOC(peer, lnet_cpt_table(), cpt, sizeof(*peer));
119 atomic_set(&peer->ksnp_refcount, 1); /* 1 ref for caller */
120 peer->ksnp_closing = 0;
121 peer->ksnp_accepting = 0;
122 peer->ksnp_proto = NULL;
123 peer->ksnp_last_alive = 0;
124 peer->ksnp_zc_next_cookie = SOCKNAL_KEEPALIVE_PING + 1;
126 INIT_LIST_HEAD(&peer->ksnp_conns);
127 INIT_LIST_HEAD(&peer->ksnp_routes);
128 INIT_LIST_HEAD(&peer->ksnp_tx_queue);
129 INIT_LIST_HEAD(&peer->ksnp_zc_req_list);
130 spin_lock_init(&peer->ksnp_lock);
132 spin_lock_bh(&net->ksnn_lock);
134 if (net->ksnn_shutdown) {
135 spin_unlock_bh(&net->ksnn_lock);
137 LIBCFS_FREE(peer, sizeof(*peer));
138 CERROR("Can't create peer: network shutdown\n");
144 spin_unlock_bh(&net->ksnn_lock);
151 ksocknal_destroy_peer (ksock_peer_t *peer)
153 ksock_net_t *net = peer->ksnp_ni->ni_data;
155 CDEBUG (D_NET, "peer %s %p deleted\n",
156 libcfs_id2str(peer->ksnp_id), peer);
158 LASSERT(atomic_read(&peer->ksnp_refcount) == 0);
159 LASSERT(peer->ksnp_accepting == 0);
160 LASSERT(list_empty(&peer->ksnp_conns));
161 LASSERT(list_empty(&peer->ksnp_routes));
162 LASSERT(list_empty(&peer->ksnp_tx_queue));
163 LASSERT(list_empty(&peer->ksnp_zc_req_list));
165 LIBCFS_FREE(peer, sizeof(*peer));
167 /* NB a peer's connections and routes keep a reference on their peer
168 * until they are destroyed, so we can be assured that _all_ state to
169 * do with this peer has been cleaned up when its refcount drops to
171 spin_lock_bh(&net->ksnn_lock);
173 spin_unlock_bh(&net->ksnn_lock);
177 ksocknal_find_peer_locked (lnet_ni_t *ni, lnet_process_id_t id)
179 struct list_head *peer_list = ksocknal_nid2peerlist(id.nid);
180 struct list_head *tmp;
183 list_for_each(tmp, peer_list) {
185 peer = list_entry(tmp, ksock_peer_t, ksnp_list);
187 LASSERT(!peer->ksnp_closing);
189 if (peer->ksnp_ni != ni)
192 if (peer->ksnp_id.nid != id.nid ||
193 peer->ksnp_id.pid != id.pid)
196 CDEBUG(D_NET, "got peer [%p] -> %s (%d)\n",
197 peer, libcfs_id2str(id),
198 atomic_read(&peer->ksnp_refcount));
205 ksocknal_find_peer (lnet_ni_t *ni, lnet_process_id_t id)
209 read_lock(&ksocknal_data.ksnd_global_lock);
210 peer = ksocknal_find_peer_locked(ni, id);
211 if (peer != NULL) /* +1 ref for caller? */
212 ksocknal_peer_addref(peer);
213 read_unlock(&ksocknal_data.ksnd_global_lock);
219 ksocknal_unlink_peer_locked (ksock_peer_t *peer)
223 ksock_interface_t *iface;
225 for (i = 0; i < peer->ksnp_n_passive_ips; i++) {
226 LASSERT (i < LNET_MAX_INTERFACES);
227 ip = peer->ksnp_passive_ips[i];
229 iface = ksocknal_ip2iface(peer->ksnp_ni, ip);
230 /* All IPs in peer->ksnp_passive_ips[] come from the
231 * interface list, therefore the call must succeed. */
232 LASSERT (iface != NULL);
234 CDEBUG(D_NET, "peer=%p iface=%p ksni_nroutes=%d\n",
235 peer, iface, iface->ksni_nroutes);
236 iface->ksni_npeers--;
239 LASSERT(list_empty(&peer->ksnp_conns));
240 LASSERT(list_empty(&peer->ksnp_routes));
241 LASSERT(!peer->ksnp_closing);
242 peer->ksnp_closing = 1;
243 list_del(&peer->ksnp_list);
244 /* lose peerlist's ref */
245 ksocknal_peer_decref(peer);
249 ksocknal_get_peer_info (lnet_ni_t *ni, int index,
250 lnet_process_id_t *id, __u32 *myip, __u32 *peer_ip,
251 int *port, int *conn_count, int *share_count)
254 struct list_head *ptmp;
255 ksock_route_t *route;
256 struct list_head *rtmp;
261 read_lock(&ksocknal_data.ksnd_global_lock);
263 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
264 list_for_each(ptmp, &ksocknal_data.ksnd_peers[i]) {
265 peer = list_entry(ptmp, ksock_peer_t, ksnp_list);
267 if (peer->ksnp_ni != ni)
270 if (peer->ksnp_n_passive_ips == 0 &&
271 list_empty(&peer->ksnp_routes)) {
285 for (j = 0; j < peer->ksnp_n_passive_ips; j++) {
290 *myip = peer->ksnp_passive_ips[j];
299 list_for_each(rtmp, &peer->ksnp_routes) {
303 route = list_entry(rtmp, ksock_route_t,
307 *myip = route->ksnr_myipaddr;
308 *peer_ip = route->ksnr_ipaddr;
309 *port = route->ksnr_port;
310 *conn_count = route->ksnr_conn_count;
311 *share_count = route->ksnr_share_count;
318 read_unlock(&ksocknal_data.ksnd_global_lock);
323 ksocknal_associate_route_conn_locked(ksock_route_t *route, ksock_conn_t *conn)
325 ksock_peer_t *peer = route->ksnr_peer;
326 int type = conn->ksnc_type;
327 ksock_interface_t *iface;
329 conn->ksnc_route = route;
330 ksocknal_route_addref(route);
332 if (route->ksnr_myipaddr != conn->ksnc_myipaddr) {
333 if (route->ksnr_myipaddr == 0) {
334 /* route wasn't bound locally yet (the initial route) */
335 CDEBUG(D_NET, "Binding %s %pI4h to %pI4h\n",
336 libcfs_id2str(peer->ksnp_id),
338 &conn->ksnc_myipaddr);
340 CDEBUG(D_NET, "Rebinding %s %pI4h from %pI4h "
341 "to %pI4h\n", libcfs_id2str(peer->ksnp_id),
343 &route->ksnr_myipaddr,
344 &conn->ksnc_myipaddr);
346 iface = ksocknal_ip2iface(route->ksnr_peer->ksnp_ni,
347 route->ksnr_myipaddr);
349 iface->ksni_nroutes--;
351 route->ksnr_myipaddr = conn->ksnc_myipaddr;
352 iface = ksocknal_ip2iface(route->ksnr_peer->ksnp_ni,
353 route->ksnr_myipaddr);
355 iface->ksni_nroutes++;
358 route->ksnr_connected |= (1<<type);
359 route->ksnr_conn_count++;
361 /* Successful connection => further attempts can
362 * proceed immediately */
363 route->ksnr_retry_interval = 0;
367 ksocknal_add_route_locked (ksock_peer_t *peer, ksock_route_t *route)
369 struct list_head *tmp;
371 ksock_route_t *route2;
373 LASSERT(!peer->ksnp_closing);
374 LASSERT(route->ksnr_peer == NULL);
375 LASSERT(!route->ksnr_scheduled);
376 LASSERT(!route->ksnr_connecting);
377 LASSERT(route->ksnr_connected == 0);
379 /* LASSERT(unique) */
380 list_for_each(tmp, &peer->ksnp_routes) {
381 route2 = list_entry(tmp, ksock_route_t, ksnr_list);
383 if (route2->ksnr_ipaddr == route->ksnr_ipaddr) {
384 CERROR("Duplicate route %s %pI4h\n",
385 libcfs_id2str(peer->ksnp_id),
386 &route->ksnr_ipaddr);
391 route->ksnr_peer = peer;
392 ksocknal_peer_addref(peer);
393 /* peer's routelist takes over my ref on 'route' */
394 list_add_tail(&route->ksnr_list, &peer->ksnp_routes);
396 list_for_each(tmp, &peer->ksnp_conns) {
397 conn = list_entry(tmp, ksock_conn_t, ksnc_list);
399 if (conn->ksnc_ipaddr != route->ksnr_ipaddr)
402 ksocknal_associate_route_conn_locked(route, conn);
403 /* keep going (typed routes) */
408 ksocknal_del_route_locked (ksock_route_t *route)
410 ksock_peer_t *peer = route->ksnr_peer;
411 ksock_interface_t *iface;
413 struct list_head *ctmp;
414 struct list_head *cnxt;
416 LASSERT(!route->ksnr_deleted);
418 /* Close associated conns */
419 list_for_each_safe(ctmp, cnxt, &peer->ksnp_conns) {
420 conn = list_entry(ctmp, ksock_conn_t, ksnc_list);
422 if (conn->ksnc_route != route)
425 ksocknal_close_conn_locked(conn, 0);
428 if (route->ksnr_myipaddr != 0) {
429 iface = ksocknal_ip2iface(route->ksnr_peer->ksnp_ni,
430 route->ksnr_myipaddr);
432 iface->ksni_nroutes--;
435 route->ksnr_deleted = 1;
436 list_del(&route->ksnr_list);
437 ksocknal_route_decref(route); /* drop peer's ref */
439 if (list_empty(&peer->ksnp_routes) &&
440 list_empty(&peer->ksnp_conns)) {
441 /* I've just removed the last route to a peer with no active
443 ksocknal_unlink_peer_locked(peer);
448 ksocknal_add_peer(lnet_ni_t *ni, lnet_process_id_t id, __u32 ipaddr, int port)
450 struct list_head *tmp;
453 ksock_route_t *route;
454 ksock_route_t *route2;
457 if (id.nid == LNET_NID_ANY ||
458 id.pid == LNET_PID_ANY)
461 /* Have a brand new peer ready... */
462 rc = ksocknal_create_peer(&peer, ni, id);
466 route = ksocknal_create_route (ipaddr, port);
468 ksocknal_peer_decref(peer);
472 write_lock_bh(&ksocknal_data.ksnd_global_lock);
474 /* always called with a ref on ni, so shutdown can't have started */
475 LASSERT (((ksock_net_t *) ni->ni_data)->ksnn_shutdown == 0);
477 peer2 = ksocknal_find_peer_locked(ni, id);
479 ksocknal_peer_decref(peer);
482 /* peer table takes my ref on peer */
483 list_add_tail(&peer->ksnp_list,
484 ksocknal_nid2peerlist(id.nid));
488 list_for_each(tmp, &peer->ksnp_routes) {
489 route2 = list_entry(tmp, ksock_route_t, ksnr_list);
491 if (route2->ksnr_ipaddr == ipaddr)
496 if (route2 == NULL) {
497 ksocknal_add_route_locked(peer, route);
498 route->ksnr_share_count++;
500 ksocknal_route_decref(route);
501 route2->ksnr_share_count++;
504 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
510 ksocknal_del_peer_locked (ksock_peer_t *peer, __u32 ip)
513 ksock_route_t *route;
514 struct list_head *tmp;
515 struct list_head *nxt;
518 LASSERT(!peer->ksnp_closing);
520 /* Extra ref prevents peer disappearing until I'm done with it */
521 ksocknal_peer_addref(peer);
523 list_for_each_safe(tmp, nxt, &peer->ksnp_routes) {
524 route = list_entry(tmp, ksock_route_t, ksnr_list);
527 if (!(ip == 0 || route->ksnr_ipaddr == ip))
530 route->ksnr_share_count = 0;
531 /* This deletes associated conns too */
532 ksocknal_del_route_locked(route);
536 list_for_each_safe(tmp, nxt, &peer->ksnp_routes) {
537 route = list_entry(tmp, ksock_route_t, ksnr_list);
538 nshared += route->ksnr_share_count;
542 /* remove everything else if there are no explicit entries
545 list_for_each_safe(tmp, nxt, &peer->ksnp_routes) {
546 route = list_entry(tmp, ksock_route_t, ksnr_list);
548 /* we should only be removing auto-entries */
549 LASSERT(route->ksnr_share_count == 0);
550 ksocknal_del_route_locked(route);
553 list_for_each_safe(tmp, nxt, &peer->ksnp_conns) {
554 conn = list_entry(tmp, ksock_conn_t, ksnc_list);
556 ksocknal_close_conn_locked(conn, 0);
560 ksocknal_peer_decref(peer);
561 /* NB peer unlinks itself when last conn/route is removed */
565 ksocknal_del_peer (lnet_ni_t *ni, lnet_process_id_t id, __u32 ip)
567 struct list_head zombies = LIST_HEAD_INIT(zombies);
568 struct list_head *ptmp;
569 struct list_head *pnxt;
576 write_lock_bh(&ksocknal_data.ksnd_global_lock);
578 if (id.nid != LNET_NID_ANY) {
579 hi = (int)(ksocknal_nid2peerlist(id.nid) -
580 ksocknal_data.ksnd_peers);
584 hi = ksocknal_data.ksnd_peer_hash_size - 1;
587 for (i = lo; i <= hi; i++) {
588 list_for_each_safe(ptmp, pnxt,
589 &ksocknal_data.ksnd_peers[i]) {
590 peer = list_entry(ptmp, ksock_peer_t, ksnp_list);
592 if (peer->ksnp_ni != ni)
595 if (!((id.nid == LNET_NID_ANY ||
596 peer->ksnp_id.nid == id.nid) &&
597 (id.pid == LNET_PID_ANY ||
598 peer->ksnp_id.pid == id.pid)))
601 ksocknal_peer_addref(peer); /* a ref for me... */
603 ksocknal_del_peer_locked(peer, ip);
605 if (peer->ksnp_closing &&
606 !list_empty(&peer->ksnp_tx_queue)) {
607 LASSERT(list_empty(&peer->ksnp_conns));
608 LASSERT(list_empty(&peer->ksnp_routes));
610 list_splice_init(&peer->ksnp_tx_queue,
614 ksocknal_peer_decref(peer); /* ...till here */
616 rc = 0; /* matched! */
620 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
622 ksocknal_txlist_done(ni, &zombies, 1);
627 static ksock_conn_t *
628 ksocknal_get_conn_by_idx (lnet_ni_t *ni, int index)
631 struct list_head *ptmp;
633 struct list_head *ctmp;
636 read_lock(&ksocknal_data.ksnd_global_lock);
638 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
639 list_for_each(ptmp, &ksocknal_data.ksnd_peers[i]) {
640 peer = list_entry(ptmp, ksock_peer_t, ksnp_list);
642 LASSERT(!peer->ksnp_closing);
644 if (peer->ksnp_ni != ni)
647 list_for_each(ctmp, &peer->ksnp_conns) {
651 conn = list_entry(ctmp, ksock_conn_t,
653 ksocknal_conn_addref(conn);
654 read_unlock(&ksocknal_data. \
661 read_unlock(&ksocknal_data.ksnd_global_lock);
665 static ksock_sched_t *
666 ksocknal_choose_scheduler_locked(unsigned int cpt)
668 struct ksock_sched_info *info = ksocknal_data.ksnd_sched_info[cpt];
669 ksock_sched_t *sched;
672 LASSERT(info->ksi_nthreads > 0);
674 sched = &info->ksi_scheds[0];
676 * NB: it's safe so far, but info->ksi_nthreads could be changed
677 * at runtime when we have dynamic LNet configuration, then we
678 * need to take care of this.
680 for (i = 1; i < info->ksi_nthreads; i++) {
681 if (sched->kss_nconns > info->ksi_scheds[i].kss_nconns)
682 sched = &info->ksi_scheds[i];
689 ksocknal_local_ipvec (lnet_ni_t *ni, __u32 *ipaddrs)
691 ksock_net_t *net = ni->ni_data;
695 read_lock(&ksocknal_data.ksnd_global_lock);
697 nip = net->ksnn_ninterfaces;
698 LASSERT (nip <= LNET_MAX_INTERFACES);
700 /* Only offer interfaces for additional connections if I have
703 read_unlock(&ksocknal_data.ksnd_global_lock);
707 for (i = 0; i < nip; i++) {
708 ipaddrs[i] = net->ksnn_interfaces[i].ksni_ipaddr;
709 LASSERT (ipaddrs[i] != 0);
712 read_unlock(&ksocknal_data.ksnd_global_lock);
717 ksocknal_match_peerip (ksock_interface_t *iface, __u32 *ips, int nips)
719 int best_netmatch = 0;
726 for (i = 0; i < nips; i++) {
730 this_xor = (ips[i] ^ iface->ksni_ipaddr);
731 this_netmatch = ((this_xor & iface->ksni_netmask) == 0) ? 1 : 0;
734 best_netmatch < this_netmatch ||
735 (best_netmatch == this_netmatch &&
736 best_xor > this_xor)))
740 best_netmatch = this_netmatch;
749 ksocknal_select_ips(ksock_peer_t *peer, __u32 *peerips, int n_peerips)
751 rwlock_t *global_lock = &ksocknal_data.ksnd_global_lock;
752 ksock_net_t *net = peer->ksnp_ni->ni_data;
753 ksock_interface_t *iface;
754 ksock_interface_t *best_iface;
765 /* CAVEAT EMPTOR: We do all our interface matching with an
766 * exclusive hold of global lock at IRQ priority. We're only
767 * expecting to be dealing with small numbers of interfaces, so the
768 * O(n**3)-ness shouldn't matter */
770 /* Also note that I'm not going to return more than n_peerips
771 * interfaces, even if I have more myself */
773 write_lock_bh(global_lock);
775 LASSERT (n_peerips <= LNET_MAX_INTERFACES);
776 LASSERT (net->ksnn_ninterfaces <= LNET_MAX_INTERFACES);
778 /* Only match interfaces for additional connections
779 * if I have > 1 interface */
780 n_ips = (net->ksnn_ninterfaces < 2) ? 0 :
781 MIN(n_peerips, net->ksnn_ninterfaces);
783 for (i = 0; peer->ksnp_n_passive_ips < n_ips; i++) {
784 /* ^ yes really... */
786 /* If we have any new interfaces, first tick off all the
787 * peer IPs that match old interfaces, then choose new
788 * interfaces to match the remaining peer IPS.
789 * We don't forget interfaces we've stopped using; we might
790 * start using them again... */
792 if (i < peer->ksnp_n_passive_ips) {
794 ip = peer->ksnp_passive_ips[i];
795 best_iface = ksocknal_ip2iface(peer->ksnp_ni, ip);
797 /* peer passive ips are kept up to date */
798 LASSERT(best_iface != NULL);
800 /* choose a new interface */
801 LASSERT (i == peer->ksnp_n_passive_ips);
807 for (j = 0; j < net->ksnn_ninterfaces; j++) {
808 iface = &net->ksnn_interfaces[j];
809 ip = iface->ksni_ipaddr;
811 for (k = 0; k < peer->ksnp_n_passive_ips; k++)
812 if (peer->ksnp_passive_ips[k] == ip)
815 if (k < peer->ksnp_n_passive_ips) /* using it already */
818 k = ksocknal_match_peerip(iface, peerips, n_peerips);
819 xor = (ip ^ peerips[k]);
820 this_netmatch = ((xor & iface->ksni_netmask) == 0) ? 1 : 0;
822 if (!(best_iface == NULL ||
823 best_netmatch < this_netmatch ||
824 (best_netmatch == this_netmatch &&
825 best_npeers > iface->ksni_npeers)))
829 best_netmatch = this_netmatch;
830 best_npeers = iface->ksni_npeers;
833 LASSERT(best_iface != NULL);
835 best_iface->ksni_npeers++;
836 ip = best_iface->ksni_ipaddr;
837 peer->ksnp_passive_ips[i] = ip;
838 peer->ksnp_n_passive_ips = i+1;
841 /* mark the best matching peer IP used */
842 j = ksocknal_match_peerip(best_iface, peerips, n_peerips);
846 /* Overwrite input peer IP addresses */
847 memcpy(peerips, peer->ksnp_passive_ips, n_ips * sizeof(*peerips));
849 write_unlock_bh(global_lock);
855 ksocknal_create_routes(ksock_peer_t *peer, int port,
856 __u32 *peer_ipaddrs, int npeer_ipaddrs)
858 ksock_route_t *newroute = NULL;
859 rwlock_t *global_lock = &ksocknal_data.ksnd_global_lock;
860 lnet_ni_t *ni = peer->ksnp_ni;
861 ksock_net_t *net = ni->ni_data;
862 struct list_head *rtmp;
863 ksock_route_t *route;
864 ksock_interface_t *iface;
865 ksock_interface_t *best_iface;
872 /* CAVEAT EMPTOR: We do all our interface matching with an
873 * exclusive hold of global lock at IRQ priority. We're only
874 * expecting to be dealing with small numbers of interfaces, so the
875 * O(n**3)-ness here shouldn't matter */
877 write_lock_bh(global_lock);
879 if (net->ksnn_ninterfaces < 2) {
880 /* Only create additional connections
881 * if I have > 1 interface */
882 write_unlock_bh(global_lock);
886 LASSERT (npeer_ipaddrs <= LNET_MAX_INTERFACES);
888 for (i = 0; i < npeer_ipaddrs; i++) {
889 if (newroute != NULL) {
890 newroute->ksnr_ipaddr = peer_ipaddrs[i];
892 write_unlock_bh(global_lock);
894 newroute = ksocknal_create_route(peer_ipaddrs[i], port);
895 if (newroute == NULL)
898 write_lock_bh(global_lock);
901 if (peer->ksnp_closing) {
902 /* peer got closed under me */
906 /* Already got a route? */
908 list_for_each(rtmp, &peer->ksnp_routes) {
909 route = list_entry(rtmp, ksock_route_t, ksnr_list);
911 if (route->ksnr_ipaddr == newroute->ksnr_ipaddr)
923 LASSERT(net->ksnn_ninterfaces <= LNET_MAX_INTERFACES);
925 /* Select interface to connect from */
926 for (j = 0; j < net->ksnn_ninterfaces; j++) {
927 iface = &net->ksnn_interfaces[j];
929 /* Using this interface already? */
930 list_for_each(rtmp, &peer->ksnp_routes) {
931 route = list_entry(rtmp, ksock_route_t,
934 if (route->ksnr_myipaddr == iface->ksni_ipaddr)
942 this_netmatch = (((iface->ksni_ipaddr ^
943 newroute->ksnr_ipaddr) &
944 iface->ksni_netmask) == 0) ? 1 : 0;
946 if (!(best_iface == NULL ||
947 best_netmatch < this_netmatch ||
948 (best_netmatch == this_netmatch &&
949 best_nroutes > iface->ksni_nroutes)))
953 best_netmatch = this_netmatch;
954 best_nroutes = iface->ksni_nroutes;
957 if (best_iface == NULL)
960 newroute->ksnr_myipaddr = best_iface->ksni_ipaddr;
961 best_iface->ksni_nroutes++;
963 ksocknal_add_route_locked(peer, newroute);
967 write_unlock_bh(global_lock);
968 if (newroute != NULL)
969 ksocknal_route_decref(newroute);
973 ksocknal_accept(lnet_ni_t *ni, struct socket *sock)
980 rc = lnet_sock_getaddr(sock, true, &peer_ip, &peer_port);
981 LASSERT(rc == 0); /* we succeeded before */
983 LIBCFS_ALLOC(cr, sizeof(*cr));
985 LCONSOLE_ERROR_MSG(0x12f, "Dropping connection request from "
986 "%pI4h: memory exhausted\n", &peer_ip);
992 cr->ksncr_sock = sock;
994 spin_lock_bh(&ksocknal_data.ksnd_connd_lock);
996 list_add_tail(&cr->ksncr_list, &ksocknal_data.ksnd_connd_connreqs);
997 wake_up(&ksocknal_data.ksnd_connd_waitq);
999 spin_unlock_bh(&ksocknal_data.ksnd_connd_lock);
1004 ksocknal_connecting (ksock_peer_t *peer, __u32 ipaddr)
1006 ksock_route_t *route;
1008 list_for_each_entry(route, &peer->ksnp_routes, ksnr_list) {
1009 if (route->ksnr_ipaddr == ipaddr)
1010 return route->ksnr_connecting;
1016 ksocknal_create_conn(lnet_ni_t *ni, ksock_route_t *route,
1017 struct socket *sock, int type)
1019 rwlock_t *global_lock = &ksocknal_data.ksnd_global_lock;
1020 struct list_head zombies = LIST_HEAD_INIT(zombies);
1021 lnet_process_id_t peerid;
1022 struct list_head *tmp;
1025 ksock_conn_t *conn2;
1026 ksock_peer_t *peer = NULL;
1027 ksock_peer_t *peer2;
1028 ksock_sched_t *sched;
1029 ksock_hello_msg_t *hello;
1037 active = (route != NULL);
1039 LASSERT (active == (type != SOCKLND_CONN_NONE));
1041 LIBCFS_ALLOC(conn, sizeof(*conn));
1047 memset (conn, 0, sizeof (*conn));
1049 conn->ksnc_peer = NULL;
1050 conn->ksnc_route = NULL;
1051 conn->ksnc_sock = sock;
1052 /* 2 ref, 1 for conn, another extra ref prevents socket
1053 * being closed before establishment of connection */
1054 atomic_set (&conn->ksnc_sock_refcount, 2);
1055 conn->ksnc_type = type;
1056 ksocknal_lib_save_callback(sock, conn);
1057 atomic_set (&conn->ksnc_conn_refcount, 1); /* 1 ref for me */
1059 conn->ksnc_rx_ready = 0;
1060 conn->ksnc_rx_scheduled = 0;
1062 INIT_LIST_HEAD(&conn->ksnc_tx_queue);
1063 conn->ksnc_tx_ready = 0;
1064 conn->ksnc_tx_scheduled = 0;
1065 conn->ksnc_tx_carrier = NULL;
1066 atomic_set (&conn->ksnc_tx_nob, 0);
1068 LIBCFS_ALLOC(hello, offsetof(ksock_hello_msg_t,
1069 kshm_ips[LNET_MAX_INTERFACES]));
1070 if (hello == NULL) {
1075 /* stash conn's local and remote addrs */
1076 rc = ksocknal_lib_get_conn_addrs (conn);
1080 /* Find out/confirm peer's NID and connection type and get the
1081 * vector of interfaces she's willing to let me connect to.
1082 * Passive connections use the listener timeout since the peer sends
1086 peer = route->ksnr_peer;
1087 LASSERT(ni == peer->ksnp_ni);
1089 /* Active connection sends HELLO eagerly */
1090 hello->kshm_nips = ksocknal_local_ipvec(ni, hello->kshm_ips);
1091 peerid = peer->ksnp_id;
1093 write_lock_bh(global_lock);
1094 conn->ksnc_proto = peer->ksnp_proto;
1095 write_unlock_bh(global_lock);
1097 if (conn->ksnc_proto == NULL) {
1098 conn->ksnc_proto = &ksocknal_protocol_v3x;
1099 #if SOCKNAL_VERSION_DEBUG
1100 if (*ksocknal_tunables.ksnd_protocol == 2)
1101 conn->ksnc_proto = &ksocknal_protocol_v2x;
1102 else if (*ksocknal_tunables.ksnd_protocol == 1)
1103 conn->ksnc_proto = &ksocknal_protocol_v1x;
1107 rc = ksocknal_send_hello (ni, conn, peerid.nid, hello);
1111 peerid.nid = LNET_NID_ANY;
1112 peerid.pid = LNET_PID_ANY;
1114 /* Passive, get protocol from peer */
1115 conn->ksnc_proto = NULL;
1118 rc = ksocknal_recv_hello (ni, conn, hello, &peerid, &incarnation);
1122 LASSERT (rc == 0 || active);
1123 LASSERT (conn->ksnc_proto != NULL);
1124 LASSERT (peerid.nid != LNET_NID_ANY);
1126 cpt = lnet_cpt_of_nid(peerid.nid);
1129 ksocknal_peer_addref(peer);
1130 write_lock_bh(global_lock);
1132 rc = ksocknal_create_peer(&peer, ni, peerid);
1136 write_lock_bh(global_lock);
1138 /* called with a ref on ni, so shutdown can't have started */
1139 LASSERT (((ksock_net_t *) ni->ni_data)->ksnn_shutdown == 0);
1141 peer2 = ksocknal_find_peer_locked(ni, peerid);
1142 if (peer2 == NULL) {
1143 /* NB this puts an "empty" peer in the peer
1144 * table (which takes my ref) */
1145 list_add_tail(&peer->ksnp_list,
1146 ksocknal_nid2peerlist(peerid.nid));
1148 ksocknal_peer_decref(peer);
1153 ksocknal_peer_addref(peer);
1154 peer->ksnp_accepting++;
1156 /* Am I already connecting to this guy? Resolve in
1157 * favour of higher NID... */
1158 if (peerid.nid < ni->ni_nid &&
1159 ksocknal_connecting(peer, conn->ksnc_ipaddr)) {
1161 warn = "connection race resolution";
1166 if (peer->ksnp_closing ||
1167 (active && route->ksnr_deleted)) {
1168 /* peer/route got closed under me */
1170 warn = "peer/route removed";
1174 if (peer->ksnp_proto == NULL) {
1175 /* Never connected before.
1176 * NB recv_hello may have returned EPROTO to signal my peer
1177 * wants a different protocol than the one I asked for.
1179 LASSERT(list_empty(&peer->ksnp_conns));
1181 peer->ksnp_proto = conn->ksnc_proto;
1182 peer->ksnp_incarnation = incarnation;
1185 if (peer->ksnp_proto != conn->ksnc_proto ||
1186 peer->ksnp_incarnation != incarnation) {
1187 /* Peer rebooted or I've got the wrong protocol version */
1188 ksocknal_close_peer_conns_locked(peer, 0, 0);
1190 peer->ksnp_proto = NULL;
1192 warn = peer->ksnp_incarnation != incarnation ?
1194 "wrong proto version";
1204 warn = "lost conn race";
1207 warn = "retry with different protocol version";
1211 /* Refuse to duplicate an existing connection, unless this is a
1212 * loopback connection */
1213 if (conn->ksnc_ipaddr != conn->ksnc_myipaddr) {
1214 list_for_each(tmp, &peer->ksnp_conns) {
1215 conn2 = list_entry(tmp, ksock_conn_t, ksnc_list);
1217 if (conn2->ksnc_ipaddr != conn->ksnc_ipaddr ||
1218 conn2->ksnc_myipaddr != conn->ksnc_myipaddr ||
1219 conn2->ksnc_type != conn->ksnc_type)
1222 /* Reply on a passive connection attempt so the peer
1223 * realises we're connected. */
1233 /* If the connection created by this route didn't bind to the IP
1234 * address the route connected to, the connection/route matching
1235 * code below probably isn't going to work. */
1237 route->ksnr_ipaddr != conn->ksnc_ipaddr) {
1238 CERROR("Route %s %pI4h connected to %pI4h\n",
1239 libcfs_id2str(peer->ksnp_id),
1240 &route->ksnr_ipaddr,
1241 &conn->ksnc_ipaddr);
1244 /* Search for a route corresponding to the new connection and
1245 * create an association. This allows incoming connections created
1246 * by routes in my peer to match my own route entries so I don't
1247 * continually create duplicate routes. */
1248 list_for_each(tmp, &peer->ksnp_routes) {
1249 route = list_entry(tmp, ksock_route_t, ksnr_list);
1251 if (route->ksnr_ipaddr != conn->ksnc_ipaddr)
1254 ksocknal_associate_route_conn_locked(route, conn);
1258 conn->ksnc_peer = peer; /* conn takes my ref on peer */
1259 peer->ksnp_last_alive = cfs_time_current();
1260 peer->ksnp_send_keepalive = 0;
1261 peer->ksnp_error = 0;
1263 sched = ksocknal_choose_scheduler_locked(cpt);
1264 sched->kss_nconns++;
1265 conn->ksnc_scheduler = sched;
1267 conn->ksnc_tx_last_post = cfs_time_current();
1268 /* Set the deadline for the outgoing HELLO to drain */
1269 conn->ksnc_tx_bufnob = sock->sk->sk_wmem_queued;
1270 conn->ksnc_tx_deadline = cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
1271 smp_mb(); /* order with adding to peer's conn list */
1273 list_add(&conn->ksnc_list, &peer->ksnp_conns);
1274 ksocknal_conn_addref(conn);
1276 ksocknal_new_packet(conn, 0);
1278 conn->ksnc_zc_capable = ksocknal_lib_zc_capable(conn);
1280 /* Take packets blocking for this connection. */
1281 list_for_each_entry_safe(tx, txtmp, &peer->ksnp_tx_queue, tx_list) {
1282 if (conn->ksnc_proto->pro_match_tx(conn, tx, tx->tx_nonblk) ==
1286 list_del(&tx->tx_list);
1287 ksocknal_queue_tx_locked(tx, conn);
1290 write_unlock_bh(global_lock);
1292 /* We've now got a new connection. Any errors from here on are just
1293 * like "normal" comms errors and we close the connection normally.
1294 * NB (a) we still have to send the reply HELLO for passive
1296 * (b) normal I/O on the conn is blocked until I setup and call the
1300 CDEBUG(D_NET, "New conn %s p %d.x %pI4h -> %pI4h/%d"
1301 " incarnation:%lld sched[%d:%d]\n",
1302 libcfs_id2str(peerid), conn->ksnc_proto->pro_version,
1303 &conn->ksnc_myipaddr, &conn->ksnc_ipaddr,
1304 conn->ksnc_port, incarnation, cpt,
1305 (int)(sched - &sched->kss_info->ksi_scheds[0]));
1308 /* additional routes after interface exchange? */
1309 ksocknal_create_routes(peer, conn->ksnc_port,
1310 hello->kshm_ips, hello->kshm_nips);
1312 hello->kshm_nips = ksocknal_select_ips(peer, hello->kshm_ips,
1314 rc = ksocknal_send_hello(ni, conn, peerid.nid, hello);
1317 LIBCFS_FREE(hello, offsetof(ksock_hello_msg_t,
1318 kshm_ips[LNET_MAX_INTERFACES]));
1320 /* setup the socket AFTER I've received hello (it disables
1321 * SO_LINGER). I might call back to the acceptor who may want
1322 * to send a protocol version response and then close the
1323 * socket; this ensures the socket only tears down after the
1324 * response has been sent. */
1326 rc = ksocknal_lib_setup_sock(sock);
1328 write_lock_bh(global_lock);
1330 /* NB my callbacks block while I hold ksnd_global_lock */
1331 ksocknal_lib_set_callback(sock, conn);
1334 peer->ksnp_accepting--;
1336 write_unlock_bh(global_lock);
1339 write_lock_bh(global_lock);
1340 if (!conn->ksnc_closing) {
1341 /* could be closed by another thread */
1342 ksocknal_close_conn_locked(conn, rc);
1344 write_unlock_bh(global_lock);
1345 } else if (ksocknal_connsock_addref(conn) == 0) {
1346 /* Allow I/O to proceed. */
1347 ksocknal_read_callback(conn);
1348 ksocknal_write_callback(conn);
1349 ksocknal_connsock_decref(conn);
1352 ksocknal_connsock_decref(conn);
1353 ksocknal_conn_decref(conn);
1357 if (!peer->ksnp_closing &&
1358 list_empty(&peer->ksnp_conns) &&
1359 list_empty(&peer->ksnp_routes)) {
1360 list_add(&zombies, &peer->ksnp_tx_queue);
1361 list_del_init(&peer->ksnp_tx_queue);
1362 ksocknal_unlink_peer_locked(peer);
1365 write_unlock_bh(global_lock);
1369 CERROR("Not creating conn %s type %d: %s\n",
1370 libcfs_id2str(peerid), conn->ksnc_type, warn);
1372 CDEBUG(D_NET, "Not creating conn %s type %d: %s\n",
1373 libcfs_id2str(peerid), conn->ksnc_type, warn);
1378 /* Request retry by replying with CONN_NONE
1379 * ksnc_proto has been set already */
1380 conn->ksnc_type = SOCKLND_CONN_NONE;
1381 hello->kshm_nips = 0;
1382 ksocknal_send_hello(ni, conn, peerid.nid, hello);
1385 write_lock_bh(global_lock);
1386 peer->ksnp_accepting--;
1387 write_unlock_bh(global_lock);
1390 ksocknal_txlist_done(ni, &zombies, 1);
1391 ksocknal_peer_decref(peer);
1395 LIBCFS_FREE(hello, offsetof(ksock_hello_msg_t,
1396 kshm_ips[LNET_MAX_INTERFACES]));
1398 LIBCFS_FREE(conn, sizeof(*conn));
1406 ksocknal_close_conn_locked (ksock_conn_t *conn, int error)
1408 /* This just does the immmediate housekeeping, and queues the
1409 * connection for the reaper to terminate.
1410 * Caller holds ksnd_global_lock exclusively in irq context */
1411 ksock_peer_t *peer = conn->ksnc_peer;
1412 ksock_route_t *route;
1413 ksock_conn_t *conn2;
1414 struct list_head *tmp;
1416 LASSERT(peer->ksnp_error == 0);
1417 LASSERT(!conn->ksnc_closing);
1418 conn->ksnc_closing = 1;
1420 /* ksnd_deathrow_conns takes over peer's ref */
1421 list_del(&conn->ksnc_list);
1423 route = conn->ksnc_route;
1424 if (route != NULL) {
1425 /* dissociate conn from route... */
1426 LASSERT(!route->ksnr_deleted);
1427 LASSERT((route->ksnr_connected & (1 << conn->ksnc_type)) != 0);
1430 list_for_each(tmp, &peer->ksnp_conns) {
1431 conn2 = list_entry(tmp, ksock_conn_t, ksnc_list);
1433 if (conn2->ksnc_route == route &&
1434 conn2->ksnc_type == conn->ksnc_type)
1440 route->ksnr_connected &= ~(1 << conn->ksnc_type);
1442 conn->ksnc_route = NULL;
1444 #if 0 /* irrelevent with only eager routes */
1445 /* make route least favourite */
1446 list_del(&route->ksnr_list);
1447 list_add_tail(&route->ksnr_list, &peer->ksnp_routes);
1449 ksocknal_route_decref(route); /* drop conn's ref on route */
1452 if (list_empty(&peer->ksnp_conns)) {
1453 /* No more connections to this peer */
1455 if (!list_empty(&peer->ksnp_tx_queue)) {
1458 LASSERT(conn->ksnc_proto == &ksocknal_protocol_v3x);
1460 /* throw them to the last connection...,
1461 * these TXs will be send to /dev/null by scheduler */
1462 list_for_each_entry(tx, &peer->ksnp_tx_queue,
1464 ksocknal_tx_prep(conn, tx);
1466 spin_lock_bh(&conn->ksnc_scheduler->kss_lock);
1467 list_splice_init(&peer->ksnp_tx_queue,
1468 &conn->ksnc_tx_queue);
1469 spin_unlock_bh(&conn->ksnc_scheduler->kss_lock);
1472 /* renegotiate protocol version */
1473 peer->ksnp_proto = NULL;
1474 /* stash last conn close reason */
1475 peer->ksnp_error = error;
1477 if (list_empty(&peer->ksnp_routes)) {
1478 /* I've just closed last conn belonging to a
1479 * peer with no routes to it */
1480 ksocknal_unlink_peer_locked(peer);
1484 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
1486 list_add_tail(&conn->ksnc_list,
1487 &ksocknal_data.ksnd_deathrow_conns);
1488 wake_up(&ksocknal_data.ksnd_reaper_waitq);
1490 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
1494 ksocknal_peer_failed (ksock_peer_t *peer)
1497 cfs_time_t last_alive = 0;
1499 /* There has been a connection failure or comms error; but I'll only
1500 * tell LNET I think the peer is dead if it's to another kernel and
1501 * there are no connections or connection attempts in existence. */
1503 read_lock(&ksocknal_data.ksnd_global_lock);
1505 if ((peer->ksnp_id.pid & LNET_PID_USERFLAG) == 0 &&
1506 list_empty(&peer->ksnp_conns) &&
1507 peer->ksnp_accepting == 0 &&
1508 ksocknal_find_connecting_route_locked(peer) == NULL) {
1510 last_alive = peer->ksnp_last_alive;
1513 read_unlock(&ksocknal_data.ksnd_global_lock);
1516 lnet_notify(peer->ksnp_ni, peer->ksnp_id.nid, 0,
1521 ksocknal_finalize_zcreq(ksock_conn_t *conn)
1523 ksock_peer_t *peer = conn->ksnc_peer;
1526 struct list_head zlist = LIST_HEAD_INIT(zlist);
1528 /* NB safe to finalize TXs because closing of socket will
1529 * abort all buffered data */
1530 LASSERT(conn->ksnc_sock == NULL);
1532 spin_lock(&peer->ksnp_lock);
1534 list_for_each_entry_safe(tx, tmp, &peer->ksnp_zc_req_list, tx_zc_list) {
1535 if (tx->tx_conn != conn)
1538 LASSERT(tx->tx_msg.ksm_zc_cookies[0] != 0);
1540 tx->tx_msg.ksm_zc_cookies[0] = 0;
1541 tx->tx_zc_aborted = 1; /* mark it as not-acked */
1542 list_del(&tx->tx_zc_list);
1543 list_add(&tx->tx_zc_list, &zlist);
1546 spin_unlock(&peer->ksnp_lock);
1548 while (!list_empty(&zlist)) {
1549 tx = list_entry(zlist.next, ksock_tx_t, tx_zc_list);
1551 list_del(&tx->tx_zc_list);
1552 ksocknal_tx_decref(tx);
1557 ksocknal_terminate_conn(ksock_conn_t *conn)
1559 /* This gets called by the reaper (guaranteed thread context) to
1560 * disengage the socket from its callbacks and close it.
1561 * ksnc_refcount will eventually hit zero, and then the reaper will
1563 ksock_peer_t *peer = conn->ksnc_peer;
1564 ksock_sched_t *sched = conn->ksnc_scheduler;
1567 LASSERT(conn->ksnc_closing);
1569 /* wake up the scheduler to "send" all remaining packets to /dev/null */
1570 spin_lock_bh(&sched->kss_lock);
1572 /* a closing conn is always ready to tx */
1573 conn->ksnc_tx_ready = 1;
1575 if (!conn->ksnc_tx_scheduled &&
1576 !list_empty(&conn->ksnc_tx_queue)) {
1577 list_add_tail(&conn->ksnc_tx_list,
1578 &sched->kss_tx_conns);
1579 conn->ksnc_tx_scheduled = 1;
1580 /* extra ref for scheduler */
1581 ksocknal_conn_addref(conn);
1583 wake_up (&sched->kss_waitq);
1586 spin_unlock_bh(&sched->kss_lock);
1588 /* serialise with callbacks */
1589 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1591 ksocknal_lib_reset_callback(conn->ksnc_sock, conn);
1593 /* OK, so this conn may not be completely disengaged from its
1594 * scheduler yet, but it _has_ committed to terminate... */
1595 conn->ksnc_scheduler->kss_nconns--;
1597 if (peer->ksnp_error != 0) {
1598 /* peer's last conn closed in error */
1599 LASSERT(list_empty(&peer->ksnp_conns));
1601 peer->ksnp_error = 0; /* avoid multiple notifications */
1604 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1607 ksocknal_peer_failed(peer);
1609 /* The socket is closed on the final put; either here, or in
1610 * ksocknal_{send,recv}msg(). Since we set up the linger2 option
1611 * when the connection was established, this will close the socket
1612 * immediately, aborting anything buffered in it. Any hung
1613 * zero-copy transmits will therefore complete in finite time. */
1614 ksocknal_connsock_decref(conn);
1618 ksocknal_queue_zombie_conn (ksock_conn_t *conn)
1620 /* Queue the conn for the reaper to destroy */
1622 LASSERT(atomic_read(&conn->ksnc_conn_refcount) == 0);
1623 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
1625 list_add_tail(&conn->ksnc_list, &ksocknal_data.ksnd_zombie_conns);
1626 wake_up(&ksocknal_data.ksnd_reaper_waitq);
1628 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
1632 ksocknal_destroy_conn (ksock_conn_t *conn)
1634 cfs_time_t last_rcv;
1636 /* Final coup-de-grace of the reaper */
1637 CDEBUG (D_NET, "connection %p\n", conn);
1639 LASSERT (atomic_read (&conn->ksnc_conn_refcount) == 0);
1640 LASSERT (atomic_read (&conn->ksnc_sock_refcount) == 0);
1641 LASSERT (conn->ksnc_sock == NULL);
1642 LASSERT (conn->ksnc_route == NULL);
1643 LASSERT (!conn->ksnc_tx_scheduled);
1644 LASSERT (!conn->ksnc_rx_scheduled);
1645 LASSERT(list_empty(&conn->ksnc_tx_queue));
1647 /* complete current receive if any */
1648 switch (conn->ksnc_rx_state) {
1649 case SOCKNAL_RX_LNET_PAYLOAD:
1650 last_rcv = conn->ksnc_rx_deadline -
1651 cfs_time_seconds(*ksocknal_tunables.ksnd_timeout);
1652 CERROR("Completing partial receive from %s[%d], "
1653 "ip %pI4h:%d, with error, wanted: %d, left: %d, "
1654 "last alive is %ld secs ago\n",
1655 libcfs_id2str(conn->ksnc_peer->ksnp_id), conn->ksnc_type,
1656 &conn->ksnc_ipaddr, conn->ksnc_port,
1657 conn->ksnc_rx_nob_wanted, conn->ksnc_rx_nob_left,
1658 cfs_duration_sec(cfs_time_sub(cfs_time_current(),
1660 lnet_finalize (conn->ksnc_peer->ksnp_ni,
1661 conn->ksnc_cookie, -EIO);
1663 case SOCKNAL_RX_LNET_HEADER:
1664 if (conn->ksnc_rx_started)
1665 CERROR("Incomplete receive of lnet header from %s, "
1666 "ip %pI4h:%d, with error, protocol: %d.x.\n",
1667 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1668 &conn->ksnc_ipaddr, conn->ksnc_port,
1669 conn->ksnc_proto->pro_version);
1671 case SOCKNAL_RX_KSM_HEADER:
1672 if (conn->ksnc_rx_started)
1673 CERROR("Incomplete receive of ksock message from %s, "
1674 "ip %pI4h:%d, with error, protocol: %d.x.\n",
1675 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1676 &conn->ksnc_ipaddr, conn->ksnc_port,
1677 conn->ksnc_proto->pro_version);
1679 case SOCKNAL_RX_SLOP:
1680 if (conn->ksnc_rx_started)
1681 CERROR("Incomplete receive of slops from %s, "
1682 "ip %pI4h:%d, with error\n",
1683 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1684 &conn->ksnc_ipaddr, conn->ksnc_port);
1691 ksocknal_peer_decref(conn->ksnc_peer);
1693 LIBCFS_FREE (conn, sizeof (*conn));
1697 ksocknal_close_peer_conns_locked (ksock_peer_t *peer, __u32 ipaddr, int why)
1700 struct list_head *ctmp;
1701 struct list_head *cnxt;
1704 list_for_each_safe(ctmp, cnxt, &peer->ksnp_conns) {
1705 conn = list_entry(ctmp, ksock_conn_t, ksnc_list);
1708 conn->ksnc_ipaddr == ipaddr) {
1710 ksocknal_close_conn_locked (conn, why);
1718 ksocknal_close_conn_and_siblings (ksock_conn_t *conn, int why)
1720 ksock_peer_t *peer = conn->ksnc_peer;
1721 __u32 ipaddr = conn->ksnc_ipaddr;
1724 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1726 count = ksocknal_close_peer_conns_locked (peer, ipaddr, why);
1728 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1734 ksocknal_close_matching_conns (lnet_process_id_t id, __u32 ipaddr)
1737 struct list_head *ptmp;
1738 struct list_head *pnxt;
1744 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1746 if (id.nid != LNET_NID_ANY)
1747 lo = hi = (int)(ksocknal_nid2peerlist(id.nid) - ksocknal_data.ksnd_peers);
1750 hi = ksocknal_data.ksnd_peer_hash_size - 1;
1753 for (i = lo; i <= hi; i++) {
1754 list_for_each_safe(ptmp, pnxt, &ksocknal_data.ksnd_peers[i]) {
1756 peer = list_entry(ptmp, ksock_peer_t, ksnp_list);
1758 if (!((id.nid == LNET_NID_ANY || id.nid == peer->ksnp_id.nid) &&
1759 (id.pid == LNET_PID_ANY || id.pid == peer->ksnp_id.pid)))
1762 count += ksocknal_close_peer_conns_locked (peer, ipaddr, 0);
1766 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1768 /* wildcards always succeed */
1769 if (id.nid == LNET_NID_ANY || id.pid == LNET_PID_ANY || ipaddr == 0)
1772 return (count == 0 ? -ENOENT : 0);
1776 ksocknal_notify (lnet_ni_t *ni, lnet_nid_t gw_nid, int alive)
1778 /* The router is telling me she's been notified of a change in
1779 * gateway state.... */
1780 lnet_process_id_t id = {0};
1783 id.pid = LNET_PID_ANY;
1785 CDEBUG (D_NET, "gw %s %s\n", libcfs_nid2str(gw_nid),
1786 alive ? "up" : "down");
1789 /* If the gateway crashed, close all open connections... */
1790 ksocknal_close_matching_conns (id, 0);
1794 /* ...otherwise do nothing. We can only establish new connections
1795 * if we have autroutes, and these connect on demand. */
1799 ksocknal_query (lnet_ni_t *ni, lnet_nid_t nid, cfs_time_t *when)
1802 cfs_time_t last_alive = 0;
1803 cfs_time_t now = cfs_time_current();
1804 ksock_peer_t *peer = NULL;
1805 rwlock_t *glock = &ksocknal_data.ksnd_global_lock;
1806 lnet_process_id_t id = {
1808 .pid = LNET_PID_LUSTRE,
1813 peer = ksocknal_find_peer_locked(ni, id);
1815 struct list_head *tmp;
1819 list_for_each(tmp, &peer->ksnp_conns) {
1820 conn = list_entry(tmp, ksock_conn_t, ksnc_list);
1821 bufnob = conn->ksnc_sock->sk->sk_wmem_queued;
1823 if (bufnob < conn->ksnc_tx_bufnob) {
1824 /* something got ACKed */
1825 conn->ksnc_tx_deadline =
1826 cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
1827 peer->ksnp_last_alive = now;
1828 conn->ksnc_tx_bufnob = bufnob;
1832 last_alive = peer->ksnp_last_alive;
1833 if (ksocknal_find_connectable_route_locked(peer) == NULL)
1839 if (last_alive != 0)
1842 CDEBUG(D_NET, "Peer %s %p, alive %ld secs ago, connect %d\n",
1843 libcfs_nid2str(nid), peer,
1844 last_alive ? cfs_duration_sec(now - last_alive) : -1,
1850 ksocknal_add_peer(ni, id, LNET_NIDADDR(nid), lnet_acceptor_port());
1852 write_lock_bh(glock);
1854 peer = ksocknal_find_peer_locked(ni, id);
1856 ksocknal_launch_all_connections_locked(peer);
1858 write_unlock_bh(glock);
1863 ksocknal_push_peer (ksock_peer_t *peer)
1867 struct list_head *tmp;
1870 for (index = 0; ; index++) {
1871 read_lock(&ksocknal_data.ksnd_global_lock);
1876 list_for_each(tmp, &peer->ksnp_conns) {
1878 conn = list_entry(tmp, ksock_conn_t,
1880 ksocknal_conn_addref(conn);
1885 read_unlock(&ksocknal_data.ksnd_global_lock);
1890 ksocknal_lib_push_conn (conn);
1891 ksocknal_conn_decref(conn);
1896 ksocknal_push (lnet_ni_t *ni, lnet_process_id_t id)
1898 struct list_head *start;
1899 struct list_head *end;
1900 struct list_head *tmp;
1902 unsigned int hsize = ksocknal_data.ksnd_peer_hash_size;
1904 if (id.nid == LNET_NID_ANY) {
1905 start = &ksocknal_data.ksnd_peers[0];
1906 end = &ksocknal_data.ksnd_peers[hsize - 1];
1908 start = end = ksocknal_nid2peerlist(id.nid);
1911 for (tmp = start; tmp <= end; tmp++) {
1912 int peer_off; /* searching offset in peer hash table */
1914 for (peer_off = 0; ; peer_off++) {
1918 read_lock(&ksocknal_data.ksnd_global_lock);
1919 list_for_each_entry(peer, tmp, ksnp_list) {
1920 if (!((id.nid == LNET_NID_ANY ||
1921 id.nid == peer->ksnp_id.nid) &&
1922 (id.pid == LNET_PID_ANY ||
1923 id.pid == peer->ksnp_id.pid)))
1926 if (i++ == peer_off) {
1927 ksocknal_peer_addref(peer);
1931 read_unlock(&ksocknal_data.ksnd_global_lock);
1933 if (i == 0) /* no match */
1937 ksocknal_push_peer(peer);
1938 ksocknal_peer_decref(peer);
1945 ksocknal_add_interface(lnet_ni_t *ni, __u32 ipaddress, __u32 netmask)
1947 ksock_net_t *net = ni->ni_data;
1948 ksock_interface_t *iface;
1952 struct list_head *ptmp;
1954 struct list_head *rtmp;
1955 ksock_route_t *route;
1957 if (ipaddress == 0 ||
1961 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1963 iface = ksocknal_ip2iface(ni, ipaddress);
1964 if (iface != NULL) {
1965 /* silently ignore dups */
1967 } else if (net->ksnn_ninterfaces == LNET_MAX_INTERFACES) {
1970 iface = &net->ksnn_interfaces[net->ksnn_ninterfaces++];
1972 iface->ksni_ipaddr = ipaddress;
1973 iface->ksni_netmask = netmask;
1974 iface->ksni_nroutes = 0;
1975 iface->ksni_npeers = 0;
1977 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
1978 list_for_each(ptmp, &ksocknal_data.ksnd_peers[i]) {
1979 peer = list_entry(ptmp, ksock_peer_t,
1982 for (j = 0; j < peer->ksnp_n_passive_ips; j++)
1983 if (peer->ksnp_passive_ips[j] == ipaddress)
1984 iface->ksni_npeers++;
1986 list_for_each(rtmp, &peer->ksnp_routes) {
1987 route = list_entry(rtmp,
1991 if (route->ksnr_myipaddr == ipaddress)
1992 iface->ksni_nroutes++;
1998 /* NB only new connections will pay attention to the new interface! */
2001 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
2007 ksocknal_peer_del_interface_locked(ksock_peer_t *peer, __u32 ipaddr)
2009 struct list_head *tmp;
2010 struct list_head *nxt;
2011 ksock_route_t *route;
2016 for (i = 0; i < peer->ksnp_n_passive_ips; i++)
2017 if (peer->ksnp_passive_ips[i] == ipaddr) {
2018 for (j = i+1; j < peer->ksnp_n_passive_ips; j++)
2019 peer->ksnp_passive_ips[j-1] =
2020 peer->ksnp_passive_ips[j];
2021 peer->ksnp_n_passive_ips--;
2025 list_for_each_safe(tmp, nxt, &peer->ksnp_routes) {
2026 route = list_entry(tmp, ksock_route_t, ksnr_list);
2028 if (route->ksnr_myipaddr != ipaddr)
2031 if (route->ksnr_share_count != 0) {
2032 /* Manually created; keep, but unbind */
2033 route->ksnr_myipaddr = 0;
2035 ksocknal_del_route_locked(route);
2039 list_for_each_safe(tmp, nxt, &peer->ksnp_conns) {
2040 conn = list_entry(tmp, ksock_conn_t, ksnc_list);
2042 if (conn->ksnc_myipaddr == ipaddr)
2043 ksocknal_close_conn_locked (conn, 0);
2048 ksocknal_del_interface(lnet_ni_t *ni, __u32 ipaddress)
2050 ksock_net_t *net = ni->ni_data;
2052 struct list_head *tmp;
2053 struct list_head *nxt;
2059 write_lock_bh(&ksocknal_data.ksnd_global_lock);
2061 for (i = 0; i < net->ksnn_ninterfaces; i++) {
2062 this_ip = net->ksnn_interfaces[i].ksni_ipaddr;
2064 if (!(ipaddress == 0 ||
2065 ipaddress == this_ip))
2070 for (j = i+1; j < net->ksnn_ninterfaces; j++)
2071 net->ksnn_interfaces[j-1] =
2072 net->ksnn_interfaces[j];
2074 net->ksnn_ninterfaces--;
2076 for (j = 0; j < ksocknal_data.ksnd_peer_hash_size; j++) {
2077 list_for_each_safe(tmp, nxt,
2078 &ksocknal_data.ksnd_peers[j]) {
2079 peer = list_entry(tmp, ksock_peer_t,
2082 if (peer->ksnp_ni != ni)
2085 ksocknal_peer_del_interface_locked(peer, this_ip);
2090 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
2096 ksocknal_ctl(lnet_ni_t *ni, unsigned int cmd, void *arg)
2098 lnet_process_id_t id = {0};
2099 struct libcfs_ioctl_data *data = arg;
2103 case IOC_LIBCFS_GET_INTERFACE: {
2104 ksock_net_t *net = ni->ni_data;
2105 ksock_interface_t *iface;
2107 read_lock(&ksocknal_data.ksnd_global_lock);
2109 if (data->ioc_count >= (__u32)net->ksnn_ninterfaces) {
2113 iface = &net->ksnn_interfaces[data->ioc_count];
2115 data->ioc_u32[0] = iface->ksni_ipaddr;
2116 data->ioc_u32[1] = iface->ksni_netmask;
2117 data->ioc_u32[2] = iface->ksni_npeers;
2118 data->ioc_u32[3] = iface->ksni_nroutes;
2121 read_unlock(&ksocknal_data.ksnd_global_lock);
2125 case IOC_LIBCFS_ADD_INTERFACE:
2126 return ksocknal_add_interface(ni,
2127 data->ioc_u32[0], /* IP address */
2128 data->ioc_u32[1]); /* net mask */
2130 case IOC_LIBCFS_DEL_INTERFACE:
2131 return ksocknal_del_interface(ni,
2132 data->ioc_u32[0]); /* IP address */
2134 case IOC_LIBCFS_GET_PEER: {
2139 int share_count = 0;
2141 rc = ksocknal_get_peer_info(ni, data->ioc_count,
2142 &id, &myip, &ip, &port,
2143 &conn_count, &share_count);
2147 data->ioc_nid = id.nid;
2148 data->ioc_count = share_count;
2149 data->ioc_u32[0] = ip;
2150 data->ioc_u32[1] = port;
2151 data->ioc_u32[2] = myip;
2152 data->ioc_u32[3] = conn_count;
2153 data->ioc_u32[4] = id.pid;
2157 case IOC_LIBCFS_ADD_PEER:
2158 id.nid = data->ioc_nid;
2159 id.pid = LNET_PID_LUSTRE;
2160 return ksocknal_add_peer (ni, id,
2161 data->ioc_u32[0], /* IP */
2162 data->ioc_u32[1]); /* port */
2164 case IOC_LIBCFS_DEL_PEER:
2165 id.nid = data->ioc_nid;
2166 id.pid = LNET_PID_ANY;
2167 return ksocknal_del_peer (ni, id,
2168 data->ioc_u32[0]); /* IP */
2170 case IOC_LIBCFS_GET_CONN: {
2174 ksock_conn_t *conn = ksocknal_get_conn_by_idx (ni, data->ioc_count);
2179 ksocknal_lib_get_conn_tunables(conn, &txmem, &rxmem, &nagle);
2181 data->ioc_count = txmem;
2182 data->ioc_nid = conn->ksnc_peer->ksnp_id.nid;
2183 data->ioc_flags = nagle;
2184 data->ioc_u32[0] = conn->ksnc_ipaddr;
2185 data->ioc_u32[1] = conn->ksnc_port;
2186 data->ioc_u32[2] = conn->ksnc_myipaddr;
2187 data->ioc_u32[3] = conn->ksnc_type;
2188 data->ioc_u32[4] = conn->ksnc_scheduler->kss_info->ksi_cpt;
2189 data->ioc_u32[5] = rxmem;
2190 data->ioc_u32[6] = conn->ksnc_peer->ksnp_id.pid;
2191 ksocknal_conn_decref(conn);
2195 case IOC_LIBCFS_CLOSE_CONNECTION:
2196 id.nid = data->ioc_nid;
2197 id.pid = LNET_PID_ANY;
2198 return ksocknal_close_matching_conns (id,
2201 case IOC_LIBCFS_REGISTER_MYNID:
2202 /* Ignore if this is a noop */
2203 if (data->ioc_nid == ni->ni_nid)
2206 CERROR("obsolete IOC_LIBCFS_REGISTER_MYNID: %s(%s)\n",
2207 libcfs_nid2str(data->ioc_nid),
2208 libcfs_nid2str(ni->ni_nid));
2211 case IOC_LIBCFS_PUSH_CONNECTION:
2212 id.nid = data->ioc_nid;
2213 id.pid = LNET_PID_ANY;
2214 return ksocknal_push(ni, id);
2223 ksocknal_free_buffers (void)
2225 LASSERT (atomic_read(&ksocknal_data.ksnd_nactive_txs) == 0);
2227 if (ksocknal_data.ksnd_sched_info != NULL) {
2228 struct ksock_sched_info *info;
2231 cfs_percpt_for_each(info, i, ksocknal_data.ksnd_sched_info) {
2232 if (info->ksi_scheds != NULL) {
2233 LIBCFS_FREE(info->ksi_scheds,
2234 info->ksi_nthreads_max *
2235 sizeof(info->ksi_scheds[0]));
2238 cfs_percpt_free(ksocknal_data.ksnd_sched_info);
2241 LIBCFS_FREE (ksocknal_data.ksnd_peers,
2242 sizeof(struct list_head) *
2243 ksocknal_data.ksnd_peer_hash_size);
2245 spin_lock(&ksocknal_data.ksnd_tx_lock);
2247 if (!list_empty(&ksocknal_data.ksnd_idle_noop_txs)) {
2248 struct list_head zlist;
2251 list_add(&zlist, &ksocknal_data.ksnd_idle_noop_txs);
2252 list_del_init(&ksocknal_data.ksnd_idle_noop_txs);
2253 spin_unlock(&ksocknal_data.ksnd_tx_lock);
2255 while (!list_empty(&zlist)) {
2256 tx = list_entry(zlist.next, ksock_tx_t, tx_list);
2257 list_del(&tx->tx_list);
2258 LIBCFS_FREE(tx, tx->tx_desc_size);
2261 spin_unlock(&ksocknal_data.ksnd_tx_lock);
2266 ksocknal_base_shutdown(void)
2268 struct ksock_sched_info *info;
2269 ksock_sched_t *sched;
2273 CDEBUG(D_MALLOC, "before NAL cleanup: kmem %d\n",
2274 atomic_read (&libcfs_kmemory));
2275 LASSERT (ksocknal_data.ksnd_nnets == 0);
2277 switch (ksocknal_data.ksnd_init) {
2281 case SOCKNAL_INIT_ALL:
2282 case SOCKNAL_INIT_DATA:
2283 LASSERT (ksocknal_data.ksnd_peers != NULL);
2284 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
2285 LASSERT(list_empty(&ksocknal_data.ksnd_peers[i]));
2288 LASSERT(list_empty(&ksocknal_data.ksnd_nets));
2289 LASSERT(list_empty(&ksocknal_data.ksnd_enomem_conns));
2290 LASSERT(list_empty(&ksocknal_data.ksnd_zombie_conns));
2291 LASSERT(list_empty(&ksocknal_data.ksnd_connd_connreqs));
2292 LASSERT(list_empty(&ksocknal_data.ksnd_connd_routes));
2294 if (ksocknal_data.ksnd_sched_info != NULL) {
2295 cfs_percpt_for_each(info, i,
2296 ksocknal_data.ksnd_sched_info) {
2297 if (info->ksi_scheds == NULL)
2300 for (j = 0; j < info->ksi_nthreads_max; j++) {
2302 sched = &info->ksi_scheds[j];
2303 LASSERT(list_empty(&sched->\
2305 LASSERT(list_empty(&sched->\
2307 LASSERT(list_empty(&sched-> \
2308 kss_zombie_noop_txs));
2309 LASSERT(sched->kss_nconns == 0);
2314 /* flag threads to terminate; wake and wait for them to die */
2315 ksocknal_data.ksnd_shuttingdown = 1;
2316 wake_up_all(&ksocknal_data.ksnd_connd_waitq);
2317 wake_up_all(&ksocknal_data.ksnd_reaper_waitq);
2319 if (ksocknal_data.ksnd_sched_info != NULL) {
2320 cfs_percpt_for_each(info, i,
2321 ksocknal_data.ksnd_sched_info) {
2322 if (info->ksi_scheds == NULL)
2325 for (j = 0; j < info->ksi_nthreads_max; j++) {
2326 sched = &info->ksi_scheds[j];
2327 wake_up_all(&sched->kss_waitq);
2333 read_lock(&ksocknal_data.ksnd_global_lock);
2334 while (ksocknal_data.ksnd_nthreads != 0) {
2337 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
2338 "waiting for %d threads to terminate\n",
2339 ksocknal_data.ksnd_nthreads);
2340 read_unlock(&ksocknal_data.ksnd_global_lock);
2341 set_current_state(TASK_UNINTERRUPTIBLE);
2342 schedule_timeout(cfs_time_seconds(1));
2343 read_lock(&ksocknal_data.ksnd_global_lock);
2345 read_unlock(&ksocknal_data.ksnd_global_lock);
2347 ksocknal_free_buffers();
2349 ksocknal_data.ksnd_init = SOCKNAL_INIT_NOTHING;
2353 CDEBUG(D_MALLOC, "after NAL cleanup: kmem %d\n",
2354 atomic_read (&libcfs_kmemory));
2356 module_put(THIS_MODULE);
2359 static __u64 ksocknal_new_incarnation(void)
2363 /* The incarnation number is the time this module loaded and it
2364 * identifies this particular instance of the socknal. Hopefully
2365 * we won't be able to reboot more frequently than 1MHz for the
2366 * forseeable future :) */
2368 do_gettimeofday(&tv);
2370 return (((__u64)tv.tv_sec) * 1000000) + tv.tv_usec;
2374 ksocknal_base_startup(void)
2376 struct ksock_sched_info *info;
2380 LASSERT (ksocknal_data.ksnd_init == SOCKNAL_INIT_NOTHING);
2381 LASSERT (ksocknal_data.ksnd_nnets == 0);
2383 memset (&ksocknal_data, 0, sizeof (ksocknal_data)); /* zero pointers */
2385 ksocknal_data.ksnd_peer_hash_size = SOCKNAL_PEER_HASH_SIZE;
2386 LIBCFS_ALLOC(ksocknal_data.ksnd_peers,
2387 sizeof(struct list_head) *
2388 ksocknal_data.ksnd_peer_hash_size);
2389 if (ksocknal_data.ksnd_peers == NULL)
2392 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++)
2393 INIT_LIST_HEAD(&ksocknal_data.ksnd_peers[i]);
2395 rwlock_init(&ksocknal_data.ksnd_global_lock);
2396 INIT_LIST_HEAD(&ksocknal_data.ksnd_nets);
2398 spin_lock_init(&ksocknal_data.ksnd_reaper_lock);
2399 INIT_LIST_HEAD(&ksocknal_data.ksnd_enomem_conns);
2400 INIT_LIST_HEAD(&ksocknal_data.ksnd_zombie_conns);
2401 INIT_LIST_HEAD(&ksocknal_data.ksnd_deathrow_conns);
2402 init_waitqueue_head(&ksocknal_data.ksnd_reaper_waitq);
2404 spin_lock_init(&ksocknal_data.ksnd_connd_lock);
2405 INIT_LIST_HEAD(&ksocknal_data.ksnd_connd_connreqs);
2406 INIT_LIST_HEAD(&ksocknal_data.ksnd_connd_routes);
2407 init_waitqueue_head(&ksocknal_data.ksnd_connd_waitq);
2409 spin_lock_init(&ksocknal_data.ksnd_tx_lock);
2410 INIT_LIST_HEAD(&ksocknal_data.ksnd_idle_noop_txs);
2412 /* NB memset above zeros whole of ksocknal_data */
2414 /* flag lists/ptrs/locks initialised */
2415 ksocknal_data.ksnd_init = SOCKNAL_INIT_DATA;
2416 try_module_get(THIS_MODULE);
2418 ksocknal_data.ksnd_sched_info = cfs_percpt_alloc(lnet_cpt_table(),
2420 if (ksocknal_data.ksnd_sched_info == NULL)
2423 cfs_percpt_for_each(info, i, ksocknal_data.ksnd_sched_info) {
2424 ksock_sched_t *sched;
2427 nthrs = cfs_cpt_weight(lnet_cpt_table(), i);
2428 if (*ksocknal_tunables.ksnd_nscheds > 0) {
2429 nthrs = min(nthrs, *ksocknal_tunables.ksnd_nscheds);
2431 /* max to half of CPUs, assume another half should be
2432 * reserved for upper layer modules */
2433 nthrs = min(max(SOCKNAL_NSCHEDS, nthrs >> 1), nthrs);
2436 info->ksi_nthreads_max = nthrs;
2439 LIBCFS_CPT_ALLOC(info->ksi_scheds, lnet_cpt_table(), i,
2440 info->ksi_nthreads_max * sizeof(*sched));
2441 if (info->ksi_scheds == NULL)
2444 for (; nthrs > 0; nthrs--) {
2445 sched = &info->ksi_scheds[nthrs - 1];
2447 sched->kss_info = info;
2448 spin_lock_init(&sched->kss_lock);
2449 INIT_LIST_HEAD(&sched->kss_rx_conns);
2450 INIT_LIST_HEAD(&sched->kss_tx_conns);
2451 INIT_LIST_HEAD(&sched->kss_zombie_noop_txs);
2452 init_waitqueue_head(&sched->kss_waitq);
2456 ksocknal_data.ksnd_connd_starting = 0;
2457 ksocknal_data.ksnd_connd_failed_stamp = 0;
2458 ksocknal_data.ksnd_connd_starting_stamp = cfs_time_current_sec();
2459 /* must have at least 2 connds to remain responsive to accepts while
2461 if (*ksocknal_tunables.ksnd_nconnds < SOCKNAL_CONND_RESV + 1)
2462 *ksocknal_tunables.ksnd_nconnds = SOCKNAL_CONND_RESV + 1;
2464 if (*ksocknal_tunables.ksnd_nconnds_max <
2465 *ksocknal_tunables.ksnd_nconnds) {
2466 ksocknal_tunables.ksnd_nconnds_max =
2467 ksocknal_tunables.ksnd_nconnds;
2470 for (i = 0; i < *ksocknal_tunables.ksnd_nconnds; i++) {
2472 spin_lock_bh(&ksocknal_data.ksnd_connd_lock);
2473 ksocknal_data.ksnd_connd_starting++;
2474 spin_unlock_bh(&ksocknal_data.ksnd_connd_lock);
2477 snprintf(name, sizeof(name), "socknal_cd%02d", i);
2478 rc = ksocknal_thread_start(ksocknal_connd,
2479 (void *)((ulong_ptr_t)i), name);
2481 spin_lock_bh(&ksocknal_data.ksnd_connd_lock);
2482 ksocknal_data.ksnd_connd_starting--;
2483 spin_unlock_bh(&ksocknal_data.ksnd_connd_lock);
2484 CERROR("Can't spawn socknal connd: %d\n", rc);
2489 rc = ksocknal_thread_start(ksocknal_reaper, NULL, "socknal_reaper");
2491 CERROR ("Can't spawn socknal reaper: %d\n", rc);
2495 /* flag everything initialised */
2496 ksocknal_data.ksnd_init = SOCKNAL_INIT_ALL;
2501 ksocknal_base_shutdown();
2506 ksocknal_debug_peerhash (lnet_ni_t *ni)
2508 ksock_peer_t *peer = NULL;
2509 struct list_head *tmp;
2512 read_lock(&ksocknal_data.ksnd_global_lock);
2514 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
2515 list_for_each(tmp, &ksocknal_data.ksnd_peers[i]) {
2516 peer = list_entry(tmp, ksock_peer_t, ksnp_list);
2518 if (peer->ksnp_ni == ni) break;
2525 ksock_route_t *route;
2528 CWARN ("Active peer on shutdown: %s, ref %d, scnt %d, "
2529 "closing %d, accepting %d, err %d, zcookie "LPU64", "
2530 "txq %d, zc_req %d\n", libcfs_id2str(peer->ksnp_id),
2531 atomic_read(&peer->ksnp_refcount),
2532 peer->ksnp_sharecount, peer->ksnp_closing,
2533 peer->ksnp_accepting, peer->ksnp_error,
2534 peer->ksnp_zc_next_cookie,
2535 !list_empty(&peer->ksnp_tx_queue),
2536 !list_empty(&peer->ksnp_zc_req_list));
2538 list_for_each(tmp, &peer->ksnp_routes) {
2539 route = list_entry(tmp, ksock_route_t, ksnr_list);
2540 CWARN ("Route: ref %d, schd %d, conn %d, cnted %d, "
2541 "del %d\n", atomic_read(&route->ksnr_refcount),
2542 route->ksnr_scheduled, route->ksnr_connecting,
2543 route->ksnr_connected, route->ksnr_deleted);
2546 list_for_each(tmp, &peer->ksnp_conns) {
2547 conn = list_entry(tmp, ksock_conn_t, ksnc_list);
2548 CWARN ("Conn: ref %d, sref %d, t %d, c %d\n",
2549 atomic_read(&conn->ksnc_conn_refcount),
2550 atomic_read(&conn->ksnc_sock_refcount),
2551 conn->ksnc_type, conn->ksnc_closing);
2555 read_unlock(&ksocknal_data.ksnd_global_lock);
2560 ksocknal_shutdown (lnet_ni_t *ni)
2562 ksock_net_t *net = ni->ni_data;
2564 lnet_process_id_t anyid = {0};
2566 anyid.nid = LNET_NID_ANY;
2567 anyid.pid = LNET_PID_ANY;
2569 LASSERT(ksocknal_data.ksnd_init == SOCKNAL_INIT_ALL);
2570 LASSERT(ksocknal_data.ksnd_nnets > 0);
2572 spin_lock_bh(&net->ksnn_lock);
2573 net->ksnn_shutdown = 1; /* prevent new peers */
2574 spin_unlock_bh(&net->ksnn_lock);
2576 /* Delete all peers */
2577 ksocknal_del_peer(ni, anyid, 0);
2579 /* Wait for all peer state to clean up */
2581 spin_lock_bh(&net->ksnn_lock);
2582 while (net->ksnn_npeers != 0) {
2583 spin_unlock_bh(&net->ksnn_lock);
2586 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET, /* power of 2? */
2587 "waiting for %d peers to disconnect\n",
2589 set_current_state(TASK_UNINTERRUPTIBLE);
2590 schedule_timeout(cfs_time_seconds(1));
2592 ksocknal_debug_peerhash(ni);
2594 spin_lock_bh(&net->ksnn_lock);
2596 spin_unlock_bh(&net->ksnn_lock);
2598 for (i = 0; i < net->ksnn_ninterfaces; i++) {
2599 LASSERT (net->ksnn_interfaces[i].ksni_npeers == 0);
2600 LASSERT (net->ksnn_interfaces[i].ksni_nroutes == 0);
2603 list_del(&net->ksnn_list);
2604 LIBCFS_FREE(net, sizeof(*net));
2606 ksocknal_data.ksnd_nnets--;
2607 if (ksocknal_data.ksnd_nnets == 0)
2608 ksocknal_base_shutdown();
2612 ksocknal_enumerate_interfaces(ksock_net_t *net)
2620 n = lnet_ipif_enumerate(&names);
2622 CERROR("Can't enumerate interfaces: %d\n", n);
2626 for (i = j = 0; i < n; i++) {
2631 if (!strcmp(names[i], "lo")) /* skip the loopback IF */
2634 rc = lnet_ipif_query(names[i], &up, &ip, &mask);
2636 CWARN("Can't get interface %s info: %d\n",
2642 CWARN("Ignoring interface %s (down)\n",
2647 if (j == LNET_MAX_INTERFACES) {
2648 CWARN("Ignoring interface %s (too many interfaces)\n",
2653 net->ksnn_interfaces[j].ksni_ipaddr = ip;
2654 net->ksnn_interfaces[j].ksni_netmask = mask;
2655 strlcpy(net->ksnn_interfaces[j].ksni_name,
2656 names[i], sizeof(net->ksnn_interfaces[j].ksni_name));
2660 lnet_ipif_free_enumeration(names, n);
2663 CERROR("Can't find any usable interfaces\n");
2669 ksocknal_search_new_ipif(ksock_net_t *net)
2674 for (i = 0; i < net->ksnn_ninterfaces; i++) {
2675 char *ifnam = &net->ksnn_interfaces[i].ksni_name[0];
2676 char *colon = strchr(ifnam, ':');
2681 if (colon != NULL) /* ignore alias device */
2684 list_for_each_entry(tmp, &ksocknal_data.ksnd_nets,
2686 for (j = 0; !found && j < tmp->ksnn_ninterfaces; j++) {
2687 char *ifnam2 = &tmp->ksnn_interfaces[j].\
2689 char *colon2 = strchr(ifnam2, ':');
2694 found = strcmp(ifnam, ifnam2) == 0;
2711 ksocknal_start_schedulers(struct ksock_sched_info *info)
2717 if (info->ksi_nthreads == 0) {
2718 if (*ksocknal_tunables.ksnd_nscheds > 0) {
2719 nthrs = info->ksi_nthreads_max;
2721 nthrs = cfs_cpt_weight(lnet_cpt_table(),
2723 nthrs = min(max(SOCKNAL_NSCHEDS, nthrs >> 1), nthrs);
2724 nthrs = min(SOCKNAL_NSCHEDS_HIGH, nthrs);
2726 nthrs = min(nthrs, info->ksi_nthreads_max);
2728 LASSERT(info->ksi_nthreads <= info->ksi_nthreads_max);
2729 /* increase two threads if there is new interface */
2730 nthrs = min(2, info->ksi_nthreads_max - info->ksi_nthreads);
2733 for (i = 0; i < nthrs; i++) {
2736 ksock_sched_t *sched;
2737 id = KSOCK_THREAD_ID(info->ksi_cpt, info->ksi_nthreads + i);
2738 sched = &info->ksi_scheds[KSOCK_THREAD_SID(id)];
2739 snprintf(name, sizeof(name), "socknal_sd%02d_%02d",
2740 info->ksi_cpt, (int)(sched - &info->ksi_scheds[0]));
2742 rc = ksocknal_thread_start(ksocknal_scheduler,
2747 CERROR("Can't spawn thread %d for scheduler[%d]: %d\n",
2748 info->ksi_cpt, info->ksi_nthreads + i, rc);
2752 info->ksi_nthreads += i;
2757 ksocknal_net_start_threads(ksock_net_t *net, __u32 *cpts, int ncpts)
2759 int newif = ksocknal_search_new_ipif(net);
2763 LASSERT(ncpts > 0 && ncpts <= cfs_cpt_number(lnet_cpt_table()));
2765 for (i = 0; i < ncpts; i++) {
2766 struct ksock_sched_info *info;
2767 int cpt = (cpts == NULL) ? i : cpts[i];
2769 LASSERT(cpt < cfs_cpt_number(lnet_cpt_table()));
2770 info = ksocknal_data.ksnd_sched_info[cpt];
2772 if (!newif && info->ksi_nthreads > 0)
2775 rc = ksocknal_start_schedulers(info);
2783 ksocknal_startup (lnet_ni_t *ni)
2789 LASSERT (ni->ni_lnd == &the_ksocklnd);
2791 if (ksocknal_data.ksnd_init == SOCKNAL_INIT_NOTHING) {
2792 rc = ksocknal_base_startup();
2797 LIBCFS_ALLOC(net, sizeof(*net));
2801 spin_lock_init(&net->ksnn_lock);
2802 net->ksnn_incarnation = ksocknal_new_incarnation();
2804 ni->ni_peertimeout = *ksocknal_tunables.ksnd_peertimeout;
2805 ni->ni_maxtxcredits = *ksocknal_tunables.ksnd_credits;
2806 ni->ni_peertxcredits = *ksocknal_tunables.ksnd_peertxcredits;
2807 ni->ni_peerrtrcredits = *ksocknal_tunables.ksnd_peerrtrcredits;
2809 if (ni->ni_interfaces[0] == NULL) {
2810 rc = ksocknal_enumerate_interfaces(net);
2814 net->ksnn_ninterfaces = 1;
2816 for (i = 0; i < LNET_MAX_INTERFACES; i++) {
2819 if (ni->ni_interfaces[i] == NULL)
2822 rc = lnet_ipif_query(ni->ni_interfaces[i], &up,
2823 &net->ksnn_interfaces[i].ksni_ipaddr,
2824 &net->ksnn_interfaces[i].ksni_netmask);
2827 CERROR("Can't get interface %s info: %d\n",
2828 ni->ni_interfaces[i], rc);
2833 CERROR("Interface %s is down\n",
2834 ni->ni_interfaces[i]);
2838 strlcpy(net->ksnn_interfaces[i].ksni_name,
2839 ni->ni_interfaces[i],
2840 sizeof(net->ksnn_interfaces[i].ksni_name));
2842 net->ksnn_ninterfaces = i;
2845 /* call it before add it to ksocknal_data.ksnd_nets */
2846 rc = ksocknal_net_start_threads(net, ni->ni_cpts, ni->ni_ncpts);
2850 ni->ni_nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid),
2851 net->ksnn_interfaces[0].ksni_ipaddr);
2852 list_add(&net->ksnn_list, &ksocknal_data.ksnd_nets);
2854 ksocknal_data.ksnd_nnets++;
2859 LIBCFS_FREE(net, sizeof(*net));
2861 if (ksocknal_data.ksnd_nnets == 0)
2862 ksocknal_base_shutdown();
2868 static void __exit ksocklnd_exit(void)
2870 lnet_unregister_lnd(&the_ksocklnd);
2871 ksocknal_tunables_fini();
2874 static int __init ksocklnd_init(void)
2878 /* check ksnr_connected/connecting field large enough */
2879 CLASSERT(SOCKLND_CONN_NTYPES <= 4);
2880 CLASSERT(SOCKLND_CONN_ACK == SOCKLND_CONN_BULK_IN);
2882 /* initialize the_ksocklnd */
2883 the_ksocklnd.lnd_type = SOCKLND;
2884 the_ksocklnd.lnd_startup = ksocknal_startup;
2885 the_ksocklnd.lnd_shutdown = ksocknal_shutdown;
2886 the_ksocklnd.lnd_ctl = ksocknal_ctl;
2887 the_ksocklnd.lnd_send = ksocknal_send;
2888 the_ksocklnd.lnd_recv = ksocknal_recv;
2889 the_ksocklnd.lnd_notify = ksocknal_notify;
2890 the_ksocklnd.lnd_query = ksocknal_query;
2891 the_ksocklnd.lnd_accept = ksocknal_accept;
2893 rc = ksocknal_tunables_init();
2897 lnet_register_lnd(&the_ksocklnd);
2902 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2903 MODULE_DESCRIPTION("TCP Socket LNet Network Driver");
2904 MODULE_VERSION("2.8.0");
2905 MODULE_LICENSE("GPL");
2907 module_init(ksocklnd_init);
2908 module_exit(ksocklnd_exit);