netback.c 53 KB

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