1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2013-2018, 2021, The Linux Foundation. All rights reserved.
3 *
4 * RMNET Data MAP protocol
5 */
6
7 #include <linux/netdevice.h>
8 #include <linux/ip.h>
9 #include <linux/ipv6.h>
10 #include <net/ip6_checksum.h>
11 #include <linux/bitfield.h>
12 #include "rmnet_config.h"
13 #include "rmnet_map.h"
14 #include "rmnet_private.h"
15 #include "rmnet_vnd.h"
16
17 #define RMNET_MAP_DEAGGR_SPACING 64
18 #define RMNET_MAP_DEAGGR_HEADROOM (RMNET_MAP_DEAGGR_SPACING / 2)
19
rmnet_map_get_csum_field(unsigned char protocol,const void * txporthdr)20 static __sum16 *rmnet_map_get_csum_field(unsigned char protocol,
21 const void *txporthdr)
22 {
23 if (protocol == IPPROTO_TCP)
24 return &((struct tcphdr *)txporthdr)->check;
25
26 if (protocol == IPPROTO_UDP)
27 return &((struct udphdr *)txporthdr)->check;
28
29 return NULL;
30 }
31
32 static int
rmnet_map_ipv4_dl_csum_trailer(struct sk_buff * skb,struct rmnet_map_dl_csum_trailer * csum_trailer,struct rmnet_priv * priv)33 rmnet_map_ipv4_dl_csum_trailer(struct sk_buff *skb,
34 struct rmnet_map_dl_csum_trailer *csum_trailer,
35 struct rmnet_priv *priv)
36 {
37 struct iphdr *ip4h = (struct iphdr *)skb->data;
38 void *txporthdr = skb->data + ip4h->ihl * 4;
39 __sum16 *csum_field, pseudo_csum;
40 __sum16 ip_payload_csum;
41
42 /* Computing the checksum over just the IPv4 header--including its
43 * checksum field--should yield 0. If it doesn't, the IP header
44 * is bad, so return an error and let the IP layer drop it.
45 */
46 if (ip_fast_csum(ip4h, ip4h->ihl)) {
47 priv->stats.csum_ip4_header_bad++;
48 return -EINVAL;
49 }
50
51 /* We don't support checksum offload on IPv4 fragments */
52 if (ip_is_fragment(ip4h)) {
53 priv->stats.csum_fragmented_pkt++;
54 return -EOPNOTSUPP;
55 }
56
57 /* Checksum offload is only supported for UDP and TCP protocols */
58 csum_field = rmnet_map_get_csum_field(ip4h->protocol, txporthdr);
59 if (!csum_field) {
60 priv->stats.csum_err_invalid_transport++;
61 return -EPROTONOSUPPORT;
62 }
63
64 /* RFC 768: UDP checksum is optional for IPv4, and is 0 if unused */
65 if (!*csum_field && ip4h->protocol == IPPROTO_UDP) {
66 priv->stats.csum_skipped++;
67 return 0;
68 }
69
70 /* The checksum value in the trailer is computed over the entire
71 * IP packet, including the IP header and payload. To derive the
72 * transport checksum from this, we first subract the contribution
73 * of the IP header from the trailer checksum. We then add the
74 * checksum computed over the pseudo header.
75 *
76 * We verified above that the IP header contributes zero to the
77 * trailer checksum. Therefore the checksum in the trailer is
78 * just the checksum computed over the IP payload.
79
80 * If the IP payload arrives intact, adding the pseudo header
81 * checksum to the IP payload checksum will yield 0xffff (negative
82 * zero). This means the trailer checksum and the pseudo checksum
83 * are additive inverses of each other. Put another way, the
84 * message passes the checksum test if the trailer checksum value
85 * is the negated pseudo header checksum.
86 *
87 * Knowing this, we don't even need to examine the transport
88 * header checksum value; it is already accounted for in the
89 * checksum value found in the trailer.
90 */
91 ip_payload_csum = csum_trailer->csum_value;
92
93 pseudo_csum = csum_tcpudp_magic(ip4h->saddr, ip4h->daddr,
94 ntohs(ip4h->tot_len) - ip4h->ihl * 4,
95 ip4h->protocol, 0);
96
97 /* The cast is required to ensure only the low 16 bits are examined */
98 if (ip_payload_csum != (__sum16)~pseudo_csum) {
99 priv->stats.csum_validation_failed++;
100 return -EINVAL;
101 }
102
103 priv->stats.csum_ok++;
104 return 0;
105 }
106
107 #if IS_ENABLED(CONFIG_IPV6)
108 static int
rmnet_map_ipv6_dl_csum_trailer(struct sk_buff * skb,struct rmnet_map_dl_csum_trailer * csum_trailer,struct rmnet_priv * priv)109 rmnet_map_ipv6_dl_csum_trailer(struct sk_buff *skb,
110 struct rmnet_map_dl_csum_trailer *csum_trailer,
111 struct rmnet_priv *priv)
112 {
113 struct ipv6hdr *ip6h = (struct ipv6hdr *)skb->data;
114 void *txporthdr = skb->data + sizeof(*ip6h);
115 __sum16 *csum_field, pseudo_csum;
116 __sum16 ip6_payload_csum;
117 __be16 ip_header_csum;
118
119 /* Checksum offload is only supported for UDP and TCP protocols;
120 * the packet cannot include any IPv6 extension headers
121 */
122 csum_field = rmnet_map_get_csum_field(ip6h->nexthdr, txporthdr);
123 if (!csum_field) {
124 priv->stats.csum_err_invalid_transport++;
125 return -EPROTONOSUPPORT;
126 }
127
128 /* The checksum value in the trailer is computed over the entire
129 * IP packet, including the IP header and payload. To derive the
130 * transport checksum from this, we first subract the contribution
131 * of the IP header from the trailer checksum. We then add the
132 * checksum computed over the pseudo header.
133 */
134 ip_header_csum = (__force __be16)ip_fast_csum(ip6h, sizeof(*ip6h) / 4);
135 ip6_payload_csum = csum16_sub(csum_trailer->csum_value, ip_header_csum);
136
137 pseudo_csum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
138 ntohs(ip6h->payload_len),
139 ip6h->nexthdr, 0);
140
141 /* It's sufficient to compare the IP payload checksum with the
142 * negated pseudo checksum to determine whether the packet
143 * checksum was good. (See further explanation in comments
144 * in rmnet_map_ipv4_dl_csum_trailer()).
145 *
146 * The cast is required to ensure only the low 16 bits are
147 * examined.
148 */
149 if (ip6_payload_csum != (__sum16)~pseudo_csum) {
150 priv->stats.csum_validation_failed++;
151 return -EINVAL;
152 }
153
154 priv->stats.csum_ok++;
155 return 0;
156 }
157 #else
158 static int
rmnet_map_ipv6_dl_csum_trailer(struct sk_buff * skb,struct rmnet_map_dl_csum_trailer * csum_trailer,struct rmnet_priv * priv)159 rmnet_map_ipv6_dl_csum_trailer(struct sk_buff *skb,
160 struct rmnet_map_dl_csum_trailer *csum_trailer,
161 struct rmnet_priv *priv)
162 {
163 return 0;
164 }
165 #endif
166
rmnet_map_complement_ipv4_txporthdr_csum_field(struct iphdr * ip4h)167 static void rmnet_map_complement_ipv4_txporthdr_csum_field(struct iphdr *ip4h)
168 {
169 void *txphdr;
170 u16 *csum;
171
172 txphdr = (void *)ip4h + ip4h->ihl * 4;
173
174 if (ip4h->protocol == IPPROTO_TCP || ip4h->protocol == IPPROTO_UDP) {
175 csum = (u16 *)rmnet_map_get_csum_field(ip4h->protocol, txphdr);
176 *csum = ~(*csum);
177 }
178 }
179
180 static void
rmnet_map_ipv4_ul_csum_header(struct iphdr * iphdr,struct rmnet_map_ul_csum_header * ul_header,struct sk_buff * skb)181 rmnet_map_ipv4_ul_csum_header(struct iphdr *iphdr,
182 struct rmnet_map_ul_csum_header *ul_header,
183 struct sk_buff *skb)
184 {
185 u16 val;
186
187 val = MAP_CSUM_UL_ENABLED_FLAG;
188 if (iphdr->protocol == IPPROTO_UDP)
189 val |= MAP_CSUM_UL_UDP_FLAG;
190 val |= skb->csum_offset & MAP_CSUM_UL_OFFSET_MASK;
191
192 ul_header->csum_start_offset = htons(skb_network_header_len(skb));
193 ul_header->csum_info = htons(val);
194
195 skb->ip_summed = CHECKSUM_NONE;
196
197 rmnet_map_complement_ipv4_txporthdr_csum_field(iphdr);
198 }
199
200 #if IS_ENABLED(CONFIG_IPV6)
201 static void
rmnet_map_complement_ipv6_txporthdr_csum_field(struct ipv6hdr * ip6h)202 rmnet_map_complement_ipv6_txporthdr_csum_field(struct ipv6hdr *ip6h)
203 {
204 void *txphdr;
205 u16 *csum;
206
207 txphdr = ip6h + 1;
208
209 if (ip6h->nexthdr == IPPROTO_TCP || ip6h->nexthdr == IPPROTO_UDP) {
210 csum = (u16 *)rmnet_map_get_csum_field(ip6h->nexthdr, txphdr);
211 *csum = ~(*csum);
212 }
213 }
214
215 static void
rmnet_map_ipv6_ul_csum_header(struct ipv6hdr * ipv6hdr,struct rmnet_map_ul_csum_header * ul_header,struct sk_buff * skb)216 rmnet_map_ipv6_ul_csum_header(struct ipv6hdr *ipv6hdr,
217 struct rmnet_map_ul_csum_header *ul_header,
218 struct sk_buff *skb)
219 {
220 u16 val;
221
222 val = MAP_CSUM_UL_ENABLED_FLAG;
223 if (ipv6hdr->nexthdr == IPPROTO_UDP)
224 val |= MAP_CSUM_UL_UDP_FLAG;
225 val |= skb->csum_offset & MAP_CSUM_UL_OFFSET_MASK;
226
227 ul_header->csum_start_offset = htons(skb_network_header_len(skb));
228 ul_header->csum_info = htons(val);
229
230 skb->ip_summed = CHECKSUM_NONE;
231
232 rmnet_map_complement_ipv6_txporthdr_csum_field(ipv6hdr);
233 }
234 #else
235 static void
rmnet_map_ipv6_ul_csum_header(void * ip6hdr,struct rmnet_map_ul_csum_header * ul_header,struct sk_buff * skb)236 rmnet_map_ipv6_ul_csum_header(void *ip6hdr,
237 struct rmnet_map_ul_csum_header *ul_header,
238 struct sk_buff *skb)
239 {
240 }
241 #endif
242
rmnet_map_v5_checksum_uplink_packet(struct sk_buff * skb,struct rmnet_port * port,struct net_device * orig_dev)243 static void rmnet_map_v5_checksum_uplink_packet(struct sk_buff *skb,
244 struct rmnet_port *port,
245 struct net_device *orig_dev)
246 {
247 struct rmnet_priv *priv = netdev_priv(orig_dev);
248 struct rmnet_map_v5_csum_header *ul_header;
249
250 ul_header = skb_push(skb, sizeof(*ul_header));
251 memset(ul_header, 0, sizeof(*ul_header));
252 ul_header->header_info = u8_encode_bits(RMNET_MAP_HEADER_TYPE_CSUM_OFFLOAD,
253 MAPV5_HDRINFO_HDR_TYPE_FMASK);
254
255 if (skb->ip_summed == CHECKSUM_PARTIAL) {
256 void *iph = ip_hdr(skb);
257 __sum16 *check;
258 void *trans;
259 u8 proto;
260
261 if (skb->protocol == htons(ETH_P_IP)) {
262 u16 ip_len = ((struct iphdr *)iph)->ihl * 4;
263
264 proto = ((struct iphdr *)iph)->protocol;
265 trans = iph + ip_len;
266 } else if (IS_ENABLED(CONFIG_IPV6) &&
267 skb->protocol == htons(ETH_P_IPV6)) {
268 u16 ip_len = sizeof(struct ipv6hdr);
269
270 proto = ((struct ipv6hdr *)iph)->nexthdr;
271 trans = iph + ip_len;
272 } else {
273 priv->stats.csum_err_invalid_ip_version++;
274 goto sw_csum;
275 }
276
277 check = rmnet_map_get_csum_field(proto, trans);
278 if (check) {
279 skb->ip_summed = CHECKSUM_NONE;
280 /* Ask for checksum offloading */
281 ul_header->csum_info |= MAPV5_CSUMINFO_VALID_FLAG;
282 priv->stats.csum_hw++;
283 return;
284 }
285 }
286
287 sw_csum:
288 priv->stats.csum_sw++;
289 }
290
291 /* Adds MAP header to front of skb->data
292 * Padding is calculated and set appropriately in MAP header. Mux ID is
293 * initialized to 0.
294 */
rmnet_map_add_map_header(struct sk_buff * skb,int hdrlen,struct rmnet_port * port,int pad)295 struct rmnet_map_header *rmnet_map_add_map_header(struct sk_buff *skb,
296 int hdrlen,
297 struct rmnet_port *port,
298 int pad)
299 {
300 struct rmnet_map_header *map_header;
301 u32 padding, map_datalen;
302
303 map_datalen = skb->len - hdrlen;
304 map_header = (struct rmnet_map_header *)
305 skb_push(skb, sizeof(struct rmnet_map_header));
306 memset(map_header, 0, sizeof(struct rmnet_map_header));
307
308 /* Set next_hdr bit for csum offload packets */
309 if (port->data_format & RMNET_FLAGS_EGRESS_MAP_CKSUMV5)
310 map_header->flags |= MAP_NEXT_HEADER_FLAG;
311
312 if (pad == RMNET_MAP_NO_PAD_BYTES) {
313 map_header->pkt_len = htons(map_datalen);
314 return map_header;
315 }
316
317 BUILD_BUG_ON(MAP_PAD_LEN_MASK < 3);
318 padding = ALIGN(map_datalen, 4) - map_datalen;
319
320 if (padding == 0)
321 goto done;
322
323 if (skb_tailroom(skb) < padding)
324 return NULL;
325
326 skb_put_zero(skb, padding);
327
328 done:
329 map_header->pkt_len = htons(map_datalen + padding);
330 /* This is a data packet, so the CMD bit is 0 */
331 map_header->flags = padding & MAP_PAD_LEN_MASK;
332
333 return map_header;
334 }
335
rmnet_map_validate_packet_len(struct sk_buff * skb,struct rmnet_port * port)336 u32 rmnet_map_validate_packet_len(struct sk_buff *skb, struct rmnet_port *port)
337 {
338 struct rmnet_map_v5_csum_header *next_hdr = NULL;
339 struct rmnet_map_header *maph;
340 void *data = skb->data;
341 u32 packet_len;
342
343 if (skb->len < sizeof(*maph))
344 return 0;
345
346 maph = (struct rmnet_map_header *)skb->data;
347
348 /* Some hardware can send us empty frames. Catch them */
349 if (!maph->pkt_len)
350 return 0;
351
352 packet_len = ntohs(maph->pkt_len) + sizeof(*maph);
353
354 if (port->data_format & RMNET_FLAGS_INGRESS_MAP_CKSUMV4) {
355 packet_len += sizeof(struct rmnet_map_dl_csum_trailer);
356 } else if ((port->data_format & RMNET_FLAGS_INGRESS_MAP_CKSUMV5) &&
357 !(maph->flags & MAP_CMD_FLAG)) {
358 /* Mapv5 data pkt without csum hdr is invalid */
359 if (!(maph->flags & MAP_NEXT_HEADER_FLAG))
360 return 0;
361
362 packet_len += sizeof(*next_hdr);
363 next_hdr = data + sizeof(*maph);
364 }
365
366 if (skb->len < packet_len)
367 return 0;
368
369 if (next_hdr &&
370 u8_get_bits(next_hdr->header_info, MAPV5_HDRINFO_HDR_TYPE_FMASK) !=
371 RMNET_MAP_HEADER_TYPE_CSUM_OFFLOAD)
372 return 0;
373
374 return packet_len;
375 }
376
377 /* Deaggregates a single packet
378 * A whole new buffer is allocated for each portion of an aggregated frame.
379 * Caller should keep calling deaggregate() on the source skb until 0 is
380 * returned, indicating that there are no more packets to deaggregate. Caller
381 * is responsible for freeing the original skb.
382 */
rmnet_map_deaggregate(struct sk_buff * skb,struct rmnet_port * port)383 struct sk_buff *rmnet_map_deaggregate(struct sk_buff *skb,
384 struct rmnet_port *port)
385 {
386 struct sk_buff *skbn;
387 u32 packet_len;
388
389 packet_len = rmnet_map_validate_packet_len(skb, port);
390 if (!packet_len)
391 return NULL;
392
393 skbn = alloc_skb(packet_len + RMNET_MAP_DEAGGR_SPACING, GFP_ATOMIC);
394 if (!skbn)
395 return NULL;
396
397 skbn->dev = skb->dev;
398 skb_reserve(skbn, RMNET_MAP_DEAGGR_HEADROOM);
399 skb_put(skbn, packet_len);
400 memcpy(skbn->data, skb->data, packet_len);
401 skb_pull(skb, packet_len);
402
403 return skbn;
404 }
405
406 /* Validates packet checksums. Function takes a pointer to
407 * the beginning of a buffer which contains the IP payload +
408 * padding + checksum trailer.
409 * Only IPv4 and IPv6 are supported along with TCP & UDP.
410 * Fragmented or tunneled packets are not supported.
411 */
rmnet_map_checksum_downlink_packet(struct sk_buff * skb,u16 len)412 int rmnet_map_checksum_downlink_packet(struct sk_buff *skb, u16 len)
413 {
414 struct rmnet_priv *priv = netdev_priv(skb->dev);
415 struct rmnet_map_dl_csum_trailer *csum_trailer;
416
417 if (unlikely(!(skb->dev->features & NETIF_F_RXCSUM))) {
418 priv->stats.csum_sw++;
419 return -EOPNOTSUPP;
420 }
421
422 csum_trailer = (struct rmnet_map_dl_csum_trailer *)(skb->data + len);
423
424 if (!(csum_trailer->flags & MAP_CSUM_DL_VALID_FLAG)) {
425 priv->stats.csum_valid_unset++;
426 return -EINVAL;
427 }
428
429 if (skb->protocol == htons(ETH_P_IP))
430 return rmnet_map_ipv4_dl_csum_trailer(skb, csum_trailer, priv);
431
432 if (IS_ENABLED(CONFIG_IPV6) && skb->protocol == htons(ETH_P_IPV6))
433 return rmnet_map_ipv6_dl_csum_trailer(skb, csum_trailer, priv);
434
435 priv->stats.csum_err_invalid_ip_version++;
436
437 return -EPROTONOSUPPORT;
438 }
439
rmnet_map_v4_checksum_uplink_packet(struct sk_buff * skb,struct net_device * orig_dev)440 static void rmnet_map_v4_checksum_uplink_packet(struct sk_buff *skb,
441 struct net_device *orig_dev)
442 {
443 struct rmnet_priv *priv = netdev_priv(orig_dev);
444 struct rmnet_map_ul_csum_header *ul_header;
445 void *iphdr;
446
447 ul_header = (struct rmnet_map_ul_csum_header *)
448 skb_push(skb, sizeof(struct rmnet_map_ul_csum_header));
449
450 if (unlikely(!(orig_dev->features &
451 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))))
452 goto sw_csum;
453
454 if (skb->ip_summed != CHECKSUM_PARTIAL)
455 goto sw_csum;
456
457 iphdr = (char *)ul_header +
458 sizeof(struct rmnet_map_ul_csum_header);
459
460 if (skb->protocol == htons(ETH_P_IP)) {
461 rmnet_map_ipv4_ul_csum_header(iphdr, ul_header, skb);
462 priv->stats.csum_hw++;
463 return;
464 }
465
466 if (IS_ENABLED(CONFIG_IPV6) && skb->protocol == htons(ETH_P_IPV6)) {
467 rmnet_map_ipv6_ul_csum_header(iphdr, ul_header, skb);
468 priv->stats.csum_hw++;
469 return;
470 }
471
472 priv->stats.csum_err_invalid_ip_version++;
473
474 sw_csum:
475 memset(ul_header, 0, sizeof(*ul_header));
476
477 priv->stats.csum_sw++;
478 }
479
480 /* Generates UL checksum meta info header for IPv4 and IPv6 over TCP and UDP
481 * packets that are supported for UL checksum offload.
482 */
rmnet_map_checksum_uplink_packet(struct sk_buff * skb,struct rmnet_port * port,struct net_device * orig_dev,int csum_type)483 void rmnet_map_checksum_uplink_packet(struct sk_buff *skb,
484 struct rmnet_port *port,
485 struct net_device *orig_dev,
486 int csum_type)
487 {
488 switch (csum_type) {
489 case RMNET_FLAGS_EGRESS_MAP_CKSUMV4:
490 rmnet_map_v4_checksum_uplink_packet(skb, orig_dev);
491 break;
492 case RMNET_FLAGS_EGRESS_MAP_CKSUMV5:
493 rmnet_map_v5_checksum_uplink_packet(skb, port, orig_dev);
494 break;
495 default:
496 break;
497 }
498 }
499
500 /* Process a MAPv5 packet header */
rmnet_map_process_next_hdr_packet(struct sk_buff * skb,u16 len)501 int rmnet_map_process_next_hdr_packet(struct sk_buff *skb,
502 u16 len)
503 {
504 struct rmnet_priv *priv = netdev_priv(skb->dev);
505 struct rmnet_map_v5_csum_header *next_hdr;
506 u8 nexthdr_type;
507
508 next_hdr = (struct rmnet_map_v5_csum_header *)(skb->data +
509 sizeof(struct rmnet_map_header));
510
511 nexthdr_type = u8_get_bits(next_hdr->header_info,
512 MAPV5_HDRINFO_HDR_TYPE_FMASK);
513
514 if (nexthdr_type != RMNET_MAP_HEADER_TYPE_CSUM_OFFLOAD)
515 return -EINVAL;
516
517 if (unlikely(!(skb->dev->features & NETIF_F_RXCSUM))) {
518 priv->stats.csum_sw++;
519 } else if (next_hdr->csum_info & MAPV5_CSUMINFO_VALID_FLAG) {
520 priv->stats.csum_ok++;
521 skb->ip_summed = CHECKSUM_UNNECESSARY;
522 } else {
523 priv->stats.csum_valid_unset++;
524 }
525
526 /* Pull csum v5 header */
527 skb_pull(skb, sizeof(*next_hdr));
528
529 return 0;
530 }
531
532 #define RMNET_AGG_BYPASS_TIME_NSEC 10000000L
533
reset_aggr_params(struct rmnet_port * port)534 static void reset_aggr_params(struct rmnet_port *port)
535 {
536 port->skbagg_head = NULL;
537 port->agg_count = 0;
538 port->agg_state = 0;
539 memset(&port->agg_time, 0, sizeof(struct timespec64));
540 }
541
rmnet_send_skb(struct rmnet_port * port,struct sk_buff * skb)542 static void rmnet_send_skb(struct rmnet_port *port, struct sk_buff *skb)
543 {
544 if (skb_needs_linearize(skb, port->dev->features)) {
545 if (unlikely(__skb_linearize(skb))) {
546 struct rmnet_priv *priv;
547
548 priv = netdev_priv(port->rmnet_dev);
549 this_cpu_inc(priv->pcpu_stats->stats.tx_drops);
550 dev_kfree_skb_any(skb);
551 return;
552 }
553 }
554
555 dev_queue_xmit(skb);
556 }
557
rmnet_map_flush_tx_packet_work(struct work_struct * work)558 static void rmnet_map_flush_tx_packet_work(struct work_struct *work)
559 {
560 struct sk_buff *skb = NULL;
561 struct rmnet_port *port;
562
563 port = container_of(work, struct rmnet_port, agg_wq);
564
565 spin_lock_bh(&port->agg_lock);
566 if (likely(port->agg_state == -EINPROGRESS)) {
567 /* Buffer may have already been shipped out */
568 if (likely(port->skbagg_head)) {
569 skb = port->skbagg_head;
570 reset_aggr_params(port);
571 }
572 port->agg_state = 0;
573 }
574
575 spin_unlock_bh(&port->agg_lock);
576 if (skb)
577 rmnet_send_skb(port, skb);
578 }
579
rmnet_map_flush_tx_packet_queue(struct hrtimer * t)580 static enum hrtimer_restart rmnet_map_flush_tx_packet_queue(struct hrtimer *t)
581 {
582 struct rmnet_port *port;
583
584 port = container_of(t, struct rmnet_port, hrtimer);
585
586 schedule_work(&port->agg_wq);
587
588 return HRTIMER_NORESTART;
589 }
590
rmnet_map_tx_aggregate(struct sk_buff * skb,struct rmnet_port * port,struct net_device * orig_dev)591 unsigned int rmnet_map_tx_aggregate(struct sk_buff *skb, struct rmnet_port *port,
592 struct net_device *orig_dev)
593 {
594 struct timespec64 diff, last;
595 unsigned int len = skb->len;
596 struct sk_buff *agg_skb;
597 int size;
598
599 spin_lock_bh(&port->agg_lock);
600 memcpy(&last, &port->agg_last, sizeof(struct timespec64));
601 ktime_get_real_ts64(&port->agg_last);
602
603 if (!port->skbagg_head) {
604 /* Check to see if we should agg first. If the traffic is very
605 * sparse, don't aggregate.
606 */
607 new_packet:
608 diff = timespec64_sub(port->agg_last, last);
609 size = port->egress_agg_params.bytes - skb->len;
610
611 if (size < 0) {
612 /* dropped */
613 spin_unlock_bh(&port->agg_lock);
614 return 0;
615 }
616
617 if (diff.tv_sec > 0 || diff.tv_nsec > RMNET_AGG_BYPASS_TIME_NSEC ||
618 size == 0)
619 goto no_aggr;
620
621 port->skbagg_head = skb_copy_expand(skb, 0, size, GFP_ATOMIC);
622 if (!port->skbagg_head)
623 goto no_aggr;
624
625 dev_kfree_skb_any(skb);
626 port->skbagg_head->protocol = htons(ETH_P_MAP);
627 port->agg_count = 1;
628 ktime_get_real_ts64(&port->agg_time);
629 skb_frag_list_init(port->skbagg_head);
630 goto schedule;
631 }
632 diff = timespec64_sub(port->agg_last, port->agg_time);
633 size = port->egress_agg_params.bytes - port->skbagg_head->len;
634
635 if (skb->len > size) {
636 agg_skb = port->skbagg_head;
637 reset_aggr_params(port);
638 spin_unlock_bh(&port->agg_lock);
639 hrtimer_cancel(&port->hrtimer);
640 rmnet_send_skb(port, agg_skb);
641 spin_lock_bh(&port->agg_lock);
642 goto new_packet;
643 }
644
645 if (skb_has_frag_list(port->skbagg_head))
646 port->skbagg_tail->next = skb;
647 else
648 skb_shinfo(port->skbagg_head)->frag_list = skb;
649
650 port->skbagg_head->len += skb->len;
651 port->skbagg_head->data_len += skb->len;
652 port->skbagg_head->truesize += skb->truesize;
653 port->skbagg_tail = skb;
654 port->agg_count++;
655
656 if (diff.tv_sec > 0 || diff.tv_nsec > port->egress_agg_params.time_nsec ||
657 port->agg_count >= port->egress_agg_params.count ||
658 port->skbagg_head->len == port->egress_agg_params.bytes) {
659 agg_skb = port->skbagg_head;
660 reset_aggr_params(port);
661 spin_unlock_bh(&port->agg_lock);
662 hrtimer_cancel(&port->hrtimer);
663 rmnet_send_skb(port, agg_skb);
664 return len;
665 }
666
667 schedule:
668 if (!hrtimer_active(&port->hrtimer) && port->agg_state != -EINPROGRESS) {
669 port->agg_state = -EINPROGRESS;
670 hrtimer_start(&port->hrtimer,
671 ns_to_ktime(port->egress_agg_params.time_nsec),
672 HRTIMER_MODE_REL);
673 }
674 spin_unlock_bh(&port->agg_lock);
675
676 return len;
677
678 no_aggr:
679 spin_unlock_bh(&port->agg_lock);
680 skb->protocol = htons(ETH_P_MAP);
681 dev_queue_xmit(skb);
682
683 return len;
684 }
685
rmnet_map_update_ul_agg_config(struct rmnet_port * port,u32 size,u32 count,u32 time)686 void rmnet_map_update_ul_agg_config(struct rmnet_port *port, u32 size,
687 u32 count, u32 time)
688 {
689 spin_lock_bh(&port->agg_lock);
690 port->egress_agg_params.bytes = size;
691 WRITE_ONCE(port->egress_agg_params.count, count);
692 port->egress_agg_params.time_nsec = time * NSEC_PER_USEC;
693 spin_unlock_bh(&port->agg_lock);
694 }
695
rmnet_map_tx_aggregate_init(struct rmnet_port * port)696 void rmnet_map_tx_aggregate_init(struct rmnet_port *port)
697 {
698 hrtimer_setup(&port->hrtimer, rmnet_map_flush_tx_packet_queue, CLOCK_MONOTONIC,
699 HRTIMER_MODE_REL);
700 spin_lock_init(&port->agg_lock);
701 rmnet_map_update_ul_agg_config(port, 4096, 1, 800);
702 INIT_WORK(&port->agg_wq, rmnet_map_flush_tx_packet_work);
703 }
704
rmnet_map_tx_aggregate_exit(struct rmnet_port * port)705 void rmnet_map_tx_aggregate_exit(struct rmnet_port *port)
706 {
707 hrtimer_cancel(&port->hrtimer);
708 cancel_work_sync(&port->agg_wq);
709
710 spin_lock_bh(&port->agg_lock);
711 if (port->agg_state == -EINPROGRESS) {
712 if (port->skbagg_head) {
713 dev_kfree_skb_any(port->skbagg_head);
714 reset_aggr_params(port);
715 }
716
717 port->agg_state = 0;
718 }
719 spin_unlock_bh(&port->agg_lock);
720 }
721