hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/samples/bpf/xdp_fwd_user.c
....@@ -24,17 +24,25 @@
2424 #include <fcntl.h>
2525 #include <libgen.h>
2626
27
-#include "bpf/libbpf.h"
27
+#include <bpf/libbpf.h>
2828 #include <bpf/bpf.h>
2929
30
+static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
3031
31
-static int do_attach(int idx, int fd, const char *name)
32
+static int do_attach(int idx, int prog_fd, int map_fd, const char *name)
3233 {
3334 int err;
3435
35
- err = bpf_set_link_xdp_fd(idx, fd, 0);
36
- if (err < 0)
36
+ err = bpf_set_link_xdp_fd(idx, prog_fd, xdp_flags);
37
+ if (err < 0) {
3738 printf("ERROR: failed to attach program to %s\n", name);
39
+ return err;
40
+ }
41
+
42
+ /* Adding ifindex as a possible egress TX port */
43
+ err = bpf_map_update_elem(map_fd, &idx, &idx, 0);
44
+ if (err)
45
+ printf("ERROR: failed using device %s as TX-port\n", name);
3846
3947 return err;
4048 }
....@@ -43,10 +51,13 @@
4351 {
4452 int err;
4553
46
- err = bpf_set_link_xdp_fd(idx, -1, 0);
54
+ err = bpf_set_link_xdp_fd(idx, -1, xdp_flags);
4755 if (err < 0)
4856 printf("ERROR: failed to detach program from %s\n", name);
4957
58
+ /* TODO: Remember to cleanup map, when adding use of shared map
59
+ * bpf_map_delete_elem((map_fd, &idx);
60
+ */
5061 return err;
5162 }
5263
....@@ -67,17 +78,23 @@
6778 };
6879 const char *prog_name = "xdp_fwd";
6980 struct bpf_program *prog;
81
+ int prog_fd, map_fd = -1;
7082 char filename[PATH_MAX];
7183 struct bpf_object *obj;
7284 int opt, i, idx, err;
73
- int prog_fd, map_fd;
7485 int attach = 1;
7586 int ret = 0;
7687
77
- while ((opt = getopt(argc, argv, ":dD")) != -1) {
88
+ while ((opt = getopt(argc, argv, ":dDSF")) != -1) {
7889 switch (opt) {
7990 case 'd':
8091 attach = 0;
92
+ break;
93
+ case 'S':
94
+ xdp_flags |= XDP_FLAGS_SKB_MODE;
95
+ break;
96
+ case 'F':
97
+ xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
8198 break;
8299 case 'D':
83100 prog_name = "xdp_fwd_direct";
....@@ -87,6 +104,9 @@
87104 return 1;
88105 }
89106 }
107
+
108
+ if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
109
+ xdp_flags |= XDP_FLAGS_DRV_MODE;
90110
91111 if (optind == argc) {
92112 usage(basename(argv[0]));
....@@ -103,8 +123,14 @@
103123 return 1;
104124 }
105125
106
- if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
126
+ err = bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd);
127
+ if (err) {
128
+ printf("Does kernel support devmap lookup?\n");
129
+ /* If not, the error message will be:
130
+ * "cannot pass map_type 14 into func bpf_map_lookup_elem#1"
131
+ */
107132 return 1;
133
+ }
108134
109135 prog = bpf_object__find_program_by_title(obj, prog_name);
110136 prog_fd = bpf_program__fd(prog);
....@@ -113,15 +139,11 @@
113139 return 1;
114140 }
115141 map_fd = bpf_map__fd(bpf_object__find_map_by_name(obj,
116
- "tx_port"));
142
+ "xdp_tx_ports"));
117143 if (map_fd < 0) {
118144 printf("map not found: %s\n", strerror(map_fd));
119145 return 1;
120146 }
121
- }
122
- if (attach) {
123
- for (i = 1; i < 64; ++i)
124
- bpf_map_update_elem(map_fd, &i, &i, 0);
125147 }
126148
127149 for (i = optind; i < argc; ++i) {
....@@ -138,7 +160,7 @@
138160 if (err)
139161 ret = err;
140162 } else {
141
- err = do_attach(idx, prog_fd, argv[i]);
163
+ err = do_attach(idx, prog_fd, map_fd, argv[i]);
142164 if (err)
143165 ret = err;
144166 }