netback.c 48 KB

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