netback.c 44 KB

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