diff options
author | Steffen Vogel <post@steffenvogel.de> | 2017-12-20 10:09:08 (GMT) |
---|---|---|
committer | Steffen Vogel <post@steffenvogel.de> | 2017-12-20 10:09:08 (GMT) |
commit | b590366ffbc759505dabe438bea18ce0c3bce515 (patch) | |
tree | fa961679d4a165f32d6b42582a2097fdcb31827a | |
parent | 165611034621b0eda1a4747ea382600fed436437 (diff) | |
download | libnl-b590366ffbc759505dabe438bea18ce0c3bce515.zip libnl-b590366ffbc759505dabe438bea18ce0c3bce515.tar.gz libnl-b590366ffbc759505dabe438bea18ce0c3bce515.tar.bz2 |
route: add separate function to set netem qdisc delay distribution
A new function rtnl_netem_set_delay_distribution_data() has been added
to allow the user to pass the delay distribution directly without loading
it from a file.
In conjunction with the maketable code (see iproute2 / NISTnet) this can
be used to generate and load custom delay distributions on the fly.
-rw-r--r-- | include/netlink/route/qdisc/netem.h | 1 | ||||
-rw-r--r-- | lib/route/qdisc/netem.c | 36 |
2 files changed, 27 insertions, 10 deletions
diff --git a/include/netlink/route/qdisc/netem.h b/include/netlink/route/qdisc/netem.h index 4b071bf..707d665 100644 --- a/include/netlink/route/qdisc/netem.h +++ b/include/netlink/route/qdisc/netem.h @@ -66,6 +66,7 @@ extern int rtnl_netem_get_delay_correlation(struct rtnl_qdisc *); /* Delay Distribution */ #define MAXDIST 65536 extern int rtnl_netem_set_delay_distribution(struct rtnl_qdisc *, const char *); +extern int rtnl_netem_set_delay_distribution_data(struct rtnl_qdisc *, int16_t *, size_t len); extern int rtnl_netem_get_delay_distribution_size(struct rtnl_qdisc *); extern int rtnl_netem_get_delay_distribution(struct rtnl_qdisc *, int16_t **); diff --git a/lib/route/qdisc/netem.c b/lib/route/qdisc/netem.c index c4ba2a1..0df5b01 100644 --- a/lib/route/qdisc/netem.c +++ b/lib/route/qdisc/netem.c @@ -867,17 +867,36 @@ int rtnl_netem_get_delay_distribution(struct rtnl_qdisc *qdisc, int16_t **dist_p } /** - * Set the delay distribution. Latency/jitter must be set before applying. + * Set the delay distribution data. Latency/jitter must be set before applying. * @arg qdisc Netem qdisc. - * @arg dist_type The name of the distribution (type, file, path/file). * @return 0 on success, error code on failure. */ -int rtnl_netem_set_delay_distribution(struct rtnl_qdisc *qdisc, const char *dist_type) { +int rtnl_netem_set_delay_distribution_data(struct rtnl_qdisc *qdisc, int16_t *data, size_t len) { struct rtnl_netem *netem; if (!(netem = rtnl_tc_data(TC_CAST(qdisc)))) BUG(); + if (len > MAXDIST) + return -NLE_INVAL; + + netem->qnm_dist.dist_data = (int16_t *) calloc(len, sizeof(int16_t)); + + memcpy(netem->qnm_dist.dist_data, data, len * sizeof(int16_t)); + + netem->qnm_dist.dist_size = len; + netem->qnm_mask |= SCH_NETEM_ATTR_DIST; + + return 0; +} + +/** + * Load the delay distribution from a file. Latency/jitter must be set before applying. + * @arg qdisc Netem qdisc. + * @arg dist_type The name of the distribution (type, file, path/file). + * @return 0 on success, error code on failure. + */ +int rtnl_netem_set_delay_distribution(struct rtnl_qdisc *qdisc, const char *dist_type) { FILE *f; int n = 0; size_t i; @@ -909,7 +928,7 @@ int rtnl_netem_set_delay_distribution(struct rtnl_qdisc *qdisc, const char *dist if (f == NULL) return -nl_syserr2nlerr(errno); - netem->qnm_dist.dist_data = (int16_t *) calloc (MAXDIST, sizeof(int16_t)); + int16_t * data = (int16_t *) calloc (MAXDIST, sizeof(int16_t)); line = (char *) calloc (sizeof(char), len + 1); @@ -928,17 +947,14 @@ int rtnl_netem_set_delay_distribution(struct rtnl_qdisc *qdisc, const char *dist fclose(f); return -NLE_INVAL; } - netem->qnm_dist.dist_data[n++] = x; + data[n++] = x; } } free(line); - - netem->qnm_dist.dist_size = n; - netem->qnm_mask |= SCH_NETEM_ATTR_DIST; - fclose(f); - return 0; + + return rtnl_netem_set_delay_distribution_data(qdisc, data, n); } /** @} */ |