/* Returns 0 (invalid base version) on error */ static uint8_t get_base_version_from_ni(int fd, uint32_t aid, int pkey_index) { uint8_t rc; void *umad_p = NULL; struct umad_smp *send_mad; size_t length; umad_p = umad_alloc(1, sizeof(*send_mad) + umad_size()); if (!umad_p) { OUTPUT_ERROR ("can't alloc umad for OPA check; send_size %ld\n", sizeof(*send_mad)); return 0; } memset(umad_p, 0, sizeof(*send_mad) + umad_size()); umad_set_grh(umad_p, 0); send_mad = umad_get_mad(umad_p); send_mad->base_version = UMAD_BASE_VERSION; send_mad->mgmt_class = UMAD_CLASS_SUBN_DIRECTED_ROUTE; send_mad->class_version = 0x01; send_mad->method = UMAD_METHOD_GET; send_mad->tid = htonl(0xDEADBEEF); send_mad->attr_id = htons(UMAD_SM_ATTR_NODE_INFO); send_mad->dr_slid = 0xffff; send_mad->dr_dlid = 0xffff; umad_set_pkey(umad_p, pkey_index); umad_set_addr(umad_p, 0xffff, 0, 0, 0); rc = 0; if (umad_send(fd, aid, umad_p, sizeof(*send_mad), 100, 1) < 0) goto free_mad; length = sizeof(*send_mad); if (umad_recv(fd, umad_p, (int *)&length, 100) < 0) goto free_mad; if (length < sizeof(*send_mad)) goto free_mad; if (umad_status(umad_p) != 0) goto free_mad; rc = ((NODE_INFO *)(send_mad->data))->BaseVersion; free_mad: free(umad_p); return rc; }
/****************************************************************************** * join_multicast_group ******************************************************************************/ int join_multicast_group(subn_adm_method method,struct mcast_parameters *params) { int portid = -1; int agentid = -1; void *umad_buff = NULL; void *mad = NULL; int length = MAD_SIZE; int test_result = 0; // mlid will be assigned to the new LID after the join if (umad_init() < 0) { fprintf(stderr, "failed to init the UMAD library\n"); goto cleanup; } /* use casting to loose the "const char0 *" */ portid = umad_open_port((char*)params->ib_devname,params->ib_port); if (portid < 0) { fprintf(stderr,"failed to open UMAD port %d\n",params->ib_port); goto cleanup; } agentid = umad_register(portid,MANAGMENT_CLASS_SUBN_ADM, 2, 0, 0); if (agentid < 0) { fprintf(stderr,"failed to register UMAD agent for MADs\n"); goto cleanup; } umad_buff = umad_alloc(1, umad_size() + MAD_SIZE); if (!umad_buff) { fprintf(stderr, "failed to allocate MAD buffer\n"); goto cleanup; } mad = umad_get_mad(umad_buff); prepare_mcast_mad(method,params,(struct sa_mad_packet_t *)mad); if (umad_set_addr(umad_buff,params->sm_lid,1,params->sm_sl,QP1_WELL_KNOWN_Q_KEY) < 0) { fprintf(stderr, "failed to set the destination address of the SMP\n"); goto cleanup; } if (umad_send(portid,agentid,umad_buff,MAD_SIZE,100,5) < 0) { fprintf(stderr, "failed to send MAD\n"); goto cleanup; } if (umad_recv(portid,umad_buff,&length,5000) < 0) { fprintf(stderr, "failed to receive MAD response\n"); goto cleanup; } if (check_mad_status((struct sa_mad_packet_t*)mad)) { fprintf(stderr, "failed to get mlid from MAD\n"); goto cleanup; } // "Join multicast group" message was sent if (method == SUBN_ADM_METHOD_SET) { get_mlid_from_mad((struct sa_mad_packet_t*)mad,¶ms->mlid); params->mcast_state |= MCAST_IS_JOINED; // "Leave multicast group" message was sent } else { params->mcast_state &= ~MCAST_IS_JOINED; } cleanup: if (umad_buff) umad_free(umad_buff); if (portid >= 0) { if (agentid >= 0) { if (umad_unregister(portid, agentid)) { fprintf(stderr, "failed to deregister UMAD agent for MADs\n"); test_result = 1; } } if (umad_close_port(portid)) { fprintf(stderr, "failed to close UMAD portid\n"); test_result = 1; } } return test_result; }
/** ========================================================================= */ FSTATUS oib_recv_mad_alloc(struct oib_port *port, uint8_t **recv_mad, size_t *recv_size, int timeout_ms, struct oib_mad_addr *addr) { #define STL_MAD_SIZE 2048 // Note, hack should reference size value OFED standard header file FSTATUS status = FSUCCESS; ib_user_mad_t *umad = NULL; int mad_agent; uint32_t my_umad_status = 0; size_t length; if (!port || !recv_mad || !recv_size) return FINVALID_PARAMETER; length = STL_MAD_SIZE; umad = umad_alloc(1, umad_size() + length); if (!umad) { OUTPUT_ERROR("can't alloc MAD sized umad\n"); status = FINSUFFICIENT_MEMORY; goto done; } retry: mad_agent = umad_recv(port->umad_fd, umad, (int *)&length, timeout_ms); // There are 4 combinations: // assorted errors: mad_agent < 0, length <= MAD_SIZE // large RMPP response: mad_agent < 0, length > MAD_SIZE, umad_status==0 // got response: mad_agent >= 0, length <= MAD_SIZE, umad_status==0 // no response: mad_agent >= 0, length <= MAD_SIZE, umad_status == error if (mad_agent < 0) { if (length <= STL_MAD_SIZE) { // no MAD returned. None available. DBGPRINT ("recv error on MAD sized umad (%s) length=%ld\n", strerror(errno), length); if (errno == EINTR) goto retry; status = (errno == ETIMEDOUT)?FNOT_DONE:FERROR; goto done; } else { /* Need a larger buffer for RMPP */ DBGPRINT ("Received 1st MAD length=%ld\n",length); umad_free(umad); umad = umad_alloc(1, umad_size() + length); if (!umad) { OUTPUT_ERROR ("can't alloc umad length %ld\n", length); status = FINSUFFICIENT_MEMORY; goto done; } // just to be safe, we supply a timeout. However it // should be unnecessary since we know we have a packet retry2: if ((mad_agent = umad_recv(port->umad_fd, umad, (int *)&length, OIB_UTILS_DEF_TIMEOUT_MS)) < 0) { OUTPUT_ERROR ("recv error on umad length %ld (%s)\n", length, strerror(errno)); if (errno == EINTR) goto retry2; status = FOVERRUN; *recv_size = length; goto done; } } } if (mad_agent >= UMAD_CA_MAX_AGENTS) { OUTPUT_ERROR ("invalid mad agent %d - dropping\n", mad_agent); status = FERROR; goto done; } my_umad_status = umad_status(umad); DBGPRINT("UMAD Status: %s (%d)\n", strerror(my_umad_status), my_umad_status); if (my_umad_status != 0) { status = (my_umad_status == ETIMEDOUT) ? FTIMEOUT : FREJECT; } DBGPRINT("Received MAD length=%ld, total umad size=%ld\n",length, length + umad_size()); if (dbg_file) { struct umad_hdr * umad_hdr = (struct umad_hdr *)umad_get_mad(umad); DBGPRINT(" Base_Version 0x%x Class 0x%x Method 0x%x attrId 0x%x attr_mod 0x%x status 0x%x\n", umad_hdr->base_version, umad_hdr->mgmt_class, umad_hdr->method, umad_hdr->attr_id, umad_hdr->attr_mod, umad_hdr->status); umad_dump(umad); oib_dump_mad(dbg_file, umad_get_mad(umad), length, "rcv mad\n"); } // Allocate and copy to new buffer. *recv_mad = calloc (1, length); if (*recv_mad == NULL) { OUTPUT_ERROR ("can't alloc return buffer length %ld\n", length); status = FINSUFFICIENT_MEMORY; goto done; } memcpy (*recv_mad, umad_get_mad(umad), length); *recv_size = length; if (addr != NULL) { addr->lid = IB2STL_LID(ntoh16(umad->addr.lid)); addr->sl = umad->addr.sl; addr->qkey = ntoh32(umad->addr.qkey); addr->qpn = ntoh32(umad->addr.qpn); addr->pkey = oib_find_pkey_from_idx(port, umad_get_pkey(umad)); } done: if (umad != NULL) { umad_free(umad); } return status; }
/** ========================================================================= */ FSTATUS oib_recv_mad_no_alloc(struct oib_port *port, uint8_t *recv_mad, size_t *recv_size, int timeout_ms, struct oib_mad_addr *addr) { size_t length = *recv_size; ib_user_mad_t *umad = NULL; int mad_agent; uint32_t my_umad_status = 0; FSTATUS status = FSUCCESS; if (!port || !recv_mad || !*recv_size) return FINVALID_PARAMETER; umad = umad_alloc(1, length + umad_size()); if (!umad) { OUTPUT_ERROR ("can't alloc umad length %ld\n", length); status = FINSUFFICIENT_MEMORY; goto done; } retry: mad_agent = umad_recv(port->umad_fd, umad, (int *)&length, timeout_ms); // There are 4 combinations: // assorted errors: mad_agent < 0, length <= MAD_SIZE // large RMPP response: mad_agent < 0, length > MAD_SIZE, umad_status==0 // got response: mad_agent >= 0, length <= MAD_SIZE, umad_status==0 // no response: mad_agent >= 0, length <= MAD_SIZE, umad_status == error if (mad_agent < 0) { if (length <= *recv_size) { // no MAD returned. None available. DBGPRINT ("recv error on umad (size %zu) (%s)\n", *recv_size, strerror(errno)); if (errno == EINTR) goto retry; status = (errno == ETIMEDOUT) ? FNOT_DONE:FERROR; goto done; } else { // this routine is not expecting large responses OUTPUT_ERROR ("Rx Packet size %zu larger than mad-size %zu\n", length, *recv_size); status = FOVERRUN; if (recv_mad) memcpy(recv_mad, umad_get_mad(umad), *recv_size); // Clean out Rx packet 'cause it will never go away.. umad_free(umad); umad = umad_alloc(1, umad_size() + length); if (!umad) { OUTPUT_ERROR ("can't alloc umad for rx cleanup, length %ld\n", length); status = FINSUFFICIENT_MEMORY; goto done; } // just to be safe, we supply a timeout. However it // should be unnecessary since we know we have a packet retry2: if (umad_recv(port->umad_fd, umad, (int *)&length, OIB_UTILS_DEF_TIMEOUT_MS) < 0) { OUTPUT_ERROR ("recv error on cleanup, length %ld (%s)\n", length, strerror(errno)); if (errno == EINTR) goto retry2; goto done; } if (dbg_file) { umad_dump(umad); oib_dump_mad(dbg_file, umad_get_mad(umad), length, "rcv mad discarded\n"); } goto done; } } if (mad_agent >= UMAD_CA_MAX_AGENTS) { OUTPUT_ERROR ("invalid mad agent %d\n", mad_agent); status = FERROR; goto done; } my_umad_status = umad_status(umad); DBGPRINT("UMAD Status: %s (%d)\n", strerror(my_umad_status), my_umad_status); if (my_umad_status != 0) { status = (my_umad_status == ETIMEDOUT) ? FTIMEOUT : FREJECT; } DBGPRINT("Received MAD: Agent %d, length=%ld\n", mad_agent, length); if (dbg_file) { umad_dump(umad); oib_dump_mad(dbg_file, umad_get_mad(umad), length, "rcv mad\n"); } // Copy the data if (recv_mad && length > 0) { *recv_size = length; memcpy(recv_mad, umad_get_mad(umad), length); } if (addr != NULL) { addr->lid = IB2STL_LID(ntoh16(umad->addr.lid)); addr->sl = umad->addr.sl; addr->qkey = ntoh32(umad->addr.qkey); addr->qpn = ntoh32(umad->addr.qpn); addr->pkey = oib_find_pkey_from_idx(port, umad_get_pkey(umad)); } done: if (umad != NULL) { umad_free(umad); } return status; }
/** ========================================================================= */ FSTATUS oib_send_mad2(struct oib_port *port, uint8_t *send_mad, size_t send_size, struct oib_mad_addr *addr, int timeout_ms, int retries) { FSTATUS status = FSUCCESS; void *umad_p = NULL; int response; uint8_t mclass, class_ver; int aid; int correctedTimeout; struct umad_hdr *mad_hdr = (struct umad_hdr *)send_mad; uint16_t ib_lid; int pkey_idx; size_t padded_size; if (!port || !send_mad || !send_size || !addr) return FINVALID_PARAMETER; ib_lid = addr->lid & 0xffff; // Make sure we are registered for this class/version... mclass = mad_hdr->mgmt_class; class_ver = mad_hdr->class_version; response = (mad_hdr->method & 0x80) || (mad_hdr->method == UMAD_METHOD_TRAP_REPRESS) || (mclass == UMAD_CLASS_BM && ntohl(mad_hdr->attr_mod) & BM_ATTRIB_MOD_RESPONSE); aid = port->umad_agents[class_ver][mclass]; DBGPRINT (" Management Class 0x%x method 0x%x attrId 0x%x attrM 0x%x\n",mclass, mad_hdr->method, ntohs(mad_hdr->attr_id), ntohl(mad_hdr->attr_mod)); DBGPRINT (" base_version 0x%x class_version 0x%x\n",mad_hdr->base_version, mad_hdr->class_version); if (aid == OIB_INVALID_AGENTID) { // automatically register for "send" only int err = 0; struct oib_class_args mgmt_class[2]; memset(mgmt_class, 0, sizeof(mgmt_class)); mgmt_class[0].base_version = mad_hdr->base_version; mgmt_class[0].mgmt_class = mad_hdr->mgmt_class; mgmt_class[0].class_version = mad_hdr->class_version; mgmt_class[0].is_responding_client = 0; mgmt_class[0].is_trap_client = 0; mgmt_class[0].is_report_client = 0; mgmt_class[0].kernel_rmpp = 1; mgmt_class[0].use_methods = 0; DBGPRINT ("auto registering class 0x%02x; version 0x%x for send only\n", mclass, class_ver); if ((err = oib_bind_classes(port, mgmt_class)) != 0) { OUTPUT_ERROR ("Failed to auto register for class 0x%02x: %s\n", mclass, strerror(err)); status = FERROR; goto done; } aid = port->umad_agents[class_ver][mclass]; } // Initialize the user mad. // umad has limititation that outgoing packets must be > 36 bytes. padded_size = ( MAX(send_size,36) + 7) & ~0x7; DBGPRINT ("dlid %d qpn %d qkey %x sl %d\n", ib_lid, addr->qpn, addr->qkey, addr->sl); umad_p = umad_alloc(1, padded_size + umad_size()); if (!umad_p) { OUTPUT_ERROR ("can't alloc umad send_size %ld\n", padded_size); status = FINSUFFICIENT_MEMORY; goto done; } memset(umad_p, 0, padded_size + umad_size()); memcpy (umad_get_mad(umad_p), send_mad, send_size); /* Copy mad to umad */ umad_set_grh(umad_p, 0); pkey_idx = oib_find_pkey(port, addr->pkey); if (pkey_idx < 0) { DBGPRINT("P_Key 0x%x not found in pkey table\n", addr->pkey); if (addr->pkey == 0xffff) { pkey_idx = oib_find_pkey(port, 0x7fff); if (pkey_idx < 0) { OUTPUT_ERROR("Failed to find 0x7fff pkey defaulting to index 1\n"); pkey_idx = 1; } else { DBGPRINT("... using 0x7fff found at index %d\n", pkey_idx); } } else { // Previously, this code would try to find the limited management pkey // if it could not find the requested pkey, and use that pkey instead. // This would often "work" because all nodes should have the limited // management pkey, but b/c it was a limited member, this would result // in potential timeouts - especially where the full management pkey was // required. // Changed this code fail immediately without retrying a new pkey. OUTPUT_ERROR("Failed to find requested pkey:0x%x, class 0x%x aid:0x%x \n", addr->pkey, mclass, ntohs(mad_hdr->attr_id)); status = FPROTECTION; goto done; } } umad_set_pkey(umad_p, pkey_idx); umad_set_addr(umad_p, ib_lid?ib_lid:0xffff, addr->qpn, addr->sl, addr->qkey); if (dbg_file) { DBGPRINT(">>> sending: len %ld pktsz %zu\n", send_size, umad_size() + padded_size); umad_dump(umad_p); oib_dump_mad(dbg_file, umad_get_mad(umad_p), send_size, "send mad\n"); } correctedTimeout = (timeout_ms == OIB_SEND_TIMEOUT_DEFAULT) ? OIB_UTILS_DEF_TIMEOUT_MS : timeout_ms; if (umad_send(port->umad_fd, aid, umad_p, padded_size, (response ? 0 : correctedTimeout), retries) < 0) { OUTPUT_ERROR("send failed; %s, agent id %u MClass 0x%x method 0x%x attrId 0x%x attrM 0x%x\n", strerror(errno), aid, mclass, mad_hdr->method, ntohs(mad_hdr->attr_id), ntohl(mad_hdr->attr_mod)); status = FNOT_DONE; goto done; } done: // Free umad if allocated. if (umad_p != NULL) { umad_free(umad_p); } return status; }