netback.c 43 KB

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