netback.c 42 KB

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