netback.c 56 KB

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