netback.c 42 KB

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