1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2021 SUSE LLC
4 */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <string.h>
10
11 #include <sys/types.h>
12 #include <sys/socket.h>
13 #include <sys/ioctl.h>
14 #include <sys/time.h>
15
16 #include "tst_cmd.h"
17 #include "tst_safe_stdio.h"
18 #include "tst_safe_file_ops.h"
19
20 #include <linux/if.h>
21 #include <linux/can.h>
22 #include <linux/can/raw.h>
23
24 static char *can_dev_name;
25 static int can_created_dev;
26
can_cmd(const char * const argv[])27 static void can_cmd(const char *const argv[])
28 {
29 tst_cmd(argv, NULL, NULL, TST_CMD_TCONF_ON_MISSING);
30 }
31
32 #define CAN_CMD(...) can_cmd((const char *const[]){ __VA_ARGS__, NULL })
33
can_setup_vcan(void)34 static void can_setup_vcan(void)
35 {
36 unsigned int flags;
37 char *path;
38
39 if (can_dev_name)
40 goto check_echo;
41
42 can_dev_name = "vcan0";
43
44 tst_res(TINFO, "Creating vcan0 device; use -D option to avoid this");
45
46 CAN_CMD("modprobe", "-r", "vcan");
47 CAN_CMD("modprobe", "vcan", "echo=1");
48
49 can_created_dev = 1;
50
51 CAN_CMD("ip", "link", "add", "dev", "vcan0", "type", "vcan");
52 CAN_CMD("ip", "link", "set", "dev", "vcan0", "up");
53
54 check_echo:
55 SAFE_ASPRINTF(&path, "/sys/class/net/%s/flags", can_dev_name);
56 if (FILE_SCANF(path, "%x", &flags) || !(flags & IFF_ECHO)) {
57 tst_res(TWARN,
58 "Could not determine if ECHO is set on %s. This may effect code coverage.",
59 can_dev_name);
60 }
61 }
62
can_cleanup_vcan(void)63 static void can_cleanup_vcan(void)
64 {
65 if (!can_created_dev)
66 return;
67
68 CAN_CMD("ip", "link", "set", "dev", "vcan0", "down");
69 CAN_CMD("ip", "link", "del", "dev", "vcan0");
70 CAN_CMD("modprobe", "-r", "vcan");
71 }
72