Blame view

samples/bpf/tc_l2_redirect_user.c 1.46 KB
90e02896f   Martin KaFai Lau   bpf: Add test for...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  /* 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 <stdlib.h>
  #include <stdio.h>
  #include <unistd.h>
  #include <string.h>
  #include <errno.h>
  
  #include "libbpf.h"
  
  static void usage(void)
  {
  	printf("Usage: tc_l2_ipip_redirect [...]
  ");
  	printf("       -U <file>   Update an already pinned BPF array
  ");
  	printf("       -i <ifindex> Interface index
  ");
  	printf("       -h          Display this help
  ");
  }
  
  int main(int argc, char **argv)
  {
  	const char *pinned_file = NULL;
  	int ifindex = -1;
  	int array_key = 0;
  	int array_fd = -1;
  	int ret = -1;
  	int opt;
  
  	while ((opt = getopt(argc, argv, "F:U:i:")) != -1) {
  		switch (opt) {
  		/* General args */
  		case 'U':
  			pinned_file = optarg;
  			break;
  		case 'i':
  			ifindex = atoi(optarg);
  			break;
  		default:
  			usage();
  			goto out;
  		}
  	}
  
  	if (ifindex < 0 || !pinned_file) {
  		usage();
  		goto out;
  	}
  
  	array_fd = bpf_obj_get(pinned_file);
  	if (array_fd < 0) {
  		fprintf(stderr, "bpf_obj_get(%s): %s(%d)
  ",
  			pinned_file, strerror(errno), errno);
  		goto out;
  	}
  
  	/* bpf_tunnel_key.remote_ipv4 expects host byte orders */
d40fc181e   Joe Stringer   samples/bpf: Make...
68
  	ret = bpf_map_update_elem(array_fd, &array_key, &ifindex, 0);
90e02896f   Martin KaFai Lau   bpf: Add test for...
69
  	if (ret) {
d40fc181e   Joe Stringer   samples/bpf: Make...
70
  		perror("bpf_map_update_elem");
90e02896f   Martin KaFai Lau   bpf: Add test for...
71
72
73
74
75
76
77
78
  		goto out;
  	}
  
  out:
  	if (array_fd != -1)
  		close(array_fd);
  	return ret;
  }