1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16-
17- #include <bpf/xsk.h> // for xsk_setup_xdp_prog, bpf_set_link_xdp_fd
1816#include <linux/if_link.h> // for XDP_FLAGS_DRV_MODE
1917#include <net/if.h> // for if_nametoindex
18+ #include <xdp/libxdp.h>
19+ #include <xdp/xsk.h> // for xsk_setup_xdp_prog, bpf_set_link_xdp_fd
2020
2121#include "bpfWrapper.h"
2222#include "log.h"
@@ -40,12 +40,12 @@ int Load_bpf_send_xsk_map(char *ifname) {
4040 } else {
4141 Log_Info ("%s: if_index for interface %s is %d" , __FUNCTION__ , ifname , if_index );
4242 }
43-
4443 Log_Info ("%s: starting setup of xdp program on "
4544 "interface %s (%d)" ,
4645 __FUNCTION__ , ifname , if_index );
4746
4847 err = xsk_setup_xdp_prog (if_index , & fd );
48+ Log_Info ("Error value: %d" , err );
4949 if (err ) {
5050 Log_Error ("%s: setup of xdp program failed, "
5151 "returned: %d" ,
@@ -59,7 +59,7 @@ int Load_bpf_send_xsk_map(char *ifname) {
5959 __FUNCTION__ , ifname , if_index , fd );
6060 return fd ;
6161 }
62-
62+ Log_Info ( "FD value: %d" , fd );
6363 return -1 ;
6464}
6565
@@ -136,6 +136,7 @@ int Configure_busy_poll(int fd, int busy_timeout, int busy_budget) {
136136int Clean_bpf (char * ifname ) {
137137 int if_index , err ;
138138 int fd = -1 ;
139+ struct xdp_multiprog * mp = NULL ;
139140
140141 Log_Info ("%s: disovering if_index for interface %s" , __FUNCTION__ , ifname );
141142
@@ -150,19 +151,19 @@ int Clean_bpf(char *ifname) {
150151 Log_Info ("%s: starting removal of xdp program on interface %s (%d)" , __FUNCTION__ , ifname ,
151152 if_index );
152153
153- err = bpf_set_link_xdp_fd (if_index , fd , XDP_FLAGS_UPDATE_IF_NOEXIST );
154+ mp = xdp_multiprog__get_from_ifindex (if_index );
155+ if (!mp ) {
156+ Log_Error ("%s: unable to receive correct multi_prog reference : %s" , __FUNCTION__ ,
157+ mp );
158+ return -1 ;
159+ }
160+
161+ err = xdp_multiprog__detach (mp );
154162 if (err ) {
155- if (err == EBUSY_CODE_WARNING ) {
156- // unloading of XDP program found to return EBUSY error of -16 on certain
157- // host libbpf versions. doesn't break functionality and this problem is
158- // being investigated.
159- Log_Warning ("%s: Removal of xdp program is reporting error code: (%d)" ,
160- __FUNCTION__ , err );
161- } else {
162- Log_Error ("%s: Removal of xdp program failed, returned: (%d)" , __FUNCTION__ ,
163- err );
164- return 1 ;
165- }
163+ Log_Error ("%s: Removal of xdp program failed, returned: "
164+ "returned: %d" ,
165+ __FUNCTION__ , err );
166+ return -1 ;
166167 }
167168
168169 Log_Info ("%s: removed xdp program from interface %s (%d)" , __FUNCTION__ , ifname , if_index );
@@ -173,6 +174,7 @@ int Load_attach_bpf_xdp_pass(char *ifname) {
173174 int prog_fd = -1 , err , ifindex ;
174175 char * filename = "/afxdp/xdp_pass.o" ;
175176 struct bpf_object * obj ;
177+ struct xdp_program * prog ;
176178 __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST | XDP_FLAGS_DRV_MODE ;
177179
178180 Log_Info ("%s: disovering if_index for interface %s" , __FUNCTION__ , ifname );
@@ -190,17 +192,22 @@ int Load_attach_bpf_xdp_pass(char *ifname) {
190192 __FUNCTION__ , ifname , ifindex );
191193
192194 /* Load the BPF program */
193- err = bpf_prog_load (filename , BPF_PROG_TYPE_XDP , & obj , & prog_fd );
194- if (err < 0 ) {
195- Log_Error ("%s: Couldn't load BPF-OBJ file(%s)\n" , __FUNCTION__ , filename );
196- return -1 ;
195+ prog = xdp_program__open_file (filename , NULL , NULL );
196+ err = libxdp_get_error (prog );
197+ if (err ) {
198+ libxdp_strerror (err , "Couldn’t load XDP program" ,
199+ sizeof ("Couldn’t load XDP program" ));
200+ Log_Error ("%s: Couldn’t load XDP program\n" , __FUNCTION__ , filename );
201+ return err ;
197202 }
198203
199204 /* Attach the program to the interface at the xdp hook */
200- err = bpf_set_link_xdp_fd (ifindex , prog_fd , xdp_flags );
201- if (err < 0 ) {
205+ err = xdp_program__attach (prog , ifindex , XDP_FLAGS_UPDATE_IF_NOEXIST , 0 );
206+ if (err ) {
207+ libxdp_strerror (err , "Couldn't attach the xdp pass program" ,
208+ sizeof ("Couldn't attach the xdp pass program" ));
202209 Log_Error ("%s: Couldn't attach the XDP PASS PROGRAM TO %s\n" , __FUNCTION__ , ifname );
203- return -1 ;
210+ return err ;
204211 }
205212
206213 Log_Info ("%s: xdp-pass program loaded on %s (%d)" , __FUNCTION__ , ifname , ifindex );
0 commit comments