2 * A tool for measuring FWP latency. This program aims to be simpler
3 * than wme_test and is intended to be run on a single machine with
4 * multiple wifi interfaces and send-to-self kernel patch applied.
6 * This program creates both sides of communication and measures the
7 * communication delays.
10 #include <netinet/in.h>
11 #include <arpa/inet.h>
15 #include <netinet/in.h>
17 #include <sys/socket.h>
18 #include <sys/types.h>
20 #include <sys/param.h>
24 #include <semaphore.h>
26 void set_rt_prio(int priority)
29 static struct sched_param param;
31 if ((maxpri = sched_get_priority_max(SCHED_FIFO)) == -1) {
32 fprintf(stderr, "warning: sched_get_priority_max failed");
35 if ((minpri = sched_get_priority_min(SCHED_FIFO)) == -1) {
36 fprintf(stderr, "warning: sched_get_priority_min failed");
39 if (priority > maxpri) {
40 fprintf(stderr, "warning: maximum priority allowed is %d.\n", maxpri);
42 if (priority < minpri) {
43 fprintf(stderr, "warning: minimum priority allowed is %d.\n", minpri);
46 param.sched_priority = priority;
48 if (sched_setscheduler(0, SCHED_FIFO, ¶m) == -1) {
49 fprintf(stderr, "warning: sched_setscheduler failed");
52 mlockall(MCL_CURRENT | MCL_FUTURE);
55 struct stream_params {
59 struct in_addr src, dst;
66 int negotiate_contract(struct stream_params *p, frsh_vres_id_t *vres)
68 frsh_contract_t contract;
70 frsh_rel_time_t period;
71 frsh_rel_time_t budget;
72 frsh_rel_time_t deadline;
75 ret = frsh_contract_init(&contract);
76 if (ret) PERROR_AND_EXIT(ret, "frsh_contract_init");
78 ret = frsh_contract_set_resource_and_label(
80 FRSH_RT_NETWORK, FRSH_NETPF_FWP,
82 if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_resource_and_label");
84 frsh_network_bytes_to_budget(FRSH_NETPF_FWP, p->budget, &budget);
85 period = fosa_msec_to_rel_time(p->period_ms);
86 ret = frsh_contract_set_basic_params(&contract,
91 if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_basic_params");
94 /* FWP doesn't accept smaller deadlines than 30 ms. */
95 if (frsh_rel_time_smaller(period, frsh_msec_to_rel_time(30)))
96 deadline = frsh_msec_to_rel_time(30);
99 ret = frsh_contract_set_timing_reqs(&contract, false, &deadline);
100 if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_timing_reqs");
102 fwp = malloc(sizeof(*fwp));
103 if (!fwp) PERROR_AND_EXIT(errno, "malloc");
104 fwp->src = p->src.s_addr;
105 ret = fres_contract_add_fwp(contract, fwp);
106 if (ret) PERROR_AND_EXIT(ret, "fres_contract_add_fwp");
108 ret = frsh_contract_negotiate(&contract, vres);
109 if (ret) PERROR_AND_EXIT(ret, "frsh_contract_negotiate");
111 frsh_contract_destroy(&contract);
116 void create_endpoints(struct stream_params *p,
118 frsh_send_endpoint_t *epsrc,
119 frsh_receive_endpoint_t *epdst)
122 frsh_send_endpoint_protocol_info_t spi = { NULL, 0 };
123 frsh_receive_endpoint_protocol_info_t rpi = { NULL, 0 };
124 frsh_endpoint_queueing_info_t qi = { .queue_size=0,
125 .queue_policy=FRSH_QRP_OLDEST };
127 ret = frsh_receive_endpoint_create(FRSH_NETPF_FWP, 0, qi, rpi,
129 if (ret != 0) error(1, errno, "fwp_receive_endpoint_create");
132 frsh_receive_endpoint_get_params(*epdst, NULL, &port, NULL, NULL);
134 ret = frsh_send_endpoint_create(FRSH_NETPF_FWP,
135 p->dst.s_addr, port, spi,
137 if (ret < 0) error(1, errno, "frsh_send_endpoint_create()");
139 ret = frsh_send_endpoint_bind(vres, *epsrc);
140 if (ret != 0) error(1, errno, "frsh_send_endpoint_bind");
144 static struct option long_opts[] = {
145 { "period", 1, 0, 'p' },
146 { "budget", 1, 0, 'b' },
147 { "source", 1, 0, 's' },
148 { "dest", 1, 0, 'd' },
149 { "async", 0, 0, 'a' },
150 { "number", 0, 0, 'n' },
151 { "count", 0, 0, 'c' },
158 printf("usage: fwp-timing [ options ]\n");
159 printf(" -p, --period <ms> period in miliseconds\n");
160 printf(" -b, --budget <bytes> how many bytes is sent in each period\n");
161 printf(" -s, --source <ip> source IP address\n");
162 printf(" -d, --dest <ip:port> destination IP address and port\n");
163 printf(" -a, --async Send packets asynchronously\n");
164 printf(" -n, --number Number of streams with the same parameters\n");
165 printf(" -c, --count Number of messages to send [infinity]\n");
166 printf(" -/, --stream New stream separator\n");
169 int parse_opts(int *argc, char **argv[], struct stream_params *p)
173 bool options_found = false;
175 while ((opt = getopt_long(*argc, *argv, "/ab:c:p:s:d:n:", long_opts, NULL)) != -1) {
176 options_found = true;
182 p->budget = atoi(optarg);
185 p->count = atoi(optarg);
188 p->period_ms = atoi(optarg);
191 ret = inet_aton(optarg, &p->src);
193 fprintf(stderr, "Source IP address not recognized: %s\n",
200 ret = inet_aton(optarg, &p->dst);
202 fprintf(stderr, "Destination IP address not recognized: %s\n",
209 p->number = atoi(optarg);
220 return (options_found) ? 0 : -1;
223 volatile bool exit_flag = false;
231 timespec_subtract (result, x, y)
232 struct timespec *result, *x, *y;
234 /* Perform the carry for the later subtraction by updating Y. */
235 if (x->tv_nsec < y->tv_nsec) {
236 int nsec = (y->tv_nsec - x->tv_nsec) / 1000000000 + 1;
237 y->tv_nsec -= 1000000000 * nsec;
240 if (x->tv_nsec - y->tv_nsec > 1000000000) {
241 int nsec = (x->tv_nsec - y->tv_nsec) / 1000000000;
242 y->tv_nsec += 1000000000 * nsec;
246 /* Compute the time remaining to wait.
247 `tv_nsec' is certainly positive. */
248 result->tv_sec = x->tv_sec - y->tv_sec;
249 result->tv_nsec = x->tv_nsec - y->tv_nsec;
251 /* Return 1 if result is negative. */
252 return x->tv_sec < y->tv_sec;
255 static inline double ts2d(struct timespec *ts)
257 return ts->tv_sec + 1e-9*ts->tv_nsec;
260 static inline double tsdiff2d(struct timespec *x,
264 timespec_subtract(&r, x, y);
273 struct receiver_params {
275 frsh_receive_endpoint_t epdst;
279 void *receiver(void *arg)
281 struct receiver_params *rp = arg;
282 frsh_receive_endpoint_t epdst = rp->epdst;
286 struct timespec tss, tsr;
288 msg = malloc(rp->budget);
289 if (!msg) error(1, errno, "malloc msg");
292 ret = frsh_receive_sync(epdst, msg, rp->budget, &mlen, NULL);
293 clock_gettime(CLOCK_MONOTONIC, &tsr);
295 if (msg->cnt != last_cnt+1)
296 printf("packet(s) lost!\n");
297 printf("%3d: %10d: %10.3lf ms\n",
298 rp->id, msg->cnt, tsdiff2d(&tsr, &tss)*1000);
307 void *sender(void *arg)
309 struct stream_params *p = arg;
311 frsh_send_endpoint_t epsrc;
312 struct receiver_params *rp;
316 pthread_t receiver_id;
318 msg = malloc(p->budget);
319 if (!msg) error(1, errno, "malloc msg");
321 negotiate_contract(p, &vres);
323 rp = malloc(sizeof(*rp));
324 rp->budget = p->budget;
327 create_endpoints(p, vres, &epsrc, &rp->epdst);
331 ret = pthread_create(&receiver_id, NULL, receiver, rp);
333 if (signal(SIGTERM, stopper) == SIG_ERR)
334 error(1, errno, "Error in signal registration");
335 if (signal(SIGINT, stopper) == SIG_ERR)
336 error(1, errno, "Signal handler registration error");
338 struct timespec next_period;
340 clock_gettime(CLOCK_MONOTONIC, &next_period);
341 while (!exit_flag && (p->count == -1 || p->count--)) {
342 clock_gettime(CLOCK_MONOTONIC, &tss);
346 ret = frsh_send_async(epsrc, msg, p->budget);
348 ret = frsh_send_sync(epsrc, msg, p->budget);
349 clock_gettime(CLOCK_MONOTONIC, &next_period);
351 next_period.tv_sec += (p->period_ms/1000);
352 next_period.tv_nsec += (p->period_ms%1000) * 1000000;
353 if (next_period.tv_nsec >= 1000000000) {
354 next_period.tv_nsec -= 1000000000;
355 next_period.tv_sec++;
357 clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME,
361 frsh_contract_cancel(vres);
367 int main(int argc, char *argv[])
371 struct stream_params sp = {
375 .src.s_addr = htonl(INADDR_LOOPBACK),
376 .dst.s_addr = htonl(INADDR_LOOPBACK),
380 struct stream_params *p = NULL;
382 sem_init(&finished, 0, 0);
385 if (ret) PERROR_AND_EXIT(ret, "frsh_init");
388 ret = parse_opts(&argc, &argv, &sp);
389 if (num == 0 || ret == 0) {
391 for (i=0; i<sp.number; i++) {
393 p = malloc(sizeof(*p));
394 if (!p) error(1, errno, "malloc");
397 pthread_create(&thread, NULL, sender, p);