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 const 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));
99 static struct ksock_peer_ni *
100 ksocknal_create_peer(struct lnet_ni *ni, struct lnet_process_id id)
102 int cpt = lnet_cpt_of_nid(id.nid, ni);
103 struct ksock_net *net = ni->ni_data;
104 struct ksock_peer_ni *peer_ni;
106 LASSERT(id.nid != LNET_NID_ANY);
107 LASSERT(id.pid != LNET_PID_ANY);
108 LASSERT(!in_interrupt());
110 if (!atomic_inc_unless_negative(&net->ksnn_npeers)) {
111 CERROR("Can't create peer_ni: network shutdown\n");
112 return ERR_PTR(-ESHUTDOWN);
115 LIBCFS_CPT_ALLOC(peer_ni, lnet_cpt_table(), cpt, sizeof(*peer_ni));
117 atomic_dec(&net->ksnn_npeers);
118 return ERR_PTR(-ENOMEM);
121 peer_ni->ksnp_ni = ni;
122 peer_ni->ksnp_id = id;
123 atomic_set(&peer_ni->ksnp_refcount, 1); /* 1 ref for caller */
124 peer_ni->ksnp_closing = 0;
125 peer_ni->ksnp_accepting = 0;
126 peer_ni->ksnp_proto = NULL;
127 peer_ni->ksnp_last_alive = 0;
128 peer_ni->ksnp_zc_next_cookie = SOCKNAL_KEEPALIVE_PING + 1;
130 INIT_LIST_HEAD(&peer_ni->ksnp_conns);
131 INIT_LIST_HEAD(&peer_ni->ksnp_routes);
132 INIT_LIST_HEAD(&peer_ni->ksnp_tx_queue);
133 INIT_LIST_HEAD(&peer_ni->ksnp_zc_req_list);
134 spin_lock_init(&peer_ni->ksnp_lock);
140 ksocknal_destroy_peer(struct ksock_peer_ni *peer_ni)
142 struct ksock_net *net = peer_ni->ksnp_ni->ni_data;
144 CDEBUG (D_NET, "peer_ni %s %p deleted\n",
145 libcfs_id2str(peer_ni->ksnp_id), peer_ni);
147 LASSERT(atomic_read(&peer_ni->ksnp_refcount) == 0);
148 LASSERT(peer_ni->ksnp_accepting == 0);
149 LASSERT(list_empty(&peer_ni->ksnp_conns));
150 LASSERT(list_empty(&peer_ni->ksnp_routes));
151 LASSERT(list_empty(&peer_ni->ksnp_tx_queue));
152 LASSERT(list_empty(&peer_ni->ksnp_zc_req_list));
154 LIBCFS_FREE(peer_ni, sizeof(*peer_ni));
156 /* NB a peer_ni's connections and routes keep a reference on their
157 * peer_ni until they are destroyed, so we can be assured that _all_
158 * state to do with this peer_ni has been cleaned up when its refcount
161 atomic_dec(&net->ksnn_npeers);
164 struct ksock_peer_ni *
165 ksocknal_find_peer_locked(struct lnet_ni *ni, struct lnet_process_id id)
167 struct ksock_peer_ni *peer_ni;
169 hash_for_each_possible(ksocknal_data.ksnd_peers, peer_ni,
171 LASSERT(!peer_ni->ksnp_closing);
173 if (peer_ni->ksnp_ni != ni)
176 if (peer_ni->ksnp_id.nid != id.nid ||
177 peer_ni->ksnp_id.pid != id.pid)
180 CDEBUG(D_NET, "got peer_ni [%p] -> %s (%d)\n",
181 peer_ni, libcfs_id2str(id),
182 atomic_read(&peer_ni->ksnp_refcount));
188 struct ksock_peer_ni *
189 ksocknal_find_peer(struct lnet_ni *ni, struct lnet_process_id id)
191 struct ksock_peer_ni *peer_ni;
193 read_lock(&ksocknal_data.ksnd_global_lock);
194 peer_ni = ksocknal_find_peer_locked(ni, id);
195 if (peer_ni != NULL) /* +1 ref for caller? */
196 ksocknal_peer_addref(peer_ni);
197 read_unlock(&ksocknal_data.ksnd_global_lock);
203 ksocknal_unlink_peer_locked(struct ksock_peer_ni *peer_ni)
207 struct ksock_interface *iface;
209 for (i = 0; i < peer_ni->ksnp_n_passive_ips; i++) {
210 LASSERT(i < LNET_INTERFACES_NUM);
211 ip = peer_ni->ksnp_passive_ips[i];
213 iface = ksocknal_ip2iface(peer_ni->ksnp_ni, ip);
215 * All IPs in peer_ni->ksnp_passive_ips[] come from the
216 * interface list, therefore the call must succeed.
218 LASSERT(iface != NULL);
220 CDEBUG(D_NET, "peer_ni=%p iface=%p ksni_nroutes=%d\n",
221 peer_ni, iface, iface->ksni_nroutes);
222 iface->ksni_npeers--;
225 LASSERT(list_empty(&peer_ni->ksnp_conns));
226 LASSERT(list_empty(&peer_ni->ksnp_routes));
227 LASSERT(!peer_ni->ksnp_closing);
228 peer_ni->ksnp_closing = 1;
229 hlist_del(&peer_ni->ksnp_list);
230 /* lose peerlist's ref */
231 ksocknal_peer_decref(peer_ni);
235 ksocknal_get_peer_info(struct lnet_ni *ni, int index,
236 struct lnet_process_id *id, __u32 *myip, __u32 *peer_ip,
237 int *port, int *conn_count, int *share_count)
239 struct ksock_peer_ni *peer_ni;
240 struct ksock_route *route;
241 struct list_head *rtmp;
246 read_lock(&ksocknal_data.ksnd_global_lock);
248 hash_for_each(ksocknal_data.ksnd_peers, i, peer_ni, ksnp_list) {
250 if (peer_ni->ksnp_ni != ni)
253 if (peer_ni->ksnp_n_passive_ips == 0 &&
254 list_empty(&peer_ni->ksnp_routes)) {
258 *id = peer_ni->ksnp_id;
268 for (j = 0; j < peer_ni->ksnp_n_passive_ips; j++) {
272 *id = peer_ni->ksnp_id;
273 *myip = peer_ni->ksnp_passive_ips[j];
282 list_for_each(rtmp, &peer_ni->ksnp_routes) {
286 route = list_entry(rtmp, struct ksock_route,
289 *id = peer_ni->ksnp_id;
290 *myip = route->ksnr_myipaddr;
291 *peer_ip = route->ksnr_ipaddr;
292 *port = route->ksnr_port;
293 *conn_count = route->ksnr_conn_count;
294 *share_count = route->ksnr_share_count;
300 read_unlock(&ksocknal_data.ksnd_global_lock);
305 ksocknal_associate_route_conn_locked(struct ksock_route *route, struct ksock_conn *conn)
307 struct ksock_peer_ni *peer_ni = route->ksnr_peer;
308 int type = conn->ksnc_type;
309 struct ksock_interface *iface;
311 conn->ksnc_route = route;
312 ksocknal_route_addref(route);
314 if (route->ksnr_myipaddr != conn->ksnc_myipaddr) {
315 if (route->ksnr_myipaddr == 0) {
316 /* route wasn't bound locally yet (the initial route) */
317 CDEBUG(D_NET, "Binding %s %pI4h to %pI4h\n",
318 libcfs_id2str(peer_ni->ksnp_id),
320 &conn->ksnc_myipaddr);
322 CDEBUG(D_NET, "Rebinding %s %pI4h from %pI4h "
323 "to %pI4h\n", libcfs_id2str(peer_ni->ksnp_id),
325 &route->ksnr_myipaddr,
326 &conn->ksnc_myipaddr);
328 iface = ksocknal_ip2iface(route->ksnr_peer->ksnp_ni,
329 route->ksnr_myipaddr);
331 iface->ksni_nroutes--;
333 route->ksnr_myipaddr = conn->ksnc_myipaddr;
334 iface = ksocknal_ip2iface(route->ksnr_peer->ksnp_ni,
335 route->ksnr_myipaddr);
337 iface->ksni_nroutes++;
340 route->ksnr_connected |= (1<<type);
341 route->ksnr_conn_count++;
343 /* Successful connection => further attempts can
344 * proceed immediately */
345 route->ksnr_retry_interval = 0;
349 ksocknal_add_route_locked(struct ksock_peer_ni *peer_ni, struct ksock_route *route)
351 struct list_head *tmp;
352 struct ksock_conn *conn;
353 struct ksock_route *route2;
355 LASSERT(!peer_ni->ksnp_closing);
356 LASSERT(route->ksnr_peer == NULL);
357 LASSERT(!route->ksnr_scheduled);
358 LASSERT(!route->ksnr_connecting);
359 LASSERT(route->ksnr_connected == 0);
361 /* LASSERT(unique) */
362 list_for_each(tmp, &peer_ni->ksnp_routes) {
363 route2 = list_entry(tmp, struct ksock_route, ksnr_list);
365 if (route2->ksnr_ipaddr == route->ksnr_ipaddr) {
366 CERROR("Duplicate route %s %pI4h\n",
367 libcfs_id2str(peer_ni->ksnp_id),
368 &route->ksnr_ipaddr);
373 route->ksnr_peer = peer_ni;
374 ksocknal_peer_addref(peer_ni);
375 /* peer_ni's routelist takes over my ref on 'route' */
376 list_add_tail(&route->ksnr_list, &peer_ni->ksnp_routes);
378 list_for_each(tmp, &peer_ni->ksnp_conns) {
379 conn = list_entry(tmp, struct ksock_conn, ksnc_list);
381 if (conn->ksnc_ipaddr != route->ksnr_ipaddr)
384 ksocknal_associate_route_conn_locked(route, conn);
385 /* keep going (typed routes) */
390 ksocknal_del_route_locked(struct ksock_route *route)
392 struct ksock_peer_ni *peer_ni = route->ksnr_peer;
393 struct ksock_interface *iface;
394 struct ksock_conn *conn;
395 struct list_head *ctmp;
396 struct list_head *cnxt;
398 LASSERT(!route->ksnr_deleted);
400 /* Close associated conns */
401 list_for_each_safe(ctmp, cnxt, &peer_ni->ksnp_conns) {
402 conn = list_entry(ctmp, struct ksock_conn, ksnc_list);
404 if (conn->ksnc_route != route)
407 ksocknal_close_conn_locked(conn, 0);
410 if (route->ksnr_myipaddr != 0) {
411 iface = ksocknal_ip2iface(route->ksnr_peer->ksnp_ni,
412 route->ksnr_myipaddr);
414 iface->ksni_nroutes--;
417 route->ksnr_deleted = 1;
418 list_del(&route->ksnr_list);
419 ksocknal_route_decref(route); /* drop peer_ni's ref */
421 if (list_empty(&peer_ni->ksnp_routes) &&
422 list_empty(&peer_ni->ksnp_conns)) {
423 /* I've just removed the last route to a peer_ni with no active
425 ksocknal_unlink_peer_locked(peer_ni);
430 ksocknal_add_peer(struct lnet_ni *ni, struct lnet_process_id id, __u32 ipaddr,
433 struct list_head *tmp;
434 struct ksock_peer_ni *peer_ni;
435 struct ksock_peer_ni *peer2;
436 struct ksock_route *route;
437 struct ksock_route *route2;
439 if (id.nid == LNET_NID_ANY ||
440 id.pid == LNET_PID_ANY)
443 /* Have a brand new peer_ni ready... */
444 peer_ni = ksocknal_create_peer(ni, id);
446 return PTR_ERR(peer_ni);
448 route = ksocknal_create_route (ipaddr, port);
450 ksocknal_peer_decref(peer_ni);
454 write_lock_bh(&ksocknal_data.ksnd_global_lock);
456 /* always called with a ref on ni, so shutdown can't have started */
457 LASSERT(atomic_read(&((struct ksock_net *)ni->ni_data)->ksnn_npeers)
460 peer2 = ksocknal_find_peer_locked(ni, id);
462 ksocknal_peer_decref(peer_ni);
465 /* peer_ni table takes my ref on peer_ni */
466 hash_add(ksocknal_data.ksnd_peers, &peer_ni->ksnp_list, id.nid);
470 list_for_each(tmp, &peer_ni->ksnp_routes) {
471 route2 = list_entry(tmp, struct ksock_route, ksnr_list);
473 if (route2->ksnr_ipaddr == ipaddr)
478 if (route2 == NULL) {
479 ksocknal_add_route_locked(peer_ni, route);
480 route->ksnr_share_count++;
482 ksocknal_route_decref(route);
483 route2->ksnr_share_count++;
486 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
492 ksocknal_del_peer_locked(struct ksock_peer_ni *peer_ni, __u32 ip)
494 struct ksock_conn *conn;
495 struct ksock_route *route;
496 struct list_head *tmp;
497 struct list_head *nxt;
500 LASSERT(!peer_ni->ksnp_closing);
502 /* Extra ref prevents peer_ni disappearing until I'm done with it */
503 ksocknal_peer_addref(peer_ni);
505 list_for_each_safe(tmp, nxt, &peer_ni->ksnp_routes) {
506 route = list_entry(tmp, struct ksock_route, ksnr_list);
509 if (!(ip == 0 || route->ksnr_ipaddr == ip))
512 route->ksnr_share_count = 0;
513 /* This deletes associated conns too */
514 ksocknal_del_route_locked(route);
518 list_for_each_safe(tmp, nxt, &peer_ni->ksnp_routes) {
519 route = list_entry(tmp, struct ksock_route, ksnr_list);
520 nshared += route->ksnr_share_count;
524 /* remove everything else if there are no explicit entries
527 list_for_each_safe(tmp, nxt, &peer_ni->ksnp_routes) {
528 route = list_entry(tmp, struct ksock_route, ksnr_list);
530 /* we should only be removing auto-entries */
531 LASSERT(route->ksnr_share_count == 0);
532 ksocknal_del_route_locked(route);
535 list_for_each_safe(tmp, nxt, &peer_ni->ksnp_conns) {
536 conn = list_entry(tmp, struct ksock_conn, ksnc_list);
538 ksocknal_close_conn_locked(conn, 0);
542 ksocknal_peer_decref(peer_ni);
543 /* NB peer_ni unlinks itself when last conn/route is removed */
547 ksocknal_del_peer(struct lnet_ni *ni, struct lnet_process_id id, __u32 ip)
550 struct hlist_node *pnxt;
551 struct ksock_peer_ni *peer_ni;
557 write_lock_bh(&ksocknal_data.ksnd_global_lock);
559 if (id.nid != LNET_NID_ANY) {
560 lo = hash_min(id.nid, HASH_BITS(ksocknal_data.ksnd_peers));
564 hi = HASH_SIZE(ksocknal_data.ksnd_peers) - 1;
567 for (i = lo; i <= hi; i++) {
568 hlist_for_each_entry_safe(peer_ni, pnxt,
569 &ksocknal_data.ksnd_peers[i],
571 if (peer_ni->ksnp_ni != ni)
574 if (!((id.nid == LNET_NID_ANY ||
575 peer_ni->ksnp_id.nid == id.nid) &&
576 (id.pid == LNET_PID_ANY ||
577 peer_ni->ksnp_id.pid == id.pid)))
580 ksocknal_peer_addref(peer_ni); /* a ref for me... */
582 ksocknal_del_peer_locked(peer_ni, ip);
584 if (peer_ni->ksnp_closing &&
585 !list_empty(&peer_ni->ksnp_tx_queue)) {
586 LASSERT(list_empty(&peer_ni->ksnp_conns));
587 LASSERT(list_empty(&peer_ni->ksnp_routes));
589 list_splice_init(&peer_ni->ksnp_tx_queue,
593 ksocknal_peer_decref(peer_ni); /* ...till here */
595 rc = 0; /* matched! */
599 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
601 ksocknal_txlist_done(ni, &zombies, -ENETDOWN);
606 static struct ksock_conn *
607 ksocknal_get_conn_by_idx(struct lnet_ni *ni, int index)
609 struct ksock_peer_ni *peer_ni;
610 struct ksock_conn *conn;
611 struct list_head *ctmp;
614 read_lock(&ksocknal_data.ksnd_global_lock);
616 hash_for_each(ksocknal_data.ksnd_peers, i, peer_ni, ksnp_list) {
617 LASSERT(!peer_ni->ksnp_closing);
619 if (peer_ni->ksnp_ni != ni)
622 list_for_each(ctmp, &peer_ni->ksnp_conns) {
626 conn = list_entry(ctmp, struct ksock_conn,
628 ksocknal_conn_addref(conn);
629 read_unlock(&ksocknal_data.ksnd_global_lock);
634 read_unlock(&ksocknal_data.ksnd_global_lock);
638 static struct ksock_sched *
639 ksocknal_choose_scheduler_locked(unsigned int cpt)
641 struct ksock_sched *sched = ksocknal_data.ksnd_schedulers[cpt];
644 if (sched->kss_nthreads == 0) {
645 cfs_percpt_for_each(sched, i, ksocknal_data.ksnd_schedulers) {
646 if (sched->kss_nthreads > 0) {
647 CDEBUG(D_NET, "scheduler[%d] has no threads. selected scheduler[%d]\n",
648 cpt, sched->kss_cpt);
659 ksocknal_local_ipvec(struct lnet_ni *ni, __u32 *ipaddrs)
661 struct ksock_net *net = ni->ni_data;
665 read_lock(&ksocknal_data.ksnd_global_lock);
667 nip = net->ksnn_ninterfaces;
668 LASSERT(nip <= LNET_INTERFACES_NUM);
671 * Only offer interfaces for additional connections if I have
675 read_unlock(&ksocknal_data.ksnd_global_lock);
679 for (i = 0; i < nip; i++) {
680 ipaddrs[i] = net->ksnn_interfaces[i].ksni_ipaddr;
681 LASSERT(ipaddrs[i] != 0);
684 read_unlock(&ksocknal_data.ksnd_global_lock);
689 ksocknal_match_peerip(struct ksock_interface *iface, __u32 *ips, int nips)
691 int best_netmatch = 0;
698 for (i = 0; i < nips; i++) {
702 this_xor = (ips[i] ^ iface->ksni_ipaddr);
703 this_netmatch = ((this_xor & iface->ksni_netmask) == 0) ? 1 : 0;
706 best_netmatch < this_netmatch ||
707 (best_netmatch == this_netmatch &&
708 best_xor > this_xor)))
712 best_netmatch = this_netmatch;
721 ksocknal_select_ips(struct ksock_peer_ni *peer_ni, __u32 *peerips, int n_peerips)
723 rwlock_t *global_lock = &ksocknal_data.ksnd_global_lock;
724 struct ksock_net *net = peer_ni->ksnp_ni->ni_data;
725 struct ksock_interface *iface;
726 struct ksock_interface *best_iface;
737 /* CAVEAT EMPTOR: We do all our interface matching with an
738 * exclusive hold of global lock at IRQ priority. We're only
739 * expecting to be dealing with small numbers of interfaces, so the
740 * O(n**3)-ness shouldn't matter */
742 /* Also note that I'm not going to return more than n_peerips
743 * interfaces, even if I have more myself */
745 write_lock_bh(global_lock);
747 LASSERT(n_peerips <= LNET_INTERFACES_NUM);
748 LASSERT(net->ksnn_ninterfaces <= LNET_INTERFACES_NUM);
750 /* Only match interfaces for additional connections
751 * if I have > 1 interface
753 n_ips = (net->ksnn_ninterfaces < 2) ? 0 :
754 min(n_peerips, net->ksnn_ninterfaces);
756 for (i = 0; peer_ni->ksnp_n_passive_ips < n_ips; i++) {
757 /* ^ yes really... */
759 /* If we have any new interfaces, first tick off all the
760 * peer_ni IPs that match old interfaces, then choose new
761 * interfaces to match the remaining peer_ni IPS.
762 * We don't forget interfaces we've stopped using; we might
763 * start using them again... */
765 if (i < peer_ni->ksnp_n_passive_ips) {
767 ip = peer_ni->ksnp_passive_ips[i];
768 best_iface = ksocknal_ip2iface(peer_ni->ksnp_ni, ip);
770 /* peer_ni passive ips are kept up to date */
771 LASSERT(best_iface != NULL);
773 /* choose a new interface */
774 LASSERT (i == peer_ni->ksnp_n_passive_ips);
780 for (j = 0; j < net->ksnn_ninterfaces; j++) {
781 iface = &net->ksnn_interfaces[j];
782 ip = iface->ksni_ipaddr;
784 for (k = 0; k < peer_ni->ksnp_n_passive_ips; k++)
785 if (peer_ni->ksnp_passive_ips[k] == ip)
788 if (k < peer_ni->ksnp_n_passive_ips) /* using it already */
791 k = ksocknal_match_peerip(iface, peerips, n_peerips);
792 xor = (ip ^ peerips[k]);
793 this_netmatch = ((xor & iface->ksni_netmask) == 0) ? 1 : 0;
795 if (!(best_iface == NULL ||
796 best_netmatch < this_netmatch ||
797 (best_netmatch == this_netmatch &&
798 best_npeers > iface->ksni_npeers)))
802 best_netmatch = this_netmatch;
803 best_npeers = iface->ksni_npeers;
806 LASSERT(best_iface != NULL);
808 best_iface->ksni_npeers++;
809 ip = best_iface->ksni_ipaddr;
810 peer_ni->ksnp_passive_ips[i] = ip;
811 peer_ni->ksnp_n_passive_ips = i+1;
814 /* mark the best matching peer_ni IP used */
815 j = ksocknal_match_peerip(best_iface, peerips, n_peerips);
819 /* Overwrite input peer_ni IP addresses */
820 memcpy(peerips, peer_ni->ksnp_passive_ips, n_ips * sizeof(*peerips));
822 write_unlock_bh(global_lock);
828 ksocknal_create_routes(struct ksock_peer_ni *peer_ni, int port,
829 __u32 *peer_ipaddrs, int npeer_ipaddrs)
831 struct ksock_route *newroute = NULL;
832 rwlock_t *global_lock = &ksocknal_data.ksnd_global_lock;
833 struct lnet_ni *ni = peer_ni->ksnp_ni;
834 struct ksock_net *net = ni->ni_data;
835 struct list_head *rtmp;
836 struct ksock_route *route;
837 struct ksock_interface *iface;
838 struct ksock_interface *best_iface;
845 /* CAVEAT EMPTOR: We do all our interface matching with an
846 * exclusive hold of global lock at IRQ priority. We're only
847 * expecting to be dealing with small numbers of interfaces, so the
848 * O(n**3)-ness here shouldn't matter */
850 write_lock_bh(global_lock);
852 if (net->ksnn_ninterfaces < 2) {
853 /* Only create additional connections
854 * if I have > 1 interface */
855 write_unlock_bh(global_lock);
859 LASSERT(npeer_ipaddrs <= LNET_INTERFACES_NUM);
861 for (i = 0; i < npeer_ipaddrs; i++) {
862 if (newroute != NULL) {
863 newroute->ksnr_ipaddr = peer_ipaddrs[i];
865 write_unlock_bh(global_lock);
867 newroute = ksocknal_create_route(peer_ipaddrs[i], port);
868 if (newroute == NULL)
871 write_lock_bh(global_lock);
874 if (peer_ni->ksnp_closing) {
875 /* peer_ni got closed under me */
879 /* Already got a route? */
881 list_for_each(rtmp, &peer_ni->ksnp_routes) {
882 route = list_entry(rtmp, struct ksock_route, ksnr_list);
884 if (route->ksnr_ipaddr == newroute->ksnr_ipaddr)
896 LASSERT(net->ksnn_ninterfaces <= LNET_INTERFACES_NUM);
898 /* Select interface to connect from */
899 for (j = 0; j < net->ksnn_ninterfaces; j++) {
900 iface = &net->ksnn_interfaces[j];
902 /* Using this interface already? */
903 list_for_each(rtmp, &peer_ni->ksnp_routes) {
904 route = list_entry(rtmp, struct ksock_route,
907 if (route->ksnr_myipaddr == iface->ksni_ipaddr)
915 this_netmatch = (((iface->ksni_ipaddr ^
916 newroute->ksnr_ipaddr) &
917 iface->ksni_netmask) == 0) ? 1 : 0;
919 if (!(best_iface == NULL ||
920 best_netmatch < this_netmatch ||
921 (best_netmatch == this_netmatch &&
922 best_nroutes > iface->ksni_nroutes)))
926 best_netmatch = this_netmatch;
927 best_nroutes = iface->ksni_nroutes;
930 if (best_iface == NULL)
933 newroute->ksnr_myipaddr = best_iface->ksni_ipaddr;
934 best_iface->ksni_nroutes++;
936 ksocknal_add_route_locked(peer_ni, newroute);
940 write_unlock_bh(global_lock);
941 if (newroute != NULL)
942 ksocknal_route_decref(newroute);
946 ksocknal_accept(struct lnet_ni *ni, struct socket *sock)
948 struct ksock_connreq *cr;
953 rc = lnet_sock_getaddr(sock, true, &peer_ip, &peer_port);
954 LASSERT(rc == 0); /* we succeeded before */
956 LIBCFS_ALLOC(cr, sizeof(*cr));
958 LCONSOLE_ERROR_MSG(0x12f, "Dropping connection request from "
959 "%pI4h: memory exhausted\n", &peer_ip);
965 cr->ksncr_sock = sock;
967 spin_lock_bh(&ksocknal_data.ksnd_connd_lock);
969 list_add_tail(&cr->ksncr_list, &ksocknal_data.ksnd_connd_connreqs);
970 wake_up(&ksocknal_data.ksnd_connd_waitq);
972 spin_unlock_bh(&ksocknal_data.ksnd_connd_lock);
977 ksocknal_connecting(struct ksock_peer_ni *peer_ni, __u32 ipaddr)
979 struct ksock_route *route;
981 list_for_each_entry(route, &peer_ni->ksnp_routes, ksnr_list) {
982 if (route->ksnr_ipaddr == ipaddr)
983 return route->ksnr_connecting;
989 ksocknal_create_conn(struct lnet_ni *ni, struct ksock_route *route,
990 struct socket *sock, int type)
992 rwlock_t *global_lock = &ksocknal_data.ksnd_global_lock;
994 struct lnet_process_id peerid;
995 struct list_head *tmp;
997 struct ksock_conn *conn;
998 struct ksock_conn *conn2;
999 struct ksock_peer_ni *peer_ni = NULL;
1000 struct ksock_peer_ni *peer2;
1001 struct ksock_sched *sched;
1002 struct ksock_hello_msg *hello;
1004 struct ksock_tx *tx;
1005 struct ksock_tx *txtmp;
1011 active = (route != NULL);
1013 LASSERT (active == (type != SOCKLND_CONN_NONE));
1015 LIBCFS_ALLOC(conn, sizeof(*conn));
1021 conn->ksnc_peer = NULL;
1022 conn->ksnc_route = NULL;
1023 conn->ksnc_sock = sock;
1024 /* 2 ref, 1 for conn, another extra ref prevents socket
1025 * being closed before establishment of connection */
1026 atomic_set (&conn->ksnc_sock_refcount, 2);
1027 conn->ksnc_type = type;
1028 ksocknal_lib_save_callback(sock, conn);
1029 atomic_set (&conn->ksnc_conn_refcount, 1); /* 1 ref for me */
1031 conn->ksnc_rx_ready = 0;
1032 conn->ksnc_rx_scheduled = 0;
1034 INIT_LIST_HEAD(&conn->ksnc_tx_queue);
1035 conn->ksnc_tx_ready = 0;
1036 conn->ksnc_tx_scheduled = 0;
1037 conn->ksnc_tx_carrier = NULL;
1038 atomic_set (&conn->ksnc_tx_nob, 0);
1040 LIBCFS_ALLOC(hello, offsetof(struct ksock_hello_msg,
1041 kshm_ips[LNET_INTERFACES_NUM]));
1042 if (hello == NULL) {
1047 /* stash conn's local and remote addrs */
1048 rc = ksocknal_lib_get_conn_addrs (conn);
1052 /* Find out/confirm peer_ni's NID and connection type and get the
1053 * vector of interfaces she's willing to let me connect to.
1054 * Passive connections use the listener timeout since the peer_ni sends
1058 peer_ni = route->ksnr_peer;
1059 LASSERT(ni == peer_ni->ksnp_ni);
1061 /* Active connection sends HELLO eagerly */
1062 hello->kshm_nips = ksocknal_local_ipvec(ni, hello->kshm_ips);
1063 peerid = peer_ni->ksnp_id;
1065 write_lock_bh(global_lock);
1066 conn->ksnc_proto = peer_ni->ksnp_proto;
1067 write_unlock_bh(global_lock);
1069 if (conn->ksnc_proto == NULL) {
1070 conn->ksnc_proto = &ksocknal_protocol_v3x;
1071 #if SOCKNAL_VERSION_DEBUG
1072 if (*ksocknal_tunables.ksnd_protocol == 2)
1073 conn->ksnc_proto = &ksocknal_protocol_v2x;
1074 else if (*ksocknal_tunables.ksnd_protocol == 1)
1075 conn->ksnc_proto = &ksocknal_protocol_v1x;
1079 rc = ksocknal_send_hello (ni, conn, peerid.nid, hello);
1083 peerid.nid = LNET_NID_ANY;
1084 peerid.pid = LNET_PID_ANY;
1086 /* Passive, get protocol from peer_ni */
1087 conn->ksnc_proto = NULL;
1090 rc = ksocknal_recv_hello (ni, conn, hello, &peerid, &incarnation);
1094 LASSERT (rc == 0 || active);
1095 LASSERT (conn->ksnc_proto != NULL);
1096 LASSERT (peerid.nid != LNET_NID_ANY);
1098 cpt = lnet_cpt_of_nid(peerid.nid, ni);
1101 ksocknal_peer_addref(peer_ni);
1102 write_lock_bh(global_lock);
1104 peer_ni = ksocknal_create_peer(ni, peerid);
1105 if (IS_ERR(peer_ni)) {
1106 rc = PTR_ERR(peer_ni);
1110 write_lock_bh(global_lock);
1112 /* called with a ref on ni, so shutdown can't have started */
1113 LASSERT(atomic_read(&((struct ksock_net *)ni->ni_data)->ksnn_npeers) >= 0);
1115 peer2 = ksocknal_find_peer_locked(ni, peerid);
1116 if (peer2 == NULL) {
1117 /* NB this puts an "empty" peer_ni in the peer_ni
1118 * table (which takes my ref) */
1119 hash_add(ksocknal_data.ksnd_peers,
1120 &peer_ni->ksnp_list, peerid.nid);
1122 ksocknal_peer_decref(peer_ni);
1127 ksocknal_peer_addref(peer_ni);
1128 peer_ni->ksnp_accepting++;
1130 /* Am I already connecting to this guy? Resolve in
1131 * favour of higher NID... */
1132 if (peerid.nid < ni->ni_nid &&
1133 ksocknal_connecting(peer_ni, conn->ksnc_ipaddr)) {
1135 warn = "connection race resolution";
1140 if (peer_ni->ksnp_closing ||
1141 (active && route->ksnr_deleted)) {
1142 /* peer_ni/route got closed under me */
1144 warn = "peer_ni/route removed";
1148 if (peer_ni->ksnp_proto == NULL) {
1149 /* Never connected before.
1150 * NB recv_hello may have returned EPROTO to signal my peer_ni
1151 * wants a different protocol than the one I asked for.
1153 LASSERT(list_empty(&peer_ni->ksnp_conns));
1155 peer_ni->ksnp_proto = conn->ksnc_proto;
1156 peer_ni->ksnp_incarnation = incarnation;
1159 if (peer_ni->ksnp_proto != conn->ksnc_proto ||
1160 peer_ni->ksnp_incarnation != incarnation) {
1161 /* peer_ni rebooted or I've got the wrong protocol version */
1162 ksocknal_close_peer_conns_locked(peer_ni, 0, 0);
1164 peer_ni->ksnp_proto = NULL;
1166 warn = peer_ni->ksnp_incarnation != incarnation ?
1167 "peer_ni rebooted" :
1168 "wrong proto version";
1178 warn = "lost conn race";
1181 warn = "retry with different protocol version";
1185 /* Refuse to duplicate an existing connection, unless this is a
1186 * loopback connection */
1187 if (conn->ksnc_ipaddr != conn->ksnc_myipaddr) {
1188 list_for_each(tmp, &peer_ni->ksnp_conns) {
1189 conn2 = list_entry(tmp, struct ksock_conn, ksnc_list);
1191 if (conn2->ksnc_ipaddr != conn->ksnc_ipaddr ||
1192 conn2->ksnc_myipaddr != conn->ksnc_myipaddr ||
1193 conn2->ksnc_type != conn->ksnc_type)
1196 /* Reply on a passive connection attempt so the peer_ni
1197 * realises we're connected. */
1207 /* If the connection created by this route didn't bind to the IP
1208 * address the route connected to, the connection/route matching
1209 * code below probably isn't going to work. */
1211 route->ksnr_ipaddr != conn->ksnc_ipaddr) {
1212 CERROR("Route %s %pI4h connected to %pI4h\n",
1213 libcfs_id2str(peer_ni->ksnp_id),
1214 &route->ksnr_ipaddr,
1215 &conn->ksnc_ipaddr);
1218 /* Search for a route corresponding to the new connection and
1219 * create an association. This allows incoming connections created
1220 * by routes in my peer_ni to match my own route entries so I don't
1221 * continually create duplicate routes. */
1222 list_for_each(tmp, &peer_ni->ksnp_routes) {
1223 route = list_entry(tmp, struct ksock_route, ksnr_list);
1225 if (route->ksnr_ipaddr != conn->ksnc_ipaddr)
1228 ksocknal_associate_route_conn_locked(route, conn);
1232 conn->ksnc_peer = peer_ni; /* conn takes my ref on peer_ni */
1233 peer_ni->ksnp_last_alive = ktime_get_seconds();
1234 peer_ni->ksnp_send_keepalive = 0;
1235 peer_ni->ksnp_error = 0;
1237 sched = ksocknal_choose_scheduler_locked(cpt);
1239 CERROR("no schedulers available. node is unhealthy\n");
1243 * The cpt might have changed if we ended up selecting a non cpt
1244 * native scheduler. So use the scheduler's cpt instead.
1246 cpt = sched->kss_cpt;
1247 sched->kss_nconns++;
1248 conn->ksnc_scheduler = sched;
1250 conn->ksnc_tx_last_post = ktime_get_seconds();
1251 /* Set the deadline for the outgoing HELLO to drain */
1252 conn->ksnc_tx_bufnob = sock->sk->sk_wmem_queued;
1253 conn->ksnc_tx_deadline = ktime_get_seconds() +
1254 lnet_get_lnd_timeout();
1255 smp_mb(); /* order with adding to peer_ni's conn list */
1257 list_add(&conn->ksnc_list, &peer_ni->ksnp_conns);
1258 ksocknal_conn_addref(conn);
1260 ksocknal_new_packet(conn, 0);
1262 conn->ksnc_zc_capable = ksocknal_lib_zc_capable(conn);
1264 /* Take packets blocking for this connection. */
1265 list_for_each_entry_safe(tx, txtmp, &peer_ni->ksnp_tx_queue, tx_list) {
1266 if (conn->ksnc_proto->pro_match_tx(conn, tx, tx->tx_nonblk) ==
1270 list_del(&tx->tx_list);
1271 ksocknal_queue_tx_locked(tx, conn);
1274 write_unlock_bh(global_lock);
1276 /* We've now got a new connection. Any errors from here on are just
1277 * like "normal" comms errors and we close the connection normally.
1278 * NB (a) we still have to send the reply HELLO for passive
1280 * (b) normal I/O on the conn is blocked until I setup and call the
1284 CDEBUG(D_NET, "New conn %s p %d.x %pI4h -> %pI4h/%d"
1285 " incarnation:%lld sched[%d]\n",
1286 libcfs_id2str(peerid), conn->ksnc_proto->pro_version,
1287 &conn->ksnc_myipaddr, &conn->ksnc_ipaddr,
1288 conn->ksnc_port, incarnation, cpt);
1291 /* additional routes after interface exchange? */
1292 ksocknal_create_routes(peer_ni, conn->ksnc_port,
1293 hello->kshm_ips, hello->kshm_nips);
1295 hello->kshm_nips = ksocknal_select_ips(peer_ni, hello->kshm_ips,
1297 rc = ksocknal_send_hello(ni, conn, peerid.nid, hello);
1300 LIBCFS_FREE(hello, offsetof(struct ksock_hello_msg,
1301 kshm_ips[LNET_INTERFACES_NUM]));
1303 /* setup the socket AFTER I've received hello (it disables
1304 * SO_LINGER). I might call back to the acceptor who may want
1305 * to send a protocol version response and then close the
1306 * socket; this ensures the socket only tears down after the
1307 * response has been sent. */
1309 rc = ksocknal_lib_setup_sock(sock);
1311 write_lock_bh(global_lock);
1313 /* NB my callbacks block while I hold ksnd_global_lock */
1314 ksocknal_lib_set_callback(sock, conn);
1317 peer_ni->ksnp_accepting--;
1319 write_unlock_bh(global_lock);
1322 write_lock_bh(global_lock);
1323 if (!conn->ksnc_closing) {
1324 /* could be closed by another thread */
1325 ksocknal_close_conn_locked(conn, rc);
1327 write_unlock_bh(global_lock);
1328 } else if (ksocknal_connsock_addref(conn) == 0) {
1329 /* Allow I/O to proceed. */
1330 ksocknal_read_callback(conn);
1331 ksocknal_write_callback(conn);
1332 ksocknal_connsock_decref(conn);
1335 ksocknal_connsock_decref(conn);
1336 ksocknal_conn_decref(conn);
1340 if (!peer_ni->ksnp_closing &&
1341 list_empty(&peer_ni->ksnp_conns) &&
1342 list_empty(&peer_ni->ksnp_routes)) {
1343 list_add(&zombies, &peer_ni->ksnp_tx_queue);
1344 list_del_init(&peer_ni->ksnp_tx_queue);
1345 ksocknal_unlink_peer_locked(peer_ni);
1348 write_unlock_bh(global_lock);
1352 CERROR("Not creating conn %s type %d: %s\n",
1353 libcfs_id2str(peerid), conn->ksnc_type, warn);
1355 CDEBUG(D_NET, "Not creating conn %s type %d: %s\n",
1356 libcfs_id2str(peerid), conn->ksnc_type, warn);
1361 /* Request retry by replying with CONN_NONE
1362 * ksnc_proto has been set already */
1363 conn->ksnc_type = SOCKLND_CONN_NONE;
1364 hello->kshm_nips = 0;
1365 ksocknal_send_hello(ni, conn, peerid.nid, hello);
1368 write_lock_bh(global_lock);
1369 peer_ni->ksnp_accepting--;
1370 write_unlock_bh(global_lock);
1374 * If we get here without an error code, just use -EALREADY.
1375 * Depending on how we got here, the error may be positive
1376 * or negative. Normalize the value for ksocknal_txlist_done().
1378 rc2 = (rc == 0 ? -EALREADY : (rc > 0 ? -rc : rc));
1379 ksocknal_txlist_done(ni, &zombies, rc2);
1380 ksocknal_peer_decref(peer_ni);
1384 LIBCFS_FREE(hello, offsetof(struct ksock_hello_msg,
1385 kshm_ips[LNET_INTERFACES_NUM]));
1387 LIBCFS_FREE(conn, sizeof(*conn));
1395 ksocknal_close_conn_locked(struct ksock_conn *conn, int error)
1397 /* This just does the immmediate housekeeping, and queues the
1398 * connection for the reaper to terminate.
1399 * Caller holds ksnd_global_lock exclusively in irq context */
1400 struct ksock_peer_ni *peer_ni = conn->ksnc_peer;
1401 struct ksock_route *route;
1402 struct ksock_conn *conn2;
1403 struct list_head *tmp;
1405 LASSERT(peer_ni->ksnp_error == 0);
1406 LASSERT(!conn->ksnc_closing);
1407 conn->ksnc_closing = 1;
1409 /* ksnd_deathrow_conns takes over peer_ni's ref */
1410 list_del(&conn->ksnc_list);
1412 route = conn->ksnc_route;
1413 if (route != NULL) {
1414 /* dissociate conn from route... */
1415 LASSERT(!route->ksnr_deleted);
1416 LASSERT((route->ksnr_connected & (1 << conn->ksnc_type)) != 0);
1419 list_for_each(tmp, &peer_ni->ksnp_conns) {
1420 conn2 = list_entry(tmp, struct ksock_conn, ksnc_list);
1422 if (conn2->ksnc_route == route &&
1423 conn2->ksnc_type == conn->ksnc_type)
1429 route->ksnr_connected &= ~(1 << conn->ksnc_type);
1431 conn->ksnc_route = NULL;
1433 ksocknal_route_decref(route); /* drop conn's ref on route */
1436 if (list_empty(&peer_ni->ksnp_conns)) {
1437 /* No more connections to this peer_ni */
1439 if (!list_empty(&peer_ni->ksnp_tx_queue)) {
1440 struct ksock_tx *tx;
1442 LASSERT(conn->ksnc_proto == &ksocknal_protocol_v3x);
1444 /* throw them to the last connection...,
1445 * these TXs will be send to /dev/null by scheduler */
1446 list_for_each_entry(tx, &peer_ni->ksnp_tx_queue,
1448 ksocknal_tx_prep(conn, tx);
1450 spin_lock_bh(&conn->ksnc_scheduler->kss_lock);
1451 list_splice_init(&peer_ni->ksnp_tx_queue,
1452 &conn->ksnc_tx_queue);
1453 spin_unlock_bh(&conn->ksnc_scheduler->kss_lock);
1456 /* renegotiate protocol version */
1457 peer_ni->ksnp_proto = NULL;
1458 /* stash last conn close reason */
1459 peer_ni->ksnp_error = error;
1461 if (list_empty(&peer_ni->ksnp_routes)) {
1462 /* I've just closed last conn belonging to a
1463 * peer_ni with no routes to it */
1464 ksocknal_unlink_peer_locked(peer_ni);
1468 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
1470 list_add_tail(&conn->ksnc_list, &ksocknal_data.ksnd_deathrow_conns);
1471 wake_up(&ksocknal_data.ksnd_reaper_waitq);
1473 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
1477 ksocknal_peer_failed(struct ksock_peer_ni *peer_ni)
1480 time64_t last_alive = 0;
1482 /* There has been a connection failure or comms error; but I'll only
1483 * tell LNET I think the peer_ni is dead if it's to another kernel and
1484 * there are no connections or connection attempts in existence. */
1486 read_lock(&ksocknal_data.ksnd_global_lock);
1488 if ((peer_ni->ksnp_id.pid & LNET_PID_USERFLAG) == 0 &&
1489 list_empty(&peer_ni->ksnp_conns) &&
1490 peer_ni->ksnp_accepting == 0 &&
1491 ksocknal_find_connecting_route_locked(peer_ni) == NULL) {
1493 last_alive = peer_ni->ksnp_last_alive;
1496 read_unlock(&ksocknal_data.ksnd_global_lock);
1499 lnet_notify(peer_ni->ksnp_ni, peer_ni->ksnp_id.nid,
1500 false, false, last_alive);
1504 ksocknal_finalize_zcreq(struct ksock_conn *conn)
1506 struct ksock_peer_ni *peer_ni = conn->ksnc_peer;
1507 struct ksock_tx *tx;
1508 struct ksock_tx *tmp;
1511 /* NB safe to finalize TXs because closing of socket will
1512 * abort all buffered data */
1513 LASSERT(conn->ksnc_sock == NULL);
1515 spin_lock(&peer_ni->ksnp_lock);
1517 list_for_each_entry_safe(tx, tmp, &peer_ni->ksnp_zc_req_list, tx_zc_list) {
1518 if (tx->tx_conn != conn)
1521 LASSERT(tx->tx_msg.ksm_zc_cookies[0] != 0);
1523 tx->tx_msg.ksm_zc_cookies[0] = 0;
1524 tx->tx_zc_aborted = 1; /* mark it as not-acked */
1525 list_move(&tx->tx_zc_list, &zlist);
1528 spin_unlock(&peer_ni->ksnp_lock);
1530 while (!list_empty(&zlist)) {
1531 tx = list_entry(zlist.next, struct ksock_tx, tx_zc_list);
1533 list_del(&tx->tx_zc_list);
1534 ksocknal_tx_decref(tx);
1539 ksocknal_terminate_conn(struct ksock_conn *conn)
1541 /* This gets called by the reaper (guaranteed thread context) to
1542 * disengage the socket from its callbacks and close it.
1543 * ksnc_refcount will eventually hit zero, and then the reaper will
1545 struct ksock_peer_ni *peer_ni = conn->ksnc_peer;
1546 struct ksock_sched *sched = conn->ksnc_scheduler;
1549 LASSERT(conn->ksnc_closing);
1551 /* wake up the scheduler to "send" all remaining packets to /dev/null */
1552 spin_lock_bh(&sched->kss_lock);
1554 /* a closing conn is always ready to tx */
1555 conn->ksnc_tx_ready = 1;
1557 if (!conn->ksnc_tx_scheduled &&
1558 !list_empty(&conn->ksnc_tx_queue)) {
1559 list_add_tail(&conn->ksnc_tx_list,
1560 &sched->kss_tx_conns);
1561 conn->ksnc_tx_scheduled = 1;
1562 /* extra ref for scheduler */
1563 ksocknal_conn_addref(conn);
1565 wake_up (&sched->kss_waitq);
1568 spin_unlock_bh(&sched->kss_lock);
1570 /* serialise with callbacks */
1571 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1573 ksocknal_lib_reset_callback(conn->ksnc_sock, conn);
1575 /* OK, so this conn may not be completely disengaged from its
1576 * scheduler yet, but it _has_ committed to terminate... */
1577 conn->ksnc_scheduler->kss_nconns--;
1579 if (peer_ni->ksnp_error != 0) {
1580 /* peer_ni's last conn closed in error */
1581 LASSERT(list_empty(&peer_ni->ksnp_conns));
1583 peer_ni->ksnp_error = 0; /* avoid multiple notifications */
1586 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1589 ksocknal_peer_failed(peer_ni);
1591 /* The socket is closed on the final put; either here, or in
1592 * ksocknal_{send,recv}msg(). Since we set up the linger2 option
1593 * when the connection was established, this will close the socket
1594 * immediately, aborting anything buffered in it. Any hung
1595 * zero-copy transmits will therefore complete in finite time. */
1596 ksocknal_connsock_decref(conn);
1600 ksocknal_queue_zombie_conn(struct ksock_conn *conn)
1602 /* Queue the conn for the reaper to destroy */
1603 LASSERT(atomic_read(&conn->ksnc_conn_refcount) == 0);
1604 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
1606 list_add_tail(&conn->ksnc_list, &ksocknal_data.ksnd_zombie_conns);
1607 wake_up(&ksocknal_data.ksnd_reaper_waitq);
1609 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
1613 ksocknal_destroy_conn(struct ksock_conn *conn)
1617 /* Final coup-de-grace of the reaper */
1618 CDEBUG (D_NET, "connection %p\n", conn);
1620 LASSERT (atomic_read (&conn->ksnc_conn_refcount) == 0);
1621 LASSERT (atomic_read (&conn->ksnc_sock_refcount) == 0);
1622 LASSERT (conn->ksnc_sock == NULL);
1623 LASSERT (conn->ksnc_route == NULL);
1624 LASSERT (!conn->ksnc_tx_scheduled);
1625 LASSERT (!conn->ksnc_rx_scheduled);
1626 LASSERT(list_empty(&conn->ksnc_tx_queue));
1628 /* complete current receive if any */
1629 switch (conn->ksnc_rx_state) {
1630 case SOCKNAL_RX_LNET_PAYLOAD:
1631 last_rcv = conn->ksnc_rx_deadline -
1632 lnet_get_lnd_timeout();
1633 CERROR("Completing partial receive from %s[%d], "
1634 "ip %pI4h:%d, with error, wanted: %d, left: %d, "
1635 "last alive is %lld secs ago\n",
1636 libcfs_id2str(conn->ksnc_peer->ksnp_id), conn->ksnc_type,
1637 &conn->ksnc_ipaddr, conn->ksnc_port,
1638 conn->ksnc_rx_nob_wanted, conn->ksnc_rx_nob_left,
1639 ktime_get_seconds() - last_rcv);
1640 if (conn->ksnc_lnet_msg)
1641 conn->ksnc_lnet_msg->msg_health_status =
1642 LNET_MSG_STATUS_REMOTE_ERROR;
1643 lnet_finalize(conn->ksnc_lnet_msg, -EIO);
1645 case SOCKNAL_RX_LNET_HEADER:
1646 if (conn->ksnc_rx_started)
1647 CERROR("Incomplete receive of lnet header from %s, "
1648 "ip %pI4h:%d, with error, protocol: %d.x.\n",
1649 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1650 &conn->ksnc_ipaddr, conn->ksnc_port,
1651 conn->ksnc_proto->pro_version);
1653 case SOCKNAL_RX_KSM_HEADER:
1654 if (conn->ksnc_rx_started)
1655 CERROR("Incomplete receive of ksock message from %s, "
1656 "ip %pI4h:%d, with error, protocol: %d.x.\n",
1657 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1658 &conn->ksnc_ipaddr, conn->ksnc_port,
1659 conn->ksnc_proto->pro_version);
1661 case SOCKNAL_RX_SLOP:
1662 if (conn->ksnc_rx_started)
1663 CERROR("Incomplete receive of slops from %s, "
1664 "ip %pI4h:%d, with error\n",
1665 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1666 &conn->ksnc_ipaddr, conn->ksnc_port);
1673 ksocknal_peer_decref(conn->ksnc_peer);
1675 LIBCFS_FREE (conn, sizeof (*conn));
1679 ksocknal_close_peer_conns_locked(struct ksock_peer_ni *peer_ni, __u32 ipaddr, int why)
1681 struct ksock_conn *conn;
1682 struct list_head *ctmp;
1683 struct list_head *cnxt;
1686 list_for_each_safe(ctmp, cnxt, &peer_ni->ksnp_conns) {
1687 conn = list_entry(ctmp, struct ksock_conn, ksnc_list);
1690 conn->ksnc_ipaddr == ipaddr) {
1692 ksocknal_close_conn_locked (conn, why);
1700 ksocknal_close_conn_and_siblings(struct ksock_conn *conn, int why)
1702 struct ksock_peer_ni *peer_ni = conn->ksnc_peer;
1703 u32 ipaddr = conn->ksnc_ipaddr;
1706 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1708 count = ksocknal_close_peer_conns_locked (peer_ni, ipaddr, why);
1710 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1716 ksocknal_close_matching_conns(struct lnet_process_id id, __u32 ipaddr)
1718 struct ksock_peer_ni *peer_ni;
1719 struct hlist_node *pnxt;
1725 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1727 if (id.nid != LNET_NID_ANY) {
1728 lo = hash_min(id.nid, HASH_BITS(ksocknal_data.ksnd_peers));
1732 hi = HASH_SIZE(ksocknal_data.ksnd_peers) - 1;
1735 for (i = lo; i <= hi; i++) {
1736 hlist_for_each_entry_safe(peer_ni, pnxt,
1737 &ksocknal_data.ksnd_peers[i],
1740 if (!((id.nid == LNET_NID_ANY ||
1741 id.nid == peer_ni->ksnp_id.nid) &&
1742 (id.pid == LNET_PID_ANY ||
1743 id.pid == peer_ni->ksnp_id.pid)))
1746 count += ksocknal_close_peer_conns_locked(peer_ni,
1751 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1753 /* wildcards always succeed */
1754 if (id.nid == LNET_NID_ANY || id.pid == LNET_PID_ANY || ipaddr == 0)
1757 return (count == 0 ? -ENOENT : 0);
1761 ksocknal_notify_gw_down(lnet_nid_t gw_nid)
1763 /* The router is telling me she's been notified of a change in
1766 struct lnet_process_id id = {
1768 .pid = LNET_PID_ANY,
1771 CDEBUG(D_NET, "gw %s down\n", libcfs_nid2str(gw_nid));
1773 /* If the gateway crashed, close all open connections... */
1774 ksocknal_close_matching_conns(id, 0);
1777 /* We can only establish new connections
1778 * if we have autroutes, and these connect on demand. */
1782 ksocknal_push_peer(struct ksock_peer_ni *peer_ni)
1786 struct list_head *tmp;
1787 struct ksock_conn *conn;
1789 for (index = 0; ; index++) {
1790 read_lock(&ksocknal_data.ksnd_global_lock);
1795 list_for_each(tmp, &peer_ni->ksnp_conns) {
1797 conn = list_entry(tmp, struct ksock_conn,
1799 ksocknal_conn_addref(conn);
1804 read_unlock(&ksocknal_data.ksnd_global_lock);
1809 ksocknal_lib_push_conn (conn);
1810 ksocknal_conn_decref(conn);
1815 ksocknal_push(struct lnet_ni *ni, struct lnet_process_id id)
1822 if (id.nid != LNET_NID_ANY) {
1823 lo = hash_min(id.nid, HASH_BITS(ksocknal_data.ksnd_peers));
1827 hi = HASH_SIZE(ksocknal_data.ksnd_peers) - 1;
1830 for (bkt = lo; bkt <= hi; bkt++) {
1831 int peer_off; /* searching offset in peer_ni hash table */
1833 for (peer_off = 0; ; peer_off++) {
1834 struct ksock_peer_ni *peer_ni;
1837 read_lock(&ksocknal_data.ksnd_global_lock);
1838 hlist_for_each_entry(peer_ni,
1839 &ksocknal_data.ksnd_peers[bkt],
1841 if (!((id.nid == LNET_NID_ANY ||
1842 id.nid == peer_ni->ksnp_id.nid) &&
1843 (id.pid == LNET_PID_ANY ||
1844 id.pid == peer_ni->ksnp_id.pid)))
1847 if (i++ == peer_off) {
1848 ksocknal_peer_addref(peer_ni);
1852 read_unlock(&ksocknal_data.ksnd_global_lock);
1854 if (i <= peer_off) /* no match */
1858 ksocknal_push_peer(peer_ni);
1859 ksocknal_peer_decref(peer_ni);
1866 ksocknal_add_interface(struct lnet_ni *ni, __u32 ipaddress, __u32 netmask)
1868 struct ksock_net *net = ni->ni_data;
1869 struct ksock_interface *iface;
1873 struct ksock_peer_ni *peer_ni;
1874 struct list_head *rtmp;
1875 struct ksock_route *route;
1877 if (ipaddress == 0 ||
1881 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1883 iface = ksocknal_ip2iface(ni, ipaddress);
1884 if (iface != NULL) {
1885 /* silently ignore dups */
1887 } else if (net->ksnn_ninterfaces == LNET_INTERFACES_NUM) {
1890 iface = &net->ksnn_interfaces[net->ksnn_ninterfaces++];
1892 iface->ksni_ipaddr = ipaddress;
1893 iface->ksni_netmask = netmask;
1894 iface->ksni_nroutes = 0;
1895 iface->ksni_npeers = 0;
1897 hash_for_each(ksocknal_data.ksnd_peers, i, peer_ni, ksnp_list) {
1898 for (j = 0; j < peer_ni->ksnp_n_passive_ips; j++)
1899 if (peer_ni->ksnp_passive_ips[j] == ipaddress)
1900 iface->ksni_npeers++;
1902 list_for_each(rtmp, &peer_ni->ksnp_routes) {
1903 route = list_entry(rtmp,
1907 if (route->ksnr_myipaddr == ipaddress)
1908 iface->ksni_nroutes++;
1913 /* NB only new connections will pay attention to the new
1918 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1924 ksocknal_peer_del_interface_locked(struct ksock_peer_ni *peer_ni, __u32 ipaddr)
1926 struct list_head *tmp;
1927 struct list_head *nxt;
1928 struct ksock_route *route;
1929 struct ksock_conn *conn;
1933 for (i = 0; i < peer_ni->ksnp_n_passive_ips; i++)
1934 if (peer_ni->ksnp_passive_ips[i] == ipaddr) {
1935 for (j = i+1; j < peer_ni->ksnp_n_passive_ips; j++)
1936 peer_ni->ksnp_passive_ips[j-1] =
1937 peer_ni->ksnp_passive_ips[j];
1938 peer_ni->ksnp_n_passive_ips--;
1942 list_for_each_safe(tmp, nxt, &peer_ni->ksnp_routes) {
1943 route = list_entry(tmp, struct ksock_route, ksnr_list);
1945 if (route->ksnr_myipaddr != ipaddr)
1948 if (route->ksnr_share_count != 0) {
1949 /* Manually created; keep, but unbind */
1950 route->ksnr_myipaddr = 0;
1952 ksocknal_del_route_locked(route);
1956 list_for_each_safe(tmp, nxt, &peer_ni->ksnp_conns) {
1957 conn = list_entry(tmp, struct ksock_conn, ksnc_list);
1959 if (conn->ksnc_myipaddr == ipaddr)
1960 ksocknal_close_conn_locked (conn, 0);
1965 ksocknal_del_interface(struct lnet_ni *ni, __u32 ipaddress)
1967 struct ksock_net *net = ni->ni_data;
1969 struct hlist_node *nxt;
1970 struct ksock_peer_ni *peer_ni;
1975 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1977 for (i = 0; i < net->ksnn_ninterfaces; i++) {
1978 this_ip = net->ksnn_interfaces[i].ksni_ipaddr;
1980 if (!(ipaddress == 0 ||
1981 ipaddress == this_ip))
1986 for (j = i+1; j < net->ksnn_ninterfaces; j++)
1987 net->ksnn_interfaces[j-1] =
1988 net->ksnn_interfaces[j];
1990 net->ksnn_ninterfaces--;
1992 hash_for_each_safe(ksocknal_data.ksnd_peers, j,
1993 nxt, peer_ni, ksnp_list) {
1994 if (peer_ni->ksnp_ni != ni)
1997 ksocknal_peer_del_interface_locked(peer_ni, this_ip);
2001 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
2007 ksocknal_ctl(struct lnet_ni *ni, unsigned int cmd, void *arg)
2009 struct lnet_process_id id = {0};
2010 struct libcfs_ioctl_data *data = arg;
2014 case IOC_LIBCFS_GET_INTERFACE: {
2015 struct ksock_net *net = ni->ni_data;
2016 struct ksock_interface *iface;
2018 read_lock(&ksocknal_data.ksnd_global_lock);
2020 if (data->ioc_count >= (__u32)net->ksnn_ninterfaces) {
2024 iface = &net->ksnn_interfaces[data->ioc_count];
2026 data->ioc_u32[0] = iface->ksni_ipaddr;
2027 data->ioc_u32[1] = iface->ksni_netmask;
2028 data->ioc_u32[2] = iface->ksni_npeers;
2029 data->ioc_u32[3] = iface->ksni_nroutes;
2032 read_unlock(&ksocknal_data.ksnd_global_lock);
2036 case IOC_LIBCFS_ADD_INTERFACE:
2037 return ksocknal_add_interface(ni,
2038 data->ioc_u32[0], /* IP address */
2039 data->ioc_u32[1]); /* net mask */
2041 case IOC_LIBCFS_DEL_INTERFACE:
2042 return ksocknal_del_interface(ni,
2043 data->ioc_u32[0]); /* IP address */
2045 case IOC_LIBCFS_GET_PEER: {
2050 int share_count = 0;
2052 rc = ksocknal_get_peer_info(ni, data->ioc_count,
2053 &id, &myip, &ip, &port,
2054 &conn_count, &share_count);
2058 data->ioc_nid = id.nid;
2059 data->ioc_count = share_count;
2060 data->ioc_u32[0] = ip;
2061 data->ioc_u32[1] = port;
2062 data->ioc_u32[2] = myip;
2063 data->ioc_u32[3] = conn_count;
2064 data->ioc_u32[4] = id.pid;
2068 case IOC_LIBCFS_ADD_PEER:
2069 id.nid = data->ioc_nid;
2070 id.pid = LNET_PID_LUSTRE;
2071 return ksocknal_add_peer (ni, id,
2072 data->ioc_u32[0], /* IP */
2073 data->ioc_u32[1]); /* port */
2075 case IOC_LIBCFS_DEL_PEER:
2076 id.nid = data->ioc_nid;
2077 id.pid = LNET_PID_ANY;
2078 return ksocknal_del_peer (ni, id,
2079 data->ioc_u32[0]); /* IP */
2081 case IOC_LIBCFS_GET_CONN: {
2085 struct ksock_conn *conn = ksocknal_get_conn_by_idx(ni, data->ioc_count);
2090 ksocknal_lib_get_conn_tunables(conn, &txmem, &rxmem, &nagle);
2092 data->ioc_count = txmem;
2093 data->ioc_nid = conn->ksnc_peer->ksnp_id.nid;
2094 data->ioc_flags = nagle;
2095 data->ioc_u32[0] = conn->ksnc_ipaddr;
2096 data->ioc_u32[1] = conn->ksnc_port;
2097 data->ioc_u32[2] = conn->ksnc_myipaddr;
2098 data->ioc_u32[3] = conn->ksnc_type;
2099 data->ioc_u32[4] = conn->ksnc_scheduler->kss_cpt;
2100 data->ioc_u32[5] = rxmem;
2101 data->ioc_u32[6] = conn->ksnc_peer->ksnp_id.pid;
2102 ksocknal_conn_decref(conn);
2106 case IOC_LIBCFS_CLOSE_CONNECTION:
2107 id.nid = data->ioc_nid;
2108 id.pid = LNET_PID_ANY;
2109 return ksocknal_close_matching_conns (id,
2112 case IOC_LIBCFS_REGISTER_MYNID:
2113 /* Ignore if this is a noop */
2114 if (data->ioc_nid == ni->ni_nid)
2117 CERROR("obsolete IOC_LIBCFS_REGISTER_MYNID: %s(%s)\n",
2118 libcfs_nid2str(data->ioc_nid),
2119 libcfs_nid2str(ni->ni_nid));
2122 case IOC_LIBCFS_PUSH_CONNECTION:
2123 id.nid = data->ioc_nid;
2124 id.pid = LNET_PID_ANY;
2125 return ksocknal_push(ni, id);
2134 ksocknal_free_buffers (void)
2136 LASSERT (atomic_read(&ksocknal_data.ksnd_nactive_txs) == 0);
2138 if (ksocknal_data.ksnd_schedulers != NULL)
2139 cfs_percpt_free(ksocknal_data.ksnd_schedulers);
2141 spin_lock(&ksocknal_data.ksnd_tx_lock);
2143 if (!list_empty(&ksocknal_data.ksnd_idle_noop_txs)) {
2144 struct list_head zlist;
2145 struct ksock_tx *tx;
2147 list_add(&zlist, &ksocknal_data.ksnd_idle_noop_txs);
2148 list_del_init(&ksocknal_data.ksnd_idle_noop_txs);
2149 spin_unlock(&ksocknal_data.ksnd_tx_lock);
2151 while (!list_empty(&zlist)) {
2152 tx = list_entry(zlist.next, struct ksock_tx, tx_list);
2153 list_del(&tx->tx_list);
2154 LIBCFS_FREE(tx, tx->tx_desc_size);
2157 spin_unlock(&ksocknal_data.ksnd_tx_lock);
2162 ksocknal_base_shutdown(void)
2164 struct ksock_sched *sched;
2165 struct ksock_peer_ni *peer_ni;
2168 CDEBUG(D_MALLOC, "before NAL cleanup: kmem %d\n",
2169 atomic_read (&libcfs_kmemory));
2170 LASSERT (ksocknal_data.ksnd_nnets == 0);
2172 switch (ksocknal_data.ksnd_init) {
2177 case SOCKNAL_INIT_ALL:
2178 case SOCKNAL_INIT_DATA:
2179 hash_for_each(ksocknal_data.ksnd_peers, i, peer_ni, ksnp_list)
2182 LASSERT(list_empty(&ksocknal_data.ksnd_nets));
2183 LASSERT(list_empty(&ksocknal_data.ksnd_enomem_conns));
2184 LASSERT(list_empty(&ksocknal_data.ksnd_zombie_conns));
2185 LASSERT(list_empty(&ksocknal_data.ksnd_connd_connreqs));
2186 LASSERT(list_empty(&ksocknal_data.ksnd_connd_routes));
2188 if (ksocknal_data.ksnd_schedulers != NULL) {
2189 cfs_percpt_for_each(sched, i,
2190 ksocknal_data.ksnd_schedulers) {
2192 LASSERT(list_empty(&sched->kss_tx_conns));
2193 LASSERT(list_empty(&sched->kss_rx_conns));
2194 LASSERT(list_empty(&sched->kss_zombie_noop_txs));
2195 LASSERT(sched->kss_nconns == 0);
2199 /* flag threads to terminate; wake and wait for them to die */
2200 ksocknal_data.ksnd_shuttingdown = 1;
2201 wake_up_all(&ksocknal_data.ksnd_connd_waitq);
2202 wake_up_all(&ksocknal_data.ksnd_reaper_waitq);
2204 if (ksocknal_data.ksnd_schedulers != NULL) {
2205 cfs_percpt_for_each(sched, i,
2206 ksocknal_data.ksnd_schedulers)
2207 wake_up_all(&sched->kss_waitq);
2211 read_lock(&ksocknal_data.ksnd_global_lock);
2212 while (ksocknal_data.ksnd_nthreads != 0) {
2215 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
2216 "waiting for %d threads to terminate\n",
2217 ksocknal_data.ksnd_nthreads);
2218 read_unlock(&ksocknal_data.ksnd_global_lock);
2219 set_current_state(TASK_UNINTERRUPTIBLE);
2220 schedule_timeout(cfs_time_seconds(1));
2221 read_lock(&ksocknal_data.ksnd_global_lock);
2223 read_unlock(&ksocknal_data.ksnd_global_lock);
2225 ksocknal_free_buffers();
2227 ksocknal_data.ksnd_init = SOCKNAL_INIT_NOTHING;
2231 CDEBUG(D_MALLOC, "after NAL cleanup: kmem %d\n",
2232 atomic_read (&libcfs_kmemory));
2234 module_put(THIS_MODULE);
2238 ksocknal_base_startup(void)
2240 struct ksock_sched *sched;
2244 LASSERT(ksocknal_data.ksnd_init == SOCKNAL_INIT_NOTHING);
2245 LASSERT(ksocknal_data.ksnd_nnets == 0);
2247 memset(&ksocknal_data, 0, sizeof(ksocknal_data)); /* zero pointers */
2249 hash_init(ksocknal_data.ksnd_peers);
2251 rwlock_init(&ksocknal_data.ksnd_global_lock);
2252 INIT_LIST_HEAD(&ksocknal_data.ksnd_nets);
2254 spin_lock_init(&ksocknal_data.ksnd_reaper_lock);
2255 INIT_LIST_HEAD(&ksocknal_data.ksnd_enomem_conns);
2256 INIT_LIST_HEAD(&ksocknal_data.ksnd_zombie_conns);
2257 INIT_LIST_HEAD(&ksocknal_data.ksnd_deathrow_conns);
2258 init_waitqueue_head(&ksocknal_data.ksnd_reaper_waitq);
2260 spin_lock_init(&ksocknal_data.ksnd_connd_lock);
2261 INIT_LIST_HEAD(&ksocknal_data.ksnd_connd_connreqs);
2262 INIT_LIST_HEAD(&ksocknal_data.ksnd_connd_routes);
2263 init_waitqueue_head(&ksocknal_data.ksnd_connd_waitq);
2265 spin_lock_init(&ksocknal_data.ksnd_tx_lock);
2266 INIT_LIST_HEAD(&ksocknal_data.ksnd_idle_noop_txs);
2268 /* NB memset above zeros whole of ksocknal_data */
2270 /* flag lists/ptrs/locks initialised */
2271 ksocknal_data.ksnd_init = SOCKNAL_INIT_DATA;
2272 if (!try_module_get(THIS_MODULE))
2275 /* Create a scheduler block per available CPT */
2276 ksocknal_data.ksnd_schedulers = cfs_percpt_alloc(lnet_cpt_table(),
2278 if (ksocknal_data.ksnd_schedulers == NULL)
2281 cfs_percpt_for_each(sched, i, ksocknal_data.ksnd_schedulers) {
2285 * make sure not to allocate more threads than there are
2286 * cores/CPUs in teh CPT
2288 nthrs = cfs_cpt_weight(lnet_cpt_table(), i);
2289 if (*ksocknal_tunables.ksnd_nscheds > 0) {
2290 nthrs = min(nthrs, *ksocknal_tunables.ksnd_nscheds);
2293 * max to half of CPUs, assume another half should be
2294 * reserved for upper layer modules
2296 nthrs = min(max(SOCKNAL_NSCHEDS, nthrs >> 1), nthrs);
2299 sched->kss_nthreads_max = nthrs;
2302 spin_lock_init(&sched->kss_lock);
2303 INIT_LIST_HEAD(&sched->kss_rx_conns);
2304 INIT_LIST_HEAD(&sched->kss_tx_conns);
2305 INIT_LIST_HEAD(&sched->kss_zombie_noop_txs);
2306 init_waitqueue_head(&sched->kss_waitq);
2309 ksocknal_data.ksnd_connd_starting = 0;
2310 ksocknal_data.ksnd_connd_failed_stamp = 0;
2311 ksocknal_data.ksnd_connd_starting_stamp = ktime_get_real_seconds();
2312 /* must have at least 2 connds to remain responsive to accepts while
2314 if (*ksocknal_tunables.ksnd_nconnds < SOCKNAL_CONND_RESV + 1)
2315 *ksocknal_tunables.ksnd_nconnds = SOCKNAL_CONND_RESV + 1;
2317 if (*ksocknal_tunables.ksnd_nconnds_max <
2318 *ksocknal_tunables.ksnd_nconnds) {
2319 ksocknal_tunables.ksnd_nconnds_max =
2320 ksocknal_tunables.ksnd_nconnds;
2323 for (i = 0; i < *ksocknal_tunables.ksnd_nconnds; i++) {
2325 spin_lock_bh(&ksocknal_data.ksnd_connd_lock);
2326 ksocknal_data.ksnd_connd_starting++;
2327 spin_unlock_bh(&ksocknal_data.ksnd_connd_lock);
2330 snprintf(name, sizeof(name), "socknal_cd%02d", i);
2331 rc = ksocknal_thread_start(ksocknal_connd,
2332 (void *)((uintptr_t)i), name);
2334 spin_lock_bh(&ksocknal_data.ksnd_connd_lock);
2335 ksocknal_data.ksnd_connd_starting--;
2336 spin_unlock_bh(&ksocknal_data.ksnd_connd_lock);
2337 CERROR("Can't spawn socknal connd: %d\n", rc);
2342 rc = ksocknal_thread_start(ksocknal_reaper, NULL, "socknal_reaper");
2344 CERROR ("Can't spawn socknal reaper: %d\n", rc);
2348 /* flag everything initialised */
2349 ksocknal_data.ksnd_init = SOCKNAL_INIT_ALL;
2354 ksocknal_base_shutdown();
2359 ksocknal_debug_peerhash(struct lnet_ni *ni)
2361 struct ksock_peer_ni *peer_ni;
2364 read_lock(&ksocknal_data.ksnd_global_lock);
2366 hash_for_each(ksocknal_data.ksnd_peers, i, peer_ni, ksnp_list) {
2367 struct ksock_route *route;
2368 struct ksock_conn *conn;
2370 if (peer_ni->ksnp_ni != ni)
2373 CWARN("Active peer_ni on shutdown: %s, ref %d, "
2374 "closing %d, accepting %d, err %d, zcookie %llu, "
2375 "txq %d, zc_req %d\n", libcfs_id2str(peer_ni->ksnp_id),
2376 atomic_read(&peer_ni->ksnp_refcount),
2377 peer_ni->ksnp_closing,
2378 peer_ni->ksnp_accepting, peer_ni->ksnp_error,
2379 peer_ni->ksnp_zc_next_cookie,
2380 !list_empty(&peer_ni->ksnp_tx_queue),
2381 !list_empty(&peer_ni->ksnp_zc_req_list));
2383 list_for_each_entry(route, &peer_ni->ksnp_routes, ksnr_list) {
2384 CWARN("Route: ref %d, schd %d, conn %d, cnted %d, "
2385 "del %d\n", atomic_read(&route->ksnr_refcount),
2386 route->ksnr_scheduled, route->ksnr_connecting,
2387 route->ksnr_connected, route->ksnr_deleted);
2390 list_for_each_entry(conn, &peer_ni->ksnp_conns, ksnc_list) {
2391 CWARN("Conn: ref %d, sref %d, t %d, c %d\n",
2392 atomic_read(&conn->ksnc_conn_refcount),
2393 atomic_read(&conn->ksnc_sock_refcount),
2394 conn->ksnc_type, conn->ksnc_closing);
2399 read_unlock(&ksocknal_data.ksnd_global_lock);
2403 ksocknal_shutdown(struct lnet_ni *ni)
2405 struct ksock_net *net = ni->ni_data;
2406 struct lnet_process_id anyid = {
2407 .nid = LNET_NID_ANY,
2408 .pid = LNET_PID_ANY,
2412 LASSERT(ksocknal_data.ksnd_init == SOCKNAL_INIT_ALL);
2413 LASSERT(ksocknal_data.ksnd_nnets > 0);
2415 /* prevent new peers */
2416 atomic_add(SOCKNAL_SHUTDOWN_BIAS, &net->ksnn_npeers);
2418 /* Delete all peers */
2419 ksocknal_del_peer(ni, anyid, 0);
2421 /* Wait for all peer_ni state to clean up */
2423 while (atomic_read(&net->ksnn_npeers) > SOCKNAL_SHUTDOWN_BIAS) {
2425 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET, /* power of 2? */
2426 "waiting for %d peers to disconnect\n",
2427 atomic_read(&net->ksnn_npeers) - SOCKNAL_SHUTDOWN_BIAS);
2428 set_current_state(TASK_UNINTERRUPTIBLE);
2429 schedule_timeout(cfs_time_seconds(1));
2431 ksocknal_debug_peerhash(ni);
2434 for (i = 0; i < net->ksnn_ninterfaces; i++) {
2435 LASSERT(net->ksnn_interfaces[i].ksni_npeers == 0);
2436 LASSERT(net->ksnn_interfaces[i].ksni_nroutes == 0);
2439 list_del(&net->ksnn_list);
2440 LIBCFS_FREE(net, sizeof(*net));
2442 ksocknal_data.ksnd_nnets--;
2443 if (ksocknal_data.ksnd_nnets == 0)
2444 ksocknal_base_shutdown();
2448 ksocknal_search_new_ipif(struct ksock_net *net)
2453 for (i = 0; i < net->ksnn_ninterfaces; i++) {
2454 char *ifnam = &net->ksnn_interfaces[i].ksni_name[0];
2455 char *colon = strchr(ifnam, ':');
2457 struct ksock_net *tmp;
2460 if (colon != NULL) /* ignore alias device */
2463 list_for_each_entry(tmp, &ksocknal_data.ksnd_nets,
2465 for (j = 0; !found && j < tmp->ksnn_ninterfaces; j++) {
2466 char *ifnam2 = &tmp->ksnn_interfaces[j].\
2468 char *colon2 = strchr(ifnam2, ':');
2473 found = strcmp(ifnam, ifnam2) == 0;
2490 ksocknal_start_schedulers(struct ksock_sched *sched)
2496 if (sched->kss_nthreads == 0) {
2497 if (*ksocknal_tunables.ksnd_nscheds > 0) {
2498 nthrs = sched->kss_nthreads_max;
2500 nthrs = cfs_cpt_weight(lnet_cpt_table(),
2502 nthrs = min(max(SOCKNAL_NSCHEDS, nthrs >> 1), nthrs);
2503 nthrs = min(SOCKNAL_NSCHEDS_HIGH, nthrs);
2505 nthrs = min(nthrs, sched->kss_nthreads_max);
2507 LASSERT(sched->kss_nthreads <= sched->kss_nthreads_max);
2508 /* increase two threads if there is new interface */
2509 nthrs = min(2, sched->kss_nthreads_max - sched->kss_nthreads);
2512 for (i = 0; i < nthrs; i++) {
2516 id = KSOCK_THREAD_ID(sched->kss_cpt, sched->kss_nthreads + i);
2517 snprintf(name, sizeof(name), "socknal_sd%02d_%02d",
2518 sched->kss_cpt, (int)KSOCK_THREAD_SID(id));
2520 rc = ksocknal_thread_start(ksocknal_scheduler,
2525 CERROR("Can't spawn thread %d for scheduler[%d]: %d\n",
2526 sched->kss_cpt, (int) KSOCK_THREAD_SID(id), rc);
2530 sched->kss_nthreads += i;
2535 ksocknal_net_start_threads(struct ksock_net *net, __u32 *cpts, int ncpts)
2537 int newif = ksocknal_search_new_ipif(net);
2541 if (ncpts > 0 && ncpts > cfs_cpt_number(lnet_cpt_table()))
2544 for (i = 0; i < ncpts; i++) {
2545 struct ksock_sched *sched;
2546 int cpt = (cpts == NULL) ? i : cpts[i];
2548 LASSERT(cpt < cfs_cpt_number(lnet_cpt_table()));
2549 sched = ksocknal_data.ksnd_schedulers[cpt];
2551 if (!newif && sched->kss_nthreads > 0)
2554 rc = ksocknal_start_schedulers(sched);
2562 ksocknal_startup(struct lnet_ni *ni)
2564 struct ksock_net *net;
2565 struct lnet_ioctl_config_lnd_cmn_tunables *net_tunables;
2566 struct ksock_interface *ksi = NULL;
2567 struct lnet_inetdev *ifaces = NULL;
2571 LASSERT (ni->ni_net->net_lnd == &the_ksocklnd);
2573 if (ksocknal_data.ksnd_init == SOCKNAL_INIT_NOTHING) {
2574 rc = ksocknal_base_startup();
2579 LIBCFS_ALLOC(net, sizeof(*net));
2583 net->ksnn_incarnation = ktime_get_real_ns();
2585 net_tunables = &ni->ni_net->net_tunables;
2587 if (net_tunables->lct_peer_timeout == -1)
2588 net_tunables->lct_peer_timeout =
2589 *ksocknal_tunables.ksnd_peertimeout;
2591 if (net_tunables->lct_max_tx_credits == -1)
2592 net_tunables->lct_max_tx_credits =
2593 *ksocknal_tunables.ksnd_credits;
2595 if (net_tunables->lct_peer_tx_credits == -1)
2596 net_tunables->lct_peer_tx_credits =
2597 *ksocknal_tunables.ksnd_peertxcredits;
2599 if (net_tunables->lct_peer_tx_credits >
2600 net_tunables->lct_max_tx_credits)
2601 net_tunables->lct_peer_tx_credits =
2602 net_tunables->lct_max_tx_credits;
2604 if (net_tunables->lct_peer_rtr_credits == -1)
2605 net_tunables->lct_peer_rtr_credits =
2606 *ksocknal_tunables.ksnd_peerrtrcredits;
2608 rc = lnet_inet_enumerate(&ifaces, ni->ni_net_ns);
2612 if (!ni->ni_interfaces[0]) {
2613 ksi = &net->ksnn_interfaces[0];
2615 /* Use the first discovered interface */
2616 net->ksnn_ninterfaces = 1;
2617 ni->ni_dev_cpt = ifaces[0].li_cpt;
2618 ksi->ksni_ipaddr = ifaces[0].li_ipaddr;
2619 ksi->ksni_netmask = ifaces[0].li_netmask;
2620 strlcpy(ksi->ksni_name, ifaces[0].li_name,
2621 sizeof(ksi->ksni_name));
2623 /* Before Multi-Rail ksocklnd would manage
2624 * multiple interfaces with its own tcp bonding.
2625 * If we encounter an old configuration using
2626 * this tcp bonding approach then we need to
2627 * handle more than one ni_interfaces.
2629 * In Multi-Rail configuration only ONE ni_interface
2630 * should exist. Each IP alias should be mapped to
2631 * each 'struct net_ni'.
2633 for (i = 0; i < LNET_INTERFACES_NUM; i++) {
2636 if (!ni->ni_interfaces[i])
2639 for (j = 0; j < LNET_INTERFACES_NUM; j++) {
2640 if (i != j && ni->ni_interfaces[j] &&
2641 strcmp(ni->ni_interfaces[i],
2642 ni->ni_interfaces[j]) == 0) {
2644 CERROR("ksocklnd: found duplicate %s at %d and %d, rc = %d\n",
2645 ni->ni_interfaces[i], i, j, rc);
2650 for (j = 0; j < rc; j++) {
2651 if (strcmp(ifaces[j].li_name,
2652 ni->ni_interfaces[i]) != 0)
2655 ksi = &net->ksnn_interfaces[j];
2656 ni->ni_dev_cpt = ifaces[j].li_cpt;
2657 ksi->ksni_ipaddr = ifaces[j].li_ipaddr;
2658 ksi->ksni_netmask = ifaces[j].li_netmask;
2659 strlcpy(ksi->ksni_name, ifaces[j].li_name,
2660 sizeof(ksi->ksni_name));
2661 net->ksnn_ninterfaces++;
2665 /* ni_interfaces don't map to all network interfaces */
2666 if (!ksi || net->ksnn_ninterfaces != i) {
2667 CERROR("ksocklnd: requested %d but only %d interfaces found\n",
2668 i, net->ksnn_ninterfaces);
2673 /* call it before add it to ksocknal_data.ksnd_nets */
2674 rc = ksocknal_net_start_threads(net, ni->ni_cpts, ni->ni_ncpts);
2679 ni->ni_nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid), ksi->ksni_ipaddr);
2680 list_add(&net->ksnn_list, &ksocknal_data.ksnd_nets);
2682 ksocknal_data.ksnd_nnets++;
2687 LIBCFS_FREE(net, sizeof(*net));
2689 if (ksocknal_data.ksnd_nnets == 0)
2690 ksocknal_base_shutdown();
2696 static void __exit ksocklnd_exit(void)
2698 lnet_unregister_lnd(&the_ksocklnd);
2701 static const struct lnet_lnd the_ksocklnd = {
2702 .lnd_type = SOCKLND,
2703 .lnd_startup = ksocknal_startup,
2704 .lnd_shutdown = ksocknal_shutdown,
2705 .lnd_ctl = ksocknal_ctl,
2706 .lnd_send = ksocknal_send,
2707 .lnd_recv = ksocknal_recv,
2708 .lnd_notify_peer_down = ksocknal_notify_gw_down,
2709 .lnd_accept = ksocknal_accept,
2712 static int __init ksocklnd_init(void)
2716 /* check ksnr_connected/connecting field large enough */
2717 BUILD_BUG_ON(SOCKLND_CONN_NTYPES > 4);
2718 BUILD_BUG_ON(SOCKLND_CONN_ACK != SOCKLND_CONN_BULK_IN);
2720 rc = ksocknal_tunables_init();
2724 lnet_register_lnd(&the_ksocklnd);
2729 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2730 MODULE_DESCRIPTION("TCP Socket LNet Network Driver");
2731 MODULE_VERSION("2.8.0");
2732 MODULE_LICENSE("GPL");
2734 module_init(ksocklnd_init);
2735 module_exit(ksocklnd_exit);