Add a function send_and_get that handles the communication and retries. Reduce redundancy. Increment TID on retry. Bound the number of retries.
Signed-off-by: Ishai Rabinovitz <[EMAIL PROTECTED]> Index: last_stable/src/userspace/srptools/src/srp-dm.c =================================================================== --- last_stable.orig/src/userspace/srptools/src/srp-dm.c 2006-04-21 01:35:10.000000000 +0300 +++ last_stable/src/userspace/srptools/src/srp-dm.c 2006-04-21 01:41:28.000000000 +0300 @@ -85,6 +85,36 @@ static void usage(const char *argv0) fprintf(stderr, "Usage: %s [-vc] [-d <umad device>]\n", argv0); } +#define NUM_OF_RETRIES 3 +int send_and_get(int fd, struct ib_user_mad *out_mad, + struct ib_user_mad *in_mad, long in_mad_size) +{ + int i, len, in_mad_real_size; + struct srp_dm_mad *out_dm_mad; + + in_mad_real_size = (in_mad_size ? in_mad_size : sizeof(struct ib_user_mad)); + for (i = 0; i < NUM_OF_RETRIES; ++i) + { + len = write(fd, out_mad, sizeof(struct ib_user_mad)); + if (len != sizeof(struct ib_user_mad)) { + fprintf(stderr, "write: %s\n", strerror(errno)); + return -1; + } + + len = read(fd, in_mad, in_mad_real_size); + if ((in_mad_size == 0 && len == in_mad_real_size) || + (in_mad_size != 0 && len > 0)) + return len; + else if (in_mad->hdr.status != ETIMEDOUT) { + fprintf(stderr, "%s/%d: read: %s\n", __func__, __LINE__, strerror(errno)); + return -1; + } + out_dm_mad = (void *) out_mad->data; + ((uint32_t *) &out_dm_mad->tid)[1] = tid++; + } + return -1; +} + static int read_file(const char *dir, const char *file, char *buf, size_t size) { char *path; @@ -234,19 +264,8 @@ static int set_class_port_info(int fd, u ((uint16_t *) cpi->trap_gid)[i] = htons(strtol(val + i * 5, NULL, 16)); } -again: - if (write(fd, &out_mad, sizeof out_mad) != sizeof out_mad) { - perror("write"); + if (send_and_get(fd, &out_mad, &in_mad, 0) < 0) return -1; - } - - if (read(fd, &in_mad, sizeof in_mad) != sizeof in_mad) { - if (in_mad.hdr.status == ETIMEDOUT) - goto again; - fprintf(stderr, "%s/%d: ", __func__, __LINE__); - perror("read"); - return -1; - } in_dm_mad = (void *) in_mad.data; if (in_dm_mad->status) { @@ -266,19 +285,8 @@ static int get_iou_info(int fd, uint32_t init_srp_dm_mad(&out_mad, agent[1], dlid, SRP_DM_ATTR_IO_UNIT_INFO, 0); -again: - if (write(fd, &out_mad, sizeof out_mad) != sizeof out_mad) { - perror("write"); - return -1; - } - - if (read(fd, &in_mad, sizeof in_mad) != sizeof in_mad) { - if (in_mad.hdr.status == ETIMEDOUT) - goto again; - fprintf(stderr, "%s/%d: ", __func__, __LINE__); - perror("read"); + if (send_and_get(fd, &out_mad, &in_mad, 0) < 0) return -1; - } in_dm_mad = (void *) in_mad.data; if (in_dm_mad->status) { @@ -300,19 +308,8 @@ static int get_ioc_prof(int fd, uint32_t init_srp_dm_mad(&out_mad, agent[1], dlid, SRP_DM_ATTR_IO_CONTROLLER_PROFILE, ioc); -again: - if (write(fd, &out_mad, sizeof out_mad) != sizeof out_mad) { - perror("write"); + if (send_and_get(fd, &out_mad, &in_mad, 0) < 0) return -1; - } - - if (read(fd, &in_mad, sizeof in_mad) != sizeof in_mad) { - if (in_mad.hdr.status == ETIMEDOUT) - goto again; - fprintf(stderr, "%s/%d: ", __func__, __LINE__); - perror("read"); - return -1; - } if (in_mad.hdr.status != 0) { fprintf(stderr, "IO Controller Profile query timed out\n"); @@ -340,19 +337,8 @@ static int get_svc_entries(int fd, uint3 init_srp_dm_mad(&out_mad, agent[1], dlid, SRP_DM_ATTR_SERVICE_ENTRIES, (ioc << 16) | (end << 8) | start); -again: - if (write(fd, &out_mad, sizeof out_mad) != sizeof out_mad) { - perror("write"); + if (send_and_get(fd, &out_mad, &in_mad, 0) < 0) return -1; - } - - if (read(fd, &in_mad, sizeof in_mad) != sizeof in_mad) { - if (in_mad.hdr.status == ETIMEDOUT) - goto again; - fprintf(stderr, "%s/%d: ", __func__, __LINE__); - perror("read"); - return -1; - } if (in_mad.hdr.status != 0) { fprintf(stderr, "Service Entries query timed out\n"); @@ -486,20 +472,8 @@ static int get_port_info(int fd, uint32_ port_info = (void *) out_sa_mad->data; port_info->endport_lid = htons(dlid); -again: - if (write(fd, &out_mad, sizeof out_mad) != sizeof out_mad) { - perror("write"); - return -1; - } - - if (read(fd, &in_mad, sizeof in_mad) != sizeof in_mad) { - if (in_mad.hdr.status == ETIMEDOUT) - goto again; - - fprintf(stderr, "%s/%d: ", __func__, __LINE__); - perror("read"); + if (send_and_get(fd, &out_mad, &in_mad, 0) < 0) return -1; - } port_info = (void *) in_sa_mad->data; *subnet_prefix = ntohll(port_info->subnet_prefix); @@ -542,21 +516,8 @@ static int get_port_list(int fd, uint32_ node = (void *) out_sa_mad->data; node->type = 1; /* CA */ -again: - if (write(fd, &out_mad, sizeof out_mad) != sizeof out_mad) { - perror("write"); + if ((len = send_and_get(fd, &out_mad, in_mad, SIZE_OF_QUERY_RESPONSE)) < 0) return -1; - } - - len = read(fd, in_mad, SIZE_OF_QUERY_RESPONSE); - if (len < 0) { - fprintf(stderr, "%s/%d: ", __func__, __LINE__); - perror("read"); - return -1; - } - - if (in_mad->hdr.status == ETIMEDOUT) - goto again; size = ntohs(in_sa_mad->attr_offset) * 8; -- Ishai Rabinovitz _______________________________________________ openib-general mailing list openib-general@openib.org http://openib.org/mailman/listinfo/openib-general To unsubscribe, please visit http://openib.org/mailman/listinfo/openib-general