[CRIU] [PATCH 4/5] zdtm: add irand_range helper and use it in netns-dev test
Pavel Tikhomirov
ptikhomirov at virtuozzo.com
Thu Jul 14 06:51:13 PDT 2016
fixup wrong range order just in case
https://jira.sw.ru/browse/PSBM-48397
Signed-off-by: Pavel Tikhomirov <ptikhomirov at virtuozzo.com>
---
test/zdtm/lib/Makefile | 2 +-
test/zdtm/lib/randrange.c | 36 ++++++++++++++++++++++++++++++++++
test/zdtm/lib/randrange.h | 13 +++++++++++++
test/zdtm/static/netns-dev.c | 46 +++++++++-----------------------------------
4 files changed, 59 insertions(+), 38 deletions(-)
create mode 100644 test/zdtm/lib/randrange.c
create mode 100644 test/zdtm/lib/randrange.h
diff --git a/test/zdtm/lib/Makefile b/test/zdtm/lib/Makefile
index 592d934..8089c6f 100644
--- a/test/zdtm/lib/Makefile
+++ b/test/zdtm/lib/Makefile
@@ -7,7 +7,7 @@ LIBDIR = .
LIB = libzdtmtst.a
GRPS = groups
-LIBSRC = datagen.c msg.c parseargs.c test.c streamutil.c lock.c ns.c tcp.c fs.c
+LIBSRC = datagen.c msg.c parseargs.c test.c streamutil.c lock.c ns.c tcp.c fs.c randrange.c
LIBOBJ = $(LIBSRC:%.c=%.o)
LIBDEP = $(LIBSRC:%.c=%.d)
diff --git a/test/zdtm/lib/randrange.c b/test/zdtm/lib/randrange.c
new file mode 100644
index 0000000..76b5ea2
--- /dev/null
+++ b/test/zdtm/lib/randrange.c
@@ -0,0 +1,36 @@
+#include <stdlib.h>
+
+#include "randrange.h"
+
+/* Generate random integers for sysctls */
+
+static int irand_small_range(struct rand_range *rr) {
+ return lrand48() % (rr->max - rr->min + 1) + rr->min;
+}
+
+int irand_range(struct rand_range *rr) {
+ struct rand_range small;
+ int mid, half;
+
+ if (rr->max < rr->min) {
+ /* Fixup wrong order */
+ int tmp = rr->min;
+ rr->min = rr->max;
+ rr->max = tmp;
+ }
+ mid = rr->max / 2 + rr->min / 2;
+ half = rr->max / 2 - rr->min / 2;
+
+ if (half < INT_MAX / 2)
+ return irand_small_range(rr);
+
+ if (lrand48() % 2) {
+ small.min = rr->min;
+ small.max = mid;
+ } else {
+ small.min = mid + 1;
+ small.max = rr->max;
+ }
+
+ return irand_small_range(&small);
+}
diff --git a/test/zdtm/lib/randrange.h b/test/zdtm/lib/randrange.h
new file mode 100644
index 0000000..76833f6
--- /dev/null
+++ b/test/zdtm/lib/randrange.h
@@ -0,0 +1,13 @@
+#ifndef ZDTM_RANDRANGE_H_
+#define ZDTM_RANDRANGE_H_
+
+#define INT_MAX ((int)(~0U>>1))
+
+struct rand_range {
+ int min;
+ int max;
+};
+
+extern int irand_range(struct rand_range *rr);
+
+#endif /* ZDTM_RANDRANGE_H_ */
diff --git a/test/zdtm/static/netns-dev.c b/test/zdtm/static/netns-dev.c
index 4efbeba..c4f3920 100644
--- a/test/zdtm/static/netns-dev.c
+++ b/test/zdtm/static/netns-dev.c
@@ -3,6 +3,7 @@
#include <dirent.h>
#include "zdtmtst.h"
+#include "randrange.h"
#define LO_CONF_DIR_PATH "/proc/sys/net/ipv4/conf/lo"
#define DEF_CONF_DIR_PATH "/proc/sys/net/ipv4/conf/default"
@@ -11,7 +12,6 @@
#define DEF_CONF6_DIR_PATH "/proc/sys/net/ipv6/conf/default"
#define ALL_CONF6_DIR_PATH "/proc/sys/net/ipv6/conf/all"
-#define INT_MAX ((int)(~0U>>1))
#define INT_MIN (-INT_MAX - 1)
char *devconfs4[] = {
@@ -49,12 +49,7 @@ char *devconfs4[] = {
NULL,
};
-struct range {
- int min;
- int max;
-};
-
-struct range rand_range4[] = {
+struct rand_range rand_range4[] = {
{0, 1}, /* accept_local */
{-1, 0}, /* accept_source_route */
{0, 1}, /* arp_accept */
@@ -138,7 +133,7 @@ char *devconfs6[] = {
/* According to kernel docs do not make max_addresses too large */
#define MAX_ADDRESSES 128
-struct range rand_range6[] = {
+struct rand_range rand_range6[] = {
{0, 2}, /* accept_dad */
{0, 2}, /* accept_ra */
{0, 1}, /* accept_ra_defrtr */
@@ -192,7 +187,7 @@ struct test_conf {
} lo, def, all;
static int save_conf(FILE *fp, int *conf, int *conf_rand,
- struct range *range, char *path) {
+ struct rand_range *range, char *path) {
int ret;
/*
@@ -207,36 +202,13 @@ static int save_conf(FILE *fp, int *conf, int *conf_rand,
return 0;
}
-static int rand_in_small_range(struct range *r) {
- return lrand48() % (r->max - r->min + 1) + r->min;
-}
-
-static int rand_in_range(struct range *r) {
- struct range small;
- int mid = r->max / 2 + r->min / 2;
- int half = r->max / 2 - r->min / 2;
-
- if (half < INT_MAX / 2)
- return rand_in_small_range(r);
-
- if (lrand48() % 2) {
- small.min = r->min;
- small.max = mid;
- } else {
- small.min = mid + 1;
- small.max = r->max;
- }
-
- return rand_in_small_range(&small);
-}
-
static int gen_conf(FILE *fp, int *conf, int *conf_rand,
- struct range *range, char *path) {
+ struct rand_range *range, char *path) {
int ret;
/*
* Set random value
*/
- *conf_rand = rand_in_range(range);
+ *conf_rand = irand_range(range);
ret = fprintf(fp, "%d", *conf_rand);
if (ret < 0) {
@@ -250,7 +222,7 @@ static int gen_conf(FILE *fp, int *conf, int *conf_rand,
#define MAX_MSEC_GRANULARITY 10
static int check_conf(FILE *fp, int *conf, int *conf_rand,
- struct range *range, char *path) {
+ struct rand_range *range, char *path) {
int ret;
int val;
@@ -277,7 +249,7 @@ static int check_conf(FILE *fp, int *conf, int *conf_rand,
}
static int restore_conf(FILE *fp, int *conf, int *conf_rand,
- struct range *range, char *path) {
+ struct rand_range *range, char *path) {
int ret;
/*
* Restore opt
@@ -292,7 +264,7 @@ static int restore_conf(FILE *fp, int *conf, int *conf_rand,
}
static int for_each_option_do(int (*f)(FILE *fp, int *conf, int *conf_rand,
- struct range *range, char *path), struct test_conf *tc) {
+ struct rand_range *range, char *path), struct test_conf *tc) {
int ret;
int i;
--
2.5.5
More information about the CRIU
mailing list