src/core/ngx_bpf.c - nginx source code

Functions defined

Macros defined

Source code


  1. /*
  2. * Copyright (C) Nginx, Inc.
  3. */


  4. #include <ngx_config.h>
  5. #include <ngx_core.h>

  6. #define NGX_BPF_LOGBUF_SIZE  (16 * 1024)


  7. static ngx_inline int
  8. ngx_bpf(enum bpf_cmd cmd, union bpf_attr *attr, unsigned int size)
  9. {
  10.     return syscall(__NR_bpf, cmd, attr, size);
  11. }


  12. void
  13. ngx_bpf_program_link(ngx_bpf_program_t *program, const char *symbol, int fd)
  14. {
  15.     ngx_uint_t        i;
  16.     ngx_bpf_reloc_t  *rl;

  17.     rl = program->relocs;

  18.     for (i = 0; i < program->nrelocs; i++) {
  19.         if (ngx_strcmp(rl[i].name, symbol) == 0) {
  20.             program->ins[rl[i].offset].src_reg = 1;
  21.             program->ins[rl[i].offset].imm = fd;
  22.         }
  23.     }
  24. }


  25. int
  26. ngx_bpf_load_program(ngx_log_t *log, ngx_bpf_program_t *program)
  27. {
  28.     int             fd;
  29.     union bpf_attr  attr;
  30. #if (NGX_DEBUG)
  31.     char            buf[NGX_BPF_LOGBUF_SIZE];
  32. #endif

  33.     ngx_memzero(&attr, sizeof(union bpf_attr));

  34.     attr.license = (uintptr_t) program->license;
  35.     attr.prog_type = program->type;
  36.     attr.insns = (uintptr_t) program->ins;
  37.     attr.insn_cnt = program->nins;

  38. #if (NGX_DEBUG)
  39.     /* for verifier errors */
  40.     attr.log_buf = (uintptr_t) buf;
  41.     attr.log_size = NGX_BPF_LOGBUF_SIZE;
  42.     attr.log_level = 1;
  43. #endif

  44.     fd = ngx_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
  45.     if (fd < 0) {
  46.         ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
  47.                       "failed to load BPF program");

  48.         ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0,
  49.                        "bpf verifier: %s", buf);

  50.         return -1;
  51.     }

  52.     return fd;
  53. }


  54. int
  55. ngx_bpf_map_create(ngx_log_t *log, enum bpf_map_type type, int key_size,
  56.     int value_size, int max_entries, uint32_t map_flags)
  57. {
  58.     int             fd;
  59.     union bpf_attr  attr;

  60.     ngx_memzero(&attr, sizeof(union bpf_attr));

  61.     attr.map_type = type;
  62.     attr.key_size = key_size;
  63.     attr.value_size = value_size;
  64.     attr.max_entries = max_entries;
  65.     attr.map_flags = map_flags;

  66.     fd = ngx_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
  67.     if (fd < 0) {
  68.         ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
  69.                       "failed to create BPF map");
  70.         return NGX_ERROR;
  71.     }

  72.     return fd;
  73. }


  74. int
  75. ngx_bpf_map_update(int fd, const void *key, const void *value, uint64_t flags)
  76. {
  77.     union bpf_attr attr;

  78.     ngx_memzero(&attr, sizeof(union bpf_attr));

  79.     attr.map_fd = fd;
  80.     attr.key = (uintptr_t) key;
  81.     attr.value = (uintptr_t) value;
  82.     attr.flags = flags;

  83.     return ngx_bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
  84. }


  85. int
  86. ngx_bpf_map_delete(int fd, const void *key)
  87. {
  88.     union bpf_attr attr;

  89.     ngx_memzero(&attr, sizeof(union bpf_attr));

  90.     attr.map_fd = fd;
  91.     attr.key = (uintptr_t) key;

  92.     return ngx_bpf(BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
  93. }


  94. int
  95. ngx_bpf_map_lookup(int fd, const void *key, void *value)
  96. {
  97.     union bpf_attr attr;

  98.     ngx_memzero(&attr, sizeof(union bpf_attr));

  99.     attr.map_fd = fd;
  100.     attr.key = (uintptr_t) key;
  101.     attr.value = (uintptr_t) value;

  102.     return ngx_bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
  103. }