1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
5 * Author: Zach Brown <zab@zabbo.net>
6 * Author: Peter J. Braam <braam@clusterfs.com>
7 * Author: Phil Schwan <phil@clusterfs.com>
8 * Author: Eric Barton <eric@bartonsoftware.com>
10 * This file is part of Portals, http://www.sf.net/projects/sandiaportals/
12 * Portals is free software; you can redistribute it and/or
13 * modify it under the terms of version 2 of the GNU General Public
14 * License as published by the Free Software Foundation.
16 * Portals is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with Portals; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 lnd_t the_ksocklnd = {
30 .lnd_startup = ksocknal_startup,
31 .lnd_shutdown = ksocknal_shutdown,
32 .lnd_ctl = ksocknal_ctl,
33 .lnd_send = ksocknal_send,
34 .lnd_recv = ksocknal_recv,
35 .lnd_notify = ksocknal_notify,
36 .lnd_accept = ksocknal_accept,
39 ksock_nal_data_t ksocknal_data;
42 ksocknal_ip2iface(lnet_ni_t *ni, __u32 ip)
44 ksock_net_t *net = ni->ni_data;
46 ksock_interface_t *iface;
48 for (i = 0; i < net->ksnn_ninterfaces; i++) {
49 LASSERT(i < LNET_MAX_INTERFACES);
50 iface = &net->ksnn_interfaces[i];
52 if (iface->ksni_ipaddr == ip)
60 ksocknal_create_route (__u32 ipaddr, int port)
64 LIBCFS_ALLOC (route, sizeof (*route));
68 atomic_set (&route->ksnr_refcount, 1);
69 route->ksnr_peer = NULL;
70 route->ksnr_retry_interval = 0; /* OK to connect at any time */
71 route->ksnr_ipaddr = ipaddr;
72 route->ksnr_port = port;
73 route->ksnr_scheduled = 0;
74 route->ksnr_connecting = 0;
75 route->ksnr_connected = 0;
76 route->ksnr_deleted = 0;
77 route->ksnr_conn_count = 0;
78 route->ksnr_share_count = 0;
84 ksocknal_destroy_route (ksock_route_t *route)
86 LASSERT (atomic_read(&route->ksnr_refcount) == 0);
88 if (route->ksnr_peer != NULL)
89 ksocknal_peer_decref(route->ksnr_peer);
91 LIBCFS_FREE (route, sizeof (*route));
95 ksocknal_create_peer (ksock_peer_t **peerp, lnet_ni_t *ni, lnet_process_id_t id)
97 ksock_net_t *net = ni->ni_data;
100 LASSERT (id.nid != LNET_NID_ANY);
101 LASSERT (id.pid != LNET_PID_ANY);
102 LASSERT (!in_interrupt());
104 LIBCFS_ALLOC (peer, sizeof (*peer));
108 memset (peer, 0, sizeof (*peer)); /* NULL pointers/clear flags etc */
112 atomic_set (&peer->ksnp_refcount, 1); /* 1 ref for caller */
113 peer->ksnp_closing = 0;
114 peer->ksnp_accepting = 0;
115 peer->ksnp_zc_next_cookie = 1;
116 peer->ksnp_proto = NULL;
117 CFS_INIT_LIST_HEAD (&peer->ksnp_conns);
118 CFS_INIT_LIST_HEAD (&peer->ksnp_routes);
119 CFS_INIT_LIST_HEAD (&peer->ksnp_tx_queue);
120 CFS_INIT_LIST_HEAD (&peer->ksnp_zc_req_list);
121 spin_lock_init(&peer->ksnp_lock);
123 spin_lock_bh (&net->ksnn_lock);
125 if (net->ksnn_shutdown) {
126 spin_unlock_bh (&net->ksnn_lock);
128 LIBCFS_FREE(peer, sizeof(*peer));
129 CERROR("Can't create peer: network shutdown\n");
135 spin_unlock_bh (&net->ksnn_lock);
142 ksocknal_destroy_peer (ksock_peer_t *peer)
144 ksock_net_t *net = peer->ksnp_ni->ni_data;
146 CDEBUG (D_NET, "peer %s %p deleted\n",
147 libcfs_id2str(peer->ksnp_id), peer);
149 LASSERT (atomic_read (&peer->ksnp_refcount) == 0);
150 LASSERT (peer->ksnp_accepting == 0);
151 LASSERT (list_empty (&peer->ksnp_conns));
152 LASSERT (list_empty (&peer->ksnp_routes));
153 LASSERT (list_empty (&peer->ksnp_tx_queue));
154 LASSERT (list_empty (&peer->ksnp_zc_req_list));
156 LIBCFS_FREE (peer, sizeof (*peer));
158 /* NB a peer's connections and routes keep a reference on their peer
159 * until they are destroyed, so we can be assured that _all_ state to
160 * do with this peer has been cleaned up when its refcount drops to
162 spin_lock_bh (&net->ksnn_lock);
164 spin_unlock_bh (&net->ksnn_lock);
168 ksocknal_find_peer_locked (lnet_ni_t *ni, lnet_process_id_t id)
170 struct list_head *peer_list = ksocknal_nid2peerlist(id.nid);
171 struct list_head *tmp;
174 list_for_each (tmp, peer_list) {
176 peer = list_entry (tmp, ksock_peer_t, ksnp_list);
178 LASSERT (!peer->ksnp_closing);
180 if (peer->ksnp_ni != ni)
183 if (peer->ksnp_id.nid != id.nid ||
184 peer->ksnp_id.pid != id.pid)
187 CDEBUG(D_NET, "got peer [%p] -> %s (%d)\n",
188 peer, libcfs_id2str(id),
189 atomic_read(&peer->ksnp_refcount));
196 ksocknal_find_peer (lnet_ni_t *ni, lnet_process_id_t id)
200 read_lock (&ksocknal_data.ksnd_global_lock);
201 peer = ksocknal_find_peer_locked (ni, id);
202 if (peer != NULL) /* +1 ref for caller? */
203 ksocknal_peer_addref(peer);
204 read_unlock (&ksocknal_data.ksnd_global_lock);
210 ksocknal_unlink_peer_locked (ksock_peer_t *peer)
214 ksock_interface_t *iface;
216 for (i = 0; i < peer->ksnp_n_passive_ips; i++) {
217 LASSERT (i < LNET_MAX_INTERFACES);
218 ip = peer->ksnp_passive_ips[i];
220 iface = ksocknal_ip2iface(peer->ksnp_ni, ip);
221 /* All IPs in peer->ksnp_passive_ips[] come from the
222 * interface list, therefore the call must succeed. */
223 LASSERT (iface != NULL);
225 CDEBUG(D_NET, "peer=%p iface=%p ksni_nroutes=%d\n",
226 peer, iface, iface->ksni_nroutes);
227 iface->ksni_npeers--;
230 LASSERT (list_empty(&peer->ksnp_conns));
231 LASSERT (list_empty(&peer->ksnp_routes));
232 LASSERT (!peer->ksnp_closing);
233 peer->ksnp_closing = 1;
234 list_del (&peer->ksnp_list);
235 /* lose peerlist's ref */
236 ksocknal_peer_decref(peer);
240 ksocknal_get_peer_info (lnet_ni_t *ni, int index,
241 lnet_process_id_t *id, __u32 *myip, __u32 *peer_ip, int *port,
242 int *conn_count, int *share_count)
245 struct list_head *ptmp;
246 ksock_route_t *route;
247 struct list_head *rtmp;
252 read_lock (&ksocknal_data.ksnd_global_lock);
254 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
256 list_for_each (ptmp, &ksocknal_data.ksnd_peers[i]) {
257 peer = list_entry (ptmp, ksock_peer_t, ksnp_list);
259 if (peer->ksnp_ni != ni)
262 if (peer->ksnp_n_passive_ips == 0 &&
263 list_empty(&peer->ksnp_routes)) {
277 for (j = 0; j < peer->ksnp_n_passive_ips; j++) {
282 *myip = peer->ksnp_passive_ips[j];
291 list_for_each (rtmp, &peer->ksnp_routes) {
295 route = list_entry(rtmp, ksock_route_t,
299 *myip = route->ksnr_myipaddr;
300 *peer_ip = route->ksnr_ipaddr;
301 *port = route->ksnr_port;
302 *conn_count = route->ksnr_conn_count;
303 *share_count = route->ksnr_share_count;
310 read_unlock (&ksocknal_data.ksnd_global_lock);
315 ksocknal_associate_route_conn_locked(ksock_route_t *route, ksock_conn_t *conn)
317 ksock_peer_t *peer = route->ksnr_peer;
318 int type = conn->ksnc_type;
319 ksock_interface_t *iface;
321 conn->ksnc_route = route;
322 ksocknal_route_addref(route);
324 if (route->ksnr_myipaddr != conn->ksnc_myipaddr) {
325 if (route->ksnr_myipaddr == 0) {
326 /* route wasn't bound locally yet (the initial route) */
327 CDEBUG(D_NET, "Binding %s %u.%u.%u.%u to %u.%u.%u.%u\n",
328 libcfs_id2str(peer->ksnp_id),
329 HIPQUAD(route->ksnr_ipaddr),
330 HIPQUAD(conn->ksnc_myipaddr));
332 CDEBUG(D_NET, "Rebinding %s %u.%u.%u.%u from "
333 "%u.%u.%u.%u to %u.%u.%u.%u\n",
334 libcfs_id2str(peer->ksnp_id),
335 HIPQUAD(route->ksnr_ipaddr),
336 HIPQUAD(route->ksnr_myipaddr),
337 HIPQUAD(conn->ksnc_myipaddr));
339 iface = ksocknal_ip2iface(route->ksnr_peer->ksnp_ni,
340 route->ksnr_myipaddr);
342 iface->ksni_nroutes--;
344 route->ksnr_myipaddr = conn->ksnc_myipaddr;
345 iface = ksocknal_ip2iface(route->ksnr_peer->ksnp_ni,
346 route->ksnr_myipaddr);
348 iface->ksni_nroutes++;
351 route->ksnr_connected |= (1<<type);
352 route->ksnr_conn_count++;
354 /* Successful connection => further attempts can
355 * proceed immediately */
356 route->ksnr_retry_interval = 0;
360 ksocknal_add_route_locked (ksock_peer_t *peer, ksock_route_t *route)
362 struct list_head *tmp;
364 ksock_route_t *route2;
366 LASSERT (!peer->ksnp_closing);
367 LASSERT (route->ksnr_peer == NULL);
368 LASSERT (!route->ksnr_scheduled);
369 LASSERT (!route->ksnr_connecting);
370 LASSERT (route->ksnr_connected == 0);
372 /* LASSERT(unique) */
373 list_for_each(tmp, &peer->ksnp_routes) {
374 route2 = list_entry(tmp, ksock_route_t, ksnr_list);
376 if (route2->ksnr_ipaddr == route->ksnr_ipaddr) {
377 CERROR ("Duplicate route %s %u.%u.%u.%u\n",
378 libcfs_id2str(peer->ksnp_id),
379 HIPQUAD(route->ksnr_ipaddr));
384 route->ksnr_peer = peer;
385 ksocknal_peer_addref(peer);
386 /* peer's routelist takes over my ref on 'route' */
387 list_add_tail(&route->ksnr_list, &peer->ksnp_routes);
389 list_for_each(tmp, &peer->ksnp_conns) {
390 conn = list_entry(tmp, ksock_conn_t, ksnc_list);
392 if (conn->ksnc_ipaddr != route->ksnr_ipaddr)
395 ksocknal_associate_route_conn_locked(route, conn);
396 /* keep going (typed routes) */
401 ksocknal_del_route_locked (ksock_route_t *route)
403 ksock_peer_t *peer = route->ksnr_peer;
404 ksock_interface_t *iface;
406 struct list_head *ctmp;
407 struct list_head *cnxt;
409 LASSERT (!route->ksnr_deleted);
411 /* Close associated conns */
412 list_for_each_safe (ctmp, cnxt, &peer->ksnp_conns) {
413 conn = list_entry(ctmp, ksock_conn_t, ksnc_list);
415 if (conn->ksnc_route != route)
418 ksocknal_close_conn_locked (conn, 0);
421 if (route->ksnr_myipaddr != 0) {
422 iface = ksocknal_ip2iface(route->ksnr_peer->ksnp_ni,
423 route->ksnr_myipaddr);
425 iface->ksni_nroutes--;
428 route->ksnr_deleted = 1;
429 list_del (&route->ksnr_list);
430 ksocknal_route_decref(route); /* drop peer's ref */
432 if (list_empty (&peer->ksnp_routes) &&
433 list_empty (&peer->ksnp_conns)) {
434 /* I've just removed the last route to a peer with no active
436 ksocknal_unlink_peer_locked (peer);
441 ksocknal_add_peer (lnet_ni_t *ni, lnet_process_id_t id, __u32 ipaddr, int port)
443 struct list_head *tmp;
446 ksock_route_t *route;
447 ksock_route_t *route2;
450 if (id.nid == LNET_NID_ANY ||
451 id.pid == LNET_PID_ANY)
454 /* Have a brand new peer ready... */
455 rc = ksocknal_create_peer(&peer, ni, id);
459 route = ksocknal_create_route (ipaddr, port);
461 ksocknal_peer_decref(peer);
465 write_lock_bh (&ksocknal_data.ksnd_global_lock);
467 /* always called with a ref on ni, so shutdown can't have started */
468 LASSERT (((ksock_net_t *) ni->ni_data)->ksnn_shutdown == 0);
470 peer2 = ksocknal_find_peer_locked (ni, id);
472 ksocknal_peer_decref(peer);
475 /* peer table takes my ref on peer */
476 list_add_tail (&peer->ksnp_list,
477 ksocknal_nid2peerlist (id.nid));
481 list_for_each (tmp, &peer->ksnp_routes) {
482 route2 = list_entry(tmp, ksock_route_t, ksnr_list);
484 if (route2->ksnr_ipaddr == ipaddr)
489 if (route2 == NULL) {
490 ksocknal_add_route_locked(peer, route);
491 route->ksnr_share_count++;
493 ksocknal_route_decref(route);
494 route2->ksnr_share_count++;
497 write_unlock_bh (&ksocknal_data.ksnd_global_lock);
503 ksocknal_del_peer_locked (ksock_peer_t *peer, __u32 ip)
506 ksock_route_t *route;
507 struct list_head *tmp;
508 struct list_head *nxt;
511 LASSERT (!peer->ksnp_closing);
513 /* Extra ref prevents peer disappearing until I'm done with it */
514 ksocknal_peer_addref(peer);
516 list_for_each_safe (tmp, nxt, &peer->ksnp_routes) {
517 route = list_entry(tmp, ksock_route_t, ksnr_list);
520 if (!(ip == 0 || route->ksnr_ipaddr == ip))
523 route->ksnr_share_count = 0;
524 /* This deletes associated conns too */
525 ksocknal_del_route_locked (route);
529 list_for_each_safe (tmp, nxt, &peer->ksnp_routes) {
530 route = list_entry(tmp, ksock_route_t, ksnr_list);
531 nshared += route->ksnr_share_count;
535 /* remove everything else if there are no explicit entries
538 list_for_each_safe (tmp, nxt, &peer->ksnp_routes) {
539 route = list_entry(tmp, ksock_route_t, ksnr_list);
541 /* we should only be removing auto-entries */
542 LASSERT(route->ksnr_share_count == 0);
543 ksocknal_del_route_locked (route);
546 list_for_each_safe (tmp, nxt, &peer->ksnp_conns) {
547 conn = list_entry(tmp, ksock_conn_t, ksnc_list);
549 ksocknal_close_conn_locked(conn, 0);
553 ksocknal_peer_decref(peer);
554 /* NB peer unlinks itself when last conn/route is removed */
558 ksocknal_del_peer (lnet_ni_t *ni, lnet_process_id_t id, __u32 ip)
560 CFS_LIST_HEAD (zombies);
561 struct list_head *ptmp;
562 struct list_head *pnxt;
569 write_lock_bh (&ksocknal_data.ksnd_global_lock);
571 if (id.nid != LNET_NID_ANY)
572 lo = hi = ksocknal_nid2peerlist(id.nid) - ksocknal_data.ksnd_peers;
575 hi = ksocknal_data.ksnd_peer_hash_size - 1;
578 for (i = lo; i <= hi; i++) {
579 list_for_each_safe (ptmp, pnxt, &ksocknal_data.ksnd_peers[i]) {
580 peer = list_entry (ptmp, ksock_peer_t, ksnp_list);
582 if (peer->ksnp_ni != ni)
585 if (!((id.nid == LNET_NID_ANY || peer->ksnp_id.nid == id.nid) &&
586 (id.pid == LNET_PID_ANY || peer->ksnp_id.pid == id.pid)))
589 ksocknal_peer_addref(peer); /* a ref for me... */
591 ksocknal_del_peer_locked (peer, ip);
593 if (peer->ksnp_closing && !list_empty(&peer->ksnp_tx_queue)) {
594 LASSERT (list_empty(&peer->ksnp_conns));
595 LASSERT (list_empty(&peer->ksnp_routes));
597 list_splice_init(&peer->ksnp_tx_queue, &zombies);
600 ksocknal_peer_decref(peer); /* ...till here */
602 rc = 0; /* matched! */
606 write_unlock_bh (&ksocknal_data.ksnd_global_lock);
608 ksocknal_txlist_done(ni, &zombies, 1);
614 ksocknal_get_conn_by_idx (lnet_ni_t *ni, int index)
617 struct list_head *ptmp;
619 struct list_head *ctmp;
622 read_lock (&ksocknal_data.ksnd_global_lock);
624 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
625 list_for_each (ptmp, &ksocknal_data.ksnd_peers[i]) {
626 peer = list_entry (ptmp, ksock_peer_t, ksnp_list);
628 LASSERT (!peer->ksnp_closing);
630 if (peer->ksnp_ni != ni)
633 list_for_each (ctmp, &peer->ksnp_conns) {
637 conn = list_entry (ctmp, ksock_conn_t, ksnc_list);
638 ksocknal_conn_addref(conn);
639 read_unlock (&ksocknal_data.ksnd_global_lock);
645 read_unlock (&ksocknal_data.ksnd_global_lock);
650 ksocknal_choose_scheduler_locked (unsigned int irq)
652 ksock_sched_t *sched;
653 ksock_irqinfo_t *info;
656 LASSERT (irq < NR_IRQS);
657 info = &ksocknal_data.ksnd_irqinfo[irq];
659 if (irq != 0 && /* hardware NIC */
660 info->ksni_valid) { /* already set up */
661 return (&ksocknal_data.ksnd_schedulers[info->ksni_sched]);
664 /* software NIC (irq == 0) || not associated with a scheduler yet.
665 * Choose the CPU with the fewest connections... */
666 sched = &ksocknal_data.ksnd_schedulers[0];
667 for (i = 1; i < ksocknal_data.ksnd_nschedulers; i++)
668 if (sched->kss_nconns >
669 ksocknal_data.ksnd_schedulers[i].kss_nconns)
670 sched = &ksocknal_data.ksnd_schedulers[i];
672 if (irq != 0) { /* Hardware NIC */
673 info->ksni_valid = 1;
674 info->ksni_sched = sched - ksocknal_data.ksnd_schedulers;
677 LASSERT (info->ksni_sched == sched - ksocknal_data.ksnd_schedulers);
684 ksocknal_local_ipvec (lnet_ni_t *ni, __u32 *ipaddrs)
686 ksock_net_t *net = ni->ni_data;
690 read_lock (&ksocknal_data.ksnd_global_lock);
692 nip = net->ksnn_ninterfaces;
693 LASSERT (nip <= LNET_MAX_INTERFACES);
695 /* Only offer interfaces for additional connections if I have
698 read_unlock (&ksocknal_data.ksnd_global_lock);
702 for (i = 0; i < nip; i++) {
703 ipaddrs[i] = net->ksnn_interfaces[i].ksni_ipaddr;
704 LASSERT (ipaddrs[i] != 0);
707 read_unlock (&ksocknal_data.ksnd_global_lock);
712 ksocknal_match_peerip (ksock_interface_t *iface, __u32 *ips, int nips)
714 int best_netmatch = 0;
721 for (i = 0; i < nips; i++) {
725 this_xor = (ips[i] ^ iface->ksni_ipaddr);
726 this_netmatch = ((this_xor & iface->ksni_netmask) == 0) ? 1 : 0;
729 best_netmatch < this_netmatch ||
730 (best_netmatch == this_netmatch &&
731 best_xor > this_xor)))
735 best_netmatch = this_netmatch;
744 ksocknal_select_ips(ksock_peer_t *peer, __u32 *peerips, int n_peerips)
746 rwlock_t *global_lock = &ksocknal_data.ksnd_global_lock;
747 ksock_net_t *net = peer->ksnp_ni->ni_data;
748 ksock_interface_t *iface;
749 ksock_interface_t *best_iface;
760 /* CAVEAT EMPTOR: We do all our interface matching with an
761 * exclusive hold of global lock at IRQ priority. We're only
762 * expecting to be dealing with small numbers of interfaces, so the
763 * O(n**3)-ness shouldn't matter */
765 /* Also note that I'm not going to return more than n_peerips
766 * interfaces, even if I have more myself */
768 write_lock_bh (global_lock);
770 LASSERT (n_peerips <= LNET_MAX_INTERFACES);
771 LASSERT (net->ksnn_ninterfaces <= LNET_MAX_INTERFACES);
773 /* Only match interfaces for additional connections
774 * if I have > 1 interface */
775 n_ips = (net->ksnn_ninterfaces < 2) ? 0 :
776 MIN(n_peerips, net->ksnn_ninterfaces);
778 for (i = 0; peer->ksnp_n_passive_ips < n_ips; i++) {
779 /* ^ yes really... */
781 /* If we have any new interfaces, first tick off all the
782 * peer IPs that match old interfaces, then choose new
783 * interfaces to match the remaining peer IPS.
784 * We don't forget interfaces we've stopped using; we might
785 * start using them again... */
787 if (i < peer->ksnp_n_passive_ips) {
789 ip = peer->ksnp_passive_ips[i];
790 best_iface = ksocknal_ip2iface(peer->ksnp_ni, ip);
792 /* peer passive ips are kept up to date */
793 LASSERT(best_iface != NULL);
795 /* choose a new interface */
796 LASSERT (i == peer->ksnp_n_passive_ips);
802 for (j = 0; j < net->ksnn_ninterfaces; j++) {
803 iface = &net->ksnn_interfaces[j];
804 ip = iface->ksni_ipaddr;
806 for (k = 0; k < peer->ksnp_n_passive_ips; k++)
807 if (peer->ksnp_passive_ips[k] == ip)
810 if (k < peer->ksnp_n_passive_ips) /* using it already */
813 k = ksocknal_match_peerip(iface, peerips, n_peerips);
814 xor = (ip ^ peerips[k]);
815 this_netmatch = ((xor & iface->ksni_netmask) == 0) ? 1 : 0;
817 if (!(best_iface == NULL ||
818 best_netmatch < this_netmatch ||
819 (best_netmatch == this_netmatch &&
820 best_npeers > iface->ksni_npeers)))
824 best_netmatch = this_netmatch;
825 best_npeers = iface->ksni_npeers;
828 best_iface->ksni_npeers++;
829 ip = best_iface->ksni_ipaddr;
830 peer->ksnp_passive_ips[i] = ip;
831 peer->ksnp_n_passive_ips = i+1;
834 LASSERT (best_iface != NULL);
836 /* mark the best matching peer IP used */
837 j = ksocknal_match_peerip(best_iface, peerips, n_peerips);
841 /* Overwrite input peer IP addresses */
842 memcpy(peerips, peer->ksnp_passive_ips, n_ips * sizeof(*peerips));
844 write_unlock_bh (global_lock);
850 ksocknal_create_routes(ksock_peer_t *peer, int port,
851 __u32 *peer_ipaddrs, int npeer_ipaddrs)
853 ksock_route_t *newroute = NULL;
854 rwlock_t *global_lock = &ksocknal_data.ksnd_global_lock;
855 lnet_ni_t *ni = peer->ksnp_ni;
856 ksock_net_t *net = ni->ni_data;
857 struct list_head *rtmp;
858 ksock_route_t *route;
859 ksock_interface_t *iface;
860 ksock_interface_t *best_iface;
867 /* CAVEAT EMPTOR: We do all our interface matching with an
868 * exclusive hold of global lock at IRQ priority. We're only
869 * expecting to be dealing with small numbers of interfaces, so the
870 * O(n**3)-ness here shouldn't matter */
872 write_lock_bh (global_lock);
874 if (net->ksnn_ninterfaces < 2) {
875 /* Only create additional connections
876 * if I have > 1 interface */
877 write_unlock_bh (global_lock);
881 LASSERT (npeer_ipaddrs <= LNET_MAX_INTERFACES);
883 for (i = 0; i < npeer_ipaddrs; i++) {
884 if (newroute != NULL) {
885 newroute->ksnr_ipaddr = peer_ipaddrs[i];
887 write_unlock_bh (global_lock);
889 newroute = ksocknal_create_route(peer_ipaddrs[i], port);
890 if (newroute == NULL)
893 write_lock_bh (global_lock);
896 if (peer->ksnp_closing) {
897 /* peer got closed under me */
901 /* Already got a route? */
903 list_for_each(rtmp, &peer->ksnp_routes) {
904 route = list_entry(rtmp, ksock_route_t, ksnr_list);
906 if (route->ksnr_ipaddr == newroute->ksnr_ipaddr)
918 LASSERT (net->ksnn_ninterfaces <= LNET_MAX_INTERFACES);
920 /* Select interface to connect from */
921 for (j = 0; j < net->ksnn_ninterfaces; j++) {
922 iface = &net->ksnn_interfaces[j];
924 /* Using this interface already? */
925 list_for_each(rtmp, &peer->ksnp_routes) {
926 route = list_entry(rtmp, ksock_route_t, ksnr_list);
928 if (route->ksnr_myipaddr == iface->ksni_ipaddr)
936 this_netmatch = (((iface->ksni_ipaddr ^
937 newroute->ksnr_ipaddr) &
938 iface->ksni_netmask) == 0) ? 1 : 0;
940 if (!(best_iface == NULL ||
941 best_netmatch < this_netmatch ||
942 (best_netmatch == this_netmatch &&
943 best_nroutes > iface->ksni_nroutes)))
947 best_netmatch = this_netmatch;
948 best_nroutes = iface->ksni_nroutes;
951 if (best_iface == NULL)
954 newroute->ksnr_myipaddr = best_iface->ksni_ipaddr;
955 best_iface->ksni_nroutes++;
957 ksocknal_add_route_locked(peer, newroute);
961 write_unlock_bh (global_lock);
962 if (newroute != NULL)
963 ksocknal_route_decref(newroute);
967 ksocknal_accept (lnet_ni_t *ni, cfs_socket_t *sock)
974 rc = libcfs_sock_getaddr(sock, 1, &peer_ip, &peer_port);
975 LASSERT (rc == 0); /* we succeeded before */
977 LIBCFS_ALLOC(cr, sizeof(*cr));
979 LCONSOLE_ERROR_MSG(0x12f, "Dropping connection request from "
980 "%u.%u.%u.%u: memory exhausted\n",
987 cr->ksncr_sock = sock;
989 spin_lock_bh (&ksocknal_data.ksnd_connd_lock);
991 list_add_tail(&cr->ksncr_list, &ksocknal_data.ksnd_connd_connreqs);
992 cfs_waitq_signal(&ksocknal_data.ksnd_connd_waitq);
994 spin_unlock_bh (&ksocknal_data.ksnd_connd_lock);
999 ksocknal_connecting (ksock_peer_t *peer, __u32 ipaddr)
1001 ksock_route_t *route;
1003 list_for_each_entry (route, &peer->ksnp_routes, ksnr_list) {
1005 if (route->ksnr_ipaddr == ipaddr)
1006 return route->ksnr_connecting;
1012 ksocknal_create_conn (lnet_ni_t *ni, ksock_route_t *route,
1013 cfs_socket_t *sock, int type)
1015 rwlock_t *global_lock = &ksocknal_data.ksnd_global_lock;
1016 CFS_LIST_HEAD (zombies);
1017 lnet_process_id_t peerid;
1018 struct list_head *tmp;
1021 ksock_conn_t *conn2;
1022 ksock_peer_t *peer = NULL;
1023 ksock_peer_t *peer2;
1024 ksock_sched_t *sched;
1025 ksock_hello_msg_t *hello;
1032 active = (route != NULL);
1034 LASSERT (active == (type != SOCKLND_CONN_NONE));
1036 irq = ksocknal_lib_sock_irq (sock);
1038 LIBCFS_ALLOC(conn, sizeof(*conn));
1044 memset (conn, 0, sizeof (*conn));
1045 conn->ksnc_peer = NULL;
1046 conn->ksnc_route = NULL;
1047 conn->ksnc_sock = sock;
1048 atomic_set (&conn->ksnc_sock_refcount, 1); /* 1 ref for conn */
1049 conn->ksnc_type = type;
1050 ksocknal_lib_save_callback(sock, conn);
1051 atomic_set (&conn->ksnc_conn_refcount, 1); /* 1 ref for me */
1053 conn->ksnc_zc_capable = ksocknal_lib_zc_capable(sock);
1054 conn->ksnc_rx_ready = 0;
1055 conn->ksnc_rx_scheduled = 0;
1057 CFS_INIT_LIST_HEAD (&conn->ksnc_tx_queue);
1058 conn->ksnc_tx_ready = 0;
1059 conn->ksnc_tx_scheduled = 0;
1060 conn->ksnc_tx_mono = NULL;
1061 atomic_set (&conn->ksnc_tx_nob, 0);
1063 LIBCFS_ALLOC(hello, offsetof(ksock_hello_msg_t,
1064 kshm_ips[LNET_MAX_INTERFACES]));
1065 if (hello == NULL) {
1070 /* stash conn's local and remote addrs */
1071 rc = ksocknal_lib_get_conn_addrs (conn);
1075 /* Find out/confirm peer's NID and connection type and get the
1076 * vector of interfaces she's willing to let me connect to.
1077 * Passive connections use the listener timeout since the peer sends
1081 peer = route->ksnr_peer;
1082 LASSERT(ni == peer->ksnp_ni);
1084 /* Active connection sends HELLO eagerly */
1085 hello->kshm_nips = ksocknal_local_ipvec(ni, hello->kshm_ips);
1086 peerid = peer->ksnp_id;
1088 write_lock_bh(global_lock);
1089 conn->ksnc_proto = peer->ksnp_proto;
1090 write_unlock_bh(global_lock);
1092 if (conn->ksnc_proto == NULL) {
1093 conn->ksnc_proto = &ksocknal_protocol_v2x;
1094 #if SOCKNAL_VERSION_DEBUG
1095 if (*ksocknal_tunables.ksnd_protocol != 2)
1096 conn->ksnc_proto = &ksocknal_protocol_v1x;
1100 rc = ksocknal_send_hello (ni, conn, peerid.nid, hello);
1104 peerid.nid = LNET_NID_ANY;
1105 peerid.pid = LNET_PID_ANY;
1107 /* Passive, get protocol from peer */
1108 conn->ksnc_proto = NULL;
1111 rc = ksocknal_recv_hello (ni, conn, hello, &peerid, &incarnation);
1115 LASSERT (rc == 0 || active);
1116 LASSERT (conn->ksnc_proto != NULL);
1117 LASSERT (peerid.nid != LNET_NID_ANY);
1120 ksocknal_peer_addref(peer);
1121 write_lock_bh (global_lock);
1123 rc = ksocknal_create_peer(&peer, ni, peerid);
1127 write_lock_bh (global_lock);
1129 /* called with a ref on ni, so shutdown can't have started */
1130 LASSERT (((ksock_net_t *) ni->ni_data)->ksnn_shutdown == 0);
1132 peer2 = ksocknal_find_peer_locked(ni, peerid);
1133 if (peer2 == NULL) {
1134 /* NB this puts an "empty" peer in the peer
1135 * table (which takes my ref) */
1136 list_add_tail(&peer->ksnp_list,
1137 ksocknal_nid2peerlist(peerid.nid));
1139 ksocknal_peer_decref(peer);
1144 ksocknal_peer_addref(peer);
1145 peer->ksnp_accepting++;
1147 /* Am I already connecting to this guy? Resolve in
1148 * favour of higher NID... */
1149 if (peerid.nid < ni->ni_nid &&
1150 ksocknal_connecting(peer, conn->ksnc_ipaddr)) {
1152 warn = "connection race resolution";
1157 if (peer->ksnp_closing ||
1158 (active && route->ksnr_deleted)) {
1159 /* peer/route got closed under me */
1161 warn = "peer/route removed";
1165 if (peer->ksnp_proto == NULL) {
1166 /* Never connected before.
1167 * NB recv_hello may have returned EPROTO to signal my peer
1168 * wants a different protocol than the one I asked for.
1170 LASSERT (list_empty(&peer->ksnp_conns));
1172 peer->ksnp_proto = conn->ksnc_proto;
1173 peer->ksnp_incarnation = incarnation;
1176 if (peer->ksnp_proto != conn->ksnc_proto ||
1177 peer->ksnp_incarnation != incarnation) {
1178 /* Peer rebooted or I've got the wrong protocol version */
1179 ksocknal_close_peer_conns_locked(peer, 0, 0);
1181 peer->ksnp_proto = NULL;
1183 warn = peer->ksnp_incarnation != incarnation ?
1185 "wrong proto version";
1195 warn = "lost conn race";
1198 warn = "retry with different protocol version";
1202 /* Refuse to duplicate an existing connection, unless this is a
1203 * loopback connection */
1204 if (conn->ksnc_ipaddr != conn->ksnc_myipaddr) {
1205 list_for_each(tmp, &peer->ksnp_conns) {
1206 conn2 = list_entry(tmp, ksock_conn_t, ksnc_list);
1208 if (conn2->ksnc_ipaddr != conn->ksnc_ipaddr ||
1209 conn2->ksnc_myipaddr != conn->ksnc_myipaddr ||
1210 conn2->ksnc_type != conn->ksnc_type)
1213 /* Reply on a passive connection attempt so the peer
1214 * realises we're connected. */
1224 /* If the connection created by this route didn't bind to the IP
1225 * address the route connected to, the connection/route matching
1226 * code below probably isn't going to work. */
1228 route->ksnr_ipaddr != conn->ksnc_ipaddr) {
1229 CERROR("Route %s %u.%u.%u.%u connected to %u.%u.%u.%u\n",
1230 libcfs_id2str(peer->ksnp_id),
1231 HIPQUAD(route->ksnr_ipaddr),
1232 HIPQUAD(conn->ksnc_ipaddr));
1235 /* Search for a route corresponding to the new connection and
1236 * create an association. This allows incoming connections created
1237 * by routes in my peer to match my own route entries so I don't
1238 * continually create duplicate routes. */
1239 list_for_each (tmp, &peer->ksnp_routes) {
1240 route = list_entry(tmp, ksock_route_t, ksnr_list);
1242 if (route->ksnr_ipaddr != conn->ksnc_ipaddr)
1245 ksocknal_associate_route_conn_locked(route, conn);
1249 conn->ksnc_peer = peer; /* conn takes my ref on peer */
1250 peer->ksnp_last_alive = cfs_time_current();
1251 peer->ksnp_error = 0;
1253 sched = ksocknal_choose_scheduler_locked (irq);
1254 sched->kss_nconns++;
1255 conn->ksnc_scheduler = sched;
1257 /* Set the deadline for the outgoing HELLO to drain */
1258 conn->ksnc_tx_bufnob = SOCK_WMEM_QUEUED(sock);
1259 conn->ksnc_tx_deadline = cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
1260 mb(); /* order with adding to peer's conn list */
1262 list_add (&conn->ksnc_list, &peer->ksnp_conns);
1263 ksocknal_conn_addref(conn);
1265 ksocknal_new_packet(conn, 0);
1267 /* Take all the packets blocking for a connection.
1268 * NB, it might be nicer to share these blocked packets among any
1269 * other connections that are becoming established. */
1270 while (!list_empty (&peer->ksnp_tx_queue)) {
1271 tx = list_entry (peer->ksnp_tx_queue.next,
1272 ksock_tx_t, tx_list);
1274 list_del (&tx->tx_list);
1275 ksocknal_queue_tx_locked (tx, conn);
1278 write_unlock_bh (global_lock);
1280 /* We've now got a new connection. Any errors from here on are just
1281 * like "normal" comms errors and we close the connection normally.
1282 * NB (a) we still have to send the reply HELLO for passive
1284 * (b) normal I/O on the conn is blocked until I setup and call the
1288 ksocknal_lib_bind_irq (irq);
1290 CDEBUG(D_NET, "New conn %s p %d.x %u.%u.%u.%u -> %u.%u.%u.%u/%d"
1291 " incarnation:"LPD64" sched[%d]/%d\n",
1292 libcfs_id2str(peerid), conn->ksnc_proto->pro_version,
1293 HIPQUAD(conn->ksnc_myipaddr), HIPQUAD(conn->ksnc_ipaddr),
1294 conn->ksnc_port, incarnation,
1295 (int)(conn->ksnc_scheduler - ksocknal_data.ksnd_schedulers), irq);
1298 /* additional routes after interface exchange? */
1299 ksocknal_create_routes(peer, conn->ksnc_port,
1300 hello->kshm_ips, hello->kshm_nips);
1302 hello->kshm_nips = ksocknal_select_ips(peer, hello->kshm_ips,
1304 rc = ksocknal_send_hello(ni, conn, peerid.nid, hello);
1307 LIBCFS_FREE(hello, offsetof(ksock_hello_msg_t,
1308 kshm_ips[LNET_MAX_INTERFACES]));
1310 /* setup the socket AFTER I've received hello (it disables
1311 * SO_LINGER). I might call back to the acceptor who may want
1312 * to send a protocol version response and then close the
1313 * socket; this ensures the socket only tears down after the
1314 * response has been sent. */
1316 rc = ksocknal_lib_setup_sock(sock);
1318 write_lock_bh(global_lock);
1320 /* NB my callbacks block while I hold ksnd_global_lock */
1321 ksocknal_lib_set_callback(sock, conn);
1324 peer->ksnp_accepting--;
1326 write_unlock_bh(global_lock);
1329 write_lock_bh(global_lock);
1330 ksocknal_close_conn_locked(conn, rc);
1331 write_unlock_bh(global_lock);
1332 } else if (ksocknal_connsock_addref(conn) == 0) {
1333 /* Allow I/O to proceed. */
1334 ksocknal_read_callback(conn);
1335 ksocknal_write_callback(conn);
1336 ksocknal_connsock_decref(conn);
1339 ksocknal_conn_decref(conn);
1343 if (!peer->ksnp_closing &&
1344 list_empty (&peer->ksnp_conns) &&
1345 list_empty (&peer->ksnp_routes)) {
1346 list_add(&zombies, &peer->ksnp_tx_queue);
1347 list_del_init(&peer->ksnp_tx_queue);
1348 ksocknal_unlink_peer_locked(peer);
1351 write_unlock_bh (global_lock);
1355 CERROR("Not creating conn %s type %d: %s\n",
1356 libcfs_id2str(peerid), conn->ksnc_type, warn);
1358 CDEBUG(D_NET, "Not creating conn %s type %d: %s\n",
1359 libcfs_id2str(peerid), conn->ksnc_type, warn);
1364 /* Request retry by replying with CONN_NONE
1365 * ksnc_proto has been set already */
1366 conn->ksnc_type = SOCKLND_CONN_NONE;
1367 hello->kshm_nips = 0;
1368 ksocknal_send_hello(ni, conn, peerid.nid, hello);
1371 write_lock_bh(global_lock);
1372 peer->ksnp_accepting--;
1373 write_unlock_bh(global_lock);
1376 ksocknal_txlist_done(ni, &zombies, 1);
1377 ksocknal_peer_decref(peer);
1381 LIBCFS_FREE(hello, offsetof(ksock_hello_msg_t,
1382 kshm_ips[LNET_MAX_INTERFACES]));
1384 LIBCFS_FREE (conn, sizeof(*conn));
1387 libcfs_sock_release(sock);
1392 ksocknal_close_conn_locked (ksock_conn_t *conn, int error)
1394 /* This just does the immmediate housekeeping, and queues the
1395 * connection for the reaper to terminate.
1396 * Caller holds ksnd_global_lock exclusively in irq context */
1397 ksock_peer_t *peer = conn->ksnc_peer;
1398 ksock_route_t *route;
1399 ksock_conn_t *conn2;
1400 struct list_head *tmp;
1402 LASSERT (peer->ksnp_error == 0);
1403 LASSERT (!conn->ksnc_closing);
1404 conn->ksnc_closing = 1;
1406 /* ksnd_deathrow_conns takes over peer's ref */
1407 list_del (&conn->ksnc_list);
1409 route = conn->ksnc_route;
1410 if (route != NULL) {
1411 /* dissociate conn from route... */
1412 LASSERT (!route->ksnr_deleted);
1413 LASSERT ((route->ksnr_connected & (1 << conn->ksnc_type)) != 0);
1416 list_for_each(tmp, &peer->ksnp_conns) {
1417 conn2 = list_entry(tmp, ksock_conn_t, ksnc_list);
1419 if (conn2->ksnc_route == route &&
1420 conn2->ksnc_type == conn->ksnc_type)
1426 route->ksnr_connected &= ~(1 << conn->ksnc_type);
1428 conn->ksnc_route = NULL;
1430 #if 0 /* irrelevent with only eager routes */
1431 list_del (&route->ksnr_list); /* make route least favourite */
1432 list_add_tail (&route->ksnr_list, &peer->ksnp_routes);
1434 ksocknal_route_decref(route); /* drop conn's ref on route */
1437 if (list_empty (&peer->ksnp_conns)) {
1438 /* No more connections to this peer */
1440 peer->ksnp_proto = NULL; /* renegotiate protocol version */
1441 peer->ksnp_error = error; /* stash last conn close reason */
1443 if (list_empty (&peer->ksnp_routes)) {
1444 /* I've just closed last conn belonging to a
1445 * peer with no routes to it */
1446 ksocknal_unlink_peer_locked (peer);
1450 spin_lock_bh (&ksocknal_data.ksnd_reaper_lock);
1452 list_add_tail (&conn->ksnc_list, &ksocknal_data.ksnd_deathrow_conns);
1453 cfs_waitq_signal (&ksocknal_data.ksnd_reaper_waitq);
1455 spin_unlock_bh (&ksocknal_data.ksnd_reaper_lock);
1459 ksocknal_peer_failed (ksock_peer_t *peer)
1461 time_t last_alive = 0;
1464 /* There has been a connection failure or comms error; but I'll only
1465 * tell LNET I think the peer is dead if it's to another kernel and
1466 * there are no connections or connection attempts in existance. */
1468 read_lock (&ksocknal_data.ksnd_global_lock);
1470 if ((peer->ksnp_id.pid & LNET_PID_USERFLAG) == 0 &&
1471 list_empty(&peer->ksnp_conns) &&
1472 peer->ksnp_accepting == 0 &&
1473 ksocknal_find_connecting_route_locked(peer) == NULL) {
1475 last_alive = cfs_time_current_sec() -
1476 cfs_duration_sec(cfs_time_current() -
1477 peer->ksnp_last_alive);
1480 read_unlock (&ksocknal_data.ksnd_global_lock);
1483 lnet_notify (peer->ksnp_ni, peer->ksnp_id.nid, 0,
1488 ksocknal_terminate_conn (ksock_conn_t *conn)
1490 /* This gets called by the reaper (guaranteed thread context) to
1491 * disengage the socket from its callbacks and close it.
1492 * ksnc_refcount will eventually hit zero, and then the reaper will
1494 ksock_peer_t *peer = conn->ksnc_peer;
1495 ksock_sched_t *sched = conn->ksnc_scheduler;
1497 struct list_head *tmp;
1498 struct list_head *nxt;
1502 LASSERT(conn->ksnc_closing);
1504 /* wake up the scheduler to "send" all remaining packets to /dev/null */
1505 spin_lock_bh (&sched->kss_lock);
1507 if (!conn->ksnc_tx_scheduled &&
1508 !list_empty(&conn->ksnc_tx_queue)){
1509 list_add_tail (&conn->ksnc_tx_list,
1510 &sched->kss_tx_conns);
1511 /* a closing conn is always ready to tx */
1512 conn->ksnc_tx_ready = 1;
1513 conn->ksnc_tx_scheduled = 1;
1514 /* extra ref for scheduler */
1515 ksocknal_conn_addref(conn);
1517 cfs_waitq_signal (&sched->kss_waitq);
1520 spin_unlock_bh (&sched->kss_lock);
1522 spin_lock(&peer->ksnp_lock);
1524 list_for_each_safe(tmp, nxt, &peer->ksnp_zc_req_list) {
1525 tx = list_entry(tmp, ksock_tx_t, tx_zc_list);
1527 if (tx->tx_conn != conn)
1530 LASSERT (tx->tx_msg.ksm_zc_req_cookie != 0);
1532 tx->tx_msg.ksm_zc_req_cookie = 0;
1533 list_del(&tx->tx_zc_list);
1534 list_add(&tx->tx_zc_list, &zlist);
1537 spin_unlock(&peer->ksnp_lock);
1539 list_for_each_safe(tmp, nxt, &zlist) {
1540 tx = list_entry(tmp, ksock_tx_t, tx_zc_list);
1542 list_del(&tx->tx_zc_list);
1543 ksocknal_tx_decref(tx);
1546 /* serialise with callbacks */
1547 write_lock_bh (&ksocknal_data.ksnd_global_lock);
1549 ksocknal_lib_reset_callback(conn->ksnc_sock, conn);
1551 /* OK, so this conn may not be completely disengaged from its
1552 * scheduler yet, but it _has_ committed to terminate... */
1553 conn->ksnc_scheduler->kss_nconns--;
1555 if (peer->ksnp_error != 0) {
1556 /* peer's last conn closed in error */
1557 LASSERT (list_empty (&peer->ksnp_conns));
1559 peer->ksnp_error = 0; /* avoid multiple notifications */
1562 write_unlock_bh (&ksocknal_data.ksnd_global_lock);
1565 ksocknal_peer_failed(peer);
1567 /* The socket is closed on the final put; either here, or in
1568 * ksocknal_{send,recv}msg(). Since we set up the linger2 option
1569 * when the connection was established, this will close the socket
1570 * immediately, aborting anything buffered in it. Any hung
1571 * zero-copy transmits will therefore complete in finite time. */
1572 ksocknal_connsock_decref(conn);
1576 ksocknal_queue_zombie_conn (ksock_conn_t *conn)
1578 /* Queue the conn for the reaper to destroy */
1580 LASSERT (atomic_read(&conn->ksnc_conn_refcount) == 0);
1581 spin_lock_bh (&ksocknal_data.ksnd_reaper_lock);
1583 list_add_tail(&conn->ksnc_list, &ksocknal_data.ksnd_zombie_conns);
1584 cfs_waitq_signal(&ksocknal_data.ksnd_reaper_waitq);
1586 spin_unlock_bh (&ksocknal_data.ksnd_reaper_lock);
1590 ksocknal_destroy_conn (ksock_conn_t *conn)
1592 /* Final coup-de-grace of the reaper */
1593 CDEBUG (D_NET, "connection %p\n", conn);
1595 LASSERT (atomic_read (&conn->ksnc_conn_refcount) == 0);
1596 LASSERT (atomic_read (&conn->ksnc_sock_refcount) == 0);
1597 LASSERT (conn->ksnc_sock == NULL);
1598 LASSERT (conn->ksnc_route == NULL);
1599 LASSERT (!conn->ksnc_tx_scheduled);
1600 LASSERT (!conn->ksnc_rx_scheduled);
1601 LASSERT (list_empty(&conn->ksnc_tx_queue));
1603 /* complete current receive if any */
1604 switch (conn->ksnc_rx_state) {
1605 case SOCKNAL_RX_LNET_PAYLOAD:
1606 CERROR("Completing partial receive from %s"
1607 ", ip %d.%d.%d.%d:%d, with error\n",
1608 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1609 HIPQUAD(conn->ksnc_ipaddr), conn->ksnc_port);
1610 lnet_finalize (conn->ksnc_peer->ksnp_ni,
1611 conn->ksnc_cookie, -EIO);
1613 case SOCKNAL_RX_LNET_HEADER:
1614 if (conn->ksnc_rx_started)
1615 CERROR("Incomplete receive of lnet header from %s"
1616 ", ip %d.%d.%d.%d:%d, with error, protocol: %d.x.\n",
1617 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1618 HIPQUAD(conn->ksnc_ipaddr), conn->ksnc_port,
1619 conn->ksnc_proto->pro_version);
1621 case SOCKNAL_RX_KSM_HEADER:
1622 if (conn->ksnc_rx_started)
1623 CERROR("Incomplete receive of ksock message from %s"
1624 ", ip %d.%d.%d.%d:%d, with error, protocol: %d.x.\n",
1625 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1626 HIPQUAD(conn->ksnc_ipaddr), conn->ksnc_port,
1627 conn->ksnc_proto->pro_version);
1629 case SOCKNAL_RX_SLOP:
1630 if (conn->ksnc_rx_started)
1631 CERROR("Incomplete receive of slops from %s"
1632 ", ip %d.%d.%d.%d:%d, with error\n",
1633 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1634 HIPQUAD(conn->ksnc_ipaddr), conn->ksnc_port);
1641 ksocknal_peer_decref(conn->ksnc_peer);
1643 LIBCFS_FREE (conn, sizeof (*conn));
1647 ksocknal_close_peer_conns_locked (ksock_peer_t *peer, __u32 ipaddr, int why)
1650 struct list_head *ctmp;
1651 struct list_head *cnxt;
1654 list_for_each_safe (ctmp, cnxt, &peer->ksnp_conns) {
1655 conn = list_entry (ctmp, ksock_conn_t, ksnc_list);
1658 conn->ksnc_ipaddr == ipaddr) {
1660 ksocknal_close_conn_locked (conn, why);
1668 ksocknal_close_conn_and_siblings (ksock_conn_t *conn, int why)
1670 ksock_peer_t *peer = conn->ksnc_peer;
1671 __u32 ipaddr = conn->ksnc_ipaddr;
1674 write_lock_bh (&ksocknal_data.ksnd_global_lock);
1676 count = ksocknal_close_peer_conns_locked (peer, ipaddr, why);
1678 write_unlock_bh (&ksocknal_data.ksnd_global_lock);
1684 ksocknal_close_matching_conns (lnet_process_id_t id, __u32 ipaddr)
1687 struct list_head *ptmp;
1688 struct list_head *pnxt;
1694 write_lock_bh (&ksocknal_data.ksnd_global_lock);
1696 if (id.nid != LNET_NID_ANY)
1697 lo = hi = ksocknal_nid2peerlist(id.nid) - ksocknal_data.ksnd_peers;
1700 hi = ksocknal_data.ksnd_peer_hash_size - 1;
1703 for (i = lo; i <= hi; i++) {
1704 list_for_each_safe (ptmp, pnxt, &ksocknal_data.ksnd_peers[i]) {
1706 peer = list_entry (ptmp, ksock_peer_t, ksnp_list);
1708 if (!((id.nid == LNET_NID_ANY || id.nid == peer->ksnp_id.nid) &&
1709 (id.pid == LNET_PID_ANY || id.pid == peer->ksnp_id.pid)))
1712 count += ksocknal_close_peer_conns_locked (peer, ipaddr, 0);
1716 write_unlock_bh (&ksocknal_data.ksnd_global_lock);
1718 /* wildcards always succeed */
1719 if (id.nid == LNET_NID_ANY || id.pid == LNET_PID_ANY || ipaddr == 0)
1722 return (count == 0 ? -ENOENT : 0);
1726 ksocknal_notify (lnet_ni_t *ni, lnet_nid_t gw_nid, int alive)
1728 /* The router is telling me she's been notified of a change in
1729 * gateway state.... */
1730 lnet_process_id_t id = {.nid = gw_nid, .pid = LNET_PID_ANY};
1732 CDEBUG (D_NET, "gw %s %s\n", libcfs_nid2str(gw_nid),
1733 alive ? "up" : "down");
1736 /* If the gateway crashed, close all open connections... */
1737 ksocknal_close_matching_conns (id, 0);
1741 /* ...otherwise do nothing. We can only establish new connections
1742 * if we have autroutes, and these connect on demand. */
1746 ksocknal_push_peer (ksock_peer_t *peer)
1750 struct list_head *tmp;
1753 for (index = 0; ; index++) {
1754 read_lock (&ksocknal_data.ksnd_global_lock);
1759 list_for_each (tmp, &peer->ksnp_conns) {
1761 conn = list_entry (tmp, ksock_conn_t, ksnc_list);
1762 ksocknal_conn_addref(conn);
1767 read_unlock (&ksocknal_data.ksnd_global_lock);
1772 ksocknal_lib_push_conn (conn);
1773 ksocknal_conn_decref(conn);
1778 ksocknal_push (lnet_ni_t *ni, lnet_process_id_t id)
1781 struct list_head *tmp;
1787 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
1788 for (j = 0; ; j++) {
1789 read_lock (&ksocknal_data.ksnd_global_lock);
1794 list_for_each (tmp, &ksocknal_data.ksnd_peers[i]) {
1795 peer = list_entry(tmp, ksock_peer_t,
1798 if (!((id.nid == LNET_NID_ANY ||
1799 id.nid == peer->ksnp_id.nid) &&
1800 (id.pid == LNET_PID_ANY ||
1801 id.pid == peer->ksnp_id.pid))) {
1807 ksocknal_peer_addref(peer);
1812 read_unlock (&ksocknal_data.ksnd_global_lock);
1816 ksocknal_push_peer (peer);
1817 ksocknal_peer_decref(peer);
1827 ksocknal_add_interface(lnet_ni_t *ni, __u32 ipaddress, __u32 netmask)
1829 ksock_net_t *net = ni->ni_data;
1830 ksock_interface_t *iface;
1834 struct list_head *ptmp;
1836 struct list_head *rtmp;
1837 ksock_route_t *route;
1839 if (ipaddress == 0 ||
1843 write_lock_bh (&ksocknal_data.ksnd_global_lock);
1845 iface = ksocknal_ip2iface(ni, ipaddress);
1846 if (iface != NULL) {
1847 /* silently ignore dups */
1849 } else if (net->ksnn_ninterfaces == LNET_MAX_INTERFACES) {
1852 iface = &net->ksnn_interfaces[net->ksnn_ninterfaces++];
1854 iface->ksni_ipaddr = ipaddress;
1855 iface->ksni_netmask = netmask;
1856 iface->ksni_nroutes = 0;
1857 iface->ksni_npeers = 0;
1859 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
1860 list_for_each(ptmp, &ksocknal_data.ksnd_peers[i]) {
1861 peer = list_entry(ptmp, ksock_peer_t, ksnp_list);
1863 for (j = 0; j < peer->ksnp_n_passive_ips; j++)
1864 if (peer->ksnp_passive_ips[j] == ipaddress)
1865 iface->ksni_npeers++;
1867 list_for_each(rtmp, &peer->ksnp_routes) {
1868 route = list_entry(rtmp, ksock_route_t, ksnr_list);
1870 if (route->ksnr_myipaddr == ipaddress)
1871 iface->ksni_nroutes++;
1877 /* NB only new connections will pay attention to the new interface! */
1880 write_unlock_bh (&ksocknal_data.ksnd_global_lock);
1886 ksocknal_peer_del_interface_locked(ksock_peer_t *peer, __u32 ipaddr)
1888 struct list_head *tmp;
1889 struct list_head *nxt;
1890 ksock_route_t *route;
1895 for (i = 0; i < peer->ksnp_n_passive_ips; i++)
1896 if (peer->ksnp_passive_ips[i] == ipaddr) {
1897 for (j = i+1; j < peer->ksnp_n_passive_ips; j++)
1898 peer->ksnp_passive_ips[j-1] =
1899 peer->ksnp_passive_ips[j];
1900 peer->ksnp_n_passive_ips--;
1904 list_for_each_safe(tmp, nxt, &peer->ksnp_routes) {
1905 route = list_entry (tmp, ksock_route_t, ksnr_list);
1907 if (route->ksnr_myipaddr != ipaddr)
1910 if (route->ksnr_share_count != 0) {
1911 /* Manually created; keep, but unbind */
1912 route->ksnr_myipaddr = 0;
1914 ksocknal_del_route_locked(route);
1918 list_for_each_safe(tmp, nxt, &peer->ksnp_conns) {
1919 conn = list_entry(tmp, ksock_conn_t, ksnc_list);
1921 if (conn->ksnc_myipaddr == ipaddr)
1922 ksocknal_close_conn_locked (conn, 0);
1927 ksocknal_del_interface(lnet_ni_t *ni, __u32 ipaddress)
1929 ksock_net_t *net = ni->ni_data;
1931 struct list_head *tmp;
1932 struct list_head *nxt;
1938 write_lock_bh (&ksocknal_data.ksnd_global_lock);
1940 for (i = 0; i < net->ksnn_ninterfaces; i++) {
1941 this_ip = net->ksnn_interfaces[i].ksni_ipaddr;
1943 if (!(ipaddress == 0 ||
1944 ipaddress == this_ip))
1949 for (j = i+1; j < net->ksnn_ninterfaces; j++)
1950 net->ksnn_interfaces[j-1] =
1951 net->ksnn_interfaces[j];
1953 net->ksnn_ninterfaces--;
1955 for (j = 0; j < ksocknal_data.ksnd_peer_hash_size; j++) {
1956 list_for_each_safe(tmp, nxt, &ksocknal_data.ksnd_peers[j]) {
1957 peer = list_entry(tmp, ksock_peer_t, ksnp_list);
1959 if (peer->ksnp_ni != ni)
1962 ksocknal_peer_del_interface_locked(peer, this_ip);
1967 write_unlock_bh (&ksocknal_data.ksnd_global_lock);
1973 ksocknal_ctl(lnet_ni_t *ni, unsigned int cmd, void *arg)
1975 struct libcfs_ioctl_data *data = arg;
1979 case IOC_LIBCFS_GET_INTERFACE: {
1980 ksock_net_t *net = ni->ni_data;
1981 ksock_interface_t *iface;
1983 read_lock (&ksocknal_data.ksnd_global_lock);
1985 if (data->ioc_count < 0 ||
1986 data->ioc_count >= net->ksnn_ninterfaces) {
1990 iface = &net->ksnn_interfaces[data->ioc_count];
1992 data->ioc_u32[0] = iface->ksni_ipaddr;
1993 data->ioc_u32[1] = iface->ksni_netmask;
1994 data->ioc_u32[2] = iface->ksni_npeers;
1995 data->ioc_u32[3] = iface->ksni_nroutes;
1998 read_unlock (&ksocknal_data.ksnd_global_lock);
2002 case IOC_LIBCFS_ADD_INTERFACE:
2003 return ksocknal_add_interface(ni,
2004 data->ioc_u32[0], /* IP address */
2005 data->ioc_u32[1]); /* net mask */
2007 case IOC_LIBCFS_DEL_INTERFACE:
2008 return ksocknal_del_interface(ni,
2009 data->ioc_u32[0]); /* IP address */
2011 case IOC_LIBCFS_GET_PEER: {
2012 lnet_process_id_t id = {0,};
2017 int share_count = 0;
2019 rc = ksocknal_get_peer_info(ni, data->ioc_count,
2020 &id, &myip, &ip, &port,
2021 &conn_count, &share_count);
2025 data->ioc_nid = id.nid;
2026 data->ioc_count = share_count;
2027 data->ioc_u32[0] = ip;
2028 data->ioc_u32[1] = port;
2029 data->ioc_u32[2] = myip;
2030 data->ioc_u32[3] = conn_count;
2031 data->ioc_u32[4] = id.pid;
2035 case IOC_LIBCFS_ADD_PEER: {
2036 lnet_process_id_t id = {.nid = data->ioc_nid,
2037 .pid = LUSTRE_SRV_LNET_PID};
2038 return ksocknal_add_peer (ni, id,
2039 data->ioc_u32[0], /* IP */
2040 data->ioc_u32[1]); /* port */
2042 case IOC_LIBCFS_DEL_PEER: {
2043 lnet_process_id_t id = {.nid = data->ioc_nid,
2044 .pid = LNET_PID_ANY};
2045 return ksocknal_del_peer (ni, id,
2046 data->ioc_u32[0]); /* IP */
2048 case IOC_LIBCFS_GET_CONN: {
2052 ksock_conn_t *conn = ksocknal_get_conn_by_idx (ni, data->ioc_count);
2057 ksocknal_lib_get_conn_tunables(conn, &txmem, &rxmem, &nagle);
2059 data->ioc_count = txmem;
2060 data->ioc_nid = conn->ksnc_peer->ksnp_id.nid;
2061 data->ioc_flags = nagle;
2062 data->ioc_u32[0] = conn->ksnc_ipaddr;
2063 data->ioc_u32[1] = conn->ksnc_port;
2064 data->ioc_u32[2] = conn->ksnc_myipaddr;
2065 data->ioc_u32[3] = conn->ksnc_type;
2066 data->ioc_u32[4] = conn->ksnc_scheduler -
2067 ksocknal_data.ksnd_schedulers;
2068 data->ioc_u32[5] = rxmem;
2069 data->ioc_u32[6] = conn->ksnc_peer->ksnp_id.pid;
2070 ksocknal_conn_decref(conn);
2074 case IOC_LIBCFS_CLOSE_CONNECTION: {
2075 lnet_process_id_t id = {.nid = data->ioc_nid,
2076 .pid = LNET_PID_ANY};
2078 return ksocknal_close_matching_conns (id,
2081 case IOC_LIBCFS_REGISTER_MYNID:
2082 /* Ignore if this is a noop */
2083 if (data->ioc_nid == ni->ni_nid)
2086 CERROR("obsolete IOC_LIBCFS_REGISTER_MYNID: %s(%s)\n",
2087 libcfs_nid2str(data->ioc_nid),
2088 libcfs_nid2str(ni->ni_nid));
2091 case IOC_LIBCFS_PUSH_CONNECTION: {
2092 lnet_process_id_t id = {.nid = data->ioc_nid,
2093 .pid = LNET_PID_ANY};
2095 return ksocknal_push(ni, id);
2104 ksocknal_free_buffers (void)
2106 LASSERT (atomic_read(&ksocknal_data.ksnd_nactive_txs) == 0);
2108 if (ksocknal_data.ksnd_schedulers != NULL)
2109 LIBCFS_FREE (ksocknal_data.ksnd_schedulers,
2110 sizeof (ksock_sched_t) * ksocknal_data.ksnd_nschedulers);
2112 LIBCFS_FREE (ksocknal_data.ksnd_peers,
2113 sizeof (struct list_head) *
2114 ksocknal_data.ksnd_peer_hash_size);
2116 spin_lock(&ksocknal_data.ksnd_tx_lock);
2118 if (!list_empty(&ksocknal_data.ksnd_idle_noop_txs)) {
2119 struct list_head zlist;
2122 list_add(&zlist, &ksocknal_data.ksnd_idle_noop_txs);
2123 list_del_init(&ksocknal_data.ksnd_idle_noop_txs);
2124 spin_unlock(&ksocknal_data.ksnd_tx_lock);
2126 while(!list_empty(&zlist)) {
2127 tx = list_entry(zlist.next, ksock_tx_t, tx_list);
2128 list_del(&tx->tx_list);
2129 LIBCFS_FREE(tx, tx->tx_desc_size);
2132 spin_unlock(&ksocknal_data.ksnd_tx_lock);
2137 ksocknal_base_shutdown (void)
2139 ksock_sched_t *sched;
2142 CDEBUG(D_MALLOC, "before NAL cleanup: kmem %d\n",
2143 atomic_read (&libcfs_kmemory));
2144 LASSERT (ksocknal_data.ksnd_nnets == 0);
2146 switch (ksocknal_data.ksnd_init) {
2150 case SOCKNAL_INIT_ALL:
2151 case SOCKNAL_INIT_DATA:
2152 LASSERT (ksocknal_data.ksnd_peers != NULL);
2153 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
2154 LASSERT (list_empty (&ksocknal_data.ksnd_peers[i]));
2156 LASSERT (list_empty (&ksocknal_data.ksnd_enomem_conns));
2157 LASSERT (list_empty (&ksocknal_data.ksnd_zombie_conns));
2158 LASSERT (list_empty (&ksocknal_data.ksnd_connd_connreqs));
2159 LASSERT (list_empty (&ksocknal_data.ksnd_connd_routes));
2161 if (ksocknal_data.ksnd_schedulers != NULL)
2162 for (i = 0; i < ksocknal_data.ksnd_nschedulers; i++) {
2163 ksock_sched_t *kss =
2164 &ksocknal_data.ksnd_schedulers[i];
2166 LASSERT (list_empty (&kss->kss_tx_conns));
2167 LASSERT (list_empty (&kss->kss_rx_conns));
2168 LASSERT (list_empty (&kss->kss_zombie_noop_txs));
2169 LASSERT (kss->kss_nconns == 0);
2172 /* flag threads to terminate; wake and wait for them to die */
2173 ksocknal_data.ksnd_shuttingdown = 1;
2174 cfs_waitq_broadcast (&ksocknal_data.ksnd_connd_waitq);
2175 cfs_waitq_broadcast (&ksocknal_data.ksnd_reaper_waitq);
2177 if (ksocknal_data.ksnd_schedulers != NULL)
2178 for (i = 0; i < ksocknal_data.ksnd_nschedulers; i++) {
2179 sched = &ksocknal_data.ksnd_schedulers[i];
2180 cfs_waitq_broadcast(&sched->kss_waitq);
2184 read_lock (&ksocknal_data.ksnd_global_lock);
2185 while (ksocknal_data.ksnd_nthreads != 0) {
2187 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET, /* power of 2? */
2188 "waiting for %d threads to terminate\n",
2189 ksocknal_data.ksnd_nthreads);
2190 read_unlock (&ksocknal_data.ksnd_global_lock);
2191 cfs_pause(cfs_time_seconds(1));
2192 read_lock (&ksocknal_data.ksnd_global_lock);
2194 read_unlock (&ksocknal_data.ksnd_global_lock);
2196 ksocknal_free_buffers();
2198 ksocknal_data.ksnd_init = SOCKNAL_INIT_NOTHING;
2202 CDEBUG(D_MALLOC, "after NAL cleanup: kmem %d\n",
2203 atomic_read (&libcfs_kmemory));
2205 PORTAL_MODULE_UNUSE;
2210 ksocknal_new_incarnation (void)
2214 /* The incarnation number is the time this module loaded and it
2215 * identifies this particular instance of the socknal. Hopefully
2216 * we won't be able to reboot more frequently than 1MHz for the
2217 * forseeable future :) */
2219 do_gettimeofday(&tv);
2221 return (((__u64)tv.tv_sec) * 1000000) + tv.tv_usec;
2225 ksocknal_base_startup (void)
2230 LASSERT (ksocknal_data.ksnd_init == SOCKNAL_INIT_NOTHING);
2231 LASSERT (ksocknal_data.ksnd_nnets == 0);
2233 memset (&ksocknal_data, 0, sizeof (ksocknal_data)); /* zero pointers */
2235 ksocknal_data.ksnd_peer_hash_size = SOCKNAL_PEER_HASH_SIZE;
2236 LIBCFS_ALLOC (ksocknal_data.ksnd_peers,
2237 sizeof (struct list_head) * ksocknal_data.ksnd_peer_hash_size);
2238 if (ksocknal_data.ksnd_peers == NULL)
2241 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++)
2242 CFS_INIT_LIST_HEAD(&ksocknal_data.ksnd_peers[i]);
2244 rwlock_init(&ksocknal_data.ksnd_global_lock);
2246 spin_lock_init (&ksocknal_data.ksnd_reaper_lock);
2247 CFS_INIT_LIST_HEAD (&ksocknal_data.ksnd_enomem_conns);
2248 CFS_INIT_LIST_HEAD (&ksocknal_data.ksnd_zombie_conns);
2249 CFS_INIT_LIST_HEAD (&ksocknal_data.ksnd_deathrow_conns);
2250 cfs_waitq_init(&ksocknal_data.ksnd_reaper_waitq);
2252 spin_lock_init (&ksocknal_data.ksnd_connd_lock);
2253 CFS_INIT_LIST_HEAD (&ksocknal_data.ksnd_connd_connreqs);
2254 CFS_INIT_LIST_HEAD (&ksocknal_data.ksnd_connd_routes);
2255 cfs_waitq_init(&ksocknal_data.ksnd_connd_waitq);
2257 spin_lock_init (&ksocknal_data.ksnd_tx_lock);
2258 CFS_INIT_LIST_HEAD (&ksocknal_data.ksnd_idle_noop_txs);
2260 /* NB memset above zeros whole of ksocknal_data, including
2261 * ksocknal_data.ksnd_irqinfo[all].ksni_valid */
2263 /* flag lists/ptrs/locks initialised */
2264 ksocknal_data.ksnd_init = SOCKNAL_INIT_DATA;
2267 ksocknal_data.ksnd_nschedulers = ksocknal_nsched();
2268 LIBCFS_ALLOC(ksocknal_data.ksnd_schedulers,
2269 sizeof(ksock_sched_t) * ksocknal_data.ksnd_nschedulers);
2270 if (ksocknal_data.ksnd_schedulers == NULL)
2273 for (i = 0; i < ksocknal_data.ksnd_nschedulers; i++) {
2274 ksock_sched_t *kss = &ksocknal_data.ksnd_schedulers[i];
2276 spin_lock_init (&kss->kss_lock);
2277 CFS_INIT_LIST_HEAD (&kss->kss_rx_conns);
2278 CFS_INIT_LIST_HEAD (&kss->kss_tx_conns);
2279 CFS_INIT_LIST_HEAD (&kss->kss_zombie_noop_txs);
2280 cfs_waitq_init (&kss->kss_waitq);
2283 for (i = 0; i < ksocknal_data.ksnd_nschedulers; i++) {
2284 rc = ksocknal_thread_start (ksocknal_scheduler,
2285 &ksocknal_data.ksnd_schedulers[i]);
2287 CERROR("Can't spawn socknal scheduler[%d]: %d\n",
2293 /* must have at least 2 connds to remain responsive to accepts while
2295 if (*ksocknal_tunables.ksnd_nconnds < 2)
2296 *ksocknal_tunables.ksnd_nconnds = 2;
2298 for (i = 0; i < *ksocknal_tunables.ksnd_nconnds; i++) {
2299 rc = ksocknal_thread_start (ksocknal_connd, (void *)((long)i));
2301 CERROR("Can't spawn socknal connd: %d\n", rc);
2306 rc = ksocknal_thread_start (ksocknal_reaper, NULL);
2308 CERROR ("Can't spawn socknal reaper: %d\n", rc);
2312 /* flag everything initialised */
2313 ksocknal_data.ksnd_init = SOCKNAL_INIT_ALL;
2318 ksocknal_base_shutdown();
2323 ksocknal_debug_peerhash (lnet_ni_t *ni)
2325 ksock_peer_t *peer = NULL;
2326 struct list_head *tmp;
2329 read_lock (&ksocknal_data.ksnd_global_lock);
2331 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
2332 list_for_each (tmp, &ksocknal_data.ksnd_peers[i]) {
2333 peer = list_entry (tmp, ksock_peer_t, ksnp_list);
2335 if (peer->ksnp_ni == ni) break;
2342 ksock_route_t *route;
2345 CWARN ("Active peer on shutdown: %s, ref %d, scnt %d, "
2346 "closing %d, accepting %d, err %d, zcookie "LPU64", "
2347 "txq %d, zc_req %d\n", libcfs_id2str(peer->ksnp_id),
2348 atomic_read(&peer->ksnp_refcount),
2349 peer->ksnp_sharecount, peer->ksnp_closing,
2350 peer->ksnp_accepting, peer->ksnp_error,
2351 peer->ksnp_zc_next_cookie,
2352 !list_empty(&peer->ksnp_tx_queue),
2353 !list_empty(&peer->ksnp_zc_req_list));
2355 list_for_each (tmp, &peer->ksnp_routes) {
2356 route = list_entry(tmp, ksock_route_t, ksnr_list);
2357 CWARN ("Route: ref %d, schd %d, conn %d, cnted %d, "
2358 "del %d\n", atomic_read(&route->ksnr_refcount),
2359 route->ksnr_scheduled, route->ksnr_connecting,
2360 route->ksnr_connected, route->ksnr_deleted);
2363 list_for_each (tmp, &peer->ksnp_conns) {
2364 conn = list_entry(tmp, ksock_conn_t, ksnc_list);
2365 CWARN ("Conn: ref %d, sref %d, t %d, c %d\n",
2366 atomic_read(&conn->ksnc_conn_refcount),
2367 atomic_read(&conn->ksnc_sock_refcount),
2368 conn->ksnc_type, conn->ksnc_closing);
2372 read_unlock (&ksocknal_data.ksnd_global_lock);
2377 ksocknal_shutdown (lnet_ni_t *ni)
2379 ksock_net_t *net = ni->ni_data;
2381 lnet_process_id_t anyid = {.nid = LNET_NID_ANY,
2382 .pid = LNET_PID_ANY};
2384 LASSERT(ksocknal_data.ksnd_init == SOCKNAL_INIT_ALL);
2385 LASSERT(ksocknal_data.ksnd_nnets > 0);
2387 spin_lock_bh (&net->ksnn_lock);
2388 net->ksnn_shutdown = 1; /* prevent new peers */
2389 spin_unlock_bh (&net->ksnn_lock);
2391 /* Delete all peers */
2392 ksocknal_del_peer(ni, anyid, 0);
2394 /* Wait for all peer state to clean up */
2396 spin_lock_bh (&net->ksnn_lock);
2397 while (net->ksnn_npeers != 0) {
2398 spin_unlock_bh (&net->ksnn_lock);
2401 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET, /* power of 2? */
2402 "waiting for %d peers to disconnect\n",
2404 cfs_pause(cfs_time_seconds(1));
2406 ksocknal_debug_peerhash(ni);
2408 spin_lock_bh (&net->ksnn_lock);
2410 spin_unlock_bh (&net->ksnn_lock);
2412 for (i = 0; i < net->ksnn_ninterfaces; i++) {
2413 LASSERT (net->ksnn_interfaces[i].ksni_npeers == 0);
2414 LASSERT (net->ksnn_interfaces[i].ksni_nroutes == 0);
2417 LIBCFS_FREE(net, sizeof(*net));
2419 ksocknal_data.ksnd_nnets--;
2420 if (ksocknal_data.ksnd_nnets == 0)
2421 ksocknal_base_shutdown();
2425 ksocknal_enumerate_interfaces(ksock_net_t *net)
2433 n = libcfs_ipif_enumerate(&names);
2435 CERROR("Can't enumerate interfaces: %d\n", n);
2439 for (i = j = 0; i < n; i++) {
2444 if (!strcmp(names[i], "lo")) /* skip the loopback IF */
2447 rc = libcfs_ipif_query(names[i], &up, &ip, &mask);
2449 CWARN("Can't get interface %s info: %d\n",
2455 CWARN("Ignoring interface %s (down)\n",
2460 if (j == LNET_MAX_INTERFACES) {
2461 CWARN("Ignoring interface %s (too many interfaces)\n",
2466 net->ksnn_interfaces[j].ksni_ipaddr = ip;
2467 net->ksnn_interfaces[j].ksni_netmask = mask;
2471 libcfs_ipif_free_enumeration(names, n);
2474 CERROR("Can't find any usable interfaces\n");
2480 ksocknal_startup (lnet_ni_t *ni)
2486 LASSERT (ni->ni_lnd == &the_ksocklnd);
2488 if (ksocknal_data.ksnd_init == SOCKNAL_INIT_NOTHING) {
2489 rc = ksocknal_base_startup();
2494 LIBCFS_ALLOC(net, sizeof(*net));
2498 memset(net, 0, sizeof(*net));
2499 spin_lock_init(&net->ksnn_lock);
2500 net->ksnn_incarnation = ksocknal_new_incarnation();
2502 ni->ni_maxtxcredits = *ksocknal_tunables.ksnd_credits;
2503 ni->ni_peertxcredits = *ksocknal_tunables.ksnd_peercredits;
2505 if (ni->ni_interfaces[0] == NULL) {
2506 rc = ksocknal_enumerate_interfaces(net);
2510 net->ksnn_ninterfaces = 1;
2512 for (i = 0; i < LNET_MAX_INTERFACES; i++) {
2515 if (ni->ni_interfaces[i] == NULL)
2518 rc = libcfs_ipif_query(
2519 ni->ni_interfaces[i], &up,
2520 &net->ksnn_interfaces[i].ksni_ipaddr,
2521 &net->ksnn_interfaces[i].ksni_netmask);
2524 CERROR("Can't get interface %s info: %d\n",
2525 ni->ni_interfaces[i], rc);
2530 CERROR("Interface %s is down\n",
2531 ni->ni_interfaces[i]);
2535 net->ksnn_ninterfaces = i;
2538 ni->ni_nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid),
2539 net->ksnn_interfaces[0].ksni_ipaddr);
2541 ksocknal_data.ksnd_nnets++;
2546 LIBCFS_FREE(net, sizeof(*net));
2548 if (ksocknal_data.ksnd_nnets == 0)
2549 ksocknal_base_shutdown();
2556 ksocknal_module_fini (void)
2558 lnet_unregister_lnd(&the_ksocklnd);
2559 ksocknal_lib_tunables_fini();
2563 ksocknal_module_init (void)
2567 /* check ksnr_connected/connecting field large enough */
2568 CLASSERT(SOCKLND_CONN_NTYPES <= 4);
2570 rc = ksocknal_lib_tunables_init();
2574 lnet_register_lnd(&the_ksocklnd);
2579 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2580 MODULE_DESCRIPTION("Kernel TCP Socket LND v2.0.0");
2581 MODULE_LICENSE("GPL");
2583 cfs_module(ksocknal, "2.0.0", ksocknal_module_init, ksocknal_module_fini);