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 skb_reserve(skbn, RMNET_MAP_DEAGGR_HEADROOM);
398 skb_put(skbn, packet_len);
399 memcpy(skbn->data, skb->data, packet_len);
400 skb_pull(skb, packet_len);
401
402 return skbn;
403 }
404
405 /* Validates packet checksums. Function takes a pointer to
406 * the beginning of a buffer which contains the IP payload +
407 * padding + checksum trailer.
408 * Only IPv4 and IPv6 are supported along with TCP & UDP.
409 * Fragmented or tunneled packets are not supported.
410 */
rmnet_map_checksum_downlink_packet(struct sk_buff * skb,u16 len)411 int rmnet_map_checksum_downlink_packet(struct sk_buff *skb, u16 len)
412 {
413 struct rmnet_priv *priv = netdev_priv(skb->dev);
414 struct rmnet_map_dl_csum_trailer *csum_trailer;
415
416 if (unlikely(!(skb->dev->features & NETIF_F_RXCSUM))) {
417 priv->stats.csum_sw++;
418 return -EOPNOTSUPP;
419 }
420
421 csum_trailer = (struct rmnet_map_dl_csum_trailer *)(skb->data + len);
422
423 if (!(csum_trailer->flags & MAP_CSUM_DL_VALID_FLAG)) {
424 priv->stats.csum_valid_unset++;
425 return -EINVAL;
426 }
427
428 if (skb->protocol == htons(ETH_P_IP))
429 return rmnet_map_ipv4_dl_csum_trailer(skb, csum_trailer, priv);
430
431 if (IS_ENABLED(CONFIG_IPV6) && skb->protocol == htons(ETH_P_IPV6))
432 return rmnet_map_ipv6_dl_csum_trailer(skb, csum_trailer, priv);
433
434 priv->stats.csum_err_invalid_ip_version++;
435
436 return -EPROTONOSUPPORT;
437 }
438
rmnet_map_v4_checksum_uplink_packet(struct sk_buff * skb,struct net_device * orig_dev)439 static void rmnet_map_v4_checksum_uplink_packet(struct sk_buff *skb,
440 struct net_device *orig_dev)
441 {
442 struct rmnet_priv *priv = netdev_priv(orig_dev);
443 struct rmnet_map_ul_csum_header *ul_header;
444 void *iphdr;
445
446 ul_header = (struct rmnet_map_ul_csum_header *)
447 skb_push(skb, sizeof(struct rmnet_map_ul_csum_header));
448
449 if (unlikely(!(orig_dev->features &
450 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))))
451 goto sw_csum;
452
453 if (skb->ip_summed != CHECKSUM_PARTIAL)
454 goto sw_csum;
455
456 iphdr = (char *)ul_header +
457 sizeof(struct rmnet_map_ul_csum_header);
458
459 if (skb->protocol == htons(ETH_P_IP)) {
460 rmnet_map_ipv4_ul_csum_header(iphdr, ul_header, skb);
461 priv->stats.csum_hw++;
462 return;
463 }
464
465 if (IS_ENABLED(CONFIG_IPV6) && skb->protocol == htons(ETH_P_IPV6)) {
466 rmnet_map_ipv6_ul_csum_header(iphdr, ul_header, skb);
467 priv->stats.csum_hw++;
468 return;
469 }
470
471 priv->stats.csum_err_invalid_ip_version++;
472
473 sw_csum:
474 memset(ul_header, 0, sizeof(*ul_header));
475
476 priv->stats.csum_sw++;
477 }
478
479 /* Generates UL checksum meta info header for IPv4 and IPv6 over TCP and UDP
480 * packets that are supported for UL checksum offload.
481 */
rmnet_map_checksum_uplink_packet(struct sk_buff * skb,struct rmnet_port * port,struct net_device * orig_dev,int csum_type)482 void rmnet_map_checksum_uplink_packet(struct sk_buff *skb,
483 struct rmnet_port *port,
484 struct net_device *orig_dev,
485 int csum_type)
486 {
487 switch (csum_type) {
488 case RMNET_FLAGS_EGRESS_MAP_CKSUMV4:
489 rmnet_map_v4_checksum_uplink_packet(skb, orig_dev);
490 break;
491 case RMNET_FLAGS_EGRESS_MAP_CKSUMV5:
492 rmnet_map_v5_checksum_uplink_packet(skb, port, orig_dev);
493 break;
494 default:
495 break;
496 }
497 }
498
499 /* Process a MAPv5 packet header */
rmnet_map_process_next_hdr_packet(struct sk_buff * skb,u16 len)500 int rmnet_map_process_next_hdr_packet(struct sk_buff *skb,
501 u16 len)
502 {
503 struct rmnet_priv *priv = netdev_priv(skb->dev);
504 struct rmnet_map_v5_csum_header *next_hdr;
505 u8 nexthdr_type;
506
507 next_hdr = (struct rmnet_map_v5_csum_header *)(skb->data +
508 sizeof(struct rmnet_map_header));
509
510 nexthdr_type = u8_get_bits(next_hdr->header_info,
511 MAPV5_HDRINFO_HDR_TYPE_FMASK);
512
513 if (nexthdr_type != RMNET_MAP_HEADER_TYPE_CSUM_OFFLOAD)
514 return -EINVAL;
515
516 if (unlikely(!(skb->dev->features & NETIF_F_RXCSUM))) {
517 priv->stats.csum_sw++;
518 } else if (next_hdr->csum_info & MAPV5_CSUMINFO_VALID_FLAG) {
519 priv->stats.csum_ok++;
520 skb->ip_summed = CHECKSUM_UNNECESSARY;
521 } else {
522 priv->stats.csum_valid_unset++;
523 }
524
525 /* Pull csum v5 header */
526 skb_pull(skb, sizeof(*next_hdr));
527
528 return 0;
529 }
530
531 #define RMNET_AGG_BYPASS_TIME_NSEC 10000000L
532
reset_aggr_params(struct rmnet_port * port)533 static void reset_aggr_params(struct rmnet_port *port)
534 {
535 port->skbagg_head = NULL;
536 port->agg_count = 0;
537 port->agg_state = 0;
538 memset(&port->agg_time, 0, sizeof(struct timespec64));
539 }
540
rmnet_send_skb(struct rmnet_port * port,struct sk_buff * skb)541 static void rmnet_send_skb(struct rmnet_port *port, struct sk_buff *skb)
542 {
543 if (skb_needs_linearize(skb, port->dev->features)) {
544 if (unlikely(__skb_linearize(skb))) {
545 struct rmnet_priv *priv;
546
547 priv = netdev_priv(port->rmnet_dev);
548 this_cpu_inc(priv->pcpu_stats->stats.tx_drops);
549 dev_kfree_skb_any(skb);
550 return;
551 }
552 }
553
554 dev_queue_xmit(skb);
555 }
556
rmnet_map_flush_tx_packet_work(struct work_struct * work)557 static void rmnet_map_flush_tx_packet_work(struct work_struct *work)
558 {
559 struct sk_buff *skb = NULL;
560 struct rmnet_port *port;
561
562 port = container_of(work, struct rmnet_port, agg_wq);
563
564 spin_lock_bh(&port->agg_lock);
565 if (likely(port->agg_state == -EINPROGRESS)) {
566 /* Buffer may have already been shipped out */
567 if (likely(port->skbagg_head)) {
568 skb = port->skbagg_head;
569 reset_aggr_params(port);
570 }
571 port->agg_state = 0;
572 }
573
574 spin_unlock_bh(&port->agg_lock);
575 if (skb)
576 rmnet_send_skb(port, skb);
577 }
578
rmnet_map_flush_tx_packet_queue(struct hrtimer * t)579 static enum hrtimer_restart rmnet_map_flush_tx_packet_queue(struct hrtimer *t)
580 {
581 struct rmnet_port *port;
582
583 port = container_of(t, struct rmnet_port, hrtimer);
584
585 schedule_work(&port->agg_wq);
586
587 return HRTIMER_NORESTART;
588 }
589
rmnet_map_tx_aggregate(struct sk_buff * skb,struct rmnet_port * port,struct net_device * orig_dev)590 unsigned int rmnet_map_tx_aggregate(struct sk_buff *skb, struct rmnet_port *port,
591 struct net_device *orig_dev)
592 {
593 struct timespec64 diff, last;
594 unsigned int len = skb->len;
595 struct sk_buff *agg_skb;
596 int size;
597
598 spin_lock_bh(&port->agg_lock);
599 memcpy(&last, &port->agg_last, sizeof(struct timespec64));
600 ktime_get_real_ts64(&port->agg_last);
601
602 if (!port->skbagg_head) {
603 /* Check to see if we should agg first. If the traffic is very
604 * sparse, don't aggregate.
605 */
606 new_packet:
607 diff = timespec64_sub(port->agg_last, last);
608 size = port->egress_agg_params.bytes - skb->len;
609
610 if (size < 0) {
611 /* dropped */
612 spin_unlock_bh(&port->agg_lock);
613 return 0;
614 }
615
616 if (diff.tv_sec > 0 || diff.tv_nsec > RMNET_AGG_BYPASS_TIME_NSEC ||
617 size == 0)
618 goto no_aggr;
619
620 port->skbagg_head = skb_copy_expand(skb, 0, size, GFP_ATOMIC);
621 if (!port->skbagg_head)
622 goto no_aggr;
623
624 dev_kfree_skb_any(skb);
625 port->skbagg_head->protocol = htons(ETH_P_MAP);
626 port->agg_count = 1;
627 ktime_get_real_ts64(&port->agg_time);
628 skb_frag_list_init(port->skbagg_head);
629 goto schedule;
630 }
631 diff = timespec64_sub(port->agg_last, port->agg_time);
632 size = port->egress_agg_params.bytes - port->skbagg_head->len;
633
634 if (skb->len > size) {
635 agg_skb = port->skbagg_head;
636 reset_aggr_params(port);
637 spin_unlock_bh(&port->agg_lock);
638 hrtimer_cancel(&port->hrtimer);
639 rmnet_send_skb(port, agg_skb);
640 spin_lock_bh(&port->agg_lock);
641 goto new_packet;
642 }
643
644 if (skb_has_frag_list(port->skbagg_head))
645 port->skbagg_tail->next = skb;
646 else
647 skb_shinfo(port->skbagg_head)->frag_list = skb;
648
649 port->skbagg_head->len += skb->len;
650 port->skbagg_head->data_len += skb->len;
651 port->skbagg_head->truesize += skb->truesize;
652 port->skbagg_tail = skb;
653 port->agg_count++;
654
655 if (diff.tv_sec > 0 || diff.tv_nsec > port->egress_agg_params.time_nsec ||
656 port->agg_count >= port->egress_agg_params.count ||
657 port->skbagg_head->len == port->egress_agg_params.bytes) {
658 agg_skb = port->skbagg_head;
659 reset_aggr_params(port);
660 spin_unlock_bh(&port->agg_lock);
661 hrtimer_cancel(&port->hrtimer);
662 rmnet_send_skb(port, agg_skb);
663 return len;
664 }
665
666 schedule:
667 if (!hrtimer_active(&port->hrtimer) && port->agg_state != -EINPROGRESS) {
668 port->agg_state = -EINPROGRESS;
669 hrtimer_start(&port->hrtimer,
670 ns_to_ktime(port->egress_agg_params.time_nsec),
671 HRTIMER_MODE_REL);
672 }
673 spin_unlock_bh(&port->agg_lock);
674
675 return len;
676
677 no_aggr:
678 spin_unlock_bh(&port->agg_lock);
679 skb->protocol = htons(ETH_P_MAP);
680 dev_queue_xmit(skb);
681
682 return len;
683 }
684
rmnet_map_update_ul_agg_config(struct rmnet_port * port,u32 size,u32 count,u32 time)685 void rmnet_map_update_ul_agg_config(struct rmnet_port *port, u32 size,
686 u32 count, u32 time)
687 {
688 spin_lock_bh(&port->agg_lock);
689 port->egress_agg_params.bytes = size;
690 WRITE_ONCE(port->egress_agg_params.count, count);
691 port->egress_agg_params.time_nsec = time * NSEC_PER_USEC;
692 spin_unlock_bh(&port->agg_lock);
693 }
694
rmnet_map_tx_aggregate_init(struct rmnet_port * port)695 void rmnet_map_tx_aggregate_init(struct rmnet_port *port)
696 {
697 hrtimer_setup(&port->hrtimer, rmnet_map_flush_tx_packet_queue, CLOCK_MONOTONIC,
698 HRTIMER_MODE_REL);
699 spin_lock_init(&port->agg_lock);
700 rmnet_map_update_ul_agg_config(port, 4096, 1, 800);
701 INIT_WORK(&port->agg_wq, rmnet_map_flush_tx_packet_work);
702 }
703
rmnet_map_tx_aggregate_exit(struct rmnet_port * port)704 void rmnet_map_tx_aggregate_exit(struct rmnet_port *port)
705 {
706 hrtimer_cancel(&port->hrtimer);
707 cancel_work_sync(&port->agg_wq);
708
709 spin_lock_bh(&port->agg_lock);
710 if (port->agg_state == -EINPROGRESS) {
711 if (port->skbagg_head) {
712 dev_kfree_skb_any(port->skbagg_head);
713 reset_aggr_params(port);
714 }
715
716 port->agg_state = 0;
717 }
718 spin_unlock_bh(&port->agg_lock);
719 }
720