Whamcloud - gitweb
LU-1154 clio: rename coo_attr_set to coo_attr_update
[fs/lustre-release.git] / lnet / utils / portals.c
index a89f4f7..f1e34f0 100644 (file)
@@ -1,7 +1,7 @@
-/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=8:tabstop=8:
+/*
+ * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
  *
- * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
+ * Copyright (c) 2013, 2014, Intel Corporation.
  *
  *   This file is part of Portals, http://www.sf.net/projects/lustre/
  *
  *
  */
 
-#include <stdio.h>
-#include <sys/types.h>
+#include <libcfs/libcfsutil.h>
+#include <lnet/nidstr.h>
+#include <lnet/lnetctl.h>
+#include <lnet/socklnd.h>
+#include <lnet/lib-dlc.h>
+#include <getopt.h>
 #include <netdb.h>
-#include <sys/socket.h>
-#include <netinet/tcp.h>
-#include <netdb.h>
-#include <stdlib.h>
-#include <string.h>
-#include <fcntl.h>
-#include <sys/ioctl.h>
-#include <errno.h>
-#include <unistd.h>
-#include <time.h>
-#include <asm/byteorder.h>
-
-#include <portals/api-support.h>
-#include <portals/ptlctl.h>
-#include <portals/list.h>
-#include <portals/lib-types.h>
-#include "parser.h"
-
-unsigned int portal_debug;
-unsigned int portal_printk;
-unsigned int portal_stack;
-
-
-static ptl_nid_t g_nid = 0;
-static unsigned int g_nal = 0;
-static unsigned short g_port = 0;
-
-static int g_socket_txmem = 0;
-static int g_socket_rxmem = 0;
-static int g_socket_nonagle = 1;
-
-typedef struct
-{
-        char *name;
-        int   num;
-} name2num_t;
-
-static name2num_t nalnames[] = {
-        {"tcp",                SOCKNAL},
-        {"toe",                TOENAL},
-        {"elan",       QSWNAL},
-        {"gm",         GMNAL},
-        {"scimac",      SCIMACNAL},
-        {NULL,         -1}
-};
-
-static name2num_t *
-name2num_lookup_name (name2num_t *table, char *str)
-{
-        while (table->name != NULL)
-                if (!strcmp (str, table->name))
-                        return (table);
-                else
-                        table++;
-        return (NULL);
-}
 
-static name2num_t *
-name2num_lookup_num (name2num_t *table, int num)
-{
-        while (table->name != NULL)
-                if (num == table->num)
-                        return (table);
-                else
-                        table++;
-        return (NULL);
-}
+unsigned int libcfs_debug;
+unsigned int libcfs_printk = D_CANTMASK;
+
+static int   g_net_set;
+static __u32 g_net;
+
+#define IOC_BUF_SIZE   8192
+static char local_buf[IOC_BUF_SIZE];
+static char *ioc_buf = local_buf;
 
+/* Convert a string boolean to an int; "enable" -> 1 */
 int
-ptl_name2nal (char *str)
+lnet_parse_bool (int *b, char *str)
 {
-        name2num_t *e = name2num_lookup_name (nalnames, str);
+        if (!strcasecmp (str, "no") ||
+            !strcasecmp (str, "n") ||
+            !strcasecmp (str, "off") ||
+            !strcasecmp (str, "down") ||
+            !strcasecmp (str, "disable"))
+        {
+                *b = 0;
+                return (0);
+        }
+
+        if (!strcasecmp (str, "yes") ||
+            !strcasecmp (str, "y") ||
+            !strcasecmp (str, "on") ||
+            !strcasecmp (str, "up") ||
+            !strcasecmp (str, "enable"))
+        {
+                *b = 1;
+                return (0);
+        }
 
-        return ((e == NULL) ? 0 : e->num);
+        return (-1);
 }
 
-static char *
-nal2name (int nal)
+int
+lnet_parse_port (int *port, char *str)
 {
-        name2num_t *e = name2num_lookup_num (nalnames, nal);
+        char      *end;
+
+        *port = strtol (str, &end, 0);
+
+        if (*end == 0 &&                        /* parsed whole string */
+            *port > 0 && *port < 65536)         /* minimal sanity check */
+                return (0);
 
-        return ((e == NULL) ? "???" : e->name);
+        return (-1);
 }
 
+#ifdef HAVE_GETHOSTBYNAME
 static struct hostent *
 ptl_gethostbyname(char * hname) {
         struct hostent *he;
@@ -119,880 +92,1703 @@ ptl_gethostbyname(char * hname) {
                                 hname);
                         break;
                 default:
-                        fprintf(stderr, "gethostbyname error: %s\n",
-                                strerror(errno));
+                        fprintf(stderr, "gethostbyname error for %s: %s\n",
+                                hname, strerror(h_errno));
                         break;
                 }
                 return NULL;
         }
         return he;
 }
+#endif
 
 int
-ptl_parse_nid (ptl_nid_t *nidp, char *str)
+lnet_parse_ipquad (__u32 *ipaddrp, char *str)
 {
-        struct hostent *he;
         int             a;
         int             b;
         int             c;
         int             d;
-        
+
         if (sscanf (str, "%d.%d.%d.%d", &a, &b, &c, &d) == 4 &&
             (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
             (c & ~0xff) == 0 && (d & ~0xff) == 0)
         {
-                __u32 addr = (a<<24)|(b<<16)|(c<<8)|d;
-
-                *nidp = (ptl_nid_t)addr;
+                *ipaddrp = (a<<24)|(b<<16)|(c<<8)|d;
                 return (0);
         }
-        
-        if ((('a' <= str[0] && str[0] <= 'z') ||
-             ('A' <= str[0] && str[0] <= 'Z')) &&
-             (he = ptl_gethostbyname (str)) != NULL)
-        {
-                __u32 addr = *(__u32 *)he->h_addr;
 
-                *nidp = (ptl_nid_t)ntohl(addr);  /* HOST byte order */
+        return (-1);
+}
+
+int
+lnet_parse_ipaddr (__u32 *ipaddrp, char *str)
+{
+#ifdef HAVE_GETHOSTBYNAME
+        struct hostent *he;
+#endif
+
+        if (!strcmp (str, "_all_")) {
+                *ipaddrp = 0;
                 return (0);
         }
 
-        if (sscanf (str, "%i", &a) == 1)
-        {
-                *nidp = (ptl_nid_t)a;
+        if (lnet_parse_ipquad(ipaddrp, str) == 0)
                 return (0);
-        }
 
-        if (sscanf (str, "%x", &a) == 1)
-        {
-                *nidp = (ptl_nid_t) a;
+#ifdef HAVE_GETHOSTBYNAME
+        if ((('a' <= str[0] && str[0] <= 'z') ||
+             ('A' <= str[0] && str[0] <= 'Z')) &&
+             (he = ptl_gethostbyname (str)) != NULL) {
+                __u32 addr = *(__u32 *)he->h_addr;
+
+                *ipaddrp = ntohl(addr);         /* HOST byte order */
                 return (0);
         }
+#endif
 
         return (-1);
 }
 
 char *
-ptl_nid2str (char *buffer, ptl_nid_t nid)
+ptl_ipaddr_2_str(__u32 ipaddr, char *str, size_t strsize, int lookup)
 {
-        __u32           addr = htonl((__u32)nid); /* back to NETWORK byte order */
-        struct hostent *he = gethostbyaddr ((const char *)&addr, sizeof (addr), AF_INET);
+#ifdef HAVE_GETHOSTBYNAME
+        __u32           net_ip;
+        struct hostent *he;
 
-        if (he != NULL)
-                strcpy (buffer, he->h_name);
-        else
-                sprintf (buffer, "0x"LPX64, nid);
-        
-        return (buffer);
+        if (lookup) {
+                net_ip = htonl (ipaddr);
+                he = gethostbyaddr (&net_ip, sizeof (net_ip), AF_INET);
+                if (he != NULL) {
+                       strlcpy(str, he->h_name, strsize);
+                        return (str);
+                }
+        }
+#endif
+
+        sprintf (str, "%d.%d.%d.%d",
+                 (ipaddr >> 24) & 0xff, (ipaddr >> 16) & 0xff,
+                 (ipaddr >> 8) & 0xff, ipaddr & 0xff);
+        return (str);
 }
 
 int
-sock_write (int cfd, void *buffer, int nob)
+lnet_parse_time (time_t *t, char *str)
 {
-        while (nob > 0)
-        {
-                int rc = write (cfd, buffer, nob);
+        char          *end;
+        int            n;
+        struct tm      tm;
 
-                if (rc < 0)
-                {
-                        if (errno == EINTR)
-                                continue;
-                        
-                        return (rc);
-                }
+        *t = strtol (str, &end, 0);
+        if (*end == 0) /* parsed whole string */
+                return (0);
 
-                if (rc == 0)
-                {
-                        fprintf (stderr, "Unexpected zero sock_write\n");
-                        abort();
-                }
+        memset (&tm, 0, sizeof (tm));
+        n = sscanf (str, "%d-%d-%d-%d:%d:%d",
+                    &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
+                    &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
+        if (n != 6)
+                return (-1);
+
+        tm.tm_mon--;                    /* convert to 0 == Jan */
+        tm.tm_year -= 1900;             /* y2k quirk */
+        tm.tm_isdst = -1;               /* dunno if it's daylight savings... */
+
+        *t = mktime (&tm);
+        if (*t == (time_t)-1)
+                return (-1);
 
-                nob -= rc;
-                buffer = (char *)buffer + nob;
-        }
-        
         return (0);
 }
 
 int
-sock_read (int cfd, void *buffer, int nob)
+lnet_parse_nid(char *nid_str, lnet_process_id_t *id_ptr)
 {
-        while (nob > 0)
-        {
-                int rc = read (cfd, buffer, nob);
-                
-                if (rc < 0)
-                {
-                        if (errno == EINTR)
-                                continue;
-                        
-                        return (rc);
-                }
-                
-                if (rc == 0)                    /* EOF */
-                {
-                        errno = ECONNABORTED;
-                        return (-1);
-                }
-                
-                nob -= rc;
-                buffer = (char *)buffer + nob;
+        id_ptr->pid = LNET_PID_ANY;
+        id_ptr->nid = libcfs_str2nid(nid_str);
+        if (id_ptr->nid == LNET_NID_ANY) {
+                fprintf (stderr, "Can't parse nid \"%s\"\n", nid_str);
+                return -1;
         }
-        
-        return (0);
+
+        return 0;
 }
 
-int ptl_initialize(int argc, char **argv) 
+int g_net_is_set (char *cmd)
 {
-        register_ioc_dev(PORTALS_DEV_ID, PORTALS_DEV_PATH);
+        if (g_net_set)
+                return 1;
+
+        if (cmd != NULL)
+                fprintf(stderr,
+                        "You must run the 'network' command before '%s'.\n",
+                        cmd);
         return 0;
 }
 
-
-int jt_ptl_network(int argc, char **argv)
+int g_net_is_compatible (char *cmd, ...)
 {
-        int  nal;
-        
-        if (argc != 2 ||
-            (nal = ptl_name2nal (argv[1])) == 0)
-        {
-                name2num_t *entry;
-                
-                fprintf(stderr, "usage: %s \n", argv[0]);
-                for (entry = nalnames; entry->name != NULL; entry++)
-                        fprintf (stderr, "%s%s", entry == nalnames ? "<" : "|", entry->name);
-                fprintf(stderr, ">\n");
-        }
-        else
-                g_nal = nal;
+        va_list       ap;
+        int           nal;
 
-        return (0);
+        if (!g_net_is_set(cmd))
+                return 0;
+
+        va_start(ap, cmd);
+
+        do {
+                nal = va_arg (ap, int);
+                if (nal == LNET_NETTYP(g_net)) {
+                        va_end (ap);
+                        return 1;
+                }
+        } while (nal != 0);
+
+        va_end (ap);
+
+        if (cmd != NULL)
+                fprintf (stderr,
+                         "Command %s not compatible with %s NAL\n",
+                         cmd,
+                         libcfs_lnd2str(LNET_NETTYP(g_net)));
+        return 0;
 }
 
-int
-exchange_nids (int cfd, ptl_nid_t my_nid, ptl_nid_t *peer_nid)
+int ptl_initialize(int argc, char **argv)
+{
+        register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH,
+                         LNET_DEV_MAJOR, LNET_DEV_MINOR);
+        return 0;
+}
+
+
+int jt_ptl_network(int argc, char **argv)
 {
+        struct libcfs_ioctl_data data;
+        __u32                    net = LNET_NIDNET(LNET_NID_ANY);
         int                      rc;
-        ptl_hdr_t                hdr;
-        ptl_magicversion_t      *hmv = (ptl_magicversion_t *)&hdr.dest_nid;
-
-        LASSERT (sizeof (*hmv) == sizeof (hdr.dest_nid));
-
-        memset (&hdr, 0, sizeof (hdr));
-        
-        hmv->magic          = __cpu_to_le32 (PORTALS_PROTO_MAGIC);
-        hmv->version_major  = __cpu_to_le16 (PORTALS_PROTO_VERSION_MAJOR);
-        hmv->version_minor  = __cpu_to_le16 (PORTALS_PROTO_VERSION_MINOR);
-
-        hdr.src_nid = __cpu_to_le64 (my_nid);
-        hdr.type = __cpu_to_le32 (PTL_MSG_HELLO);
-        
-        /* Assume there's sufficient socket buffering for a portals HELLO header */
-        rc = sock_write (cfd, &hdr, sizeof (hdr));
-        if (rc != 0) {
-                perror ("Can't send initial HELLO");
-                return (-1);
+
+        if (argc < 2) {
+                fprintf(stderr, "usage: %s <net>|up|down\n", argv[0]);
+                return 0;
         }
 
-        /* First few bytes down the wire are the portals protocol magic and
-         * version, no matter what protocol version we're running. */
+        if (!strcmp(argv[1], "unconfigure") ||
+            !strcmp(argv[1], "down")) {
+                LIBCFS_IOC_INIT(data);
+                rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_UNCONFIGURE, &data);
 
-        rc = sock_read (cfd, hmv, sizeof (*hmv));
-        if (rc != 0) {
-                perror ("Can't read from peer");
-                return (-1);
-        }
+                if (rc == 0) {
+                        printf ("LNET ready to unload\n");
+                        return 0;
+                }
 
-        if (__cpu_to_le32 (hmv->magic) != PORTALS_PROTO_MAGIC) {
-                fprintf (stderr, "Bad magic %#08x (%#08x expected)\n", 
-                         __cpu_to_le32 (hmv->magic), PORTALS_PROTO_MAGIC);
-                return (-1);
+                if (errno == EBUSY)
+                        fprintf(stderr, "LNET busy\n");
+                else
+                        fprintf(stderr, "LNET unconfigure error %d: %s\n",
+                                errno, strerror(errno));
+                return -1;
         }
 
-        if (__cpu_to_le16 (hmv->version_major) != PORTALS_PROTO_VERSION_MAJOR ||
-            __cpu_to_le16 (hmv->version_minor) != PORTALS_PROTO_VERSION_MINOR) {
-                fprintf (stderr, "Incompatible protocol version %d.%d (%d.%d expected)\n",
-                         __cpu_to_le16 (hmv->version_major),
-                         __cpu_to_le16 (hmv->version_minor),
-                         PORTALS_PROTO_VERSION_MAJOR,
-                         PORTALS_PROTO_VERSION_MINOR);
+        if (!strcmp(argv[1], "configure") ||
+            !strcmp(argv[1], "up")) {
+                LIBCFS_IOC_INIT(data);
+                rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIGURE, &data);
+
+                if (rc == 0) {
+                        printf ("LNET configured\n");
+                        return 0;
+                }
+
+                fprintf(stderr, "LNET configure error %d: %s\n",
+                        errno, strerror(errno));
+                return -1;
         }
 
-        /* version 0 sends magic/version as the dest_nid of a 'hello' header,
-         * so read the rest of it in now... */
-        LASSERT (PORTALS_PROTO_VERSION_MAJOR == 0);
-        rc = sock_read (cfd, hmv + 1, sizeof (hdr) - sizeof (*hmv));
-        if (rc != 0) {
-                perror ("Can't read rest of HELLO hdr");
-                return (-1);
+        net = libcfs_str2net(argv[1]);
+        if (net == LNET_NIDNET(LNET_NID_ANY)) {
+                fprintf(stderr, "Can't parse net %s\n", argv[1]);
+                return -1;
         }
 
-        /* ...and check we got what we expected */
-        if (__cpu_to_le32 (hdr.type) != PTL_MSG_HELLO ||
-            __cpu_to_le32 (PTL_HDR_LENGTH (&hdr)) != 0) {
-                fprintf (stderr, "Expecting a HELLO hdr with 0 payload,"
-                         " but got type %d with %d payload\n",
-                         __cpu_to_le32 (hdr.type),
-                         __cpu_to_le32 (PTL_HDR_LENGTH (&hdr)));
-                return (-1);
+        if (LNET_NETTYP(net) == CIBLND    ||
+            LNET_NETTYP(net) == OPENIBLND ||
+            LNET_NETTYP(net) == IIBLND    ||
+            LNET_NETTYP(net) == VIBLND) {
+                fprintf(stderr, "Net %s obsoleted\n", libcfs_lnd2str(net));
+                return -1;
         }
-        
-        *peer_nid = __le64_to_cpu (hdr.src_nid);
-        return (0);
+
+        g_net_set = 1;
+        g_net = net;
+        return 0;
 }
 
-int jt_ptl_connect(int argc, char **argv)
+int
+jt_ptl_list_nids(int argc, char **argv)
 {
-        if (argc < 2) {
-        usage:
-                fprintf(stderr, "usage: %s <hostname port [xi]> or <elan ID>\n",
-                        argv[0]);
+        struct libcfs_ioctl_data data;
+        int                      all = 0, return_nid = 0;
+        int                      count;
+        int                      rc;
+
+        all = (argc == 2) && (strcmp(argv[1], "all") == 0);
+        /* Hack to pass back value */
+        return_nid = (argc == 2) && (argv[1][0] == 1);
+
+        if ((argc > 2) && !(all || return_nid)) {
+                fprintf(stderr, "usage: %s [all]\n", argv[0]);
                 return 0;
         }
-        if (g_nal == 0) {
-                fprintf(stderr, "Error: you must run the 'network' command "
-                        "first.\n");
-                return -1;
-        }
-        if (g_nal == SOCKNAL || g_nal == TOENAL) {
-                ptl_nid_t peer_nid;
-                struct hostent *he;
-                struct portal_ioctl_data data;
-                struct sockaddr_in srvaddr;
-                char *flag;
-                int fd, rc;
-                int nonagle = 0;
-                int rxmem = 0;
-                int txmem = 0;
-                int bind_irq = 0;
-                int xchange_nids = 0;
-                int o;
-                int olen;
-                
-                if (argc < 3) {
-                        goto usage;
-                }
-
-                he = ptl_gethostbyname(argv[1]);
-                if (!he)
-                        return -1;
 
-                g_port = atol(argv[2]);
-
-                if (argc > 3)
-                        for (flag = argv[3]; *flag != 0; flag++)
-                                switch (*flag)
-                                {
-                                case 'i':
-                                        bind_irq = 1;
-                                        break;
-                                        
-                                case 'x':
-                                        xchange_nids = 1;
-                                        break;
-
-                                default:
-                                        fprintf (stderr, "unrecognised flag '%c'\n",
-                                                 *flag);
-                                        return (-1);
-                                }
-                
-                memset(&srvaddr, 0, sizeof(srvaddr));
-                srvaddr.sin_family = AF_INET;
-                srvaddr.sin_port = htons(g_port);
-                srvaddr.sin_addr.s_addr = *(__u32 *)he->h_addr;
-        
-                fd = socket(PF_INET, SOCK_STREAM, 0);
-                if ( fd < 0 ) {
-                        fprintf(stderr, "socket() failed: %s\n",
-                                strerror(errno));
+        for (count = 0;; count++) {
+                LIBCFS_IOC_INIT (data);
+                data.ioc_count = count;
+                rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NI, &data);
+
+                if (rc < 0) {
+                        if ((count > 0) && (errno == ENOENT))
+                                /* We found them all */
+                                break;
+                        fprintf(stderr,"IOC_LIBCFS_GET_NI error %d: %s\n",
+                                errno, strerror(errno));
                         return -1;
                 }
 
-                if (g_socket_nonagle)
-                {
-                        o = 1;
-                        if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &o, sizeof (o)) != 0)
-                        { 
-                                fprintf(stderr, "cannot disable nagle: %s\n", strerror(errno));
-                                return (-1);
+                if (all || (LNET_NETTYP(LNET_NIDNET(data.ioc_nid)) != LOLND)) {
+                        printf("%s\n", libcfs_nid2str(data.ioc_nid));
+                        if (return_nid) {
+                                *(__u64 *)(argv[1]) = data.ioc_nid;
+                                return_nid--;
                         }
                 }
+        }
 
-                if (g_socket_rxmem != 0)
-                {
-                        o = g_socket_rxmem;
-                        if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &o, sizeof (o)) != 0)
-                        { 
-                                fprintf(stderr, "cannot set receive buffer size: %s\n", strerror(errno));
-                                return (-1);
-                        }
-                }
+        return 0;
+}
 
-                if (g_socket_txmem != 0)
-                {
-                        o = g_socket_txmem;
-                        if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &o, sizeof (o)) != 0)
-                        { 
-                                fprintf(stderr, "cannot set send buffer size: %s\n", strerror(errno));
-                                return (-1);
-                        }
-                }
+int
+jt_ptl_which_nid (int argc, char **argv)
+{
+        struct libcfs_ioctl_data data;
+        int          best_dist = 0;
+        int          best_order = 0;
+        lnet_nid_t   best_nid = LNET_NID_ANY;
+        int          dist;
+        int          order;
+        lnet_nid_t   nid;
+        char        *nidstr;
+        int          rc;
+        int          i;
 
-                rc = connect(fd, (struct sockaddr *)&srvaddr, sizeof(srvaddr));
-                if ( rc == -1 ) { 
-                        fprintf(stderr, "connect() failed: %s\n",
-                                strerror(errno));
+        if (argc < 2) {
+                fprintf(stderr, "usage: %s NID [NID...]\n", argv[0]);
+                return 0;
+        }
+
+        for (i = 1; i < argc; i++) {
+                nidstr = argv[i];
+                nid = libcfs_str2nid(nidstr);
+                if (nid == LNET_NID_ANY) {
+                        fprintf(stderr, "Can't parse NID %s\n", nidstr);
                         return -1;
                 }
 
-                olen = sizeof (txmem);
-                if (getsockopt (fd, SOL_SOCKET, SO_SNDBUF, &txmem, &olen) != 0)
-                        fprintf (stderr, "Can't get send buffer size: %s\n", strerror (errno));
-                olen = sizeof (rxmem);
-                if (getsockopt (fd, SOL_SOCKET, SO_RCVBUF, &rxmem, &olen) != 0)
-                        fprintf (stderr, "Can't get receive buffer size: %s\n", strerror (errno));
-                olen = sizeof (nonagle);
-                if (getsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &nonagle, &olen) != 0)
-                        fprintf (stderr, "Can't get nagle: %s\n", strerror (errno));
-
-                if (xchange_nids) {
-                        
-                        PORTAL_IOC_INIT (data);
-                        data.ioc_nal = g_nal;
-                        rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_GET_NID, &data);
-                        if (rc != 0)
-                        {
-                                fprintf (stderr, "failed to get my nid: %s\n",
-                                         strerror (errno));
-                                close (fd);
-                                return (-1);
-                        }
-                        
-                        rc = exchange_nids (fd, data.ioc_nid, &peer_nid);
-                        if (rc != 0)
-                        {
-                                close (fd);
-                                return (-1);
-                        }
-                }
-                else
-                        peer_nid = ntohl (srvaddr.sin_addr.s_addr); /* HOST byte order */
-
-                printf("Connected host: %s NID "LPX64" snd: %d rcv: %d nagle: %s\n", argv[1],
-                       peer_nid, txmem, rxmem, nonagle ? "Disabled" : "Enabled");
-
-                PORTAL_IOC_INIT(data);
-                data.ioc_fd = fd;
-                data.ioc_nal = g_nal;
-                data.ioc_nal_cmd = NAL_CMD_REGISTER_PEER_FD;
-                data.ioc_nid = peer_nid;
-                data.ioc_flags = bind_irq;
-                
-                rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_NAL_CMD, &data);
-                if (rc) {
-                        fprintf(stderr, "failed to register fd with portals: "
-                                "%s\n", strerror(errno));
-                        close (fd);
+                LIBCFS_IOC_INIT(data);
+                data.ioc_nid = nid;
+
+                rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_DIST, &data);
+                if (rc != 0) {
+                        fprintf(stderr, "Can't get distance to %s: %s\n",
+                                nidstr, strerror(errno));
                         return -1;
                 }
 
-                g_nid = peer_nid;
-                printf("Connection to "LPX64" registered with socknal\n", g_nid);
+                dist = data.ioc_u32[0];
+                order = data.ioc_u32[1];
 
-                rc = close(fd);
-                if (rc) {
-                        fprintf(stderr, "close failed: %d\n", rc);
-                }
-        } else if (g_nal == QSWNAL) {
-                g_nid = atoi(argv[1]);
-        } else if (g_nal == GMNAL) {
-                g_nid = atoi(argv[1]);
-        } else if (g_nal == SCIMACNAL) {
-                unsigned int    tmpnid;
-                if(sscanf(argv[1], "%x", &tmpnid) == 1) {
-                        g_nid=tmpnid;
-                }
-                else {
-                        fprintf(stderr, "nid %s invalid for SCI nal\n", argv[1]);
+                if (dist < 0) {
+                        if (dist == -EHOSTUNREACH)
+                                continue;
+
+                        fprintf(stderr, "Unexpected distance to %s: %d\n",
+                                nidstr, dist);
+                        return -1;
                 }
 
+                if (best_nid == LNET_NID_ANY ||
+                    dist < best_dist ||
+                    (dist == best_dist && order < best_order)) {
+                        best_dist = dist;
+                        best_order = order;
+                        best_nid = nid;
+                }
+        }
 
-        } else {
-                fprintf(stderr, "This should never happen.  Also it is very "
-                        "bad.\n");
+        if (best_nid == LNET_NID_ANY) {
+                fprintf(stderr, "No reachable NID\n");
+                return -1;
         }
 
+        printf("%s\n", libcfs_nid2str(best_nid));
         return 0;
 }
 
-int jt_ptl_disconnect(int argc, char **argv)
+int
+jt_ptl_print_interfaces (int argc, char **argv)
 {
-        if (argc > 2) {
-                fprintf(stderr, "usage: %s [hostname]\n", argv[0]);
-                return 0;
-        }
-        if (g_nal == 0) {
-                fprintf(stderr, "Error: you must run the 'network' command "
-                        "first.\n");
+        struct libcfs_ioctl_data data;
+       char                     buffer[3][HOST_NAME_MAX + 1];
+        int                      index;
+        int                      rc;
+
+        if (!g_net_is_compatible (argv[0], SOCKLND, 0))
                 return -1;
-        }
-        if (g_nal == SOCKNAL || g_nal == TOENAL) {
-                struct hostent *he;
-                struct portal_ioctl_data data;
-                int rc;
 
-                PORTAL_IOC_INIT(data);
-                if (argc == 2) {
-                        he = ptl_gethostbyname(argv[1]);
-                        if (!he) 
-                                return -1;
-                        
-                        data.ioc_nid = ntohl (*(__u32 *)he->h_addr); /* HOST byte order */
+        for (index = 0;;index++) {
+                LIBCFS_IOC_INIT(data);
+                data.ioc_net   = g_net;
+                data.ioc_count = index;
 
+                rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_INTERFACE, &data);
+                if (rc != 0)
+                        break;
+
+                printf ("%s: (%s/%s) npeer %d nroute %d\n",
+                       ptl_ipaddr_2_str(data.ioc_u32[0], buffer[2],
+                                        sizeof(buffer[2]), 1),
+                       ptl_ipaddr_2_str(data.ioc_u32[0], buffer[0],
+                                        sizeof(buffer[0]), 0),
+                       ptl_ipaddr_2_str(data.ioc_u32[1], buffer[1],
+                                        sizeof(buffer[1]), 0),
+                        data.ioc_u32[2], data.ioc_u32[3]);
+        }
+
+        if (index == 0) {
+                if (errno == ENOENT) {
+                        printf ("<no interfaces>\n");
                 } else {
-                        printf("Disconnecting ALL connections.\n");
-                        /* leave ioc_nid zeroed == disconnect all */
-                }
-                data.ioc_nal = g_nal;
-                data.ioc_nal_cmd = NAL_CMD_CLOSE_CONNECTION;
-                rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_NAL_CMD, &data);
-                if (rc) {
-                        fprintf(stderr, "failed to remove connection: %s\n",
+                        fprintf(stderr, "Error getting interfaces: %s: "
+                                "check dmesg.\n",
                                 strerror(errno));
-                        return -1;
                 }
-        } else if (g_nal == QSWNAL) {
-                printf("'disconnect' doesn't make any sense for "
-                        "elan.\n");
-        } else if (g_nal == GMNAL) {
-                printf("'disconnect' doesn't make any sense for "
-                        "GM.\n");
-        } else if (g_nal == SCIMACNAL) {
-                printf("'disconnect' doesn't make any sense for "
-                        "SCI.\n");
-        } else {
-                fprintf(stderr, "This should never happen.  Also it is very "
-                        "bad.\n");
-                return -1;
         }
 
         return 0;
 }
 
-int jt_ptl_push_connection (int argc, char **argv)
+int
+jt_ptl_add_interface (int argc, char **argv)
 {
-        if (argc > 2) {
-                fprintf(stderr, "usage: %s [hostname]\n", argv[0]);
+        struct libcfs_ioctl_data data;
+        __u32                    ipaddr;
+        int                      rc;
+        __u32                    netmask = 0xffffff00;
+        int                      i;
+        int                      count;
+        char                    *end;
+
+        if (argc < 2 || argc > 3) {
+                fprintf (stderr, "usage: %s ipaddr [netmask]\n", argv[0]);
                 return 0;
         }
-        if (g_nal == 0) {
-                fprintf(stderr, "Error: you must run the 'network' command "
-                        "first.\n");
+
+        if (!g_net_is_compatible(argv[0], SOCKLND, 0))
                 return -1;
-        }
-        if (g_nal == SOCKNAL || g_nal == TOENAL) {
-                struct hostent *he;
-                struct portal_ioctl_data data;
-                int rc;
 
-                PORTAL_IOC_INIT(data);
-                if (argc == 2) {
-                        he = ptl_gethostbyname(argv[1]);
-                        if (!he)
-                                return -1;
-                        
-                        data.ioc_nid = ntohl (*(__u32 *)he->h_addr); /* HOST byte order */
+        if (lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
+                fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
+                return -1;
+        }
 
-                } else {
-                        printf("Pushing ALL connections.\n");
-                        /* leave ioc_nid zeroed == disconnect all */
-                }
-                data.ioc_nal = g_nal;
-                data.ioc_nal_cmd = NAL_CMD_PUSH_CONNECTION;
-                rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_NAL_CMD, &data);
-                if (rc) {
-                        fprintf(stderr, "failed to push connection: %s\n",
-                                strerror(errno));
+        if (argc > 2 ) {
+                count = strtol(argv[2], &end, 0);
+                if (count > 0 && count < 32 && *end == 0) {
+                        netmask = 0;
+                        for (i = count; i > 0; i--)
+                                netmask = netmask|(1<<(32-i));
+                } else if (lnet_parse_ipquad(&netmask, argv[2]) != 0) {
+                        fprintf (stderr, "Can't parse netmask: %s\n", argv[2]);
                         return -1;
                 }
-        } else if (g_nal == QSWNAL) {
-                printf("'push' doesn't make any sense for elan.\n");
-        } else if (g_nal == GMNAL) {
-                printf("'push' doesn't make any sense for GM.\n");
-        } else if (g_nal == SCIMACNAL) {
-                printf("'push' doesn't make any sense for SCI.\n");
-        } else {
-                fprintf(stderr, "This should never happen.  Also it is very "
-                        "bad.\n");
+        }
+
+        LIBCFS_IOC_INIT(data);
+        data.ioc_net    = g_net;
+        data.ioc_u32[0] = ipaddr;
+        data.ioc_u32[1] = netmask;
+
+        rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_INTERFACE, &data);
+        if (rc != 0) {
+                fprintf (stderr, "failed to add interface: %s\n",
+                         strerror (errno));
                 return -1;
         }
 
         return 0;
 }
 
-int jt_ptl_ping(int argc, char **argv)
+int
+jt_ptl_del_interface (int argc, char **argv)
 {
-        int       rc;
-        ptl_nid_t nid;
-        long      count   = 1;
-        long      size    = 4;
-        long      timeout = 1;
-        struct portal_ioctl_data data;
+        struct libcfs_ioctl_data data;
+        int                      rc;
+        __u32                    ipaddr = 0;
 
-        if (argc < 2) {
-                fprintf(stderr, "usage: %s nid [count] [size] [timeout (secs)]\n", argv[0]);
+        if (argc > 2) {
+                fprintf (stderr, "usage: %s [ipaddr]\n", argv[0]);
                 return 0;
         }
 
-        if (g_nal == 0) {
-                fprintf(stderr, "Error: you must run the 'network' command "
-                        "first.\n");
+        if (!g_net_is_compatible(argv[0], SOCKLND, 0))
                 return -1;
-        }
 
-        if (ptl_parse_nid (&nid, argv[1]) != 0)
-        {
-                fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
-                return (-1);
+        if (argc == 2 &&
+            lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
+                fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
+                return -1;
         }
-        
-        if (argc > 2)
-        {
-                count = atol(argv[2]);
 
-                if (count < 0 || count > 20000) 
-                {
-                        fprintf(stderr, "are you insane?  %ld is a crazy count.\n", count);
-                        return -1;
-                }
-        }
-        
-        if (argc > 3)
-                size= atol(argv[3]);
+        LIBCFS_IOC_INIT(data);
+        data.ioc_net    = g_net;
+        data.ioc_u32[0] = ipaddr;
 
-        if (argc > 4)
-                timeout = atol (argv[4]);
-        
-        PORTAL_IOC_INIT (data);
-        data.ioc_count   = count;
-        data.ioc_size    = size;
-        data.ioc_nid     = nid;
-        data.ioc_nal     = g_nal;
-        data.ioc_timeout = timeout;
-        
-        rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_PING, &data);
-        if (rc) {
-                fprintf(stderr, "failed to start pinger: %s\n",
-                        strerror(errno));
+        rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_INTERFACE, &data);
+        if (rc != 0) {
+                fprintf (stderr, "failed to delete interface: %s\n",
+                         strerror (errno));
                 return -1;
         }
+
         return 0;
 }
 
-int jt_ptl_shownid(int argc, char **argv)
+int
+jt_ptl_print_peers (int argc, char **argv)
 {
-        struct portal_ioctl_data data;
+        struct libcfs_ioctl_data data;
+        lnet_process_id_t        id;
+       char                     buffer[2][HOST_NAME_MAX + 1];
+        int                      index;
         int                      rc;
-        
-        if (argc > 1) {
-                fprintf(stderr, "usage: %s\n", argv[0]);
-                return 0;
-        }
-        
-        if (g_nal == 0) {
-                fprintf(stderr, "Error: you must run the 'network' command first\n");
+
+       if (!g_net_is_compatible (argv[0], SOCKLND, RALND, MXLND,
+                                 O2IBLND, GNILND, 0))
                 return -1;
+
+        for (index = 0;;index++) {
+                LIBCFS_IOC_INIT(data);
+                data.ioc_net     = g_net;
+                data.ioc_count   = index;
+
+                rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER, &data);
+                if (rc != 0)
+                        break;
+
+                if (g_net_is_compatible(NULL, SOCKLND, 0)) {
+                        id.nid = data.ioc_nid;
+                        id.pid = data.ioc_u32[4];
+                        printf ("%-20s [%d]%s->%s:%d #%d\n",
+                                libcfs_id2str(id), 
+                                data.ioc_count, /* persistence */
+                               /* my ip */
+                               ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
+                                                sizeof(buffer[0]), 1),
+                               /* peer ip */
+                               ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
+                                                sizeof(buffer[1]), 1),
+                                data.ioc_u32[1], /* peer port */
+                                data.ioc_u32[3]); /* conn_count */
+                } else if (g_net_is_compatible(NULL, RALND, 0)) {
+                        printf ("%-20s [%d]@%s:%d\n",
+                                libcfs_nid2str(data.ioc_nid), /* peer nid */
+                                data.ioc_count,   /* peer persistence */
+                               /* peer ip */
+                               ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
+                                                sizeof(buffer[1]), 1),
+                                data.ioc_u32[1]); /* peer port */
+               } else if (g_net_is_compatible(NULL, GNILND, 0)) {
+                       int disconn = data.ioc_flags >> 16;
+                       char *state;
+
+                       if (disconn)
+                               state = "D";
+                       else
+                               state = data.ioc_flags & 0xffff ? "C" : "U";
+
+                       printf ("%-20s (%d) %s [%d] "LPU64" "
+                               "sq %d/%d tx %d/%d/%d\n",
+                               libcfs_nid2str(data.ioc_nid), /* peer nid */
+                               data.ioc_net, /* gemini device id */
+                               state, /* peer is Connecting, Up, or Down */
+                               data.ioc_count,   /* peer refcount */
+                               data.ioc_u64[0], /* peerstamp */
+                               data.ioc_u32[2], data.ioc_u32[3], /* tx and rx seq */
+                               /* fmaq, nfma, nrdma */
+                               data.ioc_u32[0], data.ioc_u32[1], data.ioc_u32[4]
+                               );
+                } else {
+                        printf ("%-20s [%d]\n",
+                                libcfs_nid2str(data.ioc_nid), data.ioc_count);
+                }
+        }
+
+        if (index == 0) {
+                if (errno == ENOENT) {
+                        printf ("<no peers>\n");
+                } else {
+                        fprintf(stderr, "Error getting peer list: %s: "
+                                "check dmesg.\n",
+                                strerror(errno));
+                }
         }
-        
-        PORTAL_IOC_INIT (data);
-        data.ioc_nal = g_nal;
-        rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_GET_NID, &data);
-        if (rc < 0)
-                fprintf(stderr, "getting my NID failed: %s\n",
-                        strerror (errno));
-        else
-                printf(LPX64"\n", data.ioc_nid);
         return 0;
 }
 
-int jt_ptl_mynid(int argc, char **argv)
+int
+jt_ptl_add_peer (int argc, char **argv)
 {
-        int rc;
-        char hostname[1024];
-        char *nidstr;
-        struct portal_ioctl_data data;
-        ptl_nid_t mynid;
-        
-        if (argc > 2) {
-                fprintf(stderr, "usage: %s [NID]\n", argv[0]);
-                fprintf(stderr, "NID defaults to the primary IP address of the machine.\n");
+        struct libcfs_ioctl_data data;
+        lnet_nid_t               nid;
+        __u32                    ip = 0;
+        int                      port = 0;
+        int                      rc;
+
+       if (!g_net_is_compatible (argv[0], SOCKLND, RALND,
+                                 GNILND, 0))
+                return -1;
+
+        if (argc != 4) {
+               fprintf (stderr, "usage(tcp,ra,gni): %s nid ipaddr port\n",
+                         argv[0]);
                 return 0;
         }
 
-        if (g_nal == 0) {
-                fprintf(stderr, "Error: you must run the 'network' command "
-                        "first.\n");
+        nid = libcfs_str2nid(argv[1]);
+        if (nid == LNET_NID_ANY) {
+                fprintf (stderr, "Can't parse NID: %s\n", argv[1]);
                 return -1;
         }
 
-        if (argc >= 2)
-                nidstr = argv[1];
-        else if (gethostname(hostname, sizeof(hostname)) != 0) {
-                fprintf(stderr, "gethostname failed: %s\n",
-                        strerror(errno));
+        if (lnet_parse_ipaddr (&ip, argv[2]) != 0) {
+                fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
                 return -1;
         }
-        else
-                nidstr = hostname;
 
-        rc = ptl_parse_nid (&mynid, nidstr);
+        if (lnet_parse_port (&port, argv[3]) != 0) {
+                fprintf (stderr, "Can't parse port: %s\n", argv[3]);
+                return -1;
+        }
+
+        LIBCFS_IOC_INIT(data);
+        data.ioc_net    = g_net;
+        data.ioc_nid    = nid;
+        data.ioc_u32[0] = ip;
+        data.ioc_u32[1] = port;
+
+        rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_ADD_PEER, &data);
         if (rc != 0) {
-                fprintf (stderr, "Can't convert '%s' into a NID\n", nidstr);
+                fprintf (stderr, "failed to add peer: %s\n",
+                         strerror (errno));
                 return -1;
         }
-        
-        PORTAL_IOC_INIT(data);
-        data.ioc_nid = mynid;
-        data.ioc_nal = g_nal;
-        data.ioc_nal_cmd = NAL_CMD_REGISTER_MYNID;
 
-        rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_NAL_CMD, &data);
-        if (rc < 0)
-                fprintf(stderr, "setting my NID failed: %s\n",
-                       strerror(errno));
-        else
-                printf("registered my nid "LPX64" (%s)\n", mynid, hostname);
         return 0;
 }
 
 int
-jt_ptl_fail_nid (int argc, char **argv)
+jt_ptl_del_peer (int argc, char **argv)
 {
+        struct libcfs_ioctl_data data;
+        lnet_nid_t               nid = LNET_NID_ANY;
+        lnet_pid_t               pid = LNET_PID_ANY;
+        __u32                    ip = 0;
         int                      rc;
-        ptl_nid_t                nid;
-        unsigned int             threshold;
-        struct portal_ioctl_data data;
 
-        if (argc < 2 || argc > 3)
-        {
-                fprintf (stderr, "usage: %s nid|\"_all_\" [count (0 == mend)]\n", argv[0]);
-                return (0);
+       if (!g_net_is_compatible (argv[0], SOCKLND, RALND, MXLND,
+                                 O2IBLND, GNILND, 0))
+                return -1;
+
+        if (g_net_is_compatible(NULL, SOCKLND, 0)) {
+                if (argc > 3) {
+                        fprintf (stderr, "usage: %s [nid] [ipaddr]\n",
+                                 argv[0]);
+                        return 0;
+                }
+        } else if (argc > 2) {
+                fprintf (stderr, "usage: %s [nid]\n", argv[0]);
+                return 0;
         }
-        
-        if (g_nal == 0) {
-                fprintf(stderr, "Error: you must run the 'network' command "
-                        "first.\n");
-                return (-1);
+
+        if (argc > 1 &&
+            !libcfs_str2anynid(&nid, argv[1])) {
+                fprintf (stderr, "Can't parse nid: %s\n", argv[1]);
+                return -1;
         }
 
-        if (!strcmp (argv[1], "_all_"))
-                nid = PTL_NID_ANY;
-        else if (ptl_parse_nid (&nid, argv[1]) != 0)
-        {
-                fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
-                return (-1);
+        if (g_net_is_compatible(NULL, SOCKLND, 0)) {
+                if (argc > 2 &&
+                    lnet_parse_ipaddr (&ip, argv[2]) != 0) {
+                        fprintf (stderr, "Can't parse ip addr: %s\n",
+                                 argv[2]);
+                        return -1;
+                }
         }
 
-        if (argc < 3)
-                threshold = PTL_MD_THRESH_INF;
-        else if (sscanf (argv[2], "%i", &threshold) != 1) {
-                fprintf (stderr, "Can't parse count \"%s\"\n", argv[2]);
-                return (-1);
-        }
-        
-        PORTAL_IOC_INIT (data);
-        data.ioc_nal = g_nal;
-        data.ioc_nid = nid;
-        data.ioc_count = threshold;
-        
-        rc = l_ioctl (PORTALS_DEV_ID, IOC_PORTAL_FAIL_NID, &data);
-        if (rc < 0)
-                fprintf (stderr, "IOC_PORTAL_FAIL_NID failed: %s\n",
+        LIBCFS_IOC_INIT(data);
+        data.ioc_net    = g_net;
+        data.ioc_nid    = nid;
+        data.ioc_u32[0] = ip;
+        data.ioc_u32[1] = pid;
+
+        rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_PEER, &data);
+        if (rc != 0) {
+                fprintf (stderr, "failed to remove peer: %s\n",
                          strerror (errno));
-        else
-                printf ("%s %s\n", threshold == 0 ? "Unfailing" : "Failing", argv[1]);
-        
-        return (0);
+                return -1;
+        }
+
+        return 0;
 }
 
 int
-jt_ptl_rxmem (int argc, char **argv)
+jt_ptl_print_connections (int argc, char **argv)
 {
-        int   size;
-        
-        if (argc > 1)
-        {
-                if (Parser_size (&size, argv[1]) != 0 || size < 0)
-                {
-                        fprintf (stderr, "Can't parse size %s\n", argv[1]);
-                        return (0);
+        struct libcfs_ioctl_data data;
+        lnet_process_id_t        id;
+       char                     buffer[2][HOST_NAME_MAX + 1];
+        int                      index;
+        int                      rc;
+
+       if (!g_net_is_compatible (argv[0], SOCKLND, RALND, MXLND, O2IBLND,
+                                 GNILND, 0))
+                return -1;
+
+        for (index = 0; ; index++) {
+                LIBCFS_IOC_INIT(data);
+                data.ioc_net     = g_net;
+                data.ioc_count   = index;
+
+                rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_CONN, &data);
+                if (rc != 0)
+                        break;
+
+                if (g_net_is_compatible (NULL, SOCKLND, 0)) {
+                        id.nid = data.ioc_nid;
+                        id.pid = data.ioc_u32[6];
+                        printf ("%-20s %s[%d]%s->%s:%d %d/%d %s\n",
+                                libcfs_id2str(id),
+                                (data.ioc_u32[3] == SOCKLND_CONN_ANY) ? "A" :
+                                (data.ioc_u32[3] == SOCKLND_CONN_CONTROL) ? "C" :
+                                (data.ioc_u32[3] == SOCKLND_CONN_BULK_IN) ? "I" :
+                                (data.ioc_u32[3] == SOCKLND_CONN_BULK_OUT) ? "O" : "?",
+                                data.ioc_u32[4], /* scheduler */
+                               /* local IP addr */
+                               ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
+                                                sizeof(buffer[0]), 1),
+                               /* remote IP addr */
+                               ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
+                                                sizeof(buffer[1]), 1),
+                                data.ioc_u32[1],         /* remote port */
+                                data.ioc_count, /* tx buffer size */
+                                data.ioc_u32[5], /* rx buffer size */
+                                data.ioc_flags ? "nagle" : "nonagle");
+                } else if (g_net_is_compatible (NULL, RALND, 0)) {
+                        printf ("%-20s [%d]\n",
+                                libcfs_nid2str(data.ioc_nid),
+                                data.ioc_u32[0] /* device id */);
+                } else if (g_net_is_compatible (NULL, O2IBLND, 0)) {
+                        printf ("%s mtu %d\n",
+                                libcfs_nid2str(data.ioc_nid),
+                                data.ioc_u32[0]); /* path MTU */
+               } else if (g_net_is_compatible (NULL, GNILND, 0)) {
+                       printf ("%-20s [%d]\n",
+                               libcfs_nid2str(data.ioc_nid),
+                               data.ioc_u32[0] /* device id */);
+                } else {
+                        printf ("%s\n", libcfs_nid2str(data.ioc_nid));
                 }
+        }
 
-                g_socket_rxmem = size;
+        if (index == 0) {
+                if (errno == ENOENT) {
+                        printf ("<no connections>\n");
+                } else {
+                        fprintf(stderr, "Error getting connection list: %s: "
+                                "check dmesg.\n",
+                                strerror(errno));
+                }
         }
-        printf ("Socket rmem = %d\n", g_socket_rxmem);        
-        return (0);
+        return 0;
 }
 
-int
-jt_ptl_txmem (int argc, char **argv)
+int jt_ptl_disconnect(int argc, char **argv)
 {
-        int   size;
-        
-        if (argc > 1)
-        {
-                if (Parser_size (&size, argv[1]) != 0 || size < 0)
-                {
-                        fprintf (stderr, "Can't parse size %s\n", argv[1]);
-                        return (0);
-                }
-                g_socket_txmem = size;
+        struct libcfs_ioctl_data data;
+        lnet_nid_t               nid = LNET_NID_ANY;
+        __u32                    ipaddr = 0;
+        int                      rc;
+
+        if (argc > 3) {
+                fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
+                return 0;
         }
-        printf ("Socket txmem = %d\n", g_socket_txmem);
-        return (0);
+
+       if (!g_net_is_compatible (NULL, SOCKLND, RALND, MXLND, O2IBLND,
+                                 GNILND, 0))
+                return 0;
+
+        if (argc >= 2 &&
+            !libcfs_str2anynid(&nid, argv[1])) {
+                fprintf (stderr, "Can't parse nid %s\n", argv[1]);
+                return -1;
+        }
+
+        if (g_net_is_compatible (NULL, SOCKLND, 0) &&
+            argc >= 3 &&
+            lnet_parse_ipaddr (&ipaddr, argv[2]) != 0) {
+                fprintf (stderr, "Can't parse ip addr %s\n", argv[2]);
+                return -1;
+        }
+
+        LIBCFS_IOC_INIT(data);
+        data.ioc_net     = g_net;
+        data.ioc_nid     = nid;
+        data.ioc_u32[0]  = ipaddr;
+
+        rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLOSE_CONNECTION, &data);
+        if (rc != 0) {
+                fprintf(stderr, "failed to remove connection: %s\n",
+                        strerror(errno));
+                return -1;
+        }
+
+        return 0;
+}
+
+int jt_ptl_push_connection (int argc, char **argv)
+{
+        struct libcfs_ioctl_data data;
+        int                      rc;
+        lnet_nid_t               nid = LNET_NID_ANY;
+
+        if (argc > 2) {
+                fprintf(stderr, "usage: %s [nid]\n", argv[0]);
+                return 0;
+        }
+
+       if (!g_net_is_compatible (argv[0], SOCKLND, GNILND, 0))
+                return -1;
+
+        if (argc > 1 &&
+            !libcfs_str2anynid(&nid, argv[1])) {
+                fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
+                return -1;
+        }
+
+        LIBCFS_IOC_INIT(data);
+        data.ioc_net     = g_net;
+        data.ioc_nid     = nid;
+
+        rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PUSH_CONNECTION, &data);
+        if (rc != 0) {
+                fprintf(stderr, "failed to push connection: %s\n",
+                        strerror(errno));
+                return -1;
+        }
+
+        return 0;
 }
 
 int
-jt_ptl_nagle (int argc, char **argv)
+jt_ptl_print_active_txs (int argc, char **argv)
 {
-        int enable;
+        struct libcfs_ioctl_data data;
+        int                      index;
+        int                      rc;
 
-        if (argc > 1)
-        {
-                if (Parser_bool (&enable, argv[1]) != 0)
-                {
-                        fprintf (stderr, "Can't parse boolean %s\n", argv[1]);
-                        return (0);
+        if (!g_net_is_compatible (argv[0], QSWLND, 0))
+                return -1;
+
+        for (index = 0;;index++) {
+                LIBCFS_IOC_INIT(data);
+                data.ioc_net   = g_net;
+                data.ioc_count = index;
+
+                rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_TXDESC, &data);
+                if (rc != 0)
+                        break;
+
+                printf ("type %u payload %6d to %s via %s by pid %6d: "
+                        "%s, %s, state %d\n",
+                        data.ioc_u32[0],
+                        data.ioc_count,
+                        libcfs_nid2str(data.ioc_nid),
+                        libcfs_nid2str(data.ioc_u64[0]),
+                        data.ioc_u32[1],
+                        (data.ioc_flags & 1) ? "delayed" : "immediate",
+                        (data.ioc_flags & 2) ? "nblk"    : "normal",
+                        data.ioc_flags >> 2);
+        }
+
+        if (index == 0) {
+                if (errno == ENOENT) {
+                        printf ("<no active descs>\n");
+                } else {
+                        fprintf(stderr, "Error getting active transmits list: "
+                                "%s: check dmesg.\n",
+                                strerror(errno));
                 }
-                g_socket_nonagle = !enable;
         }
-        printf ("Nagle %s\n", g_socket_nonagle ? "disabled" : "enabled");
-        return (0);
+        return 0;
 }
 
-int
-jt_ptl_add_route (int argc, char **argv)
+int jt_ptl_ping(int argc, char **argv)
 {
-        struct portal_ioctl_data data;
-        ptl_nid_t                nid1;
-        ptl_nid_t                nid2;
-        ptl_nid_t                gateway_nid;
         int                      rc;
-        
-        if (argc < 3)
-        {
-                fprintf (stderr, "usage: %s gateway target [target]\n", argv[0]);
-                return (0);
+        int                      timeout;
+        lnet_process_id_t        id;
+        lnet_process_id_t        ids[16];
+        int                      maxids = sizeof(ids)/sizeof(ids[0]);
+        struct libcfs_ioctl_data data;
+        char                    *sep;
+        int                      i;
+
+        if (argc < 2) {
+                fprintf(stderr, "usage: %s id [timeout (secs)]\n", argv[0]);
+                return 0;
         }
 
-        if (g_nal == 0) {
-                fprintf(stderr, "Error: you must run the 'network' command "
-                        "first.\n");
-                return (-1);
+        sep = strchr(argv[1], '-');
+        if (sep == NULL) {
+                rc = lnet_parse_nid(argv[1], &id);
+                if (rc != 0)
+                        return -1;
+        } else {
+                char   *end;
+
+                if (argv[1][0] == 'u' ||
+                    argv[1][0] == 'U')
+                        id.pid = strtoul(&argv[1][1], &end, 0) | LNET_PID_USERFLAG;
+                else
+                        id.pid = strtoul(argv[1], &end, 0);
+
+                if (end != sep) { /* assuming '-' is part of hostname */
+                        rc = lnet_parse_nid(argv[1], &id);
+                        if (rc != 0)
+                                return -1;
+                } else {
+                        id.nid = libcfs_str2nid(sep + 1);
+
+                        if (id.nid == LNET_NID_ANY) {
+                                fprintf(stderr,
+                                        "Can't parse process id \"%s\"\n",
+                                        argv[1]);
+                                return -1;
+                        }
+                }
         }
 
-        if (ptl_parse_nid (&gateway_nid, argv[1]) != 0)
-        {
-                fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
-                return (-1);
+        if (argc > 2)
+                timeout = 1000 * atol(argv[2]);
+        else
+                timeout = 1000;                 /* default 1 second timeout */
+
+        LIBCFS_IOC_INIT (data);
+        data.ioc_nid     = id.nid;
+        data.ioc_u32[0]  = id.pid;
+        data.ioc_u32[1]  = timeout;
+        data.ioc_plen1   = sizeof(ids);
+        data.ioc_pbuf1   = (char *)ids;
+
+        rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PING, &data);
+        if (rc != 0) {
+                fprintf(stderr, "failed to ping %s: %s\n",
+                        id.pid == LNET_PID_ANY ?
+                        libcfs_nid2str(id.nid) : libcfs_id2str(id),
+                        strerror(errno));
+                return -1;
+        }
+
+        for (i = 0; i < data.ioc_count && i < maxids; i++)
+                printf("%s\n", libcfs_id2str(ids[i]));
+
+        if (data.ioc_count > maxids)
+                printf("%d out of %d ids listed\n", maxids, data.ioc_count);
+
+        return 0;
+}
+
+int jt_ptl_mynid(int argc, char **argv)
+{
+        struct libcfs_ioctl_data data;
+        lnet_nid_t               nid;
+        int rc;
+
+        if (argc != 2) {
+                fprintf(stderr, "usage: %s NID\n", argv[0]);
+                return 0;
         }
 
-        if (ptl_parse_nid (&nid1, argv[2]) != 0)
+        nid = libcfs_str2nid(argv[1]);
+        if (nid == LNET_NID_ANY) {
+                fprintf(stderr, "Can't parse NID '%s'\n", argv[1]);
+                return -1;
+        }
+
+        LIBCFS_IOC_INIT(data);
+        data.ioc_net = LNET_NIDNET(nid);
+        data.ioc_nid = nid;
+
+        rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_REGISTER_MYNID, &data);
+        if (rc < 0)
+                fprintf(stderr, "setting my NID failed: %s\n",
+                       strerror(errno));
+        else
+                printf("registered my nid %s\n", libcfs_nid2str(nid));
+
+        return 0;
+}
+
+int
+jt_ptl_fail_nid (int argc, char **argv)
+{
+        int                      rc;
+        lnet_nid_t               nid;
+       int                      threshold;
+        struct libcfs_ioctl_data data;
+
+        if (argc < 2 || argc > 3)
         {
-                fprintf (stderr, "Can't parse first target NID \"%s\"\n", argv[2]);
-                return (-1);
+                fprintf (stderr, "usage: %s nid|\"*\" [count (0 == mend)]\n", argv[0]);
+                return (0);
         }
 
-        if (argc < 4)
-                nid2 = nid1;
-        else if (ptl_parse_nid (&nid2, argv[3]) != 0)
+        if (!libcfs_str2anynid(&nid, argv[1]))
         {
-                fprintf (stderr, "Can't parse second target NID \"%s\"\n", argv[4]);
+                fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
                 return (-1);
         }
 
-        PORTAL_IOC_INIT(data);
-        data.ioc_nid = gateway_nid;
-        data.ioc_nal = g_nal;
-        data.ioc_nid2 = MIN (nid1, nid2);
-        data.ioc_nid3 = MAX (nid1, nid2);
-
-        rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_ADD_ROUTE, &data);
-        if (rc != 0) 
-        {
-                fprintf (stderr, "IOC_PORTAL_ADD_ROUTE failed: %s\n", strerror (errno));
+        if (argc < 3) {
+                threshold = LNET_MD_THRESH_INF;
+       } else if (sscanf(argv[2], "%i", &threshold) != 1) {
+                fprintf (stderr, "Can't parse count \"%s\"\n", argv[2]);
                 return (-1);
         }
-        
+
+        LIBCFS_IOC_INIT (data);
+        data.ioc_nid = nid;
+        data.ioc_count = threshold;
+
+        rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_FAIL_NID, &data);
+        if (rc < 0)
+                fprintf (stderr, "IOC_LIBCFS_FAIL_NID failed: %s\n",
+                         strerror (errno));
+        else
+                printf ("%s %s\n", threshold == 0 ? "Unfailing" : "Failing", argv[1]);
+
         return (0);
 }
 
 int
+jt_ptl_add_route (int argc, char **argv)
+{
+       struct lnet_ioctl_config_data data;
+       lnet_nid_t               gateway_nid;
+       unsigned int             hops = 1;
+       unsigned int             priority = 0;
+       char                    *end;
+       int                      rc;
+
+       if (argc < 2 || argc > 4) {
+               fprintf(stderr, "usage: %s gateway [hopcount [priority]]\n",
+                       argv[0]);
+               return -1;
+       }
+
+       if (g_net_is_set(argv[0]) == 0)
+               return -1;
+
+       gateway_nid = libcfs_str2nid(argv[1]);
+       if (gateway_nid == LNET_NID_ANY) {
+               fprintf(stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
+               return -1;
+       }
+
+       if (argc > 2) {
+               hops = strtoul(argv[2], &end, 0);
+               if (hops == 0 || hops >= 256 || (end != NULL && *end != 0)) {
+                       fprintf(stderr, "Can't parse hopcount \"%s\"\n",
+                               argv[2]);
+                       return -1;
+               }
+               if (argc == 4) {
+                       priority = strtoul(argv[3], &end, 0);
+                       if (end != NULL && *end != 0) {
+                               fprintf(stderr,
+                                       "Can't parse priority \"%s\"\n",
+                                       argv[3]);
+                               return -1;
+                       }
+               }
+       }
+
+       LIBCFS_IOC_INIT_V2(data, cfg_hdr);
+       data.cfg_net = g_net;
+       data.cfg_config_u.cfg_route.rtr_hop = hops;
+       data.cfg_nid = gateway_nid;
+       data.cfg_config_u.cfg_route.rtr_priority = priority;
+
+       rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
+       if (rc != 0) {
+               fprintf(stderr, "IOC_LIBCFS_ADD_ROUTE failed: %s\n",
+                       strerror(errno));
+               return -1;
+       }
+
+       return 0;
+}
+
+int
 jt_ptl_del_route (int argc, char **argv)
 {
-        struct portal_ioctl_data data;
-        ptl_nid_t                nid;
+       struct lnet_ioctl_config_data data;
+       lnet_nid_t               nid;
+       int                      rc;
+
+       if (argc != 2) {
+               fprintf(stderr, "usage: %s gatewayNID\n", argv[0]);
+               return 0;
+       }
+
+       if (libcfs_str2anynid(&nid, argv[1]) == 0) {
+               fprintf(stderr, "Can't parse gateway NID "
+                       "\"%s\"\n", argv[1]);
+               return -1;
+       }
+
+       LIBCFS_IOC_INIT_V2(data, cfg_hdr);
+       data.cfg_net = g_net_set ? g_net : LNET_NIDNET(LNET_NID_ANY);
+       data.cfg_nid = nid;
+
+       rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
+       if (rc != 0) {
+               fprintf(stderr, "IOC_LIBCFS_DEL_ROUTE (%s) failed: %s\n",
+                       libcfs_nid2str(nid), strerror(errno));
+               return -1;
+       }
+
+       return 0;
+}
+
+int
+jt_ptl_notify_router (int argc, char **argv)
+{
+        struct libcfs_ioctl_data data;
+        int                      enable;
+        lnet_nid_t               nid;
         int                      rc;
-        
-        if (argc < 2)
+        struct timeval           now;
+        time_t                   when;
+
+        if (argc < 3)
         {
-                fprintf (stderr, "usage: %s targetNID\n", argv[0]);
+                fprintf (stderr, "usage: %s targetNID <up/down> [<time>]\n", 
+                         argv[0]);
                 return (0);
         }
 
-        if (ptl_parse_nid (&nid, argv[1]) != 0)
-        {
+        nid = libcfs_str2nid(argv[1]);
+        if (nid == LNET_NID_ANY) {
                 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[1]);
                 return (-1);
         }
 
-        PORTAL_IOC_INIT(data);
+        if (lnet_parse_bool (&enable, argv[2]) != 0) {
+                fprintf (stderr, "Can't parse boolean %s\n", argv[2]);
+                return (-1);
+        }
+
+        gettimeofday(&now, NULL);
+
+        if (argc < 4) {
+                when = now.tv_sec;
+        } else if (lnet_parse_time (&when, argv[3]) != 0) {
+                fprintf(stderr, "Can't parse time %s\n"
+                        "Please specify either 'YYYY-MM-DD-HH:MM:SS'\n"
+                        "or an absolute unix time in seconds\n", argv[3]);
+                return (-1);
+        } else if (when > now.tv_sec) {
+                fprintf (stderr, "%s specifies a time in the future\n",
+                         argv[3]);
+                return (-1);
+        }
+
+        LIBCFS_IOC_INIT(data);
         data.ioc_nid = nid;
+        data.ioc_flags = enable;
+        /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
+        data.ioc_u64[0] = (__u64)when;
 
-        rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_DEL_ROUTE, &data);
-        if (rc != 0) 
-        {
-                fprintf (stderr, "IOC_PORTAL_DEL_ROUTE ("LPX64") failed: %s\n", nid, strerror (errno));
+        rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_NOTIFY_ROUTER, &data);
+        if (rc != 0) {
+                fprintf (stderr, "IOC_LIBCFS_NOTIFY_ROUTER (%s) failed: %s\n",
+                         libcfs_nid2str(nid), strerror (errno));
                 return (-1);
         }
-        
+
         return (0);
 }
 
 int
 jt_ptl_print_routes (int argc, char **argv)
 {
-        char                      buffer[3][128];
-        struct portal_ioctl_data  data;
+       struct lnet_ioctl_config_data  data;
+       int                       rc;
+       int                       index;
+       __u32                     net;
+       lnet_nid_t                nid;
+       unsigned int              hops;
+       int                       alive;
+       unsigned int              pri;
+
+       for (index = 0; ; index++) {
+               LIBCFS_IOC_INIT_V2(data, cfg_hdr);
+               data.cfg_count = index;
+
+               rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
+               if (rc != 0)
+                       break;
+
+               net     = data.cfg_net;
+               hops    = data.cfg_config_u.cfg_route.rtr_hop;
+               nid     = data.cfg_nid;
+               alive   = data.cfg_config_u.cfg_route.rtr_flags;
+               pri     = data.cfg_config_u.cfg_route.rtr_priority;
+
+               printf("net %18s hops %u gw %32s %s pri %u\n",
+                      libcfs_net2str(net), hops,
+                      libcfs_nid2str(nid), alive ? "up" : "down", pri);
+       }
+
+       if (errno != ENOENT)
+               fprintf(stderr, "Error getting routes: %s: check dmesg.\n",
+                       strerror(errno));
+
+       return 0;
+}
+
+static int
+fault_attr_nid_parse(char *str, lnet_nid_t *nid_p)
+{
+       lnet_nid_t nid;
+       __u32      net;
+       int        rc = 0;
+
+       /* NB: can't support range ipaddress except * and *@net */
+       if (strlen(str) > 2 && str[0] == '*' && str[1] == '@') {
+               net = libcfs_str2net(str + 2);
+               if (net == LNET_NIDNET(LNET_NID_ANY))
+                       goto failed;
+
+               nid = LNET_MKNID(net, LNET_NIDADDR(LNET_NID_ANY));
+       } else {
+               rc = libcfs_str2anynid(&nid, str);
+               if (!rc)
+                       goto failed;
+       }
+
+       *nid_p = nid;
+       return 0;
+failed:
+       fprintf(stderr, "Invalid NID : %s\n", str);
+       return -1;
+}
+
+static int
+fault_attr_msg_parse(char *msg_str, __u32 *mask_p)
+{
+       if (!strcasecmp(msg_str, "put")) {
+               *mask_p |= LNET_PUT_BIT;
+               return 0;
+
+       } else if (!strcasecmp(msg_str, "ack")) {
+               *mask_p |= LNET_ACK_BIT;
+               return 0;
+
+       } else if (!strcasecmp(msg_str, "get")) {
+               *mask_p |= LNET_GET_BIT;
+               return 0;
+
+       } else if (!strcasecmp(msg_str, "reply")) {
+               *mask_p |= LNET_REPLY_BIT;
+               return 0;
+       }
+
+       fprintf(stderr, "unknown message type %s\n", msg_str);
+       return -1;
+}
+
+static int
+fault_attr_ptl_parse(char *ptl_str, __u64 *mask_p)
+{
+       unsigned long rc = strtoul(optarg, NULL, 0);
+
+       if (rc >= 64) {
+               fprintf(stderr, "invalid portal: %lu\n", rc);
+               return -1;
+       }
+
+       *mask_p |= (1ULL << rc);
+       return 0;
+}
+
+static int
+fault_simul_rule_add(__u32 opc, char *name, int argc, char **argv)
+{
+       struct libcfs_ioctl_data  data = {{0}};
+       struct lnet_fault_attr    attr;
+       char                     *optstr;
+       int                       rc;
+
+       static struct option opts[] = {
+               {"source",      required_argument,      0,      's'},
+               {"dest",        required_argument,      0,      'd'},
+               {"rate",        required_argument,      0,      'r'},
+               {"interval",    required_argument,      0,      'i'},
+               {"latency",     required_argument,      0,      'l'},
+               {"portal",      required_argument,      0,      'p'},
+               {"message",     required_argument,      0,      'm'},
+               {0, 0, 0, 0}
+       };
+
+       if (argc == 1) {
+               fprintf(stderr, "Failed, please provide source, destination "
+                               "and rate of rule\n");
+               return -1;
+       }
+
+       optstr = opc == LNET_CTL_DROP_ADD ? "s:d:r:i:p:m:" : "s:d:r:l:p:m:";
+       memset(&attr, 0, sizeof(attr));
+       while (1) {
+               char c = getopt_long(argc, argv, optstr, opts, NULL);
+
+               if (c == -1)
+                       break;
+
+               switch (c) {
+               case 's': /* source NID/NET */
+                       rc = fault_attr_nid_parse(optarg, &attr.fa_src);
+                       if (rc != 0)
+                               goto getopt_failed;
+                       break;
+
+               case 'd': /* dest NID/NET */
+                       rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
+                       if (rc != 0)
+                               goto getopt_failed;
+                       break;
+
+               case 'r': /* drop rate */
+                       if (opc == LNET_CTL_DROP_ADD)
+                               attr.u.drop.da_rate = strtoul(optarg, NULL, 0);
+                       else
+                               attr.u.delay.la_rate = strtoul(optarg, NULL, 0);
+                       break;
+
+               case 'i': /* time interval (# seconds) for message drop */
+                       if (opc == LNET_CTL_DROP_ADD)
+                               attr.u.drop.da_interval = strtoul(optarg,
+                                                                 NULL, 0);
+                       else
+                               attr.u.delay.la_interval = strtoul(optarg,
+                                                                  NULL, 0);
+                       break;
+
+               case 'l': /* seconds to wait before activating rule */
+                       attr.u.delay.la_latency = strtoul(optarg, NULL, 0);
+                       break;
+
+               case 'p': /* portal to filter */
+                       rc = fault_attr_ptl_parse(optarg, &attr.fa_ptl_mask);
+                       if (rc != 0)
+                               goto getopt_failed;
+                       break;
+
+               case 'm': /* message types to filter */
+                       rc = fault_attr_msg_parse(optarg, &attr.fa_msg_mask);
+                       if (rc != 0)
+                               goto getopt_failed;
+                       break;
+
+               default:
+                       fprintf(stderr, "error: %s: option '%s' "
+                               "unrecognized\n", argv[0], argv[optind - 1]);
+                       goto getopt_failed;
+               }
+       }
+       optind = 1;
+
+       if (opc == LNET_CTL_DROP_ADD) {
+               /* NB: drop rate and interval are exclusive to each other */
+               if (!((attr.u.drop.da_rate == 0) ^
+                     (attr.u.drop.da_interval == 0))) {
+                       fprintf(stderr,
+                               "please provide either drop rate or interval "
+                               "but not both at the same time.\n");
+                       return -1;
+               }
+       } else if (opc == LNET_CTL_DELAY_ADD) {
+               if (!((attr.u.delay.la_rate == 0) ^
+                     (attr.u.delay.la_interval == 0))) {
+                       fprintf(stderr,
+                               "please provide either delay rate or interval "
+                               "but not both at the same time.\n");
+                       return -1;
+               }
+
+               if (attr.u.delay.la_latency == 0) {
+                       fprintf(stderr, "latency cannot be zero\n");
+                       return -1;
+               }
+       }
+
+       if (attr.fa_src == 0 || attr.fa_dst == 0) {
+               fprintf(stderr, "Please provide both source and destination "
+                               "of %s rule\n", name);
+               return -1;
+       }
+
+       data.ioc_flags = opc;
+       data.ioc_inllen1 = sizeof(attr);
+       data.ioc_inlbuf1 = (char *)&attr;
+       if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
+               fprintf(stderr, "libcfs_ioctl_pack failed\n");
+               return -1;
+       }
+
+       rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
+       if (rc != 0) {
+               fprintf(stderr, "add %s rule %s->%s failed: %s\n",
+                       name, libcfs_nid2str(attr.fa_src),
+                       libcfs_nid2str(attr.fa_dst), strerror(errno));
+               return -1;
+       }
+
+       printf("Added %s rule %s->%s (1/%d)\n",
+              name, libcfs_nid2str(attr.fa_src), libcfs_nid2str(attr.fa_dst),
+              opc == LNET_CTL_DROP_ADD ?
+              attr.u.drop.da_rate : attr.u.delay.la_rate);
+       return 0;
+
+getopt_failed:
+       optind = 1;
+       return -1;
+}
+
+int
+jt_ptl_drop_add(int argc, char **argv)
+{
+       return fault_simul_rule_add(LNET_CTL_DROP_ADD, "drop", argc, argv);
+}
+
+int
+jt_ptl_delay_add(int argc, char **argv)
+{
+       return fault_simul_rule_add(LNET_CTL_DELAY_ADD, "delay", argc, argv);
+}
+
+static int
+fault_simul_rule_del(__u32 opc, char *name, int argc, char **argv)
+{
+       struct libcfs_ioctl_data data = {{0}};
+       struct lnet_fault_attr   attr;
+       bool                     all = false;
+       int                      rc;
+
+       static struct option opts[] = {
+               {"source",      required_argument,      0,      's'},
+               {"dest",        required_argument,      0,      'd'},
+               {"all",         no_argument,            0,      'a'},
+               {0, 0, 0, 0}
+       };
+
+       if (argc == 1) {
+               fprintf(stderr, "Failed, please provide source and "
+                               "destination of rule\n");
+               return -1;
+       }
+
+       memset(&attr, 0, sizeof(attr));
+       while (1) {
+               char c = getopt_long(argc, argv, "s:d:a", opts, NULL);
+
+               if (c == -1 || all)
+                       break;
+
+               switch (c) {
+               case 's':
+                       rc = fault_attr_nid_parse(optarg, &attr.fa_src);
+                       if (rc != 0)
+                               goto getopt_failed;
+                       break;
+               case 'd':
+                       rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
+                       if (rc != 0)
+                               goto getopt_failed;
+                       break;
+               case 'a':
+                       attr.fa_src = attr.fa_dst = 0;
+                       all = true;
+                       break;
+               default:
+                       fprintf(stderr, "error: %s: option '%s' "
+                               "unrecognized\n", argv[0], argv[optind - 1]);
+                       goto getopt_failed;
+               }
+       }
+       optind = 1;
+
+       data.ioc_flags = opc;
+       data.ioc_inllen1 = sizeof(attr);
+       data.ioc_inlbuf1 = (char *)&attr;
+       if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
+               fprintf(stderr, "libcfs_ioctl_pack failed\n");
+               return -1;
+       }
+
+       rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
+       if (rc != 0) {
+               fprintf(stderr, "remove %s rule %s->%s failed: %s\n", name,
+                       all ? "all" : libcfs_nid2str(attr.fa_src),
+                       all ? "all" : libcfs_nid2str(attr.fa_dst),
+                       strerror(errno));
+               return -1;
+       }
+
+       libcfs_ioctl_unpack(&data, ioc_buf);
+       printf("Removed %d %s rules\n", data.ioc_count, name);
+       return 0;
+
+getopt_failed:
+       optind = 1;
+       return -1;
+}
+
+int
+jt_ptl_drop_del(int argc, char **argv)
+{
+       return fault_simul_rule_del(LNET_CTL_DROP_DEL, "drop", argc, argv);
+}
+
+int
+jt_ptl_delay_del(int argc, char **argv)
+{
+       return fault_simul_rule_del(LNET_CTL_DELAY_DEL, "delay", argc, argv);
+}
+
+static int
+fault_simul_rule_reset(__u32 opc, char *name, int argc, char **argv)
+{
+       struct libcfs_ioctl_data   data = {{0}};
+       int                        rc;
+
+       LIBCFS_IOC_INIT(data);
+       data.ioc_flags = opc;
+
+       rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, &data);
+       if (rc != 0) {
+               fprintf(stderr, "failed to reset %s stats: %s\n",
+                       name, strerror(errno));
+               return -1;
+       }
+       return 0;
+}
+
+int
+jt_ptl_drop_reset(int argc, char **argv)
+{
+       return fault_simul_rule_reset(LNET_CTL_DROP_RESET, "drop", argc, argv);
+}
+
+int
+jt_ptl_delay_reset(int argc, char **argv)
+{
+       return fault_simul_rule_reset(LNET_CTL_DELAY_RESET, "delay",
+                                     argc, argv);
+}
+
+static int
+fault_simul_rule_list(__u32 opc, char *name, int argc, char **argv)
+{
+       struct libcfs_ioctl_data data = {{0}};
+       struct lnet_fault_attr   attr;
+       struct lnet_fault_stat   stat;
+       int                      pos;
+
+       printf("LNet %s rules:\n", name);
+       for (pos = 0;; pos++) {
+               int             rc;
+
+               memset(&attr, 0, sizeof(attr));
+               memset(&stat, 0, sizeof(stat));
+
+               data.ioc_count = pos;
+               data.ioc_flags = opc;
+               data.ioc_inllen1 = sizeof(attr);
+               data.ioc_inlbuf1 = (char *)&attr;
+               data.ioc_inllen2 = sizeof(stat);
+               data.ioc_inlbuf2 = (char *)&stat;
+               if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
+                       fprintf(stderr, "libcfs_ioctl_pack failed\n");
+                       return -1;
+               }
+
+               rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
+               if (rc != 0)
+                       break;
+
+               libcfs_ioctl_unpack(&data, ioc_buf);
+
+               if (opc == LNET_CTL_DROP_LIST) {
+                       printf("%s->%s (1/%d | %d) ptl "LPX64", msg %x, "
+                              LPU64"/"LPU64", PUT "LPU64", ACK "LPU64", GET "
+                              LPU64", REP "LPU64"\n",
+                              libcfs_nid2str(attr.fa_src),
+                              libcfs_nid2str(attr.fa_dst),
+                              attr.u.drop.da_rate, attr.u.drop.da_interval,
+                              attr.fa_ptl_mask, attr.fa_msg_mask,
+                              stat.u.drop.ds_dropped, stat.fs_count,
+                              stat.fs_put, stat.fs_ack,
+                              stat.fs_get, stat.fs_reply);
+
+               } else if (opc == LNET_CTL_DELAY_LIST) {
+                       printf("%s->%s (1/%d | %d, latency %d) ptl "LPX64
+                              ", msg %x, "LPU64"/"LPU64", PUT "LPU64
+                              ", ACK "LPU64", GET "LPU64", REP "LPU64"\n",
+                              libcfs_nid2str(attr.fa_src),
+                              libcfs_nid2str(attr.fa_dst),
+                              attr.u.delay.la_rate, attr.u.delay.la_interval,
+                              attr.u.delay.la_latency,
+                              attr.fa_ptl_mask, attr.fa_msg_mask,
+                              stat.u.delay.ls_delayed, stat.fs_count,
+                              stat.fs_put, stat.fs_ack, stat.fs_get,
+                              stat.fs_reply);
+               }
+       }
+       printf("found total %d\n", pos);
+
+       return 0;
+}
+
+int
+jt_ptl_drop_list(int argc, char **argv)
+{
+       return fault_simul_rule_list(LNET_CTL_DROP_LIST, "drop", argc, argv);
+}
+
+int
+jt_ptl_delay_list(int argc, char **argv)
+{
+       return fault_simul_rule_list(LNET_CTL_DELAY_LIST, "delay", argc, argv);
+}
+
+double
+get_cycles_per_usec ()
+{
+        FILE      *f = fopen ("/proc/cpuinfo", "r");
+        double     mhz;
+        char      line[64];
+
+        if (f != NULL) {
+                while (fgets (line, sizeof (line), f) != NULL)
+                        if (sscanf (line, "cpu MHz : %lf", &mhz) == 1) {
+                                fclose (f);
+                                return (mhz);
+                        }
+                fclose (f);
+        }
+
+        fprintf (stderr, "Can't read/parse /proc/cpuinfo\n");
+        return (1000.0);
+}
+
+int jt_ptl_memhog(int argc, char **argv)
+{
+        static int                gfp = 0;        /* sticky! */
+
+        struct libcfs_ioctl_data  data;
         int                       rc;
-        int                       index;
-        int                      gateway_nal;
-        ptl_nid_t                gateway_nid;
-        ptl_nid_t                nid1;
-        ptl_nid_t                nid2;
-        
-        
-        for (index = 0;;index++)
-        {
-                PORTAL_IOC_INIT(data);
-                data.ioc_count = index;
-                
-                rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_GET_ROUTE, &data);
-                if (rc != 0)
-                        break;
+        int                       count;
+        char                     *end;
+
+        if (argc < 2)  {
+                fprintf(stderr, "usage: %s <npages> [<GFP flags>]\n", argv[0]);
+                return 0;
+        }
 
-                gateway_nal = data.ioc_nal;
-                gateway_nid = data.ioc_nid;
-                nid1 = data.ioc_nid2;
-                nid2 = data.ioc_nid3;
-                
-                printf ("%8s %18s : %s - %s\n", 
-                        nal2name (gateway_nal), 
-                        ptl_nid2str (buffer[0], gateway_nid),
-                        ptl_nid2str (buffer[1], nid1),
-                        ptl_nid2str (buffer[2], nid2));
+        count = strtol(argv[1], &end, 0);
+        if (count < 0 || *end != 0) {
+                fprintf(stderr, "Can't parse page count '%s'\n", argv[1]);
+                return -1;
         }
-        return (0);
+
+        if (argc >= 3) {
+                rc = strtol(argv[2], &end, 0);
+                if (*end != 0) {
+                        fprintf(stderr, "Can't parse gfp flags '%s'\n", argv[2]);
+                        return -1;
+                }
+                gfp = rc;
+        }
+
+        LIBCFS_IOC_INIT(data);
+        data.ioc_count = count;
+        data.ioc_flags = gfp;
+        rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_MEMHOG, &data);
+
+        if (rc != 0) {
+                fprintf(stderr, "memhog %d failed: %s\n", count, strerror(errno));
+                return -1;
+        }
+
+        printf("memhog %d OK\n", count);
+        return 0;
 }
 
+int jt_ptl_testprotocompat(int argc, char **argv)
+{
+        struct libcfs_ioctl_data  data;
+        int                       rc;
+        int                       flags;
+        char                     *end;
+
+        if (argc < 2)  {
+                fprintf(stderr, "usage: %s <number>\n", argv[0]);
+                return 0;
+        }
+
+        flags = strtol(argv[1], &end, 0);
+        if (flags < 0 || *end != 0) {
+                fprintf(stderr, "Can't parse flags '%s'\n", argv[1]);
+                return -1;
+        }
+
+        LIBCFS_IOC_INIT(data);
+        data.ioc_flags = flags;
+        rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_TESTPROTOCOMPAT, &data);
+
+        if (rc != 0) {
+                fprintf(stderr, "test proto compat %x failed: %s\n",
+                        flags, strerror(errno));
+                return -1;
+        }
+
+        printf("test proto compat %x OK\n", flags);
+        return 0;
+}
+
+