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, 2017, 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>
41 #include <linux/inetdevice.h>
43 static struct lnet_lnd the_ksocklnd;
44 struct ksock_nal_data ksocknal_data;
46 static struct ksock_interface *
47 ksocknal_ip2iface(struct lnet_ni *ni, __u32 ip)
49 struct ksock_net *net = ni->ni_data;
51 struct ksock_interface *iface;
53 for (i = 0; i < net->ksnn_ninterfaces; i++) {
54 LASSERT(i < LNET_INTERFACES_NUM);
55 iface = &net->ksnn_interfaces[i];
57 if (iface->ksni_ipaddr == ip)
64 static struct ksock_route *
65 ksocknal_create_route(__u32 ipaddr, int port)
67 struct ksock_route *route;
69 LIBCFS_ALLOC (route, sizeof (*route));
73 atomic_set (&route->ksnr_refcount, 1);
74 route->ksnr_peer = NULL;
75 route->ksnr_retry_interval = 0; /* OK to connect at any time */
76 route->ksnr_ipaddr = ipaddr;
77 route->ksnr_port = port;
78 route->ksnr_scheduled = 0;
79 route->ksnr_connecting = 0;
80 route->ksnr_connected = 0;
81 route->ksnr_deleted = 0;
82 route->ksnr_conn_count = 0;
83 route->ksnr_share_count = 0;
89 ksocknal_destroy_route(struct ksock_route *route)
91 LASSERT (atomic_read(&route->ksnr_refcount) == 0);
93 if (route->ksnr_peer != NULL)
94 ksocknal_peer_decref(route->ksnr_peer);
96 LIBCFS_FREE (route, sizeof (*route));
100 ksocknal_create_peer(struct ksock_peer_ni **peerp, struct lnet_ni *ni,
101 struct lnet_process_id id)
103 int cpt = lnet_cpt_of_nid(id.nid, ni);
104 struct ksock_net *net = ni->ni_data;
105 struct ksock_peer_ni *peer_ni;
107 LASSERT(id.nid != LNET_NID_ANY);
108 LASSERT(id.pid != LNET_PID_ANY);
109 LASSERT(!in_interrupt());
111 LIBCFS_CPT_ALLOC(peer_ni, lnet_cpt_table(), cpt, sizeof(*peer_ni));
115 peer_ni->ksnp_ni = ni;
116 peer_ni->ksnp_id = id;
117 atomic_set(&peer_ni->ksnp_refcount, 1); /* 1 ref for caller */
118 peer_ni->ksnp_closing = 0;
119 peer_ni->ksnp_accepting = 0;
120 peer_ni->ksnp_proto = NULL;
121 peer_ni->ksnp_last_alive = 0;
122 peer_ni->ksnp_zc_next_cookie = SOCKNAL_KEEPALIVE_PING + 1;
124 INIT_LIST_HEAD(&peer_ni->ksnp_conns);
125 INIT_LIST_HEAD(&peer_ni->ksnp_routes);
126 INIT_LIST_HEAD(&peer_ni->ksnp_tx_queue);
127 INIT_LIST_HEAD(&peer_ni->ksnp_zc_req_list);
128 spin_lock_init(&peer_ni->ksnp_lock);
130 spin_lock_bh(&net->ksnn_lock);
132 if (net->ksnn_shutdown) {
133 spin_unlock_bh(&net->ksnn_lock);
135 LIBCFS_FREE(peer_ni, sizeof(*peer_ni));
136 CERROR("Can't create peer_ni: network shutdown\n");
142 spin_unlock_bh(&net->ksnn_lock);
149 ksocknal_destroy_peer(struct ksock_peer_ni *peer_ni)
151 struct ksock_net *net = peer_ni->ksnp_ni->ni_data;
153 CDEBUG (D_NET, "peer_ni %s %p deleted\n",
154 libcfs_id2str(peer_ni->ksnp_id), peer_ni);
156 LASSERT(atomic_read(&peer_ni->ksnp_refcount) == 0);
157 LASSERT(peer_ni->ksnp_accepting == 0);
158 LASSERT(list_empty(&peer_ni->ksnp_conns));
159 LASSERT(list_empty(&peer_ni->ksnp_routes));
160 LASSERT(list_empty(&peer_ni->ksnp_tx_queue));
161 LASSERT(list_empty(&peer_ni->ksnp_zc_req_list));
163 LIBCFS_FREE(peer_ni, sizeof(*peer_ni));
165 /* NB a peer_ni's connections and routes keep a reference on their peer_ni
166 * until they are destroyed, so we can be assured that _all_ state to
167 * do with this peer_ni has been cleaned up when its refcount drops to
169 spin_lock_bh(&net->ksnn_lock);
171 spin_unlock_bh(&net->ksnn_lock);
174 struct ksock_peer_ni *
175 ksocknal_find_peer_locked(struct lnet_ni *ni, struct lnet_process_id id)
177 struct list_head *peer_list = ksocknal_nid2peerlist(id.nid);
178 struct list_head *tmp;
179 struct ksock_peer_ni *peer_ni;
181 list_for_each(tmp, peer_list) {
182 peer_ni = list_entry(tmp, struct ksock_peer_ni, ksnp_list);
184 LASSERT(!peer_ni->ksnp_closing);
186 if (peer_ni->ksnp_ni != ni)
189 if (peer_ni->ksnp_id.nid != id.nid ||
190 peer_ni->ksnp_id.pid != id.pid)
193 CDEBUG(D_NET, "got peer_ni [%p] -> %s (%d)\n",
194 peer_ni, libcfs_id2str(id),
195 atomic_read(&peer_ni->ksnp_refcount));
201 struct ksock_peer_ni *
202 ksocknal_find_peer(struct lnet_ni *ni, struct lnet_process_id id)
204 struct ksock_peer_ni *peer_ni;
206 read_lock(&ksocknal_data.ksnd_global_lock);
207 peer_ni = ksocknal_find_peer_locked(ni, id);
208 if (peer_ni != NULL) /* +1 ref for caller? */
209 ksocknal_peer_addref(peer_ni);
210 read_unlock(&ksocknal_data.ksnd_global_lock);
216 ksocknal_unlink_peer_locked(struct ksock_peer_ni *peer_ni)
220 struct ksock_interface *iface;
222 for (i = 0; i < peer_ni->ksnp_n_passive_ips; i++) {
223 LASSERT(i < LNET_INTERFACES_NUM);
224 ip = peer_ni->ksnp_passive_ips[i];
226 iface = ksocknal_ip2iface(peer_ni->ksnp_ni, ip);
228 * All IPs in peer_ni->ksnp_passive_ips[] come from the
229 * interface list, therefore the call must succeed.
231 LASSERT(iface != NULL);
233 CDEBUG(D_NET, "peer_ni=%p iface=%p ksni_nroutes=%d\n",
234 peer_ni, iface, iface->ksni_nroutes);
235 iface->ksni_npeers--;
238 LASSERT(list_empty(&peer_ni->ksnp_conns));
239 LASSERT(list_empty(&peer_ni->ksnp_routes));
240 LASSERT(!peer_ni->ksnp_closing);
241 peer_ni->ksnp_closing = 1;
242 list_del(&peer_ni->ksnp_list);
243 /* lose peerlist's ref */
244 ksocknal_peer_decref(peer_ni);
248 ksocknal_get_peer_info(struct lnet_ni *ni, int index,
249 struct lnet_process_id *id, __u32 *myip, __u32 *peer_ip,
250 int *port, int *conn_count, int *share_count)
252 struct ksock_peer_ni *peer_ni;
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_ni = list_entry(ptmp, struct ksock_peer_ni, ksnp_list);
266 if (peer_ni->ksnp_ni != ni)
269 if (peer_ni->ksnp_n_passive_ips == 0 &&
270 list_empty(&peer_ni->ksnp_routes)) {
274 *id = peer_ni->ksnp_id;
284 for (j = 0; j < peer_ni->ksnp_n_passive_ips; j++) {
288 *id = peer_ni->ksnp_id;
289 *myip = peer_ni->ksnp_passive_ips[j];
298 list_for_each(rtmp, &peer_ni->ksnp_routes) {
302 route = list_entry(rtmp, struct ksock_route,
305 *id = peer_ni->ksnp_id;
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_ni *peer_ni = 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 == 0) {
333 /* route wasn't bound locally yet (the initial route) */
334 CDEBUG(D_NET, "Binding %s %pI4h to %pI4h\n",
335 libcfs_id2str(peer_ni->ksnp_id),
337 &conn->ksnc_myipaddr);
339 CDEBUG(D_NET, "Rebinding %s %pI4h from %pI4h "
340 "to %pI4h\n", libcfs_id2str(peer_ni->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++;
360 /* Successful connection => further attempts can
361 * proceed immediately */
362 route->ksnr_retry_interval = 0;
366 ksocknal_add_route_locked(struct ksock_peer_ni *peer_ni, struct ksock_route *route)
368 struct list_head *tmp;
369 struct ksock_conn *conn;
370 struct ksock_route *route2;
372 LASSERT(!peer_ni->ksnp_closing);
373 LASSERT(route->ksnr_peer == NULL);
374 LASSERT(!route->ksnr_scheduled);
375 LASSERT(!route->ksnr_connecting);
376 LASSERT(route->ksnr_connected == 0);
378 /* LASSERT(unique) */
379 list_for_each(tmp, &peer_ni->ksnp_routes) {
380 route2 = list_entry(tmp, struct ksock_route, ksnr_list);
382 if (route2->ksnr_ipaddr == route->ksnr_ipaddr) {
383 CERROR("Duplicate route %s %pI4h\n",
384 libcfs_id2str(peer_ni->ksnp_id),
385 &route->ksnr_ipaddr);
390 route->ksnr_peer = peer_ni;
391 ksocknal_peer_addref(peer_ni);
392 /* peer_ni's routelist takes over my ref on 'route' */
393 list_add_tail(&route->ksnr_list, &peer_ni->ksnp_routes);
395 list_for_each(tmp, &peer_ni->ksnp_conns) {
396 conn = list_entry(tmp, struct ksock_conn, ksnc_list);
398 if (conn->ksnc_ipaddr != route->ksnr_ipaddr)
401 ksocknal_associate_route_conn_locked(route, conn);
402 /* keep going (typed routes) */
407 ksocknal_del_route_locked(struct ksock_route *route)
409 struct ksock_peer_ni *peer_ni = route->ksnr_peer;
410 struct ksock_interface *iface;
411 struct ksock_conn *conn;
412 struct list_head *ctmp;
413 struct list_head *cnxt;
415 LASSERT(!route->ksnr_deleted);
417 /* Close associated conns */
418 list_for_each_safe(ctmp, cnxt, &peer_ni->ksnp_conns) {
419 conn = list_entry(ctmp, struct ksock_conn, ksnc_list);
421 if (conn->ksnc_route != route)
424 ksocknal_close_conn_locked(conn, 0);
427 if (route->ksnr_myipaddr != 0) {
428 iface = ksocknal_ip2iface(route->ksnr_peer->ksnp_ni,
429 route->ksnr_myipaddr);
431 iface->ksni_nroutes--;
434 route->ksnr_deleted = 1;
435 list_del(&route->ksnr_list);
436 ksocknal_route_decref(route); /* drop peer_ni's ref */
438 if (list_empty(&peer_ni->ksnp_routes) &&
439 list_empty(&peer_ni->ksnp_conns)) {
440 /* I've just removed the last route to a peer_ni with no active
442 ksocknal_unlink_peer_locked(peer_ni);
447 ksocknal_add_peer(struct lnet_ni *ni, struct lnet_process_id id, __u32 ipaddr,
450 struct list_head *tmp;
451 struct ksock_peer_ni *peer_ni;
452 struct ksock_peer_ni *peer2;
453 struct ksock_route *route;
454 struct ksock_route *route2;
457 if (id.nid == LNET_NID_ANY ||
458 id.pid == LNET_PID_ANY)
461 /* Have a brand new peer_ni ready... */
462 rc = ksocknal_create_peer(&peer_ni, ni, id);
466 route = ksocknal_create_route (ipaddr, port);
468 ksocknal_peer_decref(peer_ni);
472 write_lock_bh(&ksocknal_data.ksnd_global_lock);
474 /* always called with a ref on ni, so shutdown can't have started */
475 LASSERT(((struct ksock_net *) ni->ni_data)->ksnn_shutdown == 0);
477 peer2 = ksocknal_find_peer_locked(ni, id);
479 ksocknal_peer_decref(peer_ni);
482 /* peer_ni table takes my ref on peer_ni */
483 list_add_tail(&peer_ni->ksnp_list,
484 ksocknal_nid2peerlist(id.nid));
488 list_for_each(tmp, &peer_ni->ksnp_routes) {
489 route2 = list_entry(tmp, struct ksock_route, ksnr_list);
491 if (route2->ksnr_ipaddr == ipaddr)
496 if (route2 == NULL) {
497 ksocknal_add_route_locked(peer_ni, route);
498 route->ksnr_share_count++;
500 ksocknal_route_decref(route);
501 route2->ksnr_share_count++;
504 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
510 ksocknal_del_peer_locked(struct ksock_peer_ni *peer_ni, __u32 ip)
512 struct ksock_conn *conn;
513 struct ksock_route *route;
514 struct list_head *tmp;
515 struct list_head *nxt;
518 LASSERT(!peer_ni->ksnp_closing);
520 /* Extra ref prevents peer_ni disappearing until I'm done with it */
521 ksocknal_peer_addref(peer_ni);
523 list_for_each_safe(tmp, nxt, &peer_ni->ksnp_routes) {
524 route = list_entry(tmp, struct ksock_route, ksnr_list);
527 if (!(ip == 0 || route->ksnr_ipaddr == ip))
530 route->ksnr_share_count = 0;
531 /* This deletes associated conns too */
532 ksocknal_del_route_locked(route);
536 list_for_each_safe(tmp, nxt, &peer_ni->ksnp_routes) {
537 route = list_entry(tmp, struct ksock_route, ksnr_list);
538 nshared += route->ksnr_share_count;
542 /* remove everything else if there are no explicit entries
545 list_for_each_safe(tmp, nxt, &peer_ni->ksnp_routes) {
546 route = list_entry(tmp, struct ksock_route, ksnr_list);
548 /* we should only be removing auto-entries */
549 LASSERT(route->ksnr_share_count == 0);
550 ksocknal_del_route_locked(route);
553 list_for_each_safe(tmp, nxt, &peer_ni->ksnp_conns) {
554 conn = list_entry(tmp, struct ksock_conn, ksnc_list);
556 ksocknal_close_conn_locked(conn, 0);
560 ksocknal_peer_decref(peer_ni);
561 /* NB peer_ni unlinks itself when last conn/route is removed */
565 ksocknal_del_peer(struct lnet_ni *ni, struct lnet_process_id id, __u32 ip)
567 struct list_head zombies = LIST_HEAD_INIT(zombies);
568 struct list_head *ptmp;
569 struct list_head *pnxt;
570 struct ksock_peer_ni *peer_ni;
576 write_lock_bh(&ksocknal_data.ksnd_global_lock);
578 if (id.nid != LNET_NID_ANY) {
579 hi = (int)(ksocknal_nid2peerlist(id.nid) -
580 ksocknal_data.ksnd_peers);
584 hi = ksocknal_data.ksnd_peer_hash_size - 1;
587 for (i = lo; i <= hi; i++) {
588 list_for_each_safe(ptmp, pnxt,
589 &ksocknal_data.ksnd_peers[i]) {
590 peer_ni = list_entry(ptmp, struct ksock_peer_ni, ksnp_list);
592 if (peer_ni->ksnp_ni != ni)
595 if (!((id.nid == LNET_NID_ANY ||
596 peer_ni->ksnp_id.nid == id.nid) &&
597 (id.pid == LNET_PID_ANY ||
598 peer_ni->ksnp_id.pid == id.pid)))
601 ksocknal_peer_addref(peer_ni); /* a ref for me... */
603 ksocknal_del_peer_locked(peer_ni, ip);
605 if (peer_ni->ksnp_closing &&
606 !list_empty(&peer_ni->ksnp_tx_queue)) {
607 LASSERT(list_empty(&peer_ni->ksnp_conns));
608 LASSERT(list_empty(&peer_ni->ksnp_routes));
610 list_splice_init(&peer_ni->ksnp_tx_queue,
614 ksocknal_peer_decref(peer_ni); /* ...till here */
616 rc = 0; /* matched! */
620 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
622 ksocknal_txlist_done(ni, &zombies, -ENETDOWN);
627 static struct ksock_conn *
628 ksocknal_get_conn_by_idx(struct lnet_ni *ni, int index)
630 struct ksock_peer_ni *peer_ni;
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_ni = list_entry(ptmp, struct ksock_peer_ni, ksnp_list);
642 LASSERT(!peer_ni->ksnp_closing);
644 if (peer_ni->ksnp_ni != ni)
647 list_for_each(ctmp, &peer_ni->ksnp_conns) {
651 conn = list_entry(ctmp, struct ksock_conn,
653 ksocknal_conn_addref(conn);
654 read_unlock(&ksocknal_data. \
661 read_unlock(&ksocknal_data.ksnd_global_lock);
665 static struct ksock_sched *
666 ksocknal_choose_scheduler_locked(unsigned int cpt)
668 struct ksock_sched *sched = ksocknal_data.ksnd_schedulers[cpt];
671 if (sched->kss_nthreads == 0) {
672 cfs_percpt_for_each(sched, i, ksocknal_data.ksnd_schedulers) {
673 if (sched->kss_nthreads > 0) {
674 CDEBUG(D_NET, "scheduler[%d] has no threads. selected scheduler[%d]\n",
675 cpt, sched->kss_cpt);
686 ksocknal_local_ipvec(struct lnet_ni *ni, __u32 *ipaddrs)
688 struct ksock_net *net = ni->ni_data;
692 read_lock(&ksocknal_data.ksnd_global_lock);
694 nip = net->ksnn_ninterfaces;
695 LASSERT(nip <= LNET_INTERFACES_NUM);
698 * Only offer interfaces for additional connections if I have
702 read_unlock(&ksocknal_data.ksnd_global_lock);
706 for (i = 0; i < nip; i++) {
707 ipaddrs[i] = net->ksnn_interfaces[i].ksni_ipaddr;
708 LASSERT(ipaddrs[i] != 0);
711 read_unlock(&ksocknal_data.ksnd_global_lock);
716 ksocknal_match_peerip(struct ksock_interface *iface, __u32 *ips, int nips)
718 int best_netmatch = 0;
725 for (i = 0; i < nips; i++) {
729 this_xor = (ips[i] ^ iface->ksni_ipaddr);
730 this_netmatch = ((this_xor & iface->ksni_netmask) == 0) ? 1 : 0;
733 best_netmatch < this_netmatch ||
734 (best_netmatch == this_netmatch &&
735 best_xor > this_xor)))
739 best_netmatch = this_netmatch;
748 ksocknal_select_ips(struct ksock_peer_ni *peer_ni, __u32 *peerips, int n_peerips)
750 rwlock_t *global_lock = &ksocknal_data.ksnd_global_lock;
751 struct ksock_net *net = peer_ni->ksnp_ni->ni_data;
752 struct ksock_interface *iface;
753 struct ksock_interface *best_iface;
764 /* CAVEAT EMPTOR: We do all our interface matching with an
765 * exclusive hold of global lock at IRQ priority. We're only
766 * expecting to be dealing with small numbers of interfaces, so the
767 * O(n**3)-ness shouldn't matter */
769 /* Also note that I'm not going to return more than n_peerips
770 * interfaces, even if I have more myself */
772 write_lock_bh(global_lock);
774 LASSERT(n_peerips <= LNET_INTERFACES_NUM);
775 LASSERT(net->ksnn_ninterfaces <= LNET_INTERFACES_NUM);
777 /* Only match interfaces for additional connections
778 * if I have > 1 interface */
779 n_ips = (net->ksnn_ninterfaces < 2) ? 0 :
780 MIN(n_peerips, net->ksnn_ninterfaces);
782 for (i = 0; peer_ni->ksnp_n_passive_ips < n_ips; i++) {
783 /* ^ yes really... */
785 /* If we have any new interfaces, first tick off all the
786 * peer_ni IPs that match old interfaces, then choose new
787 * interfaces to match the remaining peer_ni IPS.
788 * We don't forget interfaces we've stopped using; we might
789 * start using them again... */
791 if (i < peer_ni->ksnp_n_passive_ips) {
793 ip = peer_ni->ksnp_passive_ips[i];
794 best_iface = ksocknal_ip2iface(peer_ni->ksnp_ni, ip);
796 /* peer_ni passive ips are kept up to date */
797 LASSERT(best_iface != NULL);
799 /* choose a new interface */
800 LASSERT (i == peer_ni->ksnp_n_passive_ips);
806 for (j = 0; j < net->ksnn_ninterfaces; j++) {
807 iface = &net->ksnn_interfaces[j];
808 ip = iface->ksni_ipaddr;
810 for (k = 0; k < peer_ni->ksnp_n_passive_ips; k++)
811 if (peer_ni->ksnp_passive_ips[k] == ip)
814 if (k < peer_ni->ksnp_n_passive_ips) /* using it already */
817 k = ksocknal_match_peerip(iface, peerips, n_peerips);
818 xor = (ip ^ peerips[k]);
819 this_netmatch = ((xor & iface->ksni_netmask) == 0) ? 1 : 0;
821 if (!(best_iface == NULL ||
822 best_netmatch < this_netmatch ||
823 (best_netmatch == this_netmatch &&
824 best_npeers > iface->ksni_npeers)))
828 best_netmatch = this_netmatch;
829 best_npeers = iface->ksni_npeers;
832 LASSERT(best_iface != NULL);
834 best_iface->ksni_npeers++;
835 ip = best_iface->ksni_ipaddr;
836 peer_ni->ksnp_passive_ips[i] = ip;
837 peer_ni->ksnp_n_passive_ips = i+1;
840 /* mark the best matching peer_ni IP used */
841 j = ksocknal_match_peerip(best_iface, peerips, n_peerips);
845 /* Overwrite input peer_ni IP addresses */
846 memcpy(peerips, peer_ni->ksnp_passive_ips, n_ips * sizeof(*peerips));
848 write_unlock_bh(global_lock);
854 ksocknal_create_routes(struct ksock_peer_ni *peer_ni, int port,
855 __u32 *peer_ipaddrs, int npeer_ipaddrs)
857 struct ksock_route *newroute = NULL;
858 rwlock_t *global_lock = &ksocknal_data.ksnd_global_lock;
859 struct lnet_ni *ni = peer_ni->ksnp_ni;
860 struct ksock_net *net = ni->ni_data;
861 struct list_head *rtmp;
862 struct ksock_route *route;
863 struct ksock_interface *iface;
864 struct ksock_interface *best_iface;
871 /* CAVEAT EMPTOR: We do all our interface matching with an
872 * exclusive hold of global lock at IRQ priority. We're only
873 * expecting to be dealing with small numbers of interfaces, so the
874 * O(n**3)-ness here shouldn't matter */
876 write_lock_bh(global_lock);
878 if (net->ksnn_ninterfaces < 2) {
879 /* Only create additional connections
880 * if I have > 1 interface */
881 write_unlock_bh(global_lock);
885 LASSERT(npeer_ipaddrs <= LNET_INTERFACES_NUM);
887 for (i = 0; i < npeer_ipaddrs; i++) {
888 if (newroute != NULL) {
889 newroute->ksnr_ipaddr = peer_ipaddrs[i];
891 write_unlock_bh(global_lock);
893 newroute = ksocknal_create_route(peer_ipaddrs[i], port);
894 if (newroute == NULL)
897 write_lock_bh(global_lock);
900 if (peer_ni->ksnp_closing) {
901 /* peer_ni got closed under me */
905 /* Already got a route? */
907 list_for_each(rtmp, &peer_ni->ksnp_routes) {
908 route = list_entry(rtmp, struct ksock_route, ksnr_list);
910 if (route->ksnr_ipaddr == newroute->ksnr_ipaddr)
922 LASSERT(net->ksnn_ninterfaces <= LNET_INTERFACES_NUM);
924 /* Select interface to connect from */
925 for (j = 0; j < net->ksnn_ninterfaces; j++) {
926 iface = &net->ksnn_interfaces[j];
928 /* Using this interface already? */
929 list_for_each(rtmp, &peer_ni->ksnp_routes) {
930 route = list_entry(rtmp, struct ksock_route,
933 if (route->ksnr_myipaddr == iface->ksni_ipaddr)
941 this_netmatch = (((iface->ksni_ipaddr ^
942 newroute->ksnr_ipaddr) &
943 iface->ksni_netmask) == 0) ? 1 : 0;
945 if (!(best_iface == NULL ||
946 best_netmatch < this_netmatch ||
947 (best_netmatch == this_netmatch &&
948 best_nroutes > iface->ksni_nroutes)))
952 best_netmatch = this_netmatch;
953 best_nroutes = iface->ksni_nroutes;
956 if (best_iface == NULL)
959 newroute->ksnr_myipaddr = best_iface->ksni_ipaddr;
960 best_iface->ksni_nroutes++;
962 ksocknal_add_route_locked(peer_ni, newroute);
966 write_unlock_bh(global_lock);
967 if (newroute != NULL)
968 ksocknal_route_decref(newroute);
972 ksocknal_accept(struct lnet_ni *ni, struct socket *sock)
974 struct ksock_connreq *cr;
979 rc = lnet_sock_getaddr(sock, true, &peer_ip, &peer_port);
980 LASSERT(rc == 0); /* we succeeded before */
982 LIBCFS_ALLOC(cr, sizeof(*cr));
984 LCONSOLE_ERROR_MSG(0x12f, "Dropping connection request from "
985 "%pI4h: memory exhausted\n", &peer_ip);
991 cr->ksncr_sock = sock;
993 spin_lock_bh(&ksocknal_data.ksnd_connd_lock);
995 list_add_tail(&cr->ksncr_list, &ksocknal_data.ksnd_connd_connreqs);
996 wake_up(&ksocknal_data.ksnd_connd_waitq);
998 spin_unlock_bh(&ksocknal_data.ksnd_connd_lock);
1003 ksocknal_connecting(struct ksock_peer_ni *peer_ni, __u32 ipaddr)
1005 struct ksock_route *route;
1007 list_for_each_entry(route, &peer_ni->ksnp_routes, ksnr_list) {
1008 if (route->ksnr_ipaddr == ipaddr)
1009 return route->ksnr_connecting;
1015 ksocknal_create_conn(struct lnet_ni *ni, struct ksock_route *route,
1016 struct socket *sock, int type)
1018 rwlock_t *global_lock = &ksocknal_data.ksnd_global_lock;
1019 struct list_head zombies = LIST_HEAD_INIT(zombies);
1020 struct lnet_process_id peerid;
1021 struct list_head *tmp;
1023 struct ksock_conn *conn;
1024 struct ksock_conn *conn2;
1025 struct ksock_peer_ni *peer_ni = NULL;
1026 struct ksock_peer_ni *peer2;
1027 struct ksock_sched *sched;
1028 struct ksock_hello_msg *hello;
1030 struct ksock_tx *tx;
1031 struct ksock_tx *txtmp;
1037 active = (route != NULL);
1039 LASSERT (active == (type != SOCKLND_CONN_NONE));
1041 LIBCFS_ALLOC(conn, sizeof(*conn));
1047 conn->ksnc_peer = NULL;
1048 conn->ksnc_route = NULL;
1049 conn->ksnc_sock = sock;
1050 /* 2 ref, 1 for conn, another extra ref prevents socket
1051 * being closed before establishment of connection */
1052 atomic_set (&conn->ksnc_sock_refcount, 2);
1053 conn->ksnc_type = type;
1054 ksocknal_lib_save_callback(sock, conn);
1055 atomic_set (&conn->ksnc_conn_refcount, 1); /* 1 ref for me */
1057 conn->ksnc_rx_ready = 0;
1058 conn->ksnc_rx_scheduled = 0;
1060 INIT_LIST_HEAD(&conn->ksnc_tx_queue);
1061 conn->ksnc_tx_ready = 0;
1062 conn->ksnc_tx_scheduled = 0;
1063 conn->ksnc_tx_carrier = NULL;
1064 atomic_set (&conn->ksnc_tx_nob, 0);
1066 LIBCFS_ALLOC(hello, offsetof(struct ksock_hello_msg,
1067 kshm_ips[LNET_INTERFACES_NUM]));
1068 if (hello == NULL) {
1073 /* stash conn's local and remote addrs */
1074 rc = ksocknal_lib_get_conn_addrs (conn);
1078 /* Find out/confirm peer_ni's NID and connection type and get the
1079 * vector of interfaces she's willing to let me connect to.
1080 * Passive connections use the listener timeout since the peer_ni sends
1084 peer_ni = route->ksnr_peer;
1085 LASSERT(ni == peer_ni->ksnp_ni);
1087 /* Active connection sends HELLO eagerly */
1088 hello->kshm_nips = ksocknal_local_ipvec(ni, hello->kshm_ips);
1089 peerid = peer_ni->ksnp_id;
1091 write_lock_bh(global_lock);
1092 conn->ksnc_proto = peer_ni->ksnp_proto;
1093 write_unlock_bh(global_lock);
1095 if (conn->ksnc_proto == NULL) {
1096 conn->ksnc_proto = &ksocknal_protocol_v3x;
1097 #if SOCKNAL_VERSION_DEBUG
1098 if (*ksocknal_tunables.ksnd_protocol == 2)
1099 conn->ksnc_proto = &ksocknal_protocol_v2x;
1100 else if (*ksocknal_tunables.ksnd_protocol == 1)
1101 conn->ksnc_proto = &ksocknal_protocol_v1x;
1105 rc = ksocknal_send_hello (ni, conn, peerid.nid, hello);
1109 peerid.nid = LNET_NID_ANY;
1110 peerid.pid = LNET_PID_ANY;
1112 /* Passive, get protocol from peer_ni */
1113 conn->ksnc_proto = NULL;
1116 rc = ksocknal_recv_hello (ni, conn, hello, &peerid, &incarnation);
1120 LASSERT (rc == 0 || active);
1121 LASSERT (conn->ksnc_proto != NULL);
1122 LASSERT (peerid.nid != LNET_NID_ANY);
1124 cpt = lnet_cpt_of_nid(peerid.nid, ni);
1127 ksocknal_peer_addref(peer_ni);
1128 write_lock_bh(global_lock);
1130 rc = ksocknal_create_peer(&peer_ni, ni, peerid);
1134 write_lock_bh(global_lock);
1136 /* called with a ref on ni, so shutdown can't have started */
1137 LASSERT(((struct ksock_net *) ni->ni_data)->ksnn_shutdown == 0);
1139 peer2 = ksocknal_find_peer_locked(ni, peerid);
1140 if (peer2 == NULL) {
1141 /* NB this puts an "empty" peer_ni in the peer_ni
1142 * table (which takes my ref) */
1143 list_add_tail(&peer_ni->ksnp_list,
1144 ksocknal_nid2peerlist(peerid.nid));
1146 ksocknal_peer_decref(peer_ni);
1151 ksocknal_peer_addref(peer_ni);
1152 peer_ni->ksnp_accepting++;
1154 /* Am I already connecting to this guy? Resolve in
1155 * favour of higher NID... */
1156 if (peerid.nid < ni->ni_nid &&
1157 ksocknal_connecting(peer_ni, conn->ksnc_ipaddr)) {
1159 warn = "connection race resolution";
1164 if (peer_ni->ksnp_closing ||
1165 (active && route->ksnr_deleted)) {
1166 /* peer_ni/route got closed under me */
1168 warn = "peer_ni/route removed";
1172 if (peer_ni->ksnp_proto == NULL) {
1173 /* Never connected before.
1174 * NB recv_hello may have returned EPROTO to signal my peer_ni
1175 * wants a different protocol than the one I asked for.
1177 LASSERT(list_empty(&peer_ni->ksnp_conns));
1179 peer_ni->ksnp_proto = conn->ksnc_proto;
1180 peer_ni->ksnp_incarnation = incarnation;
1183 if (peer_ni->ksnp_proto != conn->ksnc_proto ||
1184 peer_ni->ksnp_incarnation != incarnation) {
1185 /* peer_ni rebooted or I've got the wrong protocol version */
1186 ksocknal_close_peer_conns_locked(peer_ni, 0, 0);
1188 peer_ni->ksnp_proto = NULL;
1190 warn = peer_ni->ksnp_incarnation != incarnation ?
1191 "peer_ni rebooted" :
1192 "wrong proto version";
1202 warn = "lost conn race";
1205 warn = "retry with different protocol version";
1209 /* Refuse to duplicate an existing connection, unless this is a
1210 * loopback connection */
1211 if (conn->ksnc_ipaddr != conn->ksnc_myipaddr) {
1212 list_for_each(tmp, &peer_ni->ksnp_conns) {
1213 conn2 = list_entry(tmp, struct ksock_conn, ksnc_list);
1215 if (conn2->ksnc_ipaddr != conn->ksnc_ipaddr ||
1216 conn2->ksnc_myipaddr != conn->ksnc_myipaddr ||
1217 conn2->ksnc_type != conn->ksnc_type)
1220 /* Reply on a passive connection attempt so the peer_ni
1221 * realises we're connected. */
1231 /* If the connection created by this route didn't bind to the IP
1232 * address the route connected to, the connection/route matching
1233 * code below probably isn't going to work. */
1235 route->ksnr_ipaddr != conn->ksnc_ipaddr) {
1236 CERROR("Route %s %pI4h connected to %pI4h\n",
1237 libcfs_id2str(peer_ni->ksnp_id),
1238 &route->ksnr_ipaddr,
1239 &conn->ksnc_ipaddr);
1242 /* Search for a route corresponding to the new connection and
1243 * create an association. This allows incoming connections created
1244 * by routes in my peer_ni to match my own route entries so I don't
1245 * continually create duplicate routes. */
1246 list_for_each(tmp, &peer_ni->ksnp_routes) {
1247 route = list_entry(tmp, struct ksock_route, ksnr_list);
1249 if (route->ksnr_ipaddr != conn->ksnc_ipaddr)
1252 ksocknal_associate_route_conn_locked(route, conn);
1256 conn->ksnc_peer = peer_ni; /* conn takes my ref on peer_ni */
1257 peer_ni->ksnp_last_alive = ktime_get_seconds();
1258 peer_ni->ksnp_send_keepalive = 0;
1259 peer_ni->ksnp_error = 0;
1261 sched = ksocknal_choose_scheduler_locked(cpt);
1263 CERROR("no schedulers available. node is unhealthy\n");
1267 * The cpt might have changed if we ended up selecting a non cpt
1268 * native scheduler. So use the scheduler's cpt instead.
1270 cpt = sched->kss_cpt;
1271 sched->kss_nconns++;
1272 conn->ksnc_scheduler = sched;
1274 conn->ksnc_tx_last_post = ktime_get_seconds();
1275 /* Set the deadline for the outgoing HELLO to drain */
1276 conn->ksnc_tx_bufnob = sock->sk->sk_wmem_queued;
1277 conn->ksnc_tx_deadline = ktime_get_seconds() +
1278 lnet_get_lnd_timeout();
1279 smp_mb(); /* order with adding to peer_ni's conn list */
1281 list_add(&conn->ksnc_list, &peer_ni->ksnp_conns);
1282 ksocknal_conn_addref(conn);
1284 ksocknal_new_packet(conn, 0);
1286 conn->ksnc_zc_capable = ksocknal_lib_zc_capable(conn);
1288 /* Take packets blocking for this connection. */
1289 list_for_each_entry_safe(tx, txtmp, &peer_ni->ksnp_tx_queue, tx_list) {
1290 if (conn->ksnc_proto->pro_match_tx(conn, tx, tx->tx_nonblk) ==
1294 list_del(&tx->tx_list);
1295 ksocknal_queue_tx_locked(tx, conn);
1298 write_unlock_bh(global_lock);
1300 /* We've now got a new connection. Any errors from here on are just
1301 * like "normal" comms errors and we close the connection normally.
1302 * NB (a) we still have to send the reply HELLO for passive
1304 * (b) normal I/O on the conn is blocked until I setup and call the
1308 CDEBUG(D_NET, "New conn %s p %d.x %pI4h -> %pI4h/%d"
1309 " incarnation:%lld sched[%d]\n",
1310 libcfs_id2str(peerid), conn->ksnc_proto->pro_version,
1311 &conn->ksnc_myipaddr, &conn->ksnc_ipaddr,
1312 conn->ksnc_port, incarnation, cpt);
1315 /* additional routes after interface exchange? */
1316 ksocknal_create_routes(peer_ni, conn->ksnc_port,
1317 hello->kshm_ips, hello->kshm_nips);
1319 hello->kshm_nips = ksocknal_select_ips(peer_ni, hello->kshm_ips,
1321 rc = ksocknal_send_hello(ni, conn, peerid.nid, hello);
1324 LIBCFS_FREE(hello, offsetof(struct ksock_hello_msg,
1325 kshm_ips[LNET_INTERFACES_NUM]));
1327 /* setup the socket AFTER I've received hello (it disables
1328 * SO_LINGER). I might call back to the acceptor who may want
1329 * to send a protocol version response and then close the
1330 * socket; this ensures the socket only tears down after the
1331 * response has been sent. */
1333 rc = ksocknal_lib_setup_sock(sock);
1335 write_lock_bh(global_lock);
1337 /* NB my callbacks block while I hold ksnd_global_lock */
1338 ksocknal_lib_set_callback(sock, conn);
1341 peer_ni->ksnp_accepting--;
1343 write_unlock_bh(global_lock);
1346 write_lock_bh(global_lock);
1347 if (!conn->ksnc_closing) {
1348 /* could be closed by another thread */
1349 ksocknal_close_conn_locked(conn, rc);
1351 write_unlock_bh(global_lock);
1352 } else if (ksocknal_connsock_addref(conn) == 0) {
1353 /* Allow I/O to proceed. */
1354 ksocknal_read_callback(conn);
1355 ksocknal_write_callback(conn);
1356 ksocknal_connsock_decref(conn);
1359 ksocknal_connsock_decref(conn);
1360 ksocknal_conn_decref(conn);
1364 if (!peer_ni->ksnp_closing &&
1365 list_empty(&peer_ni->ksnp_conns) &&
1366 list_empty(&peer_ni->ksnp_routes)) {
1367 list_add(&zombies, &peer_ni->ksnp_tx_queue);
1368 list_del_init(&peer_ni->ksnp_tx_queue);
1369 ksocknal_unlink_peer_locked(peer_ni);
1372 write_unlock_bh(global_lock);
1376 CERROR("Not creating conn %s type %d: %s\n",
1377 libcfs_id2str(peerid), conn->ksnc_type, warn);
1379 CDEBUG(D_NET, "Not creating conn %s type %d: %s\n",
1380 libcfs_id2str(peerid), conn->ksnc_type, warn);
1385 /* Request retry by replying with CONN_NONE
1386 * ksnc_proto has been set already */
1387 conn->ksnc_type = SOCKLND_CONN_NONE;
1388 hello->kshm_nips = 0;
1389 ksocknal_send_hello(ni, conn, peerid.nid, hello);
1392 write_lock_bh(global_lock);
1393 peer_ni->ksnp_accepting--;
1394 write_unlock_bh(global_lock);
1398 * If we get here without an error code, just use -EALREADY.
1399 * Depending on how we got here, the error may be positive
1400 * or negative. Normalize the value for ksocknal_txlist_done().
1402 rc2 = (rc == 0 ? -EALREADY : (rc > 0 ? -rc : rc));
1403 ksocknal_txlist_done(ni, &zombies, rc2);
1404 ksocknal_peer_decref(peer_ni);
1408 LIBCFS_FREE(hello, offsetof(struct ksock_hello_msg,
1409 kshm_ips[LNET_INTERFACES_NUM]));
1411 LIBCFS_FREE(conn, sizeof(*conn));
1419 ksocknal_close_conn_locked(struct ksock_conn *conn, int error)
1421 /* This just does the immmediate housekeeping, and queues the
1422 * connection for the reaper to terminate.
1423 * Caller holds ksnd_global_lock exclusively in irq context */
1424 struct ksock_peer_ni *peer_ni = conn->ksnc_peer;
1425 struct ksock_route *route;
1426 struct ksock_conn *conn2;
1427 struct list_head *tmp;
1429 LASSERT(peer_ni->ksnp_error == 0);
1430 LASSERT(!conn->ksnc_closing);
1431 conn->ksnc_closing = 1;
1433 /* ksnd_deathrow_conns takes over peer_ni's ref */
1434 list_del(&conn->ksnc_list);
1436 route = conn->ksnc_route;
1437 if (route != NULL) {
1438 /* dissociate conn from route... */
1439 LASSERT(!route->ksnr_deleted);
1440 LASSERT((route->ksnr_connected & (1 << conn->ksnc_type)) != 0);
1443 list_for_each(tmp, &peer_ni->ksnp_conns) {
1444 conn2 = list_entry(tmp, struct ksock_conn, ksnc_list);
1446 if (conn2->ksnc_route == route &&
1447 conn2->ksnc_type == conn->ksnc_type)
1453 route->ksnr_connected &= ~(1 << conn->ksnc_type);
1455 conn->ksnc_route = NULL;
1457 ksocknal_route_decref(route); /* drop conn's ref on route */
1460 if (list_empty(&peer_ni->ksnp_conns)) {
1461 /* No more connections to this peer_ni */
1463 if (!list_empty(&peer_ni->ksnp_tx_queue)) {
1464 struct ksock_tx *tx;
1466 LASSERT(conn->ksnc_proto == &ksocknal_protocol_v3x);
1468 /* throw them to the last connection...,
1469 * these TXs will be send to /dev/null by scheduler */
1470 list_for_each_entry(tx, &peer_ni->ksnp_tx_queue,
1472 ksocknal_tx_prep(conn, tx);
1474 spin_lock_bh(&conn->ksnc_scheduler->kss_lock);
1475 list_splice_init(&peer_ni->ksnp_tx_queue,
1476 &conn->ksnc_tx_queue);
1477 spin_unlock_bh(&conn->ksnc_scheduler->kss_lock);
1480 /* renegotiate protocol version */
1481 peer_ni->ksnp_proto = NULL;
1482 /* stash last conn close reason */
1483 peer_ni->ksnp_error = error;
1485 if (list_empty(&peer_ni->ksnp_routes)) {
1486 /* I've just closed last conn belonging to a
1487 * peer_ni with no routes to it */
1488 ksocknal_unlink_peer_locked(peer_ni);
1492 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
1494 list_add_tail(&conn->ksnc_list,
1495 &ksocknal_data.ksnd_deathrow_conns);
1496 wake_up(&ksocknal_data.ksnd_reaper_waitq);
1498 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
1502 ksocknal_peer_failed(struct ksock_peer_ni *peer_ni)
1505 time64_t last_alive = 0;
1507 /* There has been a connection failure or comms error; but I'll only
1508 * tell LNET I think the peer_ni is dead if it's to another kernel and
1509 * there are no connections or connection attempts in existence. */
1511 read_lock(&ksocknal_data.ksnd_global_lock);
1513 if ((peer_ni->ksnp_id.pid & LNET_PID_USERFLAG) == 0 &&
1514 list_empty(&peer_ni->ksnp_conns) &&
1515 peer_ni->ksnp_accepting == 0 &&
1516 ksocknal_find_connecting_route_locked(peer_ni) == NULL) {
1518 last_alive = peer_ni->ksnp_last_alive;
1521 read_unlock(&ksocknal_data.ksnd_global_lock);
1524 lnet_notify(peer_ni->ksnp_ni, peer_ni->ksnp_id.nid, 0,
1529 ksocknal_finalize_zcreq(struct ksock_conn *conn)
1531 struct ksock_peer_ni *peer_ni = conn->ksnc_peer;
1532 struct ksock_tx *tx;
1533 struct ksock_tx *tmp;
1534 struct list_head zlist = LIST_HEAD_INIT(zlist);
1536 /* NB safe to finalize TXs because closing of socket will
1537 * abort all buffered data */
1538 LASSERT(conn->ksnc_sock == NULL);
1540 spin_lock(&peer_ni->ksnp_lock);
1542 list_for_each_entry_safe(tx, tmp, &peer_ni->ksnp_zc_req_list, tx_zc_list) {
1543 if (tx->tx_conn != conn)
1546 LASSERT(tx->tx_msg.ksm_zc_cookies[0] != 0);
1548 tx->tx_msg.ksm_zc_cookies[0] = 0;
1549 tx->tx_zc_aborted = 1; /* mark it as not-acked */
1550 list_del(&tx->tx_zc_list);
1551 list_add(&tx->tx_zc_list, &zlist);
1554 spin_unlock(&peer_ni->ksnp_lock);
1556 while (!list_empty(&zlist)) {
1557 tx = list_entry(zlist.next, struct ksock_tx, tx_zc_list);
1559 list_del(&tx->tx_zc_list);
1560 ksocknal_tx_decref(tx);
1565 ksocknal_terminate_conn(struct ksock_conn *conn)
1567 /* This gets called by the reaper (guaranteed thread context) to
1568 * disengage the socket from its callbacks and close it.
1569 * ksnc_refcount will eventually hit zero, and then the reaper will
1571 struct ksock_peer_ni *peer_ni = conn->ksnc_peer;
1572 struct ksock_sched *sched = conn->ksnc_scheduler;
1575 LASSERT(conn->ksnc_closing);
1577 /* wake up the scheduler to "send" all remaining packets to /dev/null */
1578 spin_lock_bh(&sched->kss_lock);
1580 /* a closing conn is always ready to tx */
1581 conn->ksnc_tx_ready = 1;
1583 if (!conn->ksnc_tx_scheduled &&
1584 !list_empty(&conn->ksnc_tx_queue)) {
1585 list_add_tail(&conn->ksnc_tx_list,
1586 &sched->kss_tx_conns);
1587 conn->ksnc_tx_scheduled = 1;
1588 /* extra ref for scheduler */
1589 ksocknal_conn_addref(conn);
1591 wake_up (&sched->kss_waitq);
1594 spin_unlock_bh(&sched->kss_lock);
1596 /* serialise with callbacks */
1597 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1599 ksocknal_lib_reset_callback(conn->ksnc_sock, conn);
1601 /* OK, so this conn may not be completely disengaged from its
1602 * scheduler yet, but it _has_ committed to terminate... */
1603 conn->ksnc_scheduler->kss_nconns--;
1605 if (peer_ni->ksnp_error != 0) {
1606 /* peer_ni's last conn closed in error */
1607 LASSERT(list_empty(&peer_ni->ksnp_conns));
1609 peer_ni->ksnp_error = 0; /* avoid multiple notifications */
1612 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1615 ksocknal_peer_failed(peer_ni);
1617 /* The socket is closed on the final put; either here, or in
1618 * ksocknal_{send,recv}msg(). Since we set up the linger2 option
1619 * when the connection was established, this will close the socket
1620 * immediately, aborting anything buffered in it. Any hung
1621 * zero-copy transmits will therefore complete in finite time. */
1622 ksocknal_connsock_decref(conn);
1626 ksocknal_queue_zombie_conn(struct ksock_conn *conn)
1628 /* Queue the conn for the reaper to destroy */
1629 LASSERT(atomic_read(&conn->ksnc_conn_refcount) == 0);
1630 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
1632 list_add_tail(&conn->ksnc_list, &ksocknal_data.ksnd_zombie_conns);
1633 wake_up(&ksocknal_data.ksnd_reaper_waitq);
1635 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
1639 ksocknal_destroy_conn(struct ksock_conn *conn)
1643 /* Final coup-de-grace of the reaper */
1644 CDEBUG (D_NET, "connection %p\n", conn);
1646 LASSERT (atomic_read (&conn->ksnc_conn_refcount) == 0);
1647 LASSERT (atomic_read (&conn->ksnc_sock_refcount) == 0);
1648 LASSERT (conn->ksnc_sock == NULL);
1649 LASSERT (conn->ksnc_route == NULL);
1650 LASSERT (!conn->ksnc_tx_scheduled);
1651 LASSERT (!conn->ksnc_rx_scheduled);
1652 LASSERT(list_empty(&conn->ksnc_tx_queue));
1654 /* complete current receive if any */
1655 switch (conn->ksnc_rx_state) {
1656 case SOCKNAL_RX_LNET_PAYLOAD:
1657 last_rcv = conn->ksnc_rx_deadline -
1658 lnet_get_lnd_timeout();
1659 CERROR("Completing partial receive from %s[%d], "
1660 "ip %pI4h:%d, with error, wanted: %d, left: %d, "
1661 "last alive is %lld secs ago\n",
1662 libcfs_id2str(conn->ksnc_peer->ksnp_id), conn->ksnc_type,
1663 &conn->ksnc_ipaddr, conn->ksnc_port,
1664 conn->ksnc_rx_nob_wanted, conn->ksnc_rx_nob_left,
1665 ktime_get_seconds() - last_rcv);
1666 if (conn->ksnc_lnet_msg)
1667 conn->ksnc_lnet_msg->msg_health_status =
1668 LNET_MSG_STATUS_REMOTE_ERROR;
1669 lnet_finalize(conn->ksnc_lnet_msg, -EIO);
1671 case SOCKNAL_RX_LNET_HEADER:
1672 if (conn->ksnc_rx_started)
1673 CERROR("Incomplete receive of lnet header from %s, "
1674 "ip %pI4h:%d, with error, protocol: %d.x.\n",
1675 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1676 &conn->ksnc_ipaddr, conn->ksnc_port,
1677 conn->ksnc_proto->pro_version);
1679 case SOCKNAL_RX_KSM_HEADER:
1680 if (conn->ksnc_rx_started)
1681 CERROR("Incomplete receive of ksock message from %s, "
1682 "ip %pI4h:%d, with error, protocol: %d.x.\n",
1683 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1684 &conn->ksnc_ipaddr, conn->ksnc_port,
1685 conn->ksnc_proto->pro_version);
1687 case SOCKNAL_RX_SLOP:
1688 if (conn->ksnc_rx_started)
1689 CERROR("Incomplete receive of slops from %s, "
1690 "ip %pI4h:%d, with error\n",
1691 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1692 &conn->ksnc_ipaddr, conn->ksnc_port);
1699 ksocknal_peer_decref(conn->ksnc_peer);
1701 LIBCFS_FREE (conn, sizeof (*conn));
1705 ksocknal_close_peer_conns_locked(struct ksock_peer_ni *peer_ni, __u32 ipaddr, int why)
1707 struct ksock_conn *conn;
1708 struct list_head *ctmp;
1709 struct list_head *cnxt;
1712 list_for_each_safe(ctmp, cnxt, &peer_ni->ksnp_conns) {
1713 conn = list_entry(ctmp, struct ksock_conn, ksnc_list);
1716 conn->ksnc_ipaddr == ipaddr) {
1718 ksocknal_close_conn_locked (conn, why);
1726 ksocknal_close_conn_and_siblings(struct ksock_conn *conn, int why)
1728 struct ksock_peer_ni *peer_ni = conn->ksnc_peer;
1729 u32 ipaddr = conn->ksnc_ipaddr;
1732 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1734 count = ksocknal_close_peer_conns_locked (peer_ni, ipaddr, why);
1736 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1742 ksocknal_close_matching_conns(struct lnet_process_id id, __u32 ipaddr)
1744 struct ksock_peer_ni *peer_ni;
1745 struct list_head *ptmp;
1746 struct list_head *pnxt;
1752 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1754 if (id.nid != LNET_NID_ANY)
1755 lo = hi = (int)(ksocknal_nid2peerlist(id.nid) - ksocknal_data.ksnd_peers);
1758 hi = ksocknal_data.ksnd_peer_hash_size - 1;
1761 for (i = lo; i <= hi; i++) {
1762 list_for_each_safe(ptmp, pnxt, &ksocknal_data.ksnd_peers[i]) {
1764 peer_ni = list_entry(ptmp, struct ksock_peer_ni, ksnp_list);
1766 if (!((id.nid == LNET_NID_ANY || id.nid == peer_ni->ksnp_id.nid) &&
1767 (id.pid == LNET_PID_ANY || id.pid == peer_ni->ksnp_id.pid)))
1770 count += ksocknal_close_peer_conns_locked (peer_ni, ipaddr, 0);
1774 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1776 /* wildcards always succeed */
1777 if (id.nid == LNET_NID_ANY || id.pid == LNET_PID_ANY || ipaddr == 0)
1780 return (count == 0 ? -ENOENT : 0);
1784 ksocknal_notify(struct lnet_ni *ni, lnet_nid_t gw_nid, int alive)
1786 /* The router is telling me she's been notified of a change in
1789 struct lnet_process_id id = {
1791 .pid = LNET_PID_ANY,
1794 CDEBUG (D_NET, "gw %s %s\n", libcfs_nid2str(gw_nid),
1795 alive ? "up" : "down");
1798 /* If the gateway crashed, close all open connections... */
1799 ksocknal_close_matching_conns (id, 0);
1803 /* ...otherwise do nothing. We can only establish new connections
1804 * if we have autroutes, and these connect on demand. */
1808 ksocknal_query(struct lnet_ni *ni, lnet_nid_t nid, time64_t *when)
1811 time64_t last_alive = 0;
1812 time64_t now = ktime_get_seconds();
1813 struct ksock_peer_ni *peer_ni = NULL;
1814 rwlock_t *glock = &ksocknal_data.ksnd_global_lock;
1815 struct lnet_process_id id = {
1817 .pid = LNET_PID_LUSTRE,
1822 peer_ni = ksocknal_find_peer_locked(ni, id);
1823 if (peer_ni != NULL) {
1824 struct list_head *tmp;
1825 struct ksock_conn *conn;
1828 list_for_each(tmp, &peer_ni->ksnp_conns) {
1829 conn = list_entry(tmp, struct ksock_conn, ksnc_list);
1830 bufnob = conn->ksnc_sock->sk->sk_wmem_queued;
1832 if (bufnob < conn->ksnc_tx_bufnob) {
1833 /* something got ACKed */
1834 conn->ksnc_tx_deadline = ktime_get_seconds() +
1835 lnet_get_lnd_timeout();
1836 peer_ni->ksnp_last_alive = now;
1837 conn->ksnc_tx_bufnob = bufnob;
1841 last_alive = peer_ni->ksnp_last_alive;
1842 if (ksocknal_find_connectable_route_locked(peer_ni) == NULL)
1848 if (last_alive != 0)
1851 CDEBUG(D_NET, "peer_ni %s %p, alive %lld secs ago, connect %d\n",
1852 libcfs_nid2str(nid), peer_ni,
1853 last_alive ? now - last_alive : -1,
1859 ksocknal_add_peer(ni, id, LNET_NIDADDR(nid), lnet_acceptor_port());
1861 write_lock_bh(glock);
1863 peer_ni = ksocknal_find_peer_locked(ni, id);
1864 if (peer_ni != NULL)
1865 ksocknal_launch_all_connections_locked(peer_ni);
1867 write_unlock_bh(glock);
1872 ksocknal_push_peer(struct ksock_peer_ni *peer_ni)
1876 struct list_head *tmp;
1877 struct ksock_conn *conn;
1879 for (index = 0; ; index++) {
1880 read_lock(&ksocknal_data.ksnd_global_lock);
1885 list_for_each(tmp, &peer_ni->ksnp_conns) {
1887 conn = list_entry(tmp, struct ksock_conn,
1889 ksocknal_conn_addref(conn);
1894 read_unlock(&ksocknal_data.ksnd_global_lock);
1899 ksocknal_lib_push_conn (conn);
1900 ksocknal_conn_decref(conn);
1905 ksocknal_push(struct lnet_ni *ni, struct lnet_process_id id)
1907 struct list_head *start;
1908 struct list_head *end;
1909 struct list_head *tmp;
1911 unsigned int hsize = ksocknal_data.ksnd_peer_hash_size;
1913 if (id.nid == LNET_NID_ANY) {
1914 start = &ksocknal_data.ksnd_peers[0];
1915 end = &ksocknal_data.ksnd_peers[hsize - 1];
1917 start = end = ksocknal_nid2peerlist(id.nid);
1920 for (tmp = start; tmp <= end; tmp++) {
1921 int peer_off; /* searching offset in peer_ni hash table */
1923 for (peer_off = 0; ; peer_off++) {
1924 struct ksock_peer_ni *peer_ni;
1927 read_lock(&ksocknal_data.ksnd_global_lock);
1928 list_for_each_entry(peer_ni, tmp, ksnp_list) {
1929 if (!((id.nid == LNET_NID_ANY ||
1930 id.nid == peer_ni->ksnp_id.nid) &&
1931 (id.pid == LNET_PID_ANY ||
1932 id.pid == peer_ni->ksnp_id.pid)))
1935 if (i++ == peer_off) {
1936 ksocknal_peer_addref(peer_ni);
1940 read_unlock(&ksocknal_data.ksnd_global_lock);
1942 if (i == 0) /* no match */
1946 ksocknal_push_peer(peer_ni);
1947 ksocknal_peer_decref(peer_ni);
1954 ksocknal_add_interface(struct lnet_ni *ni, __u32 ipaddress, __u32 netmask)
1956 struct ksock_net *net = ni->ni_data;
1957 struct ksock_interface *iface;
1961 struct list_head *ptmp;
1962 struct ksock_peer_ni *peer_ni;
1963 struct list_head *rtmp;
1964 struct ksock_route *route;
1966 if (ipaddress == 0 ||
1970 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1972 iface = ksocknal_ip2iface(ni, ipaddress);
1973 if (iface != NULL) {
1974 /* silently ignore dups */
1976 } else if (net->ksnn_ninterfaces == LNET_INTERFACES_NUM) {
1979 iface = &net->ksnn_interfaces[net->ksnn_ninterfaces++];
1981 iface->ksni_ipaddr = ipaddress;
1982 iface->ksni_netmask = netmask;
1983 iface->ksni_nroutes = 0;
1984 iface->ksni_npeers = 0;
1986 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
1987 list_for_each(ptmp, &ksocknal_data.ksnd_peers[i]) {
1988 peer_ni = list_entry(ptmp, struct ksock_peer_ni,
1991 for (j = 0; j < peer_ni->ksnp_n_passive_ips; j++)
1992 if (peer_ni->ksnp_passive_ips[j] == ipaddress)
1993 iface->ksni_npeers++;
1995 list_for_each(rtmp, &peer_ni->ksnp_routes) {
1996 route = list_entry(rtmp,
2000 if (route->ksnr_myipaddr == ipaddress)
2001 iface->ksni_nroutes++;
2007 /* NB only new connections will pay attention to the new interface! */
2010 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
2016 ksocknal_peer_del_interface_locked(struct ksock_peer_ni *peer_ni, __u32 ipaddr)
2018 struct list_head *tmp;
2019 struct list_head *nxt;
2020 struct ksock_route *route;
2021 struct ksock_conn *conn;
2025 for (i = 0; i < peer_ni->ksnp_n_passive_ips; i++)
2026 if (peer_ni->ksnp_passive_ips[i] == ipaddr) {
2027 for (j = i+1; j < peer_ni->ksnp_n_passive_ips; j++)
2028 peer_ni->ksnp_passive_ips[j-1] =
2029 peer_ni->ksnp_passive_ips[j];
2030 peer_ni->ksnp_n_passive_ips--;
2034 list_for_each_safe(tmp, nxt, &peer_ni->ksnp_routes) {
2035 route = list_entry(tmp, struct ksock_route, ksnr_list);
2037 if (route->ksnr_myipaddr != ipaddr)
2040 if (route->ksnr_share_count != 0) {
2041 /* Manually created; keep, but unbind */
2042 route->ksnr_myipaddr = 0;
2044 ksocknal_del_route_locked(route);
2048 list_for_each_safe(tmp, nxt, &peer_ni->ksnp_conns) {
2049 conn = list_entry(tmp, struct ksock_conn, ksnc_list);
2051 if (conn->ksnc_myipaddr == ipaddr)
2052 ksocknal_close_conn_locked (conn, 0);
2057 ksocknal_del_interface(struct lnet_ni *ni, __u32 ipaddress)
2059 struct ksock_net *net = ni->ni_data;
2061 struct list_head *tmp;
2062 struct list_head *nxt;
2063 struct ksock_peer_ni *peer_ni;
2068 write_lock_bh(&ksocknal_data.ksnd_global_lock);
2070 for (i = 0; i < net->ksnn_ninterfaces; i++) {
2071 this_ip = net->ksnn_interfaces[i].ksni_ipaddr;
2073 if (!(ipaddress == 0 ||
2074 ipaddress == this_ip))
2079 for (j = i+1; j < net->ksnn_ninterfaces; j++)
2080 net->ksnn_interfaces[j-1] =
2081 net->ksnn_interfaces[j];
2083 net->ksnn_ninterfaces--;
2085 for (j = 0; j < ksocknal_data.ksnd_peer_hash_size; j++) {
2086 list_for_each_safe(tmp, nxt,
2087 &ksocknal_data.ksnd_peers[j]) {
2088 peer_ni = list_entry(tmp, struct ksock_peer_ni,
2091 if (peer_ni->ksnp_ni != ni)
2094 ksocknal_peer_del_interface_locked(peer_ni, this_ip);
2099 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
2105 ksocknal_ctl(struct lnet_ni *ni, unsigned int cmd, void *arg)
2107 struct lnet_process_id id = {0};
2108 struct libcfs_ioctl_data *data = arg;
2112 case IOC_LIBCFS_GET_INTERFACE: {
2113 struct ksock_net *net = ni->ni_data;
2114 struct ksock_interface *iface;
2116 read_lock(&ksocknal_data.ksnd_global_lock);
2118 if (data->ioc_count >= (__u32)net->ksnn_ninterfaces) {
2122 iface = &net->ksnn_interfaces[data->ioc_count];
2124 data->ioc_u32[0] = iface->ksni_ipaddr;
2125 data->ioc_u32[1] = iface->ksni_netmask;
2126 data->ioc_u32[2] = iface->ksni_npeers;
2127 data->ioc_u32[3] = iface->ksni_nroutes;
2130 read_unlock(&ksocknal_data.ksnd_global_lock);
2134 case IOC_LIBCFS_ADD_INTERFACE:
2135 return ksocknal_add_interface(ni,
2136 data->ioc_u32[0], /* IP address */
2137 data->ioc_u32[1]); /* net mask */
2139 case IOC_LIBCFS_DEL_INTERFACE:
2140 return ksocknal_del_interface(ni,
2141 data->ioc_u32[0]); /* IP address */
2143 case IOC_LIBCFS_GET_PEER: {
2148 int share_count = 0;
2150 rc = ksocknal_get_peer_info(ni, data->ioc_count,
2151 &id, &myip, &ip, &port,
2152 &conn_count, &share_count);
2156 data->ioc_nid = id.nid;
2157 data->ioc_count = share_count;
2158 data->ioc_u32[0] = ip;
2159 data->ioc_u32[1] = port;
2160 data->ioc_u32[2] = myip;
2161 data->ioc_u32[3] = conn_count;
2162 data->ioc_u32[4] = id.pid;
2166 case IOC_LIBCFS_ADD_PEER:
2167 id.nid = data->ioc_nid;
2168 id.pid = LNET_PID_LUSTRE;
2169 return ksocknal_add_peer (ni, id,
2170 data->ioc_u32[0], /* IP */
2171 data->ioc_u32[1]); /* port */
2173 case IOC_LIBCFS_DEL_PEER:
2174 id.nid = data->ioc_nid;
2175 id.pid = LNET_PID_ANY;
2176 return ksocknal_del_peer (ni, id,
2177 data->ioc_u32[0]); /* IP */
2179 case IOC_LIBCFS_GET_CONN: {
2183 struct ksock_conn *conn = ksocknal_get_conn_by_idx(ni, data->ioc_count);
2188 ksocknal_lib_get_conn_tunables(conn, &txmem, &rxmem, &nagle);
2190 data->ioc_count = txmem;
2191 data->ioc_nid = conn->ksnc_peer->ksnp_id.nid;
2192 data->ioc_flags = nagle;
2193 data->ioc_u32[0] = conn->ksnc_ipaddr;
2194 data->ioc_u32[1] = conn->ksnc_port;
2195 data->ioc_u32[2] = conn->ksnc_myipaddr;
2196 data->ioc_u32[3] = conn->ksnc_type;
2197 data->ioc_u32[4] = conn->ksnc_scheduler->kss_cpt;
2198 data->ioc_u32[5] = rxmem;
2199 data->ioc_u32[6] = conn->ksnc_peer->ksnp_id.pid;
2200 ksocknal_conn_decref(conn);
2204 case IOC_LIBCFS_CLOSE_CONNECTION:
2205 id.nid = data->ioc_nid;
2206 id.pid = LNET_PID_ANY;
2207 return ksocknal_close_matching_conns (id,
2210 case IOC_LIBCFS_REGISTER_MYNID:
2211 /* Ignore if this is a noop */
2212 if (data->ioc_nid == ni->ni_nid)
2215 CERROR("obsolete IOC_LIBCFS_REGISTER_MYNID: %s(%s)\n",
2216 libcfs_nid2str(data->ioc_nid),
2217 libcfs_nid2str(ni->ni_nid));
2220 case IOC_LIBCFS_PUSH_CONNECTION:
2221 id.nid = data->ioc_nid;
2222 id.pid = LNET_PID_ANY;
2223 return ksocknal_push(ni, id);
2232 ksocknal_free_buffers (void)
2234 LASSERT (atomic_read(&ksocknal_data.ksnd_nactive_txs) == 0);
2236 if (ksocknal_data.ksnd_schedulers != NULL)
2237 cfs_percpt_free(ksocknal_data.ksnd_schedulers);
2239 LIBCFS_FREE (ksocknal_data.ksnd_peers,
2240 sizeof(struct list_head) *
2241 ksocknal_data.ksnd_peer_hash_size);
2243 spin_lock(&ksocknal_data.ksnd_tx_lock);
2245 if (!list_empty(&ksocknal_data.ksnd_idle_noop_txs)) {
2246 struct list_head zlist;
2247 struct ksock_tx *tx;
2249 list_add(&zlist, &ksocknal_data.ksnd_idle_noop_txs);
2250 list_del_init(&ksocknal_data.ksnd_idle_noop_txs);
2251 spin_unlock(&ksocknal_data.ksnd_tx_lock);
2253 while (!list_empty(&zlist)) {
2254 tx = list_entry(zlist.next, struct ksock_tx, tx_list);
2255 list_del(&tx->tx_list);
2256 LIBCFS_FREE(tx, tx->tx_desc_size);
2259 spin_unlock(&ksocknal_data.ksnd_tx_lock);
2264 ksocknal_base_shutdown(void)
2266 struct ksock_sched *sched;
2269 CDEBUG(D_MALLOC, "before NAL cleanup: kmem %d\n",
2270 atomic_read (&libcfs_kmemory));
2271 LASSERT (ksocknal_data.ksnd_nnets == 0);
2273 switch (ksocknal_data.ksnd_init) {
2277 case SOCKNAL_INIT_ALL:
2278 case SOCKNAL_INIT_DATA:
2279 LASSERT (ksocknal_data.ksnd_peers != NULL);
2280 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
2281 LASSERT(list_empty(&ksocknal_data.ksnd_peers[i]));
2284 LASSERT(list_empty(&ksocknal_data.ksnd_nets));
2285 LASSERT(list_empty(&ksocknal_data.ksnd_enomem_conns));
2286 LASSERT(list_empty(&ksocknal_data.ksnd_zombie_conns));
2287 LASSERT(list_empty(&ksocknal_data.ksnd_connd_connreqs));
2288 LASSERT(list_empty(&ksocknal_data.ksnd_connd_routes));
2290 if (ksocknal_data.ksnd_schedulers != NULL) {
2291 cfs_percpt_for_each(sched, i,
2292 ksocknal_data.ksnd_schedulers) {
2294 LASSERT(list_empty(&sched->kss_tx_conns));
2295 LASSERT(list_empty(&sched->kss_rx_conns));
2296 LASSERT(list_empty(&sched->kss_zombie_noop_txs));
2297 LASSERT(sched->kss_nconns == 0);
2301 /* flag threads to terminate; wake and wait for them to die */
2302 ksocknal_data.ksnd_shuttingdown = 1;
2303 wake_up_all(&ksocknal_data.ksnd_connd_waitq);
2304 wake_up_all(&ksocknal_data.ksnd_reaper_waitq);
2306 if (ksocknal_data.ksnd_schedulers != NULL) {
2307 cfs_percpt_for_each(sched, i,
2308 ksocknal_data.ksnd_schedulers)
2309 wake_up_all(&sched->kss_waitq);
2313 read_lock(&ksocknal_data.ksnd_global_lock);
2314 while (ksocknal_data.ksnd_nthreads != 0) {
2317 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
2318 "waiting for %d threads to terminate\n",
2319 ksocknal_data.ksnd_nthreads);
2320 read_unlock(&ksocknal_data.ksnd_global_lock);
2321 set_current_state(TASK_UNINTERRUPTIBLE);
2322 schedule_timeout(cfs_time_seconds(1));
2323 read_lock(&ksocknal_data.ksnd_global_lock);
2325 read_unlock(&ksocknal_data.ksnd_global_lock);
2327 ksocknal_free_buffers();
2329 ksocknal_data.ksnd_init = SOCKNAL_INIT_NOTHING;
2333 CDEBUG(D_MALLOC, "after NAL cleanup: kmem %d\n",
2334 atomic_read (&libcfs_kmemory));
2336 module_put(THIS_MODULE);
2340 ksocknal_base_startup(void)
2342 struct ksock_sched *sched;
2346 LASSERT (ksocknal_data.ksnd_init == SOCKNAL_INIT_NOTHING);
2347 LASSERT (ksocknal_data.ksnd_nnets == 0);
2349 memset (&ksocknal_data, 0, sizeof (ksocknal_data)); /* zero pointers */
2351 ksocknal_data.ksnd_peer_hash_size = SOCKNAL_PEER_HASH_SIZE;
2352 LIBCFS_ALLOC(ksocknal_data.ksnd_peers,
2353 sizeof(struct list_head) *
2354 ksocknal_data.ksnd_peer_hash_size);
2355 if (ksocknal_data.ksnd_peers == NULL)
2358 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++)
2359 INIT_LIST_HEAD(&ksocknal_data.ksnd_peers[i]);
2361 rwlock_init(&ksocknal_data.ksnd_global_lock);
2362 INIT_LIST_HEAD(&ksocknal_data.ksnd_nets);
2364 spin_lock_init(&ksocknal_data.ksnd_reaper_lock);
2365 INIT_LIST_HEAD(&ksocknal_data.ksnd_enomem_conns);
2366 INIT_LIST_HEAD(&ksocknal_data.ksnd_zombie_conns);
2367 INIT_LIST_HEAD(&ksocknal_data.ksnd_deathrow_conns);
2368 init_waitqueue_head(&ksocknal_data.ksnd_reaper_waitq);
2370 spin_lock_init(&ksocknal_data.ksnd_connd_lock);
2371 INIT_LIST_HEAD(&ksocknal_data.ksnd_connd_connreqs);
2372 INIT_LIST_HEAD(&ksocknal_data.ksnd_connd_routes);
2373 init_waitqueue_head(&ksocknal_data.ksnd_connd_waitq);
2375 spin_lock_init(&ksocknal_data.ksnd_tx_lock);
2376 INIT_LIST_HEAD(&ksocknal_data.ksnd_idle_noop_txs);
2378 /* NB memset above zeros whole of ksocknal_data */
2380 /* flag lists/ptrs/locks initialised */
2381 ksocknal_data.ksnd_init = SOCKNAL_INIT_DATA;
2382 try_module_get(THIS_MODULE);
2384 /* Create a scheduler block per available CPT */
2385 ksocknal_data.ksnd_schedulers = cfs_percpt_alloc(lnet_cpt_table(),
2387 if (ksocknal_data.ksnd_schedulers == NULL)
2390 cfs_percpt_for_each(sched, i, ksocknal_data.ksnd_schedulers) {
2394 * make sure not to allocate more threads than there are
2395 * cores/CPUs in teh CPT
2397 nthrs = cfs_cpt_weight(lnet_cpt_table(), i);
2398 if (*ksocknal_tunables.ksnd_nscheds > 0) {
2399 nthrs = min(nthrs, *ksocknal_tunables.ksnd_nscheds);
2402 * max to half of CPUs, assume another half should be
2403 * reserved for upper layer modules
2405 nthrs = min(max(SOCKNAL_NSCHEDS, nthrs >> 1), nthrs);
2408 sched->kss_nthreads_max = nthrs;
2411 spin_lock_init(&sched->kss_lock);
2412 INIT_LIST_HEAD(&sched->kss_rx_conns);
2413 INIT_LIST_HEAD(&sched->kss_tx_conns);
2414 INIT_LIST_HEAD(&sched->kss_zombie_noop_txs);
2415 init_waitqueue_head(&sched->kss_waitq);
2418 ksocknal_data.ksnd_connd_starting = 0;
2419 ksocknal_data.ksnd_connd_failed_stamp = 0;
2420 ksocknal_data.ksnd_connd_starting_stamp = ktime_get_real_seconds();
2421 /* must have at least 2 connds to remain responsive to accepts while
2423 if (*ksocknal_tunables.ksnd_nconnds < SOCKNAL_CONND_RESV + 1)
2424 *ksocknal_tunables.ksnd_nconnds = SOCKNAL_CONND_RESV + 1;
2426 if (*ksocknal_tunables.ksnd_nconnds_max <
2427 *ksocknal_tunables.ksnd_nconnds) {
2428 ksocknal_tunables.ksnd_nconnds_max =
2429 ksocknal_tunables.ksnd_nconnds;
2432 for (i = 0; i < *ksocknal_tunables.ksnd_nconnds; i++) {
2434 spin_lock_bh(&ksocknal_data.ksnd_connd_lock);
2435 ksocknal_data.ksnd_connd_starting++;
2436 spin_unlock_bh(&ksocknal_data.ksnd_connd_lock);
2439 snprintf(name, sizeof(name), "socknal_cd%02d", i);
2440 rc = ksocknal_thread_start(ksocknal_connd,
2441 (void *)((uintptr_t)i), name);
2443 spin_lock_bh(&ksocknal_data.ksnd_connd_lock);
2444 ksocknal_data.ksnd_connd_starting--;
2445 spin_unlock_bh(&ksocknal_data.ksnd_connd_lock);
2446 CERROR("Can't spawn socknal connd: %d\n", rc);
2451 rc = ksocknal_thread_start(ksocknal_reaper, NULL, "socknal_reaper");
2453 CERROR ("Can't spawn socknal reaper: %d\n", rc);
2457 /* flag everything initialised */
2458 ksocknal_data.ksnd_init = SOCKNAL_INIT_ALL;
2463 ksocknal_base_shutdown();
2468 ksocknal_debug_peerhash(struct lnet_ni *ni)
2470 struct ksock_peer_ni *peer_ni = NULL;
2471 struct list_head *tmp;
2474 read_lock(&ksocknal_data.ksnd_global_lock);
2476 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
2477 list_for_each(tmp, &ksocknal_data.ksnd_peers[i]) {
2478 peer_ni = list_entry(tmp, struct ksock_peer_ni, ksnp_list);
2480 if (peer_ni->ksnp_ni == ni) break;
2486 if (peer_ni != NULL) {
2487 struct ksock_route *route;
2488 struct ksock_conn *conn;
2490 CWARN ("Active peer_ni on shutdown: %s, ref %d, scnt %d, "
2491 "closing %d, accepting %d, err %d, zcookie %llu, "
2492 "txq %d, zc_req %d\n", libcfs_id2str(peer_ni->ksnp_id),
2493 atomic_read(&peer_ni->ksnp_refcount),
2494 peer_ni->ksnp_sharecount, peer_ni->ksnp_closing,
2495 peer_ni->ksnp_accepting, peer_ni->ksnp_error,
2496 peer_ni->ksnp_zc_next_cookie,
2497 !list_empty(&peer_ni->ksnp_tx_queue),
2498 !list_empty(&peer_ni->ksnp_zc_req_list));
2500 list_for_each(tmp, &peer_ni->ksnp_routes) {
2501 route = list_entry(tmp, struct ksock_route, ksnr_list);
2502 CWARN ("Route: ref %d, schd %d, conn %d, cnted %d, "
2503 "del %d\n", atomic_read(&route->ksnr_refcount),
2504 route->ksnr_scheduled, route->ksnr_connecting,
2505 route->ksnr_connected, route->ksnr_deleted);
2508 list_for_each(tmp, &peer_ni->ksnp_conns) {
2509 conn = list_entry(tmp, struct ksock_conn, ksnc_list);
2510 CWARN ("Conn: ref %d, sref %d, t %d, c %d\n",
2511 atomic_read(&conn->ksnc_conn_refcount),
2512 atomic_read(&conn->ksnc_sock_refcount),
2513 conn->ksnc_type, conn->ksnc_closing);
2517 read_unlock(&ksocknal_data.ksnd_global_lock);
2522 ksocknal_shutdown(struct lnet_ni *ni)
2524 struct ksock_net *net = ni->ni_data;
2525 struct lnet_process_id anyid = {
2526 .nid = LNET_NID_ANY,
2527 .pid = LNET_PID_ANY,
2531 LASSERT(ksocknal_data.ksnd_init == SOCKNAL_INIT_ALL);
2532 LASSERT(ksocknal_data.ksnd_nnets > 0);
2534 spin_lock_bh(&net->ksnn_lock);
2535 net->ksnn_shutdown = 1; /* prevent new peers */
2536 spin_unlock_bh(&net->ksnn_lock);
2538 /* Delete all peers */
2539 ksocknal_del_peer(ni, anyid, 0);
2541 /* Wait for all peer_ni state to clean up */
2543 spin_lock_bh(&net->ksnn_lock);
2544 while (net->ksnn_npeers != 0) {
2545 spin_unlock_bh(&net->ksnn_lock);
2548 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET, /* power of 2? */
2549 "waiting for %d peers to disconnect\n",
2551 set_current_state(TASK_UNINTERRUPTIBLE);
2552 schedule_timeout(cfs_time_seconds(1));
2554 ksocknal_debug_peerhash(ni);
2556 spin_lock_bh(&net->ksnn_lock);
2558 spin_unlock_bh(&net->ksnn_lock);
2560 for (i = 0; i < net->ksnn_ninterfaces; i++) {
2561 LASSERT (net->ksnn_interfaces[i].ksni_npeers == 0);
2562 LASSERT (net->ksnn_interfaces[i].ksni_nroutes == 0);
2565 list_del(&net->ksnn_list);
2566 LIBCFS_FREE(net, sizeof(*net));
2568 ksocknal_data.ksnd_nnets--;
2569 if (ksocknal_data.ksnd_nnets == 0)
2570 ksocknal_base_shutdown();
2574 ksocknal_enumerate_interfaces(struct ksock_net *net)
2577 struct net_device *dev;
2580 for_each_netdev(&init_net, dev) {
2581 const char *name = dev->name;
2582 struct in_device *in_dev;
2583 struct ksock_interface *ksi;
2585 if (strcmp(name, "lo") == 0) /* skip the loopback IF */
2588 if (!(dev_get_flags(dev) & IFF_UP)) {
2589 CWARN("Ignoring interface %s (down)\n", name);
2593 if (j == LNET_INTERFACES_NUM) {
2594 CWARN("Ignoring interface %s (too many interfaces)\n",
2599 in_dev = __in_dev_get_rtnl(dev);
2601 CWARN("Interface %s has no IPv4 status.\n", name);
2605 ksi = &net->ksnn_interfaces[j];
2607 for_primary_ifa(in_dev)
2608 if (strcmp(ifa->ifa_label, name) == 0) {
2609 ksi->ksni_ipaddr = ntohl(ifa->ifa_local);
2610 ksi->ksni_netmask = ifa->ifa_mask;
2611 strlcpy(ksi->ksni_name,
2612 name, sizeof(ksi->ksni_name));
2622 CERROR("Can't find any usable interfaces\n");
2628 ksocknal_search_new_ipif(struct ksock_net *net)
2633 for (i = 0; i < net->ksnn_ninterfaces; i++) {
2634 char *ifnam = &net->ksnn_interfaces[i].ksni_name[0];
2635 char *colon = strchr(ifnam, ':');
2637 struct ksock_net *tmp;
2640 if (colon != NULL) /* ignore alias device */
2643 list_for_each_entry(tmp, &ksocknal_data.ksnd_nets,
2645 for (j = 0; !found && j < tmp->ksnn_ninterfaces; j++) {
2646 char *ifnam2 = &tmp->ksnn_interfaces[j].\
2648 char *colon2 = strchr(ifnam2, ':');
2653 found = strcmp(ifnam, ifnam2) == 0;
2670 ksocknal_start_schedulers(struct ksock_sched *sched)
2676 if (sched->kss_nthreads == 0) {
2677 if (*ksocknal_tunables.ksnd_nscheds > 0) {
2678 nthrs = sched->kss_nthreads_max;
2680 nthrs = cfs_cpt_weight(lnet_cpt_table(),
2682 nthrs = min(max(SOCKNAL_NSCHEDS, nthrs >> 1), nthrs);
2683 nthrs = min(SOCKNAL_NSCHEDS_HIGH, nthrs);
2685 nthrs = min(nthrs, sched->kss_nthreads_max);
2687 LASSERT(sched->kss_nthreads <= sched->kss_nthreads_max);
2688 /* increase two threads if there is new interface */
2689 nthrs = min(2, sched->kss_nthreads_max - sched->kss_nthreads);
2692 for (i = 0; i < nthrs; i++) {
2696 id = KSOCK_THREAD_ID(sched->kss_cpt, sched->kss_nthreads + i);
2697 snprintf(name, sizeof(name), "socknal_sd%02d_%02d",
2698 sched->kss_cpt, (int)KSOCK_THREAD_SID(id));
2700 rc = ksocknal_thread_start(ksocknal_scheduler,
2705 CERROR("Can't spawn thread %d for scheduler[%d]: %d\n",
2706 sched->kss_cpt, (int) KSOCK_THREAD_SID(id), rc);
2710 sched->kss_nthreads += i;
2715 ksocknal_net_start_threads(struct ksock_net *net, __u32 *cpts, int ncpts)
2717 int newif = ksocknal_search_new_ipif(net);
2721 if (ncpts > 0 && ncpts > cfs_cpt_number(lnet_cpt_table()))
2724 for (i = 0; i < ncpts; i++) {
2725 struct ksock_sched *sched;
2726 int cpt = (cpts == NULL) ? i : cpts[i];
2728 LASSERT(cpt < cfs_cpt_number(lnet_cpt_table()));
2729 sched = ksocknal_data.ksnd_schedulers[cpt];
2731 if (!newif && sched->kss_nthreads > 0)
2734 rc = ksocknal_start_schedulers(sched);
2742 ksocknal_startup(struct lnet_ni *ni)
2744 struct ksock_net *net;
2745 struct lnet_ioctl_config_lnd_cmn_tunables *net_tunables;
2748 struct net_device *net_dev;
2751 LASSERT (ni->ni_net->net_lnd == &the_ksocklnd);
2753 if (ksocknal_data.ksnd_init == SOCKNAL_INIT_NOTHING) {
2754 rc = ksocknal_base_startup();
2759 LIBCFS_ALLOC(net, sizeof(*net));
2763 spin_lock_init(&net->ksnn_lock);
2764 net->ksnn_incarnation = ktime_get_real_ns();
2766 net_tunables = &ni->ni_net->net_tunables;
2768 if (net_tunables->lct_peer_timeout == -1)
2769 net_tunables->lct_peer_timeout =
2770 *ksocknal_tunables.ksnd_peertimeout;
2772 if (net_tunables->lct_max_tx_credits == -1)
2773 net_tunables->lct_max_tx_credits =
2774 *ksocknal_tunables.ksnd_credits;
2776 if (net_tunables->lct_peer_tx_credits == -1)
2777 net_tunables->lct_peer_tx_credits =
2778 *ksocknal_tunables.ksnd_peertxcredits;
2780 if (net_tunables->lct_peer_tx_credits >
2781 net_tunables->lct_max_tx_credits)
2782 net_tunables->lct_peer_tx_credits =
2783 net_tunables->lct_max_tx_credits;
2785 if (net_tunables->lct_peer_rtr_credits == -1)
2786 net_tunables->lct_peer_rtr_credits =
2787 *ksocknal_tunables.ksnd_peerrtrcredits;
2789 if (ni->ni_interfaces[0] == NULL) {
2790 rc = ksocknal_enumerate_interfaces(net);
2794 net->ksnn_ninterfaces = 1;
2796 for (i = 0; i < LNET_INTERFACES_NUM; i++) {
2799 if (ni->ni_interfaces[i] == NULL)
2802 rc = lnet_ipif_query(ni->ni_interfaces[i], &up,
2803 &net->ksnn_interfaces[i].ksni_ipaddr,
2804 &net->ksnn_interfaces[i].ksni_netmask);
2807 CERROR("Can't get interface %s info: %d\n",
2808 ni->ni_interfaces[i], rc);
2813 CERROR("Interface %s is down\n",
2814 ni->ni_interfaces[i]);
2818 strlcpy(net->ksnn_interfaces[i].ksni_name,
2819 ni->ni_interfaces[i],
2820 sizeof(net->ksnn_interfaces[i].ksni_name));
2823 net->ksnn_ninterfaces = i;
2826 net_dev = dev_get_by_name(&init_net,
2827 net->ksnn_interfaces[0].ksni_name);
2828 if (net_dev != NULL) {
2829 node_id = dev_to_node(&net_dev->dev);
2830 ni->ni_dev_cpt = cfs_cpt_of_node(lnet_cpt_table(), node_id);
2833 ni->ni_dev_cpt = CFS_CPT_ANY;
2836 /* call it before add it to ksocknal_data.ksnd_nets */
2837 rc = ksocknal_net_start_threads(net, ni->ni_cpts, ni->ni_ncpts);
2841 ni->ni_nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid),
2842 net->ksnn_interfaces[0].ksni_ipaddr);
2843 list_add(&net->ksnn_list, &ksocknal_data.ksnd_nets);
2845 ksocknal_data.ksnd_nnets++;
2850 LIBCFS_FREE(net, sizeof(*net));
2852 if (ksocknal_data.ksnd_nnets == 0)
2853 ksocknal_base_shutdown();
2859 static void __exit ksocklnd_exit(void)
2861 lnet_unregister_lnd(&the_ksocklnd);
2864 static int __init ksocklnd_init(void)
2868 /* check ksnr_connected/connecting field large enough */
2869 CLASSERT(SOCKLND_CONN_NTYPES <= 4);
2870 CLASSERT(SOCKLND_CONN_ACK == SOCKLND_CONN_BULK_IN);
2872 /* initialize the_ksocklnd */
2873 the_ksocklnd.lnd_type = SOCKLND;
2874 the_ksocklnd.lnd_startup = ksocknal_startup;
2875 the_ksocklnd.lnd_shutdown = ksocknal_shutdown;
2876 the_ksocklnd.lnd_ctl = ksocknal_ctl;
2877 the_ksocklnd.lnd_send = ksocknal_send;
2878 the_ksocklnd.lnd_recv = ksocknal_recv;
2879 the_ksocklnd.lnd_notify = ksocknal_notify;
2880 the_ksocklnd.lnd_query = ksocknal_query;
2881 the_ksocklnd.lnd_accept = ksocknal_accept;
2883 rc = ksocknal_tunables_init();
2887 lnet_register_lnd(&the_ksocklnd);
2892 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2893 MODULE_DESCRIPTION("TCP Socket LNet Network Driver");
2894 MODULE_VERSION("2.8.0");
2895 MODULE_LICENSE("GPL");
2897 module_init(ksocklnd_init);
2898 module_exit(ksocklnd_exit);