diff options
author | Chema Gonzalez <chema@google.com> | 2014-04-21 12:21:24 -0400 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2014-04-22 21:27:57 -0400 |
commit | 4cd3675ebf74d7f559038ded6aa8088e4099a83d (patch) | |
tree | 6988d7dc3352fbcce10c5c510ffa11e6f30217de /tools/net | |
parent | 5a4ae5f6e7d4b2b5a9b8981d513345053e40b6ac (diff) |
filter: added BPF random opcode
Added a new ancillary load (bpf call in eBPF parlance) that produces
a 32-bit random number. We are implementing it as an ancillary load
(instead of an ISA opcode) because (a) it is simpler, (b) allows easy
JITing, and (c) seems more in line with generic ISAs that do not have
"get a random number" as a instruction, but as an OS call.
The main use for this ancillary load is to perform random packet sampling.
Signed-off-by: Chema Gonzalez <chema@google.com>
Acked-by: Alexei Starovoitov <ast@plumgrid.com>
Acked-by: Daniel Borkmann <dborkman@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'tools/net')
-rw-r--r-- | tools/net/bpf_exp.l | 1 | ||||
-rw-r--r-- | tools/net/bpf_exp.y | 11 |
2 files changed, 11 insertions, 1 deletions
diff --git a/tools/net/bpf_exp.l b/tools/net/bpf_exp.l index bf7be77ddd62..833a96611da6 100644 --- a/tools/net/bpf_exp.l +++ b/tools/net/bpf_exp.l | |||
@@ -92,6 +92,7 @@ extern void yyerror(const char *str); | |||
92 | "#"?("cpu") { return K_CPU; } | 92 | "#"?("cpu") { return K_CPU; } |
93 | "#"?("vlan_tci") { return K_VLANT; } | 93 | "#"?("vlan_tci") { return K_VLANT; } |
94 | "#"?("vlan_pr") { return K_VLANP; } | 94 | "#"?("vlan_pr") { return K_VLANP; } |
95 | "#"?("rand") { return K_RAND; } | ||
95 | 96 | ||
96 | ":" { return ':'; } | 97 | ":" { return ':'; } |
97 | "," { return ','; } | 98 | "," { return ','; } |
diff --git a/tools/net/bpf_exp.y b/tools/net/bpf_exp.y index d15efc989ef5..e6306c51c26f 100644 --- a/tools/net/bpf_exp.y +++ b/tools/net/bpf_exp.y | |||
@@ -56,7 +56,7 @@ static void bpf_set_jmp_label(char *label, enum jmp_type type); | |||
56 | %token OP_LDXI | 56 | %token OP_LDXI |
57 | 57 | ||
58 | %token K_PKT_LEN K_PROTO K_TYPE K_NLATTR K_NLATTR_NEST K_MARK K_QUEUE K_HATYPE | 58 | %token K_PKT_LEN K_PROTO K_TYPE K_NLATTR K_NLATTR_NEST K_MARK K_QUEUE K_HATYPE |
59 | %token K_RXHASH K_CPU K_IFIDX K_VLANT K_VLANP K_POFF | 59 | %token K_RXHASH K_CPU K_IFIDX K_VLANT K_VLANP K_POFF K_RAND |
60 | 60 | ||
61 | %token ':' ',' '[' ']' '(' ')' 'x' 'a' '+' 'M' '*' '&' '#' '%' | 61 | %token ':' ',' '[' ']' '(' ')' 'x' 'a' '+' 'M' '*' '&' '#' '%' |
62 | 62 | ||
@@ -164,6 +164,9 @@ ldb | |||
164 | | OP_LDB K_POFF { | 164 | | OP_LDB K_POFF { |
165 | bpf_set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0, | 165 | bpf_set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0, |
166 | SKF_AD_OFF + SKF_AD_PAY_OFFSET); } | 166 | SKF_AD_OFF + SKF_AD_PAY_OFFSET); } |
167 | | OP_LDB K_RAND { | ||
168 | bpf_set_curr_instr(BPF_LD | BPF_B | BPF_ABS, 0, 0, | ||
169 | SKF_AD_OFF + SKF_AD_RANDOM); } | ||
167 | ; | 170 | ; |
168 | 171 | ||
169 | ldh | 172 | ldh |
@@ -212,6 +215,9 @@ ldh | |||
212 | | OP_LDH K_POFF { | 215 | | OP_LDH K_POFF { |
213 | bpf_set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0, | 216 | bpf_set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0, |
214 | SKF_AD_OFF + SKF_AD_PAY_OFFSET); } | 217 | SKF_AD_OFF + SKF_AD_PAY_OFFSET); } |
218 | | OP_LDH K_RAND { | ||
219 | bpf_set_curr_instr(BPF_LD | BPF_H | BPF_ABS, 0, 0, | ||
220 | SKF_AD_OFF + SKF_AD_RANDOM); } | ||
215 | ; | 221 | ; |
216 | 222 | ||
217 | ldi | 223 | ldi |
@@ -265,6 +271,9 @@ ld | |||
265 | | OP_LD K_POFF { | 271 | | OP_LD K_POFF { |
266 | bpf_set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0, | 272 | bpf_set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0, |
267 | SKF_AD_OFF + SKF_AD_PAY_OFFSET); } | 273 | SKF_AD_OFF + SKF_AD_PAY_OFFSET); } |
274 | | OP_LD K_RAND { | ||
275 | bpf_set_curr_instr(BPF_LD | BPF_W | BPF_ABS, 0, 0, | ||
276 | SKF_AD_OFF + SKF_AD_RANDOM); } | ||
268 | | OP_LD 'M' '[' number ']' { | 277 | | OP_LD 'M' '[' number ']' { |
269 | bpf_set_curr_instr(BPF_LD | BPF_MEM, 0, 0, $4); } | 278 | bpf_set_curr_instr(BPF_LD | BPF_MEM, 0, 0, $4); } |
270 | | OP_LD '[' 'x' '+' number ']' { | 279 | | OP_LD '[' 'x' '+' number ']' { |