netback.c 49 KB

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