2 * Copyright (c) 2020 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #ifndef included_gro_func_h
17 #define included_gro_func_h
19 #include <vnet/ethernet/ethernet.h>
20 #include <vnet/gso/gro.h>
21 #include <vnet/gso/hdr_offset_parser.h>
22 #include <vnet/ip/ip4.h>
23 #include <vnet/ip/ip6.h>
24 #include <vnet/udp/udp_packet.h>
25 #include <vnet/tcp/tcp_packet.h>
26 #include <vnet/vnet.h>
28 static_always_inline u8
29 gro_is_bad_packet (vlib_buffer_t * b, u8 flags, i16 l234_sz)
31 if (((b->current_length - l234_sz) <= 0) ||
32 ((flags &= ~(TCP_FLAG_ACK | TCP_FLAG_PSH)) != 0))
37 static_always_inline void
38 gro_get_ip4_flow_from_packet (u32 * sw_if_index,
39 ip4_header_t * ip4, tcp_header_t * tcp,
40 gro_flow_key_t * flow_key, int is_l2)
42 flow_key->sw_if_index[VLIB_RX] = sw_if_index[VLIB_RX];
43 flow_key->sw_if_index[VLIB_TX] = sw_if_index[VLIB_TX];
44 ip46_address_set_ip4 (&flow_key->src_address, &ip4->src_address);
45 ip46_address_set_ip4 (&flow_key->dst_address, &ip4->dst_address);
46 flow_key->src_port = tcp->src_port;
47 flow_key->dst_port = tcp->dst_port;
50 static_always_inline void
51 gro_get_ip6_flow_from_packet (u32 * sw_if_index,
52 ip6_header_t * ip6, tcp_header_t * tcp,
53 gro_flow_key_t * flow_key, int is_l2)
55 flow_key->sw_if_index[VLIB_RX] = sw_if_index[VLIB_RX];
56 flow_key->sw_if_index[VLIB_TX] = sw_if_index[VLIB_TX];
57 ip46_address_set_ip6 (&flow_key->src_address, &ip6->src_address);
58 ip46_address_set_ip6 (&flow_key->dst_address, &ip6->dst_address);
59 flow_key->src_port = tcp->src_port;
60 flow_key->dst_port = tcp->dst_port;
63 static_always_inline u32
64 gro_is_ip4_or_ip6_packet (vlib_buffer_t *b0, u8 is_l2)
66 if (b0->flags & VNET_BUFFER_F_IS_IP4)
67 return VNET_BUFFER_F_IS_IP4;
68 if (b0->flags & VNET_BUFFER_F_IS_IP6)
69 return VNET_BUFFER_F_IS_IP6;
72 ethernet_header_t *eh =
73 (ethernet_header_t *) vlib_buffer_get_current (b0);
74 u16 ethertype = clib_net_to_host_u16 (eh->type);
76 if (ethernet_frame_is_tagged (ethertype))
78 ethernet_vlan_header_t *vlan = (ethernet_vlan_header_t *) (eh + 1);
80 ethertype = clib_net_to_host_u16 (vlan->type);
81 if (ethertype == ETHERNET_TYPE_VLAN)
84 ethertype = clib_net_to_host_u16 (vlan->type);
87 if (ethertype == ETHERNET_TYPE_IP4)
88 return VNET_BUFFER_F_IS_IP4;
89 if (ethertype == ETHERNET_TYPE_IP6)
90 return VNET_BUFFER_F_IS_IP6;
94 if ((((u8 *) vlib_buffer_get_current (b0))[0] & 0xf0) == 0x40)
95 return VNET_BUFFER_F_IS_IP4;
96 if ((((u8 *) vlib_buffer_get_current (b0))[0] & 0xf0) == 0x60)
97 return VNET_BUFFER_F_IS_IP6;
105 GRO_PACKET_ACTION_NONE = 0,
106 GRO_PACKET_ACTION_ENQUEUE = 1,
107 GRO_PACKET_ACTION_FLUSH = 2,
108 } gro_packet_action_t;
110 static_always_inline gro_packet_action_t
111 gro_tcp_sequence_check (tcp_header_t * tcp0, tcp_header_t * tcp1,
114 u32 next_tcp_seq0 = clib_net_to_host_u32 (tcp0->seq_number);
115 u32 next_tcp_seq1 = clib_net_to_host_u32 (tcp1->seq_number);
117 /* next packet, enqueue */
118 if (PREDICT_TRUE (next_tcp_seq0 + payload_len0 == next_tcp_seq1))
119 return GRO_PACKET_ACTION_ENQUEUE;
120 /* flush all packets */
122 return GRO_PACKET_ACTION_FLUSH;
125 static_always_inline void
126 gro_merge_buffers (vlib_main_t * vm, vlib_buffer_t * b0,
127 vlib_buffer_t * b1, u32 bi1, u32 payload_len1,
130 vlib_buffer_t *pb = b0;
132 if (PREDICT_FALSE ((b0->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
133 b0->total_length_not_including_first_buffer = 0;
135 while (pb->flags & VLIB_BUFFER_NEXT_PRESENT)
136 pb = vlib_get_buffer (vm, pb->next_buffer);
138 vlib_buffer_advance (b1, l234_sz1);
139 pb->flags |= VLIB_BUFFER_NEXT_PRESENT;
140 pb->next_buffer = bi1;
141 b0->total_length_not_including_first_buffer += payload_len1;
142 b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
145 static_always_inline u32
146 gro_validate_checksum (vlib_main_t * vm, vlib_buffer_t * b0,
147 generic_header_offset_t * gho0, int is_ip4)
151 if (b0->flags & VNET_BUFFER_F_OFFLOAD)
152 return VNET_BUFFER_F_L4_CHECKSUM_CORRECT;
153 vlib_buffer_advance (b0, gho0->l3_hdr_offset);
155 flags = ip4_tcp_udp_validate_checksum (vm, b0);
157 flags = ip6_tcp_udp_icmp_validate_checksum (vm, b0);
158 vlib_buffer_advance (b0, -gho0->l3_hdr_offset);
162 static_always_inline u32
163 gro_get_packet_data (vlib_main_t *vm, vlib_buffer_t *b0,
164 generic_header_offset_t *gho0, gro_flow_key_t *flow_key0,
167 ip4_header_t *ip4_0 = 0;
168 ip6_header_t *ip6_0 = 0;
169 tcp_header_t *tcp0 = 0;
173 u32 sw_if_index0[VLIB_N_RX_TX] = { ~0 };
175 u32 is_ip0 = gro_is_ip4_or_ip6_packet (b0, is_l2);
177 if (is_ip0 & VNET_BUFFER_F_IS_IP4)
178 vnet_generic_header_offset_parser (b0, gho0, is_l2, 1 /* is_ip4 */ ,
180 else if (is_ip0 & VNET_BUFFER_F_IS_IP6)
181 vnet_generic_header_offset_parser (b0, gho0, is_l2, 0 /* is_ip4 */ ,
186 if (PREDICT_FALSE ((gho0->gho_flags & GHO_F_TCP) == 0))
190 (ip4_header_t *) (vlib_buffer_get_current (b0) + gho0->l3_hdr_offset);
192 (ip6_header_t *) (vlib_buffer_get_current (b0) + gho0->l3_hdr_offset);
194 (tcp_header_t *) (vlib_buffer_get_current (b0) + gho0->l4_hdr_offset);
196 l234_sz0 = gho0->hdr_sz;
197 if (PREDICT_FALSE (gro_is_bad_packet (b0, tcp0->flags, l234_sz0)))
200 sw_if_index0[VLIB_RX] = vnet_buffer (b0)->sw_if_index[VLIB_RX];
201 sw_if_index0[VLIB_TX] = vnet_buffer (b0)->sw_if_index[VLIB_TX];
203 if (gho0->gho_flags & GHO_F_IP4)
205 flags = gro_validate_checksum (vm, b0, gho0, 1);
206 gro_get_ip4_flow_from_packet (sw_if_index0, ip4_0, tcp0, flow_key0,
209 else if (gho0->gho_flags & GHO_F_IP6)
211 flags = gro_validate_checksum (vm, b0, gho0, 0);
212 gro_get_ip6_flow_from_packet (sw_if_index0, ip6_0, tcp0, flow_key0,
218 if (PREDICT_FALSE ((flags & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) == 0))
221 pkt_len0 = vlib_buffer_length_in_chain (vm, b0);
222 if (PREDICT_FALSE (pkt_len0 >= TCP_MAX_GSO_SZ))
228 static_always_inline u32
229 gro_coalesce_buffers (vlib_main_t *vm, vlib_buffer_t *b0, vlib_buffer_t *b1,
232 generic_header_offset_t gho0 = { 0 };
233 generic_header_offset_t gho1 = { 0 };
234 gro_flow_key_t flow_key0, flow_key1;
235 ip4_header_t *ip4_0, *ip4_1;
236 ip6_header_t *ip6_0, *ip6_1;
237 tcp_header_t *tcp0, *tcp1;
238 u16 l234_sz0, l234_sz1;
239 u32 pkt_len0, pkt_len1, payload_len0, payload_len1;
240 u32 sw_if_index0[VLIB_N_RX_TX] = { ~0 };
241 u32 sw_if_index1[VLIB_N_RX_TX] = { ~0 };
243 u32 is_ip0 = gro_is_ip4_or_ip6_packet (b0, is_l2);
244 u32 is_ip1 = gro_is_ip4_or_ip6_packet (b1, is_l2);
246 if (is_ip0 & VNET_BUFFER_F_IS_IP4)
247 vnet_generic_header_offset_parser (b0, &gho0, is_l2, 1 /* is_ip4 */ ,
249 else if (is_ip0 & VNET_BUFFER_F_IS_IP6)
250 vnet_generic_header_offset_parser (b0, &gho0, is_l2, 0 /* is_ip4 */ ,
255 if (is_ip1 & VNET_BUFFER_F_IS_IP4)
256 vnet_generic_header_offset_parser (b1, &gho1, is_l2, 1 /* is_ip4 */ ,
258 else if (is_ip1 & VNET_BUFFER_F_IS_IP6)
259 vnet_generic_header_offset_parser (b1, &gho1, is_l2, 0 /* is_ip4 */ ,
264 pkt_len0 = vlib_buffer_length_in_chain (vm, b0);
265 pkt_len1 = vlib_buffer_length_in_chain (vm, b1);
267 if (((gho0.gho_flags & GHO_F_TCP) == 0)
268 || ((gho1.gho_flags & GHO_F_TCP) == 0))
272 (ip4_header_t *) (vlib_buffer_get_current (b0) + gho0.l3_hdr_offset);
274 (ip4_header_t *) (vlib_buffer_get_current (b1) + gho1.l3_hdr_offset);
276 (ip6_header_t *) (vlib_buffer_get_current (b0) + gho0.l3_hdr_offset);
278 (ip6_header_t *) (vlib_buffer_get_current (b1) + gho1.l3_hdr_offset);
280 tcp0 = (tcp_header_t *) (vlib_buffer_get_current (b0) + gho0.l4_hdr_offset);
281 tcp1 = (tcp_header_t *) (vlib_buffer_get_current (b1) + gho1.l4_hdr_offset);
283 l234_sz0 = gho0.hdr_sz;
284 l234_sz1 = gho1.hdr_sz;
286 if (gro_is_bad_packet (b0, tcp0->flags, l234_sz0)
287 || gro_is_bad_packet (b1, tcp1->flags, l234_sz1))
290 sw_if_index0[VLIB_RX] = vnet_buffer (b0)->sw_if_index[VLIB_RX];
291 sw_if_index0[VLIB_TX] = vnet_buffer (b0)->sw_if_index[VLIB_TX];
293 sw_if_index1[VLIB_RX] = vnet_buffer (b1)->sw_if_index[VLIB_RX];
294 sw_if_index1[VLIB_TX] = vnet_buffer (b1)->sw_if_index[VLIB_TX];
296 if ((gho0.gho_flags & GHO_F_IP4) && (gho1.gho_flags & GHO_F_IP4))
298 gro_get_ip4_flow_from_packet (sw_if_index0, ip4_0, tcp0, &flow_key0,
300 gro_get_ip4_flow_from_packet (sw_if_index1, ip4_1, tcp1, &flow_key1,
303 else if ((gho0.gho_flags & GHO_F_IP6) && (gho1.gho_flags & GHO_F_IP6))
305 gro_get_ip6_flow_from_packet (sw_if_index0, ip6_0, tcp0, &flow_key0,
307 gro_get_ip6_flow_from_packet (sw_if_index1, ip6_1, tcp1, &flow_key1,
313 if (gro_flow_is_equal (&flow_key0, &flow_key1) == 0)
316 payload_len0 = pkt_len0 - l234_sz0;
317 payload_len1 = pkt_len1 - l234_sz1;
319 if (pkt_len0 >= TCP_MAX_GSO_SZ || pkt_len1 >= TCP_MAX_GSO_SZ
320 || (pkt_len0 + payload_len1) >= TCP_MAX_GSO_SZ)
323 if (gro_tcp_sequence_check (tcp0, tcp1, payload_len0) ==
324 GRO_PACKET_ACTION_ENQUEUE)
326 gro_merge_buffers (vm, b0, b1, bi1, payload_len1, l234_sz1);
327 tcp0->flags |= tcp1->flags;
328 return tcp1->ack_number;
334 static_always_inline void
335 gro_fixup_header (vlib_main_t *vm, vlib_buffer_t *b0, u32 ack_number, u8 is_l2)
337 generic_header_offset_t gho0 = { 0 };
339 u32 is_ip0 = gro_is_ip4_or_ip6_packet (b0, is_l2);
341 if (is_ip0 & VNET_BUFFER_F_IS_IP4)
342 vnet_generic_header_offset_parser (b0, &gho0, is_l2, 1 /* is_ip4 */ ,
344 else if (is_ip0 & VNET_BUFFER_F_IS_IP6)
345 vnet_generic_header_offset_parser (b0, &gho0, is_l2, 0 /* is_ip4 */ ,
348 vnet_buffer2 (b0)->gso_size = b0->current_length - gho0.hdr_sz;
350 if (gho0.gho_flags & GHO_F_IP4)
353 (ip4_header_t *) (vlib_buffer_get_current (b0) + gho0.l3_hdr_offset);
355 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) -
357 b0->flags |= (VNET_BUFFER_F_GSO | VNET_BUFFER_F_IS_IP4);
358 vnet_buffer_offload_flags_set (b0, (VNET_BUFFER_OFFLOAD_F_TCP_CKSUM |
359 VNET_BUFFER_OFFLOAD_F_IP_CKSUM));
361 else if (gho0.gho_flags & GHO_F_IP6)
364 (ip6_header_t *) (vlib_buffer_get_current (b0) + gho0.l3_hdr_offset);
365 ip6->payload_length =
366 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) -
368 b0->flags |= (VNET_BUFFER_F_GSO | VNET_BUFFER_F_IS_IP6);
369 vnet_buffer_offload_flags_set (b0, VNET_BUFFER_OFFLOAD_F_TCP_CKSUM);
373 (tcp_header_t *) (vlib_buffer_get_current (b0) + gho0.l4_hdr_offset);
374 tcp0->ack_number = ack_number;
375 b0->flags &= ~VLIB_BUFFER_IS_TRACED;
378 static_always_inline u32
379 vnet_gro_flow_table_flush (vlib_main_t * vm, gro_flow_table_t * flow_table,
382 if (flow_table->flow_table_size > 0)
384 gro_flow_t *gro_flow;
386 while (i < GRO_FLOW_TABLE_MAX_SIZE)
388 gro_flow = &flow_table->gro_flow[i];
389 if (gro_flow->n_buffers && gro_flow_is_timeout (vm, gro_flow))
393 vlib_get_buffer (vm, gro_flow->buffer_index);
394 gro_fixup_header (vm, b0, gro_flow->last_ack_number,
396 to[j] = gro_flow->buffer_index;
397 gro_flow_table_reset_flow (flow_table, gro_flow);
398 flow_table->n_vectors++;
409 static_always_inline void
410 vnet_gro_flow_table_schedule_node_on_dispatcher (vlib_main_t * vm,
414 if (gro_flow_table_is_timeout (vm, flow_table))
416 u32 to[GRO_FLOW_TABLE_MAX_SIZE] = { 0 };
417 u32 n_to = vnet_gro_flow_table_flush (vm, flow_table, to);
421 u32 node_index = flow_table->node_index;
422 vlib_frame_t *f = vlib_get_frame_to_node (vm, node_index);
423 u32 *f_to = vlib_frame_vector_args (f);
428 f_to[f->n_vectors] = to[i];
432 vlib_put_frame_to_node (vm, node_index, f);
434 gro_flow_table_set_timeout (vm, flow_table, GRO_FLOW_TABLE_FLUSH);
438 static_always_inline u32
439 vnet_gro_flush_all_packets (vlib_main_t *vm, gro_flow_table_t *flow_table,
440 gro_flow_t *gro_flow, vlib_buffer_t *b_s, u32 *to,
441 u32 bi_s, u32 bi0, u8 is_l2)
443 flow_table->n_vectors++;
444 flow_table->total_vectors++;
445 gro_fixup_header (vm, b_s, gro_flow->last_ack_number, is_l2);
446 gro_flow->n_buffers = 0;
447 gro_flow_table_reset_flow (flow_table, gro_flow);
453 static_always_inline u32
454 vnet_gro_flow_table_inline (vlib_main_t * vm, gro_flow_table_t * flow_table,
457 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
458 generic_header_offset_t gho0 = { 0 };
459 gro_flow_t *gro_flow = 0;
460 gro_flow_key_t flow_key0 = { };
461 tcp_header_t *tcp0 = 0;
464 u8 is_l2 = flow_table->is_l2;
466 if (!gro_flow_table_is_enable (flow_table))
472 if (PREDICT_FALSE (b0->flags & VNET_BUFFER_F_GSO))
478 pkt_len0 = gro_get_packet_data (vm, b0, &gho0, &flow_key0, is_l2);
485 tcp0 = (tcp_header_t *) (vlib_buffer_get_current (b0) + gho0.l4_hdr_offset);
486 if (PREDICT_TRUE ((tcp0->flags & TCP_FLAG_PSH) == 0))
487 gro_flow = gro_flow_table_find_or_add_flow (flow_table, &flow_key0);
491 gro_flow = gro_flow_table_get_flow (flow_table, &flow_key0);
500 if (PREDICT_FALSE (gro_flow->n_buffers == 0))
502 flow_table->total_vectors++;
503 gro_flow_store_packet (gro_flow, bi0);
504 gro_flow->last_ack_number = tcp0->ack_number;
505 gro_flow_set_timeout (vm, gro_flow, GRO_FLOW_TIMEOUT);
510 generic_header_offset_t gho_s = { 0 };
512 u16 l234_sz0, l234_sz_s;
513 u32 pkt_len_s, payload_len0, payload_len_s;
514 u32 bi_s = gro_flow->buffer_index;
516 vlib_buffer_t *b_s = vlib_get_buffer (vm, bi_s);
517 u32 is_ip_s = gro_is_ip4_or_ip6_packet (b_s, is_l2);
518 if (is_ip_s & VNET_BUFFER_F_IS_IP4)
519 vnet_generic_header_offset_parser (b_s, &gho_s, is_l2,
520 1 /* is_ip4 */ , 0 /* is_ip6 */ );
521 else if (is_ip_s & VNET_BUFFER_F_IS_IP6)
522 vnet_generic_header_offset_parser (b_s, &gho_s, is_l2,
523 0 /* is_ip4 */ , 1 /* is_ip6 */ );
526 (tcp_header_t *) (vlib_buffer_get_current (b_s) +
527 gho_s.l4_hdr_offset);
528 pkt_len_s = vlib_buffer_length_in_chain (vm, b_s);
529 l234_sz0 = gho0.hdr_sz;
530 l234_sz_s = gho_s.hdr_sz;
531 payload_len0 = pkt_len0 - l234_sz0;
532 payload_len_s = pkt_len_s - l234_sz_s;
533 gro_packet_action_t action =
534 gro_tcp_sequence_check (tcp_s, tcp0, payload_len_s);
536 if (PREDICT_TRUE (action == GRO_PACKET_ACTION_ENQUEUE))
538 if (PREDICT_TRUE (((pkt_len_s + payload_len0) < TCP_MAX_GSO_SZ) &&
539 (gro_flow->n_buffers < GRO_FLOW_N_BUFFERS)))
541 flow_table->total_vectors++;
542 gro_merge_buffers (vm, b_s, b0, bi0, payload_len0, l234_sz0);
543 gro_flow_store_packet (gro_flow, bi0);
544 gro_flow->last_ack_number = tcp0->ack_number;
545 if (PREDICT_FALSE (is_flush))
547 flow_table->n_vectors++;
548 tcp_s->flags |= tcp0->flags;
549 gro_fixup_header (vm, b_s, gro_flow->last_ack_number, is_l2);
550 gro_flow->n_buffers = 0;
551 gro_flow_table_reset_flow (flow_table, gro_flow);
557 else if (PREDICT_FALSE (is_flush))
558 // flush the all (current and stored) packets
559 return vnet_gro_flush_all_packets (vm, flow_table, gro_flow, b_s,
560 to, bi_s, bi0, is_l2);
563 // flush the stored GSO size packet and buffer the current packet
564 flow_table->n_vectors++;
565 flow_table->total_vectors++;
566 gro_fixup_header (vm, b_s, gro_flow->last_ack_number, is_l2);
567 gro_flow->n_buffers = 0;
568 gro_flow_store_packet (gro_flow, bi0);
569 gro_flow->last_ack_number = tcp0->ack_number;
570 gro_flow_set_timeout (vm, gro_flow, GRO_FLOW_TIMEOUT);
577 // flush the all (current and stored) packets
578 return vnet_gro_flush_all_packets (vm, flow_table, gro_flow, b_s, to,
585 * coalesce buffers with flow tables
587 static_always_inline u32
588 vnet_gro_inline (vlib_main_t * vm, gro_flow_table_t * flow_table, u32 * from,
589 u16 n_left_from, u32 * to)
591 u16 count = 0, i = 0;
593 for (i = 0; i < n_left_from; i++)
594 count += vnet_gro_flow_table_inline (vm, flow_table, from[i], &to[count]);
600 * coalesce buffers in opportunistic way without flow tables
602 static_always_inline u32
603 vnet_gro_simple_inline (vlib_main_t * vm, u32 * from, u16 n_left_from,
606 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
607 vlib_get_buffers (vm, from, b, n_left_from);
608 u32 bi = 1, ack_number = 0;
609 if (PREDICT_TRUE (((b[0]->flags & VNET_BUFFER_F_GSO) == 0)))
611 while (n_left_from > 1)
613 if (PREDICT_TRUE (((b[bi]->flags & VNET_BUFFER_F_GSO) == 0)))
617 gro_coalesce_buffers (vm, b[0], b[bi], from[bi],
634 gro_fixup_header (vm, b[0], ack_number, is_l2);
639 #endif /* included_gro_func_h */
642 * fd.io coding-style-patch-verification: ON
645 * eval: (c-set-style "gnu")