Skip to content

Commit 2559282

Browse files
committed
Generate cleaner code for "host", "net" and "gateway".
Following the same logic as for DECnet in commit 6f8a28c earlier, take the link-layer protocol value out of any recursion gen_hostop() and gen_hostop6() might do, instead just call gen_linktype() beforehand exactly once. In gen_host6() join two equivalent cases together for clarity. For ARP-capable DLTs this improves SNR in the unoptimized filter programs of: * "arp (host|net)" * "ip (host|net)" * "ip6 (host|net)" * "rarp (host|net)" * "(host|net)" (which is a logical OR of the above four) * "arp gateway" (which is a superset of "not arp host") * "ip gateway" (which is a superset of "not ip host") * "rarp gateway" (which is a superset of "not rarp host") * "gateway" (which is a logical OR of the above three) For DLTs with a relatively simple structure this change makes the unoptimized filter program for most of the above primitives the same as the optimized one. For IEEE 802.11 and ARCnet there is now fewer instructions and less work to do for the optimizer.
1 parent d68b805 commit 2559282

File tree

2 files changed

+291
-387
lines changed

2 files changed

+291
-387
lines changed

gencode.c

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -659,10 +659,10 @@ static struct block *gen_linktype(compiler_state_t *, bpf_u_int32);
659659
static struct block *gen_snap(compiler_state_t *, bpf_u_int32, bpf_u_int32);
660660
static struct block *gen_llc_linktype(compiler_state_t *, bpf_u_int32);
661661
static struct block *gen_hostop(compiler_state_t *, bpf_u_int32, bpf_u_int32,
662-
int, bpf_u_int32, u_int, u_int);
662+
int, u_int, u_int);
663663
#ifdef INET6
664664
static struct block *gen_hostop6(compiler_state_t *, struct in6_addr *,
665-
struct in6_addr *, int, bpf_u_int32, u_int, u_int);
665+
struct in6_addr *, int, u_int, u_int);
666666
#endif
667667
static struct block *gen_ahostop(compiler_state_t *, const uint8_t, int);
668668
static struct block *gen_ehostop(compiler_state_t *, const u_char *, int);
@@ -4174,7 +4174,7 @@ gen_llc_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto)
41744174

41754175
static struct block *
41764176
gen_hostop(compiler_state_t *cstate, bpf_u_int32 addr, bpf_u_int32 mask,
4177-
int dir, bpf_u_int32 ll_proto, u_int src_off, u_int dst_off)
4177+
int dir, u_int src_off, u_int dst_off)
41784178
{
41794179
struct block *b0, *b1;
41804180
u_int offset;
@@ -4190,15 +4190,15 @@ gen_hostop(compiler_state_t *cstate, bpf_u_int32 addr, bpf_u_int32 mask,
41904190
break;
41914191

41924192
case Q_AND:
4193-
b0 = gen_hostop(cstate, addr, mask, Q_SRC, ll_proto, src_off, dst_off);
4194-
b1 = gen_hostop(cstate, addr, mask, Q_DST, ll_proto, src_off, dst_off);
4193+
b0 = gen_hostop(cstate, addr, mask, Q_SRC, src_off, dst_off);
4194+
b1 = gen_hostop(cstate, addr, mask, Q_DST, src_off, dst_off);
41954195
gen_and(b0, b1);
41964196
return b1;
41974197

41984198
case Q_DEFAULT:
41994199
case Q_OR:
4200-
b0 = gen_hostop(cstate, addr, mask, Q_SRC, ll_proto, src_off, dst_off);
4201-
b1 = gen_hostop(cstate, addr, mask, Q_DST, ll_proto, src_off, dst_off);
4200+
b0 = gen_hostop(cstate, addr, mask, Q_SRC, src_off, dst_off);
4201+
b1 = gen_hostop(cstate, addr, mask, Q_DST, src_off, dst_off);
42024202
gen_or(b0, b1);
42034203
return b1;
42044204

@@ -4230,17 +4230,13 @@ gen_hostop(compiler_state_t *cstate, bpf_u_int32 addr, bpf_u_int32 mask,
42304230
abort();
42314231
/*NOTREACHED*/
42324232
}
4233-
b0 = gen_linktype(cstate, ll_proto);
4234-
b1 = gen_mcmp(cstate, OR_LINKPL, offset, BPF_W, addr, mask);
4235-
gen_and(b0, b1);
4236-
return b1;
4233+
return gen_mcmp(cstate, OR_LINKPL, offset, BPF_W, addr, mask);
42374234
}
42384235

42394236
#ifdef INET6
42404237
static struct block *
42414238
gen_hostop6(compiler_state_t *cstate, struct in6_addr *addr,
4242-
struct in6_addr *mask, int dir, bpf_u_int32 ll_proto, u_int src_off,
4243-
u_int dst_off)
4239+
struct in6_addr *mask, int dir, u_int src_off, u_int dst_off)
42444240
{
42454241
struct block *b0, *b1;
42464242
u_int offset;
@@ -4266,15 +4262,15 @@ gen_hostop6(compiler_state_t *cstate, struct in6_addr *addr,
42664262
break;
42674263

42684264
case Q_AND:
4269-
b0 = gen_hostop6(cstate, addr, mask, Q_SRC, ll_proto, src_off, dst_off);
4270-
b1 = gen_hostop6(cstate, addr, mask, Q_DST, ll_proto, src_off, dst_off);
4265+
b0 = gen_hostop6(cstate, addr, mask, Q_SRC, src_off, dst_off);
4266+
b1 = gen_hostop6(cstate, addr, mask, Q_DST, src_off, dst_off);
42714267
gen_and(b0, b1);
42724268
return b1;
42734269

42744270
case Q_DEFAULT:
42754271
case Q_OR:
4276-
b0 = gen_hostop6(cstate, addr, mask, Q_SRC, ll_proto, src_off, dst_off);
4277-
b1 = gen_hostop6(cstate, addr, mask, Q_DST, ll_proto, src_off, dst_off);
4272+
b0 = gen_hostop6(cstate, addr, mask, Q_SRC, src_off, dst_off);
4273+
b1 = gen_hostop6(cstate, addr, mask, Q_DST, src_off, dst_off);
42784274
gen_or(b0, b1);
42794275
return b1;
42804276

@@ -4316,8 +4312,6 @@ gen_hostop6(compiler_state_t *cstate, struct in6_addr *addr,
43164312
gen_and(b0, b1);
43174313
b0 = gen_mcmp(cstate, OR_LINKPL, offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0]));
43184314
gen_and(b0, b1);
4319-
b0 = gen_linktype(cstate, ll_proto);
4320-
gen_and(b0, b1);
43214315
return b1;
43224316
}
43234317
#endif
@@ -5182,13 +5176,22 @@ gen_host(compiler_state_t *cstate, bpf_u_int32 addr, bpf_u_int32 mask,
51825176
bpf_error(cstate, "link-layer modifier applied to %s", typestr);
51835177

51845178
case Q_IP:
5185-
return gen_hostop(cstate, addr, mask, dir, ETHERTYPE_IP, 12, 16);
5179+
b0 = gen_linktype(cstate, ETHERTYPE_IP);
5180+
b1 = gen_hostop(cstate, addr, mask, dir, 12, 16);
5181+
gen_and(b0, b1);
5182+
return b1;
51865183

51875184
case Q_RARP:
5188-
return gen_hostop(cstate, addr, mask, dir, ETHERTYPE_REVARP, 14, 24);
5185+
b0 = gen_linktype(cstate, ETHERTYPE_REVARP);
5186+
b1 = gen_hostop(cstate, addr, mask, dir, 14, 24);
5187+
gen_and(b0, b1);
5188+
return b1;
51895189

51905190
case Q_ARP:
5191-
return gen_hostop(cstate, addr, mask, dir, ETHERTYPE_ARP, 14, 24);
5191+
b0 = gen_linktype(cstate, ETHERTYPE_ARP);
5192+
b1 = gen_hostop(cstate, addr, mask, dir, 14, 24);
5193+
gen_and(b0, b1);
5194+
return b1;
51925195

51935196
case Q_SCTP:
51945197
bpf_error(cstate, "'sctp' modifier applied to %s", typestr);
@@ -5309,6 +5312,7 @@ static struct block *
53095312
gen_host6(compiler_state_t *cstate, struct in6_addr *addr,
53105313
struct in6_addr *mask, int proto, int dir, int type)
53115314
{
5315+
struct block *b0, *b1;
53125316
const char *typestr;
53135317

53145318
if (type == Q_NET)
@@ -5319,7 +5323,11 @@ gen_host6(compiler_state_t *cstate, struct in6_addr *addr,
53195323
switch (proto) {
53205324

53215325
case Q_DEFAULT:
5322-
return gen_host6(cstate, addr, mask, Q_IPV6, dir, type);
5326+
case Q_IPV6:
5327+
b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
5328+
b1 = gen_hostop6(cstate, addr, mask, dir, 8, 24);
5329+
gen_and(b0, b1);
5330+
return b1;
53235331

53245332
case Q_LINK:
53255333
bpf_error(cstate, "link-layer modifier applied to ip6 %s", typestr);
@@ -5369,9 +5377,6 @@ gen_host6(compiler_state_t *cstate, struct in6_addr *addr,
53695377
case Q_MOPDL:
53705378
bpf_error(cstate, "'mopdl' modifier applied to ip6 %s", typestr);
53715379

5372-
case Q_IPV6:
5373-
return gen_hostop6(cstate, addr, mask, dir, ETHERTYPE_IPV6, 8, 24);
5374-
53755380
case Q_ICMPV6:
53765381
bpf_error(cstate, "'icmp6' modifier applied to ip6 %s", typestr);
53775382

0 commit comments

Comments
 (0)