netback.c 43 KB

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