@@ -990,6 +990,157 @@ impl_from_pin!(
990
990
Iter ,
991
991
) ;
992
992
993
+ macro_rules! impl_from_prog_info {
994
+ ( $( $struct_name: ident) ,+ $( , ) ?) => {
995
+ $(
996
+ impl $struct_name {
997
+ /// Consructs a program from a [ProgramInfo].
998
+ ///
999
+ /// This allows you to get a handle to an already loaded program
1000
+ /// from the kernel without having to load it again.
1001
+ ///
1002
+ /// # Errors
1003
+ ///
1004
+ /// - If the program type is not a match
1005
+ /// - If the file descriptor of the program cannot be cloned
1006
+ pub fn from_program_info(
1007
+ name: Option <String >,
1008
+ info: ProgramInfo ,
1009
+ ) -> Result <$struct_name, ProgramError > {
1010
+ if info. program_type( ) ? != ProgramType :: $struct_name {
1011
+ return Err ( ProgramError :: UnexpectedProgramType { } ) ;
1012
+ }
1013
+ Ok ( $struct_name {
1014
+ data: ProgramData :: from_bpf_prog_info(
1015
+ name,
1016
+ crate :: MockableFd :: from_fd( info. fd( ) ?. as_fd( ) . try_clone_to_owned( ) ?) ,
1017
+ Path :: new( "" ) ,
1018
+ info. 0 ,
1019
+ VerifierLogLevel :: default ( ) ,
1020
+ ) ?,
1021
+ } )
1022
+ }
1023
+ }
1024
+ ) +
1025
+ }
1026
+ }
1027
+
1028
+ macro_rules! impl_from_prog_info_attach_type {
1029
+ ( $( $struct_name: ident, $attach_type: ty) ,+ $( , ) ?) => {
1030
+ $(
1031
+ impl $struct_name {
1032
+
1033
+ /// Consructs a program from a [ProgramInfo].
1034
+ ///
1035
+ /// This allows you to get a handle to an already loaded program
1036
+ /// from the kernel without having to load it again.
1037
+ ///
1038
+ /// # Errors
1039
+ ///
1040
+ /// - If the program type is not a match
1041
+ /// - If the file descriptor of the program cannot be cloned
1042
+ pub fn from_program_info(
1043
+ name: Option <String >,
1044
+ info: ProgramInfo ,
1045
+ attach_type: $attach_type,
1046
+ ) -> Result <$struct_name, ProgramError > {
1047
+ if info. program_type( ) ? != ProgramType :: $struct_name {
1048
+ return Err ( ProgramError :: UnexpectedProgramType { } ) ;
1049
+ }
1050
+ Ok ( $struct_name {
1051
+ data: ProgramData :: from_bpf_prog_info(
1052
+ name,
1053
+ crate :: MockableFd :: from_fd( info. fd( ) ?. as_fd( ) . try_clone_to_owned( ) ?) ,
1054
+ Path :: new( "" ) ,
1055
+ info. 0 ,
1056
+ VerifierLogLevel :: default ( ) ,
1057
+ ) ?,
1058
+ attach_type,
1059
+ } )
1060
+ }
1061
+ }
1062
+ ) +
1063
+ }
1064
+ }
1065
+
1066
+ macro_rules! impl_from_prog_info_unsafe {
1067
+ ( $( $struct_name: ident, $prog_type: expr) ,+ $( , ) ?) => {
1068
+ $(
1069
+ impl $struct_name {
1070
+
1071
+ /// Consructs a program from a [ProgramInfo].
1072
+ ///
1073
+ /// This allows you to get a handle to an already loaded program
1074
+ /// from the kernel without having to load it again.
1075
+ ///
1076
+ /// # Errors
1077
+ ///
1078
+ /// - If the program type is not a match
1079
+ /// - If the file descriptor of the program cannot be cloned
1080
+ ///
1081
+ /// # Safety
1082
+ ///
1083
+ /// The type of program in the [ProgramInfo] may be
1084
+ /// ambiguous due to missing information in the kernel.
1085
+ /// The caller is responsible for ensuring that the program
1086
+ /// type is correct otherwise the behavior is undefined.
1087
+ pub unsafe fn from_program_info(
1088
+ name: Option <String >,
1089
+ info: ProgramInfo ,
1090
+ ) -> Result <$struct_name, ProgramError > {
1091
+ if info. program_type( ) ? != $prog_type {
1092
+ return Err ( ProgramError :: UnexpectedProgramType { } ) ;
1093
+ }
1094
+ Ok ( $struct_name {
1095
+ data: ProgramData :: from_bpf_prog_info(
1096
+ name,
1097
+ crate :: MockableFd :: from_fd( info. fd( ) ?. as_fd( ) . try_clone_to_owned( ) ?) ,
1098
+ Path :: new( "" ) ,
1099
+ info. 0 ,
1100
+ VerifierLogLevel :: default ( ) ,
1101
+ ) ?,
1102
+ } )
1103
+ }
1104
+ }
1105
+ ) +
1106
+ }
1107
+ }
1108
+
1109
+ impl_from_prog_info_unsafe ! (
1110
+ BtfTracePoint ,
1111
+ ProgramType :: Tracing ,
1112
+ FEntry ,
1113
+ ProgramType :: Tracing ,
1114
+ FExit ,
1115
+ ProgramType :: Tracing ,
1116
+ ) ;
1117
+
1118
+ impl_from_prog_info ! (
1119
+ CgroupDevice ,
1120
+ CgroupSysctl ,
1121
+ Extension ,
1122
+ LircMode2 ,
1123
+ Lsm ,
1124
+ PerfEvent ,
1125
+ RawTracePoint ,
1126
+ SkLookup ,
1127
+ SkMsg ,
1128
+ SockOps ,
1129
+ SocketFilter ,
1130
+ SchedClassifier ,
1131
+ ) ;
1132
+
1133
+ impl_from_prog_info_attach_type ! (
1134
+ CgroupSkb ,
1135
+ Option <CgroupSkbAttachType >,
1136
+ CgroupSockAddr ,
1137
+ CgroupSockAddrAttachType ,
1138
+ CgroupSock ,
1139
+ CgroupSockAttachType ,
1140
+ CgroupSockopt ,
1141
+ CgroupSockoptAttachType ,
1142
+ ) ;
1143
+
993
1144
macro_rules! impl_try_from_program {
994
1145
( $( $ty: ident) ,+ $( , ) ?) => {
995
1146
$(
0 commit comments