LCOV - code coverage report
Current view: top level - bpfilter/cgen - cgroup.c (source / functions) Coverage Total Hit
Test: coverage.lcov Lines: 100.0 % 35 35
Test Date: 2025-11-22 12:33:16 Functions: 100.0 % 5 5
Branches: 50.0 % 42 21

             Branch data     Line data    Source code
       1                 :             : /* SPDX-License-Identifier: GPL-2.0-only */
       2                 :             : /*
       3                 :             :  * Copyright (c) 2023 Meta Platforms, Inc. and affiliates.
       4                 :             :  */
       5                 :             : 
       6                 :             : #include "cgen/cgroup.h"
       7                 :             : 
       8                 :             : #include <linux/bpf_common.h>
       9                 :             : #include <linux/if_ether.h>
      10                 :             : 
      11                 :             : #include <stddef.h>
      12                 :             : #include <stdint.h>
      13                 :             : #include <sys/socket.h>
      14                 :             : 
      15                 :             : #include <bpfilter/btf.h>
      16                 :             : #include <bpfilter/flavor.h>
      17                 :             : #include <bpfilter/helper.h>
      18                 :             : #include <bpfilter/verdict.h>
      19                 :             : 
      20                 :             : #include "cgen/cgen.h"
      21                 :             : #include "cgen/program.h"
      22                 :             : #include "cgen/stub.h"
      23                 :             : #include "cgen/swich.h"
      24                 :             : #include "filter.h"
      25                 :             : #include "linux/bpf.h"
      26                 :             : 
      27                 :             : // Forward definition to avoid headers clusterfuck.
      28                 :             : uint16_t htons(uint16_t hostshort);
      29                 :             : 
      30                 :           4 : static int _bf_cgroup_gen_inline_prologue(struct bf_program *program)
      31                 :             : {
      32                 :             :     int offset;
      33                 :             :     int r;
      34                 :             : 
      35                 :             :     bf_assert(program);
      36                 :             : 
      37                 :             :     // Copy the packet size (+ETH_HLEN) into the runtime context
      38         [ -  + ]:           4 :     EMIT(program, BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
      39                 :             :                               offsetof(struct __sk_buff, len)));
      40         [ -  + ]:           4 :     EMIT(program, BPF_ALU64_IMM(BPF_ADD, BPF_REG_3, ETH_HLEN));
      41         [ -  + ]:           4 :     EMIT(program,
      42                 :             :          BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_3, BF_PROG_CTX_OFF(pkt_size)));
      43                 :             : 
      44                 :             :     /** The @c __sk_buff structure contains two fields related to the interface
      45                 :             :      * index: @c ingress_ifindex and @c ifindex . @c ingress_ifindex is the
      46                 :             :      * interface index the packet has been received on. However, we use
      47                 :             :      * @c ifindex which is the interface index the packet is processed by: if
      48                 :             :      * a packet is redirected locally from interface #1 to interface #2, then
      49                 :             :      * @c ingress_ifindex will contain @c 1 but @c ifindex will contains @c 2 .
      50                 :             :      * For egress, only @c ifindex is used.
      51                 :             :      */
      52         [ +  - ]:           4 :     if ((r = bf_btf_get_field_off("__sk_buff", "ifindex")) < 0)
      53                 :             :         return r;
      54         [ -  + ]:           4 :     EMIT(program, BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1, r));
      55         [ -  + ]:           4 :     EMIT(program,
      56                 :             :          BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_2, BF_PROG_CTX_OFF(ifindex)));
      57                 :             : 
      58                 :             :     /* BPF_PROG_TYPE_CGROUP_SKB doesn't provide access the the Ethernet header,
      59                 :             :      * so we can't parse it and discover the L3 protocol ID.
      60                 :             :      * Instead, we use the __sk_buff.family value and convert it to the
      61                 :             :      * corresponding ethertype. */
      62         [ +  - ]:           4 :     if ((offset = bf_btf_get_field_off("__sk_buff", "family")) < 0)
      63                 :             :         return offset;
      64         [ -  + ]:           4 :     EMIT(program, BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1, offset));
      65                 :             : 
      66                 :             :     {
      67                 :           4 :         _clean_bf_swich_ struct bf_swich swich =
      68         [ -  + ]:           4 :             bf_swich_get(program, BPF_REG_2);
      69                 :             : 
      70         [ -  + ]:           4 :         EMIT_SWICH_OPTION(&swich, AF_INET,
      71                 :             :                           BPF_MOV64_IMM(BPF_REG_7, htons(ETH_P_IP)));
      72         [ -  + ]:           4 :         EMIT_SWICH_OPTION(&swich, AF_INET6,
      73                 :             :                           BPF_MOV64_IMM(BPF_REG_7, htons(ETH_P_IPV6)));
      74         [ -  + ]:           4 :         EMIT_SWICH_DEFAULT(&swich, BPF_MOV64_IMM(BPF_REG_7, 0));
      75                 :             : 
      76                 :           4 :         r = bf_swich_generate(&swich);
      77         [ +  - ]:           4 :         if (r)
      78                 :             :             return r;
      79                 :             :     }
      80                 :             : 
      81         [ -  + ]:           4 :     EMIT(program, BPF_ST_MEM(BPF_W, BPF_REG_10, BF_PROG_CTX_OFF(l3_offset), 0));
      82                 :             : 
      83                 :           4 :     r = bf_stub_make_ctx_skb_dynptr(program, BPF_REG_1);
      84         [ +  - ]:           4 :     if (r)
      85                 :             :         return r;
      86                 :             : 
      87                 :           4 :     r = bf_stub_parse_l3_hdr(program);
      88         [ +  - ]:           4 :     if (r)
      89                 :             :         return r;
      90                 :             : 
      91                 :           4 :     r = bf_stub_parse_l4_hdr(program);
      92                 :             :     if (r)
      93                 :             :         return r;
      94                 :             : 
      95                 :             :     return 0;
      96                 :             : }
      97                 :             : 
      98                 :           4 : static int _bf_cgroup_gen_inline_epilogue(struct bf_program *program)
      99                 :             : {
     100                 :             :     UNUSED(program);
     101                 :             : 
     102                 :           4 :     return 0;
     103                 :             : }
     104                 :             : 
     105                 :           4 : static int _bf_cgroup_gen_inline_set_mark(struct bf_program *program,
     106                 :             :                                           uint32_t mark)
     107                 :             : {
     108         [ -  + ]:           4 :     EMIT(program,
     109                 :             :          BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_10, BF_PROG_CTX_OFF(arg)));
     110         [ -  + ]:           4 :     EMIT(program, BPF_MOV64_IMM(BPF_REG_2, mark));
     111         [ -  + ]:           4 :     EMIT(program, BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_2,
     112                 :             :                               offsetof(struct __sk_buff, mark)));
     113                 :             : 
     114                 :           4 :     return 0;
     115                 :             : }
     116                 :             : 
     117                 :           4 : static int _bf_cgroup_gen_inline_get_mark(struct bf_program *program, int reg)
     118                 :             : {
     119         [ -  + ]:           4 :     EMIT(program,
     120                 :             :          BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_10, BF_PROG_CTX_OFF(arg)));
     121         [ -  + ]:           4 :     EMIT(program,
     122                 :             :          BPF_LDX_MEM(BPF_W, reg, BPF_REG_1, offsetof(struct __sk_buff, mark)));
     123                 :             : 
     124                 :           4 :     return 0;
     125                 :             : }
     126                 :             : 
     127                 :             : /**
     128                 :             :  * Convert a standard verdict into a return value.
     129                 :             :  *
     130                 :             :  * @param verdict Verdict to convert. Must be valid.
     131                 :             :  * @return TC return code corresponding to the verdict, as an integer.
     132                 :             :  */
     133                 :          65 : static int _bf_cgroup_get_verdict(enum bf_verdict verdict)
     134                 :             : {
     135                 :             :     bf_assert(0 <= verdict && verdict < _BF_TERMINAL_VERDICT_MAX);
     136                 :             : 
     137                 :             :     static const int verdicts[] = {
     138                 :             :         [BF_VERDICT_ACCEPT] = 1,
     139                 :             :         [BF_VERDICT_DROP] = 0,
     140                 :             :     };
     141                 :             : 
     142                 :             :     static_assert(ARRAY_SIZE(verdicts) == _BF_TERMINAL_VERDICT_MAX);
     143                 :             : 
     144                 :          65 :     return verdicts[verdict];
     145                 :             : }
     146                 :             : 
     147                 :             : const struct bf_flavor_ops bf_flavor_ops_cgroup = {
     148                 :             :     .gen_inline_prologue = _bf_cgroup_gen_inline_prologue,
     149                 :             :     .gen_inline_epilogue = _bf_cgroup_gen_inline_epilogue,
     150                 :             :     .gen_inline_set_mark = _bf_cgroup_gen_inline_set_mark,
     151                 :             :     .gen_inline_get_mark = _bf_cgroup_gen_inline_get_mark,
     152                 :             :     .get_verdict = _bf_cgroup_get_verdict,
     153                 :             : };
        

Generated by: LCOV version 2.0-1