netback.c 42 KB

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