Microsoft MVP성태의 닷넷 이야기
Linux: 123. eBPF (bpf2go) - BPF_PROG_TYPE_SOCKET_FILTER 예제 - SEC("socket") [링크 복사], [링크+제목 복사],
조회: 715
글쓴 사람
정성태 (seongtaejeong at gmail.com)
홈페이지
첨부 파일
 
(연관된 글이 1개 있습니다.)
(시리즈 글이 16개 있습니다.)
Linux: 86. Golang + bpf2go를 사용한 eBPF 기본 예제
; https://www.sysnet.pe.kr/2/0/13769

Linux: 94. eBPF - vmlinux.h 헤더 포함하는 방법 (bpf2go에서 사용)
; https://www.sysnet.pe.kr/2/0/13783

Linux: 95. eBPF - kprobe를 이용한 트레이스
; https://www.sysnet.pe.kr/2/0/13784

Linux: 96. eBPF (bpf2go) - fentry, fexit를 이용한 트레이스
; https://www.sysnet.pe.kr/2/0/13788

Linux: 100.  eBPF의 2가지 방식 - libbcc와 libbpf(CO-RE)
; https://www.sysnet.pe.kr/2/0/13801

Linux: 103. eBPF (bpf2go) - Tracepoint를 이용한 트레이스 (BPF_PROG_TYPE_TRACEPOINT)
; https://www.sysnet.pe.kr/2/0/13810

Linux: 105. eBPF - bpf2go에서 전역 변수 설정 방법
; https://www.sysnet.pe.kr/2/0/13815

Linux: 106. eBPF (bpf2go) - (BPF_MAP_TYPE_HASH) Map을 이용한 전역 변수 구현
; https://www.sysnet.pe.kr/2/0/13817

Linux: 107. eBPF - libbpf CO-RE의 CONFIG_DEBUG_INFO_BTF 빌드 여부에 대한 의존성
; https://www.sysnet.pe.kr/2/0/13819

Linux: 109. eBPF (bpf2go) - BPF_PERF_OUTPUT / BPF_MAP_TYPE_PERF_EVENT_ARRAY 사용법
; https://www.sysnet.pe.kr/2/0/13824

Linux: 110. eBPF (bpf2go) - BPF_RINGBUF_OUTPUT / BPF_MAP_TYPE_RINGBUF 사용법
; https://www.sysnet.pe.kr/2/0/13825

Linux: 115. eBPF (bpf2go) - ARRAY / HASH map 기본 사용법
; https://www.sysnet.pe.kr/2/0/13893

Linux: 116. eBPF (bpf2go) - BTF Style Maps 정의 구문과 데이터 정렬 문제
; https://www.sysnet.pe.kr/2/0/13894

Linux: 117. eBPF (bpf2go) - Map에 추가된 요소의 개수를 확인하는 방법
; https://www.sysnet.pe.kr/2/0/13895

Linux: 122. eBPF - __attribute__((preserve_access_index)) 사용법
; https://www.sysnet.pe.kr/2/0/14016

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




eBPF (bpf2go) - BPF_PROG_TYPE_SOCKET_FILTER 예제 - SEC("socket")

이번엔 소켓을 모니터링하는 eBPF 프로그램의 하나인 Socket Filter 유형(BPF_PROG_TYPE_SOCKET_FILTER)에 대해 알아보겠습니다.

Program type BPF_PROG_TYPE_SOCKET_FILTER
; https://docs.ebpf.io/linux/program-type/BPF_PROG_TYPE_SOCKET_FILTER/

아래의 글에서 이와 관련한 예제를 볼 수 있는데,

L7 Tracing with eBPF: HTTP and Beyond via Socket Filters and Syscall Tracepoints
; https://eunomia.dev/en/tutorials/23-http/

SEC("socket") 매크로를 사용하여 소켓 필터 프로그램을 정의하고 있습니다.

SEC("socket")
int socket_handler(struct __sk_buff *skb)
{
    // ...[생략]...Your eBPF program logic here

    return skb->len; // 반드시 패킷의 길이를 반환해야 통신이 허용됩니다.
}

함수의 인자로 전달되는 __sk_buff 구조체는 sk_buff 구조체의 eBPF 표현인데요,

// (제 테스트 시스템 기준으로) sizeof(__sk_buff) == 184
// cat vmlinux.h | grep -A 38 "struct __sk_buff {"
struct __sk_buff {
        __u32 len;
        __u32 pkt_type;
        __u32 mark;
        __u32 queue_mapping;
        __u32 protocol; // This field indicates the Layer 3 protocol of the packet and is one of the ETH_P_* values defined in include/uapi/linux/if_ether.h.
        __u32 vlan_present;
        __u32 vlan_tci;
        __u32 vlan_proto;
        __u32 priority;
        __u32 ingress_ifindex;
        __u32 ifindex;
        __u32 tc_index;
        __u32 cb[5];
        __u32 hash;
        __u32 tc_classid;
        __u32 data;
        __u32 data_end;
        __u32 napi_id;
        __u32 family;
        __u32 remote_ip4;
        __u32 local_ip4;
        __u32 remote_ip6[4];
        __u32 local_ip6[4];
        __u32 remote_port;
        __u32 local_port;
        __u32 data_meta;
        union {
                struct bpf_flow_keys *flow_keys; 
        };
        __u64 tstamp;
        __u32 wire_len;
        __u32 gso_segs;
        union {
                struct bpf_sock *sk;
        };
        __u32 gso_size;
};

// cat vmlinux.h | grep -A 118 "struct sk_buff {"
struct sk_buff {
        union {
                struct {
                        struct sk_buff *next;
                        struct sk_buff *prev;
                        union {
                                struct net_device *dev;
                                long unsigned int dev_scratch;
                        };
                };
                struct rb_node rbnode;
                struct list_head list;
        };
        union {
                struct sock *sk;
                int ip_defrag_offset;
        };
        union {
                ktime_t tstamp;
                u64 skb_mstamp_ns;
        };
        char cb[48];
        union {
                struct {
                        long unsigned int _skb_refdst;
                        void (*destructor)(struct sk_buff *);
                };
                struct list_head tcp_tsorted_anchor;
                long unsigned int _sk_redir;
        };
        long unsigned int _nfct;
        unsigned int len;
        unsigned int data_len;
        __u16 mac_len;
        __u16 hdr_len;
        __u16 queue_mapping;
        __u8 __cloned_offset[0];
        __u8 cloned: 1;
        __u8 nohdr: 1;
        __u8 fclone: 2;
        __u8 peeked: 1;
        __u8 head_frag: 1;
        __u8 pfmemalloc: 1;
        __u8 pp_recycle: 1;
        __u8 active_extensions;
        __u32 headers_start[0];
        __u8 __pkt_type_offset[0];
        __u8 pkt_type: 3;
        __u8 ignore_df: 1;
        __u8 nf_trace: 1;
        __u8 ip_summed: 2;
        __u8 ooo_okay: 1;
        __u8 l4_hash: 1;
        __u8 sw_hash: 1;
        __u8 wifi_acked_valid: 1;
        __u8 wifi_acked: 1;
        __u8 no_fcs: 1;
        __u8 encapsulation: 1;
        __u8 encap_hdr_csum: 1;
        __u8 csum_valid: 1;
        __u8 __pkt_vlan_present_offset[0];
        __u8 vlan_present: 1;
        __u8 csum_complete_sw: 1;
        __u8 csum_level: 2;
        __u8 csum_not_inet: 1;
        __u8 dst_pending_confirm: 1;
        __u8 ndisc_nodetype: 2;
        __u8 ipvs_property: 1;
        __u8 inner_protocol_type: 1;
        __u8 remcsum_offload: 1;
        __u8 offload_fwd_mark: 1;
        __u8 offload_l3_fwd_mark: 1;
        __u8 tc_skip_classify: 1;
        __u8 tc_at_ingress: 1;
        __u8 redirected: 1;
        __u8 slow_gro: 1;
        __u8 scm_io_uring: 1;
        __u16 tc_index;
        union {
                __wsum csum;
                struct {
                        __u16 csum_start;
                        __u16 csum_offset;
                };
        };
        __u32 priority;
        int skb_iif;
        __u32 hash;
        __be16 vlan_proto;
        __u16 vlan_tci;
        union {
                unsigned int napi_id;
                unsigned int sender_cpu;
        };
        __u32 secmark;
        union {
                __u32 mark;
                __u32 reserved_tailroom;
        };
        union {
                __be16 inner_protocol;
                __u8 inner_ipproto;
        };
        __u16 inner_transport_header;
        __u16 inner_network_header;
        __u16 inner_mac_header;
        __be16 protocol;
        __u16 transport_header;
        __u16 network_header;
        __u16 mac_header;
        __u32 headers_end[0];
        sk_buff_data_t tail;
        sk_buff_data_t end;
        unsigned char *head;
        unsigned char *data;
        unsigned int truesize;
        refcount_t users;
        struct skb_ext *extensions;
};

대충 이런 식으로 접근해 사용할 수 있습니다.

SEC("socket")
int socket_handler(struct __sk_buff *skb)
{
    __u16 protocol = skb->protocol;

    char isIPv4Packet = (protocol == bpf_htons(ETH_P_IP)); // ETH_P_IP == 0x0800(BE) == 8(LE)
    char isIPv6Packet = (protocol == bpf_htons(ETH_P_IPV6)); // ETH_P_IPV6 == 0x86DD(BE) == dd86(LE)

    if (isIPv4Packet == 0 && isIPv6Packet == 0) {
        return skb->len;
    }

    // Your eBPF program logic here

    return skb->len;
}

하지만, 무시할 수 없는 제약이 좀 있는데요, 우선, 1) 오직 ingoing 패킷에 대해서만 작동하고, 2) 무엇보다도 특정 socket descriptor에 attach 시키는 식으로만 활성화할 수 있기 때문에,

remoteAddr, err := net.ResolveTCPAddr("tcp", "192.168.100.50:80")

// 이렇게 Socket 관련 File Descriptor를 얻어온 다음,
conn, err := net.DialTCP("tcp", nil, remoteAddr)
if err != nil {
	return
}
defer conn.Close()

// 해당 FD에 eBPF 프로그램을 attach 시켜야 합니다.
err = link.AttachSocketFilter(conn, bpfObj.SocketHandler)
if err != nil {
	fmt.Printf("link.AttachNetfilter: %v\n", err)
	return
}
defer func(tcpConn *net.TCPConn) {
	_ = link.DetachSocketFilter(tcpConn)
}(conn)

(일반적인 다른 eBPF 프로그램들이 기본적으로 전역 모니터링할 수 있었다는 점과 비교해) 로컬 프로세스 내의 정보만을 얻을 수 있다는 점이 아쉽습니다. 즉, in-process 내부의 소켓에 대해 Input 패킷만을 모니터링할 수 있을 뿐입니다.

아마도 그런 점 때문에, "Program type BPF_PROG_TYPE_SOCKET_FILTER" 문서에 다음과 같은 주석이 있는 것 같습니다.

Socket filters pre-date eBPF itself, socket filters were the first ever prototype in the original BPF implementation, now referred to as cBPF (classic BPF). In fact, usage of this program type was the reason for inventing the whole system1.


참고로, 전에 SEC("socket") 매크로를 전역 변수를 제어하기 위한 함수를 위해서도 사용한 적이 있는데요, 이것이 BPF_PROG_TYPE_SOCKET_FILTER 유형의 프로그램이라는 것을 알았음에도... 왜 유독 SEC("socket") 매크로만 유효했는지 여전히 의문이 남는군요. ^^




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

[연관 글]






[최초 등록일: ]
[최종 수정일: 9/29/2025]

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

비밀번호

댓글 작성자
 




... 76  77  [78]  79  80  81  82  83  84  85  86  87  88  89  90  ...
NoWriterDateCnt.TitleFile(s)
12084정성태12/19/201923783디버깅 기술: 143. windbg/sos - Hashtable의 buckets 배열 내용을 모두 덤프하는 방법 (do_hashtable.py) [1]
12083정성태12/17/201926206Linux: 27. linux - lldb를 이용한 .NET Core 응용 프로그램의 메모리 덤프 분석 방법 [2]
12082정성태12/17/201924795오류 유형: 585. lsof: WARNING: can't stat() fuse.gvfsd-fuse file system
12081정성태12/16/201927651개발 환경 구성: 465. 로컬 PC에서 개발 중인 ASP.NET Core 웹 응용 프로그램을 다른 PC에서도 접근하는 방법 [5]
12080정성태12/16/201923992.NET Framework: 870. C# - 프로세스의 모든 핸들을 열람
12079정성태12/13/201926215오류 유형: 584. 원격 데스크톱(rdp) 환경에서 다중 또는 고용량 파일 복사 시 "Unspecified error" 오류 발생
12078정성태12/13/201926466Linux: 26. .NET Core 응용 프로그램을 위한 메모리 덤프 방법 [3]
12077정성태12/13/201923458Linux: 25. 자주 실행할 명령어 또는 초기 환경을 "~/.bashrc" 파일에 등록
12076정성태12/12/201923605디버깅 기술: 142. Linux - lldb 환경에서 sos 확장 명령어를 이용한 닷넷 프로세스 디버깅 - 배포 방법에 따른 차이
12075정성태12/11/201925062디버깅 기술: 141. Linux - lldb 환경에서 sos 확장 명령어를 이용한 닷넷 프로세스 디버깅
12074정성태12/10/201923941디버깅 기술: 140. windbg/Visual Studio - 값이 변경된 경우를 위한 정지점(BP) 설정(Data Breakpoint)
12073정성태12/10/201924008Linux: 24. Linux/C# - 실행 파일이 아닌 스크립트 형식의 명령어를 Process.Start로 실행하는 방법
12072정성태12/9/201920398오류 유형: 583. iisreset 수행 시 "No such interface supported" 오류
12071정성태12/9/201925370오류 유형: 582. 리눅스 디스크 공간 부족 및 safemode 부팅 방법
12070정성태12/9/201926735오류 유형: 581. resize2fs: Bad magic number in super-block while trying to open /dev/.../root
12069정성태12/2/201924307디버깅 기술: 139. windbg - x64 덤프 분석 시 메서드의 인자 또는 로컬 변수의 값을 확인하는 방법
12068정성태11/28/201932817디버깅 기술: 138. windbg와 Win32 API로 알아보는 Windows Heap 정보 분석 [3]파일 다운로드2
12067정성태11/27/201924155디버깅 기술: 137. 실제 사례를 통해 Debug Diagnostics 도구가 생성한 닷넷 웹 응용 프로그램의 성능 장애 보고서 설명 [1]파일 다운로드1
12066정성태11/27/201923788디버깅 기술: 136. windbg - C# PInvoke 호출 시 마샬링을 담당하는 함수 분석 - OracleCommand.ExecuteReader에서 OpsSql.Prepare2 PInvoke 호출 분석
12065정성태11/25/201921117디버깅 기술: 135. windbg - C# PInvoke 호출 시 마샬링을 담당하는 함수 분석파일 다운로드1
12064정성태11/25/201925095오류 유형: 580. HTTP Error 500.0/500.33 - ANCM In-Process Handler Load Failure
12063정성태11/21/201924403디버깅 기술: 134. windbg - RtlReportCriticalFailure로부터 parameters 정보 찾는 방법
12062정성태11/21/201922504디버깅 기술: 133. windbg - CoTaskMemFree/FreeCoTaskMem에서 발생한 덤프 분석 사례 - 두 번째 이야기
12061정성태11/20/201922831Windows: 167. CoTaskMemAlloc/CoTaskMemFree과 윈도우 Heap의 관계
12060정성태11/20/201926773디버깅 기술: 132. windbg/Visual Studio - HeapFree x64의 동작 분석
12059정성태11/20/201925526디버깅 기술: 131. windbg/Visual Studio - HeapFree x86의 동작 분석
... 76  77  [78]  79  80  81  82  83  84  85  86  87  88  89  90  ...