1 #include "libmisc/misc.h"
2 #include <sys/socket.h>
3 #include "glib.h"
4 #include "libmisc/file.h"
5
6 #define CLIENT_LISTEN_PORT 6234
7 #define CLIENT_WRITE_PORT 8349
8
9 #define SERVER_WRITE_PORT 6234
10 #define SERVER_LISTEN_PORT 8349
11
12 #define LOCAL_HOST_ADDR_LO "127.0.0.1"
13 #define LOCAL_HOST_ADDR_ETH "eth0"
14
15 #define NUM_MESSAGES 1050
16 #define NUM_MESSAGES_QUEUED 50
17
18 #define BLOCKING
19
20 int convert(int num)
21 {
22 if (num == 64)
23 return 6;
24 if (num == 128)
25 return 7;
26 else if (num == 256)
27 return 8;
28 else if (num == 512)
29 return 9;
30 else if (num == 1024)
31 return 10;
32 else if (num == 2048)
33 return 11;
34 else if (num == 4096)
35 return 12;
36 else if (num == 8192)
37 return 13;
38 else if (num == 16384)
39 return 14;
40 else if (num == 32768)
41 return 15;
42 else
43 return -1;
44 }
45
46 int main(int argc, char **argv)
47 {
48 int i,write_sock,listen_port, write_port;
49 float pktsPSec, bytesPSec;
50 char snd_buf[65535];
51 struct in_addr bcast_addr = {
52 };
53 struct in_addr local_addr = {
54 };
55 struct sockaddr_in addr = {
56 sin_family: AF_INET
57 };
58
59 char interface[15];
60 char type[10];
61 struct timeval tvi;
62 int pkt_size, nonblock, client = 0;
63 int64_t timeT;
64 struct timeval tvs;
65 int t_fd;
66 char buf[255];
67
68 if (argc < 5) {
69 printf("ERROR, argc: %d < 4\n",argc);
70 printf("USAGE: ping_udp <server/client> <lo/eth> <pkt_size> <nonblocking>\n");
71 printf("\t\tOptional: <write_port> <read_port>\n");
72 exit(1);
73 }
74
75 /* Read parameters */
76 sprintf(type,argv[1]);
77 sprintf(interface,argv[2]);
78 pkt_size = atoi(argv[3]);
79 nonblock = atoi(argv[4]);
80
81
82 /* Assign ports */
83 if (argc >= 7) {
84 write_port = atoi(argv[5]);
85 listen_port = atoi(argv[6]);
86 } else {
87 if (strcmp(type,"client") == 0) {
88 write_port = CLIENT_WRITE_PORT;
89 listen_port = CLIENT_LISTEN_PORT;
90 } else if (strcmp(type,"server") == 0) {
91 listen_port = SERVER_LISTEN_PORT;
92 write_port = SERVER_WRITE_PORT;
93 }
94 }
95
96 /* Assign interface address */
97 if ((get_ifaddr_by_name(LOCAL_HOST_ADDR_ETH, &local_addr, &bcast_addr)) < 0) {
98 printf("Can't get intf address for: %s\n", LOCAL_HOST_ADDR_ETH);
99 exit(1);
100 }
101
102 if (strcmp(interface,"lo") == 0)
103 addr.sin_addr = local_addr;
104 else
105 addr.sin_addr = bcast_addr;
106 write_sock = udp_listen_if(write_port, &addr.sin_addr);
107
108 addr.sin_port = htons(write_port);
109
110 if (write_sock < 0) {
111 printf("ERROR, Couldn't open listen socket\n");
112 goto fail;
113 }
114
115 if (strcmp(type,"client") == 0) {
116 if ((t_fd = open("/tmp/throughput.out",O_CREAT|O_APPEND|O_RDWR, S_IRWXU|S_IRWXG|S_IRWXO)) < 0)
117 printf("ERROR, unable to open throughput.out output file!\n");
118 client = 1;
119 }
120
121 else if (strcmp(type,"server") != 0) {
122 printf("ERROR: unrecognized argument!\n");
123 goto fail;
124 }
125
126 /* Open socket for writing */
127
128
129 /* Send and read ping packets */
130 i = 0;
131 while ( i < NUM_MESSAGES) {
132 if (sendto(write_sock, snd_buf, pkt_size, 0,
133 (struct sockaddr *) &addr, sizeof(addr)) == pkt_size) {
134 i++;
135 sprintf(buf, "%d successful msgs\n", i);
136 }
137
138 else {
139 printf("ERROR sending!\n");
140 }
141
142 if (i == NUM_MESSAGES_QUEUED) {
143 gettimeofday(&tvi,NULL);
144 printf("Getting time: %s\n", misc_tv_to_str(&tvi));
145 }
146 }
147
148 gettimeofday(&tvs,NULL);
149 printf("Getting final time: %s\n", misc_tv_to_str(&tvs));
150
151 if (client > 0) {
152 misc_tv_sub(&tvs,&tvi);
153 timeT = misc_timeval_to_int64(&tvs);
154 printf("diff in usec: %lld\n", timeT);
155
156 /* Calculate overall throughput: pkts / sec
157 * timeT is in usec, convert to seconds */
158 if (timeT > 0) {
159 pktsPSec = (NUM_MESSAGES - NUM_MESSAGES_QUEUED) / ((float)timeT/ 1000000.0);
160 bytesPSec = pkt_size * pktsPSec;
161 printf("pkt_size: %d * pktsPSec: %f = %f\n", pkt_size, pktsPSec, bytesPSec);
162 }
163
164 sprintf(snd_buf, "%s", "#h log2pkt Pkts-sec Bytes-sec\n");
165 write(t_fd, snd_buf, strlen(snd_buf));
166
167 sprintf(snd_buf, "%d\t%d\t%d\n",convert(pkt_size), (int) pktsPSec, (int) bytesPSec);
168 write(t_fd, snd_buf, strlen(snd_buf));
169 }
170
171 printf("done\n");
172
173 fail:
174 close(write_sock);
175 close(t_fd);
176 printf("Closing files\n");
177 return 0;
178 }
179
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.