rmnet_map_data.c 19 KB

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