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
));
97 ksocknal_peer_decref(route
->ksnr_peer
);
99 LIBCFS_FREE(route
, sizeof(*route
));
103 ksocknal_create_peer(ksock_peer_t
**peerp
, lnet_ni_t
*ni
, lnet_process_id_t id
)
105 ksock_net_t
*net
= ni
->ni_data
;
108 LASSERT(id
.nid
!= LNET_NID_ANY
);
109 LASSERT(id
.pid
!= LNET_PID_ANY
);
110 LASSERT(!in_interrupt());
112 LIBCFS_ALLOC(peer
, sizeof(*peer
));
118 atomic_set(&peer
->ksnp_refcount
, 1); /* 1 ref for caller */
119 peer
->ksnp_closing
= 0;
120 peer
->ksnp_accepting
= 0;
121 peer
->ksnp_proto
= NULL
;
122 peer
->ksnp_last_alive
= 0;
123 peer
->ksnp_zc_next_cookie
= SOCKNAL_KEEPALIVE_PING
+ 1;
125 INIT_LIST_HEAD(&peer
->ksnp_conns
);
126 INIT_LIST_HEAD(&peer
->ksnp_routes
);
127 INIT_LIST_HEAD(&peer
->ksnp_tx_queue
);
128 INIT_LIST_HEAD(&peer
->ksnp_zc_req_list
);
129 spin_lock_init(&peer
->ksnp_lock
);
131 spin_lock_bh(&net
->ksnn_lock
);
133 if (net
->ksnn_shutdown
) {
134 spin_unlock_bh(&net
->ksnn_lock
);
136 LIBCFS_FREE(peer
, sizeof(*peer
));
137 CERROR("Can't create peer: network shutdown\n");
143 spin_unlock_bh(&net
->ksnn_lock
);
150 ksocknal_destroy_peer(ksock_peer_t
*peer
)
152 ksock_net_t
*net
= peer
->ksnp_ni
->ni_data
;
154 CDEBUG(D_NET
, "peer %s %p deleted\n",
155 libcfs_id2str(peer
->ksnp_id
), peer
);
157 LASSERT(!atomic_read(&peer
->ksnp_refcount
));
158 LASSERT(!peer
->ksnp_accepting
);
159 LASSERT(list_empty(&peer
->ksnp_conns
));
160 LASSERT(list_empty(&peer
->ksnp_routes
));
161 LASSERT(list_empty(&peer
->ksnp_tx_queue
));
162 LASSERT(list_empty(&peer
->ksnp_zc_req_list
));
164 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
172 spin_lock_bh(&net
->ksnn_lock
);
174 spin_unlock_bh(&net
->ksnn_lock
);
178 ksocknal_find_peer_locked(lnet_ni_t
*ni
, lnet_process_id_t id
)
180 struct list_head
*peer_list
= ksocknal_nid2peerlist(id
.nid
);
181 struct list_head
*tmp
;
184 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
) /* +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
);
231 * All IPs in peer->ksnp_passive_ips[] come from the
232 * interface list, therefore the call must succeed.
236 CDEBUG(D_NET
, "peer=%p iface=%p ksni_nroutes=%d\n",
237 peer
, iface
, iface
->ksni_nroutes
);
238 iface
->ksni_npeers
--;
241 LASSERT(list_empty(&peer
->ksnp_conns
));
242 LASSERT(list_empty(&peer
->ksnp_routes
));
243 LASSERT(!peer
->ksnp_closing
);
244 peer
->ksnp_closing
= 1;
245 list_del(&peer
->ksnp_list
);
246 /* lose peerlist's ref */
247 ksocknal_peer_decref(peer
);
251 ksocknal_get_peer_info(lnet_ni_t
*ni
, int index
,
252 lnet_process_id_t
*id
, __u32
*myip
, __u32
*peer_ip
,
253 int *port
, int *conn_count
, int *share_count
)
256 struct list_head
*ptmp
;
257 ksock_route_t
*route
;
258 struct list_head
*rtmp
;
263 read_lock(&ksocknal_data
.ksnd_global_lock
);
265 for (i
= 0; i
< ksocknal_data
.ksnd_peer_hash_size
; i
++) {
266 list_for_each(ptmp
, &ksocknal_data
.ksnd_peers
[i
]) {
267 peer
= list_entry(ptmp
, ksock_peer_t
, ksnp_list
);
269 if (peer
->ksnp_ni
!= ni
)
272 if (!peer
->ksnp_n_passive_ips
&&
273 list_empty(&peer
->ksnp_routes
)) {
287 for (j
= 0; j
< peer
->ksnp_n_passive_ips
; j
++) {
292 *myip
= peer
->ksnp_passive_ips
[j
];
301 list_for_each(rtmp
, &peer
->ksnp_routes
) {
305 route
= list_entry(rtmp
, ksock_route_t
,
309 *myip
= route
->ksnr_myipaddr
;
310 *peer_ip
= route
->ksnr_ipaddr
;
311 *port
= route
->ksnr_port
;
312 *conn_count
= route
->ksnr_conn_count
;
313 *share_count
= route
->ksnr_share_count
;
320 read_unlock(&ksocknal_data
.ksnd_global_lock
);
325 ksocknal_associate_route_conn_locked(ksock_route_t
*route
, ksock_conn_t
*conn
)
327 ksock_peer_t
*peer
= route
->ksnr_peer
;
328 int type
= conn
->ksnc_type
;
329 ksock_interface_t
*iface
;
331 conn
->ksnc_route
= route
;
332 ksocknal_route_addref(route
);
334 if (route
->ksnr_myipaddr
!= conn
->ksnc_myipaddr
) {
335 if (!route
->ksnr_myipaddr
) {
336 /* route wasn't bound locally yet (the initial route) */
337 CDEBUG(D_NET
, "Binding %s %pI4h to %pI4h\n",
338 libcfs_id2str(peer
->ksnp_id
),
340 &conn
->ksnc_myipaddr
);
342 CDEBUG(D_NET
, "Rebinding %s %pI4h from %pI4h to %pI4h\n",
343 libcfs_id2str(peer
->ksnp_id
),
345 &route
->ksnr_myipaddr
,
346 &conn
->ksnc_myipaddr
);
348 iface
= ksocknal_ip2iface(route
->ksnr_peer
->ksnp_ni
,
349 route
->ksnr_myipaddr
);
351 iface
->ksni_nroutes
--;
353 route
->ksnr_myipaddr
= conn
->ksnc_myipaddr
;
354 iface
= ksocknal_ip2iface(route
->ksnr_peer
->ksnp_ni
,
355 route
->ksnr_myipaddr
);
357 iface
->ksni_nroutes
++;
360 route
->ksnr_connected
|= (1 << type
);
361 route
->ksnr_conn_count
++;
364 * Successful connection => further attempts can
365 * proceed immediately
367 route
->ksnr_retry_interval
= 0;
371 ksocknal_add_route_locked(ksock_peer_t
*peer
, ksock_route_t
*route
)
373 struct list_head
*tmp
;
375 ksock_route_t
*route2
;
377 LASSERT(!peer
->ksnp_closing
);
378 LASSERT(!route
->ksnr_peer
);
379 LASSERT(!route
->ksnr_scheduled
);
380 LASSERT(!route
->ksnr_connecting
);
381 LASSERT(!route
->ksnr_connected
);
383 /* LASSERT(unique) */
384 list_for_each(tmp
, &peer
->ksnp_routes
) {
385 route2
= list_entry(tmp
, ksock_route_t
, ksnr_list
);
387 if (route2
->ksnr_ipaddr
== route
->ksnr_ipaddr
) {
388 CERROR("Duplicate route %s %pI4h\n",
389 libcfs_id2str(peer
->ksnp_id
),
390 &route
->ksnr_ipaddr
);
395 route
->ksnr_peer
= peer
;
396 ksocknal_peer_addref(peer
);
397 /* peer's routelist takes over my ref on 'route' */
398 list_add_tail(&route
->ksnr_list
, &peer
->ksnp_routes
);
400 list_for_each(tmp
, &peer
->ksnp_conns
) {
401 conn
= list_entry(tmp
, ksock_conn_t
, ksnc_list
);
403 if (conn
->ksnc_ipaddr
!= route
->ksnr_ipaddr
)
406 ksocknal_associate_route_conn_locked(route
, conn
);
407 /* keep going (typed routes) */
412 ksocknal_del_route_locked(ksock_route_t
*route
)
414 ksock_peer_t
*peer
= route
->ksnr_peer
;
415 ksock_interface_t
*iface
;
417 struct list_head
*ctmp
;
418 struct list_head
*cnxt
;
420 LASSERT(!route
->ksnr_deleted
);
422 /* Close associated conns */
423 list_for_each_safe(ctmp
, cnxt
, &peer
->ksnp_conns
) {
424 conn
= list_entry(ctmp
, ksock_conn_t
, ksnc_list
);
426 if (conn
->ksnc_route
!= route
)
429 ksocknal_close_conn_locked(conn
, 0);
432 if (route
->ksnr_myipaddr
) {
433 iface
= ksocknal_ip2iface(route
->ksnr_peer
->ksnp_ni
,
434 route
->ksnr_myipaddr
);
436 iface
->ksni_nroutes
--;
439 route
->ksnr_deleted
= 1;
440 list_del(&route
->ksnr_list
);
441 ksocknal_route_decref(route
); /* drop peer's ref */
443 if (list_empty(&peer
->ksnp_routes
) &&
444 list_empty(&peer
->ksnp_conns
)) {
446 * I've just removed the last route to a peer with no active
449 ksocknal_unlink_peer_locked(peer
);
454 ksocknal_add_peer(lnet_ni_t
*ni
, lnet_process_id_t id
, __u32 ipaddr
, int port
)
456 struct list_head
*tmp
;
459 ksock_route_t
*route
;
460 ksock_route_t
*route2
;
463 if (id
.nid
== LNET_NID_ANY
||
464 id
.pid
== LNET_PID_ANY
)
467 /* Have a brand new peer ready... */
468 rc
= ksocknal_create_peer(&peer
, ni
, id
);
472 route
= ksocknal_create_route(ipaddr
, port
);
474 ksocknal_peer_decref(peer
);
478 write_lock_bh(&ksocknal_data
.ksnd_global_lock
);
480 /* always called with a ref on ni, so shutdown can't have started */
481 LASSERT(!((ksock_net_t
*) ni
->ni_data
)->ksnn_shutdown
);
483 peer2
= ksocknal_find_peer_locked(ni
, id
);
485 ksocknal_peer_decref(peer
);
488 /* peer table takes my ref on peer */
489 list_add_tail(&peer
->ksnp_list
,
490 ksocknal_nid2peerlist(id
.nid
));
494 list_for_each(tmp
, &peer
->ksnp_routes
) {
495 route2
= list_entry(tmp
, ksock_route_t
, ksnr_list
);
497 if (route2
->ksnr_ipaddr
== ipaddr
)
503 ksocknal_add_route_locked(peer
, route
);
504 route
->ksnr_share_count
++;
506 ksocknal_route_decref(route
);
507 route2
->ksnr_share_count
++;
510 write_unlock_bh(&ksocknal_data
.ksnd_global_lock
);
516 ksocknal_del_peer_locked(ksock_peer_t
*peer
, __u32 ip
)
519 ksock_route_t
*route
;
520 struct list_head
*tmp
;
521 struct list_head
*nxt
;
524 LASSERT(!peer
->ksnp_closing
);
526 /* Extra ref prevents peer disappearing until I'm done with it */
527 ksocknal_peer_addref(peer
);
529 list_for_each_safe(tmp
, nxt
, &peer
->ksnp_routes
) {
530 route
= list_entry(tmp
, ksock_route_t
, ksnr_list
);
533 if (!(!ip
|| route
->ksnr_ipaddr
== ip
))
536 route
->ksnr_share_count
= 0;
537 /* This deletes associated conns too */
538 ksocknal_del_route_locked(route
);
542 list_for_each_safe(tmp
, nxt
, &peer
->ksnp_routes
) {
543 route
= list_entry(tmp
, ksock_route_t
, ksnr_list
);
544 nshared
+= route
->ksnr_share_count
;
549 * remove everything else if there are no explicit entries
552 list_for_each_safe(tmp
, nxt
, &peer
->ksnp_routes
) {
553 route
= list_entry(tmp
, ksock_route_t
, ksnr_list
);
555 /* we should only be removing auto-entries */
556 LASSERT(!route
->ksnr_share_count
);
557 ksocknal_del_route_locked(route
);
560 list_for_each_safe(tmp
, nxt
, &peer
->ksnp_conns
) {
561 conn
= list_entry(tmp
, ksock_conn_t
, ksnc_list
);
563 ksocknal_close_conn_locked(conn
, 0);
567 ksocknal_peer_decref(peer
);
568 /* NB peer unlinks itself when last conn/route is removed */
572 ksocknal_del_peer(lnet_ni_t
*ni
, lnet_process_id_t id
, __u32 ip
)
575 struct list_head
*ptmp
;
576 struct list_head
*pnxt
;
583 write_lock_bh(&ksocknal_data
.ksnd_global_lock
);
585 if (id
.nid
!= LNET_NID_ANY
) {
586 lo
= (int)(ksocknal_nid2peerlist(id
.nid
) - ksocknal_data
.ksnd_peers
);
587 hi
= (int)(ksocknal_nid2peerlist(id
.nid
) - ksocknal_data
.ksnd_peers
);
590 hi
= ksocknal_data
.ksnd_peer_hash_size
- 1;
593 for (i
= lo
; i
<= hi
; i
++) {
594 list_for_each_safe(ptmp
, pnxt
, &ksocknal_data
.ksnd_peers
[i
]) {
595 peer
= list_entry(ptmp
, ksock_peer_t
, ksnp_list
);
597 if (peer
->ksnp_ni
!= ni
)
600 if (!((id
.nid
== LNET_NID_ANY
|| peer
->ksnp_id
.nid
== id
.nid
) &&
601 (id
.pid
== LNET_PID_ANY
|| peer
->ksnp_id
.pid
== id
.pid
)))
604 ksocknal_peer_addref(peer
); /* a ref for me... */
606 ksocknal_del_peer_locked(peer
, ip
);
608 if (peer
->ksnp_closing
&&
609 !list_empty(&peer
->ksnp_tx_queue
)) {
610 LASSERT(list_empty(&peer
->ksnp_conns
));
611 LASSERT(list_empty(&peer
->ksnp_routes
));
613 list_splice_init(&peer
->ksnp_tx_queue
,
617 ksocknal_peer_decref(peer
); /* ...till here */
619 rc
= 0; /* matched! */
623 write_unlock_bh(&ksocknal_data
.ksnd_global_lock
);
625 ksocknal_txlist_done(ni
, &zombies
, 1);
630 static ksock_conn_t
*
631 ksocknal_get_conn_by_idx(lnet_ni_t
*ni
, int index
)
634 struct list_head
*ptmp
;
636 struct list_head
*ctmp
;
639 read_lock(&ksocknal_data
.ksnd_global_lock
);
641 for (i
= 0; i
< ksocknal_data
.ksnd_peer_hash_size
; i
++) {
642 list_for_each(ptmp
, &ksocknal_data
.ksnd_peers
[i
]) {
643 peer
= list_entry(ptmp
, ksock_peer_t
, ksnp_list
);
645 LASSERT(!peer
->ksnp_closing
);
647 if (peer
->ksnp_ni
!= ni
)
650 list_for_each(ctmp
, &peer
->ksnp_conns
) {
654 conn
= list_entry(ctmp
, ksock_conn_t
,
656 ksocknal_conn_addref(conn
);
657 read_unlock(&ksocknal_data
.ksnd_global_lock
);
663 read_unlock(&ksocknal_data
.ksnd_global_lock
);
667 static ksock_sched_t
*
668 ksocknal_choose_scheduler_locked(unsigned int cpt
)
670 struct ksock_sched_info
*info
= ksocknal_data
.ksnd_sched_info
[cpt
];
671 ksock_sched_t
*sched
;
674 LASSERT(info
->ksi_nthreads
> 0);
676 sched
= &info
->ksi_scheds
[0];
678 * NB: it's safe so far, but info->ksi_nthreads could be changed
679 * at runtime when we have dynamic LNet configuration, then we
680 * need to take care of this.
682 for (i
= 1; i
< info
->ksi_nthreads
; i
++) {
683 if (sched
->kss_nconns
> info
->ksi_scheds
[i
].kss_nconns
)
684 sched
= &info
->ksi_scheds
[i
];
691 ksocknal_local_ipvec(lnet_ni_t
*ni
, __u32
*ipaddrs
)
693 ksock_net_t
*net
= ni
->ni_data
;
697 read_lock(&ksocknal_data
.ksnd_global_lock
);
699 nip
= net
->ksnn_ninterfaces
;
700 LASSERT(nip
<= LNET_MAX_INTERFACES
);
703 * Only offer interfaces for additional connections if I have
707 read_unlock(&ksocknal_data
.ksnd_global_lock
);
711 for (i
= 0; i
< nip
; i
++) {
712 ipaddrs
[i
] = net
->ksnn_interfaces
[i
].ksni_ipaddr
;
716 read_unlock(&ksocknal_data
.ksnd_global_lock
);
721 ksocknal_match_peerip(ksock_interface_t
*iface
, __u32
*ips
, int nips
)
723 int best_netmatch
= 0;
730 for (i
= 0; i
< nips
; i
++) {
734 this_xor
= ips
[i
] ^ iface
->ksni_ipaddr
;
735 this_netmatch
= !(this_xor
& iface
->ksni_netmask
) ? 1 : 0;
738 best_netmatch
< this_netmatch
||
739 (best_netmatch
== this_netmatch
&&
740 best_xor
> this_xor
)))
744 best_netmatch
= this_netmatch
;
753 ksocknal_select_ips(ksock_peer_t
*peer
, __u32
*peerips
, int n_peerips
)
755 rwlock_t
*global_lock
= &ksocknal_data
.ksnd_global_lock
;
756 ksock_net_t
*net
= peer
->ksnp_ni
->ni_data
;
757 ksock_interface_t
*iface
;
758 ksock_interface_t
*best_iface
;
770 * CAVEAT EMPTOR: We do all our interface matching with an
771 * exclusive hold of global lock at IRQ priority. We're only
772 * expecting to be dealing with small numbers of interfaces, so the
773 * O(n**3)-ness shouldn't matter
776 * Also note that I'm not going to return more than n_peerips
777 * interfaces, even if I have more myself
779 write_lock_bh(global_lock
);
781 LASSERT(n_peerips
<= LNET_MAX_INTERFACES
);
782 LASSERT(net
->ksnn_ninterfaces
<= LNET_MAX_INTERFACES
);
785 * Only match interfaces for additional connections
786 * if I have > 1 interface
788 n_ips
= (net
->ksnn_ninterfaces
< 2) ? 0 :
789 min(n_peerips
, net
->ksnn_ninterfaces
);
791 for (i
= 0; peer
->ksnp_n_passive_ips
< n_ips
; i
++) {
792 /* ^ yes really... */
795 * If we have any new interfaces, first tick off all the
796 * peer IPs that match old interfaces, then choose new
797 * interfaces to match the remaining peer IPS.
798 * We don't forget interfaces we've stopped using; we might
799 * start using them again...
801 if (i
< peer
->ksnp_n_passive_ips
) {
803 ip
= peer
->ksnp_passive_ips
[i
];
804 best_iface
= ksocknal_ip2iface(peer
->ksnp_ni
, ip
);
807 /* choose a new interface */
808 LASSERT(i
== peer
->ksnp_n_passive_ips
);
814 for (j
= 0; j
< net
->ksnn_ninterfaces
; j
++) {
815 iface
= &net
->ksnn_interfaces
[j
];
816 ip
= iface
->ksni_ipaddr
;
818 for (k
= 0; k
< peer
->ksnp_n_passive_ips
; k
++)
819 if (peer
->ksnp_passive_ips
[k
] == ip
)
822 if (k
< peer
->ksnp_n_passive_ips
) /* using it already */
825 k
= ksocknal_match_peerip(iface
, peerips
, n_peerips
);
826 xor = ip
^ peerips
[k
];
827 this_netmatch
= !(xor & iface
->ksni_netmask
) ? 1 : 0;
830 best_netmatch
< this_netmatch
||
831 (best_netmatch
== this_netmatch
&&
832 best_npeers
> iface
->ksni_npeers
)))
836 best_netmatch
= this_netmatch
;
837 best_npeers
= iface
->ksni_npeers
;
840 best_iface
->ksni_npeers
++;
841 ip
= best_iface
->ksni_ipaddr
;
842 peer
->ksnp_passive_ips
[i
] = ip
;
843 peer
->ksnp_n_passive_ips
= i
+ 1;
846 /* mark the best matching peer IP used */
847 j
= ksocknal_match_peerip(best_iface
, peerips
, n_peerips
);
851 /* Overwrite input peer IP addresses */
852 memcpy(peerips
, peer
->ksnp_passive_ips
, n_ips
* sizeof(*peerips
));
854 write_unlock_bh(global_lock
);
860 ksocknal_create_routes(ksock_peer_t
*peer
, int port
,
861 __u32
*peer_ipaddrs
, int npeer_ipaddrs
)
863 ksock_route_t
*newroute
= NULL
;
864 rwlock_t
*global_lock
= &ksocknal_data
.ksnd_global_lock
;
865 lnet_ni_t
*ni
= peer
->ksnp_ni
;
866 ksock_net_t
*net
= ni
->ni_data
;
867 struct list_head
*rtmp
;
868 ksock_route_t
*route
;
869 ksock_interface_t
*iface
;
870 ksock_interface_t
*best_iface
;
878 * CAVEAT EMPTOR: We do all our interface matching with an
879 * exclusive hold of global lock at IRQ priority. We're only
880 * expecting to be dealing with small numbers of interfaces, so the
881 * O(n**3)-ness here shouldn't matter
883 write_lock_bh(global_lock
);
885 if (net
->ksnn_ninterfaces
< 2) {
887 * Only create additional connections
888 * if I have > 1 interface
890 write_unlock_bh(global_lock
);
894 LASSERT(npeer_ipaddrs
<= LNET_MAX_INTERFACES
);
896 for (i
= 0; i
< npeer_ipaddrs
; i
++) {
898 newroute
->ksnr_ipaddr
= peer_ipaddrs
[i
];
900 write_unlock_bh(global_lock
);
902 newroute
= ksocknal_create_route(peer_ipaddrs
[i
], port
);
906 write_lock_bh(global_lock
);
909 if (peer
->ksnp_closing
) {
910 /* peer got closed under me */
914 /* Already got a route? */
916 list_for_each(rtmp
, &peer
->ksnp_routes
) {
917 route
= list_entry(rtmp
, ksock_route_t
, ksnr_list
);
919 if (route
->ksnr_ipaddr
== newroute
->ksnr_ipaddr
)
931 LASSERT(net
->ksnn_ninterfaces
<= LNET_MAX_INTERFACES
);
933 /* Select interface to connect from */
934 for (j
= 0; j
< net
->ksnn_ninterfaces
; j
++) {
935 iface
= &net
->ksnn_interfaces
[j
];
937 /* Using this interface already? */
938 list_for_each(rtmp
, &peer
->ksnp_routes
) {
939 route
= list_entry(rtmp
, ksock_route_t
,
942 if (route
->ksnr_myipaddr
== iface
->ksni_ipaddr
)
950 this_netmatch
= (!((iface
->ksni_ipaddr
^
951 newroute
->ksnr_ipaddr
) &
952 iface
->ksni_netmask
)) ? 1 : 0;
955 best_netmatch
< this_netmatch
||
956 (best_netmatch
== this_netmatch
&&
957 best_nroutes
> iface
->ksni_nroutes
)))
961 best_netmatch
= this_netmatch
;
962 best_nroutes
= iface
->ksni_nroutes
;
968 newroute
->ksnr_myipaddr
= best_iface
->ksni_ipaddr
;
969 best_iface
->ksni_nroutes
++;
971 ksocknal_add_route_locked(peer
, newroute
);
975 write_unlock_bh(global_lock
);
977 ksocknal_route_decref(newroute
);
981 ksocknal_accept(lnet_ni_t
*ni
, struct socket
*sock
)
988 rc
= lnet_sock_getaddr(sock
, 1, &peer_ip
, &peer_port
);
989 LASSERT(!rc
); /* we succeeded before */
991 LIBCFS_ALLOC(cr
, sizeof(*cr
));
993 LCONSOLE_ERROR_MSG(0x12f, "Dropping connection request from %pI4h: memory exhausted\n",
1000 cr
->ksncr_sock
= sock
;
1002 spin_lock_bh(&ksocknal_data
.ksnd_connd_lock
);
1004 list_add_tail(&cr
->ksncr_list
, &ksocknal_data
.ksnd_connd_connreqs
);
1005 wake_up(&ksocknal_data
.ksnd_connd_waitq
);
1007 spin_unlock_bh(&ksocknal_data
.ksnd_connd_lock
);
1012 ksocknal_connecting(ksock_peer_t
*peer
, __u32 ipaddr
)
1014 ksock_route_t
*route
;
1016 list_for_each_entry(route
, &peer
->ksnp_routes
, ksnr_list
) {
1017 if (route
->ksnr_ipaddr
== ipaddr
)
1018 return route
->ksnr_connecting
;
1024 ksocknal_create_conn(lnet_ni_t
*ni
, ksock_route_t
*route
,
1025 struct socket
*sock
, int type
)
1027 rwlock_t
*global_lock
= &ksocknal_data
.ksnd_global_lock
;
1029 lnet_process_id_t peerid
;
1030 struct list_head
*tmp
;
1033 ksock_conn_t
*conn2
;
1034 ksock_peer_t
*peer
= NULL
;
1035 ksock_peer_t
*peer2
;
1036 ksock_sched_t
*sched
;
1037 ksock_hello_msg_t
*hello
;
1047 LASSERT(active
== (type
!= SOCKLND_CONN_NONE
));
1049 LIBCFS_ALLOC(conn
, sizeof(*conn
));
1055 conn
->ksnc_peer
= NULL
;
1056 conn
->ksnc_route
= NULL
;
1057 conn
->ksnc_sock
= sock
;
1059 * 2 ref, 1 for conn, another extra ref prevents socket
1060 * being closed before establishment of connection
1062 atomic_set(&conn
->ksnc_sock_refcount
, 2);
1063 conn
->ksnc_type
= type
;
1064 ksocknal_lib_save_callback(sock
, conn
);
1065 atomic_set(&conn
->ksnc_conn_refcount
, 1); /* 1 ref for me */
1067 conn
->ksnc_rx_ready
= 0;
1068 conn
->ksnc_rx_scheduled
= 0;
1070 INIT_LIST_HEAD(&conn
->ksnc_tx_queue
);
1071 conn
->ksnc_tx_ready
= 0;
1072 conn
->ksnc_tx_scheduled
= 0;
1073 conn
->ksnc_tx_carrier
= NULL
;
1074 atomic_set(&conn
->ksnc_tx_nob
, 0);
1076 LIBCFS_ALLOC(hello
, offsetof(ksock_hello_msg_t
,
1077 kshm_ips
[LNET_MAX_INTERFACES
]));
1083 /* stash conn's local and remote addrs */
1084 rc
= ksocknal_lib_get_conn_addrs(conn
);
1089 * Find out/confirm peer's NID and connection type and get the
1090 * vector of interfaces she's willing to let me connect to.
1091 * Passive connections use the listener timeout since the peer sends
1095 peer
= route
->ksnr_peer
;
1096 LASSERT(ni
== peer
->ksnp_ni
);
1098 /* Active connection sends HELLO eagerly */
1099 hello
->kshm_nips
= ksocknal_local_ipvec(ni
, hello
->kshm_ips
);
1100 peerid
= peer
->ksnp_id
;
1102 write_lock_bh(global_lock
);
1103 conn
->ksnc_proto
= peer
->ksnp_proto
;
1104 write_unlock_bh(global_lock
);
1106 if (!conn
->ksnc_proto
) {
1107 conn
->ksnc_proto
= &ksocknal_protocol_v3x
;
1108 #if SOCKNAL_VERSION_DEBUG
1109 if (*ksocknal_tunables
.ksnd_protocol
== 2)
1110 conn
->ksnc_proto
= &ksocknal_protocol_v2x
;
1111 else if (*ksocknal_tunables
.ksnd_protocol
== 1)
1112 conn
->ksnc_proto
= &ksocknal_protocol_v1x
;
1116 rc
= ksocknal_send_hello(ni
, conn
, peerid
.nid
, hello
);
1120 peerid
.nid
= LNET_NID_ANY
;
1121 peerid
.pid
= LNET_PID_ANY
;
1123 /* Passive, get protocol from peer */
1124 conn
->ksnc_proto
= NULL
;
1127 rc
= ksocknal_recv_hello(ni
, conn
, hello
, &peerid
, &incarnation
);
1131 LASSERT(!rc
|| active
);
1132 LASSERT(conn
->ksnc_proto
);
1133 LASSERT(peerid
.nid
!= LNET_NID_ANY
);
1135 cpt
= lnet_cpt_of_nid(peerid
.nid
);
1138 ksocknal_peer_addref(peer
);
1139 write_lock_bh(global_lock
);
1141 rc
= ksocknal_create_peer(&peer
, ni
, peerid
);
1145 write_lock_bh(global_lock
);
1147 /* called with a ref on ni, so shutdown can't have started */
1148 LASSERT(!((ksock_net_t
*) ni
->ni_data
)->ksnn_shutdown
);
1150 peer2
= ksocknal_find_peer_locked(ni
, peerid
);
1153 * NB this puts an "empty" peer in the peer
1154 * table (which takes my ref)
1156 list_add_tail(&peer
->ksnp_list
,
1157 ksocknal_nid2peerlist(peerid
.nid
));
1159 ksocknal_peer_decref(peer
);
1164 ksocknal_peer_addref(peer
);
1165 peer
->ksnp_accepting
++;
1168 * Am I already connecting to this guy? Resolve in
1169 * favour of higher NID...
1171 if (peerid
.nid
< ni
->ni_nid
&&
1172 ksocknal_connecting(peer
, conn
->ksnc_ipaddr
)) {
1174 warn
= "connection race resolution";
1179 if (peer
->ksnp_closing
||
1180 (active
&& route
->ksnr_deleted
)) {
1181 /* peer/route got closed under me */
1183 warn
= "peer/route removed";
1187 if (!peer
->ksnp_proto
) {
1189 * Never connected before.
1190 * NB recv_hello may have returned EPROTO to signal my peer
1191 * wants a different protocol than the one I asked for.
1193 LASSERT(list_empty(&peer
->ksnp_conns
));
1195 peer
->ksnp_proto
= conn
->ksnc_proto
;
1196 peer
->ksnp_incarnation
= incarnation
;
1199 if (peer
->ksnp_proto
!= conn
->ksnc_proto
||
1200 peer
->ksnp_incarnation
!= incarnation
) {
1201 /* Peer rebooted or I've got the wrong protocol version */
1202 ksocknal_close_peer_conns_locked(peer
, 0, 0);
1204 peer
->ksnp_proto
= NULL
;
1206 warn
= peer
->ksnp_incarnation
!= incarnation
?
1208 "wrong proto version";
1218 warn
= "lost conn race";
1221 warn
= "retry with different protocol version";
1226 * Refuse to duplicate an existing connection, unless this is a
1227 * loopback connection
1229 if (conn
->ksnc_ipaddr
!= conn
->ksnc_myipaddr
) {
1230 list_for_each(tmp
, &peer
->ksnp_conns
) {
1231 conn2
= list_entry(tmp
, ksock_conn_t
, ksnc_list
);
1233 if (conn2
->ksnc_ipaddr
!= conn
->ksnc_ipaddr
||
1234 conn2
->ksnc_myipaddr
!= conn
->ksnc_myipaddr
||
1235 conn2
->ksnc_type
!= conn
->ksnc_type
)
1239 * Reply on a passive connection attempt so the peer
1240 * realises we're connected.
1252 * If the connection created by this route didn't bind to the IP
1253 * address the route connected to, the connection/route matching
1254 * code below probably isn't going to work.
1257 route
->ksnr_ipaddr
!= conn
->ksnc_ipaddr
) {
1258 CERROR("Route %s %pI4h connected to %pI4h\n",
1259 libcfs_id2str(peer
->ksnp_id
),
1260 &route
->ksnr_ipaddr
,
1261 &conn
->ksnc_ipaddr
);
1265 * Search for a route corresponding to the new connection and
1266 * create an association. This allows incoming connections created
1267 * by routes in my peer to match my own route entries so I don't
1268 * continually create duplicate routes.
1270 list_for_each(tmp
, &peer
->ksnp_routes
) {
1271 route
= list_entry(tmp
, ksock_route_t
, ksnr_list
);
1273 if (route
->ksnr_ipaddr
!= conn
->ksnc_ipaddr
)
1276 ksocknal_associate_route_conn_locked(route
, conn
);
1280 conn
->ksnc_peer
= peer
; /* conn takes my ref on peer */
1281 peer
->ksnp_last_alive
= cfs_time_current();
1282 peer
->ksnp_send_keepalive
= 0;
1283 peer
->ksnp_error
= 0;
1285 sched
= ksocknal_choose_scheduler_locked(cpt
);
1286 sched
->kss_nconns
++;
1287 conn
->ksnc_scheduler
= sched
;
1289 conn
->ksnc_tx_last_post
= cfs_time_current();
1290 /* Set the deadline for the outgoing HELLO to drain */
1291 conn
->ksnc_tx_bufnob
= sock
->sk
->sk_wmem_queued
;
1292 conn
->ksnc_tx_deadline
= cfs_time_shift(*ksocknal_tunables
.ksnd_timeout
);
1293 mb(); /* order with adding to peer's conn list */
1295 list_add(&conn
->ksnc_list
, &peer
->ksnp_conns
);
1296 ksocknal_conn_addref(conn
);
1298 ksocknal_new_packet(conn
, 0);
1300 conn
->ksnc_zc_capable
= ksocknal_lib_zc_capable(conn
);
1302 /* Take packets blocking for this connection. */
1303 list_for_each_entry_safe(tx
, txtmp
, &peer
->ksnp_tx_queue
, tx_list
) {
1304 if (conn
->ksnc_proto
->pro_match_tx(conn
, tx
, tx
->tx_nonblk
) == SOCKNAL_MATCH_NO
)
1307 list_del(&tx
->tx_list
);
1308 ksocknal_queue_tx_locked(tx
, conn
);
1311 write_unlock_bh(global_lock
);
1314 * We've now got a new connection. Any errors from here on are just
1315 * like "normal" comms errors and we close the connection normally.
1316 * NB (a) we still have to send the reply HELLO for passive
1318 * (b) normal I/O on the conn is blocked until I setup and call the
1321 CDEBUG(D_NET
, "New conn %s p %d.x %pI4h -> %pI4h/%d incarnation:%lld sched[%d:%d]\n",
1322 libcfs_id2str(peerid
), conn
->ksnc_proto
->pro_version
,
1323 &conn
->ksnc_myipaddr
, &conn
->ksnc_ipaddr
,
1324 conn
->ksnc_port
, incarnation
, cpt
,
1325 (int)(sched
- &sched
->kss_info
->ksi_scheds
[0]));
1328 /* additional routes after interface exchange? */
1329 ksocknal_create_routes(peer
, conn
->ksnc_port
,
1330 hello
->kshm_ips
, hello
->kshm_nips
);
1332 hello
->kshm_nips
= ksocknal_select_ips(peer
, hello
->kshm_ips
,
1334 rc
= ksocknal_send_hello(ni
, conn
, peerid
.nid
, hello
);
1337 LIBCFS_FREE(hello
, offsetof(ksock_hello_msg_t
,
1338 kshm_ips
[LNET_MAX_INTERFACES
]));
1341 * setup the socket AFTER I've received hello (it disables
1342 * SO_LINGER). I might call back to the acceptor who may want
1343 * to send a protocol version response and then close the
1344 * socket; this ensures the socket only tears down after the
1345 * response has been sent.
1348 rc
= ksocknal_lib_setup_sock(sock
);
1350 write_lock_bh(global_lock
);
1352 /* NB my callbacks block while I hold ksnd_global_lock */
1353 ksocknal_lib_set_callback(sock
, conn
);
1356 peer
->ksnp_accepting
--;
1358 write_unlock_bh(global_lock
);
1361 write_lock_bh(global_lock
);
1362 if (!conn
->ksnc_closing
) {
1363 /* could be closed by another thread */
1364 ksocknal_close_conn_locked(conn
, rc
);
1366 write_unlock_bh(global_lock
);
1367 } else if (!ksocknal_connsock_addref(conn
)) {
1368 /* Allow I/O to proceed. */
1369 ksocknal_read_callback(conn
);
1370 ksocknal_write_callback(conn
);
1371 ksocknal_connsock_decref(conn
);
1374 ksocknal_connsock_decref(conn
);
1375 ksocknal_conn_decref(conn
);
1379 if (!peer
->ksnp_closing
&&
1380 list_empty(&peer
->ksnp_conns
) &&
1381 list_empty(&peer
->ksnp_routes
)) {
1382 list_add(&zombies
, &peer
->ksnp_tx_queue
);
1383 list_del_init(&peer
->ksnp_tx_queue
);
1384 ksocknal_unlink_peer_locked(peer
);
1387 write_unlock_bh(global_lock
);
1391 CERROR("Not creating conn %s type %d: %s\n",
1392 libcfs_id2str(peerid
), conn
->ksnc_type
, warn
);
1394 CDEBUG(D_NET
, "Not creating conn %s type %d: %s\n",
1395 libcfs_id2str(peerid
), conn
->ksnc_type
, warn
);
1401 * Request retry by replying with CONN_NONE
1402 * ksnc_proto has been set already
1404 conn
->ksnc_type
= SOCKLND_CONN_NONE
;
1405 hello
->kshm_nips
= 0;
1406 ksocknal_send_hello(ni
, conn
, peerid
.nid
, hello
);
1409 write_lock_bh(global_lock
);
1410 peer
->ksnp_accepting
--;
1411 write_unlock_bh(global_lock
);
1414 ksocknal_txlist_done(ni
, &zombies
, 1);
1415 ksocknal_peer_decref(peer
);
1419 LIBCFS_FREE(hello
, offsetof(ksock_hello_msg_t
,
1420 kshm_ips
[LNET_MAX_INTERFACES
]));
1422 LIBCFS_FREE(conn
, sizeof(*conn
));
1430 ksocknal_close_conn_locked(ksock_conn_t
*conn
, int error
)
1433 * This just does the immmediate housekeeping, and queues the
1434 * connection for the reaper to terminate.
1435 * Caller holds ksnd_global_lock exclusively in irq context
1437 ksock_peer_t
*peer
= conn
->ksnc_peer
;
1438 ksock_route_t
*route
;
1439 ksock_conn_t
*conn2
;
1440 struct list_head
*tmp
;
1442 LASSERT(!peer
->ksnp_error
);
1443 LASSERT(!conn
->ksnc_closing
);
1444 conn
->ksnc_closing
= 1;
1446 /* ksnd_deathrow_conns takes over peer's ref */
1447 list_del(&conn
->ksnc_list
);
1449 route
= conn
->ksnc_route
;
1451 /* dissociate conn from route... */
1452 LASSERT(!route
->ksnr_deleted
);
1453 LASSERT(route
->ksnr_connected
& (1 << conn
->ksnc_type
));
1456 list_for_each(tmp
, &peer
->ksnp_conns
) {
1457 conn2
= list_entry(tmp
, ksock_conn_t
, ksnc_list
);
1459 if (conn2
->ksnc_route
== route
&&
1460 conn2
->ksnc_type
== conn
->ksnc_type
)
1466 route
->ksnr_connected
&= ~(1 << conn
->ksnc_type
);
1468 conn
->ksnc_route
= NULL
;
1470 #if 0 /* irrelevant with only eager routes */
1471 /* make route least favourite */
1472 list_del(&route
->ksnr_list
);
1473 list_add_tail(&route
->ksnr_list
, &peer
->ksnp_routes
);
1475 ksocknal_route_decref(route
); /* drop conn's ref on route */
1478 if (list_empty(&peer
->ksnp_conns
)) {
1479 /* No more connections to this peer */
1481 if (!list_empty(&peer
->ksnp_tx_queue
)) {
1484 LASSERT(conn
->ksnc_proto
== &ksocknal_protocol_v3x
);
1487 * throw them to the last connection...,
1488 * these TXs will be send to /dev/null by scheduler
1490 list_for_each_entry(tx
, &peer
->ksnp_tx_queue
,
1492 ksocknal_tx_prep(conn
, tx
);
1494 spin_lock_bh(&conn
->ksnc_scheduler
->kss_lock
);
1495 list_splice_init(&peer
->ksnp_tx_queue
,
1496 &conn
->ksnc_tx_queue
);
1497 spin_unlock_bh(&conn
->ksnc_scheduler
->kss_lock
);
1500 peer
->ksnp_proto
= NULL
; /* renegotiate protocol version */
1501 peer
->ksnp_error
= error
; /* stash last conn close reason */
1503 if (list_empty(&peer
->ksnp_routes
)) {
1505 * I've just closed last conn belonging to a
1506 * peer with no routes to it
1508 ksocknal_unlink_peer_locked(peer
);
1512 spin_lock_bh(&ksocknal_data
.ksnd_reaper_lock
);
1514 list_add_tail(&conn
->ksnc_list
,
1515 &ksocknal_data
.ksnd_deathrow_conns
);
1516 wake_up(&ksocknal_data
.ksnd_reaper_waitq
);
1518 spin_unlock_bh(&ksocknal_data
.ksnd_reaper_lock
);
1522 ksocknal_peer_failed(ksock_peer_t
*peer
)
1525 unsigned long last_alive
= 0;
1528 * There has been a connection failure or comms error; but I'll only
1529 * tell LNET I think the peer is dead if it's to another kernel and
1530 * there are no connections or connection attempts in existence.
1532 read_lock(&ksocknal_data
.ksnd_global_lock
);
1534 if (!(peer
->ksnp_id
.pid
& LNET_PID_USERFLAG
) &&
1535 list_empty(&peer
->ksnp_conns
) &&
1536 !peer
->ksnp_accepting
&&
1537 !ksocknal_find_connecting_route_locked(peer
)) {
1539 last_alive
= peer
->ksnp_last_alive
;
1542 read_unlock(&ksocknal_data
.ksnd_global_lock
);
1545 lnet_notify(peer
->ksnp_ni
, peer
->ksnp_id
.nid
, 0,
1550 ksocknal_finalize_zcreq(ksock_conn_t
*conn
)
1552 ksock_peer_t
*peer
= conn
->ksnc_peer
;
1558 * NB safe to finalize TXs because closing of socket will
1559 * abort all buffered data
1561 LASSERT(!conn
->ksnc_sock
);
1563 spin_lock(&peer
->ksnp_lock
);
1565 list_for_each_entry_safe(tx
, tmp
, &peer
->ksnp_zc_req_list
, tx_zc_list
) {
1566 if (tx
->tx_conn
!= conn
)
1569 LASSERT(tx
->tx_msg
.ksm_zc_cookies
[0]);
1571 tx
->tx_msg
.ksm_zc_cookies
[0] = 0;
1572 tx
->tx_zc_aborted
= 1; /* mark it as not-acked */
1573 list_del(&tx
->tx_zc_list
);
1574 list_add(&tx
->tx_zc_list
, &zlist
);
1577 spin_unlock(&peer
->ksnp_lock
);
1579 while (!list_empty(&zlist
)) {
1580 tx
= list_entry(zlist
.next
, ksock_tx_t
, tx_zc_list
);
1582 list_del(&tx
->tx_zc_list
);
1583 ksocknal_tx_decref(tx
);
1588 ksocknal_terminate_conn(ksock_conn_t
*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 ksock_peer_t
*peer
= conn
->ksnc_peer
;
1597 ksock_sched_t
*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(ksock_conn_t
*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(ksock_conn_t
*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(ksock_peer_t
*peer
, __u32 ipaddr
, int why
)
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
, ksock_conn_t
, ksnc_list
);
1739 if (!ipaddr
|| conn
->ksnc_ipaddr
== ipaddr
) {
1741 ksocknal_close_conn_locked(conn
, why
);
1749 ksocknal_close_conn_and_siblings(ksock_conn_t
*conn
, int why
)
1751 ksock_peer_t
*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
)
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
, ksock_peer_t
, 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 ksock_peer_t
*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
;
1858 list_for_each(tmp
, &peer
->ksnp_conns
) {
1859 conn
= list_entry(tmp
, ksock_conn_t
, 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(ksock_peer_t
*peer
)
1905 struct list_head
*tmp
;
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
, ksock_conn_t
,
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
++) {
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 ksock_net_t
*net
= ni
->ni_data
;
1986 ksock_interface_t
*iface
;
1990 struct list_head
*ptmp
;
1992 struct list_head
*rtmp
;
1993 ksock_route_t
*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
, ksock_peer_t
,
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
, ksock_route_t
,
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(ksock_peer_t
*peer
, __u32 ipaddr
)
2045 struct list_head
*tmp
;
2046 struct list_head
*nxt
;
2047 ksock_route_t
*route
;
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
, ksock_route_t
, 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
, ksock_conn_t
, 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 ksock_net_t
*net
= ni
->ni_data
;
2088 struct list_head
*tmp
;
2089 struct list_head
*nxt
;
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
, ksock_peer_t
, 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 ksock_net_t
*net
= ni
->ni_data
;
2139 ksock_interface_t
*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 ksock_conn_t
*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
;
2285 list_add(&zlist
, &ksocknal_data
.ksnd_idle_noop_txs
);
2286 list_del_init(&ksocknal_data
.ksnd_idle_noop_txs
);
2287 spin_unlock(&ksocknal_data
.ksnd_tx_lock
);
2289 while (!list_empty(&zlist
)) {
2290 tx
= list_entry(zlist
.next
, ksock_tx_t
, 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 ksock_sched_t
*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 ksock_sched_t
*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 ksock_peer_t
*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
, ksock_peer_t
, ksnp_list
);
2543 if (peer
->ksnp_ni
== ni
)
2551 ksock_route_t
*route
;
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
, ksock_route_t
, 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
, ksock_conn_t
, 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
);
2585 ksocknal_shutdown(lnet_ni_t
*ni
)
2587 ksock_net_t
*net
= ni
->ni_data
;
2589 lnet_process_id_t anyid
= {0};
2591 anyid
.nid
= LNET_NID_ANY
;
2592 anyid
.pid
= LNET_PID_ANY
;
2594 LASSERT(ksocknal_data
.ksnd_init
== SOCKNAL_INIT_ALL
);
2595 LASSERT(ksocknal_data
.ksnd_nnets
> 0);
2597 spin_lock_bh(&net
->ksnn_lock
);
2598 net
->ksnn_shutdown
= 1; /* prevent new peers */
2599 spin_unlock_bh(&net
->ksnn_lock
);
2601 /* Delete all peers */
2602 ksocknal_del_peer(ni
, anyid
, 0);
2604 /* Wait for all peer state to clean up */
2606 spin_lock_bh(&net
->ksnn_lock
);
2607 while (net
->ksnn_npeers
) {
2608 spin_unlock_bh(&net
->ksnn_lock
);
2611 CDEBUG(((i
& (-i
)) == i
) ? D_WARNING
: D_NET
, /* power of 2? */
2612 "waiting for %d peers to disconnect\n",
2614 set_current_state(TASK_UNINTERRUPTIBLE
);
2615 schedule_timeout(cfs_time_seconds(1));
2617 ksocknal_debug_peerhash(ni
);
2619 spin_lock_bh(&net
->ksnn_lock
);
2621 spin_unlock_bh(&net
->ksnn_lock
);
2623 for (i
= 0; i
< net
->ksnn_ninterfaces
; i
++) {
2624 LASSERT(!net
->ksnn_interfaces
[i
].ksni_npeers
);
2625 LASSERT(!net
->ksnn_interfaces
[i
].ksni_nroutes
);
2628 list_del(&net
->ksnn_list
);
2629 LIBCFS_FREE(net
, sizeof(*net
));
2631 ksocknal_data
.ksnd_nnets
--;
2632 if (!ksocknal_data
.ksnd_nnets
)
2633 ksocknal_base_shutdown();
2637 ksocknal_enumerate_interfaces(ksock_net_t
*net
)
2645 n
= lnet_ipif_enumerate(&names
);
2647 CERROR("Can't enumerate interfaces: %d\n", n
);
2651 for (i
= j
= 0; i
< n
; i
++) {
2656 if (!strcmp(names
[i
], "lo")) /* skip the loopback IF */
2659 rc
= lnet_ipif_query(names
[i
], &up
, &ip
, &mask
);
2661 CWARN("Can't get interface %s info: %d\n",
2667 CWARN("Ignoring interface %s (down)\n",
2672 if (j
== LNET_MAX_INTERFACES
) {
2673 CWARN("Ignoring interface %s (too many interfaces)\n",
2678 net
->ksnn_interfaces
[j
].ksni_ipaddr
= ip
;
2679 net
->ksnn_interfaces
[j
].ksni_netmask
= mask
;
2680 strlcpy(net
->ksnn_interfaces
[j
].ksni_name
,
2681 names
[i
], sizeof(net
->ksnn_interfaces
[j
].ksni_name
));
2685 lnet_ipif_free_enumeration(names
, n
);
2688 CERROR("Can't find any usable interfaces\n");
2694 ksocknal_search_new_ipif(ksock_net_t
*net
)
2699 for (i
= 0; i
< net
->ksnn_ninterfaces
; i
++) {
2700 char *ifnam
= &net
->ksnn_interfaces
[i
].ksni_name
[0];
2701 char *colon
= strchr(ifnam
, ':');
2706 if (colon
) /* ignore alias device */
2709 list_for_each_entry(tmp
, &ksocknal_data
.ksnd_nets
, ksnn_list
) {
2710 for (j
= 0; !found
&& j
< tmp
->ksnn_ninterfaces
; j
++) {
2712 &tmp
->ksnn_interfaces
[j
].ksni_name
[0];
2713 char *colon2
= strchr(ifnam2
, ':');
2718 found
= !strcmp(ifnam
, ifnam2
);
2735 ksocknal_start_schedulers(struct ksock_sched_info
*info
)
2741 if (!info
->ksi_nthreads
) {
2742 if (*ksocknal_tunables
.ksnd_nscheds
> 0) {
2743 nthrs
= info
->ksi_nthreads_max
;
2745 nthrs
= cfs_cpt_weight(lnet_cpt_table(),
2747 nthrs
= min(max(SOCKNAL_NSCHEDS
, nthrs
>> 1), nthrs
);
2748 nthrs
= min(SOCKNAL_NSCHEDS_HIGH
, nthrs
);
2750 nthrs
= min(nthrs
, info
->ksi_nthreads_max
);
2752 LASSERT(info
->ksi_nthreads
<= info
->ksi_nthreads_max
);
2753 /* increase two threads if there is new interface */
2754 nthrs
= min(2, info
->ksi_nthreads_max
- info
->ksi_nthreads
);
2757 for (i
= 0; i
< nthrs
; i
++) {
2760 ksock_sched_t
*sched
;
2762 id
= KSOCK_THREAD_ID(info
->ksi_cpt
, info
->ksi_nthreads
+ i
);
2763 sched
= &info
->ksi_scheds
[KSOCK_THREAD_SID(id
)];
2764 snprintf(name
, sizeof(name
), "socknal_sd%02d_%02d",
2765 info
->ksi_cpt
, (int)(sched
- &info
->ksi_scheds
[0]));
2767 rc
= ksocknal_thread_start(ksocknal_scheduler
,
2772 CERROR("Can't spawn thread %d for scheduler[%d]: %d\n",
2773 info
->ksi_cpt
, info
->ksi_nthreads
+ i
, rc
);
2777 info
->ksi_nthreads
+= i
;
2782 ksocknal_net_start_threads(ksock_net_t
*net
, __u32
*cpts
, int ncpts
)
2784 int newif
= ksocknal_search_new_ipif(net
);
2788 LASSERT(ncpts
> 0 && ncpts
<= cfs_cpt_number(lnet_cpt_table()));
2790 for (i
= 0; i
< ncpts
; i
++) {
2791 struct ksock_sched_info
*info
;
2792 int cpt
= !cpts
? i
: cpts
[i
];
2794 LASSERT(cpt
< cfs_cpt_number(lnet_cpt_table()));
2795 info
= ksocknal_data
.ksnd_sched_info
[cpt
];
2797 if (!newif
&& info
->ksi_nthreads
> 0)
2800 rc
= ksocknal_start_schedulers(info
);
2808 ksocknal_startup(lnet_ni_t
*ni
)
2814 LASSERT(ni
->ni_lnd
== &the_ksocklnd
);
2816 if (ksocknal_data
.ksnd_init
== SOCKNAL_INIT_NOTHING
) {
2817 rc
= ksocknal_base_startup();
2822 LIBCFS_ALLOC(net
, sizeof(*net
));
2826 spin_lock_init(&net
->ksnn_lock
);
2827 net
->ksnn_incarnation
= ksocknal_new_incarnation();
2829 ni
->ni_peertimeout
= *ksocknal_tunables
.ksnd_peertimeout
;
2830 ni
->ni_maxtxcredits
= *ksocknal_tunables
.ksnd_credits
;
2831 ni
->ni_peertxcredits
= *ksocknal_tunables
.ksnd_peertxcredits
;
2832 ni
->ni_peerrtrcredits
= *ksocknal_tunables
.ksnd_peerrtrcredits
;
2834 if (!ni
->ni_interfaces
[0]) {
2835 rc
= ksocknal_enumerate_interfaces(net
);
2839 net
->ksnn_ninterfaces
= 1;
2841 for (i
= 0; i
< LNET_MAX_INTERFACES
; i
++) {
2844 if (!ni
->ni_interfaces
[i
])
2847 rc
= lnet_ipif_query(ni
->ni_interfaces
[i
], &up
,
2848 &net
->ksnn_interfaces
[i
].ksni_ipaddr
,
2849 &net
->ksnn_interfaces
[i
].ksni_netmask
);
2852 CERROR("Can't get interface %s info: %d\n",
2853 ni
->ni_interfaces
[i
], rc
);
2858 CERROR("Interface %s is down\n",
2859 ni
->ni_interfaces
[i
]);
2863 strlcpy(net
->ksnn_interfaces
[i
].ksni_name
,
2864 ni
->ni_interfaces
[i
],
2865 sizeof(net
->ksnn_interfaces
[i
].ksni_name
));
2867 net
->ksnn_ninterfaces
= i
;
2870 /* call it before add it to ksocknal_data.ksnd_nets */
2871 rc
= ksocknal_net_start_threads(net
, ni
->ni_cpts
, ni
->ni_ncpts
);
2875 ni
->ni_nid
= LNET_MKNID(LNET_NIDNET(ni
->ni_nid
),
2876 net
->ksnn_interfaces
[0].ksni_ipaddr
);
2877 list_add(&net
->ksnn_list
, &ksocknal_data
.ksnd_nets
);
2879 ksocknal_data
.ksnd_nnets
++;
2884 LIBCFS_FREE(net
, sizeof(*net
));
2886 if (!ksocknal_data
.ksnd_nnets
)
2887 ksocknal_base_shutdown();
2893 ksocknal_module_fini(void)
2895 lnet_unregister_lnd(&the_ksocklnd
);
2899 ksocknal_module_init(void)
2903 /* check ksnr_connected/connecting field large enough */
2904 CLASSERT(SOCKLND_CONN_NTYPES
<= 4);
2905 CLASSERT(SOCKLND_CONN_ACK
== SOCKLND_CONN_BULK_IN
);
2907 /* initialize the_ksocklnd */
2908 the_ksocklnd
.lnd_type
= SOCKLND
;
2909 the_ksocklnd
.lnd_startup
= ksocknal_startup
;
2910 the_ksocklnd
.lnd_shutdown
= ksocknal_shutdown
;
2911 the_ksocklnd
.lnd_ctl
= ksocknal_ctl
;
2912 the_ksocklnd
.lnd_send
= ksocknal_send
;
2913 the_ksocklnd
.lnd_recv
= ksocknal_recv
;
2914 the_ksocklnd
.lnd_notify
= ksocknal_notify
;
2915 the_ksocklnd
.lnd_query
= ksocknal_query
;
2916 the_ksocklnd
.lnd_accept
= ksocknal_accept
;
2918 rc
= ksocknal_tunables_init();
2922 lnet_register_lnd(&the_ksocklnd
);
2927 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2928 MODULE_DESCRIPTION("Kernel TCP Socket LND v3.0.0");
2929 MODULE_VERSION("2.7.0");
2930 MODULE_LICENSE("GPL");
2932 module_init(ksocknal_module_init
);
2933 module_exit(ksocknal_module_fini
);