netback.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706
  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 are 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. gop->source.u.ref = txp->gref;
  763. gop->source.domid = vif->domid;
  764. gop->source.offset = txp->offset;
  765. gop->dest.u.gmfn = virt_to_mfn(page_address(page));
  766. gop->dest.domid = DOMID_SELF;
  767. gop->dest.offset = txp->offset;
  768. gop->len = txp->size;
  769. gop->flags = GNTCOPY_source_gref;
  770. gop++;
  771. memcpy(&pending_tx_info[pending_idx].req, txp, sizeof(*txp));
  772. xenvif_get(vif);
  773. pending_tx_info[pending_idx].vif = vif;
  774. frag_set_pending_idx(&frags[i], pending_idx);
  775. }
  776. return gop;
  777. }
  778. static int xen_netbk_tx_check_gop(struct xen_netbk *netbk,
  779. struct sk_buff *skb,
  780. struct gnttab_copy **gopp)
  781. {
  782. struct gnttab_copy *gop = *gopp;
  783. u16 pending_idx = *((u16 *)skb->data);
  784. struct pending_tx_info *pending_tx_info = netbk->pending_tx_info;
  785. struct xenvif *vif = pending_tx_info[pending_idx].vif;
  786. struct xen_netif_tx_request *txp;
  787. struct skb_shared_info *shinfo = skb_shinfo(skb);
  788. int nr_frags = shinfo->nr_frags;
  789. int i, err, start;
  790. /* Check status of header. */
  791. err = gop->status;
  792. if (unlikely(err)) {
  793. pending_ring_idx_t index;
  794. index = pending_index(netbk->pending_prod++);
  795. txp = &pending_tx_info[pending_idx].req;
  796. make_tx_response(vif, txp, XEN_NETIF_RSP_ERROR);
  797. netbk->pending_ring[index] = pending_idx;
  798. xenvif_put(vif);
  799. }
  800. /* Skip first skb fragment if it is on same page as header fragment. */
  801. start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
  802. for (i = start; i < nr_frags; i++) {
  803. int j, newerr;
  804. pending_ring_idx_t index;
  805. pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
  806. /* Check error status: if okay then remember grant handle. */
  807. newerr = (++gop)->status;
  808. if (likely(!newerr)) {
  809. /* Had a previous error? Invalidate this fragment. */
  810. if (unlikely(err))
  811. xen_netbk_idx_release(netbk, pending_idx);
  812. continue;
  813. }
  814. /* Error on this fragment: respond to client with an error. */
  815. txp = &netbk->pending_tx_info[pending_idx].req;
  816. make_tx_response(vif, txp, XEN_NETIF_RSP_ERROR);
  817. index = pending_index(netbk->pending_prod++);
  818. netbk->pending_ring[index] = pending_idx;
  819. xenvif_put(vif);
  820. /* Not the first error? Preceding frags already invalidated. */
  821. if (err)
  822. continue;
  823. /* First error: invalidate header and preceding fragments. */
  824. pending_idx = *((u16 *)skb->data);
  825. xen_netbk_idx_release(netbk, pending_idx);
  826. for (j = start; j < i; j++) {
  827. pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
  828. xen_netbk_idx_release(netbk, pending_idx);
  829. }
  830. /* Remember the error: invalidate all subsequent fragments. */
  831. err = newerr;
  832. }
  833. *gopp = gop + 1;
  834. return err;
  835. }
  836. static void xen_netbk_fill_frags(struct xen_netbk *netbk, struct sk_buff *skb)
  837. {
  838. struct skb_shared_info *shinfo = skb_shinfo(skb);
  839. int nr_frags = shinfo->nr_frags;
  840. int i;
  841. for (i = 0; i < nr_frags; i++) {
  842. skb_frag_t *frag = shinfo->frags + i;
  843. struct xen_netif_tx_request *txp;
  844. struct page *page;
  845. u16 pending_idx;
  846. pending_idx = frag_get_pending_idx(frag);
  847. txp = &netbk->pending_tx_info[pending_idx].req;
  848. page = virt_to_page(idx_to_kaddr(netbk, pending_idx));
  849. __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
  850. skb->len += txp->size;
  851. skb->data_len += txp->size;
  852. skb->truesize += txp->size;
  853. /* Take an extra reference to offset xen_netbk_idx_release */
  854. get_page(netbk->mmap_pages[pending_idx]);
  855. xen_netbk_idx_release(netbk, pending_idx);
  856. }
  857. }
  858. static int xen_netbk_get_extras(struct xenvif *vif,
  859. struct xen_netif_extra_info *extras,
  860. int work_to_do)
  861. {
  862. struct xen_netif_extra_info extra;
  863. RING_IDX cons = vif->tx.req_cons;
  864. do {
  865. if (unlikely(work_to_do-- <= 0)) {
  866. netdev_dbg(vif->dev, "Missing extra info\n");
  867. return -EBADR;
  868. }
  869. memcpy(&extra, RING_GET_REQUEST(&vif->tx, cons),
  870. sizeof(extra));
  871. if (unlikely(!extra.type ||
  872. extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
  873. vif->tx.req_cons = ++cons;
  874. netdev_dbg(vif->dev,
  875. "Invalid extra type: %d\n", extra.type);
  876. return -EINVAL;
  877. }
  878. memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
  879. vif->tx.req_cons = ++cons;
  880. } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
  881. return work_to_do;
  882. }
  883. static int netbk_set_skb_gso(struct xenvif *vif,
  884. struct sk_buff *skb,
  885. struct xen_netif_extra_info *gso)
  886. {
  887. if (!gso->u.gso.size) {
  888. netdev_dbg(vif->dev, "GSO size must not be zero.\n");
  889. return -EINVAL;
  890. }
  891. /* Currently only TCPv4 S.O. is supported. */
  892. if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4) {
  893. netdev_dbg(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
  894. return -EINVAL;
  895. }
  896. skb_shinfo(skb)->gso_size = gso->u.gso.size;
  897. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  898. /* Header must be checked, and gso_segs computed. */
  899. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  900. skb_shinfo(skb)->gso_segs = 0;
  901. return 0;
  902. }
  903. static int checksum_setup(struct xenvif *vif, struct sk_buff *skb)
  904. {
  905. struct iphdr *iph;
  906. unsigned char *th;
  907. int err = -EPROTO;
  908. int recalculate_partial_csum = 0;
  909. /*
  910. * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
  911. * peers can fail to set NETRXF_csum_blank when sending a GSO
  912. * frame. In this case force the SKB to CHECKSUM_PARTIAL and
  913. * recalculate the partial checksum.
  914. */
  915. if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
  916. vif->rx_gso_checksum_fixup++;
  917. skb->ip_summed = CHECKSUM_PARTIAL;
  918. recalculate_partial_csum = 1;
  919. }
  920. /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
  921. if (skb->ip_summed != CHECKSUM_PARTIAL)
  922. return 0;
  923. if (skb->protocol != htons(ETH_P_IP))
  924. goto out;
  925. iph = (void *)skb->data;
  926. th = skb->data + 4 * iph->ihl;
  927. if (th >= skb_tail_pointer(skb))
  928. goto out;
  929. skb->csum_start = th - skb->head;
  930. switch (iph->protocol) {
  931. case IPPROTO_TCP:
  932. skb->csum_offset = offsetof(struct tcphdr, check);
  933. if (recalculate_partial_csum) {
  934. struct tcphdr *tcph = (struct tcphdr *)th;
  935. tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
  936. skb->len - iph->ihl*4,
  937. IPPROTO_TCP, 0);
  938. }
  939. break;
  940. case IPPROTO_UDP:
  941. skb->csum_offset = offsetof(struct udphdr, check);
  942. if (recalculate_partial_csum) {
  943. struct udphdr *udph = (struct udphdr *)th;
  944. udph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
  945. skb->len - iph->ihl*4,
  946. IPPROTO_UDP, 0);
  947. }
  948. break;
  949. default:
  950. if (net_ratelimit())
  951. netdev_err(vif->dev,
  952. "Attempting to checksum a non-TCP/UDP packet, dropping a protocol %d packet\n",
  953. iph->protocol);
  954. goto out;
  955. }
  956. if ((th + skb->csum_offset + 2) > skb_tail_pointer(skb))
  957. goto out;
  958. err = 0;
  959. out:
  960. return err;
  961. }
  962. static bool tx_credit_exceeded(struct xenvif *vif, unsigned size)
  963. {
  964. unsigned long now = jiffies;
  965. unsigned long next_credit =
  966. vif->credit_timeout.expires +
  967. msecs_to_jiffies(vif->credit_usec / 1000);
  968. /* Timer could already be pending in rare cases. */
  969. if (timer_pending(&vif->credit_timeout))
  970. return true;
  971. /* Passed the point where we can replenish credit? */
  972. if (time_after_eq(now, next_credit)) {
  973. vif->credit_timeout.expires = now;
  974. tx_add_credit(vif);
  975. }
  976. /* Still too big to send right now? Set a callback. */
  977. if (size > vif->remaining_credit) {
  978. vif->credit_timeout.data =
  979. (unsigned long)vif;
  980. vif->credit_timeout.function =
  981. tx_credit_callback;
  982. mod_timer(&vif->credit_timeout,
  983. next_credit);
  984. return true;
  985. }
  986. return false;
  987. }
  988. static unsigned xen_netbk_tx_build_gops(struct xen_netbk *netbk)
  989. {
  990. struct gnttab_copy *gop = netbk->tx_copy_ops, *request_gop;
  991. struct sk_buff *skb;
  992. int ret;
  993. while (((nr_pending_reqs(netbk) + MAX_SKB_FRAGS) < MAX_PENDING_REQS) &&
  994. !list_empty(&netbk->net_schedule_list)) {
  995. struct xenvif *vif;
  996. struct xen_netif_tx_request txreq;
  997. struct xen_netif_tx_request txfrags[MAX_SKB_FRAGS];
  998. struct page *page;
  999. struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
  1000. u16 pending_idx;
  1001. RING_IDX idx;
  1002. int work_to_do;
  1003. unsigned int data_len;
  1004. pending_ring_idx_t index;
  1005. /* Get a netif from the list with work to do. */
  1006. vif = poll_net_schedule_list(netbk);
  1007. if (!vif)
  1008. continue;
  1009. RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, work_to_do);
  1010. if (!work_to_do) {
  1011. xenvif_put(vif);
  1012. continue;
  1013. }
  1014. idx = vif->tx.req_cons;
  1015. rmb(); /* Ensure that we see the request before we copy it. */
  1016. memcpy(&txreq, RING_GET_REQUEST(&vif->tx, idx), sizeof(txreq));
  1017. /* Credit-based scheduling. */
  1018. if (txreq.size > vif->remaining_credit &&
  1019. tx_credit_exceeded(vif, txreq.size)) {
  1020. xenvif_put(vif);
  1021. continue;
  1022. }
  1023. vif->remaining_credit -= txreq.size;
  1024. work_to_do--;
  1025. vif->tx.req_cons = ++idx;
  1026. memset(extras, 0, sizeof(extras));
  1027. if (txreq.flags & XEN_NETTXF_extra_info) {
  1028. work_to_do = xen_netbk_get_extras(vif, extras,
  1029. work_to_do);
  1030. idx = vif->tx.req_cons;
  1031. if (unlikely(work_to_do < 0)) {
  1032. netbk_tx_err(vif, &txreq, idx);
  1033. continue;
  1034. }
  1035. }
  1036. ret = netbk_count_requests(vif, &txreq, txfrags, work_to_do);
  1037. if (unlikely(ret < 0)) {
  1038. netbk_tx_err(vif, &txreq, idx - ret);
  1039. continue;
  1040. }
  1041. idx += ret;
  1042. if (unlikely(txreq.size < ETH_HLEN)) {
  1043. netdev_dbg(vif->dev,
  1044. "Bad packet size: %d\n", txreq.size);
  1045. netbk_tx_err(vif, &txreq, idx);
  1046. continue;
  1047. }
  1048. /* No crossing a page as the payload mustn't fragment. */
  1049. if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
  1050. netdev_dbg(vif->dev,
  1051. "txreq.offset: %x, size: %u, end: %lu\n",
  1052. txreq.offset, txreq.size,
  1053. (txreq.offset&~PAGE_MASK) + txreq.size);
  1054. netbk_tx_err(vif, &txreq, idx);
  1055. continue;
  1056. }
  1057. index = pending_index(netbk->pending_cons);
  1058. pending_idx = netbk->pending_ring[index];
  1059. data_len = (txreq.size > PKT_PROT_LEN &&
  1060. ret < MAX_SKB_FRAGS) ?
  1061. PKT_PROT_LEN : txreq.size;
  1062. skb = alloc_skb(data_len + NET_SKB_PAD + NET_IP_ALIGN,
  1063. GFP_ATOMIC | __GFP_NOWARN);
  1064. if (unlikely(skb == NULL)) {
  1065. netdev_dbg(vif->dev,
  1066. "Can't allocate a skb in start_xmit.\n");
  1067. netbk_tx_err(vif, &txreq, idx);
  1068. break;
  1069. }
  1070. /* Packets passed to netif_rx() must have some headroom. */
  1071. skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
  1072. if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
  1073. struct xen_netif_extra_info *gso;
  1074. gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
  1075. if (netbk_set_skb_gso(vif, skb, gso)) {
  1076. kfree_skb(skb);
  1077. netbk_tx_err(vif, &txreq, idx);
  1078. continue;
  1079. }
  1080. }
  1081. /* XXX could copy straight to head */
  1082. page = xen_netbk_alloc_page(netbk, skb, pending_idx);
  1083. if (!page) {
  1084. kfree_skb(skb);
  1085. netbk_tx_err(vif, &txreq, idx);
  1086. continue;
  1087. }
  1088. gop->source.u.ref = txreq.gref;
  1089. gop->source.domid = vif->domid;
  1090. gop->source.offset = txreq.offset;
  1091. gop->dest.u.gmfn = virt_to_mfn(page_address(page));
  1092. gop->dest.domid = DOMID_SELF;
  1093. gop->dest.offset = txreq.offset;
  1094. gop->len = txreq.size;
  1095. gop->flags = GNTCOPY_source_gref;
  1096. gop++;
  1097. memcpy(&netbk->pending_tx_info[pending_idx].req,
  1098. &txreq, sizeof(txreq));
  1099. netbk->pending_tx_info[pending_idx].vif = vif;
  1100. *((u16 *)skb->data) = pending_idx;
  1101. __skb_put(skb, data_len);
  1102. skb_shinfo(skb)->nr_frags = ret;
  1103. if (data_len < txreq.size) {
  1104. skb_shinfo(skb)->nr_frags++;
  1105. frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
  1106. pending_idx);
  1107. } else {
  1108. frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
  1109. INVALID_PENDING_IDX);
  1110. }
  1111. __skb_queue_tail(&netbk->tx_queue, skb);
  1112. netbk->pending_cons++;
  1113. request_gop = xen_netbk_get_requests(netbk, vif,
  1114. skb, txfrags, gop);
  1115. if (request_gop == NULL) {
  1116. kfree_skb(skb);
  1117. netbk_tx_err(vif, &txreq, idx);
  1118. continue;
  1119. }
  1120. gop = request_gop;
  1121. vif->tx.req_cons = idx;
  1122. xen_netbk_check_rx_xenvif(vif);
  1123. if ((gop-netbk->tx_copy_ops) >= ARRAY_SIZE(netbk->tx_copy_ops))
  1124. break;
  1125. }
  1126. return gop - netbk->tx_copy_ops;
  1127. }
  1128. static void xen_netbk_tx_submit(struct xen_netbk *netbk)
  1129. {
  1130. struct gnttab_copy *gop = netbk->tx_copy_ops;
  1131. struct sk_buff *skb;
  1132. while ((skb = __skb_dequeue(&netbk->tx_queue)) != NULL) {
  1133. struct xen_netif_tx_request *txp;
  1134. struct xenvif *vif;
  1135. u16 pending_idx;
  1136. unsigned data_len;
  1137. pending_idx = *((u16 *)skb->data);
  1138. vif = netbk->pending_tx_info[pending_idx].vif;
  1139. txp = &netbk->pending_tx_info[pending_idx].req;
  1140. /* Check the remap error code. */
  1141. if (unlikely(xen_netbk_tx_check_gop(netbk, skb, &gop))) {
  1142. netdev_dbg(vif->dev, "netback grant failed.\n");
  1143. skb_shinfo(skb)->nr_frags = 0;
  1144. kfree_skb(skb);
  1145. continue;
  1146. }
  1147. data_len = skb->len;
  1148. memcpy(skb->data,
  1149. (void *)(idx_to_kaddr(netbk, pending_idx)|txp->offset),
  1150. data_len);
  1151. if (data_len < txp->size) {
  1152. /* Append the packet payload as a fragment. */
  1153. txp->offset += data_len;
  1154. txp->size -= data_len;
  1155. } else {
  1156. /* Schedule a response immediately. */
  1157. xen_netbk_idx_release(netbk, pending_idx);
  1158. }
  1159. if (txp->flags & XEN_NETTXF_csum_blank)
  1160. skb->ip_summed = CHECKSUM_PARTIAL;
  1161. else if (txp->flags & XEN_NETTXF_data_validated)
  1162. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1163. xen_netbk_fill_frags(netbk, skb);
  1164. /*
  1165. * If the initial fragment was < PKT_PROT_LEN then
  1166. * pull through some bytes from the other fragments to
  1167. * increase the linear region to PKT_PROT_LEN bytes.
  1168. */
  1169. if (skb_headlen(skb) < PKT_PROT_LEN && skb_is_nonlinear(skb)) {
  1170. int target = min_t(int, skb->len, PKT_PROT_LEN);
  1171. __pskb_pull_tail(skb, target - skb_headlen(skb));
  1172. }
  1173. skb->dev = vif->dev;
  1174. skb->protocol = eth_type_trans(skb, skb->dev);
  1175. if (checksum_setup(vif, skb)) {
  1176. netdev_dbg(vif->dev,
  1177. "Can't setup checksum in net_tx_action\n");
  1178. kfree_skb(skb);
  1179. continue;
  1180. }
  1181. vif->dev->stats.rx_bytes += skb->len;
  1182. vif->dev->stats.rx_packets++;
  1183. xenvif_receive_skb(vif, skb);
  1184. }
  1185. }
  1186. /* Called after netfront has transmitted */
  1187. static void xen_netbk_tx_action(struct xen_netbk *netbk)
  1188. {
  1189. unsigned nr_gops;
  1190. int ret;
  1191. nr_gops = xen_netbk_tx_build_gops(netbk);
  1192. if (nr_gops == 0)
  1193. return;
  1194. ret = HYPERVISOR_grant_table_op(GNTTABOP_copy,
  1195. netbk->tx_copy_ops, nr_gops);
  1196. BUG_ON(ret);
  1197. xen_netbk_tx_submit(netbk);
  1198. }
  1199. static void xen_netbk_idx_release(struct xen_netbk *netbk, u16 pending_idx)
  1200. {
  1201. struct xenvif *vif;
  1202. struct pending_tx_info *pending_tx_info;
  1203. pending_ring_idx_t index;
  1204. /* Already complete? */
  1205. if (netbk->mmap_pages[pending_idx] == NULL)
  1206. return;
  1207. pending_tx_info = &netbk->pending_tx_info[pending_idx];
  1208. vif = pending_tx_info->vif;
  1209. make_tx_response(vif, &pending_tx_info->req, XEN_NETIF_RSP_OKAY);
  1210. index = pending_index(netbk->pending_prod++);
  1211. netbk->pending_ring[index] = pending_idx;
  1212. xenvif_put(vif);
  1213. netbk->mmap_pages[pending_idx]->mapping = 0;
  1214. put_page(netbk->mmap_pages[pending_idx]);
  1215. netbk->mmap_pages[pending_idx] = NULL;
  1216. }
  1217. static void make_tx_response(struct xenvif *vif,
  1218. struct xen_netif_tx_request *txp,
  1219. s8 st)
  1220. {
  1221. RING_IDX i = vif->tx.rsp_prod_pvt;
  1222. struct xen_netif_tx_response *resp;
  1223. int notify;
  1224. resp = RING_GET_RESPONSE(&vif->tx, i);
  1225. resp->id = txp->id;
  1226. resp->status = st;
  1227. if (txp->flags & XEN_NETTXF_extra_info)
  1228. RING_GET_RESPONSE(&vif->tx, ++i)->status = XEN_NETIF_RSP_NULL;
  1229. vif->tx.rsp_prod_pvt = ++i;
  1230. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->tx, notify);
  1231. if (notify)
  1232. notify_remote_via_irq(vif->irq);
  1233. }
  1234. static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
  1235. u16 id,
  1236. s8 st,
  1237. u16 offset,
  1238. u16 size,
  1239. u16 flags)
  1240. {
  1241. RING_IDX i = vif->rx.rsp_prod_pvt;
  1242. struct xen_netif_rx_response *resp;
  1243. resp = RING_GET_RESPONSE(&vif->rx, i);
  1244. resp->offset = offset;
  1245. resp->flags = flags;
  1246. resp->id = id;
  1247. resp->status = (s16)size;
  1248. if (st < 0)
  1249. resp->status = (s16)st;
  1250. vif->rx.rsp_prod_pvt = ++i;
  1251. return resp;
  1252. }
  1253. static inline int rx_work_todo(struct xen_netbk *netbk)
  1254. {
  1255. return !skb_queue_empty(&netbk->rx_queue);
  1256. }
  1257. static inline int tx_work_todo(struct xen_netbk *netbk)
  1258. {
  1259. if (((nr_pending_reqs(netbk) + MAX_SKB_FRAGS) < MAX_PENDING_REQS) &&
  1260. !list_empty(&netbk->net_schedule_list))
  1261. return 1;
  1262. return 0;
  1263. }
  1264. static int xen_netbk_kthread(void *data)
  1265. {
  1266. struct xen_netbk *netbk = data;
  1267. while (!kthread_should_stop()) {
  1268. wait_event_interruptible(netbk->wq,
  1269. rx_work_todo(netbk) ||
  1270. tx_work_todo(netbk) ||
  1271. kthread_should_stop());
  1272. cond_resched();
  1273. if (kthread_should_stop())
  1274. break;
  1275. if (rx_work_todo(netbk))
  1276. xen_netbk_rx_action(netbk);
  1277. if (tx_work_todo(netbk))
  1278. xen_netbk_tx_action(netbk);
  1279. }
  1280. return 0;
  1281. }
  1282. void xen_netbk_unmap_frontend_rings(struct xenvif *vif)
  1283. {
  1284. if (vif->tx.sring)
  1285. xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
  1286. vif->tx.sring);
  1287. if (vif->rx.sring)
  1288. xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
  1289. vif->rx.sring);
  1290. }
  1291. int xen_netbk_map_frontend_rings(struct xenvif *vif,
  1292. grant_ref_t tx_ring_ref,
  1293. grant_ref_t rx_ring_ref)
  1294. {
  1295. void *addr;
  1296. struct xen_netif_tx_sring *txs;
  1297. struct xen_netif_rx_sring *rxs;
  1298. int err = -ENOMEM;
  1299. err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
  1300. tx_ring_ref, &addr);
  1301. if (err)
  1302. goto err;
  1303. txs = (struct xen_netif_tx_sring *)addr;
  1304. BACK_RING_INIT(&vif->tx, txs, PAGE_SIZE);
  1305. err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
  1306. rx_ring_ref, &addr);
  1307. if (err)
  1308. goto err;
  1309. rxs = (struct xen_netif_rx_sring *)addr;
  1310. BACK_RING_INIT(&vif->rx, rxs, PAGE_SIZE);
  1311. vif->rx_req_cons_peek = 0;
  1312. return 0;
  1313. err:
  1314. xen_netbk_unmap_frontend_rings(vif);
  1315. return err;
  1316. }
  1317. static int __init netback_init(void)
  1318. {
  1319. int i;
  1320. int rc = 0;
  1321. int group;
  1322. if (!xen_domain())
  1323. return -ENODEV;
  1324. xen_netbk_group_nr = num_online_cpus();
  1325. xen_netbk = vzalloc(sizeof(struct xen_netbk) * xen_netbk_group_nr);
  1326. if (!xen_netbk)
  1327. return -ENOMEM;
  1328. for (group = 0; group < xen_netbk_group_nr; group++) {
  1329. struct xen_netbk *netbk = &xen_netbk[group];
  1330. skb_queue_head_init(&netbk->rx_queue);
  1331. skb_queue_head_init(&netbk->tx_queue);
  1332. init_timer(&netbk->net_timer);
  1333. netbk->net_timer.data = (unsigned long)netbk;
  1334. netbk->net_timer.function = xen_netbk_alarm;
  1335. netbk->pending_cons = 0;
  1336. netbk->pending_prod = MAX_PENDING_REQS;
  1337. for (i = 0; i < MAX_PENDING_REQS; i++)
  1338. netbk->pending_ring[i] = i;
  1339. init_waitqueue_head(&netbk->wq);
  1340. netbk->task = kthread_create(xen_netbk_kthread,
  1341. (void *)netbk,
  1342. "netback/%u", group);
  1343. if (IS_ERR(netbk->task)) {
  1344. printk(KERN_ALERT "kthread_create() fails at netback\n");
  1345. del_timer(&netbk->net_timer);
  1346. rc = PTR_ERR(netbk->task);
  1347. goto failed_init;
  1348. }
  1349. kthread_bind(netbk->task, group);
  1350. INIT_LIST_HEAD(&netbk->net_schedule_list);
  1351. spin_lock_init(&netbk->net_schedule_list_lock);
  1352. atomic_set(&netbk->netfront_count, 0);
  1353. wake_up_process(netbk->task);
  1354. }
  1355. rc = xenvif_xenbus_init();
  1356. if (rc)
  1357. goto failed_init;
  1358. return 0;
  1359. failed_init:
  1360. while (--group >= 0) {
  1361. struct xen_netbk *netbk = &xen_netbk[group];
  1362. for (i = 0; i < MAX_PENDING_REQS; i++) {
  1363. if (netbk->mmap_pages[i])
  1364. __free_page(netbk->mmap_pages[i]);
  1365. }
  1366. del_timer(&netbk->net_timer);
  1367. kthread_stop(netbk->task);
  1368. }
  1369. vfree(xen_netbk);
  1370. return rc;
  1371. }
  1372. module_init(netback_init);
  1373. MODULE_LICENSE("Dual BSD/GPL");
  1374. MODULE_ALIAS("xen-backend:vif");