Skip to content

Commit 9836914

Browse files
committed
Move open_bpf_map_file to common/
Move the open_bpf_map_file function to the common/ directory and move code to get map info to this function from the check_map_fd_info function. This patch is needed to make the implementation of basic04 solutions shorter. Signed-off-by: Anton Protopopov <a.s.protopopov@gmail.com>
1 parent 843cb4a commit 9836914

4 files changed

Lines changed: 49 additions & 67 deletions

File tree

basic04-pinning-maps/xdp_stats.c

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -214,30 +214,6 @@ static void stats_poll(int map_fd, __u32 map_type, int interval)
214214
#define PATH_MAX 4096
215215
#endif
216216

217-
int open_bpf_map_file(const char *pin_dir, const char *mapname)
218-
{
219-
char filename[PATH_MAX];
220-
int len, fd;
221-
222-
len = snprintf(filename, PATH_MAX, "%s/%s", pin_dir, mapname);
223-
if (len < 0) {
224-
fprintf(stderr, "ERR: constructing full mapname path\n");
225-
return -1;
226-
}
227-
228-
/* Lesson#1: There is only a weak dependency to libbpf here as
229-
* bpf_obj_get is a simple wrapper around the bpf-syscall
230-
*/
231-
fd = bpf_obj_get(filename);
232-
if (fd < 0) {
233-
fprintf(stderr,
234-
"WARN: Failed to open bpf map file:%s err(%d):%s\n",
235-
filename, errno, strerror(errno));
236-
return fd;
237-
}
238-
return fd;
239-
}
240-
241217
const char *pin_basedir = "/sys/fs/bpf";
242218

243219
int main(int argc, char **argv)
@@ -271,7 +247,7 @@ int main(int argc, char **argv)
271247
return EXIT_FAIL_OPTION;
272248
}
273249

274-
stats_map_fd = open_bpf_map_file(pin_dir, "xdp_stats_map");
250+
stats_map_fd = open_bpf_map_file(pin_dir, "xdp_stats_map", &info);
275251
if (stats_map_fd < 0) {
276252
return EXIT_FAIL_BPF;
277253
}
@@ -280,7 +256,7 @@ int main(int argc, char **argv)
280256
map_expect.key_size = sizeof(__u32);
281257
map_expect.value_size = sizeof(struct datarec);
282258
map_expect.max_entries = XDP_ACTION_MAX;
283-
err = check_map_fd_info(stats_map_fd, &info, &map_expect);
259+
err = check_map_fd_info(&info, &map_expect);
284260
if (err) {
285261
fprintf(stderr, "ERR: map via FD not compatible\n");
286262
return err;

common/common_user_bpf_xdp.c

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -200,23 +200,9 @@ const char *action2str(__u32 action)
200200
return NULL;
201201
}
202202

203-
int check_map_fd_info(int map_fd, struct bpf_map_info *info,
204-
struct bpf_map_info *exp)
203+
int check_map_fd_info(const struct bpf_map_info *info,
204+
const struct bpf_map_info *exp)
205205
{
206-
__u32 info_len = sizeof(*info);
207-
int err;
208-
209-
if (map_fd < 0)
210-
return EXIT_FAIL;
211-
212-
/* BPF-info via bpf-syscall */
213-
err = bpf_obj_get_info_by_fd(map_fd, info, &info_len);
214-
if (err) {
215-
fprintf(stderr, "ERR: %s() can't get info - %s\n",
216-
__func__, strerror(errno));
217-
return EXIT_FAIL_BPF;
218-
}
219-
220206
if (exp->key_size && exp->key_size != info->key_size) {
221207
fprintf(stderr, "ERR: %s() "
222208
"Map key size(%d) mismatch expected size(%d)\n",
@@ -244,3 +230,40 @@ int check_map_fd_info(int map_fd, struct bpf_map_info *info,
244230

245231
return 0;
246232
}
233+
234+
int open_bpf_map_file(const char *pin_dir,
235+
const char *mapname,
236+
struct bpf_map_info *info)
237+
{
238+
char filename[PATH_MAX];
239+
int err, len, fd;
240+
__u32 info_len = sizeof(*info);
241+
242+
len = snprintf(filename, PATH_MAX, "%s/%s", pin_dir, mapname);
243+
if (len < 0) {
244+
fprintf(stderr, "ERR: constructing full mapname path\n");
245+
return -1;
246+
}
247+
248+
/* Lesson#1: There is only a weak dependency to libbpf here as
249+
* bpf_obj_get is a simple wrapper around the bpf-syscall
250+
*/
251+
fd = bpf_obj_get(filename);
252+
if (fd < 0) {
253+
fprintf(stderr,
254+
"WARN: Failed to open bpf map file:%s err(%d):%s\n",
255+
filename, errno, strerror(errno));
256+
return fd;
257+
}
258+
259+
if (info) {
260+
err = bpf_obj_get_info_by_fd(fd, info, &info_len);
261+
if (err) {
262+
fprintf(stderr, "ERR: %s() can't get info - %s\n",
263+
__func__, strerror(errno));
264+
return EXIT_FAIL_BPF;
265+
}
266+
}
267+
268+
return fd;
269+
}

common/common_user_bpf_xdp.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ struct bpf_object *load_bpf_and_xdp_attach(struct config *cfg);
1010

1111
const char *action2str(__u32 action);
1212

13-
int check_map_fd_info(int map_fd, struct bpf_map_info *info,
14-
struct bpf_map_info *exp);
13+
int check_map_fd_info(const struct bpf_map_info *info,
14+
const struct bpf_map_info *exp);
15+
16+
int open_bpf_map_file(const char *pin_dir,
17+
const char *mapname,
18+
struct bpf_map_info *info);
1519

1620
#endif /* __COMMON_USER_BPF_XDP_H */

packet-solutions/xdp_prog_user.c

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -103,27 +103,6 @@ static int write_iface_params(int map_fd, unsigned char *src, unsigned char *des
103103
#define PATH_MAX 4096
104104
#endif
105105

106-
int open_bpf_map_file(const char *pin_dir, const char *mapname)
107-
{
108-
char filename[PATH_MAX];
109-
int len, fd;
110-
111-
len = snprintf(filename, PATH_MAX, "%s/%s", pin_dir, mapname);
112-
if (len < 0) {
113-
fprintf(stderr, "ERR: constructing full mapname path\n");
114-
return -1;
115-
}
116-
117-
fd = bpf_obj_get(filename);
118-
if (fd < 0) {
119-
fprintf(stderr,
120-
"WARN: Failed to open bpf map file:%s err(%d):%s\n",
121-
filename, errno, strerror(errno));
122-
return fd;
123-
}
124-
return fd;
125-
}
126-
127106
const char *pin_basedir = "/sys/fs/bpf";
128107

129108
int main(int argc, char **argv)
@@ -169,7 +148,7 @@ int main(int argc, char **argv)
169148
}
170149

171150
/* Open the tx_port map corresponding to the cfg.ifname interface */
172-
map_fd = open_bpf_map_file(pin_dir, "tx_port");
151+
map_fd = open_bpf_map_file(pin_dir, "tx_port", NULL);
173152
if (map_fd < 0) {
174153
return EXIT_FAIL_BPF;
175154
}
@@ -183,7 +162,7 @@ int main(int argc, char **argv)
183162
printf("redirect from ifnum=%d to ifnum=%d\n", cfg.ifindex, cfg.redirect_ifindex);
184163

185164
/* Open the redirect_params map */
186-
map_fd = open_bpf_map_file(pin_dir, "redirect_params");
165+
map_fd = open_bpf_map_file(pin_dir, "redirect_params", NULL);
187166
if (map_fd < 0) {
188167
return EXIT_FAIL_BPF;
189168
}

0 commit comments

Comments
 (0)