15#include <netlink-private/netlink.h>
16#include <netlink-private/tc.h>
17#include <netlink/netlink.h>
18#include <netlink/utils.h>
19#include <netlink-private/route/tc-api.h>
20#include <netlink/route/qdisc.h>
21#include <netlink/route/qdisc/netem.h>
23#include "netlink-private/utils.h"
26#define SCH_NETEM_ATTR_LATENCY 0x0001
27#define SCH_NETEM_ATTR_LIMIT 0x0002
28#define SCH_NETEM_ATTR_LOSS 0x0004
29#define SCH_NETEM_ATTR_GAP 0x0008
30#define SCH_NETEM_ATTR_DUPLICATE 0x0010
31#define SCH_NETEM_ATTR_JITTER 0x0020
32#define SCH_NETEM_ATTR_DELAY_CORR 0x0040
33#define SCH_NETEM_ATTR_LOSS_CORR 0x0080
34#define SCH_NETEM_ATTR_DUP_CORR 0x0100
35#define SCH_NETEM_ATTR_RO_PROB 0x0200
36#define SCH_NETEM_ATTR_RO_CORR 0x0400
37#define SCH_NETEM_ATTR_CORRUPT_PROB 0x0800
38#define SCH_NETEM_ATTR_CORRUPT_CORR 0x1000
39#define SCH_NETEM_ATTR_DIST 0x2000
42static struct nla_policy netem_policy[TCA_NETEM_MAX+1] = {
43 [TCA_NETEM_CORR] = { .
minlen =
sizeof(
struct tc_netem_corr) },
44 [TCA_NETEM_REORDER] = { .minlen =
sizeof(
struct tc_netem_reorder) },
45 [TCA_NETEM_CORRUPT] = { .minlen =
sizeof(
struct tc_netem_corrupt) },
48static int netem_msg_parser(
struct rtnl_tc *tc,
void *data)
50 struct rtnl_netem *netem = data;
51 struct tc_netem_qopt *opts;
54 if (tc->tc_opts->d_size <
sizeof(*opts))
57 opts = (
struct tc_netem_qopt *) tc->tc_opts->d_data;
58 netem->qnm_latency = opts->latency;
59 netem->qnm_limit = opts->limit;
60 netem->qnm_loss = opts->loss;
61 netem->qnm_gap = opts->gap;
62 netem->qnm_duplicate = opts->duplicate;
63 netem->qnm_jitter = opts->jitter;
65 netem->qnm_mask = (SCH_NETEM_ATTR_LATENCY | SCH_NETEM_ATTR_LIMIT |
66 SCH_NETEM_ATTR_LOSS | SCH_NETEM_ATTR_GAP |
67 SCH_NETEM_ATTR_DUPLICATE | SCH_NETEM_ATTR_JITTER);
69 len = tc->tc_opts->d_size -
sizeof(*opts);
72 struct nlattr *tb[TCA_NETEM_MAX+1];
74 err =
nla_parse(tb, TCA_NETEM_MAX, (
struct nlattr *)
75 ((
char *) tc->tc_opts->d_data +
sizeof(*opts)),
82 if (tb[TCA_NETEM_CORR]) {
83 struct tc_netem_corr cor;
85 nla_memcpy(&cor, tb[TCA_NETEM_CORR],
sizeof(cor));
86 netem->qnm_corr.nmc_delay = cor.delay_corr;
87 netem->qnm_corr.nmc_loss = cor.loss_corr;
88 netem->qnm_corr.nmc_duplicate = cor.dup_corr;
90 netem->qnm_mask |= (SCH_NETEM_ATTR_DELAY_CORR |
91 SCH_NETEM_ATTR_LOSS_CORR |
92 SCH_NETEM_ATTR_DUP_CORR);
95 if (tb[TCA_NETEM_REORDER]) {
96 struct tc_netem_reorder ro;
98 nla_memcpy(&ro, tb[TCA_NETEM_REORDER],
sizeof(ro));
99 netem->qnm_ro.nmro_probability = ro.probability;
100 netem->qnm_ro.nmro_correlation = ro.correlation;
102 netem->qnm_mask |= (SCH_NETEM_ATTR_RO_PROB |
103 SCH_NETEM_ATTR_RO_CORR);
106 if (tb[TCA_NETEM_CORRUPT]) {
107 struct tc_netem_corrupt corrupt;
109 nla_memcpy(&corrupt, tb[TCA_NETEM_CORRUPT],
sizeof(corrupt));
110 netem->qnm_crpt.nmcr_probability = corrupt.probability;
111 netem->qnm_crpt.nmcr_correlation = corrupt.correlation;
113 netem->qnm_mask |= (SCH_NETEM_ATTR_CORRUPT_PROB |
114 SCH_NETEM_ATTR_CORRUPT_CORR);
118 netem->qnm_dist.dist_data = NULL;
119 netem->qnm_dist.dist_size = 0;
125static void netem_free_data(
struct rtnl_tc *tc,
void *data)
127 struct rtnl_netem *netem = data;
132 free(netem->qnm_dist.dist_data);
135static void netem_dump_line(
struct rtnl_tc *tc,
void *data,
138 struct rtnl_netem *netem = data;
141 if (netem->qnm_mask & SCH_NETEM_ATTR_LIMIT && netem->qnm_limit > 0)
142 nl_dump(p,
" limit %dpkts", netem->qnm_limit);
148static void netem_dump_details(
struct rtnl_tc *tc,
void *data,
151 struct rtnl_netem *netem = data;
155 if (netem->qnm_mask & SCH_NETEM_ATTR_LATENCY && netem->qnm_latency > 0) {
157 nl_dump(p,
" latency %s", buf);
159 if (netem->qnm_mask & SCH_NETEM_ATTR_JITTER && netem->qnm_jitter > 0) {
163 if (netem->qnm_mask & SCH_NETEM_ATTR_DELAY_CORR && netem->qnm_corr.nmc_delay > 0)
164 nl_dump(p,
" %d", netem->qnm_corr.nmc_delay);
168 if (netem->qnm_mask & SCH_NETEM_ATTR_LOSS && netem->qnm_loss > 0) {
169 nl_dump(p,
" loss %d", netem->qnm_loss);
171 if (netem->qnm_mask & SCH_NETEM_ATTR_LOSS_CORR && netem->qnm_corr.nmc_loss > 0)
172 nl_dump(p,
" %d", netem->qnm_corr.nmc_loss);
175 if (netem->qnm_mask & SCH_NETEM_ATTR_DUPLICATE && netem->qnm_duplicate > 0) {
176 nl_dump(p,
" duplicate %d", netem->qnm_duplicate);
178 if (netem->qnm_mask & SCH_NETEM_ATTR_DUP_CORR && netem->qnm_corr.nmc_duplicate > 0)
179 nl_dump(p,
" %d", netem->qnm_corr.nmc_duplicate);
182 if (netem->qnm_mask & SCH_NETEM_ATTR_RO_PROB && netem->qnm_ro.nmro_probability > 0) {
183 nl_dump(p,
" reorder %d", netem->qnm_ro.nmro_probability);
185 if (netem->qnm_mask & SCH_NETEM_ATTR_RO_CORR && netem->qnm_ro.nmro_correlation > 0)
186 nl_dump(p,
" %d", netem->qnm_ro.nmro_correlation);
188 if (netem->qnm_mask & SCH_NETEM_ATTR_GAP && netem->qnm_gap > 0)
189 nl_dump(p,
" gap %d", netem->qnm_gap);
192 if (netem->qnm_mask & SCH_NETEM_ATTR_CORRUPT_PROB && netem->qnm_crpt.nmcr_probability > 0) {
193 nl_dump(p,
" reorder %d", netem->qnm_crpt.nmcr_probability);
195 if (netem->qnm_mask & SCH_NETEM_ATTR_CORRUPT_CORR && netem->qnm_crpt.nmcr_correlation > 0)
196 nl_dump(p,
" %d", netem->qnm_crpt.nmcr_correlation);
201static int netem_msg_fill_raw(
struct rtnl_tc *tc,
void *data,
205 struct tc_netem_qopt opts;
206 struct tc_netem_corr cor;
207 struct tc_netem_reorder reorder;
208 struct tc_netem_corrupt corrupt;
209 struct rtnl_netem *netem = data;
211 unsigned char set_correlation = 0, set_reorder = 0;
212 unsigned char set_corrupt = 0, set_dist = 0;
221 memset(&opts, 0,
sizeof(opts));
222 memset(&cor, 0,
sizeof(cor));
223 memset(&reorder, 0,
sizeof(reorder));
224 memset(&corrupt, 0,
sizeof(corrupt));
226 msg->nm_nlh->nlmsg_flags |= NLM_F_REQUEST;
228 if (netem->qnm_ro.nmro_probability != 0) {
229 if (netem->qnm_latency == 0)
230 return -NLE_MISSING_ATTR;
231 if (netem->qnm_gap == 0)
233 }
else if (netem->qnm_gap)
234 return -NLE_MISSING_ATTR;
236 if (netem->qnm_corr.nmc_delay != 0) {
237 if (netem->qnm_latency == 0 || netem->qnm_jitter == 0)
238 return -NLE_MISSING_ATTR;
242 if (netem->qnm_corr.nmc_loss != 0) {
243 if (netem->qnm_loss == 0)
244 return -NLE_MISSING_ATTR;
248 if (netem->qnm_corr.nmc_duplicate != 0) {
249 if (netem->qnm_duplicate == 0)
250 return -NLE_MISSING_ATTR;
254 if (netem->qnm_ro.nmro_probability != 0)
256 else if (netem->qnm_ro.nmro_correlation != 0)
257 return -NLE_MISSING_ATTR;
259 if (netem->qnm_crpt.nmcr_probability != 0)
261 else if (netem->qnm_crpt.nmcr_correlation != 0)
262 return -NLE_MISSING_ATTR;
264 if (netem->qnm_dist.dist_data && netem->qnm_dist.dist_size) {
265 if (netem->qnm_latency == 0 || netem->qnm_jitter == 0)
266 return -NLE_MISSING_ATTR;
269 int new_msg_len = msg->nm_size + netem->qnm_dist.dist_size *
270 sizeof(netem->qnm_dist.dist_data[0]);
271 struct nlmsghdr *new_nlh = realloc(msg->nm_nlh, new_msg_len);
275 msg->nm_nlh = new_nlh;
276 msg->nm_size = new_msg_len;
281 opts.latency = netem->qnm_latency;
282 opts.limit = netem->qnm_limit ? netem->qnm_limit : 1000;
283 opts.loss = netem->qnm_loss;
284 opts.gap = netem->qnm_gap;
285 opts.duplicate = netem->qnm_duplicate;
286 opts.jitter = netem->qnm_jitter;
288 NLA_PUT(msg, TCA_OPTIONS,
sizeof(opts), &opts);
290 if (set_correlation) {
291 cor.delay_corr = netem->qnm_corr.nmc_delay;
292 cor.loss_corr = netem->qnm_corr.nmc_loss;
293 cor.dup_corr = netem->qnm_corr.nmc_duplicate;
295 NLA_PUT(msg, TCA_NETEM_CORR,
sizeof(cor), &cor);
299 reorder.probability = netem->qnm_ro.nmro_probability;
300 reorder.correlation = netem->qnm_ro.nmro_correlation;
302 NLA_PUT(msg, TCA_NETEM_REORDER,
sizeof(reorder), &reorder);
306 corrupt.probability = netem->qnm_crpt.nmcr_probability;
307 corrupt.correlation = netem->qnm_crpt.nmcr_correlation;
309 NLA_PUT(msg, TCA_NETEM_CORRUPT,
sizeof(corrupt), &corrupt);
313 NLA_PUT(msg, TCA_NETEM_DELAY_DIST,
314 netem->qnm_dist.dist_size *
sizeof(netem->qnm_dist.dist_data[0]),
315 netem->qnm_dist.dist_data);
322 head = (
struct nlattr *)(((
char *) NLMSG_DATA(msg->nm_nlh)) +
323 NLMSG_LENGTH(
sizeof(
struct tcmsg)) - NLMSG_ALIGNTO);
325 tail = (
struct nlattr *)(((
char *) (msg->nm_nlh)) +
326 NLMSG_ALIGN(msg->nm_nlh->nlmsg_len));
328 old_len = head->nla_len;
329 head->nla_len = (
char *)tail - (
char *)head;
330 msg->nm_nlh->nlmsg_len += (head->nla_len - old_len);
350 struct rtnl_netem *netem;
355 netem->qnm_limit = limit;
356 netem->qnm_mask |= SCH_NETEM_ATTR_LIMIT;
366 struct rtnl_netem *netem;
371 if (netem->qnm_mask & SCH_NETEM_ATTR_LIMIT)
372 return netem->qnm_limit;
392 struct rtnl_netem *netem;
397 netem->qnm_gap = gap;
398 netem->qnm_mask |= SCH_NETEM_ATTR_GAP;
408 struct rtnl_netem *netem;
413 if (netem->qnm_mask & SCH_NETEM_ATTR_GAP)
414 return netem->qnm_gap;
427 struct rtnl_netem *netem;
432 netem->qnm_ro.nmro_probability = prob;
433 netem->qnm_mask |= SCH_NETEM_ATTR_RO_PROB;
443 struct rtnl_netem *netem;
448 if (netem->qnm_mask & SCH_NETEM_ATTR_RO_PROB)
449 return netem->qnm_ro.nmro_probability;
462 struct rtnl_netem *netem;
467 netem->qnm_ro.nmro_correlation = prob;
468 netem->qnm_mask |= SCH_NETEM_ATTR_RO_CORR;
478 struct rtnl_netem *netem;
483 if (netem->qnm_mask & SCH_NETEM_ATTR_RO_CORR)
484 return netem->qnm_ro.nmro_correlation;
504 struct rtnl_netem *netem;
509 netem->qnm_crpt.nmcr_probability = prob;
510 netem->qnm_mask |= SCH_NETEM_ATTR_CORRUPT_PROB;
520 struct rtnl_netem *netem;
525 if (netem->qnm_mask & SCH_NETEM_ATTR_CORRUPT_PROB)
526 return netem->qnm_crpt.nmcr_probability;
539 struct rtnl_netem *netem;
544 netem->qnm_crpt.nmcr_correlation = prob;
545 netem->qnm_mask |= SCH_NETEM_ATTR_CORRUPT_CORR;
555 struct rtnl_netem *netem;
560 if (netem->qnm_mask & SCH_NETEM_ATTR_CORRUPT_CORR)
561 return netem->qnm_crpt.nmcr_correlation;
581 struct rtnl_netem *netem;
586 netem->qnm_loss = prob;
587 netem->qnm_mask |= SCH_NETEM_ATTR_LOSS;
597 struct rtnl_netem *netem;
602 if (netem->qnm_mask & SCH_NETEM_ATTR_LOSS)
603 return netem->qnm_loss;
616 struct rtnl_netem *netem;
621 netem->qnm_corr.nmc_loss = prob;
622 netem->qnm_mask |= SCH_NETEM_ATTR_LOSS_CORR;
632 struct rtnl_netem *netem;
637 if (netem->qnm_mask & SCH_NETEM_ATTR_LOSS_CORR)
638 return netem->qnm_corr.nmc_loss;
658 struct rtnl_netem *netem;
663 netem->qnm_duplicate = prob;
664 netem->qnm_mask |= SCH_NETEM_ATTR_DUPLICATE;
674 struct rtnl_netem *netem;
679 if (netem->qnm_mask & SCH_NETEM_ATTR_DUPLICATE)
680 return netem->qnm_duplicate;
693 struct rtnl_netem *netem;
698 netem->qnm_corr.nmc_duplicate = prob;
699 netem->qnm_mask |= SCH_NETEM_ATTR_DUP_CORR;
709 struct rtnl_netem *netem;
714 if (netem->qnm_mask & SCH_NETEM_ATTR_DUP_CORR)
715 return netem->qnm_corr.nmc_duplicate;
735 struct rtnl_netem *netem;
741 netem->qnm_mask |= SCH_NETEM_ATTR_LATENCY;
751 struct rtnl_netem *netem;
756 if (netem->qnm_mask & SCH_NETEM_ATTR_LATENCY)
770 struct rtnl_netem *netem;
776 netem->qnm_mask |= SCH_NETEM_ATTR_JITTER;
786 struct rtnl_netem *netem;
791 if (netem->qnm_mask & SCH_NETEM_ATTR_JITTER)
804 struct rtnl_netem *netem;
809 netem->qnm_corr.nmc_delay = prob;
810 netem->qnm_mask |= SCH_NETEM_ATTR_DELAY_CORR;
820 struct rtnl_netem *netem;
825 if (netem->qnm_mask & SCH_NETEM_ATTR_DELAY_CORR)
826 return netem->qnm_corr.nmc_delay;
838 struct rtnl_netem *netem;
843 if (netem->qnm_mask & SCH_NETEM_ATTR_DIST)
844 return netem->qnm_dist.dist_size;
857 struct rtnl_netem *netem;
862 if (netem->qnm_mask & SCH_NETEM_ATTR_DIST) {
863 *dist_ptr = netem->qnm_dist.dist_data;
875 struct rtnl_netem *netem;
884 new_data = (int16_t *) calloc(len,
sizeof(int16_t));
888 free (netem->qnm_dist.dist_data);
889 netem->qnm_dist.dist_data = new_data;
891 memcpy(netem->qnm_dist.dist_data, data, len *
sizeof(int16_t));
893 netem->qnm_dist.dist_size = len;
894 netem->qnm_mask |= SCH_NETEM_ATTR_DIST;
910 _nl_auto_free
char *line = NULL;
912 char dist_suffix[] =
".dist";
913 _nl_auto_free int16_t *data = NULL;
917 char *test_path[] = {
922 "/usr/local/lib/tc/",
926 test_suffix = strstr(dist_type, dist_suffix);
927 if (test_suffix != NULL && strlen(test_suffix) == 5)
928 strcpy(dist_suffix,
"");
930 for (i = 0; i < ARRAY_SIZE(test_path); i++) {
931 snprintf(name, NAME_MAX,
"%s%s%s", test_path[i], dist_type, dist_suffix);
932 if ((f = fopen(name,
"re")))
937 return -nl_syserr2nlerr(errno);
939 data = (int16_t *) calloc(MAXDIST,
sizeof(int16_t));
940 line = (
char *) calloc(
sizeof(
char), len + 1);
941 if (!data || !line) {
946 while (getline(&line, &len, f) != -1) {
949 if (*line ==
'\n' || *line ==
'#')
952 for (p = line; ; p = endp) {
953 long x = strtol(p, &endp, 0);
954 if (endp == p)
break;
971static struct rtnl_tc_ops netem_ops = {
973 .to_type = RTNL_TC_TYPE_QDISC,
974 .to_size =
sizeof(
struct rtnl_netem),
975 .to_msg_parser = netem_msg_parser,
976 .to_free_data = netem_free_data,
979 .to_msg_fill_raw = netem_msg_fill_raw,
982static void __init netem_init(
void)
987static void __exit netem_exit(
void)
int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len, const struct nla_policy *policy)
Create attribute index based on a stream of attributes.
#define NLA_PUT(msg, attrtype, attrlen, data)
Add unspecific attribute to netlink message.
int nla_memcpy(void *dest, const struct nlattr *src, int count)
Copy attribute payload to another memory area.
void rtnl_netem_set_duplicate_correlation(struct rtnl_qdisc *qdisc, int prob)
Set packet duplication correlation probability of netem qdisc.
int rtnl_netem_get_duplicate(struct rtnl_qdisc *qdisc)
Get packet duplication probability of netem qdisc.
int rtnl_netem_get_delay_distribution(struct rtnl_qdisc *qdisc, int16_t **dist_ptr)
Get a pointer to the distribution table.
int rtnl_netem_get_reorder_correlation(struct rtnl_qdisc *qdisc)
Get re-ordering correlation probability of netem qdisc.
void rtnl_netem_set_delay_correlation(struct rtnl_qdisc *qdisc, int prob)
Set packet delay correlation probability of netem qdisc.
int rtnl_netem_get_delay_correlation(struct rtnl_qdisc *qdisc)
Get packet delay correlation probability of netem qdisc.
int rtnl_netem_get_loss(struct rtnl_qdisc *qdisc)
Get packet loss probability of netem qdisc.
int rtnl_netem_get_delay_distribution_size(struct rtnl_qdisc *qdisc)
Get the size of the distribution table.
int rtnl_netem_get_gap(struct rtnl_qdisc *qdisc)
Get re-ordering gap of netem qdisc.
int rtnl_netem_get_reorder_probability(struct rtnl_qdisc *qdisc)
Get re-ordering probability of netem qdisc.
int rtnl_netem_get_duplicate_correlation(struct rtnl_qdisc *qdisc)
Get packet duplication correlation probability of netem qdisc.
void rtnl_netem_set_jitter(struct rtnl_qdisc *qdisc, int jitter)
Set packet delay jitter of netem qdisc.
int rtnl_netem_get_corruption_correlation(struct rtnl_qdisc *qdisc)
Get corruption correlation probability of netem qdisc.
void rtnl_netem_set_loss(struct rtnl_qdisc *qdisc, int prob)
Set packet loss probability of netem qdisc.
void rtnl_netem_set_limit(struct rtnl_qdisc *qdisc, int limit)
Set limit of netem qdisc.
void rtnl_netem_set_reorder_correlation(struct rtnl_qdisc *qdisc, int prob)
Set re-order correlation probability of netem qdisc.
int rtnl_netem_get_corruption_probability(struct rtnl_qdisc *qdisc)
Get corruption probability of netem qdisc.
int rtnl_netem_get_jitter(struct rtnl_qdisc *qdisc)
Get packet delay jitter of netem qdisc.
void rtnl_netem_set_corruption_correlation(struct rtnl_qdisc *qdisc, int prob)
Set corruption correlation probability of netem qdisc.
int rtnl_netem_get_delay(struct rtnl_qdisc *qdisc)
Get packet delay of netem qdisc.
void rtnl_netem_set_reorder_probability(struct rtnl_qdisc *qdisc, int prob)
Set re-ordering probability of netem qdisc.
void rtnl_netem_set_gap(struct rtnl_qdisc *qdisc, int gap)
Set re-ordering gap of netem qdisc.
void rtnl_netem_set_corruption_probability(struct rtnl_qdisc *qdisc, int prob)
Set corruption probability of netem qdisc.
void rtnl_netem_set_duplicate(struct rtnl_qdisc *qdisc, int prob)
Set packet duplication probability of netem qdisc.
void rtnl_netem_set_loss_correlation(struct rtnl_qdisc *qdisc, int prob)
Set packet loss correlation probability of netem qdisc.
int rtnl_netem_set_delay_distribution(struct rtnl_qdisc *qdisc, const char *dist_type)
Load the delay distribution from a file.
void rtnl_netem_set_delay(struct rtnl_qdisc *qdisc, int delay)
Set packet delay of netem qdisc.
int rtnl_netem_get_loss_correlation(struct rtnl_qdisc *qdisc)
Get packet loss correlation probability of netem qdisc.
int rtnl_netem_set_delay_distribution_data(struct rtnl_qdisc *qdisc, const int16_t *data, size_t len)
Set the delay distribution data.
int rtnl_netem_get_limit(struct rtnl_qdisc *qdisc)
Get limit of netem qdisc.
#define TC_CAST(ptr)
Macro to cast qdisc/class/classifier to tc object.
void * rtnl_tc_data(struct rtnl_tc *tc)
Return pointer to private data of traffic control object.
int rtnl_tc_register(struct rtnl_tc_ops *ops)
Register a traffic control module.
void rtnl_tc_unregister(struct rtnl_tc_ops *ops)
Unregister a traffic control module.
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
uint32_t nl_ticks2us(uint32_t ticks)
Convert ticks to micro seconds.
uint32_t nl_us2ticks(uint32_t us)
Convert micro seconds to ticks.
char * nl_msec2str(uint64_t msec, char *buf, size_t len)
Convert milliseconds to a character string.
@ NL_DUMP_LINE
Dump object briefly on one line.
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
Attribute validation policy.
uint16_t minlen
Minimal length of payload required.