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>
25 int opt_budget = 1024;
28 struct in_addr src, dst;
30 void set_rt_prio(int priority)
33 static struct sched_param param;
35 if ((maxpri = sched_get_priority_max(SCHED_FIFO)) == -1) {
36 fprintf(stderr, "warning: sched_get_priority_max failed");
39 if ((minpri = sched_get_priority_min(SCHED_FIFO)) == -1) {
40 fprintf(stderr, "warning: sched_get_priority_min failed");
43 if (priority > maxpri) {
44 fprintf(stderr, "warning: maximum priority allowed is %d.\n", maxpri);
46 if (priority < minpri) {
47 fprintf(stderr, "warning: minimum priority allowed is %d.\n", minpri);
50 param.sched_priority = priority;
52 if (sched_setscheduler(0, SCHED_FIFO, ¶m) == -1) {
53 fprintf(stderr, "warning: sched_setscheduler failed");
56 mlockall(MCL_CURRENT | MCL_FUTURE);
60 int negotiate_contract(frsh_vres_id_t *vres)
62 frsh_contract_t contract;
64 frsh_rel_time_t period;
65 frsh_rel_time_t budget;
66 frsh_rel_time_t deadline;
69 ret = frsh_contract_init(&contract);
70 if (ret) PERROR_AND_EXIT(ret, "frsh_contract_init");
72 ret = frsh_contract_set_resource_and_label(
74 FRSH_RT_NETWORK, FRSH_NETPF_FWP,
76 if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_resource_and_label");
78 frsh_network_bytes_to_budget(FRSH_NETPF_FWP, opt_budget, &budget);
79 period = fosa_msec_to_rel_time(opt_period);
80 ret = frsh_contract_set_basic_params(&contract,
85 if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_basic_params");
88 /* FWP doesn't accept smaller deadlines than 30 ms. */
89 if (frsh_rel_time_smaller(period, frsh_msec_to_rel_time(30)))
90 deadline = frsh_msec_to_rel_time(30);
93 ret = frsh_contract_set_timing_reqs(&contract, false, &deadline);
94 if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_timing_reqs");
96 fwp = malloc(sizeof(*fwp));
97 if (!fwp) PERROR_AND_EXIT(errno, "malloc");
98 fwp->src = src.s_addr;
99 ret = fres_contract_add_fwp(contract, fwp);
100 if (ret) PERROR_AND_EXIT(ret, "fres_contract_add_fwp");
102 ret = frsh_contract_negotiate(&contract, vres);
103 if (ret) PERROR_AND_EXIT(ret, "frsh_contract_negotiate");
105 frsh_contract_destroy(&contract);
110 void create_endpoints(frsh_vres_id_t vres,
111 frsh_send_endpoint_t *epsrc,
112 frsh_receive_endpoint_t *epdst)
115 frsh_send_endpoint_protocol_info_t spi = { NULL, 0 };
116 frsh_receive_endpoint_protocol_info_t rpi = { NULL, 0 };
117 frsh_endpoint_queueing_info_t qi = { .queue_size=0,
118 .queue_policy=FRSH_QRP_OLDEST };
120 ret = frsh_receive_endpoint_create(FRSH_NETPF_FWP, 0, qi, rpi,
122 if (ret != 0) error(1, errno, "fwp_receive_endpoint_create");
125 frsh_receive_endpoint_get_params(*epdst, NULL, &port, NULL, NULL);
127 ret = frsh_send_endpoint_create(FRSH_NETPF_FWP,
128 dst.s_addr, port, spi,
130 if (ret < 0) error(1, errno, "frsh_send_endpoint_create()");
132 ret = frsh_send_endpoint_bind(vres, *epsrc);
133 if (ret != 0) error(1, errno, "frsh_send_endpoint_bind");
137 static struct option long_opts[] = {
138 { "period", 1, 0, 'p' },
139 { "budget", 1, 0, 'b' },
140 { "source", 1, 0, 's' },
141 { "dest", 1, 0, 'd' },
148 printf("usage: fwp-timing [ options ]\n");
149 printf(" -p, --period <ms> period in miliseconds\n");
150 printf(" -b, --budget <bytes> how many bytes is sent in each period\n");
151 printf(" -s, --source <ip> source IP address\n");
152 printf(" -d, --dest <ip:port> destination IP address and port\n");
155 void parse_opts(int argc, char *argv[])
160 while ((opt = getopt_long(argc, argv, "b:p:s:d:", long_opts, NULL)) != -1) {
163 opt_budget = atoi(optarg);
166 opt_period = atoi(optarg);
169 ret = inet_aton(optarg, &src);
171 fprintf(stderr, "Source IP address not recognized: %s\n",
178 ret = inet_aton(optarg, &dst);
180 fprintf(stderr, "Destination IP address not recognized: %s\n",
193 volatile bool exit_flag = false;
201 timespec_subtract (result, x, y)
202 struct timespec *result, *x, *y;
204 /* Perform the carry for the later subtraction by updating Y. */
205 if (x->tv_nsec < y->tv_nsec) {
206 int nsec = (y->tv_nsec - x->tv_nsec) / 1000000000 + 1;
207 y->tv_nsec -= 1000000000 * nsec;
210 if (x->tv_nsec - y->tv_nsec > 1000000000) {
211 int nsec = (x->tv_nsec - y->tv_nsec) / 1000000000;
212 y->tv_nsec += 1000000000 * nsec;
216 /* Compute the time remaining to wait.
217 `tv_nsec' is certainly positive. */
218 result->tv_sec = x->tv_sec - y->tv_sec;
219 result->tv_nsec = x->tv_nsec - y->tv_nsec;
221 /* Return 1 if result is negative. */
222 return x->tv_sec < y->tv_sec;
225 static inline double ts2d(struct timespec *ts)
227 return ts->tv_sec + 1e-9*ts->tv_nsec;
230 static inline double tsdiff2d(struct timespec *x,
234 timespec_subtract(&r, x, y);
243 void *receiver(void *arg)
245 frsh_receive_endpoint_t epdst = (frsh_receive_endpoint_t)arg;
248 struct timespec tss, tsr;
250 msg = malloc(opt_budget);
251 if (!msg) error(1, errno, "malloc msg");
254 ret = frsh_receive_sync(epdst, msg, opt_budget, &mlen, NULL);
255 clock_gettime(CLOCK_MONOTONIC, &tsr);
257 printf("%10d: %10.3lf ms\n",
258 msg->cnt, tsdiff2d(&tsr, &tss)*1000);
266 frsh_send_endpoint_t epsrc;
267 frsh_receive_endpoint_t epdst;
271 pthread_t receiver_id;
273 msg = malloc(opt_budget);
274 if (!msg) error(1, errno, "malloc msg");
277 if (ret) PERROR_AND_EXIT(ret, "frsh_init");
279 negotiate_contract(&vres);
280 create_endpoints(vres, &epsrc, &epdst);
284 ret = pthread_create(&receiver_id, NULL, receiver, epdst);
286 if (signal(SIGTERM, stopper) == SIG_ERR)
287 error(1, errno, "Error in signal registration");
288 if (signal(SIGINT, stopper) == SIG_ERR)
289 error(1, errno, "Signal handler registration error");
291 struct timespec next_period, tss;
292 clock_gettime(CLOCK_MONOTONIC, &next_period);
294 clock_gettime(CLOCK_MONOTONIC, &tss);
297 ret = frsh_send_async(epsrc, msg, opt_budget);
299 next_period.tv_nsec += opt_period * 1000000;
300 if (next_period.tv_nsec >= 1000000000) {
301 next_period.tv_nsec -= 1000000000;
302 next_period.tv_sec++;
305 clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME,
309 frsh_contract_cancel(vres);
312 int main(int argc, char *argv[])
314 src.s_addr = htonl(INADDR_LOOPBACK);
315 dst.s_addr = htonl(INADDR_LOOPBACK);
317 parse_opts(argc, argv);