Microsoft MVP성태의 닷넷 이야기
Linux: 124. eBPF - __sk_buff / sk_buff 구조체 [링크 복사], [링크+제목 복사],
조회: 773
글쓴 사람
정성태 (seongtaejeong at gmail.com)
홈페이지
첨부 파일
 

eBPF - __sk_buff / sk_buff 구조체

지난 글에서 설명한,

eBPF (bpf2go) - BPF_PROG_TYPE_SOCKET_FILTER 예제 - SEC("socket")
; https://www.sysnet.pe.kr/2/0/14017

BPF_PROG_TYPE_SOCKET_FILTER 프로그램에서 __sk_buff 구조체를 사용했었는데요,

SEC("socket")
int socket_handler(struct __sk_buff *skb)
{
    // Your eBPF program logic here
    return skb->len;
}

이에 대한 자료를 찾아보면,

Understanding struct __sk_buff
; https://wangcong.org/2021-08-15-understanding-struct-__sk_buff.html

Program context __sk_buff
; https://docs.ebpf.io/linux/program-context/__sk_buff/

원래의 커널 구조체는 sk_buff이고, eBPF 로더가 런타임 시에 __sk_buff의 필드에 대한 접근을 sk_buff로 변환해 준다고 합니다. 이에 대한 변환 코드를 bpf_convert_ctx_access 함수에서 확인할 수 있는데,

// https://elixir.bootlin.com/linux/v5.13.10/source/net/core/filter.c#L8411

static u32 bpf_convert_ctx_access(enum bpf_access_type type,
                  const struct bpf_insn *si,
                  struct bpf_insn *insn_buf,
                  struct bpf_prog *prog, u32 *target_size)
{
    struct bpf_insn *insn = insn_buf;
    int off;

    switch (si->off) {
    case offsetof(struct __sk_buff, len):
        *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
                      bpf_target_off(struct sk_buff, len, 4,
                             target_size));
        break;

    case offsetof(struct __sk_buff, protocol):
        *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
                      bpf_target_off(struct sk_buff, protocol, 2,
                             target_size));
        break;

    case offsetof(struct __sk_buff, vlan_proto):
        *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
                      bpf_target_off(struct sk_buff, vlan_proto, 2,
                             target_size));
        break;

    case offsetof(struct __sk_buff, priority):
        if (type == BPF_WRITE)
            *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
                          bpf_target_off(struct sk_buff, priority, 4,
                                 target_size));
        else
            *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
                          bpf_target_off(struct sk_buff, priority, 4,
                                 target_size));
        break;

    case offsetof(struct __sk_buff, ingress_ifindex):
        *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
                      bpf_target_off(struct sk_buff, skb_iif, 4,
                             target_size));
        break;

    case offsetof(struct __sk_buff, ifindex):
        *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
                      si->dst_reg, si->src_reg,
                      offsetof(struct sk_buff, dev));
        *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
        *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
                      bpf_target_off(struct net_device, ifindex, 4,
                             target_size));
        break;

    case offsetof(struct __sk_buff, hash):
        *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
                      bpf_target_off(struct sk_buff, hash, 4,
                             target_size));
        break;

    case offsetof(struct __sk_buff, mark):
        if (type == BPF_WRITE)
            *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
                          bpf_target_off(struct sk_buff, mark, 4,
                                 target_size));
        else
            *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
                          bpf_target_off(struct sk_buff, mark, 4,
                                 target_size));
        break;

    case offsetof(struct __sk_buff, pkt_type):
        *target_size = 1;
        *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
                      PKT_TYPE_OFFSET());
        *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX);
#ifdef __BIG_ENDIAN_BITFIELD
        *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5);
#endif
        break;

    case offsetof(struct __sk_buff, queue_mapping):
        if (type == BPF_WRITE) {
            *insn++ = BPF_JMP_IMM(BPF_JGE, si->src_reg, NO_QUEUE_MAPPING, 1);
            *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
                          bpf_target_off(struct sk_buff,
                                 queue_mapping,
                                 2, target_size));
        } else {
            *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
                          bpf_target_off(struct sk_buff,
                                 queue_mapping,
                                 2, target_size));
        }
        break;

    case offsetof(struct __sk_buff, vlan_present):
        *target_size = 1;
        *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
                      PKT_VLAN_PRESENT_OFFSET());
        if (PKT_VLAN_PRESENT_BIT)
            *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, PKT_VLAN_PRESENT_BIT);
        if (PKT_VLAN_PRESENT_BIT < 7)
            *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, 1);
        break;

    case offsetof(struct __sk_buff, vlan_tci):
        *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
                      bpf_target_off(struct sk_buff, vlan_tci, 2,
                             target_size));
        break;

    case offsetof(struct __sk_buff, cb[0]) ...
         offsetofend(struct __sk_buff, cb[4]) - 1:
        BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, data) < 20);
        BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
                  offsetof(struct qdisc_skb_cb, data)) %
                 sizeof(__u64));

        prog->cb_access = 1;
        off  = si->off;
        off -= offsetof(struct __sk_buff, cb[0]);
        off += offsetof(struct sk_buff, cb);
        off += offsetof(struct qdisc_skb_cb, data);
        if (type == BPF_WRITE)
            *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg,
                          si->src_reg, off);
        else
            *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
                          si->src_reg, off);
        break;

    case offsetof(struct __sk_buff, tc_classid):
        BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, tc_classid) != 2);

        off  = si->off;
        off -= offsetof(struct __sk_buff, tc_classid);
        off += offsetof(struct sk_buff, cb);
        off += offsetof(struct qdisc_skb_cb, tc_classid);
        *target_size = 2;
        if (type == BPF_WRITE)
            *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg,
                          si->src_reg, off);
        else
            *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg,
                          si->src_reg, off);
        break;

    case offsetof(struct __sk_buff, data):
        *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
                      si->dst_reg, si->src_reg,
                      offsetof(struct sk_buff, data));
        break;

    case offsetof(struct __sk_buff, data_meta):
        off  = si->off;
        off -= offsetof(struct __sk_buff, data_meta);
        off += offsetof(struct sk_buff, cb);
        off += offsetof(struct bpf_skb_data_end, data_meta);
        *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
                      si->src_reg, off);
        break;

    case offsetof(struct __sk_buff, data_end):
        off  = si->off;
        off -= offsetof(struct __sk_buff, data_end);
        off += offsetof(struct sk_buff, cb);
        off += offsetof(struct bpf_skb_data_end, data_end);
        *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
                      si->src_reg, off);
        break;

    case offsetof(struct __sk_buff, tc_index):
#ifdef CONFIG_NET_SCHED
        if (type == BPF_WRITE)
            *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
                          bpf_target_off(struct sk_buff, tc_index, 2,
                                 target_size));
        else
            *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
                          bpf_target_off(struct sk_buff, tc_index, 2,
                                 target_size));
#else
        *target_size = 2;
        if (type == BPF_WRITE)
            *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg);
        else
            *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
#endif
        break;

    case offsetof(struct __sk_buff, napi_id):
#if defined(CONFIG_NET_RX_BUSY_POLL)
        *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
                      bpf_target_off(struct sk_buff, napi_id, 4,
                             target_size));
        *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1);
        *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
#else
        *target_size = 4;
        *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
#endif
        break;
    case offsetof(struct __sk_buff, family):
        BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2);

        *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
                      si->dst_reg, si->src_reg,
                      offsetof(struct sk_buff, sk));
        *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
                      bpf_target_off(struct sock_common,
                             skc_family,
                             2, target_size));
        break;
    case offsetof(struct __sk_buff, remote_ip4):
        BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4);

        *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
                      si->dst_reg, si->src_reg,
                      offsetof(struct sk_buff, sk));
        *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
                      bpf_target_off(struct sock_common,
                             skc_daddr,
                             4, target_size));
        break;
    case offsetof(struct __sk_buff, local_ip4):
        BUILD_BUG_ON(sizeof_field(struct sock_common,
                      skc_rcv_saddr) != 4);

        *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
                      si->dst_reg, si->src_reg,
                      offsetof(struct sk_buff, sk));
        *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
                      bpf_target_off(struct sock_common,
                             skc_rcv_saddr,
                             4, target_size));
        break;
    case offsetof(struct __sk_buff, remote_ip6[0]) ...
         offsetof(struct __sk_buff, remote_ip6[3]):
#if IS_ENABLED(CONFIG_IPV6)
        BUILD_BUG_ON(sizeof_field(struct sock_common,
                      skc_v6_daddr.s6_addr32[0]) != 4);

        off = si->off;
        off -= offsetof(struct __sk_buff, remote_ip6[0]);

        *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
                      si->dst_reg, si->src_reg,
                      offsetof(struct sk_buff, sk));
        *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
                      offsetof(struct sock_common,
                           skc_v6_daddr.s6_addr32[0]) +
                      off);
#else
        *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
#endif
        break;
    case offsetof(struct __sk_buff, local_ip6[0]) ...
         offsetof(struct __sk_buff, local_ip6[3]):
#if IS_ENABLED(CONFIG_IPV6)
        BUILD_BUG_ON(sizeof_field(struct sock_common,
                      skc_v6_rcv_saddr.s6_addr32[0]) != 4);

        off = si->off;
        off -= offsetof(struct __sk_buff, local_ip6[0]);

        *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
                      si->dst_reg, si->src_reg,
                      offsetof(struct sk_buff, sk));
        *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
                      offsetof(struct sock_common,
                           skc_v6_rcv_saddr.s6_addr32[0]) +
                      off);
#else
        *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
#endif
        break;

    case offsetof(struct __sk_buff, remote_port):
        BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2);

        *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
                      si->dst_reg, si->src_reg,
                      offsetof(struct sk_buff, sk));
        *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
                      bpf_target_off(struct sock_common,
                             skc_dport,
                             2, target_size));
#ifndef __BIG_ENDIAN_BITFIELD
        *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
#endif
        break;

    case offsetof(struct __sk_buff, local_port):
        BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2);

        *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
                      si->dst_reg, si->src_reg,
                      offsetof(struct sk_buff, sk));
        *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
                      bpf_target_off(struct sock_common,
                             skc_num, 2, target_size));
        break;

    case offsetof(struct __sk_buff, tstamp):
        BUILD_BUG_ON(sizeof_field(struct sk_buff, tstamp) != 8);

        if (type == BPF_WRITE)
            *insn++ = BPF_STX_MEM(BPF_DW,
                          si->dst_reg, si->src_reg,
                          bpf_target_off(struct sk_buff,
                                 tstamp, 8,
                                 target_size));
        else
            *insn++ = BPF_LDX_MEM(BPF_DW,
                          si->dst_reg, si->src_reg,
                          bpf_target_off(struct sk_buff,
                                 tstamp, 8,
                                 target_size));
        break;

    case offsetof(struct __sk_buff, gso_segs):
        insn = bpf_convert_shinfo_access(si, insn);
        *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct skb_shared_info, gso_segs),
                      si->dst_reg, si->dst_reg,
                      bpf_target_off(struct skb_shared_info,
                             gso_segs, 2,
                             target_size));
        break;
    case offsetof(struct __sk_buff, gso_size):
        insn = bpf_convert_shinfo_access(si, insn);
        *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct skb_shared_info, gso_size),
                      si->dst_reg, si->dst_reg,
                      bpf_target_off(struct skb_shared_info,
                             gso_size, 2,
                             target_size));
        break;
    case offsetof(struct __sk_buff, wire_len):
        BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, pkt_len) != 4);

        off = si->off;
        off -= offsetof(struct __sk_buff, wire_len);
        off += offsetof(struct sk_buff, cb);
        off += offsetof(struct qdisc_skb_cb, pkt_len);
        *target_size = 4;
        *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, off);
        break;

    case offsetof(struct __sk_buff, sk):
        *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
                      si->dst_reg, si->src_reg,
                      offsetof(struct sk_buff, sk));
        break;
    }

    return insn - insn_buf;
}

예를 들어, __sk_buff.gso_size 필드는 sk_buff 구조체의 skb_shared_info.gso_size 필드로 변환되는 식입니다.




그런데, 좀 모호한 면도 있습니다. 가령, __sk_buff.data 필드는 "__u32" 타입이지만 그것의 대응이 되는 sk_buff.data 필드는 포인터에 해당하는 "unsigned char *" 타입입니다. 그래서인지 모르겠지만 __sk_buff.data 필드를 직접 접근하면,

SEC("socket")
int socket_handler(struct __sk_buff *ctx)
{
    bpf_printk("[__sk_buff] data == %d\n", ctx->data);
    // ...[생략]...
}

eBPF 로딩 시 이런 오류가 발생합니다. (이에 대해서는 아래에서 더 다룰 것입니다.)

// 테스트 환경: Ubuntu 24.04 LTS

load program: permission denied: 0: (61) r3 = *(u32 *)(r1 +76): invalid bpf_context access off=76 size=4 (3 line(s) omitted)

// 76 오프셋은 struct __sk_buff.data 필드 위치

참고로, data 필드는 패킷의 시작 위치를 가리킨다고 합니다.

// struct sk_buff
// ; https://docs.kernel.org/networking/skbuff.html
//
// Measuring TCP Latency Using eBPF: Part 2 - Kernel Space - struct __sk_buff
// ; https://fiwippi.net/tcplat-p2
//
// struct sk_buff 관련 함수
// ; https://hand-over.tistory.com/2

    sk_buff.data --> +-----------------+
                     | Ethernet Header |
                     +-----------------+
                     |    IP Header    |
sk_buff.data_end --> +-----------------+
                     |   TCP Header    |
                     +-----------------+
                     |     Payload     |
                     +-----------------+

[출처: https://www.cubrid.org/blog/3826497]
sk_buff

다행히 eBPF에서는 저 영역을 접근할 수 있는 도우미 함수를 제공하는데요,

Helper function bpf_skb_load_bytes (커널 버전 4.5)
; https://docs.ebpf.io/linux/helper-function/bpf_skb_load_bytes/

관련해서 검색해 보면 "L7 Tracing with eBPF: HTTP and Beyond via Socket Filters and Syscall Tracepoints" 글에 bpf_skb_load_bytes 함수를 사용한 예제가 나옵니다.

SEC("socket")
int socket_handler(struct __sk_buff *skb)
{
    __u16 proto;

    bpf_skb_load_bytes(skb, 12, &proto, 2);
    proto = __bpf_ntohs(proto);

    return skb->len;

/* proto 값의 예:
0x0800 for IPv4
0x0806 for ARP
0x86DD for IPv6
*/
}

위의 코드를 해석해 보면, skb->data 필드가 가리키는 위치가 ethernet 헤더의 시작 위치이므로,

// cat vmlinux.h | grep -A 18 "struct ethhdr {"

struct ethhdr {
        unsigned char h_dest[6];  // offset 0
        unsigned char h_source[6]; // offset 6
        __be16 h_proto; // offset 12
};

0 1 2 3 4 5 6 7 8 9 0 1 2 3 4
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|h_dest     | h_source  |** |  // (** h_proto)
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 

결과적으로, 12 위치는 h_proto 필드의 offset 값이 됩니다. 즉, protocol 값을 가져오는 것인데요, 그런데 제 실습 환경에서 실행해 보면 엉뚱한 값이 나왔습니다.

// TCP 소켓 예제에서 출력된 h_proto 값들

bpf_trace_printk: __sk_buff.h_proto == 8019
bpf_trace_printk: __sk_buff.h_proto == b010
bpf_trace_printk: __sk_buff.h_proto == 5014
bpf_trace_printk: __sk_buff.h_proto == 5004

반면 ctx->protocol 필드를 직접 읽어오면, (TCP 소켓 예제이므로) 항상 정확하게 ETH_P_IP에 해당하는 8(BE 표현으로는 0x0800) 값이 나옵니다. 이해할 수 없군요, ^^ 분명히 "L7 Tracing with eBPF: HTTP and Beyond via Socket Filters and Syscall Tracepoints" 글에서의 코드와 같은데... 이유를 모르겠습니다. (혹시 아시는 분은 덧글 부탁드립니다. ^^;)

이와 관련해 좀 더 검색했더니 SEC("tc") 프로그램 예제가 나오는데요,

hello-libbpfgo/25-tc-parse-packet-with-direct-memory-access/main.bpf.c
; https://github.com/mozillazg/hello-libbpfgo/blob/master/25-tc-parse-packet-with-direct-memory-access/main.bpf.c#L24

Measuring TCP Latency Using eBPF: Part 2 - Kernel Space
; https://fiwippi.net/tcplat-p2

SEC("tc")
int handle_ingress(struct __sk_buff *skb) {
    bpf_skb_pull_data(skb, 0);

    void *data_end = (void *)(long)skb->data_end;
    void *data = (void *)(long)skb->data;

    struct iphdr *ip_hdr = data + ETH_HLEN;
    if ((void *)ip_hdr + sizeof(struct iphdr) > data_end) {
        return TC_ACT_OK;
    }

    // ...[생략]...
}

오호~~~ 32비트 값을 포인터로 변환해 사용하고 있습니다. 리눅스 커널은 저런 구조체를 4GB 하위에 할당하는 걸까요? (그러고 보니 전에도 한 번 이와 관련한 의문을 남긴 적이 있습니다.) 과연 이것을 올바른 예제라고 봐야 할지 모르겠지만... 어쨌든 동작했으니까 ^^ 저렇게 github에 올렸을 것입니다. 그리고 위의 예제에서 특이한 점은 bpf_skb_pull_data 함수를 사용했다는 점인데,

Helper function bpf_skb_pull_data (커널 버전 4.9)
; https://docs.ebpf.io/linux/helper-function/bpf_skb_pull_data/

문서에 보면, skb.data 영역을 접근할 수 있는 용도로 제공한 첫 번째 함수가 bpf_skb_load_bytes였고, 두 번째로 나온 것이 "direct packet access"를 가능하게 만든 bpf_skb_pull_data였다고 합니다. (이후 Linux 4.7부터는 bpf_skb_load_bytes 함수도 "direct packet access"를 지원하게 돼 이제는 그런 차이는 없어졌습니다.)

참고로, bpf_skb_pull_data는 SEC("socket")에 해당하는 BPF_PROG_TYPE_SOCKET_FILTER 프로그램에서는 사용할 수 없습니다.

SEC("socket") // SEC("tc")에서는, 즉 BPF_PROG_TYPE_SCHED_CLS 유형의 프로그램에서는 bpf_skb_pull_data 사용 불가능
int socket_handler(struct __sk_buff *ctx)
{
    bpf_skb_pull_data(ctx, 0); // eBPF 프로그램 로딩 시 오류 발생: invalid argument: program of this type cannot use helper bpf_skb_pull_data#39 (7 line(s) omitted)

    return ctx->len;
}




github에 있는 다른 예제를 하나 더 테스트해 봤는데요,

// https://github.com/mozillazg/hello-libbpfgo/blob/master/19-socket-filter-capture-icmp-traffic-userspace-parse/main.bpf.c

#include "vmlinux.h"

#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>
#include <bpf/bpf_endian.h>

unsigned long long load_half(void *skb, unsigned long long off) asm("llvm.bpf.load.half");

SEC("socket")
int socket_handler(struct __sk_buff *ctx)
{
    // load_half 함수는 data 포인터가 가리키는 영역으로부터 offset 위치의 16비트 값을 읽어오는 도우미 함수
    __u16 proto = load_half(ctx, offsetof(struct ethhdr, h_proto));
    bpf_printk("ethhdr.h_proto == %04x, protocol == %04x\n", proto, __bpf_htons(ctx->protocol));

    return ctx->len;
}

// ETH_P_IP == 0x0800

이것 역시 ETH_P_IP 값이 나오는 것으로 사용하고 있지만, 제가 테스트한 Ubuntu 24.04 환경에서는 다른 결과가 나옵니다.

bpf_trace_printk: ethhdr.h_proto == 8019, protocol == 0800
bpf_trace_printk: ethhdr.h_proto == 8010, protocol == 0800
bpf_trace_printk: ethhdr.h_proto == 5014, protocol == 0800

원래 의도했던 값은 h_proto와 protocol 값이 같아야 할 텐데, 이번에도 protocol은 정상인 반면 h_proto는 (if_ether.h에는 없는) 이상한 값이 나옵니다.




마지막으로 "Understanding struct __sk_buff" 글에 보면, "Notice that the base address is same for both sk_buff and __sk_buff."라는 설명도 나옵니다. 그렇다면 struct sk_buff *skb로 대체해도 되는 걸까요?

하지만, 이것 역시 실제로 해보면,

SEC("socket")
int socket_handler(struct sk_buff *ctx)
{
    return ctx->len;
}

eBPF 로더에서 오류가 발생합니다.

load program: permission denied: 0: (79) r3 = *(u64 *)(r1 +112): invalid bpf_context access off=112 size=8 (3 line(s) omitted)

// 112 오프셋은 struct sk_buff.len 필드 위치

따라서 eBPF 프로그램에 따라 struct __sk_buff와 struct __sk_buff 타입은 정해진 대로 구별해서 사용해야 하는 것이 맞습니다. 찾아보면, StackOverflow 글에서도 이에 대한 언급이 있군요. ^^

Cannot access __skb_buff in eBPF
; https://stackoverflow.com/questions/72717564/cannot-access-skb-buff-in-ebpf

First is that the argument to the tracepoint is of type struct sk_buff, not struct __sk_buff.





암튼, eBPF를 무작위로 공부하고는 있지만 실습도 잘 안되는 예제들이 있어 답답한 면이 좀 있습니다. ^^ 그나마 다행인 것은 리눅스 커널 소스 코드가 공개인 덕분에 많은 Q&A에서 소스 코드를 함께 언급하며 설명하고 있어 이해하는 데 도움은 되고 있다는 점입니다.




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 10/2/2025]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... 106  107  108  109  110  111  112  113  114  115  [116]  117  118  119  120  ...
NoWriterDateCnt.TitleFile(s)
11122정성태1/3/201725983.NET Framework: 628. TransactionScope에 사용자 정의 트랜잭션을 참여시키는 방법 [2]파일 다운로드1
11121정성태1/1/201723774개발 환경 구성: 308. "ASP.NET Core Web Application (.NET Core)"와 "ASP.NET Core Web Application (.NET Framework)" 차이점
11120정성태12/25/201631914개발 환경 구성: 307. ASP.NET Core Web Application을 IIS에서 호스팅하는 방법
11119정성태12/23/201651945개발 환경 구성: 306. Visual Studio Code에서 Python 개발 환경 구성 [2]
11118정성태12/22/201642265오류 유형: 374. Python 64비트 설치 시 0x80070659 오류 발생 [3]
11117정성태12/21/201626950웹: 35. nopCommerce 예제 사이트 구성 방법
11116정성태12/21/201629296디버깅 기술: 84. NopCommerce의 Autofac 부하(CPU, Memory) [2]
11115정성태12/21/201632226Windows: 133. 윈도우 서버 2016에서 플래시가 동작하지 않는 경우 [2]
11114정성태12/19/201644963Windows: 132. 역슬래시(backslash) 문자가 왜 통화 표기 문자(한글인 경우 "\")로 보일까요? [2]
11113정성태12/6/201623717오류 유형: 373. ICOMAdminCatalog::GetCollection에서 CO_E_ISOLEVELMISMATCH(0x8004E02F) 오류 발생파일 다운로드1
11112정성태11/23/201629449오류 유형: 372. MySQL 서비스가 올라오지 않는 경우 - Error 1067
11111정성태11/23/201638623.NET Framework: 627. C++로 만든 DLL을 C#에서 사용하기 [2]
11110정성태11/17/201626486.NET Framework: 626. Commit 메모리가 낮은 상황에서도 메모리 부족(Out-of-memory) 예외 발생 [2]
11109정성태11/17/201625908.NET Framework: 625. ASP.NET에서 System.Web.HttpApplication 인스턴스는 다중으로 생성됩니다.
11108정성태11/13/201624913.NET Framework: 624. WPF - Line 요소를 Canvas에 위치시켰을 때 흐림(blur) 현상파일 다운로드1
11107정성태11/9/201631375오류 유형: 371. Post cache substitution is not compatible with modules in the IIS integrated pipeline that modify the response buffers.파일 다운로드1
11106정성태11/8/201629522.NET Framework: 623. C# - PeerFinder를 이용한 Wi-Fi Direct 데이터 통신 예제 [2]파일 다운로드1
11105정성태11/8/201624594.NET Framework: 622. PeerFinder Wi-Fi Direct 통신 시 Read/Write/Dispose 문제
11104정성태11/8/201622583개발 환경 구성: 305. PeerFinder로 Wi-Fi Direct 연결 시 방화벽 문제
11103정성태11/8/201623743오류 유형: 370. PeerFinder.ConnectAsync의 결과 값인 Task.Result를 호출할 때 System.AggregateException 예외 발생
11102정성태11/8/201624093오류 유형: 369. PeerFinder.FindAllPeersAsync 호출 시 System.UnauthorizedAccessException 예외 발생
11101정성태11/8/201625597.NET Framework: 621. 닷넷 프로파일러의 오류 코드 - 0x80131363
11100정성태11/7/201634110개발 환경 구성: 304. Wi-Fi Direct 지원 여부 확인 방법 [1]
11099정성태11/7/201635862.NET Framework: 620. C#에서 C/C++ 함수로 콜백 함수를 전달하는 예제 코드파일 다운로드1
11098정성태11/7/201624933오류 유형: 368. 빌드 이벤트에서 robocopy 사용 시 $(TargetDir) 매크로를 지정하는 경우 오류 발생
11097정성태11/7/201627433오류 유형: 367. go install: no install location for directory [...경로...] outside GOPATH
... 106  107  108  109  110  111  112  113  114  115  [116]  117  118  119  120  ...