@@ -24,6 +24,7 @@ enum sockopt_test_error {
24
24
static struct sockopt_test {
25
25
const char * descr ;
26
26
const struct bpf_insn insns [64 ];
27
+ enum bpf_prog_type prog_type ;
27
28
enum bpf_attach_type attach_type ;
28
29
enum bpf_attach_type expected_attach_type ;
29
30
@@ -928,9 +929,40 @@ static struct sockopt_test {
928
929
929
930
.error = EPERM_SETSOCKOPT ,
930
931
},
932
+
933
+ /* ==================== prog_type ==================== */
934
+
935
+ {
936
+ .descr = "can attach only BPF_CGROUP_SETSOCKOP" ,
937
+ .insns = {
938
+ /* return 1 */
939
+ BPF_MOV64_IMM (BPF_REG_0 , 1 ),
940
+ BPF_EXIT_INSN (),
941
+
942
+ },
943
+ .prog_type = BPF_PROG_TYPE_CGROUP_SKB ,
944
+ .attach_type = BPF_CGROUP_SETSOCKOPT ,
945
+ .expected_attach_type = 0 ,
946
+ .error = DENY_ATTACH ,
947
+ },
948
+
949
+ {
950
+ .descr = "can attach only BPF_CGROUP_GETSOCKOP" ,
951
+ .insns = {
952
+ /* return 1 */
953
+ BPF_MOV64_IMM (BPF_REG_0 , 1 ),
954
+ BPF_EXIT_INSN (),
955
+
956
+ },
957
+ .prog_type = BPF_PROG_TYPE_CGROUP_SKB ,
958
+ .attach_type = BPF_CGROUP_GETSOCKOPT ,
959
+ .expected_attach_type = 0 ,
960
+ .error = DENY_ATTACH ,
961
+ },
931
962
};
932
963
933
964
static int load_prog (const struct bpf_insn * insns ,
965
+ enum bpf_prog_type prog_type ,
934
966
enum bpf_attach_type expected_attach_type )
935
967
{
936
968
LIBBPF_OPTS (bpf_prog_load_opts , opts ,
@@ -947,7 +979,7 @@ static int load_prog(const struct bpf_insn *insns,
947
979
}
948
980
insns_cnt ++ ;
949
981
950
- fd = bpf_prog_load (BPF_PROG_TYPE_CGROUP_SOCKOPT , NULL , "GPL" , insns , insns_cnt , & opts );
982
+ fd = bpf_prog_load (prog_type , NULL , "GPL" , insns , insns_cnt , & opts );
951
983
if (verbose && fd < 0 )
952
984
fprintf (stderr , "%s\n" , bpf_log_buf );
953
985
@@ -1036,13 +1068,18 @@ static int call_getsockopt(bool use_io_uring, int fd, int level, int optname,
1036
1068
return getsockopt (fd , level , optname , optval , optlen );
1037
1069
}
1038
1070
1039
- static int run_test (int cgroup_fd , struct sockopt_test * test , bool use_io_uring )
1071
+ static int run_test (int cgroup_fd , struct sockopt_test * test , bool use_io_uring ,
1072
+ bool use_link )
1040
1073
{
1041
- int sock_fd , err , prog_fd ;
1074
+ int prog_type = BPF_PROG_TYPE_CGROUP_SOCKOPT ;
1075
+ int sock_fd , err , prog_fd , link_fd = -1 ;
1042
1076
void * optval = NULL ;
1043
1077
int ret = 0 ;
1044
1078
1045
- prog_fd = load_prog (test -> insns , test -> expected_attach_type );
1079
+ if (test -> prog_type )
1080
+ prog_type = test -> prog_type ;
1081
+
1082
+ prog_fd = load_prog (test -> insns , prog_type , test -> expected_attach_type );
1046
1083
if (prog_fd < 0 ) {
1047
1084
if (test -> error == DENY_LOAD )
1048
1085
return 0 ;
@@ -1051,7 +1088,12 @@ static int run_test(int cgroup_fd, struct sockopt_test *test, bool use_io_uring)
1051
1088
return -1 ;
1052
1089
}
1053
1090
1054
- err = bpf_prog_attach (prog_fd , cgroup_fd , test -> attach_type , 0 );
1091
+ if (use_link ) {
1092
+ err = bpf_link_create (prog_fd , cgroup_fd , test -> attach_type , NULL );
1093
+ link_fd = err ;
1094
+ } else {
1095
+ err = bpf_prog_attach (prog_fd , cgroup_fd , test -> attach_type , 0 );
1096
+ }
1055
1097
if (err < 0 ) {
1056
1098
if (test -> error == DENY_ATTACH )
1057
1099
goto close_prog_fd ;
@@ -1142,7 +1184,12 @@ static int run_test(int cgroup_fd, struct sockopt_test *test, bool use_io_uring)
1142
1184
close_sock_fd :
1143
1185
close (sock_fd );
1144
1186
detach_prog :
1145
- bpf_prog_detach2 (prog_fd , cgroup_fd , test -> attach_type );
1187
+ if (use_link ) {
1188
+ if (link_fd >= 0 )
1189
+ close (link_fd );
1190
+ } else {
1191
+ bpf_prog_detach2 (prog_fd , cgroup_fd , test -> attach_type );
1192
+ }
1146
1193
close_prog_fd :
1147
1194
close (prog_fd );
1148
1195
return ret ;
@@ -1160,10 +1207,12 @@ void test_sockopt(void)
1160
1207
if (!test__start_subtest (tests [i ].descr ))
1161
1208
continue ;
1162
1209
1163
- ASSERT_OK (run_test (cgroup_fd , & tests [i ], false),
1210
+ ASSERT_OK (run_test (cgroup_fd , & tests [i ], false, false),
1211
+ tests [i ].descr );
1212
+ ASSERT_OK (run_test (cgroup_fd , & tests [i ], false, true),
1164
1213
tests [i ].descr );
1165
1214
if (tests [i ].io_uring_support )
1166
- ASSERT_OK (run_test (cgroup_fd , & tests [i ], true),
1215
+ ASSERT_OK (run_test (cgroup_fd , & tests [i ], true, false ),
1167
1216
tests [i ].descr );
1168
1217
}
1169
1218
0 commit comments