From 9a5d808f7143d16bc5d7800271cfa78f666de111 Mon Sep 17 00:00:00 2001 From: Nathan Straz Date: Thu, 19 Sep 2013 15:06:55 -0400 Subject: Reduce size of some fields sent on the wire We don't need 32bits for packet type or remote fd. Keeping the field small also helps with reading traces. --- qarsh_packet.c | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/qarsh_packet.c b/qarsh_packet.c index 24f4661..2ca3f13 100644 --- a/qarsh_packet.c +++ b/qarsh_packet.c @@ -203,6 +203,20 @@ qp_packet_type(enum qa_packet_type t) * Packet parsing functions */ +char * +fetch_uint8(char *buf, int *buflen, int *out) +{ + uint8_t i; + if (*buflen < sizeof i) { + lprintf(0, "Not enough data to unpack a uint8_t, %d\n", *buflen); + return buf; + } + memcpy(&i, buf, sizeof i); + *out = i; + *buflen -= sizeof i; + return buf + sizeof i; +} + char * fetch_int(char *buf, int *buflen, int *out) { @@ -338,7 +352,7 @@ parse_qp_rstat(char *buf, int *buflen, struct qa_packet *qp) void parse_qp_data(char *buf, int *buflen, struct qa_packet *qp) { - buf = fetch_int(buf, buflen, (int *)&(qp->qp_data.qp_remfd)); + buf = fetch_uint8(buf, buflen, (int *)&(qp->qp_data.qp_remfd)); buf = fetch_off_t(buf, buflen, &(qp->qp_data.qp_offset)); buf = fetch_int(buf, buflen, (int *)&(qp->qp_data.qp_count)); if (qp->qp_data.qp_count > *buflen) { @@ -355,7 +369,7 @@ parse_qp_data(char *buf, int *buflen, struct qa_packet *qp) void parse_qp_data_allow(char *buf, int *buflen, struct qa_packet *qp) { - buf = fetch_int(buf, buflen, (int *)&(qp->qp_dallow.qp_remfd)); + buf = fetch_uint8(buf, buflen, (int *)&(qp->qp_dallow.qp_remfd)); buf = fetch_int(buf, buflen, (int *)&(qp->qp_dallow.qp_count)); } @@ -369,7 +383,7 @@ parse_packet(char *buf, int buflen) memset(qp, 0, sizeof *qp); buf = fetch_int(buf, &buflen, &(qp->qp_seq)); - buf = fetch_int(buf, &buflen, (int *)&(qp->qp_type)); + buf = fetch_uint8(buf, &buflen, (int *)&(qp->qp_type)); if (qa_pi[qp->qp_type].pi_parse) qa_pi[qp->qp_type].pi_parse(buf, &buflen, qp); @@ -386,6 +400,14 @@ parse_packet(char *buf, int buflen) * Packet serialization functions * */ +static char * +store_uint8(char *buf, int i) +{ + uint8_t a = i; + memcpy(buf, &a, sizeof a); + return buf + sizeof a; +} + static char * store_int(char *buf, int i) { @@ -504,7 +526,7 @@ store_qp_rstat(char *buf, struct qa_packet *qp) char * store_qp_data(char *buf, struct qa_packet *qp) { - buf = store_int(buf, qp->qp_data.qp_remfd); + buf = store_uint8(buf, qp->qp_data.qp_remfd); buf = store_off_t(buf, qp->qp_data.qp_offset); buf = store_void(buf, qp->qp_data.qp_count, qp->qp_data.qp_blob); return buf; @@ -513,7 +535,7 @@ store_qp_data(char *buf, struct qa_packet *qp) char * store_qp_data_allow(char *buf, struct qa_packet *qp) { - buf = store_int(buf, qp->qp_dallow.qp_remfd); + buf = store_uint8(buf, qp->qp_dallow.qp_remfd); buf = store_int(buf, qp->qp_dallow.qp_count); return buf; } @@ -528,7 +550,7 @@ qptostr(struct qa_packet *qp, char *qpstr, int maxsize) cp = qpstr; cp = store_int(cp, qp->qp_seq); - cp = store_int(cp, qp->qp_type); + cp = store_uint8(cp, qp->qp_type); if (qa_pi[qp->qp_type].pi_store) { cp = qa_pi[qp->qp_type].pi_store(cp, qp); -- cgit