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