net/mlx5e: Add UDP GSO remaining counter
[sfrench/cifs-2.6.git] / drivers / net / ethernet / mellanox / mlx5 / core / en_accel / rxtx.c
1 #include "en_accel/rxtx.h"
2
3 static void mlx5e_udp_gso_prepare_last_skb(struct sk_buff *skb,
4                                            struct sk_buff *nskb,
5                                            int remaining)
6 {
7         int bytes_needed = remaining, remaining_headlen, remaining_page_offset;
8         int headlen = skb_transport_offset(skb) + sizeof(struct udphdr);
9         int payload_len = remaining + sizeof(struct udphdr);
10         int k = 0, i, j;
11
12         skb_copy_bits(skb, 0, nskb->data, headlen);
13         nskb->dev = skb->dev;
14         skb_reset_mac_header(nskb);
15         skb_set_network_header(nskb, skb_network_offset(skb));
16         skb_set_transport_header(nskb, skb_transport_offset(skb));
17         skb_set_tail_pointer(nskb, headlen);
18
19         /* How many frags do we need? */
20         for (i = skb_shinfo(skb)->nr_frags - 1; i >= 0; i--) {
21                 bytes_needed -= skb_frag_size(&skb_shinfo(skb)->frags[i]);
22                 k++;
23                 if (bytes_needed <= 0)
24                         break;
25         }
26
27         /* Fill the first frag and split it if necessary */
28         j = skb_shinfo(skb)->nr_frags - k;
29         remaining_page_offset = -bytes_needed;
30         skb_fill_page_desc(nskb, 0,
31                            skb_shinfo(skb)->frags[j].page.p,
32                            skb_shinfo(skb)->frags[j].page_offset + remaining_page_offset,
33                            skb_shinfo(skb)->frags[j].size - remaining_page_offset);
34
35         skb_frag_ref(skb, j);
36
37         /* Fill the rest of the frags */
38         for (i = 1; i < k; i++) {
39                 j = skb_shinfo(skb)->nr_frags - k + i;
40
41                 skb_fill_page_desc(nskb, i,
42                                    skb_shinfo(skb)->frags[j].page.p,
43                                    skb_shinfo(skb)->frags[j].page_offset,
44                                    skb_shinfo(skb)->frags[j].size);
45                 skb_frag_ref(skb, j);
46         }
47         skb_shinfo(nskb)->nr_frags = k;
48
49         remaining_headlen = remaining - skb->data_len;
50
51         /* headlen contains remaining data? */
52         if (remaining_headlen > 0)
53                 skb_copy_bits(skb, skb->len - remaining, nskb->data + headlen,
54                               remaining_headlen);
55         nskb->len = remaining + headlen;
56         nskb->data_len =  payload_len - sizeof(struct udphdr) +
57                 max_t(int, 0, remaining_headlen);
58         nskb->protocol = skb->protocol;
59         if (nskb->protocol == htons(ETH_P_IP)) {
60                 ip_hdr(nskb)->id = htons(ntohs(ip_hdr(nskb)->id) +
61                                          skb_shinfo(skb)->gso_segs);
62                 ip_hdr(nskb)->tot_len =
63                         htons(payload_len + sizeof(struct iphdr));
64         } else {
65                 ipv6_hdr(nskb)->payload_len = htons(payload_len);
66         }
67         udp_hdr(nskb)->len = htons(payload_len);
68         skb_shinfo(nskb)->gso_size = 0;
69         nskb->ip_summed = skb->ip_summed;
70         nskb->csum_start = skb->csum_start;
71         nskb->csum_offset = skb->csum_offset;
72         nskb->queue_mapping = skb->queue_mapping;
73 }
74
75 /* might send skbs and update wqe and pi */
76 struct sk_buff *mlx5e_udp_gso_handle_tx_skb(struct net_device *netdev,
77                                             struct mlx5e_txqsq *sq,
78                                             struct sk_buff *skb,
79                                             struct mlx5e_tx_wqe **wqe,
80                                             u16 *pi)
81 {
82         int payload_len = skb_shinfo(skb)->gso_size + sizeof(struct udphdr);
83         int headlen = skb_transport_offset(skb) + sizeof(struct udphdr);
84         int remaining = (skb->len - headlen) % skb_shinfo(skb)->gso_size;
85         struct sk_buff *nskb;
86
87         if (skb->protocol == htons(ETH_P_IP))
88                 ip_hdr(skb)->tot_len = htons(payload_len + sizeof(struct iphdr));
89         else
90                 ipv6_hdr(skb)->payload_len = htons(payload_len);
91         udp_hdr(skb)->len = htons(payload_len);
92         if (!remaining)
93                 return skb;
94
95         sq->stats->udp_seg_rem++;
96         nskb = alloc_skb(max_t(int, headlen, headlen + remaining - skb->data_len), GFP_ATOMIC);
97         if (unlikely(!nskb)) {
98                 sq->stats->dropped++;
99                 return NULL;
100         }
101
102         mlx5e_udp_gso_prepare_last_skb(skb, nskb, remaining);
103
104         skb_shinfo(skb)->gso_segs--;
105         pskb_trim(skb, skb->len - remaining);
106         mlx5e_sq_xmit(sq, skb, *wqe, *pi);
107         mlx5e_sq_fetch_wqe(sq, wqe, pi);
108         return nskb;
109 }