1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2017 Mellanox Technologies, Ltd
10 #include <rte_malloc.h>
11 #include <rte_eth_tap.h>
13 #include <tap_autoconf.h>
14 #include <tap_tcmsgs.h>
16 #include <tap_bpf_insns.h>
19 * Load BPF program (section cls_q) into the kernel and return a bpf fd
22 * Queue index matching packet cb
25 * -1 if the BPF program couldn't be loaded. An fd (int) otherwise.
27 int tap_flow_bpf_cls_q(__u32 queue_idx)
29 cls_q_insns[1].imm = queue_idx;
31 return bpf_load(BPF_PROG_TYPE_SCHED_CLS,
32 (struct bpf_insn *)cls_q_insns,
38 * Load BPF program (section l3_l4) into the kernel and return a bpf fd.
44 * BPF RSS map file descriptor
47 * -1 if the BPF program couldn't be loaded. An fd (int) otherwise.
49 int tap_flow_bpf_calc_l3_l4_hash(__u32 key_idx, int map_fd)
51 l3_l4_hash_insns[4].imm = key_idx;
52 l3_l4_hash_insns[9].imm = map_fd;
54 return bpf_load(BPF_PROG_TYPE_SCHED_ACT,
55 (struct bpf_insn *)l3_l4_hash_insns,
56 RTE_DIM(l3_l4_hash_insns),
61 * Helper function to convert a pointer to unsigned 64 bits
67 * 64 bit unsigned long type of pointer address
69 static inline __u64 ptr_to_u64(const void *ptr)
71 return (__u64)(unsigned long)ptr;
75 * Call BPF system call
78 * BPF command for program loading, map creation, map entry update, etc
81 * System call attributes relevant to system call command
84 * size of attr parameter
87 * -1 if BPF system call failed, 0 otherwise
89 static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
92 return syscall(__NR_bpf, cmd, attr, size);
96 * Load BPF instructions to kernel
99 * BPF program type: classifieir or action
102 * Array of BPF instructions (equivalent to BPF instructions)
104 * @param[in] insns_cnt
105 * Number of BPF instructions (size of array)
107 * @param[in] lincense
108 * License string that must be acknowledged by the kernel
111 * -1 if the BPF program couldn't be loaded, fd (file descriptor) otherwise
113 static int bpf_load(enum bpf_prog_type type,
114 const struct bpf_insn *insns,
118 union bpf_attr attr = {};
120 bzero(&attr, sizeof(attr));
121 attr.prog_type = type;
122 attr.insn_cnt = (__u32)insns_cnt;
123 attr.insns = ptr_to_u64(insns);
124 attr.license = ptr_to_u64(license);
125 attr.log_buf = ptr_to_u64(NULL);
127 attr.kern_version = 0;
129 return sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
133 * Create BPF map for RSS rules
135 * @param[in] key_size
138 * @param[in] value_size
141 * @param[in] max_entries
142 * Map max number of RSS entries (limit on max RSS rules)
145 * -1 if BPF map couldn't be created, map fd otherwise
147 int tap_flow_bpf_rss_map_create(unsigned int key_size,
148 unsigned int value_size,
149 unsigned int max_entries)
151 union bpf_attr attr = {};
153 bzero(&attr, sizeof(attr));
154 attr.map_type = BPF_MAP_TYPE_HASH;
155 attr.key_size = key_size;
156 attr.value_size = value_size;
157 attr.max_entries = max_entries;
159 return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
163 * Update RSS entry in BPF map
169 * Pointer to RSS key whose entry is updated
172 * Pointer to RSS new updated value
175 * -1 if RSS entry failed to be updated, 0 otherwise
177 int tap_flow_bpf_update_rss_elem(int fd, void *key, void *value)
179 union bpf_attr attr = {};
181 bzero(&attr, sizeof(attr));
183 attr.map_type = BPF_MAP_TYPE_HASH;
185 attr.key = ptr_to_u64(key);
186 attr.value = ptr_to_u64(value);
187 attr.flags = BPF_ANY;
189 return sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));