netback.c 57 KB

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