common.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Copyright (C) 2017 Netronome Systems, Inc.
  3. *
  4. * This software is dual licensed under the GNU General License Version 2,
  5. * June 1991 as shown in the file COPYING in the top-level directory of this
  6. * source tree or the BSD 2-Clause License provided below. You have the
  7. * option to license this software under the complete terms of either license.
  8. *
  9. * The BSD 2-Clause License:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * 2. Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. /* Author: Jakub Kicinski <kubakici@wp.pl> */
  34. #include <errno.h>
  35. #include <libgen.h>
  36. #include <stdbool.h>
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <unistd.h>
  41. #include <linux/limits.h>
  42. #include <linux/magic.h>
  43. #include <sys/types.h>
  44. #include <sys/vfs.h>
  45. #include <bpf.h>
  46. #include "main.h"
  47. static bool is_bpffs(char *path)
  48. {
  49. struct statfs st_fs;
  50. if (statfs(path, &st_fs) < 0)
  51. return false;
  52. return (unsigned long)st_fs.f_type == BPF_FS_MAGIC;
  53. }
  54. int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type)
  55. {
  56. enum bpf_obj_type type;
  57. int fd;
  58. fd = bpf_obj_get(path);
  59. if (fd < 0) {
  60. p_err("bpf obj get (%s): %s", path,
  61. errno == EACCES && !is_bpffs(dirname(path)) ?
  62. "directory not in bpf file system (bpffs)" :
  63. strerror(errno));
  64. return -1;
  65. }
  66. type = get_fd_type(fd);
  67. if (type < 0) {
  68. close(fd);
  69. return type;
  70. }
  71. if (type != exp_type) {
  72. p_err("incorrect object type: %s", get_fd_type_name(type));
  73. close(fd);
  74. return -1;
  75. }
  76. return fd;
  77. }
  78. int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32))
  79. {
  80. unsigned int id;
  81. char *endptr;
  82. int err;
  83. int fd;
  84. if (!is_prefix(*argv, "id")) {
  85. p_err("expected 'id' got %s", *argv);
  86. return -1;
  87. }
  88. NEXT_ARG();
  89. id = strtoul(*argv, &endptr, 0);
  90. if (*endptr) {
  91. p_err("can't parse %s as ID", *argv);
  92. return -1;
  93. }
  94. NEXT_ARG();
  95. if (argc != 1)
  96. usage();
  97. fd = get_fd_by_id(id);
  98. if (fd < 0) {
  99. p_err("can't get prog by id (%u): %s", id, strerror(errno));
  100. return -1;
  101. }
  102. err = bpf_obj_pin(fd, *argv);
  103. close(fd);
  104. if (err) {
  105. p_err("can't pin the object (%s): %s", *argv,
  106. errno == EACCES && !is_bpffs(dirname(*argv)) ?
  107. "directory not in bpf file system (bpffs)" :
  108. strerror(errno));
  109. return -1;
  110. }
  111. return 0;
  112. }
  113. const char *get_fd_type_name(enum bpf_obj_type type)
  114. {
  115. static const char * const names[] = {
  116. [BPF_OBJ_UNKNOWN] = "unknown",
  117. [BPF_OBJ_PROG] = "prog",
  118. [BPF_OBJ_MAP] = "map",
  119. };
  120. if (type < 0 || type >= ARRAY_SIZE(names) || !names[type])
  121. return names[BPF_OBJ_UNKNOWN];
  122. return names[type];
  123. }
  124. int get_fd_type(int fd)
  125. {
  126. char path[PATH_MAX];
  127. char buf[512];
  128. ssize_t n;
  129. snprintf(path, sizeof(path), "/proc/%d/fd/%d", getpid(), fd);
  130. n = readlink(path, buf, sizeof(buf));
  131. if (n < 0) {
  132. p_err("can't read link type: %s", strerror(errno));
  133. return -1;
  134. }
  135. if (n == sizeof(path)) {
  136. p_err("can't read link type: path too long!");
  137. return -1;
  138. }
  139. if (strstr(buf, "bpf-map"))
  140. return BPF_OBJ_MAP;
  141. else if (strstr(buf, "bpf-prog"))
  142. return BPF_OBJ_PROG;
  143. return BPF_OBJ_UNKNOWN;
  144. }
  145. char *get_fdinfo(int fd, const char *key)
  146. {
  147. char path[PATH_MAX];
  148. char *line = NULL;
  149. size_t line_n = 0;
  150. ssize_t n;
  151. FILE *fdi;
  152. snprintf(path, sizeof(path), "/proc/%d/fdinfo/%d", getpid(), fd);
  153. fdi = fopen(path, "r");
  154. if (!fdi) {
  155. p_err("can't open fdinfo: %s", strerror(errno));
  156. return NULL;
  157. }
  158. while ((n = getline(&line, &line_n, fdi))) {
  159. char *value;
  160. int len;
  161. if (!strstr(line, key))
  162. continue;
  163. fclose(fdi);
  164. value = strchr(line, '\t');
  165. if (!value || !value[1]) {
  166. p_err("malformed fdinfo!?");
  167. free(line);
  168. return NULL;
  169. }
  170. value++;
  171. len = strlen(value);
  172. memmove(line, value, len);
  173. line[len - 1] = '\0';
  174. return line;
  175. }
  176. p_err("key '%s' not found in fdinfo", key);
  177. free(line);
  178. fclose(fdi);
  179. return NULL;
  180. }
  181. void print_hex_data_json(uint8_t *data, size_t len)
  182. {
  183. unsigned int i;
  184. jsonw_start_array(json_wtr);
  185. for (i = 0; i < len; i++)
  186. jsonw_printf(json_wtr, "\"0x%02hhx\"", data[i]);
  187. jsonw_end_array(json_wtr);
  188. }