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, __u32 ipaddr,
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;
523 struct sockaddr_in sa = {.sin_family = AF_INET};
525 if (id.nid == LNET_NID_ANY ||
526 id.pid == LNET_PID_ANY)
529 /* Have a brand new peer_ni ready... */
530 peer_ni = ksocknal_create_peer(ni, id);
532 return PTR_ERR(peer_ni);
534 sa.sin_addr.s_addr = htonl(ipaddr);
535 sa.sin_port = htons(port);
536 route = ksocknal_create_route((struct sockaddr *)&sa);
538 ksocknal_peer_decref(peer_ni);
542 write_lock_bh(&ksocknal_data.ksnd_global_lock);
544 /* always called with a ref on ni, so shutdown can't have started */
545 LASSERT(atomic_read(&((struct ksock_net *)ni->ni_data)->ksnn_npeers)
548 peer2 = ksocknal_find_peer_locked(ni, id);
550 ksocknal_peer_decref(peer_ni);
553 /* peer_ni table takes my ref on peer_ni */
554 hash_add(ksocknal_data.ksnd_peers, &peer_ni->ksnp_list, id.nid);
558 list_for_each(tmp, &peer_ni->ksnp_routes) {
559 route2 = list_entry(tmp, struct ksock_route, ksnr_list);
561 if (route2->ksnr_addr.ss_family == AF_INET &&
562 ((struct sockaddr_in *)&route2->ksnr_addr)->sin_addr.s_addr
568 if (route2 == NULL) {
569 ksocknal_add_route_locked(peer_ni, route);
570 route->ksnr_share_count++;
572 ksocknal_route_decref(route);
573 route2->ksnr_share_count++;
576 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
582 ksocknal_del_peer_locked(struct ksock_peer_ni *peer_ni, __u32 ip)
584 struct ksock_conn *conn;
585 struct ksock_conn *cnxt;
586 struct ksock_route *route;
587 struct ksock_route *rnxt;
590 LASSERT(!peer_ni->ksnp_closing);
592 /* Extra ref prevents peer_ni disappearing until I'm done with it */
593 ksocknal_peer_addref(peer_ni);
595 list_for_each_entry_safe(route, rnxt, &peer_ni->ksnp_routes,
599 if (route->ksnr_addr.ss_family != AF_INET)
601 if (((struct sockaddr_in *)&route->ksnr_addr)
602 ->sin_addr.s_addr != htonl(ip))
606 route->ksnr_share_count = 0;
607 /* This deletes associated conns too */
608 ksocknal_del_route_locked(route);
612 list_for_each_entry(route, &peer_ni->ksnp_routes, ksnr_list)
613 nshared += route->ksnr_share_count;
616 /* remove everything else if there are no explicit entries
619 list_for_each_entry_safe(route, rnxt, &peer_ni->ksnp_routes,
621 /* we should only be removing auto-entries */
622 LASSERT(route->ksnr_share_count == 0);
623 ksocknal_del_route_locked(route);
626 list_for_each_entry_safe(conn, cnxt, &peer_ni->ksnp_conns,
628 ksocknal_close_conn_locked(conn, 0);
631 ksocknal_peer_decref(peer_ni);
632 /* NB peer_ni unlinks itself when last conn/route is removed */
636 ksocknal_del_peer(struct lnet_ni *ni, struct lnet_process_id id, __u32 ip)
639 struct hlist_node *pnxt;
640 struct ksock_peer_ni *peer_ni;
646 write_lock_bh(&ksocknal_data.ksnd_global_lock);
648 if (id.nid != LNET_NID_ANY) {
649 lo = hash_min(id.nid, HASH_BITS(ksocknal_data.ksnd_peers));
653 hi = HASH_SIZE(ksocknal_data.ksnd_peers) - 1;
656 for (i = lo; i <= hi; i++) {
657 hlist_for_each_entry_safe(peer_ni, pnxt,
658 &ksocknal_data.ksnd_peers[i],
660 if (peer_ni->ksnp_ni != ni)
663 if (!((id.nid == LNET_NID_ANY ||
664 peer_ni->ksnp_id.nid == id.nid) &&
665 (id.pid == LNET_PID_ANY ||
666 peer_ni->ksnp_id.pid == id.pid)))
669 ksocknal_peer_addref(peer_ni); /* a ref for me... */
671 ksocknal_del_peer_locked(peer_ni, ip);
673 if (peer_ni->ksnp_closing &&
674 !list_empty(&peer_ni->ksnp_tx_queue)) {
675 LASSERT(list_empty(&peer_ni->ksnp_conns));
676 LASSERT(list_empty(&peer_ni->ksnp_routes));
678 list_splice_init(&peer_ni->ksnp_tx_queue,
682 ksocknal_peer_decref(peer_ni); /* ...till here */
684 rc = 0; /* matched! */
688 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
690 ksocknal_txlist_done(ni, &zombies, -ENETDOWN);
695 static struct ksock_conn *
696 ksocknal_get_conn_by_idx(struct lnet_ni *ni, int index)
698 struct ksock_peer_ni *peer_ni;
699 struct ksock_conn *conn;
700 struct list_head *ctmp;
703 read_lock(&ksocknal_data.ksnd_global_lock);
705 hash_for_each(ksocknal_data.ksnd_peers, i, peer_ni, ksnp_list) {
706 LASSERT(!peer_ni->ksnp_closing);
708 if (peer_ni->ksnp_ni != ni)
711 list_for_each(ctmp, &peer_ni->ksnp_conns) {
715 conn = list_entry(ctmp, struct ksock_conn,
717 ksocknal_conn_addref(conn);
718 read_unlock(&ksocknal_data.ksnd_global_lock);
723 read_unlock(&ksocknal_data.ksnd_global_lock);
727 static struct ksock_sched *
728 ksocknal_choose_scheduler_locked(unsigned int cpt)
730 struct ksock_sched *sched = ksocknal_data.ksnd_schedulers[cpt];
733 if (sched->kss_nthreads == 0) {
734 cfs_percpt_for_each(sched, i, ksocknal_data.ksnd_schedulers) {
735 if (sched->kss_nthreads > 0) {
736 CDEBUG(D_NET, "scheduler[%d] has no threads. selected scheduler[%d]\n",
737 cpt, sched->kss_cpt);
748 ksocknal_local_ipvec(struct lnet_ni *ni, __u32 *ipaddrs)
750 struct ksock_net *net = ni->ni_data;
754 read_lock(&ksocknal_data.ksnd_global_lock);
756 nip = net->ksnn_ninterfaces;
757 LASSERT(nip <= LNET_INTERFACES_NUM);
759 for (i = 0, j = 0; i < nip; i++)
760 if (net->ksnn_interfaces[i].ksni_addr.ss_family == AF_INET) {
761 struct sockaddr_in *sa =
762 (void *)&net->ksnn_interfaces[i].ksni_addr;
764 ipaddrs[j] = ntohl(sa->sin_addr.s_addr);
765 LASSERT(ipaddrs[j] != 0);
770 read_unlock(&ksocknal_data.ksnd_global_lock);
772 * Only offer interfaces for additional connections if I have
775 return nip < 2 ? 0 : nip;
779 ksocknal_match_peerip(struct ksock_interface *iface, __u32 *ips, int nips)
781 int best_netmatch = 0;
787 struct sockaddr_in *sa;
790 sa = (struct sockaddr_in *)&iface->ksni_addr;
791 LASSERT(sa->sin_family == AF_INET);
792 ip = ntohl(sa->sin_addr.s_addr);
794 for (i = 0; i < nips; i++) {
798 this_xor = ips[i] ^ ip;
799 this_netmatch = ((this_xor & iface->ksni_netmask) == 0) ? 1 : 0;
802 best_netmatch < this_netmatch ||
803 (best_netmatch == this_netmatch &&
804 best_xor > this_xor)))
808 best_netmatch = this_netmatch;
817 ksocknal_select_ips(struct ksock_peer_ni *peer_ni, __u32 *peerips, int n_peerips)
819 rwlock_t *global_lock = &ksocknal_data.ksnd_global_lock;
820 struct ksock_net *net = peer_ni->ksnp_ni->ni_data;
821 struct ksock_interface *iface;
822 struct ksock_interface *best_iface;
833 /* CAVEAT EMPTOR: We do all our interface matching with an
834 * exclusive hold of global lock at IRQ priority. We're only
835 * expecting to be dealing with small numbers of interfaces, so the
836 * O(n**3)-ness shouldn't matter */
838 /* Also note that I'm not going to return more than n_peerips
839 * interfaces, even if I have more myself */
841 write_lock_bh(global_lock);
843 LASSERT(n_peerips <= LNET_INTERFACES_NUM);
844 LASSERT(net->ksnn_ninterfaces <= LNET_INTERFACES_NUM);
846 /* Only match interfaces for additional connections
847 * if I have > 1 interface
849 n_ips = (net->ksnn_ninterfaces < 2) ? 0 :
850 min(n_peerips, net->ksnn_ninterfaces);
852 for (i = 0; peer_ni->ksnp_n_passive_ips < n_ips; i++) {
853 /* ^ yes really... */
855 /* If we have any new interfaces, first tick off all the
856 * peer_ni IPs that match old interfaces, then choose new
857 * interfaces to match the remaining peer_ni IPS.
858 * We don't forget interfaces we've stopped using; we might
859 * start using them again... */
861 if (i < peer_ni->ksnp_n_passive_ips) {
863 struct sockaddr_in sa = { .sin_family = AF_INET};
866 htonl(peer_ni->ksnp_passive_ips[i]);
867 best_iface = ksocknal_ip2iface(peer_ni->ksnp_ni,
868 (struct sockaddr *)&sa);
870 /* peer_ni passive ips are kept up to date */
871 LASSERT(best_iface != NULL);
873 /* choose a new interface */
874 struct sockaddr_in *sa;
876 LASSERT (i == peer_ni->ksnp_n_passive_ips);
882 for (j = 0; j < net->ksnn_ninterfaces; j++) {
883 iface = &net->ksnn_interfaces[j];
884 sa = (void *)&iface->ksni_addr;
885 if (sa->sin_family != AF_INET)
887 ip = ntohl(sa->sin_addr.s_addr);
889 for (k = 0; k < peer_ni->ksnp_n_passive_ips; k++)
890 if (peer_ni->ksnp_passive_ips[k] == ip)
893 if (k < peer_ni->ksnp_n_passive_ips) /* using it already */
896 k = ksocknal_match_peerip(iface, peerips, n_peerips);
897 xor = (ip ^ peerips[k]);
898 this_netmatch = ((xor & iface->ksni_netmask) == 0) ? 1 : 0;
900 if (!(best_iface == NULL ||
901 best_netmatch < this_netmatch ||
902 (best_netmatch == this_netmatch &&
903 best_npeers > iface->ksni_npeers)))
907 best_netmatch = this_netmatch;
908 best_npeers = iface->ksni_npeers;
911 LASSERT(best_iface != NULL);
913 best_iface->ksni_npeers++;
914 sa = (void *)&best_iface->ksni_addr;
915 ip = ntohl(sa->sin_addr.s_addr);
916 peer_ni->ksnp_passive_ips[i] = ip;
917 peer_ni->ksnp_n_passive_ips = i+1;
920 /* mark the best matching peer_ni IP used */
921 j = ksocknal_match_peerip(best_iface, peerips, n_peerips);
925 /* Overwrite input peer_ni IP addresses */
926 memcpy(peerips, peer_ni->ksnp_passive_ips, n_ips * sizeof(*peerips));
928 write_unlock_bh(global_lock);
934 ksocknal_create_routes(struct ksock_peer_ni *peer_ni, int port,
935 __u32 *peer_ipaddrs, int npeer_ipaddrs)
937 struct ksock_route *newroute = NULL;
938 rwlock_t *global_lock = &ksocknal_data.ksnd_global_lock;
939 struct lnet_ni *ni = peer_ni->ksnp_ni;
940 struct ksock_net *net = ni->ni_data;
941 struct list_head *rtmp;
942 struct ksock_route *route;
943 struct ksock_interface *iface;
944 struct ksock_interface *best_iface;
951 /* CAVEAT EMPTOR: We do all our interface matching with an
952 * exclusive hold of global lock at IRQ priority. We're only
953 * expecting to be dealing with small numbers of interfaces, so the
954 * O(n**3)-ness here shouldn't matter */
956 write_lock_bh(global_lock);
958 if (net->ksnn_ninterfaces < 2) {
959 /* Only create additional connections
960 * if I have > 1 interface */
961 write_unlock_bh(global_lock);
965 LASSERT(npeer_ipaddrs <= LNET_INTERFACES_NUM);
967 for (i = 0; i < npeer_ipaddrs; i++) {
969 struct sockaddr_in *sa = (void *)&newroute->ksnr_addr;
971 memset(sa, 0, sizeof(*sa));
972 sa->sin_family = AF_INET;
973 sa->sin_addr.s_addr = htonl(peer_ipaddrs[i]);
975 struct sockaddr_in sa = {.sin_family = AF_INET};
977 write_unlock_bh(global_lock);
979 sa.sin_addr.s_addr = htonl(peer_ipaddrs[i]);
980 sa.sin_port = htons(port);
982 ksocknal_create_route((struct sockaddr *)&sa);
986 write_lock_bh(global_lock);
989 if (peer_ni->ksnp_closing) {
990 /* peer_ni got closed under me */
994 /* Already got a route? */
996 list_for_each(rtmp, &peer_ni->ksnp_routes) {
997 route = list_entry(rtmp, struct ksock_route, ksnr_list);
1000 (struct sockaddr *)&route->ksnr_addr,
1001 (struct sockaddr *)&newroute->ksnr_addr))
1013 LASSERT(net->ksnn_ninterfaces <= LNET_INTERFACES_NUM);
1015 /* Select interface to connect from */
1016 for (j = 0; j < net->ksnn_ninterfaces; j++) {
1017 __u32 iface_ip, route_ip;
1019 iface = &net->ksnn_interfaces[j];
1021 /* Using this interface already? */
1022 list_for_each(rtmp, &peer_ni->ksnp_routes) {
1023 route = list_entry(rtmp, struct ksock_route,
1026 if (route->ksnr_myiface == iface->ksni_index)
1033 if (iface->ksni_addr.ss_family != AF_INET)
1035 if (newroute->ksnr_addr.ss_family != AF_INET)
1039 ntohl(((struct sockaddr_in *)
1040 &iface->ksni_addr)->sin_addr.s_addr);
1042 ntohl(((struct sockaddr_in *)
1043 &newroute->ksnr_addr)->sin_addr.s_addr);
1045 this_netmatch = (((iface_ip ^ route_ip) &
1046 iface->ksni_netmask) == 0) ? 1 : 0;
1048 if (!(best_iface == NULL ||
1049 best_netmatch < this_netmatch ||
1050 (best_netmatch == this_netmatch &&
1051 best_nroutes > iface->ksni_nroutes)))
1055 best_netmatch = this_netmatch;
1056 best_nroutes = iface->ksni_nroutes;
1059 if (best_iface == NULL)
1062 newroute->ksnr_myiface = best_iface->ksni_index;
1063 best_iface->ksni_nroutes++;
1065 ksocknal_add_route_locked(peer_ni, newroute);
1069 write_unlock_bh(global_lock);
1070 if (newroute != NULL)
1071 ksocknal_route_decref(newroute);
1075 ksocknal_accept(struct lnet_ni *ni, struct socket *sock)
1077 struct ksock_connreq *cr;
1079 struct sockaddr_storage peer;
1081 rc = lnet_sock_getaddr(sock, true, &peer);
1082 LASSERT(rc == 0); /* we succeeded before */
1084 LIBCFS_ALLOC(cr, sizeof(*cr));
1086 LCONSOLE_ERROR_MSG(0x12f,
1087 "Dropping connection request from %pIS: memory exhausted\n",
1094 cr->ksncr_sock = sock;
1096 spin_lock_bh(&ksocknal_data.ksnd_connd_lock);
1098 list_add_tail(&cr->ksncr_list, &ksocknal_data.ksnd_connd_connreqs);
1099 wake_up(&ksocknal_data.ksnd_connd_waitq);
1101 spin_unlock_bh(&ksocknal_data.ksnd_connd_lock);
1106 ksocknal_connecting(struct ksock_peer_ni *peer_ni, struct sockaddr *sa)
1108 struct ksock_route *route;
1110 list_for_each_entry(route, &peer_ni->ksnp_routes, ksnr_list) {
1111 if (rpc_cmp_addr((struct sockaddr *)&route->ksnr_addr, sa))
1112 return route->ksnr_connecting;
1118 ksocknal_create_conn(struct lnet_ni *ni, struct ksock_route *route,
1119 struct socket *sock, int type)
1121 rwlock_t *global_lock = &ksocknal_data.ksnd_global_lock;
1123 struct lnet_process_id peerid;
1124 struct list_head *tmp;
1126 struct ksock_conn *conn;
1127 struct ksock_conn *conn2;
1128 struct ksock_peer_ni *peer_ni = NULL;
1129 struct ksock_peer_ni *peer2;
1130 struct ksock_sched *sched;
1131 struct ksock_hello_msg *hello;
1133 struct ksock_tx *tx;
1134 struct ksock_tx *txtmp;
1140 active = (route != NULL);
1142 LASSERT (active == (type != SOCKLND_CONN_NONE));
1144 LIBCFS_ALLOC(conn, sizeof(*conn));
1150 conn->ksnc_peer = NULL;
1151 conn->ksnc_route = NULL;
1152 conn->ksnc_sock = sock;
1153 /* 2 ref, 1 for conn, another extra ref prevents socket
1154 * being closed before establishment of connection */
1155 refcount_set(&conn->ksnc_sock_refcount, 2);
1156 conn->ksnc_type = type;
1157 ksocknal_lib_save_callback(sock, conn);
1158 refcount_set(&conn->ksnc_conn_refcount, 1); /* 1 ref for me */
1160 conn->ksnc_rx_ready = 0;
1161 conn->ksnc_rx_scheduled = 0;
1163 INIT_LIST_HEAD(&conn->ksnc_tx_queue);
1164 conn->ksnc_tx_ready = 0;
1165 conn->ksnc_tx_scheduled = 0;
1166 conn->ksnc_tx_carrier = NULL;
1167 atomic_set (&conn->ksnc_tx_nob, 0);
1169 LIBCFS_ALLOC(hello, offsetof(struct ksock_hello_msg,
1170 kshm_ips[LNET_INTERFACES_NUM]));
1171 if (hello == NULL) {
1176 /* stash conn's local and remote addrs */
1177 rc = ksocknal_lib_get_conn_addrs (conn);
1181 /* Find out/confirm peer_ni's NID and connection type and get the
1182 * vector of interfaces she's willing to let me connect to.
1183 * Passive connections use the listener timeout since the peer_ni sends
1187 peer_ni = route->ksnr_peer;
1188 LASSERT(ni == peer_ni->ksnp_ni);
1190 /* Active connection sends HELLO eagerly */
1191 hello->kshm_nips = ksocknal_local_ipvec(ni, hello->kshm_ips);
1192 peerid = peer_ni->ksnp_id;
1194 write_lock_bh(global_lock);
1195 conn->ksnc_proto = peer_ni->ksnp_proto;
1196 write_unlock_bh(global_lock);
1198 if (conn->ksnc_proto == NULL) {
1199 conn->ksnc_proto = &ksocknal_protocol_v3x;
1200 #if SOCKNAL_VERSION_DEBUG
1201 if (*ksocknal_tunables.ksnd_protocol == 2)
1202 conn->ksnc_proto = &ksocknal_protocol_v2x;
1203 else if (*ksocknal_tunables.ksnd_protocol == 1)
1204 conn->ksnc_proto = &ksocknal_protocol_v1x;
1208 rc = ksocknal_send_hello (ni, conn, peerid.nid, hello);
1212 peerid.nid = LNET_NID_ANY;
1213 peerid.pid = LNET_PID_ANY;
1215 /* Passive, get protocol from peer_ni */
1216 conn->ksnc_proto = NULL;
1219 rc = ksocknal_recv_hello (ni, conn, hello, &peerid, &incarnation);
1223 LASSERT (rc == 0 || active);
1224 LASSERT (conn->ksnc_proto != NULL);
1225 LASSERT (peerid.nid != LNET_NID_ANY);
1227 cpt = lnet_cpt_of_nid(peerid.nid, ni);
1230 ksocknal_peer_addref(peer_ni);
1231 write_lock_bh(global_lock);
1233 peer_ni = ksocknal_create_peer(ni, peerid);
1234 if (IS_ERR(peer_ni)) {
1235 rc = PTR_ERR(peer_ni);
1239 write_lock_bh(global_lock);
1241 /* called with a ref on ni, so shutdown can't have started */
1242 LASSERT(atomic_read(&((struct ksock_net *)ni->ni_data)->ksnn_npeers) >= 0);
1244 peer2 = ksocknal_find_peer_locked(ni, peerid);
1245 if (peer2 == NULL) {
1246 /* NB this puts an "empty" peer_ni in the peer_ni
1247 * table (which takes my ref) */
1248 hash_add(ksocknal_data.ksnd_peers,
1249 &peer_ni->ksnp_list, peerid.nid);
1251 ksocknal_peer_decref(peer_ni);
1256 ksocknal_peer_addref(peer_ni);
1257 peer_ni->ksnp_accepting++;
1259 /* Am I already connecting to this guy? Resolve in
1260 * favour of higher NID...
1262 if (peerid.nid < ni->ni_nid &&
1263 ksocknal_connecting(peer_ni, ((struct sockaddr *)
1264 &conn->ksnc_peeraddr))) {
1266 warn = "connection race resolution";
1271 if (peer_ni->ksnp_closing ||
1272 (active && route->ksnr_deleted)) {
1273 /* peer_ni/route got closed under me */
1275 warn = "peer_ni/route removed";
1279 if (peer_ni->ksnp_proto == NULL) {
1280 /* Never connected before.
1281 * NB recv_hello may have returned EPROTO to signal my peer_ni
1282 * wants a different protocol than the one I asked for.
1284 LASSERT(list_empty(&peer_ni->ksnp_conns));
1286 peer_ni->ksnp_proto = conn->ksnc_proto;
1287 peer_ni->ksnp_incarnation = incarnation;
1290 if (peer_ni->ksnp_proto != conn->ksnc_proto ||
1291 peer_ni->ksnp_incarnation != incarnation) {
1292 /* peer_ni rebooted or I've got the wrong protocol version */
1293 ksocknal_close_peer_conns_locked(peer_ni, NULL, 0);
1295 peer_ni->ksnp_proto = NULL;
1297 warn = peer_ni->ksnp_incarnation != incarnation ?
1298 "peer_ni rebooted" :
1299 "wrong proto version";
1309 warn = "lost conn race";
1312 warn = "retry with different protocol version";
1316 /* Refuse to duplicate an existing connection, unless this is a
1317 * loopback connection */
1318 if (!rpc_cmp_addr((struct sockaddr *)&conn->ksnc_peeraddr,
1319 (struct sockaddr *)&conn->ksnc_myaddr)) {
1320 list_for_each(tmp, &peer_ni->ksnp_conns) {
1321 conn2 = list_entry(tmp, struct ksock_conn, ksnc_list);
1324 (struct sockaddr *)&conn2->ksnc_peeraddr,
1325 (struct sockaddr *)&conn->ksnc_peeraddr) ||
1327 (struct sockaddr *)&conn2->ksnc_myaddr,
1328 (struct sockaddr *)&conn->ksnc_myaddr) ||
1329 conn2->ksnc_type != conn->ksnc_type)
1332 /* Reply on a passive connection attempt so the peer_ni
1333 * realises we're connected. */
1343 /* If the connection created by this route didn't bind to the IP
1344 * address the route connected to, the connection/route matching
1345 * code below probably isn't going to work. */
1347 !rpc_cmp_addr((struct sockaddr *)&route->ksnr_addr,
1348 (struct sockaddr *)&conn->ksnc_peeraddr)) {
1349 CERROR("Route %s %pIS connected to %pIS\n",
1350 libcfs_id2str(peer_ni->ksnp_id),
1352 &conn->ksnc_peeraddr);
1355 /* Search for a route corresponding to the new connection and
1356 * create an association. This allows incoming connections created
1357 * by routes in my peer_ni to match my own route entries so I don't
1358 * continually create duplicate routes. */
1359 list_for_each(tmp, &peer_ni->ksnp_routes) {
1360 route = list_entry(tmp, struct ksock_route, ksnr_list);
1362 if (!rpc_cmp_addr((struct sockaddr *)&route->ksnr_addr,
1363 (struct sockaddr *)&conn->ksnc_peeraddr))
1366 ksocknal_associate_route_conn_locked(route, conn);
1370 conn->ksnc_peer = peer_ni; /* conn takes my ref on peer_ni */
1371 peer_ni->ksnp_last_alive = ktime_get_seconds();
1372 peer_ni->ksnp_send_keepalive = 0;
1373 peer_ni->ksnp_error = 0;
1375 sched = ksocknal_choose_scheduler_locked(cpt);
1377 CERROR("no schedulers available. node is unhealthy\n");
1381 * The cpt might have changed if we ended up selecting a non cpt
1382 * native scheduler. So use the scheduler's cpt instead.
1384 cpt = sched->kss_cpt;
1385 sched->kss_nconns++;
1386 conn->ksnc_scheduler = sched;
1388 conn->ksnc_tx_last_post = ktime_get_seconds();
1389 /* Set the deadline for the outgoing HELLO to drain */
1390 conn->ksnc_tx_bufnob = sock->sk->sk_wmem_queued;
1391 conn->ksnc_tx_deadline = ktime_get_seconds() +
1393 smp_mb(); /* order with adding to peer_ni's conn list */
1395 list_add(&conn->ksnc_list, &peer_ni->ksnp_conns);
1396 ksocknal_conn_addref(conn);
1398 ksocknal_new_packet(conn, 0);
1400 conn->ksnc_zc_capable = ksocknal_lib_zc_capable(conn);
1402 /* Take packets blocking for this connection. */
1403 list_for_each_entry_safe(tx, txtmp, &peer_ni->ksnp_tx_queue, tx_list) {
1404 if (conn->ksnc_proto->pro_match_tx(conn, tx, tx->tx_nonblk) ==
1408 list_del(&tx->tx_list);
1409 ksocknal_queue_tx_locked(tx, conn);
1412 write_unlock_bh(global_lock);
1414 /* We've now got a new connection. Any errors from here on are just
1415 * like "normal" comms errors and we close the connection normally.
1416 * NB (a) we still have to send the reply HELLO for passive
1418 * (b) normal I/O on the conn is blocked until I setup and call the
1422 CDEBUG(D_NET, "New conn %s p %d.x %pIS -> %pISp"
1423 " incarnation:%lld sched[%d]\n",
1424 libcfs_id2str(peerid), conn->ksnc_proto->pro_version,
1425 &conn->ksnc_myaddr, &conn->ksnc_peeraddr,
1429 /* additional routes after interface exchange? */
1430 ksocknal_create_routes(
1432 rpc_get_port((struct sockaddr *)&conn->ksnc_peeraddr),
1433 hello->kshm_ips, hello->kshm_nips);
1435 hello->kshm_nips = ksocknal_select_ips(peer_ni, hello->kshm_ips,
1437 rc = ksocknal_send_hello(ni, conn, peerid.nid, hello);
1440 LIBCFS_FREE(hello, offsetof(struct ksock_hello_msg,
1441 kshm_ips[LNET_INTERFACES_NUM]));
1443 /* setup the socket AFTER I've received hello (it disables
1444 * SO_LINGER). I might call back to the acceptor who may want
1445 * to send a protocol version response and then close the
1446 * socket; this ensures the socket only tears down after the
1447 * response has been sent. */
1449 rc = ksocknal_lib_setup_sock(sock);
1451 write_lock_bh(global_lock);
1453 /* NB my callbacks block while I hold ksnd_global_lock */
1454 ksocknal_lib_set_callback(sock, conn);
1457 peer_ni->ksnp_accepting--;
1459 write_unlock_bh(global_lock);
1462 write_lock_bh(global_lock);
1463 if (!conn->ksnc_closing) {
1464 /* could be closed by another thread */
1465 ksocknal_close_conn_locked(conn, rc);
1467 write_unlock_bh(global_lock);
1468 } else if (ksocknal_connsock_addref(conn) == 0) {
1469 /* Allow I/O to proceed. */
1470 ksocknal_read_callback(conn);
1471 ksocknal_write_callback(conn);
1472 ksocknal_connsock_decref(conn);
1475 ksocknal_connsock_decref(conn);
1476 ksocknal_conn_decref(conn);
1480 if (!peer_ni->ksnp_closing &&
1481 list_empty(&peer_ni->ksnp_conns) &&
1482 list_empty(&peer_ni->ksnp_routes)) {
1483 list_splice_init(&peer_ni->ksnp_tx_queue, &zombies);
1484 ksocknal_unlink_peer_locked(peer_ni);
1487 write_unlock_bh(global_lock);
1491 CERROR("Not creating conn %s type %d: %s\n",
1492 libcfs_id2str(peerid), conn->ksnc_type, warn);
1494 CDEBUG(D_NET, "Not creating conn %s type %d: %s\n",
1495 libcfs_id2str(peerid), conn->ksnc_type, warn);
1500 /* Request retry by replying with CONN_NONE
1501 * ksnc_proto has been set already */
1502 conn->ksnc_type = SOCKLND_CONN_NONE;
1503 hello->kshm_nips = 0;
1504 ksocknal_send_hello(ni, conn, peerid.nid, hello);
1507 write_lock_bh(global_lock);
1508 peer_ni->ksnp_accepting--;
1509 write_unlock_bh(global_lock);
1513 * If we get here without an error code, just use -EALREADY.
1514 * Depending on how we got here, the error may be positive
1515 * or negative. Normalize the value for ksocknal_txlist_done().
1517 rc2 = (rc == 0 ? -EALREADY : (rc > 0 ? -rc : rc));
1518 ksocknal_txlist_done(ni, &zombies, rc2);
1519 ksocknal_peer_decref(peer_ni);
1523 LIBCFS_FREE(hello, offsetof(struct ksock_hello_msg,
1524 kshm_ips[LNET_INTERFACES_NUM]));
1526 LIBCFS_FREE(conn, sizeof(*conn));
1534 ksocknal_close_conn_locked(struct ksock_conn *conn, int error)
1536 /* This just does the immmediate housekeeping, and queues the
1537 * connection for the reaper to terminate.
1538 * Caller holds ksnd_global_lock exclusively in irq context */
1539 struct ksock_peer_ni *peer_ni = conn->ksnc_peer;
1540 struct ksock_route *route;
1541 struct ksock_conn *conn2;
1542 struct list_head *tmp;
1544 LASSERT(peer_ni->ksnp_error == 0);
1545 LASSERT(!conn->ksnc_closing);
1546 conn->ksnc_closing = 1;
1548 /* ksnd_deathrow_conns takes over peer_ni's ref */
1549 list_del(&conn->ksnc_list);
1551 route = conn->ksnc_route;
1552 if (route != NULL) {
1553 /* dissociate conn from route... */
1554 LASSERT(!route->ksnr_deleted);
1555 LASSERT((route->ksnr_connected & BIT(conn->ksnc_type)) != 0);
1558 list_for_each(tmp, &peer_ni->ksnp_conns) {
1559 conn2 = list_entry(tmp, struct ksock_conn, ksnc_list);
1561 if (conn2->ksnc_route == route &&
1562 conn2->ksnc_type == conn->ksnc_type)
1568 route->ksnr_connected &= ~BIT(conn->ksnc_type);
1570 conn->ksnc_route = NULL;
1572 ksocknal_route_decref(route); /* drop conn's ref on route */
1575 if (list_empty(&peer_ni->ksnp_conns)) {
1576 /* No more connections to this peer_ni */
1578 if (!list_empty(&peer_ni->ksnp_tx_queue)) {
1579 struct ksock_tx *tx;
1581 LASSERT(conn->ksnc_proto == &ksocknal_protocol_v3x);
1583 /* throw them to the last connection...,
1584 * these TXs will be send to /dev/null by scheduler */
1585 list_for_each_entry(tx, &peer_ni->ksnp_tx_queue,
1587 ksocknal_tx_prep(conn, tx);
1589 spin_lock_bh(&conn->ksnc_scheduler->kss_lock);
1590 list_splice_init(&peer_ni->ksnp_tx_queue,
1591 &conn->ksnc_tx_queue);
1592 spin_unlock_bh(&conn->ksnc_scheduler->kss_lock);
1595 /* renegotiate protocol version */
1596 peer_ni->ksnp_proto = NULL;
1597 /* stash last conn close reason */
1598 peer_ni->ksnp_error = error;
1600 if (list_empty(&peer_ni->ksnp_routes)) {
1601 /* I've just closed last conn belonging to a
1602 * peer_ni with no routes to it */
1603 ksocknal_unlink_peer_locked(peer_ni);
1607 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
1609 list_add_tail(&conn->ksnc_list, &ksocknal_data.ksnd_deathrow_conns);
1610 wake_up(&ksocknal_data.ksnd_reaper_waitq);
1612 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
1616 ksocknal_peer_failed(struct ksock_peer_ni *peer_ni)
1619 time64_t last_alive = 0;
1621 /* There has been a connection failure or comms error; but I'll only
1622 * tell LNET I think the peer_ni is dead if it's to another kernel and
1623 * there are no connections or connection attempts in existence. */
1625 read_lock(&ksocknal_data.ksnd_global_lock);
1627 if ((peer_ni->ksnp_id.pid & LNET_PID_USERFLAG) == 0 &&
1628 list_empty(&peer_ni->ksnp_conns) &&
1629 peer_ni->ksnp_accepting == 0 &&
1630 ksocknal_find_connecting_route_locked(peer_ni) == NULL) {
1632 last_alive = peer_ni->ksnp_last_alive;
1635 read_unlock(&ksocknal_data.ksnd_global_lock);
1638 lnet_notify(peer_ni->ksnp_ni, peer_ni->ksnp_id.nid,
1639 false, false, last_alive);
1643 ksocknal_finalize_zcreq(struct ksock_conn *conn)
1645 struct ksock_peer_ni *peer_ni = conn->ksnc_peer;
1646 struct ksock_tx *tx;
1647 struct ksock_tx *tmp;
1650 /* NB safe to finalize TXs because closing of socket will
1651 * abort all buffered data */
1652 LASSERT(conn->ksnc_sock == NULL);
1654 spin_lock(&peer_ni->ksnp_lock);
1656 list_for_each_entry_safe(tx, tmp, &peer_ni->ksnp_zc_req_list, tx_zc_list) {
1657 if (tx->tx_conn != conn)
1660 LASSERT(tx->tx_msg.ksm_zc_cookies[0] != 0);
1662 tx->tx_msg.ksm_zc_cookies[0] = 0;
1663 tx->tx_zc_aborted = 1; /* mark it as not-acked */
1664 list_move(&tx->tx_zc_list, &zlist);
1667 spin_unlock(&peer_ni->ksnp_lock);
1669 while (!list_empty(&zlist)) {
1670 tx = list_entry(zlist.next, struct ksock_tx, tx_zc_list);
1672 list_del(&tx->tx_zc_list);
1673 ksocknal_tx_decref(tx);
1678 ksocknal_terminate_conn(struct ksock_conn *conn)
1680 /* This gets called by the reaper (guaranteed thread context) to
1681 * disengage the socket from its callbacks and close it.
1682 * ksnc_refcount will eventually hit zero, and then the reaper will
1684 struct ksock_peer_ni *peer_ni = conn->ksnc_peer;
1685 struct ksock_sched *sched = conn->ksnc_scheduler;
1688 LASSERT(conn->ksnc_closing);
1690 /* wake up the scheduler to "send" all remaining packets to /dev/null */
1691 spin_lock_bh(&sched->kss_lock);
1693 /* a closing conn is always ready to tx */
1694 conn->ksnc_tx_ready = 1;
1696 if (!conn->ksnc_tx_scheduled &&
1697 !list_empty(&conn->ksnc_tx_queue)) {
1698 list_add_tail(&conn->ksnc_tx_list,
1699 &sched->kss_tx_conns);
1700 conn->ksnc_tx_scheduled = 1;
1701 /* extra ref for scheduler */
1702 ksocknal_conn_addref(conn);
1704 wake_up (&sched->kss_waitq);
1707 spin_unlock_bh(&sched->kss_lock);
1709 /* serialise with callbacks */
1710 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1712 ksocknal_lib_reset_callback(conn->ksnc_sock, conn);
1714 /* OK, so this conn may not be completely disengaged from its
1715 * scheduler yet, but it _has_ committed to terminate... */
1716 conn->ksnc_scheduler->kss_nconns--;
1718 if (peer_ni->ksnp_error != 0) {
1719 /* peer_ni's last conn closed in error */
1720 LASSERT(list_empty(&peer_ni->ksnp_conns));
1722 peer_ni->ksnp_error = 0; /* avoid multiple notifications */
1725 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1728 ksocknal_peer_failed(peer_ni);
1730 /* The socket is closed on the final put; either here, or in
1731 * ksocknal_{send,recv}msg(). Since we set up the linger2 option
1732 * when the connection was established, this will close the socket
1733 * immediately, aborting anything buffered in it. Any hung
1734 * zero-copy transmits will therefore complete in finite time. */
1735 ksocknal_connsock_decref(conn);
1739 ksocknal_queue_zombie_conn(struct ksock_conn *conn)
1741 /* Queue the conn for the reaper to destroy */
1742 LASSERT(refcount_read(&conn->ksnc_conn_refcount) == 0);
1743 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
1745 list_add_tail(&conn->ksnc_list, &ksocknal_data.ksnd_zombie_conns);
1746 wake_up(&ksocknal_data.ksnd_reaper_waitq);
1748 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
1752 ksocknal_destroy_conn(struct ksock_conn *conn)
1756 /* Final coup-de-grace of the reaper */
1757 CDEBUG (D_NET, "connection %p\n", conn);
1759 LASSERT(refcount_read(&conn->ksnc_conn_refcount) == 0);
1760 LASSERT(refcount_read(&conn->ksnc_sock_refcount) == 0);
1761 LASSERT (conn->ksnc_sock == NULL);
1762 LASSERT (conn->ksnc_route == NULL);
1763 LASSERT (!conn->ksnc_tx_scheduled);
1764 LASSERT (!conn->ksnc_rx_scheduled);
1765 LASSERT(list_empty(&conn->ksnc_tx_queue));
1767 /* complete current receive if any */
1768 switch (conn->ksnc_rx_state) {
1769 case SOCKNAL_RX_LNET_PAYLOAD:
1770 last_rcv = conn->ksnc_rx_deadline -
1772 CERROR("Completing partial receive from %s[%d], ip %pISp, with error, wanted: %d, left: %d, last alive is %lld secs ago\n",
1773 libcfs_id2str(conn->ksnc_peer->ksnp_id), conn->ksnc_type,
1774 &conn->ksnc_peeraddr,
1775 conn->ksnc_rx_nob_wanted, conn->ksnc_rx_nob_left,
1776 ktime_get_seconds() - last_rcv);
1777 if (conn->ksnc_lnet_msg)
1778 conn->ksnc_lnet_msg->msg_health_status =
1779 LNET_MSG_STATUS_REMOTE_ERROR;
1780 lnet_finalize(conn->ksnc_lnet_msg, -EIO);
1782 case SOCKNAL_RX_LNET_HEADER:
1783 if (conn->ksnc_rx_started)
1784 CERROR("Incomplete receive of lnet header from %s, ip %pISp, with error, protocol: %d.x.\n",
1785 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1786 &conn->ksnc_peeraddr,
1787 conn->ksnc_proto->pro_version);
1789 case SOCKNAL_RX_KSM_HEADER:
1790 if (conn->ksnc_rx_started)
1791 CERROR("Incomplete receive of ksock message from %s, ip %pISp, with error, protocol: %d.x.\n",
1792 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1793 &conn->ksnc_peeraddr,
1794 conn->ksnc_proto->pro_version);
1796 case SOCKNAL_RX_SLOP:
1797 if (conn->ksnc_rx_started)
1798 CERROR("Incomplete receive of slops from %s, ip %pISp, with error\n",
1799 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1800 &conn->ksnc_peeraddr);
1807 ksocknal_peer_decref(conn->ksnc_peer);
1809 LIBCFS_FREE (conn, sizeof (*conn));
1813 ksocknal_close_peer_conns_locked(struct ksock_peer_ni *peer_ni,
1814 struct sockaddr *addr, int why)
1816 struct ksock_conn *conn;
1817 struct ksock_conn *cnxt;
1820 list_for_each_entry_safe(conn, cnxt, &peer_ni->ksnp_conns, ksnc_list) {
1823 (struct sockaddr *)&conn->ksnc_peeraddr)) {
1825 ksocknal_close_conn_locked(conn, why);
1833 ksocknal_close_conn_and_siblings(struct ksock_conn *conn, int why)
1835 struct ksock_peer_ni *peer_ni = conn->ksnc_peer;
1838 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1840 count = ksocknal_close_peer_conns_locked(
1841 peer_ni, (struct sockaddr *)&conn->ksnc_peeraddr, why);
1843 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1849 ksocknal_close_matching_conns(struct lnet_process_id id, __u32 ipaddr)
1851 struct ksock_peer_ni *peer_ni;
1852 struct hlist_node *pnxt;
1857 struct sockaddr_in sa = {.sin_family = AF_INET};
1859 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1861 if (id.nid != LNET_NID_ANY) {
1862 lo = hash_min(id.nid, HASH_BITS(ksocknal_data.ksnd_peers));
1866 hi = HASH_SIZE(ksocknal_data.ksnd_peers) - 1;
1869 sa.sin_addr.s_addr = htonl(ipaddr);
1870 for (i = lo; i <= hi; i++) {
1871 hlist_for_each_entry_safe(peer_ni, pnxt,
1872 &ksocknal_data.ksnd_peers[i],
1875 if (!((id.nid == LNET_NID_ANY ||
1876 id.nid == peer_ni->ksnp_id.nid) &&
1877 (id.pid == LNET_PID_ANY ||
1878 id.pid == peer_ni->ksnp_id.pid)))
1881 count += ksocknal_close_peer_conns_locked(
1883 ipaddr ? (struct sockaddr *)&sa : NULL, 0);
1887 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1889 /* wildcards always succeed */
1890 if (id.nid == LNET_NID_ANY || id.pid == LNET_PID_ANY || ipaddr == 0)
1893 return (count == 0 ? -ENOENT : 0);
1897 ksocknal_notify_gw_down(lnet_nid_t gw_nid)
1899 /* The router is telling me she's been notified of a change in
1902 struct lnet_process_id id = {
1904 .pid = LNET_PID_ANY,
1907 CDEBUG(D_NET, "gw %s down\n", libcfs_nid2str(gw_nid));
1909 /* If the gateway crashed, close all open connections... */
1910 ksocknal_close_matching_conns(id, 0);
1913 /* We can only establish new connections
1914 * if we have autroutes, and these connect on demand. */
1918 ksocknal_push_peer(struct ksock_peer_ni *peer_ni)
1922 struct list_head *tmp;
1923 struct ksock_conn *conn;
1925 for (index = 0; ; index++) {
1926 read_lock(&ksocknal_data.ksnd_global_lock);
1931 list_for_each(tmp, &peer_ni->ksnp_conns) {
1933 conn = list_entry(tmp, struct ksock_conn,
1935 ksocknal_conn_addref(conn);
1940 read_unlock(&ksocknal_data.ksnd_global_lock);
1945 ksocknal_lib_push_conn (conn);
1946 ksocknal_conn_decref(conn);
1951 ksocknal_push(struct lnet_ni *ni, struct lnet_process_id id)
1958 if (id.nid != LNET_NID_ANY) {
1959 lo = hash_min(id.nid, HASH_BITS(ksocknal_data.ksnd_peers));
1963 hi = HASH_SIZE(ksocknal_data.ksnd_peers) - 1;
1966 for (bkt = lo; bkt <= hi; bkt++) {
1967 int peer_off; /* searching offset in peer_ni hash table */
1969 for (peer_off = 0; ; peer_off++) {
1970 struct ksock_peer_ni *peer_ni;
1973 read_lock(&ksocknal_data.ksnd_global_lock);
1974 hlist_for_each_entry(peer_ni,
1975 &ksocknal_data.ksnd_peers[bkt],
1977 if (!((id.nid == LNET_NID_ANY ||
1978 id.nid == peer_ni->ksnp_id.nid) &&
1979 (id.pid == LNET_PID_ANY ||
1980 id.pid == peer_ni->ksnp_id.pid)))
1983 if (i++ == peer_off) {
1984 ksocknal_peer_addref(peer_ni);
1988 read_unlock(&ksocknal_data.ksnd_global_lock);
1990 if (i <= peer_off) /* no match */
1994 ksocknal_push_peer(peer_ni);
1995 ksocknal_peer_decref(peer_ni);
2002 ksocknal_add_interface(struct lnet_ni *ni, __u32 ipaddress, __u32 netmask)
2004 struct ksock_net *net = ni->ni_data;
2005 struct ksock_interface *iface;
2006 struct sockaddr_in sa = { .sin_family = AF_INET };
2010 struct ksock_peer_ni *peer_ni;
2011 struct list_head *rtmp;
2012 struct ksock_route *route;
2014 if (ipaddress == 0 ||
2018 write_lock_bh(&ksocknal_data.ksnd_global_lock);
2020 sa.sin_addr.s_addr = htonl(ipaddress);
2021 iface = ksocknal_ip2iface(ni, (struct sockaddr *)&sa);
2022 if (iface != NULL) {
2023 /* silently ignore dups */
2025 } else if (net->ksnn_ninterfaces == LNET_INTERFACES_NUM) {
2028 iface = &net->ksnn_interfaces[net->ksnn_ninterfaces++];
2030 iface->ksni_index = ksocknal_ip2index((struct sockaddr *)&sa,
2032 rpc_copy_addr((struct sockaddr *)&iface->ksni_addr,
2033 (struct sockaddr *)&sa);
2034 iface->ksni_netmask = netmask;
2035 iface->ksni_nroutes = 0;
2036 iface->ksni_npeers = 0;
2038 hash_for_each(ksocknal_data.ksnd_peers, i, peer_ni, ksnp_list) {
2039 for (j = 0; j < peer_ni->ksnp_n_passive_ips; j++)
2040 if (peer_ni->ksnp_passive_ips[j] == ipaddress)
2041 iface->ksni_npeers++;
2043 list_for_each(rtmp, &peer_ni->ksnp_routes) {
2044 route = list_entry(rtmp,
2048 if (route->ksnr_myiface ==
2050 iface->ksni_nroutes++;
2055 /* NB only new connections will pay attention to the new
2060 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
2066 ksocknal_peer_del_interface_locked(struct ksock_peer_ni *peer_ni,
2067 __u32 ipaddr, int index)
2069 struct ksock_route *route;
2070 struct ksock_route *rnxt;
2071 struct ksock_conn *conn;
2072 struct ksock_conn *cnxt;
2076 for (i = 0; i < peer_ni->ksnp_n_passive_ips; i++)
2077 if (peer_ni->ksnp_passive_ips[i] == ipaddr) {
2078 for (j = i+1; j < peer_ni->ksnp_n_passive_ips; j++)
2079 peer_ni->ksnp_passive_ips[j-1] =
2080 peer_ni->ksnp_passive_ips[j];
2081 peer_ni->ksnp_n_passive_ips--;
2085 list_for_each_entry_safe(route, rnxt, &peer_ni->ksnp_routes,
2087 if (route->ksnr_myiface != index)
2090 if (route->ksnr_share_count != 0) {
2091 /* Manually created; keep, but unbind */
2092 route->ksnr_myiface = -1;
2094 ksocknal_del_route_locked(route);
2098 list_for_each_entry_safe(conn, cnxt, &peer_ni->ksnp_conns, ksnc_list)
2099 if (conn->ksnc_route->ksnr_myiface == index)
2100 ksocknal_close_conn_locked (conn, 0);
2104 ksocknal_del_interface(struct lnet_ni *ni, __u32 ipaddress)
2106 struct ksock_net *net = ni->ni_data;
2108 struct hlist_node *nxt;
2109 struct ksock_peer_ni *peer_ni;
2111 struct sockaddr_in sa = {.sin_family = AF_INET };
2116 sa.sin_addr.s_addr = htonl(ipaddress);
2117 index = ksocknal_ip2index((struct sockaddr *)&sa, ni);
2119 write_lock_bh(&ksocknal_data.ksnd_global_lock);
2121 for (i = 0; i < net->ksnn_ninterfaces; i++) {
2122 struct sockaddr_in *sa =
2123 (void *)&net->ksnn_interfaces[i].ksni_addr;
2125 if (sa->sin_family != AF_INET)
2127 this_ip = ntohl(sa->sin_addr.s_addr);
2129 if (!(ipaddress == 0 ||
2130 ipaddress == this_ip))
2135 for (j = i+1; j < net->ksnn_ninterfaces; j++)
2136 net->ksnn_interfaces[j-1] =
2137 net->ksnn_interfaces[j];
2139 net->ksnn_ninterfaces--;
2141 hash_for_each_safe(ksocknal_data.ksnd_peers, j,
2142 nxt, peer_ni, ksnp_list) {
2143 if (peer_ni->ksnp_ni != ni)
2146 ksocknal_peer_del_interface_locked(peer_ni,
2151 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
2157 ksocknal_ctl(struct lnet_ni *ni, unsigned int cmd, void *arg)
2159 struct lnet_process_id id = {0};
2160 struct libcfs_ioctl_data *data = arg;
2164 case IOC_LIBCFS_GET_INTERFACE: {
2165 struct ksock_net *net = ni->ni_data;
2166 struct ksock_interface *iface;
2167 struct sockaddr_in *sa;
2169 read_lock(&ksocknal_data.ksnd_global_lock);
2171 if (data->ioc_count >= (__u32)net->ksnn_ninterfaces) {
2175 iface = &net->ksnn_interfaces[data->ioc_count];
2177 sa = (void *)&iface->ksni_addr;
2178 if (sa->sin_family == AF_INET)
2179 data->ioc_u32[0] = ntohl(sa->sin_addr.s_addr);
2181 data->ioc_u32[0] = 0xFFFFFFFF;
2182 data->ioc_u32[1] = iface->ksni_netmask;
2183 data->ioc_u32[2] = iface->ksni_npeers;
2184 data->ioc_u32[3] = iface->ksni_nroutes;
2187 read_unlock(&ksocknal_data.ksnd_global_lock);
2191 case IOC_LIBCFS_ADD_INTERFACE:
2192 return ksocknal_add_interface(ni,
2193 data->ioc_u32[0], /* IP address */
2194 data->ioc_u32[1]); /* net mask */
2196 case IOC_LIBCFS_DEL_INTERFACE:
2197 return ksocknal_del_interface(ni,
2198 data->ioc_u32[0]); /* IP address */
2200 case IOC_LIBCFS_GET_PEER: {
2205 int share_count = 0;
2207 rc = ksocknal_get_peer_info(ni, data->ioc_count,
2208 &id, &myip, &ip, &port,
2209 &conn_count, &share_count);
2213 data->ioc_nid = id.nid;
2214 data->ioc_count = share_count;
2215 data->ioc_u32[0] = ip;
2216 data->ioc_u32[1] = port;
2217 data->ioc_u32[2] = myip;
2218 data->ioc_u32[3] = conn_count;
2219 data->ioc_u32[4] = id.pid;
2223 case IOC_LIBCFS_ADD_PEER:
2224 id.nid = data->ioc_nid;
2225 id.pid = LNET_PID_LUSTRE;
2226 return ksocknal_add_peer (ni, id,
2227 data->ioc_u32[0], /* IP */
2228 data->ioc_u32[1]); /* port */
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);