rmnet_map_data.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658
  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. #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. u32 dlen = coal_meta->data_len * coal_meta->pkt_count;
  624. u32 hlen = coal_meta->ip_len + coal_meta->trans_len;
  625. bool zero_csum = false;
  626. /* We can avoid copying the data if the SKB we got from the lower-level
  627. * drivers was nonlinear.
  628. */
  629. if (skb_is_nonlinear(coal_skb))
  630. alloc_len = hlen;
  631. else
  632. alloc_len = hlen + dlen;
  633. skbn = alloc_skb(alloc_len, GFP_ATOMIC);
  634. if (!skbn)
  635. return;
  636. skb_reserve(skbn, hlen);
  637. rmnet_map_nonlinear_copy(coal_skb, coal_meta, skbn);
  638. /* Push transport header and update necessary fields */
  639. skb_push(skbn, coal_meta->trans_len);
  640. memcpy(skbn->data, coal_meta->trans_header, coal_meta->trans_len);
  641. skb_reset_transport_header(skbn);
  642. if (coal_meta->trans_proto == IPPROTO_TCP) {
  643. struct tcphdr *th = tcp_hdr(skbn);
  644. th->seq = htonl(ntohl(th->seq) + coal_meta->data_offset);
  645. check = &th->check;
  646. /* Don't allow dangerous flags to be set in any segment but the
  647. * last one.
  648. */
  649. if (th->fin || th->psh) {
  650. if (hlen + coal_meta->data_offset + dlen <
  651. coal_skb->len) {
  652. th->fin = 0;
  653. th->psh = 0;
  654. }
  655. }
  656. } else if (coal_meta->trans_proto == IPPROTO_UDP) {
  657. struct udphdr *uh = udp_hdr(skbn);
  658. uh->len = htons(skbn->len);
  659. check = &uh->check;
  660. if (coal_meta->ip_proto == 4 && !uh->check)
  661. zero_csum = true;
  662. }
  663. /* Push IP header and update necessary fields */
  664. skb_push(skbn, coal_meta->ip_len);
  665. memcpy(skbn->data, coal_meta->ip_header, coal_meta->ip_len);
  666. skb_reset_network_header(skbn);
  667. if (coal_meta->ip_proto == 4) {
  668. struct iphdr *iph = ip_hdr(skbn);
  669. iph->id = htons(ntohs(iph->id) + coal_meta->pkt_id);
  670. iph->tot_len = htons(skbn->len);
  671. iph->check = 0;
  672. iph->check = ip_fast_csum(iph, iph->ihl);
  673. } else {
  674. /* Payload length includes any extension headers */
  675. ipv6_hdr(skbn)->payload_len = htons(skbn->len -
  676. sizeof(struct ipv6hdr));
  677. }
  678. /* Handle checksum status */
  679. if (likely(csum_valid) || zero_csum) {
  680. /* Set the partial checksum information */
  681. rmnet_map_partial_csum(skbn, coal_meta);
  682. } else if (check) {
  683. /* Unfortunately, we have to fake a bad checksum here, since
  684. * the original bad value is lost by the hardware. The only
  685. * reliable way to do it is to calculate the actual checksum
  686. * and corrupt it.
  687. */
  688. __wsum csum;
  689. unsigned int offset = skb_transport_offset(skbn);
  690. __sum16 pseudo;
  691. /* Calculate pseudo header */
  692. if (coal_meta->ip_proto == 4) {
  693. struct iphdr *iph = ip_hdr(skbn);
  694. pseudo = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
  695. skbn->len -
  696. coal_meta->ip_len,
  697. coal_meta->trans_proto, 0);
  698. } else {
  699. struct ipv6hdr *ip6h = ipv6_hdr(skbn);
  700. pseudo = ~csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
  701. skbn->len - coal_meta->ip_len,
  702. coal_meta->trans_proto, 0);
  703. }
  704. *check = pseudo;
  705. csum = skb_checksum(skbn, offset, skbn->len - offset, 0);
  706. /* Add 1 to corrupt. This cannot produce a final value of 0
  707. * since csum_fold() can't return a value of 0xFFFF.
  708. */
  709. *check = csum16_add(csum_fold(csum), htons(1));
  710. skbn->ip_summed = CHECKSUM_NONE;
  711. }
  712. skbn->dev = coal_skb->dev;
  713. priv->stats.coal.coal_reconstruct++;
  714. /* Stamp GSO information if necessary */
  715. if (coal_meta->pkt_count > 1)
  716. rmnet_map_gso_stamp(skbn, coal_meta);
  717. __skb_queue_tail(list, skbn);
  718. /* Update meta information to move past the data we just segmented */
  719. coal_meta->data_offset += dlen;
  720. coal_meta->pkt_id = pkt_id + 1;
  721. coal_meta->pkt_count = 0;
  722. }
  723. static bool rmnet_map_validate_csum(struct sk_buff *skb,
  724. struct rmnet_map_coal_metadata *meta)
  725. {
  726. u8 *data = rmnet_map_data_ptr(skb);
  727. unsigned int datagram_len;
  728. __wsum csum;
  729. __sum16 pseudo;
  730. datagram_len = skb->len - meta->ip_len;
  731. if (meta->ip_proto == 4) {
  732. struct iphdr *iph = (struct iphdr *)data;
  733. pseudo = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
  734. datagram_len,
  735. meta->trans_proto, 0);
  736. } else {
  737. struct ipv6hdr *ip6h = (struct ipv6hdr *)data;
  738. pseudo = ~csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
  739. datagram_len, meta->trans_proto,
  740. 0);
  741. }
  742. csum = skb_checksum(skb, meta->ip_len, datagram_len,
  743. csum_unfold(pseudo));
  744. return !csum_fold(csum);
  745. }
  746. /* Converts the coalesced SKB into a list of SKBs.
  747. * NLOs containing csum erros will not be included.
  748. * The original coalesced SKB should be treated as invalid and
  749. * must be freed by the caller
  750. */
  751. static void rmnet_map_segment_coal_skb(struct sk_buff *coal_skb,
  752. u64 nlo_err_mask,
  753. struct sk_buff_head *list)
  754. {
  755. struct iphdr *iph;
  756. struct rmnet_priv *priv = netdev_priv(coal_skb->dev);
  757. struct rmnet_map_v5_coal_header *coal_hdr;
  758. struct rmnet_map_coal_metadata coal_meta;
  759. u16 pkt_len;
  760. u8 pkt, total_pkt = 0;
  761. u8 nlo;
  762. bool gro = coal_skb->dev->features & NETIF_F_GRO_HW;
  763. bool zero_csum = false;
  764. memset(&coal_meta, 0, sizeof(coal_meta));
  765. /* Pull off the headers we no longer need */
  766. pskb_pull(coal_skb, sizeof(struct rmnet_map_header));
  767. coal_hdr = (struct rmnet_map_v5_coal_header *)
  768. rmnet_map_data_ptr(coal_skb);
  769. pskb_pull(coal_skb, sizeof(*coal_hdr));
  770. iph = (struct iphdr *)rmnet_map_data_ptr(coal_skb);
  771. if (iph->version == 4) {
  772. coal_meta.ip_proto = 4;
  773. coal_meta.ip_len = iph->ihl * 4;
  774. coal_meta.trans_proto = iph->protocol;
  775. coal_meta.ip_header = iph;
  776. /* Don't allow coalescing of any packets with IP options */
  777. if (iph->ihl != 5)
  778. gro = false;
  779. } else if (iph->version == 6) {
  780. struct ipv6hdr *ip6h = (struct ipv6hdr *)iph;
  781. __be16 frag_off;
  782. u8 protocol = ip6h->nexthdr;
  783. coal_meta.ip_proto = 6;
  784. coal_meta.ip_len = ipv6_skip_exthdr(coal_skb, sizeof(*ip6h),
  785. &protocol, &frag_off);
  786. coal_meta.trans_proto = protocol;
  787. coal_meta.ip_header = ip6h;
  788. /* If we run into a problem, or this has a fragment header
  789. * (which should technically not be possible, if the HW
  790. * works as intended...), bail.
  791. */
  792. if (coal_meta.ip_len < 0 || frag_off) {
  793. priv->stats.coal.coal_ip_invalid++;
  794. return;
  795. } else if (coal_meta.ip_len > sizeof(*ip6h)) {
  796. /* Don't allow coalescing of any packets with IPv6
  797. * extension headers.
  798. */
  799. gro = false;
  800. }
  801. } else {
  802. priv->stats.coal.coal_ip_invalid++;
  803. return;
  804. }
  805. if (coal_meta.trans_proto == IPPROTO_TCP) {
  806. struct tcphdr *th;
  807. th = (struct tcphdr *)((u8 *)iph + coal_meta.ip_len);
  808. coal_meta.trans_len = th->doff * 4;
  809. coal_meta.trans_header = th;
  810. } else if (coal_meta.trans_proto == IPPROTO_UDP) {
  811. struct udphdr *uh;
  812. uh = (struct udphdr *)((u8 *)iph + coal_meta.ip_len);
  813. coal_meta.trans_len = sizeof(*uh);
  814. coal_meta.trans_header = uh;
  815. /* Check for v4 zero checksum */
  816. if (coal_meta.ip_proto == 4 && !uh->check)
  817. zero_csum = true;
  818. } else {
  819. priv->stats.coal.coal_trans_invalid++;
  820. return;
  821. }
  822. if (rmnet_map_v5_csum_buggy(coal_hdr) && !zero_csum) {
  823. rmnet_map_move_headers(coal_skb);
  824. /* Mark as valid if it checks out */
  825. if (rmnet_map_validate_csum(coal_skb, &coal_meta))
  826. coal_skb->ip_summed = CHECKSUM_UNNECESSARY;
  827. __skb_queue_tail(list, coal_skb);
  828. return;
  829. }
  830. /* Fast-forward the case where we have 1 NLO (i.e. 1 packet length),
  831. * no checksum errors, and are allowing GRO. We can just reuse this
  832. * SKB unchanged.
  833. */
  834. if (gro && coal_hdr->num_nlos == 1 && coal_hdr->csum_valid) {
  835. rmnet_map_move_headers(coal_skb);
  836. coal_skb->ip_summed = CHECKSUM_UNNECESSARY;
  837. coal_meta.data_len = ntohs(coal_hdr->nl_pairs[0].pkt_len);
  838. coal_meta.data_len -= coal_meta.ip_len + coal_meta.trans_len;
  839. coal_meta.pkt_count = coal_hdr->nl_pairs[0].num_packets;
  840. if (coal_meta.pkt_count > 1) {
  841. rmnet_map_partial_csum(coal_skb, &coal_meta);
  842. rmnet_map_gso_stamp(coal_skb, &coal_meta);
  843. }
  844. __skb_queue_tail(list, coal_skb);
  845. return;
  846. }
  847. /* Segment the coalesced SKB into new packets */
  848. for (nlo = 0; nlo < coal_hdr->num_nlos; nlo++) {
  849. pkt_len = ntohs(coal_hdr->nl_pairs[nlo].pkt_len);
  850. pkt_len -= coal_meta.ip_len + coal_meta.trans_len;
  851. coal_meta.data_len = pkt_len;
  852. for (pkt = 0; pkt < coal_hdr->nl_pairs[nlo].num_packets;
  853. pkt++, total_pkt++, nlo_err_mask >>= 1) {
  854. bool csum_err = nlo_err_mask & 1;
  855. /* Segment the packet if we're not sending the larger
  856. * packet up the stack.
  857. */
  858. if (!gro) {
  859. coal_meta.pkt_count = 1;
  860. if (csum_err)
  861. priv->stats.coal.coal_csum_err++;
  862. __rmnet_map_segment_coal_skb(coal_skb,
  863. &coal_meta, list,
  864. total_pkt,
  865. !csum_err);
  866. continue;
  867. }
  868. if (csum_err) {
  869. priv->stats.coal.coal_csum_err++;
  870. /* Segment out the good data */
  871. if (gro && coal_meta.pkt_count)
  872. __rmnet_map_segment_coal_skb(coal_skb,
  873. &coal_meta,
  874. list,
  875. total_pkt,
  876. true);
  877. /* Segment out the bad checksum */
  878. coal_meta.pkt_count = 1;
  879. __rmnet_map_segment_coal_skb(coal_skb,
  880. &coal_meta, list,
  881. total_pkt, false);
  882. } else {
  883. coal_meta.pkt_count++;
  884. }
  885. }
  886. /* If we're switching NLOs, we need to send out everything from
  887. * the previous one, if we haven't done so. NLOs only switch
  888. * when the packet length changes.
  889. */
  890. if (coal_meta.pkt_count)
  891. __rmnet_map_segment_coal_skb(coal_skb, &coal_meta, list,
  892. total_pkt, true);
  893. }
  894. }
  895. /* Record reason for coalescing pipe closure */
  896. static void rmnet_map_data_log_close_stats(struct rmnet_priv *priv, u8 type,
  897. u8 code)
  898. {
  899. struct rmnet_coal_close_stats *stats = &priv->stats.coal.close;
  900. switch (type) {
  901. case RMNET_MAP_COAL_CLOSE_NON_COAL:
  902. stats->non_coal++;
  903. break;
  904. case RMNET_MAP_COAL_CLOSE_IP_MISS:
  905. stats->ip_miss++;
  906. break;
  907. case RMNET_MAP_COAL_CLOSE_TRANS_MISS:
  908. stats->trans_miss++;
  909. break;
  910. case RMNET_MAP_COAL_CLOSE_HW:
  911. switch (code) {
  912. case RMNET_MAP_COAL_CLOSE_HW_NL:
  913. stats->hw_nl++;
  914. break;
  915. case RMNET_MAP_COAL_CLOSE_HW_PKT:
  916. stats->hw_pkt++;
  917. break;
  918. case RMNET_MAP_COAL_CLOSE_HW_BYTE:
  919. stats->hw_byte++;
  920. break;
  921. case RMNET_MAP_COAL_CLOSE_HW_TIME:
  922. stats->hw_time++;
  923. break;
  924. case RMNET_MAP_COAL_CLOSE_HW_EVICT:
  925. stats->hw_evict++;
  926. break;
  927. default:
  928. break;
  929. }
  930. break;
  931. case RMNET_MAP_COAL_CLOSE_COAL:
  932. stats->coal++;
  933. break;
  934. default:
  935. break;
  936. }
  937. }
  938. /* Check if the coalesced header has any incorrect values, in which case, the
  939. * entire coalesced skb must be dropped. Then check if there are any
  940. * checksum issues
  941. */
  942. static int rmnet_map_data_check_coal_header(struct sk_buff *skb,
  943. u64 *nlo_err_mask)
  944. {
  945. struct rmnet_map_v5_coal_header *coal_hdr;
  946. unsigned char *data = rmnet_map_data_ptr(skb);
  947. struct rmnet_priv *priv = netdev_priv(skb->dev);
  948. u64 mask = 0;
  949. int i;
  950. u8 veid, pkts = 0;
  951. coal_hdr = ((struct rmnet_map_v5_coal_header *)
  952. (data + sizeof(struct rmnet_map_header)));
  953. veid = coal_hdr->virtual_channel_id;
  954. if (coal_hdr->num_nlos == 0 ||
  955. coal_hdr->num_nlos > RMNET_MAP_V5_MAX_NLOS) {
  956. priv->stats.coal.coal_hdr_nlo_err++;
  957. return -EINVAL;
  958. }
  959. for (i = 0; i < RMNET_MAP_V5_MAX_NLOS; i++) {
  960. /* If there is a checksum issue, we need to split
  961. * up the skb. Rebuild the full csum error field
  962. */
  963. u8 err = coal_hdr->nl_pairs[i].csum_error_bitmap;
  964. u8 pkt = coal_hdr->nl_pairs[i].num_packets;
  965. mask |= ((u64)err) << (8 * i);
  966. /* Track total packets in frame */
  967. pkts += pkt;
  968. if (pkts > RMNET_MAP_V5_MAX_PACKETS) {
  969. priv->stats.coal.coal_hdr_pkt_err++;
  970. return -EINVAL;
  971. }
  972. }
  973. /* Track number of packets we get inside of coalesced frames */
  974. priv->stats.coal.coal_pkts += pkts;
  975. /* Update ethtool stats */
  976. rmnet_map_data_log_close_stats(priv,
  977. coal_hdr->close_type,
  978. coal_hdr->close_value);
  979. if (veid < RMNET_MAX_VEID)
  980. priv->stats.coal.coal_veid[veid]++;
  981. *nlo_err_mask = mask;
  982. return 0;
  983. }
  984. /* Process a QMAPv5 packet header */
  985. int rmnet_map_process_next_hdr_packet(struct sk_buff *skb,
  986. struct sk_buff_head *list,
  987. u16 len)
  988. {
  989. struct rmnet_priv *priv = netdev_priv(skb->dev);
  990. u64 nlo_err_mask;
  991. int rc = 0;
  992. switch (rmnet_map_get_next_hdr_type(skb)) {
  993. case RMNET_MAP_HEADER_TYPE_COALESCING:
  994. priv->stats.coal.coal_rx++;
  995. rc = rmnet_map_data_check_coal_header(skb, &nlo_err_mask);
  996. if (rc)
  997. return rc;
  998. rmnet_map_segment_coal_skb(skb, nlo_err_mask, list);
  999. if (skb_peek(list) != skb)
  1000. consume_skb(skb);
  1001. break;
  1002. case RMNET_MAP_HEADER_TYPE_CSUM_OFFLOAD:
  1003. if (unlikely(!(skb->dev->features & NETIF_F_RXCSUM))) {
  1004. priv->stats.csum_sw++;
  1005. } else if (rmnet_map_get_csum_valid(skb)) {
  1006. priv->stats.csum_ok++;
  1007. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1008. } else {
  1009. priv->stats.csum_valid_unset++;
  1010. }
  1011. /* Pull unnecessary headers and move the rest to the linear
  1012. * section of the skb.
  1013. */
  1014. pskb_pull(skb,
  1015. (sizeof(struct rmnet_map_header) +
  1016. sizeof(struct rmnet_map_v5_csum_header)));
  1017. rmnet_map_move_headers(skb);
  1018. /* Remove padding only for csum offload packets.
  1019. * Coalesced packets should never have padding.
  1020. */
  1021. pskb_trim(skb, len);
  1022. __skb_queue_tail(list, skb);
  1023. break;
  1024. default:
  1025. rc = -EINVAL;
  1026. break;
  1027. }
  1028. return rc;
  1029. }
  1030. long rmnet_agg_time_limit __read_mostly = 1000000L;
  1031. long rmnet_agg_bypass_time __read_mostly = 10000000L;
  1032. int rmnet_map_tx_agg_skip(struct sk_buff *skb, int offset)
  1033. {
  1034. u8 *packet_start = skb->data + offset;
  1035. int is_icmp = 0;
  1036. if (skb->protocol == htons(ETH_P_IP)) {
  1037. struct iphdr *ip4h = (struct iphdr *)(packet_start);
  1038. if (ip4h->protocol == IPPROTO_ICMP)
  1039. is_icmp = 1;
  1040. } else if (skb->protocol == htons(ETH_P_IPV6)) {
  1041. struct ipv6hdr *ip6h = (struct ipv6hdr *)(packet_start);
  1042. if (ip6h->nexthdr == IPPROTO_ICMPV6) {
  1043. is_icmp = 1;
  1044. } else if (ip6h->nexthdr == NEXTHDR_FRAGMENT) {
  1045. struct frag_hdr *frag;
  1046. frag = (struct frag_hdr *)(packet_start
  1047. + sizeof(struct ipv6hdr));
  1048. if (frag->nexthdr == IPPROTO_ICMPV6)
  1049. is_icmp = 1;
  1050. }
  1051. }
  1052. return is_icmp;
  1053. }
  1054. static void rmnet_map_flush_tx_packet_work(struct work_struct *work)
  1055. {
  1056. struct sk_buff *skb = NULL;
  1057. struct rmnet_port *port;
  1058. unsigned long flags;
  1059. port = container_of(work, struct rmnet_port, agg_wq);
  1060. spin_lock_irqsave(&port->agg_lock, flags);
  1061. if (likely(port->agg_state == -EINPROGRESS)) {
  1062. /* Buffer may have already been shipped out */
  1063. if (likely(port->agg_skb)) {
  1064. skb = port->agg_skb;
  1065. port->agg_skb = NULL;
  1066. port->agg_count = 0;
  1067. memset(&port->agg_time, 0, sizeof(port->agg_time));
  1068. }
  1069. port->agg_state = 0;
  1070. }
  1071. spin_unlock_irqrestore(&port->agg_lock, flags);
  1072. if (skb)
  1073. dev_queue_xmit(skb);
  1074. }
  1075. enum hrtimer_restart rmnet_map_flush_tx_packet_queue(struct hrtimer *t)
  1076. {
  1077. struct rmnet_port *port;
  1078. port = container_of(t, struct rmnet_port, hrtimer);
  1079. schedule_work(&port->agg_wq);
  1080. return HRTIMER_NORESTART;
  1081. }
  1082. static void rmnet_map_linearize_copy(struct sk_buff *dst, struct sk_buff *src)
  1083. {
  1084. unsigned int linear = src->len - src->data_len, target = src->len;
  1085. unsigned char *src_buf;
  1086. struct sk_buff *skb;
  1087. src_buf = src->data;
  1088. skb_put_data(dst, src_buf, linear);
  1089. target -= linear;
  1090. skb = src;
  1091. while (target) {
  1092. unsigned int i = 0, non_linear = 0;
  1093. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1094. non_linear = skb_frag_size(&skb_shinfo(skb)->frags[i]);
  1095. src_buf = skb_frag_address(&skb_shinfo(skb)->frags[i]);
  1096. skb_put_data(dst, src_buf, non_linear);
  1097. target -= non_linear;
  1098. }
  1099. if (skb_shinfo(skb)->frag_list) {
  1100. skb = skb_shinfo(skb)->frag_list;
  1101. continue;
  1102. }
  1103. if (skb->next)
  1104. skb = skb->next;
  1105. }
  1106. }
  1107. static void rmnet_free_agg_pages(struct rmnet_port *port)
  1108. {
  1109. struct rmnet_agg_page *agg_page, *idx;
  1110. list_for_each_entry_safe(agg_page, idx, &port->agg_list, list) {
  1111. list_del(&agg_page->list);
  1112. put_page(agg_page->page);
  1113. kfree(agg_page);
  1114. }
  1115. port->agg_head = NULL;
  1116. }
  1117. static struct page *rmnet_get_agg_pages(struct rmnet_port *port)
  1118. {
  1119. struct rmnet_agg_page *agg_page;
  1120. struct page *page = NULL;
  1121. int i = 0;
  1122. if (!(port->egress_agg_params.agg_features & RMNET_PAGE_RECYCLE))
  1123. goto alloc;
  1124. do {
  1125. agg_page = port->agg_head;
  1126. if (unlikely(!agg_page))
  1127. break;
  1128. if (page_ref_count(agg_page->page) == 1) {
  1129. page = agg_page->page;
  1130. page_ref_inc(agg_page->page);
  1131. port->stats.agg.ul_agg_reuse++;
  1132. port->agg_head = list_next_entry(agg_page, list);
  1133. break;
  1134. }
  1135. port->agg_head = list_next_entry(agg_page, list);
  1136. i++;
  1137. } while (i <= 5);
  1138. alloc:
  1139. if (!page) {
  1140. page = __dev_alloc_pages(GFP_ATOMIC, port->agg_size_order);
  1141. port->stats.agg.ul_agg_alloc++;
  1142. }
  1143. return page;
  1144. }
  1145. static struct rmnet_agg_page *__rmnet_alloc_agg_pages(struct rmnet_port *port)
  1146. {
  1147. struct rmnet_agg_page *agg_page;
  1148. struct page *page;
  1149. agg_page = kzalloc(sizeof(*agg_page), GFP_ATOMIC);
  1150. if (!agg_page)
  1151. return NULL;
  1152. page = __dev_alloc_pages(GFP_ATOMIC, port->agg_size_order);
  1153. if (!page) {
  1154. kfree(agg_page);
  1155. return NULL;
  1156. }
  1157. agg_page->page = page;
  1158. INIT_LIST_HEAD(&agg_page->list);
  1159. return agg_page;
  1160. }
  1161. static void rmnet_alloc_agg_pages(struct rmnet_port *port)
  1162. {
  1163. struct rmnet_agg_page *agg_page = NULL;
  1164. int i = 0;
  1165. for (i = 0; i < 512; i++) {
  1166. agg_page = __rmnet_alloc_agg_pages(port);
  1167. if (agg_page)
  1168. list_add_tail(&agg_page->list, &port->agg_list);
  1169. }
  1170. port->agg_head = list_first_entry_or_null(&port->agg_list,
  1171. struct rmnet_agg_page, list);
  1172. }
  1173. static struct sk_buff *rmnet_map_build_skb(struct rmnet_port *port)
  1174. {
  1175. struct sk_buff *skb;
  1176. unsigned int size;
  1177. struct page *page;
  1178. void *vaddr;
  1179. page = rmnet_get_agg_pages(port);
  1180. if (!page)
  1181. return NULL;
  1182. vaddr = page_address(page);
  1183. size = PAGE_SIZE << port->agg_size_order;
  1184. skb = build_skb(vaddr, size);
  1185. if (!skb) {
  1186. put_page(page);
  1187. return NULL;
  1188. }
  1189. return skb;
  1190. }
  1191. void rmnet_map_send_agg_skb(struct rmnet_port *port, unsigned long flags)
  1192. {
  1193. struct sk_buff *agg_skb;
  1194. if (!port->agg_skb) {
  1195. spin_unlock_irqrestore(&port->agg_lock, flags);
  1196. return;
  1197. }
  1198. agg_skb = port->agg_skb;
  1199. /* Reset the aggregation state */
  1200. port->agg_skb = NULL;
  1201. port->agg_count = 0;
  1202. memset(&port->agg_time, 0, sizeof(port->agg_time));
  1203. port->agg_state = 0;
  1204. spin_unlock_irqrestore(&port->agg_lock, flags);
  1205. hrtimer_cancel(&port->hrtimer);
  1206. dev_queue_xmit(agg_skb);
  1207. }
  1208. void rmnet_map_tx_aggregate(struct sk_buff *skb, struct rmnet_port *port)
  1209. {
  1210. struct timespec64 diff, last;
  1211. int size;
  1212. unsigned long flags;
  1213. new_packet:
  1214. spin_lock_irqsave(&port->agg_lock, flags);
  1215. memcpy(&last, &port->agg_last, sizeof(last));
  1216. ktime_get_real_ts64(&port->agg_last);
  1217. if ((port->data_format & RMNET_EGRESS_FORMAT_PRIORITY) &&
  1218. skb->priority) {
  1219. /* Send out any aggregated SKBs we have */
  1220. rmnet_map_send_agg_skb(port, flags);
  1221. /* Send out the priority SKB. Not holding agg_lock anymore */
  1222. skb->protocol = htons(ETH_P_MAP);
  1223. dev_queue_xmit(skb);
  1224. return;
  1225. }
  1226. if (!port->agg_skb) {
  1227. /* Check to see if we should agg first. If the traffic is very
  1228. * sparse, don't aggregate. We will need to tune this later
  1229. */
  1230. diff = timespec64_sub(port->agg_last, last);
  1231. size = port->egress_agg_params.agg_size - skb->len;
  1232. if (diff.tv_sec > 0 || diff.tv_nsec > rmnet_agg_bypass_time ||
  1233. size <= 0) {
  1234. spin_unlock_irqrestore(&port->agg_lock, flags);
  1235. skb->protocol = htons(ETH_P_MAP);
  1236. dev_queue_xmit(skb);
  1237. return;
  1238. }
  1239. port->agg_skb = rmnet_map_build_skb(port);
  1240. if (!port->agg_skb) {
  1241. port->agg_skb = 0;
  1242. port->agg_count = 0;
  1243. memset(&port->agg_time, 0, sizeof(port->agg_time));
  1244. spin_unlock_irqrestore(&port->agg_lock, flags);
  1245. skb->protocol = htons(ETH_P_MAP);
  1246. dev_queue_xmit(skb);
  1247. return;
  1248. }
  1249. rmnet_map_linearize_copy(port->agg_skb, skb);
  1250. port->agg_skb->dev = skb->dev;
  1251. port->agg_skb->protocol = htons(ETH_P_MAP);
  1252. port->agg_count = 1;
  1253. ktime_get_real_ts64(&port->agg_time);
  1254. dev_kfree_skb_any(skb);
  1255. goto schedule;
  1256. }
  1257. diff = timespec64_sub(port->agg_last, port->agg_time);
  1258. size = skb_tailroom(port->agg_skb);
  1259. if (skb->len > size ||
  1260. port->agg_count >= port->egress_agg_params.agg_count ||
  1261. diff.tv_sec > 0 || diff.tv_nsec > rmnet_agg_time_limit) {
  1262. rmnet_map_send_agg_skb(port, flags);
  1263. goto new_packet;
  1264. }
  1265. rmnet_map_linearize_copy(port->agg_skb, skb);
  1266. port->agg_count++;
  1267. dev_kfree_skb_any(skb);
  1268. schedule:
  1269. if (port->agg_state != -EINPROGRESS) {
  1270. port->agg_state = -EINPROGRESS;
  1271. hrtimer_start(&port->hrtimer,
  1272. ns_to_ktime(port->egress_agg_params.agg_time),
  1273. HRTIMER_MODE_REL);
  1274. }
  1275. spin_unlock_irqrestore(&port->agg_lock, flags);
  1276. }
  1277. void rmnet_map_update_ul_agg_config(struct rmnet_port *port, u16 size,
  1278. u8 count, u8 features, u32 time)
  1279. {
  1280. unsigned long irq_flags;
  1281. spin_lock_irqsave(&port->agg_lock, irq_flags);
  1282. port->egress_agg_params.agg_count = count;
  1283. port->egress_agg_params.agg_time = time;
  1284. port->egress_agg_params.agg_size = size;
  1285. port->egress_agg_params.agg_features = features;
  1286. rmnet_free_agg_pages(port);
  1287. /* This effectively disables recycling in case the UL aggregation
  1288. * size is lesser than PAGE_SIZE.
  1289. */
  1290. if (size < PAGE_SIZE)
  1291. goto done;
  1292. port->agg_size_order = get_order(size);
  1293. size = PAGE_SIZE << port->agg_size_order;
  1294. size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  1295. port->egress_agg_params.agg_size = size;
  1296. if (port->egress_agg_params.agg_features == RMNET_PAGE_RECYCLE)
  1297. rmnet_alloc_agg_pages(port);
  1298. done:
  1299. spin_unlock_irqrestore(&port->agg_lock, irq_flags);
  1300. }
  1301. void rmnet_map_tx_aggregate_init(struct rmnet_port *port)
  1302. {
  1303. hrtimer_init(&port->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1304. port->hrtimer.function = rmnet_map_flush_tx_packet_queue;
  1305. spin_lock_init(&port->agg_lock);
  1306. INIT_LIST_HEAD(&port->agg_list);
  1307. /* Since PAGE_SIZE - 1 is specified here, no pages are pre-allocated.
  1308. * This is done to reduce memory usage in cases where
  1309. * UL aggregation is disabled.
  1310. * Additionally, the features flag is also set to 0.
  1311. */
  1312. rmnet_map_update_ul_agg_config(port, PAGE_SIZE - 1, 20, 0, 3000000);
  1313. INIT_WORK(&port->agg_wq, rmnet_map_flush_tx_packet_work);
  1314. }
  1315. void rmnet_map_tx_aggregate_exit(struct rmnet_port *port)
  1316. {
  1317. unsigned long flags;
  1318. hrtimer_cancel(&port->hrtimer);
  1319. cancel_work_sync(&port->agg_wq);
  1320. spin_lock_irqsave(&port->agg_lock, flags);
  1321. if (port->agg_state == -EINPROGRESS) {
  1322. if (port->agg_skb) {
  1323. kfree_skb(port->agg_skb);
  1324. port->agg_skb = NULL;
  1325. port->agg_count = 0;
  1326. memset(&port->agg_time, 0, sizeof(port->agg_time));
  1327. }
  1328. port->agg_state = 0;
  1329. }
  1330. rmnet_free_agg_pages(port);
  1331. spin_unlock_irqrestore(&port->agg_lock, flags);
  1332. }
  1333. void rmnet_map_tx_qmap_cmd(struct sk_buff *qmap_skb)
  1334. {
  1335. struct rmnet_port *port;
  1336. struct sk_buff *agg_skb;
  1337. unsigned long flags;
  1338. port = rmnet_get_port(qmap_skb->dev);
  1339. if (port && (port->data_format & RMNET_EGRESS_FORMAT_AGGREGATION)) {
  1340. spin_lock_irqsave(&port->agg_lock, flags);
  1341. if (port->agg_skb) {
  1342. agg_skb = port->agg_skb;
  1343. port->agg_skb = 0;
  1344. port->agg_count = 0;
  1345. memset(&port->agg_time, 0, sizeof(port->agg_time));
  1346. port->agg_state = 0;
  1347. spin_unlock_irqrestore(&port->agg_lock, flags);
  1348. hrtimer_cancel(&port->hrtimer);
  1349. dev_queue_xmit(agg_skb);
  1350. } else {
  1351. spin_unlock_irqrestore(&port->agg_lock, flags);
  1352. }
  1353. }
  1354. dev_queue_xmit(qmap_skb);
  1355. }
  1356. EXPORT_SYMBOL(rmnet_map_tx_qmap_cmd);
  1357. int rmnet_map_add_tso_header(struct sk_buff *skb, struct rmnet_port *port,
  1358. struct net_device *orig_dev)
  1359. {
  1360. struct rmnet_priv *priv = netdev_priv(orig_dev);
  1361. struct rmnet_map_v5_tso_header *ul_header;
  1362. if (!(orig_dev->features & (NETIF_F_ALL_TSO | NETIF_F_GSO_UDP_L4))) {
  1363. priv->stats.tso_arriv_errs++;
  1364. return -EINVAL;
  1365. }
  1366. ul_header = (struct rmnet_map_v5_tso_header *)
  1367. skb_push(skb, sizeof(*ul_header));
  1368. memset(ul_header, 0, sizeof(*ul_header));
  1369. ul_header->header_type = RMNET_MAP_HEADER_TYPE_TSO;
  1370. if (port->data_format & RMNET_EGRESS_FORMAT_PRIORITY)
  1371. rmnet_map_v5_check_priority(skb, orig_dev,
  1372. (struct rmnet_map_v5_csum_header *)ul_header);
  1373. ul_header->segment_size = htons(skb_shinfo(skb)->gso_size);
  1374. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_FIXEDID)
  1375. ul_header->ip_id_cfg = 1;
  1376. skb->ip_summed = CHECKSUM_NONE;
  1377. skb_shinfo(skb)->gso_size = 0;
  1378. skb_shinfo(skb)->gso_segs = 0;
  1379. skb_shinfo(skb)->gso_type = 0;
  1380. priv->stats.tso_pkts++;
  1381. return 0;
  1382. }