Whamcloud - gitweb
* landed unified portals (b_hd_cleanup_merge_singleportals) on HEAD
[fs/lustre-release.git] / lnet / utils / portals.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of Portals, http://www.sf.net/projects/lustre/
7  *
8  *   Portals is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Portals is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Portals; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #include <stdio.h>
24 #include <sys/types.h>
25 #ifdef HAVE_NETDB_H
26 #include <netdb.h>
27 #endif
28 #include <sys/socket.h>
29 #ifdef HAVE_NETINET_TCP_H
30 #include <netinet/tcp.h>
31 #endif
32 #include <stdlib.h>
33 #include <string.h>
34 #include <fcntl.h>
35 #include "ioctl.h"
36 #include <sys/ioctl.h>
37 #include <errno.h>
38 #include <unistd.h>
39 #include <time.h>
40 #include <stdarg.h>
41 #include <endian.h>
42
43 #ifdef __CYGWIN__
44
45 #include <netinet/in.h>
46
47 #endif /* __CYGWIN__ */
48  
49 #include <portals/api-support.h>
50 #include <portals/ptlctl.h>
51 #include <portals/list.h>
52 #include <portals/lib-types.h>
53 #include <portals/socknal.h>
54 #include "parser.h"
55
56 unsigned int portal_debug;
57 unsigned int portal_printk;
58
59 static unsigned int g_nal = 0;
60
61 typedef struct
62 {
63         char *name;
64         int   num;
65 } name2num_t;
66
67 static name2num_t nalnames[] = {
68         {"any",         0},
69         {"tcp",         SOCKNAL},
70         {"elan",        QSWNAL},
71         {"gm",          GMNAL},
72         {"openib",      OPENIBNAL},
73         {"iib",         IIBNAL},
74         {NULL,          -1}
75 };
76
77 static cfg_record_cb_t g_record_cb;
78
79 /* Convert a string boolean to an int; "enable" -> 1 */
80 int ptl_parse_bool (int *b, char *str) {
81         if (!strcasecmp (str, "no") ||
82             !strcasecmp (str, "n") ||
83             !strcasecmp (str, "off") ||
84             !strcasecmp (str, "down") ||
85             !strcasecmp (str, "disable"))
86         {
87                 *b = 0;
88                 return (0);
89         }
90         
91         if (!strcasecmp (str, "yes") ||
92             !strcasecmp (str, "y") ||
93             !strcasecmp (str, "on") ||
94             !strcasecmp (str, "up") ||
95             !strcasecmp (str, "enable"))
96         {
97                 *b = 1;
98                 return (0);
99         }
100         
101         return (-1);
102 }
103
104 /* Convert human readable size string to and int; "1k" -> 1000 */
105 int ptl_parse_size (int *sizep, char *str) {
106         int size;
107         char mod[32];
108
109         switch (sscanf (str, "%d%1[gGmMkK]", &size, mod)) {
110         default:
111                 return (-1);
112
113         case 1:
114                 *sizep = size;
115                 return (0);
116
117         case 2:
118                 switch (*mod) {
119                 case 'g':
120                 case 'G':
121                         *sizep = size << 30;
122                         return (0);
123
124                 case 'm':
125                 case 'M':
126                         *sizep = size << 20;
127                         return (0);
128
129                 case 'k':
130                 case 'K':
131                         *sizep = size << 10;
132                         return (0);
133
134                 default:
135                         *sizep = size;
136                         return (0);
137                 }
138         }
139 }
140
141 int 
142 ptl_set_cfg_record_cb(cfg_record_cb_t cb)
143 {
144         g_record_cb = cb;
145         return 0;
146 }
147
148 int 
149 pcfg_ioctl(struct portals_cfg *pcfg) 
150 {
151         int rc;
152
153         if (pcfg->pcfg_nal ==0)
154                 pcfg->pcfg_nal    = g_nal;
155
156         if (g_record_cb) {
157                 rc = g_record_cb(PORTALS_CFG_TYPE, sizeof(*pcfg), pcfg);
158         } else {
159                 struct portal_ioctl_data data;
160                 PORTAL_IOC_INIT (data);
161                 data.ioc_pbuf1   = (char*)pcfg;
162                 data.ioc_plen1   = sizeof(*pcfg);
163                 /* XXX liblustre hack XXX */
164                 data.ioc_nal_cmd = pcfg->pcfg_command;
165                 data.ioc_nid = pcfg->pcfg_nid;
166
167                 rc = l_ioctl (PORTALS_DEV_ID, IOC_PORTAL_NAL_CMD, &data);
168         }
169
170         return (rc);
171 }
172
173
174
175 static name2num_t *
176 name2num_lookup_name (name2num_t *table, char *str)
177 {
178         while (table->name != NULL)
179                 if (!strcmp (str, table->name))
180                         return (table);
181                 else
182                         table++;
183         return (NULL);
184 }
185
186 static name2num_t *
187 name2num_lookup_num (name2num_t *table, int num)
188 {
189         while (table->name != NULL)
190                 if (num == table->num)
191                         return (table);
192                 else
193                         table++;
194         return (NULL);
195 }
196
197 int
198 ptl_name2nal (char *str)
199 {
200         name2num_t *e = name2num_lookup_name (nalnames, str);
201
202         return ((e == NULL) ? -1 : e->num);
203 }
204
205 static char *
206 nal2name (int nal)
207 {
208         name2num_t *e = name2num_lookup_num (nalnames, nal);
209
210         return ((e == NULL) ? "???" : e->name);
211 }
212
213 #ifdef HAVE_GETHOSTBYNAME
214 static struct hostent *
215 ptl_gethostbyname(char * hname) {
216         struct hostent *he;
217         he = gethostbyname(hname);
218         if (!he) {
219                 switch(h_errno) {
220                 case HOST_NOT_FOUND:
221                 case NO_ADDRESS:
222                         fprintf(stderr, "Unable to resolve hostname: %s\n",
223                                 hname);
224                         break;
225                 default:
226                         fprintf(stderr, "gethostbyname error: %s\n",
227                                 strerror(errno));
228                         break;
229                 }
230                 return NULL;
231         }
232         return he;
233 }
234 #endif
235
236 int
237 ptl_parse_port (int *port, char *str)
238 {
239         char      *end;
240         
241         *port = strtol (str, &end, 0);
242
243         if (*end == 0 &&                        /* parsed whole string */
244             *port > 0 && *port < 65536)         /* minimal sanity check */
245                 return (0);
246         
247         return (-1);
248 }
249
250 int
251 ptl_parse_time (time_t *t, char *str) 
252 {
253         char          *end;
254         int            n;
255         struct tm      tm;
256         
257         *t = strtol (str, &end, 0);
258         if (*end == 0) /* parsed whole string */
259                 return (0);
260         
261         memset (&tm, 0, sizeof (tm));
262         n = sscanf (str, "%d-%d-%d-%d:%d:%d",
263                     &tm.tm_year, &tm.tm_mon, &tm.tm_mday, 
264                     &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
265         if (n != 6)
266                 return (-1);
267         
268         tm.tm_mon--;                    /* convert to 0 == Jan */
269         tm.tm_year -= 1900;             /* y2k quirk */
270         tm.tm_isdst = -1;               /* dunno if it's daylight savings... */
271         
272         *t = mktime (&tm);
273         if (*t == (time_t)-1)
274                 return (-1);
275                         
276         return (0);
277 }
278
279 int
280 ptl_parse_ipquad (__u32 *ipaddrp, char *str)
281 {
282         int             a;
283         int             b;
284         int             c;
285         int             d;
286
287         if (sscanf (str, "%d.%d.%d.%d", &a, &b, &c, &d) == 4 &&
288             (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
289             (c & ~0xff) == 0 && (d & ~0xff) == 0)
290         {
291                 *ipaddrp = (a<<24)|(b<<16)|(c<<8)|d;
292                 return (0);
293         }
294
295         return (-1);
296 }
297
298 int
299 ptl_parse_ipaddr (__u32 *ipaddrp, char *str)
300 {
301 #ifdef HAVE_GETHOSTBYNAME
302         struct hostent *he;
303 #endif
304
305         if (!strcmp (str, "_all_")) 
306         {
307                 *ipaddrp = 0;
308                 return (0);
309         }
310
311         if (ptl_parse_ipquad(ipaddrp, str) == 0)
312                 return (0);
313
314 #if HAVE_GETHOSTBYNAME        
315         if ((('a' <= str[0] && str[0] <= 'z') ||
316              ('A' <= str[0] && str[0] <= 'Z')) &&
317              (he = ptl_gethostbyname (str)) != NULL)
318         {
319                 __u32 addr = *(__u32 *)he->h_addr;
320
321                 *ipaddrp = ntohl(addr);         /* HOST byte order */
322                 return (0);
323         }
324 #endif
325
326         return (-1);
327 }
328
329 char *
330 ptl_ipaddr_2_str (__u32 ipaddr, char *str, int lookup)
331 {
332 #ifdef HAVE_GETHOSTBYNAME
333         __u32           net_ip;
334         struct hostent *he;
335
336         if (lookup) {
337                 net_ip = htonl (ipaddr);
338                 he = gethostbyaddr (&net_ip, sizeof (net_ip), AF_INET);
339                 if (he != NULL) {
340                         strcpy(str, he->h_name);
341                         return (str);
342                 }
343         }
344 #endif
345
346         sprintf (str, "%d.%d.%d.%d",
347                  (ipaddr >> 24) & 0xff, (ipaddr >> 16) & 0xff,
348                  (ipaddr >> 8) & 0xff, ipaddr & 0xff);
349         return (str);
350 }
351
352 int
353 ptl_parse_nid (ptl_nid_t *nidp, char *str)
354 {
355         __u32               ipaddr;
356         char               *end;
357         unsigned long long  ullval;
358         
359         if (!strcmp (str, "_all_")) {
360                 *nidp = PTL_NID_ANY;
361                 return (0);
362         }
363
364         if (ptl_parse_ipaddr (&ipaddr, str) == 0) {
365                 *nidp = (ptl_nid_t)ipaddr;
366                 return (0);
367         }
368
369         ullval = strtoull(str, &end, 0);
370         if (*end == 0) {
371                 /* parsed whole string */
372                 *nidp = (ptl_nid_t)ullval;
373                 return (0);
374         }
375
376         return (-1);
377 }
378
379 __u64 ptl_nid2u64(ptl_nid_t nid)
380 {
381         switch (sizeof (nid)) {
382         case 8:
383                 return (nid);
384         case 4:
385                 return ((__u32)nid);
386         default:
387                 fprintf(stderr, "Unexpected sizeof(ptl_nid_t) == %u\n", sizeof(nid));
388                 abort();
389                 /* notreached */
390                 return (-1);
391         }
392 }
393
394 char *
395 ptl_nid2str (char *buffer, ptl_nid_t nid)
396 {
397         __u64           nid64 = ptl_nid2u64(nid);
398 #ifdef HAVE_GETHOSTBYNAME
399         struct hostent *he = 0;
400
401         /* Don't try to resolve NIDs that are e.g. Elan host IDs.  Assume
402          * TCP addresses in the 0.x.x.x subnet are not in use.  This can
403          * happen on routers and slows things down a _lot_.  Bug 3442. */
404         if (nid & 0xff000000) {
405                 __u32 addr = htonl((__u32)nid); /* back to NETWORK byte order */
406
407                 he = gethostbyaddr ((const char *)&addr, sizeof (addr), AF_INET);
408         }
409
410         if (he != NULL)
411                 sprintf(buffer, "%#x:%s", (int)(nid64 >> 32), he->h_name);
412         else
413 #endif /* HAVE_GETHOSTBYNAME */
414                 sprintf(buffer, LPX64, nid64);
415
416         return (buffer);
417 }
418
419 int g_nal_is_set () 
420 {
421         if (g_nal == 0) {
422                 fprintf (stderr, "Error: you must run the 'network' command first.\n");
423                 return (0);
424         }
425
426         return (1);
427 }
428
429 int g_nal_is_compatible (char *cmd, ...)
430 {
431         va_list       ap;
432         int           nal;
433
434         if (!g_nal_is_set ())
435                 return (0);
436
437         va_start (ap, cmd);
438
439         do {
440                 nal = va_arg (ap, int);
441         } while (nal != 0 && nal != g_nal);
442         
443         va_end (ap);
444         
445         if (g_nal == nal)
446                 return (1);
447
448         if (cmd != NULL) {
449                 /* Don't complain verbosely if we've not been passed a command
450                  * name to complain about! */
451                 fprintf (stderr, "Command %s not compatible with nal %s\n",
452                          cmd, nal2name (g_nal));
453         }
454         return (0);
455 }
456
457 int
458 sock_write (int cfd, void *buffer, int nob)
459 {
460         while (nob > 0)
461         {
462                 int rc = write (cfd, buffer, nob);
463
464                 if (rc < 0)
465                 {
466                         if (errno == EINTR)
467                                 continue;
468                         
469                         return (rc);
470                 }
471
472                 if (rc == 0)
473                 {
474                         fprintf (stderr, "Unexpected zero sock_write\n");
475                         abort();
476                 }
477
478                 nob -= rc;
479                 buffer = (char *)buffer + nob;
480         }
481         
482         return (0);
483 }
484
485 int
486 sock_read (int cfd, void *buffer, int nob)
487 {
488         while (nob > 0)
489         {
490                 int rc = read (cfd, buffer, nob);
491                 
492                 if (rc < 0)
493                 {
494                         if (errno == EINTR)
495                                 continue;
496                         
497                         return (rc);
498                 }
499                 
500                 if (rc == 0)                    /* EOF */
501                 {
502                         errno = ECONNABORTED;
503                         return (-1);
504                 }
505                 
506                 nob -= rc;
507                 buffer = (char *)buffer + nob;
508         }
509         
510         return (0);
511 }
512
513 int ptl_initialize(int argc, char **argv) 
514 {
515         register_ioc_dev(PORTALS_DEV_ID, PORTALS_DEV_PATH);
516         return 0;
517 }
518
519
520 int jt_ptl_network(int argc, char **argv)
521 {
522         name2num_t *entry;
523         int         nal;
524         
525         if (argc == 2 &&
526             (nal = ptl_name2nal (argv[1])) >= 0) {
527                 g_nal = nal;
528                 return (0);
529         }
530                 
531         fprintf(stderr, "usage: %s \n", argv[0]);
532         for (entry = nalnames; entry->name != NULL; entry++)
533                 fprintf (stderr, "%s%s", entry == nalnames ? "<" : "|", entry->name);
534         fprintf(stderr, ">\n");
535         return (-1);
536 }
537
538 int
539 jt_ptl_print_interfaces (int argc, char **argv)
540 {
541         struct portals_cfg       pcfg;
542         char                     buffer[3][64];
543         int                      index;
544         int                      rc;
545
546         if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
547                 return -1;
548
549         for (index = 0;;index++) {
550                 PCFG_INIT (pcfg, NAL_CMD_GET_INTERFACE);
551                 pcfg.pcfg_count = index;
552
553                 rc = pcfg_ioctl (&pcfg);
554                 if (rc != 0)
555                         break;
556
557                 printf ("%s: (%s/%s) npeer %d nroute %d\n",
558                         ptl_ipaddr_2_str(pcfg.pcfg_id, buffer[2], 1),
559                         ptl_ipaddr_2_str(pcfg.pcfg_id, buffer[0], 0),
560                         ptl_ipaddr_2_str(pcfg.pcfg_misc, buffer[1], 0),
561                         pcfg.pcfg_fd, pcfg.pcfg_count);
562         }
563
564         if (index == 0)
565                 printf ("<no interfaces>\n");
566         return 0;
567 }
568
569 int
570 jt_ptl_add_interface (int argc, char **argv)
571 {
572         struct portals_cfg       pcfg;
573         __u32                    ipaddr;
574         int                      rc;
575         __u32                    netmask = 0xffffff00;
576         int                      i;
577         int                      count;
578         char                    *end;
579
580         if (argc < 2 || argc > 3) {
581                 fprintf (stderr, "usage: %s ipaddr [netmask]\n", argv[0]);
582                 return 0;
583         }
584
585         if (!g_nal_is_compatible(argv[0], SOCKNAL, 0))
586                 return -1;
587
588         if (ptl_parse_ipaddr(&ipaddr, argv[1]) != 0) {
589                 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
590                 return -1;
591         }
592
593         if (argc > 2 ) {
594                 count = strtol(argv[2], &end, 0);
595                 if (count > 0 && count < 32 && *end == 0) {
596                         netmask = 0;
597                         for (i = count; i > 0; i--)
598                                 netmask = netmask|(1<<(32-i));
599                 } else if (ptl_parse_ipquad(&netmask, argv[2]) != 0) {
600                         fprintf (stderr, "Can't parse netmask: %s\n", argv[2]);
601                         return -1;
602                 }
603         }
604
605         PCFG_INIT(pcfg, NAL_CMD_ADD_INTERFACE);
606         pcfg.pcfg_id     = ipaddr;
607         pcfg.pcfg_misc   = netmask;
608
609         rc = pcfg_ioctl (&pcfg);
610         if (rc != 0) {
611                 fprintf (stderr, "failed to add interface: %s\n",
612                          strerror (errno));
613                 return -1;
614         }
615
616         return 0;
617 }
618
619 int
620 jt_ptl_del_interface (int argc, char **argv)
621 {
622         struct portals_cfg       pcfg;
623         int                      rc;
624         __u32                    ipaddr = 0;
625
626         if (argc > 2) {
627                 fprintf (stderr, "usage: %s [ipaddr]\n", argv[0]);
628                 return 0;
629         }
630
631         if (!g_nal_is_compatible(argv[0], SOCKNAL, 0))
632                 return -1;
633
634         if (argc == 2 &&
635             ptl_parse_ipaddr(&ipaddr, argv[1]) != 0) {
636                 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
637                 return -1;
638         }
639         
640         PCFG_INIT(pcfg, NAL_CMD_DEL_INTERFACE);
641         pcfg.pcfg_id = ipaddr;
642
643         rc = pcfg_ioctl (&pcfg);
644         if (rc != 0) {
645                 fprintf (stderr, "failed to delete interface: %s\n",
646                          strerror (errno));
647                 return -1;
648         }
649
650         return 0;
651 }
652
653 int
654 jt_ptl_print_peers (int argc, char **argv)
655 {
656         struct portals_cfg       pcfg;
657         char                     buffer[2][64];
658         int                      index;
659         int                      rc;
660
661         if (!g_nal_is_compatible (argv[0], SOCKNAL, OPENIBNAL, IIBNAL, 0))
662                 return -1;
663
664         for (index = 0;;index++) {
665                 PCFG_INIT (pcfg, NAL_CMD_GET_PEER);
666                 pcfg.pcfg_count   = index;
667
668                 rc = pcfg_ioctl (&pcfg);
669                 if (rc != 0)
670                         break;
671
672                 if (g_nal_is_compatible(NULL, SOCKNAL, 0))
673                         printf (LPX64"[%d]%s@%s:%d #%d\n",
674                                 pcfg.pcfg_nid, pcfg.pcfg_wait,
675                                 ptl_ipaddr_2_str (pcfg.pcfg_size, buffer[0], 1),
676                                 ptl_ipaddr_2_str (pcfg.pcfg_id, buffer[1], 1),
677                                 pcfg.pcfg_misc, pcfg.pcfg_count);
678                 else
679                         printf (LPX64"[%d]\n",
680                                 pcfg.pcfg_nid, pcfg.pcfg_wait);
681         }
682
683         if (index == 0)
684                 printf ("<no peers>\n");
685         return 0;
686 }
687
688 int 
689 jt_ptl_add_peer (int argc, char **argv)
690 {
691         struct portals_cfg       pcfg;
692         ptl_nid_t                nid;
693         __u32                    ip = 0;
694         int                      port = 0;
695         int                      rc;
696
697         if (!g_nal_is_compatible (argv[0], SOCKNAL, OPENIBNAL, IIBNAL, 0))
698                 return -1;
699
700         if (g_nal_is_compatible(NULL, SOCKNAL, 0)) {
701                 if (argc != 4) {
702                         fprintf (stderr, "usage(tcp): %s nid ipaddr port\n", 
703                                  argv[0]);
704                         return 0;
705                 }
706         } else if (argc != 2) {
707                 fprintf (stderr, "usage(openib,iib): %s nid\n", argv[0]);
708                 return 0;
709         }
710
711         if (ptl_parse_nid (&nid, argv[1]) != 0 ||
712                 nid == PTL_NID_ANY) {
713                 fprintf (stderr, "Can't parse NID: %s\n", argv[1]);
714                 return -1;
715         }
716
717         if (g_nal_is_compatible (NULL, SOCKNAL, 0)) {
718                 if (ptl_parse_ipaddr (&ip, argv[2]) != 0) {
719                         fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
720                         return -1;
721                 }
722
723                 if (ptl_parse_port (&port, argv[3]) != 0) {
724                         fprintf (stderr, "Can't parse port: %s\n", argv[3]);
725                         return -1;
726                 }
727         }
728
729         PCFG_INIT(pcfg, NAL_CMD_ADD_PEER);
730         pcfg.pcfg_nid     = nid;
731         pcfg.pcfg_id      = ip;
732         pcfg.pcfg_misc    = port;
733
734         rc = pcfg_ioctl (&pcfg);
735         if (rc != 0) {
736                 fprintf (stderr, "failed to add peer: %s\n",
737                          strerror (errno));
738                 return -1;
739         }
740         
741         return 0;
742 }
743
744 int 
745 jt_ptl_del_peer (int argc, char **argv)
746 {
747         struct portals_cfg       pcfg;
748         ptl_nid_t                nid = PTL_NID_ANY;
749         __u32                    ip = 0;
750         int                      single_share = 0;
751         int                      argidx;
752         int                      rc;
753
754         if (!g_nal_is_compatible (argv[0], SOCKNAL, OPENIBNAL, IIBNAL, 0))
755                 return -1;
756
757         if (g_nal_is_compatible(NULL, SOCKNAL, 0)) {
758                 if (argc > 4) {
759                         fprintf (stderr, "usage: %s [nid] [ipaddr] [single_share]\n",
760                                  argv[0]);
761                         return 0;
762                 }
763         } else if (argc > 3) {
764                 fprintf (stderr, "usage: %s [nid] [single_share]\n", argv[0]);
765                 return 0;
766         }
767                 
768         if (argc > 1 &&
769             ptl_parse_nid (&nid, argv[1]) != 0) {
770                 fprintf (stderr, "Can't parse nid: %s\n", argv[1]);
771                 return -1;
772         }
773
774         argidx = 2;
775         if (g_nal_is_compatible(NULL, SOCKNAL, 0)) {
776                 if (argc > argidx &&
777                     ptl_parse_ipaddr (&ip, argv[argidx]) != 0) {
778                         fprintf (stderr, "Can't parse ip addr: %s\n",
779                                  argv[argidx]);
780                         return -1;
781                 }
782                 argidx++;
783         }
784         
785         if (argc > argidx) {
786                 if (!strcmp (argv[argidx], "single_share")) {
787                         single_share = 1;
788                 } else {
789                         fprintf (stderr, "Unrecognised arg %s'\n", argv[3]);
790                         return -1;
791                 }
792         }
793
794         PCFG_INIT(pcfg, NAL_CMD_DEL_PEER);
795         pcfg.pcfg_nid = nid;
796         pcfg.pcfg_id = ip;
797         pcfg.pcfg_flags = single_share;
798
799         rc = pcfg_ioctl (&pcfg);
800         if (rc != 0) {
801                 fprintf (stderr, "failed to remove peer: %s\n",
802                          strerror (errno));
803                 return -1;
804         }
805         
806         return 0;
807 }
808
809 int 
810 jt_ptl_print_connections (int argc, char **argv)
811 {
812         struct portals_cfg       pcfg;
813         char                     buffer[2][64];
814         int                      index;
815         int                      rc;
816
817         if (!g_nal_is_compatible (argv[0], SOCKNAL, OPENIBNAL, IIBNAL, 0))
818                 return -1;
819
820         for (index = 0;;index++) {
821                 PCFG_INIT (pcfg,  NAL_CMD_GET_CONN);
822                 pcfg.pcfg_count   = index;
823                 
824                 rc = pcfg_ioctl (&pcfg);
825                 if (rc != 0)
826                         break;
827
828                 if (g_nal_is_compatible (NULL, SOCKNAL, 0))
829                         printf ("[%d]%s:"LPX64"@%s:%d:%s %d/%d %s\n",
830                                 pcfg.pcfg_gw_nal,       /* scheduler */
831                                 ptl_ipaddr_2_str (pcfg.pcfg_fd, buffer[0], 1), /* local IP addr */
832                                 pcfg.pcfg_nid, 
833                                 ptl_ipaddr_2_str (pcfg.pcfg_id, buffer[1], 1), /* remote IP addr */
834                                 pcfg.pcfg_misc,         /* remote port */
835                                 (pcfg.pcfg_flags == SOCKNAL_CONN_ANY) ? "A" :
836                                 (pcfg.pcfg_flags == SOCKNAL_CONN_CONTROL) ? "C" :
837                                 (pcfg.pcfg_flags == SOCKNAL_CONN_BULK_IN) ? "I" :
838                                 (pcfg.pcfg_flags == SOCKNAL_CONN_BULK_OUT) ? "O" : "?",
839                                 pcfg.pcfg_count,        /* tx buffer size */
840                                 pcfg.pcfg_size,         /* rx buffer size */
841                                 pcfg.pcfg_wait ? "nagle" : "nonagle");
842                 else
843                         printf (LPX64"\n",
844                                 pcfg.pcfg_nid);
845         }
846
847         if (index == 0)
848                 printf ("<no connections>\n");
849         return 0;
850 }
851
852 int jt_ptl_connect(int argc, char **argv)
853 {
854 #ifndef HAVE_CONNECT
855         /* no connect() support */
856         return -1;
857 #else /* HAVE_CONNECT */
858         struct portals_cfg pcfg;
859         struct sockaddr_in srvaddr;
860         struct sockaddr_in locaddr;
861         __u32 ipaddr;
862         char *flag;
863         int fd, rc;
864         int type = SOCKNAL_CONN_ANY;
865         int port, rport;
866         int o;
867
868         if (argc < 3) {
869                 fprintf(stderr, "usage: %s ip port [type]\n", argv[0]);
870                 return 0;
871         }
872
873         if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
874                 return -1;
875         
876         rc = ptl_parse_ipaddr (&ipaddr, argv[1]);
877         if (rc != 0) {
878                 fprintf(stderr, "Can't parse hostname: %s\n", argv[1]);
879                 return -1;
880         }
881
882         if (ptl_parse_port (&port, argv[2]) != 0) {
883                 fprintf (stderr, "Can't parse port: %s\n", argv[2]);
884                 return -1;
885         }
886
887         if (argc > 3)
888                 for (flag = argv[3]; *flag != 0; flag++)
889                         switch (*flag)
890                         {
891                         case 'I':
892                                 if (type != SOCKNAL_CONN_ANY) {
893                                         fprintf(stderr, "Can't flag type twice\n");
894                                         return -1;
895                                 }
896                                 type = SOCKNAL_CONN_BULK_IN;
897                                 break;
898
899                         case 'O':
900                                 if (type != SOCKNAL_CONN_ANY) {
901                                         fprintf(stderr, "Can't flag type twice\n");
902                                         return -1;
903                                 }
904                                 type = SOCKNAL_CONN_BULK_OUT;
905                                 break;
906
907                         case 'C':
908                                 if (type != SOCKNAL_CONN_ANY) {
909                                         fprintf(stderr, "Can't flag type twice\n");
910                                         return -1;
911                                 }
912                                 type = SOCKNAL_CONN_CONTROL;
913                                 break;
914                                 
915                         default:
916                                 fprintf (stderr, "unrecognised flag '%c'\n",
917                                          *flag);
918                                 return (-1);
919                         }
920
921         memset(&locaddr, 0, sizeof(locaddr)); 
922         locaddr.sin_family = AF_INET; 
923         locaddr.sin_addr.s_addr = INADDR_ANY;
924
925         memset(&srvaddr, 0, sizeof(srvaddr));
926         srvaddr.sin_family = AF_INET;
927         srvaddr.sin_port = htons(port);
928         srvaddr.sin_addr.s_addr = htonl(ipaddr);
929
930
931         for (rport = IPPORT_RESERVED - 1; rport > IPPORT_RESERVED / 2; --rport) {
932                 fd = socket(PF_INET, SOCK_STREAM, 0); 
933                 if ( fd < 0 ) { 
934                         fprintf(stderr, "socket() failed: %s\n", strerror(errno)); 
935                         return -1; 
936                 }
937
938                 o = 1;
939                 rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, 
940                                 &o, sizeof(o));
941                 
942                 locaddr.sin_port = htons(rport);
943                 rc = bind(fd, (struct sockaddr *)&locaddr, sizeof(locaddr)); 
944                 if (rc == 0 || errno == EACCES) {
945                         rc = connect(fd, (struct sockaddr *)&srvaddr, sizeof(srvaddr));
946                         if (rc == 0) {
947                                 break;
948                         } else if (errno != EADDRINUSE) {
949                                 fprintf(stderr, "Error connecting to host: %s\n", strerror(errno));
950                                 close(fd);
951                                 return -1;
952                         }
953                 } else if (errno != EADDRINUSE) {
954                         fprintf(stderr, "Error binding to port %d: %d: %s\n", port, errno, strerror(errno));
955                         close(fd);
956                         return -1;
957                 }
958         }
959
960         if (rport == IPPORT_RESERVED / 2) {
961                 fprintf(stderr,
962                         "Warning: all privileged ports are in use.\n"); 
963                 return -1;
964         }
965
966         printf("Connected host: %s type: %s\n", 
967                argv[1],
968                (type == SOCKNAL_CONN_ANY) ? "A" :
969                (type == SOCKNAL_CONN_CONTROL) ? "C" :
970                (type == SOCKNAL_CONN_BULK_IN) ? "I" :
971                (type == SOCKNAL_CONN_BULK_OUT) ? "O" : "?");
972
973         PCFG_INIT(pcfg, NAL_CMD_REGISTER_PEER_FD);
974         pcfg.pcfg_nal = g_nal;
975         pcfg.pcfg_fd = fd;
976         pcfg.pcfg_misc = type;
977         
978         rc = pcfg_ioctl(&pcfg);
979         if (rc) {
980                 fprintf(stderr, "failed to register fd with portals: %s\n", 
981                         strerror(errno));
982                 close (fd);
983                 return -1;
984         }
985
986         printf("Connection to %s registered with socknal\n", argv[1]);
987
988         rc = close(fd);
989         if (rc)
990                 fprintf(stderr, "close failed: %d\n", rc);
991
992         return 0;
993 #endif /* HAVE_CONNECT */
994 }
995
996 int jt_ptl_disconnect(int argc, char **argv)
997 {
998         struct portals_cfg       pcfg;
999         ptl_nid_t                nid = PTL_NID_ANY;
1000         __u32                    ipaddr = 0;
1001         int                      rc;
1002
1003         if (argc > 3) {
1004                 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
1005                 return 0;
1006         }
1007
1008         if (!g_nal_is_compatible (NULL, SOCKNAL, OPENIBNAL, IIBNAL, 0))
1009                 return 0;
1010
1011         if (argc >= 2 &&
1012             ptl_parse_nid (&nid, argv[1]) != 0) {
1013                 fprintf (stderr, "Can't parse nid %s\n", argv[1]);
1014                 return -1;
1015         }
1016
1017         if (g_nal_is_compatible (NULL, SOCKNAL, 0) &&
1018             argc >= 3 &&
1019             ptl_parse_ipaddr (&ipaddr, argv[2]) != 0) {
1020                 fprintf (stderr, "Can't parse ip addr %s\n", argv[2]);
1021                 return -1;
1022         }
1023
1024         PCFG_INIT(pcfg, NAL_CMD_CLOSE_CONNECTION);
1025         pcfg.pcfg_nid     = nid;
1026         pcfg.pcfg_id      = ipaddr;
1027         
1028         rc = pcfg_ioctl(&pcfg);
1029         if (rc) {
1030                 fprintf(stderr, "failed to remove connection: %s\n",
1031                         strerror(errno));
1032                 return -1;
1033         }
1034
1035         return 0;
1036 }
1037
1038 int jt_ptl_push_connection (int argc, char **argv)
1039 {
1040         struct portals_cfg       pcfg;
1041         int                      rc;
1042         ptl_nid_t                nid = PTL_NID_ANY;
1043         __u32                    ipaddr = 0;
1044
1045         if (argc > 3) {
1046                 fprintf(stderr, "usage: %s [nid] [ip]\n", argv[0]);
1047                 return 0;
1048         }
1049
1050         if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
1051                 return -1;
1052         
1053         if (argc > 1 &&
1054             ptl_parse_nid (&nid, argv[1]) != 0) {
1055                 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
1056                 return -1;
1057         }
1058                         
1059         if (argc > 2 &&
1060             ptl_parse_ipaddr (&ipaddr, argv[2]) != 0) {
1061                 fprintf(stderr, "Can't parse ipaddr: %s\n", argv[2]);
1062         }
1063
1064         PCFG_INIT(pcfg, NAL_CMD_PUSH_CONNECTION);
1065         pcfg.pcfg_nid     = nid;
1066         pcfg.pcfg_id      = ipaddr;
1067         
1068         rc = pcfg_ioctl(&pcfg);
1069         if (rc) {
1070                 fprintf(stderr, "failed to push connection: %s\n",
1071                         strerror(errno));
1072                 return -1;
1073         }
1074
1075         return 0;
1076 }
1077
1078 int 
1079 jt_ptl_print_active_txs (int argc, char **argv)
1080 {
1081         struct portals_cfg       pcfg;
1082         int                      index;
1083         int                      rc;
1084
1085         if (!g_nal_is_compatible (argv[0], QSWNAL, 0))
1086                 return -1;
1087
1088         for (index = 0;;index++) {
1089                 PCFG_INIT(pcfg, NAL_CMD_GET_TXDESC);
1090                 pcfg.pcfg_count   = index;
1091         
1092                 rc = pcfg_ioctl(&pcfg);
1093                 if (rc != 0)
1094                         break;
1095
1096                 printf ("%p: %5s payload %6d bytes to "LPX64" via "LPX64" by pid %6d: %s, %s, state %d\n",
1097                         pcfg.pcfg_pbuf1,
1098                         pcfg.pcfg_count == PTL_MSG_ACK ? "ACK" :
1099                         pcfg.pcfg_count == PTL_MSG_PUT ? "PUT" :
1100                         pcfg.pcfg_count == PTL_MSG_GET ? "GET" :
1101                         pcfg.pcfg_count == PTL_MSG_REPLY ? "REPLY" : "<wierd message>",
1102                         pcfg.pcfg_size,
1103                         pcfg.pcfg_nid,
1104                         pcfg.pcfg_nid2,
1105                         pcfg.pcfg_misc,
1106                         (pcfg.pcfg_flags & 1) ? "delayed" : "immediate",
1107                         (pcfg.pcfg_flags & 2) ? "nblk"    : "normal",
1108                         pcfg.pcfg_flags >> 2);
1109         }
1110
1111         if (index == 0)
1112                 printf ("<no active descs>\n");
1113         return 0;
1114 }
1115
1116 int jt_ptl_ping(int argc, char **argv)
1117 {
1118         int       rc;
1119         ptl_nid_t nid;
1120         long      count   = 1;
1121         long      size    = 4;
1122         long      timeout = 1;
1123         struct portal_ioctl_data data;
1124
1125         if (argc < 2) {
1126                 fprintf(stderr, "usage: %s nid [count] [size] [timeout (secs)]\n", argv[0]);
1127                 return 0;
1128         }
1129
1130         if (!g_nal_is_set())
1131                 return -1;
1132
1133         if (ptl_parse_nid (&nid, argv[1]) != 0)
1134         {
1135                 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1136                 return (-1);
1137         }
1138         
1139         if (argc > 2)
1140         {
1141                 count = atol(argv[2]);
1142
1143                 if (count < 0 || count > 20000) 
1144                 {
1145                         fprintf(stderr, "are you insane?  %ld is a crazy count.\n", count);
1146                         return -1;
1147                 }
1148         }
1149         
1150         if (argc > 3)
1151                 size= atol(argv[3]);
1152
1153         if (argc > 4)
1154                 timeout = atol (argv[4]);
1155         
1156         PORTAL_IOC_INIT (data);
1157         data.ioc_count   = count;
1158         data.ioc_size    = size;
1159         data.ioc_nid     = nid;
1160         data.ioc_nal     = g_nal;
1161         data.ioc_timeout = timeout;
1162         
1163         rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_PING, &data);
1164         if (rc) {
1165                 fprintf(stderr, "failed to start pinger: %s\n",
1166                         strerror(errno));
1167                 return -1;
1168         }
1169         return 0;
1170 }
1171
1172 int jt_ptl_shownid(int argc, char **argv)
1173 {
1174         struct portal_ioctl_data data;
1175         int                      rc;
1176         
1177         if (argc > 1) {
1178                 fprintf(stderr, "usage: %s\n", argv[0]);
1179                 return 0;
1180         }
1181         
1182         if (!g_nal_is_set())
1183                 return -1;
1184
1185         PORTAL_IOC_INIT (data);
1186         data.ioc_nal = g_nal;
1187         rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_GET_NID, &data);
1188         if (rc < 0)
1189                 fprintf(stderr, "getting my NID failed: %s\n",
1190                         strerror (errno));
1191         else
1192                 printf(LPX64"\n", data.ioc_nid);
1193         return 0;
1194 }
1195
1196 int jt_ptl_mynid(int argc, char **argv)
1197 {
1198         int rc;
1199         char hostname[1024];
1200         char *nidstr;
1201         struct portals_cfg pcfg;
1202         ptl_nid_t mynid;
1203
1204         if (argc > 2) {
1205                 fprintf(stderr, "usage: %s [NID]\n", argv[0]);
1206                 fprintf(stderr, "NID defaults to the primary IP address of the machine.\n");
1207                 return 0;
1208         }
1209
1210         if (!g_nal_is_set())
1211                 return -1;
1212
1213         if (argc >= 2)
1214                 nidstr = argv[1];
1215         else if (gethostname(hostname, sizeof(hostname)) != 0) {
1216                 fprintf(stderr, "gethostname failed: %s\n",
1217                         strerror(errno));
1218                 return -1;
1219         }
1220         else
1221                 nidstr = hostname;
1222
1223         rc = ptl_parse_nid (&mynid, nidstr);
1224         if (rc != 0) {
1225                 fprintf (stderr, "Can't convert '%s' into a NID\n", nidstr);
1226                 return -1;
1227         }
1228         
1229         PCFG_INIT(pcfg, NAL_CMD_REGISTER_MYNID);
1230         pcfg.pcfg_nid = mynid;
1231
1232         rc = pcfg_ioctl(&pcfg);
1233         if (rc < 0)
1234                 fprintf(stderr, "setting my NID failed: %s\n",
1235                        strerror(errno));
1236         else
1237                 printf("registered my nid "LPX64" (%s)\n", 
1238                        ptl_nid2u64(mynid), hostname);
1239         return 0;
1240 }
1241
1242 int
1243 jt_ptl_fail_nid (int argc, char **argv)
1244 {
1245         int                      rc;
1246         ptl_nid_t                nid;
1247         unsigned int             threshold;
1248         struct portal_ioctl_data data;
1249
1250         if (argc < 2 || argc > 3)
1251         {
1252                 fprintf (stderr, "usage: %s nid|\"_all_\" [count (0 == mend)]\n", argv[0]);
1253                 return (0);
1254         }
1255         
1256         if (!g_nal_is_set())
1257                 return (-1);
1258
1259         if (!strcmp (argv[1], "_all_"))
1260                 nid = PTL_NID_ANY;
1261         else if (ptl_parse_nid (&nid, argv[1]) != 0)
1262         {
1263                 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1264                 return (-1);
1265         }
1266
1267         if (argc < 3)
1268                 threshold = PTL_MD_THRESH_INF;
1269         else if (sscanf (argv[2], "%i", &threshold) != 1) {
1270                 fprintf (stderr, "Can't parse count \"%s\"\n", argv[2]);
1271                 return (-1);
1272         }
1273         
1274         PORTAL_IOC_INIT (data);
1275         data.ioc_nal = g_nal;
1276         data.ioc_nid = nid;
1277         data.ioc_count = threshold;
1278         
1279         rc = l_ioctl (PORTALS_DEV_ID, IOC_PORTAL_FAIL_NID, &data);
1280         if (rc < 0)
1281                 fprintf (stderr, "IOC_PORTAL_FAIL_NID failed: %s\n",
1282                          strerror (errno));
1283         else
1284                 printf ("%s %s\n", threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1285         
1286         return (0);
1287 }
1288
1289 int
1290 jt_ptl_add_route (int argc, char **argv)
1291 {
1292         struct portals_cfg       pcfg;
1293         ptl_nid_t                nid1;
1294         ptl_nid_t                nid2;
1295         ptl_nid_t                gateway_nid;
1296         int                      rc;
1297         
1298         if (argc < 3)
1299         {
1300                 fprintf (stderr, "usage: %s gateway target [target]\n", argv[0]);
1301                 return (0);
1302         }
1303
1304         if (!g_nal_is_set())
1305                 return (-1);
1306
1307         if (ptl_parse_nid (&gateway_nid, argv[1]) != 0)
1308         {
1309                 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1310                 return (-1);
1311         }
1312
1313         if (ptl_parse_nid (&nid1, argv[2]) != 0)
1314         {
1315                 fprintf (stderr, "Can't parse first target NID \"%s\"\n", argv[2]);
1316                 return (-1);
1317         }
1318
1319         if (argc < 4)
1320                 nid2 = nid1;
1321         else if (ptl_parse_nid (&nid2, argv[3]) != 0)
1322         {
1323                 fprintf (stderr, "Can't parse second target NID \"%s\"\n", argv[4]);
1324                 return (-1);
1325         }
1326
1327         PCFG_INIT(pcfg, NAL_CMD_ADD_ROUTE);
1328         pcfg.pcfg_nid = gateway_nid;
1329         pcfg.pcfg_nal = ROUTER;
1330         pcfg.pcfg_gw_nal = g_nal;
1331         pcfg.pcfg_nid2 = MIN (nid1, nid2);
1332         pcfg.pcfg_nid3 = MAX (nid1, nid2);
1333
1334         rc = pcfg_ioctl(&pcfg);
1335         if (rc != 0) 
1336         {
1337                 fprintf (stderr, "NAL_CMD_ADD_ROUTE failed: %s\n", strerror (errno));
1338                 return (-1);
1339         }
1340         
1341         return (0);
1342 }
1343
1344 int
1345 jt_ptl_del_route (int argc, char **argv)
1346 {
1347         struct portals_cfg       pcfg;
1348         ptl_nid_t                nid;
1349         ptl_nid_t                nid1 = PTL_NID_ANY;
1350         ptl_nid_t                nid2 = PTL_NID_ANY;
1351         int                      rc;
1352         
1353         if (argc < 2)
1354         {
1355                 fprintf (stderr, "usage: %s targetNID\n", argv[0]);
1356                 return (0);
1357         }
1358
1359         if (!g_nal_is_set())
1360                 return (-1);
1361
1362         if (ptl_parse_nid (&nid, argv[1]) != 0)
1363         {
1364                 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1365                 return (-1);
1366         }
1367
1368         if (argc >= 3 &&
1369             ptl_parse_nid (&nid1, argv[2]) != 0)
1370         {
1371                 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[2]);
1372                 return (-1);
1373         }
1374
1375         if (argc < 4) {
1376                 nid2 = nid1;
1377         } else {
1378                 if (ptl_parse_nid (&nid2, argv[3]) != 0) {
1379                         fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[3]);
1380                         return (-1);
1381                 }
1382
1383                 if (nid1 > nid2) {
1384                         ptl_nid_t tmp = nid1;
1385                         
1386                         nid1 = nid2;
1387                         nid2 = tmp;
1388                 }
1389         }
1390         
1391         PCFG_INIT(pcfg, NAL_CMD_DEL_ROUTE);
1392         pcfg.pcfg_nal = ROUTER;
1393         pcfg.pcfg_gw_nal = g_nal;
1394         pcfg.pcfg_nid = nid;
1395         pcfg.pcfg_nid2 = nid1;
1396         pcfg.pcfg_nid3 = nid2;
1397
1398         rc = pcfg_ioctl(&pcfg);
1399         if (rc != 0) 
1400         {
1401                 fprintf (stderr, "NAL_CMD_DEL_ROUTE ("LPX64") failed: %s\n", 
1402                          ptl_nid2u64(nid), strerror (errno));
1403                 return (-1);
1404         }
1405         
1406         return (0);
1407 }
1408
1409 int
1410 jt_ptl_notify_router (int argc, char **argv)
1411 {
1412         struct portals_cfg       pcfg;
1413         int                      enable;
1414         ptl_nid_t                nid;
1415         int                      rc;
1416         struct timeval           now;
1417         time_t                   when;
1418
1419         if (argc < 3)
1420         {
1421                 fprintf (stderr, "usage: %s targetNID <up/down> [<time>]\n", 
1422                          argv[0]);
1423                 return (0);
1424         }
1425
1426         if (ptl_parse_nid (&nid, argv[1]) != 0)
1427         {
1428                 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1429                 return (-1);
1430         }
1431
1432         if (ptl_parse_bool (&enable, argv[2]) != 0) {
1433                 fprintf (stderr, "Can't parse boolean %s\n", argv[2]);
1434                 return (-1);
1435         }
1436
1437         gettimeofday(&now, NULL);
1438         
1439         if (argc < 4) {
1440                 when = now.tv_sec;
1441         } else if (ptl_parse_time (&when, argv[3]) != 0) {
1442                 fprintf(stderr, "Can't parse time %s\n"
1443                         "Please specify either 'YYYY-MM-DD-HH:MM:SS'\n"
1444                         "or an absolute unix time in seconds\n", argv[3]);
1445                 return (-1);
1446         } else if (when > now.tv_sec) {
1447                 fprintf (stderr, "%s specifies a time in the future\n",
1448                          argv[3]);
1449                 return (-1);
1450         }
1451
1452         PCFG_INIT(pcfg, NAL_CMD_NOTIFY_ROUTER);
1453         pcfg.pcfg_nal = ROUTER;
1454         pcfg.pcfg_gw_nal = g_nal;
1455         pcfg.pcfg_nid = nid;
1456         pcfg.pcfg_flags = enable;
1457         /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1458         pcfg.pcfg_nid3 = (__u64)when;
1459         
1460         rc = pcfg_ioctl(&pcfg);
1461         if (rc != 0) 
1462         {
1463                 fprintf (stderr, "NAL_CMD_NOTIFY_ROUTER ("LPX64") failed: %s\n",
1464                          ptl_nid2u64(nid), strerror (errno));
1465                 return (-1);
1466         }
1467         
1468         return (0);
1469 }
1470
1471 int
1472 jt_ptl_print_routes (int argc, char **argv)
1473 {
1474         char                      buffer[3][128];
1475         struct portals_cfg        pcfg;
1476         int                       rc;
1477         int                       index;
1478         int                       gateway_nal;
1479         ptl_nid_t                 gateway_nid;
1480         ptl_nid_t                 nid1;
1481         ptl_nid_t                 nid2;
1482         int                       alive;
1483
1484         for (index = 0;;index++)
1485         {
1486                 PCFG_INIT(pcfg, NAL_CMD_GET_ROUTE);
1487                 pcfg.pcfg_nal = ROUTER;
1488                 pcfg.pcfg_count = index;
1489                 
1490                 rc = pcfg_ioctl(&pcfg);
1491                 if (rc != 0)
1492                         break;
1493
1494                 gateway_nal = pcfg.pcfg_gw_nal;
1495                 gateway_nid = pcfg.pcfg_nid;
1496                 nid1 = pcfg.pcfg_nid2;
1497                 nid2 = pcfg.pcfg_nid3;
1498                 alive = pcfg.pcfg_flags;
1499
1500                 printf ("%8s %18s : %s - %s, %s\n", 
1501                         nal2name (gateway_nal), 
1502                         ptl_nid2str (buffer[0], gateway_nid),
1503                         ptl_nid2str (buffer[1], nid1),
1504                         ptl_nid2str (buffer[2], nid2),
1505                         alive ? "up" : "down");
1506         }
1507         return (0);
1508 }
1509
1510 static int
1511 lwt_control(int enable, int clear)
1512 {
1513         struct portal_ioctl_data data;
1514         int                      rc;
1515
1516         PORTAL_IOC_INIT(data);
1517         data.ioc_flags = enable;
1518         data.ioc_misc = clear;
1519
1520         rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_CONTROL, &data);
1521         if (rc == 0)
1522                 return (0);
1523
1524         fprintf(stderr, "IOC_PORTAL_LWT_CONTROL failed: %s\n",
1525                 strerror(errno));
1526         return (-1);
1527 }
1528
1529 static int
1530 lwt_snapshot(cycles_t *now, int *ncpu, int *totalsize, 
1531              lwt_event_t *events, int size)
1532 {
1533         struct portal_ioctl_data data;
1534         int                      rc;
1535
1536         PORTAL_IOC_INIT(data);
1537         data.ioc_pbuf1 = (char *)events;
1538         data.ioc_plen1 = size;
1539
1540         rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_SNAPSHOT, &data);
1541         if (rc != 0) {
1542                 fprintf(stderr, "IOC_PORTAL_LWT_SNAPSHOT failed: %s\n",
1543                         strerror(errno));
1544                 return (-1);
1545         }
1546
1547         /* crappy overloads */
1548         if (data.ioc_nid2 != sizeof(lwt_event_t) ||
1549             data.ioc_nid3 != offsetof(lwt_event_t, lwte_where)) {
1550                 fprintf(stderr,"kernel/user LWT event mismatch %d(%d),%d(%d)\n",
1551                         (int)data.ioc_nid2, sizeof(lwt_event_t),
1552                         (int)data.ioc_nid3,
1553                         (int)offsetof(lwt_event_t, lwte_where));
1554                 return (-1);
1555         }
1556
1557         LASSERT (data.ioc_count != 0);
1558         LASSERT (data.ioc_misc != 0);
1559
1560         if (now != NULL)
1561                 *now = data.ioc_nid;
1562
1563         if (ncpu != NULL)
1564                 *ncpu = data.ioc_count;
1565
1566         if (totalsize != NULL)
1567                 *totalsize = data.ioc_misc;
1568
1569         return (0);
1570 }
1571
1572 static char *
1573 lwt_get_string(char *kstr)
1574 {
1575         char                     *ustr;
1576         struct portal_ioctl_data  data;
1577         int                       size;
1578         int                       rc;
1579
1580         /* FIXME: this could maintain a symbol table since we expect to be
1581          * looking up the same strings all the time... */
1582
1583         PORTAL_IOC_INIT(data);
1584         data.ioc_pbuf1 = kstr;
1585         data.ioc_plen1 = 1;        /* non-zero just to fool portal_ioctl_is_invalid() */
1586         data.ioc_pbuf2 = NULL;
1587         data.ioc_plen2 = 0;
1588
1589         rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_LOOKUP_STRING, &data);
1590         if (rc != 0) {
1591                 fprintf(stderr, "IOC_PORTAL_LWT_LOOKUP_STRING failed: %s\n",
1592                         strerror(errno));
1593                 return (NULL);
1594         }
1595
1596         size = data.ioc_count;
1597         ustr = (char *)malloc(size);
1598         if (ustr == NULL) {
1599                 fprintf(stderr, "Can't allocate string storage of size %d\n",
1600                         size);
1601                 return (NULL);
1602         }
1603
1604         PORTAL_IOC_INIT(data);
1605         data.ioc_pbuf1 = kstr;
1606         data.ioc_plen1 = 1;        /* non-zero just to fool portal_ioctl_is_invalid() */
1607         data.ioc_pbuf2 = ustr;
1608         data.ioc_plen2 = size;
1609
1610         rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_LOOKUP_STRING, &data);
1611         if (rc != 0) {
1612                 fprintf(stderr, "IOC_PORTAL_LWT_LOOKUP_STRING failed: %s\n",
1613                         strerror(errno));
1614                 return (NULL);
1615         }
1616
1617         LASSERT(strlen(ustr) == size - 1);
1618         return (ustr);
1619 }
1620
1621 static void
1622 lwt_put_string(char *ustr)
1623 {
1624         free(ustr);
1625 }
1626
1627 static int
1628 lwt_print(FILE *f, cycles_t t0, cycles_t tlast, double mhz, int cpu, lwt_event_t *e)
1629 {
1630 #ifndef __WORDSIZE
1631 # error "__WORDSIZE not defined"
1632 #elif __WORDSIZE == 32
1633 # define XFMT "%#010lx"
1634 #elif __WORDSIZE== 64
1635 # define XFMT "%#018lx"
1636 #else
1637 # error "Unexpected __WORDSIZE"
1638 #endif
1639         char           *where = lwt_get_string(e->lwte_where);
1640
1641         if (where == NULL)
1642                 return (-1);
1643
1644         fprintf(f, XFMT" "XFMT" "XFMT" "XFMT": "XFMT" %2d %10.6f %10.2f %s\n",
1645                 e->lwte_p1, e->lwte_p2, e->lwte_p3, e->lwte_p4,
1646                 (long)e->lwte_task, cpu, (e->lwte_when - t0) / (mhz * 1000000.0),
1647                 (t0 == e->lwte_when) ? 0.0 : (e->lwte_when - tlast) / mhz,
1648                 where);
1649
1650         lwt_put_string(where);
1651
1652         return (0);
1653 #undef XFMT
1654 }
1655
1656 double
1657 get_cycles_per_usec ()
1658 {
1659         FILE      *f = fopen ("/proc/cpuinfo", "r");
1660         double     mhz;
1661         char      line[64];
1662         
1663         if (f != NULL) {
1664                 while (fgets (line, sizeof (line), f) != NULL)
1665                         if (sscanf (line, "cpu MHz : %lf", &mhz) == 1) {
1666                                 fclose (f);
1667                                 return (mhz);
1668                         }
1669                 fclose (f);
1670         }
1671
1672         fprintf (stderr, "Can't read/parse /proc/cpuinfo\n");
1673         return (1000.0);
1674 }
1675
1676 int
1677 jt_ptl_lwt(int argc, char **argv)
1678 {
1679         const int       lwt_max_cpus = 32;
1680         int             ncpus;
1681         int             totalspace;
1682         int             nevents_per_cpu;
1683         lwt_event_t    *events;
1684         lwt_event_t    *cpu_event[lwt_max_cpus + 1];
1685         lwt_event_t    *next_event[lwt_max_cpus];
1686         lwt_event_t    *first_event[lwt_max_cpus];
1687         int             cpu;
1688         lwt_event_t    *e;
1689         int             rc;
1690         int             i;
1691         double          mhz;
1692         cycles_t        t0;
1693         cycles_t        tlast;
1694         cycles_t        tnow;
1695         struct timeval  tvnow;
1696         int             printed_date = 0;
1697         int             nlines = 0;
1698         FILE           *f = stdout;
1699
1700         if (argc < 2 ||
1701             (strcmp(argv[1], "start") &&
1702              strcmp(argv[1], "stop"))) {
1703                 fprintf(stderr, 
1704                         "usage:  %s start\n"
1705                         "        %s stop [fname]\n", argv[0], argv[0]);
1706                 return (-1);
1707         }
1708         
1709         if (!strcmp(argv[1], "start")) {
1710                 /* disable */
1711                 if (lwt_control(0, 0) != 0)
1712                         return (-1);
1713
1714                 /* clear */
1715                 if (lwt_control(0, 1) != 0)
1716                         return (-1);
1717
1718                 /* enable */
1719                 if (lwt_control(1, 0) != 0)
1720                         return (-1);
1721
1722                 return (0);
1723         }
1724                 
1725         if (lwt_snapshot(NULL, &ncpus, &totalspace, NULL, 0) != 0)
1726                 return (-1);
1727
1728         if (ncpus > lwt_max_cpus) {
1729                 fprintf(stderr, "Too many cpus: %d (%d)\n", 
1730                         ncpus, lwt_max_cpus);
1731                 return (-1);
1732         }
1733
1734         events = (lwt_event_t *)malloc(totalspace);
1735         if (events == NULL) {
1736                 fprintf(stderr, "Can't allocate %d\n", totalspace);
1737                 return (-1);
1738         }
1739
1740         if (lwt_control(0, 0) != 0) {           /* disable */
1741                 free(events);
1742                 return (-1);
1743         }
1744
1745         if (lwt_snapshot(&tnow, NULL, NULL, events, totalspace)) {
1746                 free(events);
1747                 return (-1);
1748         }
1749
1750         /* we want this time to be sampled at snapshot time */
1751         gettimeofday(&tvnow, NULL);
1752
1753         if (argc > 2) {
1754                 f = fopen (argv[2], "w");
1755                 if (f == NULL) {
1756                         fprintf(stderr, "Can't open %s for writing: %s\n", argv[2], strerror (errno));
1757                         free(events);
1758                         return (-1);
1759                 }
1760         }
1761
1762         mhz = get_cycles_per_usec();
1763         
1764         /* carve events into per-cpu slices */
1765         nevents_per_cpu = totalspace / (ncpus * sizeof(lwt_event_t));
1766         for (cpu = 0; cpu <= ncpus; cpu++)
1767                 cpu_event[cpu] = &events[cpu * nevents_per_cpu];
1768
1769         /* find the earliest event on each cpu */
1770         for (cpu = 0; cpu < ncpus; cpu++) {
1771                 first_event[cpu] = NULL;
1772
1773                 for (e = cpu_event[cpu]; e < cpu_event[cpu + 1]; e++) {
1774
1775                         if (e->lwte_where == NULL) /* not an event */
1776                                 continue;
1777
1778                         if (first_event[cpu] == NULL ||
1779                             first_event[cpu]->lwte_when > e->lwte_when)
1780                                 first_event[cpu] = e;
1781                 }
1782
1783                 next_event[cpu] = first_event[cpu];
1784         }
1785
1786         t0 = tlast = 0;
1787         for (cpu = 0; cpu < ncpus; cpu++) {
1788                 e = first_event[cpu];
1789                 if (e == NULL)                  /* no events this cpu */
1790                         continue;
1791                 
1792                 if (e == cpu_event[cpu])
1793                         e = cpu_event[cpu + 1] - 1;
1794                 else 
1795                         e = e - 1;
1796                 
1797                 /* If there's an event immediately before the first one, this
1798                  * cpu wrapped its event buffer */
1799                 if (e->lwte_where == NULL)
1800                         continue;
1801          
1802                 /* We should only start outputting events from the most recent
1803                  * first event in any wrapped cpu.  Events before this time on
1804                  * other cpus won't have any events from this CPU to interleave
1805                  * with. */
1806                 if (t0 < first_event[cpu]->lwte_when)
1807                         t0 = first_event[cpu]->lwte_when;
1808         }
1809
1810         for (;;) {
1811                 /* find which cpu has the next event */
1812                 cpu = -1;
1813                 for (i = 0; i < ncpus; i++) {
1814
1815                         if (next_event[i] == NULL) /* this cpu exhausted */
1816                                 continue;
1817
1818                         if (cpu < 0 ||
1819                             next_event[i]->lwte_when < next_event[cpu]->lwte_when)
1820                                 cpu = i;
1821                 }
1822
1823                 if (cpu < 0)                    /* all cpus exhausted */
1824                         break;
1825
1826                 if (t0 == 0) {
1827                         /* no wrapped cpus and this is he first ever event */
1828                         t0 = next_event[cpu]->lwte_when;
1829                 }
1830                 
1831                 if (t0 <= next_event[cpu]->lwte_when) {
1832                         /* on or after the first event */
1833                         if (!printed_date) {
1834                                 cycles_t du = (tnow - t0) / mhz;
1835                                 time_t   then = tvnow.tv_sec - du/1000000;
1836                                 
1837                                 if (du % 1000000 > tvnow.tv_usec)
1838                                         then--;
1839
1840                                 fprintf(f, "%s", ctime(&then));
1841                                 printed_date = 1;
1842                         }
1843                         
1844                         rc = lwt_print(f, t0, tlast, mhz, cpu, next_event[cpu]);
1845                         if (rc != 0)
1846                                 break;
1847
1848                         if (++nlines % 10000 == 0 && f != stdout) {
1849                                 /* show some activity... */
1850                                 printf(".");
1851                                 fflush (stdout);
1852                         }
1853                 }
1854
1855                 tlast = next_event[cpu]->lwte_when;
1856                 
1857                 next_event[cpu]++;
1858                 if (next_event[cpu] == cpu_event[cpu + 1])
1859                         next_event[cpu] = cpu_event[cpu];
1860
1861                 if (next_event[cpu]->lwte_where == NULL ||
1862                     next_event[cpu] == first_event[cpu])
1863                         next_event[cpu] = NULL;
1864         }
1865
1866         if (f != stdout) {
1867                 printf("\n");
1868                 fclose(f);
1869         }
1870
1871         free(events);
1872         return (0);
1873 }
1874
1875 int jt_ptl_memhog(int argc, char **argv)
1876 {
1877         static int                gfp = 0;        /* sticky! */
1878
1879         struct portal_ioctl_data  data;
1880         int                       rc;
1881         int                       count;
1882         char                     *end;
1883         
1884         if (argc < 2)  {
1885                 fprintf(stderr, "usage: %s <npages> [<GFP flags>]\n", argv[0]);
1886                 return 0;
1887         }
1888
1889         count = strtol(argv[1], &end, 0);
1890         if (count < 0 || *end != 0) {
1891                 fprintf(stderr, "Can't parse page count '%s'\n", argv[1]);
1892                 return -1;
1893         }
1894
1895         if (argc >= 3) {
1896                 rc = strtol(argv[2], &end, 0);
1897                 if (*end != 0) {
1898                         fprintf(stderr, "Can't parse gfp flags '%s'\n", argv[2]);
1899                         return -1;
1900                 }
1901                 gfp = rc;
1902         }
1903         
1904         PORTAL_IOC_INIT(data);
1905         data.ioc_count = count;
1906         data.ioc_flags = gfp;
1907         rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_MEMHOG, &data);
1908
1909         if (rc != 0) {
1910                 fprintf(stderr, "memhog %d failed: %s\n", count, strerror(errno));
1911                 return -1;
1912         }
1913         
1914         printf("memhog %d OK\n", count);
1915         return 0;
1916 }
1917