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)
215 for (i = 0; i < peer->ksnp_n_passive_ips; i++) {
216 LASSERT (i < LNET_MAX_INTERFACES);
217 ip = peer->ksnp_passive_ips[i];
219 ksocknal_ip2iface(peer->ksnp_ni, ip)->ksni_npeers--;
222 LASSERT (list_empty(&peer->ksnp_conns));
223 LASSERT (list_empty(&peer->ksnp_routes));
224 LASSERT (!peer->ksnp_closing);
225 peer->ksnp_closing = 1;
226 list_del (&peer->ksnp_list);
227 /* lose peerlist's ref */
228 ksocknal_peer_decref(peer);
232 ksocknal_get_peer_info (lnet_ni_t *ni, int index,
233 lnet_process_id_t *id, __u32 *myip, __u32 *peer_ip, int *port,
234 int *conn_count, int *share_count)
237 struct list_head *ptmp;
238 ksock_route_t *route;
239 struct list_head *rtmp;
244 read_lock (&ksocknal_data.ksnd_global_lock);
246 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
248 list_for_each (ptmp, &ksocknal_data.ksnd_peers[i]) {
249 peer = list_entry (ptmp, ksock_peer_t, ksnp_list);
251 if (peer->ksnp_ni != ni)
254 if (peer->ksnp_n_passive_ips == 0 &&
255 list_empty(&peer->ksnp_routes)) {
269 for (j = 0; j < peer->ksnp_n_passive_ips; j++) {
274 *myip = peer->ksnp_passive_ips[j];
283 list_for_each (rtmp, &peer->ksnp_routes) {
287 route = list_entry(rtmp, ksock_route_t,
291 *myip = route->ksnr_myipaddr;
292 *peer_ip = route->ksnr_ipaddr;
293 *port = route->ksnr_port;
294 *conn_count = route->ksnr_conn_count;
295 *share_count = route->ksnr_share_count;
302 read_unlock (&ksocknal_data.ksnd_global_lock);
307 ksocknal_associate_route_conn_locked(ksock_route_t *route, ksock_conn_t *conn)
309 ksock_peer_t *peer = route->ksnr_peer;
310 int type = conn->ksnc_type;
311 ksock_interface_t *iface;
313 conn->ksnc_route = route;
314 ksocknal_route_addref(route);
316 if (route->ksnr_myipaddr != conn->ksnc_myipaddr) {
317 if (route->ksnr_myipaddr == 0) {
318 /* route wasn't bound locally yet (the initial route) */
319 CDEBUG(D_NET, "Binding %s %u.%u.%u.%u to %u.%u.%u.%u\n",
320 libcfs_id2str(peer->ksnp_id),
321 HIPQUAD(route->ksnr_ipaddr),
322 HIPQUAD(conn->ksnc_myipaddr));
324 CDEBUG(D_NET, "Rebinding %s %u.%u.%u.%u from "
325 "%u.%u.%u.%u to %u.%u.%u.%u\n",
326 libcfs_id2str(peer->ksnp_id),
327 HIPQUAD(route->ksnr_ipaddr),
328 HIPQUAD(route->ksnr_myipaddr),
329 HIPQUAD(conn->ksnc_myipaddr));
331 iface = ksocknal_ip2iface(route->ksnr_peer->ksnp_ni,
332 route->ksnr_myipaddr);
334 iface->ksni_nroutes--;
336 route->ksnr_myipaddr = conn->ksnc_myipaddr;
337 iface = ksocknal_ip2iface(route->ksnr_peer->ksnp_ni,
338 route->ksnr_myipaddr);
340 iface->ksni_nroutes++;
343 route->ksnr_connected |= (1<<type);
344 route->ksnr_conn_count++;
346 /* Successful connection => further attempts can
347 * proceed immediately */
348 route->ksnr_retry_interval = 0;
352 ksocknal_add_route_locked (ksock_peer_t *peer, ksock_route_t *route)
354 struct list_head *tmp;
356 ksock_route_t *route2;
358 LASSERT (!peer->ksnp_closing);
359 LASSERT (route->ksnr_peer == NULL);
360 LASSERT (!route->ksnr_scheduled);
361 LASSERT (!route->ksnr_connecting);
362 LASSERT (route->ksnr_connected == 0);
364 /* LASSERT(unique) */
365 list_for_each(tmp, &peer->ksnp_routes) {
366 route2 = list_entry(tmp, ksock_route_t, ksnr_list);
368 if (route2->ksnr_ipaddr == route->ksnr_ipaddr) {
369 CERROR ("Duplicate route %s %u.%u.%u.%u\n",
370 libcfs_id2str(peer->ksnp_id),
371 HIPQUAD(route->ksnr_ipaddr));
376 route->ksnr_peer = peer;
377 ksocknal_peer_addref(peer);
378 /* peer's routelist takes over my ref on 'route' */
379 list_add_tail(&route->ksnr_list, &peer->ksnp_routes);
381 list_for_each(tmp, &peer->ksnp_conns) {
382 conn = list_entry(tmp, ksock_conn_t, ksnc_list);
384 if (conn->ksnc_ipaddr != route->ksnr_ipaddr)
387 ksocknal_associate_route_conn_locked(route, conn);
388 /* keep going (typed routes) */
393 ksocknal_del_route_locked (ksock_route_t *route)
395 ksock_peer_t *peer = route->ksnr_peer;
396 ksock_interface_t *iface;
398 struct list_head *ctmp;
399 struct list_head *cnxt;
401 LASSERT (!route->ksnr_deleted);
403 /* Close associated conns */
404 list_for_each_safe (ctmp, cnxt, &peer->ksnp_conns) {
405 conn = list_entry(ctmp, ksock_conn_t, ksnc_list);
407 if (conn->ksnc_route != route)
410 ksocknal_close_conn_locked (conn, 0);
413 if (route->ksnr_myipaddr != 0) {
414 iface = ksocknal_ip2iface(route->ksnr_peer->ksnp_ni,
415 route->ksnr_myipaddr);
417 iface->ksni_nroutes--;
420 route->ksnr_deleted = 1;
421 list_del (&route->ksnr_list);
422 ksocknal_route_decref(route); /* drop peer's ref */
424 if (list_empty (&peer->ksnp_routes) &&
425 list_empty (&peer->ksnp_conns)) {
426 /* I've just removed the last route to a peer with no active
428 ksocknal_unlink_peer_locked (peer);
433 ksocknal_add_peer (lnet_ni_t *ni, lnet_process_id_t id, __u32 ipaddr, int port)
435 struct list_head *tmp;
438 ksock_route_t *route;
439 ksock_route_t *route2;
442 if (id.nid == LNET_NID_ANY ||
443 id.pid == LNET_PID_ANY)
446 /* Have a brand new peer ready... */
447 rc = ksocknal_create_peer(&peer, ni, id);
451 route = ksocknal_create_route (ipaddr, port);
453 ksocknal_peer_decref(peer);
457 write_lock_bh (&ksocknal_data.ksnd_global_lock);
459 /* always called with a ref on ni, so shutdown can't have started */
460 LASSERT (((ksock_net_t *) ni->ni_data)->ksnn_shutdown == 0);
462 peer2 = ksocknal_find_peer_locked (ni, id);
464 ksocknal_peer_decref(peer);
467 /* peer table takes my ref on peer */
468 list_add_tail (&peer->ksnp_list,
469 ksocknal_nid2peerlist (id.nid));
473 list_for_each (tmp, &peer->ksnp_routes) {
474 route2 = list_entry(tmp, ksock_route_t, ksnr_list);
476 if (route2->ksnr_ipaddr == ipaddr)
481 if (route2 == NULL) {
482 ksocknal_add_route_locked(peer, route);
483 route->ksnr_share_count++;
485 ksocknal_route_decref(route);
486 route2->ksnr_share_count++;
489 write_unlock_bh (&ksocknal_data.ksnd_global_lock);
495 ksocknal_del_peer_locked (ksock_peer_t *peer, __u32 ip)
498 ksock_route_t *route;
499 struct list_head *tmp;
500 struct list_head *nxt;
503 LASSERT (!peer->ksnp_closing);
505 /* Extra ref prevents peer disappearing until I'm done with it */
506 ksocknal_peer_addref(peer);
508 list_for_each_safe (tmp, nxt, &peer->ksnp_routes) {
509 route = list_entry(tmp, ksock_route_t, ksnr_list);
512 if (!(ip == 0 || route->ksnr_ipaddr == ip))
515 route->ksnr_share_count = 0;
516 /* This deletes associated conns too */
517 ksocknal_del_route_locked (route);
521 list_for_each_safe (tmp, nxt, &peer->ksnp_routes) {
522 route = list_entry(tmp, ksock_route_t, ksnr_list);
523 nshared += route->ksnr_share_count;
527 /* remove everything else if there are no explicit entries
530 list_for_each_safe (tmp, nxt, &peer->ksnp_routes) {
531 route = list_entry(tmp, ksock_route_t, ksnr_list);
533 /* we should only be removing auto-entries */
534 LASSERT(route->ksnr_share_count == 0);
535 ksocknal_del_route_locked (route);
538 list_for_each_safe (tmp, nxt, &peer->ksnp_conns) {
539 conn = list_entry(tmp, ksock_conn_t, ksnc_list);
541 ksocknal_close_conn_locked(conn, 0);
545 ksocknal_peer_decref(peer);
546 /* NB peer unlinks itself when last conn/route is removed */
550 ksocknal_del_peer (lnet_ni_t *ni, lnet_process_id_t id, __u32 ip)
552 CFS_LIST_HEAD (zombies);
553 struct list_head *ptmp;
554 struct list_head *pnxt;
561 write_lock_bh (&ksocknal_data.ksnd_global_lock);
563 if (id.nid != LNET_NID_ANY)
564 lo = hi = ksocknal_nid2peerlist(id.nid) - ksocknal_data.ksnd_peers;
567 hi = ksocknal_data.ksnd_peer_hash_size - 1;
570 for (i = lo; i <= hi; i++) {
571 list_for_each_safe (ptmp, pnxt, &ksocknal_data.ksnd_peers[i]) {
572 peer = list_entry (ptmp, ksock_peer_t, ksnp_list);
574 if (peer->ksnp_ni != ni)
577 if (!((id.nid == LNET_NID_ANY || peer->ksnp_id.nid == id.nid) &&
578 (id.pid == LNET_PID_ANY || peer->ksnp_id.pid == id.pid)))
581 ksocknal_peer_addref(peer); /* a ref for me... */
583 ksocknal_del_peer_locked (peer, ip);
585 if (peer->ksnp_closing && !list_empty(&peer->ksnp_tx_queue)) {
586 LASSERT (list_empty(&peer->ksnp_conns));
587 LASSERT (list_empty(&peer->ksnp_routes));
589 list_splice_init(&peer->ksnp_tx_queue, &zombies);
592 ksocknal_peer_decref(peer); /* ...till here */
594 rc = 0; /* matched! */
598 write_unlock_bh (&ksocknal_data.ksnd_global_lock);
600 ksocknal_txlist_done(ni, &zombies, 1);
606 ksocknal_get_conn_by_idx (lnet_ni_t *ni, int index)
609 struct list_head *ptmp;
611 struct list_head *ctmp;
614 read_lock (&ksocknal_data.ksnd_global_lock);
616 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
617 list_for_each (ptmp, &ksocknal_data.ksnd_peers[i]) {
618 peer = list_entry (ptmp, ksock_peer_t, ksnp_list);
620 LASSERT (!peer->ksnp_closing);
622 if (peer->ksnp_ni != ni)
625 list_for_each (ctmp, &peer->ksnp_conns) {
629 conn = list_entry (ctmp, ksock_conn_t, ksnc_list);
630 ksocknal_conn_addref(conn);
631 read_unlock (&ksocknal_data.ksnd_global_lock);
637 read_unlock (&ksocknal_data.ksnd_global_lock);
642 ksocknal_choose_scheduler_locked (unsigned int irq)
644 ksock_sched_t *sched;
645 ksock_irqinfo_t *info;
648 LASSERT (irq < NR_IRQS);
649 info = &ksocknal_data.ksnd_irqinfo[irq];
651 if (irq != 0 && /* hardware NIC */
652 info->ksni_valid) { /* already set up */
653 return (&ksocknal_data.ksnd_schedulers[info->ksni_sched]);
656 /* software NIC (irq == 0) || not associated with a scheduler yet.
657 * Choose the CPU with the fewest connections... */
658 sched = &ksocknal_data.ksnd_schedulers[0];
659 for (i = 1; i < ksocknal_data.ksnd_nschedulers; i++)
660 if (sched->kss_nconns >
661 ksocknal_data.ksnd_schedulers[i].kss_nconns)
662 sched = &ksocknal_data.ksnd_schedulers[i];
664 if (irq != 0) { /* Hardware NIC */
665 info->ksni_valid = 1;
666 info->ksni_sched = sched - ksocknal_data.ksnd_schedulers;
669 LASSERT (info->ksni_sched == sched - ksocknal_data.ksnd_schedulers);
676 ksocknal_local_ipvec (lnet_ni_t *ni, __u32 *ipaddrs)
678 ksock_net_t *net = ni->ni_data;
682 read_lock (&ksocknal_data.ksnd_global_lock);
684 nip = net->ksnn_ninterfaces;
685 LASSERT (nip <= LNET_MAX_INTERFACES);
687 /* Only offer interfaces for additional connections if I have
690 read_unlock (&ksocknal_data.ksnd_global_lock);
694 for (i = 0; i < nip; i++) {
695 ipaddrs[i] = net->ksnn_interfaces[i].ksni_ipaddr;
696 LASSERT (ipaddrs[i] != 0);
699 read_unlock (&ksocknal_data.ksnd_global_lock);
704 ksocknal_match_peerip (ksock_interface_t *iface, __u32 *ips, int nips)
706 int best_netmatch = 0;
713 for (i = 0; i < nips; i++) {
717 this_xor = (ips[i] ^ iface->ksni_ipaddr);
718 this_netmatch = ((this_xor & iface->ksni_netmask) == 0) ? 1 : 0;
721 best_netmatch < this_netmatch ||
722 (best_netmatch == this_netmatch &&
723 best_xor > this_xor)))
727 best_netmatch = this_netmatch;
736 ksocknal_select_ips(ksock_peer_t *peer, __u32 *peerips, int n_peerips)
738 rwlock_t *global_lock = &ksocknal_data.ksnd_global_lock;
739 ksock_net_t *net = peer->ksnp_ni->ni_data;
740 ksock_interface_t *iface;
741 ksock_interface_t *best_iface;
752 /* CAVEAT EMPTOR: We do all our interface matching with an
753 * exclusive hold of global lock at IRQ priority. We're only
754 * expecting to be dealing with small numbers of interfaces, so the
755 * O(n**3)-ness shouldn't matter */
757 /* Also note that I'm not going to return more than n_peerips
758 * interfaces, even if I have more myself */
760 write_lock_bh (global_lock);
762 LASSERT (n_peerips <= LNET_MAX_INTERFACES);
763 LASSERT (net->ksnn_ninterfaces <= LNET_MAX_INTERFACES);
765 /* Only match interfaces for additional connections
766 * if I have > 1 interface */
767 n_ips = (net->ksnn_ninterfaces < 2) ? 0 :
768 MIN(n_peerips, net->ksnn_ninterfaces);
770 for (i = 0; peer->ksnp_n_passive_ips < n_ips; i++) {
771 /* ^ yes really... */
773 /* If we have any new interfaces, first tick off all the
774 * peer IPs that match old interfaces, then choose new
775 * interfaces to match the remaining peer IPS.
776 * We don't forget interfaces we've stopped using; we might
777 * start using them again... */
779 if (i < peer->ksnp_n_passive_ips) {
781 ip = peer->ksnp_passive_ips[i];
782 best_iface = ksocknal_ip2iface(peer->ksnp_ni, ip);
784 /* peer passive ips are kept up to date */
785 LASSERT(best_iface != NULL);
787 /* choose a new interface */
788 LASSERT (i == peer->ksnp_n_passive_ips);
794 for (j = 0; j < net->ksnn_ninterfaces; j++) {
795 iface = &net->ksnn_interfaces[j];
796 ip = iface->ksni_ipaddr;
798 for (k = 0; k < peer->ksnp_n_passive_ips; k++)
799 if (peer->ksnp_passive_ips[k] == ip)
802 if (k < peer->ksnp_n_passive_ips) /* using it already */
805 k = ksocknal_match_peerip(iface, peerips, n_peerips);
806 xor = (ip ^ peerips[k]);
807 this_netmatch = ((xor & iface->ksni_netmask) == 0) ? 1 : 0;
809 if (!(best_iface == NULL ||
810 best_netmatch < this_netmatch ||
811 (best_netmatch == this_netmatch &&
812 best_npeers > iface->ksni_npeers)))
816 best_netmatch = this_netmatch;
817 best_npeers = iface->ksni_npeers;
820 best_iface->ksni_npeers++;
821 ip = best_iface->ksni_ipaddr;
822 peer->ksnp_passive_ips[i] = ip;
823 peer->ksnp_n_passive_ips = i+1;
826 LASSERT (best_iface != NULL);
828 /* mark the best matching peer IP used */
829 j = ksocknal_match_peerip(best_iface, peerips, n_peerips);
833 /* Overwrite input peer IP addresses */
834 memcpy(peerips, peer->ksnp_passive_ips, n_ips * sizeof(*peerips));
836 write_unlock_bh (global_lock);
842 ksocknal_create_routes(ksock_peer_t *peer, int port,
843 __u32 *peer_ipaddrs, int npeer_ipaddrs)
845 ksock_route_t *newroute = NULL;
846 rwlock_t *global_lock = &ksocknal_data.ksnd_global_lock;
847 lnet_ni_t *ni = peer->ksnp_ni;
848 ksock_net_t *net = ni->ni_data;
849 struct list_head *rtmp;
850 ksock_route_t *route;
851 ksock_interface_t *iface;
852 ksock_interface_t *best_iface;
859 /* CAVEAT EMPTOR: We do all our interface matching with an
860 * exclusive hold of global lock at IRQ priority. We're only
861 * expecting to be dealing with small numbers of interfaces, so the
862 * O(n**3)-ness here shouldn't matter */
864 write_lock_bh (global_lock);
866 if (net->ksnn_ninterfaces < 2) {
867 /* Only create additional connections
868 * if I have > 1 interface */
869 write_unlock_bh (global_lock);
873 LASSERT (npeer_ipaddrs <= LNET_MAX_INTERFACES);
875 for (i = 0; i < npeer_ipaddrs; i++) {
876 if (newroute != NULL) {
877 newroute->ksnr_ipaddr = peer_ipaddrs[i];
879 write_unlock_bh (global_lock);
881 newroute = ksocknal_create_route(peer_ipaddrs[i], port);
882 if (newroute == NULL)
885 write_lock_bh (global_lock);
888 if (peer->ksnp_closing) {
889 /* peer got closed under me */
893 /* Already got a route? */
895 list_for_each(rtmp, &peer->ksnp_routes) {
896 route = list_entry(rtmp, ksock_route_t, ksnr_list);
898 if (route->ksnr_ipaddr == newroute->ksnr_ipaddr)
910 LASSERT (net->ksnn_ninterfaces <= LNET_MAX_INTERFACES);
912 /* Select interface to connect from */
913 for (j = 0; j < net->ksnn_ninterfaces; j++) {
914 iface = &net->ksnn_interfaces[j];
916 /* Using this interface already? */
917 list_for_each(rtmp, &peer->ksnp_routes) {
918 route = list_entry(rtmp, ksock_route_t, ksnr_list);
920 if (route->ksnr_myipaddr == iface->ksni_ipaddr)
928 this_netmatch = (((iface->ksni_ipaddr ^
929 newroute->ksnr_ipaddr) &
930 iface->ksni_netmask) == 0) ? 1 : 0;
932 if (!(best_iface == NULL ||
933 best_netmatch < this_netmatch ||
934 (best_netmatch == this_netmatch &&
935 best_nroutes > iface->ksni_nroutes)))
939 best_netmatch = this_netmatch;
940 best_nroutes = iface->ksni_nroutes;
943 if (best_iface == NULL)
946 newroute->ksnr_myipaddr = best_iface->ksni_ipaddr;
947 best_iface->ksni_nroutes++;
949 ksocknal_add_route_locked(peer, newroute);
953 write_unlock_bh (global_lock);
954 if (newroute != NULL)
955 ksocknal_route_decref(newroute);
959 ksocknal_accept (lnet_ni_t *ni, cfs_socket_t *sock)
966 rc = libcfs_sock_getaddr(sock, 1, &peer_ip, &peer_port);
967 LASSERT (rc == 0); /* we succeeded before */
969 LIBCFS_ALLOC(cr, sizeof(*cr));
971 LCONSOLE_ERROR_MSG(0x12f, "Dropping connection request from "
972 "%u.%u.%u.%u: memory exhausted\n",
979 cr->ksncr_sock = sock;
981 spin_lock_bh (&ksocknal_data.ksnd_connd_lock);
983 list_add_tail(&cr->ksncr_list, &ksocknal_data.ksnd_connd_connreqs);
984 cfs_waitq_signal(&ksocknal_data.ksnd_connd_waitq);
986 spin_unlock_bh (&ksocknal_data.ksnd_connd_lock);
991 ksocknal_connecting (ksock_peer_t *peer, __u32 ipaddr)
993 ksock_route_t *route;
995 list_for_each_entry (route, &peer->ksnp_routes, ksnr_list) {
997 if (route->ksnr_ipaddr == ipaddr)
998 return route->ksnr_connecting;
1004 ksocknal_create_conn (lnet_ni_t *ni, ksock_route_t *route,
1005 cfs_socket_t *sock, int type)
1007 rwlock_t *global_lock = &ksocknal_data.ksnd_global_lock;
1008 CFS_LIST_HEAD (zombies);
1009 lnet_process_id_t peerid;
1010 struct list_head *tmp;
1013 ksock_conn_t *conn2;
1014 ksock_peer_t *peer = NULL;
1015 ksock_peer_t *peer2;
1016 ksock_sched_t *sched;
1017 ksock_hello_msg_t *hello;
1024 active = (route != NULL);
1026 LASSERT (active == (type != SOCKLND_CONN_NONE));
1028 irq = ksocknal_lib_sock_irq (sock);
1030 LIBCFS_ALLOC(conn, sizeof(*conn));
1036 memset (conn, 0, sizeof (*conn));
1037 conn->ksnc_peer = NULL;
1038 conn->ksnc_route = NULL;
1039 conn->ksnc_sock = sock;
1040 atomic_set (&conn->ksnc_sock_refcount, 1); /* 1 ref for conn */
1041 conn->ksnc_type = type;
1042 ksocknal_lib_save_callback(sock, conn);
1043 atomic_set (&conn->ksnc_conn_refcount, 1); /* 1 ref for me */
1045 conn->ksnc_zc_capable = ksocknal_lib_zc_capable(sock);
1046 conn->ksnc_rx_ready = 0;
1047 conn->ksnc_rx_scheduled = 0;
1049 CFS_INIT_LIST_HEAD (&conn->ksnc_tx_queue);
1050 conn->ksnc_tx_ready = 0;
1051 conn->ksnc_tx_scheduled = 0;
1052 conn->ksnc_tx_mono = NULL;
1053 atomic_set (&conn->ksnc_tx_nob, 0);
1055 LIBCFS_ALLOC(hello, offsetof(ksock_hello_msg_t,
1056 kshm_ips[LNET_MAX_INTERFACES]));
1057 if (hello == NULL) {
1062 /* stash conn's local and remote addrs */
1063 rc = ksocknal_lib_get_conn_addrs (conn);
1067 /* Find out/confirm peer's NID and connection type and get the
1068 * vector of interfaces she's willing to let me connect to.
1069 * Passive connections use the listener timeout since the peer sends
1073 peer = route->ksnr_peer;
1074 LASSERT(ni == peer->ksnp_ni);
1076 /* Active connection sends HELLO eagerly */
1077 hello->kshm_nips = ksocknal_local_ipvec(ni, hello->kshm_ips);
1078 peerid = peer->ksnp_id;
1080 write_lock_bh(global_lock);
1081 conn->ksnc_proto = peer->ksnp_proto;
1082 write_unlock_bh(global_lock);
1084 if (conn->ksnc_proto == NULL) {
1085 conn->ksnc_proto = &ksocknal_protocol_v2x;
1086 #if SOCKNAL_VERSION_DEBUG
1087 if (*ksocknal_tunables.ksnd_protocol != 2)
1088 conn->ksnc_proto = &ksocknal_protocol_v1x;
1092 rc = ksocknal_send_hello (ni, conn, peerid.nid, hello);
1096 peerid.nid = LNET_NID_ANY;
1097 peerid.pid = LNET_PID_ANY;
1099 /* Passive, get protocol from peer */
1100 conn->ksnc_proto = NULL;
1103 rc = ksocknal_recv_hello (ni, conn, hello, &peerid, &incarnation);
1107 LASSERT (rc == 0 || active);
1108 LASSERT (conn->ksnc_proto != NULL);
1109 LASSERT (peerid.nid != LNET_NID_ANY);
1112 ksocknal_peer_addref(peer);
1113 write_lock_bh (global_lock);
1115 rc = ksocknal_create_peer(&peer, ni, peerid);
1119 write_lock_bh (global_lock);
1121 /* called with a ref on ni, so shutdown can't have started */
1122 LASSERT (((ksock_net_t *) ni->ni_data)->ksnn_shutdown == 0);
1124 peer2 = ksocknal_find_peer_locked(ni, peerid);
1125 if (peer2 == NULL) {
1126 /* NB this puts an "empty" peer in the peer
1127 * table (which takes my ref) */
1128 list_add_tail(&peer->ksnp_list,
1129 ksocknal_nid2peerlist(peerid.nid));
1131 ksocknal_peer_decref(peer);
1136 ksocknal_peer_addref(peer);
1137 peer->ksnp_accepting++;
1139 /* Am I already connecting to this guy? Resolve in
1140 * favour of higher NID... */
1141 if (peerid.nid < ni->ni_nid &&
1142 ksocknal_connecting(peer, conn->ksnc_ipaddr)) {
1144 warn = "connection race resolution";
1149 if (peer->ksnp_closing ||
1150 (active && route->ksnr_deleted)) {
1151 /* peer/route got closed under me */
1153 warn = "peer/route removed";
1157 if (peer->ksnp_proto == NULL) {
1158 /* Never connected before.
1159 * NB recv_hello may have returned EPROTO to signal my peer
1160 * wants a different protocol than the one I asked for.
1162 LASSERT (list_empty(&peer->ksnp_conns));
1164 peer->ksnp_proto = conn->ksnc_proto;
1165 peer->ksnp_incarnation = incarnation;
1168 if (peer->ksnp_proto != conn->ksnc_proto ||
1169 peer->ksnp_incarnation != incarnation) {
1170 /* Peer rebooted or I've got the wrong protocol version */
1171 ksocknal_close_peer_conns_locked(peer, 0, 0);
1173 peer->ksnp_proto = NULL;
1175 warn = peer->ksnp_incarnation != incarnation ?
1177 "wrong proto version";
1187 warn = "lost conn race";
1190 warn = "retry with different protocol version";
1194 /* Refuse to duplicate an existing connection, unless this is a
1195 * loopback connection */
1196 if (conn->ksnc_ipaddr != conn->ksnc_myipaddr) {
1197 list_for_each(tmp, &peer->ksnp_conns) {
1198 conn2 = list_entry(tmp, ksock_conn_t, ksnc_list);
1200 if (conn2->ksnc_ipaddr != conn->ksnc_ipaddr ||
1201 conn2->ksnc_myipaddr != conn->ksnc_myipaddr ||
1202 conn2->ksnc_type != conn->ksnc_type)
1205 /* Reply on a passive connection attempt so the peer
1206 * realises we're connected. */
1216 /* If the connection created by this route didn't bind to the IP
1217 * address the route connected to, the connection/route matching
1218 * code below probably isn't going to work. */
1220 route->ksnr_ipaddr != conn->ksnc_ipaddr) {
1221 CERROR("Route %s %u.%u.%u.%u connected to %u.%u.%u.%u\n",
1222 libcfs_id2str(peer->ksnp_id),
1223 HIPQUAD(route->ksnr_ipaddr),
1224 HIPQUAD(conn->ksnc_ipaddr));
1227 /* Search for a route corresponding to the new connection and
1228 * create an association. This allows incoming connections created
1229 * by routes in my peer to match my own route entries so I don't
1230 * continually create duplicate routes. */
1231 list_for_each (tmp, &peer->ksnp_routes) {
1232 route = list_entry(tmp, ksock_route_t, ksnr_list);
1234 if (route->ksnr_ipaddr != conn->ksnc_ipaddr)
1237 ksocknal_associate_route_conn_locked(route, conn);
1241 conn->ksnc_peer = peer; /* conn takes my ref on peer */
1242 peer->ksnp_last_alive = cfs_time_current();
1243 peer->ksnp_error = 0;
1245 sched = ksocknal_choose_scheduler_locked (irq);
1246 sched->kss_nconns++;
1247 conn->ksnc_scheduler = sched;
1249 /* Set the deadline for the outgoing HELLO to drain */
1250 conn->ksnc_tx_bufnob = SOCK_WMEM_QUEUED(sock);
1251 conn->ksnc_tx_deadline = cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
1252 mb(); /* order with adding to peer's conn list */
1254 list_add (&conn->ksnc_list, &peer->ksnp_conns);
1255 ksocknal_conn_addref(conn);
1257 ksocknal_new_packet(conn, 0);
1259 /* Take all the packets blocking for a connection.
1260 * NB, it might be nicer to share these blocked packets among any
1261 * other connections that are becoming established. */
1262 while (!list_empty (&peer->ksnp_tx_queue)) {
1263 tx = list_entry (peer->ksnp_tx_queue.next,
1264 ksock_tx_t, tx_list);
1266 list_del (&tx->tx_list);
1267 ksocknal_queue_tx_locked (tx, conn);
1270 write_unlock_bh (global_lock);
1272 /* We've now got a new connection. Any errors from here on are just
1273 * like "normal" comms errors and we close the connection normally.
1274 * NB (a) we still have to send the reply HELLO for passive
1276 * (b) normal I/O on the conn is blocked until I setup and call the
1280 ksocknal_lib_bind_irq (irq);
1282 CDEBUG(D_NET, "New conn %s p %d.x %u.%u.%u.%u -> %u.%u.%u.%u/%d"
1283 " incarnation:"LPD64" sched[%d]/%d\n",
1284 libcfs_id2str(peerid), conn->ksnc_proto->pro_version,
1285 HIPQUAD(conn->ksnc_myipaddr), HIPQUAD(conn->ksnc_ipaddr),
1286 conn->ksnc_port, incarnation,
1287 (int)(conn->ksnc_scheduler - ksocknal_data.ksnd_schedulers), irq);
1290 /* additional routes after interface exchange? */
1291 ksocknal_create_routes(peer, conn->ksnc_port,
1292 hello->kshm_ips, hello->kshm_nips);
1294 hello->kshm_nips = ksocknal_select_ips(peer, hello->kshm_ips,
1296 rc = ksocknal_send_hello(ni, conn, peerid.nid, hello);
1299 LIBCFS_FREE(hello, offsetof(ksock_hello_msg_t,
1300 kshm_ips[LNET_MAX_INTERFACES]));
1302 /* setup the socket AFTER I've received hello (it disables
1303 * SO_LINGER). I might call back to the acceptor who may want
1304 * to send a protocol version response and then close the
1305 * socket; this ensures the socket only tears down after the
1306 * response has been sent. */
1308 rc = ksocknal_lib_setup_sock(sock);
1310 write_lock_bh(global_lock);
1312 /* NB my callbacks block while I hold ksnd_global_lock */
1313 ksocknal_lib_set_callback(sock, conn);
1316 peer->ksnp_accepting--;
1318 write_unlock_bh(global_lock);
1321 write_lock_bh(global_lock);
1322 ksocknal_close_conn_locked(conn, rc);
1323 write_unlock_bh(global_lock);
1324 } else if (ksocknal_connsock_addref(conn) == 0) {
1325 /* Allow I/O to proceed. */
1326 ksocknal_read_callback(conn);
1327 ksocknal_write_callback(conn);
1328 ksocknal_connsock_decref(conn);
1331 ksocknal_conn_decref(conn);
1335 if (!peer->ksnp_closing &&
1336 list_empty (&peer->ksnp_conns) &&
1337 list_empty (&peer->ksnp_routes)) {
1338 list_add(&zombies, &peer->ksnp_tx_queue);
1339 list_del_init(&peer->ksnp_tx_queue);
1340 ksocknal_unlink_peer_locked(peer);
1343 write_unlock_bh (global_lock);
1347 CERROR("Not creating conn %s type %d: %s\n",
1348 libcfs_id2str(peerid), conn->ksnc_type, warn);
1350 CDEBUG(D_NET, "Not creating conn %s type %d: %s\n",
1351 libcfs_id2str(peerid), conn->ksnc_type, warn);
1356 /* Request retry by replying with CONN_NONE
1357 * ksnc_proto has been set already */
1358 conn->ksnc_type = SOCKLND_CONN_NONE;
1359 hello->kshm_nips = 0;
1360 ksocknal_send_hello(ni, conn, peerid.nid, hello);
1363 write_lock_bh(global_lock);
1364 peer->ksnp_accepting--;
1365 write_unlock_bh(global_lock);
1368 ksocknal_txlist_done(ni, &zombies, 1);
1369 ksocknal_peer_decref(peer);
1373 LIBCFS_FREE(hello, offsetof(ksock_hello_msg_t,
1374 kshm_ips[LNET_MAX_INTERFACES]));
1376 LIBCFS_FREE (conn, sizeof(*conn));
1379 libcfs_sock_release(sock);
1384 ksocknal_close_conn_locked (ksock_conn_t *conn, int error)
1386 /* This just does the immmediate housekeeping, and queues the
1387 * connection for the reaper to terminate.
1388 * Caller holds ksnd_global_lock exclusively in irq context */
1389 ksock_peer_t *peer = conn->ksnc_peer;
1390 ksock_route_t *route;
1391 ksock_conn_t *conn2;
1392 struct list_head *tmp;
1394 LASSERT (peer->ksnp_error == 0);
1395 LASSERT (!conn->ksnc_closing);
1396 conn->ksnc_closing = 1;
1398 /* ksnd_deathrow_conns takes over peer's ref */
1399 list_del (&conn->ksnc_list);
1401 route = conn->ksnc_route;
1402 if (route != NULL) {
1403 /* dissociate conn from route... */
1404 LASSERT (!route->ksnr_deleted);
1405 LASSERT ((route->ksnr_connected & (1 << conn->ksnc_type)) != 0);
1408 list_for_each(tmp, &peer->ksnp_conns) {
1409 conn2 = list_entry(tmp, ksock_conn_t, ksnc_list);
1411 if (conn2->ksnc_route == route &&
1412 conn2->ksnc_type == conn->ksnc_type)
1418 route->ksnr_connected &= ~(1 << conn->ksnc_type);
1420 conn->ksnc_route = NULL;
1422 #if 0 /* irrelevent with only eager routes */
1423 list_del (&route->ksnr_list); /* make route least favourite */
1424 list_add_tail (&route->ksnr_list, &peer->ksnp_routes);
1426 ksocknal_route_decref(route); /* drop conn's ref on route */
1429 if (list_empty (&peer->ksnp_conns)) {
1430 /* No more connections to this peer */
1432 peer->ksnp_proto = NULL; /* renegotiate protocol version */
1433 peer->ksnp_error = error; /* stash last conn close reason */
1435 if (list_empty (&peer->ksnp_routes)) {
1436 /* I've just closed last conn belonging to a
1437 * peer with no routes to it */
1438 ksocknal_unlink_peer_locked (peer);
1442 spin_lock_bh (&ksocknal_data.ksnd_reaper_lock);
1444 list_add_tail (&conn->ksnc_list, &ksocknal_data.ksnd_deathrow_conns);
1445 cfs_waitq_signal (&ksocknal_data.ksnd_reaper_waitq);
1447 spin_unlock_bh (&ksocknal_data.ksnd_reaper_lock);
1451 ksocknal_peer_failed (ksock_peer_t *peer)
1453 time_t last_alive = 0;
1456 /* There has been a connection failure or comms error; but I'll only
1457 * tell LNET I think the peer is dead if it's to another kernel and
1458 * there are no connections or connection attempts in existance. */
1460 read_lock (&ksocknal_data.ksnd_global_lock);
1462 if ((peer->ksnp_id.pid & LNET_PID_USERFLAG) == 0 &&
1463 list_empty(&peer->ksnp_conns) &&
1464 peer->ksnp_accepting == 0 &&
1465 ksocknal_find_connecting_route_locked(peer) == NULL) {
1467 last_alive = cfs_time_current_sec() -
1468 cfs_duration_sec(cfs_time_current() -
1469 peer->ksnp_last_alive);
1472 read_unlock (&ksocknal_data.ksnd_global_lock);
1475 lnet_notify (peer->ksnp_ni, peer->ksnp_id.nid, 0,
1480 ksocknal_terminate_conn (ksock_conn_t *conn)
1482 /* This gets called by the reaper (guaranteed thread context) to
1483 * disengage the socket from its callbacks and close it.
1484 * ksnc_refcount will eventually hit zero, and then the reaper will
1486 ksock_peer_t *peer = conn->ksnc_peer;
1487 ksock_sched_t *sched = conn->ksnc_scheduler;
1489 struct list_head *tmp;
1490 struct list_head *nxt;
1494 LASSERT(conn->ksnc_closing);
1496 /* wake up the scheduler to "send" all remaining packets to /dev/null */
1497 spin_lock_bh (&sched->kss_lock);
1499 if (!conn->ksnc_tx_scheduled &&
1500 !list_empty(&conn->ksnc_tx_queue)){
1501 list_add_tail (&conn->ksnc_tx_list,
1502 &sched->kss_tx_conns);
1503 /* a closing conn is always ready to tx */
1504 conn->ksnc_tx_ready = 1;
1505 conn->ksnc_tx_scheduled = 1;
1506 /* extra ref for scheduler */
1507 ksocknal_conn_addref(conn);
1509 cfs_waitq_signal (&sched->kss_waitq);
1512 spin_unlock_bh (&sched->kss_lock);
1514 spin_lock(&peer->ksnp_lock);
1516 list_for_each_safe(tmp, nxt, &peer->ksnp_zc_req_list) {
1517 tx = list_entry(tmp, ksock_tx_t, tx_zc_list);
1519 if (tx->tx_conn != conn)
1522 LASSERT (tx->tx_msg.ksm_zc_req_cookie != 0);
1524 tx->tx_msg.ksm_zc_req_cookie = 0;
1525 list_del(&tx->tx_zc_list);
1526 list_add(&tx->tx_zc_list, &zlist);
1529 spin_unlock(&peer->ksnp_lock);
1531 list_for_each_safe(tmp, nxt, &zlist) {
1532 tx = list_entry(tmp, ksock_tx_t, tx_zc_list);
1534 list_del(&tx->tx_zc_list);
1535 ksocknal_tx_decref(tx);
1538 /* serialise with callbacks */
1539 write_lock_bh (&ksocknal_data.ksnd_global_lock);
1541 ksocknal_lib_reset_callback(conn->ksnc_sock, conn);
1543 /* OK, so this conn may not be completely disengaged from its
1544 * scheduler yet, but it _has_ committed to terminate... */
1545 conn->ksnc_scheduler->kss_nconns--;
1547 if (peer->ksnp_error != 0) {
1548 /* peer's last conn closed in error */
1549 LASSERT (list_empty (&peer->ksnp_conns));
1551 peer->ksnp_error = 0; /* avoid multiple notifications */
1554 write_unlock_bh (&ksocknal_data.ksnd_global_lock);
1557 ksocknal_peer_failed(peer);
1559 /* The socket is closed on the final put; either here, or in
1560 * ksocknal_{send,recv}msg(). Since we set up the linger2 option
1561 * when the connection was established, this will close the socket
1562 * immediately, aborting anything buffered in it. Any hung
1563 * zero-copy transmits will therefore complete in finite time. */
1564 ksocknal_connsock_decref(conn);
1568 ksocknal_queue_zombie_conn (ksock_conn_t *conn)
1570 /* Queue the conn for the reaper to destroy */
1572 LASSERT (atomic_read(&conn->ksnc_conn_refcount) == 0);
1573 spin_lock_bh (&ksocknal_data.ksnd_reaper_lock);
1575 list_add_tail(&conn->ksnc_list, &ksocknal_data.ksnd_zombie_conns);
1576 cfs_waitq_signal(&ksocknal_data.ksnd_reaper_waitq);
1578 spin_unlock_bh (&ksocknal_data.ksnd_reaper_lock);
1582 ksocknal_destroy_conn (ksock_conn_t *conn)
1584 /* Final coup-de-grace of the reaper */
1585 CDEBUG (D_NET, "connection %p\n", conn);
1587 LASSERT (atomic_read (&conn->ksnc_conn_refcount) == 0);
1588 LASSERT (atomic_read (&conn->ksnc_sock_refcount) == 0);
1589 LASSERT (conn->ksnc_sock == NULL);
1590 LASSERT (conn->ksnc_route == NULL);
1591 LASSERT (!conn->ksnc_tx_scheduled);
1592 LASSERT (!conn->ksnc_rx_scheduled);
1593 LASSERT (list_empty(&conn->ksnc_tx_queue));
1595 /* complete current receive if any */
1596 switch (conn->ksnc_rx_state) {
1597 case SOCKNAL_RX_LNET_PAYLOAD:
1598 CERROR("Completing partial receive from %s"
1599 ", ip %d.%d.%d.%d:%d, with error\n",
1600 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1601 HIPQUAD(conn->ksnc_ipaddr), conn->ksnc_port);
1602 lnet_finalize (conn->ksnc_peer->ksnp_ni,
1603 conn->ksnc_cookie, -EIO);
1605 case SOCKNAL_RX_LNET_HEADER:
1606 if (conn->ksnc_rx_started)
1607 CERROR("Incomplete receive of lnet header from %s"
1608 ", ip %d.%d.%d.%d:%d, with error, protocol: %d.x.\n",
1609 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1610 HIPQUAD(conn->ksnc_ipaddr), conn->ksnc_port,
1611 conn->ksnc_proto->pro_version);
1613 case SOCKNAL_RX_KSM_HEADER:
1614 if (conn->ksnc_rx_started)
1615 CERROR("Incomplete receive of ksock message 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_SLOP:
1622 if (conn->ksnc_rx_started)
1623 CERROR("Incomplete receive of slops from %s"
1624 ", ip %d.%d.%d.%d:%d, with error\n",
1625 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1626 HIPQUAD(conn->ksnc_ipaddr), conn->ksnc_port);
1633 ksocknal_peer_decref(conn->ksnc_peer);
1635 LIBCFS_FREE (conn, sizeof (*conn));
1639 ksocknal_close_peer_conns_locked (ksock_peer_t *peer, __u32 ipaddr, int why)
1642 struct list_head *ctmp;
1643 struct list_head *cnxt;
1646 list_for_each_safe (ctmp, cnxt, &peer->ksnp_conns) {
1647 conn = list_entry (ctmp, ksock_conn_t, ksnc_list);
1650 conn->ksnc_ipaddr == ipaddr) {
1652 ksocknal_close_conn_locked (conn, why);
1660 ksocknal_close_conn_and_siblings (ksock_conn_t *conn, int why)
1662 ksock_peer_t *peer = conn->ksnc_peer;
1663 __u32 ipaddr = conn->ksnc_ipaddr;
1666 write_lock_bh (&ksocknal_data.ksnd_global_lock);
1668 count = ksocknal_close_peer_conns_locked (peer, ipaddr, why);
1670 write_unlock_bh (&ksocknal_data.ksnd_global_lock);
1676 ksocknal_close_matching_conns (lnet_process_id_t id, __u32 ipaddr)
1679 struct list_head *ptmp;
1680 struct list_head *pnxt;
1686 write_lock_bh (&ksocknal_data.ksnd_global_lock);
1688 if (id.nid != LNET_NID_ANY)
1689 lo = hi = ksocknal_nid2peerlist(id.nid) - ksocknal_data.ksnd_peers;
1692 hi = ksocknal_data.ksnd_peer_hash_size - 1;
1695 for (i = lo; i <= hi; i++) {
1696 list_for_each_safe (ptmp, pnxt, &ksocknal_data.ksnd_peers[i]) {
1698 peer = list_entry (ptmp, ksock_peer_t, ksnp_list);
1700 if (!((id.nid == LNET_NID_ANY || id.nid == peer->ksnp_id.nid) &&
1701 (id.pid == LNET_PID_ANY || id.pid == peer->ksnp_id.pid)))
1704 count += ksocknal_close_peer_conns_locked (peer, ipaddr, 0);
1708 write_unlock_bh (&ksocknal_data.ksnd_global_lock);
1710 /* wildcards always succeed */
1711 if (id.nid == LNET_NID_ANY || id.pid == LNET_PID_ANY || ipaddr == 0)
1714 return (count == 0 ? -ENOENT : 0);
1718 ksocknal_notify (lnet_ni_t *ni, lnet_nid_t gw_nid, int alive)
1720 /* The router is telling me she's been notified of a change in
1721 * gateway state.... */
1722 lnet_process_id_t id = {.nid = gw_nid, .pid = LNET_PID_ANY};
1724 CDEBUG (D_NET, "gw %s %s\n", libcfs_nid2str(gw_nid),
1725 alive ? "up" : "down");
1728 /* If the gateway crashed, close all open connections... */
1729 ksocknal_close_matching_conns (id, 0);
1733 /* ...otherwise do nothing. We can only establish new connections
1734 * if we have autroutes, and these connect on demand. */
1738 ksocknal_push_peer (ksock_peer_t *peer)
1742 struct list_head *tmp;
1745 for (index = 0; ; index++) {
1746 read_lock (&ksocknal_data.ksnd_global_lock);
1751 list_for_each (tmp, &peer->ksnp_conns) {
1753 conn = list_entry (tmp, ksock_conn_t, ksnc_list);
1754 ksocknal_conn_addref(conn);
1759 read_unlock (&ksocknal_data.ksnd_global_lock);
1764 ksocknal_lib_push_conn (conn);
1765 ksocknal_conn_decref(conn);
1770 ksocknal_push (lnet_ni_t *ni, lnet_process_id_t id)
1773 struct list_head *tmp;
1779 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
1780 for (j = 0; ; j++) {
1781 read_lock (&ksocknal_data.ksnd_global_lock);
1786 list_for_each (tmp, &ksocknal_data.ksnd_peers[i]) {
1787 peer = list_entry(tmp, ksock_peer_t,
1790 if (!((id.nid == LNET_NID_ANY ||
1791 id.nid == peer->ksnp_id.nid) &&
1792 (id.pid == LNET_PID_ANY ||
1793 id.pid == peer->ksnp_id.pid))) {
1799 ksocknal_peer_addref(peer);
1804 read_unlock (&ksocknal_data.ksnd_global_lock);
1808 ksocknal_push_peer (peer);
1809 ksocknal_peer_decref(peer);
1819 ksocknal_add_interface(lnet_ni_t *ni, __u32 ipaddress, __u32 netmask)
1821 ksock_net_t *net = ni->ni_data;
1822 ksock_interface_t *iface;
1826 struct list_head *ptmp;
1828 struct list_head *rtmp;
1829 ksock_route_t *route;
1831 if (ipaddress == 0 ||
1835 write_lock_bh (&ksocknal_data.ksnd_global_lock);
1837 iface = ksocknal_ip2iface(ni, ipaddress);
1838 if (iface != NULL) {
1839 /* silently ignore dups */
1841 } else if (net->ksnn_ninterfaces == LNET_MAX_INTERFACES) {
1844 iface = &net->ksnn_interfaces[net->ksnn_ninterfaces++];
1846 iface->ksni_ipaddr = ipaddress;
1847 iface->ksni_netmask = netmask;
1848 iface->ksni_nroutes = 0;
1849 iface->ksni_npeers = 0;
1851 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
1852 list_for_each(ptmp, &ksocknal_data.ksnd_peers[i]) {
1853 peer = list_entry(ptmp, ksock_peer_t, ksnp_list);
1855 for (j = 0; j < peer->ksnp_n_passive_ips; j++)
1856 if (peer->ksnp_passive_ips[j] == ipaddress)
1857 iface->ksni_npeers++;
1859 list_for_each(rtmp, &peer->ksnp_routes) {
1860 route = list_entry(rtmp, ksock_route_t, ksnr_list);
1862 if (route->ksnr_myipaddr == ipaddress)
1863 iface->ksni_nroutes++;
1869 /* NB only new connections will pay attention to the new interface! */
1872 write_unlock_bh (&ksocknal_data.ksnd_global_lock);
1878 ksocknal_peer_del_interface_locked(ksock_peer_t *peer, __u32 ipaddr)
1880 struct list_head *tmp;
1881 struct list_head *nxt;
1882 ksock_route_t *route;
1887 for (i = 0; i < peer->ksnp_n_passive_ips; i++)
1888 if (peer->ksnp_passive_ips[i] == ipaddr) {
1889 for (j = i+1; j < peer->ksnp_n_passive_ips; j++)
1890 peer->ksnp_passive_ips[j-1] =
1891 peer->ksnp_passive_ips[j];
1892 peer->ksnp_n_passive_ips--;
1896 list_for_each_safe(tmp, nxt, &peer->ksnp_routes) {
1897 route = list_entry (tmp, ksock_route_t, ksnr_list);
1899 if (route->ksnr_myipaddr != ipaddr)
1902 if (route->ksnr_share_count != 0) {
1903 /* Manually created; keep, but unbind */
1904 route->ksnr_myipaddr = 0;
1906 ksocknal_del_route_locked(route);
1910 list_for_each_safe(tmp, nxt, &peer->ksnp_conns) {
1911 conn = list_entry(tmp, ksock_conn_t, ksnc_list);
1913 if (conn->ksnc_myipaddr == ipaddr)
1914 ksocknal_close_conn_locked (conn, 0);
1919 ksocknal_del_interface(lnet_ni_t *ni, __u32 ipaddress)
1921 ksock_net_t *net = ni->ni_data;
1923 struct list_head *tmp;
1924 struct list_head *nxt;
1930 write_lock_bh (&ksocknal_data.ksnd_global_lock);
1932 for (i = 0; i < net->ksnn_ninterfaces; i++) {
1933 this_ip = net->ksnn_interfaces[i].ksni_ipaddr;
1935 if (!(ipaddress == 0 ||
1936 ipaddress == this_ip))
1941 for (j = i+1; j < net->ksnn_ninterfaces; j++)
1942 net->ksnn_interfaces[j-1] =
1943 net->ksnn_interfaces[j];
1945 net->ksnn_ninterfaces--;
1947 for (j = 0; j < ksocknal_data.ksnd_peer_hash_size; j++) {
1948 list_for_each_safe(tmp, nxt, &ksocknal_data.ksnd_peers[j]) {
1949 peer = list_entry(tmp, ksock_peer_t, ksnp_list);
1951 if (peer->ksnp_ni != ni)
1954 ksocknal_peer_del_interface_locked(peer, this_ip);
1959 write_unlock_bh (&ksocknal_data.ksnd_global_lock);
1965 ksocknal_ctl(lnet_ni_t *ni, unsigned int cmd, void *arg)
1967 struct libcfs_ioctl_data *data = arg;
1971 case IOC_LIBCFS_GET_INTERFACE: {
1972 ksock_net_t *net = ni->ni_data;
1973 ksock_interface_t *iface;
1975 read_lock (&ksocknal_data.ksnd_global_lock);
1977 if (data->ioc_count < 0 ||
1978 data->ioc_count >= net->ksnn_ninterfaces) {
1982 iface = &net->ksnn_interfaces[data->ioc_count];
1984 data->ioc_u32[0] = iface->ksni_ipaddr;
1985 data->ioc_u32[1] = iface->ksni_netmask;
1986 data->ioc_u32[2] = iface->ksni_npeers;
1987 data->ioc_u32[3] = iface->ksni_nroutes;
1990 read_unlock (&ksocknal_data.ksnd_global_lock);
1994 case IOC_LIBCFS_ADD_INTERFACE:
1995 return ksocknal_add_interface(ni,
1996 data->ioc_u32[0], /* IP address */
1997 data->ioc_u32[1]); /* net mask */
1999 case IOC_LIBCFS_DEL_INTERFACE:
2000 return ksocknal_del_interface(ni,
2001 data->ioc_u32[0]); /* IP address */
2003 case IOC_LIBCFS_GET_PEER: {
2004 lnet_process_id_t id = {0,};
2009 int share_count = 0;
2011 rc = ksocknal_get_peer_info(ni, data->ioc_count,
2012 &id, &myip, &ip, &port,
2013 &conn_count, &share_count);
2017 data->ioc_nid = id.nid;
2018 data->ioc_count = share_count;
2019 data->ioc_u32[0] = ip;
2020 data->ioc_u32[1] = port;
2021 data->ioc_u32[2] = myip;
2022 data->ioc_u32[3] = conn_count;
2023 data->ioc_u32[4] = id.pid;
2027 case IOC_LIBCFS_ADD_PEER: {
2028 lnet_process_id_t id = {.nid = data->ioc_nid,
2029 .pid = LUSTRE_SRV_LNET_PID};
2030 return ksocknal_add_peer (ni, id,
2031 data->ioc_u32[0], /* IP */
2032 data->ioc_u32[1]); /* port */
2034 case IOC_LIBCFS_DEL_PEER: {
2035 lnet_process_id_t id = {.nid = data->ioc_nid,
2036 .pid = LNET_PID_ANY};
2037 return ksocknal_del_peer (ni, id,
2038 data->ioc_u32[0]); /* IP */
2040 case IOC_LIBCFS_GET_CONN: {
2044 ksock_conn_t *conn = ksocknal_get_conn_by_idx (ni, data->ioc_count);
2049 ksocknal_lib_get_conn_tunables(conn, &txmem, &rxmem, &nagle);
2051 data->ioc_count = txmem;
2052 data->ioc_nid = conn->ksnc_peer->ksnp_id.nid;
2053 data->ioc_flags = nagle;
2054 data->ioc_u32[0] = conn->ksnc_ipaddr;
2055 data->ioc_u32[1] = conn->ksnc_port;
2056 data->ioc_u32[2] = conn->ksnc_myipaddr;
2057 data->ioc_u32[3] = conn->ksnc_type;
2058 data->ioc_u32[4] = conn->ksnc_scheduler -
2059 ksocknal_data.ksnd_schedulers;
2060 data->ioc_u32[5] = rxmem;
2061 data->ioc_u32[6] = conn->ksnc_peer->ksnp_id.pid;
2062 ksocknal_conn_decref(conn);
2066 case IOC_LIBCFS_CLOSE_CONNECTION: {
2067 lnet_process_id_t id = {.nid = data->ioc_nid,
2068 .pid = LNET_PID_ANY};
2070 return ksocknal_close_matching_conns (id,
2073 case IOC_LIBCFS_REGISTER_MYNID:
2074 /* Ignore if this is a noop */
2075 if (data->ioc_nid == ni->ni_nid)
2078 CERROR("obsolete IOC_LIBCFS_REGISTER_MYNID: %s(%s)\n",
2079 libcfs_nid2str(data->ioc_nid),
2080 libcfs_nid2str(ni->ni_nid));
2083 case IOC_LIBCFS_PUSH_CONNECTION: {
2084 lnet_process_id_t id = {.nid = data->ioc_nid,
2085 .pid = LNET_PID_ANY};
2087 return ksocknal_push(ni, id);
2096 ksocknal_free_buffers (void)
2098 LASSERT (atomic_read(&ksocknal_data.ksnd_nactive_txs) == 0);
2100 if (ksocknal_data.ksnd_schedulers != NULL)
2101 LIBCFS_FREE (ksocknal_data.ksnd_schedulers,
2102 sizeof (ksock_sched_t) * ksocknal_data.ksnd_nschedulers);
2104 LIBCFS_FREE (ksocknal_data.ksnd_peers,
2105 sizeof (struct list_head) *
2106 ksocknal_data.ksnd_peer_hash_size);
2108 spin_lock(&ksocknal_data.ksnd_tx_lock);
2110 if (!list_empty(&ksocknal_data.ksnd_idle_noop_txs)) {
2111 struct list_head zlist;
2114 list_add(&zlist, &ksocknal_data.ksnd_idle_noop_txs);
2115 list_del_init(&ksocknal_data.ksnd_idle_noop_txs);
2116 spin_unlock(&ksocknal_data.ksnd_tx_lock);
2118 while(!list_empty(&zlist)) {
2119 tx = list_entry(zlist.next, ksock_tx_t, tx_list);
2120 list_del(&tx->tx_list);
2121 LIBCFS_FREE(tx, tx->tx_desc_size);
2124 spin_unlock(&ksocknal_data.ksnd_tx_lock);
2129 ksocknal_base_shutdown (void)
2131 ksock_sched_t *sched;
2134 CDEBUG(D_MALLOC, "before NAL cleanup: kmem %d\n",
2135 atomic_read (&libcfs_kmemory));
2136 LASSERT (ksocknal_data.ksnd_nnets == 0);
2138 switch (ksocknal_data.ksnd_init) {
2142 case SOCKNAL_INIT_ALL:
2143 case SOCKNAL_INIT_DATA:
2144 LASSERT (ksocknal_data.ksnd_peers != NULL);
2145 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
2146 LASSERT (list_empty (&ksocknal_data.ksnd_peers[i]));
2148 LASSERT (list_empty (&ksocknal_data.ksnd_enomem_conns));
2149 LASSERT (list_empty (&ksocknal_data.ksnd_zombie_conns));
2150 LASSERT (list_empty (&ksocknal_data.ksnd_connd_connreqs));
2151 LASSERT (list_empty (&ksocknal_data.ksnd_connd_routes));
2153 if (ksocknal_data.ksnd_schedulers != NULL)
2154 for (i = 0; i < ksocknal_data.ksnd_nschedulers; i++) {
2155 ksock_sched_t *kss =
2156 &ksocknal_data.ksnd_schedulers[i];
2158 LASSERT (list_empty (&kss->kss_tx_conns));
2159 LASSERT (list_empty (&kss->kss_rx_conns));
2160 LASSERT (list_empty (&kss->kss_zombie_noop_txs));
2161 LASSERT (kss->kss_nconns == 0);
2164 /* flag threads to terminate; wake and wait for them to die */
2165 ksocknal_data.ksnd_shuttingdown = 1;
2166 cfs_waitq_broadcast (&ksocknal_data.ksnd_connd_waitq);
2167 cfs_waitq_broadcast (&ksocknal_data.ksnd_reaper_waitq);
2169 if (ksocknal_data.ksnd_schedulers != NULL)
2170 for (i = 0; i < ksocknal_data.ksnd_nschedulers; i++) {
2171 sched = &ksocknal_data.ksnd_schedulers[i];
2172 cfs_waitq_broadcast(&sched->kss_waitq);
2176 read_lock (&ksocknal_data.ksnd_global_lock);
2177 while (ksocknal_data.ksnd_nthreads != 0) {
2179 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET, /* power of 2? */
2180 "waiting for %d threads to terminate\n",
2181 ksocknal_data.ksnd_nthreads);
2182 read_unlock (&ksocknal_data.ksnd_global_lock);
2183 cfs_pause(cfs_time_seconds(1));
2184 read_lock (&ksocknal_data.ksnd_global_lock);
2186 read_unlock (&ksocknal_data.ksnd_global_lock);
2188 ksocknal_free_buffers();
2190 ksocknal_data.ksnd_init = SOCKNAL_INIT_NOTHING;
2194 CDEBUG(D_MALLOC, "after NAL cleanup: kmem %d\n",
2195 atomic_read (&libcfs_kmemory));
2197 PORTAL_MODULE_UNUSE;
2202 ksocknal_new_incarnation (void)
2206 /* The incarnation number is the time this module loaded and it
2207 * identifies this particular instance of the socknal. Hopefully
2208 * we won't be able to reboot more frequently than 1MHz for the
2209 * forseeable future :) */
2211 do_gettimeofday(&tv);
2213 return (((__u64)tv.tv_sec) * 1000000) + tv.tv_usec;
2217 ksocknal_base_startup (void)
2222 LASSERT (ksocknal_data.ksnd_init == SOCKNAL_INIT_NOTHING);
2223 LASSERT (ksocknal_data.ksnd_nnets == 0);
2225 memset (&ksocknal_data, 0, sizeof (ksocknal_data)); /* zero pointers */
2227 ksocknal_data.ksnd_peer_hash_size = SOCKNAL_PEER_HASH_SIZE;
2228 LIBCFS_ALLOC (ksocknal_data.ksnd_peers,
2229 sizeof (struct list_head) * ksocknal_data.ksnd_peer_hash_size);
2230 if (ksocknal_data.ksnd_peers == NULL)
2233 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++)
2234 CFS_INIT_LIST_HEAD(&ksocknal_data.ksnd_peers[i]);
2236 rwlock_init(&ksocknal_data.ksnd_global_lock);
2238 spin_lock_init (&ksocknal_data.ksnd_reaper_lock);
2239 CFS_INIT_LIST_HEAD (&ksocknal_data.ksnd_enomem_conns);
2240 CFS_INIT_LIST_HEAD (&ksocknal_data.ksnd_zombie_conns);
2241 CFS_INIT_LIST_HEAD (&ksocknal_data.ksnd_deathrow_conns);
2242 cfs_waitq_init(&ksocknal_data.ksnd_reaper_waitq);
2244 spin_lock_init (&ksocknal_data.ksnd_connd_lock);
2245 CFS_INIT_LIST_HEAD (&ksocknal_data.ksnd_connd_connreqs);
2246 CFS_INIT_LIST_HEAD (&ksocknal_data.ksnd_connd_routes);
2247 cfs_waitq_init(&ksocknal_data.ksnd_connd_waitq);
2249 spin_lock_init (&ksocknal_data.ksnd_tx_lock);
2250 CFS_INIT_LIST_HEAD (&ksocknal_data.ksnd_idle_noop_txs);
2252 /* NB memset above zeros whole of ksocknal_data, including
2253 * ksocknal_data.ksnd_irqinfo[all].ksni_valid */
2255 /* flag lists/ptrs/locks initialised */
2256 ksocknal_data.ksnd_init = SOCKNAL_INIT_DATA;
2259 ksocknal_data.ksnd_nschedulers = ksocknal_nsched();
2260 LIBCFS_ALLOC(ksocknal_data.ksnd_schedulers,
2261 sizeof(ksock_sched_t) * ksocknal_data.ksnd_nschedulers);
2262 if (ksocknal_data.ksnd_schedulers == NULL)
2265 for (i = 0; i < ksocknal_data.ksnd_nschedulers; i++) {
2266 ksock_sched_t *kss = &ksocknal_data.ksnd_schedulers[i];
2268 spin_lock_init (&kss->kss_lock);
2269 CFS_INIT_LIST_HEAD (&kss->kss_rx_conns);
2270 CFS_INIT_LIST_HEAD (&kss->kss_tx_conns);
2271 CFS_INIT_LIST_HEAD (&kss->kss_zombie_noop_txs);
2272 cfs_waitq_init (&kss->kss_waitq);
2275 for (i = 0; i < ksocknal_data.ksnd_nschedulers; i++) {
2276 rc = ksocknal_thread_start (ksocknal_scheduler,
2277 &ksocknal_data.ksnd_schedulers[i]);
2279 CERROR("Can't spawn socknal scheduler[%d]: %d\n",
2285 /* must have at least 2 connds to remain responsive to accepts while
2287 if (*ksocknal_tunables.ksnd_nconnds < 2)
2288 *ksocknal_tunables.ksnd_nconnds = 2;
2290 for (i = 0; i < *ksocknal_tunables.ksnd_nconnds; i++) {
2291 rc = ksocknal_thread_start (ksocknal_connd, (void *)((long)i));
2293 CERROR("Can't spawn socknal connd: %d\n", rc);
2298 rc = ksocknal_thread_start (ksocknal_reaper, NULL);
2300 CERROR ("Can't spawn socknal reaper: %d\n", rc);
2304 /* flag everything initialised */
2305 ksocknal_data.ksnd_init = SOCKNAL_INIT_ALL;
2310 ksocknal_base_shutdown();
2315 ksocknal_debug_peerhash (lnet_ni_t *ni)
2317 ksock_peer_t *peer = NULL;
2318 struct list_head *tmp;
2321 read_lock (&ksocknal_data.ksnd_global_lock);
2323 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
2324 list_for_each (tmp, &ksocknal_data.ksnd_peers[i]) {
2325 peer = list_entry (tmp, ksock_peer_t, ksnp_list);
2327 if (peer->ksnp_ni == ni) break;
2334 ksock_route_t *route;
2337 CWARN ("Active peer on shutdown: %s, ref %d, scnt %d, "
2338 "closing %d, accepting %d, err %d, zcookie "LPU64", "
2339 "txq %d, zc_req %d\n", libcfs_id2str(peer->ksnp_id),
2340 atomic_read(&peer->ksnp_refcount),
2341 peer->ksnp_sharecount, peer->ksnp_closing,
2342 peer->ksnp_accepting, peer->ksnp_error,
2343 peer->ksnp_zc_next_cookie,
2344 !list_empty(&peer->ksnp_tx_queue),
2345 !list_empty(&peer->ksnp_zc_req_list));
2347 list_for_each (tmp, &peer->ksnp_routes) {
2348 route = list_entry(tmp, ksock_route_t, ksnr_list);
2349 CWARN ("Route: ref %d, schd %d, conn %d, cnted %d, "
2350 "del %d\n", atomic_read(&route->ksnr_refcount),
2351 route->ksnr_scheduled, route->ksnr_connecting,
2352 route->ksnr_connected, route->ksnr_deleted);
2355 list_for_each (tmp, &peer->ksnp_conns) {
2356 conn = list_entry(tmp, ksock_conn_t, ksnc_list);
2357 CWARN ("Conn: ref %d, sref %d, t %d, c %d\n",
2358 atomic_read(&conn->ksnc_conn_refcount),
2359 atomic_read(&conn->ksnc_sock_refcount),
2360 conn->ksnc_type, conn->ksnc_closing);
2364 read_unlock (&ksocknal_data.ksnd_global_lock);
2369 ksocknal_shutdown (lnet_ni_t *ni)
2371 ksock_net_t *net = ni->ni_data;
2373 lnet_process_id_t anyid = {.nid = LNET_NID_ANY,
2374 .pid = LNET_PID_ANY};
2376 LASSERT(ksocknal_data.ksnd_init == SOCKNAL_INIT_ALL);
2377 LASSERT(ksocknal_data.ksnd_nnets > 0);
2379 spin_lock_bh (&net->ksnn_lock);
2380 net->ksnn_shutdown = 1; /* prevent new peers */
2381 spin_unlock_bh (&net->ksnn_lock);
2383 /* Delete all peers */
2384 ksocknal_del_peer(ni, anyid, 0);
2386 /* Wait for all peer state to clean up */
2388 spin_lock_bh (&net->ksnn_lock);
2389 while (net->ksnn_npeers != 0) {
2390 spin_unlock_bh (&net->ksnn_lock);
2393 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET, /* power of 2? */
2394 "waiting for %d peers to disconnect\n",
2396 cfs_pause(cfs_time_seconds(1));
2398 ksocknal_debug_peerhash(ni);
2400 spin_lock_bh (&net->ksnn_lock);
2402 spin_unlock_bh (&net->ksnn_lock);
2404 for (i = 0; i < net->ksnn_ninterfaces; i++) {
2405 LASSERT (net->ksnn_interfaces[i].ksni_npeers == 0);
2406 LASSERT (net->ksnn_interfaces[i].ksni_nroutes == 0);
2409 LIBCFS_FREE(net, sizeof(*net));
2411 ksocknal_data.ksnd_nnets--;
2412 if (ksocknal_data.ksnd_nnets == 0)
2413 ksocknal_base_shutdown();
2417 ksocknal_enumerate_interfaces(ksock_net_t *net)
2425 n = libcfs_ipif_enumerate(&names);
2427 CERROR("Can't enumerate interfaces: %d\n", n);
2431 for (i = j = 0; i < n; i++) {
2436 if (!strcmp(names[i], "lo")) /* skip the loopback IF */
2439 rc = libcfs_ipif_query(names[i], &up, &ip, &mask);
2441 CWARN("Can't get interface %s info: %d\n",
2447 CWARN("Ignoring interface %s (down)\n",
2452 if (j == LNET_MAX_INTERFACES) {
2453 CWARN("Ignoring interface %s (too many interfaces)\n",
2458 net->ksnn_interfaces[j].ksni_ipaddr = ip;
2459 net->ksnn_interfaces[j].ksni_netmask = mask;
2463 libcfs_ipif_free_enumeration(names, n);
2466 CERROR("Can't find any usable interfaces\n");
2472 ksocknal_startup (lnet_ni_t *ni)
2478 LASSERT (ni->ni_lnd == &the_ksocklnd);
2480 if (ksocknal_data.ksnd_init == SOCKNAL_INIT_NOTHING) {
2481 rc = ksocknal_base_startup();
2486 LIBCFS_ALLOC(net, sizeof(*net));
2490 memset(net, 0, sizeof(*net));
2491 spin_lock_init(&net->ksnn_lock);
2492 net->ksnn_incarnation = ksocknal_new_incarnation();
2494 ni->ni_maxtxcredits = *ksocknal_tunables.ksnd_credits;
2495 ni->ni_peertxcredits = *ksocknal_tunables.ksnd_peercredits;
2497 if (ni->ni_interfaces[0] == NULL) {
2498 rc = ksocknal_enumerate_interfaces(net);
2502 net->ksnn_ninterfaces = 1;
2504 for (i = 0; i < LNET_MAX_INTERFACES; i++) {
2507 if (ni->ni_interfaces[i] == NULL)
2510 rc = libcfs_ipif_query(
2511 ni->ni_interfaces[i], &up,
2512 &net->ksnn_interfaces[i].ksni_ipaddr,
2513 &net->ksnn_interfaces[i].ksni_netmask);
2516 CERROR("Can't get interface %s info: %d\n",
2517 ni->ni_interfaces[i], rc);
2522 CERROR("Interface %s is down\n",
2523 ni->ni_interfaces[i]);
2527 net->ksnn_ninterfaces = i;
2530 ni->ni_nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid),
2531 net->ksnn_interfaces[0].ksni_ipaddr);
2533 ksocknal_data.ksnd_nnets++;
2538 LIBCFS_FREE(net, sizeof(*net));
2540 if (ksocknal_data.ksnd_nnets == 0)
2541 ksocknal_base_shutdown();
2548 ksocknal_module_fini (void)
2550 lnet_unregister_lnd(&the_ksocklnd);
2551 ksocknal_lib_tunables_fini();
2555 ksocknal_module_init (void)
2559 /* check ksnr_connected/connecting field large enough */
2560 CLASSERT(SOCKLND_CONN_NTYPES <= 4);
2562 rc = ksocknal_lib_tunables_init();
2566 lnet_register_lnd(&the_ksocklnd);
2571 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2572 MODULE_DESCRIPTION("Kernel TCP Socket LND v2.0.0");
2573 MODULE_LICENSE("GPL");
2575 cfs_module(ksocknal, "2.0.0", ksocknal_module_init, ksocknal_module_fini);