Skip to content
Snippets Groups Projects
Select Git revision
  • a3f74617340b598dbc7eb5b68d4ed53b4a70f5eb
  • master default
  • kernelci-local
  • kernelci-stable
  • beaglebone-black-next-20190304-debug
  • next-20190301-beaglebone-black-debug
  • kernelci/slash-in-branch-name
  • next-20180215-fixup
  • kernelci-ref
  • linux-4.20-mali-bifrost-r16p0
  • kernelci-staging
  • kernelci-bisect
  • next-20181206-jeston-tk1-nouveau-fix
  • 4.20-rc1-beagle-xm-smp-preempt-bisect
  • bisect-exp
  • linux-4.4.152-bisect-atom-x5
  • linux-3.18.y-exynos-nfsroot
  • linux-4.14-mali-rk3288
  • linux-4.4.110-odroidxu3-fixup-mfc-regions
  • linux-4.15-rc4-drm-tegra124-fixup
  • bisect-3199-peach-pi
  • kernelci-local-snapshot-033
  • v4.20.16
  • kernelci-local-snapshot-032
  • next-20190304
  • next-20190301
  • v4.20.12
  • kernelci-local-snapshot-030
  • kernelci-local-snapshot-029
  • kernelci-local-snapshot-028
  • kernelci-local-snapshot-027
  • kernelci-local-snapshot-026
  • kernelci-local-snapshot-025
  • kernelci-local-snapshot-024
  • kernelci-local-snapshot-023
  • kernelci-local-snapshot-022
  • kernelci-local-snapshot-021
  • v4.20.5
  • kernelci-local-snapshot-020
  • kernelci-local-snapshot-019
  • kernelci-local-snapshot-018
41 results

test_cgrp2_array_pin.c

Blame
  • Forked from Guillaume Tucker / linux
    Source project has a limited visibility.
    • Martin KaFai Lau's avatar
      a3f74617
      cgroup: bpf: Add an example to do cgroup checking in BPF · a3f74617
      Martin KaFai Lau authored
      
      test_cgrp2_array_pin.c:
      A userland program that creates a bpf_map (BPF_MAP_TYPE_GROUP_ARRAY),
      pouplates/updates it with a cgroup2's backed fd and pins it to a
      bpf-fs's file.  The pinned file can be loaded by tc and then used
      by the bpf prog later.  This program can also update an existing pinned
      array and it could be useful for debugging/testing purpose.
      
      test_cgrp2_tc_kern.c:
      A bpf prog which should be loaded by tc.  It is to demonstrate
      the usage of bpf_skb_in_cgroup.
      
      test_cgrp2_tc.sh:
      A script that glues the test_cgrp2_array_pin.c and
      test_cgrp2_tc_kern.c together.  The idea is like:
      1. Load the test_cgrp2_tc_kern.o by tc
      2. Use test_cgrp2_array_pin.c to populate a BPF_MAP_TYPE_CGROUP_ARRAY
         with a cgroup fd
      3. Do a 'ping -6 ff02::1%ve' to ensure the packet has been
         dropped because of a match on the cgroup
      
      Most of the lines in test_cgrp2_tc.sh is the boilerplate
      to setup the cgroup/bpf-fs/net-devices/netns...etc.  It is
      not bulletproof on errors but should work well enough and
      give enough debug info if things did not go well.
      
      Signed-off-by: default avatarMartin KaFai Lau <kafai@fb.com>
      Cc: Alexei Starovoitov <ast@fb.com>
      Cc: Daniel Borkmann <daniel@iogearbox.net>
      Cc: Tejun Heo <tj@kernel.org>
      Acked-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
      a3f74617
      History
      cgroup: bpf: Add an example to do cgroup checking in BPF
      Martin KaFai Lau authored
      
      test_cgrp2_array_pin.c:
      A userland program that creates a bpf_map (BPF_MAP_TYPE_GROUP_ARRAY),
      pouplates/updates it with a cgroup2's backed fd and pins it to a
      bpf-fs's file.  The pinned file can be loaded by tc and then used
      by the bpf prog later.  This program can also update an existing pinned
      array and it could be useful for debugging/testing purpose.
      
      test_cgrp2_tc_kern.c:
      A bpf prog which should be loaded by tc.  It is to demonstrate
      the usage of bpf_skb_in_cgroup.
      
      test_cgrp2_tc.sh:
      A script that glues the test_cgrp2_array_pin.c and
      test_cgrp2_tc_kern.c together.  The idea is like:
      1. Load the test_cgrp2_tc_kern.o by tc
      2. Use test_cgrp2_array_pin.c to populate a BPF_MAP_TYPE_CGROUP_ARRAY
         with a cgroup fd
      3. Do a 'ping -6 ff02::1%ve' to ensure the packet has been
         dropped because of a match on the cgroup
      
      Most of the lines in test_cgrp2_tc.sh is the boilerplate
      to setup the cgroup/bpf-fs/net-devices/netns...etc.  It is
      not bulletproof on errors but should work well enough and
      give enough debug info if things did not go well.
      
      Signed-off-by: default avatarMartin KaFai Lau <kafai@fb.com>
      Cc: Alexei Starovoitov <ast@fb.com>
      Cc: Daniel Borkmann <daniel@iogearbox.net>
      Cc: Tejun Heo <tj@kernel.org>
      Acked-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
    test_cgrp2_array_pin.c 2.24 KiB
    /* Copyright (c) 2016 Facebook
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of version 2 of the GNU General Public
     * License as published by the Free Software Foundation.
     */
    #include <linux/unistd.h>
    #include <linux/bpf.h>
    
    #include <stdio.h>
    #include <stdint.h>
    #include <unistd.h>
    #include <string.h>
    #include <errno.h>
    #include <fcntl.h>
    
    #include "libbpf.h"
    
    static void usage(void)
    {
    	printf("Usage: test_cgrp2_array_pin [...]\n");
    	printf("       -F <file>   File to pin an BPF cgroup array\n");
    	printf("       -U <file>   Update an already pinned BPF cgroup array\n");
    	printf("       -v <value>  Full path of the cgroup2\n");
    	printf("       -h          Display this help\n");
    }
    
    int main(int argc, char **argv)
    {
    	const char *pinned_file = NULL, *cg2 = NULL;
    	int create_array = 1;
    	int array_key = 0;
    	int array_fd = -1;
    	int cg2_fd = -1;
    	int ret = -1;
    	int opt;
    
    	while ((opt = getopt(argc, argv, "F:U:v:")) != -1) {
    		switch (opt) {
    		/* General args */
    		case 'F':
    			pinned_file = optarg;
    			break;
    		case 'U':
    			pinned_file = optarg;
    			create_array = 0;
    			break;
    		case 'v':
    			cg2 = optarg;
    			break;
    		default:
    			usage();
    			goto out;
    		}
    	}
    
    	if (!cg2 || !pinned_file) {
    		usage();
    		goto out;
    	}
    
    	cg2_fd = open(cg2, O_RDONLY);
    	if (cg2_fd < 0) {
    		fprintf(stderr, "open(%s,...): %s(%d)\n",
    			cg2, strerror(errno), errno);
    		goto out;
    	}
    
    	if (create_array) {
    		array_fd = bpf_create_map(BPF_MAP_TYPE_CGROUP_ARRAY,
    					  sizeof(uint32_t), sizeof(uint32_t),
    					  1, 0);
    		if (array_fd < 0) {
    			fprintf(stderr,
    				"bpf_create_map(BPF_MAP_TYPE_CGROUP_ARRAY,...): %s(%d)\n",
    				strerror(errno), errno);
    			goto out;
    		}
    	} else {
    		array_fd = bpf_obj_get(pinned_file);
    		if (array_fd < 0) {
    			fprintf(stderr, "bpf_obj_get(%s): %s(%d)\n",
    				pinned_file, strerror(errno), errno);
    			goto out;
    		}
    	}
    
    	ret = bpf_update_elem(array_fd, &array_key, &cg2_fd, 0);
    	if (ret) {
    		perror("bpf_update_elem");
    		goto out;
    	}
    
    	if (create_array) {
    		ret = bpf_obj_pin(array_fd, pinned_file);
    		if (ret) {
    			fprintf(stderr, "bpf_obj_pin(..., %s): %s(%d)\n",
    				pinned_file, strerror(errno), errno);
    			goto out;
    		}
    	}
    
    out:
    	if (array_fd != -1)
    		close(array_fd);
    	if (cg2_fd != -1)
    		close(cg2_fd);
    	return ret;
    }