netback.c 44 KB

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