1219820Sjeff/* 2219820Sjeff * Copyright (c) 2004-2008 Voltaire Inc. All rights reserved. 3219820Sjeff * 4219820Sjeff * This software is available to you under a choice of one of two 5219820Sjeff * licenses. You may choose to be licensed under the terms of the GNU 6219820Sjeff * General Public License (GPL) Version 2, available from the file 7219820Sjeff * COPYING in the main directory of this source tree, or the 8219820Sjeff * OpenIB.org BSD license below: 9219820Sjeff * 10219820Sjeff * Redistribution and use in source and binary forms, with or 11219820Sjeff * without modification, are permitted provided that the following 12219820Sjeff * conditions are met: 13219820Sjeff * 14219820Sjeff * - Redistributions of source code must retain the above 15219820Sjeff * copyright notice, this list of conditions and the following 16219820Sjeff * disclaimer. 17219820Sjeff * 18219820Sjeff * - Redistributions in binary form must reproduce the above 19219820Sjeff * copyright notice, this list of conditions and the following 20219820Sjeff * disclaimer in the documentation and/or other materials 21219820Sjeff * provided with the distribution. 22219820Sjeff * 23219820Sjeff * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 24219820Sjeff * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 25219820Sjeff * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 26219820Sjeff * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 27219820Sjeff * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 28219820Sjeff * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 29219820Sjeff * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 30219820Sjeff * SOFTWARE. 31219820Sjeff * 32219820Sjeff */ 33219820Sjeff 34219820Sjeff#if HAVE_CONFIG_H 35219820Sjeff# include <config.h> 36219820Sjeff#endif /* HAVE_CONFIG_H */ 37219820Sjeff 38219820Sjeff#include <stdio.h> 39219820Sjeff#include <stdlib.h> 40219820Sjeff#include <unistd.h> 41219820Sjeff#include <stdarg.h> 42219820Sjeff#include <time.h> 43219820Sjeff#include <string.h> 44219820Sjeff#include <getopt.h> 45219820Sjeff 46219820Sjeff#include <infiniband/common.h> 47219820Sjeff#include <infiniband/umad.h> 48219820Sjeff#include <infiniband/mad.h> 49219820Sjeff 50219820Sjeff#include "ibdiag_common.h" 51219820Sjeff 52219820Sjeff#undef DEBUG 53219820Sjeff#define DEBUG if (verbose) IBWARN 54219820Sjeff 55219820Sjeffstatic int dest_type = IB_DEST_LID; 56219820Sjeffstatic int verbose; 57219820Sjeff 58219820Sjeff#define MAX_CPUS 8 59219820Sjeff 60219820Sjeffenum ib_sysstat_attr_t { 61219820Sjeff IB_PING_ATTR = 0x10, 62219820Sjeff IB_HOSTINFO_ATTR = 0x11, 63219820Sjeff IB_CPUINFO_ATTR = 0x12, 64219820Sjeff}; 65219820Sjeff 66219820Sjefftypedef struct cpu_info { 67219820Sjeff char *model; 68219820Sjeff char *mhz; 69219820Sjeff} cpu_info; 70219820Sjeff 71219820Sjeffstatic cpu_info cpus[MAX_CPUS]; 72219820Sjeffstatic int host_ncpu; 73219820Sjeff 74219820Sjeffchar *argv0 = "ibsysstat"; 75219820Sjeff 76219820Sjeffstatic void 77219820Sjeffmk_reply(int attr, void *data, int sz) 78219820Sjeff{ 79219820Sjeff char *s = data; 80219820Sjeff int n, i; 81219820Sjeff 82219820Sjeff switch (attr) { 83219820Sjeff case IB_PING_ATTR: 84219820Sjeff break; /* nothing to do here, just reply */ 85219820Sjeff case IB_HOSTINFO_ATTR: 86219820Sjeff if (gethostname(s, sz) < 0) 87219820Sjeff snprintf(s, sz, "?hostname?"); 88219820Sjeff s[sz-1] = 0; 89219820Sjeff if ((n = strlen(s)) >= sz) 90219820Sjeff break; 91219820Sjeff s[n] = '.'; 92219820Sjeff s += n+1; 93219820Sjeff sz -= n+1; 94219820Sjeff if (getdomainname(s, sz) < 0) 95219820Sjeff snprintf(s, sz, "?domainname?"); 96219820Sjeff if (strlen(s) == 0) 97219820Sjeff s[-1] = 0; /* no domain */ 98219820Sjeff break; 99219820Sjeff case IB_CPUINFO_ATTR: 100219820Sjeff for (i = 0; i < host_ncpu && sz > 0; i++) { 101219820Sjeff n = snprintf(s, sz, "cpu %d: model %s MHZ %s\n", 102219820Sjeff i, cpus[i].model, cpus[i].mhz); 103219820Sjeff if (n >= sz) { 104219820Sjeff IBWARN("cpuinfo truncated"); 105219820Sjeff break; 106219820Sjeff } 107219820Sjeff sz -= n; 108219820Sjeff s += n; 109219820Sjeff } 110219820Sjeff break; 111219820Sjeff default: 112219820Sjeff DEBUG("unknown attr %d", attr); 113219820Sjeff } 114219820Sjeff} 115219820Sjeff 116219820Sjeffstatic char * 117219820Sjeffibsystat_serv(void) 118219820Sjeff{ 119219820Sjeff void *umad; 120219820Sjeff void *mad; 121219820Sjeff int attr, mod; 122219820Sjeff 123219820Sjeff DEBUG("starting to serve..."); 124219820Sjeff 125219820Sjeff while ((umad = mad_receive(0, -1))) { 126219820Sjeff 127219820Sjeff mad = umad_get_mad(umad); 128219820Sjeff 129219820Sjeff attr = mad_get_field(mad, 0, IB_MAD_ATTRID_F); 130219820Sjeff mod = mad_get_field(mad, 0, IB_MAD_ATTRMOD_F); 131219820Sjeff 132219820Sjeff DEBUG("got packet: attr 0x%x mod 0x%x", attr, mod); 133219820Sjeff 134219820Sjeff mk_reply(attr, (char *)mad + IB_VENDOR_RANGE2_DATA_OFFS, IB_VENDOR_RANGE2_DATA_SIZE); 135219820Sjeff 136219820Sjeff if (mad_respond(umad, 0, 0) < 0) 137219820Sjeff DEBUG("respond failed"); 138219820Sjeff 139219820Sjeff mad_free(umad); 140219820Sjeff } 141219820Sjeff 142219820Sjeff DEBUG("server out"); 143219820Sjeff return 0; 144219820Sjeff} 145219820Sjeff 146219820Sjeffstatic int 147219820Sjeffmatch_attr(char *str) 148219820Sjeff{ 149219820Sjeff if (!strcmp(str, "ping")) 150219820Sjeff return IB_PING_ATTR; 151219820Sjeff if (!strcmp(str, "host")) 152219820Sjeff return IB_HOSTINFO_ATTR; 153219820Sjeff if (!strcmp(str, "cpu")) 154219820Sjeff return IB_CPUINFO_ATTR; 155219820Sjeff return -1; 156219820Sjeff} 157219820Sjeff 158219820Sjeffstatic char * 159219820Sjeffibsystat(ib_portid_t *portid, int attr) 160219820Sjeff{ 161219820Sjeff char data[IB_VENDOR_RANGE2_DATA_SIZE] = {0}; 162219820Sjeff ib_vendor_call_t call; 163219820Sjeff 164219820Sjeff DEBUG("Sysstat ping.."); 165219820Sjeff 166219820Sjeff call.method = IB_MAD_METHOD_GET; 167219820Sjeff call.mgmt_class = IB_VENDOR_OPENIB_SYSSTAT_CLASS; 168219820Sjeff call.attrid = attr; 169219820Sjeff call.mod = 0; 170219820Sjeff call.oui = IB_OPENIB_OUI; 171219820Sjeff call.timeout = 0; 172219820Sjeff memset(&call.rmpp, 0, sizeof call.rmpp); 173219820Sjeff 174219820Sjeff if (!ib_vendor_call(data, portid, &call)) 175219820Sjeff return "vendor call failed"; 176219820Sjeff 177219820Sjeff DEBUG("Got sysstat pong.."); 178219820Sjeff if (attr != IB_PING_ATTR) 179219820Sjeff puts(data); 180219820Sjeff else 181219820Sjeff printf("sysstat ping succeeded\n"); 182219820Sjeff return 0; 183219820Sjeff} 184219820Sjeff 185219820Sjeffint 186219820Sjeffbuild_cpuinfo(void) 187219820Sjeff{ 188219820Sjeff char line[1024] = {0}, *s, *e; 189219820Sjeff FILE *f; 190219820Sjeff int ncpu = 0; 191219820Sjeff 192219820Sjeff if (!(f = fopen("/proc/cpuinfo", "r"))) { 193219820Sjeff IBWARN("couldn't open /proc/cpuinfo"); 194219820Sjeff return 0; 195219820Sjeff } 196219820Sjeff 197219820Sjeff while (fgets(line, sizeof(line) - 1, f)) { 198219820Sjeff if (!strncmp(line, "processor\t", 10)) { 199219820Sjeff ncpu++; 200219820Sjeff if (ncpu > MAX_CPUS) 201219820Sjeff return MAX_CPUS; 202219820Sjeff continue; 203219820Sjeff } 204219820Sjeff 205219820Sjeff if (!ncpu || !(s = strchr(line, ':'))) 206219820Sjeff continue; 207219820Sjeff 208219820Sjeff if ((e = strchr(s, '\n'))) 209219820Sjeff *e = 0; 210219820Sjeff if (!strncmp(line, "model name\t", 11)) 211219820Sjeff cpus[ncpu-1].model = strdup(s+1); 212219820Sjeff else if (!strncmp(line, "cpu MHz\t", 8)) 213219820Sjeff cpus[ncpu-1].mhz = strdup(s+1); 214219820Sjeff } 215219820Sjeff 216219820Sjeff fclose(f); 217219820Sjeff 218219820Sjeff DEBUG("ncpu %d", ncpu); 219219820Sjeff 220219820Sjeff return ncpu; 221219820Sjeff} 222219820Sjeff 223219820Sjeffstatic void 224219820Sjeffusage(void) 225219820Sjeff{ 226219820Sjeff char *basename; 227219820Sjeff 228219820Sjeff if (!(basename = strrchr(argv0, '/'))) 229219820Sjeff basename = argv0; 230219820Sjeff else 231219820Sjeff basename++; 232219820Sjeff 233219820Sjeff fprintf(stderr, "Usage: %s [-d(ebug) -e(rr_show) -v(erbose) -G(uid) -s smlid -V(ersion) -C ca_name -P ca_port " 234219820Sjeff "-t(imeout) timeout_ms -o oui -S(erver)] <dest lid|guid> [<op>]\n", 235219820Sjeff basename); 236219820Sjeff exit(-1); 237219820Sjeff} 238219820Sjeff 239219820Sjeffint 240219820Sjeffmain(int argc, char **argv) 241219820Sjeff{ 242219820Sjeff int mgmt_classes[3] = {IB_SMI_CLASS, IB_SMI_DIRECT_CLASS, IB_SA_CLASS}; 243219820Sjeff int sysstat_class = IB_VENDOR_OPENIB_SYSSTAT_CLASS; 244219820Sjeff ib_portid_t portid = {0}; 245219820Sjeff ib_portid_t *sm_id = 0, sm_portid = {0}; 246219820Sjeff int timeout = 0, udebug = 0, server = 0; 247219820Sjeff int oui = IB_OPENIB_OUI, attr = IB_PING_ATTR; 248219820Sjeff extern int ibdebug; 249219820Sjeff char *err; 250219820Sjeff char *ca = 0; 251219820Sjeff int ca_port = 0; 252219820Sjeff 253219820Sjeff static char const str_opts[] = "C:P:t:s:o:devGSVhu"; 254219820Sjeff static const struct option long_opts[] = { 255219820Sjeff { "C", 1, 0, 'C'}, 256219820Sjeff { "P", 1, 0, 'P'}, 257219820Sjeff { "debug", 0, 0, 'd'}, 258219820Sjeff { "err_show", 0, 0, 'e'}, 259219820Sjeff { "verbose", 0, 0, 'v'}, 260219820Sjeff { "Guid", 0, 0, 'G'}, 261219820Sjeff { "timeout", 1, 0, 't'}, 262219820Sjeff { "s", 1, 0, 's'}, 263219820Sjeff { "o", 1, 0, 'o'}, 264219820Sjeff { "Server", 0, 0, 'S'}, 265219820Sjeff { "Version", 0, 0, 'V'}, 266219820Sjeff { "help", 0, 0, 'h'}, 267219820Sjeff { "usage", 0, 0, 'u'}, 268219820Sjeff { } 269219820Sjeff }; 270219820Sjeff 271219820Sjeff argv0 = argv[0]; 272219820Sjeff 273219820Sjeff while (1) { 274219820Sjeff int ch = getopt_long(argc, argv, str_opts, long_opts, NULL); 275219820Sjeff if ( ch == -1 ) 276219820Sjeff break; 277219820Sjeff switch(ch) { 278219820Sjeff case 'C': 279219820Sjeff ca = optarg; 280219820Sjeff break; 281219820Sjeff case 'P': 282219820Sjeff ca_port = strtoul(optarg, 0, 0); 283219820Sjeff break; 284219820Sjeff case 'd': 285219820Sjeff ibdebug++; 286219820Sjeff madrpc_show_errors(1); 287219820Sjeff umad_debug(udebug); 288219820Sjeff udebug++; 289219820Sjeff break; 290219820Sjeff case 'e': 291219820Sjeff madrpc_show_errors(1); 292219820Sjeff break; 293219820Sjeff case 'G': 294219820Sjeff dest_type = IB_DEST_GUID; 295219820Sjeff break; 296219820Sjeff case 'o': 297219820Sjeff oui = strtoul(optarg, 0, 0); 298219820Sjeff break; 299219820Sjeff case 's': 300219820Sjeff if (ib_resolve_portid_str(&sm_portid, optarg, IB_DEST_LID, 0) < 0) 301219820Sjeff IBERROR("can't resolve SM destination port %s", optarg); 302219820Sjeff sm_id = &sm_portid; 303219820Sjeff break; 304219820Sjeff case 'S': 305219820Sjeff server++; 306219820Sjeff break; 307219820Sjeff case 't': 308219820Sjeff timeout = strtoul(optarg, 0, 0); 309219820Sjeff madrpc_set_timeout(timeout); 310219820Sjeff break; 311219820Sjeff case 'v': 312219820Sjeff verbose++; 313219820Sjeff break; 314219820Sjeff case 'V': 315219820Sjeff fprintf(stderr, "%s %s\n", argv0, get_build_version() ); 316219820Sjeff exit(-1); 317219820Sjeff default: 318219820Sjeff usage(); 319219820Sjeff break; 320219820Sjeff } 321219820Sjeff } 322219820Sjeff argc -= optind; 323219820Sjeff argv += optind; 324219820Sjeff 325219820Sjeff if (!argc && !server) 326219820Sjeff usage(); 327219820Sjeff 328219820Sjeff if (argc > 1 && (attr = match_attr(argv[1])) < 0) 329219820Sjeff usage(); 330219820Sjeff 331219820Sjeff madrpc_init(ca, ca_port, mgmt_classes, 3); 332219820Sjeff 333219820Sjeff if (server) { 334219820Sjeff if (mad_register_server(sysstat_class, 0, 0, oui) < 0) 335219820Sjeff IBERROR("can't serve class %d", sysstat_class); 336219820Sjeff 337219820Sjeff host_ncpu = build_cpuinfo(); 338219820Sjeff 339219820Sjeff if ((err = ibsystat_serv())) 340219820Sjeff IBERROR("ibssystat to %s: %s", portid2str(&portid), err); 341219820Sjeff exit(0); 342219820Sjeff } 343219820Sjeff 344219820Sjeff if (mad_register_client(sysstat_class, 0) < 0) 345219820Sjeff IBERROR("can't register to sysstat class %d", sysstat_class); 346219820Sjeff 347219820Sjeff if (ib_resolve_portid_str(&portid, argv[0], dest_type, sm_id) < 0) 348219820Sjeff IBERROR("can't resolve destination port %s", argv[0]); 349219820Sjeff 350219820Sjeff if ((err = ibsystat(&portid, attr))) 351219820Sjeff IBERROR("ibsystat to %s: %s", portid2str(&portid), err); 352219820Sjeff 353219820Sjeff exit(0); 354219820Sjeff} 355