netback.c 43 KB

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