1
2
3
4
5#include <errno.h>
6#include <string.h>
7#include <unistd.h>
8#include <sys/queue.h>
9
10#include <rte_malloc.h>
11#include <rte_eth_tap.h>
12#include <tap_flow.h>
13#include <tap_autoconf.h>
14#include <tap_tcmsgs.h>
15#include <tap_bpf.h>
16#include <tap_bpf_insns.h>
17
18
19
20
21
22
23
24
25
26
27int tap_flow_bpf_cls_q(__u32 queue_idx)
28{
29 cls_q_insns[1].imm = queue_idx;
30
31 return bpf_load(BPF_PROG_TYPE_SCHED_CLS,
32 (struct bpf_insn *)cls_q_insns,
33 RTE_DIM(cls_q_insns),
34 "Dual BSD/GPL");
35}
36
37
38
39
40
41
42
43
44
45
46
47
48
49int tap_flow_bpf_calc_l3_l4_hash(__u32 key_idx, int map_fd)
50{
51 l3_l4_hash_insns[4].imm = key_idx;
52 l3_l4_hash_insns[9].imm = map_fd;
53
54 return bpf_load(BPF_PROG_TYPE_SCHED_ACT,
55 (struct bpf_insn *)l3_l4_hash_insns,
56 RTE_DIM(l3_l4_hash_insns),
57 "Dual BSD/GPL");
58}
59
60
61
62
63
64
65
66
67
68
69static inline __u64 ptr_to_u64(const void *ptr)
70{
71 return (__u64)(unsigned long)ptr;
72}
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
90 unsigned int size)
91{
92 return syscall(__NR_bpf, cmd, attr, size);
93}
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113static int bpf_load(enum bpf_prog_type type,
114 const struct bpf_insn *insns,
115 size_t insns_cnt,
116 const char *license)
117{
118 union bpf_attr attr = {};
119
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);
126 attr.log_level = 0;
127 attr.kern_version = 0;
128
129 return sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
130}
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147int tap_flow_bpf_rss_map_create(unsigned int key_size,
148 unsigned int value_size,
149 unsigned int max_entries)
150{
151 union bpf_attr attr = {};
152
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;
158
159 return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
160}
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177int tap_flow_bpf_update_rss_elem(int fd, void *key, void *value)
178{
179 union bpf_attr attr = {};
180
181 bzero(&attr, sizeof(attr));
182
183 attr.map_type = BPF_MAP_TYPE_HASH;
184 attr.map_fd = fd;
185 attr.key = ptr_to_u64(key);
186 attr.value = ptr_to_u64(value);
187 attr.flags = BPF_ANY;
188
189 return sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
190}
191