netback.c 48 KB

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