netback.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843
  1. /*
  2. * Back-end of the driver for virtual network devices. This portion of the
  3. * driver exports a 'unified' network-device interface that can be accessed
  4. * by any operating system that implements a compatible front end. A
  5. * reference front-end implementation can be found in:
  6. * drivers/net/xen-netfront.c
  7. *
  8. * Copyright (c) 2002-2005, K A Fraser
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License version 2
  12. * as published by the Free Software Foundation; or, when distributed
  13. * separately from the Linux kernel or incorporated into other
  14. * software packages, subject to the following license:
  15. *
  16. * Permission is hereby granted, free of charge, to any person obtaining a copy
  17. * of this source file (the "Software"), to deal in the Software without
  18. * restriction, including without limitation the rights to use, copy, modify,
  19. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  20. * and to permit persons to whom the Software is furnished to do so, subject to
  21. * the following conditions:
  22. *
  23. * The above copyright notice and this permission notice shall be included in
  24. * all copies or substantial portions of the Software.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  27. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  28. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  29. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  30. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  31. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  32. * IN THE SOFTWARE.
  33. */
  34. #include "common.h"
  35. #include <linux/kthread.h>
  36. #include <linux/if_vlan.h>
  37. #include <linux/udp.h>
  38. #include <net/tcp.h>
  39. #include <net/ip6_checksum.h>
  40. #include <xen/xen.h>
  41. #include <xen/events.h>
  42. #include <xen/interface/memory.h>
  43. #include <asm/xen/hypercall.h>
  44. #include <asm/xen/page.h>
  45. /* Provide an option to disable split event channels at load time as
  46. * event channels are limited resource. Split event channels are
  47. * enabled by default.
  48. */
  49. bool separate_tx_rx_irq = 1;
  50. module_param(separate_tx_rx_irq, bool, 0644);
  51. /*
  52. * This is the maximum slots a skb can have. If a guest sends a skb
  53. * which exceeds this limit it is considered malicious.
  54. */
  55. #define FATAL_SKB_SLOTS_DEFAULT 20
  56. static unsigned int fatal_skb_slots = FATAL_SKB_SLOTS_DEFAULT;
  57. module_param(fatal_skb_slots, uint, 0444);
  58. /*
  59. * To avoid confusion, we define XEN_NETBK_LEGACY_SLOTS_MAX indicating
  60. * the maximum slots a valid packet can use. Now this value is defined
  61. * to be XEN_NETIF_NR_SLOTS_MIN, which is supposed to be supported by
  62. * all backend.
  63. */
  64. #define XEN_NETBK_LEGACY_SLOTS_MAX XEN_NETIF_NR_SLOTS_MIN
  65. /*
  66. * If head != INVALID_PENDING_RING_IDX, it means this tx request is head of
  67. * one or more merged tx requests, otherwise it is the continuation of
  68. * previous tx request.
  69. */
  70. static inline int pending_tx_is_head(struct xenvif *vif, RING_IDX idx)
  71. {
  72. return vif->pending_tx_info[idx].head != INVALID_PENDING_RING_IDX;
  73. }
  74. static void xenvif_idx_release(struct xenvif *vif, u16 pending_idx,
  75. u8 status);
  76. static void make_tx_response(struct xenvif *vif,
  77. struct xen_netif_tx_request *txp,
  78. s8 st);
  79. static inline int tx_work_todo(struct xenvif *vif);
  80. static inline int rx_work_todo(struct xenvif *vif);
  81. static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
  82. u16 id,
  83. s8 st,
  84. u16 offset,
  85. u16 size,
  86. u16 flags);
  87. static inline unsigned long idx_to_pfn(struct xenvif *vif,
  88. u16 idx)
  89. {
  90. return page_to_pfn(vif->mmap_pages[idx]);
  91. }
  92. static inline unsigned long idx_to_kaddr(struct xenvif *vif,
  93. u16 idx)
  94. {
  95. return (unsigned long)pfn_to_kaddr(idx_to_pfn(vif, idx));
  96. }
  97. /* This is a miniumum size for the linear area to avoid lots of
  98. * calls to __pskb_pull_tail() as we set up checksum offsets. The
  99. * value 128 was chosen as it covers all IPv4 and most likely
  100. * IPv6 headers.
  101. */
  102. #define PKT_PROT_LEN 128
  103. static u16 frag_get_pending_idx(skb_frag_t *frag)
  104. {
  105. return (u16)frag->page_offset;
  106. }
  107. static void frag_set_pending_idx(skb_frag_t *frag, u16 pending_idx)
  108. {
  109. frag->page_offset = pending_idx;
  110. }
  111. static inline pending_ring_idx_t pending_index(unsigned i)
  112. {
  113. return i & (MAX_PENDING_REQS-1);
  114. }
  115. static inline pending_ring_idx_t nr_pending_reqs(struct xenvif *vif)
  116. {
  117. return MAX_PENDING_REQS -
  118. vif->pending_prod + vif->pending_cons;
  119. }
  120. bool xenvif_rx_ring_slots_available(struct xenvif *vif, int needed)
  121. {
  122. RING_IDX prod, cons;
  123. do {
  124. prod = vif->rx.sring->req_prod;
  125. cons = vif->rx.req_cons;
  126. if (prod - cons >= needed)
  127. return true;
  128. vif->rx.sring->req_event = prod + 1;
  129. /* Make sure event is visible before we check prod
  130. * again.
  131. */
  132. mb();
  133. } while (vif->rx.sring->req_prod != prod);
  134. return false;
  135. }
  136. /*
  137. * Returns true if we should start a new receive buffer instead of
  138. * adding 'size' bytes to a buffer which currently contains 'offset'
  139. * bytes.
  140. */
  141. static bool start_new_rx_buffer(int offset, unsigned long size, int head)
  142. {
  143. /* simple case: we have completely filled the current buffer. */
  144. if (offset == MAX_BUFFER_OFFSET)
  145. return true;
  146. /*
  147. * complex case: start a fresh buffer if the current frag
  148. * would overflow the current buffer but only if:
  149. * (i) this frag would fit completely in the next buffer
  150. * and (ii) there is already some data in the current buffer
  151. * and (iii) this is not the head buffer.
  152. *
  153. * Where:
  154. * - (i) stops us splitting a frag into two copies
  155. * unless the frag is too large for a single buffer.
  156. * - (ii) stops us from leaving a buffer pointlessly empty.
  157. * - (iii) stops us leaving the first buffer
  158. * empty. Strictly speaking this is already covered
  159. * by (ii) but is explicitly checked because
  160. * netfront relies on the first buffer being
  161. * non-empty and can crash otherwise.
  162. *
  163. * This means we will effectively linearise small
  164. * frags but do not needlessly split large buffers
  165. * into multiple copies tend to give large frags their
  166. * own buffers as before.
  167. */
  168. if ((offset + size > MAX_BUFFER_OFFSET) &&
  169. (size <= MAX_BUFFER_OFFSET) && offset && !head)
  170. return true;
  171. return false;
  172. }
  173. struct netrx_pending_operations {
  174. unsigned copy_prod, copy_cons;
  175. unsigned meta_prod, meta_cons;
  176. struct gnttab_copy *copy;
  177. struct xenvif_rx_meta *meta;
  178. int copy_off;
  179. grant_ref_t copy_gref;
  180. };
  181. static struct xenvif_rx_meta *get_next_rx_buffer(struct xenvif *vif,
  182. struct netrx_pending_operations *npo)
  183. {
  184. struct xenvif_rx_meta *meta;
  185. struct xen_netif_rx_request *req;
  186. req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
  187. meta = npo->meta + npo->meta_prod++;
  188. meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
  189. meta->gso_size = 0;
  190. meta->size = 0;
  191. meta->id = req->id;
  192. npo->copy_off = 0;
  193. npo->copy_gref = req->gref;
  194. return meta;
  195. }
  196. /*
  197. * Set up the grant operations for this fragment. If it's a flipping
  198. * interface, we also set up the unmap request from here.
  199. */
  200. static void xenvif_gop_frag_copy(struct xenvif *vif, struct sk_buff *skb,
  201. struct netrx_pending_operations *npo,
  202. struct page *page, unsigned long size,
  203. unsigned long offset, int *head)
  204. {
  205. struct gnttab_copy *copy_gop;
  206. struct xenvif_rx_meta *meta;
  207. unsigned long bytes;
  208. int gso_type;
  209. /* Data must not cross a page boundary. */
  210. BUG_ON(size + offset > PAGE_SIZE<<compound_order(page));
  211. meta = npo->meta + npo->meta_prod - 1;
  212. /* Skip unused frames from start of page */
  213. page += offset >> PAGE_SHIFT;
  214. offset &= ~PAGE_MASK;
  215. while (size > 0) {
  216. BUG_ON(offset >= PAGE_SIZE);
  217. BUG_ON(npo->copy_off > MAX_BUFFER_OFFSET);
  218. bytes = PAGE_SIZE - offset;
  219. if (bytes > size)
  220. bytes = size;
  221. if (start_new_rx_buffer(npo->copy_off, bytes, *head)) {
  222. /*
  223. * Netfront requires there to be some data in the head
  224. * buffer.
  225. */
  226. BUG_ON(*head);
  227. meta = get_next_rx_buffer(vif, npo);
  228. }
  229. if (npo->copy_off + bytes > MAX_BUFFER_OFFSET)
  230. bytes = MAX_BUFFER_OFFSET - npo->copy_off;
  231. copy_gop = npo->copy + npo->copy_prod++;
  232. copy_gop->flags = GNTCOPY_dest_gref;
  233. copy_gop->len = bytes;
  234. copy_gop->source.domid = DOMID_SELF;
  235. copy_gop->source.u.gmfn = virt_to_mfn(page_address(page));
  236. copy_gop->source.offset = offset;
  237. copy_gop->dest.domid = vif->domid;
  238. copy_gop->dest.offset = npo->copy_off;
  239. copy_gop->dest.u.ref = npo->copy_gref;
  240. npo->copy_off += bytes;
  241. meta->size += bytes;
  242. offset += bytes;
  243. size -= bytes;
  244. /* Next frame */
  245. if (offset == PAGE_SIZE && size) {
  246. BUG_ON(!PageCompound(page));
  247. page++;
  248. offset = 0;
  249. }
  250. /* Leave a gap for the GSO descriptor. */
  251. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
  252. gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
  253. else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
  254. gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
  255. else
  256. gso_type = XEN_NETIF_GSO_TYPE_NONE;
  257. if (*head && ((1 << gso_type) & vif->gso_mask))
  258. vif->rx.req_cons++;
  259. *head = 0; /* There must be something in this buffer now. */
  260. }
  261. }
  262. /*
  263. * Prepare an SKB to be transmitted to the frontend.
  264. *
  265. * This function is responsible for allocating grant operations, meta
  266. * structures, etc.
  267. *
  268. * It returns the number of meta structures consumed. The number of
  269. * ring slots used is always equal to the number of meta slots used
  270. * plus the number of GSO descriptors used. Currently, we use either
  271. * zero GSO descriptors (for non-GSO packets) or one descriptor (for
  272. * frontend-side LRO).
  273. */
  274. static int xenvif_gop_skb(struct sk_buff *skb,
  275. struct netrx_pending_operations *npo)
  276. {
  277. struct xenvif *vif = netdev_priv(skb->dev);
  278. int nr_frags = skb_shinfo(skb)->nr_frags;
  279. int i;
  280. struct xen_netif_rx_request *req;
  281. struct xenvif_rx_meta *meta;
  282. unsigned char *data;
  283. int head = 1;
  284. int old_meta_prod;
  285. int gso_type;
  286. int gso_size;
  287. old_meta_prod = npo->meta_prod;
  288. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
  289. gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
  290. gso_size = skb_shinfo(skb)->gso_size;
  291. } else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
  292. gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
  293. gso_size = skb_shinfo(skb)->gso_size;
  294. } else {
  295. gso_type = XEN_NETIF_GSO_TYPE_NONE;
  296. gso_size = 0;
  297. }
  298. /* Set up a GSO prefix descriptor, if necessary */
  299. if ((1 << skb_shinfo(skb)->gso_type) & vif->gso_prefix_mask) {
  300. req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
  301. meta = npo->meta + npo->meta_prod++;
  302. meta->gso_type = gso_type;
  303. meta->gso_size = gso_size;
  304. meta->size = 0;
  305. meta->id = req->id;
  306. }
  307. req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
  308. meta = npo->meta + npo->meta_prod++;
  309. if ((1 << gso_type) & vif->gso_mask) {
  310. meta->gso_type = gso_type;
  311. meta->gso_size = gso_size;
  312. } else {
  313. meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
  314. meta->gso_size = 0;
  315. }
  316. meta->size = 0;
  317. meta->id = req->id;
  318. npo->copy_off = 0;
  319. npo->copy_gref = req->gref;
  320. data = skb->data;
  321. while (data < skb_tail_pointer(skb)) {
  322. unsigned int offset = offset_in_page(data);
  323. unsigned int len = PAGE_SIZE - offset;
  324. if (data + len > skb_tail_pointer(skb))
  325. len = skb_tail_pointer(skb) - data;
  326. xenvif_gop_frag_copy(vif, skb, npo,
  327. virt_to_page(data), len, offset, &head);
  328. data += len;
  329. }
  330. for (i = 0; i < nr_frags; i++) {
  331. xenvif_gop_frag_copy(vif, skb, npo,
  332. skb_frag_page(&skb_shinfo(skb)->frags[i]),
  333. skb_frag_size(&skb_shinfo(skb)->frags[i]),
  334. skb_shinfo(skb)->frags[i].page_offset,
  335. &head);
  336. }
  337. return npo->meta_prod - old_meta_prod;
  338. }
  339. /*
  340. * This is a twin to xenvif_gop_skb. Assume that xenvif_gop_skb was
  341. * used to set up the operations on the top of
  342. * netrx_pending_operations, which have since been done. Check that
  343. * they didn't give any errors and advance over them.
  344. */
  345. static int xenvif_check_gop(struct xenvif *vif, int nr_meta_slots,
  346. struct netrx_pending_operations *npo)
  347. {
  348. struct gnttab_copy *copy_op;
  349. int status = XEN_NETIF_RSP_OKAY;
  350. int i;
  351. for (i = 0; i < nr_meta_slots; i++) {
  352. copy_op = npo->copy + npo->copy_cons++;
  353. if (copy_op->status != GNTST_okay) {
  354. netdev_dbg(vif->dev,
  355. "Bad status %d from copy to DOM%d.\n",
  356. copy_op->status, vif->domid);
  357. status = XEN_NETIF_RSP_ERROR;
  358. }
  359. }
  360. return status;
  361. }
  362. static void xenvif_add_frag_responses(struct xenvif *vif, int status,
  363. struct xenvif_rx_meta *meta,
  364. int nr_meta_slots)
  365. {
  366. int i;
  367. unsigned long offset;
  368. /* No fragments used */
  369. if (nr_meta_slots <= 1)
  370. return;
  371. nr_meta_slots--;
  372. for (i = 0; i < nr_meta_slots; i++) {
  373. int flags;
  374. if (i == nr_meta_slots - 1)
  375. flags = 0;
  376. else
  377. flags = XEN_NETRXF_more_data;
  378. offset = 0;
  379. make_rx_response(vif, meta[i].id, status, offset,
  380. meta[i].size, flags);
  381. }
  382. }
  383. struct skb_cb_overlay {
  384. int meta_slots_used;
  385. };
  386. void xenvif_kick_thread(struct xenvif *vif)
  387. {
  388. wake_up(&vif->wq);
  389. }
  390. static void xenvif_rx_action(struct xenvif *vif)
  391. {
  392. s8 status;
  393. u16 flags;
  394. struct xen_netif_rx_response *resp;
  395. struct sk_buff_head rxq;
  396. struct sk_buff *skb;
  397. LIST_HEAD(notify);
  398. int ret;
  399. unsigned long offset;
  400. struct skb_cb_overlay *sco;
  401. int need_to_notify = 0;
  402. struct netrx_pending_operations npo = {
  403. .copy = vif->grant_copy_op,
  404. .meta = vif->meta,
  405. };
  406. skb_queue_head_init(&rxq);
  407. while ((skb = skb_dequeue(&vif->rx_queue)) != NULL) {
  408. int max_slots_needed;
  409. int i;
  410. /* We need a cheap worse case estimate for the number of
  411. * slots we'll use.
  412. */
  413. max_slots_needed = DIV_ROUND_UP(offset_in_page(skb->data) +
  414. skb_headlen(skb),
  415. PAGE_SIZE);
  416. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  417. unsigned int size;
  418. size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
  419. max_slots_needed += DIV_ROUND_UP(size, PAGE_SIZE);
  420. }
  421. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4 ||
  422. skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
  423. max_slots_needed++;
  424. /* If the skb may not fit then bail out now */
  425. if (!xenvif_rx_ring_slots_available(vif, max_slots_needed)) {
  426. skb_queue_head(&vif->rx_queue, skb);
  427. need_to_notify = 1;
  428. break;
  429. }
  430. sco = (struct skb_cb_overlay *)skb->cb;
  431. sco->meta_slots_used = xenvif_gop_skb(skb, &npo);
  432. BUG_ON(sco->meta_slots_used > max_slots_needed);
  433. __skb_queue_tail(&rxq, skb);
  434. }
  435. BUG_ON(npo.meta_prod > ARRAY_SIZE(vif->meta));
  436. if (!npo.copy_prod)
  437. goto done;
  438. BUG_ON(npo.copy_prod > ARRAY_SIZE(vif->grant_copy_op));
  439. gnttab_batch_copy(vif->grant_copy_op, npo.copy_prod);
  440. while ((skb = __skb_dequeue(&rxq)) != NULL) {
  441. sco = (struct skb_cb_overlay *)skb->cb;
  442. if ((1 << vif->meta[npo.meta_cons].gso_type) &
  443. vif->gso_prefix_mask) {
  444. resp = RING_GET_RESPONSE(&vif->rx,
  445. vif->rx.rsp_prod_pvt++);
  446. resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
  447. resp->offset = vif->meta[npo.meta_cons].gso_size;
  448. resp->id = vif->meta[npo.meta_cons].id;
  449. resp->status = sco->meta_slots_used;
  450. npo.meta_cons++;
  451. sco->meta_slots_used--;
  452. }
  453. vif->dev->stats.tx_bytes += skb->len;
  454. vif->dev->stats.tx_packets++;
  455. status = xenvif_check_gop(vif, sco->meta_slots_used, &npo);
  456. if (sco->meta_slots_used == 1)
  457. flags = 0;
  458. else
  459. flags = XEN_NETRXF_more_data;
  460. if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */
  461. flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated;
  462. else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
  463. /* remote but checksummed. */
  464. flags |= XEN_NETRXF_data_validated;
  465. offset = 0;
  466. resp = make_rx_response(vif, vif->meta[npo.meta_cons].id,
  467. status, offset,
  468. vif->meta[npo.meta_cons].size,
  469. flags);
  470. if ((1 << vif->meta[npo.meta_cons].gso_type) &
  471. vif->gso_mask) {
  472. struct xen_netif_extra_info *gso =
  473. (struct xen_netif_extra_info *)
  474. RING_GET_RESPONSE(&vif->rx,
  475. vif->rx.rsp_prod_pvt++);
  476. resp->flags |= XEN_NETRXF_extra_info;
  477. gso->u.gso.type = vif->meta[npo.meta_cons].gso_type;
  478. gso->u.gso.size = vif->meta[npo.meta_cons].gso_size;
  479. gso->u.gso.pad = 0;
  480. gso->u.gso.features = 0;
  481. gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
  482. gso->flags = 0;
  483. }
  484. xenvif_add_frag_responses(vif, status,
  485. vif->meta + npo.meta_cons + 1,
  486. sco->meta_slots_used);
  487. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->rx, ret);
  488. if (ret)
  489. need_to_notify = 1;
  490. npo.meta_cons += sco->meta_slots_used;
  491. dev_kfree_skb(skb);
  492. }
  493. done:
  494. if (need_to_notify)
  495. notify_remote_via_irq(vif->rx_irq);
  496. }
  497. void xenvif_check_rx_xenvif(struct xenvif *vif)
  498. {
  499. int more_to_do;
  500. RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do);
  501. if (more_to_do)
  502. napi_schedule(&vif->napi);
  503. }
  504. static void tx_add_credit(struct xenvif *vif)
  505. {
  506. unsigned long max_burst, max_credit;
  507. /*
  508. * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
  509. * Otherwise the interface can seize up due to insufficient credit.
  510. */
  511. max_burst = RING_GET_REQUEST(&vif->tx, vif->tx.req_cons)->size;
  512. max_burst = min(max_burst, 131072UL);
  513. max_burst = max(max_burst, vif->credit_bytes);
  514. /* Take care that adding a new chunk of credit doesn't wrap to zero. */
  515. max_credit = vif->remaining_credit + vif->credit_bytes;
  516. if (max_credit < vif->remaining_credit)
  517. max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
  518. vif->remaining_credit = min(max_credit, max_burst);
  519. }
  520. static void tx_credit_callback(unsigned long data)
  521. {
  522. struct xenvif *vif = (struct xenvif *)data;
  523. tx_add_credit(vif);
  524. xenvif_check_rx_xenvif(vif);
  525. }
  526. static void xenvif_tx_err(struct xenvif *vif,
  527. struct xen_netif_tx_request *txp, RING_IDX end)
  528. {
  529. RING_IDX cons = vif->tx.req_cons;
  530. do {
  531. make_tx_response(vif, txp, XEN_NETIF_RSP_ERROR);
  532. if (cons == end)
  533. break;
  534. txp = RING_GET_REQUEST(&vif->tx, cons++);
  535. } while (1);
  536. vif->tx.req_cons = cons;
  537. }
  538. static void xenvif_fatal_tx_err(struct xenvif *vif)
  539. {
  540. netdev_err(vif->dev, "fatal error; disabling device\n");
  541. xenvif_carrier_off(vif);
  542. }
  543. static int xenvif_count_requests(struct xenvif *vif,
  544. struct xen_netif_tx_request *first,
  545. struct xen_netif_tx_request *txp,
  546. int work_to_do)
  547. {
  548. RING_IDX cons = vif->tx.req_cons;
  549. int slots = 0;
  550. int drop_err = 0;
  551. int more_data;
  552. if (!(first->flags & XEN_NETTXF_more_data))
  553. return 0;
  554. do {
  555. struct xen_netif_tx_request dropped_tx = { 0 };
  556. if (slots >= work_to_do) {
  557. netdev_err(vif->dev,
  558. "Asked for %d slots but exceeds this limit\n",
  559. work_to_do);
  560. xenvif_fatal_tx_err(vif);
  561. return -ENODATA;
  562. }
  563. /* This guest is really using too many slots and
  564. * considered malicious.
  565. */
  566. if (unlikely(slots >= fatal_skb_slots)) {
  567. netdev_err(vif->dev,
  568. "Malicious frontend using %d slots, threshold %u\n",
  569. slots, fatal_skb_slots);
  570. xenvif_fatal_tx_err(vif);
  571. return -E2BIG;
  572. }
  573. /* Xen network protocol had implicit dependency on
  574. * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
  575. * the historical MAX_SKB_FRAGS value 18 to honor the
  576. * same behavior as before. Any packet using more than
  577. * 18 slots but less than fatal_skb_slots slots is
  578. * dropped
  579. */
  580. if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
  581. if (net_ratelimit())
  582. netdev_dbg(vif->dev,
  583. "Too many slots (%d) exceeding limit (%d), dropping packet\n",
  584. slots, XEN_NETBK_LEGACY_SLOTS_MAX);
  585. drop_err = -E2BIG;
  586. }
  587. if (drop_err)
  588. txp = &dropped_tx;
  589. memcpy(txp, RING_GET_REQUEST(&vif->tx, cons + slots),
  590. sizeof(*txp));
  591. /* If the guest submitted a frame >= 64 KiB then
  592. * first->size overflowed and following slots will
  593. * appear to be larger than the frame.
  594. *
  595. * This cannot be fatal error as there are buggy
  596. * frontends that do this.
  597. *
  598. * Consume all slots and drop the packet.
  599. */
  600. if (!drop_err && txp->size > first->size) {
  601. if (net_ratelimit())
  602. netdev_dbg(vif->dev,
  603. "Invalid tx request, slot size %u > remaining size %u\n",
  604. txp->size, first->size);
  605. drop_err = -EIO;
  606. }
  607. first->size -= txp->size;
  608. slots++;
  609. if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
  610. netdev_err(vif->dev, "Cross page boundary, txp->offset: %x, size: %u\n",
  611. txp->offset, txp->size);
  612. xenvif_fatal_tx_err(vif);
  613. return -EINVAL;
  614. }
  615. more_data = txp->flags & XEN_NETTXF_more_data;
  616. if (!drop_err)
  617. txp++;
  618. } while (more_data);
  619. if (drop_err) {
  620. xenvif_tx_err(vif, first, cons + slots);
  621. return drop_err;
  622. }
  623. return slots;
  624. }
  625. static struct page *xenvif_alloc_page(struct xenvif *vif,
  626. u16 pending_idx)
  627. {
  628. struct page *page;
  629. page = alloc_page(GFP_ATOMIC|__GFP_COLD);
  630. if (!page)
  631. return NULL;
  632. vif->mmap_pages[pending_idx] = page;
  633. return page;
  634. }
  635. static struct gnttab_copy *xenvif_get_requests(struct xenvif *vif,
  636. struct sk_buff *skb,
  637. struct xen_netif_tx_request *txp,
  638. struct gnttab_copy *gop)
  639. {
  640. struct skb_shared_info *shinfo = skb_shinfo(skb);
  641. skb_frag_t *frags = shinfo->frags;
  642. u16 pending_idx = *((u16 *)skb->data);
  643. u16 head_idx = 0;
  644. int slot, start;
  645. struct page *page;
  646. pending_ring_idx_t index, start_idx = 0;
  647. uint16_t dst_offset;
  648. unsigned int nr_slots;
  649. struct pending_tx_info *first = NULL;
  650. /* At this point shinfo->nr_frags is in fact the number of
  651. * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
  652. */
  653. nr_slots = shinfo->nr_frags;
  654. /* Skip first skb fragment if it is on same page as header fragment. */
  655. start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
  656. /* Coalesce tx requests, at this point the packet passed in
  657. * should be <= 64K. Any packets larger than 64K have been
  658. * handled in xenvif_count_requests().
  659. */
  660. for (shinfo->nr_frags = slot = start; slot < nr_slots;
  661. shinfo->nr_frags++) {
  662. struct pending_tx_info *pending_tx_info =
  663. vif->pending_tx_info;
  664. page = alloc_page(GFP_ATOMIC|__GFP_COLD);
  665. if (!page)
  666. goto err;
  667. dst_offset = 0;
  668. first = NULL;
  669. while (dst_offset < PAGE_SIZE && slot < nr_slots) {
  670. gop->flags = GNTCOPY_source_gref;
  671. gop->source.u.ref = txp->gref;
  672. gop->source.domid = vif->domid;
  673. gop->source.offset = txp->offset;
  674. gop->dest.domid = DOMID_SELF;
  675. gop->dest.offset = dst_offset;
  676. gop->dest.u.gmfn = virt_to_mfn(page_address(page));
  677. if (dst_offset + txp->size > PAGE_SIZE) {
  678. /* This page can only merge a portion
  679. * of tx request. Do not increment any
  680. * pointer / counter here. The txp
  681. * will be dealt with in future
  682. * rounds, eventually hitting the
  683. * `else` branch.
  684. */
  685. gop->len = PAGE_SIZE - dst_offset;
  686. txp->offset += gop->len;
  687. txp->size -= gop->len;
  688. dst_offset += gop->len; /* quit loop */
  689. } else {
  690. /* This tx request can be merged in the page */
  691. gop->len = txp->size;
  692. dst_offset += gop->len;
  693. index = pending_index(vif->pending_cons++);
  694. pending_idx = vif->pending_ring[index];
  695. memcpy(&pending_tx_info[pending_idx].req, txp,
  696. sizeof(*txp));
  697. /* Poison these fields, corresponding
  698. * fields for head tx req will be set
  699. * to correct values after the loop.
  700. */
  701. vif->mmap_pages[pending_idx] = (void *)(~0UL);
  702. pending_tx_info[pending_idx].head =
  703. INVALID_PENDING_RING_IDX;
  704. if (!first) {
  705. first = &pending_tx_info[pending_idx];
  706. start_idx = index;
  707. head_idx = pending_idx;
  708. }
  709. txp++;
  710. slot++;
  711. }
  712. gop++;
  713. }
  714. first->req.offset = 0;
  715. first->req.size = dst_offset;
  716. first->head = start_idx;
  717. vif->mmap_pages[head_idx] = page;
  718. frag_set_pending_idx(&frags[shinfo->nr_frags], head_idx);
  719. }
  720. BUG_ON(shinfo->nr_frags > MAX_SKB_FRAGS);
  721. return gop;
  722. err:
  723. /* Unwind, freeing all pages and sending error responses. */
  724. while (shinfo->nr_frags-- > start) {
  725. xenvif_idx_release(vif,
  726. frag_get_pending_idx(&frags[shinfo->nr_frags]),
  727. XEN_NETIF_RSP_ERROR);
  728. }
  729. /* The head too, if necessary. */
  730. if (start)
  731. xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
  732. return NULL;
  733. }
  734. static int xenvif_tx_check_gop(struct xenvif *vif,
  735. struct sk_buff *skb,
  736. struct gnttab_copy **gopp)
  737. {
  738. struct gnttab_copy *gop = *gopp;
  739. u16 pending_idx = *((u16 *)skb->data);
  740. struct skb_shared_info *shinfo = skb_shinfo(skb);
  741. struct pending_tx_info *tx_info;
  742. int nr_frags = shinfo->nr_frags;
  743. int i, err, start;
  744. u16 peek; /* peek into next tx request */
  745. /* Check status of header. */
  746. err = gop->status;
  747. if (unlikely(err))
  748. xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
  749. /* Skip first skb fragment if it is on same page as header fragment. */
  750. start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
  751. for (i = start; i < nr_frags; i++) {
  752. int j, newerr;
  753. pending_ring_idx_t head;
  754. pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
  755. tx_info = &vif->pending_tx_info[pending_idx];
  756. head = tx_info->head;
  757. /* Check error status: if okay then remember grant handle. */
  758. do {
  759. newerr = (++gop)->status;
  760. if (newerr)
  761. break;
  762. peek = vif->pending_ring[pending_index(++head)];
  763. } while (!pending_tx_is_head(vif, peek));
  764. if (likely(!newerr)) {
  765. /* Had a previous error? Invalidate this fragment. */
  766. if (unlikely(err))
  767. xenvif_idx_release(vif, pending_idx,
  768. XEN_NETIF_RSP_OKAY);
  769. continue;
  770. }
  771. /* Error on this fragment: respond to client with an error. */
  772. xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
  773. /* Not the first error? Preceding frags already invalidated. */
  774. if (err)
  775. continue;
  776. /* First error: invalidate header and preceding fragments. */
  777. pending_idx = *((u16 *)skb->data);
  778. xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_OKAY);
  779. for (j = start; j < i; j++) {
  780. pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
  781. xenvif_idx_release(vif, pending_idx,
  782. XEN_NETIF_RSP_OKAY);
  783. }
  784. /* Remember the error: invalidate all subsequent fragments. */
  785. err = newerr;
  786. }
  787. *gopp = gop + 1;
  788. return err;
  789. }
  790. static void xenvif_fill_frags(struct xenvif *vif, struct sk_buff *skb)
  791. {
  792. struct skb_shared_info *shinfo = skb_shinfo(skb);
  793. int nr_frags = shinfo->nr_frags;
  794. int i;
  795. for (i = 0; i < nr_frags; i++) {
  796. skb_frag_t *frag = shinfo->frags + i;
  797. struct xen_netif_tx_request *txp;
  798. struct page *page;
  799. u16 pending_idx;
  800. pending_idx = frag_get_pending_idx(frag);
  801. txp = &vif->pending_tx_info[pending_idx].req;
  802. page = virt_to_page(idx_to_kaddr(vif, pending_idx));
  803. __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
  804. skb->len += txp->size;
  805. skb->data_len += txp->size;
  806. skb->truesize += txp->size;
  807. /* Take an extra reference to offset xenvif_idx_release */
  808. get_page(vif->mmap_pages[pending_idx]);
  809. xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_OKAY);
  810. }
  811. }
  812. static int xenvif_get_extras(struct xenvif *vif,
  813. struct xen_netif_extra_info *extras,
  814. int work_to_do)
  815. {
  816. struct xen_netif_extra_info extra;
  817. RING_IDX cons = vif->tx.req_cons;
  818. do {
  819. if (unlikely(work_to_do-- <= 0)) {
  820. netdev_err(vif->dev, "Missing extra info\n");
  821. xenvif_fatal_tx_err(vif);
  822. return -EBADR;
  823. }
  824. memcpy(&extra, RING_GET_REQUEST(&vif->tx, cons),
  825. sizeof(extra));
  826. if (unlikely(!extra.type ||
  827. extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
  828. vif->tx.req_cons = ++cons;
  829. netdev_err(vif->dev,
  830. "Invalid extra type: %d\n", extra.type);
  831. xenvif_fatal_tx_err(vif);
  832. return -EINVAL;
  833. }
  834. memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
  835. vif->tx.req_cons = ++cons;
  836. } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
  837. return work_to_do;
  838. }
  839. static int xenvif_set_skb_gso(struct xenvif *vif,
  840. struct sk_buff *skb,
  841. struct xen_netif_extra_info *gso)
  842. {
  843. if (!gso->u.gso.size) {
  844. netdev_err(vif->dev, "GSO size must not be zero.\n");
  845. xenvif_fatal_tx_err(vif);
  846. return -EINVAL;
  847. }
  848. switch (gso->u.gso.type) {
  849. case XEN_NETIF_GSO_TYPE_TCPV4:
  850. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  851. break;
  852. case XEN_NETIF_GSO_TYPE_TCPV6:
  853. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  854. break;
  855. default:
  856. netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
  857. xenvif_fatal_tx_err(vif);
  858. return -EINVAL;
  859. }
  860. skb_shinfo(skb)->gso_size = gso->u.gso.size;
  861. /* Header must be checked, and gso_segs computed. */
  862. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  863. skb_shinfo(skb)->gso_segs = 0;
  864. return 0;
  865. }
  866. static inline int maybe_pull_tail(struct sk_buff *skb, unsigned int len,
  867. unsigned int max)
  868. {
  869. if (skb_headlen(skb) >= len)
  870. return 0;
  871. /* If we need to pullup then pullup to the max, so we
  872. * won't need to do it again.
  873. */
  874. if (max > skb->len)
  875. max = skb->len;
  876. if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
  877. return -ENOMEM;
  878. if (skb_headlen(skb) < len)
  879. return -EPROTO;
  880. return 0;
  881. }
  882. /* This value should be large enough to cover a tagged ethernet header plus
  883. * maximally sized IP and TCP or UDP headers.
  884. */
  885. #define MAX_IP_HDR_LEN 128
  886. static int checksum_setup_ip(struct xenvif *vif, struct sk_buff *skb,
  887. int recalculate_partial_csum)
  888. {
  889. unsigned int off;
  890. bool fragment;
  891. int err;
  892. fragment = false;
  893. err = maybe_pull_tail(skb,
  894. sizeof(struct iphdr),
  895. MAX_IP_HDR_LEN);
  896. if (err < 0)
  897. goto out;
  898. if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
  899. fragment = true;
  900. off = ip_hdrlen(skb);
  901. err = -EPROTO;
  902. switch (ip_hdr(skb)->protocol) {
  903. case IPPROTO_TCP:
  904. if (!skb_partial_csum_set(skb, off,
  905. offsetof(struct tcphdr, check)))
  906. goto out;
  907. if (recalculate_partial_csum) {
  908. err = maybe_pull_tail(skb,
  909. off + sizeof(struct tcphdr),
  910. MAX_IP_HDR_LEN);
  911. if (err < 0)
  912. goto out;
  913. tcp_hdr(skb)->check =
  914. ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
  915. ip_hdr(skb)->daddr,
  916. skb->len - off,
  917. IPPROTO_TCP, 0);
  918. }
  919. break;
  920. case IPPROTO_UDP:
  921. if (!skb_partial_csum_set(skb, off,
  922. offsetof(struct udphdr, check)))
  923. goto out;
  924. if (recalculate_partial_csum) {
  925. err = maybe_pull_tail(skb,
  926. off + sizeof(struct udphdr),
  927. MAX_IP_HDR_LEN);
  928. if (err < 0)
  929. goto out;
  930. udp_hdr(skb)->check =
  931. ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
  932. ip_hdr(skb)->daddr,
  933. skb->len - off,
  934. IPPROTO_UDP, 0);
  935. }
  936. break;
  937. default:
  938. goto out;
  939. }
  940. err = 0;
  941. out:
  942. return err;
  943. }
  944. /* This value should be large enough to cover a tagged ethernet header plus
  945. * an IPv6 header, all options, and a maximal TCP or UDP header.
  946. */
  947. #define MAX_IPV6_HDR_LEN 256
  948. #define OPT_HDR(type, skb, off) \
  949. (type *)(skb_network_header(skb) + (off))
  950. static int checksum_setup_ipv6(struct xenvif *vif, struct sk_buff *skb,
  951. int recalculate_partial_csum)
  952. {
  953. int err;
  954. u8 nexthdr;
  955. unsigned int off;
  956. unsigned int len;
  957. bool fragment;
  958. bool done;
  959. fragment = false;
  960. done = false;
  961. off = sizeof(struct ipv6hdr);
  962. err = maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
  963. if (err < 0)
  964. goto out;
  965. nexthdr = ipv6_hdr(skb)->nexthdr;
  966. len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
  967. while (off <= len && !done) {
  968. switch (nexthdr) {
  969. case IPPROTO_DSTOPTS:
  970. case IPPROTO_HOPOPTS:
  971. case IPPROTO_ROUTING: {
  972. struct ipv6_opt_hdr *hp;
  973. err = maybe_pull_tail(skb,
  974. off +
  975. sizeof(struct ipv6_opt_hdr),
  976. MAX_IPV6_HDR_LEN);
  977. if (err < 0)
  978. goto out;
  979. hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
  980. nexthdr = hp->nexthdr;
  981. off += ipv6_optlen(hp);
  982. break;
  983. }
  984. case IPPROTO_AH: {
  985. struct ip_auth_hdr *hp;
  986. err = maybe_pull_tail(skb,
  987. off +
  988. sizeof(struct ip_auth_hdr),
  989. MAX_IPV6_HDR_LEN);
  990. if (err < 0)
  991. goto out;
  992. hp = OPT_HDR(struct ip_auth_hdr, skb, off);
  993. nexthdr = hp->nexthdr;
  994. off += ipv6_authlen(hp);
  995. break;
  996. }
  997. case IPPROTO_FRAGMENT: {
  998. struct frag_hdr *hp;
  999. err = maybe_pull_tail(skb,
  1000. off +
  1001. sizeof(struct frag_hdr),
  1002. MAX_IPV6_HDR_LEN);
  1003. if (err < 0)
  1004. goto out;
  1005. hp = OPT_HDR(struct frag_hdr, skb, off);
  1006. if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
  1007. fragment = true;
  1008. nexthdr = hp->nexthdr;
  1009. off += sizeof(struct frag_hdr);
  1010. break;
  1011. }
  1012. default:
  1013. done = true;
  1014. break;
  1015. }
  1016. }
  1017. err = -EPROTO;
  1018. if (!done || fragment)
  1019. goto out;
  1020. switch (nexthdr) {
  1021. case IPPROTO_TCP:
  1022. if (!skb_partial_csum_set(skb, off,
  1023. offsetof(struct tcphdr, check)))
  1024. goto out;
  1025. if (recalculate_partial_csum) {
  1026. err = maybe_pull_tail(skb,
  1027. off + sizeof(struct tcphdr),
  1028. MAX_IPV6_HDR_LEN);
  1029. if (err < 0)
  1030. goto out;
  1031. tcp_hdr(skb)->check =
  1032. ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  1033. &ipv6_hdr(skb)->daddr,
  1034. skb->len - off,
  1035. IPPROTO_TCP, 0);
  1036. }
  1037. break;
  1038. case IPPROTO_UDP:
  1039. if (!skb_partial_csum_set(skb, off,
  1040. offsetof(struct udphdr, check)))
  1041. goto out;
  1042. if (recalculate_partial_csum) {
  1043. err = maybe_pull_tail(skb,
  1044. off + sizeof(struct udphdr),
  1045. MAX_IPV6_HDR_LEN);
  1046. if (err < 0)
  1047. goto out;
  1048. udp_hdr(skb)->check =
  1049. ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  1050. &ipv6_hdr(skb)->daddr,
  1051. skb->len - off,
  1052. IPPROTO_UDP, 0);
  1053. }
  1054. break;
  1055. default:
  1056. goto out;
  1057. }
  1058. err = 0;
  1059. out:
  1060. return err;
  1061. }
  1062. static int checksum_setup(struct xenvif *vif, struct sk_buff *skb)
  1063. {
  1064. int err = -EPROTO;
  1065. int recalculate_partial_csum = 0;
  1066. /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
  1067. * peers can fail to set NETRXF_csum_blank when sending a GSO
  1068. * frame. In this case force the SKB to CHECKSUM_PARTIAL and
  1069. * recalculate the partial checksum.
  1070. */
  1071. if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
  1072. vif->rx_gso_checksum_fixup++;
  1073. skb->ip_summed = CHECKSUM_PARTIAL;
  1074. recalculate_partial_csum = 1;
  1075. }
  1076. /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
  1077. if (skb->ip_summed != CHECKSUM_PARTIAL)
  1078. return 0;
  1079. if (skb->protocol == htons(ETH_P_IP))
  1080. err = checksum_setup_ip(vif, skb, recalculate_partial_csum);
  1081. else if (skb->protocol == htons(ETH_P_IPV6))
  1082. err = checksum_setup_ipv6(vif, skb, recalculate_partial_csum);
  1083. return err;
  1084. }
  1085. static bool tx_credit_exceeded(struct xenvif *vif, unsigned size)
  1086. {
  1087. u64 now = get_jiffies_64();
  1088. u64 next_credit = vif->credit_window_start +
  1089. msecs_to_jiffies(vif->credit_usec / 1000);
  1090. /* Timer could already be pending in rare cases. */
  1091. if (timer_pending(&vif->credit_timeout))
  1092. return true;
  1093. /* Passed the point where we can replenish credit? */
  1094. if (time_after_eq64(now, next_credit)) {
  1095. vif->credit_window_start = now;
  1096. tx_add_credit(vif);
  1097. }
  1098. /* Still too big to send right now? Set a callback. */
  1099. if (size > vif->remaining_credit) {
  1100. vif->credit_timeout.data =
  1101. (unsigned long)vif;
  1102. vif->credit_timeout.function =
  1103. tx_credit_callback;
  1104. mod_timer(&vif->credit_timeout,
  1105. next_credit);
  1106. vif->credit_window_start = next_credit;
  1107. return true;
  1108. }
  1109. return false;
  1110. }
  1111. static unsigned xenvif_tx_build_gops(struct xenvif *vif)
  1112. {
  1113. struct gnttab_copy *gop = vif->tx_copy_ops, *request_gop;
  1114. struct sk_buff *skb;
  1115. int ret;
  1116. while ((nr_pending_reqs(vif) + XEN_NETBK_LEGACY_SLOTS_MAX
  1117. < MAX_PENDING_REQS)) {
  1118. struct xen_netif_tx_request txreq;
  1119. struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
  1120. struct page *page;
  1121. struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
  1122. u16 pending_idx;
  1123. RING_IDX idx;
  1124. int work_to_do;
  1125. unsigned int data_len;
  1126. pending_ring_idx_t index;
  1127. if (vif->tx.sring->req_prod - vif->tx.req_cons >
  1128. XEN_NETIF_TX_RING_SIZE) {
  1129. netdev_err(vif->dev,
  1130. "Impossible number of requests. "
  1131. "req_prod %d, req_cons %d, size %ld\n",
  1132. vif->tx.sring->req_prod, vif->tx.req_cons,
  1133. XEN_NETIF_TX_RING_SIZE);
  1134. xenvif_fatal_tx_err(vif);
  1135. continue;
  1136. }
  1137. RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, work_to_do);
  1138. if (!work_to_do)
  1139. break;
  1140. idx = vif->tx.req_cons;
  1141. rmb(); /* Ensure that we see the request before we copy it. */
  1142. memcpy(&txreq, RING_GET_REQUEST(&vif->tx, idx), sizeof(txreq));
  1143. /* Credit-based scheduling. */
  1144. if (txreq.size > vif->remaining_credit &&
  1145. tx_credit_exceeded(vif, txreq.size))
  1146. break;
  1147. vif->remaining_credit -= txreq.size;
  1148. work_to_do--;
  1149. vif->tx.req_cons = ++idx;
  1150. memset(extras, 0, sizeof(extras));
  1151. if (txreq.flags & XEN_NETTXF_extra_info) {
  1152. work_to_do = xenvif_get_extras(vif, extras,
  1153. work_to_do);
  1154. idx = vif->tx.req_cons;
  1155. if (unlikely(work_to_do < 0))
  1156. break;
  1157. }
  1158. ret = xenvif_count_requests(vif, &txreq, txfrags, work_to_do);
  1159. if (unlikely(ret < 0))
  1160. break;
  1161. idx += ret;
  1162. if (unlikely(txreq.size < ETH_HLEN)) {
  1163. netdev_dbg(vif->dev,
  1164. "Bad packet size: %d\n", txreq.size);
  1165. xenvif_tx_err(vif, &txreq, idx);
  1166. break;
  1167. }
  1168. /* No crossing a page as the payload mustn't fragment. */
  1169. if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
  1170. netdev_err(vif->dev,
  1171. "txreq.offset: %x, size: %u, end: %lu\n",
  1172. txreq.offset, txreq.size,
  1173. (txreq.offset&~PAGE_MASK) + txreq.size);
  1174. xenvif_fatal_tx_err(vif);
  1175. break;
  1176. }
  1177. index = pending_index(vif->pending_cons);
  1178. pending_idx = vif->pending_ring[index];
  1179. data_len = (txreq.size > PKT_PROT_LEN &&
  1180. ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
  1181. PKT_PROT_LEN : txreq.size;
  1182. skb = alloc_skb(data_len + NET_SKB_PAD + NET_IP_ALIGN,
  1183. GFP_ATOMIC | __GFP_NOWARN);
  1184. if (unlikely(skb == NULL)) {
  1185. netdev_dbg(vif->dev,
  1186. "Can't allocate a skb in start_xmit.\n");
  1187. xenvif_tx_err(vif, &txreq, idx);
  1188. break;
  1189. }
  1190. /* Packets passed to netif_rx() must have some headroom. */
  1191. skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
  1192. if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
  1193. struct xen_netif_extra_info *gso;
  1194. gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
  1195. if (xenvif_set_skb_gso(vif, skb, gso)) {
  1196. /* Failure in xenvif_set_skb_gso is fatal. */
  1197. kfree_skb(skb);
  1198. break;
  1199. }
  1200. }
  1201. /* XXX could copy straight to head */
  1202. page = xenvif_alloc_page(vif, pending_idx);
  1203. if (!page) {
  1204. kfree_skb(skb);
  1205. xenvif_tx_err(vif, &txreq, idx);
  1206. break;
  1207. }
  1208. gop->source.u.ref = txreq.gref;
  1209. gop->source.domid = vif->domid;
  1210. gop->source.offset = txreq.offset;
  1211. gop->dest.u.gmfn = virt_to_mfn(page_address(page));
  1212. gop->dest.domid = DOMID_SELF;
  1213. gop->dest.offset = txreq.offset;
  1214. gop->len = txreq.size;
  1215. gop->flags = GNTCOPY_source_gref;
  1216. gop++;
  1217. memcpy(&vif->pending_tx_info[pending_idx].req,
  1218. &txreq, sizeof(txreq));
  1219. vif->pending_tx_info[pending_idx].head = index;
  1220. *((u16 *)skb->data) = pending_idx;
  1221. __skb_put(skb, data_len);
  1222. skb_shinfo(skb)->nr_frags = ret;
  1223. if (data_len < txreq.size) {
  1224. skb_shinfo(skb)->nr_frags++;
  1225. frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
  1226. pending_idx);
  1227. } else {
  1228. frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
  1229. INVALID_PENDING_IDX);
  1230. }
  1231. vif->pending_cons++;
  1232. request_gop = xenvif_get_requests(vif, skb, txfrags, gop);
  1233. if (request_gop == NULL) {
  1234. kfree_skb(skb);
  1235. xenvif_tx_err(vif, &txreq, idx);
  1236. break;
  1237. }
  1238. gop = request_gop;
  1239. __skb_queue_tail(&vif->tx_queue, skb);
  1240. vif->tx.req_cons = idx;
  1241. if ((gop-vif->tx_copy_ops) >= ARRAY_SIZE(vif->tx_copy_ops))
  1242. break;
  1243. }
  1244. return gop - vif->tx_copy_ops;
  1245. }
  1246. static int xenvif_tx_submit(struct xenvif *vif, int budget)
  1247. {
  1248. struct gnttab_copy *gop = vif->tx_copy_ops;
  1249. struct sk_buff *skb;
  1250. int work_done = 0;
  1251. while (work_done < budget &&
  1252. (skb = __skb_dequeue(&vif->tx_queue)) != NULL) {
  1253. struct xen_netif_tx_request *txp;
  1254. u16 pending_idx;
  1255. unsigned data_len;
  1256. pending_idx = *((u16 *)skb->data);
  1257. txp = &vif->pending_tx_info[pending_idx].req;
  1258. /* Check the remap error code. */
  1259. if (unlikely(xenvif_tx_check_gop(vif, skb, &gop))) {
  1260. netdev_dbg(vif->dev, "netback grant failed.\n");
  1261. skb_shinfo(skb)->nr_frags = 0;
  1262. kfree_skb(skb);
  1263. continue;
  1264. }
  1265. data_len = skb->len;
  1266. memcpy(skb->data,
  1267. (void *)(idx_to_kaddr(vif, pending_idx)|txp->offset),
  1268. data_len);
  1269. if (data_len < txp->size) {
  1270. /* Append the packet payload as a fragment. */
  1271. txp->offset += data_len;
  1272. txp->size -= data_len;
  1273. } else {
  1274. /* Schedule a response immediately. */
  1275. xenvif_idx_release(vif, pending_idx,
  1276. XEN_NETIF_RSP_OKAY);
  1277. }
  1278. if (txp->flags & XEN_NETTXF_csum_blank)
  1279. skb->ip_summed = CHECKSUM_PARTIAL;
  1280. else if (txp->flags & XEN_NETTXF_data_validated)
  1281. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1282. xenvif_fill_frags(vif, skb);
  1283. if (skb_is_nonlinear(skb) && skb_headlen(skb) < PKT_PROT_LEN) {
  1284. int target = min_t(int, skb->len, PKT_PROT_LEN);
  1285. __pskb_pull_tail(skb, target - skb_headlen(skb));
  1286. }
  1287. skb->dev = vif->dev;
  1288. skb->protocol = eth_type_trans(skb, skb->dev);
  1289. skb_reset_network_header(skb);
  1290. if (checksum_setup(vif, skb)) {
  1291. netdev_dbg(vif->dev,
  1292. "Can't setup checksum in net_tx_action\n");
  1293. kfree_skb(skb);
  1294. continue;
  1295. }
  1296. skb_probe_transport_header(skb, 0);
  1297. vif->dev->stats.rx_bytes += skb->len;
  1298. vif->dev->stats.rx_packets++;
  1299. work_done++;
  1300. netif_receive_skb(skb);
  1301. }
  1302. return work_done;
  1303. }
  1304. /* Called after netfront has transmitted */
  1305. int xenvif_tx_action(struct xenvif *vif, int budget)
  1306. {
  1307. unsigned nr_gops;
  1308. int work_done;
  1309. if (unlikely(!tx_work_todo(vif)))
  1310. return 0;
  1311. nr_gops = xenvif_tx_build_gops(vif);
  1312. if (nr_gops == 0)
  1313. return 0;
  1314. gnttab_batch_copy(vif->tx_copy_ops, nr_gops);
  1315. work_done = xenvif_tx_submit(vif, nr_gops);
  1316. return work_done;
  1317. }
  1318. static void xenvif_idx_release(struct xenvif *vif, u16 pending_idx,
  1319. u8 status)
  1320. {
  1321. struct pending_tx_info *pending_tx_info;
  1322. pending_ring_idx_t head;
  1323. u16 peek; /* peek into next tx request */
  1324. BUG_ON(vif->mmap_pages[pending_idx] == (void *)(~0UL));
  1325. /* Already complete? */
  1326. if (vif->mmap_pages[pending_idx] == NULL)
  1327. return;
  1328. pending_tx_info = &vif->pending_tx_info[pending_idx];
  1329. head = pending_tx_info->head;
  1330. BUG_ON(!pending_tx_is_head(vif, head));
  1331. BUG_ON(vif->pending_ring[pending_index(head)] != pending_idx);
  1332. do {
  1333. pending_ring_idx_t index;
  1334. pending_ring_idx_t idx = pending_index(head);
  1335. u16 info_idx = vif->pending_ring[idx];
  1336. pending_tx_info = &vif->pending_tx_info[info_idx];
  1337. make_tx_response(vif, &pending_tx_info->req, status);
  1338. /* Setting any number other than
  1339. * INVALID_PENDING_RING_IDX indicates this slot is
  1340. * starting a new packet / ending a previous packet.
  1341. */
  1342. pending_tx_info->head = 0;
  1343. index = pending_index(vif->pending_prod++);
  1344. vif->pending_ring[index] = vif->pending_ring[info_idx];
  1345. peek = vif->pending_ring[pending_index(++head)];
  1346. } while (!pending_tx_is_head(vif, peek));
  1347. put_page(vif->mmap_pages[pending_idx]);
  1348. vif->mmap_pages[pending_idx] = NULL;
  1349. }
  1350. static void make_tx_response(struct xenvif *vif,
  1351. struct xen_netif_tx_request *txp,
  1352. s8 st)
  1353. {
  1354. RING_IDX i = vif->tx.rsp_prod_pvt;
  1355. struct xen_netif_tx_response *resp;
  1356. int notify;
  1357. resp = RING_GET_RESPONSE(&vif->tx, i);
  1358. resp->id = txp->id;
  1359. resp->status = st;
  1360. if (txp->flags & XEN_NETTXF_extra_info)
  1361. RING_GET_RESPONSE(&vif->tx, ++i)->status = XEN_NETIF_RSP_NULL;
  1362. vif->tx.rsp_prod_pvt = ++i;
  1363. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->tx, notify);
  1364. if (notify)
  1365. notify_remote_via_irq(vif->tx_irq);
  1366. }
  1367. static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
  1368. u16 id,
  1369. s8 st,
  1370. u16 offset,
  1371. u16 size,
  1372. u16 flags)
  1373. {
  1374. RING_IDX i = vif->rx.rsp_prod_pvt;
  1375. struct xen_netif_rx_response *resp;
  1376. resp = RING_GET_RESPONSE(&vif->rx, i);
  1377. resp->offset = offset;
  1378. resp->flags = flags;
  1379. resp->id = id;
  1380. resp->status = (s16)size;
  1381. if (st < 0)
  1382. resp->status = (s16)st;
  1383. vif->rx.rsp_prod_pvt = ++i;
  1384. return resp;
  1385. }
  1386. static inline int rx_work_todo(struct xenvif *vif)
  1387. {
  1388. return !skb_queue_empty(&vif->rx_queue) || vif->rx_event;
  1389. }
  1390. static inline int tx_work_todo(struct xenvif *vif)
  1391. {
  1392. if (likely(RING_HAS_UNCONSUMED_REQUESTS(&vif->tx)) &&
  1393. (nr_pending_reqs(vif) + XEN_NETBK_LEGACY_SLOTS_MAX
  1394. < MAX_PENDING_REQS))
  1395. return 1;
  1396. return 0;
  1397. }
  1398. void xenvif_unmap_frontend_rings(struct xenvif *vif)
  1399. {
  1400. if (vif->tx.sring)
  1401. xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
  1402. vif->tx.sring);
  1403. if (vif->rx.sring)
  1404. xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
  1405. vif->rx.sring);
  1406. }
  1407. int xenvif_map_frontend_rings(struct xenvif *vif,
  1408. grant_ref_t tx_ring_ref,
  1409. grant_ref_t rx_ring_ref)
  1410. {
  1411. void *addr;
  1412. struct xen_netif_tx_sring *txs;
  1413. struct xen_netif_rx_sring *rxs;
  1414. int err = -ENOMEM;
  1415. err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
  1416. tx_ring_ref, &addr);
  1417. if (err)
  1418. goto err;
  1419. txs = (struct xen_netif_tx_sring *)addr;
  1420. BACK_RING_INIT(&vif->tx, txs, PAGE_SIZE);
  1421. err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
  1422. rx_ring_ref, &addr);
  1423. if (err)
  1424. goto err;
  1425. rxs = (struct xen_netif_rx_sring *)addr;
  1426. BACK_RING_INIT(&vif->rx, rxs, PAGE_SIZE);
  1427. return 0;
  1428. err:
  1429. xenvif_unmap_frontend_rings(vif);
  1430. return err;
  1431. }
  1432. void xenvif_stop_queue(struct xenvif *vif)
  1433. {
  1434. if (!vif->can_queue)
  1435. return;
  1436. netif_stop_queue(vif->dev);
  1437. }
  1438. static void xenvif_start_queue(struct xenvif *vif)
  1439. {
  1440. if (xenvif_schedulable(vif))
  1441. netif_wake_queue(vif->dev);
  1442. }
  1443. int xenvif_kthread(void *data)
  1444. {
  1445. struct xenvif *vif = data;
  1446. struct sk_buff *skb;
  1447. while (!kthread_should_stop()) {
  1448. wait_event_interruptible(vif->wq,
  1449. rx_work_todo(vif) ||
  1450. kthread_should_stop());
  1451. if (kthread_should_stop())
  1452. break;
  1453. if (!skb_queue_empty(&vif->rx_queue))
  1454. xenvif_rx_action(vif);
  1455. vif->rx_event = false;
  1456. if (skb_queue_empty(&vif->rx_queue) &&
  1457. netif_queue_stopped(vif->dev))
  1458. xenvif_start_queue(vif);
  1459. cond_resched();
  1460. }
  1461. /* Bin any remaining skbs */
  1462. while ((skb = skb_dequeue(&vif->rx_queue)) != NULL)
  1463. dev_kfree_skb(skb);
  1464. return 0;
  1465. }
  1466. static int __init netback_init(void)
  1467. {
  1468. int rc = 0;
  1469. if (!xen_domain())
  1470. return -ENODEV;
  1471. if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
  1472. pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
  1473. fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
  1474. fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
  1475. }
  1476. rc = xenvif_xenbus_init();
  1477. if (rc)
  1478. goto failed_init;
  1479. return 0;
  1480. failed_init:
  1481. return rc;
  1482. }
  1483. module_init(netback_init);
  1484. static void __exit netback_fini(void)
  1485. {
  1486. xenvif_xenbus_fini();
  1487. }
  1488. module_exit(netback_fini);
  1489. MODULE_LICENSE("Dual BSD/GPL");
  1490. MODULE_ALIAS("xen-backend:vif");