diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/route/qdisc/netem.c | 36 |
1 files changed, 26 insertions, 10 deletions
diff --git a/lib/route/qdisc/netem.c b/lib/route/qdisc/netem.c index c4ba2a1f..0df5b017 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); } /** @} */ |