netback.c 57 KB

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