Skip to content

Commit 464bc0f

Browse files
jrfastabdavem330
authored andcommitted
bpf: convert sockmap field attach_bpf_fd2 to type
In the initial sockmap API we provided strparser and verdict programs using a single attach command by extending the attach API with a the attach_bpf_fd2 field. However, if we add other programs in the future we will be adding a field for every new possible type, attach_bpf_fd(3,4,..). This seems a bit clumsy for an API. So lets push the programs using two new type fields. BPF_SK_SKB_STREAM_PARSER BPF_SK_SKB_STREAM_VERDICT This has the advantage of having a readable name and can easily be extended in the future. Updates to samples and sockmap included here also generalize tests slightly to support upcoming patch for multiple map support. Signed-off-by: John Fastabend <[email protected]> Fixes: 174a79f ("bpf: sockmap with sk redirect support") Suggested-by: Alexei Starovoitov <[email protected]> Acked-by: Alexei Starovoitov <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 901c5d2 commit 464bc0f

File tree

13 files changed

+116
-151
lines changed

13 files changed

+116
-151
lines changed

include/linux/bpf.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ struct bpf_map_ops {
3939
void (*map_fd_put_ptr)(void *ptr);
4040
u32 (*map_gen_lookup)(struct bpf_map *map, struct bpf_insn *insn_buf);
4141
u32 (*map_fd_sys_lookup_elem)(void *ptr);
42-
int (*map_attach)(struct bpf_map *map,
43-
struct bpf_prog *p1, struct bpf_prog *p2);
4442
};
4543

4644
struct bpf_map {
@@ -387,11 +385,19 @@ static inline void __dev_map_flush(struct bpf_map *map)
387385

388386
#if defined(CONFIG_STREAM_PARSER) && defined(CONFIG_BPF_SYSCALL)
389387
struct sock *__sock_map_lookup_elem(struct bpf_map *map, u32 key);
388+
int sock_map_attach_prog(struct bpf_map *map, struct bpf_prog *prog, u32 type);
390389
#else
391390
static inline struct sock *__sock_map_lookup_elem(struct bpf_map *map, u32 key)
392391
{
393392
return NULL;
394393
}
394+
395+
static inline int sock_map_attach_prog(struct bpf_map *map,
396+
struct bpf_prog *prog,
397+
u32 type)
398+
{
399+
return -EOPNOTSUPP;
400+
}
395401
#endif
396402

397403
/* verifier prototypes for helper functions called from eBPF programs */

include/uapi/linux/bpf.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ enum bpf_attach_type {
136136
BPF_CGROUP_INET_EGRESS,
137137
BPF_CGROUP_INET_SOCK_CREATE,
138138
BPF_CGROUP_SOCK_OPS,
139-
BPF_CGROUP_SMAP_INGRESS,
139+
BPF_SK_SKB_STREAM_PARSER,
140+
BPF_SK_SKB_STREAM_VERDICT,
140141
__MAX_BPF_ATTACH_TYPE
141142
};
142143

@@ -224,7 +225,6 @@ union bpf_attr {
224225
__u32 attach_bpf_fd; /* eBPF program to attach */
225226
__u32 attach_type;
226227
__u32 attach_flags;
227-
__u32 attach_bpf_fd2;
228228
};
229229

230230
struct { /* anonymous struct used by BPF_PROG_TEST_RUN command */
@@ -580,14 +580,11 @@ union bpf_attr {
580580
* @flags: reserved for future use
581581
* Return: SK_REDIRECT
582582
*
583-
* int bpf_sock_map_update(skops, map, key, flags, map_flags)
583+
* int bpf_sock_map_update(skops, map, key, flags)
584584
* @skops: pointer to bpf_sock_ops
585585
* @map: pointer to sockmap to update
586586
* @key: key to insert/update sock in map
587587
* @flags: same flags as map update elem
588-
* @map_flags: sock map specific flags
589-
* bit 1: Enable strparser
590-
* other bits: reserved
591588
*/
592589
#define __BPF_FUNC_MAPPER(FN) \
593590
FN(unspec), \

kernel/bpf/sockmap.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -723,20 +723,24 @@ static int sock_map_ctx_update_elem(struct bpf_sock_ops_kern *skops,
723723
return err;
724724
}
725725

726-
static int sock_map_attach_prog(struct bpf_map *map,
727-
struct bpf_prog *parse,
728-
struct bpf_prog *verdict)
726+
int sock_map_attach_prog(struct bpf_map *map, struct bpf_prog *prog, u32 type)
729727
{
730728
struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
731-
struct bpf_prog *_parse, *_verdict;
729+
struct bpf_prog *orig;
732730

733-
_parse = xchg(&stab->bpf_parse, parse);
734-
_verdict = xchg(&stab->bpf_verdict, verdict);
731+
switch (type) {
732+
case BPF_SK_SKB_STREAM_PARSER:
733+
orig = xchg(&stab->bpf_parse, prog);
734+
break;
735+
case BPF_SK_SKB_STREAM_VERDICT:
736+
orig = xchg(&stab->bpf_verdict, prog);
737+
break;
738+
default:
739+
return -EOPNOTSUPP;
740+
}
735741

736-
if (_parse)
737-
bpf_prog_put(_parse);
738-
if (_verdict)
739-
bpf_prog_put(_verdict);
742+
if (orig)
743+
bpf_prog_put(orig);
740744

741745
return 0;
742746
}
@@ -777,7 +781,6 @@ const struct bpf_map_ops sock_map_ops = {
777781
.map_get_next_key = sock_map_get_next_key,
778782
.map_update_elem = sock_map_update_elem,
779783
.map_delete_elem = sock_map_delete_elem,
780-
.map_attach = sock_map_attach_prog,
781784
};
782785

783786
BPF_CALL_5(bpf_sock_map_update, struct bpf_sock_ops_kern *, bpf_sock,

kernel/bpf/syscall.c

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,12 +1093,12 @@ static int bpf_obj_get(const union bpf_attr *attr)
10931093

10941094
#ifdef CONFIG_CGROUP_BPF
10951095

1096-
#define BPF_PROG_ATTACH_LAST_FIELD attach_bpf_fd2
1096+
#define BPF_PROG_ATTACH_LAST_FIELD attach_flags
10971097

1098-
static int sockmap_get_from_fd(const union bpf_attr *attr, int ptype)
1098+
static int sockmap_get_from_fd(const union bpf_attr *attr)
10991099
{
1100-
struct bpf_prog *prog1, *prog2;
11011100
int ufd = attr->target_fd;
1101+
struct bpf_prog *prog;
11021102
struct bpf_map *map;
11031103
struct fd f;
11041104
int err;
@@ -1108,29 +1108,16 @@ static int sockmap_get_from_fd(const union bpf_attr *attr, int ptype)
11081108
if (IS_ERR(map))
11091109
return PTR_ERR(map);
11101110

1111-
if (!map->ops->map_attach) {
1112-
fdput(f);
1113-
return -EOPNOTSUPP;
1114-
}
1115-
1116-
prog1 = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1117-
if (IS_ERR(prog1)) {
1111+
prog = bpf_prog_get_type(attr->attach_bpf_fd, BPF_PROG_TYPE_SK_SKB);
1112+
if (IS_ERR(prog)) {
11181113
fdput(f);
1119-
return PTR_ERR(prog1);
1120-
}
1121-
1122-
prog2 = bpf_prog_get_type(attr->attach_bpf_fd2, ptype);
1123-
if (IS_ERR(prog2)) {
1124-
fdput(f);
1125-
bpf_prog_put(prog1);
1126-
return PTR_ERR(prog2);
1114+
return PTR_ERR(prog);
11271115
}
11281116

1129-
err = map->ops->map_attach(map, prog1, prog2);
1117+
err = sock_map_attach_prog(map, prog, attr->attach_type);
11301118
if (err) {
11311119
fdput(f);
1132-
bpf_prog_put(prog1);
1133-
bpf_prog_put(prog2);
1120+
bpf_prog_put(prog);
11341121
return err;
11351122
}
11361123

@@ -1165,16 +1152,13 @@ static int bpf_prog_attach(const union bpf_attr *attr)
11651152
case BPF_CGROUP_SOCK_OPS:
11661153
ptype = BPF_PROG_TYPE_SOCK_OPS;
11671154
break;
1168-
case BPF_CGROUP_SMAP_INGRESS:
1169-
ptype = BPF_PROG_TYPE_SK_SKB;
1170-
break;
1155+
case BPF_SK_SKB_STREAM_PARSER:
1156+
case BPF_SK_SKB_STREAM_VERDICT:
1157+
return sockmap_get_from_fd(attr);
11711158
default:
11721159
return -EINVAL;
11731160
}
11741161

1175-
if (attr->attach_type == BPF_CGROUP_SMAP_INGRESS)
1176-
return sockmap_get_from_fd(attr, ptype);
1177-
11781162
prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
11791163
if (IS_ERR(prog))
11801164
return PTR_ERR(prog);

samples/sockmap/sockmap_kern.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ int bpf_sockmap(struct bpf_sock_ops *skops)
8282
if (lport == 10000) {
8383
ret = 1;
8484
err = bpf_sock_map_update(skops, &sock_map, &ret,
85-
BPF_NOEXIST,
86-
BPF_SOCKMAP_STRPARSER);
85+
BPF_NOEXIST);
8786
bpf_printk("passive(%i -> %i) map ctx update err: %d\n",
8887
lport, bpf_ntohl(rport), err);
8988
}
@@ -95,8 +94,7 @@ int bpf_sockmap(struct bpf_sock_ops *skops)
9594
if (bpf_ntohl(rport) == 10001) {
9695
ret = 10;
9796
err = bpf_sock_map_update(skops, &sock_map, &ret,
98-
BPF_NOEXIST,
99-
BPF_SOCKMAP_STRPARSER);
97+
BPF_NOEXIST);
10098
bpf_printk("active(%i -> %i) map ctx update err: %d\n",
10199
lport, bpf_ntohl(rport), err);
102100
}

samples/sockmap/sockmap_user.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,16 @@ int main(int argc, char **argv)
256256
}
257257

258258
/* Attach programs to sockmap */
259-
err = __bpf_prog_attach(prog_fd[0], prog_fd[1], map_fd[0],
260-
BPF_CGROUP_SMAP_INGRESS, 0);
259+
err = bpf_prog_attach(prog_fd[0], map_fd[0],
260+
BPF_SK_SKB_STREAM_PARSER, 0);
261+
if (err) {
262+
fprintf(stderr, "ERROR: bpf_prog_attach (sockmap): %d (%s)\n",
263+
err, strerror(errno));
264+
return err;
265+
}
266+
267+
err = bpf_prog_attach(prog_fd[1], map_fd[0],
268+
BPF_SK_SKB_STREAM_VERDICT, 0);
261269
if (err) {
262270
fprintf(stderr, "ERROR: bpf_prog_attach (sockmap): %d (%s)\n",
263271
err, strerror(errno));

tools/include/uapi/linux/bpf.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ enum bpf_attach_type {
136136
BPF_CGROUP_INET_EGRESS,
137137
BPF_CGROUP_INET_SOCK_CREATE,
138138
BPF_CGROUP_SOCK_OPS,
139-
BPF_CGROUP_SMAP_INGRESS,
139+
BPF_SK_SKB_STREAM_PARSER,
140+
BPF_SK_SKB_STREAM_VERDICT,
140141
__MAX_BPF_ATTACH_TYPE
141142
};
142143

@@ -227,7 +228,6 @@ union bpf_attr {
227228
__u32 attach_bpf_fd; /* eBPF program to attach */
228229
__u32 attach_type;
229230
__u32 attach_flags;
230-
__u32 attach_bpf_fd2;
231231
};
232232

233233
struct { /* anonymous struct used by BPF_PROG_TEST_RUN command */
@@ -572,14 +572,11 @@ union bpf_attr {
572572
* @flags: reserved for future use
573573
* Return: SK_REDIRECT
574574
*
575-
* int bpf_sock_map_update(skops, map, key, flags, map_flags)
575+
* int bpf_sock_map_update(skops, map, key, flags)
576576
* @skops: pointer to bpf_sock_ops
577577
* @map: pointer to sockmap to update
578578
* @key: key to insert/update sock in map
579579
* @flags: same flags as map update elem
580-
* @map_flags: sock map specific flags
581-
* bit 1: Enable strparser
582-
* other bits: reserved
583580
*/
584581
#define __BPF_FUNC_MAPPER(FN) \
585582
FN(unspec), \

tools/lib/bpf/bpf.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -235,28 +235,20 @@ int bpf_obj_get(const char *pathname)
235235
return sys_bpf(BPF_OBJ_GET, &attr, sizeof(attr));
236236
}
237237

238-
int __bpf_prog_attach(int prog_fd1, int prog_fd2, int target_fd,
239-
enum bpf_attach_type type,
240-
unsigned int flags)
238+
int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type,
239+
unsigned int flags)
241240
{
242241
union bpf_attr attr;
243242

244243
bzero(&attr, sizeof(attr));
245244
attr.target_fd = target_fd;
246-
attr.attach_bpf_fd = prog_fd1;
247-
attr.attach_bpf_fd2 = prog_fd2;
245+
attr.attach_bpf_fd = prog_fd;
248246
attr.attach_type = type;
249247
attr.attach_flags = flags;
250248

251249
return sys_bpf(BPF_PROG_ATTACH, &attr, sizeof(attr));
252250
}
253251

254-
int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type,
255-
unsigned int flags)
256-
{
257-
return __bpf_prog_attach(prog_fd, 0, target_fd, type, flags);
258-
}
259-
260252
int bpf_prog_detach(int target_fd, enum bpf_attach_type type)
261253
{
262254
union bpf_attr attr;

tools/lib/bpf/bpf.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,6 @@ int bpf_obj_pin(int fd, const char *pathname);
5656
int bpf_obj_get(const char *pathname);
5757
int bpf_prog_attach(int prog_fd, int attachable_fd, enum bpf_attach_type type,
5858
unsigned int flags);
59-
int __bpf_prog_attach(int prog1, int prog2,
60-
int attachable_fd,
61-
enum bpf_attach_type type,
62-
unsigned int flags);
6359
int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type);
6460
int bpf_prog_test_run(int prog_fd, int repeat, void *data, __u32 size,
6561
void *data_out, __u32 *size_out, __u32 *retval,

tools/testing/selftests/bpf/bpf_helpers.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ static int (*bpf_setsockopt)(void *ctx, int level, int optname, void *optval,
6868
static int (*bpf_sk_redirect_map)(void *map, int key, int flags) =
6969
(void *) BPF_FUNC_sk_redirect_map;
7070
static int (*bpf_sock_map_update)(void *map, void *key, void *value,
71-
unsigned long long flags,
72-
unsigned long long map_lags) =
71+
unsigned long long flags) =
7372
(void *) BPF_FUNC_sock_map_update;
7473

7574

tools/testing/selftests/bpf/sockmap_parse_prog.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ int bpf_prog1(struct __sk_buff *skb)
3030
*/
3131
d[0] = 1;
3232

33-
bpf_printk("data[0] = (%u): local_port %i remote %i\n",
33+
bpf_printk("parse: data[0] = (%u): local_port %i remote %i\n",
3434
d[0], lport, bpf_ntohl(rport));
3535
return skb->len;
3636
}

tools/testing/selftests/bpf/sockmap_verdict_prog.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ int bpf_prog2(struct __sk_buff *skb)
4040
d[6] = 0xe;
4141
d[7] = 0xf;
4242

43-
bpf_printk("data[0] = (%u): local_port %i remote %i\n",
43+
bpf_printk("verdict: data[0] = (%u): local_port %i remote %i redirect 5\n",
4444
d[0], lport, bpf_ntohl(rport));
4545
return bpf_sk_redirect_map(&sock_map, 5, 0);
4646
}

0 commit comments

Comments
 (0)