为什么样本/ bpf中的ebpf程序不起作用?

时间:2019-07-31 13:28:50

标签: c kernel bpf ebpf

目标:在内核源代码树4.18.0的ebpf目录中编写一个新的samples/bpf示例,进行编译和执行。

问题:在我运行sudo ./mine时编译它后,它就终止了。

mine_kern.c

#include <uapi/linux/bpf.h>
#include <uapi/linux/if_ether.h>
#include <uapi/linux/ip.h>
#include <linux/in.h>
#include <linux/if_packet.h>
#include "bpf_helpers.h" 

int icmp_filter(struct __sk_buff *skb){

        int proto = load_byte(skb, ETH_HLEN + offsetof(struct iphdr, protocol));
        if(proto == IPPROTO_ICMP && skb->pkt_type == PACKET_OUTGOING){
           return -1;
        } else {
           return 0;
        }
}

char _license[] SEC("license") = "GPL";

mine_user.c

#include <stdio.h>
#include <assert.h>
#include <linux/bpf.h>
#include <bpf/bpf.h>
#include "bpf_load.h"
#include "sock_example.h"
#include <unistd.h>
#include <arpa/inet.h>    

int main(int ac, char **argv)
{
    char filename[256];
    FILE *f;
    int i, sock;

    snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);

    if (load_bpf_file(filename)) {
        printf("%s", bpf_log_buf);
        return 1;
    }   

    sock = open_raw_sock("lo");

    assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, prog_fd,
              sizeof(prog_fd[0])) == 0);

    f = popen("ping -c5 localhost", "r");
    (void) f;

    char buf[65535];

    for(i=0; i<20; i++){
           int res = recvfrom(sock, buf, sizeof(buf), 0, NULL, 0);
           printf("res=%d\n", res);
     } 

     return 0;
}

我还修改了Makefile内的samples/bpf,并在需要时添加了mine_user.cmine_kern.c问题:此代码有什么问题?

1 个答案:

答案 0 :(得分:1)

由于 override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { if (parent?.getItemAtPosition(position)?.equals("Select branch")!!){ } if(position==1){ val aryadpt=ArrayAdapter(this@Syllabus,R.layout.text_layout,fsem) spin.adapter=aryadpt spin.onItemSelectedListener= object :AdapterView.OnItemSelectedListener{ override fun onNothingSelected(parent: AdapterView<*>?) { } override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { when(position){ 1-> btn.setOnClickListener { startActivity(Intent(this@Syllabus,First_sem::class.java)) } 2-> btn.setOnClickListener { startActivity(Intent(this@Syllabus,Second_sem::class.java)) } }} } } 加载函数的方式,您需要将BPF程序函数放在单独的ELF节中。例如,我可以用以下命令加载程序:

load_bpf_file()

在那之后,我在运行程序时看到连续的SEC("socket") int icmp_filter(struct __sk_buff *skb){ ... } 。这是因为res=-1已将open_raw_sock()的套接字设置为非阻塞状态:

sock_example.h

因此,当没有要接收的数据包时,sock = socket(PF_PACKET, SOCK_RAW|SOCK_NONBLOCK|SOCK_CLOEXEC, htons(ETH_P_ALL)); 仅返回recvfrom()(并将-1设置为errno -您应该考虑打印-EGAIN顺便说一句),而不是等待数据包。因此,您可能也想更改它。