Linux Audio

Check our new training course

Loading...
Note: File does not exist in v6.9.4.
  1/* Copyright (c) 2016 John Fastabend <john.r.fastabend@intel.com>
  2 *
  3 * This program is free software; you can redistribute it and/or
  4 * modify it under the terms of version 2 of the GNU General Public
  5 * License as published by the Free Software Foundation.
  6 *
  7 * This program is distributed in the hope that it will be useful, but
  8 * WITHOUT ANY WARRANTY; without even the implied warranty of
  9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 10 * General Public License for more details.
 11 */
 12#include <linux/bpf.h>
 13#include <linux/if_link.h>
 14#include <assert.h>
 15#include <errno.h>
 16#include <signal.h>
 17#include <stdio.h>
 18#include <stdlib.h>
 19#include <stdbool.h>
 20#include <string.h>
 21#include <unistd.h>
 22#include <libgen.h>
 23#include <sys/resource.h>
 24
 25#include "bpf_load.h"
 26#include "bpf_util.h"
 27#include "libbpf.h"
 28
 29static int ifindex_in;
 30static int ifindex_out;
 31static bool ifindex_out_xdp_dummy_attached = true;
 32
 33static __u32 xdp_flags;
 34
 35static void int_exit(int sig)
 36{
 37	bpf_set_link_xdp_fd(ifindex_in, -1, xdp_flags);
 38	if (ifindex_out_xdp_dummy_attached)
 39		bpf_set_link_xdp_fd(ifindex_out, -1, xdp_flags);
 40	exit(0);
 41}
 42
 43static void poll_stats(int interval, int ifindex)
 44{
 45	unsigned int nr_cpus = bpf_num_possible_cpus();
 46	__u64 values[nr_cpus], prev[nr_cpus];
 47
 48	memset(prev, 0, sizeof(prev));
 49
 50	while (1) {
 51		__u64 sum = 0;
 52		__u32 key = 0;
 53		int i;
 54
 55		sleep(interval);
 56		assert(bpf_map_lookup_elem(map_fd[1], &key, values) == 0);
 57		for (i = 0; i < nr_cpus; i++)
 58			sum += (values[i] - prev[i]);
 59		if (sum)
 60			printf("ifindex %i: %10llu pkt/s\n",
 61			       ifindex, sum / interval);
 62		memcpy(prev, values, sizeof(values));
 63	}
 64}
 65
 66static void usage(const char *prog)
 67{
 68	fprintf(stderr,
 69		"usage: %s [OPTS] IFINDEX_IN IFINDEX_OUT\n\n"
 70		"OPTS:\n"
 71		"    -S    use skb-mode\n"
 72		"    -N    enforce native mode\n",
 73		prog);
 74}
 75
 76
 77int main(int argc, char **argv)
 78{
 79	struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
 80	const char *optstr = "SN";
 81	char filename[256];
 82	int ret, opt, key = 0;
 83
 84	while ((opt = getopt(argc, argv, optstr)) != -1) {
 85		switch (opt) {
 86		case 'S':
 87			xdp_flags |= XDP_FLAGS_SKB_MODE;
 88			break;
 89		case 'N':
 90			xdp_flags |= XDP_FLAGS_DRV_MODE;
 91			break;
 92		default:
 93			usage(basename(argv[0]));
 94			return 1;
 95		}
 96	}
 97
 98	if (optind == argc) {
 99		printf("usage: %s IFINDEX_IN IFINDEX_OUT\n", argv[0]);
100		return 1;
101	}
102
103	if (setrlimit(RLIMIT_MEMLOCK, &r)) {
104		perror("setrlimit(RLIMIT_MEMLOCK)");
105		return 1;
106	}
107
108	ifindex_in = strtoul(argv[optind], NULL, 0);
109	ifindex_out = strtoul(argv[optind + 1], NULL, 0);
110	printf("input: %d output: %d\n", ifindex_in, ifindex_out);
111
112	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
113
114	if (load_bpf_file(filename)) {
115		printf("%s", bpf_log_buf);
116		return 1;
117	}
118
119	if (!prog_fd[0]) {
120		printf("load_bpf_file: %s\n", strerror(errno));
121		return 1;
122	}
123
124	if (bpf_set_link_xdp_fd(ifindex_in, prog_fd[0], xdp_flags) < 0) {
125		printf("ERROR: link set xdp fd failed on %d\n", ifindex_in);
126		return 1;
127	}
128
129	/* Loading dummy XDP prog on out-device */
130	if (bpf_set_link_xdp_fd(ifindex_out, prog_fd[1],
131			    (xdp_flags | XDP_FLAGS_UPDATE_IF_NOEXIST)) < 0) {
132		printf("WARN: link set xdp fd failed on %d\n", ifindex_out);
133		ifindex_out_xdp_dummy_attached = false;
134	}
135
136	signal(SIGINT, int_exit);
137	signal(SIGTERM, int_exit);
138
139	/* bpf redirect port */
140	ret = bpf_map_update_elem(map_fd[0], &key, &ifindex_out, 0);
141	if (ret) {
142		perror("bpf_update_elem");
143		goto out;
144	}
145
146	poll_stats(2, ifindex_out);
147
148out:
149	return 0;
150}