|
4 | 4 |
|
5 | 5 | extern crate rex; |
6 | 6 |
|
7 | | -use core::mem::{size_of, swap}; |
| 7 | +use core::mem::{offset_of, size_of, swap}; |
8 | 8 |
|
9 | 9 | use rex::sched_cls::*; |
10 | 10 | use rex::utils::*; |
@@ -37,21 +37,20 @@ fn fast_paxos_main(obj: &xdp, ctx: &mut xdp_md) -> Result { |
37 | 37 | let iphdr_end = iphdr_start + size_of::<iphdr>(); |
38 | 38 | let udphdr_end = iphdr_end + size_of::<udphdr>(); |
39 | 39 |
|
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>()], |
42 | 43 | ); |
43 | 44 |
|
44 | | - match u8::from_be(ip_header.protocol) as u32 { |
| 45 | + match u8::from_be(*protocol) as u32 { |
45 | 46 | IPPROTO_TCP => { |
46 | 47 | // NOTE: currently we only take care of UDP memcached |
47 | 48 | } |
48 | 49 | 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 | + )); |
55 | 54 | let payload = &mut ctx.data_slice[header_len..]; |
56 | 55 |
|
57 | 56 | // port check, our process bound to 12345. |
@@ -84,25 +83,23 @@ fn fast_broad_cast_main(obj: &sched_cls, skb: &mut __sk_buff) -> Result { |
84 | 83 | size_of::<iphdr>() + size_of::<ethhdr>() + size_of::<udphdr>(); |
85 | 84 | let iphdr_start = size_of::<ethhdr>(); |
86 | 85 | let iphdr_end = iphdr_start + size_of::<iphdr>(); |
87 | | - let udphdr_end = iphdr_end + size_of::<udphdr>(); |
88 | 86 |
|
89 | 87 | // check if the packet is long enough |
90 | 88 | if skb.data_slice.len() <= header_len { |
91 | 89 | return Ok(TC_ACT_OK as i32); |
92 | 90 | } |
93 | 91 |
|
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>()], |
96 | 95 | ); |
97 | | - match u8::from_be(ip_header.protocol) as u32 { |
| 96 | + match u8::from_be(*protocol) as u32 { |
98 | 97 | IPPROTO_UDP => { |
99 | 98 | // 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 | + )); |
106 | 103 | if port != 12345 { |
107 | 104 | return Ok(TC_ACT_OK as i32); |
108 | 105 | } |
|
0 commit comments