Skip to content

Commit 26b8f02

Browse files
committed
add traffic tool.
1 parent eed3be3 commit 26b8f02

File tree

6 files changed

+202
-6
lines changed

6 files changed

+202
-6
lines changed

lib/ff_dpdk_if.c

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ struct ff_dpdk_if_context {
188188

189189
static struct ff_dpdk_if_context *veth_ctx[RTE_MAX_ETHPORTS];
190190

191+
static struct ff_top_args ff_top_status;
192+
static struct ff_traffic_args ff_traffic;
193+
191194
extern void ff_hardclock(void);
192195

193196
static void
@@ -994,6 +997,11 @@ process_packets(uint16_t port_id, uint16_t queue_id, struct rte_mbuf **bufs,
994997
void *data = rte_pktmbuf_mtod(rtem, void*);
995998
uint16_t len = rte_pktmbuf_data_len(rtem);
996999

1000+
if (!pkts_from_ring) {
1001+
ff_traffic.rx_packets++;
1002+
ff_traffic.rx_bytes += len;
1003+
}
1004+
9971005
if (!pkts_from_ring && packet_dispatcher) {
9981006
int ret = (*packet_dispatcher)(data, len, queue_id, nb_queues);
9991007
if (ret < 0 || ret >= nb_queues) {
@@ -1183,6 +1191,13 @@ handle_ipfw_msg(struct ff_msg *msg)
11831191
}
11841192
#endif
11851193

1194+
static inline void
1195+
handle_traffic_msg(struct ff_msg *msg)
1196+
{
1197+
msg->traffic = ff_traffic;
1198+
msg->result = 0;
1199+
}
1200+
11861201
static inline void
11871202
handle_default_msg(struct ff_msg *msg)
11881203
{
@@ -1215,6 +1230,9 @@ handle_msg(struct ff_msg *msg, uint16_t proc_id)
12151230
handle_ipfw_msg(msg);
12161231
break;
12171232
#endif
1233+
case FF_TRAFFIC:
1234+
handle_traffic_msg(msg);
1235+
break;
12181236
default:
12191237
handle_default_msg(msg);
12201238
break;
@@ -1253,6 +1271,12 @@ send_burst(struct lcore_conf *qconf, uint16_t n, uint8_t port)
12531271
}
12541272
}
12551273

1274+
ff_traffic.tx_packets += n;
1275+
uint16_t i;
1276+
for (i = 0; i < n; i++) {
1277+
ff_traffic.tx_bytes += rte_pktmbuf_data_len(m_table[i]);
1278+
}
1279+
12561280
ret = rte_eth_tx_burst(port, queueid, m_table, n);
12571281
if (unlikely(ret < n)) {
12581282
do {
@@ -1509,14 +1533,14 @@ main_loop(void *arg)
15091533

15101534
if (!idle) {
15111535
sys_tsc = div_tsc - cur_tsc;
1512-
ff_status.sys_tsc += sys_tsc;
1536+
ff_top_status.sys_tsc += sys_tsc;
15131537
}
15141538

1515-
ff_status.usr_tsc += usr_tsc;
1516-
ff_status.work_tsc += end_tsc - cur_tsc;
1517-
ff_status.idle_tsc += end_tsc - cur_tsc - usr_tsc - sys_tsc;
1539+
ff_top_status.usr_tsc += usr_tsc;
1540+
ff_top_status.work_tsc += end_tsc - cur_tsc;
1541+
ff_top_status.idle_tsc += end_tsc - cur_tsc - usr_tsc - sys_tsc;
15181542

1519-
ff_status.loops++;
1543+
ff_top_status.loops++;
15201544
}
15211545

15221546
return 0;

lib/ff_msg.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ enum FF_MSG_TYPE {
4242
FF_TOP,
4343
FF_NGCTL,
4444
FF_IPFW_CTL,
45+
FF_TRAFFIC,
4546
};
4647

4748
struct ff_sysctl_args {
@@ -92,6 +93,13 @@ struct ff_ipfw_args {
9293
socklen_t *optlen;
9394
};
9495

96+
struct ff_traffic_args {
97+
uint64_t rx_packets;
98+
uint64_t rx_bytes;
99+
uint64_t tx_packets;
100+
uint64_t tx_bytes;
101+
};
102+
95103
#define MAX_MSG_BUF_SIZE 10240
96104

97105
/* structure of ipc msg */
@@ -111,6 +119,7 @@ struct ff_msg {
111119
struct ff_top_args top;
112120
struct ff_ngctl_args ngctl;
113121
struct ff_ipfw_args ipfw;
122+
struct ff_traffic_args traffic;
114123
};
115124
} __attribute__((packed)) __rte_cache_aligned;
116125

tools/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
SUBDIRS=compat libutil libmemstat libxo libnetgraph sysctl ifconfig route top netstat ngctl ipfw arp
1+
SUBDIRS=compat libutil libmemstat libxo libnetgraph sysctl ifconfig route top netstat ngctl ipfw arp traffic
22

33
all:
44
for d in $(SUBDIRS); do ( cd $$d; $(MAKE) all ) ; done

tools/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,34 @@ usage: arp -p <f-stack proc_id> [-n] [-i interface] hostname
212212

213213
For more details, see [Manual page](https://www.freebsd.org/cgi/man.cgi?arp).
214214

215+
# traffic
216+
Usage:
217+
```
218+
traffic [-p <f-stack proc_id>] [-d <secs>] [-n num]
219+
```
220+
Examples:
221+
```
222+
./tools/traffic/traffic
223+
224+
|--------------------|--------------------|--------------------|--------------------|
225+
| rx packets| rx bytes| tx packets| tx bytes|
226+
|--------------------|--------------------|--------------------|--------------------|
227+
| 0| 0| 0| 0|
228+
| 298017| 30590860| 590912| 230712836|
229+
| 475808| 49008224| 951616| 372081856|
230+
| 474720| 48896160| 949440| 371231040|
231+
| 475296| 48955488| 950592| 371681472|
232+
| 475040| 48929120| 950080| 371481280|
233+
| 474368| 48859904| 948736| 370955776|
234+
| 475616| 48988448| 951232| 371931712|
235+
| 475552| 48981856| 951104| 371881664|
236+
| 476128| 49041184| 952256| 372332096|
237+
| 475680| 48995040| 951360| 371981760|
238+
| 475552| 48981856| 951104| 371881664|
239+
| 475488| 48975264| 950976| 371831616|
240+
| 473440| 48764320| 946880| 370230080|
241+
```
242+
215243
# how to implement a custom tool for communicating with F-Stack process
216244

217245
Add a new FF_MSG_TYPE in ff_msg.h:

tools/traffic/Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# @(#)Makefile 8.1 (Berkeley) 6/6/93
2+
# $FreeBSD$
3+
4+
5+
TOPDIR?=${CURDIR}/../..
6+
7+
PROG=traffic
8+
9+
include ${TOPDIR}/tools/prog.mk

tools/traffic/traffic.c

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#include<unistd.h>
2+
#include "ff_ipc.h"
3+
4+
void
5+
usage(void)
6+
{
7+
printf("Usage:\n");
8+
printf(" top [-p <f-stack proc_id>] [-d <secs>] [-n num] [-s]\n");
9+
}
10+
11+
int traffic_status(struct ff_traffic_args *traffic)
12+
{
13+
int ret;
14+
struct ff_msg *msg, *retmsg = NULL;
15+
16+
msg = ff_ipc_msg_alloc();
17+
if (msg == NULL) {
18+
errno = ENOMEM;
19+
return -1;
20+
}
21+
22+
msg->msg_type = FF_TRAFFIC;
23+
ret = ff_ipc_send(msg);
24+
if (ret < 0) {
25+
errno = EPIPE;
26+
ff_ipc_msg_free(msg);
27+
return -1;
28+
}
29+
30+
do {
31+
if (retmsg != NULL) {
32+
ff_ipc_msg_free(retmsg);
33+
}
34+
35+
ret = ff_ipc_recv(&retmsg);
36+
if (ret < 0) {
37+
errno = EPIPE;
38+
ff_ipc_msg_free(msg);
39+
return -1;
40+
}
41+
} while (msg != retmsg);
42+
43+
*traffic = retmsg->traffic;
44+
45+
ff_ipc_msg_free(msg);
46+
47+
return 0;
48+
}
49+
50+
int main(int argc, char **argv)
51+
{
52+
int ch, delay = 1, n = 0;
53+
int single = 0;
54+
unsigned int i;
55+
struct ff_traffic_args traffic, otr;
56+
57+
ff_ipc_init();
58+
59+
while ((ch = getopt(argc, argv, "hp:d:n:s")) != -1) {
60+
switch(ch) {
61+
case 'p':
62+
ff_set_proc_id(atoi(optarg));
63+
break;
64+
case 'd':
65+
delay = atoi(optarg) ?: 1;
66+
break;
67+
case 'n':
68+
n = atoi(optarg);
69+
break;
70+
case 's':
71+
single = 1;
72+
break;
73+
case 'h':
74+
default:
75+
usage();
76+
return -1;
77+
}
78+
}
79+
80+
if (single) {
81+
if (traffic_status(&traffic)) {
82+
printf("fstack ipc message error !\n");
83+
return -1;
84+
}
85+
86+
printf("%lu,%lu,%lu,%lu\n", traffic.rx_packets, traffic.rx_bytes,
87+
traffic.tx_packets, traffic.tx_bytes);
88+
return 0;
89+
}
90+
91+
#define DIFF(member) (traffic.member - otr.member)
92+
93+
for (i = 0; ; i++) {
94+
if (traffic_status(&traffic)) {
95+
printf("fstack ipc message error !\n");
96+
return -1;
97+
}
98+
99+
if (i % 40 == 0) {
100+
printf("|--------------------|--------------------|");
101+
printf("--------------------|--------------------|\n");
102+
printf("|%20s|%20s|%20s|%20s|\n", "rx packets", "rx bytes",
103+
"tx packets", "tx bytes");
104+
printf("|--------------------|--------------------|");
105+
printf("--------------------|--------------------|\n");
106+
}
107+
108+
if (i) {
109+
uint64_t rxp = DIFF(rx_packets);
110+
uint64_t rxb = DIFF(rx_bytes);
111+
uint64_t txp = DIFF(tx_packets);
112+
uint64_t txb = DIFF(tx_bytes);
113+
114+
printf("|%20lu|%20lu|%20lu|%20lu|\n", rxp, rxb, txp, txb);
115+
}
116+
117+
if (n && i >= n) {
118+
break;
119+
}
120+
121+
otr = traffic;
122+
sleep(delay);
123+
}
124+
125+
return 0;
126+
}

0 commit comments

Comments
 (0)