rmnet_map_data.c 44 KB

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