Whamcloud - gitweb
LU-2934 lnet: Add LNet Router Priority parameter
[fs/lustre-release.git] / lnet / utils / portals.c
index 96d5a5e..2b3aada 100644 (file)
@@ -1,7 +1,5 @@
-/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=8:tabstop=8:
- *
- * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
+/*
+ * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
  *
  *   This file is part of Portals, http://www.sf.net/projects/lustre/
  *
  *
  */
 
-#include <stdio.h>
-#include <sys/types.h>
-#ifdef HAVE_NETDB_H
-#include <netdb.h>
-#endif
-#include <sys/socket.h>
-#ifdef HAVE_NETINET_TCP_H
-#include <netinet/tcp.h>
-#endif
-#include <stdlib.h>
-#include <string.h>
-#include <fcntl.h>
-#ifdef HAVE_SYS_IOCTL_H
-#include <sys/ioctl.h>
-#endif
-#ifndef _IOWR
-#include "ioctl.h"
-#endif
-#include <errno.h>
-#include <unistd.h>
-#include <time.h>
-#include <stdarg.h>
-#ifdef HAVE_ENDIAN_H
-#include <endian.h>
-#endif
-
-#include <libcfs/portals_utils.h>
+#include <libcfs/libcfsutil.h>
 #include <lnet/api-support.h>
 #include <lnet/lnetctl.h>
 #include <lnet/socklnd.h>
-#include "parser.h"
 
 unsigned int libcfs_debug;
 unsigned int libcfs_printk = D_CANTMASK;
@@ -171,7 +142,7 @@ lnet_parse_ipaddr (__u32 *ipaddrp, char *str)
 }
 
 char *
-ptl_ipaddr_2_str (__u32 ipaddr, char *str, int lookup)
+ptl_ipaddr_2_str(__u32 ipaddr, char *str, size_t strsize, int lookup)
 {
 #ifdef HAVE_GETHOSTBYNAME
         __u32           net_ip;
@@ -181,7 +152,8 @@ ptl_ipaddr_2_str (__u32 ipaddr, char *str, int lookup)
                 net_ip = htonl (ipaddr);
                 he = gethostbyaddr (&net_ip, sizeof (net_ip), AF_INET);
                 if (he != NULL) {
-                        strcpy(str, he->h_name);
+                       strncpy(str, he->h_name, strsize - 1);
+                       str[strsize - 1] = '\0';
                         return (str);
                 }
         }
@@ -222,6 +194,19 @@ lnet_parse_time (time_t *t, char *str)
         return (0);
 }
 
+int
+lnet_parse_nid(char *nid_str, lnet_process_id_t *id_ptr)
+{
+        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;
+}
+
 int g_net_is_set (char *cmd)
 {
         if (g_net_set)
@@ -320,6 +305,14 @@ int jt_ptl_network(int argc, char **argv)
                 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;
+        }
+
         g_net_set = 1;
         g_net = net;
         return 0;
@@ -439,7 +432,7 @@ int
 jt_ptl_print_interfaces (int argc, char **argv)
 {
         struct libcfs_ioctl_data data;
-        char                     buffer[3][64];
+       char                     buffer[3][HOST_NAME_MAX + 1];
         int                      index;
         int                      rc;
 
@@ -456,9 +449,12 @@ jt_ptl_print_interfaces (int argc, char **argv)
                         break;
 
                 printf ("%s: (%s/%s) npeer %d nroute %d\n",
-                        ptl_ipaddr_2_str(data.ioc_u32[0], buffer[2], 1),
-                        ptl_ipaddr_2_str(data.ioc_u32[0], buffer[0], 0),
-                        ptl_ipaddr_2_str(data.ioc_u32[1], buffer[1], 0),
+                       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]);
         }
 
@@ -566,12 +562,12 @@ jt_ptl_print_peers (int argc, char **argv)
 {
         struct libcfs_ioctl_data data;
         lnet_process_id_t        id;
-        char                     buffer[2][64];
+       char                     buffer[2][HOST_NAME_MAX + 1];
         int                      index;
         int                      rc;
 
         if (!g_net_is_compatible (argv[0], SOCKLND, RALND, PTLLND, MXLND,
-                                  OPENIBLND, CIBLND, IIBLND, VIBLND, O2IBLND, 0))
+                                 O2IBLND, GNILND, 0))
                 return -1;
 
         for (index = 0;;index++) {
@@ -589,8 +585,12 @@ jt_ptl_print_peers (int argc, char **argv)
                         printf ("%-20s [%d]%s->%s:%d #%d\n",
                                 libcfs_id2str(id), 
                                 data.ioc_count, /* persistence */
-                                ptl_ipaddr_2_str (data.ioc_u32[2], buffer[0], 1), /* my ip */
-                                ptl_ipaddr_2_str (data.ioc_u32[0], buffer[1], 1), /* peer ip */
+                               /* 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, PTLLND, 0)) {
@@ -612,12 +612,34 @@ jt_ptl_print_peers (int argc, char **argv)
                                 data.ioc_u32[5] & 0xffff, /* nactiveq */
                                 data.ioc_u32[6] >> 16, /* credits */
                                 data.ioc_u32[6] & 0xffff); /* outstanding_credits */
-                } else if (g_net_is_compatible(NULL, RALND, OPENIBLND, CIBLND, VIBLND, 0)) {
+                } 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 */
-                                ptl_ipaddr_2_str (data.ioc_u32[0], buffer[1], 1), /* peer ip */
+                               /* 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);
@@ -645,24 +667,13 @@ jt_ptl_add_peer (int argc, char **argv)
         int                      port = 0;
         int                      rc;
 
-        if (!g_net_is_compatible (argv[0], SOCKLND, RALND,
-                                  OPENIBLND, CIBLND, IIBLND, VIBLND, 0))
+       if (!g_net_is_compatible (argv[0], SOCKLND, RALND,
+                                 GNILND, 0))
                 return -1;
 
-        if (g_net_is_compatible(NULL, SOCKLND, OPENIBLND, CIBLND, RALND, 0)) {
-                if (argc != 4) {
-                        fprintf (stderr, "usage(tcp,openib,cib,ra): %s nid ipaddr port\n",
-                                 argv[0]);
-                        return 0;
-                }
-        } else if (g_net_is_compatible(NULL, VIBLND, 0)) {
-                if (argc != 3) {
-                        fprintf (stderr, "usage(vib): %s nid ipaddr\n",
-                                 argv[0]);
-                        return 0;
-                }
-        } else if (argc != 2) {
-                fprintf (stderr, "usage(iib): %s nid\n", argv[0]);
+        if (argc != 4) {
+               fprintf (stderr, "usage(tcp,ra,gni): %s nid ipaddr port\n",
+                         argv[0]);
                 return 0;
         }
 
@@ -672,14 +683,12 @@ jt_ptl_add_peer (int argc, char **argv)
                 return -1;
         }
 
-        if (g_net_is_compatible (NULL, SOCKLND, OPENIBLND, CIBLND, VIBLND, RALND, 0) &&
-            lnet_parse_ipaddr (&ip, argv[2]) != 0) {
+        if (lnet_parse_ipaddr (&ip, argv[2]) != 0) {
                 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
                 return -1;
         }
 
-        if (g_net_is_compatible (NULL, SOCKLND, OPENIBLND, CIBLND, RALND, 0) &&
-            lnet_parse_port (&port, argv[3]) != 0) {
+        if (lnet_parse_port (&port, argv[3]) != 0) {
                 fprintf (stderr, "Can't parse port: %s\n", argv[3]);
                 return -1;
         }
@@ -711,7 +720,7 @@ jt_ptl_del_peer (int argc, char **argv)
         int                      rc;
 
         if (!g_net_is_compatible (argv[0], SOCKLND, RALND, MXLND, PTLLND,
-                                  OPENIBLND, CIBLND, IIBLND, VIBLND, O2IBLND, 0))
+                                 O2IBLND, GNILND, 0))
                 return -1;
 
         if (g_net_is_compatible(NULL, SOCKLND, 0)) {
@@ -776,12 +785,12 @@ jt_ptl_print_connections (int argc, char **argv)
 {
         struct libcfs_ioctl_data data;
         lnet_process_id_t        id;
-        char                     buffer[2][64];
+       char                     buffer[2][HOST_NAME_MAX + 1];
         int                      index;
         int                      rc;
 
-        if (!g_net_is_compatible (argv[0], SOCKLND, RALND, MXLND,
-                                  OPENIBLND, CIBLND, IIBLND, VIBLND, O2IBLND, 0))
+       if (!g_net_is_compatible (argv[0], SOCKLND, RALND, MXLND, O2IBLND,
+                                 GNILND, 0))
                 return -1;
 
         for (index = 0; ; index++) {
@@ -803,8 +812,12 @@ jt_ptl_print_connections (int argc, char **argv)
                                 (data.ioc_u32[3] == SOCKLND_CONN_BULK_IN) ? "I" :
                                 (data.ioc_u32[3] == SOCKLND_CONN_BULK_OUT) ? "O" : "?",
                                 data.ioc_u32[4], /* scheduler */
-                                ptl_ipaddr_2_str (data.ioc_u32[2], buffer[0], 1), /* local IP addr */
-                                ptl_ipaddr_2_str (data.ioc_u32[0], buffer[1], 1), /* remote IP addr */
+                               /* 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 */
@@ -813,6 +826,14 @@ jt_ptl_print_connections (int argc, char **argv)
                         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));
                 }
@@ -842,8 +863,8 @@ int jt_ptl_disconnect(int argc, char **argv)
                 return 0;
         }
 
-        if (!g_net_is_compatible (NULL, SOCKLND, RALND, MXLND,
-                                  OPENIBLND, CIBLND, IIBLND, VIBLND, O2IBLND, 0))
+       if (!g_net_is_compatible (NULL, SOCKLND, RALND, MXLND, O2IBLND,
+                                 GNILND, 0))
                 return 0;
 
         if (argc >= 2 &&
@@ -885,7 +906,7 @@ int jt_ptl_push_connection (int argc, char **argv)
                 return 0;
         }
 
-        if (!g_net_is_compatible (argv[0], SOCKLND, 0))
+       if (!g_net_is_compatible (argv[0], SOCKLND, GNILND, 0))
                 return -1;
 
         if (argc > 1 &&
@@ -969,12 +990,9 @@ int jt_ptl_ping(int argc, char **argv)
 
         sep = strchr(argv[1], '-');
         if (sep == NULL) {
-                id.pid = LNET_PID_ANY;
-                id.nid = libcfs_str2nid(argv[1]);
-                if (id.nid == LNET_NID_ANY) {
-                        fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
+                rc = lnet_parse_nid(argv[1], &id);
+                if (rc != 0)
                         return -1;
-                }
         } else {
                 char   *end;
 
@@ -984,12 +1002,19 @@ int jt_ptl_ping(int argc, char **argv)
                 else
                         id.pid = strtoul(argv[1], &end, 0);
 
-                id.nid = libcfs_str2nid(sep + 1);
+                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 (end != sep ||
-                    id.nid == LNET_NID_ANY) {
-                        fprintf(stderr, "Can't parse process id \"%s\"\n", argv[1]);
-                        return -1;
+                        if (id.nid == LNET_NID_ANY) {
+                                fprintf(stderr,
+                                        "Can't parse process id \"%s\"\n",
+                                        argv[1]);
+                                return -1;
+                        }
                 }
         }
 
@@ -1101,14 +1126,15 @@ jt_ptl_add_route (int argc, char **argv)
         struct libcfs_ioctl_data data;
         lnet_nid_t               gateway_nid;
         unsigned int             hops = 1;
+       unsigned int             priority = 0;
         char                    *end;
         int                      rc;
 
-        if (argc < 2 || argc > 3)
-        {
-                fprintf (stderr, "usage: %s gateway [hopcount]\n", argv[0]);
-                return (0);
-        }
+       if (argc < 2 || argc > 4) {
+               fprintf(stderr, "usage: %s gateway [hopcount [priority]]\n",
+                       argv[0]);
+               return -1;
+       }
 
         if (!g_net_is_set(argv[0]))
                 return (-1);
@@ -1119,18 +1145,29 @@ jt_ptl_add_route (int argc, char **argv)
                 return (-1);
         }
 
-        if (argc == 3) {
-                hops = strtoul(argv[2], &end, 0);
-                if (hops >= 256 || *end != 0) {
-                        fprintf (stderr, "Can't parse hopcount \"%s\"\n", argv[2]);
-                        return -1;
-                }
-        }
-
-        LIBCFS_IOC_INIT(data);
-        data.ioc_net = g_net;
-        data.ioc_count = hops;
-        data.ioc_nid = gateway_nid;
+       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(data);
+       data.ioc_net = g_net;
+       data.ioc_count = hops;
+       data.ioc_nid = gateway_nid;
+       data.ioc_priority = priority;
 
         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
         if (rc != 0) {
@@ -1242,6 +1279,7 @@ jt_ptl_print_routes (int argc, char **argv)
         lnet_nid_t                nid;
         unsigned int              hops;
         int                       alive;
+       unsigned int              pri;
 
         for (index = 0;;index++)
         {
@@ -1256,10 +1294,11 @@ jt_ptl_print_routes (int argc, char **argv)
                 hops    = data.ioc_count;
                 nid     = data.ioc_nid;
                 alive   = data.ioc_flags;
+               pri     = data.ioc_priority;
 
-                printf ("net %18s hops %u gw %32s %s\n", 
-                        libcfs_net2str(net), hops,
-                        libcfs_nid2str(nid), alive ? "up" : "down");
+               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)
@@ -1288,7 +1327,7 @@ lwt_control(int enable, int clear)
 }
 
 static int
-lwt_snapshot(cycles_t *now, int *ncpu, int *totalsize,
+lwt_snapshot(cfs_cycles_t *now, int *ncpu, int *totalsize,
              lwt_event_t *events, int size)
 {
         struct libcfs_ioctl_data data;
@@ -1385,7 +1424,8 @@ lwt_put_string(char *ustr)
 }
 
 static int
-lwt_print(FILE *f, cycles_t t0, cycles_t tlast, double mhz, int cpu, lwt_event_t *e)
+lwt_print(FILE *f, cfs_cycles_t t0, cfs_cycles_t tlast, double mhz, int cpu,
+          lwt_event_t *e)
 {
 #ifndef __WORDSIZE
 # error "__WORDSIZE not defined"
@@ -1433,25 +1473,26 @@ get_cycles_per_usec ()
         return (1000.0);
 }
 
+#define LWT_MAX_CPUS (32)
+
 int
 jt_ptl_lwt(int argc, char **argv)
 {
-        const int       lwt_max_cpus = 32;
         int             ncpus;
         int             totalspace;
         int             nevents_per_cpu;
         lwt_event_t    *events;
-        lwt_event_t    *cpu_event[lwt_max_cpus + 1];
-        lwt_event_t    *next_event[lwt_max_cpus];
-        lwt_event_t    *first_event[lwt_max_cpus];
+        lwt_event_t    *cpu_event[LWT_MAX_CPUS + 1];
+        lwt_event_t    *next_event[LWT_MAX_CPUS];
+        lwt_event_t    *first_event[LWT_MAX_CPUS];
         int             cpu;
         lwt_event_t    *e;
         int             rc;
         int             i;
         double          mhz;
-        cycles_t        t0;
-        cycles_t        tlast;
-        cycles_t        tnow;
+        cfs_cycles_t    t0;
+        cfs_cycles_t    tlast;
+        cfs_cycles_t    tnow;
         struct timeval  tvnow;
         int             printed_date = 0;
         int             nlines = 0;
@@ -1485,9 +1526,9 @@ jt_ptl_lwt(int argc, char **argv)
         if (lwt_snapshot(NULL, &ncpus, &totalspace, NULL, 0) != 0)
                 return (-1);
 
-        if (ncpus > lwt_max_cpus) {
+        if (ncpus > LWT_MAX_CPUS) {
                 fprintf(stderr, "Too many cpus: %d (%d)\n",
-                        ncpus, lwt_max_cpus);
+                        ncpus, LWT_MAX_CPUS);
                 return (-1);
         }
 
@@ -1591,7 +1632,7 @@ jt_ptl_lwt(int argc, char **argv)
                 if (t0 <= next_event[cpu]->lwte_when) {
                         /* on or after the first event */
                         if (!printed_date) {
-                                cycles_t du = (tnow - t0) / mhz;
+                                cfs_cycles_t du = (tnow - t0) / mhz;
                                 time_t   then = tvnow.tv_sec - du/1000000;
 
                                 if (du % 1000000 > tvnow.tv_usec)