netback.c 43 KB

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