batctl: Add support for hardif settings
[feed/routing.git] / batctl / patches / 0002-batctl-Integrate-hardif-setting-framework.patch
1 From: Sven Eckelmann <sven@narfation.org>
2 Date: Thu, 13 Jun 2019 21:12:15 +0200
3 Subject: batctl: Integrate hardif setting framework
4
5 batctl currently supports settings which are either mesh interface or vlan
6 specific. But B.A.T.M.A.N. V introduced two additional settings which are
7 hard (slave) interface specific.
8
9 To support these, an additional command prefix called hardif is implemented
10 for some sysfs commands:
11
12 $ batctl -m bat0 hardif eth0 ...
13
14 Signed-off-by: Sven Eckelmann <sven@narfation.org>
15
16 Forwarded: https://patchwork.open-mesh.org/patch/17948/
17
18 diff --git a/main.c b/main.c
19 index 6ca13ac0ec4c82ee969be04737a339fd702b52bd..c806dbf4373fd082ff368cba391bdf14eebf4eae 100644
20 --- a/main.c
21 +++ b/main.c
22 @@ -35,7 +35,8 @@ static void print_usage(void)
23 {
24 .label = "commands:\n",
25 .types = BIT(SUBCOMMAND) |
26 - BIT(SUBCOMMAND_VID),
27 + BIT(SUBCOMMAND_VID) |
28 + BIT(SUBCOMMAND_HIF),
29 },
30 {
31 .label = "debug tables: \tdisplay the corresponding debug table\n",
32 @@ -51,6 +52,10 @@ static void print_usage(void)
33 "vid <vid> ",
34 NULL,
35 };
36 + const char *hardif_prefixes[] = {
37 + "hardif <netdev> ",
38 + NULL,
39 + };
40 const struct command **p;
41 const char **prefixes;
42 const char **prefix;
43 @@ -81,6 +86,9 @@ static void print_usage(void)
44 case SUBCOMMAND_VID:
45 prefixes = vlan_prefixes;
46 break;
47 + case SUBCOMMAND_HIF:
48 + prefixes = hardif_prefixes;
49 + break;
50 default:
51 prefixes = default_prefixes;
52 break;
53 @@ -133,6 +141,12 @@ static const struct command *find_command(struct state *state, const char *name)
54 if (state->vid < 0 && cmd->type == SUBCOMMAND_VID)
55 continue;
56
57 + if (state->hif > 0 && cmd->type != SUBCOMMAND_HIF)
58 + continue;
59 +
60 + if (state->hif == 0 && cmd->type == SUBCOMMAND_HIF)
61 + continue;
62 +
63 if (strcmp(cmd->name, name) == 0)
64 return cmd;
65
66 @@ -180,6 +194,18 @@ static int parse_dev_args(struct state *state, int argc, char *argv[])
67 state->arg_iface = argv[1];
68 translate_mesh_iface(state);
69
70 + return 2;
71 + } else if (strcmp(argv[0], "hardif") == 0) {
72 + state->hif = if_nametoindex(argv[1]);
73 + if (state->hif == 0) {
74 + fprintf(stderr, "Error - hard interface not found\n");
75 + return -ENODEV;
76 + }
77 +
78 + snprintf(state->hard_iface, sizeof(state->hard_iface), "%s",
79 + argv[1]);
80 +
81 + translate_mesh_iface(state);
82 return 2;
83 } else {
84 /* parse vlan as part of -m parameter */
85 @@ -193,6 +219,7 @@ int main(int argc, char **argv)
86 const struct command *cmd;
87 struct state state = {
88 .arg_iface = mesh_dfl_iface,
89 + .hif = 0,
90 .cmd = NULL,
91 };
92 int dev_arguments;
93 diff --git a/main.h b/main.h
94 index 1d952610aefb8367bd52e24bea8c04c3d70b94ea..a27d8486ef689206b27b1b50cb017b1b740e91c9 100644
95 --- a/main.h
96 +++ b/main.h
97 @@ -59,6 +59,7 @@ enum command_flags {
98 enum command_type {
99 SUBCOMMAND,
100 SUBCOMMAND_VID,
101 + SUBCOMMAND_HIF,
102 DEBUGTABLE,
103 };
104
105 @@ -66,6 +67,8 @@ struct state {
106 char *arg_iface;
107 char mesh_iface[IF_NAMESIZE];
108 unsigned int mesh_ifindex;
109 + char hard_iface[IF_NAMESIZE];
110 + unsigned int hif;
111 int vid;
112 const struct command *cmd;
113
114 diff --git a/sys.c b/sys.c
115 index f19719cfad61f36f2a5c1078305de83eb5be142a..fd34b2fa3bcf168a32bd53fc0df3f35d5532433f 100644
116 --- a/sys.c
117 +++ b/sys.c
118 @@ -150,6 +150,10 @@ static void settings_usage(struct state *state)
119 "vid <vid> ",
120 NULL,
121 };
122 + const char *hardif_prefixes[] = {
123 + "hardif <netdev> ",
124 + NULL,
125 + };
126 const char *linestart = "Usage:";
127 const char **prefixes;
128 const char **prefix;
129 @@ -158,6 +162,9 @@ static void settings_usage(struct state *state)
130 case SUBCOMMAND_VID:
131 prefixes = vlan_prefixes;
132 break;
133 + case SUBCOMMAND_HIF:
134 + prefixes = hardif_prefixes;
135 + break;
136 default:
137 prefixes = default_prefixes;
138 break;
139 @@ -259,15 +266,23 @@ int handle_sys_setting(struct state *state, int argc, char **argv)
140 return EXIT_FAILURE;
141 }
142
143 - /* if the specified interface is a VLAN then change the path to point
144 - * to the proper "vlan%{vid}" subfolder in the sysfs tree.
145 - */
146 - if (state->vid >= 0)
147 + if (state->hif > 0) {
148 + /* if a hard interface was specified then change the path to
149 + * point to the proper ${hardif}/batman-adv path in the sysfs
150 + * tree.
151 + */
152 + snprintf(path_buff, PATH_BUFF_LEN, SYS_HARDIF_PATH,
153 + state->hard_iface);
154 + } else if (state->vid >= 0) {
155 + /* if the specified interface is a VLAN then change the path to
156 + * point to the proper "vlan%{vid}" subfolder in the sysfs tree.
157 + */
158 snprintf(path_buff, PATH_BUFF_LEN, SYS_VLAN_PATH,
159 state->mesh_iface, state->vid);
160 - else
161 + } else {
162 snprintf(path_buff, PATH_BUFF_LEN, SYS_BATIF_PATH_FMT,
163 state->mesh_iface);
164 + }
165
166 if (argc == 1) {
167 res = sys_read_setting(state, path_buff, settings->sysfs_name);
168 diff --git a/sys.h b/sys.h
169 index d4f2fcf542bc66b2b1c6ec55a9ac16e10fdc5cac..b6f0f9043a9af8e3c4d4f8bf7e4af4cab0aa5df9 100644
170 --- a/sys.h
171 +++ b/sys.h
172 @@ -21,8 +21,9 @@
173 #define SYS_BATIF_PATH_FMT "/sys/class/net/%s/mesh/"
174 #define SYS_IFACE_PATH "/sys/class/net"
175 #define SYS_IFACE_DIR SYS_IFACE_PATH"/%s/"
176 -#define SYS_MESH_IFACE_FMT SYS_IFACE_PATH"/%s/batman_adv/mesh_iface"
177 -#define SYS_IFACE_STATUS_FMT SYS_IFACE_PATH"/%s/batman_adv/iface_status"
178 +#define SYS_HARDIF_PATH SYS_IFACE_DIR "batman_adv/"
179 +#define SYS_MESH_IFACE_FMT SYS_HARDIF_PATH "mesh_iface"
180 +#define SYS_IFACE_STATUS_FMT SYS_HARDIF_PATH "iface_status"
181 #define SYS_VLAN_PATH SYS_IFACE_PATH"/%s/mesh/vlan%d/"
182 #define SYS_ROUTING_ALGO_FMT SYS_IFACE_PATH"/%s/mesh/routing_algo"
183 #define VLAN_ID_MAX_LEN 4