netback.c 42 KB

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