+static int
+load_custom_xdp_prog(const char *prog_path, int if_index)
+{
+ int ret, prog_fd = -1;
+ struct bpf_object *obj;
+ struct bpf_map *map;
+
+ ret = bpf_prog_load(prog_path, BPF_PROG_TYPE_XDP, &obj, &prog_fd);
+ if (ret) {
+ AF_XDP_LOG(ERR, "Failed to load program %s\n", prog_path);
+ return ret;
+ }
+
+ /*
+ * The loaded program must provision for a map of xsks, such that some
+ * traffic can be redirected to userspace. When the xsk is created,
+ * libbpf inserts it into the map.
+ */
+ map = bpf_object__find_map_by_name(obj, "xsks_map");
+ if (!map) {
+ AF_XDP_LOG(ERR, "Failed to find xsks_map in %s\n", prog_path);
+ return -1;
+ }
+
+ /* Link the program with the given network device */
+ ret = bpf_set_link_xdp_fd(if_index, prog_fd,
+ XDP_FLAGS_UPDATE_IF_NOEXIST);
+ if (ret) {
+ AF_XDP_LOG(ERR, "Failed to set prog fd %d on interface\n",
+ prog_fd);
+ return -1;
+ }
+
+ AF_XDP_LOG(INFO, "Successfully loaded XDP program %s with fd %d\n",
+ prog_path, prog_fd);
+
+ return 0;
+}
+