[dpdk-dev] [PATCH v2][PKTGEN] Rate as a decimal number

inaki.murillo inaki.murilloa at ehu.eus
Fri Apr 22 10:07:24 CEST 2016


Currently Pktgen does not accept a decimal number for the rate. This patch makes possible to set a decimal number as a rate.

Signed-off-by: Iñaki Murillo Arroyo <inaki.murilloa at ehu.eus>
---
 app/cmd-functions.c   | 71 ++++++++++++++++++++++++++++++++++++++++++++++++---
 app/cmd-functions.h   | 38 +++++++++++++++++++++++++++
 app/lpktgenlib.c      |  2 +-
 app/pktgen-cmds.c     | 10 ++++----
 app/pktgen-cmds.h     |  2 +-
 app/pktgen-port-cfg.h |  2 +-
 app/pktgen.c          |  2 +-
 7 files changed, 115 insertions(+), 12 deletions(-)

diff --git a/app/cmd-functions.c b/app/cmd-functions.c
index b2fda7c..84809bb 100644
--- a/app/cmd-functions.c
+++ b/app/cmd-functions.c
@@ -1805,7 +1805,7 @@ struct cmd_set_result {
 	cmdline_fixed_string_t set;
 	cmdline_portlist_t portlist;
 	cmdline_fixed_string_t what;
-	uint32_t value;
+	float value;
 };
 
 /**************************************************************************//**
@@ -1865,8 +1865,8 @@ cmdline_parse_token_string_t cmd_set_what =
         TOKEN_STRING_INITIALIZER(struct cmd_set_result,
                                  what,
                                  "count#size#rate#burst#tx_cycles#sport#dport#seqCnt#prime#dump#vlanid");
-cmdline_parse_token_num_t cmd_set_value =
-        TOKEN_NUM_INITIALIZER(struct cmd_set_result, value, UINT32);
+cmdline_parse_token_float_t cmd_set_value =
+	TOKEN_FLOAT_INITIALIZER(struct cmd_set_result, value, FLOAT);
 
 cmdline_parse_inst_t cmd_set = {
 	.f = cmd_set_parsed,
@@ -4691,3 +4691,68 @@ pktgen_load_cmds(char *filename)
 	}
 	return 0;
 }
+
+struct cmdline_token_ops cmdline_token_float_ops = {
+	.parse = cmdline_parse_float,
+	.complete_get_nb = NULL,
+	.complete_get_elt = NULL,
+	.get_help = cmdline_get_help_float,
+};
+
+/* parse a float */
+int
+cmdline_parse_float(cmdline_parse_token_hdr_t *tk, const char *srcbuf, 
+			void *res, unsigned ressize)
+{
+	unsigned int token_len;
+	float tmp;
+
+	if (res && ressize < STR_TOKEN_SIZE)
+		return -1;
+
+	if (!tk || !srcbuf || !*srcbuf)
+		return -1;
+
+	token_len = 0;
+	while (!cmdline_isendoftoken(srcbuf[token_len]) && token_len < (STR_TOKEN_SIZE-1)){
+		token_len++;
+	}
+
+	/* return if token too long */
+	if (token_len >= STR_TOKEN_SIZE - 1)
+		return -1;
+
+	tmp =  atof(srcbuf);
+	if ((tmp == 0 && strcmp(srcbuf, "0") == 0) || (tmp != 0)) {
+		/* we are sure that token_len is < STR_TOKEN_SIZE-1 */
+		*(float *)res = tmp;
+		return token_len;
+	}
+
+	return -1;
+
+}
+
+/* parse a float */
+int
+cmdline_get_help_float(cmdline_parse_token_hdr_t *tk,
+			char *dstbuf, unsigned int size)
+{
+	struct cmdline_token_float_data nd;
+	int ret;
+
+	if (!tk)
+		return -1;
+
+	memcpy(&nd, &((struct cmdline_token_float *)tk)->float_data, sizeof(nd));
+
+	/* should not happen.... don't so this test */
+	/* if (nd.type >= (sizeof(num_help)/sizeof(const char *))) */
+	/* return -1; */
+
+	ret = snprintf(dstbuf, size, "FLOAT");
+	if (ret < 0)
+		return -1;
+	dstbuf[size-1] = '\0';
+	return 0;
+}
diff --git a/app/cmd-functions.h b/app/cmd-functions.h
index 0ccef6d..4b8dc4c 100644
--- a/app/cmd-functions.h
+++ b/app/cmd-functions.h
@@ -77,4 +77,42 @@ extern void pktgen_cmdline_start(void);
 
 extern int pktgen_load_cmds(char *filename);
 
+/* size of a parsed string */
+#define STR_TOKEN_SIZE 128
+
+enum cmdline_floattype {
+	FLOAT
+};
+
+struct cmdline_token_float_data {
+	const float *float_data;
+};
+
+struct cmdline_token_float {
+	struct cmdline_token_hdr hdr;
+	struct cmdline_token_float_data float_data;
+};
+typedef struct cmdline_token_float cmdline_parse_token_float_t;
+
+extern struct cmdline_token_ops cmdline_token_float_ops;
+
+int cmdline_parse_float(cmdline_parse_token_hdr_t *tk,
+	const char *srcbuf, void *res, unsigned ressize);
+int cmdline_get_help_float(cmdline_parse_token_hdr_t *tk,
+	char *dstbuf, unsigned int size);
+
+#define TOKEN_FLOAT_INITIALIZER(structure, field, float_type)    \
+{                                                           \
+	/* hdr */                                               \
+	{                                                       \
+		&cmdline_token_float_ops,         /* ops */           \
+		offsetof(structure, field),     /* offset */        \
+	},                                                      \
+	/* num_data */                                          \
+	{                                                       \
+		float_type,                        /* type */          \
+	},                                                      \
+}
+
+
 #endif /* _COMMANDS_H_ */
diff --git a/app/lpktgenlib.c b/app/lpktgenlib.c
index 693a174..f3a2a84 100644
--- a/app/lpktgenlib.c
+++ b/app/lpktgenlib.c
@@ -240,7 +240,7 @@ parse_portlist(const char *buf, void *pl) {
 
 static int
 pktgen_set(lua_State *L) {
-	uint32_t value;
+	float value;
 	cmdline_portlist_t portlist;
 	char *what;
 
diff --git a/app/pktgen-cmds.c b/app/pktgen-cmds.c
index 83ba230..d150b41 100644
--- a/app/pktgen-cmds.c
+++ b/app/pktgen-cmds.c
@@ -178,7 +178,7 @@ pktgen_save(char *path)
 		fprintf(fd, "#\n");
 		flags = rte_atomic32_read(&info->port_flags);
 		fprintf(fd,
-		        "# Port: %2d, Burst:%3d, Rate:%3d%%, Flags:%08x, TX Count:%s\n",
+		        "# Port: %2d, Burst:%3d, Rate:%3f%%, Flags:%08x, TX Count:%s\n",
 		        info->pid,
 		        info->tx_burst,
 		        info->tx_rate,
@@ -196,7 +196,7 @@ pktgen_save(char *path)
 		        "set %d size %d\n",
 		        info->pid,
 		        pkt->pktSize + FCS_SIZE);
-		fprintf(fd, "set %d rate %d\n", info->pid, info->tx_rate);
+		fprintf(fd, "set %d rate %f\n", info->pid, info->tx_rate);
 		fprintf(fd, "set %d burst %d\n", info->pid, info->tx_burst);
 		fprintf(fd, "set %d sport %d\n", info->pid, pkt->sport);
 		fprintf(fd, "set %d dport %d\n", info->pid, pkt->dport);
@@ -498,9 +498,9 @@ pktgen_transmit_count_rate(int port, char *buff, int len)
 	port_info_t *info = &pktgen.info[port];
 
 	if (rte_atomic64_read(&info->transmit_count) == 0)
-		snprintf(buff, len, "Forever /%4d%%", info->tx_rate);
+		snprintf(buff, len, "Forever /%4f%%", info->tx_rate);
 	else
-		snprintf(buff, len, "%ld /%4d%%",
+		snprintf(buff, len, "%ld /%4f%%",
 		         rte_atomic64_read(&info->transmit_count),
 		         info->tx_rate);
 
@@ -1949,7 +1949,7 @@ pktgen_set_port_value(port_info_t *info, char type, uint32_t portValue)
  */
 
 void
-pktgen_set_tx_rate(port_info_t *info, uint32_t rate)
+pktgen_set_tx_rate(port_info_t *info, float rate)
 {
 	if (rate == 0)
 		rate = 1;
diff --git a/app/pktgen-cmds.h b/app/pktgen-cmds.h
index 683abc3..1c50e59 100644
--- a/app/pktgen-cmds.h
+++ b/app/pktgen-cmds.h
@@ -135,7 +135,7 @@ extern void pktgen_set_pkt_size(port_info_t *info, uint32_t size);
 extern void pktgen_set_port_value(port_info_t *info,
                                   char type,
                                   uint32_t portValue);
-extern void pktgen_set_tx_rate(port_info_t *info, uint32_t rate);
+extern void pktgen_set_tx_rate(port_info_t *info, float rate);
 extern void pktgen_set_ipaddr(port_info_t *info, char type,
                               cmdline_ipaddr_t *ip);
 extern void pktgen_set_dst_mac(port_info_t *info, cmdline_etheraddr_t *mac);
diff --git a/app/pktgen-port-cfg.h b/app/pktgen-port-cfg.h
index bd5876c..a20286d 100644
--- a/app/pktgen-port-cfg.h
+++ b/app/pktgen-port-cfg.h
@@ -193,7 +193,7 @@ typedef struct port_info_s {
 	uint16_t pid;		/**< Port ID value */
 	uint16_t tx_burst;	/**< Number of TX burst packets */
 	uint8_t pad0;
-	uint8_t tx_rate;		/**< Percentage rate for tx packets */
+	float tx_rate;		/**< Percentage rate for tx packets */
 	rte_atomic32_t port_flags;	/**< Special send flags for ARP and other */
 
 	rte_atomic64_t transmit_count;	/**< Packets to transmit loaded into current_tx_count */
diff --git a/app/pktgen.c b/app/pktgen.c
index a0705ab..a64e0ca 100644
--- a/app/pktgen.c
+++ b/app/pktgen.c
@@ -143,7 +143,7 @@ pktgen_packet_rate(port_info_t *info)
 	info->tx_pps    = pps;
 	info->tx_cycles = ((cpp * info->tx_burst) /
 	                       wr_get_port_txcnt(pktgen.l2p, info->pid));
-	info->tx_cycles -= ff[info->tx_rate / 10];
+	info->tx_cycles -= ff[(int)info->tx_rate / 10];
 }
 
 /**************************************************************************//**
-- 
1.9.1



More information about the dev mailing list