pping: Define BPF program names

Define the BPF program names in the user space component. The strings
corresponding to the BPF program names were before inserted in several
places, including in multiple string comparison, which is error prone
and could leave to subtle errors if the program names are changed and
not updated correctly in all places. With the program name string
being defined, they only have to be changed in a single place.

Currently only the names of the ingress programs occur in multiple
places, but also define the name for the egress program to be
consistent.

Note that even after this change one has the sync the defined values
with the actual program names declared in the pping_kern.c
file. Ideally, these would all be defined in a single place, but not
aware of a convenient way to make that happen (cannot use the defined
strings as function names as they are not identifiers, and if defined
as identifiers instead it would not be possible to use them as
strings).

Signed-off-by: Simon Sundberg <simon.sundberg@kau.se>
This commit is contained in:
Simon Sundberg
2022-11-03 15:04:25 +01:00
parent ddf25abfcc
commit 832bdea23f

View File

@ -32,6 +32,10 @@ static const char *__doc__ =
#define MON_TO_REAL_UPDATE_FREQ \ #define MON_TO_REAL_UPDATE_FREQ \
(1 * NS_PER_SECOND) // Update offset between CLOCK_MONOTONIC and CLOCK_REALTIME once per second (1 * NS_PER_SECOND) // Update offset between CLOCK_MONOTONIC and CLOCK_REALTIME once per second
#define PROG_INGRESS_TC "pping_tc_ingress"
#define PROG_INGRESS_XDP "pping_xdp_ingress"
#define PROG_EGRESS_TC "pping_tc_egress"
enum PPING_OUTPUT_FORMAT { enum PPING_OUTPUT_FORMAT {
PPING_OUTPUT_STANDARD, PPING_OUTPUT_STANDARD,
PPING_OUTPUT_JSON, PPING_OUTPUT_JSON,
@ -247,9 +251,9 @@ static int parse_arguments(int argc, char *argv[], struct pping_config *config)
break; break;
case 'I': case 'I':
if (strcmp(optarg, "xdp") == 0) { if (strcmp(optarg, "xdp") == 0) {
config->ingress_prog = "pping_xdp_ingress"; config->ingress_prog = PROG_INGRESS_XDP;
} else if (strcmp(optarg, "tc") == 0) { } else if (strcmp(optarg, "tc") == 0) {
config->ingress_prog = "pping_tc_ingress"; config->ingress_prog = PROG_INGRESS_TC;
} else { } else {
fprintf(stderr, fprintf(stderr,
"ingress-hook must be \"xdp\" or \"tc\"\n"); "ingress-hook must be \"xdp\" or \"tc\"\n");
@ -849,9 +853,9 @@ static int set_programs_to_load(struct bpf_object *obj,
{ {
struct bpf_program *prog; struct bpf_program *prog;
char *unload_prog = char *unload_prog =
strcmp(config->ingress_prog, "pping_xdp_ingress") != 0 ? strcmp(config->ingress_prog, PROG_INGRESS_XDP) != 0 ?
"pping_xdp_ingress" : PROG_INGRESS_XDP :
"pping_tc_ingress"; PROG_INGRESS_TC;
prog = bpf_object__find_program_by_name(obj, unload_prog); prog = bpf_object__find_program_by_name(obj, unload_prog);
if (libbpf_get_error(prog)) if (libbpf_get_error(prog))
@ -886,7 +890,7 @@ static int load_attach_bpfprogs(struct bpf_object **obj,
set_programs_to_load(*obj, config); set_programs_to_load(*obj, config);
// Attach ingress prog // Attach ingress prog
if (strcmp(config->ingress_prog, "pping_xdp_ingress") == 0) { if (strcmp(config->ingress_prog, PROG_INGRESS_XDP) == 0) {
/* xdp_attach() loads 'obj' through libxdp */ /* xdp_attach() loads 'obj' through libxdp */
err = xdp_attach(*obj, config->ingress_prog, config->ifindex, err = xdp_attach(*obj, config->ingress_prog, config->ifindex,
&config->xdp_prog); &config->xdp_prog);
@ -1009,8 +1013,8 @@ int main(int argc, char *argv[])
.clean_args = { .cleanup_interval = 1 * NS_PER_SECOND, .clean_args = { .cleanup_interval = 1 * NS_PER_SECOND,
.valid_thread = false }, .valid_thread = false },
.object_path = "pping_kern.o", .object_path = "pping_kern.o",
.ingress_prog = "pping_xdp_ingress", .ingress_prog = PROG_INGRESS_XDP,
.egress_prog = "pping_tc_egress", .egress_prog = PROG_EGRESS_TC,
.cleanup_ts_prog = "tsmap_cleanup", .cleanup_ts_prog = "tsmap_cleanup",
.cleanup_flow_prog = "flowmap_cleanup", .cleanup_flow_prog = "flowmap_cleanup",
.packet_map = "packet_ts", .packet_map = "packet_ts",