netback.c 53 KB

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