2 * lib/route/sch/netem.c Network Emulator Qdisc
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation version 2.1
9 * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
14 * @defgroup netem Network Emulator
17 * For further documentation see http://linux-net.osdl.org/index.php/Netem
21 #include <netlink-local.h>
22 #include <netlink-tc.h>
23 #include <netlink/netlink.h>
24 #include <netlink/utils.h>
25 #include <netlink/route/qdisc.h>
26 #include <netlink/route/qdisc-modules.h>
27 #include <netlink/route/sch/netem.h>
30 #define SCH_NETEM_ATTR_LATENCY 0x0001
31 #define SCH_NETEM_ATTR_LIMIT 0x0002
32 #define SCH_NETEM_ATTR_LOSS 0x0004
33 #define SCH_NETEM_ATTR_GAP 0x0008
34 #define SCH_NETEM_ATTR_DUPLICATE 0x0010
35 #define SCH_NETEM_ATTR_JITTER 0x0020
36 #define SCH_NETEM_ATTR_DELAY_CORR 0x0040
37 #define SCH_NETEM_ATTR_LOSS_CORR 0x0080
38 #define SCH_NETEM_ATTR_DUP_CORR 0x0100
39 #define SCH_NETEM_ATTR_RO_PROB 0x0200
40 #define SCH_NETEM_ATTR_RO_CORR 0x0400
41 #define SCH_NETEM_ATTR_CORRUPT_PROB 0x0800
42 #define SCH_NETEM_ATTR_CORRUPT_CORR 0x1000
43 #define SCH_NETEM_ATTR_DIST 0x2000
46 static inline struct rtnl_netem *netem_qdisc(struct rtnl_qdisc *qdisc)
48 return (struct rtnl_netem *) qdisc->q_subdata;
51 static inline struct rtnl_netem *netem_alloc(struct rtnl_qdisc *qdisc)
53 if (!qdisc->q_subdata)
54 qdisc->q_subdata = calloc(1, sizeof(struct rtnl_netem));
56 return netem_qdisc(qdisc);
59 static struct nla_policy netem_policy[TCA_NETEM_MAX+1] = {
60 [TCA_NETEM_CORR] = { .minlen = sizeof(struct tc_netem_corr) },
61 [TCA_NETEM_REORDER] = { .minlen = sizeof(struct tc_netem_reorder) },
62 [TCA_NETEM_CORRUPT] = { .minlen = sizeof(struct tc_netem_corrupt) },
65 static int netem_msg_parser(struct rtnl_qdisc *qdisc)
68 struct rtnl_netem *netem;
69 struct tc_netem_qopt *opts;
71 if (qdisc->q_opts->d_size < sizeof(*opts))
74 netem = netem_alloc(qdisc);
78 opts = (struct tc_netem_qopt *) qdisc->q_opts->d_data;
79 netem->qnm_latency = opts->latency;
80 netem->qnm_limit = opts->limit;
81 netem->qnm_loss = opts->loss;
82 netem->qnm_gap = opts->gap;
83 netem->qnm_duplicate = opts->duplicate;
84 netem->qnm_jitter = opts->jitter;
86 netem->qnm_mask = (SCH_NETEM_ATTR_LATENCY | SCH_NETEM_ATTR_LIMIT |
87 SCH_NETEM_ATTR_LOSS | SCH_NETEM_ATTR_GAP |
88 SCH_NETEM_ATTR_DUPLICATE | SCH_NETEM_ATTR_JITTER);
90 len = qdisc->q_opts->d_size - sizeof(*opts);
93 struct nlattr *tb[TCA_NETEM_MAX+1];
95 err = nla_parse(tb, TCA_NETEM_MAX, (struct nlattr *)
96 (qdisc->q_opts->d_data + sizeof(*opts)),
103 if (tb[TCA_NETEM_CORR]) {
104 struct tc_netem_corr cor;
106 nla_memcpy(&cor, tb[TCA_NETEM_CORR], sizeof(cor));
107 netem->qnm_corr.nmc_delay = cor.delay_corr;
108 netem->qnm_corr.nmc_loss = cor.loss_corr;
109 netem->qnm_corr.nmc_duplicate = cor.dup_corr;
111 netem->qnm_mask |= (SCH_NETEM_ATTR_DELAY_CORR |
112 SCH_NETEM_ATTR_LOSS_CORR |
113 SCH_NETEM_ATTR_DUP_CORR);
116 if (tb[TCA_NETEM_REORDER]) {
117 struct tc_netem_reorder ro;
119 nla_memcpy(&ro, tb[TCA_NETEM_REORDER], sizeof(ro));
120 netem->qnm_ro.nmro_probability = ro.probability;
121 netem->qnm_ro.nmro_correlation = ro.correlation;
123 netem->qnm_mask |= (SCH_NETEM_ATTR_RO_PROB |
124 SCH_NETEM_ATTR_RO_CORR);
127 if (tb[TCA_NETEM_CORRUPT]) {
128 struct tc_netem_corrupt corrupt;
130 nla_memcpy(&corrupt, tb[TCA_NETEM_CORRUPT], sizeof(corrupt));
131 netem->qnm_crpt.nmcr_probability = corrupt.probability;
132 netem->qnm_crpt.nmcr_correlation = corrupt.correlation;
134 netem->qnm_mask |= (SCH_NETEM_ATTR_CORRUPT_PROB |
135 SCH_NETEM_ATTR_CORRUPT_CORR);
138 /* sch_netem does not currently dump TCA_NETEM_DELAY_DIST */
139 netem->qnm_dist.dist_data = NULL;
140 netem->qnm_dist.dist_size = 0;
146 static void netem_free_data(struct rtnl_qdisc *qdisc)
148 struct rtnl_netem *netem;
150 if ( ! qdisc ) return;
152 netem = netem_qdisc(qdisc);
153 if ( ! netem ) return;
155 if ( netem->qnm_dist.dist_data )
156 free(netem->qnm_dist.dist_data);
160 free (qdisc->q_subdata);
163 static void netem_dump_line(struct rtnl_qdisc *qdisc, struct nl_dump_params *p)
165 struct rtnl_netem *netem = netem_qdisc(qdisc);
168 nl_dump(p, "limit %d", netem->qnm_limit);
171 int netem_build_msg(struct rtnl_qdisc *qdisc, struct nl_msg *msg)
174 struct tc_netem_qopt opts;
175 struct tc_netem_corr cor;
176 struct tc_netem_reorder reorder;
177 struct tc_netem_corrupt corrupt;
178 struct rtnl_netem *netem;
180 unsigned char set_correlation = 0, set_reorder = 0,
181 set_corrupt = 0, set_dist = 0;
183 memset(&opts, 0, sizeof(opts));
184 memset(&cor, 0, sizeof(cor));
185 memset(&reorder, 0, sizeof(reorder));
186 memset(&corrupt, 0, sizeof(corrupt));
188 netem = netem_qdisc(qdisc);
192 msg->nm_nlh->nlmsg_flags |= NLM_F_REQUEST;
194 if ( netem->qnm_ro.nmro_probability != 0 ) {
195 if (netem->qnm_latency == 0) {
196 return -NLE_MISSING_ATTR;
198 if (netem->qnm_gap == 0) netem->qnm_gap = 1;
200 else if ( netem->qnm_gap ) {
201 return -NLE_MISSING_ATTR;
204 if ( netem->qnm_corr.nmc_delay != 0 ) {
205 if ( netem->qnm_latency == 0 || netem->qnm_jitter == 0) {
206 return -NLE_MISSING_ATTR;
211 if ( netem->qnm_corr.nmc_loss != 0 ) {
212 if ( netem->qnm_loss == 0 ) {
213 return -NLE_MISSING_ATTR;
218 if ( netem->qnm_corr.nmc_duplicate != 0 ) {
219 if ( netem->qnm_duplicate == 0 ) {
220 return -NLE_MISSING_ATTR;
225 if ( netem->qnm_ro.nmro_probability != 0 ) set_reorder = 1;
226 else if ( netem->qnm_ro.nmro_correlation != 0 ) {
227 return -NLE_MISSING_ATTR;
230 if ( netem->qnm_crpt.nmcr_probability != 0 ) set_corrupt = 1;
231 else if ( netem->qnm_crpt.nmcr_correlation != 0 ) {
232 return -NLE_MISSING_ATTR;
235 if ( netem->qnm_dist.dist_data && netem->qnm_dist.dist_size ) {
236 if (netem->qnm_latency == 0 || netem->qnm_jitter == 0) {
237 return -NLE_MISSING_ATTR;
240 /* Resize to accomodate the large distribution table */
241 int new_msg_len = msg->nm_size + netem->qnm_dist.dist_size *
242 sizeof(netem->qnm_dist.dist_data[0]);
244 msg->nm_nlh = (struct nlmsghdr *) realloc(msg->nm_nlh, new_msg_len);
245 if ( msg->nm_nlh == NULL )
247 msg->nm_size = new_msg_len;
252 opts.latency = netem->qnm_latency;
253 opts.limit = netem->qnm_limit ? netem->qnm_limit : 1000;
254 opts.loss = netem->qnm_loss;
255 opts.gap = netem->qnm_gap;
256 opts.duplicate = netem->qnm_duplicate;
257 opts.jitter = netem->qnm_jitter;
259 NLA_PUT(msg, TCA_OPTIONS, sizeof(opts), &opts);
261 if ( set_correlation ) {
262 cor.delay_corr = netem->qnm_corr.nmc_delay;
263 cor.loss_corr = netem->qnm_corr.nmc_loss;
264 cor.dup_corr = netem->qnm_corr.nmc_duplicate;
266 NLA_PUT(msg, TCA_NETEM_CORR, sizeof(cor), &cor);
270 reorder.probability = netem->qnm_ro.nmro_probability;
271 reorder.correlation = netem->qnm_ro.nmro_correlation;
273 NLA_PUT(msg, TCA_NETEM_REORDER, sizeof(reorder), &reorder);
277 corrupt.probability = netem->qnm_crpt.nmcr_probability;
278 corrupt.correlation = netem->qnm_crpt.nmcr_correlation;
280 NLA_PUT(msg, TCA_NETEM_CORRUPT, sizeof(corrupt), &corrupt);
284 NLA_PUT(msg, TCA_NETEM_DELAY_DIST,
285 netem->qnm_dist.dist_size * sizeof(netem->qnm_dist.dist_data[0]),
286 netem->qnm_dist.dist_data);
289 /* Length specified in the TCA_OPTIONS section must span the entire
290 * remainder of the message. That's just the way that sch_netem expects it.
291 * Maybe there's a more succinct way to do this at a higher level.
293 struct nlattr* head = (struct nlattr *)(NLMSG_DATA(msg->nm_nlh) +
294 NLMSG_LENGTH(sizeof(struct tcmsg)) - NLMSG_ALIGNTO);
296 struct nlattr* tail = (struct nlattr *)(((void *) (msg->nm_nlh)) +
297 NLMSG_ALIGN(msg->nm_nlh->nlmsg_len));
299 int old_len = head->nla_len;
300 head->nla_len = (void *)tail - (void *)head;
301 msg->nm_nlh->nlmsg_len += (head->nla_len - old_len);
314 * Set limit of netem qdisc.
315 * @arg qdisc Netem qdisc to be modified.
316 * @arg limit New limit in bytes.
317 * @return 0 on success or a negative error code.
319 int rtnl_netem_set_limit(struct rtnl_qdisc *qdisc, int limit)
321 struct rtnl_netem *netem;
323 netem = netem_alloc(qdisc);
327 netem->qnm_limit = limit;
328 netem->qnm_mask |= SCH_NETEM_ATTR_LIMIT;
334 * Get limit of netem qdisc.
335 * @arg qdisc Netem qdisc.
336 * @return Limit in bytes or a negative error code.
338 int rtnl_netem_get_limit(struct rtnl_qdisc *qdisc)
340 struct rtnl_netem *netem;
342 netem = netem_qdisc(qdisc);
343 if (netem && (netem->qnm_mask & SCH_NETEM_ATTR_LIMIT))
344 return netem->qnm_limit;
352 * @name Packet Re-ordering
357 * Set re-ordering gap of netem qdisc.
358 * @arg qdisc Netem qdisc to be modified.
359 * @arg gap New gap in number of packets.
360 * @return 0 on success or a negative error code.
362 int rtnl_netem_set_gap(struct rtnl_qdisc *qdisc, int gap)
364 struct rtnl_netem *netem;
366 netem = netem_alloc(qdisc);
370 netem->qnm_gap = gap;
371 netem->qnm_mask |= SCH_NETEM_ATTR_GAP;
377 * Get re-ordering gap of netem qdisc.
378 * @arg qdisc Netem qdisc.
379 * @return Re-ordering gap in packets or a negative error code.
381 int rtnl_netem_get_gap(struct rtnl_qdisc *qdisc)
383 struct rtnl_netem *netem;
385 netem = netem_qdisc(qdisc);
386 if (netem && (netem->qnm_mask & SCH_NETEM_ATTR_GAP))
387 return netem->qnm_gap;
393 * Set re-ordering probability of netem qdisc.
394 * @arg qdisc Netem qdisc to be modified.
395 * @arg prob New re-ordering probability.
396 * @return 0 on success or a negative error code.
398 int rtnl_netem_set_reorder_probability(struct rtnl_qdisc *qdisc, int prob)
400 struct rtnl_netem *netem;
402 netem = netem_alloc(qdisc);
406 netem->qnm_ro.nmro_probability = prob;
407 netem->qnm_mask |= SCH_NETEM_ATTR_RO_PROB;
413 * Get re-ordering probability of netem qdisc.
414 * @arg qdisc Netem qdisc.
415 * @return Re-ordering probability or a negative error code.
417 int rtnl_netem_get_reorder_probability(struct rtnl_qdisc *qdisc)
419 struct rtnl_netem *netem;
421 netem = netem_qdisc(qdisc);
422 if (netem && (netem->qnm_mask & SCH_NETEM_ATTR_RO_PROB))
423 return netem->qnm_ro.nmro_probability;
429 * Set re-order correlation probability of netem qdisc.
430 * @arg qdisc Netem qdisc to be modified.
431 * @arg prob New re-ordering correlation probability.
432 * @return 0 on success or a negative error code.
434 int rtnl_netem_set_reorder_correlation(struct rtnl_qdisc *qdisc, int prob)
436 struct rtnl_netem *netem;
438 netem = netem_alloc(qdisc);
442 netem->qnm_ro.nmro_correlation = prob;
443 netem->qnm_mask |= SCH_NETEM_ATTR_RO_CORR;
449 * Get re-ordering correlation probability of netem qdisc.
450 * @arg qdisc Netem qdisc.
451 * @return Re-ordering correlation probability or a negative error code.
453 int rtnl_netem_get_reorder_correlation(struct rtnl_qdisc *qdisc)
455 struct rtnl_netem *netem;
457 netem = netem_qdisc(qdisc);
458 if (netem && (netem->qnm_mask & SCH_NETEM_ATTR_RO_CORR))
459 return netem->qnm_ro.nmro_correlation;
472 * Set corruption probability of netem qdisc.
473 * @arg qdisc Netem qdisc to be modified.
474 * @arg prob New corruption probability.
475 * @return 0 on success or a negative error code.
477 int rtnl_netem_set_corruption_probability(struct rtnl_qdisc *qdisc, int prob)
479 struct rtnl_netem *netem;
481 netem = netem_alloc(qdisc);
485 netem->qnm_crpt.nmcr_probability = prob;
486 netem->qnm_mask |= SCH_NETEM_ATTR_CORRUPT_PROB;
492 * Get corruption probability of netem qdisc.
493 * @arg qdisc Netem qdisc.
494 * @return Corruption probability or a negative error code.
496 int rtnl_netem_get_corruption_probability(struct rtnl_qdisc *qdisc)
498 struct rtnl_netem *netem;
500 netem = netem_qdisc(qdisc);
501 if (netem && (netem->qnm_mask & SCH_NETEM_ATTR_CORRUPT_PROB))
502 return netem->qnm_crpt.nmcr_probability;
508 * Set corruption correlation probability of netem qdisc.
509 * @arg qdisc Netem qdisc to be modified.
510 * @arg prob New corruption correlation probability.
511 * @return 0 on success or a negative error code.
513 int rtnl_netem_set_corruption_correlation(struct rtnl_qdisc *qdisc, int prob)
515 struct rtnl_netem *netem;
517 netem = netem_alloc(qdisc);
521 netem->qnm_crpt.nmcr_correlation = prob;
522 netem->qnm_mask |= SCH_NETEM_ATTR_CORRUPT_CORR;
528 * Get corruption correlation probability of netem qdisc.
529 * @arg qdisc Netem qdisc.
530 * @return Corruption correlation probability or a negative error code.
532 int rtnl_netem_get_corruption_correlation(struct rtnl_qdisc *qdisc)
534 struct rtnl_netem *netem;
536 netem = netem_qdisc(qdisc);
537 if (netem && (netem->qnm_mask & SCH_NETEM_ATTR_CORRUPT_CORR))
538 return netem->qnm_crpt.nmcr_correlation;
551 * Set packet loss probability of netem qdisc.
552 * @arg qdisc Netem qdisc to be modified.
553 * @arg prob New packet loss probability.
554 * @return 0 on success or a negative error code.
556 int rtnl_netem_set_loss(struct rtnl_qdisc *qdisc, int prob)
558 struct rtnl_netem *netem;
560 netem = netem_alloc(qdisc);
564 netem->qnm_loss = prob;
565 netem->qnm_mask |= SCH_NETEM_ATTR_LOSS;
571 * Get packet loss probability of netem qdisc.
572 * @arg qdisc Netem qdisc.
573 * @return Packet loss probability or a negative error code.
575 int rtnl_netem_get_loss(struct rtnl_qdisc *qdisc)
577 struct rtnl_netem *netem;
579 netem = netem_qdisc(qdisc);
580 if (netem && (netem->qnm_mask & SCH_NETEM_ATTR_LOSS))
581 return netem->qnm_loss;
587 * Set packet loss correlation probability of netem qdisc.
588 * @arg qdisc Netem qdisc to be modified.
589 * @arg prob New packet loss correlation.
590 * @return 0 on success or a negative error code.
592 int rtnl_netem_set_loss_correlation(struct rtnl_qdisc *qdisc, int prob)
594 struct rtnl_netem *netem;
596 netem = netem_alloc(qdisc);
600 netem->qnm_corr.nmc_loss = prob;
601 netem->qnm_mask |= SCH_NETEM_ATTR_LOSS_CORR;
607 * Get packet loss correlation probability of netem qdisc.
608 * @arg qdisc Netem qdisc.
609 * @return Packet loss correlation probability or a negative error code.
611 int rtnl_netem_get_loss_correlation(struct rtnl_qdisc *qdisc)
613 struct rtnl_netem *netem;
615 netem = netem_qdisc(qdisc);
616 if (netem && (netem->qnm_mask & SCH_NETEM_ATTR_LOSS_CORR))
617 return netem->qnm_corr.nmc_loss;
625 * @name Packet Duplication
630 * Set packet duplication probability of netem qdisc.
631 * @arg qdisc Netem qdisc to be modified.
632 * @arg prob New packet duplication probability.
633 * @return 0 on success or a negative error code.
635 int rtnl_netem_set_duplicate(struct rtnl_qdisc *qdisc, int prob)
637 struct rtnl_netem *netem;
639 netem = netem_alloc(qdisc);
643 netem->qnm_duplicate = prob;
644 netem->qnm_mask |= SCH_NETEM_ATTR_DUPLICATE;
650 * Get packet duplication probability of netem qdisc.
651 * @arg qdisc Netem qdisc.
652 * @return Packet duplication probability or a negative error code.
654 int rtnl_netem_get_duplicate(struct rtnl_qdisc *qdisc)
656 struct rtnl_netem *netem;
658 netem = netem_qdisc(qdisc);
659 if (netem && (netem->qnm_mask & SCH_NETEM_ATTR_DUPLICATE))
660 return netem->qnm_duplicate;
666 * Set packet duplication correlation probability of netem qdisc.
667 * @arg qdisc Netem qdisc to be modified.
668 * @arg prob New packet duplication correlation probability.
669 * @return 0 on sucess or a negative error code.
671 int rtnl_netem_set_duplicate_correlation(struct rtnl_qdisc *qdisc, int prob)
673 struct rtnl_netem *netem;
675 netem = netem_alloc(qdisc);
679 netem->qnm_corr.nmc_duplicate = prob;
680 netem->qnm_mask |= SCH_NETEM_ATTR_DUP_CORR;
686 * Get packet duplication correlation probability of netem qdisc.
687 * @arg qdisc Netem qdisc.
688 * @return Packet duplication correlation probability or a negative error code.
690 int rtnl_netem_get_duplicate_correlation(struct rtnl_qdisc *qdisc)
692 struct rtnl_netem *netem;
694 netem = netem_qdisc(qdisc);
695 if (netem && (netem->qnm_mask & SCH_NETEM_ATTR_DUP_CORR))
696 return netem->qnm_corr.nmc_duplicate;
709 * Set packet delay of netem qdisc.
710 * @arg qdisc Netem qdisc to be modified.
711 * @arg delay New packet delay in micro seconds.
712 * @return 0 on success or a negative error code.
714 int rtnl_netem_set_delay(struct rtnl_qdisc *qdisc, int delay)
716 struct rtnl_netem *netem;
718 netem = netem_alloc(qdisc);
722 netem->qnm_latency = nl_us2ticks(delay);
723 netem->qnm_mask |= SCH_NETEM_ATTR_LATENCY;
729 * Get packet delay of netem qdisc.
730 * @arg qdisc Netem qdisc.
731 * @return Packet delay in micro seconds or a negative error code.
733 int rtnl_netem_get_delay(struct rtnl_qdisc *qdisc)
735 struct rtnl_netem *netem;
737 netem = netem_qdisc(qdisc);
738 if (netem && (netem->qnm_mask & SCH_NETEM_ATTR_LATENCY))
739 return nl_ticks2us(netem->qnm_latency);
745 * Set packet delay jitter of netem qdisc.
746 * @arg qdisc Netem qdisc to be modified.
747 * @arg jitter New packet delay jitter in micro seconds.
748 * @return 0 on success or a negative error code.
750 int rtnl_netem_set_jitter(struct rtnl_qdisc *qdisc, int jitter)
752 struct rtnl_netem *netem;
754 netem = netem_alloc(qdisc);
758 netem->qnm_jitter = nl_us2ticks(jitter);
759 netem->qnm_mask |= SCH_NETEM_ATTR_JITTER;
765 * Get packet delay jitter of netem qdisc.
766 * @arg qdisc Netem qdisc.
767 * @return Packet delay jitter in micro seconds or a negative error code.
769 int rtnl_netem_get_jitter(struct rtnl_qdisc *qdisc)
771 struct rtnl_netem *netem;
773 netem = netem_qdisc(qdisc);
774 if (netem && (netem->qnm_mask & SCH_NETEM_ATTR_JITTER))
775 return nl_ticks2us(netem->qnm_jitter);
781 * Set packet delay correlation probability of netem qdisc.
782 * @arg qdisc Netem qdisc to be modified.
783 * @arg prob New packet delay correlation probability.
785 int rtnl_netem_set_delay_correlation(struct rtnl_qdisc *qdisc, int prob)
787 struct rtnl_netem *netem;
789 netem = netem_alloc(qdisc);
793 netem->qnm_corr.nmc_delay = prob;
794 netem->qnm_mask |= SCH_NETEM_ATTR_DELAY_CORR;
800 * Get packet delay correlation probability of netem qdisc.
801 * @arg qdisc Netem qdisc.
802 * @return Packet delay correlation probability or a negative error code.
804 int rtnl_netem_get_delay_correlation(struct rtnl_qdisc *qdisc)
806 struct rtnl_netem *netem;
808 netem = netem_qdisc(qdisc);
809 if (netem && (netem->qnm_mask & SCH_NETEM_ATTR_DELAY_CORR))
810 return netem->qnm_corr.nmc_delay;
816 * Get the size of the distribution table.
817 * @arg qdisc Netem qdisc.
818 * @return Distribution table size or a negative error code.
820 int rtnl_netem_get_delay_distribution_size(struct rtnl_qdisc *qdisc)
822 struct rtnl_netem *netem;
824 netem = netem_qdisc(qdisc);
825 if (netem && (netem->qnm_mask & SCH_NETEM_ATTR_DIST))
826 return netem->qnm_dist.dist_size;
832 * Get a pointer to the distribution table.
833 * @arg qdisc Netem qdisc.
834 * @arg dist_ptr The pointer to set.
835 * @return Negative error code on failure or 0 on success.
837 int rtnl_netem_get_delay_distribution(struct rtnl_qdisc *qdisc, int16_t **dist_ptr)
839 struct rtnl_netem *netem;
841 netem = netem_qdisc(qdisc);
842 if (netem && (netem->qnm_mask & SCH_NETEM_ATTR_DIST)) {
843 *dist_ptr = netem->qnm_dist.dist_data;
851 * Set the delay distribution. Latency/jitter must be set before applying.
852 * @arg qdisc Netem qdisc.
853 * @arg dist_type The name of the distribution (type, file, path/file).
854 * @return 0 on success, error code on failure.
856 int rtnl_netem_set_delay_distribution(struct rtnl_qdisc *qdisc, const char *dist_type) {
857 struct rtnl_netem *netem;
859 netem = netem_alloc(qdisc);
868 char dist_suffix[] = ".dist";
870 /* If the given filename already ends in .dist, don't append it later */
871 char *test_suffix = strstr(dist_type, dist_suffix);
872 if (test_suffix != NULL && strlen(test_suffix) == 5)
873 strcpy(dist_suffix, "");
875 /* Check several locations for the dist file */
876 char *test_path[] = { "", "./", "/usr/lib/tc/", "/usr/local/lib/tc/" };
878 for (i = 0; i < sizeof(test_path) && f == NULL; i++) {
879 snprintf(name, NAME_MAX, "%s%s%s", test_path[i], dist_type, dist_suffix);
880 f = fopen(name, "r");
884 return -nl_syserr2nlerr(errno);
886 netem->qnm_dist.dist_data = (int16_t *) calloc (MAXDIST, sizeof(int16_t));
888 line = (char *) calloc (sizeof(char), len + 1);
890 while (getline(&line, &len, f) != -1) {
893 if (*line == '\n' || *line == '#')
896 for (p = line; ; p = endp) {
897 long x = strtol(p, &endp, 0);
898 if (endp == p) break;
905 netem->qnm_dist.dist_data[n++] = x;
911 netem->qnm_dist.dist_size = n;
912 netem->qnm_mask |= SCH_NETEM_ATTR_DIST;
920 static struct rtnl_qdisc_ops netem_ops = {
922 .qo_msg_parser = netem_msg_parser,
923 .qo_free_data = netem_free_data,
924 .qo_dump[NL_DUMP_LINE] = netem_dump_line,
926 .qo_build_msg = netem_build_msg
929 static void __init netem_init(void)
931 rtnl_qdisc_register(&netem_ops);
934 static void __exit netem_exit(void)
936 rtnl_qdisc_unregister(&netem_ops);