rmnet_map_data.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623
  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. static 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);