rmnet_map_data.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717
  1. /* Copyright (c) 2013-2021, The Linux Foundation. All rights reserved.
  2. * Copyright (c) 2021-2022 Qualcomm Innovation Center, Inc. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * RMNET Data MAP protocol
  14. *
  15. */
  16. #include <linux/netdevice.h>
  17. #include <linux/ip.h>
  18. #include <linux/ipv6.h>
  19. #include <net/ip6_checksum.h>
  20. #include "rmnet_config.h"
  21. #include "rmnet_map.h"
  22. #include "rmnet_private.h"
  23. #include "rmnet_handlers.h"
  24. #include "rmnet_ll.h"
  25. #define RMNET_MAP_PKT_COPY_THRESHOLD 64
  26. #define RMNET_MAP_DEAGGR_SPACING 64
  27. #define RMNET_MAP_DEAGGR_HEADROOM (RMNET_MAP_DEAGGR_SPACING / 2)
  28. struct rmnet_map_coal_metadata {
  29. void *ip_header;
  30. void *trans_header;
  31. u16 ip_len;
  32. u16 trans_len;
  33. u16 data_offset;
  34. u16 data_len;
  35. u8 ip_proto;
  36. u8 trans_proto;
  37. u8 pkt_id;
  38. u8 pkt_count;
  39. };
  40. static __sum16 *rmnet_map_get_csum_field(unsigned char protocol,
  41. const void *txporthdr)
  42. {
  43. __sum16 *check = NULL;
  44. switch (protocol) {
  45. case IPPROTO_TCP:
  46. check = &(((struct tcphdr *)txporthdr)->check);
  47. break;
  48. case IPPROTO_UDP:
  49. check = &(((struct udphdr *)txporthdr)->check);
  50. break;
  51. default:
  52. check = NULL;
  53. break;
  54. }
  55. return check;
  56. }
  57. static int
  58. rmnet_map_ipv4_dl_csum_trailer(struct sk_buff *skb,
  59. struct rmnet_map_dl_csum_trailer *csum_trailer,
  60. struct rmnet_priv *priv)
  61. {
  62. __sum16 *csum_field, csum_temp, pseudo_csum, hdr_csum, ip_payload_csum;
  63. u16 csum_value, csum_value_final;
  64. struct iphdr *ip4h;
  65. void *txporthdr;
  66. __be16 addend;
  67. ip4h = (struct iphdr *)rmnet_map_data_ptr(skb);
  68. if ((ntohs(ip4h->frag_off) & IP_MF) ||
  69. ((ntohs(ip4h->frag_off) & IP_OFFSET) > 0)) {
  70. priv->stats.csum_fragmented_pkt++;
  71. return -EOPNOTSUPP;
  72. }
  73. txporthdr = rmnet_map_data_ptr(skb) + ip4h->ihl * 4;
  74. csum_field = rmnet_map_get_csum_field(ip4h->protocol, txporthdr);
  75. if (!csum_field) {
  76. priv->stats.csum_err_invalid_transport++;
  77. return -EPROTONOSUPPORT;
  78. }
  79. /* RFC 768 - Skip IPv4 UDP packets where sender checksum field is 0 */
  80. if (*csum_field == 0 && ip4h->protocol == IPPROTO_UDP) {
  81. priv->stats.csum_skipped++;
  82. return 0;
  83. }
  84. csum_value = ~ntohs(csum_trailer->csum_value);
  85. hdr_csum = ~ip_fast_csum(ip4h, (int)ip4h->ihl);
  86. ip_payload_csum = csum16_sub((__force __sum16)csum_value,
  87. (__force __be16)hdr_csum);
  88. pseudo_csum = ~csum_tcpudp_magic(ip4h->saddr, ip4h->daddr,
  89. ntohs(ip4h->tot_len) - ip4h->ihl * 4,
  90. ip4h->protocol, 0);
  91. addend = (__force __be16)ntohs((__force __be16)pseudo_csum);
  92. pseudo_csum = csum16_add(ip_payload_csum, addend);
  93. addend = (__force __be16)ntohs((__force __be16)*csum_field);
  94. csum_temp = ~csum16_sub(pseudo_csum, addend);
  95. csum_value_final = (__force u16)csum_temp;
  96. if (unlikely(csum_value_final == 0)) {
  97. switch (ip4h->protocol) {
  98. case IPPROTO_UDP:
  99. /* RFC 768 - DL4 1's complement rule for UDP csum 0 */
  100. csum_value_final = ~csum_value_final;
  101. break;
  102. case IPPROTO_TCP:
  103. /* DL4 Non-RFC compliant TCP checksum found */
  104. if (*csum_field == (__force __sum16)0xFFFF)
  105. csum_value_final = ~csum_value_final;
  106. break;
  107. }
  108. }
  109. if (csum_value_final == ntohs((__force __be16)*csum_field)) {
  110. priv->stats.csum_ok++;
  111. return 0;
  112. } else {
  113. priv->stats.csum_validation_failed++;
  114. return -EINVAL;
  115. }
  116. }
  117. #if IS_ENABLED(CONFIG_IPV6)
  118. static int
  119. rmnet_map_ipv6_dl_csum_trailer(struct sk_buff *skb,
  120. struct rmnet_map_dl_csum_trailer *csum_trailer,
  121. struct rmnet_priv *priv)
  122. {
  123. __sum16 *csum_field, ip6_payload_csum, pseudo_csum, csum_temp;
  124. u16 csum_value, csum_value_final;
  125. __be16 ip6_hdr_csum, addend;
  126. struct ipv6hdr *ip6h;
  127. void *txporthdr, *data = rmnet_map_data_ptr(skb);
  128. u32 length;
  129. ip6h = data;
  130. txporthdr = data + sizeof(struct ipv6hdr);
  131. csum_field = rmnet_map_get_csum_field(ip6h->nexthdr, txporthdr);
  132. if (!csum_field) {
  133. priv->stats.csum_err_invalid_transport++;
  134. return -EPROTONOSUPPORT;
  135. }
  136. csum_value = ~ntohs(csum_trailer->csum_value);
  137. ip6_hdr_csum = (__force __be16)
  138. ~ntohs((__force __be16)ip_compute_csum(ip6h,
  139. (int)(txporthdr - data)));
  140. ip6_payload_csum = csum16_sub((__force __sum16)csum_value,
  141. ip6_hdr_csum);
  142. length = (ip6h->nexthdr == IPPROTO_UDP) ?
  143. ntohs(((struct udphdr *)txporthdr)->len) :
  144. ntohs(ip6h->payload_len);
  145. pseudo_csum = ~(csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
  146. length, ip6h->nexthdr, 0));
  147. addend = (__force __be16)ntohs((__force __be16)pseudo_csum);
  148. pseudo_csum = csum16_add(ip6_payload_csum, addend);
  149. addend = (__force __be16)ntohs((__force __be16)*csum_field);
  150. csum_temp = ~csum16_sub(pseudo_csum, addend);
  151. csum_value_final = (__force u16)csum_temp;
  152. if (unlikely(csum_value_final == 0)) {
  153. switch (ip6h->nexthdr) {
  154. case IPPROTO_UDP:
  155. /* RFC 2460 section 8.1
  156. * DL6 One's complement rule for UDP checksum 0
  157. */
  158. csum_value_final = ~csum_value_final;
  159. break;
  160. case IPPROTO_TCP:
  161. /* DL6 Non-RFC compliant TCP checksum found */
  162. if (*csum_field == (__force __sum16)0xFFFF)
  163. csum_value_final = ~csum_value_final;
  164. break;
  165. }
  166. }
  167. if (csum_value_final == ntohs((__force __be16)*csum_field)) {
  168. priv->stats.csum_ok++;
  169. return 0;
  170. } else {
  171. priv->stats.csum_validation_failed++;
  172. return -EINVAL;
  173. }
  174. }
  175. #endif
  176. static void rmnet_map_complement_ipv4_txporthdr_csum_field(void *iphdr)
  177. {
  178. struct iphdr *ip4h = (struct iphdr *)iphdr;
  179. void *txphdr;
  180. u16 *csum;
  181. txphdr = iphdr + ip4h->ihl * 4;
  182. if (ip4h->protocol == IPPROTO_TCP || ip4h->protocol == IPPROTO_UDP) {
  183. csum = (u16 *)rmnet_map_get_csum_field(ip4h->protocol, txphdr);
  184. *csum = ~(*csum);
  185. }
  186. }
  187. static void
  188. rmnet_map_ipv4_ul_csum_header(void *iphdr,
  189. struct rmnet_map_ul_csum_header *ul_header,
  190. struct sk_buff *skb)
  191. {
  192. struct iphdr *ip4h = (struct iphdr *)iphdr;
  193. __be16 *hdr = (__be16 *)ul_header, offset;
  194. offset = htons((__force u16)(skb_transport_header(skb) -
  195. (unsigned char *)iphdr));
  196. ul_header->csum_start_offset = offset;
  197. ul_header->csum_insert_offset = skb->csum_offset;
  198. ul_header->csum_enabled = 1;
  199. if (ip4h->protocol == IPPROTO_UDP)
  200. ul_header->udp_ind = 1;
  201. else
  202. ul_header->udp_ind = 0;
  203. /* Changing remaining fields to network order */
  204. hdr++;
  205. *hdr = htons((__force u16)*hdr);
  206. skb->ip_summed = CHECKSUM_NONE;
  207. rmnet_map_complement_ipv4_txporthdr_csum_field(iphdr);
  208. }
  209. #if IS_ENABLED(CONFIG_IPV6)
  210. static void rmnet_map_complement_ipv6_txporthdr_csum_field(void *ip6hdr)
  211. {
  212. struct ipv6hdr *ip6h = (struct ipv6hdr *)ip6hdr;
  213. void *txphdr;
  214. u16 *csum;
  215. txphdr = ip6hdr + sizeof(struct ipv6hdr);
  216. if (ip6h->nexthdr == IPPROTO_TCP || ip6h->nexthdr == IPPROTO_UDP) {
  217. csum = (u16 *)rmnet_map_get_csum_field(ip6h->nexthdr, txphdr);
  218. *csum = ~(*csum);
  219. }
  220. }
  221. static void
  222. rmnet_map_ipv6_ul_csum_header(void *ip6hdr,
  223. struct rmnet_map_ul_csum_header *ul_header,
  224. struct sk_buff *skb)
  225. {
  226. struct ipv6hdr *ip6h = (struct ipv6hdr *)ip6hdr;
  227. __be16 *hdr = (__be16 *)ul_header, offset;
  228. offset = htons((__force u16)(skb_transport_header(skb) -
  229. (unsigned char *)ip6hdr));
  230. ul_header->csum_start_offset = offset;
  231. ul_header->csum_insert_offset = skb->csum_offset;
  232. ul_header->csum_enabled = 1;
  233. if (ip6h->nexthdr == IPPROTO_UDP)
  234. ul_header->udp_ind = 1;
  235. else
  236. ul_header->udp_ind = 0;
  237. /* Changing remaining fields to network order */
  238. hdr++;
  239. *hdr = htons((__force u16)*hdr);
  240. skb->ip_summed = CHECKSUM_NONE;
  241. rmnet_map_complement_ipv6_txporthdr_csum_field(ip6hdr);
  242. }
  243. #endif
  244. /* Adds MAP header to front of skb->data
  245. * Padding is calculated and set appropriately in MAP header. Mux ID is
  246. * initialized to 0.
  247. */
  248. struct rmnet_map_header *rmnet_map_add_map_header(struct sk_buff *skb,
  249. int hdrlen, int pad,
  250. struct rmnet_port *port)
  251. {
  252. struct rmnet_map_header *map_header;
  253. u32 padding, map_datalen;
  254. u8 *padbytes;
  255. map_datalen = skb->len - hdrlen;
  256. map_header = (struct rmnet_map_header *)
  257. skb_push(skb, sizeof(struct rmnet_map_header));
  258. memset(map_header, 0, sizeof(struct rmnet_map_header));
  259. /* Set next_hdr bit for csum offload packets */
  260. if (port->data_format & RMNET_PRIV_FLAGS_EGRESS_MAP_CKSUMV5)
  261. map_header->next_hdr = 1;
  262. if (pad == RMNET_MAP_NO_PAD_BYTES) {
  263. map_header->pkt_len = htons(map_datalen);
  264. return map_header;
  265. }
  266. padding = ALIGN(map_datalen, 4) - map_datalen;
  267. if (padding == 0)
  268. goto done;
  269. if (skb_tailroom(skb) < padding)
  270. return NULL;
  271. padbytes = (u8 *)skb_put(skb, padding);
  272. memset(padbytes, 0, padding);
  273. done:
  274. map_header->pkt_len = htons(map_datalen + padding);
  275. map_header->pad_len = padding & 0x3F;
  276. return map_header;
  277. }
  278. /* Deaggregates a single packet
  279. * A whole new buffer is allocated for each portion of an aggregated frame.
  280. * Caller should keep calling deaggregate() on the source skb until 0 is
  281. * returned, indicating that there are no more packets to deaggregate. Caller
  282. * is responsible for freeing the original skb.
  283. */
  284. struct sk_buff *rmnet_map_deaggregate(struct sk_buff *skb,
  285. struct rmnet_port *port)
  286. {
  287. struct rmnet_map_header *maph;
  288. struct sk_buff *skbn;
  289. unsigned char *data = rmnet_map_data_ptr(skb), *next_hdr = NULL;
  290. u32 packet_len;
  291. if (skb->len == 0)
  292. return NULL;
  293. maph = (struct rmnet_map_header *)data;
  294. packet_len = ntohs(maph->pkt_len) + sizeof(struct rmnet_map_header);
  295. if (port->data_format & RMNET_FLAGS_INGRESS_MAP_CKSUMV4)
  296. packet_len += sizeof(struct rmnet_map_dl_csum_trailer);
  297. else if (port->data_format & RMNET_PRIV_FLAGS_INGRESS_MAP_CKSUMV5) {
  298. if (!maph->cd_bit) {
  299. packet_len += sizeof(struct rmnet_map_v5_csum_header);
  300. /* Coalescing headers require MAPv5 */
  301. next_hdr = data + sizeof(*maph);
  302. }
  303. }
  304. if (((int)skb->len - (int)packet_len) < 0)
  305. return NULL;
  306. /* Some hardware can send us empty frames. Catch them */
  307. if (ntohs(maph->pkt_len) == 0)
  308. return NULL;
  309. if (next_hdr &&
  310. ((struct rmnet_map_v5_coal_header *)next_hdr)->header_type ==
  311. RMNET_MAP_HEADER_TYPE_COALESCING)
  312. return skb;
  313. if (skb_is_nonlinear(skb)) {
  314. skb_frag_t *frag0 = skb_shinfo(skb)->frags;
  315. struct page *page = skb_frag_page(frag0);
  316. skbn = alloc_skb(RMNET_MAP_DEAGGR_HEADROOM, GFP_ATOMIC);
  317. if (!skbn)
  318. return NULL;
  319. skb_append_pagefrags(skbn, page, frag0->bv_offset,
  320. packet_len);
  321. skbn->data_len += packet_len;
  322. skbn->len += packet_len;
  323. } else {
  324. skbn = alloc_skb(packet_len + RMNET_MAP_DEAGGR_SPACING,
  325. GFP_ATOMIC);
  326. if (!skbn)
  327. return NULL;
  328. skb_reserve(skbn, RMNET_MAP_DEAGGR_HEADROOM);
  329. skb_put(skbn, packet_len);
  330. memcpy(skbn->data, data, packet_len);
  331. }
  332. skbn->priority = skb->priority;
  333. pskb_pull(skb, packet_len);
  334. return skbn;
  335. }
  336. /* Validates packet checksums. Function takes a pointer to
  337. * the beginning of a buffer which contains the IP payload +
  338. * padding + checksum trailer.
  339. * Only IPv4 and IPv6 are supported along with TCP & UDP.
  340. * Fragmented or tunneled packets are not supported.
  341. */
  342. int rmnet_map_checksum_downlink_packet(struct sk_buff *skb, u16 len)
  343. {
  344. struct rmnet_priv *priv = netdev_priv(skb->dev);
  345. struct rmnet_map_dl_csum_trailer *csum_trailer;
  346. if (unlikely(!(skb->dev->features & NETIF_F_RXCSUM))) {
  347. priv->stats.csum_sw++;
  348. return -EOPNOTSUPP;
  349. }
  350. csum_trailer = (struct rmnet_map_dl_csum_trailer *)
  351. (rmnet_map_data_ptr(skb) + len);
  352. if (!csum_trailer->valid) {
  353. priv->stats.csum_valid_unset++;
  354. return -EINVAL;
  355. }
  356. if (skb->protocol == htons(ETH_P_IP)) {
  357. return rmnet_map_ipv4_dl_csum_trailer(skb, csum_trailer, priv);
  358. } else if (skb->protocol == htons(ETH_P_IPV6)) {
  359. #if IS_ENABLED(CONFIG_IPV6)
  360. return rmnet_map_ipv6_dl_csum_trailer(skb, csum_trailer, priv);
  361. #else
  362. priv->stats.csum_err_invalid_ip_version++;
  363. return -EPROTONOSUPPORT;
  364. #endif
  365. } else {
  366. priv->stats.csum_err_invalid_ip_version++;
  367. return -EPROTONOSUPPORT;
  368. }
  369. return 0;
  370. }
  371. EXPORT_SYMBOL(rmnet_map_checksum_downlink_packet);
  372. void rmnet_map_v4_checksum_uplink_packet(struct sk_buff *skb,
  373. struct net_device *orig_dev)
  374. {
  375. struct rmnet_priv *priv = netdev_priv(orig_dev);
  376. struct rmnet_map_ul_csum_header *ul_header;
  377. void *iphdr;
  378. ul_header = (struct rmnet_map_ul_csum_header *)
  379. skb_push(skb, sizeof(struct rmnet_map_ul_csum_header));
  380. if (unlikely(!(orig_dev->features &
  381. (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))))
  382. goto sw_csum;
  383. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  384. iphdr = (char *)ul_header +
  385. sizeof(struct rmnet_map_ul_csum_header);
  386. if (skb->protocol == htons(ETH_P_IP)) {
  387. rmnet_map_ipv4_ul_csum_header(iphdr, ul_header, skb);
  388. priv->stats.csum_hw++;
  389. return;
  390. } else if (skb->protocol == htons(ETH_P_IPV6)) {
  391. #if IS_ENABLED(CONFIG_IPV6)
  392. rmnet_map_ipv6_ul_csum_header(iphdr, ul_header, skb);
  393. priv->stats.csum_hw++;
  394. return;
  395. #else
  396. priv->stats.csum_err_invalid_ip_version++;
  397. goto sw_csum;
  398. #endif
  399. } else {
  400. priv->stats.csum_err_invalid_ip_version++;
  401. }
  402. }
  403. sw_csum:
  404. ul_header->csum_start_offset = 0;
  405. ul_header->csum_insert_offset = 0;
  406. ul_header->csum_enabled = 0;
  407. ul_header->udp_ind = 0;
  408. priv->stats.csum_sw++;
  409. }
  410. static void rmnet_map_v5_check_priority(struct sk_buff *skb,
  411. struct net_device *orig_dev,
  412. struct rmnet_map_v5_csum_header *hdr,
  413. bool tso)
  414. {
  415. struct rmnet_priv *priv = netdev_priv(orig_dev);
  416. if (RMNET_LLM(skb->priority)) {
  417. priv->stats.ul_prio++;
  418. hdr->priority = 1;
  419. }
  420. /* APS priority bit is only valid for csum header */
  421. if (!tso && RMNET_APS_LLB(skb->priority)) {
  422. priv->stats.aps_prio++;
  423. hdr->aps_prio = 1;
  424. }
  425. }
  426. void rmnet_map_v5_checksum_uplink_packet(struct sk_buff *skb,
  427. struct rmnet_port *port,
  428. struct net_device *orig_dev)
  429. {
  430. struct rmnet_priv *priv = netdev_priv(orig_dev);
  431. struct rmnet_map_v5_csum_header *ul_header;
  432. ul_header = (struct rmnet_map_v5_csum_header *)
  433. skb_push(skb, sizeof(*ul_header));
  434. memset(ul_header, 0, sizeof(*ul_header));
  435. ul_header->header_type = RMNET_MAP_HEADER_TYPE_CSUM_OFFLOAD;
  436. if (port->data_format & RMNET_EGRESS_FORMAT_PRIORITY)
  437. rmnet_map_v5_check_priority(skb, orig_dev, ul_header, false);
  438. /* Allow priority w/o csum offload */
  439. if (!(port->data_format & RMNET_PRIV_FLAGS_EGRESS_MAP_CKSUMV5))
  440. return;
  441. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  442. void *iph = (char *)ul_header + sizeof(*ul_header);
  443. void *trans;
  444. __sum16 *check;
  445. u8 proto;
  446. if (skb->protocol == htons(ETH_P_IP)) {
  447. u16 ip_len = ((struct iphdr *)iph)->ihl * 4;
  448. proto = ((struct iphdr *)iph)->protocol;
  449. trans = iph + ip_len;
  450. } else if (skb->protocol == htons(ETH_P_IPV6)) {
  451. u16 ip_len = sizeof(struct ipv6hdr);
  452. proto = ((struct ipv6hdr *)iph)->nexthdr;
  453. trans = iph + ip_len;
  454. } else {
  455. priv->stats.csum_err_invalid_ip_version++;
  456. goto sw_csum;
  457. }
  458. check = rmnet_map_get_csum_field(proto, trans);
  459. if (check) {
  460. skb->ip_summed = CHECKSUM_NONE;
  461. /* Ask for checksum offloading */
  462. ul_header->csum_valid_required = 1;
  463. priv->stats.csum_hw++;
  464. return;
  465. }
  466. }
  467. sw_csum:
  468. priv->stats.csum_sw++;
  469. }
  470. /* Generates UL checksum meta info header for IPv4 and IPv6 over TCP and UDP
  471. * packets that are supported for UL checksum offload.
  472. */
  473. void rmnet_map_checksum_uplink_packet(struct sk_buff *skb,
  474. struct rmnet_port *port,
  475. struct net_device *orig_dev,
  476. int csum_type)
  477. {
  478. switch (csum_type) {
  479. case RMNET_FLAGS_EGRESS_MAP_CKSUMV4:
  480. rmnet_map_v4_checksum_uplink_packet(skb, orig_dev);
  481. break;
  482. case RMNET_PRIV_FLAGS_EGRESS_MAP_CKSUMV5:
  483. rmnet_map_v5_checksum_uplink_packet(skb, port, orig_dev);
  484. break;
  485. default:
  486. break;
  487. }
  488. }
  489. bool rmnet_map_v5_csum_buggy(struct rmnet_map_v5_coal_header *coal_hdr)
  490. {
  491. /* Only applies to frames with a single packet */
  492. if (coal_hdr->num_nlos != 1 || coal_hdr->nl_pairs[0].num_packets != 1)
  493. return false;
  494. /* TCP header has FIN or PUSH set */
  495. if (coal_hdr->close_type == RMNET_MAP_COAL_CLOSE_COAL)
  496. return true;
  497. /* Hit packet limit, byte limit, or time limit/EOF on DMA */
  498. if (coal_hdr->close_type == RMNET_MAP_COAL_CLOSE_HW) {
  499. switch (coal_hdr->close_value) {
  500. case RMNET_MAP_COAL_CLOSE_HW_PKT:
  501. case RMNET_MAP_COAL_CLOSE_HW_BYTE:
  502. case RMNET_MAP_COAL_CLOSE_HW_TIME:
  503. return true;
  504. }
  505. }
  506. return false;
  507. }
  508. static void rmnet_map_move_headers(struct sk_buff *skb)
  509. {
  510. struct iphdr *iph;
  511. u16 ip_len;
  512. u16 trans_len = 0;
  513. u8 proto;
  514. /* This only applies to non-linear SKBs */
  515. if (!skb_is_nonlinear(skb))
  516. return;
  517. iph = (struct iphdr *)rmnet_map_data_ptr(skb);
  518. if (iph->version == 4) {
  519. ip_len = iph->ihl * 4;
  520. proto = iph->protocol;
  521. if (iph->frag_off & htons(IP_OFFSET))
  522. /* No transport header information */
  523. goto pull;
  524. } else if (iph->version == 6) {
  525. struct ipv6hdr *ip6h = (struct ipv6hdr *)iph;
  526. __be16 frag_off;
  527. u8 nexthdr = ip6h->nexthdr;
  528. ip_len = ipv6_skip_exthdr(skb, sizeof(*ip6h), &nexthdr,
  529. &frag_off);
  530. if (ip_len < 0)
  531. return;
  532. proto = nexthdr;
  533. } else {
  534. return;
  535. }
  536. if (proto == IPPROTO_TCP) {
  537. struct tcphdr *tp = (struct tcphdr *)((u8 *)iph + ip_len);
  538. trans_len = tp->doff * 4;
  539. } else if (proto == IPPROTO_UDP) {
  540. trans_len = sizeof(struct udphdr);
  541. } else if (proto == NEXTHDR_FRAGMENT) {
  542. /* Non-first fragments don't have the fragment length added by
  543. * ipv6_skip_exthdr() and sho up as proto NEXTHDR_FRAGMENT, so
  544. * we account for the length here.
  545. */
  546. ip_len += sizeof(struct frag_hdr);
  547. }
  548. pull:
  549. __pskb_pull_tail(skb, ip_len + trans_len);
  550. skb_reset_network_header(skb);
  551. if (trans_len)
  552. skb_set_transport_header(skb, ip_len);
  553. }
  554. static void rmnet_map_nonlinear_copy(struct sk_buff *coal_skb,
  555. struct rmnet_map_coal_metadata *coal_meta,
  556. struct sk_buff *dest)
  557. {
  558. unsigned char *data_start = rmnet_map_data_ptr(coal_skb) +
  559. coal_meta->ip_len + coal_meta->trans_len;
  560. u32 copy_len = coal_meta->data_len * coal_meta->pkt_count;
  561. if (skb_is_nonlinear(coal_skb)) {
  562. skb_frag_t *frag0 = skb_shinfo(coal_skb)->frags;
  563. struct page *page = skb_frag_page(frag0);
  564. skb_append_pagefrags(dest, page,
  565. frag0->bv_offset + coal_meta->ip_len +
  566. coal_meta->trans_len +
  567. coal_meta->data_offset,
  568. copy_len);
  569. dest->data_len += copy_len;
  570. dest->len += copy_len;
  571. } else {
  572. skb_put_data(dest, data_start + coal_meta->data_offset,
  573. copy_len);
  574. }
  575. }
  576. /* Fill in GSO metadata to allow the SKB to be segmented by the NW stack
  577. * if needed (i.e. forwarding, UDP GRO)
  578. */
  579. static void rmnet_map_gso_stamp(struct sk_buff *skb,
  580. struct rmnet_map_coal_metadata *coal_meta)
  581. {
  582. struct skb_shared_info *shinfo = skb_shinfo(skb);
  583. if (coal_meta->trans_proto == IPPROTO_TCP)
  584. shinfo->gso_type = (coal_meta->ip_proto == 4) ?
  585. SKB_GSO_TCPV4 : SKB_GSO_TCPV6;
  586. else
  587. shinfo->gso_type = SKB_GSO_UDP_L4;
  588. shinfo->gso_size = coal_meta->data_len;
  589. shinfo->gso_segs = coal_meta->pkt_count;
  590. }
  591. /* Handles setting up the partial checksum in the skb. Sets the transport
  592. * checksum to the pseudoheader checksum and sets the csum offload metadata
  593. */
  594. static void rmnet_map_partial_csum(struct sk_buff *skb,
  595. struct rmnet_map_coal_metadata *coal_meta)
  596. {
  597. unsigned char *data = skb->data;
  598. __sum16 pseudo;
  599. u16 pkt_len = skb->len - coal_meta->ip_len;
  600. if (coal_meta->ip_proto == 4) {
  601. struct iphdr *iph = (struct iphdr *)data;
  602. pseudo = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
  603. pkt_len, coal_meta->trans_proto,
  604. 0);
  605. } else {
  606. struct ipv6hdr *ip6h = (struct ipv6hdr *)data;
  607. pseudo = ~csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
  608. pkt_len, coal_meta->trans_proto, 0);
  609. }
  610. if (coal_meta->trans_proto == IPPROTO_TCP) {
  611. struct tcphdr *tp = (struct tcphdr *)(data + coal_meta->ip_len);
  612. tp->check = pseudo;
  613. skb->csum_offset = offsetof(struct tcphdr, check);
  614. } else {
  615. struct udphdr *up = (struct udphdr *)(data + coal_meta->ip_len);
  616. up->check = pseudo;
  617. skb->csum_offset = offsetof(struct udphdr, check);
  618. }
  619. skb->ip_summed = CHECKSUM_PARTIAL;
  620. skb->csum_start = skb->data + coal_meta->ip_len - skb->head;
  621. }
  622. static void
  623. __rmnet_map_segment_coal_skb(struct sk_buff *coal_skb,
  624. struct rmnet_map_coal_metadata *coal_meta,
  625. struct sk_buff_head *list, u8 pkt_id,
  626. bool csum_valid)
  627. {
  628. struct sk_buff *skbn;
  629. struct rmnet_priv *priv = netdev_priv(coal_skb->dev);
  630. __sum16 *check = NULL;
  631. u32 alloc_len;
  632. u32 dlen = coal_meta->data_len * coal_meta->pkt_count;
  633. u32 hlen = coal_meta->ip_len + coal_meta->trans_len;
  634. bool zero_csum = false;
  635. /* We can avoid copying the data if the SKB we got from the lower-level
  636. * drivers was nonlinear.
  637. */
  638. if (skb_is_nonlinear(coal_skb))
  639. alloc_len = hlen;
  640. else
  641. alloc_len = hlen + dlen;
  642. skbn = alloc_skb(alloc_len, GFP_ATOMIC);
  643. if (!skbn)
  644. return;
  645. skb_reserve(skbn, hlen);
  646. rmnet_map_nonlinear_copy(coal_skb, coal_meta, skbn);
  647. /* Push transport header and update necessary fields */
  648. skb_push(skbn, coal_meta->trans_len);
  649. memcpy(skbn->data, coal_meta->trans_header, coal_meta->trans_len);
  650. skb_reset_transport_header(skbn);
  651. if (coal_meta->trans_proto == IPPROTO_TCP) {
  652. struct tcphdr *th = tcp_hdr(skbn);
  653. th->seq = htonl(ntohl(th->seq) + coal_meta->data_offset);
  654. check = &th->check;
  655. /* Don't allow dangerous flags to be set in any segment but the
  656. * last one.
  657. */
  658. if (th->fin || th->psh) {
  659. if (hlen + coal_meta->data_offset + dlen <
  660. coal_skb->len) {
  661. th->fin = 0;
  662. th->psh = 0;
  663. }
  664. }
  665. } else if (coal_meta->trans_proto == IPPROTO_UDP) {
  666. struct udphdr *uh = udp_hdr(skbn);
  667. uh->len = htons(skbn->len);
  668. check = &uh->check;
  669. if (coal_meta->ip_proto == 4 && !uh->check)
  670. zero_csum = true;
  671. }
  672. /* Push IP header and update necessary fields */
  673. skb_push(skbn, coal_meta->ip_len);
  674. memcpy(skbn->data, coal_meta->ip_header, coal_meta->ip_len);
  675. skb_reset_network_header(skbn);
  676. if (coal_meta->ip_proto == 4) {
  677. struct iphdr *iph = ip_hdr(skbn);
  678. iph->id = htons(ntohs(iph->id) + coal_meta->pkt_id);
  679. iph->tot_len = htons(skbn->len);
  680. iph->check = 0;
  681. iph->check = ip_fast_csum(iph, iph->ihl);
  682. } else {
  683. /* Payload length includes any extension headers */
  684. ipv6_hdr(skbn)->payload_len = htons(skbn->len -
  685. sizeof(struct ipv6hdr));
  686. }
  687. /* Handle checksum status */
  688. if (likely(csum_valid) || zero_csum) {
  689. /* Set the partial checksum information */
  690. rmnet_map_partial_csum(skbn, coal_meta);
  691. } else if (check) {
  692. /* Unfortunately, we have to fake a bad checksum here, since
  693. * the original bad value is lost by the hardware. The only
  694. * reliable way to do it is to calculate the actual checksum
  695. * and corrupt it.
  696. */
  697. __wsum csum;
  698. unsigned int offset = skb_transport_offset(skbn);
  699. __sum16 pseudo;
  700. /* Calculate pseudo header */
  701. if (coal_meta->ip_proto == 4) {
  702. struct iphdr *iph = ip_hdr(skbn);
  703. pseudo = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
  704. skbn->len -
  705. coal_meta->ip_len,
  706. coal_meta->trans_proto, 0);
  707. } else {
  708. struct ipv6hdr *ip6h = ipv6_hdr(skbn);
  709. pseudo = ~csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
  710. skbn->len - coal_meta->ip_len,
  711. coal_meta->trans_proto, 0);
  712. }
  713. *check = pseudo;
  714. csum = skb_checksum(skbn, offset, skbn->len - offset, 0);
  715. /* Add 1 to corrupt. This cannot produce a final value of 0
  716. * since csum_fold() can't return a value of 0xFFFF.
  717. */
  718. *check = csum16_add(csum_fold(csum), htons(1));
  719. skbn->ip_summed = CHECKSUM_NONE;
  720. }
  721. skbn->dev = coal_skb->dev;
  722. priv->stats.coal.coal_reconstruct++;
  723. /* Stamp GSO information if necessary */
  724. if (coal_meta->pkt_count > 1)
  725. rmnet_map_gso_stamp(skbn, coal_meta);
  726. /* Propagate priority value */
  727. skbn->priority = coal_skb->priority;
  728. __skb_queue_tail(list, skbn);
  729. /* Update meta information to move past the data we just segmented */
  730. coal_meta->data_offset += dlen;
  731. coal_meta->pkt_id = pkt_id + 1;
  732. coal_meta->pkt_count = 0;
  733. }
  734. static bool rmnet_map_validate_csum(struct sk_buff *skb,
  735. struct rmnet_map_coal_metadata *meta)
  736. {
  737. u8 *data = rmnet_map_data_ptr(skb);
  738. unsigned int datagram_len;
  739. __wsum csum;
  740. __sum16 pseudo;
  741. datagram_len = skb->len - meta->ip_len;
  742. if (meta->ip_proto == 4) {
  743. struct iphdr *iph = (struct iphdr *)data;
  744. pseudo = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
  745. datagram_len,
  746. meta->trans_proto, 0);
  747. } else {
  748. struct ipv6hdr *ip6h = (struct ipv6hdr *)data;
  749. pseudo = ~csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
  750. datagram_len, meta->trans_proto,
  751. 0);
  752. }
  753. csum = skb_checksum(skb, meta->ip_len, datagram_len,
  754. csum_unfold(pseudo));
  755. return !csum_fold(csum);
  756. }
  757. /* Converts the coalesced SKB into a list of SKBs.
  758. * NLOs containing csum erros will not be included.
  759. * The original coalesced SKB should be treated as invalid and
  760. * must be freed by the caller
  761. */
  762. static void rmnet_map_segment_coal_skb(struct sk_buff *coal_skb,
  763. u64 nlo_err_mask,
  764. struct sk_buff_head *list)
  765. {
  766. struct iphdr *iph;
  767. struct rmnet_priv *priv = netdev_priv(coal_skb->dev);
  768. struct rmnet_map_v5_coal_header *coal_hdr;
  769. struct rmnet_map_coal_metadata coal_meta;
  770. u16 pkt_len;
  771. u8 pkt, total_pkt = 0;
  772. u8 nlo;
  773. bool gro = coal_skb->dev->features & NETIF_F_GRO_HW;
  774. bool zero_csum = false;
  775. memset(&coal_meta, 0, sizeof(coal_meta));
  776. /* Pull off the headers we no longer need */
  777. pskb_pull(coal_skb, sizeof(struct rmnet_map_header));
  778. coal_hdr = (struct rmnet_map_v5_coal_header *)
  779. rmnet_map_data_ptr(coal_skb);
  780. pskb_pull(coal_skb, sizeof(*coal_hdr));
  781. iph = (struct iphdr *)rmnet_map_data_ptr(coal_skb);
  782. if (iph->version == 4) {
  783. coal_meta.ip_proto = 4;
  784. coal_meta.ip_len = iph->ihl * 4;
  785. coal_meta.trans_proto = iph->protocol;
  786. coal_meta.ip_header = iph;
  787. /* Don't allow coalescing of any packets with IP options */
  788. if (iph->ihl != 5)
  789. gro = false;
  790. } else if (iph->version == 6) {
  791. struct ipv6hdr *ip6h = (struct ipv6hdr *)iph;
  792. __be16 frag_off;
  793. u8 protocol = ip6h->nexthdr;
  794. coal_meta.ip_proto = 6;
  795. coal_meta.ip_len = ipv6_skip_exthdr(coal_skb, sizeof(*ip6h),
  796. &protocol, &frag_off);
  797. coal_meta.trans_proto = protocol;
  798. coal_meta.ip_header = ip6h;
  799. /* If we run into a problem, or this has a fragment header
  800. * (which should technically not be possible, if the HW
  801. * works as intended...), bail.
  802. */
  803. if (coal_meta.ip_len < 0 || frag_off) {
  804. priv->stats.coal.coal_ip_invalid++;
  805. return;
  806. } else if (coal_meta.ip_len > sizeof(*ip6h)) {
  807. /* Don't allow coalescing of any packets with IPv6
  808. * extension headers.
  809. */
  810. gro = false;
  811. }
  812. } else {
  813. priv->stats.coal.coal_ip_invalid++;
  814. return;
  815. }
  816. if (coal_meta.trans_proto == IPPROTO_TCP) {
  817. struct tcphdr *th;
  818. th = (struct tcphdr *)((u8 *)iph + coal_meta.ip_len);
  819. coal_meta.trans_len = th->doff * 4;
  820. coal_meta.trans_header = th;
  821. } else if (coal_meta.trans_proto == IPPROTO_UDP) {
  822. struct udphdr *uh;
  823. uh = (struct udphdr *)((u8 *)iph + coal_meta.ip_len);
  824. coal_meta.trans_len = sizeof(*uh);
  825. coal_meta.trans_header = uh;
  826. /* Check for v4 zero checksum */
  827. if (coal_meta.ip_proto == 4 && !uh->check)
  828. zero_csum = true;
  829. } else {
  830. priv->stats.coal.coal_trans_invalid++;
  831. return;
  832. }
  833. if (rmnet_map_v5_csum_buggy(coal_hdr) && !zero_csum) {
  834. rmnet_map_move_headers(coal_skb);
  835. /* Mark as valid if it checks out */
  836. if (rmnet_map_validate_csum(coal_skb, &coal_meta))
  837. coal_skb->ip_summed = CHECKSUM_UNNECESSARY;
  838. __skb_queue_tail(list, coal_skb);
  839. return;
  840. }
  841. /* Fast-forward the case where we have 1 NLO (i.e. 1 packet length),
  842. * no checksum errors, and are allowing GRO. We can just reuse this
  843. * SKB unchanged.
  844. */
  845. if (gro && coal_hdr->num_nlos == 1 && coal_hdr->csum_valid) {
  846. rmnet_map_move_headers(coal_skb);
  847. coal_skb->ip_summed = CHECKSUM_UNNECESSARY;
  848. coal_meta.data_len = ntohs(coal_hdr->nl_pairs[0].pkt_len);
  849. coal_meta.data_len -= coal_meta.ip_len + coal_meta.trans_len;
  850. coal_meta.pkt_count = coal_hdr->nl_pairs[0].num_packets;
  851. if (coal_meta.pkt_count > 1) {
  852. rmnet_map_partial_csum(coal_skb, &coal_meta);
  853. rmnet_map_gso_stamp(coal_skb, &coal_meta);
  854. }
  855. __skb_queue_tail(list, coal_skb);
  856. return;
  857. }
  858. /* Segment the coalesced SKB into new packets */
  859. for (nlo = 0; nlo < coal_hdr->num_nlos; nlo++) {
  860. pkt_len = ntohs(coal_hdr->nl_pairs[nlo].pkt_len);
  861. pkt_len -= coal_meta.ip_len + coal_meta.trans_len;
  862. coal_meta.data_len = pkt_len;
  863. for (pkt = 0; pkt < coal_hdr->nl_pairs[nlo].num_packets;
  864. pkt++, total_pkt++, nlo_err_mask >>= 1) {
  865. bool csum_err = nlo_err_mask & 1;
  866. /* Segment the packet if we're not sending the larger
  867. * packet up the stack.
  868. */
  869. if (!gro) {
  870. coal_meta.pkt_count = 1;
  871. if (csum_err)
  872. priv->stats.coal.coal_csum_err++;
  873. __rmnet_map_segment_coal_skb(coal_skb,
  874. &coal_meta, list,
  875. total_pkt,
  876. !csum_err);
  877. continue;
  878. }
  879. if (csum_err) {
  880. priv->stats.coal.coal_csum_err++;
  881. /* Segment out the good data */
  882. if (gro && coal_meta.pkt_count)
  883. __rmnet_map_segment_coal_skb(coal_skb,
  884. &coal_meta,
  885. list,
  886. total_pkt,
  887. true);
  888. /* Segment out the bad checksum */
  889. coal_meta.pkt_count = 1;
  890. __rmnet_map_segment_coal_skb(coal_skb,
  891. &coal_meta, list,
  892. total_pkt, false);
  893. } else {
  894. coal_meta.pkt_count++;
  895. }
  896. }
  897. /* If we're switching NLOs, we need to send out everything from
  898. * the previous one, if we haven't done so. NLOs only switch
  899. * when the packet length changes.
  900. */
  901. if (coal_meta.pkt_count)
  902. __rmnet_map_segment_coal_skb(coal_skb, &coal_meta, list,
  903. total_pkt, true);
  904. }
  905. }
  906. /* Record reason for coalescing pipe closure */
  907. static void rmnet_map_data_log_close_stats(struct rmnet_priv *priv, u8 type,
  908. u8 code)
  909. {
  910. struct rmnet_coal_close_stats *stats = &priv->stats.coal.close;
  911. switch (type) {
  912. case RMNET_MAP_COAL_CLOSE_NON_COAL:
  913. stats->non_coal++;
  914. break;
  915. case RMNET_MAP_COAL_CLOSE_IP_MISS:
  916. stats->ip_miss++;
  917. break;
  918. case RMNET_MAP_COAL_CLOSE_TRANS_MISS:
  919. stats->trans_miss++;
  920. break;
  921. case RMNET_MAP_COAL_CLOSE_HW:
  922. switch (code) {
  923. case RMNET_MAP_COAL_CLOSE_HW_NL:
  924. stats->hw_nl++;
  925. break;
  926. case RMNET_MAP_COAL_CLOSE_HW_PKT:
  927. stats->hw_pkt++;
  928. break;
  929. case RMNET_MAP_COAL_CLOSE_HW_BYTE:
  930. stats->hw_byte++;
  931. break;
  932. case RMNET_MAP_COAL_CLOSE_HW_TIME:
  933. stats->hw_time++;
  934. break;
  935. case RMNET_MAP_COAL_CLOSE_HW_EVICT:
  936. stats->hw_evict++;
  937. break;
  938. default:
  939. break;
  940. }
  941. break;
  942. case RMNET_MAP_COAL_CLOSE_COAL:
  943. stats->coal++;
  944. break;
  945. default:
  946. break;
  947. }
  948. }
  949. /* Check if the coalesced header has any incorrect values, in which case, the
  950. * entire coalesced skb must be dropped. Then check if there are any
  951. * checksum issues
  952. */
  953. static int rmnet_map_data_check_coal_header(struct sk_buff *skb,
  954. u64 *nlo_err_mask)
  955. {
  956. struct rmnet_map_v5_coal_header *coal_hdr;
  957. unsigned char *data = rmnet_map_data_ptr(skb);
  958. struct rmnet_priv *priv = netdev_priv(skb->dev);
  959. u64 mask = 0;
  960. int i;
  961. u8 veid, pkts = 0;
  962. coal_hdr = ((struct rmnet_map_v5_coal_header *)
  963. (data + sizeof(struct rmnet_map_header)));
  964. veid = coal_hdr->virtual_channel_id;
  965. if (coal_hdr->num_nlos == 0 ||
  966. coal_hdr->num_nlos > RMNET_MAP_V5_MAX_NLOS) {
  967. priv->stats.coal.coal_hdr_nlo_err++;
  968. return -EINVAL;
  969. }
  970. for (i = 0; i < RMNET_MAP_V5_MAX_NLOS; i++) {
  971. /* If there is a checksum issue, we need to split
  972. * up the skb. Rebuild the full csum error field
  973. */
  974. u8 err = coal_hdr->nl_pairs[i].csum_error_bitmap;
  975. u8 pkt = coal_hdr->nl_pairs[i].num_packets;
  976. mask |= ((u64)err) << (8 * i);
  977. /* Track total packets in frame */
  978. pkts += pkt;
  979. if (pkts > RMNET_MAP_V5_MAX_PACKETS) {
  980. priv->stats.coal.coal_hdr_pkt_err++;
  981. return -EINVAL;
  982. }
  983. }
  984. /* Track number of packets we get inside of coalesced frames */
  985. priv->stats.coal.coal_pkts += pkts;
  986. /* Update ethtool stats */
  987. rmnet_map_data_log_close_stats(priv,
  988. coal_hdr->close_type,
  989. coal_hdr->close_value);
  990. if (veid < RMNET_MAX_VEID)
  991. priv->stats.coal.coal_veid[veid]++;
  992. *nlo_err_mask = mask;
  993. return 0;
  994. }
  995. /* Process a QMAPv5 packet header */
  996. int rmnet_map_process_next_hdr_packet(struct sk_buff *skb,
  997. struct sk_buff_head *list,
  998. u16 len)
  999. {
  1000. struct rmnet_priv *priv = netdev_priv(skb->dev);
  1001. u64 nlo_err_mask;
  1002. int rc = 0;
  1003. switch (rmnet_map_get_next_hdr_type(skb)) {
  1004. case RMNET_MAP_HEADER_TYPE_COALESCING:
  1005. priv->stats.coal.coal_rx++;
  1006. rc = rmnet_map_data_check_coal_header(skb, &nlo_err_mask);
  1007. if (rc)
  1008. return rc;
  1009. rmnet_map_segment_coal_skb(skb, nlo_err_mask, list);
  1010. if (skb_peek(list) != skb)
  1011. consume_skb(skb);
  1012. break;
  1013. case RMNET_MAP_HEADER_TYPE_CSUM_OFFLOAD:
  1014. if (unlikely(!(skb->dev->features & NETIF_F_RXCSUM))) {
  1015. priv->stats.csum_sw++;
  1016. } else if (rmnet_map_get_csum_valid(skb)) {
  1017. priv->stats.csum_ok++;
  1018. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1019. } else {
  1020. priv->stats.csum_valid_unset++;
  1021. }
  1022. /* Pull unnecessary headers and move the rest to the linear
  1023. * section of the skb.
  1024. */
  1025. pskb_pull(skb,
  1026. (sizeof(struct rmnet_map_header) +
  1027. sizeof(struct rmnet_map_v5_csum_header)));
  1028. rmnet_map_move_headers(skb);
  1029. /* Remove padding only for csum offload packets.
  1030. * Coalesced packets should never have padding.
  1031. */
  1032. pskb_trim(skb, len);
  1033. __skb_queue_tail(list, skb);
  1034. break;
  1035. default:
  1036. rc = -EINVAL;
  1037. break;
  1038. }
  1039. return rc;
  1040. }
  1041. long rmnet_agg_time_limit __read_mostly = 1000000L;
  1042. long rmnet_agg_bypass_time __read_mostly = 10000000L;
  1043. int rmnet_map_tx_agg_skip(struct sk_buff *skb, int offset)
  1044. {
  1045. u8 *packet_start = skb->data + offset;
  1046. int is_icmp = 0;
  1047. if (skb->protocol == htons(ETH_P_IP)) {
  1048. struct iphdr *ip4h = (struct iphdr *)(packet_start);
  1049. if (ip4h->protocol == IPPROTO_ICMP)
  1050. is_icmp = 1;
  1051. } else if (skb->protocol == htons(ETH_P_IPV6)) {
  1052. struct ipv6hdr *ip6h = (struct ipv6hdr *)(packet_start);
  1053. if (ip6h->nexthdr == IPPROTO_ICMPV6) {
  1054. is_icmp = 1;
  1055. } else if (ip6h->nexthdr == NEXTHDR_FRAGMENT) {
  1056. struct frag_hdr *frag;
  1057. frag = (struct frag_hdr *)(packet_start
  1058. + sizeof(struct ipv6hdr));
  1059. if (frag->nexthdr == IPPROTO_ICMPV6)
  1060. is_icmp = 1;
  1061. }
  1062. }
  1063. return is_icmp;
  1064. }
  1065. static void rmnet_map_flush_tx_packet_work(struct work_struct *work)
  1066. {
  1067. struct sk_buff *skb = NULL;
  1068. struct rmnet_aggregation_state *state;
  1069. unsigned long flags;
  1070. state = container_of(work, struct rmnet_aggregation_state, agg_wq);
  1071. spin_lock_irqsave(&state->agg_lock, flags);
  1072. if (likely(state->agg_state == -EINPROGRESS)) {
  1073. /* Buffer may have already been shipped out */
  1074. if (likely(state->agg_skb)) {
  1075. skb = state->agg_skb;
  1076. state->agg_skb = NULL;
  1077. state->agg_count = 0;
  1078. memset(&state->agg_time, 0, sizeof(state->agg_time));
  1079. }
  1080. state->agg_state = 0;
  1081. }
  1082. spin_unlock_irqrestore(&state->agg_lock, flags);
  1083. if (skb)
  1084. state->send_agg_skb(skb);
  1085. }
  1086. enum hrtimer_restart rmnet_map_flush_tx_packet_queue(struct hrtimer *t)
  1087. {
  1088. struct rmnet_aggregation_state *state;
  1089. state = container_of(t, struct rmnet_aggregation_state, hrtimer);
  1090. schedule_work(&state->agg_wq);
  1091. return HRTIMER_NORESTART;
  1092. }
  1093. static void rmnet_map_linearize_copy(struct sk_buff *dst, struct sk_buff *src)
  1094. {
  1095. unsigned int linear = src->len - src->data_len, target = src->len;
  1096. unsigned char *src_buf;
  1097. struct sk_buff *skb;
  1098. src_buf = src->data;
  1099. skb_put_data(dst, src_buf, linear);
  1100. target -= linear;
  1101. skb = src;
  1102. while (target) {
  1103. unsigned int i = 0, non_linear = 0;
  1104. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1105. non_linear = skb_frag_size(&skb_shinfo(skb)->frags[i]);
  1106. src_buf = skb_frag_address(&skb_shinfo(skb)->frags[i]);
  1107. skb_put_data(dst, src_buf, non_linear);
  1108. target -= non_linear;
  1109. }
  1110. if (skb_shinfo(skb)->frag_list) {
  1111. skb = skb_shinfo(skb)->frag_list;
  1112. continue;
  1113. }
  1114. if (skb->next)
  1115. skb = skb->next;
  1116. }
  1117. }
  1118. static void rmnet_free_agg_pages(struct rmnet_aggregation_state *state)
  1119. {
  1120. struct rmnet_agg_page *agg_page, *idx;
  1121. list_for_each_entry_safe(agg_page, idx, &state->agg_list, list) {
  1122. list_del(&agg_page->list);
  1123. put_page(agg_page->page);
  1124. kfree(agg_page);
  1125. }
  1126. state->agg_head = NULL;
  1127. }
  1128. static struct page *rmnet_get_agg_pages(struct rmnet_aggregation_state *state)
  1129. {
  1130. struct rmnet_agg_page *agg_page;
  1131. struct page *page = NULL;
  1132. int i = 0;
  1133. if (!(state->params.agg_features & RMNET_PAGE_RECYCLE))
  1134. goto alloc;
  1135. do {
  1136. agg_page = state->agg_head;
  1137. if (unlikely(!agg_page))
  1138. break;
  1139. if (page_ref_count(agg_page->page) == 1) {
  1140. page = agg_page->page;
  1141. page_ref_inc(agg_page->page);
  1142. state->stats->ul_agg_reuse++;
  1143. state->agg_head = list_next_entry(agg_page, list);
  1144. break;
  1145. }
  1146. state->agg_head = list_next_entry(agg_page, list);
  1147. i++;
  1148. } while (i <= 5);
  1149. alloc:
  1150. if (!page) {
  1151. page = __dev_alloc_pages(GFP_ATOMIC, state->agg_size_order);
  1152. state->stats->ul_agg_alloc++;
  1153. }
  1154. return page;
  1155. }
  1156. static struct rmnet_agg_page *
  1157. __rmnet_alloc_agg_pages(struct rmnet_aggregation_state *state)
  1158. {
  1159. struct rmnet_agg_page *agg_page;
  1160. struct page *page;
  1161. agg_page = kzalloc(sizeof(*agg_page), GFP_ATOMIC);
  1162. if (!agg_page)
  1163. return NULL;
  1164. page = __dev_alloc_pages(GFP_ATOMIC, state->agg_size_order);
  1165. if (!page) {
  1166. kfree(agg_page);
  1167. return NULL;
  1168. }
  1169. agg_page->page = page;
  1170. INIT_LIST_HEAD(&agg_page->list);
  1171. return agg_page;
  1172. }
  1173. static void rmnet_alloc_agg_pages(struct rmnet_aggregation_state *state)
  1174. {
  1175. struct rmnet_agg_page *agg_page = NULL;
  1176. int i = 0;
  1177. for (i = 0; i < 512; i++) {
  1178. agg_page = __rmnet_alloc_agg_pages(state);
  1179. if (agg_page)
  1180. list_add_tail(&agg_page->list, &state->agg_list);
  1181. }
  1182. state->agg_head = list_first_entry_or_null(&state->agg_list,
  1183. struct rmnet_agg_page, list);
  1184. }
  1185. static struct sk_buff *
  1186. rmnet_map_build_skb(struct rmnet_aggregation_state *state)
  1187. {
  1188. struct sk_buff *skb;
  1189. unsigned int size;
  1190. struct page *page;
  1191. void *vaddr;
  1192. page = rmnet_get_agg_pages(state);
  1193. if (!page)
  1194. return NULL;
  1195. vaddr = page_address(page);
  1196. size = PAGE_SIZE << state->agg_size_order;
  1197. skb = build_skb(vaddr, size);
  1198. if (!skb) {
  1199. put_page(page);
  1200. return NULL;
  1201. }
  1202. return skb;
  1203. }
  1204. void rmnet_map_send_agg_skb(struct rmnet_aggregation_state *state,
  1205. unsigned long flags)
  1206. {
  1207. struct sk_buff *agg_skb;
  1208. if (!state->agg_skb) {
  1209. spin_unlock_irqrestore(&state->agg_lock, flags);
  1210. return;
  1211. }
  1212. agg_skb = state->agg_skb;
  1213. /* Reset the aggregation state */
  1214. state->agg_skb = NULL;
  1215. state->agg_count = 0;
  1216. memset(&state->agg_time, 0, sizeof(state->agg_time));
  1217. state->agg_state = 0;
  1218. spin_unlock_irqrestore(&state->agg_lock, flags);
  1219. hrtimer_cancel(&state->hrtimer);
  1220. state->send_agg_skb(agg_skb);
  1221. }
  1222. void rmnet_map_tx_aggregate(struct sk_buff *skb, struct rmnet_port *port,
  1223. bool low_latency)
  1224. {
  1225. struct rmnet_aggregation_state *state;
  1226. struct timespec64 diff, last;
  1227. int size;
  1228. unsigned long flags;
  1229. state = &port->agg_state[(low_latency) ? RMNET_LL_AGG_STATE :
  1230. RMNET_DEFAULT_AGG_STATE];
  1231. new_packet:
  1232. spin_lock_irqsave(&state->agg_lock, flags);
  1233. memcpy(&last, &state->agg_last, sizeof(last));
  1234. ktime_get_real_ts64(&state->agg_last);
  1235. if ((port->data_format & RMNET_EGRESS_FORMAT_PRIORITY) &&
  1236. (RMNET_LLM(skb->priority) || RMNET_APS_LLB(skb->priority))) {
  1237. /* Send out any aggregated SKBs we have */
  1238. rmnet_map_send_agg_skb(state, flags);
  1239. /* Send out the priority SKB. Not holding agg_lock anymore */
  1240. skb->protocol = htons(ETH_P_MAP);
  1241. state->send_agg_skb(skb);
  1242. return;
  1243. }
  1244. if (!state->agg_skb) {
  1245. /* Check to see if we should agg first. If the traffic is very
  1246. * sparse, don't aggregate. We will need to tune this later
  1247. */
  1248. diff = timespec64_sub(state->agg_last, last);
  1249. size = state->params.agg_size - skb->len;
  1250. if (diff.tv_sec > 0 || diff.tv_nsec > rmnet_agg_bypass_time ||
  1251. size <= 0) {
  1252. spin_unlock_irqrestore(&state->agg_lock, flags);
  1253. skb->protocol = htons(ETH_P_MAP);
  1254. state->send_agg_skb(skb);
  1255. return;
  1256. }
  1257. state->agg_skb = rmnet_map_build_skb(state);
  1258. if (!state->agg_skb) {
  1259. state->agg_skb = NULL;
  1260. state->agg_count = 0;
  1261. memset(&state->agg_time, 0, sizeof(state->agg_time));
  1262. spin_unlock_irqrestore(&state->agg_lock, flags);
  1263. skb->protocol = htons(ETH_P_MAP);
  1264. state->send_agg_skb(skb);
  1265. return;
  1266. }
  1267. rmnet_map_linearize_copy(state->agg_skb, skb);
  1268. state->agg_skb->dev = skb->dev;
  1269. state->agg_skb->protocol = htons(ETH_P_MAP);
  1270. state->agg_count = 1;
  1271. ktime_get_real_ts64(&state->agg_time);
  1272. dev_kfree_skb_any(skb);
  1273. goto schedule;
  1274. }
  1275. diff = timespec64_sub(state->agg_last, state->agg_time);
  1276. size = skb_tailroom(state->agg_skb);
  1277. if (skb->len > size ||
  1278. state->agg_count >= state->params.agg_count ||
  1279. diff.tv_sec > 0 || diff.tv_nsec > rmnet_agg_time_limit) {
  1280. rmnet_map_send_agg_skb(state, flags);
  1281. goto new_packet;
  1282. }
  1283. rmnet_map_linearize_copy(state->agg_skb, skb);
  1284. state->agg_count++;
  1285. dev_kfree_skb_any(skb);
  1286. schedule:
  1287. if (state->agg_state != -EINPROGRESS) {
  1288. state->agg_state = -EINPROGRESS;
  1289. hrtimer_start(&state->hrtimer,
  1290. ns_to_ktime(state->params.agg_time),
  1291. HRTIMER_MODE_REL);
  1292. }
  1293. spin_unlock_irqrestore(&state->agg_lock, flags);
  1294. }
  1295. void rmnet_map_update_ul_agg_config(struct rmnet_aggregation_state *state,
  1296. u16 size, u8 count, u8 features, u32 time)
  1297. {
  1298. unsigned long irq_flags;
  1299. spin_lock_irqsave(&state->agg_lock, irq_flags);
  1300. state->params.agg_count = count;
  1301. state->params.agg_time = time;
  1302. state->params.agg_size = size;
  1303. state->params.agg_features = features;
  1304. rmnet_free_agg_pages(state);
  1305. /* This effectively disables recycling in case the UL aggregation
  1306. * size is lesser than PAGE_SIZE.
  1307. */
  1308. if (size < PAGE_SIZE)
  1309. goto done;
  1310. state->agg_size_order = get_order(size);
  1311. size = PAGE_SIZE << state->agg_size_order;
  1312. size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  1313. state->params.agg_size = size;
  1314. if (state->params.agg_features == RMNET_PAGE_RECYCLE)
  1315. rmnet_alloc_agg_pages(state);
  1316. done:
  1317. spin_unlock_irqrestore(&state->agg_lock, irq_flags);
  1318. }
  1319. void rmnet_map_tx_aggregate_init(struct rmnet_port *port)
  1320. {
  1321. unsigned int i;
  1322. for (i = RMNET_DEFAULT_AGG_STATE; i < RMNET_MAX_AGG_STATE; i++) {
  1323. struct rmnet_aggregation_state *state = &port->agg_state[i];
  1324. spin_lock_init(&state->agg_lock);
  1325. INIT_LIST_HEAD(&state->agg_list);
  1326. hrtimer_init(&state->hrtimer, CLOCK_MONOTONIC,
  1327. HRTIMER_MODE_REL);
  1328. state->hrtimer.function = rmnet_map_flush_tx_packet_queue;
  1329. INIT_WORK(&state->agg_wq, rmnet_map_flush_tx_packet_work);
  1330. state->stats = &port->stats.agg;
  1331. /* Since PAGE_SIZE - 1 is specified here, no pages are
  1332. * pre-allocated. This is done to reduce memory usage in cases
  1333. * where UL aggregation is disabled.
  1334. * Additionally, the features flag is also set to 0.
  1335. */
  1336. rmnet_map_update_ul_agg_config(state, PAGE_SIZE - 1, 20, 0,
  1337. 3000000);
  1338. }
  1339. /* Set delivery functions for each aggregation state */
  1340. port->agg_state[RMNET_DEFAULT_AGG_STATE].send_agg_skb = dev_queue_xmit;
  1341. port->agg_state[RMNET_LL_AGG_STATE].send_agg_skb = rmnet_ll_send_skb;
  1342. }
  1343. void rmnet_map_tx_aggregate_exit(struct rmnet_port *port)
  1344. {
  1345. unsigned long flags;
  1346. unsigned int i;
  1347. for (i = RMNET_DEFAULT_AGG_STATE; i < RMNET_MAX_AGG_STATE; i++) {
  1348. struct rmnet_aggregation_state *state = &port->agg_state[i];
  1349. hrtimer_cancel(&state->hrtimer);
  1350. cancel_work_sync(&state->agg_wq);
  1351. }
  1352. for (i = RMNET_DEFAULT_AGG_STATE; i < RMNET_MAX_AGG_STATE; i++) {
  1353. struct rmnet_aggregation_state *state = &port->agg_state[i];
  1354. spin_lock_irqsave(&state->agg_lock, flags);
  1355. if (state->agg_state == -EINPROGRESS) {
  1356. if (state->agg_skb) {
  1357. kfree_skb(state->agg_skb);
  1358. state->agg_skb = NULL;
  1359. state->agg_count = 0;
  1360. memset(&state->agg_time, 0,
  1361. sizeof(state->agg_time));
  1362. }
  1363. state->agg_state = 0;
  1364. }
  1365. rmnet_free_agg_pages(state);
  1366. spin_unlock_irqrestore(&state->agg_lock, flags);
  1367. }
  1368. }
  1369. void rmnet_map_tx_qmap_cmd(struct sk_buff *qmap_skb, u8 ch, bool flush)
  1370. {
  1371. struct rmnet_aggregation_state *state;
  1372. struct rmnet_port *port;
  1373. struct sk_buff *agg_skb;
  1374. unsigned long flags;
  1375. if (unlikely(ch >= RMNET_MAX_AGG_STATE))
  1376. ch = RMNET_DEFAULT_AGG_STATE;
  1377. port = rmnet_get_port(qmap_skb->dev);
  1378. if (!port) {
  1379. kfree_skb(qmap_skb);
  1380. return;
  1381. }
  1382. state = &port->agg_state[ch];
  1383. if (!flush)
  1384. goto send;
  1385. if (!(port->data_format & RMNET_EGRESS_FORMAT_AGGREGATION))
  1386. goto send;
  1387. spin_lock_irqsave(&state->agg_lock, flags);
  1388. if (state->agg_skb) {
  1389. agg_skb = state->agg_skb;
  1390. state->agg_skb = NULL;
  1391. state->agg_count = 0;
  1392. memset(&state->agg_time, 0, sizeof(state->agg_time));
  1393. state->agg_state = 0;
  1394. spin_unlock_irqrestore(&state->agg_lock, flags);
  1395. hrtimer_cancel(&state->hrtimer);
  1396. state->send_agg_skb(agg_skb);
  1397. } else {
  1398. spin_unlock_irqrestore(&state->agg_lock, flags);
  1399. }
  1400. send:
  1401. state->send_agg_skb(qmap_skb);
  1402. }
  1403. EXPORT_SYMBOL(rmnet_map_tx_qmap_cmd);
  1404. int rmnet_map_add_tso_header(struct sk_buff *skb, struct rmnet_port *port,
  1405. struct net_device *orig_dev)
  1406. {
  1407. struct rmnet_priv *priv = netdev_priv(orig_dev);
  1408. struct rmnet_map_v5_tso_header *ul_header;
  1409. if (!(orig_dev->features & (NETIF_F_ALL_TSO | NETIF_F_GSO_UDP_L4))) {
  1410. priv->stats.tso_arriv_errs++;
  1411. return -EINVAL;
  1412. }
  1413. ul_header = (struct rmnet_map_v5_tso_header *)
  1414. skb_push(skb, sizeof(*ul_header));
  1415. memset(ul_header, 0, sizeof(*ul_header));
  1416. ul_header->header_type = RMNET_MAP_HEADER_TYPE_TSO;
  1417. if (port->data_format & RMNET_EGRESS_FORMAT_PRIORITY)
  1418. rmnet_map_v5_check_priority(skb, orig_dev,
  1419. (struct rmnet_map_v5_csum_header *)ul_header,
  1420. true);
  1421. ul_header->segment_size = htons(skb_shinfo(skb)->gso_size);
  1422. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_FIXEDID)
  1423. ul_header->ip_id_cfg = 1;
  1424. skb->ip_summed = CHECKSUM_NONE;
  1425. skb_shinfo(skb)->gso_size = 0;
  1426. skb_shinfo(skb)->gso_segs = 0;
  1427. skb_shinfo(skb)->gso_type = 0;
  1428. priv->stats.tso_pkts++;
  1429. return 0;
  1430. }