portals.c

来自「lustre 1.6.5 source code」· C语言 代码 · 共 1,711 行 · 第 1/4 页

C
1,711
字号
/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*- * vim:expandtab:shiftwidth=8:tabstop=8: * * Copyright (C) 2001, 2002 Cluster File Systems, Inc. * *   This file is part of Portals, http://www.sf.net/projects/lustre/ * *   Portals is free software; you can redistribute it and/or *   modify it under the terms of version 2 of the GNU General Public *   License as published by the Free Software Foundation. * *   Portals is distributed in the hope that it will be useful, *   but WITHOUT ANY WARRANTY; without even the implied warranty of *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the *   GNU General Public License for more details. * *   You should have received a copy of the GNU General Public License *   along with Portals; if not, write to the Free Software *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * */#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 <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;static int   g_net_set;static __u32 g_net;/* Convert a string boolean to an int; "enable" -> 1 */intlnet_parse_bool (int *b, char *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 (-1);}intlnet_parse_port (int *port, char *str){        char      *end;        *port = strtol (str, &end, 0);        if (*end == 0 &&                        /* parsed whole string */            *port > 0 && *port < 65536)         /* minimal sanity check */                return (0);        return (-1);}#ifdef HAVE_GETHOSTBYNAMEstatic struct hostent *ptl_gethostbyname(char * hname) {        struct hostent *he;        he = gethostbyname(hname);        if (!he) {                switch(h_errno) {                case HOST_NOT_FOUND:                case NO_ADDRESS:                        fprintf(stderr, "Unable to resolve hostname: %s\n",                                hname);                        break;                default:                        fprintf(stderr, "gethostbyname error for %s: %s\n",                                hname, strerror(h_errno));                        break;                }                return NULL;        }        return he;}#endifintlnet_parse_ipquad (__u32 *ipaddrp, char *str){        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)        {                *ipaddrp = (a<<24)|(b<<16)|(c<<8)|d;                return (0);        }        return (-1);}intlnet_parse_ipaddr (__u32 *ipaddrp, char *str){#ifdef HAVE_GETHOSTBYNAME        struct hostent *he;#endif        if (!strcmp (str, "_all_")) {                *ipaddrp = 0;                return (0);        }        if (lnet_parse_ipquad(ipaddrp, str) == 0)                return (0);#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_ipaddr_2_str (__u32 ipaddr, char *str, int lookup){#ifdef HAVE_GETHOSTBYNAME        __u32           net_ip;        struct hostent *he;        if (lookup) {                net_ip = htonl (ipaddr);                he = gethostbyaddr (&net_ip, sizeof (net_ip), AF_INET);                if (he != NULL) {                        strcpy(str, he->h_name);                        return (str);                }        }#endif        sprintf (str, "%d.%d.%d.%d",                 (ipaddr >> 24) & 0xff, (ipaddr >> 16) & 0xff,                 (ipaddr >> 8) & 0xff, ipaddr & 0xff);        return (str);}intlnet_parse_time (time_t *t, char *str){        char          *end;        int            n;        struct tm      tm;        *t = strtol (str, &end, 0);        if (*end == 0) /* parsed whole string */                return (0);        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);        return (0);}int g_net_is_set (char *cmd){        if (g_net_set)                return 1;        if (cmd != NULL)                fprintf(stderr,                        "You must run the 'network' command before '%s'.\n",                        cmd);        return 0;}int g_net_is_compatible (char *cmd, ...){        va_list       ap;        int           nal;        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 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;        if (argc < 2) {                fprintf(stderr, "usage: %s <net>|up|down\n", argv[0]);                return 0;        }        if (!strcmp(argv[1], "unconfigure") ||            !strcmp(argv[1], "down")) {                LIBCFS_IOC_INIT(data);                rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_UNCONFIGURE, &data);                if (rc == 0) {                        printf ("LNET ready to unload\n");                        return 0;                }                if (errno == EBUSY)                        fprintf(stderr, "LNET busy\n");                else                        fprintf(stderr, "LNET unconfigure error %d: %s\n",                                errno, strerror(errno));                return -1;        }        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;        }        net = libcfs_str2net(argv[1]);        if (net == LNET_NIDNET(LNET_NID_ANY)) {                fprintf(stderr, "Can't parse net %s\n", argv[1]);                return -1;        }        g_net_set = 1;        g_net = net;        return 0;}intjt_ptl_list_nids(int argc, char **argv){        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;        }        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 (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--;                        }                }        }        return 0;}intjt_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;        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;                }                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;                }                dist = data.ioc_u32[0];                order = data.ioc_u32[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;                }        }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?