rmnet_map_data.c 42 KB

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