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