33#include <errno.h>
44#include <string.h>
55
6+ #include <bpf/libbpf.h>
67#include <xdp/libxdp.h>
78
89#include "params.h"
910#include "util.h"
1011#include "logging.h"
1112#include "compat.h"
1213
14+ #include "xdp-forward.h"
1315#include "xdp_forward.skel.h"
1416
1517#define MAX_IFACE_NUM 32
@@ -81,6 +83,32 @@ static int find_prog(struct iface *iface, bool detach)
8183 return ret ;
8284}
8385
86+ int init_tx_port (struct xdp_program * init_prog , int ifindex )
87+ {
88+ struct port_init_config port_cfg = {.ifindex = ifindex };
89+ struct xdp_md ctx_in = {
90+ .data_end = sizeof (port_cfg ),
91+ };
92+ DECLARE_LIBBPF_OPTS (bpf_test_run_opts , opts , .data_in = & port_cfg ,
93+ .data_size_in = sizeof (port_cfg ), .ctx_in = & ctx_in ,
94+ .ctx_size_in = sizeof (ctx_in ), );
95+ int nr_cpus = libbpf_num_possible_cpus ();
96+ int err , i ;
97+
98+ for (i = 0 ; i < nr_cpus ; i ++ ) {
99+ port_cfg .cpu = i ;
100+
101+ err = xdp_program__test_run (init_prog , & opts , 0 );
102+ if (err )
103+ return - errno ;
104+
105+ if (opts .retval != XDP_PASS )
106+ return -1 ;
107+ }
108+
109+ return 0 ;
110+ }
111+
84112struct load_opts {
85113 enum fwd_mode fwd_mode ;
86114 enum xdp_attach_mode xdp_mode ;
@@ -110,10 +138,9 @@ struct prog_option load_options[] = {
110138
111139static int do_load (const void * cfg , __unused const char * pin_root_path )
112140{
141+ struct xdp_program * xdp_prog = NULL , * init_prog = NULL ;
113142 DECLARE_LIBBPF_OPTS (xdp_program_opts , opts );
114- struct xdp_program * xdp_prog = NULL ;
115143 const struct load_opts * opt = cfg ;
116- struct bpf_program * prog = NULL ;
117144 struct xdp_forward * skel ;
118145 int ret = EXIT_FAILURE ;
119146 struct iface * iface ;
@@ -135,12 +162,6 @@ static int do_load(const void *cfg, __unused const char *pin_root_path)
135162 goto end ;
136163 }
137164
138- /* Make sure we only load the one XDP program we are interested in */
139- while ((prog = bpf_object__next_program (skel -> obj , prog )) != NULL )
140- if (bpf_program__type (prog ) == BPF_PROG_TYPE_XDP &&
141- bpf_program__expected_attach_type (prog ) == BPF_XDP )
142- bpf_program__set_autoload (prog , false);
143-
144165 opts .obj = skel -> obj ;
145166 xdp_prog = xdp_program__create (& opts );
146167 if (!xdp_prog ) {
@@ -150,6 +171,15 @@ static int do_load(const void *cfg, __unused const char *pin_root_path)
150171 goto end_destroy ;
151172 }
152173
174+ opts .prog_name = "init_port" ;
175+ init_prog = xdp_program__create (& opts );
176+ if (!init_prog ) {
177+ ret = - errno ;
178+ pr_warn ("Couldn't open XDP program: %s\n" ,
179+ strerror (- ret ));
180+ goto end_destroy ;
181+ }
182+
153183 /* We always set the frags support bit: nothing the program does is
154184 * incompatible with multibuf, and it's perfectly fine to load a program
155185 * with frags support on an interface with a small MTU. We don't risk
@@ -177,13 +207,13 @@ static int do_load(const void *cfg, __unused const char *pin_root_path)
177207 goto end_detach ;
178208 }
179209
180- ret = bpf_map_update_elem (bpf_map__fd (skel -> maps .xdp_tx_ports ),
181- & iface -> ifindex , & iface -> ifindex , 0 );
210+ ret = init_tx_port (init_prog , iface -> ifindex );
182211 if (ret ) {
183- pr_warn ("Failed to update devmap value : %s\n" ,
212+ pr_warn ("Failed to initiate TX port : %s\n" ,
184213 strerror (errno ));
185214 goto end_detach ;
186215 }
216+
187217 pr_info ("Loaded on interface %s\n" , iface -> ifname );
188218 }
189219
0 commit comments