netback.c 40 KB

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