Whamcloud - gitweb
Branch HEAD
[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         /* a closing conn is always ready to tx */
1508         conn->ksnc_tx_ready = 1;
1509
1510         if (!conn->ksnc_tx_scheduled &&
1511             !list_empty(&conn->ksnc_tx_queue)){
1512                 list_add_tail (&conn->ksnc_tx_list,
1513                                &sched->kss_tx_conns);
1514                 conn->ksnc_tx_scheduled = 1;
1515                 /* extra ref for scheduler */
1516                 ksocknal_conn_addref(conn);
1517
1518                 cfs_waitq_signal (&sched->kss_waitq);
1519         }
1520
1521         spin_unlock_bh (&sched->kss_lock);
1522
1523         spin_lock(&peer->ksnp_lock);
1524
1525         list_for_each_safe(tmp, nxt, &peer->ksnp_zc_req_list) {
1526                 tx = list_entry(tmp, ksock_tx_t, tx_zc_list);
1527
1528                 if (tx->tx_conn != conn)
1529                         continue;
1530
1531                 LASSERT (tx->tx_msg.ksm_zc_req_cookie != 0);
1532
1533                 tx->tx_msg.ksm_zc_req_cookie = 0;
1534                 list_del(&tx->tx_zc_list);
1535                 list_add(&tx->tx_zc_list, &zlist);
1536         }
1537
1538         spin_unlock(&peer->ksnp_lock);
1539
1540         list_for_each_safe(tmp, nxt, &zlist) {
1541                 tx = list_entry(tmp, ksock_tx_t, tx_zc_list);
1542
1543                 list_del(&tx->tx_zc_list);
1544                 ksocknal_tx_decref(tx);
1545         }
1546
1547         /* serialise with callbacks */
1548         write_lock_bh (&ksocknal_data.ksnd_global_lock);
1549
1550         ksocknal_lib_reset_callback(conn->ksnc_sock, conn);
1551
1552         /* OK, so this conn may not be completely disengaged from its
1553          * scheduler yet, but it _has_ committed to terminate... */
1554         conn->ksnc_scheduler->kss_nconns--;
1555
1556         if (peer->ksnp_error != 0) {
1557                 /* peer's last conn closed in error */
1558                 LASSERT (list_empty (&peer->ksnp_conns));
1559                 failed = 1;
1560                 peer->ksnp_error = 0;     /* avoid multiple notifications */
1561         }
1562
1563         write_unlock_bh (&ksocknal_data.ksnd_global_lock);
1564
1565         if (failed)
1566                 ksocknal_peer_failed(peer);
1567
1568         /* The socket is closed on the final put; either here, or in
1569          * ksocknal_{send,recv}msg().  Since we set up the linger2 option
1570          * when the connection was established, this will close the socket
1571          * immediately, aborting anything buffered in it. Any hung
1572          * zero-copy transmits will therefore complete in finite time. */
1573         ksocknal_connsock_decref(conn);
1574 }
1575
1576 void
1577 ksocknal_queue_zombie_conn (ksock_conn_t *conn)
1578 {
1579         /* Queue the conn for the reaper to destroy */
1580
1581         LASSERT (atomic_read(&conn->ksnc_conn_refcount) == 0);
1582         spin_lock_bh (&ksocknal_data.ksnd_reaper_lock);
1583
1584         list_add_tail(&conn->ksnc_list, &ksocknal_data.ksnd_zombie_conns);
1585         cfs_waitq_signal(&ksocknal_data.ksnd_reaper_waitq);
1586
1587         spin_unlock_bh (&ksocknal_data.ksnd_reaper_lock);
1588 }
1589
1590 void
1591 ksocknal_destroy_conn (ksock_conn_t *conn)
1592 {
1593         /* Final coup-de-grace of the reaper */
1594         CDEBUG (D_NET, "connection %p\n", conn);
1595
1596         LASSERT (atomic_read (&conn->ksnc_conn_refcount) == 0);
1597         LASSERT (atomic_read (&conn->ksnc_sock_refcount) == 0);
1598         LASSERT (conn->ksnc_sock == NULL);
1599         LASSERT (conn->ksnc_route == NULL);
1600         LASSERT (!conn->ksnc_tx_scheduled);
1601         LASSERT (!conn->ksnc_rx_scheduled);
1602         LASSERT (list_empty(&conn->ksnc_tx_queue));
1603
1604         /* complete current receive if any */
1605         switch (conn->ksnc_rx_state) {
1606         case SOCKNAL_RX_LNET_PAYLOAD:
1607                 CERROR("Completing partial receive from %s"
1608                        ", ip %d.%d.%d.%d:%d, with error\n",
1609                        libcfs_id2str(conn->ksnc_peer->ksnp_id),
1610                        HIPQUAD(conn->ksnc_ipaddr), conn->ksnc_port);
1611                 lnet_finalize (conn->ksnc_peer->ksnp_ni,
1612                                conn->ksnc_cookie, -EIO);
1613                 break;
1614         case SOCKNAL_RX_LNET_HEADER:
1615                 if (conn->ksnc_rx_started)
1616                         CERROR("Incomplete receive of lnet header from %s"
1617                                ", ip %d.%d.%d.%d:%d, with error, protocol: %d.x.\n",
1618                                libcfs_id2str(conn->ksnc_peer->ksnp_id),
1619                                HIPQUAD(conn->ksnc_ipaddr), conn->ksnc_port,
1620                                conn->ksnc_proto->pro_version);
1621                 break;
1622         case SOCKNAL_RX_KSM_HEADER:
1623                 if (conn->ksnc_rx_started)
1624                         CERROR("Incomplete receive of ksock message from %s"
1625                                ", ip %d.%d.%d.%d:%d, with error, protocol: %d.x.\n",
1626                                libcfs_id2str(conn->ksnc_peer->ksnp_id),
1627                                HIPQUAD(conn->ksnc_ipaddr), conn->ksnc_port,
1628                                conn->ksnc_proto->pro_version);
1629                 break;
1630         case SOCKNAL_RX_SLOP:
1631                 if (conn->ksnc_rx_started)
1632                         CERROR("Incomplete receive of slops from %s"
1633                                ", ip %d.%d.%d.%d:%d, with error\n",
1634                                libcfs_id2str(conn->ksnc_peer->ksnp_id),
1635                                HIPQUAD(conn->ksnc_ipaddr), conn->ksnc_port);
1636                break;
1637         default:
1638                 LBUG ();
1639                 break;
1640         }
1641
1642         ksocknal_peer_decref(conn->ksnc_peer);
1643
1644         LIBCFS_FREE (conn, sizeof (*conn));
1645 }
1646
1647 int
1648 ksocknal_close_peer_conns_locked (ksock_peer_t *peer, __u32 ipaddr, int why)
1649 {
1650         ksock_conn_t       *conn;
1651         struct list_head   *ctmp;
1652         struct list_head   *cnxt;
1653         int                 count = 0;
1654
1655         list_for_each_safe (ctmp, cnxt, &peer->ksnp_conns) {
1656                 conn = list_entry (ctmp, ksock_conn_t, ksnc_list);
1657
1658                 if (ipaddr == 0 ||
1659                     conn->ksnc_ipaddr == ipaddr) {
1660                         count++;
1661                         ksocknal_close_conn_locked (conn, why);
1662                 }
1663         }
1664
1665         return (count);
1666 }
1667
1668 int
1669 ksocknal_close_conn_and_siblings (ksock_conn_t *conn, int why)
1670 {
1671         ksock_peer_t     *peer = conn->ksnc_peer;
1672         __u32             ipaddr = conn->ksnc_ipaddr;
1673         int               count;
1674
1675         write_lock_bh (&ksocknal_data.ksnd_global_lock);
1676
1677         count = ksocknal_close_peer_conns_locked (peer, ipaddr, why);
1678
1679         write_unlock_bh (&ksocknal_data.ksnd_global_lock);
1680
1681         return (count);
1682 }
1683
1684 int
1685 ksocknal_close_matching_conns (lnet_process_id_t id, __u32 ipaddr)
1686 {
1687         ksock_peer_t       *peer;
1688         struct list_head   *ptmp;
1689         struct list_head   *pnxt;
1690         int                 lo;
1691         int                 hi;
1692         int                 i;
1693         int                 count = 0;
1694
1695         write_lock_bh (&ksocknal_data.ksnd_global_lock);
1696
1697         if (id.nid != LNET_NID_ANY)
1698                 lo = hi = ksocknal_nid2peerlist(id.nid) - ksocknal_data.ksnd_peers;
1699         else {
1700                 lo = 0;
1701                 hi = ksocknal_data.ksnd_peer_hash_size - 1;
1702         }
1703
1704         for (i = lo; i <= hi; i++) {
1705                 list_for_each_safe (ptmp, pnxt, &ksocknal_data.ksnd_peers[i]) {
1706
1707                         peer = list_entry (ptmp, ksock_peer_t, ksnp_list);
1708
1709                         if (!((id.nid == LNET_NID_ANY || id.nid == peer->ksnp_id.nid) &&
1710                               (id.pid == LNET_PID_ANY || id.pid == peer->ksnp_id.pid)))
1711                                 continue;
1712
1713                         count += ksocknal_close_peer_conns_locked (peer, ipaddr, 0);
1714                 }
1715         }
1716
1717         write_unlock_bh (&ksocknal_data.ksnd_global_lock);
1718
1719         /* wildcards always succeed */
1720         if (id.nid == LNET_NID_ANY || id.pid == LNET_PID_ANY || ipaddr == 0)
1721                 return (0);
1722
1723         return (count == 0 ? -ENOENT : 0);
1724 }
1725
1726 void
1727 ksocknal_notify (lnet_ni_t *ni, lnet_nid_t gw_nid, int alive)
1728 {
1729         /* The router is telling me she's been notified of a change in
1730          * gateway state.... */
1731         lnet_process_id_t  id = {.nid = gw_nid, .pid = LNET_PID_ANY};
1732
1733         CDEBUG (D_NET, "gw %s %s\n", libcfs_nid2str(gw_nid),
1734                 alive ? "up" : "down");
1735
1736         if (!alive) {
1737                 /* If the gateway crashed, close all open connections... */
1738                 ksocknal_close_matching_conns (id, 0);
1739                 return;
1740         }
1741
1742         /* ...otherwise do nothing.  We can only establish new connections
1743          * if we have autroutes, and these connect on demand. */
1744 }
1745
1746 void
1747 ksocknal_push_peer (ksock_peer_t *peer)
1748 {
1749         int               index;
1750         int               i;
1751         struct list_head *tmp;
1752         ksock_conn_t     *conn;
1753
1754         for (index = 0; ; index++) {
1755                 read_lock (&ksocknal_data.ksnd_global_lock);
1756
1757                 i = 0;
1758                 conn = NULL;
1759
1760                 list_for_each (tmp, &peer->ksnp_conns) {
1761                         if (i++ == index) {
1762                                 conn = list_entry (tmp, ksock_conn_t, ksnc_list);
1763                                 ksocknal_conn_addref(conn);
1764                                 break;
1765                         }
1766                 }
1767
1768                 read_unlock (&ksocknal_data.ksnd_global_lock);
1769
1770                 if (conn == NULL)
1771                         break;
1772
1773                 ksocknal_lib_push_conn (conn);
1774                 ksocknal_conn_decref(conn);
1775         }
1776 }
1777
1778 int
1779 ksocknal_push (lnet_ni_t *ni, lnet_process_id_t id)
1780 {
1781         ksock_peer_t      *peer;
1782         struct list_head  *tmp;
1783         int                index;
1784         int                i;
1785         int                j;
1786         int                rc = -ENOENT;
1787
1788         for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
1789                 for (j = 0; ; j++) {
1790                         read_lock (&ksocknal_data.ksnd_global_lock);
1791
1792                         index = 0;
1793                         peer = NULL;
1794
1795                         list_for_each (tmp, &ksocknal_data.ksnd_peers[i]) {
1796                                 peer = list_entry(tmp, ksock_peer_t,
1797                                                   ksnp_list);
1798
1799                                 if (!((id.nid == LNET_NID_ANY ||
1800                                        id.nid == peer->ksnp_id.nid) &&
1801                                       (id.pid == LNET_PID_ANY ||
1802                                        id.pid == peer->ksnp_id.pid))) {
1803                                         peer = NULL;
1804                                         continue;
1805                                 }
1806
1807                                 if (index++ == j) {
1808                                         ksocknal_peer_addref(peer);
1809                                         break;
1810                                 }
1811                         }
1812
1813                         read_unlock (&ksocknal_data.ksnd_global_lock);
1814
1815                         if (peer != NULL) {
1816                                 rc = 0;
1817                                 ksocknal_push_peer (peer);
1818                                 ksocknal_peer_decref(peer);
1819                         }
1820                 }
1821
1822         }
1823
1824         return (rc);
1825 }
1826
1827 int
1828 ksocknal_add_interface(lnet_ni_t *ni, __u32 ipaddress, __u32 netmask)
1829 {
1830         ksock_net_t       *net = ni->ni_data;
1831         ksock_interface_t *iface;
1832         int                rc;
1833         int                i;
1834         int                j;
1835         struct list_head  *ptmp;
1836         ksock_peer_t      *peer;
1837         struct list_head  *rtmp;
1838         ksock_route_t     *route;
1839
1840         if (ipaddress == 0 ||
1841             netmask == 0)
1842                 return (-EINVAL);
1843
1844         write_lock_bh (&ksocknal_data.ksnd_global_lock);
1845
1846         iface = ksocknal_ip2iface(ni, ipaddress);
1847         if (iface != NULL) {
1848                 /* silently ignore dups */
1849                 rc = 0;
1850         } else if (net->ksnn_ninterfaces == LNET_MAX_INTERFACES) {
1851                 rc = -ENOSPC;
1852         } else {
1853                 iface = &net->ksnn_interfaces[net->ksnn_ninterfaces++];
1854
1855                 iface->ksni_ipaddr = ipaddress;
1856                 iface->ksni_netmask = netmask;
1857                 iface->ksni_nroutes = 0;
1858                 iface->ksni_npeers = 0;
1859
1860                 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
1861                         list_for_each(ptmp, &ksocknal_data.ksnd_peers[i]) {
1862                                 peer = list_entry(ptmp, ksock_peer_t, ksnp_list);
1863
1864                                 for (j = 0; j < peer->ksnp_n_passive_ips; j++)
1865                                         if (peer->ksnp_passive_ips[j] == ipaddress)
1866                                                 iface->ksni_npeers++;
1867
1868                                 list_for_each(rtmp, &peer->ksnp_routes) {
1869                                         route = list_entry(rtmp, ksock_route_t, ksnr_list);
1870
1871                                         if (route->ksnr_myipaddr == ipaddress)
1872                                                 iface->ksni_nroutes++;
1873                                 }
1874                         }
1875                 }
1876
1877                 rc = 0;
1878                 /* NB only new connections will pay attention to the new interface! */
1879         }
1880
1881         write_unlock_bh (&ksocknal_data.ksnd_global_lock);
1882
1883         return (rc);
1884 }
1885
1886 void
1887 ksocknal_peer_del_interface_locked(ksock_peer_t *peer, __u32 ipaddr)
1888 {
1889         struct list_head   *tmp;
1890         struct list_head   *nxt;
1891         ksock_route_t      *route;
1892         ksock_conn_t       *conn;
1893         int                 i;
1894         int                 j;
1895
1896         for (i = 0; i < peer->ksnp_n_passive_ips; i++)
1897                 if (peer->ksnp_passive_ips[i] == ipaddr) {
1898                         for (j = i+1; j < peer->ksnp_n_passive_ips; j++)
1899                                 peer->ksnp_passive_ips[j-1] =
1900                                         peer->ksnp_passive_ips[j];
1901                         peer->ksnp_n_passive_ips--;
1902                         break;
1903                 }
1904
1905         list_for_each_safe(tmp, nxt, &peer->ksnp_routes) {
1906                 route = list_entry (tmp, ksock_route_t, ksnr_list);
1907
1908                 if (route->ksnr_myipaddr != ipaddr)
1909                         continue;
1910
1911                 if (route->ksnr_share_count != 0) {
1912                         /* Manually created; keep, but unbind */
1913                         route->ksnr_myipaddr = 0;
1914                 } else {
1915                         ksocknal_del_route_locked(route);
1916                 }
1917         }
1918
1919         list_for_each_safe(tmp, nxt, &peer->ksnp_conns) {
1920                 conn = list_entry(tmp, ksock_conn_t, ksnc_list);
1921
1922                 if (conn->ksnc_myipaddr == ipaddr)
1923                         ksocknal_close_conn_locked (conn, 0);
1924         }
1925 }
1926
1927 int
1928 ksocknal_del_interface(lnet_ni_t *ni, __u32 ipaddress)
1929 {
1930         ksock_net_t       *net = ni->ni_data;
1931         int                rc = -ENOENT;
1932         struct list_head  *tmp;
1933         struct list_head  *nxt;
1934         ksock_peer_t      *peer;
1935         __u32              this_ip;
1936         int                i;
1937         int                j;
1938
1939         write_lock_bh (&ksocknal_data.ksnd_global_lock);
1940
1941         for (i = 0; i < net->ksnn_ninterfaces; i++) {
1942                 this_ip = net->ksnn_interfaces[i].ksni_ipaddr;
1943
1944                 if (!(ipaddress == 0 ||
1945                       ipaddress == this_ip))
1946                         continue;
1947
1948                 rc = 0;
1949
1950                 for (j = i+1; j < net->ksnn_ninterfaces; j++)
1951                         net->ksnn_interfaces[j-1] =
1952                                 net->ksnn_interfaces[j];
1953
1954                 net->ksnn_ninterfaces--;
1955
1956                 for (j = 0; j < ksocknal_data.ksnd_peer_hash_size; j++) {
1957                         list_for_each_safe(tmp, nxt, &ksocknal_data.ksnd_peers[j]) {
1958                                 peer = list_entry(tmp, ksock_peer_t, ksnp_list);
1959
1960                                 if (peer->ksnp_ni != ni)
1961                                         continue;
1962
1963                                 ksocknal_peer_del_interface_locked(peer, this_ip);
1964                         }
1965                 }
1966         }
1967
1968         write_unlock_bh (&ksocknal_data.ksnd_global_lock);
1969
1970         return (rc);
1971 }
1972
1973 int
1974 ksocknal_ctl(lnet_ni_t *ni, unsigned int cmd, void *arg)
1975 {
1976         struct libcfs_ioctl_data *data = arg;
1977         int rc;
1978
1979         switch(cmd) {
1980         case IOC_LIBCFS_GET_INTERFACE: {
1981                 ksock_net_t       *net = ni->ni_data;
1982                 ksock_interface_t *iface;
1983
1984                 read_lock (&ksocknal_data.ksnd_global_lock);
1985
1986                 if (data->ioc_count < 0 ||
1987                     data->ioc_count >= net->ksnn_ninterfaces) {
1988                         rc = -ENOENT;
1989                 } else {
1990                         rc = 0;
1991                         iface = &net->ksnn_interfaces[data->ioc_count];
1992
1993                         data->ioc_u32[0] = iface->ksni_ipaddr;
1994                         data->ioc_u32[1] = iface->ksni_netmask;
1995                         data->ioc_u32[2] = iface->ksni_npeers;
1996                         data->ioc_u32[3] = iface->ksni_nroutes;
1997                 }
1998
1999                 read_unlock (&ksocknal_data.ksnd_global_lock);
2000                 return rc;
2001         }
2002
2003         case IOC_LIBCFS_ADD_INTERFACE:
2004                 return ksocknal_add_interface(ni,
2005                                               data->ioc_u32[0], /* IP address */
2006                                               data->ioc_u32[1]); /* net mask */
2007
2008         case IOC_LIBCFS_DEL_INTERFACE:
2009                 return ksocknal_del_interface(ni,
2010                                               data->ioc_u32[0]); /* IP address */
2011
2012         case IOC_LIBCFS_GET_PEER: {
2013                 lnet_process_id_t id = {0,};
2014                 __u32            myip = 0;
2015                 __u32            ip = 0;
2016                 int              port = 0;
2017                 int              conn_count = 0;
2018                 int              share_count = 0;
2019
2020                 rc = ksocknal_get_peer_info(ni, data->ioc_count,
2021                                             &id, &myip, &ip, &port,
2022                                             &conn_count,  &share_count);
2023                 if (rc != 0)
2024                         return rc;
2025
2026                 data->ioc_nid    = id.nid;
2027                 data->ioc_count  = share_count;
2028                 data->ioc_u32[0] = ip;
2029                 data->ioc_u32[1] = port;
2030                 data->ioc_u32[2] = myip;
2031                 data->ioc_u32[3] = conn_count;
2032                 data->ioc_u32[4] = id.pid;
2033                 return 0;
2034         }
2035
2036         case IOC_LIBCFS_ADD_PEER: {
2037                 lnet_process_id_t  id = {.nid = data->ioc_nid,
2038                                          .pid = LUSTRE_SRV_LNET_PID};
2039                 return ksocknal_add_peer (ni, id,
2040                                           data->ioc_u32[0], /* IP */
2041                                           data->ioc_u32[1]); /* port */
2042         }
2043         case IOC_LIBCFS_DEL_PEER: {
2044                 lnet_process_id_t  id = {.nid = data->ioc_nid,
2045                                          .pid = LNET_PID_ANY};
2046                 return ksocknal_del_peer (ni, id,
2047                                           data->ioc_u32[0]); /* IP */
2048         }
2049         case IOC_LIBCFS_GET_CONN: {
2050                 int           txmem;
2051                 int           rxmem;
2052                 int           nagle;
2053                 ksock_conn_t *conn = ksocknal_get_conn_by_idx (ni, data->ioc_count);
2054
2055                 if (conn == NULL)
2056                         return -ENOENT;
2057
2058                 ksocknal_lib_get_conn_tunables(conn, &txmem, &rxmem, &nagle);
2059
2060                 data->ioc_count  = txmem;
2061                 data->ioc_nid    = conn->ksnc_peer->ksnp_id.nid;
2062                 data->ioc_flags  = nagle;
2063                 data->ioc_u32[0] = conn->ksnc_ipaddr;
2064                 data->ioc_u32[1] = conn->ksnc_port;
2065                 data->ioc_u32[2] = conn->ksnc_myipaddr;
2066                 data->ioc_u32[3] = conn->ksnc_type;
2067                 data->ioc_u32[4] = conn->ksnc_scheduler -
2068                                    ksocknal_data.ksnd_schedulers;
2069                 data->ioc_u32[5] = rxmem;
2070                 data->ioc_u32[6] = conn->ksnc_peer->ksnp_id.pid;
2071                 ksocknal_conn_decref(conn);
2072                 return 0;
2073         }
2074
2075         case IOC_LIBCFS_CLOSE_CONNECTION: {
2076                 lnet_process_id_t  id = {.nid = data->ioc_nid,
2077                                         .pid = LNET_PID_ANY};
2078
2079                 return ksocknal_close_matching_conns (id,
2080                                                       data->ioc_u32[0]);
2081         }
2082         case IOC_LIBCFS_REGISTER_MYNID:
2083                 /* Ignore if this is a noop */
2084                 if (data->ioc_nid == ni->ni_nid)
2085                         return 0;
2086
2087                 CERROR("obsolete IOC_LIBCFS_REGISTER_MYNID: %s(%s)\n",
2088                        libcfs_nid2str(data->ioc_nid),
2089                        libcfs_nid2str(ni->ni_nid));
2090                 return -EINVAL;
2091
2092         case IOC_LIBCFS_PUSH_CONNECTION: {
2093                 lnet_process_id_t  id = {.nid = data->ioc_nid,
2094                                         .pid = LNET_PID_ANY};
2095
2096                 return ksocknal_push(ni, id);
2097         }
2098         default:
2099                 return -EINVAL;
2100         }
2101         /* not reached */
2102 }
2103
2104 void
2105 ksocknal_free_buffers (void)
2106 {
2107         LASSERT (atomic_read(&ksocknal_data.ksnd_nactive_txs) == 0);
2108
2109         if (ksocknal_data.ksnd_schedulers != NULL)
2110                 LIBCFS_FREE (ksocknal_data.ksnd_schedulers,
2111                              sizeof (ksock_sched_t) * ksocknal_data.ksnd_nschedulers);
2112
2113         LIBCFS_FREE (ksocknal_data.ksnd_peers,
2114                      sizeof (struct list_head) *
2115                      ksocknal_data.ksnd_peer_hash_size);
2116
2117         spin_lock(&ksocknal_data.ksnd_tx_lock);
2118
2119         if (!list_empty(&ksocknal_data.ksnd_idle_noop_txs)) {
2120                 struct list_head  zlist;
2121                 ksock_tx_t       *tx;
2122
2123                 list_add(&zlist, &ksocknal_data.ksnd_idle_noop_txs);
2124                 list_del_init(&ksocknal_data.ksnd_idle_noop_txs);
2125                 spin_unlock(&ksocknal_data.ksnd_tx_lock);
2126
2127                 while(!list_empty(&zlist)) {
2128                         tx = list_entry(zlist.next, ksock_tx_t, tx_list);
2129                         list_del(&tx->tx_list);
2130                         LIBCFS_FREE(tx, tx->tx_desc_size);
2131                 }
2132         } else {
2133                 spin_unlock(&ksocknal_data.ksnd_tx_lock);
2134         }
2135 }
2136
2137 void
2138 ksocknal_base_shutdown (void)
2139 {
2140         ksock_sched_t *sched;
2141         int            i;
2142
2143         CDEBUG(D_MALLOC, "before NAL cleanup: kmem %d\n",
2144                atomic_read (&libcfs_kmemory));
2145         LASSERT (ksocknal_data.ksnd_nnets == 0);
2146
2147         switch (ksocknal_data.ksnd_init) {
2148         default:
2149                 LASSERT (0);
2150
2151         case SOCKNAL_INIT_ALL:
2152         case SOCKNAL_INIT_DATA:
2153                 LASSERT (ksocknal_data.ksnd_peers != NULL);
2154                 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
2155                         LASSERT (list_empty (&ksocknal_data.ksnd_peers[i]));
2156                 }
2157                 LASSERT (list_empty (&ksocknal_data.ksnd_enomem_conns));
2158                 LASSERT (list_empty (&ksocknal_data.ksnd_zombie_conns));
2159                 LASSERT (list_empty (&ksocknal_data.ksnd_connd_connreqs));
2160                 LASSERT (list_empty (&ksocknal_data.ksnd_connd_routes));
2161
2162                 if (ksocknal_data.ksnd_schedulers != NULL)
2163                         for (i = 0; i < ksocknal_data.ksnd_nschedulers; i++) {
2164                                 ksock_sched_t *kss =
2165                                         &ksocknal_data.ksnd_schedulers[i];
2166
2167                                 LASSERT (list_empty (&kss->kss_tx_conns));
2168                                 LASSERT (list_empty (&kss->kss_rx_conns));
2169                                 LASSERT (list_empty (&kss->kss_zombie_noop_txs));
2170                                 LASSERT (kss->kss_nconns == 0);
2171                         }
2172
2173                 /* flag threads to terminate; wake and wait for them to die */
2174                 ksocknal_data.ksnd_shuttingdown = 1;
2175                 cfs_waitq_broadcast (&ksocknal_data.ksnd_connd_waitq);
2176                 cfs_waitq_broadcast (&ksocknal_data.ksnd_reaper_waitq);
2177
2178                 if (ksocknal_data.ksnd_schedulers != NULL)
2179                         for (i = 0; i < ksocknal_data.ksnd_nschedulers; i++) {
2180                                 sched = &ksocknal_data.ksnd_schedulers[i];
2181                                 cfs_waitq_broadcast(&sched->kss_waitq);
2182                         }
2183
2184                 i = 4;
2185                 read_lock (&ksocknal_data.ksnd_global_lock);
2186                 while (ksocknal_data.ksnd_nthreads != 0) {
2187                         i++;
2188                         CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET, /* power of 2? */
2189                                "waiting for %d threads to terminate\n",
2190                                 ksocknal_data.ksnd_nthreads);
2191                         read_unlock (&ksocknal_data.ksnd_global_lock);
2192                         cfs_pause(cfs_time_seconds(1));
2193                         read_lock (&ksocknal_data.ksnd_global_lock);
2194                 }
2195                 read_unlock (&ksocknal_data.ksnd_global_lock);
2196
2197                 ksocknal_free_buffers();
2198
2199                 ksocknal_data.ksnd_init = SOCKNAL_INIT_NOTHING;
2200                 break;
2201         }
2202
2203         CDEBUG(D_MALLOC, "after NAL cleanup: kmem %d\n",
2204                atomic_read (&libcfs_kmemory));
2205
2206         PORTAL_MODULE_UNUSE;
2207 }
2208
2209
2210 __u64
2211 ksocknal_new_incarnation (void)
2212 {
2213         struct timeval tv;
2214
2215         /* The incarnation number is the time this module loaded and it
2216          * identifies this particular instance of the socknal.  Hopefully
2217          * we won't be able to reboot more frequently than 1MHz for the
2218          * forseeable future :) */
2219
2220         do_gettimeofday(&tv);
2221
2222         return (((__u64)tv.tv_sec) * 1000000) + tv.tv_usec;
2223 }
2224
2225 int
2226 ksocknal_base_startup (void)
2227 {
2228         int               rc;
2229         int               i;
2230
2231         LASSERT (ksocknal_data.ksnd_init == SOCKNAL_INIT_NOTHING);
2232         LASSERT (ksocknal_data.ksnd_nnets == 0);
2233
2234         memset (&ksocknal_data, 0, sizeof (ksocknal_data)); /* zero pointers */
2235
2236         ksocknal_data.ksnd_peer_hash_size = SOCKNAL_PEER_HASH_SIZE;
2237         LIBCFS_ALLOC (ksocknal_data.ksnd_peers,
2238                       sizeof (struct list_head) * ksocknal_data.ksnd_peer_hash_size);
2239         if (ksocknal_data.ksnd_peers == NULL)
2240                 return -ENOMEM;
2241
2242         for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++)
2243                 CFS_INIT_LIST_HEAD(&ksocknal_data.ksnd_peers[i]);
2244
2245         rwlock_init(&ksocknal_data.ksnd_global_lock);
2246
2247         spin_lock_init (&ksocknal_data.ksnd_reaper_lock);
2248         CFS_INIT_LIST_HEAD (&ksocknal_data.ksnd_enomem_conns);
2249         CFS_INIT_LIST_HEAD (&ksocknal_data.ksnd_zombie_conns);
2250         CFS_INIT_LIST_HEAD (&ksocknal_data.ksnd_deathrow_conns);
2251         cfs_waitq_init(&ksocknal_data.ksnd_reaper_waitq);
2252
2253         spin_lock_init (&ksocknal_data.ksnd_connd_lock);
2254         CFS_INIT_LIST_HEAD (&ksocknal_data.ksnd_connd_connreqs);
2255         CFS_INIT_LIST_HEAD (&ksocknal_data.ksnd_connd_routes);
2256         cfs_waitq_init(&ksocknal_data.ksnd_connd_waitq);
2257
2258         spin_lock_init (&ksocknal_data.ksnd_tx_lock);
2259         CFS_INIT_LIST_HEAD (&ksocknal_data.ksnd_idle_noop_txs);
2260
2261         /* NB memset above zeros whole of ksocknal_data, including
2262          * ksocknal_data.ksnd_irqinfo[all].ksni_valid */
2263
2264         /* flag lists/ptrs/locks initialised */
2265         ksocknal_data.ksnd_init = SOCKNAL_INIT_DATA;
2266         PORTAL_MODULE_USE;
2267
2268         ksocknal_data.ksnd_nschedulers = ksocknal_nsched();
2269         LIBCFS_ALLOC(ksocknal_data.ksnd_schedulers,
2270                      sizeof(ksock_sched_t) * ksocknal_data.ksnd_nschedulers);
2271         if (ksocknal_data.ksnd_schedulers == NULL)
2272                 goto failed;
2273
2274         for (i = 0; i < ksocknal_data.ksnd_nschedulers; i++) {
2275                 ksock_sched_t *kss = &ksocknal_data.ksnd_schedulers[i];
2276
2277                 spin_lock_init (&kss->kss_lock);
2278                 CFS_INIT_LIST_HEAD (&kss->kss_rx_conns);
2279                 CFS_INIT_LIST_HEAD (&kss->kss_tx_conns);
2280                 CFS_INIT_LIST_HEAD (&kss->kss_zombie_noop_txs);
2281                 cfs_waitq_init (&kss->kss_waitq);
2282         }
2283
2284         for (i = 0; i < ksocknal_data.ksnd_nschedulers; i++) {
2285                 rc = ksocknal_thread_start (ksocknal_scheduler,
2286                                             &ksocknal_data.ksnd_schedulers[i]);
2287                 if (rc != 0) {
2288                         CERROR("Can't spawn socknal scheduler[%d]: %d\n",
2289                                i, rc);
2290                         goto failed;
2291                 }
2292         }
2293
2294         /* must have at least 2 connds to remain responsive to accepts while
2295          * connecting */
2296         if (*ksocknal_tunables.ksnd_nconnds < 2)
2297                 *ksocknal_tunables.ksnd_nconnds = 2;
2298
2299         for (i = 0; i < *ksocknal_tunables.ksnd_nconnds; i++) {
2300                 rc = ksocknal_thread_start (ksocknal_connd, (void *)((long)i));
2301                 if (rc != 0) {
2302                         CERROR("Can't spawn socknal connd: %d\n", rc);
2303                         goto failed;
2304                 }
2305         }
2306
2307         rc = ksocknal_thread_start (ksocknal_reaper, NULL);
2308         if (rc != 0) {
2309                 CERROR ("Can't spawn socknal reaper: %d\n", rc);
2310                 goto failed;
2311         }
2312
2313         /* flag everything initialised */
2314         ksocknal_data.ksnd_init = SOCKNAL_INIT_ALL;
2315
2316         return 0;
2317
2318  failed:
2319         ksocknal_base_shutdown();
2320         return -ENETDOWN;
2321 }
2322
2323 void
2324 ksocknal_debug_peerhash (lnet_ni_t *ni)
2325 {
2326         ksock_peer_t     *peer = NULL;
2327         struct list_head *tmp;
2328         int               i;
2329
2330         read_lock (&ksocknal_data.ksnd_global_lock);
2331
2332         for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
2333                 list_for_each (tmp, &ksocknal_data.ksnd_peers[i]) {
2334                         peer = list_entry (tmp, ksock_peer_t, ksnp_list);
2335
2336                         if (peer->ksnp_ni == ni) break;
2337
2338                         peer = NULL;
2339                 }
2340         }
2341
2342         if (peer != NULL) {
2343                 ksock_route_t *route;
2344                 ksock_conn_t  *conn;
2345
2346                 CWARN ("Active peer on shutdown: %s, ref %d, scnt %d, "
2347                        "closing %d, accepting %d, err %d, zcookie "LPU64", "
2348                        "txq %d, zc_req %d\n", libcfs_id2str(peer->ksnp_id),
2349                        atomic_read(&peer->ksnp_refcount),
2350                        peer->ksnp_sharecount, peer->ksnp_closing,
2351                        peer->ksnp_accepting, peer->ksnp_error,
2352                        peer->ksnp_zc_next_cookie,
2353                        !list_empty(&peer->ksnp_tx_queue),
2354                        !list_empty(&peer->ksnp_zc_req_list));
2355
2356                 list_for_each (tmp, &peer->ksnp_routes) {
2357                         route = list_entry(tmp, ksock_route_t, ksnr_list);
2358                         CWARN ("Route: ref %d, schd %d, conn %d, cnted %d, "
2359                                "del %d\n", atomic_read(&route->ksnr_refcount),
2360                                route->ksnr_scheduled, route->ksnr_connecting,
2361                                route->ksnr_connected, route->ksnr_deleted);
2362                 }
2363
2364                 list_for_each (tmp, &peer->ksnp_conns) {
2365                         conn = list_entry(tmp, ksock_conn_t, ksnc_list);
2366                         CWARN ("Conn: ref %d, sref %d, t %d, c %d\n",
2367                                atomic_read(&conn->ksnc_conn_refcount),
2368                                atomic_read(&conn->ksnc_sock_refcount),
2369                                conn->ksnc_type, conn->ksnc_closing);
2370                 }
2371         }
2372
2373         read_unlock (&ksocknal_data.ksnd_global_lock);
2374         return;
2375 }
2376
2377 void
2378 ksocknal_shutdown (lnet_ni_t *ni)
2379 {
2380         ksock_net_t      *net = ni->ni_data;
2381         int               i;
2382         lnet_process_id_t  anyid = {.nid = LNET_NID_ANY,
2383                                    .pid = LNET_PID_ANY};
2384
2385         LASSERT(ksocknal_data.ksnd_init == SOCKNAL_INIT_ALL);
2386         LASSERT(ksocknal_data.ksnd_nnets > 0);
2387
2388         spin_lock_bh (&net->ksnn_lock);
2389         net->ksnn_shutdown = 1;                 /* prevent new peers */
2390         spin_unlock_bh (&net->ksnn_lock);
2391
2392         /* Delete all peers */
2393         ksocknal_del_peer(ni, anyid, 0);
2394
2395         /* Wait for all peer state to clean up */
2396         i = 2;
2397         spin_lock_bh (&net->ksnn_lock);
2398         while (net->ksnn_npeers != 0) {
2399                 spin_unlock_bh (&net->ksnn_lock);
2400
2401                 i++;
2402                 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET, /* power of 2? */
2403                        "waiting for %d peers to disconnect\n",
2404                        net->ksnn_npeers);
2405                 cfs_pause(cfs_time_seconds(1));
2406
2407                 ksocknal_debug_peerhash(ni);
2408
2409                 spin_lock_bh (&net->ksnn_lock);
2410         }
2411         spin_unlock_bh (&net->ksnn_lock);
2412
2413         for (i = 0; i < net->ksnn_ninterfaces; i++) {
2414                 LASSERT (net->ksnn_interfaces[i].ksni_npeers == 0);
2415                 LASSERT (net->ksnn_interfaces[i].ksni_nroutes == 0);
2416         }
2417
2418         LIBCFS_FREE(net, sizeof(*net));
2419
2420         ksocknal_data.ksnd_nnets--;
2421         if (ksocknal_data.ksnd_nnets == 0)
2422                 ksocknal_base_shutdown();
2423 }
2424
2425 int
2426 ksocknal_enumerate_interfaces(ksock_net_t *net)
2427 {
2428         char      **names;
2429         int         i;
2430         int         j;
2431         int         rc;
2432         int         n;
2433
2434         n = libcfs_ipif_enumerate(&names);
2435         if (n <= 0) {
2436                 CERROR("Can't enumerate interfaces: %d\n", n);
2437                 return n;
2438         }
2439
2440         for (i = j = 0; i < n; i++) {
2441                 int        up;
2442                 __u32      ip;
2443                 __u32      mask;
2444
2445                 if (!strcmp(names[i], "lo")) /* skip the loopback IF */
2446                         continue;
2447
2448                 rc = libcfs_ipif_query(names[i], &up, &ip, &mask);
2449                 if (rc != 0) {
2450                         CWARN("Can't get interface %s info: %d\n",
2451                               names[i], rc);
2452                         continue;
2453                 }
2454
2455                 if (!up) {
2456                         CWARN("Ignoring interface %s (down)\n",
2457                               names[i]);
2458                         continue;
2459                 }
2460
2461                 if (j == LNET_MAX_INTERFACES) {
2462                         CWARN("Ignoring interface %s (too many interfaces)\n",
2463                               names[i]);
2464                         continue;
2465                 }
2466
2467                 net->ksnn_interfaces[j].ksni_ipaddr = ip;
2468                 net->ksnn_interfaces[j].ksni_netmask = mask;
2469                 j++;
2470         }
2471
2472         libcfs_ipif_free_enumeration(names, n);
2473
2474         if (j == 0)
2475                 CERROR("Can't find any usable interfaces\n");
2476
2477         return j;
2478 }
2479
2480 int
2481 ksocknal_startup (lnet_ni_t *ni)
2482 {
2483         ksock_net_t  *net;
2484         int           rc;
2485         int           i;
2486
2487         LASSERT (ni->ni_lnd == &the_ksocklnd);
2488
2489         if (ksocknal_data.ksnd_init == SOCKNAL_INIT_NOTHING) {
2490                 rc = ksocknal_base_startup();
2491                 if (rc != 0)
2492                         return rc;
2493         }
2494
2495         LIBCFS_ALLOC(net, sizeof(*net));
2496         if (net == NULL)
2497                 goto fail_0;
2498
2499         memset(net, 0, sizeof(*net));
2500         spin_lock_init(&net->ksnn_lock);
2501         net->ksnn_incarnation = ksocknal_new_incarnation();
2502         ni->ni_data = net;
2503         ni->ni_maxtxcredits = *ksocknal_tunables.ksnd_credits;
2504         ni->ni_peertxcredits = *ksocknal_tunables.ksnd_peercredits;
2505
2506         if (ni->ni_interfaces[0] == NULL) {
2507                 rc = ksocknal_enumerate_interfaces(net);
2508                 if (rc <= 0)
2509                         goto fail_1;
2510
2511                 net->ksnn_ninterfaces = 1;
2512         } else {
2513                 for (i = 0; i < LNET_MAX_INTERFACES; i++) {
2514                         int    up;
2515
2516                         if (ni->ni_interfaces[i] == NULL)
2517                                 break;
2518
2519                         rc = libcfs_ipif_query(
2520                                 ni->ni_interfaces[i], &up,
2521                                 &net->ksnn_interfaces[i].ksni_ipaddr,
2522                                 &net->ksnn_interfaces[i].ksni_netmask);
2523
2524                         if (rc != 0) {
2525                                 CERROR("Can't get interface %s info: %d\n",
2526                                        ni->ni_interfaces[i], rc);
2527                                 goto fail_1;
2528                         }
2529
2530                         if (!up) {
2531                                 CERROR("Interface %s is down\n",
2532                                        ni->ni_interfaces[i]);
2533                                 goto fail_1;
2534                         }
2535                 }
2536                 net->ksnn_ninterfaces = i;
2537         }
2538
2539         ni->ni_nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid),
2540                                 net->ksnn_interfaces[0].ksni_ipaddr);
2541
2542         ksocknal_data.ksnd_nnets++;
2543
2544         return 0;
2545
2546  fail_1:
2547         LIBCFS_FREE(net, sizeof(*net));
2548  fail_0:
2549         if (ksocknal_data.ksnd_nnets == 0)
2550                 ksocknal_base_shutdown();
2551
2552         return -ENETDOWN;
2553 }
2554
2555
2556 void __exit
2557 ksocknal_module_fini (void)
2558 {
2559         lnet_unregister_lnd(&the_ksocklnd);
2560         ksocknal_lib_tunables_fini();
2561 }
2562
2563 int __init
2564 ksocknal_module_init (void)
2565 {
2566         int    rc;
2567
2568         /* check ksnr_connected/connecting field large enough */
2569         CLASSERT(SOCKLND_CONN_NTYPES <= 4);
2570
2571         rc = ksocknal_lib_tunables_init();
2572         if (rc != 0)
2573                 return rc;
2574
2575         lnet_register_lnd(&the_ksocklnd);
2576
2577         return 0;
2578 }
2579
2580 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2581 MODULE_DESCRIPTION("Kernel TCP Socket LND v2.0.0");
2582 MODULE_LICENSE("GPL");
2583
2584 cfs_module(ksocknal, "2.0.0", ksocknal_module_init, ksocknal_module_fini);