Whamcloud - gitweb
* Landing 12014: ASSERTION failures when upgrading to the patchless zero-copy
[fs/lustre-release.git] / lnet / klnds / socklnd / socklnd.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
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>
9  *
10  *   This file is part of Portals, http://www.sf.net/projects/sandiaportals/
11  *
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.
15  *
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.
20  *
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.
24  */
25
26 #include "socklnd.h"
27
28 lnd_t the_ksocklnd = {
29         .lnd_type       = SOCKLND,
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,
37 };
38
39 ksock_nal_data_t        ksocknal_data;
40
41 ksock_interface_t *
42 ksocknal_ip2iface(lnet_ni_t *ni, __u32 ip)
43 {
44         ksock_net_t       *net = ni->ni_data;
45         int                i;
46         ksock_interface_t *iface;
47
48         for (i = 0; i < net->ksnn_ninterfaces; i++) {
49                 LASSERT(i < LNET_MAX_INTERFACES);
50                 iface = &net->ksnn_interfaces[i];
51
52                 if (iface->ksni_ipaddr == ip)
53                         return (iface);
54         }
55
56         return (NULL);
57 }
58
59 ksock_route_t *
60 ksocknal_create_route (__u32 ipaddr, int port)
61 {
62         ksock_route_t *route;
63
64         LIBCFS_ALLOC (route, sizeof (*route));
65         if (route == NULL)
66                 return (NULL);
67
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;
79
80         return (route);
81 }
82
83 void
84 ksocknal_destroy_route (ksock_route_t *route)
85 {
86         LASSERT (atomic_read(&route->ksnr_refcount) == 0);
87
88         if (route->ksnr_peer != NULL)
89                 ksocknal_peer_decref(route->ksnr_peer);
90
91         LIBCFS_FREE (route, sizeof (*route));
92 }
93
94 int
95 ksocknal_create_peer (ksock_peer_t **peerp, lnet_ni_t *ni, lnet_process_id_t id)
96 {
97         ksock_net_t   *net = ni->ni_data;
98         ksock_peer_t  *peer;
99
100         LASSERT (id.nid != LNET_NID_ANY);
101         LASSERT (id.pid != LNET_PID_ANY);
102         LASSERT (!in_interrupt());
103
104         LIBCFS_ALLOC (peer, sizeof (*peer));
105         if (peer == NULL)
106                 return -ENOMEM;
107
108         memset (peer, 0, sizeof (*peer));       /* NULL pointers/clear flags etc */
109
110         peer->ksnp_ni = ni;
111         peer->ksnp_id = id;
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);
122
123         spin_lock_bh (&net->ksnn_lock);
124
125         if (net->ksnn_shutdown) {
126                 spin_unlock_bh (&net->ksnn_lock);
127                 
128                 LIBCFS_FREE(peer, sizeof(*peer));
129                 CERROR("Can't create peer: network shutdown\n");
130                 return -ESHUTDOWN;
131         }
132
133         net->ksnn_npeers++;
134
135         spin_unlock_bh (&net->ksnn_lock);
136
137         *peerp = peer;
138         return 0;
139 }
140
141 void
142 ksocknal_destroy_peer (ksock_peer_t *peer)
143 {
144         ksock_net_t    *net = peer->ksnp_ni->ni_data;
145
146         CDEBUG (D_NET, "peer %s %p deleted\n", 
147                 libcfs_id2str(peer->ksnp_id), peer);
148
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));
155
156         LIBCFS_FREE (peer, sizeof (*peer));
157
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
161          * zero. */
162         spin_lock_bh (&net->ksnn_lock);
163         net->ksnn_npeers--;
164         spin_unlock_bh (&net->ksnn_lock);
165 }
166
167 ksock_peer_t *
168 ksocknal_find_peer_locked (lnet_ni_t *ni, lnet_process_id_t id)
169 {
170         struct list_head *peer_list = ksocknal_nid2peerlist(id.nid);
171         struct list_head *tmp;
172         ksock_peer_t     *peer;
173
174         list_for_each (tmp, peer_list) {
175
176                 peer = list_entry (tmp, ksock_peer_t, ksnp_list);
177
178                 LASSERT (!peer->ksnp_closing);
179
180                 if (peer->ksnp_ni != ni)
181                         continue;
182
183                 if (peer->ksnp_id.nid != id.nid ||
184                     peer->ksnp_id.pid != id.pid)
185                         continue;
186
187                 CDEBUG(D_NET, "got peer [%p] -> %s (%d)\n",
188                        peer, libcfs_id2str(id), 
189                        atomic_read(&peer->ksnp_refcount));
190                 return (peer);
191         }
192         return (NULL);
193 }
194
195 ksock_peer_t *
196 ksocknal_find_peer (lnet_ni_t *ni, lnet_process_id_t id)
197 {
198         ksock_peer_t     *peer;
199
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);
205
206         return (peer);
207 }
208
209 void
210 ksocknal_unlink_peer_locked (ksock_peer_t *peer)
211 {
212         int                i;
213         __u32              ip;
214
215         for (i = 0; i < peer->ksnp_n_passive_ips; i++) {
216                 LASSERT (i < LNET_MAX_INTERFACES);
217                 ip = peer->ksnp_passive_ips[i];
218
219                 ksocknal_ip2iface(peer->ksnp_ni, ip)->ksni_npeers--;
220         }
221
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);
229 }
230
231 int
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)
235 {
236         ksock_peer_t      *peer;
237         struct list_head  *ptmp;
238         ksock_route_t     *route;
239         struct list_head  *rtmp;
240         int                i;
241         int                j;
242         int                rc = -ENOENT;
243
244         read_lock (&ksocknal_data.ksnd_global_lock);
245
246         for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
247
248                 list_for_each (ptmp, &ksocknal_data.ksnd_peers[i]) {
249                         peer = list_entry (ptmp, ksock_peer_t, ksnp_list);
250
251                         if (peer->ksnp_ni != ni)
252                                 continue;
253
254                         if (peer->ksnp_n_passive_ips == 0 &&
255                             list_empty(&peer->ksnp_routes)) {
256                                 if (index-- > 0)
257                                         continue;
258
259                                 *id = peer->ksnp_id;
260                                 *myip = 0;
261                                 *peer_ip = 0;
262                                 *port = 0;
263                                 *conn_count = 0;
264                                 *share_count = 0;
265                                 rc = 0;
266                                 goto out;
267                         }
268
269                         for (j = 0; j < peer->ksnp_n_passive_ips; j++) {
270                                 if (index-- > 0)
271                                         continue;
272
273                                 *id = peer->ksnp_id;
274                                 *myip = peer->ksnp_passive_ips[j];
275                                 *peer_ip = 0;
276                                 *port = 0;
277                                 *conn_count = 0;
278                                 *share_count = 0;
279                                 rc = 0;
280                                 goto out;
281                         }
282
283                         list_for_each (rtmp, &peer->ksnp_routes) {
284                                 if (index-- > 0)
285                                         continue;
286
287                                 route = list_entry(rtmp, ksock_route_t,
288                                                    ksnr_list);
289
290                                 *id = peer->ksnp_id;
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;
296                                 rc = 0;
297                                 goto out;
298                         }
299                 }
300         }
301  out:
302         read_unlock (&ksocknal_data.ksnd_global_lock);
303         return (rc);
304 }
305
306 void
307 ksocknal_associate_route_conn_locked(ksock_route_t *route, ksock_conn_t *conn)
308 {
309         ksock_peer_t      *peer = route->ksnr_peer;
310         int                type = conn->ksnc_type;
311         ksock_interface_t *iface;
312
313         conn->ksnc_route = route;
314         ksocknal_route_addref(route);
315
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));
323                 } else {
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));
330
331                         iface = ksocknal_ip2iface(route->ksnr_peer->ksnp_ni,
332                                                   route->ksnr_myipaddr);
333                         if (iface != NULL)
334                                 iface->ksni_nroutes--;
335                 }
336                 route->ksnr_myipaddr = conn->ksnc_myipaddr;
337                 iface = ksocknal_ip2iface(route->ksnr_peer->ksnp_ni,
338                                           route->ksnr_myipaddr);
339                 if (iface != NULL)
340                         iface->ksni_nroutes++;
341         }
342
343         route->ksnr_connected |= (1<<type);
344         route->ksnr_conn_count++;
345
346         /* Successful connection => further attempts can
347          * proceed immediately */
348         route->ksnr_retry_interval = 0;
349 }
350
351 void
352 ksocknal_add_route_locked (ksock_peer_t *peer, ksock_route_t *route)
353 {
354         struct list_head  *tmp;
355         ksock_conn_t      *conn;
356         ksock_route_t     *route2;
357
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);
363
364         /* LASSERT(unique) */
365         list_for_each(tmp, &peer->ksnp_routes) {
366                 route2 = list_entry(tmp, ksock_route_t, ksnr_list);
367
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));
372                         LBUG();
373                 }
374         }
375
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);
380
381         list_for_each(tmp, &peer->ksnp_conns) {
382                 conn = list_entry(tmp, ksock_conn_t, ksnc_list);
383
384                 if (conn->ksnc_ipaddr != route->ksnr_ipaddr)
385                         continue;
386
387                 ksocknal_associate_route_conn_locked(route, conn);
388                 /* keep going (typed routes) */
389         }
390 }
391
392 void
393 ksocknal_del_route_locked (ksock_route_t *route)
394 {
395         ksock_peer_t      *peer = route->ksnr_peer;
396         ksock_interface_t *iface;
397         ksock_conn_t      *conn;
398         struct list_head  *ctmp;
399         struct list_head  *cnxt;
400
401         LASSERT (!route->ksnr_deleted);
402
403         /* Close associated conns */
404         list_for_each_safe (ctmp, cnxt, &peer->ksnp_conns) {
405                 conn = list_entry(ctmp, ksock_conn_t, ksnc_list);
406
407                 if (conn->ksnc_route != route)
408                         continue;
409
410                 ksocknal_close_conn_locked (conn, 0);
411         }
412
413         if (route->ksnr_myipaddr != 0) {
414                 iface = ksocknal_ip2iface(route->ksnr_peer->ksnp_ni,
415                                           route->ksnr_myipaddr);
416                 if (iface != NULL)
417                         iface->ksni_nroutes--;
418         }
419
420         route->ksnr_deleted = 1;
421         list_del (&route->ksnr_list);
422         ksocknal_route_decref(route);             /* drop peer's ref */
423
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
427                  * connections */
428                 ksocknal_unlink_peer_locked (peer);
429         }
430 }
431
432 int
433 ksocknal_add_peer (lnet_ni_t *ni, lnet_process_id_t id, __u32 ipaddr, int port)
434 {
435         struct list_head  *tmp;
436         ksock_peer_t      *peer;
437         ksock_peer_t      *peer2;
438         ksock_route_t     *route;
439         ksock_route_t     *route2;
440         int                rc;
441
442         if (id.nid == LNET_NID_ANY ||
443             id.pid == LNET_PID_ANY)
444                 return (-EINVAL);
445
446         /* Have a brand new peer ready... */
447         rc = ksocknal_create_peer(&peer, ni, id);
448         if (rc != 0)
449                 return rc;
450
451         route = ksocknal_create_route (ipaddr, port);
452         if (route == NULL) {
453                 ksocknal_peer_decref(peer);
454                 return (-ENOMEM);
455         }
456
457         write_lock_bh (&ksocknal_data.ksnd_global_lock);
458
459         peer2 = ksocknal_find_peer_locked (ni, id);
460         if (peer2 != NULL) {
461                 ksocknal_peer_decref(peer);
462                 peer = peer2;
463         } else {
464                 /* peer table takes my ref on peer */
465                 list_add_tail (&peer->ksnp_list,
466                                ksocknal_nid2peerlist (id.nid));
467         }
468
469         route2 = NULL;
470         list_for_each (tmp, &peer->ksnp_routes) {
471                 route2 = list_entry(tmp, ksock_route_t, ksnr_list);
472
473                 if (route2->ksnr_ipaddr == ipaddr)
474                         break;
475
476                 route2 = NULL;
477         }
478         if (route2 == NULL) {
479                 ksocknal_add_route_locked(peer, route);
480                 route->ksnr_share_count++;
481         } else {
482                 ksocknal_route_decref(route);
483                 route2->ksnr_share_count++;
484         }
485
486         write_unlock_bh (&ksocknal_data.ksnd_global_lock);
487
488         return (0);
489 }
490
491 void
492 ksocknal_del_peer_locked (ksock_peer_t *peer, __u32 ip)
493 {
494         ksock_conn_t     *conn;
495         ksock_route_t    *route;
496         struct list_head *tmp;
497         struct list_head *nxt;
498         int               nshared;
499
500         LASSERT (!peer->ksnp_closing);
501
502         /* Extra ref prevents peer disappearing until I'm done with it */
503         ksocknal_peer_addref(peer);
504
505         list_for_each_safe (tmp, nxt, &peer->ksnp_routes) {
506                 route = list_entry(tmp, ksock_route_t, ksnr_list);
507
508                 /* no match */
509                 if (!(ip == 0 || route->ksnr_ipaddr == ip))
510                         continue;
511
512                 route->ksnr_share_count = 0;
513                 /* This deletes associated conns too */
514                 ksocknal_del_route_locked (route);
515         }
516
517         nshared = 0;
518         list_for_each_safe (tmp, nxt, &peer->ksnp_routes) {
519                 route = list_entry(tmp, ksock_route_t, ksnr_list);
520                 nshared += route->ksnr_share_count;
521         }
522
523         if (nshared == 0) {
524                 /* remove everything else if there are no explicit entries
525                  * left */
526
527                 list_for_each_safe (tmp, nxt, &peer->ksnp_routes) {
528                         route = list_entry(tmp, ksock_route_t, ksnr_list);
529
530                         /* we should only be removing auto-entries */
531                         LASSERT(route->ksnr_share_count == 0);
532                         ksocknal_del_route_locked (route);
533                 }
534
535                 list_for_each_safe (tmp, nxt, &peer->ksnp_conns) {
536                         conn = list_entry(tmp, ksock_conn_t, ksnc_list);
537
538                         ksocknal_close_conn_locked(conn, 0);
539                 }
540         }
541
542         ksocknal_peer_decref(peer);
543         /* NB peer unlinks itself when last conn/route is removed */
544 }
545
546 int
547 ksocknal_del_peer (lnet_ni_t *ni, lnet_process_id_t id, __u32 ip)
548 {
549         CFS_LIST_HEAD     (zombies);
550         struct list_head  *ptmp;
551         struct list_head  *pnxt;
552         ksock_peer_t      *peer;
553         int                lo;
554         int                hi;
555         int                i;
556         int                rc = -ENOENT;
557
558         write_lock_bh (&ksocknal_data.ksnd_global_lock);
559
560         if (id.nid != LNET_NID_ANY)
561                 lo = hi = ksocknal_nid2peerlist(id.nid) - ksocknal_data.ksnd_peers;
562         else {
563                 lo = 0;
564                 hi = ksocknal_data.ksnd_peer_hash_size - 1;
565         }
566
567         for (i = lo; i <= hi; i++) {
568                 list_for_each_safe (ptmp, pnxt, &ksocknal_data.ksnd_peers[i]) {
569                         peer = list_entry (ptmp, ksock_peer_t, ksnp_list);
570
571                         if (peer->ksnp_ni != ni)
572                                 continue;
573
574                         if (!((id.nid == LNET_NID_ANY || peer->ksnp_id.nid == id.nid) &&
575                               (id.pid == LNET_PID_ANY || peer->ksnp_id.pid == id.pid)))
576                                 continue;
577
578                         ksocknal_peer_addref(peer);     /* a ref for me... */
579
580                         ksocknal_del_peer_locked (peer, ip);
581
582                         if (peer->ksnp_closing && !list_empty(&peer->ksnp_tx_queue)) {
583                                 LASSERT (list_empty(&peer->ksnp_conns));
584                                 LASSERT (list_empty(&peer->ksnp_routes));
585
586                                 list_splice_init(&peer->ksnp_tx_queue, &zombies);
587                         }
588
589                         ksocknal_peer_decref(peer);     /* ...till here */
590
591                         rc = 0;                 /* matched! */
592                 }
593         }
594
595         write_unlock_bh (&ksocknal_data.ksnd_global_lock);
596
597         ksocknal_txlist_done(ni, &zombies, 1);
598
599         return (rc);
600 }
601
602 ksock_conn_t *
603 ksocknal_get_conn_by_idx (lnet_ni_t *ni, int index)
604 {
605         ksock_peer_t      *peer;
606         struct list_head  *ptmp;
607         ksock_conn_t      *conn;
608         struct list_head  *ctmp;
609         int                i;
610
611         read_lock (&ksocknal_data.ksnd_global_lock);
612
613         for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
614                 list_for_each (ptmp, &ksocknal_data.ksnd_peers[i]) {
615                         peer = list_entry (ptmp, ksock_peer_t, ksnp_list);
616
617                         LASSERT (!peer->ksnp_closing);
618
619                         if (peer->ksnp_ni != ni)
620                                 continue;
621
622                         list_for_each (ctmp, &peer->ksnp_conns) {
623                                 if (index-- > 0)
624                                         continue;
625
626                                 conn = list_entry (ctmp, ksock_conn_t, ksnc_list);
627                                 ksocknal_conn_addref(conn);
628                                 read_unlock (&ksocknal_data.ksnd_global_lock);
629                                 return (conn);
630                         }
631                 }
632         }
633
634         read_unlock (&ksocknal_data.ksnd_global_lock);
635         return (NULL);
636 }
637
638 ksock_sched_t *
639 ksocknal_choose_scheduler_locked (unsigned int irq)
640 {
641         ksock_sched_t    *sched;
642         ksock_irqinfo_t  *info;
643         int               i;
644
645         LASSERT (irq < NR_IRQS);
646         info = &ksocknal_data.ksnd_irqinfo[irq];
647
648         if (irq != 0 &&                         /* hardware NIC */
649             info->ksni_valid) {                 /* already set up */
650                 return (&ksocknal_data.ksnd_schedulers[info->ksni_sched]);
651         }
652
653         /* software NIC (irq == 0) || not associated with a scheduler yet.
654          * Choose the CPU with the fewest connections... */
655         sched = &ksocknal_data.ksnd_schedulers[0];
656         for (i = 1; i < ksocknal_data.ksnd_nschedulers; i++)
657                 if (sched->kss_nconns >
658                     ksocknal_data.ksnd_schedulers[i].kss_nconns)
659                         sched = &ksocknal_data.ksnd_schedulers[i];
660
661         if (irq != 0) {                         /* Hardware NIC */
662                 info->ksni_valid = 1;
663                 info->ksni_sched = sched - ksocknal_data.ksnd_schedulers;
664
665                 /* no overflow... */
666                 LASSERT (info->ksni_sched == sched - ksocknal_data.ksnd_schedulers);
667         }
668
669         return (sched);
670 }
671
672 int
673 ksocknal_local_ipvec (lnet_ni_t *ni, __u32 *ipaddrs)
674 {
675         ksock_net_t       *net = ni->ni_data;
676         int                i;
677         int                nip;
678
679         read_lock (&ksocknal_data.ksnd_global_lock);
680
681         nip = net->ksnn_ninterfaces;
682         LASSERT (nip < LNET_MAX_INTERFACES);
683
684         /* Only offer interfaces for additional connections if I have 
685          * more than one. */
686         if (nip < 2) {
687                 read_unlock (&ksocknal_data.ksnd_global_lock);
688                 return 0;
689         }
690         
691         for (i = 0; i < nip; i++) {
692                 ipaddrs[i] = net->ksnn_interfaces[i].ksni_ipaddr;
693                 LASSERT (ipaddrs[i] != 0);
694         }
695
696         read_unlock (&ksocknal_data.ksnd_global_lock);
697         return (nip);
698 }
699
700 int
701 ksocknal_match_peerip (ksock_interface_t *iface, __u32 *ips, int nips)
702 {
703         int   best_netmatch = 0;
704         int   best_xor      = 0;
705         int   best          = -1;
706         int   this_xor;
707         int   this_netmatch;
708         int   i;
709
710         for (i = 0; i < nips; i++) {
711                 if (ips[i] == 0)
712                         continue;
713
714                 this_xor = (ips[i] ^ iface->ksni_ipaddr);
715                 this_netmatch = ((this_xor & iface->ksni_netmask) == 0) ? 1 : 0;
716
717                 if (!(best < 0 ||
718                       best_netmatch < this_netmatch ||
719                       (best_netmatch == this_netmatch &&
720                        best_xor > this_xor)))
721                         continue;
722
723                 best = i;
724                 best_netmatch = this_netmatch;
725                 best_xor = this_xor;
726         }
727
728         LASSERT (best >= 0);
729         return (best);
730 }
731
732 int
733 ksocknal_select_ips(ksock_peer_t *peer, __u32 *peerips, int n_peerips)
734 {
735         rwlock_t           *global_lock = &ksocknal_data.ksnd_global_lock;
736         ksock_net_t        *net = peer->ksnp_ni->ni_data;
737         ksock_interface_t  *iface;
738         ksock_interface_t  *best_iface;
739         int                 n_ips;
740         int                 i;
741         int                 j;
742         int                 k;
743         __u32               ip;
744         __u32               xor;
745         int                 this_netmatch;
746         int                 best_netmatch;
747         int                 best_npeers;
748
749         /* CAVEAT EMPTOR: We do all our interface matching with an
750          * exclusive hold of global lock at IRQ priority.  We're only
751          * expecting to be dealing with small numbers of interfaces, so the
752          * O(n**3)-ness shouldn't matter */
753
754         /* Also note that I'm not going to return more than n_peerips
755          * interfaces, even if I have more myself */
756
757         write_lock_bh (global_lock);
758
759         LASSERT (n_peerips <= LNET_MAX_INTERFACES);
760         LASSERT (net->ksnn_ninterfaces <= LNET_MAX_INTERFACES);
761
762         /* Only match interfaces for additional connections 
763          * if I have > 1 interface */
764         n_ips = (net->ksnn_ninterfaces < 2) ? 0 :
765                 MIN(n_peerips, net->ksnn_ninterfaces);
766
767         for (i = 0; peer->ksnp_n_passive_ips < n_ips; i++) {
768                 /*              ^ yes really... */
769
770                 /* If we have any new interfaces, first tick off all the
771                  * peer IPs that match old interfaces, then choose new
772                  * interfaces to match the remaining peer IPS.
773                  * We don't forget interfaces we've stopped using; we might
774                  * start using them again... */
775
776                 if (i < peer->ksnp_n_passive_ips) {
777                         /* Old interface. */
778                         ip = peer->ksnp_passive_ips[i];
779                         best_iface = ksocknal_ip2iface(peer->ksnp_ni, ip);
780
781                         /* peer passive ips are kept up to date */
782                         LASSERT(best_iface != NULL);
783                 } else {
784                         /* choose a new interface */
785                         LASSERT (i == peer->ksnp_n_passive_ips);
786
787                         best_iface = NULL;
788                         best_netmatch = 0;
789                         best_npeers = 0;
790
791                         for (j = 0; j < net->ksnn_ninterfaces; j++) {
792                                 iface = &net->ksnn_interfaces[j];
793                                 ip = iface->ksni_ipaddr;
794
795                                 for (k = 0; k < peer->ksnp_n_passive_ips; k++)
796                                         if (peer->ksnp_passive_ips[k] == ip)
797                                                 break;
798
799                                 if (k < peer->ksnp_n_passive_ips) /* using it already */
800                                         continue;
801
802                                 k = ksocknal_match_peerip(iface, peerips, n_peerips);
803                                 xor = (ip ^ peerips[k]);
804                                 this_netmatch = ((xor & iface->ksni_netmask) == 0) ? 1 : 0;
805
806                                 if (!(best_iface == NULL ||
807                                       best_netmatch < this_netmatch ||
808                                       (best_netmatch == this_netmatch &&
809                                        best_npeers > iface->ksni_npeers)))
810                                         continue;
811
812                                 best_iface = iface;
813                                 best_netmatch = this_netmatch;
814                                 best_npeers = iface->ksni_npeers;
815                         }
816
817                         best_iface->ksni_npeers++;
818                         ip = best_iface->ksni_ipaddr;
819                         peer->ksnp_passive_ips[i] = ip;
820                         peer->ksnp_n_passive_ips = i+1;
821                 }
822
823                 LASSERT (best_iface != NULL);
824
825                 /* mark the best matching peer IP used */
826                 j = ksocknal_match_peerip(best_iface, peerips, n_peerips);
827                 peerips[j] = 0;
828         }
829
830         /* Overwrite input peer IP addresses */
831         memcpy(peerips, peer->ksnp_passive_ips, n_ips * sizeof(*peerips));
832
833         write_unlock_bh (global_lock);
834
835         return (n_ips);
836 }
837
838 void
839 ksocknal_create_routes(ksock_peer_t *peer, int port,
840                        __u32 *peer_ipaddrs, int npeer_ipaddrs)
841 {
842         ksock_route_t      *newroute = NULL;
843         rwlock_t           *global_lock = &ksocknal_data.ksnd_global_lock;
844         lnet_ni_t          *ni = peer->ksnp_ni;
845         ksock_net_t        *net = ni->ni_data;
846         struct list_head   *rtmp;
847         ksock_route_t      *route;
848         ksock_interface_t  *iface;
849         ksock_interface_t  *best_iface;
850         int                 best_netmatch;
851         int                 this_netmatch;
852         int                 best_nroutes;
853         int                 i;
854         int                 j;
855
856         /* CAVEAT EMPTOR: We do all our interface matching with an
857          * exclusive hold of global lock at IRQ priority.  We're only
858          * expecting to be dealing with small numbers of interfaces, so the
859          * O(n**3)-ness here shouldn't matter */
860
861         write_lock_bh (global_lock);
862
863         if (net->ksnn_ninterfaces < 2) {
864                 /* Only create additional connections 
865                  * if I have > 1 interface */
866                 write_unlock_bh (global_lock);
867                 return;
868         }
869         
870         LASSERT (npeer_ipaddrs <= LNET_MAX_INTERFACES);
871
872         for (i = 0; i < npeer_ipaddrs; i++) {
873                 if (newroute != NULL) {
874                         newroute->ksnr_ipaddr = peer_ipaddrs[i];
875                 } else {
876                         write_unlock_bh (global_lock);
877
878                         newroute = ksocknal_create_route(peer_ipaddrs[i], port);
879                         if (newroute == NULL)
880                                 return;
881
882                         write_lock_bh (global_lock);
883                 }
884
885                 if (peer->ksnp_closing) {
886                         /* peer got closed under me */
887                         break;
888                 }
889
890                 /* Already got a route? */
891                 route = NULL;
892                 list_for_each(rtmp, &peer->ksnp_routes) {
893                         route = list_entry(rtmp, ksock_route_t, ksnr_list);
894
895                         if (route->ksnr_ipaddr == newroute->ksnr_ipaddr)
896                                 break;
897
898                         route = NULL;
899                 }
900                 if (route != NULL)
901                         continue;
902
903                 best_iface = NULL;
904                 best_nroutes = 0;
905                 best_netmatch = 0;
906
907                 LASSERT (net->ksnn_ninterfaces <= LNET_MAX_INTERFACES);
908
909                 /* Select interface to connect from */
910                 for (j = 0; j < net->ksnn_ninterfaces; j++) {
911                         iface = &net->ksnn_interfaces[j];
912
913                         /* Using this interface already? */
914                         list_for_each(rtmp, &peer->ksnp_routes) {
915                                 route = list_entry(rtmp, ksock_route_t, ksnr_list);
916
917                                 if (route->ksnr_myipaddr == iface->ksni_ipaddr)
918                                         break;
919
920                                 route = NULL;
921                         }
922                         if (route != NULL)
923                                 continue;
924
925                         this_netmatch = (((iface->ksni_ipaddr ^
926                                            newroute->ksnr_ipaddr) &
927                                            iface->ksni_netmask) == 0) ? 1 : 0;
928
929                         if (!(best_iface == NULL ||
930                               best_netmatch < this_netmatch ||
931                               (best_netmatch == this_netmatch &&
932                                best_nroutes > iface->ksni_nroutes)))
933                                 continue;
934
935                         best_iface = iface;
936                         best_netmatch = this_netmatch;
937                         best_nroutes = iface->ksni_nroutes;
938                 }
939
940                 if (best_iface == NULL)
941                         continue;
942
943                 newroute->ksnr_myipaddr = best_iface->ksni_ipaddr;
944                 best_iface->ksni_nroutes++;
945
946                 ksocknal_add_route_locked(peer, newroute);
947                 newroute = NULL;
948         }
949
950         write_unlock_bh (global_lock);
951         if (newroute != NULL)
952                 ksocknal_route_decref(newroute);
953 }
954
955 int
956 ksocknal_accept (lnet_ni_t *ni, cfs_socket_t *sock)
957 {
958         ksock_connreq_t    *cr;
959         int                 rc;
960         __u32               peer_ip;
961         int                 peer_port;
962
963         rc = libcfs_sock_getaddr(sock, 1, &peer_ip, &peer_port);
964         LASSERT (rc == 0);                      /* we succeeded before */
965
966         LIBCFS_ALLOC(cr, sizeof(*cr));
967         if (cr == NULL) {
968                 LCONSOLE_ERROR_MSG(0x12f, "Dropping connection request from "
969                                    "%u.%u.%u.%u: memory exhausted\n",
970                                    HIPQUAD(peer_ip));
971                 return -ENOMEM;
972         }
973
974         lnet_ni_addref(ni);
975         cr->ksncr_ni   = ni;
976         cr->ksncr_sock = sock;
977
978         spin_lock_bh (&ksocknal_data.ksnd_connd_lock);
979
980         list_add_tail(&cr->ksncr_list, &ksocknal_data.ksnd_connd_connreqs);
981         cfs_waitq_signal(&ksocknal_data.ksnd_connd_waitq);
982                         
983         spin_unlock_bh (&ksocknal_data.ksnd_connd_lock);
984         return 0;
985 }
986
987 int
988 ksocknal_connecting (ksock_peer_t *peer, __u32 ipaddr) 
989 {
990         ksock_route_t   *route;
991         
992         list_for_each_entry (route, &peer->ksnp_routes, ksnr_list) {
993
994                 if (route->ksnr_ipaddr == ipaddr)
995                         return route->ksnr_connecting;
996         }
997         return 0;
998 }
999
1000 int
1001 ksocknal_create_conn (lnet_ni_t *ni, ksock_route_t *route, 
1002                       cfs_socket_t *sock, int type)
1003 {
1004         rwlock_t          *global_lock = &ksocknal_data.ksnd_global_lock;
1005         CFS_LIST_HEAD     (zombies);
1006         lnet_process_id_t  peerid;
1007         struct list_head  *tmp;
1008         __u64              incarnation;
1009         ksock_conn_t      *conn;
1010         ksock_conn_t      *conn2;
1011         ksock_peer_t      *peer = NULL;
1012         ksock_peer_t      *peer2;
1013         ksock_sched_t     *sched;
1014         ksock_hello_msg_t *hello;
1015         unsigned int       irq;
1016         ksock_tx_t        *tx;
1017         int                rc;
1018         int                active;
1019         char              *warn = NULL;
1020
1021         active = (route != NULL);
1022
1023         LASSERT (active == (type != SOCKLND_CONN_NONE));
1024
1025         irq = ksocknal_lib_sock_irq (sock);
1026
1027         LIBCFS_ALLOC(conn, sizeof(*conn));
1028         if (conn == NULL) {
1029                 rc = -ENOMEM;
1030                 goto failed_0;
1031         }
1032
1033         memset (conn, 0, sizeof (*conn));
1034         conn->ksnc_peer = NULL;
1035         conn->ksnc_route = NULL;
1036         conn->ksnc_sock = sock;
1037         atomic_set (&conn->ksnc_sock_refcount, 1); /* 1 ref for conn */
1038         conn->ksnc_type = type;
1039         ksocknal_lib_save_callback(sock, conn);
1040         atomic_set (&conn->ksnc_conn_refcount, 1); /* 1 ref for me */
1041
1042         conn->ksnc_zc_capable = ksocknal_lib_zc_capable(sock);
1043         conn->ksnc_rx_ready = 0;
1044         conn->ksnc_rx_scheduled = 0;
1045
1046         CFS_INIT_LIST_HEAD (&conn->ksnc_tx_queue);
1047         conn->ksnc_tx_ready = 0;
1048         conn->ksnc_tx_scheduled = 0;
1049         conn->ksnc_tx_mono = NULL;
1050         atomic_set (&conn->ksnc_tx_nob, 0);
1051
1052         LIBCFS_ALLOC(hello, offsetof(ksock_hello_msg_t,
1053                                      kshm_ips[LNET_MAX_INTERFACES]));
1054         if (hello == NULL) {
1055                 rc = -ENOMEM;
1056                 goto failed_1;
1057         }
1058
1059         /* stash conn's local and remote addrs */
1060         rc = ksocknal_lib_get_conn_addrs (conn);
1061         if (rc != 0)
1062                 goto failed_1;
1063
1064         /* Find out/confirm peer's NID and connection type and get the
1065          * vector of interfaces she's willing to let me connect to.
1066          * Passive connections use the listener timeout since the peer sends
1067          * eagerly */
1068
1069         if (active) {
1070                 peer = route->ksnr_peer;
1071                 LASSERT(ni == peer->ksnp_ni);
1072
1073                 /* Active connection sends HELLO eagerly */
1074                 hello->kshm_nips = ksocknal_local_ipvec(ni, hello->kshm_ips);
1075                 peerid = peer->ksnp_id;
1076
1077                 write_lock_bh(global_lock);
1078                 conn->ksnc_proto = peer->ksnp_proto;
1079                 write_unlock_bh(global_lock);
1080                 
1081                 if (conn->ksnc_proto == NULL) {
1082                         conn->ksnc_proto = &ksocknal_protocol_v2x;
1083 #if SOCKNAL_VERSION_DEBUG
1084                         if (*ksocknal_tunables.ksnd_protocol != 2)
1085                                 conn->ksnc_proto = &ksocknal_protocol_v1x;
1086 #endif
1087                 }
1088
1089                 rc = ksocknal_send_hello (ni, conn, peerid.nid, hello);
1090                 if (rc != 0)
1091                         goto failed_1;
1092         } else {
1093                 peerid.nid = LNET_NID_ANY;
1094                 peerid.pid = LNET_PID_ANY;
1095
1096                 /* Passive, get protocol from peer */
1097                 conn->ksnc_proto = NULL;
1098         }
1099
1100         rc = ksocknal_recv_hello (ni, conn, hello, &peerid, &incarnation);
1101         if (rc < 0)
1102                 goto failed_1;
1103
1104         LASSERT (rc == 0 || active);
1105         LASSERT (conn->ksnc_proto != NULL);
1106         LASSERT (peerid.nid != LNET_NID_ANY);
1107
1108         if (active) {
1109                 ksocknal_peer_addref(peer);
1110                 write_lock_bh (global_lock);
1111         } else {
1112                 rc = ksocknal_create_peer(&peer, ni, peerid);
1113                 if (rc != 0)
1114                         goto failed_1;
1115
1116                 write_lock_bh (global_lock);
1117
1118                 peer2 = ksocknal_find_peer_locked(ni, peerid);
1119                 if (peer2 == NULL) {
1120                         /* NB this puts an "empty" peer in the peer
1121                          * table (which takes my ref) */
1122                         list_add_tail(&peer->ksnp_list,
1123                                       ksocknal_nid2peerlist(peerid.nid));
1124                 } else {
1125                         ksocknal_peer_decref(peer);
1126                         peer = peer2;
1127                 }
1128
1129                 /* +1 ref for me */
1130                 ksocknal_peer_addref(peer);
1131                 peer->ksnp_accepting++;
1132                 
1133                 /* Am I already connecting to this guy?  Resolve in
1134                  * favour of higher NID... */
1135                 if (peerid.nid < ni->ni_nid &&
1136                     ksocknal_connecting(peer, conn->ksnc_ipaddr)) {
1137                         rc = EALREADY;
1138                         warn = "connection race resolution";
1139                         goto failed_2;
1140                 }
1141         }
1142
1143         if (peer->ksnp_closing ||
1144             (active && route->ksnr_deleted)) {
1145                 /* peer/route got closed under me */
1146                 rc = -ESTALE;
1147                 warn = "peer/route removed";
1148                 goto failed_2;
1149         }
1150
1151         if (peer->ksnp_proto == NULL) { 
1152                 /* Never connected before.
1153                  * NB recv_hello may have returned EPROTO to signal my peer
1154                  * wants a different protocol than the one I asked for.
1155                  */
1156                 LASSERT (list_empty(&peer->ksnp_conns));
1157                 
1158                 peer->ksnp_proto = conn->ksnc_proto;
1159                 peer->ksnp_incarnation = incarnation;
1160         }
1161
1162         if (peer->ksnp_proto != conn->ksnc_proto ||
1163             peer->ksnp_incarnation != incarnation) {
1164                 /* Peer rebooted or I've got the wrong protocol version */
1165                 ksocknal_close_peer_conns_locked(peer, 0, 0);
1166
1167                 peer->ksnp_proto = NULL;
1168                 rc = ESTALE;
1169                 warn = peer->ksnp_incarnation != incarnation ? 
1170                        "peer rebooted" :
1171                        "wrong proto version";
1172                 goto failed_2;
1173         }
1174
1175         switch (rc) {
1176         default:
1177                 LBUG();
1178         case 0:
1179                 break;
1180         case EALREADY:
1181                 warn = "lost conn race";
1182                 goto failed_2;
1183         case EPROTO:
1184                 warn = "retry with different protocol version";
1185                 goto failed_2;
1186         }
1187
1188         /* Refuse to duplicate an existing connection, unless this is a
1189          * loopback connection */
1190         if (conn->ksnc_ipaddr != conn->ksnc_myipaddr) {
1191                 list_for_each(tmp, &peer->ksnp_conns) {
1192                         conn2 = list_entry(tmp, ksock_conn_t, ksnc_list);
1193
1194                         if (conn2->ksnc_ipaddr != conn->ksnc_ipaddr ||
1195                             conn2->ksnc_myipaddr != conn->ksnc_myipaddr ||
1196                             conn2->ksnc_type != conn->ksnc_type)
1197                                 continue;
1198
1199                         /* Reply on a passive connection attempt so the peer
1200                          * realises we're connected. */
1201                         LASSERT (rc == 0);
1202                         if (!active)
1203                                 rc = EALREADY;
1204
1205                         warn = "duplicate";
1206                         goto failed_2;
1207                 }
1208         }
1209
1210         /* If the connection created by this route didn't bind to the IP
1211          * address the route connected to, the connection/route matching
1212          * code below probably isn't going to work. */
1213         if (active &&
1214             route->ksnr_ipaddr != conn->ksnc_ipaddr) {
1215                 CERROR("Route %s %u.%u.%u.%u connected to %u.%u.%u.%u\n",
1216                        libcfs_id2str(peer->ksnp_id),
1217                        HIPQUAD(route->ksnr_ipaddr),
1218                        HIPQUAD(conn->ksnc_ipaddr));
1219         }
1220
1221         /* Search for a route corresponding to the new connection and
1222          * create an association.  This allows incoming connections created
1223          * by routes in my peer to match my own route entries so I don't
1224          * continually create duplicate routes. */
1225         list_for_each (tmp, &peer->ksnp_routes) {
1226                 route = list_entry(tmp, ksock_route_t, ksnr_list);
1227
1228                 if (route->ksnr_ipaddr != conn->ksnc_ipaddr)
1229                         continue;
1230
1231                 ksocknal_associate_route_conn_locked(route, conn);
1232                 break;
1233         }
1234
1235         conn->ksnc_peer = peer;                 /* conn takes my ref on peer */
1236         peer->ksnp_last_alive = cfs_time_current();
1237         peer->ksnp_error = 0;
1238
1239         sched = ksocknal_choose_scheduler_locked (irq);
1240         sched->kss_nconns++;
1241         conn->ksnc_scheduler = sched;
1242
1243         /* Set the deadline for the outgoing HELLO to drain */
1244         conn->ksnc_tx_bufnob = SOCK_WMEM_QUEUED(sock);
1245         conn->ksnc_tx_deadline = cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
1246         mb();       /* order with adding to peer's conn list */
1247
1248         list_add (&conn->ksnc_list, &peer->ksnp_conns);
1249         ksocknal_conn_addref(conn);
1250
1251         ksocknal_new_packet(conn, 0);
1252
1253         /* Take all the packets blocking for a connection.
1254          * NB, it might be nicer to share these blocked packets among any
1255          * other connections that are becoming established. */
1256         while (!list_empty (&peer->ksnp_tx_queue)) {
1257                 tx = list_entry (peer->ksnp_tx_queue.next,
1258                                  ksock_tx_t, tx_list);
1259
1260                 list_del (&tx->tx_list);
1261                 ksocknal_queue_tx_locked (tx, conn);
1262         }
1263
1264         write_unlock_bh (global_lock);
1265
1266         /* We've now got a new connection.  Any errors from here on are just
1267          * like "normal" comms errors and we close the connection normally.
1268          * NB (a) we still have to send the reply HELLO for passive
1269          *        connections, 
1270          *    (b) normal I/O on the conn is blocked until I setup and call the
1271          *        socket callbacks.
1272          */
1273
1274         ksocknal_lib_bind_irq (irq);
1275
1276         CDEBUG(D_NET, "New conn %s p %d.x %u.%u.%u.%u -> %u.%u.%u.%u/%d"
1277                " incarnation:"LPD64" sched[%d]/%d\n",
1278                libcfs_id2str(peerid), conn->ksnc_proto->pro_version,
1279                HIPQUAD(conn->ksnc_myipaddr), HIPQUAD(conn->ksnc_ipaddr),
1280                conn->ksnc_port, incarnation,
1281                (int)(conn->ksnc_scheduler - ksocknal_data.ksnd_schedulers), irq);
1282
1283         if (active) {
1284                 /* additional routes after interface exchange? */
1285                 ksocknal_create_routes(peer, conn->ksnc_port,
1286                                        hello->kshm_ips, hello->kshm_nips);
1287         } else {
1288                 hello->kshm_nips = ksocknal_select_ips(peer, hello->kshm_ips,
1289                                                        hello->kshm_nips);
1290                 rc = ksocknal_send_hello(ni, conn, peerid.nid, hello);
1291         }
1292         
1293         LIBCFS_FREE(hello, offsetof(ksock_hello_msg_t,
1294                                     kshm_ips[LNET_MAX_INTERFACES]));
1295
1296         /* setup the socket AFTER I've received hello (it disables
1297          * SO_LINGER).  I might call back to the acceptor who may want
1298          * to send a protocol version response and then close the
1299          * socket; this ensures the socket only tears down after the
1300          * response has been sent. */
1301         if (rc == 0)
1302                 rc = ksocknal_lib_setup_sock(sock);
1303
1304         write_lock_bh(global_lock);
1305
1306         /* NB my callbacks block while I hold ksnd_global_lock */
1307         ksocknal_lib_set_callback(sock, conn);
1308
1309         if (!active)
1310                 peer->ksnp_accepting--;
1311
1312         write_unlock_bh(global_lock);
1313
1314         if (rc != 0) {
1315                 write_lock_bh(global_lock);
1316                 ksocknal_close_conn_locked(conn, rc);
1317                 write_unlock_bh(global_lock);
1318         } else if (ksocknal_connsock_addref(conn) == 0) {
1319                 /* Allow I/O to proceed. */
1320                 ksocknal_read_callback(conn);
1321                 ksocknal_write_callback(conn);
1322                 ksocknal_connsock_decref(conn);
1323         }
1324
1325         ksocknal_conn_decref(conn);
1326         return rc;
1327
1328  failed_2:
1329         if (!peer->ksnp_closing &&
1330             list_empty (&peer->ksnp_conns) &&
1331             list_empty (&peer->ksnp_routes)) {
1332                 list_add(&zombies, &peer->ksnp_tx_queue);
1333                 list_del_init(&peer->ksnp_tx_queue);
1334                 ksocknal_unlink_peer_locked(peer);
1335         }
1336         
1337         write_unlock_bh (global_lock);
1338
1339         if (warn != NULL) {
1340                 if (rc < 0)
1341                         CERROR("Not creating conn %s type %d: %s\n",
1342                                libcfs_id2str(peerid), conn->ksnc_type, warn);
1343                 else
1344                         CDEBUG(D_NET, "Not creating conn %s type %d: %s\n",
1345                               libcfs_id2str(peerid), conn->ksnc_type, warn);
1346         }
1347
1348         if (!active) {
1349                 if (rc > 0) {
1350                         /* Request retry by replying with CONN_NONE 
1351                          * ksnc_proto has been set already */
1352                         conn->ksnc_type = SOCKLND_CONN_NONE;
1353                         hello->kshm_nips = 0;
1354                         ksocknal_send_hello(ni, conn, peerid.nid, hello);
1355                 }
1356
1357                 write_lock_bh(global_lock);
1358                 peer->ksnp_accepting--;
1359                 write_unlock_bh(global_lock);
1360         }
1361         
1362         ksocknal_txlist_done(ni, &zombies, 1);
1363         ksocknal_peer_decref(peer);
1364
1365  failed_1:
1366         if (hello != NULL)
1367                 LIBCFS_FREE(hello, offsetof(ksock_hello_msg_t,
1368                                             kshm_ips[LNET_MAX_INTERFACES]));
1369
1370         LIBCFS_FREE (conn, sizeof(*conn));
1371
1372  failed_0:
1373         libcfs_sock_release(sock);
1374         return rc;
1375 }
1376
1377 void
1378 ksocknal_close_conn_locked (ksock_conn_t *conn, int error)
1379 {
1380         /* This just does the immmediate housekeeping, and queues the
1381          * connection for the reaper to terminate.
1382          * Caller holds ksnd_global_lock exclusively in irq context */
1383         ksock_peer_t      *peer = conn->ksnc_peer;
1384         ksock_route_t     *route;
1385         ksock_conn_t      *conn2;
1386         struct list_head  *tmp;
1387
1388         LASSERT (peer->ksnp_error == 0);
1389         LASSERT (!conn->ksnc_closing);
1390         conn->ksnc_closing = 1;
1391
1392         /* ksnd_deathrow_conns takes over peer's ref */
1393         list_del (&conn->ksnc_list);
1394
1395         route = conn->ksnc_route;
1396         if (route != NULL) {
1397                 /* dissociate conn from route... */
1398                 LASSERT (!route->ksnr_deleted);
1399                 LASSERT ((route->ksnr_connected & (1 << conn->ksnc_type)) != 0);
1400
1401                 conn2 = NULL;
1402                 list_for_each(tmp, &peer->ksnp_conns) {
1403                         conn2 = list_entry(tmp, ksock_conn_t, ksnc_list);
1404
1405                         if (conn2->ksnc_route == route &&
1406                             conn2->ksnc_type == conn->ksnc_type)
1407                                 break;
1408
1409                         conn2 = NULL;
1410                 }
1411                 if (conn2 == NULL)
1412                         route->ksnr_connected &= ~(1 << conn->ksnc_type);
1413
1414                 conn->ksnc_route = NULL;
1415
1416 #if 0           /* irrelevent with only eager routes */
1417                 list_del (&route->ksnr_list);   /* make route least favourite */
1418                 list_add_tail (&route->ksnr_list, &peer->ksnp_routes);
1419 #endif
1420                 ksocknal_route_decref(route);     /* drop conn's ref on route */
1421         }
1422
1423         if (list_empty (&peer->ksnp_conns)) {
1424                 /* No more connections to this peer */
1425
1426                 peer->ksnp_proto = NULL;        /* renegotiate protocol version */
1427                 peer->ksnp_error = error;       /* stash last conn close reason */
1428
1429                 if (list_empty (&peer->ksnp_routes)) {
1430                         /* I've just closed last conn belonging to a
1431                          * peer with no routes to it */
1432                         ksocknal_unlink_peer_locked (peer);
1433                 }
1434         }
1435
1436         spin_lock_bh (&ksocknal_data.ksnd_reaper_lock);
1437
1438         list_add_tail (&conn->ksnc_list, &ksocknal_data.ksnd_deathrow_conns);
1439         cfs_waitq_signal (&ksocknal_data.ksnd_reaper_waitq);
1440
1441         spin_unlock_bh (&ksocknal_data.ksnd_reaper_lock);
1442 }
1443
1444 void
1445 ksocknal_peer_failed (ksock_peer_t *peer)
1446 {
1447         time_t    last_alive = 0;
1448         int       notify = 0;
1449
1450         /* There has been a connection failure or comms error; but I'll only
1451          * tell LNET I think the peer is dead if it's to another kernel and
1452          * there are no connections or connection attempts in existance. */
1453         
1454         read_lock (&ksocknal_data.ksnd_global_lock);
1455
1456         if ((peer->ksnp_id.pid & LNET_PID_USERFLAG) == 0 &&
1457             list_empty(&peer->ksnp_conns) &&
1458             peer->ksnp_accepting == 0 &&
1459             ksocknal_find_connecting_route_locked(peer) == NULL) {
1460                 notify = 1;
1461                 last_alive = cfs_time_current_sec() - 
1462                              cfs_duration_sec(cfs_time_current() - 
1463                                               peer->ksnp_last_alive);
1464         }
1465         
1466         read_unlock (&ksocknal_data.ksnd_global_lock);
1467
1468         if (notify)
1469                 lnet_notify (peer->ksnp_ni, peer->ksnp_id.nid, 0,
1470                              last_alive);
1471 }
1472
1473 void
1474 ksocknal_terminate_conn (ksock_conn_t *conn)
1475 {
1476         /* This gets called by the reaper (guaranteed thread context) to
1477          * disengage the socket from its callbacks and close it.
1478          * ksnc_refcount will eventually hit zero, and then the reaper will
1479          * destroy it. */
1480         ksock_peer_t     *peer = conn->ksnc_peer;
1481         ksock_sched_t    *sched = conn->ksnc_scheduler;
1482         int               failed = 0;
1483         struct list_head *tmp;
1484         struct list_head *nxt;
1485         ksock_tx_t       *tx;
1486         LIST_HEAD        (zlist);
1487
1488         LASSERT(conn->ksnc_closing);
1489
1490         /* wake up the scheduler to "send" all remaining packets to /dev/null */
1491         spin_lock_bh (&sched->kss_lock);
1492
1493         if (!conn->ksnc_tx_scheduled &&
1494             !list_empty(&conn->ksnc_tx_queue)){
1495                 list_add_tail (&conn->ksnc_tx_list,
1496                                &sched->kss_tx_conns);
1497                 /* a closing conn is always ready to tx */
1498                 conn->ksnc_tx_ready = 1;
1499                 conn->ksnc_tx_scheduled = 1;
1500                 /* extra ref for scheduler */
1501                 ksocknal_conn_addref(conn);
1502
1503                 cfs_waitq_signal (&sched->kss_waitq);
1504         }
1505
1506         spin_unlock_bh (&sched->kss_lock);
1507
1508         spin_lock(&peer->ksnp_lock);
1509
1510         list_for_each_safe(tmp, nxt, &peer->ksnp_zc_req_list) {
1511                 tx = list_entry(tmp, ksock_tx_t, tx_zc_list);
1512
1513                 if (tx->tx_conn != conn)
1514                         continue;
1515
1516                 LASSERT (tx->tx_msg.ksm_zc_req_cookie != 0);
1517
1518                 tx->tx_msg.ksm_zc_req_cookie = 0;
1519                 list_del(&tx->tx_zc_list);
1520                 list_add(&tx->tx_zc_list, &zlist);
1521         }
1522
1523         spin_unlock(&peer->ksnp_lock);
1524
1525         list_for_each_safe(tmp, nxt, &zlist) {
1526                 tx = list_entry(tmp, ksock_tx_t, tx_zc_list);
1527
1528                 list_del(&tx->tx_zc_list);
1529                 ksocknal_tx_decref(tx);
1530         }
1531
1532         /* serialise with callbacks */
1533         write_lock_bh (&ksocknal_data.ksnd_global_lock);
1534
1535         ksocknal_lib_reset_callback(conn->ksnc_sock, conn);
1536
1537         /* OK, so this conn may not be completely disengaged from its
1538          * scheduler yet, but it _has_ committed to terminate... */
1539         conn->ksnc_scheduler->kss_nconns--;
1540
1541         if (peer->ksnp_error != 0) {
1542                 /* peer's last conn closed in error */
1543                 LASSERT (list_empty (&peer->ksnp_conns));
1544                 failed = 1;
1545                 peer->ksnp_error = 0;     /* avoid multiple notifications */
1546         }
1547
1548         write_unlock_bh (&ksocknal_data.ksnd_global_lock);
1549
1550         if (failed)
1551                 ksocknal_peer_failed(peer);
1552
1553         /* The socket is closed on the final put; either here, or in
1554          * ksocknal_{send,recv}msg().  Since we set up the linger2 option
1555          * when the connection was established, this will close the socket
1556          * immediately, aborting anything buffered in it. Any hung
1557          * zero-copy transmits will therefore complete in finite time. */
1558         ksocknal_connsock_decref(conn);
1559 }
1560
1561 void
1562 ksocknal_queue_zombie_conn (ksock_conn_t *conn)
1563 {
1564         /* Queue the conn for the reaper to destroy */
1565
1566         LASSERT (atomic_read(&conn->ksnc_conn_refcount) == 0);
1567         spin_lock_bh (&ksocknal_data.ksnd_reaper_lock);
1568
1569         list_add_tail(&conn->ksnc_list, &ksocknal_data.ksnd_zombie_conns);
1570         cfs_waitq_signal(&ksocknal_data.ksnd_reaper_waitq);
1571         
1572         spin_unlock_bh (&ksocknal_data.ksnd_reaper_lock);
1573 }
1574
1575 void
1576 ksocknal_destroy_conn (ksock_conn_t *conn)
1577 {
1578         /* Final coup-de-grace of the reaper */
1579         CDEBUG (D_NET, "connection %p\n", conn);
1580
1581         LASSERT (atomic_read (&conn->ksnc_conn_refcount) == 0);
1582         LASSERT (atomic_read (&conn->ksnc_sock_refcount) == 0);
1583         LASSERT (conn->ksnc_sock == NULL);
1584         LASSERT (conn->ksnc_route == NULL);
1585         LASSERT (!conn->ksnc_tx_scheduled);
1586         LASSERT (!conn->ksnc_rx_scheduled);
1587         LASSERT (list_empty(&conn->ksnc_tx_queue));
1588
1589         /* complete current receive if any */
1590         switch (conn->ksnc_rx_state) {
1591         case SOCKNAL_RX_LNET_PAYLOAD:
1592                 CERROR("Completing partial receive from %s"
1593                        ", ip %d.%d.%d.%d:%d, with error\n",
1594                        libcfs_id2str(conn->ksnc_peer->ksnp_id),
1595                        HIPQUAD(conn->ksnc_ipaddr), conn->ksnc_port);
1596                 lnet_finalize (conn->ksnc_peer->ksnp_ni, 
1597                                conn->ksnc_cookie, -EIO);
1598                 break;
1599         case SOCKNAL_RX_LNET_HEADER:
1600                 if (conn->ksnc_rx_started)
1601                         CERROR("Incomplete receive of lnet header from %s"
1602                                ", ip %d.%d.%d.%d:%d, with error, protocol: %d.x.\n",
1603                                libcfs_id2str(conn->ksnc_peer->ksnp_id),
1604                                HIPQUAD(conn->ksnc_ipaddr), conn->ksnc_port,
1605                                conn->ksnc_proto->pro_version);
1606                 break;
1607         case SOCKNAL_RX_KSM_HEADER:
1608                 if (conn->ksnc_rx_started)
1609                         CERROR("Incomplete receive of ksock message from %s"
1610                                ", ip %d.%d.%d.%d:%d, with error, protocol: %d.x.\n",
1611                                libcfs_id2str(conn->ksnc_peer->ksnp_id),
1612                                HIPQUAD(conn->ksnc_ipaddr), conn->ksnc_port,
1613                                conn->ksnc_proto->pro_version);
1614                 break;
1615         case SOCKNAL_RX_SLOP:
1616                 if (conn->ksnc_rx_started)
1617                         CERROR("Incomplete receive of slops from %s"
1618                                ", ip %d.%d.%d.%d:%d, with error\n",
1619                                libcfs_id2str(conn->ksnc_peer->ksnp_id),
1620                                HIPQUAD(conn->ksnc_ipaddr), conn->ksnc_port);
1621                break;
1622         default:
1623                 LBUG ();
1624                 break;
1625         }
1626
1627         ksocknal_peer_decref(conn->ksnc_peer);
1628
1629         LIBCFS_FREE (conn, sizeof (*conn));
1630 }
1631
1632 int
1633 ksocknal_close_peer_conns_locked (ksock_peer_t *peer, __u32 ipaddr, int why)
1634 {
1635         ksock_conn_t       *conn;
1636         struct list_head   *ctmp;
1637         struct list_head   *cnxt;
1638         int                 count = 0;
1639
1640         list_for_each_safe (ctmp, cnxt, &peer->ksnp_conns) {
1641                 conn = list_entry (ctmp, ksock_conn_t, ksnc_list);
1642
1643                 if (ipaddr == 0 ||
1644                     conn->ksnc_ipaddr == ipaddr) {
1645                         count++;
1646                         ksocknal_close_conn_locked (conn, why);
1647                 }
1648         }
1649
1650         return (count);
1651 }
1652
1653 int
1654 ksocknal_close_conn_and_siblings (ksock_conn_t *conn, int why)
1655 {
1656         ksock_peer_t     *peer = conn->ksnc_peer;
1657         __u32             ipaddr = conn->ksnc_ipaddr;
1658         int               count;
1659
1660         write_lock_bh (&ksocknal_data.ksnd_global_lock);
1661
1662         count = ksocknal_close_peer_conns_locked (peer, ipaddr, why);
1663
1664         write_unlock_bh (&ksocknal_data.ksnd_global_lock);
1665
1666         return (count);
1667 }
1668
1669 int
1670 ksocknal_close_matching_conns (lnet_process_id_t id, __u32 ipaddr)
1671 {
1672         ksock_peer_t       *peer;
1673         struct list_head   *ptmp;
1674         struct list_head   *pnxt;
1675         int                 lo;
1676         int                 hi;
1677         int                 i;
1678         int                 count = 0;
1679
1680         write_lock_bh (&ksocknal_data.ksnd_global_lock);
1681
1682         if (id.nid != LNET_NID_ANY)
1683                 lo = hi = ksocknal_nid2peerlist(id.nid) - ksocknal_data.ksnd_peers;
1684         else {
1685                 lo = 0;
1686                 hi = ksocknal_data.ksnd_peer_hash_size - 1;
1687         }
1688
1689         for (i = lo; i <= hi; i++) {
1690                 list_for_each_safe (ptmp, pnxt, &ksocknal_data.ksnd_peers[i]) {
1691
1692                         peer = list_entry (ptmp, ksock_peer_t, ksnp_list);
1693
1694                         if (!((id.nid == LNET_NID_ANY || id.nid == peer->ksnp_id.nid) &&
1695                               (id.pid == LNET_PID_ANY || id.pid == peer->ksnp_id.pid)))
1696                                 continue;
1697
1698                         count += ksocknal_close_peer_conns_locked (peer, ipaddr, 0);
1699                 }
1700         }
1701
1702         write_unlock_bh (&ksocknal_data.ksnd_global_lock);
1703
1704         /* wildcards always succeed */
1705         if (id.nid == LNET_NID_ANY || id.pid == LNET_PID_ANY || ipaddr == 0)
1706                 return (0);
1707
1708         return (count == 0 ? -ENOENT : 0);
1709 }
1710
1711 void
1712 ksocknal_notify (lnet_ni_t *ni, lnet_nid_t gw_nid, int alive)
1713 {
1714         /* The router is telling me she's been notified of a change in
1715          * gateway state.... */
1716         lnet_process_id_t  id = {.nid = gw_nid, .pid = LNET_PID_ANY};
1717
1718         CDEBUG (D_NET, "gw %s %s\n", libcfs_nid2str(gw_nid), 
1719                 alive ? "up" : "down");
1720
1721         if (!alive) {
1722                 /* If the gateway crashed, close all open connections... */
1723                 ksocknal_close_matching_conns (id, 0);
1724                 return;
1725         }
1726
1727         /* ...otherwise do nothing.  We can only establish new connections
1728          * if we have autroutes, and these connect on demand. */
1729 }
1730
1731 void
1732 ksocknal_push_peer (ksock_peer_t *peer)
1733 {
1734         int               index;
1735         int               i;
1736         struct list_head *tmp;
1737         ksock_conn_t     *conn;
1738
1739         for (index = 0; ; index++) {
1740                 read_lock (&ksocknal_data.ksnd_global_lock);
1741
1742                 i = 0;
1743                 conn = NULL;
1744
1745                 list_for_each (tmp, &peer->ksnp_conns) {
1746                         if (i++ == index) {
1747                                 conn = list_entry (tmp, ksock_conn_t, ksnc_list);
1748                                 ksocknal_conn_addref(conn);
1749                                 break;
1750                         }
1751                 }
1752
1753                 read_unlock (&ksocknal_data.ksnd_global_lock);
1754
1755                 if (conn == NULL)
1756                         break;
1757
1758                 ksocknal_lib_push_conn (conn);
1759                 ksocknal_conn_decref(conn);
1760         }
1761 }
1762
1763 int
1764 ksocknal_push (lnet_ni_t *ni, lnet_process_id_t id)
1765 {
1766         ksock_peer_t      *peer;
1767         struct list_head  *tmp;
1768         int                index;
1769         int                i;
1770         int                j;
1771         int                rc = -ENOENT;
1772
1773         for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
1774                 for (j = 0; ; j++) {
1775                         read_lock (&ksocknal_data.ksnd_global_lock);
1776
1777                         index = 0;
1778                         peer = NULL;
1779
1780                         list_for_each (tmp, &ksocknal_data.ksnd_peers[i]) {
1781                                 peer = list_entry(tmp, ksock_peer_t,
1782                                                   ksnp_list);
1783
1784                                 if (!((id.nid == LNET_NID_ANY ||
1785                                        id.nid == peer->ksnp_id.nid) &&
1786                                       (id.pid == LNET_PID_ANY ||
1787                                        id.pid == peer->ksnp_id.pid))) {
1788                                         peer = NULL;
1789                                         continue;
1790                                 }
1791
1792                                 if (index++ == j) {
1793                                         ksocknal_peer_addref(peer);
1794                                         break;
1795                                 }
1796                         }
1797
1798                         read_unlock (&ksocknal_data.ksnd_global_lock);
1799
1800                         if (peer != NULL) {
1801                                 rc = 0;
1802                                 ksocknal_push_peer (peer);
1803                                 ksocknal_peer_decref(peer);
1804                         }
1805                 }
1806
1807         }
1808
1809         return (rc);
1810 }
1811
1812 int
1813 ksocknal_add_interface(lnet_ni_t *ni, __u32 ipaddress, __u32 netmask)
1814 {
1815         ksock_net_t       *net = ni->ni_data;
1816         ksock_interface_t *iface;
1817         int                rc;
1818         int                i;
1819         int                j;
1820         struct list_head  *ptmp;
1821         ksock_peer_t      *peer;
1822         struct list_head  *rtmp;
1823         ksock_route_t     *route;
1824
1825         if (ipaddress == 0 ||
1826             netmask == 0)
1827                 return (-EINVAL);
1828
1829         write_lock_bh (&ksocknal_data.ksnd_global_lock);
1830
1831         iface = ksocknal_ip2iface(ni, ipaddress);
1832         if (iface != NULL) {
1833                 /* silently ignore dups */
1834                 rc = 0;
1835         } else if (net->ksnn_ninterfaces == LNET_MAX_INTERFACES) {
1836                 rc = -ENOSPC;
1837         } else {
1838                 iface = &net->ksnn_interfaces[net->ksnn_ninterfaces++];
1839
1840                 iface->ksni_ipaddr = ipaddress;
1841                 iface->ksni_netmask = netmask;
1842                 iface->ksni_nroutes = 0;
1843                 iface->ksni_npeers = 0;
1844
1845                 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
1846                         list_for_each(ptmp, &ksocknal_data.ksnd_peers[i]) {
1847                                 peer = list_entry(ptmp, ksock_peer_t, ksnp_list);
1848
1849                                 for (j = 0; j < peer->ksnp_n_passive_ips; j++)
1850                                         if (peer->ksnp_passive_ips[j] == ipaddress)
1851                                                 iface->ksni_npeers++;
1852
1853                                 list_for_each(rtmp, &peer->ksnp_routes) {
1854                                         route = list_entry(rtmp, ksock_route_t, ksnr_list);
1855
1856                                         if (route->ksnr_myipaddr == ipaddress)
1857                                                 iface->ksni_nroutes++;
1858                                 }
1859                         }
1860                 }
1861
1862                 rc = 0;
1863                 /* NB only new connections will pay attention to the new interface! */
1864         }
1865
1866         write_unlock_bh (&ksocknal_data.ksnd_global_lock);
1867
1868         return (rc);
1869 }
1870
1871 void
1872 ksocknal_peer_del_interface_locked(ksock_peer_t *peer, __u32 ipaddr)
1873 {
1874         struct list_head   *tmp;
1875         struct list_head   *nxt;
1876         ksock_route_t      *route;
1877         ksock_conn_t       *conn;
1878         int                 i;
1879         int                 j;
1880
1881         for (i = 0; i < peer->ksnp_n_passive_ips; i++)
1882                 if (peer->ksnp_passive_ips[i] == ipaddr) {
1883                         for (j = i+1; j < peer->ksnp_n_passive_ips; j++)
1884                                 peer->ksnp_passive_ips[j-1] =
1885                                         peer->ksnp_passive_ips[j];
1886                         peer->ksnp_n_passive_ips--;
1887                         break;
1888                 }
1889
1890         list_for_each_safe(tmp, nxt, &peer->ksnp_routes) {
1891                 route = list_entry (tmp, ksock_route_t, ksnr_list);
1892
1893                 if (route->ksnr_myipaddr != ipaddr)
1894                         continue;
1895
1896                 if (route->ksnr_share_count != 0) {
1897                         /* Manually created; keep, but unbind */
1898                         route->ksnr_myipaddr = 0;
1899                 } else {
1900                         ksocknal_del_route_locked(route);
1901                 }
1902         }
1903
1904         list_for_each_safe(tmp, nxt, &peer->ksnp_conns) {
1905                 conn = list_entry(tmp, ksock_conn_t, ksnc_list);
1906
1907                 if (conn->ksnc_myipaddr == ipaddr)
1908                         ksocknal_close_conn_locked (conn, 0);
1909         }
1910 }
1911
1912 int
1913 ksocknal_del_interface(lnet_ni_t *ni, __u32 ipaddress)
1914 {
1915         ksock_net_t       *net = ni->ni_data;
1916         int                rc = -ENOENT;
1917         struct list_head  *tmp;
1918         struct list_head  *nxt;
1919         ksock_peer_t      *peer;
1920         __u32              this_ip;
1921         int                i;
1922         int                j;
1923
1924         write_lock_bh (&ksocknal_data.ksnd_global_lock);
1925
1926         for (i = 0; i < net->ksnn_ninterfaces; i++) {
1927                 this_ip = net->ksnn_interfaces[i].ksni_ipaddr;
1928
1929                 if (!(ipaddress == 0 ||
1930                       ipaddress == this_ip))
1931                         continue;
1932
1933                 rc = 0;
1934
1935                 for (j = i+1; j < net->ksnn_ninterfaces; j++)
1936                         net->ksnn_interfaces[j-1] =
1937                                 net->ksnn_interfaces[j];
1938
1939                 net->ksnn_ninterfaces--;
1940
1941                 for (j = 0; j < ksocknal_data.ksnd_peer_hash_size; j++) {
1942                         list_for_each_safe(tmp, nxt, &ksocknal_data.ksnd_peers[j]) {
1943                                 peer = list_entry(tmp, ksock_peer_t, ksnp_list);
1944
1945                                 if (peer->ksnp_ni != ni)
1946                                         continue;
1947
1948                                 ksocknal_peer_del_interface_locked(peer, this_ip);
1949                         }
1950                 }
1951         }
1952
1953         write_unlock_bh (&ksocknal_data.ksnd_global_lock);
1954
1955         return (rc);
1956 }
1957
1958 int
1959 ksocknal_ctl(lnet_ni_t *ni, unsigned int cmd, void *arg)
1960 {
1961         struct libcfs_ioctl_data *data = arg;
1962         int rc;
1963
1964         switch(cmd) {
1965         case IOC_LIBCFS_GET_INTERFACE: {
1966                 ksock_net_t       *net = ni->ni_data;
1967                 ksock_interface_t *iface;
1968
1969                 read_lock (&ksocknal_data.ksnd_global_lock);
1970
1971                 if (data->ioc_count < 0 ||
1972                     data->ioc_count >= net->ksnn_ninterfaces) {
1973                         rc = -ENOENT;
1974                 } else {
1975                         rc = 0;
1976                         iface = &net->ksnn_interfaces[data->ioc_count];
1977
1978                         data->ioc_u32[0] = iface->ksni_ipaddr;
1979                         data->ioc_u32[1] = iface->ksni_netmask;
1980                         data->ioc_u32[2] = iface->ksni_npeers;
1981                         data->ioc_u32[3] = iface->ksni_nroutes;
1982                 }
1983
1984                 read_unlock (&ksocknal_data.ksnd_global_lock);
1985                 return rc;
1986         }
1987
1988         case IOC_LIBCFS_ADD_INTERFACE:
1989                 return ksocknal_add_interface(ni,
1990                                               data->ioc_u32[0], /* IP address */
1991                                               data->ioc_u32[1]); /* net mask */
1992
1993         case IOC_LIBCFS_DEL_INTERFACE:
1994                 return ksocknal_del_interface(ni, 
1995                                               data->ioc_u32[0]); /* IP address */
1996
1997         case IOC_LIBCFS_GET_PEER: {
1998                 lnet_process_id_t id = {0,};
1999                 __u32            myip = 0;
2000                 __u32            ip = 0;
2001                 int              port = 0;
2002                 int              conn_count = 0;
2003                 int              share_count = 0;
2004
2005                 rc = ksocknal_get_peer_info(ni, data->ioc_count,
2006                                             &id, &myip, &ip, &port,
2007                                             &conn_count,  &share_count);
2008                 if (rc != 0)
2009                         return rc;
2010                         
2011                 data->ioc_nid    = id.nid;
2012                 data->ioc_count  = share_count;
2013                 data->ioc_u32[0] = ip;
2014                 data->ioc_u32[1] = port;
2015                 data->ioc_u32[2] = myip;
2016                 data->ioc_u32[3] = conn_count;
2017                 data->ioc_u32[4] = id.pid;
2018                 return 0;
2019         }
2020
2021         case IOC_LIBCFS_ADD_PEER: {
2022                 lnet_process_id_t  id = {.nid = data->ioc_nid,
2023                                          .pid = LUSTRE_SRV_LNET_PID};
2024                 return ksocknal_add_peer (ni, id,
2025                                           data->ioc_u32[0], /* IP */
2026                                           data->ioc_u32[1]); /* port */
2027         }
2028         case IOC_LIBCFS_DEL_PEER: {
2029                 lnet_process_id_t  id = {.nid = data->ioc_nid,
2030                                          .pid = LNET_PID_ANY};
2031                 return ksocknal_del_peer (ni, id,
2032                                           data->ioc_u32[0]); /* IP */
2033         }
2034         case IOC_LIBCFS_GET_CONN: {
2035                 int           txmem;
2036                 int           rxmem;
2037                 int           nagle;
2038                 ksock_conn_t *conn = ksocknal_get_conn_by_idx (ni, data->ioc_count);
2039
2040                 if (conn == NULL)
2041                         return -ENOENT;
2042
2043                 ksocknal_lib_get_conn_tunables(conn, &txmem, &rxmem, &nagle);
2044
2045                 data->ioc_count  = txmem;
2046                 data->ioc_nid    = conn->ksnc_peer->ksnp_id.nid;
2047                 data->ioc_flags  = nagle;
2048                 data->ioc_u32[0] = conn->ksnc_ipaddr;
2049                 data->ioc_u32[1] = conn->ksnc_port;
2050                 data->ioc_u32[2] = conn->ksnc_myipaddr;
2051                 data->ioc_u32[3] = conn->ksnc_type;
2052                 data->ioc_u32[4] = conn->ksnc_scheduler -
2053                                    ksocknal_data.ksnd_schedulers;
2054                 data->ioc_u32[5] = rxmem;
2055                 data->ioc_u32[6] = conn->ksnc_peer->ksnp_id.pid;
2056                 ksocknal_conn_decref(conn);
2057                 return 0;
2058         }
2059
2060         case IOC_LIBCFS_CLOSE_CONNECTION: {
2061                 lnet_process_id_t  id = {.nid = data->ioc_nid,
2062                                         .pid = LNET_PID_ANY};
2063
2064                 return ksocknal_close_matching_conns (id,
2065                                                       data->ioc_u32[0]);
2066         }
2067         case IOC_LIBCFS_REGISTER_MYNID:
2068                 /* Ignore if this is a noop */
2069                 if (data->ioc_nid == ni->ni_nid)
2070                         return 0;
2071
2072                 CERROR("obsolete IOC_LIBCFS_REGISTER_MYNID: %s(%s)\n",
2073                        libcfs_nid2str(data->ioc_nid),
2074                        libcfs_nid2str(ni->ni_nid));
2075                 return -EINVAL;
2076
2077         case IOC_LIBCFS_PUSH_CONNECTION: {
2078                 lnet_process_id_t  id = {.nid = data->ioc_nid,
2079                                         .pid = LNET_PID_ANY};
2080                 
2081                 return ksocknal_push(ni, id);
2082         }
2083         default:
2084                 return -EINVAL;
2085         }
2086         /* not reached */
2087 }
2088
2089 void
2090 ksocknal_free_buffers (void)
2091 {
2092         LASSERT (atomic_read(&ksocknal_data.ksnd_nactive_txs) == 0);
2093
2094         if (ksocknal_data.ksnd_schedulers != NULL)
2095                 LIBCFS_FREE (ksocknal_data.ksnd_schedulers,
2096                              sizeof (ksock_sched_t) * ksocknal_data.ksnd_nschedulers);
2097
2098         LIBCFS_FREE (ksocknal_data.ksnd_peers,
2099                      sizeof (struct list_head) *
2100                      ksocknal_data.ksnd_peer_hash_size);
2101
2102         spin_lock(&ksocknal_data.ksnd_tx_lock);
2103
2104         if (!list_empty(&ksocknal_data.ksnd_idle_noop_txs)) {
2105                 struct list_head  zlist;
2106                 ksock_tx_t       *tx;
2107
2108                 list_add(&zlist, &ksocknal_data.ksnd_idle_noop_txs);
2109                 list_del_init(&ksocknal_data.ksnd_idle_noop_txs);
2110                 spin_unlock(&ksocknal_data.ksnd_tx_lock);
2111
2112                 while(!list_empty(&zlist)) {
2113                         tx = list_entry(zlist.next, ksock_tx_t, tx_list);
2114                         list_del(&tx->tx_list);
2115                         LIBCFS_FREE(tx, tx->tx_desc_size);
2116                 }
2117         } else {
2118                 spin_unlock(&ksocknal_data.ksnd_tx_lock);
2119         }
2120 }
2121
2122 void
2123 ksocknal_base_shutdown (void)
2124 {
2125         ksock_sched_t *sched;
2126         int            i;
2127
2128         CDEBUG(D_MALLOC, "before NAL cleanup: kmem %d\n",
2129                atomic_read (&libcfs_kmemory));
2130         LASSERT (ksocknal_data.ksnd_nnets == 0);
2131
2132         switch (ksocknal_data.ksnd_init) {
2133         default:
2134                 LASSERT (0);
2135
2136         case SOCKNAL_INIT_ALL:
2137         case SOCKNAL_INIT_DATA:
2138                 LASSERT (ksocknal_data.ksnd_peers != NULL);
2139                 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
2140                         LASSERT (list_empty (&ksocknal_data.ksnd_peers[i]));
2141                 }
2142                 LASSERT (list_empty (&ksocknal_data.ksnd_enomem_conns));
2143                 LASSERT (list_empty (&ksocknal_data.ksnd_zombie_conns));
2144                 LASSERT (list_empty (&ksocknal_data.ksnd_connd_connreqs));
2145                 LASSERT (list_empty (&ksocknal_data.ksnd_connd_routes));
2146
2147                 if (ksocknal_data.ksnd_schedulers != NULL)
2148                         for (i = 0; i < ksocknal_data.ksnd_nschedulers; i++) {
2149                                 ksock_sched_t *kss =
2150                                         &ksocknal_data.ksnd_schedulers[i];
2151
2152                                 LASSERT (list_empty (&kss->kss_tx_conns));
2153                                 LASSERT (list_empty (&kss->kss_rx_conns));
2154                                 LASSERT (list_empty (&kss->kss_zombie_noop_txs));
2155                                 LASSERT (kss->kss_nconns == 0);
2156                         }
2157
2158                 /* flag threads to terminate; wake and wait for them to die */
2159                 ksocknal_data.ksnd_shuttingdown = 1;
2160                 cfs_waitq_broadcast (&ksocknal_data.ksnd_connd_waitq);
2161                 cfs_waitq_broadcast (&ksocknal_data.ksnd_reaper_waitq);
2162
2163                 if (ksocknal_data.ksnd_schedulers != NULL)
2164                         for (i = 0; i < ksocknal_data.ksnd_nschedulers; i++) {
2165                                 sched = &ksocknal_data.ksnd_schedulers[i];
2166                                 cfs_waitq_broadcast(&sched->kss_waitq);
2167                         }
2168
2169                 i = 4;
2170                 read_lock (&ksocknal_data.ksnd_global_lock);
2171                 while (ksocknal_data.ksnd_nthreads != 0) {
2172                         i++;
2173                         CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET, /* power of 2? */
2174                                "waiting for %d threads to terminate\n",
2175                                 ksocknal_data.ksnd_nthreads);
2176                         read_unlock (&ksocknal_data.ksnd_global_lock);
2177                         cfs_pause(cfs_time_seconds(1));
2178                         read_lock (&ksocknal_data.ksnd_global_lock);
2179                 }
2180                 read_unlock (&ksocknal_data.ksnd_global_lock);
2181
2182                 ksocknal_free_buffers();
2183
2184                 ksocknal_data.ksnd_init = SOCKNAL_INIT_NOTHING;
2185                 break;
2186         }
2187
2188         CDEBUG(D_MALLOC, "after NAL cleanup: kmem %d\n",
2189                atomic_read (&libcfs_kmemory));
2190
2191         PORTAL_MODULE_UNUSE;
2192 }
2193
2194
2195 __u64
2196 ksocknal_new_incarnation (void)
2197 {
2198         struct timeval tv;
2199
2200         /* The incarnation number is the time this module loaded and it
2201          * identifies this particular instance of the socknal.  Hopefully
2202          * we won't be able to reboot more frequently than 1MHz for the
2203          * forseeable future :) */
2204
2205         do_gettimeofday(&tv);
2206
2207         return (((__u64)tv.tv_sec) * 1000000) + tv.tv_usec;
2208 }
2209
2210 int
2211 ksocknal_base_startup (void)
2212 {
2213         int               rc;
2214         int               i;
2215
2216         LASSERT (ksocknal_data.ksnd_init == SOCKNAL_INIT_NOTHING);
2217         LASSERT (ksocknal_data.ksnd_nnets == 0);
2218
2219         memset (&ksocknal_data, 0, sizeof (ksocknal_data)); /* zero pointers */
2220
2221         ksocknal_data.ksnd_peer_hash_size = SOCKNAL_PEER_HASH_SIZE;
2222         LIBCFS_ALLOC (ksocknal_data.ksnd_peers,
2223                       sizeof (struct list_head) * ksocknal_data.ksnd_peer_hash_size);
2224         if (ksocknal_data.ksnd_peers == NULL)
2225                 return -ENOMEM;
2226
2227         for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++)
2228                 CFS_INIT_LIST_HEAD(&ksocknal_data.ksnd_peers[i]);
2229
2230         rwlock_init(&ksocknal_data.ksnd_global_lock);
2231
2232         spin_lock_init (&ksocknal_data.ksnd_reaper_lock);
2233         CFS_INIT_LIST_HEAD (&ksocknal_data.ksnd_enomem_conns);
2234         CFS_INIT_LIST_HEAD (&ksocknal_data.ksnd_zombie_conns);
2235         CFS_INIT_LIST_HEAD (&ksocknal_data.ksnd_deathrow_conns);
2236         cfs_waitq_init(&ksocknal_data.ksnd_reaper_waitq);
2237
2238         spin_lock_init (&ksocknal_data.ksnd_connd_lock);
2239         CFS_INIT_LIST_HEAD (&ksocknal_data.ksnd_connd_connreqs);
2240         CFS_INIT_LIST_HEAD (&ksocknal_data.ksnd_connd_routes);
2241         cfs_waitq_init(&ksocknal_data.ksnd_connd_waitq);
2242
2243         spin_lock_init (&ksocknal_data.ksnd_tx_lock);
2244         CFS_INIT_LIST_HEAD (&ksocknal_data.ksnd_idle_noop_txs);
2245
2246         /* NB memset above zeros whole of ksocknal_data, including
2247          * ksocknal_data.ksnd_irqinfo[all].ksni_valid */
2248
2249         /* flag lists/ptrs/locks initialised */
2250         ksocknal_data.ksnd_init = SOCKNAL_INIT_DATA;
2251         PORTAL_MODULE_USE;
2252
2253         ksocknal_data.ksnd_nschedulers = ksocknal_nsched();
2254         LIBCFS_ALLOC(ksocknal_data.ksnd_schedulers,
2255                      sizeof(ksock_sched_t) * ksocknal_data.ksnd_nschedulers);
2256         if (ksocknal_data.ksnd_schedulers == NULL)
2257                 goto failed;
2258
2259         for (i = 0; i < ksocknal_data.ksnd_nschedulers; i++) {
2260                 ksock_sched_t *kss = &ksocknal_data.ksnd_schedulers[i];
2261
2262                 spin_lock_init (&kss->kss_lock);
2263                 CFS_INIT_LIST_HEAD (&kss->kss_rx_conns);
2264                 CFS_INIT_LIST_HEAD (&kss->kss_tx_conns);
2265                 CFS_INIT_LIST_HEAD (&kss->kss_zombie_noop_txs);
2266                 cfs_waitq_init (&kss->kss_waitq);
2267         }
2268
2269         for (i = 0; i < ksocknal_data.ksnd_nschedulers; i++) {
2270                 rc = ksocknal_thread_start (ksocknal_scheduler,
2271                                             &ksocknal_data.ksnd_schedulers[i]);
2272                 if (rc != 0) {
2273                         CERROR("Can't spawn socknal scheduler[%d]: %d\n",
2274                                i, rc);
2275                         goto failed;
2276                 }
2277         }
2278
2279         /* must have at least 2 connds to remain responsive to accepts while
2280          * connecting */
2281         if (*ksocknal_tunables.ksnd_nconnds < 2)
2282                 *ksocknal_tunables.ksnd_nconnds = 2;
2283         
2284         for (i = 0; i < *ksocknal_tunables.ksnd_nconnds; i++) {
2285                 rc = ksocknal_thread_start (ksocknal_connd, (void *)((long)i));
2286                 if (rc != 0) {
2287                         CERROR("Can't spawn socknal connd: %d\n", rc);
2288                         goto failed;
2289                 }
2290         }
2291
2292         rc = ksocknal_thread_start (ksocknal_reaper, NULL);
2293         if (rc != 0) {
2294                 CERROR ("Can't spawn socknal reaper: %d\n", rc);
2295                 goto failed;
2296         }
2297
2298         /* flag everything initialised */
2299         ksocknal_data.ksnd_init = SOCKNAL_INIT_ALL;
2300
2301         return 0;
2302
2303  failed:
2304         ksocknal_base_shutdown();
2305         return -ENETDOWN;
2306 }
2307
2308 void
2309 ksocknal_shutdown (lnet_ni_t *ni)
2310 {
2311         ksock_net_t      *net = ni->ni_data;
2312         int               i;
2313         lnet_process_id_t  anyid = {.nid = LNET_NID_ANY,
2314                                    .pid = LNET_PID_ANY};
2315
2316         LASSERT(ksocknal_data.ksnd_init == SOCKNAL_INIT_ALL);
2317         LASSERT(ksocknal_data.ksnd_nnets > 0);
2318
2319         spin_lock_bh (&net->ksnn_lock);
2320         net->ksnn_shutdown = 1;                 /* prevent new peers */
2321         spin_unlock_bh (&net->ksnn_lock);
2322
2323         /* Delete all peers */
2324         ksocknal_del_peer(ni, anyid, 0);
2325
2326         /* Wait for all peer state to clean up */
2327         i = 2;
2328         spin_lock_bh (&net->ksnn_lock);
2329         while (net->ksnn_npeers != 0) {
2330                 spin_unlock_bh (&net->ksnn_lock);
2331
2332                 i++;
2333                 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET, /* power of 2? */
2334                        "waiting for %d peers to disconnect\n",
2335                        net->ksnn_npeers);
2336                 cfs_pause(cfs_time_seconds(1));
2337
2338                 spin_lock_bh (&net->ksnn_lock);
2339         }
2340         spin_unlock_bh (&net->ksnn_lock);
2341
2342         for (i = 0; i < net->ksnn_ninterfaces; i++) {
2343                 LASSERT (net->ksnn_interfaces[i].ksni_npeers == 0);
2344                 LASSERT (net->ksnn_interfaces[i].ksni_nroutes == 0);
2345         }
2346
2347         LIBCFS_FREE(net, sizeof(*net));
2348         
2349         ksocknal_data.ksnd_nnets--;
2350         if (ksocknal_data.ksnd_nnets == 0)
2351                 ksocknal_base_shutdown();
2352 }
2353
2354 int
2355 ksocknal_enumerate_interfaces(ksock_net_t *net)
2356 {
2357         char      **names;
2358         int         i;
2359         int         j;
2360         int         rc;
2361         int         n;
2362                 
2363         n = libcfs_ipif_enumerate(&names);
2364         if (n <= 0) {
2365                 CERROR("Can't enumerate interfaces: %d\n", n);
2366                 return n;
2367         }
2368
2369         for (i = j = 0; i < n; i++) {
2370                 int        up;
2371                 __u32      ip;
2372                 __u32      mask;
2373
2374                 if (!strcmp(names[i], "lo")) /* skip the loopback IF */
2375                         continue;
2376
2377                 rc = libcfs_ipif_query(names[i], &up, &ip, &mask);
2378                 if (rc != 0) {
2379                         CWARN("Can't get interface %s info: %d\n",
2380                               names[i], rc);
2381                         continue;
2382                 }
2383                 
2384                 if (!up) {
2385                         CWARN("Ignoring interface %s (down)\n",
2386                               names[i]);
2387                         continue;
2388                 }
2389
2390                 if (j == LNET_MAX_INTERFACES) {
2391                         CWARN("Ignoring interface %s (too many interfaces)\n",
2392                               names[i]);
2393                         continue;
2394                 }
2395
2396                 net->ksnn_interfaces[j].ksni_ipaddr = ip;
2397                 net->ksnn_interfaces[j].ksni_netmask = mask;
2398                 j++;
2399         }
2400
2401         libcfs_ipif_free_enumeration(names, n);
2402         
2403         if (j == 0)
2404                 CERROR("Can't find any usable interfaces\n");
2405         
2406         return j;
2407 }
2408
2409 int
2410 ksocknal_startup (lnet_ni_t *ni)
2411 {
2412         ksock_net_t  *net;
2413         int           rc;
2414         int           i;
2415
2416         LASSERT (ni->ni_lnd == &the_ksocklnd);
2417
2418         if (ksocknal_data.ksnd_init == SOCKNAL_INIT_NOTHING) {
2419                 rc = ksocknal_base_startup();
2420                 if (rc != 0)
2421                         return rc;
2422         }
2423         
2424         LIBCFS_ALLOC(net, sizeof(*net));
2425         if (net == NULL)
2426                 goto fail_0;
2427                 
2428         memset(net, 0, sizeof(*net));
2429         spin_lock_init(&net->ksnn_lock);
2430         net->ksnn_incarnation = ksocknal_new_incarnation();
2431         ni->ni_data = net;
2432         ni->ni_maxtxcredits = *ksocknal_tunables.ksnd_credits;
2433         ni->ni_peertxcredits = *ksocknal_tunables.ksnd_peercredits;
2434         
2435         if (ni->ni_interfaces[0] == NULL) {
2436                 rc = ksocknal_enumerate_interfaces(net);
2437                 if (rc <= 0)
2438                         goto fail_1;
2439
2440                 net->ksnn_ninterfaces = 1;
2441         } else {
2442                 for (i = 0; i < LNET_MAX_INTERFACES; i++) {
2443                         int    up;
2444
2445                         if (ni->ni_interfaces[i] == NULL)
2446                                 break;
2447
2448                         rc = libcfs_ipif_query(
2449                                 ni->ni_interfaces[i], &up,
2450                                 &net->ksnn_interfaces[i].ksni_ipaddr,
2451                                 &net->ksnn_interfaces[i].ksni_netmask);
2452                         
2453                         if (rc != 0) {
2454                                 CERROR("Can't get interface %s info: %d\n",
2455                                        ni->ni_interfaces[i], rc);
2456                                 goto fail_1;
2457                         }
2458                         
2459                         if (!up) {
2460                                 CERROR("Interface %s is down\n",
2461                                        ni->ni_interfaces[i]);
2462                                 goto fail_1;
2463                         }
2464                 }
2465                 net->ksnn_ninterfaces = i;
2466         }
2467
2468         ni->ni_nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid),
2469                                 net->ksnn_interfaces[0].ksni_ipaddr);
2470
2471         ksocknal_data.ksnd_nnets++;
2472
2473         return 0;
2474         
2475  fail_1:
2476         LIBCFS_FREE(net, sizeof(*net));
2477  fail_0:
2478         if (ksocknal_data.ksnd_nnets == 0)
2479                 ksocknal_base_shutdown();
2480
2481         return -ENETDOWN;
2482 }
2483
2484
2485 void __exit
2486 ksocknal_module_fini (void)
2487 {
2488         lnet_unregister_lnd(&the_ksocklnd);
2489         ksocknal_lib_tunables_fini();
2490 }
2491
2492 int __init
2493 ksocknal_module_init (void)
2494 {
2495         int    rc;
2496
2497         /* check ksnr_connected/connecting field large enough */
2498         CLASSERT(SOCKLND_CONN_NTYPES <= 4);
2499         
2500         rc = ksocknal_lib_tunables_init();
2501         if (rc != 0)
2502                 return rc;
2503
2504         lnet_register_lnd(&the_ksocklnd);
2505
2506         return 0;
2507 }
2508
2509 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2510 MODULE_DESCRIPTION("Kernel TCP Socket LND v2.0.0");
2511 MODULE_LICENSE("GPL");
2512
2513 cfs_module(ksocknal, "2.0.0", ksocknal_module_init, ksocknal_module_fini);