netback.c 41 KB

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