cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

ringbuf_multi.c (3302B)


      1// SPDX-License-Identifier: GPL-2.0
      2#define _GNU_SOURCE
      3#include <test_progs.h>
      4#include <sys/epoll.h>
      5#include "test_ringbuf_multi.skel.h"
      6
      7static int duration = 0;
      8
      9struct sample {
     10	int pid;
     11	int seq;
     12	long value;
     13	char comm[16];
     14};
     15
     16static int process_sample(void *ctx, void *data, size_t len)
     17{
     18	int ring = (unsigned long)ctx;
     19	struct sample *s = data;
     20
     21	switch (s->seq) {
     22	case 0:
     23		CHECK(ring != 1, "sample1_ring", "exp %d, got %d\n", 1, ring);
     24		CHECK(s->value != 333, "sample1_value", "exp %ld, got %ld\n",
     25		      333L, s->value);
     26		break;
     27	case 1:
     28		CHECK(ring != 2, "sample2_ring", "exp %d, got %d\n", 2, ring);
     29		CHECK(s->value != 777, "sample2_value", "exp %ld, got %ld\n",
     30		      777L, s->value);
     31		break;
     32	default:
     33		CHECK(true, "extra_sample", "unexpected sample seq %d, val %ld\n",
     34		      s->seq, s->value);
     35		return -1;
     36	}
     37
     38	return 0;
     39}
     40
     41void test_ringbuf_multi(void)
     42{
     43	struct test_ringbuf_multi *skel;
     44	struct ring_buffer *ringbuf = NULL;
     45	int err;
     46	int page_size = getpagesize();
     47	int proto_fd = -1;
     48
     49	skel = test_ringbuf_multi__open();
     50	if (CHECK(!skel, "skel_open", "skeleton open failed\n"))
     51		return;
     52
     53	proto_fd = bpf_map_create(BPF_MAP_TYPE_RINGBUF, NULL, 0, 0, page_size, NULL);
     54	if (CHECK(proto_fd < 0, "bpf_map_create", "bpf_map_create failed\n"))
     55		goto cleanup;
     56
     57	err = bpf_map__set_inner_map_fd(skel->maps.ringbuf_hash, proto_fd);
     58	if (CHECK(err != 0, "bpf_map__set_inner_map_fd", "bpf_map__set_inner_map_fd failed\n"))
     59		goto cleanup;
     60
     61	err = test_ringbuf_multi__load(skel);
     62	if (CHECK(err != 0, "skel_load", "skeleton load failed\n"))
     63		goto cleanup;
     64
     65	close(proto_fd);
     66	proto_fd = -1;
     67
     68	/* only trigger BPF program for current process */
     69	skel->bss->pid = getpid();
     70
     71	ringbuf = ring_buffer__new(bpf_map__fd(skel->maps.ringbuf1),
     72				   process_sample, (void *)(long)1, NULL);
     73	if (CHECK(!ringbuf, "ringbuf_create", "failed to create ringbuf\n"))
     74		goto cleanup;
     75
     76	err = ring_buffer__add(ringbuf, bpf_map__fd(skel->maps.ringbuf2),
     77			      process_sample, (void *)(long)2);
     78	if (CHECK(err, "ringbuf_add", "failed to add another ring\n"))
     79		goto cleanup;
     80
     81	err = test_ringbuf_multi__attach(skel);
     82	if (CHECK(err, "skel_attach", "skeleton attachment failed: %d\n", err))
     83		goto cleanup;
     84
     85	/* trigger few samples, some will be skipped */
     86	skel->bss->target_ring = 0;
     87	skel->bss->value = 333;
     88	syscall(__NR_getpgid);
     89
     90	/* skipped, no ringbuf in slot 1 */
     91	skel->bss->target_ring = 1;
     92	skel->bss->value = 555;
     93	syscall(__NR_getpgid);
     94
     95	skel->bss->target_ring = 2;
     96	skel->bss->value = 777;
     97	syscall(__NR_getpgid);
     98
     99	/* poll for samples, should get 2 ringbufs back */
    100	err = ring_buffer__poll(ringbuf, -1);
    101	if (CHECK(err != 2, "poll_res", "expected 2 records, got %d\n", err))
    102		goto cleanup;
    103
    104	/* expect extra polling to return nothing */
    105	err = ring_buffer__poll(ringbuf, 0);
    106	if (CHECK(err < 0, "extra_samples", "poll result: %d\n", err))
    107		goto cleanup;
    108
    109	CHECK(skel->bss->dropped != 0, "err_dropped", "exp %ld, got %ld\n",
    110	      0L, skel->bss->dropped);
    111	CHECK(skel->bss->skipped != 1, "err_skipped", "exp %ld, got %ld\n",
    112	      1L, skel->bss->skipped);
    113	CHECK(skel->bss->total != 2, "err_total", "exp %ld, got %ld\n",
    114	      2L, skel->bss->total);
    115
    116cleanup:
    117	if (proto_fd >= 0)
    118		close(proto_fd);
    119	ring_buffer__free(ringbuf);
    120	test_ringbuf_multi__destroy(skel);
    121}