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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2015, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lnet/klnds/socklnd/socklnd.c
34 * Author: Zach Brown <zab@zabbo.net>
35 * Author: Peter J. Braam <braam@clusterfs.com>
36 * Author: Phil Schwan <phil@clusterfs.com>
37 * Author: Eric Barton <eric@bartonsoftware.com>
42 static lnd_t the_ksocklnd;
43 struct ksock_nal_data ksocknal_data;
45 static struct ksock_interface *
46 ksocknal_ip2iface(lnet_ni_t *ni, __u32 ip)
48 struct ksock_net *net = ni->ni_data;
50 struct ksock_interface *iface;
52 for (i = 0; i < net->ksnn_ninterfaces; i++) {
53 LASSERT(i < LNET_MAX_INTERFACES);
54 iface = &net->ksnn_interfaces[i];
56 if (iface->ksni_ipaddr == ip)
63 static struct ksock_route *
64 ksocknal_create_route(__u32 ipaddr, int port)
66 struct ksock_route *route;
68 LIBCFS_ALLOC(route, sizeof(*route));
72 atomic_set(&route->ksnr_refcount, 1);
73 route->ksnr_peer = NULL;
74 route->ksnr_retry_interval = 0; /* OK to connect at any time */
75 route->ksnr_ipaddr = ipaddr;
76 route->ksnr_port = port;
77 route->ksnr_scheduled = 0;
78 route->ksnr_connecting = 0;
79 route->ksnr_connected = 0;
80 route->ksnr_deleted = 0;
81 route->ksnr_conn_count = 0;
82 route->ksnr_share_count = 0;
88 ksocknal_destroy_route(struct ksock_route *route)
90 LASSERT(!atomic_read(&route->ksnr_refcount));
93 ksocknal_peer_decref(route->ksnr_peer);
95 LIBCFS_FREE(route, sizeof(*route));
99 ksocknal_create_peer(struct ksock_peer **peerp, lnet_ni_t *ni, lnet_process_id_t id)
101 int cpt = lnet_cpt_of_nid(id.nid);
102 struct ksock_net *net = ni->ni_data;
103 struct ksock_peer *peer;
105 LASSERT(id.nid != LNET_NID_ANY);
106 LASSERT(id.pid != LNET_PID_ANY);
107 LASSERT(!in_interrupt());
109 LIBCFS_CPT_ALLOC(peer, lnet_cpt_table(), cpt, sizeof(*peer));
115 atomic_set(&peer->ksnp_refcount, 1); /* 1 ref for caller */
116 peer->ksnp_closing = 0;
117 peer->ksnp_accepting = 0;
118 peer->ksnp_proto = NULL;
119 peer->ksnp_last_alive = 0;
120 peer->ksnp_zc_next_cookie = SOCKNAL_KEEPALIVE_PING + 1;
122 INIT_LIST_HEAD(&peer->ksnp_conns);
123 INIT_LIST_HEAD(&peer->ksnp_routes);
124 INIT_LIST_HEAD(&peer->ksnp_tx_queue);
125 INIT_LIST_HEAD(&peer->ksnp_zc_req_list);
126 spin_lock_init(&peer->ksnp_lock);
128 spin_lock_bh(&net->ksnn_lock);
130 if (net->ksnn_shutdown) {
131 spin_unlock_bh(&net->ksnn_lock);
133 LIBCFS_FREE(peer, sizeof(*peer));
134 CERROR("Can't create peer: network shutdown\n");
140 spin_unlock_bh(&net->ksnn_lock);
147 ksocknal_destroy_peer(struct ksock_peer *peer)
149 struct ksock_net *net = peer->ksnp_ni->ni_data;
151 CDEBUG(D_NET, "peer %s %p deleted\n",
152 libcfs_id2str(peer->ksnp_id), peer);
154 LASSERT(!atomic_read(&peer->ksnp_refcount));
155 LASSERT(!peer->ksnp_accepting);
156 LASSERT(list_empty(&peer->ksnp_conns));
157 LASSERT(list_empty(&peer->ksnp_routes));
158 LASSERT(list_empty(&peer->ksnp_tx_queue));
159 LASSERT(list_empty(&peer->ksnp_zc_req_list));
161 LIBCFS_FREE(peer, sizeof(*peer));
164 * NB a peer's connections and routes keep a reference on their peer
165 * until they are destroyed, so we can be assured that _all_ state to
166 * do with this peer has been cleaned up when its refcount drops to
169 spin_lock_bh(&net->ksnn_lock);
171 spin_unlock_bh(&net->ksnn_lock);
175 ksocknal_find_peer_locked(lnet_ni_t *ni, lnet_process_id_t id)
177 struct list_head *peer_list = ksocknal_nid2peerlist(id.nid);
178 struct list_head *tmp;
179 struct ksock_peer *peer;
181 list_for_each(tmp, peer_list) {
182 peer = list_entry(tmp, struct ksock_peer, ksnp_list);
184 LASSERT(!peer->ksnp_closing);
186 if (peer->ksnp_ni != ni)
189 if (peer->ksnp_id.nid != id.nid ||
190 peer->ksnp_id.pid != id.pid)
193 CDEBUG(D_NET, "got peer [%p] -> %s (%d)\n",
194 peer, libcfs_id2str(id),
195 atomic_read(&peer->ksnp_refcount));
202 ksocknal_find_peer(lnet_ni_t *ni, lnet_process_id_t id)
204 struct ksock_peer *peer;
206 read_lock(&ksocknal_data.ksnd_global_lock);
207 peer = ksocknal_find_peer_locked(ni, id);
208 if (peer) /* +1 ref for caller? */
209 ksocknal_peer_addref(peer);
210 read_unlock(&ksocknal_data.ksnd_global_lock);
216 ksocknal_unlink_peer_locked(struct ksock_peer *peer)
220 struct ksock_interface *iface;
222 for (i = 0; i < peer->ksnp_n_passive_ips; i++) {
223 LASSERT(i < LNET_MAX_INTERFACES);
224 ip = peer->ksnp_passive_ips[i];
226 iface = ksocknal_ip2iface(peer->ksnp_ni, ip);
228 * All IPs in peer->ksnp_passive_ips[] come from the
229 * interface list, therefore the call must succeed.
233 CDEBUG(D_NET, "peer=%p iface=%p ksni_nroutes=%d\n",
234 peer, iface, iface->ksni_nroutes);
235 iface->ksni_npeers--;
238 LASSERT(list_empty(&peer->ksnp_conns));
239 LASSERT(list_empty(&peer->ksnp_routes));
240 LASSERT(!peer->ksnp_closing);
241 peer->ksnp_closing = 1;
242 list_del(&peer->ksnp_list);
243 /* lose peerlist's ref */
244 ksocknal_peer_decref(peer);
248 ksocknal_get_peer_info(lnet_ni_t *ni, int index,
249 lnet_process_id_t *id, __u32 *myip, __u32 *peer_ip,
250 int *port, int *conn_count, int *share_count)
252 struct ksock_peer *peer;
253 struct list_head *ptmp;
254 struct ksock_route *route;
255 struct list_head *rtmp;
260 read_lock(&ksocknal_data.ksnd_global_lock);
262 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
263 list_for_each(ptmp, &ksocknal_data.ksnd_peers[i]) {
264 peer = list_entry(ptmp, struct ksock_peer, ksnp_list);
266 if (peer->ksnp_ni != ni)
269 if (!peer->ksnp_n_passive_ips &&
270 list_empty(&peer->ksnp_routes)) {
284 for (j = 0; j < peer->ksnp_n_passive_ips; j++) {
289 *myip = peer->ksnp_passive_ips[j];
298 list_for_each(rtmp, &peer->ksnp_routes) {
302 route = list_entry(rtmp, struct ksock_route,
306 *myip = route->ksnr_myipaddr;
307 *peer_ip = route->ksnr_ipaddr;
308 *port = route->ksnr_port;
309 *conn_count = route->ksnr_conn_count;
310 *share_count = route->ksnr_share_count;
317 read_unlock(&ksocknal_data.ksnd_global_lock);
322 ksocknal_associate_route_conn_locked(struct ksock_route *route, struct ksock_conn *conn)
324 struct ksock_peer *peer = route->ksnr_peer;
325 int type = conn->ksnc_type;
326 struct ksock_interface *iface;
328 conn->ksnc_route = route;
329 ksocknal_route_addref(route);
331 if (route->ksnr_myipaddr != conn->ksnc_myipaddr) {
332 if (!route->ksnr_myipaddr) {
333 /* route wasn't bound locally yet (the initial route) */
334 CDEBUG(D_NET, "Binding %s %pI4h to %pI4h\n",
335 libcfs_id2str(peer->ksnp_id),
337 &conn->ksnc_myipaddr);
339 CDEBUG(D_NET, "Rebinding %s %pI4h from %pI4h to %pI4h\n",
340 libcfs_id2str(peer->ksnp_id),
342 &route->ksnr_myipaddr,
343 &conn->ksnc_myipaddr);
345 iface = ksocknal_ip2iface(route->ksnr_peer->ksnp_ni,
346 route->ksnr_myipaddr);
348 iface->ksni_nroutes--;
350 route->ksnr_myipaddr = conn->ksnc_myipaddr;
351 iface = ksocknal_ip2iface(route->ksnr_peer->ksnp_ni,
352 route->ksnr_myipaddr);
354 iface->ksni_nroutes++;
357 route->ksnr_connected |= (1 << type);
358 route->ksnr_conn_count++;
361 * Successful connection => further attempts can
362 * proceed immediately
364 route->ksnr_retry_interval = 0;
368 ksocknal_add_route_locked(struct ksock_peer *peer, struct ksock_route *route)
370 struct list_head *tmp;
371 struct ksock_conn *conn;
372 struct ksock_route *route2;
374 LASSERT(!peer->ksnp_closing);
375 LASSERT(!route->ksnr_peer);
376 LASSERT(!route->ksnr_scheduled);
377 LASSERT(!route->ksnr_connecting);
378 LASSERT(!route->ksnr_connected);
380 /* LASSERT(unique) */
381 list_for_each(tmp, &peer->ksnp_routes) {
382 route2 = list_entry(tmp, struct ksock_route, ksnr_list);
384 if (route2->ksnr_ipaddr == route->ksnr_ipaddr) {
385 CERROR("Duplicate route %s %pI4h\n",
386 libcfs_id2str(peer->ksnp_id),
387 &route->ksnr_ipaddr);
392 route->ksnr_peer = peer;
393 ksocknal_peer_addref(peer);
394 /* peer's routelist takes over my ref on 'route' */
395 list_add_tail(&route->ksnr_list, &peer->ksnp_routes);
397 list_for_each(tmp, &peer->ksnp_conns) {
398 conn = list_entry(tmp, struct ksock_conn, ksnc_list);
400 if (conn->ksnc_ipaddr != route->ksnr_ipaddr)
403 ksocknal_associate_route_conn_locked(route, conn);
404 /* keep going (typed routes) */
409 ksocknal_del_route_locked(struct ksock_route *route)
411 struct ksock_peer *peer = route->ksnr_peer;
412 struct ksock_interface *iface;
413 struct ksock_conn *conn;
414 struct list_head *ctmp;
415 struct list_head *cnxt;
417 LASSERT(!route->ksnr_deleted);
419 /* Close associated conns */
420 list_for_each_safe(ctmp, cnxt, &peer->ksnp_conns) {
421 conn = list_entry(ctmp, struct ksock_conn, ksnc_list);
423 if (conn->ksnc_route != route)
426 ksocknal_close_conn_locked(conn, 0);
429 if (route->ksnr_myipaddr) {
430 iface = ksocknal_ip2iface(route->ksnr_peer->ksnp_ni,
431 route->ksnr_myipaddr);
433 iface->ksni_nroutes--;
436 route->ksnr_deleted = 1;
437 list_del(&route->ksnr_list);
438 ksocknal_route_decref(route); /* drop peer's ref */
440 if (list_empty(&peer->ksnp_routes) &&
441 list_empty(&peer->ksnp_conns)) {
443 * 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)
453 struct list_head *tmp;
454 struct ksock_peer *peer;
455 struct ksock_peer *peer2;
456 struct ksock_route *route;
457 struct ksock_route *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 write_lock_bh(&ksocknal_data.ksnd_global_lock);
477 /* always called with a ref on ni, so shutdown can't have started */
478 LASSERT(!((struct ksock_net *)ni->ni_data)->ksnn_shutdown);
480 peer2 = ksocknal_find_peer_locked(ni, id);
482 ksocknal_peer_decref(peer);
485 /* peer table takes my ref on peer */
486 list_add_tail(&peer->ksnp_list,
487 ksocknal_nid2peerlist(id.nid));
491 list_for_each(tmp, &peer->ksnp_routes) {
492 route2 = list_entry(tmp, struct ksock_route, ksnr_list);
494 if (route2->ksnr_ipaddr == ipaddr)
500 ksocknal_add_route_locked(peer, route);
501 route->ksnr_share_count++;
503 ksocknal_route_decref(route);
504 route2->ksnr_share_count++;
507 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
513 ksocknal_del_peer_locked(struct ksock_peer *peer, __u32 ip)
515 struct ksock_conn *conn;
516 struct ksock_route *route;
517 struct list_head *tmp;
518 struct list_head *nxt;
521 LASSERT(!peer->ksnp_closing);
523 /* Extra ref prevents peer disappearing until I'm done with it */
524 ksocknal_peer_addref(peer);
526 list_for_each_safe(tmp, nxt, &peer->ksnp_routes) {
527 route = list_entry(tmp, struct ksock_route, ksnr_list);
530 if (!(!ip || route->ksnr_ipaddr == ip))
533 route->ksnr_share_count = 0;
534 /* This deletes associated conns too */
535 ksocknal_del_route_locked(route);
539 list_for_each_safe(tmp, nxt, &peer->ksnp_routes) {
540 route = list_entry(tmp, struct ksock_route, ksnr_list);
541 nshared += route->ksnr_share_count;
546 * remove everything else if there are no explicit entries
549 list_for_each_safe(tmp, nxt, &peer->ksnp_routes) {
550 route = list_entry(tmp, struct ksock_route, ksnr_list);
552 /* we should only be removing auto-entries */
553 LASSERT(!route->ksnr_share_count);
554 ksocknal_del_route_locked(route);
557 list_for_each_safe(tmp, nxt, &peer->ksnp_conns) {
558 conn = list_entry(tmp, struct ksock_conn, ksnc_list);
560 ksocknal_close_conn_locked(conn, 0);
564 ksocknal_peer_decref(peer);
565 /* NB peer unlinks itself when last conn/route is removed */
569 ksocknal_del_peer(lnet_ni_t *ni, lnet_process_id_t id, __u32 ip)
572 struct list_head *ptmp;
573 struct list_head *pnxt;
574 struct ksock_peer *peer;
580 write_lock_bh(&ksocknal_data.ksnd_global_lock);
582 if (id.nid != LNET_NID_ANY) {
583 lo = (int)(ksocknal_nid2peerlist(id.nid) - ksocknal_data.ksnd_peers);
584 hi = (int)(ksocknal_nid2peerlist(id.nid) - ksocknal_data.ksnd_peers);
587 hi = ksocknal_data.ksnd_peer_hash_size - 1;
590 for (i = lo; i <= hi; i++) {
591 list_for_each_safe(ptmp, pnxt, &ksocknal_data.ksnd_peers[i]) {
592 peer = list_entry(ptmp, struct ksock_peer, ksnp_list);
594 if (peer->ksnp_ni != ni)
597 if (!((id.nid == LNET_NID_ANY || peer->ksnp_id.nid == id.nid) &&
598 (id.pid == LNET_PID_ANY || 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 struct ksock_conn *
628 ksocknal_get_conn_by_idx(lnet_ni_t *ni, int index)
630 struct ksock_peer *peer;
631 struct list_head *ptmp;
632 struct ksock_conn *conn;
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, struct ksock_peer, 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, struct ksock_conn,
653 ksocknal_conn_addref(conn);
654 read_unlock(&ksocknal_data.ksnd_global_lock);
660 read_unlock(&ksocknal_data.ksnd_global_lock);
664 static struct ksock_sched *
665 ksocknal_choose_scheduler_locked(unsigned int cpt)
667 struct ksock_sched_info *info = ksocknal_data.ksnd_sched_info[cpt];
668 struct ksock_sched *sched;
671 LASSERT(info->ksi_nthreads > 0);
673 sched = &info->ksi_scheds[0];
675 * NB: it's safe so far, but info->ksi_nthreads could be changed
676 * at runtime when we have dynamic LNet configuration, then we
677 * need to take care of this.
679 for (i = 1; i < info->ksi_nthreads; i++) {
680 if (sched->kss_nconns > info->ksi_scheds[i].kss_nconns)
681 sched = &info->ksi_scheds[i];
688 ksocknal_local_ipvec(lnet_ni_t *ni, __u32 *ipaddrs)
690 struct ksock_net *net = ni->ni_data;
694 read_lock(&ksocknal_data.ksnd_global_lock);
696 nip = net->ksnn_ninterfaces;
697 LASSERT(nip <= LNET_MAX_INTERFACES);
700 * Only offer interfaces for additional connections if I have
704 read_unlock(&ksocknal_data.ksnd_global_lock);
708 for (i = 0; i < nip; i++) {
709 ipaddrs[i] = net->ksnn_interfaces[i].ksni_ipaddr;
713 read_unlock(&ksocknal_data.ksnd_global_lock);
718 ksocknal_match_peerip(struct ksock_interface *iface, __u32 *ips, int nips)
720 int best_netmatch = 0;
727 for (i = 0; i < nips; i++) {
731 this_xor = ips[i] ^ iface->ksni_ipaddr;
732 this_netmatch = !(this_xor & iface->ksni_netmask) ? 1 : 0;
735 best_netmatch < this_netmatch ||
736 (best_netmatch == this_netmatch &&
737 best_xor > this_xor)))
741 best_netmatch = this_netmatch;
750 ksocknal_select_ips(struct ksock_peer *peer, __u32 *peerips, int n_peerips)
752 rwlock_t *global_lock = &ksocknal_data.ksnd_global_lock;
753 struct ksock_net *net = peer->ksnp_ni->ni_data;
754 struct ksock_interface *iface;
755 struct ksock_interface *best_iface;
767 * CAVEAT EMPTOR: We do all our interface matching with an
768 * exclusive hold of global lock at IRQ priority. We're only
769 * expecting to be dealing with small numbers of interfaces, so the
770 * O(n**3)-ness shouldn't matter
773 * Also note that I'm not going to return more than n_peerips
774 * interfaces, even if I have more myself
776 write_lock_bh(global_lock);
778 LASSERT(n_peerips <= LNET_MAX_INTERFACES);
779 LASSERT(net->ksnn_ninterfaces <= LNET_MAX_INTERFACES);
782 * Only match interfaces for additional connections
783 * if I have > 1 interface
785 n_ips = (net->ksnn_ninterfaces < 2) ? 0 :
786 min(n_peerips, net->ksnn_ninterfaces);
788 for (i = 0; peer->ksnp_n_passive_ips < n_ips; i++) {
789 /* ^ yes really... */
792 * If we have any new interfaces, first tick off all the
793 * peer IPs that match old interfaces, then choose new
794 * interfaces to match the remaining peer IPS.
795 * We don't forget interfaces we've stopped using; we might
796 * start using them again...
798 if (i < peer->ksnp_n_passive_ips) {
800 ip = peer->ksnp_passive_ips[i];
801 best_iface = ksocknal_ip2iface(peer->ksnp_ni, ip);
803 /* peer passive ips are kept up to date */
806 /* choose a new interface */
807 LASSERT(i == peer->ksnp_n_passive_ips);
813 for (j = 0; j < net->ksnn_ninterfaces; j++) {
814 iface = &net->ksnn_interfaces[j];
815 ip = iface->ksni_ipaddr;
817 for (k = 0; k < peer->ksnp_n_passive_ips; k++)
818 if (peer->ksnp_passive_ips[k] == ip)
821 if (k < peer->ksnp_n_passive_ips) /* using it already */
824 k = ksocknal_match_peerip(iface, peerips, n_peerips);
825 xor = ip ^ peerips[k];
826 this_netmatch = !(xor & iface->ksni_netmask) ? 1 : 0;
829 best_netmatch < this_netmatch ||
830 (best_netmatch == this_netmatch &&
831 best_npeers > iface->ksni_npeers)))
835 best_netmatch = this_netmatch;
836 best_npeers = iface->ksni_npeers;
841 best_iface->ksni_npeers++;
842 ip = best_iface->ksni_ipaddr;
843 peer->ksnp_passive_ips[i] = ip;
844 peer->ksnp_n_passive_ips = i + 1;
847 /* mark the best matching peer IP used */
848 j = ksocknal_match_peerip(best_iface, peerips, n_peerips);
852 /* Overwrite input peer IP addresses */
853 memcpy(peerips, peer->ksnp_passive_ips, n_ips * sizeof(*peerips));
855 write_unlock_bh(global_lock);
861 ksocknal_create_routes(struct ksock_peer *peer, int port,
862 __u32 *peer_ipaddrs, int npeer_ipaddrs)
864 struct ksock_route *newroute = NULL;
865 rwlock_t *global_lock = &ksocknal_data.ksnd_global_lock;
866 lnet_ni_t *ni = peer->ksnp_ni;
867 struct ksock_net *net = ni->ni_data;
868 struct list_head *rtmp;
869 struct ksock_route *route;
870 struct ksock_interface *iface;
871 struct ksock_interface *best_iface;
879 * CAVEAT EMPTOR: We do all our interface matching with an
880 * exclusive hold of global lock at IRQ priority. We're only
881 * expecting to be dealing with small numbers of interfaces, so the
882 * O(n**3)-ness here shouldn't matter
884 write_lock_bh(global_lock);
886 if (net->ksnn_ninterfaces < 2) {
888 * Only create additional connections
889 * if I have > 1 interface
891 write_unlock_bh(global_lock);
895 LASSERT(npeer_ipaddrs <= LNET_MAX_INTERFACES);
897 for (i = 0; i < npeer_ipaddrs; i++) {
899 newroute->ksnr_ipaddr = peer_ipaddrs[i];
901 write_unlock_bh(global_lock);
903 newroute = ksocknal_create_route(peer_ipaddrs[i], port);
907 write_lock_bh(global_lock);
910 if (peer->ksnp_closing) {
911 /* peer got closed under me */
915 /* Already got a route? */
917 list_for_each(rtmp, &peer->ksnp_routes) {
918 route = list_entry(rtmp, struct ksock_route, ksnr_list);
920 if (route->ksnr_ipaddr == newroute->ksnr_ipaddr)
932 LASSERT(net->ksnn_ninterfaces <= LNET_MAX_INTERFACES);
934 /* Select interface to connect from */
935 for (j = 0; j < net->ksnn_ninterfaces; j++) {
936 iface = &net->ksnn_interfaces[j];
938 /* Using this interface already? */
939 list_for_each(rtmp, &peer->ksnp_routes) {
940 route = list_entry(rtmp, struct ksock_route,
943 if (route->ksnr_myipaddr == iface->ksni_ipaddr)
951 this_netmatch = (!((iface->ksni_ipaddr ^
952 newroute->ksnr_ipaddr) &
953 iface->ksni_netmask)) ? 1 : 0;
956 best_netmatch < this_netmatch ||
957 (best_netmatch == this_netmatch &&
958 best_nroutes > iface->ksni_nroutes)))
962 best_netmatch = this_netmatch;
963 best_nroutes = iface->ksni_nroutes;
969 newroute->ksnr_myipaddr = best_iface->ksni_ipaddr;
970 best_iface->ksni_nroutes++;
972 ksocknal_add_route_locked(peer, newroute);
976 write_unlock_bh(global_lock);
978 ksocknal_route_decref(newroute);
982 ksocknal_accept(lnet_ni_t *ni, struct socket *sock)
984 struct ksock_connreq *cr;
989 rc = lnet_sock_getaddr(sock, 1, &peer_ip, &peer_port);
990 LASSERT(!rc); /* we succeeded before */
992 LIBCFS_ALLOC(cr, sizeof(*cr));
994 LCONSOLE_ERROR_MSG(0x12f, "Dropping connection request from %pI4h: memory exhausted\n",
1001 cr->ksncr_sock = sock;
1003 spin_lock_bh(&ksocknal_data.ksnd_connd_lock);
1005 list_add_tail(&cr->ksncr_list, &ksocknal_data.ksnd_connd_connreqs);
1006 wake_up(&ksocknal_data.ksnd_connd_waitq);
1008 spin_unlock_bh(&ksocknal_data.ksnd_connd_lock);
1013 ksocknal_connecting(struct ksock_peer *peer, __u32 ipaddr)
1015 struct ksock_route *route;
1017 list_for_each_entry(route, &peer->ksnp_routes, ksnr_list) {
1018 if (route->ksnr_ipaddr == ipaddr)
1019 return route->ksnr_connecting;
1025 ksocknal_create_conn(lnet_ni_t *ni, struct ksock_route *route,
1026 struct socket *sock, int type)
1028 rwlock_t *global_lock = &ksocknal_data.ksnd_global_lock;
1030 lnet_process_id_t peerid;
1031 struct list_head *tmp;
1033 struct ksock_conn *conn;
1034 struct ksock_conn *conn2;
1035 struct ksock_peer *peer = NULL;
1036 struct ksock_peer *peer2;
1037 struct ksock_sched *sched;
1038 ksock_hello_msg_t *hello;
1040 struct ksock_tx *tx;
1041 struct ksock_tx *txtmp;
1048 LASSERT(active == (type != SOCKLND_CONN_NONE));
1050 LIBCFS_ALLOC(conn, sizeof(*conn));
1056 conn->ksnc_peer = NULL;
1057 conn->ksnc_route = NULL;
1058 conn->ksnc_sock = sock;
1060 * 2 ref, 1 for conn, another extra ref prevents socket
1061 * being closed before establishment of connection
1063 atomic_set(&conn->ksnc_sock_refcount, 2);
1064 conn->ksnc_type = type;
1065 ksocknal_lib_save_callback(sock, conn);
1066 atomic_set(&conn->ksnc_conn_refcount, 1); /* 1 ref for me */
1068 conn->ksnc_rx_ready = 0;
1069 conn->ksnc_rx_scheduled = 0;
1071 INIT_LIST_HEAD(&conn->ksnc_tx_queue);
1072 conn->ksnc_tx_ready = 0;
1073 conn->ksnc_tx_scheduled = 0;
1074 conn->ksnc_tx_carrier = NULL;
1075 atomic_set(&conn->ksnc_tx_nob, 0);
1077 LIBCFS_ALLOC(hello, offsetof(ksock_hello_msg_t,
1078 kshm_ips[LNET_MAX_INTERFACES]));
1084 /* stash conn's local and remote addrs */
1085 rc = ksocknal_lib_get_conn_addrs(conn);
1090 * Find out/confirm peer's NID and connection type and get the
1091 * vector of interfaces she's willing to let me connect to.
1092 * Passive connections use the listener timeout since the peer sends
1096 peer = route->ksnr_peer;
1097 LASSERT(ni == peer->ksnp_ni);
1099 /* Active connection sends HELLO eagerly */
1100 hello->kshm_nips = ksocknal_local_ipvec(ni, hello->kshm_ips);
1101 peerid = peer->ksnp_id;
1103 write_lock_bh(global_lock);
1104 conn->ksnc_proto = peer->ksnp_proto;
1105 write_unlock_bh(global_lock);
1107 if (!conn->ksnc_proto) {
1108 conn->ksnc_proto = &ksocknal_protocol_v3x;
1109 #if SOCKNAL_VERSION_DEBUG
1110 if (*ksocknal_tunables.ksnd_protocol == 2)
1111 conn->ksnc_proto = &ksocknal_protocol_v2x;
1112 else if (*ksocknal_tunables.ksnd_protocol == 1)
1113 conn->ksnc_proto = &ksocknal_protocol_v1x;
1117 rc = ksocknal_send_hello(ni, conn, peerid.nid, hello);
1121 peerid.nid = LNET_NID_ANY;
1122 peerid.pid = LNET_PID_ANY;
1124 /* Passive, get protocol from peer */
1125 conn->ksnc_proto = NULL;
1128 rc = ksocknal_recv_hello(ni, conn, hello, &peerid, &incarnation);
1132 LASSERT(!rc || active);
1133 LASSERT(conn->ksnc_proto);
1134 LASSERT(peerid.nid != LNET_NID_ANY);
1136 cpt = lnet_cpt_of_nid(peerid.nid);
1139 ksocknal_peer_addref(peer);
1140 write_lock_bh(global_lock);
1142 rc = ksocknal_create_peer(&peer, ni, peerid);
1146 write_lock_bh(global_lock);
1148 /* called with a ref on ni, so shutdown can't have started */
1149 LASSERT(!((struct ksock_net *)ni->ni_data)->ksnn_shutdown);
1151 peer2 = ksocknal_find_peer_locked(ni, peerid);
1154 * NB this puts an "empty" peer in the peer
1155 * table (which takes my ref)
1157 list_add_tail(&peer->ksnp_list,
1158 ksocknal_nid2peerlist(peerid.nid));
1160 ksocknal_peer_decref(peer);
1165 ksocknal_peer_addref(peer);
1166 peer->ksnp_accepting++;
1169 * Am I already connecting to this guy? Resolve in
1170 * favour of higher NID...
1172 if (peerid.nid < ni->ni_nid &&
1173 ksocknal_connecting(peer, conn->ksnc_ipaddr)) {
1175 warn = "connection race resolution";
1180 if (peer->ksnp_closing ||
1181 (active && route->ksnr_deleted)) {
1182 /* peer/route got closed under me */
1184 warn = "peer/route removed";
1188 if (!peer->ksnp_proto) {
1190 * Never connected before.
1191 * NB recv_hello may have returned EPROTO to signal my peer
1192 * wants a different protocol than the one I asked for.
1194 LASSERT(list_empty(&peer->ksnp_conns));
1196 peer->ksnp_proto = conn->ksnc_proto;
1197 peer->ksnp_incarnation = incarnation;
1200 if (peer->ksnp_proto != conn->ksnc_proto ||
1201 peer->ksnp_incarnation != incarnation) {
1202 /* Peer rebooted or I've got the wrong protocol version */
1203 ksocknal_close_peer_conns_locked(peer, 0, 0);
1205 peer->ksnp_proto = NULL;
1207 warn = peer->ksnp_incarnation != incarnation ?
1209 "wrong proto version";
1219 warn = "lost conn race";
1222 warn = "retry with different protocol version";
1227 * Refuse to duplicate an existing connection, unless this is a
1228 * loopback connection
1230 if (conn->ksnc_ipaddr != conn->ksnc_myipaddr) {
1231 list_for_each(tmp, &peer->ksnp_conns) {
1232 conn2 = list_entry(tmp, struct ksock_conn, ksnc_list);
1234 if (conn2->ksnc_ipaddr != conn->ksnc_ipaddr ||
1235 conn2->ksnc_myipaddr != conn->ksnc_myipaddr ||
1236 conn2->ksnc_type != conn->ksnc_type)
1240 * Reply on a passive connection attempt so the peer
1241 * realises we're connected.
1253 * If the connection created by this route didn't bind to the IP
1254 * address the route connected to, the connection/route matching
1255 * code below probably isn't going to work.
1258 route->ksnr_ipaddr != conn->ksnc_ipaddr) {
1259 CERROR("Route %s %pI4h connected to %pI4h\n",
1260 libcfs_id2str(peer->ksnp_id),
1261 &route->ksnr_ipaddr,
1262 &conn->ksnc_ipaddr);
1266 * Search for a route corresponding to the new connection and
1267 * create an association. This allows incoming connections created
1268 * by routes in my peer to match my own route entries so I don't
1269 * continually create duplicate routes.
1271 list_for_each(tmp, &peer->ksnp_routes) {
1272 route = list_entry(tmp, struct ksock_route, ksnr_list);
1274 if (route->ksnr_ipaddr != conn->ksnc_ipaddr)
1277 ksocknal_associate_route_conn_locked(route, conn);
1281 conn->ksnc_peer = peer; /* conn takes my ref on peer */
1282 peer->ksnp_last_alive = cfs_time_current();
1283 peer->ksnp_send_keepalive = 0;
1284 peer->ksnp_error = 0;
1286 sched = ksocknal_choose_scheduler_locked(cpt);
1287 sched->kss_nconns++;
1288 conn->ksnc_scheduler = sched;
1290 conn->ksnc_tx_last_post = cfs_time_current();
1291 /* Set the deadline for the outgoing HELLO to drain */
1292 conn->ksnc_tx_bufnob = sock->sk->sk_wmem_queued;
1293 conn->ksnc_tx_deadline = cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
1294 mb(); /* order with adding to peer's conn list */
1296 list_add(&conn->ksnc_list, &peer->ksnp_conns);
1297 ksocknal_conn_addref(conn);
1299 ksocknal_new_packet(conn, 0);
1301 conn->ksnc_zc_capable = ksocknal_lib_zc_capable(conn);
1303 /* Take packets blocking for this connection. */
1304 list_for_each_entry_safe(tx, txtmp, &peer->ksnp_tx_queue, tx_list) {
1305 if (conn->ksnc_proto->pro_match_tx(conn, tx, tx->tx_nonblk) == SOCKNAL_MATCH_NO)
1308 list_del(&tx->tx_list);
1309 ksocknal_queue_tx_locked(tx, conn);
1312 write_unlock_bh(global_lock);
1315 * We've now got a new connection. Any errors from here on are just
1316 * like "normal" comms errors and we close the connection normally.
1317 * NB (a) we still have to send the reply HELLO for passive
1319 * (b) normal I/O on the conn is blocked until I setup and call the
1322 CDEBUG(D_NET, "New conn %s p %d.x %pI4h -> %pI4h/%d incarnation:%lld sched[%d:%d]\n",
1323 libcfs_id2str(peerid), conn->ksnc_proto->pro_version,
1324 &conn->ksnc_myipaddr, &conn->ksnc_ipaddr,
1325 conn->ksnc_port, incarnation, cpt,
1326 (int)(sched - &sched->kss_info->ksi_scheds[0]));
1329 /* additional routes after interface exchange? */
1330 ksocknal_create_routes(peer, conn->ksnc_port,
1331 hello->kshm_ips, hello->kshm_nips);
1333 hello->kshm_nips = ksocknal_select_ips(peer, hello->kshm_ips,
1335 rc = ksocknal_send_hello(ni, conn, peerid.nid, hello);
1338 LIBCFS_FREE(hello, offsetof(ksock_hello_msg_t,
1339 kshm_ips[LNET_MAX_INTERFACES]));
1342 * setup the socket AFTER I've received hello (it disables
1343 * SO_LINGER). I might call back to the acceptor who may want
1344 * to send a protocol version response and then close the
1345 * socket; this ensures the socket only tears down after the
1346 * response has been sent.
1349 rc = ksocknal_lib_setup_sock(sock);
1351 write_lock_bh(global_lock);
1353 /* NB my callbacks block while I hold ksnd_global_lock */
1354 ksocknal_lib_set_callback(sock, conn);
1357 peer->ksnp_accepting--;
1359 write_unlock_bh(global_lock);
1362 write_lock_bh(global_lock);
1363 if (!conn->ksnc_closing) {
1364 /* could be closed by another thread */
1365 ksocknal_close_conn_locked(conn, rc);
1367 write_unlock_bh(global_lock);
1368 } else if (!ksocknal_connsock_addref(conn)) {
1369 /* Allow I/O to proceed. */
1370 ksocknal_read_callback(conn);
1371 ksocknal_write_callback(conn);
1372 ksocknal_connsock_decref(conn);
1375 ksocknal_connsock_decref(conn);
1376 ksocknal_conn_decref(conn);
1380 if (!peer->ksnp_closing &&
1381 list_empty(&peer->ksnp_conns) &&
1382 list_empty(&peer->ksnp_routes)) {
1383 list_add(&zombies, &peer->ksnp_tx_queue);
1384 list_del_init(&peer->ksnp_tx_queue);
1385 ksocknal_unlink_peer_locked(peer);
1388 write_unlock_bh(global_lock);
1392 CERROR("Not creating conn %s type %d: %s\n",
1393 libcfs_id2str(peerid), conn->ksnc_type, warn);
1395 CDEBUG(D_NET, "Not creating conn %s type %d: %s\n",
1396 libcfs_id2str(peerid), conn->ksnc_type, warn);
1402 * Request retry by replying with CONN_NONE
1403 * ksnc_proto has been set already
1405 conn->ksnc_type = SOCKLND_CONN_NONE;
1406 hello->kshm_nips = 0;
1407 ksocknal_send_hello(ni, conn, peerid.nid, hello);
1410 write_lock_bh(global_lock);
1411 peer->ksnp_accepting--;
1412 write_unlock_bh(global_lock);
1415 ksocknal_txlist_done(ni, &zombies, 1);
1416 ksocknal_peer_decref(peer);
1420 LIBCFS_FREE(hello, offsetof(ksock_hello_msg_t,
1421 kshm_ips[LNET_MAX_INTERFACES]));
1423 LIBCFS_FREE(conn, sizeof(*conn));
1431 ksocknal_close_conn_locked(struct ksock_conn *conn, int error)
1434 * This just does the immmediate housekeeping, and queues the
1435 * connection for the reaper to terminate.
1436 * Caller holds ksnd_global_lock exclusively in irq context
1438 struct ksock_peer *peer = conn->ksnc_peer;
1439 struct ksock_route *route;
1440 struct ksock_conn *conn2;
1441 struct list_head *tmp;
1443 LASSERT(!peer->ksnp_error);
1444 LASSERT(!conn->ksnc_closing);
1445 conn->ksnc_closing = 1;
1447 /* ksnd_deathrow_conns takes over peer's ref */
1448 list_del(&conn->ksnc_list);
1450 route = conn->ksnc_route;
1452 /* dissociate conn from route... */
1453 LASSERT(!route->ksnr_deleted);
1454 LASSERT(route->ksnr_connected & (1 << conn->ksnc_type));
1457 list_for_each(tmp, &peer->ksnp_conns) {
1458 conn2 = list_entry(tmp, struct ksock_conn, ksnc_list);
1460 if (conn2->ksnc_route == route &&
1461 conn2->ksnc_type == conn->ksnc_type)
1467 route->ksnr_connected &= ~(1 << conn->ksnc_type);
1469 conn->ksnc_route = NULL;
1471 #if 0 /* irrelevant with only eager routes */
1472 /* make route least favourite */
1473 list_del(&route->ksnr_list);
1474 list_add_tail(&route->ksnr_list, &peer->ksnp_routes);
1476 ksocknal_route_decref(route); /* drop conn's ref on route */
1479 if (list_empty(&peer->ksnp_conns)) {
1480 /* No more connections to this peer */
1482 if (!list_empty(&peer->ksnp_tx_queue)) {
1483 struct ksock_tx *tx;
1485 LASSERT(conn->ksnc_proto == &ksocknal_protocol_v3x);
1488 * throw them to the last connection...,
1489 * these TXs will be send to /dev/null by scheduler
1491 list_for_each_entry(tx, &peer->ksnp_tx_queue,
1493 ksocknal_tx_prep(conn, tx);
1495 spin_lock_bh(&conn->ksnc_scheduler->kss_lock);
1496 list_splice_init(&peer->ksnp_tx_queue,
1497 &conn->ksnc_tx_queue);
1498 spin_unlock_bh(&conn->ksnc_scheduler->kss_lock);
1501 peer->ksnp_proto = NULL; /* renegotiate protocol version */
1502 peer->ksnp_error = error; /* stash last conn close reason */
1504 if (list_empty(&peer->ksnp_routes)) {
1506 * I've just closed last conn belonging to a
1507 * peer with no routes to it
1509 ksocknal_unlink_peer_locked(peer);
1513 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
1515 list_add_tail(&conn->ksnc_list,
1516 &ksocknal_data.ksnd_deathrow_conns);
1517 wake_up(&ksocknal_data.ksnd_reaper_waitq);
1519 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
1523 ksocknal_peer_failed(struct ksock_peer *peer)
1526 unsigned long last_alive = 0;
1529 * There has been a connection failure or comms error; but I'll only
1530 * tell LNET I think the peer is dead if it's to another kernel and
1531 * there are no connections or connection attempts in existence.
1533 read_lock(&ksocknal_data.ksnd_global_lock);
1535 if (!(peer->ksnp_id.pid & LNET_PID_USERFLAG) &&
1536 list_empty(&peer->ksnp_conns) &&
1537 !peer->ksnp_accepting &&
1538 !ksocknal_find_connecting_route_locked(peer)) {
1540 last_alive = peer->ksnp_last_alive;
1543 read_unlock(&ksocknal_data.ksnd_global_lock);
1546 lnet_notify(peer->ksnp_ni, peer->ksnp_id.nid, 0,
1551 ksocknal_finalize_zcreq(struct ksock_conn *conn)
1553 struct ksock_peer *peer = conn->ksnc_peer;
1554 struct ksock_tx *tx;
1555 struct ksock_tx *temp;
1556 struct ksock_tx *tmp;
1560 * NB safe to finalize TXs because closing of socket will
1561 * abort all buffered data
1563 LASSERT(!conn->ksnc_sock);
1565 spin_lock(&peer->ksnp_lock);
1567 list_for_each_entry_safe(tx, tmp, &peer->ksnp_zc_req_list, tx_zc_list) {
1568 if (tx->tx_conn != conn)
1571 LASSERT(tx->tx_msg.ksm_zc_cookies[0]);
1573 tx->tx_msg.ksm_zc_cookies[0] = 0;
1574 tx->tx_zc_aborted = 1; /* mark it as not-acked */
1575 list_del(&tx->tx_zc_list);
1576 list_add(&tx->tx_zc_list, &zlist);
1579 spin_unlock(&peer->ksnp_lock);
1581 list_for_each_entry_safe(tx, temp, &zlist, tx_zc_list) {
1582 list_del(&tx->tx_zc_list);
1583 ksocknal_tx_decref(tx);
1588 ksocknal_terminate_conn(struct ksock_conn *conn)
1591 * This gets called by the reaper (guaranteed thread context) to
1592 * disengage the socket from its callbacks and close it.
1593 * ksnc_refcount will eventually hit zero, and then the reaper will
1596 struct ksock_peer *peer = conn->ksnc_peer;
1597 struct ksock_sched *sched = conn->ksnc_scheduler;
1600 LASSERT(conn->ksnc_closing);
1602 /* wake up the scheduler to "send" all remaining packets to /dev/null */
1603 spin_lock_bh(&sched->kss_lock);
1605 /* a closing conn is always ready to tx */
1606 conn->ksnc_tx_ready = 1;
1608 if (!conn->ksnc_tx_scheduled &&
1609 !list_empty(&conn->ksnc_tx_queue)) {
1610 list_add_tail(&conn->ksnc_tx_list,
1611 &sched->kss_tx_conns);
1612 conn->ksnc_tx_scheduled = 1;
1613 /* extra ref for scheduler */
1614 ksocknal_conn_addref(conn);
1616 wake_up(&sched->kss_waitq);
1619 spin_unlock_bh(&sched->kss_lock);
1621 /* serialise with callbacks */
1622 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1624 ksocknal_lib_reset_callback(conn->ksnc_sock, conn);
1627 * OK, so this conn may not be completely disengaged from its
1628 * scheduler yet, but it _has_ committed to terminate...
1630 conn->ksnc_scheduler->kss_nconns--;
1632 if (peer->ksnp_error) {
1633 /* peer's last conn closed in error */
1634 LASSERT(list_empty(&peer->ksnp_conns));
1636 peer->ksnp_error = 0; /* avoid multiple notifications */
1639 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1642 ksocknal_peer_failed(peer);
1645 * The socket is closed on the final put; either here, or in
1646 * ksocknal_{send,recv}msg(). Since we set up the linger2 option
1647 * when the connection was established, this will close the socket
1648 * immediately, aborting anything buffered in it. Any hung
1649 * zero-copy transmits will therefore complete in finite time.
1651 ksocknal_connsock_decref(conn);
1655 ksocknal_queue_zombie_conn(struct ksock_conn *conn)
1657 /* Queue the conn for the reaper to destroy */
1659 LASSERT(!atomic_read(&conn->ksnc_conn_refcount));
1660 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
1662 list_add_tail(&conn->ksnc_list, &ksocknal_data.ksnd_zombie_conns);
1663 wake_up(&ksocknal_data.ksnd_reaper_waitq);
1665 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
1669 ksocknal_destroy_conn(struct ksock_conn *conn)
1671 unsigned long last_rcv;
1673 /* Final coup-de-grace of the reaper */
1674 CDEBUG(D_NET, "connection %p\n", conn);
1676 LASSERT(!atomic_read(&conn->ksnc_conn_refcount));
1677 LASSERT(!atomic_read(&conn->ksnc_sock_refcount));
1678 LASSERT(!conn->ksnc_sock);
1679 LASSERT(!conn->ksnc_route);
1680 LASSERT(!conn->ksnc_tx_scheduled);
1681 LASSERT(!conn->ksnc_rx_scheduled);
1682 LASSERT(list_empty(&conn->ksnc_tx_queue));
1684 /* complete current receive if any */
1685 switch (conn->ksnc_rx_state) {
1686 case SOCKNAL_RX_LNET_PAYLOAD:
1687 last_rcv = conn->ksnc_rx_deadline -
1688 cfs_time_seconds(*ksocknal_tunables.ksnd_timeout);
1689 CERROR("Completing partial receive from %s[%d], ip %pI4h:%d, with error, wanted: %d, left: %d, last alive is %ld secs ago\n",
1690 libcfs_id2str(conn->ksnc_peer->ksnp_id), conn->ksnc_type,
1691 &conn->ksnc_ipaddr, conn->ksnc_port,
1692 conn->ksnc_rx_nob_wanted, conn->ksnc_rx_nob_left,
1693 cfs_duration_sec(cfs_time_sub(cfs_time_current(),
1695 lnet_finalize(conn->ksnc_peer->ksnp_ni,
1696 conn->ksnc_cookie, -EIO);
1698 case SOCKNAL_RX_LNET_HEADER:
1699 if (conn->ksnc_rx_started)
1700 CERROR("Incomplete receive of lnet header from %s, ip %pI4h:%d, with error, protocol: %d.x.\n",
1701 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1702 &conn->ksnc_ipaddr, conn->ksnc_port,
1703 conn->ksnc_proto->pro_version);
1705 case SOCKNAL_RX_KSM_HEADER:
1706 if (conn->ksnc_rx_started)
1707 CERROR("Incomplete receive of ksock message from %s, ip %pI4h:%d, with error, protocol: %d.x.\n",
1708 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1709 &conn->ksnc_ipaddr, conn->ksnc_port,
1710 conn->ksnc_proto->pro_version);
1712 case SOCKNAL_RX_SLOP:
1713 if (conn->ksnc_rx_started)
1714 CERROR("Incomplete receive of slops from %s, ip %pI4h:%d, with error\n",
1715 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1716 &conn->ksnc_ipaddr, conn->ksnc_port);
1723 ksocknal_peer_decref(conn->ksnc_peer);
1725 LIBCFS_FREE(conn, sizeof(*conn));
1729 ksocknal_close_peer_conns_locked(struct ksock_peer *peer, __u32 ipaddr, int why)
1731 struct ksock_conn *conn;
1732 struct list_head *ctmp;
1733 struct list_head *cnxt;
1736 list_for_each_safe(ctmp, cnxt, &peer->ksnp_conns) {
1737 conn = list_entry(ctmp, struct ksock_conn, ksnc_list);
1739 if (!ipaddr || conn->ksnc_ipaddr == ipaddr) {
1741 ksocknal_close_conn_locked(conn, why);
1749 ksocknal_close_conn_and_siblings(struct ksock_conn *conn, int why)
1751 struct ksock_peer *peer = conn->ksnc_peer;
1752 __u32 ipaddr = conn->ksnc_ipaddr;
1755 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1757 count = ksocknal_close_peer_conns_locked(peer, ipaddr, why);
1759 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1765 ksocknal_close_matching_conns(lnet_process_id_t id, __u32 ipaddr)
1767 struct ksock_peer *peer;
1768 struct list_head *ptmp;
1769 struct list_head *pnxt;
1775 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1777 if (id.nid != LNET_NID_ANY) {
1778 lo = (int)(ksocknal_nid2peerlist(id.nid) - ksocknal_data.ksnd_peers);
1779 hi = (int)(ksocknal_nid2peerlist(id.nid) - ksocknal_data.ksnd_peers);
1782 hi = ksocknal_data.ksnd_peer_hash_size - 1;
1785 for (i = lo; i <= hi; i++) {
1786 list_for_each_safe(ptmp, pnxt,
1787 &ksocknal_data.ksnd_peers[i]) {
1788 peer = list_entry(ptmp, struct ksock_peer, ksnp_list);
1790 if (!((id.nid == LNET_NID_ANY || id.nid == peer->ksnp_id.nid) &&
1791 (id.pid == LNET_PID_ANY || id.pid == peer->ksnp_id.pid)))
1794 count += ksocknal_close_peer_conns_locked(peer, ipaddr, 0);
1798 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1800 /* wildcards always succeed */
1801 if (id.nid == LNET_NID_ANY || id.pid == LNET_PID_ANY || !ipaddr)
1811 ksocknal_notify(lnet_ni_t *ni, lnet_nid_t gw_nid, int alive)
1814 * The router is telling me she's been notified of a change in
1817 lnet_process_id_t id = {0};
1820 id.pid = LNET_PID_ANY;
1822 CDEBUG(D_NET, "gw %s %s\n", libcfs_nid2str(gw_nid),
1823 alive ? "up" : "down");
1826 /* If the gateway crashed, close all open connections... */
1827 ksocknal_close_matching_conns(id, 0);
1832 * ...otherwise do nothing. We can only establish new connections
1833 * if we have autroutes, and these connect on demand.
1838 ksocknal_query(lnet_ni_t *ni, lnet_nid_t nid, unsigned long *when)
1841 unsigned long last_alive = 0;
1842 unsigned long now = cfs_time_current();
1843 struct ksock_peer *peer = NULL;
1844 rwlock_t *glock = &ksocknal_data.ksnd_global_lock;
1845 lnet_process_id_t id = {
1847 .pid = LNET_PID_LUSTRE,
1852 peer = ksocknal_find_peer_locked(ni, id);
1854 struct list_head *tmp;
1855 struct ksock_conn *conn;
1858 list_for_each(tmp, &peer->ksnp_conns) {
1859 conn = list_entry(tmp, struct ksock_conn, ksnc_list);
1860 bufnob = conn->ksnc_sock->sk->sk_wmem_queued;
1862 if (bufnob < conn->ksnc_tx_bufnob) {
1863 /* something got ACKed */
1864 conn->ksnc_tx_deadline =
1865 cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
1866 peer->ksnp_last_alive = now;
1867 conn->ksnc_tx_bufnob = bufnob;
1871 last_alive = peer->ksnp_last_alive;
1872 if (!ksocknal_find_connectable_route_locked(peer))
1881 CDEBUG(D_NET, "Peer %s %p, alive %ld secs ago, connect %d\n",
1882 libcfs_nid2str(nid), peer,
1883 last_alive ? cfs_duration_sec(now - last_alive) : -1,
1889 ksocknal_add_peer(ni, id, LNET_NIDADDR(nid), lnet_acceptor_port());
1891 write_lock_bh(glock);
1893 peer = ksocknal_find_peer_locked(ni, id);
1895 ksocknal_launch_all_connections_locked(peer);
1897 write_unlock_bh(glock);
1901 ksocknal_push_peer(struct ksock_peer *peer)
1905 struct list_head *tmp;
1906 struct ksock_conn *conn;
1908 for (index = 0; ; index++) {
1909 read_lock(&ksocknal_data.ksnd_global_lock);
1914 list_for_each(tmp, &peer->ksnp_conns) {
1916 conn = list_entry(tmp, struct ksock_conn,
1918 ksocknal_conn_addref(conn);
1923 read_unlock(&ksocknal_data.ksnd_global_lock);
1928 ksocknal_lib_push_conn(conn);
1929 ksocknal_conn_decref(conn);
1933 static int ksocknal_push(lnet_ni_t *ni, lnet_process_id_t id)
1935 struct list_head *start;
1936 struct list_head *end;
1937 struct list_head *tmp;
1939 unsigned int hsize = ksocknal_data.ksnd_peer_hash_size;
1941 if (id.nid == LNET_NID_ANY) {
1942 start = &ksocknal_data.ksnd_peers[0];
1943 end = &ksocknal_data.ksnd_peers[hsize - 1];
1945 start = ksocknal_nid2peerlist(id.nid);
1946 end = ksocknal_nid2peerlist(id.nid);
1949 for (tmp = start; tmp <= end; tmp++) {
1950 int peer_off; /* searching offset in peer hash table */
1952 for (peer_off = 0; ; peer_off++) {
1953 struct ksock_peer *peer;
1956 read_lock(&ksocknal_data.ksnd_global_lock);
1957 list_for_each_entry(peer, tmp, ksnp_list) {
1958 if (!((id.nid == LNET_NID_ANY ||
1959 id.nid == peer->ksnp_id.nid) &&
1960 (id.pid == LNET_PID_ANY ||
1961 id.pid == peer->ksnp_id.pid)))
1964 if (i++ == peer_off) {
1965 ksocknal_peer_addref(peer);
1969 read_unlock(&ksocknal_data.ksnd_global_lock);
1971 if (!i) /* no match */
1975 ksocknal_push_peer(peer);
1976 ksocknal_peer_decref(peer);
1983 ksocknal_add_interface(lnet_ni_t *ni, __u32 ipaddress, __u32 netmask)
1985 struct ksock_net *net = ni->ni_data;
1986 struct ksock_interface *iface;
1990 struct list_head *ptmp;
1991 struct ksock_peer *peer;
1992 struct list_head *rtmp;
1993 struct ksock_route *route;
1995 if (!ipaddress || !netmask)
1998 write_lock_bh(&ksocknal_data.ksnd_global_lock);
2000 iface = ksocknal_ip2iface(ni, ipaddress);
2002 /* silently ignore dups */
2004 } else if (net->ksnn_ninterfaces == LNET_MAX_INTERFACES) {
2007 iface = &net->ksnn_interfaces[net->ksnn_ninterfaces++];
2009 iface->ksni_ipaddr = ipaddress;
2010 iface->ksni_netmask = netmask;
2011 iface->ksni_nroutes = 0;
2012 iface->ksni_npeers = 0;
2014 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
2015 list_for_each(ptmp, &ksocknal_data.ksnd_peers[i]) {
2016 peer = list_entry(ptmp, struct ksock_peer,
2019 for (j = 0; j < peer->ksnp_n_passive_ips; j++)
2020 if (peer->ksnp_passive_ips[j] == ipaddress)
2021 iface->ksni_npeers++;
2023 list_for_each(rtmp, &peer->ksnp_routes) {
2024 route = list_entry(rtmp, struct ksock_route,
2027 if (route->ksnr_myipaddr == ipaddress)
2028 iface->ksni_nroutes++;
2034 /* NB only new connections will pay attention to the new interface! */
2037 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
2043 ksocknal_peer_del_interface_locked(struct ksock_peer *peer, __u32 ipaddr)
2045 struct list_head *tmp;
2046 struct list_head *nxt;
2047 struct ksock_route *route;
2048 struct ksock_conn *conn;
2052 for (i = 0; i < peer->ksnp_n_passive_ips; i++)
2053 if (peer->ksnp_passive_ips[i] == ipaddr) {
2054 for (j = i + 1; j < peer->ksnp_n_passive_ips; j++)
2055 peer->ksnp_passive_ips[j - 1] =
2056 peer->ksnp_passive_ips[j];
2057 peer->ksnp_n_passive_ips--;
2061 list_for_each_safe(tmp, nxt, &peer->ksnp_routes) {
2062 route = list_entry(tmp, struct ksock_route, ksnr_list);
2064 if (route->ksnr_myipaddr != ipaddr)
2067 if (route->ksnr_share_count) {
2068 /* Manually created; keep, but unbind */
2069 route->ksnr_myipaddr = 0;
2071 ksocknal_del_route_locked(route);
2075 list_for_each_safe(tmp, nxt, &peer->ksnp_conns) {
2076 conn = list_entry(tmp, struct ksock_conn, ksnc_list);
2078 if (conn->ksnc_myipaddr == ipaddr)
2079 ksocknal_close_conn_locked(conn, 0);
2084 ksocknal_del_interface(lnet_ni_t *ni, __u32 ipaddress)
2086 struct ksock_net *net = ni->ni_data;
2088 struct list_head *tmp;
2089 struct list_head *nxt;
2090 struct ksock_peer *peer;
2095 write_lock_bh(&ksocknal_data.ksnd_global_lock);
2097 for (i = 0; i < net->ksnn_ninterfaces; i++) {
2098 this_ip = net->ksnn_interfaces[i].ksni_ipaddr;
2100 if (!(!ipaddress || ipaddress == this_ip))
2105 for (j = i + 1; j < net->ksnn_ninterfaces; j++)
2106 net->ksnn_interfaces[j - 1] =
2107 net->ksnn_interfaces[j];
2109 net->ksnn_ninterfaces--;
2111 for (j = 0; j < ksocknal_data.ksnd_peer_hash_size; j++) {
2112 list_for_each_safe(tmp, nxt,
2113 &ksocknal_data.ksnd_peers[j]) {
2114 peer = list_entry(tmp, struct ksock_peer, ksnp_list);
2116 if (peer->ksnp_ni != ni)
2119 ksocknal_peer_del_interface_locked(peer, this_ip);
2124 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
2130 ksocknal_ctl(lnet_ni_t *ni, unsigned int cmd, void *arg)
2132 lnet_process_id_t id = {0};
2133 struct libcfs_ioctl_data *data = arg;
2137 case IOC_LIBCFS_GET_INTERFACE: {
2138 struct ksock_net *net = ni->ni_data;
2139 struct ksock_interface *iface;
2141 read_lock(&ksocknal_data.ksnd_global_lock);
2143 if (data->ioc_count >= (__u32)net->ksnn_ninterfaces) {
2147 iface = &net->ksnn_interfaces[data->ioc_count];
2149 data->ioc_u32[0] = iface->ksni_ipaddr;
2150 data->ioc_u32[1] = iface->ksni_netmask;
2151 data->ioc_u32[2] = iface->ksni_npeers;
2152 data->ioc_u32[3] = iface->ksni_nroutes;
2155 read_unlock(&ksocknal_data.ksnd_global_lock);
2159 case IOC_LIBCFS_ADD_INTERFACE:
2160 return ksocknal_add_interface(ni,
2161 data->ioc_u32[0], /* IP address */
2162 data->ioc_u32[1]); /* net mask */
2164 case IOC_LIBCFS_DEL_INTERFACE:
2165 return ksocknal_del_interface(ni,
2166 data->ioc_u32[0]); /* IP address */
2168 case IOC_LIBCFS_GET_PEER: {
2173 int share_count = 0;
2175 rc = ksocknal_get_peer_info(ni, data->ioc_count,
2176 &id, &myip, &ip, &port,
2177 &conn_count, &share_count);
2181 data->ioc_nid = id.nid;
2182 data->ioc_count = share_count;
2183 data->ioc_u32[0] = ip;
2184 data->ioc_u32[1] = port;
2185 data->ioc_u32[2] = myip;
2186 data->ioc_u32[3] = conn_count;
2187 data->ioc_u32[4] = id.pid;
2191 case IOC_LIBCFS_ADD_PEER:
2192 id.nid = data->ioc_nid;
2193 id.pid = LNET_PID_LUSTRE;
2194 return ksocknal_add_peer(ni, id,
2195 data->ioc_u32[0], /* IP */
2196 data->ioc_u32[1]); /* port */
2198 case IOC_LIBCFS_DEL_PEER:
2199 id.nid = data->ioc_nid;
2200 id.pid = LNET_PID_ANY;
2201 return ksocknal_del_peer(ni, id,
2202 data->ioc_u32[0]); /* IP */
2204 case IOC_LIBCFS_GET_CONN: {
2208 struct ksock_conn *conn = ksocknal_get_conn_by_idx(ni, data->ioc_count);
2213 ksocknal_lib_get_conn_tunables(conn, &txmem, &rxmem, &nagle);
2215 data->ioc_count = txmem;
2216 data->ioc_nid = conn->ksnc_peer->ksnp_id.nid;
2217 data->ioc_flags = nagle;
2218 data->ioc_u32[0] = conn->ksnc_ipaddr;
2219 data->ioc_u32[1] = conn->ksnc_port;
2220 data->ioc_u32[2] = conn->ksnc_myipaddr;
2221 data->ioc_u32[3] = conn->ksnc_type;
2222 data->ioc_u32[4] = conn->ksnc_scheduler->kss_info->ksi_cpt;
2223 data->ioc_u32[5] = rxmem;
2224 data->ioc_u32[6] = conn->ksnc_peer->ksnp_id.pid;
2225 ksocknal_conn_decref(conn);
2229 case IOC_LIBCFS_CLOSE_CONNECTION:
2230 id.nid = data->ioc_nid;
2231 id.pid = LNET_PID_ANY;
2232 return ksocknal_close_matching_conns(id,
2235 case IOC_LIBCFS_REGISTER_MYNID:
2236 /* Ignore if this is a noop */
2237 if (data->ioc_nid == ni->ni_nid)
2240 CERROR("obsolete IOC_LIBCFS_REGISTER_MYNID: %s(%s)\n",
2241 libcfs_nid2str(data->ioc_nid),
2242 libcfs_nid2str(ni->ni_nid));
2245 case IOC_LIBCFS_PUSH_CONNECTION:
2246 id.nid = data->ioc_nid;
2247 id.pid = LNET_PID_ANY;
2248 return ksocknal_push(ni, id);
2257 ksocknal_free_buffers(void)
2259 LASSERT(!atomic_read(&ksocknal_data.ksnd_nactive_txs));
2261 if (ksocknal_data.ksnd_sched_info) {
2262 struct ksock_sched_info *info;
2265 cfs_percpt_for_each(info, i, ksocknal_data.ksnd_sched_info) {
2266 if (info->ksi_scheds) {
2267 LIBCFS_FREE(info->ksi_scheds,
2268 info->ksi_nthreads_max *
2269 sizeof(info->ksi_scheds[0]));
2272 cfs_percpt_free(ksocknal_data.ksnd_sched_info);
2275 LIBCFS_FREE(ksocknal_data.ksnd_peers,
2276 sizeof(struct list_head) *
2277 ksocknal_data.ksnd_peer_hash_size);
2279 spin_lock(&ksocknal_data.ksnd_tx_lock);
2281 if (!list_empty(&ksocknal_data.ksnd_idle_noop_txs)) {
2282 struct list_head zlist;
2283 struct ksock_tx *tx;
2284 struct ksock_tx *temp;
2286 list_add(&zlist, &ksocknal_data.ksnd_idle_noop_txs);
2287 list_del_init(&ksocknal_data.ksnd_idle_noop_txs);
2288 spin_unlock(&ksocknal_data.ksnd_tx_lock);
2290 list_for_each_entry_safe(tx, temp, &zlist, tx_list) {
2291 list_del(&tx->tx_list);
2292 LIBCFS_FREE(tx, tx->tx_desc_size);
2295 spin_unlock(&ksocknal_data.ksnd_tx_lock);
2300 ksocknal_base_shutdown(void)
2302 struct ksock_sched_info *info;
2303 struct ksock_sched *sched;
2307 LASSERT(!ksocknal_data.ksnd_nnets);
2309 switch (ksocknal_data.ksnd_init) {
2313 case SOCKNAL_INIT_ALL:
2314 case SOCKNAL_INIT_DATA:
2315 LASSERT(ksocknal_data.ksnd_peers);
2316 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++)
2317 LASSERT(list_empty(&ksocknal_data.ksnd_peers[i]));
2319 LASSERT(list_empty(&ksocknal_data.ksnd_nets));
2320 LASSERT(list_empty(&ksocknal_data.ksnd_enomem_conns));
2321 LASSERT(list_empty(&ksocknal_data.ksnd_zombie_conns));
2322 LASSERT(list_empty(&ksocknal_data.ksnd_connd_connreqs));
2323 LASSERT(list_empty(&ksocknal_data.ksnd_connd_routes));
2325 if (ksocknal_data.ksnd_sched_info) {
2326 cfs_percpt_for_each(info, i,
2327 ksocknal_data.ksnd_sched_info) {
2328 if (!info->ksi_scheds)
2331 for (j = 0; j < info->ksi_nthreads_max; j++) {
2332 sched = &info->ksi_scheds[j];
2334 &sched->kss_tx_conns));
2336 &sched->kss_rx_conns));
2338 &sched->kss_zombie_noop_txs));
2339 LASSERT(!sched->kss_nconns);
2344 /* flag threads to terminate; wake and wait for them to die */
2345 ksocknal_data.ksnd_shuttingdown = 1;
2346 wake_up_all(&ksocknal_data.ksnd_connd_waitq);
2347 wake_up_all(&ksocknal_data.ksnd_reaper_waitq);
2349 if (ksocknal_data.ksnd_sched_info) {
2350 cfs_percpt_for_each(info, i,
2351 ksocknal_data.ksnd_sched_info) {
2352 if (!info->ksi_scheds)
2355 for (j = 0; j < info->ksi_nthreads_max; j++) {
2356 sched = &info->ksi_scheds[j];
2357 wake_up_all(&sched->kss_waitq);
2363 read_lock(&ksocknal_data.ksnd_global_lock);
2364 while (ksocknal_data.ksnd_nthreads) {
2366 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET, /* power of 2? */
2367 "waiting for %d threads to terminate\n",
2368 ksocknal_data.ksnd_nthreads);
2369 read_unlock(&ksocknal_data.ksnd_global_lock);
2370 set_current_state(TASK_UNINTERRUPTIBLE);
2371 schedule_timeout(cfs_time_seconds(1));
2372 read_lock(&ksocknal_data.ksnd_global_lock);
2374 read_unlock(&ksocknal_data.ksnd_global_lock);
2376 ksocknal_free_buffers();
2378 ksocknal_data.ksnd_init = SOCKNAL_INIT_NOTHING;
2382 module_put(THIS_MODULE);
2386 ksocknal_new_incarnation(void)
2388 /* The incarnation number is the time this module loaded and it
2389 * identifies this particular instance of the socknal.
2391 return ktime_get_ns();
2395 ksocknal_base_startup(void)
2397 struct ksock_sched_info *info;
2401 LASSERT(ksocknal_data.ksnd_init == SOCKNAL_INIT_NOTHING);
2402 LASSERT(!ksocknal_data.ksnd_nnets);
2404 memset(&ksocknal_data, 0, sizeof(ksocknal_data)); /* zero pointers */
2406 ksocknal_data.ksnd_peer_hash_size = SOCKNAL_PEER_HASH_SIZE;
2407 LIBCFS_ALLOC(ksocknal_data.ksnd_peers,
2408 sizeof(struct list_head) *
2409 ksocknal_data.ksnd_peer_hash_size);
2410 if (!ksocknal_data.ksnd_peers)
2413 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++)
2414 INIT_LIST_HEAD(&ksocknal_data.ksnd_peers[i]);
2416 rwlock_init(&ksocknal_data.ksnd_global_lock);
2417 INIT_LIST_HEAD(&ksocknal_data.ksnd_nets);
2419 spin_lock_init(&ksocknal_data.ksnd_reaper_lock);
2420 INIT_LIST_HEAD(&ksocknal_data.ksnd_enomem_conns);
2421 INIT_LIST_HEAD(&ksocknal_data.ksnd_zombie_conns);
2422 INIT_LIST_HEAD(&ksocknal_data.ksnd_deathrow_conns);
2423 init_waitqueue_head(&ksocknal_data.ksnd_reaper_waitq);
2425 spin_lock_init(&ksocknal_data.ksnd_connd_lock);
2426 INIT_LIST_HEAD(&ksocknal_data.ksnd_connd_connreqs);
2427 INIT_LIST_HEAD(&ksocknal_data.ksnd_connd_routes);
2428 init_waitqueue_head(&ksocknal_data.ksnd_connd_waitq);
2430 spin_lock_init(&ksocknal_data.ksnd_tx_lock);
2431 INIT_LIST_HEAD(&ksocknal_data.ksnd_idle_noop_txs);
2433 /* NB memset above zeros whole of ksocknal_data */
2435 /* flag lists/ptrs/locks initialised */
2436 ksocknal_data.ksnd_init = SOCKNAL_INIT_DATA;
2437 try_module_get(THIS_MODULE);
2439 ksocknal_data.ksnd_sched_info = cfs_percpt_alloc(lnet_cpt_table(),
2441 if (!ksocknal_data.ksnd_sched_info)
2444 cfs_percpt_for_each(info, i, ksocknal_data.ksnd_sched_info) {
2445 struct ksock_sched *sched;
2448 nthrs = cfs_cpt_weight(lnet_cpt_table(), i);
2449 if (*ksocknal_tunables.ksnd_nscheds > 0) {
2450 nthrs = min(nthrs, *ksocknal_tunables.ksnd_nscheds);
2453 * max to half of CPUs, assume another half should be
2454 * reserved for upper layer modules
2456 nthrs = min(max(SOCKNAL_NSCHEDS, nthrs >> 1), nthrs);
2459 info->ksi_nthreads_max = nthrs;
2462 LIBCFS_CPT_ALLOC(info->ksi_scheds, lnet_cpt_table(), i,
2463 info->ksi_nthreads_max * sizeof(*sched));
2464 if (!info->ksi_scheds)
2467 for (; nthrs > 0; nthrs--) {
2468 sched = &info->ksi_scheds[nthrs - 1];
2470 sched->kss_info = info;
2471 spin_lock_init(&sched->kss_lock);
2472 INIT_LIST_HEAD(&sched->kss_rx_conns);
2473 INIT_LIST_HEAD(&sched->kss_tx_conns);
2474 INIT_LIST_HEAD(&sched->kss_zombie_noop_txs);
2475 init_waitqueue_head(&sched->kss_waitq);
2479 ksocknal_data.ksnd_connd_starting = 0;
2480 ksocknal_data.ksnd_connd_failed_stamp = 0;
2481 ksocknal_data.ksnd_connd_starting_stamp = ktime_get_real_seconds();
2483 * must have at least 2 connds to remain responsive to accepts while
2486 if (*ksocknal_tunables.ksnd_nconnds < SOCKNAL_CONND_RESV + 1)
2487 *ksocknal_tunables.ksnd_nconnds = SOCKNAL_CONND_RESV + 1;
2489 if (*ksocknal_tunables.ksnd_nconnds_max <
2490 *ksocknal_tunables.ksnd_nconnds) {
2491 ksocknal_tunables.ksnd_nconnds_max =
2492 ksocknal_tunables.ksnd_nconnds;
2495 for (i = 0; i < *ksocknal_tunables.ksnd_nconnds; i++) {
2498 spin_lock_bh(&ksocknal_data.ksnd_connd_lock);
2499 ksocknal_data.ksnd_connd_starting++;
2500 spin_unlock_bh(&ksocknal_data.ksnd_connd_lock);
2502 snprintf(name, sizeof(name), "socknal_cd%02d", i);
2503 rc = ksocknal_thread_start(ksocknal_connd,
2504 (void *)((ulong_ptr_t)i), name);
2506 spin_lock_bh(&ksocknal_data.ksnd_connd_lock);
2507 ksocknal_data.ksnd_connd_starting--;
2508 spin_unlock_bh(&ksocknal_data.ksnd_connd_lock);
2509 CERROR("Can't spawn socknal connd: %d\n", rc);
2514 rc = ksocknal_thread_start(ksocknal_reaper, NULL, "socknal_reaper");
2516 CERROR("Can't spawn socknal reaper: %d\n", rc);
2520 /* flag everything initialised */
2521 ksocknal_data.ksnd_init = SOCKNAL_INIT_ALL;
2526 ksocknal_base_shutdown();
2531 ksocknal_debug_peerhash(lnet_ni_t *ni)
2533 struct ksock_peer *peer = NULL;
2534 struct list_head *tmp;
2537 read_lock(&ksocknal_data.ksnd_global_lock);
2539 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
2540 list_for_each(tmp, &ksocknal_data.ksnd_peers[i]) {
2541 peer = list_entry(tmp, struct ksock_peer, ksnp_list);
2543 if (peer->ksnp_ni == ni)
2551 struct ksock_route *route;
2552 struct ksock_conn *conn;
2554 CWARN("Active peer on shutdown: %s, ref %d, scnt %d, closing %d, accepting %d, err %d, zcookie %llu, txq %d, zc_req %d\n",
2555 libcfs_id2str(peer->ksnp_id),
2556 atomic_read(&peer->ksnp_refcount),
2557 peer->ksnp_sharecount, peer->ksnp_closing,
2558 peer->ksnp_accepting, peer->ksnp_error,
2559 peer->ksnp_zc_next_cookie,
2560 !list_empty(&peer->ksnp_tx_queue),
2561 !list_empty(&peer->ksnp_zc_req_list));
2563 list_for_each(tmp, &peer->ksnp_routes) {
2564 route = list_entry(tmp, struct ksock_route, ksnr_list);
2565 CWARN("Route: ref %d, schd %d, conn %d, cnted %d, del %d\n",
2566 atomic_read(&route->ksnr_refcount),
2567 route->ksnr_scheduled, route->ksnr_connecting,
2568 route->ksnr_connected, route->ksnr_deleted);
2571 list_for_each(tmp, &peer->ksnp_conns) {
2572 conn = list_entry(tmp, struct ksock_conn, ksnc_list);
2573 CWARN("Conn: ref %d, sref %d, t %d, c %d\n",
2574 atomic_read(&conn->ksnc_conn_refcount),
2575 atomic_read(&conn->ksnc_sock_refcount),
2576 conn->ksnc_type, conn->ksnc_closing);
2580 read_unlock(&ksocknal_data.ksnd_global_lock);
2584 ksocknal_shutdown(lnet_ni_t *ni)
2586 struct ksock_net *net = ni->ni_data;
2588 lnet_process_id_t anyid = {0};
2590 anyid.nid = LNET_NID_ANY;
2591 anyid.pid = LNET_PID_ANY;
2593 LASSERT(ksocknal_data.ksnd_init == SOCKNAL_INIT_ALL);
2594 LASSERT(ksocknal_data.ksnd_nnets > 0);
2596 spin_lock_bh(&net->ksnn_lock);
2597 net->ksnn_shutdown = 1; /* prevent new peers */
2598 spin_unlock_bh(&net->ksnn_lock);
2600 /* Delete all peers */
2601 ksocknal_del_peer(ni, anyid, 0);
2603 /* Wait for all peer state to clean up */
2605 spin_lock_bh(&net->ksnn_lock);
2606 while (net->ksnn_npeers) {
2607 spin_unlock_bh(&net->ksnn_lock);
2610 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET, /* power of 2? */
2611 "waiting for %d peers to disconnect\n",
2613 set_current_state(TASK_UNINTERRUPTIBLE);
2614 schedule_timeout(cfs_time_seconds(1));
2616 ksocknal_debug_peerhash(ni);
2618 spin_lock_bh(&net->ksnn_lock);
2620 spin_unlock_bh(&net->ksnn_lock);
2622 for (i = 0; i < net->ksnn_ninterfaces; i++) {
2623 LASSERT(!net->ksnn_interfaces[i].ksni_npeers);
2624 LASSERT(!net->ksnn_interfaces[i].ksni_nroutes);
2627 list_del(&net->ksnn_list);
2628 LIBCFS_FREE(net, sizeof(*net));
2630 ksocknal_data.ksnd_nnets--;
2631 if (!ksocknal_data.ksnd_nnets)
2632 ksocknal_base_shutdown();
2636 ksocknal_enumerate_interfaces(struct ksock_net *net)
2644 n = lnet_ipif_enumerate(&names);
2646 CERROR("Can't enumerate interfaces: %d\n", n);
2650 for (i = j = 0; i < n; i++) {
2655 if (!strcmp(names[i], "lo")) /* skip the loopback IF */
2658 rc = lnet_ipif_query(names[i], &up, &ip, &mask);
2660 CWARN("Can't get interface %s info: %d\n",
2666 CWARN("Ignoring interface %s (down)\n",
2671 if (j == LNET_MAX_INTERFACES) {
2672 CWARN("Ignoring interface %s (too many interfaces)\n",
2677 net->ksnn_interfaces[j].ksni_ipaddr = ip;
2678 net->ksnn_interfaces[j].ksni_netmask = mask;
2679 strlcpy(net->ksnn_interfaces[j].ksni_name,
2680 names[i], sizeof(net->ksnn_interfaces[j].ksni_name));
2684 lnet_ipif_free_enumeration(names, n);
2687 CERROR("Can't find any usable interfaces\n");
2693 ksocknal_search_new_ipif(struct ksock_net *net)
2698 for (i = 0; i < net->ksnn_ninterfaces; i++) {
2699 char *ifnam = &net->ksnn_interfaces[i].ksni_name[0];
2700 char *colon = strchr(ifnam, ':');
2702 struct ksock_net *tmp;
2705 if (colon) /* ignore alias device */
2708 list_for_each_entry(tmp, &ksocknal_data.ksnd_nets, ksnn_list) {
2709 for (j = 0; !found && j < tmp->ksnn_ninterfaces; j++) {
2711 &tmp->ksnn_interfaces[j].ksni_name[0];
2712 char *colon2 = strchr(ifnam2, ':');
2717 found = !strcmp(ifnam, ifnam2);
2734 ksocknal_start_schedulers(struct ksock_sched_info *info)
2740 if (!info->ksi_nthreads) {
2741 if (*ksocknal_tunables.ksnd_nscheds > 0) {
2742 nthrs = info->ksi_nthreads_max;
2744 nthrs = cfs_cpt_weight(lnet_cpt_table(),
2746 nthrs = min(max(SOCKNAL_NSCHEDS, nthrs >> 1), nthrs);
2747 nthrs = min(SOCKNAL_NSCHEDS_HIGH, nthrs);
2749 nthrs = min(nthrs, info->ksi_nthreads_max);
2751 LASSERT(info->ksi_nthreads <= info->ksi_nthreads_max);
2752 /* increase two threads if there is new interface */
2753 nthrs = min(2, info->ksi_nthreads_max - info->ksi_nthreads);
2756 for (i = 0; i < nthrs; i++) {
2759 struct ksock_sched *sched;
2761 id = KSOCK_THREAD_ID(info->ksi_cpt, info->ksi_nthreads + i);
2762 sched = &info->ksi_scheds[KSOCK_THREAD_SID(id)];
2763 snprintf(name, sizeof(name), "socknal_sd%02d_%02d",
2764 info->ksi_cpt, (int)(sched - &info->ksi_scheds[0]));
2766 rc = ksocknal_thread_start(ksocknal_scheduler,
2771 CERROR("Can't spawn thread %d for scheduler[%d]: %d\n",
2772 info->ksi_cpt, info->ksi_nthreads + i, rc);
2776 info->ksi_nthreads += i;
2781 ksocknal_net_start_threads(struct ksock_net *net, __u32 *cpts, int ncpts)
2783 int newif = ksocknal_search_new_ipif(net);
2787 LASSERT(ncpts > 0 && ncpts <= cfs_cpt_number(lnet_cpt_table()));
2789 for (i = 0; i < ncpts; i++) {
2790 struct ksock_sched_info *info;
2791 int cpt = !cpts ? i : cpts[i];
2793 LASSERT(cpt < cfs_cpt_number(lnet_cpt_table()));
2794 info = ksocknal_data.ksnd_sched_info[cpt];
2796 if (!newif && info->ksi_nthreads > 0)
2799 rc = ksocknal_start_schedulers(info);
2807 ksocknal_startup(lnet_ni_t *ni)
2809 struct ksock_net *net;
2813 LASSERT(ni->ni_lnd == &the_ksocklnd);
2815 if (ksocknal_data.ksnd_init == SOCKNAL_INIT_NOTHING) {
2816 rc = ksocknal_base_startup();
2821 LIBCFS_ALLOC(net, sizeof(*net));
2825 spin_lock_init(&net->ksnn_lock);
2826 net->ksnn_incarnation = ksocknal_new_incarnation();
2828 ni->ni_peertimeout = *ksocknal_tunables.ksnd_peertimeout;
2829 ni->ni_maxtxcredits = *ksocknal_tunables.ksnd_credits;
2830 ni->ni_peertxcredits = *ksocknal_tunables.ksnd_peertxcredits;
2831 ni->ni_peerrtrcredits = *ksocknal_tunables.ksnd_peerrtrcredits;
2833 if (!ni->ni_interfaces[0]) {
2834 rc = ksocknal_enumerate_interfaces(net);
2838 net->ksnn_ninterfaces = 1;
2840 for (i = 0; i < LNET_MAX_INTERFACES; i++) {
2843 if (!ni->ni_interfaces[i])
2846 rc = lnet_ipif_query(ni->ni_interfaces[i], &up,
2847 &net->ksnn_interfaces[i].ksni_ipaddr,
2848 &net->ksnn_interfaces[i].ksni_netmask);
2851 CERROR("Can't get interface %s info: %d\n",
2852 ni->ni_interfaces[i], rc);
2857 CERROR("Interface %s is down\n",
2858 ni->ni_interfaces[i]);
2862 strlcpy(net->ksnn_interfaces[i].ksni_name,
2863 ni->ni_interfaces[i],
2864 sizeof(net->ksnn_interfaces[i].ksni_name));
2866 net->ksnn_ninterfaces = i;
2869 /* call it before add it to ksocknal_data.ksnd_nets */
2870 rc = ksocknal_net_start_threads(net, ni->ni_cpts, ni->ni_ncpts);
2874 ni->ni_nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid),
2875 net->ksnn_interfaces[0].ksni_ipaddr);
2876 list_add(&net->ksnn_list, &ksocknal_data.ksnd_nets);
2878 ksocknal_data.ksnd_nnets++;
2883 LIBCFS_FREE(net, sizeof(*net));
2885 if (!ksocknal_data.ksnd_nnets)
2886 ksocknal_base_shutdown();
2891 static void __exit ksocklnd_exit(void)
2893 lnet_unregister_lnd(&the_ksocklnd);
2896 static int __init ksocklnd_init(void)
2900 /* check ksnr_connected/connecting field large enough */
2901 CLASSERT(SOCKLND_CONN_NTYPES <= 4);
2902 CLASSERT(SOCKLND_CONN_ACK == SOCKLND_CONN_BULK_IN);
2904 /* initialize the_ksocklnd */
2905 the_ksocklnd.lnd_type = SOCKLND;
2906 the_ksocklnd.lnd_startup = ksocknal_startup;
2907 the_ksocklnd.lnd_shutdown = ksocknal_shutdown;
2908 the_ksocklnd.lnd_ctl = ksocknal_ctl;
2909 the_ksocklnd.lnd_send = ksocknal_send;
2910 the_ksocklnd.lnd_recv = ksocknal_recv;
2911 the_ksocklnd.lnd_notify = ksocknal_notify;
2912 the_ksocklnd.lnd_query = ksocknal_query;
2913 the_ksocklnd.lnd_accept = ksocknal_accept;
2915 rc = ksocknal_tunables_init();
2919 lnet_register_lnd(&the_ksocklnd);
2924 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2925 MODULE_DESCRIPTION("TCP Socket LNet Network Driver");
2926 MODULE_VERSION("2.7.0");
2927 MODULE_LICENSE("GPL");
2929 module_init(ksocklnd_init);
2930 module_exit(ksocklnd_exit);