diff options
author | Richard Palethorpe <rpalethorpe@suse.com> | 2021-01-29 17:03:00 +0000 |
---|---|---|
committer | Cyril Hrubis <chrubis@suse.cz> | 2021-02-01 16:02:21 +0100 |
commit | 91f98c77a2c7e31d766d408be36cdea72d39dbc5 (patch) | |
tree | 2321cf68a2512da2d0f681e5e50751df89a36612 /testcases/network/can/filter-tests | |
parent | 80284e06527c67dd49431a84a341c7e2ba6e5d3b (diff) |
can: Add can_common.h for vcan device setup
Signed-off-by: Richard Palethorpe <rpalethorpe@suse.com>
Reviewed-by: Cyril Hrubis <chrubis@suse.cz>
Reviewed-by: Petr Vorel <pvorel@suse.cz>
Acked-by: Oliver Hartkopp <socketcan@hartkopp.net>
Diffstat (limited to 'testcases/network/can/filter-tests')
-rw-r--r-- | testcases/network/can/filter-tests/can_common.h | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/testcases/network/can/filter-tests/can_common.h b/testcases/network/can/filter-tests/can_common.h new file mode 100644 index 000000000..af0cf5f00 --- /dev/null +++ b/testcases/network/can/filter-tests/can_common.h @@ -0,0 +1,71 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2021 SUSE LLC + */ + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> + +#include <sys/types.h> +#include <sys/socket.h> +#include <sys/ioctl.h> +#include <sys/time.h> + +#include "tst_cmd.h" +#include "tst_safe_stdio.h" +#include "tst_safe_file_ops.h" + +#include <linux/if.h> +#include <linux/can.h> +#include <linux/can/raw.h> + +static char *can_dev_name; +static int can_created_dev; + +static void can_cmd(const char *const argv[]) +{ + tst_cmd(argv, NULL, NULL, TST_CMD_TCONF_ON_MISSING); +} + +#define CAN_CMD(...) can_cmd((const char *const[]){ __VA_ARGS__, NULL }) + +static void can_setup_vcan(void) +{ + unsigned int flags; + char *path; + + if (can_dev_name) + goto check_echo; + + can_dev_name = "vcan0"; + + tst_res(TINFO, "Creating vcan0 device; use -D option to avoid this"); + + CAN_CMD("modprobe", "-r", "vcan"); + CAN_CMD("modprobe", "vcan", "echo=1"); + + can_created_dev = 1; + + CAN_CMD("ip", "link", "add", "dev", "vcan0", "type", "vcan"); + CAN_CMD("ip", "link", "set", "dev", "vcan0", "up"); + +check_echo: + SAFE_ASPRINTF(&path, "/sys/class/net/%s/flags", can_dev_name); + if (FILE_SCANF(path, "%x", &flags) || !(flags & IFF_ECHO)) { + tst_res(TWARN, + "Could not determine if ECHO is set on %s. This may effect code coverage.", + can_dev_name); + } +} + +static void can_cleanup_vcan(void) +{ + if (!can_created_dev) + return; + + CAN_CMD("ip", "link", "set", "dev", "vcan0", "down"); + CAN_CMD("ip", "link", "del", "dev", "vcan0"); + CAN_CMD("modprobe", "-r", "vcan"); +} |