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;
65 int negotiate_contract(struct stream_params *p, frsh_vres_id_t *vres)
67 frsh_contract_t contract;
69 frsh_rel_time_t period;
70 frsh_rel_time_t budget;
71 frsh_rel_time_t deadline;
74 ret = frsh_contract_init(&contract);
75 if (ret) PERROR_AND_EXIT(ret, "frsh_contract_init");
77 ret = frsh_contract_set_resource_and_label(
79 FRSH_RT_NETWORK, FRSH_NETPF_FWP,
81 if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_resource_and_label");
83 frsh_network_bytes_to_budget(FRSH_NETPF_FWP, p->budget, &budget);
84 period = fosa_msec_to_rel_time(p->period_ms);
85 ret = frsh_contract_set_basic_params(&contract,
90 if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_basic_params");
93 /* FWP doesn't accept smaller deadlines than 30 ms. */
94 if (frsh_rel_time_smaller(period, frsh_msec_to_rel_time(30)))
95 deadline = frsh_msec_to_rel_time(30);
98 ret = frsh_contract_set_timing_reqs(&contract, false, &deadline);
99 if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_timing_reqs");
101 fwp = malloc(sizeof(*fwp));
102 if (!fwp) PERROR_AND_EXIT(errno, "malloc");
103 fwp->src = p->src.s_addr;
104 ret = fres_contract_add_fwp(contract, fwp);
105 if (ret) PERROR_AND_EXIT(ret, "fres_contract_add_fwp");
107 ret = frsh_contract_negotiate(&contract, vres);
108 if (ret) PERROR_AND_EXIT(ret, "frsh_contract_negotiate");
110 frsh_contract_destroy(&contract);
115 void create_endpoints(struct stream_params *p,
117 frsh_send_endpoint_t *epsrc,
118 frsh_receive_endpoint_t *epdst)
121 frsh_send_endpoint_protocol_info_t spi = { NULL, 0 };
122 frsh_receive_endpoint_protocol_info_t rpi = { NULL, 0 };
123 frsh_endpoint_queueing_info_t qi = { .queue_size=0,
124 .queue_policy=FRSH_QRP_OLDEST };
126 ret = frsh_receive_endpoint_create(FRSH_NETPF_FWP, 0, qi, rpi,
128 if (ret != 0) error(1, errno, "fwp_receive_endpoint_create");
131 frsh_receive_endpoint_get_params(*epdst, NULL, &port, NULL, NULL);
133 ret = frsh_send_endpoint_create(FRSH_NETPF_FWP,
134 p->dst.s_addr, port, spi,
136 if (ret < 0) error(1, errno, "frsh_send_endpoint_create()");
138 ret = frsh_send_endpoint_bind(vres, *epsrc);
139 if (ret != 0) error(1, errno, "frsh_send_endpoint_bind");
143 static struct option long_opts[] = {
144 { "period", 1, 0, 'p' },
145 { "budget", 1, 0, 'b' },
146 { "source", 1, 0, 's' },
147 { "dest", 1, 0, 'd' },
148 { "async", 0, 0, 'a' },
149 { "number", 0, 0, 'n' },
156 printf("usage: fwp-timing [ options ]\n");
157 printf(" -p, --period <ms> period in miliseconds\n");
158 printf(" -b, --budget <bytes> how many bytes is sent in each period\n");
159 printf(" -s, --source <ip> source IP address\n");
160 printf(" -d, --dest <ip:port> destination IP address and port\n");
161 printf(" -a, --async Send packets asynchronously\n");
162 printf(" -n, --number Number of streams with the same parameters\n");
163 printf(" -/, --stream New stream separator\n");
166 int parse_opts(int *argc, char **argv[], struct stream_params *p)
170 bool options_found = false;
172 while ((opt = getopt_long(*argc, *argv, "/ab:p:s:d:n:", long_opts, NULL)) != -1) {
173 options_found = true;
179 p->budget = atoi(optarg);
182 p->period_ms = atoi(optarg);
185 ret = inet_aton(optarg, &p->src);
187 fprintf(stderr, "Source IP address not recognized: %s\n",
194 ret = inet_aton(optarg, &p->dst);
196 fprintf(stderr, "Destination IP address not recognized: %s\n",
203 p->number = atoi(optarg);
214 return (options_found) ? 0 : -1;
217 volatile bool exit_flag = false;
225 timespec_subtract (result, x, y)
226 struct timespec *result, *x, *y;
228 /* Perform the carry for the later subtraction by updating Y. */
229 if (x->tv_nsec < y->tv_nsec) {
230 int nsec = (y->tv_nsec - x->tv_nsec) / 1000000000 + 1;
231 y->tv_nsec -= 1000000000 * nsec;
234 if (x->tv_nsec - y->tv_nsec > 1000000000) {
235 int nsec = (x->tv_nsec - y->tv_nsec) / 1000000000;
236 y->tv_nsec += 1000000000 * nsec;
240 /* Compute the time remaining to wait.
241 `tv_nsec' is certainly positive. */
242 result->tv_sec = x->tv_sec - y->tv_sec;
243 result->tv_nsec = x->tv_nsec - y->tv_nsec;
245 /* Return 1 if result is negative. */
246 return x->tv_sec < y->tv_sec;
249 static inline double ts2d(struct timespec *ts)
251 return ts->tv_sec + 1e-9*ts->tv_nsec;
254 static inline double tsdiff2d(struct timespec *x,
258 timespec_subtract(&r, x, y);
267 struct receiver_params {
269 frsh_receive_endpoint_t epdst;
273 void *receiver(void *arg)
275 struct receiver_params *rp = arg;
276 frsh_receive_endpoint_t epdst = rp->epdst;
280 struct timespec tss, tsr;
282 msg = malloc(rp->budget);
283 if (!msg) error(1, errno, "malloc msg");
286 ret = frsh_receive_sync(epdst, msg, rp->budget, &mlen, NULL);
287 clock_gettime(CLOCK_MONOTONIC, &tsr);
289 if (msg->cnt != last_cnt+1)
290 printf("packet(s) lost!\n");
291 printf("%3d: %10d: %10.3lf ms\n",
292 rp->id, msg->cnt, tsdiff2d(&tsr, &tss)*1000);
301 void *sender(void *arg)
303 struct stream_params *p = arg;
305 frsh_send_endpoint_t epsrc;
306 struct receiver_params *rp;
310 pthread_t receiver_id;
312 msg = malloc(p->budget);
313 if (!msg) error(1, errno, "malloc msg");
315 negotiate_contract(p, &vres);
317 rp = malloc(sizeof(*rp));
318 rp->budget = p->budget;
321 create_endpoints(p, vres, &epsrc, &rp->epdst);
325 ret = pthread_create(&receiver_id, NULL, receiver, rp);
327 if (signal(SIGTERM, stopper) == SIG_ERR)
328 error(1, errno, "Error in signal registration");
329 if (signal(SIGINT, stopper) == SIG_ERR)
330 error(1, errno, "Signal handler registration error");
332 struct timespec next_period;
334 clock_gettime(CLOCK_MONOTONIC, &next_period);
336 clock_gettime(CLOCK_MONOTONIC, &tss);
340 ret = frsh_send_async(epsrc, msg, p->budget);
342 ret = frsh_send_sync(epsrc, msg, p->budget);
343 clock_gettime(CLOCK_MONOTONIC, &next_period);
345 next_period.tv_sec += (p->period_ms/1000);
346 next_period.tv_nsec += (p->period_ms%1000) * 1000000;
347 if (next_period.tv_nsec >= 1000000000) {
348 next_period.tv_nsec -= 1000000000;
349 next_period.tv_sec++;
351 clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME,
355 frsh_contract_cancel(vres);
361 int main(int argc, char *argv[])
365 struct stream_params sp = {
369 .src.s_addr = htonl(INADDR_LOOPBACK),
370 .dst.s_addr = htonl(INADDR_LOOPBACK),
373 struct stream_params *p = NULL;
375 sem_init(&finished, 0, 0);
378 if (ret) PERROR_AND_EXIT(ret, "frsh_init");
381 ret = parse_opts(&argc, &argv, &sp);
382 if (num == 0 || ret == 0) {
384 for (i=0; i<sp.number; i++) {
386 p = malloc(sizeof(*p));
387 if (!p) error(1, errno, "malloc");
390 pthread_create(&thread, NULL, sender, p);