netback.c 49 KB

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