netback.c 43 KB

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