2018-04-18 04:42:23 +00:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0
|
|
|
|
* Copyright (c) 2018 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/bpf.h>
|
|
|
|
#include <linux/if_link.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2019-06-25 00:55:36 +00:00
|
|
|
#include <net/if.h>
|
2018-04-18 04:42:23 +00:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netinet/ether.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <time.h>
|
2020-01-20 13:06:49 +00:00
|
|
|
#include <bpf/bpf.h>
|
|
|
|
#include <bpf/libbpf.h>
|
2018-04-18 04:42:23 +00:00
|
|
|
|
|
|
|
#define STATS_INTERVAL_S 2U
|
samples: bpf: Add max_pckt_size option at xdp_adjust_tail
Currently, at xdp_adjust_tail_kern.c, MAX_PCKT_SIZE is limited
to 600. To make this size flexible, static global variable
'max_pcktsz' is added.
By updating new packet size from the user space, xdp_adjust_tail_kern.o
will use this value as a new max packet size.
This static global variable can be accesible from .data section with
bpf_object__find_map* from user space, since it is considered as
internal map (accessible with .bss/.data/.rodata suffix).
If no '-P <MAX_PCKT_SIZE>' option is used, the size of maximum packet
will be 600 as a default.
For clarity, change the helper to fetch map from 'bpf_map__next'
to 'bpf_object__find_map_fd_by_name'. Also, changed the way to
test prog_fd, map_fd from '!= 0' to '< 0', since fd could be 0
when stdin is closed.
Signed-off-by: Daniel T. Lee <danieltimlee@gmail.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/20191007172117.3916-1-danieltimlee@gmail.com
2019-10-07 17:21:17 +00:00
|
|
|
#define MAX_PCKT_SIZE 600
|
2018-04-18 04:42:23 +00:00
|
|
|
|
|
|
|
static int ifindex = -1;
|
2019-02-01 21:42:28 +00:00
|
|
|
static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
|
2019-02-01 21:42:30 +00:00
|
|
|
static __u32 prog_id;
|
2018-04-18 04:42:23 +00:00
|
|
|
|
|
|
|
static void int_exit(int sig)
|
|
|
|
{
|
2019-02-01 21:42:30 +00:00
|
|
|
__u32 curr_prog_id = 0;
|
|
|
|
|
|
|
|
if (ifindex > -1) {
|
2022-01-20 06:14:22 +00:00
|
|
|
if (bpf_xdp_query_id(ifindex, xdp_flags, &curr_prog_id)) {
|
|
|
|
printf("bpf_xdp_query_id failed\n");
|
2019-02-01 21:42:30 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (prog_id == curr_prog_id)
|
2022-01-20 06:14:22 +00:00
|
|
|
bpf_xdp_detach(ifindex, xdp_flags, NULL);
|
2019-02-01 21:42:30 +00:00
|
|
|
else if (!curr_prog_id)
|
|
|
|
printf("couldn't find a prog id on a given iface\n");
|
|
|
|
else
|
|
|
|
printf("program on interface changed, not removing\n");
|
|
|
|
}
|
2018-04-18 04:42:23 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* simple "icmp packet too big sent" counter
|
|
|
|
*/
|
2018-05-10 17:24:43 +00:00
|
|
|
static void poll_stats(unsigned int map_fd, unsigned int kill_after_s)
|
2018-04-18 04:42:23 +00:00
|
|
|
{
|
|
|
|
time_t started_at = time(NULL);
|
|
|
|
__u64 value = 0;
|
|
|
|
int key = 0;
|
|
|
|
|
|
|
|
|
|
|
|
while (!kill_after_s || time(NULL) - started_at <= kill_after_s) {
|
|
|
|
sleep(STATS_INTERVAL_S);
|
|
|
|
|
2018-05-10 17:24:43 +00:00
|
|
|
assert(bpf_map_lookup_elem(map_fd, &key, &value) == 0);
|
2018-04-18 04:42:23 +00:00
|
|
|
|
|
|
|
printf("icmp \"packet too big\" sent: %10llu pkts\n", value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usage(const char *cmd)
|
|
|
|
{
|
|
|
|
printf("Start a XDP prog which send ICMP \"packet too big\" \n"
|
|
|
|
"messages if ingress packet is bigger then MAX_SIZE bytes\n");
|
|
|
|
printf("Usage: %s [...]\n", cmd);
|
2019-06-25 00:55:36 +00:00
|
|
|
printf(" -i <ifname|ifindex> Interface\n");
|
2018-04-18 04:42:23 +00:00
|
|
|
printf(" -T <stop-after-X-seconds> Default: 0 (forever)\n");
|
samples: bpf: Add max_pckt_size option at xdp_adjust_tail
Currently, at xdp_adjust_tail_kern.c, MAX_PCKT_SIZE is limited
to 600. To make this size flexible, static global variable
'max_pcktsz' is added.
By updating new packet size from the user space, xdp_adjust_tail_kern.o
will use this value as a new max packet size.
This static global variable can be accesible from .data section with
bpf_object__find_map* from user space, since it is considered as
internal map (accessible with .bss/.data/.rodata suffix).
If no '-P <MAX_PCKT_SIZE>' option is used, the size of maximum packet
will be 600 as a default.
For clarity, change the helper to fetch map from 'bpf_map__next'
to 'bpf_object__find_map_fd_by_name'. Also, changed the way to
test prog_fd, map_fd from '!= 0' to '< 0', since fd could be 0
when stdin is closed.
Signed-off-by: Daniel T. Lee <danieltimlee@gmail.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/20191007172117.3916-1-danieltimlee@gmail.com
2019-10-07 17:21:17 +00:00
|
|
|
printf(" -P <MAX_PCKT_SIZE> Default: %u\n", MAX_PCKT_SIZE);
|
2018-04-18 04:42:23 +00:00
|
|
|
printf(" -S use skb-mode\n");
|
|
|
|
printf(" -N enforce native mode\n");
|
2019-02-01 21:42:28 +00:00
|
|
|
printf(" -F force loading prog\n");
|
2018-04-18 04:42:23 +00:00
|
|
|
printf(" -h Display this help\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
unsigned char opt_flags[256] = {};
|
samples: bpf: Add max_pckt_size option at xdp_adjust_tail
Currently, at xdp_adjust_tail_kern.c, MAX_PCKT_SIZE is limited
to 600. To make this size flexible, static global variable
'max_pcktsz' is added.
By updating new packet size from the user space, xdp_adjust_tail_kern.o
will use this value as a new max packet size.
This static global variable can be accesible from .data section with
bpf_object__find_map* from user space, since it is considered as
internal map (accessible with .bss/.data/.rodata suffix).
If no '-P <MAX_PCKT_SIZE>' option is used, the size of maximum packet
will be 600 as a default.
For clarity, change the helper to fetch map from 'bpf_map__next'
to 'bpf_object__find_map_fd_by_name'. Also, changed the way to
test prog_fd, map_fd from '!= 0' to '< 0', since fd could be 0
when stdin is closed.
Signed-off-by: Daniel T. Lee <danieltimlee@gmail.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/20191007172117.3916-1-danieltimlee@gmail.com
2019-10-07 17:21:17 +00:00
|
|
|
const char *optstr = "i:T:P:SNFh";
|
2019-02-01 21:42:30 +00:00
|
|
|
struct bpf_prog_info info = {};
|
|
|
|
__u32 info_len = sizeof(info);
|
2018-04-18 04:42:23 +00:00
|
|
|
unsigned int kill_after_s = 0;
|
2018-05-10 17:24:43 +00:00
|
|
|
int i, prog_fd, map_fd, opt;
|
2022-02-02 22:59:16 +00:00
|
|
|
struct bpf_program *prog;
|
2018-05-10 17:24:43 +00:00
|
|
|
struct bpf_object *obj;
|
samples: bpf: Add max_pckt_size option at xdp_adjust_tail
Currently, at xdp_adjust_tail_kern.c, MAX_PCKT_SIZE is limited
to 600. To make this size flexible, static global variable
'max_pcktsz' is added.
By updating new packet size from the user space, xdp_adjust_tail_kern.o
will use this value as a new max packet size.
This static global variable can be accesible from .data section with
bpf_object__find_map* from user space, since it is considered as
internal map (accessible with .bss/.data/.rodata suffix).
If no '-P <MAX_PCKT_SIZE>' option is used, the size of maximum packet
will be 600 as a default.
For clarity, change the helper to fetch map from 'bpf_map__next'
to 'bpf_object__find_map_fd_by_name'. Also, changed the way to
test prog_fd, map_fd from '!= 0' to '< 0', since fd could be 0
when stdin is closed.
Signed-off-by: Daniel T. Lee <danieltimlee@gmail.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/20191007172117.3916-1-danieltimlee@gmail.com
2019-10-07 17:21:17 +00:00
|
|
|
__u32 max_pckt_size = 0;
|
|
|
|
__u32 key = 0;
|
2018-04-18 04:42:23 +00:00
|
|
|
char filename[256];
|
2019-02-01 21:42:30 +00:00
|
|
|
int err;
|
2018-04-18 04:42:23 +00:00
|
|
|
|
|
|
|
for (i = 0; i < strlen(optstr); i++)
|
|
|
|
if (optstr[i] != 'h' && 'a' <= optstr[i] && optstr[i] <= 'z')
|
|
|
|
opt_flags[(unsigned char)optstr[i]] = 1;
|
|
|
|
|
|
|
|
while ((opt = getopt(argc, argv, optstr)) != -1) {
|
|
|
|
|
|
|
|
switch (opt) {
|
|
|
|
case 'i':
|
2019-06-25 00:55:36 +00:00
|
|
|
ifindex = if_nametoindex(optarg);
|
|
|
|
if (!ifindex)
|
|
|
|
ifindex = atoi(optarg);
|
2018-04-18 04:42:23 +00:00
|
|
|
break;
|
|
|
|
case 'T':
|
|
|
|
kill_after_s = atoi(optarg);
|
|
|
|
break;
|
samples: bpf: Add max_pckt_size option at xdp_adjust_tail
Currently, at xdp_adjust_tail_kern.c, MAX_PCKT_SIZE is limited
to 600. To make this size flexible, static global variable
'max_pcktsz' is added.
By updating new packet size from the user space, xdp_adjust_tail_kern.o
will use this value as a new max packet size.
This static global variable can be accesible from .data section with
bpf_object__find_map* from user space, since it is considered as
internal map (accessible with .bss/.data/.rodata suffix).
If no '-P <MAX_PCKT_SIZE>' option is used, the size of maximum packet
will be 600 as a default.
For clarity, change the helper to fetch map from 'bpf_map__next'
to 'bpf_object__find_map_fd_by_name'. Also, changed the way to
test prog_fd, map_fd from '!= 0' to '< 0', since fd could be 0
when stdin is closed.
Signed-off-by: Daniel T. Lee <danieltimlee@gmail.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/20191007172117.3916-1-danieltimlee@gmail.com
2019-10-07 17:21:17 +00:00
|
|
|
case 'P':
|
|
|
|
max_pckt_size = atoi(optarg);
|
|
|
|
break;
|
2018-04-18 04:42:23 +00:00
|
|
|
case 'S':
|
|
|
|
xdp_flags |= XDP_FLAGS_SKB_MODE;
|
|
|
|
break;
|
|
|
|
case 'N':
|
samples/bpf: Attach XDP programs in driver mode by default
When attaching XDP programs, userspace can set flags to request the attach
mode (generic/SKB mode, driver mode or hw offloaded mode). If no such flags
are requested, the kernel will attempt to attach in driver mode, and then
silently fall back to SKB mode if this fails.
The silent fallback is a major source of user confusion, as users will try
to load a program on a device without XDP support, and instead of an error
they will get the silent fallback behaviour, not notice, and then wonder
why performance is not what they were expecting.
In an attempt to combat this, let's switch all the samples to default to
explicitly requesting driver-mode attach. As part of this, ensure that all
the userspace utilities have a switch to enable SKB mode. For those that
have a switch to request driver mode, keep it but turn it into a no-op.
Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Jesper Dangaard Brouer <brouer@redhat.com>
Acked-by: David Ahern <dsahern@gmail.com>
Link: https://lore.kernel.org/bpf/20191216110742.364456-1-toke@redhat.com
2019-12-16 11:07:42 +00:00
|
|
|
/* default, set below */
|
2018-04-18 04:42:23 +00:00
|
|
|
break;
|
2019-02-01 21:42:28 +00:00
|
|
|
case 'F':
|
|
|
|
xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
|
|
|
|
break;
|
2018-04-18 04:42:23 +00:00
|
|
|
default:
|
|
|
|
usage(argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
opt_flags[opt] = 0;
|
|
|
|
}
|
|
|
|
|
samples/bpf: Attach XDP programs in driver mode by default
When attaching XDP programs, userspace can set flags to request the attach
mode (generic/SKB mode, driver mode or hw offloaded mode). If no such flags
are requested, the kernel will attempt to attach in driver mode, and then
silently fall back to SKB mode if this fails.
The silent fallback is a major source of user confusion, as users will try
to load a program on a device without XDP support, and instead of an error
they will get the silent fallback behaviour, not notice, and then wonder
why performance is not what they were expecting.
In an attempt to combat this, let's switch all the samples to default to
explicitly requesting driver-mode attach. As part of this, ensure that all
the userspace utilities have a switch to enable SKB mode. For those that
have a switch to request driver mode, keep it but turn it into a no-op.
Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Jesper Dangaard Brouer <brouer@redhat.com>
Acked-by: David Ahern <dsahern@gmail.com>
Link: https://lore.kernel.org/bpf/20191216110742.364456-1-toke@redhat.com
2019-12-16 11:07:42 +00:00
|
|
|
if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
|
|
|
|
xdp_flags |= XDP_FLAGS_DRV_MODE;
|
|
|
|
|
2018-04-18 04:42:23 +00:00
|
|
|
for (i = 0; i < strlen(optstr); i++) {
|
|
|
|
if (opt_flags[(unsigned int)optstr[i]]) {
|
|
|
|
fprintf(stderr, "Missing argument -%c\n", optstr[i]);
|
|
|
|
usage(argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-25 00:55:36 +00:00
|
|
|
if (!ifindex) {
|
|
|
|
fprintf(stderr, "Invalid ifname\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-04-18 04:42:23 +00:00
|
|
|
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
|
2018-05-10 17:24:43 +00:00
|
|
|
|
2022-02-02 22:59:16 +00:00
|
|
|
obj = bpf_object__open_file(filename, NULL);
|
|
|
|
if (libbpf_get_error(obj))
|
2018-05-10 17:24:43 +00:00
|
|
|
return 1;
|
2018-04-18 04:42:23 +00:00
|
|
|
|
2022-02-02 22:59:16 +00:00
|
|
|
prog = bpf_object__next_program(obj, NULL);
|
|
|
|
bpf_program__set_type(prog, BPF_PROG_TYPE_XDP);
|
|
|
|
|
|
|
|
err = bpf_object__load(obj);
|
|
|
|
if (err)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
prog_fd = bpf_program__fd(prog);
|
|
|
|
|
samples: bpf: Add max_pckt_size option at xdp_adjust_tail
Currently, at xdp_adjust_tail_kern.c, MAX_PCKT_SIZE is limited
to 600. To make this size flexible, static global variable
'max_pcktsz' is added.
By updating new packet size from the user space, xdp_adjust_tail_kern.o
will use this value as a new max packet size.
This static global variable can be accesible from .data section with
bpf_object__find_map* from user space, since it is considered as
internal map (accessible with .bss/.data/.rodata suffix).
If no '-P <MAX_PCKT_SIZE>' option is used, the size of maximum packet
will be 600 as a default.
For clarity, change the helper to fetch map from 'bpf_map__next'
to 'bpf_object__find_map_fd_by_name'. Also, changed the way to
test prog_fd, map_fd from '!= 0' to '< 0', since fd could be 0
when stdin is closed.
Signed-off-by: Daniel T. Lee <danieltimlee@gmail.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/20191007172117.3916-1-danieltimlee@gmail.com
2019-10-07 17:21:17 +00:00
|
|
|
/* static global var 'max_pcktsz' is accessible from .data section */
|
|
|
|
if (max_pckt_size) {
|
|
|
|
map_fd = bpf_object__find_map_fd_by_name(obj, "xdp_adju.data");
|
|
|
|
if (map_fd < 0) {
|
|
|
|
printf("finding a max_pcktsz map in obj file failed\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
bpf_map_update_elem(map_fd, &key, &max_pckt_size, BPF_ANY);
|
2018-04-18 04:42:23 +00:00
|
|
|
}
|
|
|
|
|
samples: bpf: Add max_pckt_size option at xdp_adjust_tail
Currently, at xdp_adjust_tail_kern.c, MAX_PCKT_SIZE is limited
to 600. To make this size flexible, static global variable
'max_pcktsz' is added.
By updating new packet size from the user space, xdp_adjust_tail_kern.o
will use this value as a new max packet size.
This static global variable can be accesible from .data section with
bpf_object__find_map* from user space, since it is considered as
internal map (accessible with .bss/.data/.rodata suffix).
If no '-P <MAX_PCKT_SIZE>' option is used, the size of maximum packet
will be 600 as a default.
For clarity, change the helper to fetch map from 'bpf_map__next'
to 'bpf_object__find_map_fd_by_name'. Also, changed the way to
test prog_fd, map_fd from '!= 0' to '< 0', since fd could be 0
when stdin is closed.
Signed-off-by: Daniel T. Lee <danieltimlee@gmail.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/20191007172117.3916-1-danieltimlee@gmail.com
2019-10-07 17:21:17 +00:00
|
|
|
/* fetch icmpcnt map */
|
|
|
|
map_fd = bpf_object__find_map_fd_by_name(obj, "icmpcnt");
|
|
|
|
if (map_fd < 0) {
|
|
|
|
printf("finding a icmpcnt map in obj file failed\n");
|
2018-04-18 04:42:23 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
signal(SIGINT, int_exit);
|
|
|
|
signal(SIGTERM, int_exit);
|
|
|
|
|
2022-01-20 06:14:22 +00:00
|
|
|
if (bpf_xdp_attach(ifindex, prog_fd, xdp_flags, NULL) < 0) {
|
2018-04-18 04:42:23 +00:00
|
|
|
printf("link set xdp fd failed\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-02-14 23:12:17 +00:00
|
|
|
err = bpf_prog_get_info_by_fd(prog_fd, &info, &info_len);
|
2019-02-01 21:42:30 +00:00
|
|
|
if (err) {
|
|
|
|
printf("can't get prog info - %s\n", strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
prog_id = info.id;
|
2018-04-18 04:42:23 +00:00
|
|
|
|
2019-02-01 21:42:30 +00:00
|
|
|
poll_stats(map_fd, kill_after_s);
|
|
|
|
int_exit(0);
|
2018-04-18 04:42:23 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|