~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/samples/bpf/sock_example.c

Version: ~ [ linux-5.8-rc3 ] ~ [ linux-5.7.5 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.48 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.129 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.185 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.228 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.228 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /* eBPF example program:
  2  * - creates arraymap in kernel with key 4 bytes and value 8 bytes
  3  *
  4  * - loads eBPF program:
  5  *   r0 = skb->data[ETH_HLEN + offsetof(struct iphdr, protocol)];
  6  *   *(u32*)(fp - 4) = r0;
  7  *   // assuming packet is IPv4, lookup ip->proto in a map
  8  *   value = bpf_map_lookup_elem(map_fd, fp - 4);
  9  *   if (value)
 10  *        (*(u64*)value) += 1;
 11  *
 12  * - attaches this program to loopback interface "lo" raw socket
 13  *
 14  * - every second user space reads map[tcp], map[udp], map[icmp] to see
 15  *   how many packets of given protocol were seen on "lo"
 16  */
 17 #include <stdio.h>
 18 #include <unistd.h>
 19 #include <assert.h>
 20 #include <linux/bpf.h>
 21 #include <string.h>
 22 #include <stdlib.h>
 23 #include <errno.h>
 24 #include <sys/socket.h>
 25 #include <arpa/inet.h>
 26 #include <linux/if_ether.h>
 27 #include <linux/ip.h>
 28 #include <stddef.h>
 29 #include <bpf/bpf.h>
 30 #include "bpf_insn.h"
 31 #include "sock_example.h"
 32 
 33 char bpf_log_buf[BPF_LOG_BUF_SIZE];
 34 
 35 static int test_sock(void)
 36 {
 37         int sock = -1, map_fd, prog_fd, i, key;
 38         long long value = 0, tcp_cnt, udp_cnt, icmp_cnt;
 39 
 40         map_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(key), sizeof(value),
 41                                 256, 0);
 42         if (map_fd < 0) {
 43                 printf("failed to create map '%s'\n", strerror(errno));
 44                 goto cleanup;
 45         }
 46 
 47         struct bpf_insn prog[] = {
 48                 BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
 49                 BPF_LD_ABS(BPF_B, ETH_HLEN + offsetof(struct iphdr, protocol) /* R0 = ip->proto */),
 50                 BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_0, -4), /* *(u32 *)(fp - 4) = r0 */
 51                 BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
 52                 BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), /* r2 = fp - 4 */
 53                 BPF_LD_MAP_FD(BPF_REG_1, map_fd),
 54                 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
 55                 BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2),
 56                 BPF_MOV64_IMM(BPF_REG_1, 1), /* r1 = 1 */
 57                 BPF_RAW_INSN(BPF_STX | BPF_XADD | BPF_DW, BPF_REG_0, BPF_REG_1, 0, 0), /* xadd r0 += r1 */
 58                 BPF_MOV64_IMM(BPF_REG_0, 0), /* r0 = 0 */
 59                 BPF_EXIT_INSN(),
 60         };
 61         size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
 62 
 63         prog_fd = bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER, prog, insns_cnt,
 64                                    "GPL", 0, bpf_log_buf, BPF_LOG_BUF_SIZE);
 65         if (prog_fd < 0) {
 66                 printf("failed to load prog '%s'\n", strerror(errno));
 67                 goto cleanup;
 68         }
 69 
 70         sock = open_raw_sock("lo");
 71 
 72         if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &prog_fd,
 73                        sizeof(prog_fd)) < 0) {
 74                 printf("setsockopt %s\n", strerror(errno));
 75                 goto cleanup;
 76         }
 77 
 78         for (i = 0; i < 10; i++) {
 79                 key = IPPROTO_TCP;
 80                 assert(bpf_map_lookup_elem(map_fd, &key, &tcp_cnt) == 0);
 81 
 82                 key = IPPROTO_UDP;
 83                 assert(bpf_map_lookup_elem(map_fd, &key, &udp_cnt) == 0);
 84 
 85                 key = IPPROTO_ICMP;
 86                 assert(bpf_map_lookup_elem(map_fd, &key, &icmp_cnt) == 0);
 87 
 88                 printf("TCP %lld UDP %lld ICMP %lld packets\n",
 89                        tcp_cnt, udp_cnt, icmp_cnt);
 90                 sleep(1);
 91         }
 92 
 93 cleanup:
 94         /* maps, programs, raw sockets will auto cleanup on process exit */
 95         return 0;
 96 }
 97 
 98 int main(void)
 99 {
100         FILE *f;
101 
102         f = popen("ping -c5 localhost", "r");
103         (void)f;
104 
105         return test_sock();
106 }
107 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp