Lines Matching +full:can +full:- +full:fd

1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
84 /* Probe whether kernel switched from memlock-based (RLIMIT_MEMLOCK) to
85 * memcg-based memory accounting for BPF maps and programs. This was done in
86 * commit 97306be45fbe ("Merge branch 'switch to memcg-based memory
89 * Libbpf also offers to probe for memcg-based accounting vs rlimit, but does
93 * Instead, we can probe by lowering the process-based rlimit to 0, trying to
95 * memcg-based accounting is supported.
99 * that bpftool is single-threaded, this is fine to do here.
161 "mount --make-private %s failed: %s",
163 return -1;
168 "mount --bind %s %s failed: %s",
170 return -1;
177 snprintf(buff, bufflen, "mount -t %s %s %s failed: %s",
179 return -1;
192 err_str[ERR_MAX_LEN - 1] = '\0';
193 p_err("can't mount tracefs: %s", err_str);
203 int fd = -1;
212 fd = bpf_obj_get_opts(pname, opts);
213 if (fd < 0) {
225 return fd;
232 int fd;
234 fd = open_obj_pinned(path, false, opts);
235 if (fd < 0)
236 return -1;
238 type = get_fd_type(fd);
240 close(fd);
245 close(fd);
246 return -1;
249 return fd;
270 return -1;
281 if (access(parent_name, F_OK) == -1) {
282 p_err("can't create dir '%s' to pin BPF object: parent dir '%s' doesn't exist",
285 return -1;
292 p_err("no BPF file system found, not mounting it due to --nomount option");
293 return -1;
306 err_str[ERR_MAX_LEN - 1] = '\0';
307 p_err("can't mount BPF file system on given dir '%s': %s",
324 if (access(file_name, F_OK) != -1) {
325 p_err("can't pin BPF object: path '%s' already exists", file_name);
326 return -1;
332 return -1;
341 if (access(dir, F_OK) == -1) {
342 p_err("can't pin BPF object: dir '%s' doesn't exist", dir);
343 err = -1;
348 p_err("no BPF file system found, not mounting it due to --nomount option");
349 err = -1;
355 err_str[ERR_MAX_LEN - 1] = '\0';
356 p_err("can't mount BPF file system to pin the object '%s': %s",
365 int do_pin_fd(int fd, const char *name)
373 err = bpf_obj_pin(fd, name);
375 p_err("can't pin the object (%s): %s", name, strerror(errno));
383 int fd;
386 return -EINVAL;
388 fd = get_fd(&argc, &argv);
389 if (fd < 0)
390 return fd;
392 err = do_pin_fd(fd, *argv);
394 close(fd);
416 const char *prog_name = prog_info->name;
424 strlen(prog_info->name) < BPF_OBJ_NAME_LEN - 1)
427 if (!prog_info->btf_id || prog_info->nr_func_info == 0)
431 info.func_info_rec_size = prog_info->func_info_rec_size;
447 prog_name = btf__name_by_offset(prog_btf, func_type->name_off);
456 int get_fd_type(int fd)
462 snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);
466 p_err("can't read link type: %s", strerror(errno));
467 return -1;
470 p_err("can't read link type: path too long!");
471 return -1;
475 if (strstr(buf, "bpf-map"))
477 else if (strstr(buf, "bpf-prog"))
479 else if (strstr(buf, "bpf-link"))
485 char *get_fdinfo(int fd, const char *key)
493 snprintf(path, sizeof(path), "/proc/self/fdinfo/%d", fd);
517 line[len - 1] = '\0';
557 int fd, err = 0;
563 fd = open_obj_pinned(fpath, true, NULL);
564 if (fd < 0)
567 objtype = get_fd_type(fd);
572 if (bpf_prog_get_info_by_fd(fd, &pinned_info, &len))
577 err = -1;
590 close(fd);
606 return -1;
612 char *path = mntent->mnt_dir;
614 if (strncmp(mntent->mnt_type, "bpf", 3) != 0)
633 free(entry->pvalue);
652 p_err("Can't get # of possible cpus: %s", strerror(-cpus));
653 exit(-1);
666 p_err("Can't stat /proc/self: %s", strerror(errno));
680 int fd;
682 fd = open(path, O_RDONLY);
683 if (fd < 0) {
684 p_err("Can't open %s: %s", path, strerror(errno));
685 return -1;
688 len = read(fd, vendor_id_buf, sizeof(vendor_id_buf));
689 close(fd);
691 p_err("Can't read %s: %s", path, strerror(errno));
692 return -1;
696 return -1;
722 p_err("Can't get net device name for ifindex %u: %s", ifindex,
729 p_err("Can't get device vendor id for %s", devname);
740 p_info("Unknown NFP device ID, assuming it is NFP-6xxx arch");
742 return "NFP-6xxx";
746 p_err("Can't get arch name for device vendor id 0x%04x",
792 return -1;
797 p_err("can't parse %s as %s", **argv, what);
798 return -1;
816 int fd, nb_fds = 0;
833 fd = bpf_prog_get_fd_by_id(id);
834 if (fd < 0) {
835 p_err("can't get prog by id (%u): %s",
840 err = bpf_prog_get_info_by_fd(fd, &info, &len);
842 p_err("can't get prog info (%u): %s",
848 close(fd);
853 get_prog_full_name(&info, fd, prog_name,
856 close(fd);
869 (*fds)[nb_fds++] = fd;
873 close(fd);
875 while (--nb_fds >= 0)
877 return -1;
890 p_err("can't parse %s as ID", **argv);
891 return -1;
898 return -1;
909 p_err("can't parse tag");
910 return -1;
921 if (strlen(name) > MAX_PROG_FULL_NAME - 1) {
922 p_err("can't parse name");
923 return -1;
938 return -1;
943 return -1;
949 int nb_fds, fd;
954 return -1;
960 while (nb_fds--)
963 fd = -1;
967 fd = fds[0];
970 return fd;
977 int fd, nb_fds = 0;
995 /* Request a read-only fd to query the map info */
997 fd = bpf_map_get_fd_by_id_opts(id, &opts_ro);
998 if (fd < 0) {
999 p_err("can't get map by id (%u): %s",
1004 err = bpf_map_get_info_by_fd(fd, &info, &len);
1006 p_err("can't get map info (%u): %s",
1012 close(fd);
1016 /* Get an fd with the requested options, if they differ
1017 * from the read-only options used to get the fd above.
1020 close(fd);
1021 fd = bpf_map_get_fd_by_id_opts(id, opts);
1022 if (fd < 0) {
1023 p_err("can't get map by id (%u): %s", id,
1037 (*fds)[nb_fds++] = fd;
1041 close(fd);
1043 while (--nb_fds >= 0)
1045 return -1;
1063 p_err("can't parse %s as ID", **argv);
1064 return -1;
1071 return -1;
1080 if (strlen(name) > BPF_OBJ_NAME_LEN - 1) {
1081 p_err("can't parse name");
1082 return -1;
1099 return -1;
1104 return -1;
1110 int nb_fds, fd;
1115 return -1;
1121 while (nb_fds--)
1124 fd = -1;
1128 fd = fds[0];
1131 return fd;
1138 int fd;
1140 fd = map_parse_fd(argc, argv, open_flags);
1141 if (fd < 0)
1142 return -1;
1144 err = bpf_map_get_info_by_fd(fd, info, info_len);
1146 p_err("can't get map info: %s", strerror(errno));
1147 close(fd);
1151 return fd;
1207 return -EINVAL;
1209 return -ENAMETOOLONG;
1228 buf[strlen(buf) - 1] = '\0';
1255 return -1;
1258 snprintf(path, sizeof(path), "/boot/config-%s", utsn.release);
1272 p_info("skipping kernel config, can't open file: %s",
1274 return -1;
1278 p_info("skipping kernel config, can't read from file: %s",
1280 ret = -1;
1285 p_info("skipping kernel config, can't find correct file");
1286 ret = -1;