aboutsummaryrefslogtreecommitdiffstats
path: root/samples/bpf
diff options
context:
space:
mode:
authorAlexei Starovoitov <ast@fb.com>2016-03-08 00:57:20 -0500
committerDavid S. Miller <davem@davemloft.net>2016-03-08 15:28:32 -0500
commit89b976070190eb9dd14943c0d6ca4b7209f61405 (patch)
tree36762c9aedfdf442e89148c0079e6cc1cbebbd57 /samples/bpf
parent3622e7e4935105991dc648bca650c858576aecda (diff)
samples/bpf: add map_flags to bpf loader
note old loader is compatible with new kernel. map_flags are optional Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'samples/bpf')
-rw-r--r--samples/bpf/bpf_helpers.h1
-rw-r--r--samples/bpf/bpf_load.c3
-rw-r--r--samples/bpf/fds_example.c2
-rw-r--r--samples/bpf/libbpf.c5
-rw-r--r--samples/bpf/libbpf.h2
-rw-r--r--samples/bpf/sock_example.c2
-rw-r--r--samples/bpf/test_maps.c19
-rw-r--r--samples/bpf/test_verifier.c4
8 files changed, 23 insertions, 15 deletions
diff --git a/samples/bpf/bpf_helpers.h b/samples/bpf/bpf_helpers.h
index 811bcca0f29d..9363500131a7 100644
--- a/samples/bpf/bpf_helpers.h
+++ b/samples/bpf/bpf_helpers.h
@@ -61,6 +61,7 @@ struct bpf_map_def {
61 unsigned int key_size; 61 unsigned int key_size;
62 unsigned int value_size; 62 unsigned int value_size;
63 unsigned int max_entries; 63 unsigned int max_entries;
64 unsigned int map_flags;
64}; 65};
65 66
66static int (*bpf_skb_store_bytes)(void *ctx, int off, void *from, int len, int flags) = 67static int (*bpf_skb_store_bytes)(void *ctx, int off, void *from, int len, int flags) =
diff --git a/samples/bpf/bpf_load.c b/samples/bpf/bpf_load.c
index d16864293c00..58f86bd11b3d 100644
--- a/samples/bpf/bpf_load.c
+++ b/samples/bpf/bpf_load.c
@@ -157,7 +157,8 @@ static int load_maps(struct bpf_map_def *maps, int len)
157 map_fd[i] = bpf_create_map(maps[i].type, 157 map_fd[i] = bpf_create_map(maps[i].type,
158 maps[i].key_size, 158 maps[i].key_size,
159 maps[i].value_size, 159 maps[i].value_size,
160 maps[i].max_entries); 160 maps[i].max_entries,
161 maps[i].map_flags);
161 if (map_fd[i] < 0) { 162 if (map_fd[i] < 0) {
162 printf("failed to create a map: %d %s\n", 163 printf("failed to create a map: %d %s\n",
163 errno, strerror(errno)); 164 errno, strerror(errno));
diff --git a/samples/bpf/fds_example.c b/samples/bpf/fds_example.c
index e2fd16c3d0f0..625e797be6ef 100644
--- a/samples/bpf/fds_example.c
+++ b/samples/bpf/fds_example.c
@@ -44,7 +44,7 @@ static void usage(void)
44static int bpf_map_create(void) 44static int bpf_map_create(void)
45{ 45{
46 return bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(uint32_t), 46 return bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(uint32_t),
47 sizeof(uint32_t), 1024); 47 sizeof(uint32_t), 1024, 0);
48} 48}
49 49
50static int bpf_prog_create(const char *object) 50static int bpf_prog_create(const char *object)
diff --git a/samples/bpf/libbpf.c b/samples/bpf/libbpf.c
index 65a8d48d2799..9969e35550c3 100644
--- a/samples/bpf/libbpf.c
+++ b/samples/bpf/libbpf.c
@@ -19,13 +19,14 @@ static __u64 ptr_to_u64(void *ptr)
19} 19}
20 20
21int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size, 21int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size,
22 int max_entries) 22 int max_entries, int map_flags)
23{ 23{
24 union bpf_attr attr = { 24 union bpf_attr attr = {
25 .map_type = map_type, 25 .map_type = map_type,
26 .key_size = key_size, 26 .key_size = key_size,
27 .value_size = value_size, 27 .value_size = value_size,
28 .max_entries = max_entries 28 .max_entries = max_entries,
29 .map_flags = map_flags,
29 }; 30 };
30 31
31 return syscall(__NR_bpf, BPF_MAP_CREATE, &attr, sizeof(attr)); 32 return syscall(__NR_bpf, BPF_MAP_CREATE, &attr, sizeof(attr));
diff --git a/samples/bpf/libbpf.h b/samples/bpf/libbpf.h
index 014aacf916e4..364582b77888 100644
--- a/samples/bpf/libbpf.h
+++ b/samples/bpf/libbpf.h
@@ -5,7 +5,7 @@
5struct bpf_insn; 5struct bpf_insn;
6 6
7int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size, 7int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size,
8 int max_entries); 8 int max_entries, int map_flags);
9int bpf_update_elem(int fd, void *key, void *value, unsigned long long flags); 9int bpf_update_elem(int fd, void *key, void *value, unsigned long long flags);
10int bpf_lookup_elem(int fd, void *key, void *value); 10int bpf_lookup_elem(int fd, void *key, void *value);
11int bpf_delete_elem(int fd, void *key); 11int bpf_delete_elem(int fd, void *key);
diff --git a/samples/bpf/sock_example.c b/samples/bpf/sock_example.c
index a0ce251c5390..28b60baa9fa8 100644
--- a/samples/bpf/sock_example.c
+++ b/samples/bpf/sock_example.c
@@ -34,7 +34,7 @@ static int test_sock(void)
34 long long value = 0, tcp_cnt, udp_cnt, icmp_cnt; 34 long long value = 0, tcp_cnt, udp_cnt, icmp_cnt;
35 35
36 map_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(key), sizeof(value), 36 map_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(key), sizeof(value),
37 256); 37 256, 0);
38 if (map_fd < 0) { 38 if (map_fd < 0) {
39 printf("failed to create map '%s'\n", strerror(errno)); 39 printf("failed to create map '%s'\n", strerror(errno));
40 goto cleanup; 40 goto cleanup;
diff --git a/samples/bpf/test_maps.c b/samples/bpf/test_maps.c
index ad466ed33093..7bd9edd02d9b 100644
--- a/samples/bpf/test_maps.c
+++ b/samples/bpf/test_maps.c
@@ -2,6 +2,7 @@
2 * Testsuite for eBPF maps 2 * Testsuite for eBPF maps
3 * 3 *
4 * Copyright (c) 2014 PLUMgrid, http://plumgrid.com 4 * Copyright (c) 2014 PLUMgrid, http://plumgrid.com
5 * Copyright (c) 2016 Facebook
5 * 6 *
6 * This program is free software; you can redistribute it and/or 7 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public 8 * modify it under the terms of version 2 of the GNU General Public
@@ -17,13 +18,16 @@
17#include <stdlib.h> 18#include <stdlib.h>
18#include "libbpf.h" 19#include "libbpf.h"
19 20
21static int map_flags;
22
20/* sanity tests for map API */ 23/* sanity tests for map API */
21static void test_hashmap_sanity(int i, void *data) 24static void test_hashmap_sanity(int i, void *data)
22{ 25{
23 long long key, next_key, value; 26 long long key, next_key, value;
24 int map_fd; 27 int map_fd;
25 28
26 map_fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value), 2); 29 map_fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
30 2, map_flags);
27 if (map_fd < 0) { 31 if (map_fd < 0) {
28 printf("failed to create hashmap '%s'\n", strerror(errno)); 32 printf("failed to create hashmap '%s'\n", strerror(errno));
29 exit(1); 33 exit(1);
@@ -99,7 +103,7 @@ static void test_percpu_hashmap_sanity(int task, void *data)
99 int map_fd, i; 103 int map_fd, i;
100 104
101 map_fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_HASH, sizeof(key), 105 map_fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_HASH, sizeof(key),
102 sizeof(value[0]), 2); 106 sizeof(value[0]), 2, map_flags);
103 if (map_fd < 0) { 107 if (map_fd < 0) {
104 printf("failed to create hashmap '%s'\n", strerror(errno)); 108 printf("failed to create hashmap '%s'\n", strerror(errno));
105 exit(1); 109 exit(1);
@@ -188,7 +192,8 @@ static void test_arraymap_sanity(int i, void *data)
188 int key, next_key, map_fd; 192 int key, next_key, map_fd;
189 long long value; 193 long long value;
190 194
191 map_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(key), sizeof(value), 2); 195 map_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(key), sizeof(value),
196 2, 0);
192 if (map_fd < 0) { 197 if (map_fd < 0) {
193 printf("failed to create arraymap '%s'\n", strerror(errno)); 198 printf("failed to create arraymap '%s'\n", strerror(errno));
194 exit(1); 199 exit(1);
@@ -244,7 +249,7 @@ static void test_percpu_arraymap_many_keys(void)
244 int key, map_fd, i; 249 int key, map_fd, i;
245 250
246 map_fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key), 251 map_fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key),
247 sizeof(values[0]), nr_keys); 252 sizeof(values[0]), nr_keys, 0);
248 if (map_fd < 0) { 253 if (map_fd < 0) {
249 printf("failed to create per-cpu arraymap '%s'\n", 254 printf("failed to create per-cpu arraymap '%s'\n",
250 strerror(errno)); 255 strerror(errno));
@@ -275,7 +280,7 @@ static void test_percpu_arraymap_sanity(int i, void *data)
275 int key, next_key, map_fd; 280 int key, next_key, map_fd;
276 281
277 map_fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key), 282 map_fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key),
278 sizeof(values[0]), 2); 283 sizeof(values[0]), 2, 0);
279 if (map_fd < 0) { 284 if (map_fd < 0) {
280 printf("failed to create arraymap '%s'\n", strerror(errno)); 285 printf("failed to create arraymap '%s'\n", strerror(errno));
281 exit(1); 286 exit(1);
@@ -336,7 +341,7 @@ static void test_map_large(void)
336 341
337 /* allocate 4Mbyte of memory */ 342 /* allocate 4Mbyte of memory */
338 map_fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value), 343 map_fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
339 MAP_SIZE); 344 MAP_SIZE, map_flags);
340 if (map_fd < 0) { 345 if (map_fd < 0) {
341 printf("failed to create large map '%s'\n", strerror(errno)); 346 printf("failed to create large map '%s'\n", strerror(errno));
342 exit(1); 347 exit(1);
@@ -421,7 +426,7 @@ static void test_map_parallel(void)
421 int data[2]; 426 int data[2];
422 427
423 map_fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value), 428 map_fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
424 MAP_SIZE); 429 MAP_SIZE, map_flags);
425 if (map_fd < 0) { 430 if (map_fd < 0) {
426 printf("failed to create map for parallel test '%s'\n", 431 printf("failed to create map for parallel test '%s'\n",
427 strerror(errno)); 432 strerror(errno));
diff --git a/samples/bpf/test_verifier.c b/samples/bpf/test_verifier.c
index 563c507c0a09..4b51a9039c0d 100644
--- a/samples/bpf/test_verifier.c
+++ b/samples/bpf/test_verifier.c
@@ -1198,7 +1198,7 @@ static int create_map(void)
1198 int map_fd; 1198 int map_fd;
1199 1199
1200 map_fd = bpf_create_map(BPF_MAP_TYPE_HASH, 1200 map_fd = bpf_create_map(BPF_MAP_TYPE_HASH,
1201 sizeof(long long), sizeof(long long), 1024); 1201 sizeof(long long), sizeof(long long), 1024, 0);
1202 if (map_fd < 0) 1202 if (map_fd < 0)
1203 printf("failed to create map '%s'\n", strerror(errno)); 1203 printf("failed to create map '%s'\n", strerror(errno));
1204 1204
@@ -1210,7 +1210,7 @@ static int create_prog_array(void)
1210 int map_fd; 1210 int map_fd;
1211 1211
1212 map_fd = bpf_create_map(BPF_MAP_TYPE_PROG_ARRAY, 1212 map_fd = bpf_create_map(BPF_MAP_TYPE_PROG_ARRAY,
1213 sizeof(int), sizeof(int), 4); 1213 sizeof(int), sizeof(int), 4, 0);
1214 if (map_fd < 0) 1214 if (map_fd < 0)
1215 printf("failed to create prog_array '%s'\n", strerror(errno)); 1215 printf("failed to create prog_array '%s'\n", strerror(errno));
1216 1216