netback.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712
  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 <net/tcp.h>
  39. #include <xen/xen.h>
  40. #include <xen/events.h>
  41. #include <xen/interface/memory.h>
  42. #include <asm/xen/hypercall.h>
  43. #include <asm/xen/page.h>
  44. /* Provide an option to disable split event channels at load time as
  45. * event channels are limited resource. Split event channels are
  46. * enabled by default.
  47. */
  48. bool separate_tx_rx_irq = 1;
  49. module_param(separate_tx_rx_irq, bool, 0644);
  50. /*
  51. * This is the maximum slots a skb can have. If a guest sends a skb
  52. * which exceeds this limit it is considered malicious.
  53. */
  54. #define FATAL_SKB_SLOTS_DEFAULT 20
  55. static unsigned int fatal_skb_slots = FATAL_SKB_SLOTS_DEFAULT;
  56. module_param(fatal_skb_slots, uint, 0444);
  57. static void xenvif_idx_release(struct xenvif *vif, u16 pending_idx,
  58. u8 status);
  59. static void make_tx_response(struct xenvif *vif,
  60. struct xen_netif_tx_request *txp,
  61. s8 st);
  62. static inline int tx_work_todo(struct xenvif *vif);
  63. static inline int rx_work_todo(struct xenvif *vif);
  64. static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
  65. u16 id,
  66. s8 st,
  67. u16 offset,
  68. u16 size,
  69. u16 flags);
  70. static inline unsigned long idx_to_pfn(struct xenvif *vif,
  71. u16 idx)
  72. {
  73. return page_to_pfn(vif->mmap_pages[idx]);
  74. }
  75. static inline unsigned long idx_to_kaddr(struct xenvif *vif,
  76. u16 idx)
  77. {
  78. return (unsigned long)pfn_to_kaddr(idx_to_pfn(vif, idx));
  79. }
  80. /* Find the containing VIF's structure from a pointer in pending_tx_info array
  81. */
  82. static inline struct xenvif* ubuf_to_vif(struct ubuf_info *ubuf)
  83. {
  84. u16 pending_idx = ubuf->desc;
  85. struct pending_tx_info *temp =
  86. container_of(ubuf, struct pending_tx_info, callback_struct);
  87. return container_of(temp - pending_idx,
  88. struct xenvif,
  89. pending_tx_info[0]);
  90. }
  91. /* This is a miniumum size for the linear area to avoid lots of
  92. * calls to __pskb_pull_tail() as we set up checksum offsets. The
  93. * value 128 was chosen as it covers all IPv4 and most likely
  94. * IPv6 headers.
  95. */
  96. #define PKT_PROT_LEN 128
  97. static u16 frag_get_pending_idx(skb_frag_t *frag)
  98. {
  99. return (u16)frag->page_offset;
  100. }
  101. static void frag_set_pending_idx(skb_frag_t *frag, u16 pending_idx)
  102. {
  103. frag->page_offset = pending_idx;
  104. }
  105. static inline pending_ring_idx_t pending_index(unsigned i)
  106. {
  107. return i & (MAX_PENDING_REQS-1);
  108. }
  109. bool xenvif_rx_ring_slots_available(struct xenvif *vif, int needed)
  110. {
  111. RING_IDX prod, cons;
  112. do {
  113. prod = vif->rx.sring->req_prod;
  114. cons = vif->rx.req_cons;
  115. if (prod - cons >= needed)
  116. return true;
  117. vif->rx.sring->req_event = prod + 1;
  118. /* Make sure event is visible before we check prod
  119. * again.
  120. */
  121. mb();
  122. } while (vif->rx.sring->req_prod != prod);
  123. return false;
  124. }
  125. /*
  126. * Returns true if we should start a new receive buffer instead of
  127. * adding 'size' bytes to a buffer which currently contains 'offset'
  128. * bytes.
  129. */
  130. static bool start_new_rx_buffer(int offset, unsigned long size, int head)
  131. {
  132. /* simple case: we have completely filled the current buffer. */
  133. if (offset == MAX_BUFFER_OFFSET)
  134. return true;
  135. /*
  136. * complex case: start a fresh buffer if the current frag
  137. * would overflow the current buffer but only if:
  138. * (i) this frag would fit completely in the next buffer
  139. * and (ii) there is already some data in the current buffer
  140. * and (iii) this is not the head buffer.
  141. *
  142. * Where:
  143. * - (i) stops us splitting a frag into two copies
  144. * unless the frag is too large for a single buffer.
  145. * - (ii) stops us from leaving a buffer pointlessly empty.
  146. * - (iii) stops us leaving the first buffer
  147. * empty. Strictly speaking this is already covered
  148. * by (ii) but is explicitly checked because
  149. * netfront relies on the first buffer being
  150. * non-empty and can crash otherwise.
  151. *
  152. * This means we will effectively linearise small
  153. * frags but do not needlessly split large buffers
  154. * into multiple copies tend to give large frags their
  155. * own buffers as before.
  156. */
  157. if ((offset + size > MAX_BUFFER_OFFSET) &&
  158. (size <= MAX_BUFFER_OFFSET) && offset && !head)
  159. return true;
  160. return false;
  161. }
  162. struct netrx_pending_operations {
  163. unsigned copy_prod, copy_cons;
  164. unsigned meta_prod, meta_cons;
  165. struct gnttab_copy *copy;
  166. struct xenvif_rx_meta *meta;
  167. int copy_off;
  168. grant_ref_t copy_gref;
  169. };
  170. static struct xenvif_rx_meta *get_next_rx_buffer(struct xenvif *vif,
  171. struct netrx_pending_operations *npo)
  172. {
  173. struct xenvif_rx_meta *meta;
  174. struct xen_netif_rx_request *req;
  175. req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
  176. meta = npo->meta + npo->meta_prod++;
  177. meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
  178. meta->gso_size = 0;
  179. meta->size = 0;
  180. meta->id = req->id;
  181. npo->copy_off = 0;
  182. npo->copy_gref = req->gref;
  183. return meta;
  184. }
  185. /*
  186. * Set up the grant operations for this fragment. If it's a flipping
  187. * interface, we also set up the unmap request from here.
  188. */
  189. static void xenvif_gop_frag_copy(struct xenvif *vif, struct sk_buff *skb,
  190. struct netrx_pending_operations *npo,
  191. struct page *page, unsigned long size,
  192. unsigned long offset, int *head,
  193. struct xenvif *foreign_vif,
  194. grant_ref_t foreign_gref)
  195. {
  196. struct gnttab_copy *copy_gop;
  197. struct xenvif_rx_meta *meta;
  198. unsigned long bytes;
  199. int gso_type;
  200. /* Data must not cross a page boundary. */
  201. BUG_ON(size + offset > PAGE_SIZE<<compound_order(page));
  202. meta = npo->meta + npo->meta_prod - 1;
  203. /* Skip unused frames from start of page */
  204. page += offset >> PAGE_SHIFT;
  205. offset &= ~PAGE_MASK;
  206. while (size > 0) {
  207. BUG_ON(offset >= PAGE_SIZE);
  208. BUG_ON(npo->copy_off > MAX_BUFFER_OFFSET);
  209. bytes = PAGE_SIZE - offset;
  210. if (bytes > size)
  211. bytes = size;
  212. if (start_new_rx_buffer(npo->copy_off, bytes, *head)) {
  213. /*
  214. * Netfront requires there to be some data in the head
  215. * buffer.
  216. */
  217. BUG_ON(*head);
  218. meta = get_next_rx_buffer(vif, npo);
  219. }
  220. if (npo->copy_off + bytes > MAX_BUFFER_OFFSET)
  221. bytes = MAX_BUFFER_OFFSET - npo->copy_off;
  222. copy_gop = npo->copy + npo->copy_prod++;
  223. copy_gop->flags = GNTCOPY_dest_gref;
  224. copy_gop->len = bytes;
  225. if (foreign_vif) {
  226. copy_gop->source.domid = foreign_vif->domid;
  227. copy_gop->source.u.ref = foreign_gref;
  228. copy_gop->flags |= GNTCOPY_source_gref;
  229. } else {
  230. copy_gop->source.domid = DOMID_SELF;
  231. copy_gop->source.u.gmfn =
  232. virt_to_mfn(page_address(page));
  233. }
  234. copy_gop->source.offset = offset;
  235. copy_gop->dest.domid = vif->domid;
  236. copy_gop->dest.offset = npo->copy_off;
  237. copy_gop->dest.u.ref = npo->copy_gref;
  238. npo->copy_off += bytes;
  239. meta->size += bytes;
  240. offset += bytes;
  241. size -= bytes;
  242. /* Next frame */
  243. if (offset == PAGE_SIZE && size) {
  244. BUG_ON(!PageCompound(page));
  245. page++;
  246. offset = 0;
  247. }
  248. /* Leave a gap for the GSO descriptor. */
  249. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
  250. gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
  251. else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
  252. gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
  253. else
  254. gso_type = XEN_NETIF_GSO_TYPE_NONE;
  255. if (*head && ((1 << gso_type) & vif->gso_mask))
  256. vif->rx.req_cons++;
  257. *head = 0; /* There must be something in this buffer now. */
  258. }
  259. }
  260. /*
  261. * Prepare an SKB to be transmitted to the frontend.
  262. *
  263. * This function is responsible for allocating grant operations, meta
  264. * structures, etc.
  265. *
  266. * It returns the number of meta structures consumed. The number of
  267. * ring slots used is always equal to the number of meta slots used
  268. * plus the number of GSO descriptors used. Currently, we use either
  269. * zero GSO descriptors (for non-GSO packets) or one descriptor (for
  270. * frontend-side LRO).
  271. */
  272. static int xenvif_gop_skb(struct sk_buff *skb,
  273. struct netrx_pending_operations *npo)
  274. {
  275. struct xenvif *vif = netdev_priv(skb->dev);
  276. int nr_frags = skb_shinfo(skb)->nr_frags;
  277. int i;
  278. struct xen_netif_rx_request *req;
  279. struct xenvif_rx_meta *meta;
  280. unsigned char *data;
  281. int head = 1;
  282. int old_meta_prod;
  283. int gso_type;
  284. int gso_size;
  285. struct ubuf_info *ubuf = skb_shinfo(skb)->destructor_arg;
  286. grant_ref_t foreign_grefs[MAX_SKB_FRAGS];
  287. struct xenvif *foreign_vif = NULL;
  288. old_meta_prod = npo->meta_prod;
  289. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
  290. gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
  291. gso_size = skb_shinfo(skb)->gso_size;
  292. } else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
  293. gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
  294. gso_size = skb_shinfo(skb)->gso_size;
  295. } else {
  296. gso_type = XEN_NETIF_GSO_TYPE_NONE;
  297. gso_size = 0;
  298. }
  299. /* Set up a GSO prefix descriptor, if necessary */
  300. if ((1 << gso_type) & vif->gso_prefix_mask) {
  301. req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
  302. meta = npo->meta + npo->meta_prod++;
  303. meta->gso_type = gso_type;
  304. meta->gso_size = gso_size;
  305. meta->size = 0;
  306. meta->id = req->id;
  307. }
  308. req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
  309. meta = npo->meta + npo->meta_prod++;
  310. if ((1 << gso_type) & vif->gso_mask) {
  311. meta->gso_type = gso_type;
  312. meta->gso_size = gso_size;
  313. } else {
  314. meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
  315. meta->gso_size = 0;
  316. }
  317. meta->size = 0;
  318. meta->id = req->id;
  319. npo->copy_off = 0;
  320. npo->copy_gref = req->gref;
  321. if ((skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) &&
  322. (ubuf->callback == &xenvif_zerocopy_callback)) {
  323. int i = 0;
  324. foreign_vif = ubuf_to_vif(ubuf);
  325. do {
  326. u16 pending_idx = ubuf->desc;
  327. foreign_grefs[i++] =
  328. foreign_vif->pending_tx_info[pending_idx].req.gref;
  329. ubuf = (struct ubuf_info *) ubuf->ctx;
  330. } while (ubuf);
  331. }
  332. data = skb->data;
  333. while (data < skb_tail_pointer(skb)) {
  334. unsigned int offset = offset_in_page(data);
  335. unsigned int len = PAGE_SIZE - offset;
  336. if (data + len > skb_tail_pointer(skb))
  337. len = skb_tail_pointer(skb) - data;
  338. xenvif_gop_frag_copy(vif, skb, npo,
  339. virt_to_page(data), len, offset, &head,
  340. NULL,
  341. 0);
  342. data += len;
  343. }
  344. for (i = 0; i < nr_frags; i++) {
  345. xenvif_gop_frag_copy(vif, skb, npo,
  346. skb_frag_page(&skb_shinfo(skb)->frags[i]),
  347. skb_frag_size(&skb_shinfo(skb)->frags[i]),
  348. skb_shinfo(skb)->frags[i].page_offset,
  349. &head,
  350. foreign_vif,
  351. foreign_grefs[i]);
  352. }
  353. return npo->meta_prod - old_meta_prod;
  354. }
  355. /*
  356. * This is a twin to xenvif_gop_skb. Assume that xenvif_gop_skb was
  357. * used to set up the operations on the top of
  358. * netrx_pending_operations, which have since been done. Check that
  359. * they didn't give any errors and advance over them.
  360. */
  361. static int xenvif_check_gop(struct xenvif *vif, int nr_meta_slots,
  362. struct netrx_pending_operations *npo)
  363. {
  364. struct gnttab_copy *copy_op;
  365. int status = XEN_NETIF_RSP_OKAY;
  366. int i;
  367. for (i = 0; i < nr_meta_slots; i++) {
  368. copy_op = npo->copy + npo->copy_cons++;
  369. if (copy_op->status != GNTST_okay) {
  370. netdev_dbg(vif->dev,
  371. "Bad status %d from copy to DOM%d.\n",
  372. copy_op->status, vif->domid);
  373. status = XEN_NETIF_RSP_ERROR;
  374. }
  375. }
  376. return status;
  377. }
  378. static void xenvif_add_frag_responses(struct xenvif *vif, int status,
  379. struct xenvif_rx_meta *meta,
  380. int nr_meta_slots)
  381. {
  382. int i;
  383. unsigned long offset;
  384. /* No fragments used */
  385. if (nr_meta_slots <= 1)
  386. return;
  387. nr_meta_slots--;
  388. for (i = 0; i < nr_meta_slots; i++) {
  389. int flags;
  390. if (i == nr_meta_slots - 1)
  391. flags = 0;
  392. else
  393. flags = XEN_NETRXF_more_data;
  394. offset = 0;
  395. make_rx_response(vif, meta[i].id, status, offset,
  396. meta[i].size, flags);
  397. }
  398. }
  399. struct xenvif_rx_cb {
  400. int meta_slots_used;
  401. };
  402. #define XENVIF_RX_CB(skb) ((struct xenvif_rx_cb *)(skb)->cb)
  403. void xenvif_kick_thread(struct xenvif *vif)
  404. {
  405. wake_up(&vif->wq);
  406. }
  407. static void xenvif_rx_action(struct xenvif *vif)
  408. {
  409. s8 status;
  410. u16 flags;
  411. struct xen_netif_rx_response *resp;
  412. struct sk_buff_head rxq;
  413. struct sk_buff *skb;
  414. LIST_HEAD(notify);
  415. int ret;
  416. unsigned long offset;
  417. bool need_to_notify = false;
  418. struct netrx_pending_operations npo = {
  419. .copy = vif->grant_copy_op,
  420. .meta = vif->meta,
  421. };
  422. skb_queue_head_init(&rxq);
  423. while ((skb = skb_dequeue(&vif->rx_queue)) != NULL) {
  424. RING_IDX max_slots_needed;
  425. int i;
  426. /* We need a cheap worse case estimate for the number of
  427. * slots we'll use.
  428. */
  429. max_slots_needed = DIV_ROUND_UP(offset_in_page(skb->data) +
  430. skb_headlen(skb),
  431. PAGE_SIZE);
  432. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  433. unsigned int size;
  434. size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
  435. max_slots_needed += DIV_ROUND_UP(size, PAGE_SIZE);
  436. }
  437. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4 ||
  438. skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
  439. max_slots_needed++;
  440. /* If the skb may not fit then bail out now */
  441. if (!xenvif_rx_ring_slots_available(vif, max_slots_needed)) {
  442. skb_queue_head(&vif->rx_queue, skb);
  443. need_to_notify = true;
  444. vif->rx_last_skb_slots = max_slots_needed;
  445. break;
  446. } else
  447. vif->rx_last_skb_slots = 0;
  448. XENVIF_RX_CB(skb)->meta_slots_used = xenvif_gop_skb(skb, &npo);
  449. BUG_ON(XENVIF_RX_CB(skb)->meta_slots_used > max_slots_needed);
  450. __skb_queue_tail(&rxq, skb);
  451. }
  452. BUG_ON(npo.meta_prod > ARRAY_SIZE(vif->meta));
  453. if (!npo.copy_prod)
  454. goto done;
  455. BUG_ON(npo.copy_prod > MAX_GRANT_COPY_OPS);
  456. gnttab_batch_copy(vif->grant_copy_op, npo.copy_prod);
  457. while ((skb = __skb_dequeue(&rxq)) != NULL) {
  458. if ((1 << vif->meta[npo.meta_cons].gso_type) &
  459. vif->gso_prefix_mask) {
  460. resp = RING_GET_RESPONSE(&vif->rx,
  461. vif->rx.rsp_prod_pvt++);
  462. resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
  463. resp->offset = vif->meta[npo.meta_cons].gso_size;
  464. resp->id = vif->meta[npo.meta_cons].id;
  465. resp->status = XENVIF_RX_CB(skb)->meta_slots_used;
  466. npo.meta_cons++;
  467. XENVIF_RX_CB(skb)->meta_slots_used--;
  468. }
  469. vif->dev->stats.tx_bytes += skb->len;
  470. vif->dev->stats.tx_packets++;
  471. status = xenvif_check_gop(vif,
  472. XENVIF_RX_CB(skb)->meta_slots_used,
  473. &npo);
  474. if (XENVIF_RX_CB(skb)->meta_slots_used == 1)
  475. flags = 0;
  476. else
  477. flags = XEN_NETRXF_more_data;
  478. if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */
  479. flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated;
  480. else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
  481. /* remote but checksummed. */
  482. flags |= XEN_NETRXF_data_validated;
  483. offset = 0;
  484. resp = make_rx_response(vif, vif->meta[npo.meta_cons].id,
  485. status, offset,
  486. vif->meta[npo.meta_cons].size,
  487. flags);
  488. if ((1 << vif->meta[npo.meta_cons].gso_type) &
  489. vif->gso_mask) {
  490. struct xen_netif_extra_info *gso =
  491. (struct xen_netif_extra_info *)
  492. RING_GET_RESPONSE(&vif->rx,
  493. vif->rx.rsp_prod_pvt++);
  494. resp->flags |= XEN_NETRXF_extra_info;
  495. gso->u.gso.type = vif->meta[npo.meta_cons].gso_type;
  496. gso->u.gso.size = vif->meta[npo.meta_cons].gso_size;
  497. gso->u.gso.pad = 0;
  498. gso->u.gso.features = 0;
  499. gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
  500. gso->flags = 0;
  501. }
  502. xenvif_add_frag_responses(vif, status,
  503. vif->meta + npo.meta_cons + 1,
  504. XENVIF_RX_CB(skb)->meta_slots_used);
  505. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->rx, ret);
  506. need_to_notify |= !!ret;
  507. npo.meta_cons += XENVIF_RX_CB(skb)->meta_slots_used;
  508. dev_kfree_skb(skb);
  509. }
  510. done:
  511. if (need_to_notify)
  512. notify_remote_via_irq(vif->rx_irq);
  513. }
  514. void xenvif_check_rx_xenvif(struct xenvif *vif)
  515. {
  516. int more_to_do;
  517. RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do);
  518. if (more_to_do)
  519. napi_schedule(&vif->napi);
  520. }
  521. static void tx_add_credit(struct xenvif *vif)
  522. {
  523. unsigned long max_burst, max_credit;
  524. /*
  525. * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
  526. * Otherwise the interface can seize up due to insufficient credit.
  527. */
  528. max_burst = RING_GET_REQUEST(&vif->tx, vif->tx.req_cons)->size;
  529. max_burst = min(max_burst, 131072UL);
  530. max_burst = max(max_burst, vif->credit_bytes);
  531. /* Take care that adding a new chunk of credit doesn't wrap to zero. */
  532. max_credit = vif->remaining_credit + vif->credit_bytes;
  533. if (max_credit < vif->remaining_credit)
  534. max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
  535. vif->remaining_credit = min(max_credit, max_burst);
  536. }
  537. static void tx_credit_callback(unsigned long data)
  538. {
  539. struct xenvif *vif = (struct xenvif *)data;
  540. tx_add_credit(vif);
  541. xenvif_check_rx_xenvif(vif);
  542. }
  543. static void xenvif_tx_err(struct xenvif *vif,
  544. struct xen_netif_tx_request *txp, RING_IDX end)
  545. {
  546. RING_IDX cons = vif->tx.req_cons;
  547. unsigned long flags;
  548. do {
  549. spin_lock_irqsave(&vif->response_lock, flags);
  550. make_tx_response(vif, txp, XEN_NETIF_RSP_ERROR);
  551. spin_unlock_irqrestore(&vif->response_lock, flags);
  552. if (cons == end)
  553. break;
  554. txp = RING_GET_REQUEST(&vif->tx, cons++);
  555. } while (1);
  556. vif->tx.req_cons = cons;
  557. }
  558. static void xenvif_fatal_tx_err(struct xenvif *vif)
  559. {
  560. netdev_err(vif->dev, "fatal error; disabling device\n");
  561. xenvif_carrier_off(vif);
  562. }
  563. static int xenvif_count_requests(struct xenvif *vif,
  564. struct xen_netif_tx_request *first,
  565. struct xen_netif_tx_request *txp,
  566. int work_to_do)
  567. {
  568. RING_IDX cons = vif->tx.req_cons;
  569. int slots = 0;
  570. int drop_err = 0;
  571. int more_data;
  572. if (!(first->flags & XEN_NETTXF_more_data))
  573. return 0;
  574. do {
  575. struct xen_netif_tx_request dropped_tx = { 0 };
  576. if (slots >= work_to_do) {
  577. netdev_err(vif->dev,
  578. "Asked for %d slots but exceeds this limit\n",
  579. work_to_do);
  580. xenvif_fatal_tx_err(vif);
  581. return -ENODATA;
  582. }
  583. /* This guest is really using too many slots and
  584. * considered malicious.
  585. */
  586. if (unlikely(slots >= fatal_skb_slots)) {
  587. netdev_err(vif->dev,
  588. "Malicious frontend using %d slots, threshold %u\n",
  589. slots, fatal_skb_slots);
  590. xenvif_fatal_tx_err(vif);
  591. return -E2BIG;
  592. }
  593. /* Xen network protocol had implicit dependency on
  594. * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
  595. * the historical MAX_SKB_FRAGS value 18 to honor the
  596. * same behavior as before. Any packet using more than
  597. * 18 slots but less than fatal_skb_slots slots is
  598. * dropped
  599. */
  600. if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
  601. if (net_ratelimit())
  602. netdev_dbg(vif->dev,
  603. "Too many slots (%d) exceeding limit (%d), dropping packet\n",
  604. slots, XEN_NETBK_LEGACY_SLOTS_MAX);
  605. drop_err = -E2BIG;
  606. }
  607. if (drop_err)
  608. txp = &dropped_tx;
  609. memcpy(txp, RING_GET_REQUEST(&vif->tx, cons + slots),
  610. sizeof(*txp));
  611. /* If the guest submitted a frame >= 64 KiB then
  612. * first->size overflowed and following slots will
  613. * appear to be larger than the frame.
  614. *
  615. * This cannot be fatal error as there are buggy
  616. * frontends that do this.
  617. *
  618. * Consume all slots and drop the packet.
  619. */
  620. if (!drop_err && txp->size > first->size) {
  621. if (net_ratelimit())
  622. netdev_dbg(vif->dev,
  623. "Invalid tx request, slot size %u > remaining size %u\n",
  624. txp->size, first->size);
  625. drop_err = -EIO;
  626. }
  627. first->size -= txp->size;
  628. slots++;
  629. if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
  630. netdev_err(vif->dev, "Cross page boundary, txp->offset: %x, size: %u\n",
  631. txp->offset, txp->size);
  632. xenvif_fatal_tx_err(vif);
  633. return -EINVAL;
  634. }
  635. more_data = txp->flags & XEN_NETTXF_more_data;
  636. if (!drop_err)
  637. txp++;
  638. } while (more_data);
  639. if (drop_err) {
  640. xenvif_tx_err(vif, first, cons + slots);
  641. return drop_err;
  642. }
  643. return slots;
  644. }
  645. struct xenvif_tx_cb {
  646. u16 pending_idx;
  647. };
  648. #define XENVIF_TX_CB(skb) ((struct xenvif_tx_cb *)(skb)->cb)
  649. static inline void xenvif_tx_create_gop(struct xenvif *vif,
  650. u16 pending_idx,
  651. struct xen_netif_tx_request *txp,
  652. struct gnttab_map_grant_ref *gop)
  653. {
  654. vif->pages_to_map[gop-vif->tx_map_ops] = vif->mmap_pages[pending_idx];
  655. gnttab_set_map_op(gop, idx_to_kaddr(vif, pending_idx),
  656. GNTMAP_host_map | GNTMAP_readonly,
  657. txp->gref, vif->domid);
  658. memcpy(&vif->pending_tx_info[pending_idx].req, txp,
  659. sizeof(*txp));
  660. }
  661. static struct gnttab_map_grant_ref *xenvif_get_requests(struct xenvif *vif,
  662. struct sk_buff *skb,
  663. struct xen_netif_tx_request *txp,
  664. struct gnttab_map_grant_ref *gop)
  665. {
  666. struct skb_shared_info *shinfo = skb_shinfo(skb);
  667. skb_frag_t *frags = shinfo->frags;
  668. u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
  669. int start;
  670. pending_ring_idx_t index;
  671. unsigned int nr_slots;
  672. /* At this point shinfo->nr_frags is in fact the number of
  673. * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
  674. */
  675. nr_slots = shinfo->nr_frags;
  676. /* Skip first skb fragment if it is on same page as header fragment. */
  677. start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
  678. for (shinfo->nr_frags = start; shinfo->nr_frags < nr_slots;
  679. shinfo->nr_frags++, txp++, gop++) {
  680. index = pending_index(vif->pending_cons++);
  681. pending_idx = vif->pending_ring[index];
  682. xenvif_tx_create_gop(vif, pending_idx, txp, gop);
  683. frag_set_pending_idx(&frags[shinfo->nr_frags], pending_idx);
  684. }
  685. BUG_ON(shinfo->nr_frags > MAX_SKB_FRAGS);
  686. return gop;
  687. }
  688. static inline void xenvif_grant_handle_set(struct xenvif *vif,
  689. u16 pending_idx,
  690. grant_handle_t handle)
  691. {
  692. if (unlikely(vif->grant_tx_handle[pending_idx] !=
  693. NETBACK_INVALID_HANDLE)) {
  694. netdev_err(vif->dev,
  695. "Trying to overwrite active handle! pending_idx: %x\n",
  696. pending_idx);
  697. BUG();
  698. }
  699. vif->grant_tx_handle[pending_idx] = handle;
  700. }
  701. static inline void xenvif_grant_handle_reset(struct xenvif *vif,
  702. u16 pending_idx)
  703. {
  704. if (unlikely(vif->grant_tx_handle[pending_idx] ==
  705. NETBACK_INVALID_HANDLE)) {
  706. netdev_err(vif->dev,
  707. "Trying to unmap invalid handle! pending_idx: %x\n",
  708. pending_idx);
  709. BUG();
  710. }
  711. vif->grant_tx_handle[pending_idx] = NETBACK_INVALID_HANDLE;
  712. }
  713. static int xenvif_tx_check_gop(struct xenvif *vif,
  714. struct sk_buff *skb,
  715. struct gnttab_map_grant_ref **gopp)
  716. {
  717. struct gnttab_map_grant_ref *gop = *gopp;
  718. u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
  719. struct skb_shared_info *shinfo = skb_shinfo(skb);
  720. struct pending_tx_info *tx_info;
  721. int nr_frags = shinfo->nr_frags;
  722. int i, err, start;
  723. /* Check status of header. */
  724. err = gop->status;
  725. if (unlikely(err))
  726. xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
  727. else
  728. xenvif_grant_handle_set(vif, pending_idx , gop->handle);
  729. /* Skip first skb fragment if it is on same page as header fragment. */
  730. start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
  731. for (i = start; i < nr_frags; i++) {
  732. int j, newerr;
  733. pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
  734. tx_info = &vif->pending_tx_info[pending_idx];
  735. /* Check error status: if okay then remember grant handle. */
  736. newerr = (++gop)->status;
  737. if (likely(!newerr)) {
  738. xenvif_grant_handle_set(vif, pending_idx , gop->handle);
  739. /* Had a previous error? Invalidate this fragment. */
  740. if (unlikely(err))
  741. xenvif_idx_unmap(vif, pending_idx);
  742. continue;
  743. }
  744. /* Error on this fragment: respond to client with an error. */
  745. xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
  746. /* Not the first error? Preceding frags already invalidated. */
  747. if (err)
  748. continue;
  749. /* First error: invalidate header and preceding fragments. */
  750. pending_idx = XENVIF_TX_CB(skb)->pending_idx;
  751. xenvif_idx_unmap(vif, pending_idx);
  752. for (j = start; j < i; j++) {
  753. pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
  754. xenvif_idx_unmap(vif, pending_idx);
  755. }
  756. /* Remember the error: invalidate all subsequent fragments. */
  757. err = newerr;
  758. }
  759. *gopp = gop + 1;
  760. return err;
  761. }
  762. static void xenvif_fill_frags(struct xenvif *vif, struct sk_buff *skb)
  763. {
  764. struct skb_shared_info *shinfo = skb_shinfo(skb);
  765. int nr_frags = shinfo->nr_frags;
  766. int i;
  767. u16 prev_pending_idx = INVALID_PENDING_IDX;
  768. if (skb_shinfo(skb)->destructor_arg)
  769. prev_pending_idx = XENVIF_TX_CB(skb)->pending_idx;
  770. for (i = 0; i < nr_frags; i++) {
  771. skb_frag_t *frag = shinfo->frags + i;
  772. struct xen_netif_tx_request *txp;
  773. struct page *page;
  774. u16 pending_idx;
  775. pending_idx = frag_get_pending_idx(frag);
  776. /* If this is not the first frag, chain it to the previous*/
  777. if (unlikely(prev_pending_idx == INVALID_PENDING_IDX))
  778. skb_shinfo(skb)->destructor_arg =
  779. &vif->pending_tx_info[pending_idx].callback_struct;
  780. else if (likely(pending_idx != prev_pending_idx))
  781. vif->pending_tx_info[prev_pending_idx].callback_struct.ctx =
  782. &(vif->pending_tx_info[pending_idx].callback_struct);
  783. vif->pending_tx_info[pending_idx].callback_struct.ctx = NULL;
  784. prev_pending_idx = pending_idx;
  785. txp = &vif->pending_tx_info[pending_idx].req;
  786. page = virt_to_page(idx_to_kaddr(vif, pending_idx));
  787. __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
  788. skb->len += txp->size;
  789. skb->data_len += txp->size;
  790. skb->truesize += txp->size;
  791. /* Take an extra reference to offset network stack's put_page */
  792. get_page(vif->mmap_pages[pending_idx]);
  793. }
  794. /* FIXME: __skb_fill_page_desc set this to true because page->pfmemalloc
  795. * overlaps with "index", and "mapping" is not set. I think mapping
  796. * should be set. If delivered to local stack, it would drop this
  797. * skb in sk_filter unless the socket has the right to use it.
  798. */
  799. skb->pfmemalloc = false;
  800. }
  801. static int xenvif_get_extras(struct xenvif *vif,
  802. struct xen_netif_extra_info *extras,
  803. int work_to_do)
  804. {
  805. struct xen_netif_extra_info extra;
  806. RING_IDX cons = vif->tx.req_cons;
  807. do {
  808. if (unlikely(work_to_do-- <= 0)) {
  809. netdev_err(vif->dev, "Missing extra info\n");
  810. xenvif_fatal_tx_err(vif);
  811. return -EBADR;
  812. }
  813. memcpy(&extra, RING_GET_REQUEST(&vif->tx, cons),
  814. sizeof(extra));
  815. if (unlikely(!extra.type ||
  816. extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
  817. vif->tx.req_cons = ++cons;
  818. netdev_err(vif->dev,
  819. "Invalid extra type: %d\n", extra.type);
  820. xenvif_fatal_tx_err(vif);
  821. return -EINVAL;
  822. }
  823. memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
  824. vif->tx.req_cons = ++cons;
  825. } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
  826. return work_to_do;
  827. }
  828. static int xenvif_set_skb_gso(struct xenvif *vif,
  829. struct sk_buff *skb,
  830. struct xen_netif_extra_info *gso)
  831. {
  832. if (!gso->u.gso.size) {
  833. netdev_err(vif->dev, "GSO size must not be zero.\n");
  834. xenvif_fatal_tx_err(vif);
  835. return -EINVAL;
  836. }
  837. switch (gso->u.gso.type) {
  838. case XEN_NETIF_GSO_TYPE_TCPV4:
  839. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  840. break;
  841. case XEN_NETIF_GSO_TYPE_TCPV6:
  842. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  843. break;
  844. default:
  845. netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
  846. xenvif_fatal_tx_err(vif);
  847. return -EINVAL;
  848. }
  849. skb_shinfo(skb)->gso_size = gso->u.gso.size;
  850. /* gso_segs will be calculated later */
  851. return 0;
  852. }
  853. static int checksum_setup(struct xenvif *vif, struct sk_buff *skb)
  854. {
  855. bool recalculate_partial_csum = false;
  856. /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
  857. * peers can fail to set NETRXF_csum_blank when sending a GSO
  858. * frame. In this case force the SKB to CHECKSUM_PARTIAL and
  859. * recalculate the partial checksum.
  860. */
  861. if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
  862. vif->rx_gso_checksum_fixup++;
  863. skb->ip_summed = CHECKSUM_PARTIAL;
  864. recalculate_partial_csum = true;
  865. }
  866. /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
  867. if (skb->ip_summed != CHECKSUM_PARTIAL)
  868. return 0;
  869. return skb_checksum_setup(skb, recalculate_partial_csum);
  870. }
  871. static bool tx_credit_exceeded(struct xenvif *vif, unsigned size)
  872. {
  873. u64 now = get_jiffies_64();
  874. u64 next_credit = vif->credit_window_start +
  875. msecs_to_jiffies(vif->credit_usec / 1000);
  876. /* Timer could already be pending in rare cases. */
  877. if (timer_pending(&vif->credit_timeout))
  878. return true;
  879. /* Passed the point where we can replenish credit? */
  880. if (time_after_eq64(now, next_credit)) {
  881. vif->credit_window_start = now;
  882. tx_add_credit(vif);
  883. }
  884. /* Still too big to send right now? Set a callback. */
  885. if (size > vif->remaining_credit) {
  886. vif->credit_timeout.data =
  887. (unsigned long)vif;
  888. vif->credit_timeout.function =
  889. tx_credit_callback;
  890. mod_timer(&vif->credit_timeout,
  891. next_credit);
  892. vif->credit_window_start = next_credit;
  893. return true;
  894. }
  895. return false;
  896. }
  897. static unsigned xenvif_tx_build_gops(struct xenvif *vif, int budget)
  898. {
  899. struct gnttab_map_grant_ref *gop = vif->tx_map_ops, *request_gop;
  900. struct sk_buff *skb;
  901. int ret;
  902. while (xenvif_tx_pending_slots_available(vif) &&
  903. (skb_queue_len(&vif->tx_queue) < budget)) {
  904. struct xen_netif_tx_request txreq;
  905. struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
  906. struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
  907. u16 pending_idx;
  908. RING_IDX idx;
  909. int work_to_do;
  910. unsigned int data_len;
  911. pending_ring_idx_t index;
  912. if (vif->tx.sring->req_prod - vif->tx.req_cons >
  913. XEN_NETIF_TX_RING_SIZE) {
  914. netdev_err(vif->dev,
  915. "Impossible number of requests. "
  916. "req_prod %d, req_cons %d, size %ld\n",
  917. vif->tx.sring->req_prod, vif->tx.req_cons,
  918. XEN_NETIF_TX_RING_SIZE);
  919. xenvif_fatal_tx_err(vif);
  920. continue;
  921. }
  922. work_to_do = RING_HAS_UNCONSUMED_REQUESTS(&vif->tx);
  923. if (!work_to_do)
  924. break;
  925. idx = vif->tx.req_cons;
  926. rmb(); /* Ensure that we see the request before we copy it. */
  927. memcpy(&txreq, RING_GET_REQUEST(&vif->tx, idx), sizeof(txreq));
  928. /* Credit-based scheduling. */
  929. if (txreq.size > vif->remaining_credit &&
  930. tx_credit_exceeded(vif, txreq.size))
  931. break;
  932. vif->remaining_credit -= txreq.size;
  933. work_to_do--;
  934. vif->tx.req_cons = ++idx;
  935. memset(extras, 0, sizeof(extras));
  936. if (txreq.flags & XEN_NETTXF_extra_info) {
  937. work_to_do = xenvif_get_extras(vif, extras,
  938. work_to_do);
  939. idx = vif->tx.req_cons;
  940. if (unlikely(work_to_do < 0))
  941. break;
  942. }
  943. ret = xenvif_count_requests(vif, &txreq, txfrags, work_to_do);
  944. if (unlikely(ret < 0))
  945. break;
  946. idx += ret;
  947. if (unlikely(txreq.size < ETH_HLEN)) {
  948. netdev_dbg(vif->dev,
  949. "Bad packet size: %d\n", txreq.size);
  950. xenvif_tx_err(vif, &txreq, idx);
  951. break;
  952. }
  953. /* No crossing a page as the payload mustn't fragment. */
  954. if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
  955. netdev_err(vif->dev,
  956. "txreq.offset: %x, size: %u, end: %lu\n",
  957. txreq.offset, txreq.size,
  958. (txreq.offset&~PAGE_MASK) + txreq.size);
  959. xenvif_fatal_tx_err(vif);
  960. break;
  961. }
  962. index = pending_index(vif->pending_cons);
  963. pending_idx = vif->pending_ring[index];
  964. data_len = (txreq.size > PKT_PROT_LEN &&
  965. ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
  966. PKT_PROT_LEN : txreq.size;
  967. skb = alloc_skb(data_len + NET_SKB_PAD + NET_IP_ALIGN,
  968. GFP_ATOMIC | __GFP_NOWARN);
  969. if (unlikely(skb == NULL)) {
  970. netdev_dbg(vif->dev,
  971. "Can't allocate a skb in start_xmit.\n");
  972. xenvif_tx_err(vif, &txreq, idx);
  973. break;
  974. }
  975. /* Packets passed to netif_rx() must have some headroom. */
  976. skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
  977. if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
  978. struct xen_netif_extra_info *gso;
  979. gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
  980. if (xenvif_set_skb_gso(vif, skb, gso)) {
  981. /* Failure in xenvif_set_skb_gso is fatal. */
  982. kfree_skb(skb);
  983. break;
  984. }
  985. }
  986. xenvif_tx_create_gop(vif, pending_idx, &txreq, gop);
  987. gop++;
  988. XENVIF_TX_CB(skb)->pending_idx = pending_idx;
  989. __skb_put(skb, data_len);
  990. skb_shinfo(skb)->nr_frags = ret;
  991. if (data_len < txreq.size) {
  992. skb_shinfo(skb)->nr_frags++;
  993. frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
  994. pending_idx);
  995. } else {
  996. frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
  997. INVALID_PENDING_IDX);
  998. }
  999. vif->pending_cons++;
  1000. request_gop = xenvif_get_requests(vif, skb, txfrags, gop);
  1001. if (request_gop == NULL) {
  1002. kfree_skb(skb);
  1003. xenvif_tx_err(vif, &txreq, idx);
  1004. break;
  1005. }
  1006. gop = request_gop;
  1007. __skb_queue_tail(&vif->tx_queue, skb);
  1008. vif->tx.req_cons = idx;
  1009. if ((gop-vif->tx_map_ops) >= ARRAY_SIZE(vif->tx_map_ops))
  1010. break;
  1011. }
  1012. return gop - vif->tx_map_ops;
  1013. }
  1014. static int xenvif_tx_submit(struct xenvif *vif)
  1015. {
  1016. struct gnttab_map_grant_ref *gop = vif->tx_map_ops;
  1017. struct sk_buff *skb;
  1018. int work_done = 0;
  1019. while ((skb = __skb_dequeue(&vif->tx_queue)) != NULL) {
  1020. struct xen_netif_tx_request *txp;
  1021. u16 pending_idx;
  1022. unsigned data_len;
  1023. pending_idx = XENVIF_TX_CB(skb)->pending_idx;
  1024. txp = &vif->pending_tx_info[pending_idx].req;
  1025. /* Check the remap error code. */
  1026. if (unlikely(xenvif_tx_check_gop(vif, skb, &gop))) {
  1027. netdev_dbg(vif->dev, "netback grant failed.\n");
  1028. skb_shinfo(skb)->nr_frags = 0;
  1029. kfree_skb(skb);
  1030. continue;
  1031. }
  1032. data_len = skb->len;
  1033. memcpy(skb->data,
  1034. (void *)(idx_to_kaddr(vif, pending_idx)|txp->offset),
  1035. data_len);
  1036. vif->pending_tx_info[pending_idx].callback_struct.ctx = NULL;
  1037. if (data_len < txp->size) {
  1038. /* Append the packet payload as a fragment. */
  1039. txp->offset += data_len;
  1040. txp->size -= data_len;
  1041. skb_shinfo(skb)->destructor_arg =
  1042. &vif->pending_tx_info[pending_idx].callback_struct;
  1043. } else {
  1044. /* Schedule a response immediately. */
  1045. skb_shinfo(skb)->destructor_arg = NULL;
  1046. xenvif_idx_unmap(vif, pending_idx);
  1047. }
  1048. if (txp->flags & XEN_NETTXF_csum_blank)
  1049. skb->ip_summed = CHECKSUM_PARTIAL;
  1050. else if (txp->flags & XEN_NETTXF_data_validated)
  1051. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1052. xenvif_fill_frags(vif, skb);
  1053. if (skb_is_nonlinear(skb) && skb_headlen(skb) < PKT_PROT_LEN) {
  1054. int target = min_t(int, skb->len, PKT_PROT_LEN);
  1055. __pskb_pull_tail(skb, target - skb_headlen(skb));
  1056. }
  1057. skb->dev = vif->dev;
  1058. skb->protocol = eth_type_trans(skb, skb->dev);
  1059. skb_reset_network_header(skb);
  1060. if (checksum_setup(vif, skb)) {
  1061. netdev_dbg(vif->dev,
  1062. "Can't setup checksum in net_tx_action\n");
  1063. /* We have to set this flag to trigger the callback */
  1064. if (skb_shinfo(skb)->destructor_arg)
  1065. skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
  1066. kfree_skb(skb);
  1067. continue;
  1068. }
  1069. skb_probe_transport_header(skb, 0);
  1070. /* If the packet is GSO then we will have just set up the
  1071. * transport header offset in checksum_setup so it's now
  1072. * straightforward to calculate gso_segs.
  1073. */
  1074. if (skb_is_gso(skb)) {
  1075. int mss = skb_shinfo(skb)->gso_size;
  1076. int hdrlen = skb_transport_header(skb) -
  1077. skb_mac_header(skb) +
  1078. tcp_hdrlen(skb);
  1079. skb_shinfo(skb)->gso_segs =
  1080. DIV_ROUND_UP(skb->len - hdrlen, mss);
  1081. }
  1082. vif->dev->stats.rx_bytes += skb->len;
  1083. vif->dev->stats.rx_packets++;
  1084. work_done++;
  1085. /* Set this flag right before netif_receive_skb, otherwise
  1086. * someone might think this packet already left netback, and
  1087. * do a skb_copy_ubufs while we are still in control of the
  1088. * skb. E.g. the __pskb_pull_tail earlier can do such thing.
  1089. */
  1090. if (skb_shinfo(skb)->destructor_arg) {
  1091. skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
  1092. vif->tx_zerocopy_sent++;
  1093. }
  1094. netif_receive_skb(skb);
  1095. }
  1096. return work_done;
  1097. }
  1098. void xenvif_zerocopy_callback(struct ubuf_info *ubuf, bool zerocopy_success)
  1099. {
  1100. unsigned long flags;
  1101. pending_ring_idx_t index;
  1102. struct xenvif *vif = ubuf_to_vif(ubuf);
  1103. /* This is the only place where we grab this lock, to protect callbacks
  1104. * from each other.
  1105. */
  1106. spin_lock_irqsave(&vif->callback_lock, flags);
  1107. do {
  1108. u16 pending_idx = ubuf->desc;
  1109. ubuf = (struct ubuf_info *) ubuf->ctx;
  1110. BUG_ON(vif->dealloc_prod - vif->dealloc_cons >=
  1111. MAX_PENDING_REQS);
  1112. index = pending_index(vif->dealloc_prod);
  1113. vif->dealloc_ring[index] = pending_idx;
  1114. /* Sync with xenvif_tx_dealloc_action:
  1115. * insert idx then incr producer.
  1116. */
  1117. smp_wmb();
  1118. vif->dealloc_prod++;
  1119. } while (ubuf);
  1120. wake_up(&vif->dealloc_wq);
  1121. spin_unlock_irqrestore(&vif->callback_lock, flags);
  1122. if (RING_HAS_UNCONSUMED_REQUESTS(&vif->tx) &&
  1123. xenvif_tx_pending_slots_available(vif)) {
  1124. local_bh_disable();
  1125. napi_schedule(&vif->napi);
  1126. local_bh_enable();
  1127. }
  1128. if (likely(zerocopy_success))
  1129. vif->tx_zerocopy_success++;
  1130. else
  1131. vif->tx_zerocopy_fail++;
  1132. }
  1133. static inline void xenvif_tx_dealloc_action(struct xenvif *vif)
  1134. {
  1135. struct gnttab_unmap_grant_ref *gop;
  1136. pending_ring_idx_t dc, dp;
  1137. u16 pending_idx, pending_idx_release[MAX_PENDING_REQS];
  1138. unsigned int i = 0;
  1139. dc = vif->dealloc_cons;
  1140. gop = vif->tx_unmap_ops;
  1141. /* Free up any grants we have finished using */
  1142. do {
  1143. dp = vif->dealloc_prod;
  1144. /* Ensure we see all indices enqueued by all
  1145. * xenvif_zerocopy_callback().
  1146. */
  1147. smp_rmb();
  1148. while (dc != dp) {
  1149. BUG_ON(gop - vif->tx_unmap_ops > MAX_PENDING_REQS);
  1150. pending_idx =
  1151. vif->dealloc_ring[pending_index(dc++)];
  1152. pending_idx_release[gop-vif->tx_unmap_ops] =
  1153. pending_idx;
  1154. vif->pages_to_unmap[gop-vif->tx_unmap_ops] =
  1155. vif->mmap_pages[pending_idx];
  1156. gnttab_set_unmap_op(gop,
  1157. idx_to_kaddr(vif, pending_idx),
  1158. GNTMAP_host_map,
  1159. vif->grant_tx_handle[pending_idx]);
  1160. /* Btw. already unmapped? */
  1161. xenvif_grant_handle_reset(vif, pending_idx);
  1162. ++gop;
  1163. }
  1164. } while (dp != vif->dealloc_prod);
  1165. vif->dealloc_cons = dc;
  1166. if (gop - vif->tx_unmap_ops > 0) {
  1167. int ret;
  1168. ret = gnttab_unmap_refs(vif->tx_unmap_ops,
  1169. NULL,
  1170. vif->pages_to_unmap,
  1171. gop - vif->tx_unmap_ops);
  1172. if (ret) {
  1173. netdev_err(vif->dev, "Unmap fail: nr_ops %x ret %d\n",
  1174. gop - vif->tx_unmap_ops, ret);
  1175. for (i = 0; i < gop - vif->tx_unmap_ops; ++i) {
  1176. if (gop[i].status != GNTST_okay)
  1177. netdev_err(vif->dev,
  1178. " host_addr: %llx handle: %x status: %d\n",
  1179. gop[i].host_addr,
  1180. gop[i].handle,
  1181. gop[i].status);
  1182. }
  1183. BUG();
  1184. }
  1185. }
  1186. for (i = 0; i < gop - vif->tx_unmap_ops; ++i)
  1187. xenvif_idx_release(vif, pending_idx_release[i],
  1188. XEN_NETIF_RSP_OKAY);
  1189. }
  1190. /* Called after netfront has transmitted */
  1191. int xenvif_tx_action(struct xenvif *vif, int budget)
  1192. {
  1193. unsigned nr_gops;
  1194. int work_done, ret;
  1195. if (unlikely(!tx_work_todo(vif)))
  1196. return 0;
  1197. nr_gops = xenvif_tx_build_gops(vif, budget);
  1198. if (nr_gops == 0)
  1199. return 0;
  1200. ret = gnttab_map_refs(vif->tx_map_ops,
  1201. NULL,
  1202. vif->pages_to_map,
  1203. nr_gops);
  1204. BUG_ON(ret);
  1205. work_done = xenvif_tx_submit(vif);
  1206. return work_done;
  1207. }
  1208. static void xenvif_idx_release(struct xenvif *vif, u16 pending_idx,
  1209. u8 status)
  1210. {
  1211. struct pending_tx_info *pending_tx_info;
  1212. pending_ring_idx_t index;
  1213. unsigned long flags;
  1214. pending_tx_info = &vif->pending_tx_info[pending_idx];
  1215. spin_lock_irqsave(&vif->response_lock, flags);
  1216. make_tx_response(vif, &pending_tx_info->req, status);
  1217. index = pending_index(vif->pending_prod);
  1218. vif->pending_ring[index] = pending_idx;
  1219. /* TX shouldn't use the index before we give it back here */
  1220. mb();
  1221. vif->pending_prod++;
  1222. spin_unlock_irqrestore(&vif->response_lock, flags);
  1223. }
  1224. static void make_tx_response(struct xenvif *vif,
  1225. struct xen_netif_tx_request *txp,
  1226. s8 st)
  1227. {
  1228. RING_IDX i = vif->tx.rsp_prod_pvt;
  1229. struct xen_netif_tx_response *resp;
  1230. int notify;
  1231. resp = RING_GET_RESPONSE(&vif->tx, i);
  1232. resp->id = txp->id;
  1233. resp->status = st;
  1234. if (txp->flags & XEN_NETTXF_extra_info)
  1235. RING_GET_RESPONSE(&vif->tx, ++i)->status = XEN_NETIF_RSP_NULL;
  1236. vif->tx.rsp_prod_pvt = ++i;
  1237. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->tx, notify);
  1238. if (notify)
  1239. notify_remote_via_irq(vif->tx_irq);
  1240. }
  1241. static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
  1242. u16 id,
  1243. s8 st,
  1244. u16 offset,
  1245. u16 size,
  1246. u16 flags)
  1247. {
  1248. RING_IDX i = vif->rx.rsp_prod_pvt;
  1249. struct xen_netif_rx_response *resp;
  1250. resp = RING_GET_RESPONSE(&vif->rx, i);
  1251. resp->offset = offset;
  1252. resp->flags = flags;
  1253. resp->id = id;
  1254. resp->status = (s16)size;
  1255. if (st < 0)
  1256. resp->status = (s16)st;
  1257. vif->rx.rsp_prod_pvt = ++i;
  1258. return resp;
  1259. }
  1260. void xenvif_idx_unmap(struct xenvif *vif, u16 pending_idx)
  1261. {
  1262. int ret;
  1263. struct gnttab_unmap_grant_ref tx_unmap_op;
  1264. gnttab_set_unmap_op(&tx_unmap_op,
  1265. idx_to_kaddr(vif, pending_idx),
  1266. GNTMAP_host_map,
  1267. vif->grant_tx_handle[pending_idx]);
  1268. /* Btw. already unmapped? */
  1269. xenvif_grant_handle_reset(vif, pending_idx);
  1270. ret = gnttab_unmap_refs(&tx_unmap_op, NULL,
  1271. &vif->mmap_pages[pending_idx], 1);
  1272. BUG_ON(ret);
  1273. xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_OKAY);
  1274. }
  1275. static inline int rx_work_todo(struct xenvif *vif)
  1276. {
  1277. return !skb_queue_empty(&vif->rx_queue) &&
  1278. xenvif_rx_ring_slots_available(vif, vif->rx_last_skb_slots);
  1279. }
  1280. static inline int tx_work_todo(struct xenvif *vif)
  1281. {
  1282. if (likely(RING_HAS_UNCONSUMED_REQUESTS(&vif->tx)) &&
  1283. xenvif_tx_pending_slots_available(vif))
  1284. return 1;
  1285. return 0;
  1286. }
  1287. static inline bool tx_dealloc_work_todo(struct xenvif *vif)
  1288. {
  1289. return vif->dealloc_cons != vif->dealloc_prod;
  1290. }
  1291. void xenvif_unmap_frontend_rings(struct xenvif *vif)
  1292. {
  1293. if (vif->tx.sring)
  1294. xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
  1295. vif->tx.sring);
  1296. if (vif->rx.sring)
  1297. xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
  1298. vif->rx.sring);
  1299. }
  1300. int xenvif_map_frontend_rings(struct xenvif *vif,
  1301. grant_ref_t tx_ring_ref,
  1302. grant_ref_t rx_ring_ref)
  1303. {
  1304. void *addr;
  1305. struct xen_netif_tx_sring *txs;
  1306. struct xen_netif_rx_sring *rxs;
  1307. int err = -ENOMEM;
  1308. err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
  1309. tx_ring_ref, &addr);
  1310. if (err)
  1311. goto err;
  1312. txs = (struct xen_netif_tx_sring *)addr;
  1313. BACK_RING_INIT(&vif->tx, txs, PAGE_SIZE);
  1314. err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
  1315. rx_ring_ref, &addr);
  1316. if (err)
  1317. goto err;
  1318. rxs = (struct xen_netif_rx_sring *)addr;
  1319. BACK_RING_INIT(&vif->rx, rxs, PAGE_SIZE);
  1320. return 0;
  1321. err:
  1322. xenvif_unmap_frontend_rings(vif);
  1323. return err;
  1324. }
  1325. void xenvif_stop_queue(struct xenvif *vif)
  1326. {
  1327. if (!vif->can_queue)
  1328. return;
  1329. netif_stop_queue(vif->dev);
  1330. }
  1331. static void xenvif_start_queue(struct xenvif *vif)
  1332. {
  1333. if (xenvif_schedulable(vif))
  1334. netif_wake_queue(vif->dev);
  1335. }
  1336. int xenvif_kthread_guest_rx(void *data)
  1337. {
  1338. struct xenvif *vif = data;
  1339. struct sk_buff *skb;
  1340. while (!kthread_should_stop()) {
  1341. wait_event_interruptible(vif->wq,
  1342. rx_work_todo(vif) ||
  1343. kthread_should_stop());
  1344. if (kthread_should_stop())
  1345. break;
  1346. if (!skb_queue_empty(&vif->rx_queue))
  1347. xenvif_rx_action(vif);
  1348. if (skb_queue_empty(&vif->rx_queue) &&
  1349. netif_queue_stopped(vif->dev))
  1350. xenvif_start_queue(vif);
  1351. cond_resched();
  1352. }
  1353. /* Bin any remaining skbs */
  1354. while ((skb = skb_dequeue(&vif->rx_queue)) != NULL)
  1355. dev_kfree_skb(skb);
  1356. return 0;
  1357. }
  1358. int xenvif_dealloc_kthread(void *data)
  1359. {
  1360. struct xenvif *vif = data;
  1361. while (!kthread_should_stop()) {
  1362. wait_event_interruptible(vif->dealloc_wq,
  1363. tx_dealloc_work_todo(vif) ||
  1364. kthread_should_stop());
  1365. if (kthread_should_stop())
  1366. break;
  1367. xenvif_tx_dealloc_action(vif);
  1368. cond_resched();
  1369. }
  1370. /* Unmap anything remaining*/
  1371. if (tx_dealloc_work_todo(vif))
  1372. xenvif_tx_dealloc_action(vif);
  1373. return 0;
  1374. }
  1375. static int __init netback_init(void)
  1376. {
  1377. int rc = 0;
  1378. if (!xen_domain())
  1379. return -ENODEV;
  1380. if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
  1381. pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
  1382. fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
  1383. fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
  1384. }
  1385. rc = xenvif_xenbus_init();
  1386. if (rc)
  1387. goto failed_init;
  1388. return 0;
  1389. failed_init:
  1390. return rc;
  1391. }
  1392. module_init(netback_init);
  1393. static void __exit netback_fini(void)
  1394. {
  1395. xenvif_xenbus_fini();
  1396. }
  1397. module_exit(netback_fini);
  1398. MODULE_LICENSE("Dual BSD/GPL");
  1399. MODULE_ALIAS("xen-backend:vif");