@@ -130,4 +130,97 @@ int xdp_tx_func(struct xdp_md *ctx)
130130 return xdp_stats_record_action (ctx , XDP_TX );
131131}
132132
133+ struct my_timestamp {
134+ __u16 magic ;
135+ __u64 time ;
136+ } __attribute__((packed ));
137+
138+ SEC ("xdp_tailgrow_use" )
139+ int xdp_tailgrow3 (struct xdp_md * ctx )
140+ {
141+ void * data_end = (void * )(long )ctx -> data_end ;
142+ void * data = (void * )(long )ctx -> data ;
143+ //struct hdr_cursor nh;
144+ int offset ;
145+ struct my_timestamp * ts ;
146+
147+ offset = 8 ;
148+ // if (data + offset > data_end)
149+ // return XDP_ABORTED;
150+
151+ bpf_xdp_adjust_tail (ctx , offset );
152+ data_end = (void * )(long )ctx -> data_end ;
153+ data = (void * )(long )ctx -> data ;
154+
155+ if (data + offset > data_end )
156+ return XDP_ABORTED ;
157+ // if (data + 2048 > data_end)
158+ // return XDP_ABORTED;
159+
160+ ts = data ;
161+ // ts->time = 42;
162+
163+ return xdp_stats_record_action (ctx , XDP_PASS );
164+ }
165+
166+ SEC ("xdp_tailgrow_parse" )
167+ int grow_parse (struct xdp_md * ctx )
168+ {
169+ void * data_end = (void * )(long )ctx -> data_end ;
170+ void * data = (void * )(long )ctx -> data ;
171+
172+ int action = XDP_PASS ;
173+ int eth_type , ip_type ;
174+ struct ethhdr * eth ;
175+ struct iphdr * iphdr ;
176+ struct hdr_cursor nh ;
177+
178+ __u16 ip_tot_len ;
179+
180+ struct my_timestamp * ts ;
181+
182+ int offset = sizeof (* ts );
183+ bpf_xdp_adjust_tail (ctx , offset );
184+ data_end = (void * )(long )ctx -> data_end ;
185+ data = (void * )(long )ctx -> data ;
186+
187+ /* These keep track of the next header type and iterator pointer */
188+ nh .pos = data ;
189+
190+ eth_type = parse_ethhdr (& nh , data_end , & eth );
191+ if (eth_type < 0 ) {
192+ action = XDP_ABORTED ;
193+ goto out ;
194+ }
195+
196+ if (eth_type == bpf_htons (ETH_P_IP )) {
197+ ip_type = parse_iphdr (& nh , data_end , & iphdr );
198+ } else {
199+ action = XDP_PASS ;
200+ goto out ;
201+ }
202+
203+ if (ip_type == IPPROTO_ICMP ) {
204+
205+ /* Packet size in bytes, including IP header and data */
206+ ip_tot_len = bpf_ntohs (iphdr -> tot_len );
207+
208+ // ip_tot_len &= 0xFFF; /* Max 4095 - not allowed by verifier*/
209+ ip_tot_len &= 0xFF ; /* Max 255 - allowed by verifier */
210+
211+ /* Finding end of packet + offset */
212+ if ((void * )iphdr + ip_tot_len + offset > data_end ) {
213+ action = XDP_ABORTED ;
214+ goto out ;
215+ }
216+ ts = (void * )iphdr + ip_tot_len ;
217+ //ts = nh.pos + ip_tot_len - sizeof(*ts);
218+ //ts = nh.pos + ip_tot_len;
219+ ts -> magic = 0x5354 ; // String "TS" in network-byte-order
220+ ts -> time = bpf_ktime_get_ns ();
221+ }
222+ out :
223+ return xdp_stats_record_action (ctx , action );
224+ }
225+
133226char _license [] SEC ("license" ) = "GPL" ;
0 commit comments