Skip to content

Commit 55da667

Browse files
committed
samples/electrode: only access needed fields from packets
Signed-off-by: Jinghao Jia <[email protected]>
1 parent 935a88b commit 55da667

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

samples/electrode/src/main.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
extern crate rex;
66

7-
use core::mem::{size_of, swap};
7+
use core::mem::{offset_of, size_of, swap};
88

99
use rex::sched_cls::*;
1010
use rex::utils::*;
@@ -37,21 +37,20 @@ fn fast_paxos_main(obj: &xdp, ctx: &mut xdp_md) -> Result {
3737
let iphdr_end = iphdr_start + size_of::<iphdr>();
3838
let udphdr_end = iphdr_end + size_of::<udphdr>();
3939

40-
let ip_header = convert_slice_to_struct::<iphdr>(
41-
&ctx.data_slice[iphdr_start..iphdr_end],
40+
let protocol_start = iphdr_start + offset_of!(iphdr, protocol);
41+
let protocol = convert_slice_to_struct::<u8>(
42+
&ctx.data_slice[protocol_start..protocol_start + size_of::<u8>()],
4243
);
4344

44-
match u8::from_be(ip_header.protocol) as u32 {
45+
match u8::from_be(*protocol) as u32 {
4546
IPPROTO_TCP => {
4647
// NOTE: currently we only take care of UDP memcached
4748
}
4849
IPPROTO_UDP => {
49-
let port = u16::from_be(
50-
convert_slice_to_struct::<udphdr>(
51-
&ctx.data_slice[iphdr_end..udphdr_end],
52-
)
53-
.dest,
54-
);
50+
let port_start = iphdr_end + offset_of!(udphdr, dest);
51+
let port = u16::from_be(*convert_slice_to_struct::<u16>(
52+
&ctx.data_slice[port_start..port_start + size_of::<u16>()],
53+
));
5554
let payload = &mut ctx.data_slice[header_len..];
5655

5756
// port check, our process bound to 12345.
@@ -84,25 +83,23 @@ fn fast_broad_cast_main(obj: &sched_cls, skb: &mut __sk_buff) -> Result {
8483
size_of::<iphdr>() + size_of::<ethhdr>() + size_of::<udphdr>();
8584
let iphdr_start = size_of::<ethhdr>();
8685
let iphdr_end = iphdr_start + size_of::<iphdr>();
87-
let udphdr_end = iphdr_end + size_of::<udphdr>();
8886

8987
// check if the packet is long enough
9088
if skb.data_slice.len() <= header_len {
9189
return Ok(TC_ACT_OK as i32);
9290
}
9391

94-
let ip_header = convert_slice_to_struct::<iphdr>(
95-
&skb.data_slice[iphdr_start..iphdr_end],
92+
let protocol_start = iphdr_start + offset_of!(iphdr, protocol);
93+
let protocol = convert_slice_to_struct::<u8>(
94+
&skb.data_slice[protocol_start..protocol_start + size_of::<u8>()],
9695
);
97-
match u8::from_be(ip_header.protocol) as u32 {
96+
match u8::from_be(*protocol) as u32 {
9897
IPPROTO_UDP => {
9998
// only port 12345 is allowed
100-
let port = u16::from_be(
101-
convert_slice_to_struct::<udphdr>(
102-
&skb.data_slice[iphdr_end..udphdr_end],
103-
)
104-
.dest,
105-
);
99+
let port_start = iphdr_end + offset_of!(udphdr, dest);
100+
let port = u16::from_be(*convert_slice_to_struct::<u16>(
101+
&skb.data_slice[port_start..port_start + size_of::<u16>()],
102+
));
106103
if port != 12345 {
107104
return Ok(TC_ACT_OK as i32);
108105
}

0 commit comments

Comments
 (0)