netback.c 44 KB

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