netback.c 53 KB

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