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>
40 #include <linux/inetdevice.h>
42 #include <linux/sunrpc/addr.h>
44 static const struct lnet_lnd the_ksocklnd;
45 struct ksock_nal_data ksocknal_data;
47 static struct ksock_interface *
48 ksocknal_ip2iface(struct lnet_ni *ni, struct sockaddr *addr)
50 struct ksock_net *net = ni->ni_data;
52 struct ksock_interface *iface;
54 for (i = 0; i < net->ksnn_ninterfaces; i++) {
55 LASSERT(i < LNET_INTERFACES_NUM);
56 iface = &net->ksnn_interfaces[i];
58 if (rpc_cmp_addr((struct sockaddr *)&iface->ksni_addr, addr))
65 static struct ksock_interface *
66 ksocknal_index2iface(struct lnet_ni *ni, int index)
68 struct ksock_net *net = ni->ni_data;
70 struct ksock_interface *iface;
72 for (i = 0; i < net->ksnn_ninterfaces; i++) {
73 LASSERT(i < LNET_INTERFACES_NUM);
74 iface = &net->ksnn_interfaces[i];
76 if (iface->ksni_index == index)
83 static int ksocknal_ip2index(struct sockaddr *addr, struct lnet_ni *ni)
85 struct net_device *dev;
87 DECLARE_CONST_IN_IFADDR(ifa);
89 if (addr->sa_family != AF_INET)
90 /* No IPv6 support yet */
94 for_each_netdev(ni->ni_net_ns, dev) {
95 int flags = dev_get_flags(dev);
96 struct in_device *in_dev;
98 if (flags & IFF_LOOPBACK) /* skip the loopback IF */
101 if (!(flags & IFF_UP))
104 in_dev = __in_dev_get_rcu(dev);
108 in_dev_for_each_ifa_rcu(ifa, in_dev) {
109 if (ifa->ifa_local ==
110 ((struct sockaddr_in *)addr)->sin_addr.s_addr)
122 static struct ksock_route *
123 ksocknal_create_route(struct sockaddr *addr)
125 struct ksock_route *route;
127 LIBCFS_ALLOC (route, sizeof (*route));
131 refcount_set(&route->ksnr_refcount, 1);
132 route->ksnr_peer = NULL;
133 route->ksnr_retry_interval = 0; /* OK to connect at any time */
134 rpc_copy_addr((struct sockaddr *)&route->ksnr_addr, addr);
135 rpc_set_port((struct sockaddr *)&route->ksnr_addr, rpc_get_port(addr));
136 route->ksnr_myiface = -1;
137 route->ksnr_scheduled = 0;
138 route->ksnr_connecting = 0;
139 route->ksnr_connected = 0;
140 route->ksnr_deleted = 0;
141 route->ksnr_conn_count = 0;
142 route->ksnr_share_count = 0;
148 ksocknal_destroy_route(struct ksock_route *route)
150 LASSERT(refcount_read(&route->ksnr_refcount) == 0);
152 if (route->ksnr_peer != NULL)
153 ksocknal_peer_decref(route->ksnr_peer);
155 LIBCFS_FREE (route, sizeof (*route));
158 static struct ksock_peer_ni *
159 ksocknal_create_peer(struct lnet_ni *ni, struct lnet_process_id id)
161 int cpt = lnet_cpt_of_nid(id.nid, ni);
162 struct ksock_net *net = ni->ni_data;
163 struct ksock_peer_ni *peer_ni;
165 LASSERT(id.nid != LNET_NID_ANY);
166 LASSERT(id.pid != LNET_PID_ANY);
167 LASSERT(!in_interrupt());
169 if (!atomic_inc_unless_negative(&net->ksnn_npeers)) {
170 CERROR("Can't create peer_ni: network shutdown\n");
171 return ERR_PTR(-ESHUTDOWN);
174 LIBCFS_CPT_ALLOC(peer_ni, lnet_cpt_table(), cpt, sizeof(*peer_ni));
176 atomic_dec(&net->ksnn_npeers);
177 return ERR_PTR(-ENOMEM);
180 peer_ni->ksnp_ni = ni;
181 peer_ni->ksnp_id = id;
182 refcount_set(&peer_ni->ksnp_refcount, 1); /* 1 ref for caller */
183 peer_ni->ksnp_closing = 0;
184 peer_ni->ksnp_accepting = 0;
185 peer_ni->ksnp_proto = NULL;
186 peer_ni->ksnp_last_alive = 0;
187 peer_ni->ksnp_zc_next_cookie = SOCKNAL_KEEPALIVE_PING + 1;
189 INIT_LIST_HEAD(&peer_ni->ksnp_conns);
190 INIT_LIST_HEAD(&peer_ni->ksnp_routes);
191 INIT_LIST_HEAD(&peer_ni->ksnp_tx_queue);
192 INIT_LIST_HEAD(&peer_ni->ksnp_zc_req_list);
193 spin_lock_init(&peer_ni->ksnp_lock);
199 ksocknal_destroy_peer(struct ksock_peer_ni *peer_ni)
201 struct ksock_net *net = peer_ni->ksnp_ni->ni_data;
203 CDEBUG (D_NET, "peer_ni %s %p deleted\n",
204 libcfs_id2str(peer_ni->ksnp_id), peer_ni);
206 LASSERT(refcount_read(&peer_ni->ksnp_refcount) == 0);
207 LASSERT(peer_ni->ksnp_accepting == 0);
208 LASSERT(list_empty(&peer_ni->ksnp_conns));
209 LASSERT(list_empty(&peer_ni->ksnp_routes));
210 LASSERT(list_empty(&peer_ni->ksnp_tx_queue));
211 LASSERT(list_empty(&peer_ni->ksnp_zc_req_list));
213 LIBCFS_FREE(peer_ni, sizeof(*peer_ni));
215 /* NB a peer_ni's connections and routes keep a reference on their
216 * peer_ni until they are destroyed, so we can be assured that _all_
217 * state to do with this peer_ni has been cleaned up when its refcount
220 if (atomic_dec_and_test(&net->ksnn_npeers))
221 wake_up_var(&net->ksnn_npeers);
224 struct ksock_peer_ni *
225 ksocknal_find_peer_locked(struct lnet_ni *ni, struct lnet_process_id id)
227 struct ksock_peer_ni *peer_ni;
229 hash_for_each_possible(ksocknal_data.ksnd_peers, peer_ni,
231 LASSERT(!peer_ni->ksnp_closing);
233 if (peer_ni->ksnp_ni != ni)
236 if (peer_ni->ksnp_id.nid != id.nid ||
237 peer_ni->ksnp_id.pid != id.pid)
240 CDEBUG(D_NET, "got peer_ni [%p] -> %s (%d)\n",
241 peer_ni, libcfs_id2str(id),
242 refcount_read(&peer_ni->ksnp_refcount));
248 struct ksock_peer_ni *
249 ksocknal_find_peer(struct lnet_ni *ni, struct lnet_process_id id)
251 struct ksock_peer_ni *peer_ni;
253 read_lock(&ksocknal_data.ksnd_global_lock);
254 peer_ni = ksocknal_find_peer_locked(ni, id);
255 if (peer_ni != NULL) /* +1 ref for caller? */
256 ksocknal_peer_addref(peer_ni);
257 read_unlock(&ksocknal_data.ksnd_global_lock);
263 ksocknal_unlink_peer_locked(struct ksock_peer_ni *peer_ni)
266 struct ksock_interface *iface;
268 for (i = 0; i < peer_ni->ksnp_n_passive_ips; i++) {
269 struct sockaddr_in sa = { .sin_family = AF_INET };
270 LASSERT(i < LNET_INTERFACES_NUM);
271 sa.sin_addr.s_addr = htonl(peer_ni->ksnp_passive_ips[i]);
273 iface = ksocknal_ip2iface(peer_ni->ksnp_ni,
274 (struct sockaddr *)&sa);
276 * All IPs in peer_ni->ksnp_passive_ips[] come from the
277 * interface list, therefore the call must succeed.
279 LASSERT(iface != NULL);
281 CDEBUG(D_NET, "peer_ni=%p iface=%p ksni_nroutes=%d\n",
282 peer_ni, iface, iface->ksni_nroutes);
283 iface->ksni_npeers--;
286 LASSERT(list_empty(&peer_ni->ksnp_conns));
287 LASSERT(list_empty(&peer_ni->ksnp_routes));
288 LASSERT(!peer_ni->ksnp_closing);
289 peer_ni->ksnp_closing = 1;
290 hlist_del(&peer_ni->ksnp_list);
291 /* lose peerlist's ref */
292 ksocknal_peer_decref(peer_ni);
296 ksocknal_get_peer_info(struct lnet_ni *ni, int index,
297 struct lnet_process_id *id, __u32 *myip, __u32 *peer_ip,
298 int *port, int *conn_count, int *share_count)
300 struct ksock_peer_ni *peer_ni;
301 struct ksock_route *route;
302 struct list_head *rtmp;
307 read_lock(&ksocknal_data.ksnd_global_lock);
309 hash_for_each(ksocknal_data.ksnd_peers, i, peer_ni, ksnp_list) {
311 if (peer_ni->ksnp_ni != ni)
314 if (peer_ni->ksnp_n_passive_ips == 0 &&
315 list_empty(&peer_ni->ksnp_routes)) {
319 *id = peer_ni->ksnp_id;
329 for (j = 0; j < peer_ni->ksnp_n_passive_ips; j++) {
333 *id = peer_ni->ksnp_id;
334 *myip = peer_ni->ksnp_passive_ips[j];
343 list_for_each(rtmp, &peer_ni->ksnp_routes) {
347 route = list_entry(rtmp, struct ksock_route,
350 *id = peer_ni->ksnp_id;
351 if (route->ksnr_addr.ss_family == AF_INET) {
352 struct sockaddr_in *sa =
353 (void *)&route->ksnr_addr;
354 rc = choose_ipv4_src(
357 ntohl(sa->sin_addr.s_addr),
359 *peer_ip = ntohl(sa->sin_addr.s_addr);
360 *port = ntohs(sa->sin_port);
363 *peer_ip = 0xFFFFFFFF;
367 *conn_count = route->ksnr_conn_count;
368 *share_count = route->ksnr_share_count;
373 read_unlock(&ksocknal_data.ksnd_global_lock);
378 ksocknal_associate_route_conn_locked(struct ksock_route *route,
379 struct ksock_conn *conn)
381 struct ksock_peer_ni *peer_ni = route->ksnr_peer;
382 int type = conn->ksnc_type;
383 struct ksock_interface *iface;
385 ksocknal_ip2index((struct sockaddr *)&conn->ksnc_myaddr,
386 route->ksnr_peer->ksnp_ni);
388 conn->ksnc_route = route;
389 ksocknal_route_addref(route);
391 if (route->ksnr_myiface != conn_iface) {
392 if (route->ksnr_myiface < 0) {
393 /* route wasn't bound locally yet (the initial route) */
394 CDEBUG(D_NET, "Binding %s %pIS to interface %d\n",
395 libcfs_id2str(peer_ni->ksnp_id),
400 "Rebinding %s %pIS from interface %d to %d\n",
401 libcfs_id2str(peer_ni->ksnp_id),
406 iface = ksocknal_index2iface(route->ksnr_peer->ksnp_ni,
407 route->ksnr_myiface);
409 iface->ksni_nroutes--;
411 route->ksnr_myiface = conn_iface;
412 iface = ksocknal_index2iface(route->ksnr_peer->ksnp_ni,
413 route->ksnr_myiface);
415 iface->ksni_nroutes++;
418 route->ksnr_connected |= (1<<type);
419 route->ksnr_conn_count++;
421 /* Successful connection => further attempts can
422 * proceed immediately
424 route->ksnr_retry_interval = 0;
428 ksocknal_add_route_locked(struct ksock_peer_ni *peer_ni, struct ksock_route *route)
430 struct list_head *tmp;
431 struct ksock_conn *conn;
432 struct ksock_route *route2;
433 struct ksock_net *net = peer_ni->ksnp_ni->ni_data;
435 LASSERT(!peer_ni->ksnp_closing);
436 LASSERT(route->ksnr_peer == NULL);
437 LASSERT(!route->ksnr_scheduled);
438 LASSERT(!route->ksnr_connecting);
439 LASSERT(route->ksnr_connected == 0);
440 LASSERT(net->ksnn_ninterfaces > 0);
442 /* LASSERT(unique) */
443 list_for_each(tmp, &peer_ni->ksnp_routes) {
444 route2 = list_entry(tmp, struct ksock_route, ksnr_list);
446 if (rpc_cmp_addr((struct sockaddr *)&route2->ksnr_addr,
447 (struct sockaddr *)&route->ksnr_addr)) {
448 CERROR("Duplicate route %s %pI4h\n",
449 libcfs_id2str(peer_ni->ksnp_id),
455 route->ksnr_peer = peer_ni;
456 ksocknal_peer_addref(peer_ni);
458 /* set the route's interface to the current net's interface */
459 route->ksnr_myiface = net->ksnn_interfaces[0].ksni_index;
460 net->ksnn_interfaces[0].ksni_nroutes++;
462 /* peer_ni's routelist takes over my ref on 'route' */
463 list_add_tail(&route->ksnr_list, &peer_ni->ksnp_routes);
465 list_for_each(tmp, &peer_ni->ksnp_conns) {
466 conn = list_entry(tmp, struct ksock_conn, ksnc_list);
468 if (!rpc_cmp_addr((struct sockaddr *)&conn->ksnc_peeraddr,
469 (struct sockaddr *)&route->ksnr_addr))
472 ksocknal_associate_route_conn_locked(route, conn);
473 /* keep going (typed routes) */
478 ksocknal_del_route_locked(struct ksock_route *route)
480 struct ksock_peer_ni *peer_ni = route->ksnr_peer;
481 struct ksock_interface *iface;
482 struct ksock_conn *conn;
483 struct ksock_conn *cnxt;
485 LASSERT(!route->ksnr_deleted);
487 /* Close associated conns */
488 list_for_each_entry_safe(conn, cnxt, &peer_ni->ksnp_conns, ksnc_list) {
489 if (conn->ksnc_route != route)
492 ksocknal_close_conn_locked(conn, 0);
495 if (route->ksnr_myiface >= 0) {
496 iface = ksocknal_index2iface(route->ksnr_peer->ksnp_ni,
497 route->ksnr_myiface);
499 iface->ksni_nroutes--;
502 route->ksnr_deleted = 1;
503 list_del(&route->ksnr_list);
504 ksocknal_route_decref(route); /* drop peer_ni's ref */
506 if (list_empty(&peer_ni->ksnp_routes) &&
507 list_empty(&peer_ni->ksnp_conns)) {
508 /* I've just removed the last route to a peer_ni with no active
510 ksocknal_unlink_peer_locked(peer_ni);
515 ksocknal_add_peer(struct lnet_ni *ni, struct lnet_process_id id,
516 struct sockaddr *addr)
518 struct list_head *tmp;
519 struct ksock_peer_ni *peer_ni;
520 struct ksock_peer_ni *peer2;
521 struct ksock_route *route;
522 struct ksock_route *route2;
524 if (id.nid == LNET_NID_ANY ||
525 id.pid == LNET_PID_ANY)
528 /* Have a brand new peer_ni ready... */
529 peer_ni = ksocknal_create_peer(ni, id);
531 return PTR_ERR(peer_ni);
533 route = ksocknal_create_route(addr);
535 ksocknal_peer_decref(peer_ni);
539 write_lock_bh(&ksocknal_data.ksnd_global_lock);
541 /* always called with a ref on ni, so shutdown can't have started */
542 LASSERT(atomic_read(&((struct ksock_net *)ni->ni_data)->ksnn_npeers)
545 peer2 = ksocknal_find_peer_locked(ni, id);
547 ksocknal_peer_decref(peer_ni);
550 /* peer_ni table takes my ref on peer_ni */
551 hash_add(ksocknal_data.ksnd_peers, &peer_ni->ksnp_list, id.nid);
555 list_for_each(tmp, &peer_ni->ksnp_routes) {
556 route2 = list_entry(tmp, struct ksock_route, ksnr_list);
558 if (rpc_cmp_addr(addr, (struct sockaddr *)&route2->ksnr_addr))
563 if (route2 == NULL) {
564 ksocknal_add_route_locked(peer_ni, route);
565 route->ksnr_share_count++;
567 ksocknal_route_decref(route);
568 route2->ksnr_share_count++;
571 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
577 ksocknal_del_peer_locked(struct ksock_peer_ni *peer_ni, __u32 ip)
579 struct ksock_conn *conn;
580 struct ksock_conn *cnxt;
581 struct ksock_route *route;
582 struct ksock_route *rnxt;
585 LASSERT(!peer_ni->ksnp_closing);
587 /* Extra ref prevents peer_ni disappearing until I'm done with it */
588 ksocknal_peer_addref(peer_ni);
590 list_for_each_entry_safe(route, rnxt, &peer_ni->ksnp_routes,
594 if (route->ksnr_addr.ss_family != AF_INET)
596 if (((struct sockaddr_in *)&route->ksnr_addr)
597 ->sin_addr.s_addr != htonl(ip))
601 route->ksnr_share_count = 0;
602 /* This deletes associated conns too */
603 ksocknal_del_route_locked(route);
607 list_for_each_entry(route, &peer_ni->ksnp_routes, ksnr_list)
608 nshared += route->ksnr_share_count;
611 /* remove everything else if there are no explicit entries
614 list_for_each_entry_safe(route, rnxt, &peer_ni->ksnp_routes,
616 /* we should only be removing auto-entries */
617 LASSERT(route->ksnr_share_count == 0);
618 ksocknal_del_route_locked(route);
621 list_for_each_entry_safe(conn, cnxt, &peer_ni->ksnp_conns,
623 ksocknal_close_conn_locked(conn, 0);
626 ksocknal_peer_decref(peer_ni);
627 /* NB peer_ni unlinks itself when last conn/route is removed */
631 ksocknal_del_peer(struct lnet_ni *ni, struct lnet_process_id id, __u32 ip)
634 struct hlist_node *pnxt;
635 struct ksock_peer_ni *peer_ni;
641 write_lock_bh(&ksocknal_data.ksnd_global_lock);
643 if (id.nid != LNET_NID_ANY) {
644 lo = hash_min(id.nid, HASH_BITS(ksocknal_data.ksnd_peers));
648 hi = HASH_SIZE(ksocknal_data.ksnd_peers) - 1;
651 for (i = lo; i <= hi; i++) {
652 hlist_for_each_entry_safe(peer_ni, pnxt,
653 &ksocknal_data.ksnd_peers[i],
655 if (peer_ni->ksnp_ni != ni)
658 if (!((id.nid == LNET_NID_ANY ||
659 peer_ni->ksnp_id.nid == id.nid) &&
660 (id.pid == LNET_PID_ANY ||
661 peer_ni->ksnp_id.pid == id.pid)))
664 ksocknal_peer_addref(peer_ni); /* a ref for me... */
666 ksocknal_del_peer_locked(peer_ni, ip);
668 if (peer_ni->ksnp_closing &&
669 !list_empty(&peer_ni->ksnp_tx_queue)) {
670 LASSERT(list_empty(&peer_ni->ksnp_conns));
671 LASSERT(list_empty(&peer_ni->ksnp_routes));
673 list_splice_init(&peer_ni->ksnp_tx_queue,
677 ksocknal_peer_decref(peer_ni); /* ...till here */
679 rc = 0; /* matched! */
683 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
685 ksocknal_txlist_done(ni, &zombies, -ENETDOWN);
690 static struct ksock_conn *
691 ksocknal_get_conn_by_idx(struct lnet_ni *ni, int index)
693 struct ksock_peer_ni *peer_ni;
694 struct ksock_conn *conn;
695 struct list_head *ctmp;
698 read_lock(&ksocknal_data.ksnd_global_lock);
700 hash_for_each(ksocknal_data.ksnd_peers, i, peer_ni, ksnp_list) {
701 LASSERT(!peer_ni->ksnp_closing);
703 if (peer_ni->ksnp_ni != ni)
706 list_for_each(ctmp, &peer_ni->ksnp_conns) {
710 conn = list_entry(ctmp, struct ksock_conn,
712 ksocknal_conn_addref(conn);
713 read_unlock(&ksocknal_data.ksnd_global_lock);
718 read_unlock(&ksocknal_data.ksnd_global_lock);
722 static struct ksock_sched *
723 ksocknal_choose_scheduler_locked(unsigned int cpt)
725 struct ksock_sched *sched = ksocknal_data.ksnd_schedulers[cpt];
728 if (sched->kss_nthreads == 0) {
729 cfs_percpt_for_each(sched, i, ksocknal_data.ksnd_schedulers) {
730 if (sched->kss_nthreads > 0) {
731 CDEBUG(D_NET, "scheduler[%d] has no threads. selected scheduler[%d]\n",
732 cpt, sched->kss_cpt);
743 ksocknal_local_ipvec(struct lnet_ni *ni, __u32 *ipaddrs)
745 struct ksock_net *net = ni->ni_data;
749 read_lock(&ksocknal_data.ksnd_global_lock);
751 nip = net->ksnn_ninterfaces;
752 LASSERT(nip <= LNET_INTERFACES_NUM);
754 for (i = 0, j = 0; i < nip; i++)
755 if (net->ksnn_interfaces[i].ksni_addr.ss_family == AF_INET) {
756 struct sockaddr_in *sa =
757 (void *)&net->ksnn_interfaces[i].ksni_addr;
759 ipaddrs[j] = ntohl(sa->sin_addr.s_addr);
760 LASSERT(ipaddrs[j] != 0);
765 read_unlock(&ksocknal_data.ksnd_global_lock);
767 * Only offer interfaces for additional connections if I have
770 return nip < 2 ? 0 : nip;
774 ksocknal_match_peerip(struct ksock_interface *iface, __u32 *ips, int nips)
776 int best_netmatch = 0;
782 struct sockaddr_in *sa;
785 sa = (struct sockaddr_in *)&iface->ksni_addr;
786 LASSERT(sa->sin_family == AF_INET);
787 ip = ntohl(sa->sin_addr.s_addr);
789 for (i = 0; i < nips; i++) {
793 this_xor = ips[i] ^ ip;
794 this_netmatch = ((this_xor & iface->ksni_netmask) == 0) ? 1 : 0;
797 best_netmatch < this_netmatch ||
798 (best_netmatch == this_netmatch &&
799 best_xor > this_xor)))
803 best_netmatch = this_netmatch;
812 ksocknal_select_ips(struct ksock_peer_ni *peer_ni, __u32 *peerips, int n_peerips)
814 rwlock_t *global_lock = &ksocknal_data.ksnd_global_lock;
815 struct ksock_net *net = peer_ni->ksnp_ni->ni_data;
816 struct ksock_interface *iface;
817 struct ksock_interface *best_iface;
828 /* CAVEAT EMPTOR: We do all our interface matching with an
829 * exclusive hold of global lock at IRQ priority. We're only
830 * expecting to be dealing with small numbers of interfaces, so the
831 * O(n**3)-ness shouldn't matter */
833 /* Also note that I'm not going to return more than n_peerips
834 * interfaces, even if I have more myself */
836 write_lock_bh(global_lock);
838 LASSERT(n_peerips <= LNET_INTERFACES_NUM);
839 LASSERT(net->ksnn_ninterfaces <= LNET_INTERFACES_NUM);
841 /* Only match interfaces for additional connections
842 * if I have > 1 interface
844 n_ips = (net->ksnn_ninterfaces < 2) ? 0 :
845 min(n_peerips, net->ksnn_ninterfaces);
847 for (i = 0; peer_ni->ksnp_n_passive_ips < n_ips; i++) {
848 /* ^ yes really... */
850 /* If we have any new interfaces, first tick off all the
851 * peer_ni IPs that match old interfaces, then choose new
852 * interfaces to match the remaining peer_ni IPS.
853 * We don't forget interfaces we've stopped using; we might
854 * start using them again... */
856 if (i < peer_ni->ksnp_n_passive_ips) {
858 struct sockaddr_in sa = { .sin_family = AF_INET};
861 htonl(peer_ni->ksnp_passive_ips[i]);
862 best_iface = ksocknal_ip2iface(peer_ni->ksnp_ni,
863 (struct sockaddr *)&sa);
865 /* peer_ni passive ips are kept up to date */
866 LASSERT(best_iface != NULL);
868 /* choose a new interface */
869 struct sockaddr_in *sa;
871 LASSERT (i == peer_ni->ksnp_n_passive_ips);
877 for (j = 0; j < net->ksnn_ninterfaces; j++) {
878 iface = &net->ksnn_interfaces[j];
879 sa = (void *)&iface->ksni_addr;
880 if (sa->sin_family != AF_INET)
882 ip = ntohl(sa->sin_addr.s_addr);
884 for (k = 0; k < peer_ni->ksnp_n_passive_ips; k++)
885 if (peer_ni->ksnp_passive_ips[k] == ip)
888 if (k < peer_ni->ksnp_n_passive_ips) /* using it already */
891 k = ksocknal_match_peerip(iface, peerips, n_peerips);
892 xor = (ip ^ peerips[k]);
893 this_netmatch = ((xor & iface->ksni_netmask) == 0) ? 1 : 0;
895 if (!(best_iface == NULL ||
896 best_netmatch < this_netmatch ||
897 (best_netmatch == this_netmatch &&
898 best_npeers > iface->ksni_npeers)))
902 best_netmatch = this_netmatch;
903 best_npeers = iface->ksni_npeers;
906 LASSERT(best_iface != NULL);
908 best_iface->ksni_npeers++;
909 sa = (void *)&best_iface->ksni_addr;
910 ip = ntohl(sa->sin_addr.s_addr);
911 peer_ni->ksnp_passive_ips[i] = ip;
912 peer_ni->ksnp_n_passive_ips = i+1;
915 /* mark the best matching peer_ni IP used */
916 j = ksocknal_match_peerip(best_iface, peerips, n_peerips);
920 /* Overwrite input peer_ni IP addresses */
921 memcpy(peerips, peer_ni->ksnp_passive_ips, n_ips * sizeof(*peerips));
923 write_unlock_bh(global_lock);
929 ksocknal_create_routes(struct ksock_peer_ni *peer_ni, int port,
930 __u32 *peer_ipaddrs, int npeer_ipaddrs)
932 struct ksock_route *newroute = NULL;
933 rwlock_t *global_lock = &ksocknal_data.ksnd_global_lock;
934 struct lnet_ni *ni = peer_ni->ksnp_ni;
935 struct ksock_net *net = ni->ni_data;
936 struct list_head *rtmp;
937 struct ksock_route *route;
938 struct ksock_interface *iface;
939 struct ksock_interface *best_iface;
946 /* CAVEAT EMPTOR: We do all our interface matching with an
947 * exclusive hold of global lock at IRQ priority. We're only
948 * expecting to be dealing with small numbers of interfaces, so the
949 * O(n**3)-ness here shouldn't matter */
951 write_lock_bh(global_lock);
953 if (net->ksnn_ninterfaces < 2) {
954 /* Only create additional connections
955 * if I have > 1 interface */
956 write_unlock_bh(global_lock);
960 LASSERT(npeer_ipaddrs <= LNET_INTERFACES_NUM);
962 for (i = 0; i < npeer_ipaddrs; i++) {
964 struct sockaddr_in *sa = (void *)&newroute->ksnr_addr;
966 memset(sa, 0, sizeof(*sa));
967 sa->sin_family = AF_INET;
968 sa->sin_addr.s_addr = htonl(peer_ipaddrs[i]);
970 struct sockaddr_in sa = {.sin_family = AF_INET};
972 write_unlock_bh(global_lock);
974 sa.sin_addr.s_addr = htonl(peer_ipaddrs[i]);
975 sa.sin_port = htons(port);
977 ksocknal_create_route((struct sockaddr *)&sa);
981 write_lock_bh(global_lock);
984 if (peer_ni->ksnp_closing) {
985 /* peer_ni got closed under me */
989 /* Already got a route? */
991 list_for_each(rtmp, &peer_ni->ksnp_routes) {
992 route = list_entry(rtmp, struct ksock_route, ksnr_list);
995 (struct sockaddr *)&route->ksnr_addr,
996 (struct sockaddr *)&newroute->ksnr_addr))
1008 LASSERT(net->ksnn_ninterfaces <= LNET_INTERFACES_NUM);
1010 /* Select interface to connect from */
1011 for (j = 0; j < net->ksnn_ninterfaces; j++) {
1012 __u32 iface_ip, route_ip;
1014 iface = &net->ksnn_interfaces[j];
1016 /* Using this interface already? */
1017 list_for_each(rtmp, &peer_ni->ksnp_routes) {
1018 route = list_entry(rtmp, struct ksock_route,
1021 if (route->ksnr_myiface == iface->ksni_index)
1028 if (iface->ksni_addr.ss_family != AF_INET)
1030 if (newroute->ksnr_addr.ss_family != AF_INET)
1034 ntohl(((struct sockaddr_in *)
1035 &iface->ksni_addr)->sin_addr.s_addr);
1037 ntohl(((struct sockaddr_in *)
1038 &newroute->ksnr_addr)->sin_addr.s_addr);
1040 this_netmatch = (((iface_ip ^ route_ip) &
1041 iface->ksni_netmask) == 0) ? 1 : 0;
1043 if (!(best_iface == NULL ||
1044 best_netmatch < this_netmatch ||
1045 (best_netmatch == this_netmatch &&
1046 best_nroutes > iface->ksni_nroutes)))
1050 best_netmatch = this_netmatch;
1051 best_nroutes = iface->ksni_nroutes;
1054 if (best_iface == NULL)
1057 newroute->ksnr_myiface = best_iface->ksni_index;
1058 best_iface->ksni_nroutes++;
1060 ksocknal_add_route_locked(peer_ni, newroute);
1064 write_unlock_bh(global_lock);
1065 if (newroute != NULL)
1066 ksocknal_route_decref(newroute);
1070 ksocknal_accept(struct lnet_ni *ni, struct socket *sock)
1072 struct ksock_connreq *cr;
1074 struct sockaddr_storage peer;
1076 rc = lnet_sock_getaddr(sock, true, &peer);
1077 LASSERT(rc == 0); /* we succeeded before */
1079 LIBCFS_ALLOC(cr, sizeof(*cr));
1081 LCONSOLE_ERROR_MSG(0x12f,
1082 "Dropping connection request from %pIS: memory exhausted\n",
1089 cr->ksncr_sock = sock;
1091 spin_lock_bh(&ksocknal_data.ksnd_connd_lock);
1093 list_add_tail(&cr->ksncr_list, &ksocknal_data.ksnd_connd_connreqs);
1094 wake_up(&ksocknal_data.ksnd_connd_waitq);
1096 spin_unlock_bh(&ksocknal_data.ksnd_connd_lock);
1101 ksocknal_connecting(struct ksock_peer_ni *peer_ni, struct sockaddr *sa)
1103 struct ksock_route *route;
1105 list_for_each_entry(route, &peer_ni->ksnp_routes, ksnr_list) {
1106 if (rpc_cmp_addr((struct sockaddr *)&route->ksnr_addr, sa))
1107 return route->ksnr_connecting;
1113 ksocknal_create_conn(struct lnet_ni *ni, struct ksock_route *route,
1114 struct socket *sock, int type)
1116 rwlock_t *global_lock = &ksocknal_data.ksnd_global_lock;
1118 struct lnet_process_id peerid;
1119 struct list_head *tmp;
1121 struct ksock_conn *conn;
1122 struct ksock_conn *conn2;
1123 struct ksock_peer_ni *peer_ni = NULL;
1124 struct ksock_peer_ni *peer2;
1125 struct ksock_sched *sched;
1126 struct ksock_hello_msg *hello;
1128 struct ksock_tx *tx;
1129 struct ksock_tx *txtmp;
1135 active = (route != NULL);
1137 LASSERT (active == (type != SOCKLND_CONN_NONE));
1139 LIBCFS_ALLOC(conn, sizeof(*conn));
1145 conn->ksnc_peer = NULL;
1146 conn->ksnc_route = NULL;
1147 conn->ksnc_sock = sock;
1148 /* 2 ref, 1 for conn, another extra ref prevents socket
1149 * being closed before establishment of connection */
1150 refcount_set(&conn->ksnc_sock_refcount, 2);
1151 conn->ksnc_type = type;
1152 ksocknal_lib_save_callback(sock, conn);
1153 refcount_set(&conn->ksnc_conn_refcount, 1); /* 1 ref for me */
1155 conn->ksnc_rx_ready = 0;
1156 conn->ksnc_rx_scheduled = 0;
1158 INIT_LIST_HEAD(&conn->ksnc_tx_queue);
1159 conn->ksnc_tx_ready = 0;
1160 conn->ksnc_tx_scheduled = 0;
1161 conn->ksnc_tx_carrier = NULL;
1162 atomic_set (&conn->ksnc_tx_nob, 0);
1164 LIBCFS_ALLOC(hello, offsetof(struct ksock_hello_msg,
1165 kshm_ips[LNET_INTERFACES_NUM]));
1166 if (hello == NULL) {
1171 /* stash conn's local and remote addrs */
1172 rc = ksocknal_lib_get_conn_addrs (conn);
1176 /* Find out/confirm peer_ni's NID and connection type and get the
1177 * vector of interfaces she's willing to let me connect to.
1178 * Passive connections use the listener timeout since the peer_ni sends
1182 peer_ni = route->ksnr_peer;
1183 LASSERT(ni == peer_ni->ksnp_ni);
1185 /* Active connection sends HELLO eagerly */
1186 hello->kshm_nips = ksocknal_local_ipvec(ni, hello->kshm_ips);
1187 peerid = peer_ni->ksnp_id;
1189 write_lock_bh(global_lock);
1190 conn->ksnc_proto = peer_ni->ksnp_proto;
1191 write_unlock_bh(global_lock);
1193 if (conn->ksnc_proto == NULL) {
1194 conn->ksnc_proto = &ksocknal_protocol_v3x;
1195 #if SOCKNAL_VERSION_DEBUG
1196 if (*ksocknal_tunables.ksnd_protocol == 2)
1197 conn->ksnc_proto = &ksocknal_protocol_v2x;
1198 else if (*ksocknal_tunables.ksnd_protocol == 1)
1199 conn->ksnc_proto = &ksocknal_protocol_v1x;
1203 rc = ksocknal_send_hello (ni, conn, peerid.nid, hello);
1207 peerid.nid = LNET_NID_ANY;
1208 peerid.pid = LNET_PID_ANY;
1210 /* Passive, get protocol from peer_ni */
1211 conn->ksnc_proto = NULL;
1214 rc = ksocknal_recv_hello (ni, conn, hello, &peerid, &incarnation);
1218 LASSERT (rc == 0 || active);
1219 LASSERT (conn->ksnc_proto != NULL);
1220 LASSERT (peerid.nid != LNET_NID_ANY);
1222 cpt = lnet_cpt_of_nid(peerid.nid, ni);
1225 ksocknal_peer_addref(peer_ni);
1226 write_lock_bh(global_lock);
1228 peer_ni = ksocknal_create_peer(ni, peerid);
1229 if (IS_ERR(peer_ni)) {
1230 rc = PTR_ERR(peer_ni);
1234 write_lock_bh(global_lock);
1236 /* called with a ref on ni, so shutdown can't have started */
1237 LASSERT(atomic_read(&((struct ksock_net *)ni->ni_data)->ksnn_npeers) >= 0);
1239 peer2 = ksocknal_find_peer_locked(ni, peerid);
1240 if (peer2 == NULL) {
1241 /* NB this puts an "empty" peer_ni in the peer_ni
1242 * table (which takes my ref) */
1243 hash_add(ksocknal_data.ksnd_peers,
1244 &peer_ni->ksnp_list, peerid.nid);
1246 ksocknal_peer_decref(peer_ni);
1251 ksocknal_peer_addref(peer_ni);
1252 peer_ni->ksnp_accepting++;
1254 /* Am I already connecting to this guy? Resolve in
1255 * favour of higher NID...
1257 if (peerid.nid < ni->ni_nid &&
1258 ksocknal_connecting(peer_ni, ((struct sockaddr *)
1259 &conn->ksnc_peeraddr))) {
1261 warn = "connection race resolution";
1266 if (peer_ni->ksnp_closing ||
1267 (active && route->ksnr_deleted)) {
1268 /* peer_ni/route got closed under me */
1270 warn = "peer_ni/route removed";
1274 if (peer_ni->ksnp_proto == NULL) {
1275 /* Never connected before.
1276 * NB recv_hello may have returned EPROTO to signal my peer_ni
1277 * wants a different protocol than the one I asked for.
1279 LASSERT(list_empty(&peer_ni->ksnp_conns));
1281 peer_ni->ksnp_proto = conn->ksnc_proto;
1282 peer_ni->ksnp_incarnation = incarnation;
1285 if (peer_ni->ksnp_proto != conn->ksnc_proto ||
1286 peer_ni->ksnp_incarnation != incarnation) {
1287 /* peer_ni rebooted or I've got the wrong protocol version */
1288 ksocknal_close_peer_conns_locked(peer_ni, NULL, 0);
1290 peer_ni->ksnp_proto = NULL;
1292 warn = peer_ni->ksnp_incarnation != incarnation ?
1293 "peer_ni rebooted" :
1294 "wrong proto version";
1304 warn = "lost conn race";
1307 warn = "retry with different protocol version";
1311 /* Refuse to duplicate an existing connection, unless this is a
1312 * loopback connection */
1313 if (!rpc_cmp_addr((struct sockaddr *)&conn->ksnc_peeraddr,
1314 (struct sockaddr *)&conn->ksnc_myaddr)) {
1315 list_for_each(tmp, &peer_ni->ksnp_conns) {
1316 conn2 = list_entry(tmp, struct ksock_conn, ksnc_list);
1319 (struct sockaddr *)&conn2->ksnc_peeraddr,
1320 (struct sockaddr *)&conn->ksnc_peeraddr) ||
1322 (struct sockaddr *)&conn2->ksnc_myaddr,
1323 (struct sockaddr *)&conn->ksnc_myaddr) ||
1324 conn2->ksnc_type != conn->ksnc_type)
1327 /* Reply on a passive connection attempt so the peer_ni
1328 * realises we're connected. */
1338 /* If the connection created by this route didn't bind to the IP
1339 * address the route connected to, the connection/route matching
1340 * code below probably isn't going to work. */
1342 !rpc_cmp_addr((struct sockaddr *)&route->ksnr_addr,
1343 (struct sockaddr *)&conn->ksnc_peeraddr)) {
1344 CERROR("Route %s %pIS connected to %pIS\n",
1345 libcfs_id2str(peer_ni->ksnp_id),
1347 &conn->ksnc_peeraddr);
1350 /* Search for a route corresponding to the new connection and
1351 * create an association. This allows incoming connections created
1352 * by routes in my peer_ni to match my own route entries so I don't
1353 * continually create duplicate routes. */
1354 list_for_each(tmp, &peer_ni->ksnp_routes) {
1355 route = list_entry(tmp, struct ksock_route, ksnr_list);
1357 if (!rpc_cmp_addr((struct sockaddr *)&route->ksnr_addr,
1358 (struct sockaddr *)&conn->ksnc_peeraddr))
1361 ksocknal_associate_route_conn_locked(route, conn);
1365 conn->ksnc_peer = peer_ni; /* conn takes my ref on peer_ni */
1366 peer_ni->ksnp_last_alive = ktime_get_seconds();
1367 peer_ni->ksnp_send_keepalive = 0;
1368 peer_ni->ksnp_error = 0;
1370 sched = ksocknal_choose_scheduler_locked(cpt);
1372 CERROR("no schedulers available. node is unhealthy\n");
1376 * The cpt might have changed if we ended up selecting a non cpt
1377 * native scheduler. So use the scheduler's cpt instead.
1379 cpt = sched->kss_cpt;
1380 sched->kss_nconns++;
1381 conn->ksnc_scheduler = sched;
1383 conn->ksnc_tx_last_post = ktime_get_seconds();
1384 /* Set the deadline for the outgoing HELLO to drain */
1385 conn->ksnc_tx_bufnob = sock->sk->sk_wmem_queued;
1386 conn->ksnc_tx_deadline = ktime_get_seconds() +
1388 smp_mb(); /* order with adding to peer_ni's conn list */
1390 list_add(&conn->ksnc_list, &peer_ni->ksnp_conns);
1391 ksocknal_conn_addref(conn);
1393 ksocknal_new_packet(conn, 0);
1395 conn->ksnc_zc_capable = ksocknal_lib_zc_capable(conn);
1397 /* Take packets blocking for this connection. */
1398 list_for_each_entry_safe(tx, txtmp, &peer_ni->ksnp_tx_queue, tx_list) {
1399 if (conn->ksnc_proto->pro_match_tx(conn, tx, tx->tx_nonblk) ==
1403 list_del(&tx->tx_list);
1404 ksocknal_queue_tx_locked(tx, conn);
1407 write_unlock_bh(global_lock);
1409 /* We've now got a new connection. Any errors from here on are just
1410 * like "normal" comms errors and we close the connection normally.
1411 * NB (a) we still have to send the reply HELLO for passive
1413 * (b) normal I/O on the conn is blocked until I setup and call the
1417 CDEBUG(D_NET, "New conn %s p %d.x %pIS -> %pISp"
1418 " incarnation:%lld sched[%d]\n",
1419 libcfs_id2str(peerid), conn->ksnc_proto->pro_version,
1420 &conn->ksnc_myaddr, &conn->ksnc_peeraddr,
1424 /* additional routes after interface exchange? */
1425 ksocknal_create_routes(
1427 rpc_get_port((struct sockaddr *)&conn->ksnc_peeraddr),
1428 hello->kshm_ips, hello->kshm_nips);
1430 hello->kshm_nips = ksocknal_select_ips(peer_ni, hello->kshm_ips,
1432 rc = ksocknal_send_hello(ni, conn, peerid.nid, hello);
1435 LIBCFS_FREE(hello, offsetof(struct ksock_hello_msg,
1436 kshm_ips[LNET_INTERFACES_NUM]));
1438 /* setup the socket AFTER I've received hello (it disables
1439 * SO_LINGER). I might call back to the acceptor who may want
1440 * to send a protocol version response and then close the
1441 * socket; this ensures the socket only tears down after the
1442 * response has been sent. */
1444 rc = ksocknal_lib_setup_sock(sock);
1446 write_lock_bh(global_lock);
1448 /* NB my callbacks block while I hold ksnd_global_lock */
1449 ksocknal_lib_set_callback(sock, conn);
1452 peer_ni->ksnp_accepting--;
1454 write_unlock_bh(global_lock);
1457 write_lock_bh(global_lock);
1458 if (!conn->ksnc_closing) {
1459 /* could be closed by another thread */
1460 ksocknal_close_conn_locked(conn, rc);
1462 write_unlock_bh(global_lock);
1463 } else if (ksocknal_connsock_addref(conn) == 0) {
1464 /* Allow I/O to proceed. */
1465 ksocknal_read_callback(conn);
1466 ksocknal_write_callback(conn);
1467 ksocknal_connsock_decref(conn);
1470 ksocknal_connsock_decref(conn);
1471 ksocknal_conn_decref(conn);
1475 if (!peer_ni->ksnp_closing &&
1476 list_empty(&peer_ni->ksnp_conns) &&
1477 list_empty(&peer_ni->ksnp_routes)) {
1478 list_splice_init(&peer_ni->ksnp_tx_queue, &zombies);
1479 ksocknal_unlink_peer_locked(peer_ni);
1482 write_unlock_bh(global_lock);
1486 CERROR("Not creating conn %s type %d: %s\n",
1487 libcfs_id2str(peerid), conn->ksnc_type, warn);
1489 CDEBUG(D_NET, "Not creating conn %s type %d: %s\n",
1490 libcfs_id2str(peerid), conn->ksnc_type, warn);
1495 /* Request retry by replying with CONN_NONE
1496 * ksnc_proto has been set already */
1497 conn->ksnc_type = SOCKLND_CONN_NONE;
1498 hello->kshm_nips = 0;
1499 ksocknal_send_hello(ni, conn, peerid.nid, hello);
1502 write_lock_bh(global_lock);
1503 peer_ni->ksnp_accepting--;
1504 write_unlock_bh(global_lock);
1508 * If we get here without an error code, just use -EALREADY.
1509 * Depending on how we got here, the error may be positive
1510 * or negative. Normalize the value for ksocknal_txlist_done().
1512 rc2 = (rc == 0 ? -EALREADY : (rc > 0 ? -rc : rc));
1513 ksocknal_txlist_done(ni, &zombies, rc2);
1514 ksocknal_peer_decref(peer_ni);
1518 LIBCFS_FREE(hello, offsetof(struct ksock_hello_msg,
1519 kshm_ips[LNET_INTERFACES_NUM]));
1521 LIBCFS_FREE(conn, sizeof(*conn));
1529 ksocknal_close_conn_locked(struct ksock_conn *conn, int error)
1531 /* This just does the immmediate housekeeping, and queues the
1532 * connection for the reaper to terminate.
1533 * Caller holds ksnd_global_lock exclusively in irq context */
1534 struct ksock_peer_ni *peer_ni = conn->ksnc_peer;
1535 struct ksock_route *route;
1536 struct ksock_conn *conn2;
1537 struct list_head *tmp;
1539 LASSERT(peer_ni->ksnp_error == 0);
1540 LASSERT(!conn->ksnc_closing);
1541 conn->ksnc_closing = 1;
1543 /* ksnd_deathrow_conns takes over peer_ni's ref */
1544 list_del(&conn->ksnc_list);
1546 route = conn->ksnc_route;
1547 if (route != NULL) {
1548 /* dissociate conn from route... */
1549 LASSERT(!route->ksnr_deleted);
1550 LASSERT((route->ksnr_connected & BIT(conn->ksnc_type)) != 0);
1553 list_for_each(tmp, &peer_ni->ksnp_conns) {
1554 conn2 = list_entry(tmp, struct ksock_conn, ksnc_list);
1556 if (conn2->ksnc_route == route &&
1557 conn2->ksnc_type == conn->ksnc_type)
1563 route->ksnr_connected &= ~BIT(conn->ksnc_type);
1565 conn->ksnc_route = NULL;
1567 ksocknal_route_decref(route); /* drop conn's ref on route */
1570 if (list_empty(&peer_ni->ksnp_conns)) {
1571 /* No more connections to this peer_ni */
1573 if (!list_empty(&peer_ni->ksnp_tx_queue)) {
1574 struct ksock_tx *tx;
1576 LASSERT(conn->ksnc_proto == &ksocknal_protocol_v3x);
1578 /* throw them to the last connection...,
1579 * these TXs will be send to /dev/null by scheduler */
1580 list_for_each_entry(tx, &peer_ni->ksnp_tx_queue,
1582 ksocknal_tx_prep(conn, tx);
1584 spin_lock_bh(&conn->ksnc_scheduler->kss_lock);
1585 list_splice_init(&peer_ni->ksnp_tx_queue,
1586 &conn->ksnc_tx_queue);
1587 spin_unlock_bh(&conn->ksnc_scheduler->kss_lock);
1590 /* renegotiate protocol version */
1591 peer_ni->ksnp_proto = NULL;
1592 /* stash last conn close reason */
1593 peer_ni->ksnp_error = error;
1595 if (list_empty(&peer_ni->ksnp_routes)) {
1596 /* I've just closed last conn belonging to a
1597 * peer_ni with no routes to it */
1598 ksocknal_unlink_peer_locked(peer_ni);
1602 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
1604 list_add_tail(&conn->ksnc_list, &ksocknal_data.ksnd_deathrow_conns);
1605 wake_up(&ksocknal_data.ksnd_reaper_waitq);
1607 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
1611 ksocknal_peer_failed(struct ksock_peer_ni *peer_ni)
1613 bool notify = false;
1614 time64_t last_alive = 0;
1616 /* There has been a connection failure or comms error; but I'll only
1617 * tell LNET I think the peer_ni is dead if it's to another kernel and
1618 * there are no connections or connection attempts in existence. */
1620 read_lock(&ksocknal_data.ksnd_global_lock);
1622 if ((peer_ni->ksnp_id.pid & LNET_PID_USERFLAG) == 0 &&
1623 list_empty(&peer_ni->ksnp_conns) &&
1624 peer_ni->ksnp_accepting == 0 &&
1625 ksocknal_find_connecting_route_locked(peer_ni) == NULL) {
1627 last_alive = peer_ni->ksnp_last_alive;
1630 read_unlock(&ksocknal_data.ksnd_global_lock);
1633 lnet_notify(peer_ni->ksnp_ni, peer_ni->ksnp_id.nid,
1634 false, false, last_alive);
1638 ksocknal_finalize_zcreq(struct ksock_conn *conn)
1640 struct ksock_peer_ni *peer_ni = conn->ksnc_peer;
1641 struct ksock_tx *tx;
1642 struct ksock_tx *tmp;
1645 /* NB safe to finalize TXs because closing of socket will
1646 * abort all buffered data */
1647 LASSERT(conn->ksnc_sock == NULL);
1649 spin_lock(&peer_ni->ksnp_lock);
1651 list_for_each_entry_safe(tx, tmp, &peer_ni->ksnp_zc_req_list, tx_zc_list) {
1652 if (tx->tx_conn != conn)
1655 LASSERT(tx->tx_msg.ksm_zc_cookies[0] != 0);
1657 tx->tx_msg.ksm_zc_cookies[0] = 0;
1658 tx->tx_zc_aborted = 1; /* mark it as not-acked */
1659 list_move(&tx->tx_zc_list, &zlist);
1662 spin_unlock(&peer_ni->ksnp_lock);
1664 while (!list_empty(&zlist)) {
1665 tx = list_entry(zlist.next, struct ksock_tx, tx_zc_list);
1667 list_del(&tx->tx_zc_list);
1668 ksocknal_tx_decref(tx);
1673 ksocknal_terminate_conn(struct ksock_conn *conn)
1675 /* This gets called by the reaper (guaranteed thread context) to
1676 * disengage the socket from its callbacks and close it.
1677 * ksnc_refcount will eventually hit zero, and then the reaper will
1680 struct ksock_peer_ni *peer_ni = conn->ksnc_peer;
1681 struct ksock_sched *sched = conn->ksnc_scheduler;
1682 bool failed = false;
1684 LASSERT(conn->ksnc_closing);
1686 /* wake up the scheduler to "send" all remaining packets to /dev/null */
1687 spin_lock_bh(&sched->kss_lock);
1689 /* a closing conn is always ready to tx */
1690 conn->ksnc_tx_ready = 1;
1692 if (!conn->ksnc_tx_scheduled &&
1693 !list_empty(&conn->ksnc_tx_queue)) {
1694 list_add_tail(&conn->ksnc_tx_list,
1695 &sched->kss_tx_conns);
1696 conn->ksnc_tx_scheduled = 1;
1697 /* extra ref for scheduler */
1698 ksocknal_conn_addref(conn);
1700 wake_up (&sched->kss_waitq);
1703 spin_unlock_bh(&sched->kss_lock);
1705 /* serialise with callbacks */
1706 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1708 ksocknal_lib_reset_callback(conn->ksnc_sock, conn);
1710 /* OK, so this conn may not be completely disengaged from its
1711 * scheduler yet, but it _has_ committed to terminate...
1713 conn->ksnc_scheduler->kss_nconns--;
1715 if (peer_ni->ksnp_error != 0) {
1716 /* peer_ni's last conn closed in error */
1717 LASSERT(list_empty(&peer_ni->ksnp_conns));
1719 peer_ni->ksnp_error = 0; /* avoid multiple notifications */
1722 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1725 ksocknal_peer_failed(peer_ni);
1727 /* The socket is closed on the final put; either here, or in
1728 * ksocknal_{send,recv}msg(). Since we set up the linger2 option
1729 * when the connection was established, this will close the socket
1730 * immediately, aborting anything buffered in it. Any hung
1731 * zero-copy transmits will therefore complete in finite time.
1733 ksocknal_connsock_decref(conn);
1737 ksocknal_queue_zombie_conn(struct ksock_conn *conn)
1739 /* Queue the conn for the reaper to destroy */
1740 LASSERT(refcount_read(&conn->ksnc_conn_refcount) == 0);
1741 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
1743 list_add_tail(&conn->ksnc_list, &ksocknal_data.ksnd_zombie_conns);
1744 wake_up(&ksocknal_data.ksnd_reaper_waitq);
1746 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
1750 ksocknal_destroy_conn(struct ksock_conn *conn)
1754 /* Final coup-de-grace of the reaper */
1755 CDEBUG (D_NET, "connection %p\n", conn);
1757 LASSERT(refcount_read(&conn->ksnc_conn_refcount) == 0);
1758 LASSERT(refcount_read(&conn->ksnc_sock_refcount) == 0);
1759 LASSERT (conn->ksnc_sock == NULL);
1760 LASSERT (conn->ksnc_route == NULL);
1761 LASSERT (!conn->ksnc_tx_scheduled);
1762 LASSERT (!conn->ksnc_rx_scheduled);
1763 LASSERT(list_empty(&conn->ksnc_tx_queue));
1765 /* complete current receive if any */
1766 switch (conn->ksnc_rx_state) {
1767 case SOCKNAL_RX_LNET_PAYLOAD:
1768 last_rcv = conn->ksnc_rx_deadline -
1770 CERROR("Completing partial receive from %s[%d], ip %pISp, with error, wanted: %d, left: %d, last alive is %lld secs ago\n",
1771 libcfs_id2str(conn->ksnc_peer->ksnp_id), conn->ksnc_type,
1772 &conn->ksnc_peeraddr,
1773 conn->ksnc_rx_nob_wanted, conn->ksnc_rx_nob_left,
1774 ktime_get_seconds() - last_rcv);
1775 if (conn->ksnc_lnet_msg)
1776 conn->ksnc_lnet_msg->msg_health_status =
1777 LNET_MSG_STATUS_REMOTE_ERROR;
1778 lnet_finalize(conn->ksnc_lnet_msg, -EIO);
1780 case SOCKNAL_RX_LNET_HEADER:
1781 if (conn->ksnc_rx_started)
1782 CERROR("Incomplete receive of lnet header from %s, ip %pISp, with error, protocol: %d.x.\n",
1783 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1784 &conn->ksnc_peeraddr,
1785 conn->ksnc_proto->pro_version);
1787 case SOCKNAL_RX_KSM_HEADER:
1788 if (conn->ksnc_rx_started)
1789 CERROR("Incomplete receive of ksock message from %s, ip %pISp, with error, protocol: %d.x.\n",
1790 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1791 &conn->ksnc_peeraddr,
1792 conn->ksnc_proto->pro_version);
1794 case SOCKNAL_RX_SLOP:
1795 if (conn->ksnc_rx_started)
1796 CERROR("Incomplete receive of slops from %s, ip %pISp, with error\n",
1797 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1798 &conn->ksnc_peeraddr);
1805 ksocknal_peer_decref(conn->ksnc_peer);
1807 LIBCFS_FREE (conn, sizeof (*conn));
1811 ksocknal_close_peer_conns_locked(struct ksock_peer_ni *peer_ni,
1812 struct sockaddr *addr, int why)
1814 struct ksock_conn *conn;
1815 struct ksock_conn *cnxt;
1818 list_for_each_entry_safe(conn, cnxt, &peer_ni->ksnp_conns, ksnc_list) {
1821 (struct sockaddr *)&conn->ksnc_peeraddr)) {
1823 ksocknal_close_conn_locked(conn, why);
1831 ksocknal_close_conn_and_siblings(struct ksock_conn *conn, int why)
1833 struct ksock_peer_ni *peer_ni = conn->ksnc_peer;
1836 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1838 count = ksocknal_close_peer_conns_locked(
1839 peer_ni, (struct sockaddr *)&conn->ksnc_peeraddr, why);
1841 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1847 ksocknal_close_matching_conns(struct lnet_process_id id, __u32 ipaddr)
1849 struct ksock_peer_ni *peer_ni;
1850 struct hlist_node *pnxt;
1855 struct sockaddr_in sa = {.sin_family = AF_INET};
1857 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1859 if (id.nid != LNET_NID_ANY) {
1860 lo = hash_min(id.nid, HASH_BITS(ksocknal_data.ksnd_peers));
1864 hi = HASH_SIZE(ksocknal_data.ksnd_peers) - 1;
1867 sa.sin_addr.s_addr = htonl(ipaddr);
1868 for (i = lo; i <= hi; i++) {
1869 hlist_for_each_entry_safe(peer_ni, pnxt,
1870 &ksocknal_data.ksnd_peers[i],
1873 if (!((id.nid == LNET_NID_ANY ||
1874 id.nid == peer_ni->ksnp_id.nid) &&
1875 (id.pid == LNET_PID_ANY ||
1876 id.pid == peer_ni->ksnp_id.pid)))
1879 count += ksocknal_close_peer_conns_locked(
1881 ipaddr ? (struct sockaddr *)&sa : NULL, 0);
1885 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1887 /* wildcards always succeed */
1888 if (id.nid == LNET_NID_ANY || id.pid == LNET_PID_ANY || ipaddr == 0)
1891 return (count == 0 ? -ENOENT : 0);
1895 ksocknal_notify_gw_down(lnet_nid_t gw_nid)
1897 /* The router is telling me she's been notified of a change in
1900 struct lnet_process_id id = {
1902 .pid = LNET_PID_ANY,
1905 CDEBUG(D_NET, "gw %s down\n", libcfs_nid2str(gw_nid));
1907 /* If the gateway crashed, close all open connections... */
1908 ksocknal_close_matching_conns(id, 0);
1911 /* We can only establish new connections
1912 * if we have autroutes, and these connect on demand. */
1916 ksocknal_push_peer(struct ksock_peer_ni *peer_ni)
1920 struct list_head *tmp;
1921 struct ksock_conn *conn;
1923 for (index = 0; ; index++) {
1924 read_lock(&ksocknal_data.ksnd_global_lock);
1929 list_for_each(tmp, &peer_ni->ksnp_conns) {
1931 conn = list_entry(tmp, struct ksock_conn,
1933 ksocknal_conn_addref(conn);
1938 read_unlock(&ksocknal_data.ksnd_global_lock);
1943 ksocknal_lib_push_conn (conn);
1944 ksocknal_conn_decref(conn);
1949 ksocknal_push(struct lnet_ni *ni, struct lnet_process_id id)
1956 if (id.nid != LNET_NID_ANY) {
1957 lo = hash_min(id.nid, HASH_BITS(ksocknal_data.ksnd_peers));
1961 hi = HASH_SIZE(ksocknal_data.ksnd_peers) - 1;
1964 for (bkt = lo; bkt <= hi; bkt++) {
1965 int peer_off; /* searching offset in peer_ni hash table */
1967 for (peer_off = 0; ; peer_off++) {
1968 struct ksock_peer_ni *peer_ni;
1971 read_lock(&ksocknal_data.ksnd_global_lock);
1972 hlist_for_each_entry(peer_ni,
1973 &ksocknal_data.ksnd_peers[bkt],
1975 if (!((id.nid == LNET_NID_ANY ||
1976 id.nid == peer_ni->ksnp_id.nid) &&
1977 (id.pid == LNET_PID_ANY ||
1978 id.pid == peer_ni->ksnp_id.pid)))
1981 if (i++ == peer_off) {
1982 ksocknal_peer_addref(peer_ni);
1986 read_unlock(&ksocknal_data.ksnd_global_lock);
1988 if (i <= peer_off) /* no match */
1992 ksocknal_push_peer(peer_ni);
1993 ksocknal_peer_decref(peer_ni);
2000 ksocknal_add_interface(struct lnet_ni *ni, __u32 ipaddress, __u32 netmask)
2002 struct ksock_net *net = ni->ni_data;
2003 struct ksock_interface *iface;
2004 struct sockaddr_in sa = { .sin_family = AF_INET };
2008 struct ksock_peer_ni *peer_ni;
2009 struct list_head *rtmp;
2010 struct ksock_route *route;
2012 if (ipaddress == 0 ||
2016 write_lock_bh(&ksocknal_data.ksnd_global_lock);
2018 sa.sin_addr.s_addr = htonl(ipaddress);
2019 iface = ksocknal_ip2iface(ni, (struct sockaddr *)&sa);
2020 if (iface != NULL) {
2021 /* silently ignore dups */
2023 } else if (net->ksnn_ninterfaces == LNET_INTERFACES_NUM) {
2026 iface = &net->ksnn_interfaces[net->ksnn_ninterfaces++];
2028 iface->ksni_index = ksocknal_ip2index((struct sockaddr *)&sa,
2030 rpc_copy_addr((struct sockaddr *)&iface->ksni_addr,
2031 (struct sockaddr *)&sa);
2032 iface->ksni_netmask = netmask;
2033 iface->ksni_nroutes = 0;
2034 iface->ksni_npeers = 0;
2036 hash_for_each(ksocknal_data.ksnd_peers, i, peer_ni, ksnp_list) {
2037 for (j = 0; j < peer_ni->ksnp_n_passive_ips; j++)
2038 if (peer_ni->ksnp_passive_ips[j] == ipaddress)
2039 iface->ksni_npeers++;
2041 list_for_each(rtmp, &peer_ni->ksnp_routes) {
2042 route = list_entry(rtmp,
2046 if (route->ksnr_myiface ==
2048 iface->ksni_nroutes++;
2053 /* NB only new connections will pay attention to the new
2058 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
2064 ksocknal_peer_del_interface_locked(struct ksock_peer_ni *peer_ni,
2065 __u32 ipaddr, int index)
2067 struct ksock_route *route;
2068 struct ksock_route *rnxt;
2069 struct ksock_conn *conn;
2070 struct ksock_conn *cnxt;
2074 for (i = 0; i < peer_ni->ksnp_n_passive_ips; i++)
2075 if (peer_ni->ksnp_passive_ips[i] == ipaddr) {
2076 for (j = i+1; j < peer_ni->ksnp_n_passive_ips; j++)
2077 peer_ni->ksnp_passive_ips[j-1] =
2078 peer_ni->ksnp_passive_ips[j];
2079 peer_ni->ksnp_n_passive_ips--;
2083 list_for_each_entry_safe(route, rnxt, &peer_ni->ksnp_routes,
2085 if (route->ksnr_myiface != index)
2088 if (route->ksnr_share_count != 0) {
2089 /* Manually created; keep, but unbind */
2090 route->ksnr_myiface = -1;
2092 ksocknal_del_route_locked(route);
2096 list_for_each_entry_safe(conn, cnxt, &peer_ni->ksnp_conns, ksnc_list)
2097 if (conn->ksnc_route->ksnr_myiface == index)
2098 ksocknal_close_conn_locked (conn, 0);
2102 ksocknal_del_interface(struct lnet_ni *ni, __u32 ipaddress)
2104 struct ksock_net *net = ni->ni_data;
2106 struct hlist_node *nxt;
2107 struct ksock_peer_ni *peer_ni;
2109 struct sockaddr_in sa = {.sin_family = AF_INET };
2114 sa.sin_addr.s_addr = htonl(ipaddress);
2115 index = ksocknal_ip2index((struct sockaddr *)&sa, ni);
2117 write_lock_bh(&ksocknal_data.ksnd_global_lock);
2119 for (i = 0; i < net->ksnn_ninterfaces; i++) {
2120 struct sockaddr_in *sa =
2121 (void *)&net->ksnn_interfaces[i].ksni_addr;
2123 if (sa->sin_family != AF_INET)
2125 this_ip = ntohl(sa->sin_addr.s_addr);
2127 if (!(ipaddress == 0 ||
2128 ipaddress == this_ip))
2133 for (j = i+1; j < net->ksnn_ninterfaces; j++)
2134 net->ksnn_interfaces[j-1] =
2135 net->ksnn_interfaces[j];
2137 net->ksnn_ninterfaces--;
2139 hash_for_each_safe(ksocknal_data.ksnd_peers, j,
2140 nxt, peer_ni, ksnp_list) {
2141 if (peer_ni->ksnp_ni != ni)
2144 ksocknal_peer_del_interface_locked(peer_ni,
2149 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
2155 ksocknal_ctl(struct lnet_ni *ni, unsigned int cmd, void *arg)
2157 struct lnet_process_id id = {0};
2158 struct libcfs_ioctl_data *data = arg;
2162 case IOC_LIBCFS_GET_INTERFACE: {
2163 struct ksock_net *net = ni->ni_data;
2164 struct ksock_interface *iface;
2165 struct sockaddr_in *sa;
2167 read_lock(&ksocknal_data.ksnd_global_lock);
2169 if (data->ioc_count >= (__u32)net->ksnn_ninterfaces) {
2173 iface = &net->ksnn_interfaces[data->ioc_count];
2175 sa = (void *)&iface->ksni_addr;
2176 if (sa->sin_family == AF_INET)
2177 data->ioc_u32[0] = ntohl(sa->sin_addr.s_addr);
2179 data->ioc_u32[0] = 0xFFFFFFFF;
2180 data->ioc_u32[1] = iface->ksni_netmask;
2181 data->ioc_u32[2] = iface->ksni_npeers;
2182 data->ioc_u32[3] = iface->ksni_nroutes;
2185 read_unlock(&ksocknal_data.ksnd_global_lock);
2189 case IOC_LIBCFS_ADD_INTERFACE:
2190 return ksocknal_add_interface(ni,
2191 data->ioc_u32[0], /* IP address */
2192 data->ioc_u32[1]); /* net mask */
2194 case IOC_LIBCFS_DEL_INTERFACE:
2195 return ksocknal_del_interface(ni,
2196 data->ioc_u32[0]); /* IP address */
2198 case IOC_LIBCFS_GET_PEER: {
2203 int share_count = 0;
2205 rc = ksocknal_get_peer_info(ni, data->ioc_count,
2206 &id, &myip, &ip, &port,
2207 &conn_count, &share_count);
2211 data->ioc_nid = id.nid;
2212 data->ioc_count = share_count;
2213 data->ioc_u32[0] = ip;
2214 data->ioc_u32[1] = port;
2215 data->ioc_u32[2] = myip;
2216 data->ioc_u32[3] = conn_count;
2217 data->ioc_u32[4] = id.pid;
2221 case IOC_LIBCFS_ADD_PEER: {
2222 struct sockaddr_in sa = {.sin_family = AF_INET};
2224 id.nid = data->ioc_nid;
2225 id.pid = LNET_PID_LUSTRE;
2226 sa.sin_addr.s_addr = htonl(data->ioc_u32[0]);
2227 sa.sin_port = htons(data->ioc_u32[1]);
2228 return ksocknal_add_peer(ni, id, (struct sockaddr *)&sa);
2230 case IOC_LIBCFS_DEL_PEER:
2231 id.nid = data->ioc_nid;
2232 id.pid = LNET_PID_ANY;
2233 return ksocknal_del_peer (ni, id,
2234 data->ioc_u32[0]); /* IP */
2236 case IOC_LIBCFS_GET_CONN: {
2240 struct ksock_conn *conn = ksocknal_get_conn_by_idx(ni, data->ioc_count);
2241 struct sockaddr_in *psa = (void *)&conn->ksnc_peeraddr;
2242 struct sockaddr_in *mysa = (void *)&conn->ksnc_myaddr;
2247 ksocknal_lib_get_conn_tunables(conn, &txmem, &rxmem, &nagle);
2249 data->ioc_count = txmem;
2250 data->ioc_nid = conn->ksnc_peer->ksnp_id.nid;
2251 data->ioc_flags = nagle;
2252 if (psa->sin_family == AF_INET)
2253 data->ioc_u32[0] = ntohl(psa->sin_addr.s_addr);
2255 data->ioc_u32[0] = 0xFFFFFFFF;
2256 data->ioc_u32[1] = rpc_get_port((struct sockaddr *)
2257 &conn->ksnc_peeraddr);
2258 if (mysa->sin_family == AF_INET)
2259 data->ioc_u32[2] = ntohl(mysa->sin_addr.s_addr);
2261 data->ioc_u32[2] = 0xFFFFFFFF;
2262 data->ioc_u32[3] = conn->ksnc_type;
2263 data->ioc_u32[4] = conn->ksnc_scheduler->kss_cpt;
2264 data->ioc_u32[5] = rxmem;
2265 data->ioc_u32[6] = conn->ksnc_peer->ksnp_id.pid;
2266 ksocknal_conn_decref(conn);
2270 case IOC_LIBCFS_CLOSE_CONNECTION:
2271 id.nid = data->ioc_nid;
2272 id.pid = LNET_PID_ANY;
2273 return ksocknal_close_matching_conns (id,
2276 case IOC_LIBCFS_REGISTER_MYNID:
2277 /* Ignore if this is a noop */
2278 if (data->ioc_nid == ni->ni_nid)
2281 CERROR("obsolete IOC_LIBCFS_REGISTER_MYNID: %s(%s)\n",
2282 libcfs_nid2str(data->ioc_nid),
2283 libcfs_nid2str(ni->ni_nid));
2286 case IOC_LIBCFS_PUSH_CONNECTION:
2287 id.nid = data->ioc_nid;
2288 id.pid = LNET_PID_ANY;
2289 return ksocknal_push(ni, id);
2298 ksocknal_free_buffers (void)
2300 LASSERT (atomic_read(&ksocknal_data.ksnd_nactive_txs) == 0);
2302 if (ksocknal_data.ksnd_schedulers != NULL)
2303 cfs_percpt_free(ksocknal_data.ksnd_schedulers);
2305 spin_lock(&ksocknal_data.ksnd_tx_lock);
2307 if (!list_empty(&ksocknal_data.ksnd_idle_noop_txs)) {
2309 struct ksock_tx *tx;
2311 list_splice_init(&ksocknal_data.ksnd_idle_noop_txs, &zlist);
2312 spin_unlock(&ksocknal_data.ksnd_tx_lock);
2314 while (!list_empty(&zlist)) {
2315 tx = list_entry(zlist.next, struct ksock_tx, tx_list);
2316 list_del(&tx->tx_list);
2317 LIBCFS_FREE(tx, tx->tx_desc_size);
2320 spin_unlock(&ksocknal_data.ksnd_tx_lock);
2325 ksocknal_base_shutdown(void)
2327 struct ksock_sched *sched;
2328 struct ksock_peer_ni *peer_ni;
2331 CDEBUG(D_MALLOC, "before NAL cleanup: kmem %lld\n",
2332 libcfs_kmem_read());
2333 LASSERT (ksocknal_data.ksnd_nnets == 0);
2335 switch (ksocknal_data.ksnd_init) {
2340 case SOCKNAL_INIT_ALL:
2341 case SOCKNAL_INIT_DATA:
2342 hash_for_each(ksocknal_data.ksnd_peers, i, peer_ni, ksnp_list)
2345 LASSERT(list_empty(&ksocknal_data.ksnd_nets));
2346 LASSERT(list_empty(&ksocknal_data.ksnd_enomem_conns));
2347 LASSERT(list_empty(&ksocknal_data.ksnd_zombie_conns));
2348 LASSERT(list_empty(&ksocknal_data.ksnd_connd_connreqs));
2349 LASSERT(list_empty(&ksocknal_data.ksnd_connd_routes));
2351 if (ksocknal_data.ksnd_schedulers != NULL) {
2352 cfs_percpt_for_each(sched, i,
2353 ksocknal_data.ksnd_schedulers) {
2355 LASSERT(list_empty(&sched->kss_tx_conns));
2356 LASSERT(list_empty(&sched->kss_rx_conns));
2357 LASSERT(list_empty(&sched->kss_zombie_noop_txs));
2358 LASSERT(sched->kss_nconns == 0);
2362 /* flag threads to terminate; wake and wait for them to die */
2363 ksocknal_data.ksnd_shuttingdown = 1;
2364 wake_up_all(&ksocknal_data.ksnd_connd_waitq);
2365 wake_up_all(&ksocknal_data.ksnd_reaper_waitq);
2367 if (ksocknal_data.ksnd_schedulers != NULL) {
2368 cfs_percpt_for_each(sched, i,
2369 ksocknal_data.ksnd_schedulers)
2370 wake_up_all(&sched->kss_waitq);
2373 wait_var_event_warning(&ksocknal_data.ksnd_nthreads,
2374 atomic_read(&ksocknal_data.ksnd_nthreads) == 0,
2375 "waiting for %d threads to terminate\n",
2376 atomic_read(&ksocknal_data.ksnd_nthreads));
2378 ksocknal_free_buffers();
2380 ksocknal_data.ksnd_init = SOCKNAL_INIT_NOTHING;
2384 CDEBUG(D_MALLOC, "after NAL cleanup: kmem %lld\n",
2385 libcfs_kmem_read());
2387 module_put(THIS_MODULE);
2391 ksocknal_base_startup(void)
2393 struct ksock_sched *sched;
2397 LASSERT(ksocknal_data.ksnd_init == SOCKNAL_INIT_NOTHING);
2398 LASSERT(ksocknal_data.ksnd_nnets == 0);
2400 memset(&ksocknal_data, 0, sizeof(ksocknal_data)); /* zero pointers */
2402 hash_init(ksocknal_data.ksnd_peers);
2404 rwlock_init(&ksocknal_data.ksnd_global_lock);
2405 INIT_LIST_HEAD(&ksocknal_data.ksnd_nets);
2407 spin_lock_init(&ksocknal_data.ksnd_reaper_lock);
2408 INIT_LIST_HEAD(&ksocknal_data.ksnd_enomem_conns);
2409 INIT_LIST_HEAD(&ksocknal_data.ksnd_zombie_conns);
2410 INIT_LIST_HEAD(&ksocknal_data.ksnd_deathrow_conns);
2411 init_waitqueue_head(&ksocknal_data.ksnd_reaper_waitq);
2413 spin_lock_init(&ksocknal_data.ksnd_connd_lock);
2414 INIT_LIST_HEAD(&ksocknal_data.ksnd_connd_connreqs);
2415 INIT_LIST_HEAD(&ksocknal_data.ksnd_connd_routes);
2416 init_waitqueue_head(&ksocknal_data.ksnd_connd_waitq);
2418 spin_lock_init(&ksocknal_data.ksnd_tx_lock);
2419 INIT_LIST_HEAD(&ksocknal_data.ksnd_idle_noop_txs);
2421 /* NB memset above zeros whole of ksocknal_data */
2423 /* flag lists/ptrs/locks initialised */
2424 ksocknal_data.ksnd_init = SOCKNAL_INIT_DATA;
2425 if (!try_module_get(THIS_MODULE))
2428 /* Create a scheduler block per available CPT */
2429 ksocknal_data.ksnd_schedulers = cfs_percpt_alloc(lnet_cpt_table(),
2431 if (ksocknal_data.ksnd_schedulers == NULL)
2434 cfs_percpt_for_each(sched, i, ksocknal_data.ksnd_schedulers) {
2438 * make sure not to allocate more threads than there are
2439 * cores/CPUs in teh CPT
2441 nthrs = cfs_cpt_weight(lnet_cpt_table(), i);
2442 if (*ksocknal_tunables.ksnd_nscheds > 0) {
2443 nthrs = min(nthrs, *ksocknal_tunables.ksnd_nscheds);
2446 * max to half of CPUs, assume another half should be
2447 * reserved for upper layer modules
2449 nthrs = min(max(SOCKNAL_NSCHEDS, nthrs >> 1), nthrs);
2452 sched->kss_nthreads_max = nthrs;
2455 spin_lock_init(&sched->kss_lock);
2456 INIT_LIST_HEAD(&sched->kss_rx_conns);
2457 INIT_LIST_HEAD(&sched->kss_tx_conns);
2458 INIT_LIST_HEAD(&sched->kss_zombie_noop_txs);
2459 init_waitqueue_head(&sched->kss_waitq);
2462 ksocknal_data.ksnd_connd_starting = 0;
2463 ksocknal_data.ksnd_connd_failed_stamp = 0;
2464 ksocknal_data.ksnd_connd_starting_stamp = ktime_get_real_seconds();
2465 /* must have at least 2 connds to remain responsive to accepts while
2467 if (*ksocknal_tunables.ksnd_nconnds < SOCKNAL_CONND_RESV + 1)
2468 *ksocknal_tunables.ksnd_nconnds = SOCKNAL_CONND_RESV + 1;
2470 if (*ksocknal_tunables.ksnd_nconnds_max <
2471 *ksocknal_tunables.ksnd_nconnds) {
2472 ksocknal_tunables.ksnd_nconnds_max =
2473 ksocknal_tunables.ksnd_nconnds;
2476 for (i = 0; i < *ksocknal_tunables.ksnd_nconnds; i++) {
2478 spin_lock_bh(&ksocknal_data.ksnd_connd_lock);
2479 ksocknal_data.ksnd_connd_starting++;
2480 spin_unlock_bh(&ksocknal_data.ksnd_connd_lock);
2483 snprintf(name, sizeof(name), "socknal_cd%02d", i);
2484 rc = ksocknal_thread_start(ksocknal_connd,
2485 (void *)((uintptr_t)i), name);
2487 spin_lock_bh(&ksocknal_data.ksnd_connd_lock);
2488 ksocknal_data.ksnd_connd_starting--;
2489 spin_unlock_bh(&ksocknal_data.ksnd_connd_lock);
2490 CERROR("Can't spawn socknal connd: %d\n", rc);
2495 rc = ksocknal_thread_start(ksocknal_reaper, NULL, "socknal_reaper");
2497 CERROR ("Can't spawn socknal reaper: %d\n", rc);
2501 /* flag everything initialised */
2502 ksocknal_data.ksnd_init = SOCKNAL_INIT_ALL;
2507 ksocknal_base_shutdown();
2512 ksocknal_debug_peerhash(struct lnet_ni *ni)
2514 struct ksock_peer_ni *peer_ni;
2517 read_lock(&ksocknal_data.ksnd_global_lock);
2519 hash_for_each(ksocknal_data.ksnd_peers, i, peer_ni, ksnp_list) {
2520 struct ksock_route *route;
2521 struct ksock_conn *conn;
2523 if (peer_ni->ksnp_ni != ni)
2526 CWARN("Active peer_ni on shutdown: %s, ref %d, "
2527 "closing %d, accepting %d, err %d, zcookie %llu, "
2528 "txq %d, zc_req %d\n", libcfs_id2str(peer_ni->ksnp_id),
2529 refcount_read(&peer_ni->ksnp_refcount),
2530 peer_ni->ksnp_closing,
2531 peer_ni->ksnp_accepting, peer_ni->ksnp_error,
2532 peer_ni->ksnp_zc_next_cookie,
2533 !list_empty(&peer_ni->ksnp_tx_queue),
2534 !list_empty(&peer_ni->ksnp_zc_req_list));
2536 list_for_each_entry(route, &peer_ni->ksnp_routes, ksnr_list) {
2537 CWARN("Route: ref %d, schd %d, conn %d, cnted %d, del %d\n",
2538 refcount_read(&route->ksnr_refcount),
2539 route->ksnr_scheduled, route->ksnr_connecting,
2540 route->ksnr_connected, route->ksnr_deleted);
2543 list_for_each_entry(conn, &peer_ni->ksnp_conns, ksnc_list) {
2544 CWARN("Conn: ref %d, sref %d, t %d, c %d\n",
2545 refcount_read(&conn->ksnc_conn_refcount),
2546 refcount_read(&conn->ksnc_sock_refcount),
2547 conn->ksnc_type, conn->ksnc_closing);
2552 read_unlock(&ksocknal_data.ksnd_global_lock);
2557 ksocknal_shutdown(struct lnet_ni *ni)
2559 struct ksock_net *net = ni->ni_data;
2560 struct lnet_process_id anyid = {
2561 .nid = LNET_NID_ANY,
2562 .pid = LNET_PID_ANY,
2566 LASSERT(ksocknal_data.ksnd_init == SOCKNAL_INIT_ALL);
2567 LASSERT(ksocknal_data.ksnd_nnets > 0);
2569 /* prevent new peers */
2570 atomic_add(SOCKNAL_SHUTDOWN_BIAS, &net->ksnn_npeers);
2572 /* Delete all peers */
2573 ksocknal_del_peer(ni, anyid, 0);
2575 /* Wait for all peer_ni state to clean up */
2576 wait_var_event_warning(&net->ksnn_npeers,
2577 atomic_read(&net->ksnn_npeers) ==
2578 SOCKNAL_SHUTDOWN_BIAS,
2579 "waiting for %d peers to disconnect\n",
2580 ksocknal_debug_peerhash(ni) +
2581 atomic_read(&net->ksnn_npeers) -
2582 SOCKNAL_SHUTDOWN_BIAS);
2584 for (i = 0; i < net->ksnn_ninterfaces; i++) {
2585 LASSERT(net->ksnn_interfaces[i].ksni_npeers == 0);
2586 LASSERT(net->ksnn_interfaces[i].ksni_nroutes == 0);
2589 list_del(&net->ksnn_list);
2590 LIBCFS_FREE(net, sizeof(*net));
2592 ksocknal_data.ksnd_nnets--;
2593 if (ksocknal_data.ksnd_nnets == 0)
2594 ksocknal_base_shutdown();
2598 ksocknal_search_new_ipif(struct ksock_net *net)
2603 for (i = 0; i < net->ksnn_ninterfaces; i++) {
2604 char *ifnam = &net->ksnn_interfaces[i].ksni_name[0];
2605 char *colon = strchr(ifnam, ':');
2607 struct ksock_net *tmp;
2610 if (colon != NULL) /* ignore alias device */
2613 list_for_each_entry(tmp, &ksocknal_data.ksnd_nets,
2615 for (j = 0; !found && j < tmp->ksnn_ninterfaces; j++) {
2616 char *ifnam2 = &tmp->ksnn_interfaces[j].\
2618 char *colon2 = strchr(ifnam2, ':');
2623 found = strcmp(ifnam, ifnam2) == 0;
2640 ksocknal_start_schedulers(struct ksock_sched *sched)
2646 if (sched->kss_nthreads == 0) {
2647 if (*ksocknal_tunables.ksnd_nscheds > 0) {
2648 nthrs = sched->kss_nthreads_max;
2650 nthrs = cfs_cpt_weight(lnet_cpt_table(),
2652 nthrs = min(max(SOCKNAL_NSCHEDS, nthrs >> 1), nthrs);
2653 nthrs = min(SOCKNAL_NSCHEDS_HIGH, nthrs);
2655 nthrs = min(nthrs, sched->kss_nthreads_max);
2657 LASSERT(sched->kss_nthreads <= sched->kss_nthreads_max);
2658 /* increase two threads if there is new interface */
2659 nthrs = min(2, sched->kss_nthreads_max - sched->kss_nthreads);
2662 for (i = 0; i < nthrs; i++) {
2666 id = KSOCK_THREAD_ID(sched->kss_cpt, sched->kss_nthreads + i);
2667 snprintf(name, sizeof(name), "socknal_sd%02d_%02d",
2668 sched->kss_cpt, (int)KSOCK_THREAD_SID(id));
2670 rc = ksocknal_thread_start(ksocknal_scheduler,
2675 CERROR("Can't spawn thread %d for scheduler[%d]: %d\n",
2676 sched->kss_cpt, (int) KSOCK_THREAD_SID(id), rc);
2680 sched->kss_nthreads += i;
2685 ksocknal_net_start_threads(struct ksock_net *net, __u32 *cpts, int ncpts)
2687 int newif = ksocknal_search_new_ipif(net);
2691 if (ncpts > 0 && ncpts > cfs_cpt_number(lnet_cpt_table()))
2694 for (i = 0; i < ncpts; i++) {
2695 struct ksock_sched *sched;
2696 int cpt = (cpts == NULL) ? i : cpts[i];
2698 LASSERT(cpt < cfs_cpt_number(lnet_cpt_table()));
2699 sched = ksocknal_data.ksnd_schedulers[cpt];
2701 if (!newif && sched->kss_nthreads > 0)
2704 rc = ksocknal_start_schedulers(sched);
2712 ksocknal_startup(struct lnet_ni *ni)
2714 struct ksock_net *net;
2715 struct lnet_ioctl_config_lnd_cmn_tunables *net_tunables;
2716 struct ksock_interface *ksi = NULL;
2717 struct lnet_inetdev *ifaces = NULL;
2721 LASSERT (ni->ni_net->net_lnd == &the_ksocklnd);
2723 if (ksocknal_data.ksnd_init == SOCKNAL_INIT_NOTHING) {
2724 rc = ksocknal_base_startup();
2729 LIBCFS_ALLOC(net, sizeof(*net));
2733 net->ksnn_incarnation = ktime_get_real_ns();
2735 net_tunables = &ni->ni_net->net_tunables;
2737 if (net_tunables->lct_peer_timeout == -1)
2738 net_tunables->lct_peer_timeout =
2739 *ksocknal_tunables.ksnd_peertimeout;
2741 if (net_tunables->lct_max_tx_credits == -1)
2742 net_tunables->lct_max_tx_credits =
2743 *ksocknal_tunables.ksnd_credits;
2745 if (net_tunables->lct_peer_tx_credits == -1)
2746 net_tunables->lct_peer_tx_credits =
2747 *ksocknal_tunables.ksnd_peertxcredits;
2749 if (net_tunables->lct_peer_tx_credits >
2750 net_tunables->lct_max_tx_credits)
2751 net_tunables->lct_peer_tx_credits =
2752 net_tunables->lct_max_tx_credits;
2754 if (net_tunables->lct_peer_rtr_credits == -1)
2755 net_tunables->lct_peer_rtr_credits =
2756 *ksocknal_tunables.ksnd_peerrtrcredits;
2758 rc = lnet_inet_enumerate(&ifaces, ni->ni_net_ns);
2762 if (!ni->ni_interfaces[0]) {
2763 struct sockaddr_in *sa;
2765 ksi = &net->ksnn_interfaces[0];
2766 sa = (void *)&ksi->ksni_addr;
2768 /* Use the first discovered interface */
2769 net->ksnn_ninterfaces = 1;
2770 ni->ni_dev_cpt = ifaces[0].li_cpt;
2771 memset(sa, 0, sizeof(*sa));
2772 sa->sin_family = AF_INET;
2773 sa->sin_addr.s_addr = htonl(ifaces[0].li_ipaddr);
2774 ksi->ksni_index = ksocknal_ip2index((struct sockaddr *)sa, ni);
2775 ksi->ksni_netmask = ifaces[0].li_netmask;
2776 strlcpy(ksi->ksni_name, ifaces[0].li_name,
2777 sizeof(ksi->ksni_name));
2779 /* Before Multi-Rail ksocklnd would manage
2780 * multiple interfaces with its own tcp bonding.
2781 * If we encounter an old configuration using
2782 * this tcp bonding approach then we need to
2783 * handle more than one ni_interfaces.
2785 * In Multi-Rail configuration only ONE ni_interface
2786 * should exist. Each IP alias should be mapped to
2787 * each 'struct net_ni'.
2789 for (i = 0; i < LNET_INTERFACES_NUM; i++) {
2792 if (!ni->ni_interfaces[i])
2795 for (j = 0; j < LNET_INTERFACES_NUM; j++) {
2796 if (i != j && ni->ni_interfaces[j] &&
2797 strcmp(ni->ni_interfaces[i],
2798 ni->ni_interfaces[j]) == 0) {
2800 CERROR("ksocklnd: found duplicate %s at %d and %d, rc = %d\n",
2801 ni->ni_interfaces[i], i, j, rc);
2806 for (j = 0; j < rc; j++) {
2807 struct sockaddr_in *sa;
2809 if (strcmp(ifaces[j].li_name,
2810 ni->ni_interfaces[i]) != 0)
2814 &net->ksnn_interfaces[net->ksnn_ninterfaces];
2815 sa = (void *)&ksi->ksni_addr;
2816 ni->ni_dev_cpt = ifaces[j].li_cpt;
2817 memset(sa, 0, sizeof(*sa));
2818 sa->sin_family = AF_INET;
2819 sa->sin_addr.s_addr =
2820 htonl(ifaces[j].li_ipaddr);
2821 ksi->ksni_index = ksocknal_ip2index(
2822 (struct sockaddr *)sa, ni);
2823 ksi->ksni_netmask = ifaces[j].li_netmask;
2824 strlcpy(ksi->ksni_name, ifaces[j].li_name,
2825 sizeof(ksi->ksni_name));
2826 net->ksnn_ninterfaces++;
2830 /* ni_interfaces don't map to all network interfaces */
2831 if (!ksi || net->ksnn_ninterfaces != i) {
2832 CERROR("ksocklnd: requested %d but only %d interfaces found\n",
2833 i, net->ksnn_ninterfaces);
2838 /* call it before add it to ksocknal_data.ksnd_nets */
2839 rc = ksocknal_net_start_threads(net, ni->ni_cpts, ni->ni_ncpts);
2844 LASSERT(ksi->ksni_addr.ss_family == AF_INET);
2845 ni->ni_nid = LNET_MKNID(
2846 LNET_NIDNET(ni->ni_nid),
2847 ntohl(((struct sockaddr_in *)
2848 &ksi->ksni_addr)->sin_addr.s_addr));
2849 list_add(&net->ksnn_list, &ksocknal_data.ksnd_nets);
2851 ksocknal_data.ksnd_nnets++;
2856 LIBCFS_FREE(net, sizeof(*net));
2858 if (ksocknal_data.ksnd_nnets == 0)
2859 ksocknal_base_shutdown();
2865 static void __exit ksocklnd_exit(void)
2867 lnet_unregister_lnd(&the_ksocklnd);
2870 static const struct lnet_lnd the_ksocklnd = {
2871 .lnd_type = SOCKLND,
2872 .lnd_startup = ksocknal_startup,
2873 .lnd_shutdown = ksocknal_shutdown,
2874 .lnd_ctl = ksocknal_ctl,
2875 .lnd_send = ksocknal_send,
2876 .lnd_recv = ksocknal_recv,
2877 .lnd_notify_peer_down = ksocknal_notify_gw_down,
2878 .lnd_accept = ksocknal_accept,
2881 static int __init ksocklnd_init(void)
2885 /* check ksnr_connected/connecting field large enough */
2886 BUILD_BUG_ON(SOCKLND_CONN_NTYPES > 4);
2887 BUILD_BUG_ON(SOCKLND_CONN_ACK != SOCKLND_CONN_BULK_IN);
2889 rc = ksocknal_tunables_init();
2893 lnet_register_lnd(&the_ksocklnd);
2898 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2899 MODULE_DESCRIPTION("TCP Socket LNet Network Driver");
2900 MODULE_VERSION("2.8.0");
2901 MODULE_LICENSE("GPL");
2903 module_init(ksocklnd_init);
2904 module_exit(ksocklnd_exit);