samples/bpf: add map_flags to bpf loader
authorAlexei Starovoitov <ast@fb.com>
Tue, 8 Mar 2016 05:57:20 +0000 (21:57 -0800)
committerDavid S. Miller <davem@davemloft.net>
Tue, 8 Mar 2016 20:28:32 +0000 (15:28 -0500)
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>
samples/bpf/bpf_helpers.h
samples/bpf/bpf_load.c
samples/bpf/fds_example.c
samples/bpf/libbpf.c
samples/bpf/libbpf.h
samples/bpf/sock_example.c
samples/bpf/test_maps.c
samples/bpf/test_verifier.c

index 811bcca0f29dafe553beefef6303f16a9dd8b71b..9363500131a777f98cd7016a35b9cf9858dbf9a6 100644 (file)
@@ -61,6 +61,7 @@ struct bpf_map_def {
        unsigned int key_size;
        unsigned int value_size;
        unsigned int max_entries;
+       unsigned int map_flags;
 };
 
 static int (*bpf_skb_store_bytes)(void *ctx, int off, void *from, int len, int flags) =
index d16864293c00721f33ea6b945f4dbaaa9e0cce25..58f86bd11b3d239022d75a6c2c3a607bbb2ff61a 100644 (file)
@@ -157,7 +157,8 @@ static int load_maps(struct bpf_map_def *maps, int len)
                map_fd[i] = bpf_create_map(maps[i].type,
                                           maps[i].key_size,
                                           maps[i].value_size,
-                                          maps[i].max_entries);
+                                          maps[i].max_entries,
+                                          maps[i].map_flags);
                if (map_fd[i] < 0) {
                        printf("failed to create a map: %d %s\n",
                               errno, strerror(errno));
index e2fd16c3d0f07ca4461a0ad239d0c2426d880b97..625e797be6ef897f929adebffe9dcbf71cd59e03 100644 (file)
@@ -44,7 +44,7 @@ static void usage(void)
 static int bpf_map_create(void)
 {
        return bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(uint32_t),
-                             sizeof(uint32_t), 1024);
+                             sizeof(uint32_t), 1024, 0);
 }
 
 static int bpf_prog_create(const char *object)
index 65a8d48d2799e43a1cfbd57ba13e353b1fa3856a..9969e35550c3dd3c9702e7e99dab35b3df9fb7f6 100644 (file)
@@ -19,13 +19,14 @@ static __u64 ptr_to_u64(void *ptr)
 }
 
 int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size,
-                  int max_entries)
+                  int max_entries, int map_flags)
 {
        union bpf_attr attr = {
                .map_type = map_type,
                .key_size = key_size,
                .value_size = value_size,
-               .max_entries = max_entries
+               .max_entries = max_entries,
+               .map_flags = map_flags,
        };
 
        return syscall(__NR_bpf, BPF_MAP_CREATE, &attr, sizeof(attr));
index 014aacf916e4063c28227da658e38e7c011c8c46..364582b778882f782684ea1bbd675cb801ff290b 100644 (file)
@@ -5,7 +5,7 @@
 struct bpf_insn;
 
 int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size,
-                  int max_entries);
+                  int max_entries, int map_flags);
 int bpf_update_elem(int fd, void *key, void *value, unsigned long long flags);
 int bpf_lookup_elem(int fd, void *key, void *value);
 int bpf_delete_elem(int fd, void *key);
index a0ce251c53900e08617740633cc726b045c8fcc1..28b60baa9fa82b4fbb0db4526aeddec65459bf96 100644 (file)
@@ -34,7 +34,7 @@ static int test_sock(void)
        long long value = 0, tcp_cnt, udp_cnt, icmp_cnt;
 
        map_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(key), sizeof(value),
-                               256);
+                               256, 0);
        if (map_fd < 0) {
                printf("failed to create map '%s'\n", strerror(errno));
                goto cleanup;
index ad466ed3309307c79a7d393359f74a6c558eef1b..7bd9edd02d9b1e7c19339a6a98b9e513f8edd81d 100644 (file)
@@ -2,6 +2,7 @@
  * Testsuite for eBPF maps
  *
  * Copyright (c) 2014 PLUMgrid, http://plumgrid.com
+ * 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
 #include <stdlib.h>
 #include "libbpf.h"
 
+static int map_flags;
+
 /* sanity tests for map API */
 static void test_hashmap_sanity(int i, void *data)
 {
        long long key, next_key, value;
        int map_fd;
 
-       map_fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value), 2);
+       map_fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
+                               2, map_flags);
        if (map_fd < 0) {
                printf("failed to create hashmap '%s'\n", strerror(errno));
                exit(1);
@@ -99,7 +103,7 @@ static void test_percpu_hashmap_sanity(int task, void *data)
        int map_fd, i;
 
        map_fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_HASH, sizeof(key),
-                               sizeof(value[0]), 2);
+                               sizeof(value[0]), 2, map_flags);
        if (map_fd < 0) {
                printf("failed to create hashmap '%s'\n", strerror(errno));
                exit(1);
@@ -188,7 +192,8 @@ static void test_arraymap_sanity(int i, void *data)
        int key, next_key, map_fd;
        long long value;
 
-       map_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(key), sizeof(value), 2);
+       map_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(key), sizeof(value),
+                               2, 0);
        if (map_fd < 0) {
                printf("failed to create arraymap '%s'\n", strerror(errno));
                exit(1);
@@ -244,7 +249,7 @@ static void test_percpu_arraymap_many_keys(void)
        int key, map_fd, i;
 
        map_fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key),
-                               sizeof(values[0]), nr_keys);
+                               sizeof(values[0]), nr_keys, 0);
        if (map_fd < 0) {
                printf("failed to create per-cpu arraymap '%s'\n",
                       strerror(errno));
@@ -275,7 +280,7 @@ static void test_percpu_arraymap_sanity(int i, void *data)
        int key, next_key, map_fd;
 
        map_fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key),
-                               sizeof(values[0]), 2);
+                               sizeof(values[0]), 2, 0);
        if (map_fd < 0) {
                printf("failed to create arraymap '%s'\n", strerror(errno));
                exit(1);
@@ -336,7 +341,7 @@ static void test_map_large(void)
 
        /* allocate 4Mbyte of memory */
        map_fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
-                               MAP_SIZE);
+                               MAP_SIZE, map_flags);
        if (map_fd < 0) {
                printf("failed to create large map '%s'\n", strerror(errno));
                exit(1);
@@ -421,7 +426,7 @@ static void test_map_parallel(void)
        int data[2];
 
        map_fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
-                               MAP_SIZE);
+                               MAP_SIZE, map_flags);
        if (map_fd < 0) {
                printf("failed to create map for parallel test '%s'\n",
                       strerror(errno));
index 563c507c0a09f3868827ce6ce1e0e88669803016..4b51a9039c0d4abd9a4995b2df0d178e0422ff19 100644 (file)
@@ -1198,7 +1198,7 @@ static int create_map(void)
        int map_fd;
 
        map_fd = bpf_create_map(BPF_MAP_TYPE_HASH,
-                               sizeof(long long), sizeof(long long), 1024);
+                               sizeof(long long), sizeof(long long), 1024, 0);
        if (map_fd < 0)
                printf("failed to create map '%s'\n", strerror(errno));
 
@@ -1210,7 +1210,7 @@ static int create_prog_array(void)
        int map_fd;
 
        map_fd = bpf_create_map(BPF_MAP_TYPE_PROG_ARRAY,
-                               sizeof(int), sizeof(int), 4);
+                               sizeof(int), sizeof(int), 4, 0);
        if (map_fd < 0)
                printf("failed to create prog_array '%s'\n", strerror(errno));