netback.c 55 KB

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